From nobody Sun May 24 22:45:49 2026 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C022335BA8 for ; Thu, 21 May 2026 02:53:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779331991; cv=none; b=VD2pPuvM3bATkuB2AnDJI41Mus55oPqgHUTo2R7iPD7k3EviYy2h5Txs9h4IVhiXeQuJ8dkskYJ322wyU3UpkyvKuSgLx1ix/6ef50F2tCqaJkHVKHfO8VC7atcP9Z/VJfA+cVEelVnwP1/D5TELaW9go+dPmL3cc7csJbY33xA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779331991; c=relaxed/simple; bh=3LTAjEVrQ22FvDLLiqQpox+1ib99Ie5oxlZvg/9ZfFk=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=YYyYnbHuHK0x2B9iPUXRQ/+yZfpWU1r3r3mc80R4VAtTXBb5SOPI0YbWGMOZ7Rf9r4kGMKqlapoGPLeO/m80tkAD9hWzNkLtX0lXxOkmB/cA9cVCOABWmcLXhqaA6RHbyLkXGTpa6/uLJJ/yYH2j7E4Us19ddfn/PGAHnh/4X9A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PrLaKfXj; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PrLaKfXj" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2bd2c147abaso33140085ad.3 for ; Wed, 20 May 2026 19:53:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779331990; x=1779936790; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Y5m3E9nEzq5GBQxTaJ7nJii8dfxHM+tKAT8MqhcTYmE=; b=PrLaKfXjCwmj7enTIrD2zLqA51MBOQNbFyMyU5Iz974PGW3PclheMJcL0oA2QAfvws eYE4bBk3AOWwWGlVos8jx8q/7dnt/+Fp6prsqe4FHYT1tMNzMqSmHxF/VSkQCEluGbrm hd4GMFh9WxSv8m66GXO7IJ7eh5qF9vIrveSniL6KKFndlKto4pzMIY8/QmHjpAX2JN4K kano7sHTvjO4xj8ZB/3eibskdQ2kqILujuyHmlfgx5/wf9TWodiiGY1MFMnWboZciWsi o1DDSo8+OnfqyhJfPqzjn3igC6Sd84nln6t8eKO637miVbsaZcy0Ru0IaGui2SvyKYC/ n9vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779331990; x=1779936790; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Y5m3E9nEzq5GBQxTaJ7nJii8dfxHM+tKAT8MqhcTYmE=; b=YZetrMNpU264z9zQx4PtLlVIK8TwkeHE9DH5mo0wVB+symKhhPmgUTGP0CHnyIXebE n/ttpCpKAw7gZq+lgIYGAK2ZdMprsyK51vekx1WzsKZeOJpVZV6zfTs4dPAerp769/jp dJm3XMtcS4cECRTpGcylTWAFNwMQgKIn8Mz9dA0Uc40khxHwU6ycvKtf9l8s3o6LOEHN Fh7q6z8Y3r5X8y0Mw621pOHwzX/DLJkdyHSDgdZ5sY2r/FZt7TpFImtjaUeavQWeaY8l jLAQxXEqnGrpcJqIji2TAPuE4WWESdVixZXPTC1lcpcS2Qn4achejTAXJgda5V5B7pkH 61Xw== X-Forwarded-Encrypted: i=1; AFNElJ/fKt7QpnMaWMC1QKTuw9YKCckZY2RQWhW9zOi8FvqeeSsVGq0qi87IRWxY6pgtrj+FyNG9xBzyyQ5p0EI=@vger.kernel.org X-Gm-Message-State: AOJu0YzmCsfRl0sZ88tlz6igZb+ZnZqJ7byN9B2wlWgEqcdEmJ1ZvcP4 kY4naXrv0iYF6HKuCvyGkQ+q4iXtumFp6INkq0+3uYlyyniYWBwGhZ2G X-Gm-Gg: Acq92OECPtelIPlZaFeu4mJnThMfXh2UpGx9/dlj+EXFAEebHtcpesBDxnIBQR+LnLf SVoozJmJ9ECmMzL4MfQB+NWt/VSTskOt9Rpb0qeFjrnbzHDfVx0yUuwpeFevSHmwTTS7u6rbP9d EjiNlKcEgWoA1YviRnm35RiH9F6gXAW0B9SlceLetJIxG9zqiPJf2Q/DIhkUq1WiEkCIbUWa6rw f5mMAUfVlaNwJQ7uyQk0/jSfQDi6YyGK8Trh0N165PHzP0r5kvndM1G9cxdpJrP9iy8vHNLEibo NClrYWPHD2KYqkyGn4NFVT4vhXqVS7gj4CVgrjnDFTOU806SwmUurfypA/5bBU3x4f9Ij0SzYDF 7vVDdnaAw6430laqVkhshT3ICIdC7Zs1dcqywPCMqJB6S9/D7gI3ROEM5Ymjj+axqbxTBMbHGlu 0kYV3vmiYTrMYDBE+TUUdWkICC0T6xRl6DDKNx14sHmCAYO3A8oZliJmANHLgjRmA= X-Received: by 2002:a17:903:1ae7:b0:2bd:936c:8155 with SMTP id d9443c01a7336-2bea2ffc757mr8752095ad.13.1779331989779; Wed, 20 May 2026 19:53:09 -0700 (PDT) Received: from mi-HP-ProDesk-600-G5-PCI-MT.mioffice.cn ([43.224.245.239]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2bd5bd5fb0csm218684375ad.17.2026.05.20.19.53.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 19:53:09 -0700 (PDT) From: "Guanyou.Chen" X-Google-Original-From: "Guanyou.Chen" To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Andrew Morton Cc: Thomas Gleixner , Felix Moessbauer , Dietmar Eggemann , Steven Rostedt , Kees Cook , chenguanyou@xiaomi.com, linqiaoting@xiaomi.com, chunhui.li@mediatek.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] sched: restore timer_slack_ns when resetting RT policy on fork Date: Thu, 21 May 2026 10:52:50 +0800 Message-Id: <20260521025250.1469390-1-chenguanyou@xiaomi.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3D0 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=3D0 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=3D0, exposing this latent bug. This causes unnecessary timer interrupts and increased power consumption, as NORMAL threads with slack=3D0 prevent timer coalescing. Fix this by: 1. Initializing default_timer_slack_ns=3D50000 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=3D0, avg_sleep=3D38us, ~832K interrupts/s After this fix: child slack=3D50000, avg_sleep=3D88us, ~363K interrupts/s Fixes: 6976675d9404 ("hrtimer: create a "timer_slack" field in the task str= uct") Fixes: ed4fb6d7ef68 ("hrtimer: Use and report correct timerslack values for= realtime tasks") Reported-by: Qiaoting.Lin Signed-off-by: Guanyou.Chen Signed-off-by: Chunhui.Li --- 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) = =3D { INIT_CPU_TIMERS(init_task) .pi_lock =3D __RAW_SPIN_LOCK_UNLOCKED(init_task.pi_lock), .timer_slack_ns =3D 50000, /* 50 usec default slack */ + .default_timer_slack_ns =3D 50000, /* 50 usec default slack */ .thread_pid =3D &init_struct_pid, .thread_node =3D 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 =3D -EAGAIN; #endif =20 - p->default_timer_slack_ns =3D current->timer_slack_ns; - #ifdef CONFIG_PSI p->psi_flags =3D 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 =3D SCHED_NORMAL; p->static_prio =3D NICE_TO_PRIO(0); p->rt_priority =3D 0; + p->timer_slack_ns =3D p->default_timer_slack_ns; } else if (PRIO_TO_NICE(p->static_prio) < 0) p->static_prio =3D NICE_TO_PRIO(0); =20 --=20 2.34.1