From nobody Sun May 24 23:30:00 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9084023ED60 for ; Wed, 20 May 2026 13:07:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779282471; cv=none; b=iEiWcd78jcE6dOLlaV/zMdLFHRDXtbjSdCpEJUT0ZNJo1YvGl6zmrYU9y1smR66flDq1t0uXHBvVIGjWCuY2j6T6VWV9iaDYQx+r/5n5iYjieMgYWA0o2FqUjiUKpii9JCAd6PDPjhCR1nlkgzhUUAaYAZR3HXThcnV60XoS7iE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779282471; c=relaxed/simple; bh=dZ8YSndQsoNmx7w+474HEfO6+r4dLksfCHyTRMvIML0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=nMTmy8CulZtH9tvy3pZx9uJKepa1AmphMhWynEfUFDkrwj8FP6KXU8TmAtsH1twxNMBQx7uHH7sJNaWn1c17hjvx99z6bd1xHs+er7S4H6FKdiSyUyiQGsuDL5wG6/OVW0IjGtYCdpJ6OlQeKMfErZIgrWp2SvnjeRRK8AVFakk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dGbVKbMB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dGbVKbMB" Received: by smtp.kernel.org (Postfix) with ESMTPS id DCBD0C2BCB0; Wed, 20 May 2026 13:07:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779282470; bh=dZ8YSndQsoNmx7w+474HEfO6+r4dLksfCHyTRMvIML0=; h=From:Date:Subject:To:Cc:Reply-To:From; b=dGbVKbMBTmENUTX14fLZKzwa5pqcSaiWefChRqZvL6TxvVckKqG+wE94LQ2K6WF4s 4vO2Aj5Gw5oW/PTBLFpvFmGEQsVKe8WphQZRHESeW01eXu4v4X2Peog+xjq5pVHYP+ i/3n03MgXXOf/T5xC6AE2oDn6rmBoDmRIqt6MORhCr7jeTWcd07gVcUqYEJNKZ3GYO 1OcmlVOPfLzLLZEOjAEuhO02/NwonpUAZN1YJJtDL35CjX9KyZOVRNAFs4Op+dMvCN zZEYKYQPUvW7RMuvo/9tjOmAP9zCYhSoUcDr3VYH6uhc7AmjKNd5GfQT+2Zr+g9B0H mYRtfO5f8GVrQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9493CD4F3D; Wed, 20 May 2026 13:07:49 +0000 (UTC) From: Xiao Feng via B4 Relay Date: Wed, 20 May 2026 21:07:49 +0800 Subject: [PATCH] sched/fork: Fix timer_slack_ns inheritance for RT tasks Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260520-sched-fork-fix-timer-slack-v1-1-d4fc937245b2@xiaomi.com> X-B4-Tracking: v=1; b=H4sIACSyDWoC/yXMwQ6CMAyA4VchPdtkbsrBVzEeRtdBRcG0QEgI7 +6U45f8+TcwVmGDW7WB8iIm41BwPlVAXRxaRknF4J2v3dU7NOo4YR61xywrTvJmRXtF6rFOOVL jQkiXAGXwUS7Jf35/HLa5eTJNvyPs+xdtksyYfgAAAA== X-Change-ID: 20260520-sched-fork-fix-timer-slack-6dfacb033d43 To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Kees Cook , Felix Moessbauer , Thomas Gleixner Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Xiao Feng X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779282468; l=3123; i=xiaofeng5@xiaomi.com; s=20260520; h=from:subject:message-id; bh=Z8IuJn4aKUWAvDe5Kpak3owHY216za5bjiydkqmvna8=; b=Rtvgm49E2Sy9Gr8Xg2UYbouCShW1ZRLMLsLo7cdfQPp9S/JROW01+FZDMIBGw4YacqB4FdSVv elHmfiaTdouC2q7Rc3IOCOqsLqa98SPu0n1ej4v8jUXeYQw2zY+URpm X-Developer-Key: i=xiaofeng5@xiaomi.com; a=ed25519; pk=RGje+zXrBTS+7klps4Lhq/+str3IGTgzLwYEkGwZfs0= X-Endpoint-Received: by B4 Relay for xiaofeng5@xiaomi.com/20260520 with auth_id=784 X-Original-From: Xiao Feng Reply-To: xiaofeng5@xiaomi.com From: Xiao Feng Since commit ed4fb6d7ef68 ("hrtimer: Use and report correct timerslack values for realtime tasks"), RT tasks have their timer_slack_ns set to 0 in __setscheduler_params(). This introduces two related problems when RT tasks fork children that end up running as CFS: Problem 1: sched_reset_on_fork When a RT task with sched_reset_on_fork set forks a child: 1. dup_task_struct() copies timer_slack_ns (0) from the RT parent. 2. copy_process() sets: p->default_timer_slack_ns =3D current->timer_slack_ns (=3D 0) 3. sched_fork() demotes the child to SCHED_NORMAL but does not restore timer_slack_ns. Result: CFS child has timer_slack_ns =3D 0 and default_timer_slack_ns =3D 0 permanently. Problem 2: RT fork followed by later policy change When a RT task forks a child (without reset_on_fork), the child inherits RT policy with timer_slack_ns =3D 0. copy_process() sets default_timer_slack_ns =3D current->timer_slack_ns (=3D 0). If the child is later demoted to CFS via sched_setscheduler(), __setscheduler_params() tries to restore timer_slack_ns from default_timer_slack_ns, but it is 0. Result: same as above. Both problems prevent timer coalescing for these CFS tasks, causing unnecessary wakeups and increased power consumption. Writing 0 to /proc/pid/timerslack_ns also cannot restore a proper default. Fix both issues: 1. In copy_process(), inherit default_timer_slack_ns from the parent's default_timer_slack_ns (which is preserved across RT transitions) instead of timer_slack_ns (which is 0 for RT tasks). 2. In sched_fork(), when sched_reset_on_fork demotes RT/DL to CFS, explicitly restore timer_slack_ns from the parent's default_timer_slack_ns, falling back to 50us if it is also 0. Fixes: ed4fb6d7ef68 ("hrtimer: Use and report correct timerslack values for= realtime tasks") Signed-off-by: Xiao Feng --- kernel/fork.c | 2 +- kernel/sched/core.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/fork.c b/kernel/fork.c index 5f3fdfdb14c7..c3ef5ebb3037 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2174,7 +2174,7 @@ __latent_entropy struct task_struct *copy_process( retval =3D -EAGAIN; #endif =20 - p->default_timer_slack_ns =3D current->timer_slack_ns; + p->default_timer_slack_ns =3D current->default_timer_slack_ns; =20 #ifdef CONFIG_PSI p->psi_flags =3D 0; diff --git a/kernel/sched/core.c b/kernel/sched/core.c index b8871449d3c6..9b63560a45de 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -4710,6 +4710,8 @@ int sched_fork(u64 clone_flags, struct task_struct *p) p->policy =3D SCHED_NORMAL; p->static_prio =3D NICE_TO_PRIO(0); p->rt_priority =3D 0; + p->timer_slack_ns =3D current->default_timer_slack_ns ?: 50000; + p->default_timer_slack_ns =3D p->timer_slack_ns; } else if (PRIO_TO_NICE(p->static_prio) < 0) p->static_prio =3D NICE_TO_PRIO(0); =20 --- base-commit: 27fa82620cbaa89a7fc11ac3057701d598813e87 change-id: 20260520-sched-fork-fix-timer-slack-6dfacb033d43 Best regards, -- =20 Xiao Feng