From mboxrd@z Thu Jan 1 00:00:00 1970 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 2D88EA95E for ; Thu, 21 May 2026 03:18:13 +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=1779333494; cv=none; b=rjf1aK+6DLjjwxBaFPwa+coDFJNw907TlrvZerkHnvIORFm81gaxLIhQnOCT3WuVsrW+PaO53WqxcY8/sGGTnwyrx9PfPkSi/awIkGSdCjLamtX6GBOAErwJmgphEt7DWHUm02WPTuzOyc9U3eIaNuEKFHTNy1Vv6yxZsIWvR60= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779333494; c=relaxed/simple; bh=xuqc2rR1g8cDgyApgJlaNaeXt5B9SXPeNF5cam3L7qQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type:Content-Disposition; b=dTF9YYpU7NObRSzJ2Dcd2YOPivmLDnpj7ZE5l7c2na5CmBigvQSj3lzWKkYW7FE2UZJmq0N7yZZp0Pq8I50MVANawuryx78X4YqNR1si9WPNQhib6sitres0vWJVMVY7S++CotHHTngTbO8H3/gCbqpxJHZ+7eLFfp3wCBuyRd4= 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=Aix7KSkM; 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="Aix7KSkM" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2bdeed4f717so24887285ad.0 for ; Wed, 20 May 2026 20:18:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779333492; x=1779938292; darn=vger.kernel.org; h=content-transfer-encoding:content-disposition:mime-version :mail-followup-to:references:in-reply-to:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qu9iir3pCu4cSscjSYZYfAghrF1OMpQoowsUczWvTmY=; b=Aix7KSkMcJqaPDmufi1I0iuQC5grLTX42vONToz4aKXyRu/phKFHtxZqZcdOlHcDyY 0sKsG//BI8dQss39U03OQezyxkioXUvMXqo/4SA+c5iAp4kO5Ae5v7iuI3UoRzNN4M2b UmVyOR3lcD/0tONn2aiq1xmM0TdwZc6kxmXL3AiwZXC2GyMfEUQHc61AwVBCsYkUKVMP gULE9ezNXoRX+eVm8eZbHA+JbPpYIn0Ep9mUKhQkW7W865HPAvbhuxUqof4q35+kX0pC wE5Ud5ZWNLF1SF0yGAyJqjWMw9B52gXwjIAbr3oGiIBeInlbf3ri+WCD+dwIvhcaKhNR hOOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779333492; x=1779938292; h=content-transfer-encoding:content-disposition:mime-version :mail-followup-to:references:in-reply-to:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=qu9iir3pCu4cSscjSYZYfAghrF1OMpQoowsUczWvTmY=; b=gKHyuFhts3N5GvZga0tmy+tl6jSJNw/ENzFbuDlEDw82ginyQ2PPotdRWwQpelyZaZ tSv6gaUoaohZTtYjHoxOo/4sFIkkcGuU5rWzdlGWyillgSOgv6efTi08Xa9NNdJPNddy pDrffwMVQ3Mmx2z0gXmryaLc9N95mz0eBbBo5Y7ECHhyyv3tjxSO91RfkoXL9JpP2roP YqIxKaBa1kodJjX7B6scOyQnIZI+x4VPtiN5a4jrKK8ucWyMF86oMMu2BHPvq6vF9mk6 zKkUixWxcItELCyBsD+cRyRbjcyhf7aDE3Rv8kYR4wMADKfSYcdb8Nav3KOtKD8Y5LWR 7l3A== X-Forwarded-Encrypted: i=1; AFNElJ82x5RP0yaR+6+9Qlq7Yvj2yjaonlBMe7VtCzRX4RYmwK04Nyqp7NpLjgvyhvco5PpBf+Ducy2Lua/oTY0=@vger.kernel.org X-Gm-Message-State: AOJu0Yw5hssTuNXglXeQvakznchrnseh3mcu4scbsLKF1zqErsM0dkQP BdSvzZ9sCYKf7pyVRg3batc5N0xkdnnwU05jSNZPaNzSjp/FEyXhiWZH X-Gm-Gg: Acq92OGNwZaNGRRZKxjQpmyk3afKJssfvR7VaPl73OQqmIMjLRVu+/hZLmH8ZNeoskJ LZKpvmdJCPhwGspjAGdXhrmOGtk8Eh0ZoD5XMGZjZbD8NrTjeKj5EnK2lgn0/YTEMD40d9nj64J +ZHWXn0xM/eSc3g7JhyOoD3HGvULg9dnJf1Ogrv90JdRfdNvbegWmwhGL17izv6HXWkb56IKEA5 aDlFSQczZz7FzSm7Exrlceav530DWmRvm/dHCpCyKl3yzpGoEJfZAFOQ1t4nIZ/pLOkF+XJNUZv XFEJYiA4rM+eIrfM7iUXN3s/n7cP8U2JT0420oMXmfe89cYmmQzT++yPDfDdJ1vE6sQ2n1/lQDh 1vJZ7pmQrWXy2KEY1VUmoWBGTR9+/eDx2AZFqIS2ZrN0nL8v7x8owjm0Nw+ge4bNaK+xabP5D1H 5vWG6a26S/vQ71SN6mMxlbgC2+/LlKwZExLWpLdWZn8QXGfVA= X-Received: by 2002:a17:903:2bcb:b0:2bc:8101:3393 with SMTP id d9443c01a7336-2bea32d0472mr8471235ad.1.1779333492397; Wed, 20 May 2026 20:18:12 -0700 (PDT) Received: from localhost.localdomain ([116.80.91.208]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2bd5cfe47bdsm241094735ad.44.2026.05.20.20.18.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 20:18:12 -0700 (PDT) From: Cunlong Li To: "Guanyou.Chen" Cc: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Andrew Morton , 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: Re: [PATCH] sched: restore timer_slack_ns when resetting RT policy on fork Date: Thu, 21 May 2026 11:18:06 +0800 Message-Id: X-Mailer: git-send-email 2.30.2 In-Reply-To: <20260521025250.1469390-1-chenguanyou@xiaomi.com> References: <20260521025250.1469390-1-chenguanyou@xiaomi.com> Mail-Followup-To: Cunlong Li , "Guanyou.Chen" , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Andrew Morton , 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 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=us-ascii Content-Disposition: inline Content-Transfer-Encoding: 8bit On Thu, May 21, 2026 at 10:52:50AM +0800, Guanyou.Chen wrote: > 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 > 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) = { > 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; Hi Guanyou, This changes behavior for normal (non-RT) tasks. If a process calls prctl(PR_SET_TIMERSLACK, 200000) and then forks, the child currently gets default_timer_slack_ns=200000 (the parent's effective slack). With this removal, the child would get default_timer_slack_ns=50000 (the parent's original default), so a subsequent PR_SET_TIMERSLACK(0) in the child would reset to a different value than before. I think the fix should be narrowed to only handle the RT parent case: if (rt_or_dl_task_policy(current)) p->default_timer_slack_ns = current->default_timer_slack_ns; else p->default_timer_slack_ns = current->timer_slack_ns; Thanks > - > #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 >