Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] sched: restore timer_slack_ns when resetting RT policy on fork
@ 2026-05-21  2:52 Guanyou.Chen
  2026-05-21  3:18 ` Cunlong Li
  2026-05-21  7:04 ` Peter Zijlstra
  0 siblings, 2 replies; 11+ messages in thread
From: Guanyou.Chen @ 2026-05-21  2:52 UTC (permalink / raw)
  To: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Andrew Morton
  Cc: Thomas Gleixner, Felix Moessbauer, Dietmar Eggemann,
	Steven Rostedt, Kees Cook, chenguanyou, linqiaoting, chunhui.li,
	linux-kernel, linux-mm

Commit ed4fb6d7ef68 ("hrtimer: Use and report correct timerslack values
for realtime tasks") sets timer_slack_ns to 0 for RT tasks in
__setscheduler_params(). However, when an RT task with SCHED_RESET_ON_FORK
creates child threads, the children inherit timer_slack_ns=0 from the
parent. sched_fork() resets the child's policy to SCHED_NORMAL but does
not restore timer_slack_ns, leaving the child permanently running with
zero slack.

Additionally, init_task never initialized default_timer_slack_ns, so all
processes in the system have default_timer_slack_ns=0 inherited from init.
The original fork code masked this by using timer_slack_ns (50000) as the
source for default_timer_slack_ns. After ed4fb6d7ef68, RT tasks have
timer_slack_ns=0, exposing this latent bug.

This causes unnecessary timer interrupts and increased power consumption,
as NORMAL threads with slack=0 prevent timer coalescing.

Fix this by:
1. Initializing default_timer_slack_ns=50000 in init_task.
2. In copy_process(), removing the incorrect default_timer_slack_ns
   override (dup_task_struct already copies both timer_slack_ns and
   default_timer_slack_ns correctly from the parent).
3. In sched_fork(), restoring timer_slack_ns from default_timer_slack_ns
   when resetting from RT/DL to NORMAL policy.

Before this fix (RT parent, RESET_ON_FORK, 32 child threads usleep(1)):
  child slack=0, avg_sleep=38us, ~832K interrupts/s

After this fix:
  child slack=50000, avg_sleep=88us, ~363K interrupts/s

Fixes: 6976675d9404 ("hrtimer: create a "timer_slack" field in the task struct")
Fixes: ed4fb6d7ef68 ("hrtimer: Use and report correct timerslack values for realtime tasks")
Reported-by: Qiaoting.Lin <linqiaoting@xiaomi.com>
Signed-off-by: Guanyou.Chen <chenguanyou@xiaomi.com>
Signed-off-by: Chunhui.Li <chunhui.li@mediatek.com>
---
 init/init_task.c    | 1 +
 kernel/fork.c       | 2 --
 kernel/sched/core.c | 1 +
 3 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/init/init_task.c b/init/init_task.c
index 5c838757fc10..57ff8dae9bfb 100644
--- a/init/init_task.c
+++ b/init/init_task.c
@@ -170,6 +170,7 @@ struct task_struct init_task __aligned(L1_CACHE_BYTES) = {
 	INIT_CPU_TIMERS(init_task)
 	.pi_lock	= __RAW_SPIN_LOCK_UNLOCKED(init_task.pi_lock),
 	.timer_slack_ns = 50000, /* 50 usec default slack */
+	.default_timer_slack_ns = 50000, /* 50 usec default slack */
 	.thread_pid	= &init_struct_pid,
 	.thread_node	= LIST_HEAD_INIT(init_signals.thread_head),
 #ifdef CONFIG_AUDIT
diff --git a/kernel/fork.c b/kernel/fork.c
index 65113a304518..8358df80e11d 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2133,8 +2133,6 @@ __latent_entropy struct task_struct *copy_process(
 	retval = -EAGAIN;
 #endif
 
-	p->default_timer_slack_ns = current->timer_slack_ns;
-
 #ifdef CONFIG_PSI
 	p->psi_flags = 0;
 #endif
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index b7f77c165a6e..b1a241810ce0 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -4649,6 +4649,7 @@ int sched_fork(u64 clone_flags, struct task_struct *p)
 			p->policy = SCHED_NORMAL;
 			p->static_prio = NICE_TO_PRIO(0);
 			p->rt_priority = 0;
+			p->timer_slack_ns = p->default_timer_slack_ns;
 		} else if (PRIO_TO_NICE(p->static_prio) < 0)
 			p->static_prio = NICE_TO_PRIO(0);
 
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-05-22 13:11 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-21  2:52 [PATCH] sched: restore timer_slack_ns when resetting RT policy on fork Guanyou.Chen
2026-05-21  3:18 ` Cunlong Li
2026-05-21  6:23   ` Guanyou Chen
2026-05-21  6:31   ` Guanyou Chen
2026-05-21  7:04 ` Peter Zijlstra
2026-05-21  7:35   ` [PATCH v2 0/2] sched: fix timer_slack_ns for children of RT tasks Guanyou.Chen
2026-05-21  7:35     ` [PATCH 1/2] sched: restore timer_slack_ns when resetting RT policy on fork Guanyou.Chen
2026-05-21  7:35     ` [PATCH 2/2] fork: fix default_timer_slack_ns inheritance from RT parent Guanyou.Chen
2026-05-22 13:09   ` [PATCH v3 0/2] sched/fork: fix timer_slack_ns for children of RT tasks Guanyou.Chen
2026-05-22 13:09     ` [PATCH v3 1/2] sched: restore timer_slack_ns when resetting RT policy on fork Guanyou.Chen
2026-05-22 13:10     ` [PATCH v3 2/2] fork: fix default_timer_slack_ns inheritance from RT parent Guanyou.Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox