linux-pikaos-template/patches/0003-bore.patch

425 lines
14 KiB
Diff
Raw Normal View History

2023-04-10 19:42:41 +02:00
From d1d05832308e210422f7c52d052b026deb9fabf1 Mon Sep 17 00:00:00 2001
2023-02-22 16:23:11 +01:00
From: Peter Jung <admin@ptr1337.dev>
2023-04-10 19:42:41 +02:00
Date: Thu, 6 Apr 2023 19:12:01 +0200
Subject: [PATCH] bore
2023-02-22 16:23:11 +01:00
Signed-off-by: Peter Jung <admin@ptr1337.dev>
---
2023-04-10 19:42:41 +02:00
include/linux/sched.h | 6 ++
init/Kconfig | 20 ++++++
kernel/sched/core.c | 30 ++++++++
kernel/sched/debug.c | 3 +
kernel/sched/fair.c | 149 +++++++++++++++++++++++++++++++++++++++-
kernel/sched/features.h | 8 +++
6 files changed, 213 insertions(+), 3 deletions(-)
2023-02-22 16:23:11 +01:00
diff --git a/include/linux/sched.h b/include/linux/sched.h
2023-04-10 19:42:41 +02:00
index 63d242164b1a..39a046d6cf90 100644
2023-02-22 16:23:11 +01:00
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
2023-04-10 19:42:41 +02:00
@@ -555,6 +555,12 @@ struct sched_entity {
2023-02-22 16:23:11 +01:00
u64 sum_exec_runtime;
u64 vruntime;
u64 prev_sum_exec_runtime;
+#ifdef CONFIG_SCHED_BORE
+ u64 prev_burst_time;
+ u64 burst_time;
2023-04-10 19:42:41 +02:00
+ u64 max_burst_time;
+ u8 penalty_score;
2023-02-22 16:23:11 +01:00
+#endif // CONFIG_SCHED_BORE
2023-04-10 19:42:41 +02:00
u64 nr_migrations;
2023-02-22 16:23:11 +01:00
diff --git a/init/Kconfig b/init/Kconfig
2023-04-10 19:42:41 +02:00
index 1fb5f313d18f..6595e5ed2416 100644
2023-02-22 16:23:11 +01:00
--- a/init/Kconfig
+++ b/init/Kconfig
2023-04-10 19:42:41 +02:00
@@ -1285,6 +1285,26 @@ config CHECKPOINT_RESTORE
2023-02-22 16:23:11 +01:00
If unsure, say N here.
+config SCHED_BORE
+ bool "Burst-Oriented Response Enhancer"
+ default y
+ help
+ In Desktop and Mobile computing, one might prefer interactive
+ tasks to keep responsive no matter what they run in the background.
+
+ Enabling this kernel feature modifies the scheduler to discriminate
+ tasks by their burst time (runtime since it last went sleeping or
+ yielding state) and prioritize those that run less bursty.
+ Such tasks usually include window compositor, widgets backend,
+ terminal emulator, video playback, games and so on.
+ With a little impact to scheduling fairness, it may improve
+ responsiveness especially under heavy background workload.
+
+ You can turn it off by setting the sysctl kernel.sched_bore = 0.
+ Enabling this feature implies NO_GENTLE_FAIR_SLEEPERS by default.
+
+ If unsure say Y here.
+
config SCHED_AUTOGROUP
bool "Automatic process group scheduling"
select CGROUPS
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
2023-04-10 19:42:41 +02:00
index 0d18c3969f90..34db768f6ba8 100644
2023-02-22 16:23:11 +01:00
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
2023-04-10 19:42:41 +02:00
@@ -4418,6 +4418,22 @@ int wake_up_state(struct task_struct *p, unsigned int state)
2023-02-22 16:23:11 +01:00
return try_to_wake_up(p, state, 0);
}
+#ifdef CONFIG_SCHED_BORE
2023-04-10 19:42:41 +02:00
+static inline void adjust_prev_burst(struct task_struct *p)
2023-02-22 16:23:11 +01:00
+{
+ u32 cnt = 0;
+ u64 sum = 0, avg = 0;
2023-04-10 19:42:41 +02:00
+ struct task_struct *sib;
2023-02-22 16:23:11 +01:00
+ list_for_each_entry(sib, &p->sibling, sibling) {
+ cnt++;
2023-04-10 19:42:41 +02:00
+ sum += sib->se.max_burst_time >> 8;
2023-02-22 16:23:11 +01:00
+ }
+ if (cnt) avg = div_u64(sum, cnt) << 8;
+ if (p->se.prev_burst_time < avg) p->se.prev_burst_time = avg;
2023-04-10 19:42:41 +02:00
+ p->se.max_burst_time = p->se.prev_burst_time;
2023-02-22 16:23:11 +01:00
+}
+#endif // CONFIG_SCHED_BORE
+
/*
* Perform scheduler related setup for a newly forked process p.
* p is forked by current.
2023-04-10 19:42:41 +02:00
@@ -4434,6 +4450,9 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
2023-03-12 20:40:20 +01:00
p->se.prev_sum_exec_runtime = 0;
p->se.nr_migrations = 0;
2023-02-22 16:23:11 +01:00
p->se.vruntime = 0;
+#ifdef CONFIG_SCHED_BORE
+ p->se.burst_time = 0;
+#endif // CONFIG_SCHED_BORE
2023-04-10 19:42:41 +02:00
INIT_LIST_HEAD(&p->se.group_node);
#ifdef CONFIG_FAIR_GROUP_SCHED
@@ -4659,6 +4678,9 @@ late_initcall(sched_core_sysctl_init);
2023-02-22 16:23:11 +01:00
int sched_fork(unsigned long clone_flags, struct task_struct *p)
{
__sched_fork(clone_flags, p);
+#ifdef CONFIG_SCHED_BORE
2023-04-10 19:42:41 +02:00
+ adjust_prev_burst(p);
2023-02-22 16:23:11 +01:00
+#endif // CONFIG_SCHED_BORE
/*
* We mark the process as NEW here. This guarantees that
* nobody will actually run it, and a signal or other external
2023-04-10 19:42:41 +02:00
@@ -9126,6 +9148,10 @@ void __init init_idle(struct task_struct *idle, int cpu)
2023-02-22 16:23:11 +01:00
idle->__state = TASK_RUNNING;
idle->se.exec_start = sched_clock();
+#ifdef CONFIG_SCHED_BORE
+ idle->se.prev_burst_time = 0;
2023-04-10 19:42:41 +02:00
+ idle->se.max_burst_time = 0;
2023-02-22 16:23:11 +01:00
+#endif //CONFIG_SCHED_BORE
/*
* PF_KTHREAD should already be set at this point; regardless, make it
* look like a proper per-CPU kthread.
2023-04-10 19:42:41 +02:00
@@ -9793,6 +9819,10 @@ void __init sched_init(void)
2023-02-22 16:23:11 +01:00
BUG_ON(&dl_sched_class != &stop_sched_class + 1);
#endif
+#ifdef CONFIG_SCHED_BORE
2023-04-10 19:42:41 +02:00
+ printk(KERN_INFO "BORE (Burst-Oriented Response Enhancer) CPU Scheduler modification 2.1.1 by Masahito Suzuki");
2023-02-22 16:23:11 +01:00
+#endif // CONFIG_SCHED_BORE
+
wait_bit_init();
#ifdef CONFIG_FAIR_GROUP_SCHED
diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
2023-04-10 19:42:41 +02:00
index 1637b65ba07a..752c43a9ff13 100644
2023-02-22 16:23:11 +01:00
--- a/kernel/sched/debug.c
+++ b/kernel/sched/debug.c
2023-04-10 19:42:41 +02:00
@@ -547,6 +547,9 @@ print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
2023-02-22 16:23:11 +01:00
SPLIT_NS(schedstat_val_or_zero(p->stats.sum_sleep_runtime)),
SPLIT_NS(schedstat_val_or_zero(p->stats.sum_block_runtime)));
+#ifdef CONFIG_SCHED_BORE
2023-04-10 19:42:41 +02:00
+ SEQ_printf(m, " %2d", p->se.penalty_score);
2023-02-22 16:23:11 +01:00
+#endif
#ifdef CONFIG_NUMA_BALANCING
SEQ_printf(m, " %d %d", task_node(p), task_numa_group_id(p));
#endif
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
2023-04-10 19:42:41 +02:00
index 6986ea31c984..ee461e4586ca 100644
2023-02-22 16:23:11 +01:00
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -19,6 +19,9 @@
*
* Adaptive scheduling granularity, math enhancements by Peter Zijlstra
* Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
+ *
+ * Burst-Oriented Response Enhancer (BORE) CPU Scheduler
2023-04-10 19:42:41 +02:00
+ * Copyright (C) 2021-2023 Masahito Suzuki <firelzrd@gmail.com>
2023-02-22 16:23:11 +01:00
*/
#include <linux/energy_model.h>
#include <linux/mmap_lock.h>
2023-04-10 19:42:41 +02:00
@@ -126,6 +129,16 @@ static unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
2023-02-22 16:23:11 +01:00
const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
+#ifdef CONFIG_SCHED_BORE
2023-04-10 19:42:41 +02:00
+unsigned int __read_mostly sched_bore = 3;
+unsigned int __read_mostly sched_burst_penalty_offset = 12;
+unsigned int __read_mostly sched_burst_penalty_scale = 1292;
+unsigned int __read_mostly sched_burst_smoothness = 1;
2023-02-22 16:23:11 +01:00
+static int three = 3;
+static int sixty_four = 64;
+static int maxval_12_bits = 4095;
+#endif // CONFIG_SCHED_BORE
+
int sched_thermal_decay_shift;
static int __init setup_sched_thermal_decay_shift(char *str)
{
2023-04-10 19:42:41 +02:00
@@ -185,6 +198,44 @@ static unsigned int sysctl_numa_balancing_promote_rate_limit = 65536;
2023-02-22 16:23:11 +01:00
#ifdef CONFIG_SYSCTL
static struct ctl_table sched_fair_sysctls[] = {
+#ifdef CONFIG_SCHED_BORE
+ {
+ .procname = "sched_bore",
+ .data = &sched_bore,
+ .maxlen = sizeof(unsigned int),
+ .mode = 0644,
+ .proc_handler = &proc_dointvec_minmax,
+ .extra1 = SYSCTL_ZERO,
2023-04-10 19:42:41 +02:00
+ .extra2 = &three,
2023-02-22 16:23:11 +01:00
+ },
+ {
2023-04-10 19:42:41 +02:00
+ .procname = "sched_burst_penalty_offset",
+ .data = &sched_burst_penalty_offset,
2023-02-22 16:23:11 +01:00
+ .maxlen = sizeof(unsigned int),
+ .mode = 0644,
+ .proc_handler = &proc_dointvec_minmax,
+ .extra1 = SYSCTL_ZERO,
2023-04-10 19:42:41 +02:00
+ .extra2 = &sixty_four,
2023-02-22 16:23:11 +01:00
+ },
+ {
2023-04-10 19:42:41 +02:00
+ .procname = "sched_burst_penalty_scale",
+ .data = &sched_burst_penalty_scale,
2023-02-22 16:23:11 +01:00
+ .maxlen = sizeof(unsigned int),
+ .mode = 0644,
+ .proc_handler = &proc_dointvec_minmax,
+ .extra1 = SYSCTL_ZERO,
2023-04-10 19:42:41 +02:00
+ .extra2 = &maxval_12_bits,
2023-02-22 16:23:11 +01:00
+ },
+ {
+ .procname = "sched_burst_smoothness",
+ .data = &sched_burst_smoothness,
+ .maxlen = sizeof(unsigned int),
+ .mode = 0644,
+ .proc_handler = &proc_dointvec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = &three,
+ },
+#endif // CONFIG_SCHED_BORE
{
.procname = "sched_child_runs_first",
.data = &sysctl_sched_child_runs_first,
2023-04-10 19:42:41 +02:00
@@ -891,6 +942,47 @@ static void update_tg_load_avg(struct cfs_rq *cfs_rq)
2023-02-22 16:23:11 +01:00
}
#endif /* CONFIG_SMP */
+#ifdef CONFIG_SCHED_BORE
2023-04-10 19:42:41 +02:00
+static inline u32 __calc_bits10(u64 burst_time) {
+ u32 bits = fls64(burst_time);
+ u32 fdigs = likely(bits) ? bits - 1 : 0;
+ return (bits << 10) | (burst_time << (64 - fdigs) >> 54);
2023-02-22 16:23:11 +01:00
+}
+
2023-04-10 19:42:41 +02:00
+static inline u32 __calc_burst_score(u32 bits10, u32 offset) {
+ u32 val10 = max((s32)0, (s32)bits10 - (s32)(offset << 10));
+ return min((u32)39, val10 * sched_burst_penalty_scale >> 20);
2023-02-22 16:23:11 +01:00
+}
+
2023-04-10 19:42:41 +02:00
+static void update_burst_score(struct sched_entity *se) {
+ u32 bits10 = __calc_bits10(se->max_burst_time);
+ se->penalty_score = __calc_burst_score(bits10, sched_burst_penalty_offset);
+}
+
+static inline u64 penalty_scale(u64 delta, struct sched_entity *se) {
+ return mul_u64_u32_shr(delta, sched_prio_to_wmult[se->penalty_score], 22);
+}
+
+static inline u64 preempt_scale(
+ u64 delta, struct sched_entity *curr, struct sched_entity *se) {
+
+ u32 score = max(0, (s32)se->penalty_score - (s32)curr->penalty_score) >> 1;
+ return mul_u64_u32_shr(delta, sched_prio_to_wmult[min(39, 20 + score)], 22);
2023-02-22 16:23:11 +01:00
+}
+
+static inline u64 binary_smooth(u64 old, u64 new, unsigned int smoothness) {
+ return (new + old * ((1 << smoothness) - 1)) >> smoothness;
+}
+
2023-04-10 19:42:41 +02:00
+static void reset_burst(struct sched_entity *se) {
2023-02-22 16:23:11 +01:00
+ se->prev_burst_time = binary_smooth(
+ se->prev_burst_time, se->burst_time, sched_burst_smoothness);
+ se->burst_time = 0;
2023-04-10 19:42:41 +02:00
+
+ se->max_burst_time = se->prev_burst_time;
2023-02-22 16:23:11 +01:00
+}
+#endif // CONFIG_SCHED_BORE
+
/*
* Update the current task's runtime statistics.
*/
2023-04-10 19:42:41 +02:00
@@ -920,6 +1012,14 @@ static void update_curr(struct cfs_rq *cfs_rq)
2023-02-22 16:23:11 +01:00
curr->sum_exec_runtime += delta_exec;
schedstat_add(cfs_rq->exec_clock, delta_exec);
+#ifdef CONFIG_SCHED_BORE
+ curr->burst_time += delta_exec;
2023-04-10 19:42:41 +02:00
+ curr->max_burst_time = max(curr->max_burst_time, curr->burst_time);
2023-02-22 16:23:11 +01:00
+ update_burst_score(curr);
2023-04-10 19:42:41 +02:00
+ if (sched_bore & 1)
+ curr->vruntime += penalty_scale(calc_delta_fair(delta_exec, curr), curr);
2023-02-22 16:23:11 +01:00
+ else
+#endif // CONFIG_SCHED_BORE
curr->vruntime += calc_delta_fair(delta_exec, curr);
2023-04-10 19:42:41 +02:00
update_min_vruntime(cfs_rq);
@@ -5013,8 +5113,14 @@ set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
2023-02-22 16:23:11 +01:00
se->prev_sum_exec_runtime = se->sum_exec_runtime;
}
+#ifdef CONFIG_SCHED_BORE
+static int
+wakeup_preempt_entity_bscale(struct sched_entity *curr,
+ struct sched_entity *se, bool do_scale);
2023-04-10 19:42:41 +02:00
+#else // CONFIG_SCHED_BORE
2023-02-22 16:23:11 +01:00
static int
wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
2023-04-10 19:42:41 +02:00
+#endif // CONFIG_SCHED_BORE
2023-02-22 16:23:11 +01:00
2023-04-10 19:42:41 +02:00
/*
* Pick the next process, keeping these things in mind, in this order:
@@ -5053,16 +5159,34 @@ pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr)
second = curr;
}
+#ifdef CONFIG_SCHED_BORE
+ if (second && wakeup_preempt_entity_bscale(
+ second, left, sched_bore & 2) < 1)
+#else // CONFIG_SCHED_BORE
if (second && wakeup_preempt_entity(second, left) < 1)
+#endif // CONFIG_SCHED_BORE
2023-02-22 16:23:11 +01:00
se = second;
}
- if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) {
+#ifdef CONFIG_SCHED_BORE
+ if (cfs_rq->next && wakeup_preempt_entity_bscale(
2023-04-10 19:42:41 +02:00
+ cfs_rq->next, left, sched_bore & 2) < 1)
2023-02-22 16:23:11 +01:00
+#else // CONFIG_SCHED_BORE
+ if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
+#endif // CONFIG_SCHED_BORE
+ {
/*
* Someone really wants this to run. If it's not unfair, run it.
*/
2023-04-10 19:42:41 +02:00
se = cfs_rq->next;
- } else if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) {
+ }
+#ifdef CONFIG_SCHED_BORE
+ else if (cfs_rq->last && wakeup_preempt_entity_bscale(
+ cfs_rq->last, left, sched_bore & 2) < 1)
+#else // CONFIG_SCHED_BORE
+ else if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
+#endif // CONFIG_SCHED_BORE
+ {
/*
* Prefer last buddy, try to return the CPU to a preempted task.
*/
@@ -6331,6 +6455,9 @@ static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
2023-02-22 16:23:11 +01:00
util_est_dequeue(&rq->cfs, p);
for_each_sched_entity(se) {
+#ifdef CONFIG_SCHED_BORE
+ if (task_sleep) reset_burst(se);
+#endif // CONFIG_SCHED_BORE
cfs_rq = cfs_rq_of(se);
dequeue_entity(cfs_rq, se, flags);
2023-04-10 19:42:41 +02:00
@@ -7746,7 +7873,12 @@ static unsigned long wakeup_gran(struct sched_entity *se)
2023-02-22 16:23:11 +01:00
*
*/
static int
+#ifdef CONFIG_SCHED_BORE
+wakeup_preempt_entity_bscale(struct sched_entity *curr,
+ struct sched_entity *se, bool do_scale)
+#else // CONFIG_SCHED_BORE
wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
+#endif // CONFIG_SCHED_BORE
{
s64 gran, vdiff = curr->vruntime - se->vruntime;
2023-03-12 20:40:20 +01:00
2023-04-10 19:42:41 +02:00
@@ -7754,6 +7886,9 @@ wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
2023-03-12 20:40:20 +01:00
return -1;
gran = wakeup_gran(se);
2023-02-22 16:23:11 +01:00
+#ifdef CONFIG_SCHED_BORE
2023-04-10 19:42:41 +02:00
+ if (do_scale) gran = preempt_scale(gran, curr, se);
2023-02-22 16:23:11 +01:00
+#endif // CONFIG_SCHED_BORE
if (vdiff > gran)
return 1;
2023-04-10 19:42:41 +02:00
@@ -7858,7 +7993,12 @@ static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_
return;
update_curr(cfs_rq_of(se));
- if (wakeup_preempt_entity(se, pse) == 1) {
2023-02-22 16:23:11 +01:00
+#ifdef CONFIG_SCHED_BORE
2023-04-10 19:42:41 +02:00
+ if (wakeup_preempt_entity_bscale(se, pse, sched_bore & 2) == 1)
+#else // CONFIG_SCHED_BORE
+ if (wakeup_preempt_entity(se, pse) == 1)
2023-02-22 16:23:11 +01:00
+#endif // CONFIG_SCHED_BORE
2023-04-10 19:42:41 +02:00
+ {
/*
* Bias pick_next to pick the sched entity that is
* triggering this preemption.
@@ -8094,6 +8234,9 @@ static void yield_task_fair(struct rq *rq)
2023-02-22 16:23:11 +01:00
struct task_struct *curr = rq->curr;
struct cfs_rq *cfs_rq = task_cfs_rq(curr);
struct sched_entity *se = &curr->se;
+#ifdef CONFIG_SCHED_BORE
+ reset_burst(se);
+#endif // CONFIG_SCHED_BORE
/*
* Are we the only task in the tree?
2023-04-10 19:42:41 +02:00
diff --git a/kernel/sched/features.h b/kernel/sched/features.h
index ee7f23c76bd3..3115bde98211 100644
--- a/kernel/sched/features.h
+++ b/kernel/sched/features.h
@@ -4,7 +4,11 @@
* them to run sooner, but does not allow tons of sleepers to
* rip the spread apart.
*/
+#ifdef CONFIG_SCHED_BORE
+SCHED_FEAT(GENTLE_FAIR_SLEEPERS, false)
+#else // CONFIG_SCHED_BORE
SCHED_FEAT(GENTLE_FAIR_SLEEPERS, true)
+#endif // CONFIG_SCHED_BORE
/*
* Place new tasks ahead so that they do not starve already running
@@ -17,7 +21,11 @@ SCHED_FEAT(START_DEBIT, true)
* wakeup-preemption), since its likely going to consume data we
* touched, increases cache locality.
*/
+#ifdef CONFIG_SCHED_BORE
+SCHED_FEAT(NEXT_BUDDY, true)
+#else // CONFIG_SCHED_BORE
SCHED_FEAT(NEXT_BUDDY, false)
+#endif // CONFIG_SCHED_BORE
/*
* Prefer to schedule the task that ran last (when we did
2023-02-22 16:23:11 +01:00
--
2023-04-10 19:42:41 +02:00
2.40.0