Update patches/0002-bore-cachy.patch

This commit is contained in:
ferreo 2024-11-11 20:03:23 +01:00
parent 90539083bf
commit 110e3014bc

View File

@ -1,6 +1,6 @@
From 81c203e0e6bc5c0c9e53e2c81122c6387afe309e Mon Sep 17 00:00:00 2001
From 22d040c06d6becbc23319ded3bb6471596f19ffd Mon Sep 17 00:00:00 2001
From: Eric Naim <dnaim@cachyos.org>
Date: Sun, 3 Nov 2024 10:16:41 +0800
Date: Thu, 7 Nov 2024 22:52:43 +0800
Subject: [PATCH] bore-cachy
Signed-off-by: Eric Naim <dnaim@cachyos.org>
@ -8,15 +8,15 @@ Signed-off-by: Eric Naim <dnaim@cachyos.org>
include/linux/sched.h | 17 ++
include/linux/sched/bore.h | 37 ++++
init/Kconfig | 17 ++
kernel/Kconfig.hz | 43 +++++
kernel/Kconfig.hz | 17 ++
kernel/fork.c | 5 +
kernel/sched/Makefile | 1 +
kernel/sched/bore.c | 381 +++++++++++++++++++++++++++++++++++++
kernel/sched/core.c | 7 +
kernel/sched/debug.c | 67 ++++++-
kernel/sched/fair.c | 104 ++++++++--
kernel/sched/sched.h | 16 ++
11 files changed, 677 insertions(+), 18 deletions(-)
kernel/sched/debug.c | 61 +++++-
kernel/sched/fair.c | 88 +++++++--
kernel/sched/sched.h | 9 +
11 files changed, 622 insertions(+), 18 deletions(-)
create mode 100644 include/linux/sched/bore.h
create mode 100644 kernel/sched/bore.c
@ -127,10 +127,10 @@ index 504e8a7c4e2a..43daffa3ee03 100644
bool "Automatic process group scheduling"
select CGROUPS
diff --git a/kernel/Kconfig.hz b/kernel/Kconfig.hz
index 0f78364efd4f..b412ea29d0b3 100644
index 0f78364efd4f..83a6b919ab29 100644
--- a/kernel/Kconfig.hz
+++ b/kernel/Kconfig.hz
@@ -79,5 +79,48 @@ config HZ
@@ -79,5 +79,22 @@ config HZ
default 750 if HZ_750
default 1000 if HZ_1000
@ -150,40 +150,14 @@ index 0f78364efd4f..b412ea29d0b3 100644
+ Setting this value too high can cause the system to boot with
+ an unnecessarily large base slice, resulting in high scheduling
+ latency and poor system responsiveness.
+
+config MIGRATION_COST_BASE_NS
+ int "Default value for migration_cost_base_ns"
+ default 500000
+ help
+ The BORE Scheduler automatically calculates the optimal
+ migration_cost_ns using the following equation:
+
+ migration_cost_ns =
+ migration_cost_base_ns + ilog2(ncpus) * migration_cost_step_ns
+
+ This option sets the default migration_cost_base_ns
+ to be used in the automatic calculation.
+
+config MIGRATION_COST_STEP_NS
+ int "Default value for migration_cost_step_ns"
+ default 0
+ help
+ The BORE Scheduler automatically calculates the optimal
+ migration_cost_ns using the following equation:
+
+ migration_cost_ns =
+ migration_cost_base_ns + ilog2(ncpus) * migration_cost_step_ns
+
+ This option sets the default migration_cost_step_ns
+ to be used in the automatic calculation.
+
config SCHED_HRTICK
def_bool HIGH_RES_TIMERS
diff --git a/kernel/fork.c b/kernel/fork.c
index 671b85ce818b..370cfd9886d3 100644
index 8287afdd01d2..e4274ce3ac32 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -116,6 +116,8 @@
@@ -117,6 +117,8 @@
#include <asm/cacheflush.h>
#include <asm/tlbflush.h>
@ -192,7 +166,7 @@ index 671b85ce818b..370cfd9886d3 100644
#include <trace/events/sched.h>
#define CREATE_TRACE_POINTS
@@ -2350,6 +2352,9 @@ __latent_entropy struct task_struct *copy_process(
@@ -2352,6 +2354,9 @@ __latent_entropy struct task_struct *copy_process(
retval = sched_fork(clone_flags, p);
if (retval)
goto bad_fork_cleanup_policy;
@ -599,7 +573,7 @@ index 000000000000..cd7e8a8d6075
+#endif // CONFIG_SYSCTL
+#endif // CONFIG_SCHED_BORE
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index dbfb5717d6af..9aa89fa98c90 100644
index 719e0ed1e976..02f8b495738b 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -97,6 +97,8 @@
@ -616,7 +590,7 @@ index dbfb5717d6af..9aa89fa98c90 100644
#endif
+#ifdef CONFIG_SCHED_BORE
+ printk(KERN_INFO "BORE (Burst-Oriented Response Enhancer) CPU Scheduler modification 5.7.2 by Masahito Suzuki");
+ printk(KERN_INFO "BORE (Burst-Oriented Response Enhancer) CPU Scheduler modification 5.7.3 by Masahito Suzuki");
+ init_task_bore(&init_task);
+#endif // CONFIG_SCHED_BORE
+
@ -624,10 +598,10 @@ index dbfb5717d6af..9aa89fa98c90 100644
#ifdef CONFIG_FAIR_GROUP_SCHED
diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
index f4035c7a0fa1..ddd03cd517c7 100644
index f4035c7a0fa1..0edb3a216f5d 100644
--- a/kernel/sched/debug.c
+++ b/kernel/sched/debug.c
@@ -167,7 +167,55 @@ static const struct file_operations sched_feat_fops = {
@@ -167,7 +167,53 @@ static const struct file_operations sched_feat_fops = {
};
#ifdef CONFIG_SMP
@ -675,15 +649,13 @@ index f4035c7a0fa1..ddd03cd517c7 100644
+};
+
+DEFINE_SYSCTL_SCHED_FUNC(min_base_slice, min_base_slice)
+DEFINE_SYSCTL_SCHED_FUNC(migration_cost_base, migration_cost)
+DEFINE_SYSCTL_SCHED_FUNC(migration_cost_step, migration_cost)
+#undef DEFINE_SYSCTL_SCHED_FUNC
+#else // !CONFIG_SCHED_BORE
static ssize_t sched_scaling_write(struct file *filp, const char __user *ubuf,
size_t cnt, loff_t *ppos)
{
@@ -213,7 +261,7 @@ static const struct file_operations sched_scaling_fops = {
@@ -213,7 +259,7 @@ static const struct file_operations sched_scaling_fops = {
.llseek = seq_lseek,
.release = single_release,
};
@ -692,7 +664,7 @@ index f4035c7a0fa1..ddd03cd517c7 100644
#endif /* SMP */
#ifdef CONFIG_PREEMPT_DYNAMIC
@@ -504,14 +552,25 @@ static __init int sched_init_debug(void)
@@ -504,13 +550,20 @@ static __init int sched_init_debug(void)
debugfs_create_file("preempt", 0644, debugfs_sched, NULL, &sched_dynamic_fops);
#endif
@ -707,18 +679,13 @@ index f4035c7a0fa1..ddd03cd517c7 100644
debugfs_create_u32("latency_warn_once", 0644, debugfs_sched, &sysctl_resched_latency_warn_once);
#ifdef CONFIG_SMP
+#ifdef CONFIG_SCHED_BORE
+ debugfs_create_file("migration_cost_base_ns", 0644, debugfs_sched, NULL, &sched_migration_cost_base_fops);
+ debugfs_create_file("migration_cost_step_ns", 0644, debugfs_sched, NULL, &sched_migration_cost_step_fops);
+ debugfs_create_u32("migration_cost_ns", 0444, debugfs_sched, &sysctl_sched_migration_cost);
+#else // !CONFIG_SCHED_BORE
+#if !defined(CONFIG_SCHED_BORE)
debugfs_create_file("tunable_scaling", 0644, debugfs_sched, NULL, &sched_scaling_fops);
debugfs_create_u32("migration_cost_ns", 0644, debugfs_sched, &sysctl_sched_migration_cost);
+#endif // CONFIG_SCHED_BORE
debugfs_create_u32("migration_cost_ns", 0644, debugfs_sched, &sysctl_sched_migration_cost);
debugfs_create_u32("nr_migrate", 0644, debugfs_sched, &sysctl_sched_nr_migrate);
mutex_lock(&sched_domains_mutex);
@@ -755,6 +814,9 @@ print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
@@ -755,6 +808,9 @@ print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
SPLIT_NS(schedstat_val_or_zero(p->stats.sum_sleep_runtime)),
SPLIT_NS(schedstat_val_or_zero(p->stats.sum_block_runtime)));
@ -728,7 +695,7 @@ index f4035c7a0fa1..ddd03cd517c7 100644
#ifdef CONFIG_NUMA_BALANCING
SEQ_printf(m, " %d %d", task_node(p), task_numa_group_id(p));
#endif
@@ -1243,6 +1305,9 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,
@@ -1243,6 +1299,9 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,
P(se.load.weight);
#ifdef CONFIG_SMP
@ -739,7 +706,7 @@ index f4035c7a0fa1..ddd03cd517c7 100644
P(se.avg.runnable_sum);
P(se.avg.util_sum);
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index b55fa314df37..b8c4649c72e8 100644
index 54e7c4c3e2c5..94c54154a175 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -55,6 +55,8 @@
@ -751,7 +718,7 @@ index b55fa314df37..b8c4649c72e8 100644
/*
* The initial- and re-scaling of tunables is configurable
*
@@ -64,28 +66,38 @@
@@ -64,28 +66,32 @@
* SCHED_TUNABLESCALING_LOG - scaled logarithmically, *1+ilog(ncpus)
* SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
*
@ -778,7 +745,7 @@ index b55fa314df37..b8c4649c72e8 100644
-static unsigned int normalized_sysctl_sched_base_slice = 350000ULL;
-#else
+#ifdef CONFIG_SCHED_BORE
+const static unsigned int nsecs_per_tick = 1000000000ULL / HZ;
+static const unsigned int nsecs_per_tick = 1000000000ULL / HZ;
+unsigned int sysctl_sched_min_base_slice = CONFIG_MIN_BASE_SLICE_NS;
+__read_mostly uint sysctl_sched_base_slice = nsecs_per_tick;
+#else // !CONFIG_SCHED_BORE
@ -790,18 +757,12 @@ index b55fa314df37..b8c4649c72e8 100644
-#ifdef CONFIG_CACHY
-const_debug unsigned int sysctl_sched_migration_cost = 300000UL;
-#else
+#ifdef CONFIG_SCHED_BORE
+unsigned int sysctl_sched_migration_cost_base = CONFIG_MIGRATION_COST_BASE_NS;
+unsigned int sysctl_sched_migration_cost_step = CONFIG_MIGRATION_COST_STEP_NS;
+__read_mostly uint sysctl_sched_migration_cost = CONFIG_MIGRATION_COST_BASE_NS;
+#else // !CONFIG_SCHED_BORE
const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
-#endif
+#endif // CONFIG_SCHED_BORE
static int __init setup_sched_thermal_decay_shift(char *str)
{
@@ -130,12 +142,8 @@ int __weak arch_asym_cpu_priority(int cpu)
@@ -130,12 +136,8 @@ int __weak arch_asym_cpu_priority(int cpu)
*
* (default: 5 msec, units: microseconds)
*/
@ -814,48 +775,29 @@ index b55fa314df37..b8c4649c72e8 100644
#ifdef CONFIG_NUMA_BALANCING
/* Restrict the NUMA promotion throughput (MB/s) for each target node. */
@@ -201,6 +209,18 @@ static inline void update_load_set(struct load_weight *lw, unsigned long w)
@@ -201,6 +203,13 @@ static inline void update_load_set(struct load_weight *lw, unsigned long w)
*
* This idea comes from the SD scheduler of Con Kolivas:
*/
+#ifdef CONFIG_SCHED_BORE
+static void auto_calculate_base_slice(void) {
+static void update_sysctl(void) {
+ sysctl_sched_base_slice = nsecs_per_tick *
+ max(1UL, DIV_ROUND_UP(sysctl_sched_min_base_slice, nsecs_per_tick));
+}
+static void auto_calculate_migration_cost(void) {
+ sysctl_sched_migration_cost = sysctl_sched_migration_cost_base +
+ ilog2(num_online_cpus()) * sysctl_sched_migration_cost_step;
+}
+void sched_update_min_base_slice(void) { auto_calculate_base_slice(); }
+void sched_update_migration_cost(void) { auto_calculate_migration_cost(); }
+void sched_update_min_base_slice(void) { update_sysctl(); }
+#else // !CONFIG_SCHED_BORE
static unsigned int get_update_sysctl_factor(void)
{
unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8);
@@ -221,15 +241,21 @@ static unsigned int get_update_sysctl_factor(void)
return factor;
}
+#endif // CONFIG_SCHED_BORE
static void update_sysctl(void)
{
+#ifdef CONFIG_SCHED_BORE
+ auto_calculate_base_slice();
+ auto_calculate_migration_cost();
+#else // !CONFIG_SCHED_BORE
unsigned int factor = get_update_sysctl_factor();
#define SET_SYSCTL(name) \
(sysctl_##name = (factor) * normalized_sysctl_##name)
@@ -231,6 +240,7 @@ static void update_sysctl(void)
SET_SYSCTL(sched_base_slice);
#undef SET_SYSCTL
+#endif // CONFIG_SCHED_BORE
}
+#endif // CONFIG_SCHED_BORE
void __init sched_init_granularity(void)
@@ -708,6 +734,9 @@ static s64 entity_lag(u64 avruntime, struct sched_entity *se)
{
@@ -708,6 +718,9 @@ static s64 entity_lag(u64 avruntime, struct sched_entity *se)
vlag = avruntime - se->vruntime;
limit = calc_delta_fair(max_t(u64, 2*se->slice, TICK_NSEC), se);
@ -865,7 +807,7 @@ index b55fa314df37..b8c4649c72e8 100644
return clamp(vlag, -limit, limit);
}
@@ -939,6 +968,10 @@ static struct sched_entity *pick_eevdf(struct cfs_rq *cfs_rq)
@@ -939,6 +952,10 @@ static struct sched_entity *pick_eevdf(struct cfs_rq *cfs_rq)
* until it gets a new slice. See the HACK in set_next_entity().
*/
if (sched_feat(RUN_TO_PARITY) && curr && curr->vlag == curr->deadline)
@ -876,7 +818,7 @@ index b55fa314df37..b8c4649c72e8 100644
return curr;
/* Pick the leftmost entity if it's eligible */
@@ -997,6 +1030,7 @@ struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
@@ -997,6 +1014,7 @@ struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
* Scheduling class statistics methods:
*/
#ifdef CONFIG_SMP
@ -884,7 +826,7 @@ index b55fa314df37..b8c4649c72e8 100644
int sched_update_scaling(void)
{
unsigned int factor = get_update_sysctl_factor();
@@ -1008,6 +1042,7 @@ int sched_update_scaling(void)
@@ -1008,6 +1026,7 @@ int sched_update_scaling(void)
return 0;
}
@ -892,7 +834,7 @@ index b55fa314df37..b8c4649c72e8 100644
#endif
#endif
@@ -1240,6 +1275,10 @@ static void update_curr(struct cfs_rq *cfs_rq)
@@ -1240,6 +1259,10 @@ static void update_curr(struct cfs_rq *cfs_rq)
if (unlikely(delta_exec <= 0))
return;
@ -903,7 +845,7 @@ index b55fa314df37..b8c4649c72e8 100644
curr->vruntime += calc_delta_fair(delta_exec, curr);
resched = update_deadline(cfs_rq, curr);
update_min_vruntime(cfs_rq);
@@ -3884,7 +3923,7 @@ static void reweight_eevdf(struct sched_entity *se, u64 avruntime,
@@ -3884,7 +3907,7 @@ static void reweight_eevdf(struct sched_entity *se, u64 avruntime,
se->deadline = avruntime + vslice;
}
@ -912,7 +854,7 @@ index b55fa314df37..b8c4649c72e8 100644
unsigned long weight)
{
bool curr = cfs_rq->curr == se;
@@ -5293,6 +5332,9 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
@@ -5293,6 +5316,9 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
*
* EEVDF: placement strategy #1 / #2
*/
@ -922,7 +864,7 @@ index b55fa314df37..b8c4649c72e8 100644
if (sched_feat(PLACE_LAG) && cfs_rq->nr_running) {
struct sched_entity *curr = cfs_rq->curr;
unsigned long load;
@@ -5368,7 +5410,11 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
@@ -5368,7 +5394,11 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
se->rel_deadline = 0;
return;
}
@ -935,7 +877,7 @@ index b55fa314df37..b8c4649c72e8 100644
/*
* When joining the competition; the existing tasks will be,
* on average, halfway through their slice, as such start tasks
@@ -7109,6 +7155,14 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
@@ -7110,6 +7140,14 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
p = task_of(se);
h_nr_running = 1;
idle_h_nr_running = task_has_idle_policy(p);
@ -950,7 +892,7 @@ index b55fa314df37..b8c4649c72e8 100644
} else {
cfs_rq = group_cfs_rq(se);
slice = cfs_rq_min_slice(cfs_rq);
@@ -9018,16 +9072,25 @@ static void yield_task_fair(struct rq *rq)
@@ -9025,16 +9063,25 @@ static void yield_task_fair(struct rq *rq)
/*
* Are we the only task in the tree?
*/
@ -976,7 +918,7 @@ index b55fa314df37..b8c4649c72e8 100644
/*
* Tell update_rq_clock() that we've just updated,
* so we don't do microscopic update in schedule()
@@ -13079,6 +13142,9 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
@@ -13086,6 +13133,9 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
static void task_fork_fair(struct task_struct *p)
{
set_task_max_allowed_capacity(p);
@ -986,7 +928,7 @@ index b55fa314df37..b8c4649c72e8 100644
}
/*
@@ -13189,6 +13255,10 @@ static void attach_task_cfs_rq(struct task_struct *p)
@@ -13196,6 +13246,10 @@ static void attach_task_cfs_rq(struct task_struct *p)
static void switched_from_fair(struct rq *rq, struct task_struct *p)
{
@ -998,33 +940,24 @@ index b55fa314df37..b8c4649c72e8 100644
}
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 991226ab5250..043ee801bb19 100644
index f610df2e0811..996c86741e28 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2074,7 +2074,12 @@ static inline void update_sched_domain_debugfs(void) { }
@@ -2074,7 +2074,11 @@ static inline void update_sched_domain_debugfs(void) { }
static inline void dirty_sched_domain_sysctl(int cpu) { }
#endif
+#ifdef CONFIG_SCHED_BORE
+extern void sched_update_min_base_slice(void);
+extern void sched_update_migration_cost(void);
+#else // !CONFIG_SCHED_BORE
extern int sched_update_scaling(void);
+#endif // CONFIG_SCHED_BORE
static inline const struct cpumask *task_user_cpus(struct task_struct *p)
{
@@ -2792,9 +2797,20 @@ extern void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags);
#endif
@@ -2824,7 +2828,12 @@ extern void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags);
extern const_debug unsigned int sysctl_sched_nr_migrate;
+#ifdef CONFIG_SCHED_BORE
+extern unsigned int sysctl_sched_migration_cost_base;
+extern unsigned int sysctl_sched_migration_cost_step;
+extern __read_mostly uint sysctl_sched_migration_cost;
+#else // !CONFIG_SCHED_BORE
extern const_debug unsigned int sysctl_sched_migration_cost;
+#endif // CONFIG_SCHED_BORE
+#ifdef CONFIG_SCHED_BORE
+extern unsigned int sysctl_sched_min_base_slice;
@ -1037,4 +970,3 @@ index 991226ab5250..043ee801bb19 100644
extern int sysctl_resched_latency_warn_ms;
--
2.47.0