From nobody Sun Feb 8 05:27:48 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 2309B145348; Fri, 16 Jan 2026 14:52:27 +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=1768575148; cv=none; b=gBbltEhxNUnh/QXIUcyv5iSl5+5stoYKdLWmXx0l/FYtQTNwLZ2YfsZsEzRXzQM2xecDeSummsadxWFmcaVVt8d1Ao4qGxrusMAMK5igP2QjdVEsivHO91vjJbsFEqfVzt9uFT08QQdfCyskKKvOYyE5sLYwWdbzsi9jGAawsFE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575148; c=relaxed/simple; bh=AunS2qc13yjAkPCRwR4BY8llDOuJfk4lXWa5mN6Z7vE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VziwdvbvvLHOOxA7aEwl1ICsdLwwFt1aJNHMTsvjE/0x7jB1Gje6YFHqcybNk30sgCZwQRb1Bb/ojbJiV0qxVddDt4G24KieTomm9Juez6rVEKEERqWZja1e2SSkzdk6KsZHDhOSaU/E3KnTDLf95lGERm7PFMgqn1uA6COxqG0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BlqCwn2Y; 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="BlqCwn2Y" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4FDAC19421; Fri, 16 Jan 2026 14:52:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575147; bh=AunS2qc13yjAkPCRwR4BY8llDOuJfk4lXWa5mN6Z7vE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BlqCwn2YqqsP+baZapFQ2Pk8CvxIq+ZishCZjE6uTpwv+ovsptwhqtsPhBh7V+OIG 1urHhwFrOFkMLDXB62n/42kPaa8XEk9xkVQ3StO1dvAoIq7IOsSUJ7Z9gmTmn+A7Oq wOadWfFRYVWv3iyp8oPlNZYOMuOTa1J7Lg7efbMLvA1U1OOlJ2X9jlJF4Tnc6hCio2 BL593vlwI8MvVly+7Fx7wUKPtRWsClhbLa5u4jQy6KrBBMM91+cWXJ21NDC/Oq0LH7 7Fg/Gdw9RjA7uHoS/moX9agnj3h5E4Lys7jb3GLWmMqLdqnpfkgAbh37OZpnXG4SD7 j4qOYkZUJbLPA== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 01/15] sched/idle: Handle offlining first in idle loop Date: Fri, 16 Jan 2026 15:51:54 +0100 Message-ID: <20260116145208.87445-2-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" Offline handling happens from within the inner idle loop, after the beginning of dyntick cputime accounting, nohz idle load balancing and TIF_NEED_RESCHED polling. This is not necessary and even buggy because: * There is no dyntick handling to do. And calling tick_nohz_idle_enter() messes up with the struct tick_sched reset that was performed on tick_sched_timer_dying(). * There is no nohz idle balancing to do. * Polling on TIF_RESCHED is irrelevant at this stage, there are no more tasks allowed to run. * No need to check if need_resched() before offline handling since stop_machine is done and all per-cpu kthread should be done with their job. Therefore move the offline handling at the beginning of the idle loop. This will also ease the idle cputime unification later by not elapsing idle time while offline through the call to: tick_nohz_idle_enter() -> tick_nohz_start_idle() Signed-off-by: Frederic Weisbecker --- kernel/sched/idle.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c index c174afe1dd17..35d79af3286d 100644 --- a/kernel/sched/idle.c +++ b/kernel/sched/idle.c @@ -260,6 +260,12 @@ static void do_idle(void) { int cpu =3D smp_processor_id(); =20 + if (cpu_is_offline(cpu)) { + local_irq_disable(); + cpuhp_report_idle_dead(); + arch_cpu_idle_dead(); + } + /* * Check if we need to update blocked load */ @@ -311,11 +317,6 @@ static void do_idle(void) */ local_irq_disable(); =20 - if (cpu_is_offline(cpu)) { - cpuhp_report_idle_dead(); - arch_cpu_idle_dead(); - } - arch_cpu_idle_enter(); rcu_nocb_flush_deferred_wakeup(); =20 --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 3ABA8145348; Fri, 16 Jan 2026 14:52:34 +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=1768575155; cv=none; b=TSC0OarB+edWR/jUSXFV0944uQoBqDK4mPHZIiG6zf/l3BfpDAlkWiA5qiwioMVYgZZDRgfwrKXnC39HeQ6ozF+MvkGULZ/eSiWebwry5UYAuPuphnYiGqjHIOVLYpoVSMGidzU+pB6lj9RBA8dhYdV6te2OS65eyfe2xScF2n8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575155; c=relaxed/simple; bh=LzZmy6jqMt6NobQKkguWtqCpma0/ISXupISgS1lOgXU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TIqwGw8XvzRj5kXb/9GtEkrwAUnbPD+dEdoH+kWbntyLR2oKT3Kvqy+M2Eo6D8fKuveidpywHXXf4I/HRFwNMiBY6d7zT/NXPvJtmLk829dKX+5bQpvwA0YfouIBD2gytiu1UkP/Spv3G6n+EK+1s1jq7zZnmEbols2W00+p1SA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dwuikQdv; 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="dwuikQdv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 110B8C19423; Fri, 16 Jan 2026 14:52:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575154; bh=LzZmy6jqMt6NobQKkguWtqCpma0/ISXupISgS1lOgXU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dwuikQdv1p9BjGjXhU91yRg8Gw3GvSgQ0M5hzOyEUvqaUD15zFuY83kFOAlgDqtiL aq3Av8T2Mi2oEMJQB77oNtUUPugoJ7geLsaS0nsgJemsTZhW0YU6Gy+zpaXbizbI++ nJ4mgh32RcayhFdkr1DMDQB7/MzFNvmkoyAX8/lgU6JDEKdEh7sGOC/70hoxE4oSyF 7vtveo+2W7bYT1M/MT0l5IGEafZcX6e5/LLy3p/jPoWkP6CxNqQEYegvs8P+oSPr4J UAjET/LVrlea6oibw+ZGIMM81HmVRAborc7oWyiJm0nAXPvHp8IOheB0uD7EXGGxq4 i7oDCgTaceOrw== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 02/15] sched/cputime: Remove superfluous and error prone kcpustat_field() parameter Date: Fri, 16 Jan 2026 15:51:55 +0100 Message-ID: <20260116145208.87445-3-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" The first parameter to kcpustat_field() is a pointer to the cpu kcpustat to be fetched from. This parameter is error prone because a copy to a kcpustat could be passed by accident instead of the original one. Also the kcpustat structure can already be retrieved with the help of the mandatory CPU argument. Remove the needless paramater. Signed-off-by: Frederic Weisbecker --- drivers/cpufreq/cpufreq_governor.c | 6 +++--- drivers/macintosh/rack-meter.c | 2 +- include/linux/kernel_stat.h | 8 +++----- kernel/rcu/tree.c | 9 +++------ kernel/rcu/tree_stall.h | 7 +++---- kernel/sched/cputime.c | 5 ++--- 6 files changed, 15 insertions(+), 22 deletions(-) diff --git a/drivers/cpufreq/cpufreq_governor.c b/drivers/cpufreq/cpufreq_g= overnor.c index 1a7fcaf39cc9..b6683628091d 100644 --- a/drivers/cpufreq/cpufreq_governor.c +++ b/drivers/cpufreq/cpufreq_governor.c @@ -105,7 +105,7 @@ void gov_update_cpu_data(struct dbs_data *dbs_data) j_cdbs->prev_cpu_idle =3D get_cpu_idle_time(j, &j_cdbs->prev_update_tim= e, dbs_data->io_is_busy); if (dbs_data->ignore_nice_load) - j_cdbs->prev_cpu_nice =3D kcpustat_field(&kcpustat_cpu(j), CPUTIME_NIC= E, j); + j_cdbs->prev_cpu_nice =3D kcpustat_field(CPUTIME_NICE, j); } } } @@ -165,7 +165,7 @@ unsigned int dbs_update(struct cpufreq_policy *policy) j_cdbs->prev_cpu_idle =3D cur_idle_time; =20 if (ignore_nice) { - u64 cur_nice =3D kcpustat_field(&kcpustat_cpu(j), CPUTIME_NICE, j); + u64 cur_nice =3D kcpustat_field(CPUTIME_NICE, j); =20 idle_time +=3D div_u64(cur_nice - j_cdbs->prev_cpu_nice, NSEC_PER_USEC); j_cdbs->prev_cpu_nice =3D cur_nice; @@ -539,7 +539,7 @@ int cpufreq_dbs_governor_start(struct cpufreq_policy *p= olicy) j_cdbs->prev_load =3D 0; =20 if (ignore_nice) - j_cdbs->prev_cpu_nice =3D kcpustat_field(&kcpustat_cpu(j), CPUTIME_NICE= , j); + j_cdbs->prev_cpu_nice =3D kcpustat_field(CPUTIME_NICE, j); } =20 gov->start(policy); diff --git a/drivers/macintosh/rack-meter.c b/drivers/macintosh/rack-meter.c index 896a43bd819f..20b2ecd32340 100644 --- a/drivers/macintosh/rack-meter.c +++ b/drivers/macintosh/rack-meter.c @@ -87,7 +87,7 @@ static inline u64 get_cpu_idle_time(unsigned int cpu) kcpustat->cpustat[CPUTIME_IOWAIT]; =20 if (rackmeter_ignore_nice) - retval +=3D kcpustat_field(kcpustat, CPUTIME_NICE, cpu); + retval +=3D kcpustat_field(CPUTIME_NICE, cpu); =20 return retval; } diff --git a/include/linux/kernel_stat.h b/include/linux/kernel_stat.h index b97ce2df376f..dd020ecaf67b 100644 --- a/include/linux/kernel_stat.h +++ b/include/linux/kernel_stat.h @@ -100,14 +100,12 @@ static inline unsigned long kstat_cpu_irqs_sum(unsign= ed int cpu) } =20 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN -extern u64 kcpustat_field(struct kernel_cpustat *kcpustat, - enum cpu_usage_stat usage, int cpu); +extern u64 kcpustat_field(enum cpu_usage_stat usage, int cpu); extern void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu); #else -static inline u64 kcpustat_field(struct kernel_cpustat *kcpustat, - enum cpu_usage_stat usage, int cpu) +static inline u64 kcpustat_field(enum cpu_usage_stat usage, int cpu) { - return kcpustat->cpustat[usage]; + return kcpustat_cpu(cpu).cpustat[usage]; } =20 static inline void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu) diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index 293bbd9ac3f4..ceea4b2f755b 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -968,14 +968,11 @@ static int rcu_watching_snap_recheck(struct rcu_data = *rdp) if (rcu_cpu_stall_cputime && rdp->snap_record.gp_seq !=3D rdp->gp_seq) { int cpu =3D rdp->cpu; struct rcu_snap_record *rsrp; - struct kernel_cpustat *kcsp; - - kcsp =3D &kcpustat_cpu(cpu); =20 rsrp =3D &rdp->snap_record; - rsrp->cputime_irq =3D kcpustat_field(kcsp, CPUTIME_IRQ, cpu); - rsrp->cputime_softirq =3D kcpustat_field(kcsp, CPUTIME_SOFTIRQ, cpu); - rsrp->cputime_system =3D kcpustat_field(kcsp, CPUTIME_SYSTEM, cpu); + rsrp->cputime_irq =3D kcpustat_field(CPUTIME_IRQ, cpu); + rsrp->cputime_softirq =3D kcpustat_field(CPUTIME_SOFTIRQ, cpu); + rsrp->cputime_system =3D kcpustat_field(CPUTIME_SYSTEM, cpu); rsrp->nr_hardirqs =3D kstat_cpu_irqs_sum(cpu) + arch_irq_stat_cpu(cpu); rsrp->nr_softirqs =3D kstat_cpu_softirqs_sum(cpu); rsrp->nr_csw =3D nr_context_switches_cpu(cpu); diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h index b67532cb8770..cf7ae51cba40 100644 --- a/kernel/rcu/tree_stall.h +++ b/kernel/rcu/tree_stall.h @@ -479,7 +479,6 @@ static void print_cpu_stat_info(int cpu) { struct rcu_snap_record rsr, *rsrp; struct rcu_data *rdp =3D per_cpu_ptr(&rcu_data, cpu); - struct kernel_cpustat *kcsp =3D &kcpustat_cpu(cpu); =20 if (!rcu_cpu_stall_cputime) return; @@ -488,9 +487,9 @@ static void print_cpu_stat_info(int cpu) if (rsrp->gp_seq !=3D rdp->gp_seq) return; =20 - rsr.cputime_irq =3D kcpustat_field(kcsp, CPUTIME_IRQ, cpu); - rsr.cputime_softirq =3D kcpustat_field(kcsp, CPUTIME_SOFTIRQ, cpu); - rsr.cputime_system =3D kcpustat_field(kcsp, CPUTIME_SYSTEM, cpu); + rsr.cputime_irq =3D kcpustat_field(CPUTIME_IRQ, cpu); + rsr.cputime_softirq =3D kcpustat_field(CPUTIME_SOFTIRQ, cpu); + rsr.cputime_system =3D kcpustat_field(CPUTIME_SYSTEM, cpu); =20 pr_err("\t hardirqs softirqs csw/system\n"); pr_err("\t number: %8lld %10d %12lld\n", diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index 4f97896887ec..5dcb0f2e01bc 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -961,10 +961,9 @@ static int kcpustat_field_vtime(u64 *cpustat, return 0; } =20 -u64 kcpustat_field(struct kernel_cpustat *kcpustat, - enum cpu_usage_stat usage, int cpu) +u64 kcpustat_field(enum cpu_usage_stat usage, int cpu) { - u64 *cpustat =3D kcpustat->cpustat; + u64 *cpustat =3D kcpustat_cpu(cpu).cpustat; u64 val =3D cpustat[usage]; struct rq *rq; int err; --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 86CB5221DAD; Fri, 16 Jan 2026 14:52:42 +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=1768575162; cv=none; b=pAdYmHHOv4SzK6edFGNoygFcq2fO/neDNMC9F31FTUUoLyHDYUscQvx7lZgBtGRZXe7DlF7wSmZWVoUz8DCk27/xZb0nhMsv+d1wTO175NU9RPCBIF8Max+bj4LNiK3qNV+P2rW8Hh6xTRiqyf6VOT3YmWxQmoJ1+/ntk4j6ad0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575162; c=relaxed/simple; bh=KdTiRwNqvvTUCK29uJFVf97xOZdvdCR4xQadD5dd7Pc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O1C75L3p++jD9hUxULtRWXVVqadq/6d8BSE9IZuvGE8Qmo1D3M9+6vAp9GU/3uGMyt+83CPf8RdVQ819QayvqDOtuQ/hw9J27n/vkRENyQpXoYlsZkwepZCBdgD4PL49aMo3An5+S80D6KqKPxclD8CTOJSTkC2CCLPcRfDo+ds= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Adomgxsk; 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="Adomgxsk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 43D42C116C6; Fri, 16 Jan 2026 14:52:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575162; bh=KdTiRwNqvvTUCK29uJFVf97xOZdvdCR4xQadD5dd7Pc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AdomgxskzkU+sLSsmMYNhuPB6+HiDEHNe3Wx7a5u+FWDJg+MoxaTG491VnRfG9PM4 VwbMgd8WvqQBs+1XvJXxvoeBnrlgLJX4ddHNATB2nvc3bzW1P5fZH7TMd9PD7Zs0Ob iw9aQxCHGZMljcG1K9g1yIum4xwa4jXkLhrpGfUlviKobG9w5c/9QaA/VOuC7caFVD RiZ6VtCj1xk7+4MUVfNWd+NZJjGWbi5mjVOvnMERLr0gpI+C/ckt1g0ZJba6j9ssLh tpFq4s92Hngh5oZAKD5TmBIvJs71ct71mj7+/GNtJhxxEn6446EjsuepigZH1TKZnW 07Ocp25nPF4Mw== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 03/15] sched/cputime: Correctly support generic vtime idle time Date: Fri, 16 Jan 2026 15:51:56 +0100 Message-ID: <20260116145208.87445-4-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" Currently whether generic vtime is running or not, the idle cputime is fetched from the nohz accounting. However generic vtime already does its own idle cputime accounting. Only the kernel stat accessors are not plugged to support it. Read the idle generic vtime cputime when it's running, this will allow to later more clearly split nohz and vtime cputime accounting. Signed-off-by: Frederic Weisbecker --- fs/proc/stat.c | 8 ++++---- include/linux/vtime.h | 7 ++++++- kernel/sched/cputime.c | 38 +++++++++++++++++++++++++++++++------- kernel/time/tick-sched.c | 2 +- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/fs/proc/stat.c b/fs/proc/stat.c index 8b444e862319..6ac2a13b8be5 100644 --- a/fs/proc/stat.c +++ b/fs/proc/stat.c @@ -30,8 +30,8 @@ u64 get_idle_time(struct kernel_cpustat *kcs, int cpu) idle_usecs =3D get_cpu_idle_time_us(cpu, NULL); =20 if (idle_usecs =3D=3D -1ULL) - /* !NO_HZ or cpu offline so we can rely on cpustat.idle */ - idle =3D kcs->cpustat[CPUTIME_IDLE]; + /* !NO_HZ or cpu offline or vtime so we can rely on cpustat.idle */ + idle =3D kcpustat_field(CPUTIME_IDLE, cpu); else idle =3D idle_usecs * NSEC_PER_USEC; =20 @@ -46,8 +46,8 @@ static u64 get_iowait_time(struct kernel_cpustat *kcs, in= t cpu) iowait_usecs =3D get_cpu_iowait_time_us(cpu, NULL); =20 if (iowait_usecs =3D=3D -1ULL) - /* !NO_HZ or cpu offline so we can rely on cpustat.iowait */ - iowait =3D kcs->cpustat[CPUTIME_IOWAIT]; + /* !NO_HZ or cpu offline or vtime so we can rely on cpustat.iowait */ + iowait =3D kcpustat_field(CPUTIME_IOWAIT, cpu); else iowait =3D iowait_usecs * NSEC_PER_USEC; =20 diff --git a/include/linux/vtime.h b/include/linux/vtime.h index 29dd5b91dd7d..737930f66c3e 100644 --- a/include/linux/vtime.h +++ b/include/linux/vtime.h @@ -27,6 +27,11 @@ static inline void vtime_guest_exit(struct task_struct *= tsk) { } static inline void vtime_init_idle(struct task_struct *tsk, int cpu) { } #endif =20 +static inline bool vtime_generic_enabled_cpu(int cpu) +{ + return context_tracking_enabled_cpu(cpu); +} + #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE extern void vtime_account_irq(struct task_struct *tsk, unsigned int offset= ); extern void vtime_account_softirq(struct task_struct *tsk); @@ -74,7 +79,7 @@ static inline bool vtime_accounting_enabled(void) =20 static inline bool vtime_accounting_enabled_cpu(int cpu) { - return context_tracking_enabled_cpu(cpu); + return vtime_generic_enabled_cpu(cpu); } =20 static inline bool vtime_accounting_enabled_this_cpu(void) diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index 5dcb0f2e01bc..f32c169da11a 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -761,7 +761,11 @@ EXPORT_SYMBOL_GPL(vtime_guest_exit); =20 void vtime_account_idle(struct task_struct *tsk) { - account_idle_time(get_vtime_delta(&tsk->vtime)); + struct vtime *vtime =3D &tsk->vtime; + + write_seqcount_begin(&vtime->seqcount); + account_idle_time(get_vtime_delta(vtime)); + write_seqcount_end(&vtime->seqcount); } =20 void vtime_task_switch_generic(struct task_struct *prev) @@ -912,6 +916,7 @@ static int kcpustat_field_vtime(u64 *cpustat, int cpu, u64 *val) { struct vtime *vtime =3D &tsk->vtime; + struct rq *rq =3D cpu_rq(cpu); unsigned int seq; =20 do { @@ -953,6 +958,14 @@ static int kcpustat_field_vtime(u64 *cpustat, if (state =3D=3D VTIME_GUEST && task_nice(tsk) > 0) *val +=3D vtime->gtime + vtime_delta(vtime); break; + case CPUTIME_IDLE: + if (state =3D=3D VTIME_IDLE && !atomic_read(&rq->nr_iowait)) + *val +=3D vtime_delta(vtime); + break; + case CPUTIME_IOWAIT: + if (state =3D=3D VTIME_IDLE && atomic_read(&rq->nr_iowait) > 0) + *val +=3D vtime_delta(vtime); + break; default: break; } @@ -1015,8 +1028,8 @@ static int kcpustat_cpu_fetch_vtime(struct kernel_cpu= stat *dst, *dst =3D *src; cpustat =3D dst->cpustat; =20 - /* Task is sleeping, dead or idle, nothing to add */ - if (state < VTIME_SYS) + /* Task is sleeping or dead, nothing to add */ + if (state < VTIME_IDLE) continue; =20 delta =3D vtime_delta(vtime); @@ -1025,15 +1038,17 @@ static int kcpustat_cpu_fetch_vtime(struct kernel_c= pustat *dst, * Task runs either in user (including guest) or kernel space, * add pending nohz time to the right place. */ - if (state =3D=3D VTIME_SYS) { + switch (vtime->state) { + case VTIME_SYS: cpustat[CPUTIME_SYSTEM] +=3D vtime->stime + delta; - } else if (state =3D=3D VTIME_USER) { + break; + case VTIME_USER: if (task_nice(tsk) > 0) cpustat[CPUTIME_NICE] +=3D vtime->utime + delta; else cpustat[CPUTIME_USER] +=3D vtime->utime + delta; - } else { - WARN_ON_ONCE(state !=3D VTIME_GUEST); + break; + case VTIME_GUEST: if (task_nice(tsk) > 0) { cpustat[CPUTIME_GUEST_NICE] +=3D vtime->gtime + delta; cpustat[CPUTIME_NICE] +=3D vtime->gtime + delta; @@ -1041,6 +1056,15 @@ static int kcpustat_cpu_fetch_vtime(struct kernel_cp= ustat *dst, cpustat[CPUTIME_GUEST] +=3D vtime->gtime + delta; cpustat[CPUTIME_USER] +=3D vtime->gtime + delta; } + break; + case VTIME_IDLE: + if (atomic_read(&cpu_rq(cpu)->nr_iowait) > 0) + cpustat[CPUTIME_IOWAIT] +=3D delta; + else + cpustat[CPUTIME_IDLE] +=3D delta; + break; + default: + WARN_ON_ONCE(1); } } while (read_seqcount_retry(&vtime->seqcount, seq)); =20 diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index 8ddf74e705d3..f1d07a0276a5 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -780,7 +780,7 @@ static u64 get_cpu_sleep_time_us(struct tick_sched *ts,= ktime_t *sleeptime, ktime_t now, idle; unsigned int seq; =20 - if (!tick_nohz_active) + if (!tick_nohz_active || vtime_generic_enabled_cpu(cpu)) return -1; =20 now =3D ktime_get(); --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 09E0323D2B2; Fri, 16 Jan 2026 14:52:49 +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=1768575170; cv=none; b=iyonIydXSJdi0/dwst94OINOnpiFuyZbL08SkC8/Ok+y3eXeic9dNyYWY8C3LUlU/LRSEk9h7xAYElPYnJYueUyvrGPO0J/1NrE+xdB4hME7tyxiL5M94J8H5LIGWoT8JhPHGCRVrPP4NhRYP3TuI1B1MXbvx0Zhw2OGdf+etKA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575170; c=relaxed/simple; bh=vTAmht3VvCxwglnCR59QaOOlh1TAQz905bg6fnPgSy8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Au8HTO7EqnibuZrS30Ch3EBvh+0GnrSW2bLlvnRj0VYbPHt8Cz/7+1PlvweRd0A+C7afe9GwVSiwUV/uAzxLbLV7xCYs+FngrZGHftvPw9WfsCsDEepyBU4jSNuORLWDIq5oR/I7MroFt6Jy+/LJktTNWSfTDnurb53uRSHGDPo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UEoSi17t; 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="UEoSi17t" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0DE8C16AAE; Fri, 16 Jan 2026 14:52:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575169; bh=vTAmht3VvCxwglnCR59QaOOlh1TAQz905bg6fnPgSy8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UEoSi17t6WTlsc/9elJnbU5m2di5zQiFmaaEPydlxspyxVW8BJOvt2Px/1Q6krNYH 4p1ylHoqFM3zx3CX7/IHivpHfYGcmYHwb+auopjehxccS5rNt652IzTP2EJSdg2lJ1 xB568wT6GQPtqB716s6yLv8x+3ARbS6XrjSSYRzTUPQAN7+QvN2sQlW278sAbR3EqS BwDebrYlVQYnko+lQJGextI/w9bjBkkC5ED6q17ErxBNQWZn0hXmTr8im+4Q1PpOkM +HVFdPOXhG3ONyMe5zE3VuNlN36AK/qBTkGFXubevPai7RrYy3pms3o+EjKvAKuq93 +9q1kn/BXq43A== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 04/15] powerpc/time: Prepare to stop elapsing in dynticks-idle Date: Fri, 16 Jan 2026 15:51:57 +0100 Message-ID: <20260116145208.87445-5-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" Currently the tick subsystem stores the idle cputime accounting in private fields, allowing cohabitation with architecture idle vtime accounting. The former is fetched on online CPUs, the latter on offline CPUs. For consolidation purpose, architecture vtime accounting will continue to account the cputime but will make a break when the idle tick is stopped. The dyntick cputime accounting will then be relayed by the tick subsystem so that the idle cputime is still seen advancing coherently even when the tick isn't there to flush the idle vtime. Prepare for that and introduce three new APIs which will be used in subsequent patches: _ vtime_dynticks_start() is deemed to be called when idle enters in dyntick mode. The idle cputime that elapsed so far is accumulated. - vtime_dynticks_stop() is deemed to be called when idle exits from dyntick mode. The vtime entry clocks are fast-forward to current time so that idle accounting restarts elapsing from now. - vtime_reset() is deemed to be called from dynticks idle IRQ entry to fast-forward the clock to current time so that the IRQ time is still accounted by vtime while nohz cputime is paused. Also accumulated vtime won't be flushed from dyntick-idle ticks to avoid accounting twice the idle cputime, along with nohz accounting. Signed-off-by: Frederic Weisbecker --- arch/powerpc/kernel/time.c | 41 ++++++++++++++++++++++++++++++++++++++ include/linux/vtime.h | 6 ++++++ 2 files changed, 47 insertions(+) diff --git a/arch/powerpc/kernel/time.c b/arch/powerpc/kernel/time.c index 4bbeb8644d3d..9b3167274653 100644 --- a/arch/powerpc/kernel/time.c +++ b/arch/powerpc/kernel/time.c @@ -376,6 +376,47 @@ void vtime_task_switch(struct task_struct *prev) acct->starttime =3D acct0->starttime; } } + +#ifdef CONFIG_NO_HZ_COMMON +/** + * vtime_reset - Fast forward vtime entry clocks + * + * Called from dynticks idle IRQ entry to fast-forward the clocks to curre= nt time + * so that the IRQ time is still accounted by vtime while nohz cputime is = paused. + */ +void vtime_reset(void) +{ + struct cpu_accounting_data *acct =3D get_accounting(current); + + acct->starttime =3D mftb(); +#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME + acct->startspurr =3D read_spurr(now); +#endif +} + +/** + * vtime_dyntick_start - Inform vtime about entry to idle-dynticks + * + * Called when idle enters in dyntick mode. The idle cputime that elapsed = so far + * is accumulated and the tick subsystem takes over the idle cputime accou= nting. + */ +void vtime_dyntick_start(void) +{ + vtime_account_idle(current); +} + +/** + * vtime_dyntick_stop - Inform vtime about exit from idle-dynticks + * + * Called when idle exits from dyntick mode. The vtime entry clocks are + * fast-forward to current time so that idle accounting restarts elapsing = from + * now. + */ +void vtime_dyntick_stop(void) +{ + vtime_reset(); +} +#endif /* CONFIG_NO_HZ_COMMON */ #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ =20 void __no_kcsan __delay(unsigned long loops) diff --git a/include/linux/vtime.h b/include/linux/vtime.h index 737930f66c3e..10cdb08f960b 100644 --- a/include/linux/vtime.h +++ b/include/linux/vtime.h @@ -37,11 +37,17 @@ extern void vtime_account_irq(struct task_struct *tsk, = unsigned int offset); extern void vtime_account_softirq(struct task_struct *tsk); extern void vtime_account_hardirq(struct task_struct *tsk); extern void vtime_flush(struct task_struct *tsk); +extern void vtime_reset(void); +extern void vtime_dyntick_start(void); +extern void vtime_dyntick_stop(void); #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ static inline void vtime_account_irq(struct task_struct *tsk, unsigned int= offset) { } static inline void vtime_account_softirq(struct task_struct *tsk) { } static inline void vtime_account_hardirq(struct task_struct *tsk) { } static inline void vtime_flush(struct task_struct *tsk) { } +static inline void vtime_reset(void) { } +static inline void vtime_dyntick_start(void) { } +extern inline void vtime_dyntick_stop(void) { } #endif =20 /* --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 6BE453358D4; Fri, 16 Jan 2026 14:52:57 +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=1768575177; cv=none; b=Jr0p1disjRWgNW+ex9X6ba0NIDnP2Z/DnIoCaRy8Iwkfqb0z00tTTVx2kY3PicVhFlt3U/0sDoCA8iVxWF1ue3s2bb/Lzocjim94qawa7FvdjFiW2EJbj00L4F2OK1KkntvBJfc2nAzl6hygdIE1Vde3d8ctj0z0RYbMvxJU3Zs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575177; c=relaxed/simple; bh=vvbridxIQso7KWjeK226R2O4tHRCA31JE5Z2K7DDlx0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fte0X0qm54Th/gY5cv+hD81PexSlUsVgYTKnq/YI5slntG20UdWob/CLff0AMXA0FrXQWD22yfRKxOKoigAe+H0kqEDD+1qchptGyWUuwJKCUxvS6a1FkBSoUDl8KCOvqhu74olcVfMAAL4YjaxvlUu7ODgljfz/FKFVSlFfifo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lXLhbUJp; 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="lXLhbUJp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0D6E9C19421; Fri, 16 Jan 2026 14:52:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575177; bh=vvbridxIQso7KWjeK226R2O4tHRCA31JE5Z2K7DDlx0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lXLhbUJpSe2jZoljJhKsDC27GLZ/Nje4Dfdzt+9e5A8Qe8HbJQL7AtTSlBBvtQxYD JEoEUArdN7c3biSjO8pjEpKLgy7I+x7b5EXJ363hnEJgvBoZ7tBYEZi9O1iG8ihbmx OiKewfemzThz7ZiIqZtX1yp/OwINnAHXAkawnJCl5Uv7QUVyhWRkLIvuoXNMyOqUtp Wacczq3p1j2eXAzBRjlloY9pra1JLen6QZ8H5kz+I+b/GguE0H5BY85+SBDFweFt9k IrF8ut5IrXm87oPBfxgY3ztrHxHfFSNiUltGY0wE56dwU1pbhAy5XE2ybMtWzgE/yJ 7QQq0bJS/Kv5g== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 05/15] s390/time: Prepare to stop elapsing in dynticks-idle Date: Fri, 16 Jan 2026 15:51:58 +0100 Message-ID: <20260116145208.87445-6-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" Currently the tick subsystem stores the idle cputime accounting in private fields, allowing cohabitation with architecture idle vtime accounting. The former is fetched on online CPUs, the latter on offline CPUs. For consolidation purpose, architecture vtime accounting will continue to account the cputime but will make a break when the idle tick is stopped. The dyntick cputime accounting will then be relayed by the tick subsystem so that the idle cputime is still seen advancing coherently even when the tick isn't there to flush the idle vtime. Prepare for that and introduce three new APIs which will be used in subsequent patches: _ vtime_dynticks_start() is deemed to be called when idle enters in dyntick mode. The idle cputime that elapsed so far is accumulated and accounted. Also idle time accounting is ignored. - vtime_dynticks_stop() is deemed to be called when idle exits from dyntick mode. The vtime entry clocks are fast-forward to current time so that idle accounting restarts elapsing from now. Also idle time accounting is resumed. - vtime_reset() is deemed to be called from dynticks idle IRQ entry to fast-forward the clock to current time so that the IRQ time is still accounted by vtime while nohz cputime is paused. Also accumulated vtime won't be flushed from dyntick-idle ticks to avoid accounting twice the idle cputime, along with nohz accounting. Signed-off-by: Frederic Weisbecker --- arch/s390/include/asm/idle.h | 11 +++--- arch/s390/kernel/idle.c | 13 ++++++-- arch/s390/kernel/vtime.c | 65 ++++++++++++++++++++++++++++++------ 3 files changed, 71 insertions(+), 18 deletions(-) diff --git a/arch/s390/include/asm/idle.h b/arch/s390/include/asm/idle.h index 09f763b9eb40..2770c4f761e1 100644 --- a/arch/s390/include/asm/idle.h +++ b/arch/s390/include/asm/idle.h @@ -12,11 +12,12 @@ #include =20 struct s390_idle_data { - unsigned long idle_count; - unsigned long idle_time; - unsigned long clock_idle_enter; - unsigned long timer_idle_enter; - unsigned long mt_cycles_enter[8]; + bool idle_dyntick; + unsigned long idle_count; + unsigned long idle_time; + unsigned long clock_idle_enter; + unsigned long timer_idle_enter; + unsigned long mt_cycles_enter[8]; }; =20 extern struct device_attribute dev_attr_idle_count; diff --git a/arch/s390/kernel/idle.c b/arch/s390/kernel/idle.c index 39cb8d0ae348..54bb932184dd 100644 --- a/arch/s390/kernel/idle.c +++ b/arch/s390/kernel/idle.c @@ -35,6 +35,12 @@ void account_idle_time_irq(void) this_cpu_add(mt_cycles[i], cycles_new[i] - idle->mt_cycles_enter[i]); } =20 + WRITE_ONCE(idle->idle_count, READ_ONCE(idle->idle_count) + 1); + + /* Dyntick idle time accounted by nohz/scheduler */ + if (idle->idle_dyntick) + return; + idle_time =3D lc->int_clock - idle->clock_idle_enter; =20 lc->steal_timer +=3D idle->clock_idle_enter - lc->last_update_clock; @@ -45,7 +51,6 @@ void account_idle_time_irq(void) =20 /* Account time spent with enabled wait psw loaded as idle time. */ WRITE_ONCE(idle->idle_time, READ_ONCE(idle->idle_time) + idle_time); - WRITE_ONCE(idle->idle_count, READ_ONCE(idle->idle_count) + 1); account_idle_time(cputime_to_nsecs(idle_time)); } =20 @@ -61,8 +66,10 @@ void noinstr arch_cpu_idle(void) set_cpu_flag(CIF_ENABLED_WAIT); if (smp_cpu_mtid) stcctm(MT_DIAG, smp_cpu_mtid, (u64 *)&idle->mt_cycles_enter); - idle->clock_idle_enter =3D get_tod_clock_fast(); - idle->timer_idle_enter =3D get_cpu_timer(); + if (!idle->idle_dyntick) { + idle->clock_idle_enter =3D get_tod_clock_fast(); + idle->timer_idle_enter =3D get_cpu_timer(); + } bpon(); __load_psw_mask(psw_mask); } diff --git a/arch/s390/kernel/vtime.c b/arch/s390/kernel/vtime.c index 234a0ba30510..c19528eb4ee3 100644 --- a/arch/s390/kernel/vtime.c +++ b/arch/s390/kernel/vtime.c @@ -17,6 +17,7 @@ #include #include #include +#include #include =20 #include "entry.h" @@ -111,23 +112,30 @@ static void account_system_index_scaled(struct task_s= truct *p, u64 cputime, account_system_index_time(p, cputime_to_nsecs(cputime), index); } =20 -/* - * Update process times based on virtual cpu times stored by entry.S - * to the lowcore fields user_timer, system_timer & steal_clock. - */ -static int do_account_vtime(struct task_struct *tsk) +static inline void vtime_reset_last_update(struct lowcore *lc) { - u64 timer, clock, user, guest, system, hardirq, softirq; - struct lowcore *lc =3D get_lowcore(); - - timer =3D lc->last_update_timer; - clock =3D lc->last_update_clock; asm volatile( " stpt %0\n" /* Store current cpu timer value */ " stckf %1" /* Store current tod clock value */ : "=3DQ" (lc->last_update_timer), "=3DQ" (lc->last_update_clock) : : "cc"); +} + +/* + * Update process times based on virtual cpu times stored by entry.S + * to the lowcore fields user_timer, system_timer & steal_clock. + */ +static int do_account_vtime(struct task_struct *tsk) +{ + u64 timer, clock, user, guest, system, hardirq, softirq; + struct lowcore *lc =3D get_lowcore(); + + timer =3D lc->last_update_timer; + clock =3D lc->last_update_clock; + + vtime_reset_last_update(lc); + clock =3D lc->last_update_clock - clock; timer -=3D lc->last_update_timer; =20 @@ -261,6 +269,43 @@ void vtime_account_hardirq(struct task_struct *tsk) virt_timer_forward(delta); } =20 +#ifdef CONFIG_NO_HZ_COMMON +/** + * vtime_reset - Fast forward vtime entry clocks + * + * Called from dynticks idle IRQ entry to fast-forward the clocks to curre= nt time + * so that the IRQ time is still accounted by vtime while nohz cputime is = paused. + */ +void vtime_reset(void) +{ + vtime_reset_last_update(get_lowcore()); +} + +/** + * vtime_dyntick_start - Inform vtime about entry to idle-dynticks + * + * Called when idle enters in dyntick mode. The idle cputime that elapsed = so far + * is flushed and the tick subsystem takes over the idle cputime accountin= g. + */ +void vtime_dyntick_start(void) +{ + __this_cpu_write(s390_idle.idle_dyntick, true); + vtime_flush(current); +} + +/** + * vtime_dyntick_stop - Inform vtime about exit from idle-dynticks + * + * Called when idle exits from dyntick mode. The vtime entry clocks are + * fast-forward to current time and idle accounting resumes. + */ +void vtime_dyntick_stop(void) +{ + vtime_reset_last_update(get_lowcore()); + __this_cpu_write(s390_idle.idle_dyntick, false); +} +#endif /* CONFIG_NO_HZ_COMMON */ + /* * Sorted add to a list. List is linear searched until first bigger * element is found. --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 BFAA1335BD5; Fri, 16 Jan 2026 14:53:04 +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=1768575184; cv=none; b=iZN0ae0hXQzGQ4m1aUHXsXbxr5NWHhMgGRWt8wgVQxHFN8Yr/SwzXXjHr8Xpv9CljFRUQldqjpHADtKIvavpZFvi6cuEuoXbLpqdb7BuxByn0KVi5kPKh0qpVr5venNLLNTWYwh4ivbIIZaSP8ObtJxUpwC92jqARJKQzgJ+liw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575184; c=relaxed/simple; bh=oZRzhhrgyUs9gVhop4a/9/b2H0iyMtqMK1t/RPXY/I0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q/BYyT+CMBNLerGkaTo/WLf4w/SjRAVCHj1307VMgPxMdfvpOTHY0i40cmbiIwwretqFlFAiWwffYnqrrppnzZNJpNxxmAV2C3MQjm0NsVBiwSTP0IgFF63vvG7FQ9f8/FT+wVkdKzRcjn/cm6xiwFIDHHIdcutzxL0KoNOdBBY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cG3k2Lez; 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="cG3k2Lez" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 71FAFC16AAE; Fri, 16 Jan 2026 14:52:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575184; bh=oZRzhhrgyUs9gVhop4a/9/b2H0iyMtqMK1t/RPXY/I0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cG3k2LezNgpd5y2yIZznyAvfdol63MOQZGmSmXzeQ7c+Xs6tEjjqbBhOKO4MYDwG2 NSGIeHiQ6LAf2oOiSIC0SvffNzeX9ZAwt/GQ3A6eRccdU2WzZiuUszqGQSAHERu3Xt qoGqQDhm7ya0VGwPazAn8WHLTy2pohIxJavHDpobIyJQd9uy9/Y19rTAK9AIm/lqZn 2TSjnDWinamgegl2VmUHXLOXKPmRxtEntuSwemfSFl+LuGXGy5rEPOjin8egj0bO1o 9hkkYl41RJ7TJaBkVubemkstw1rYTYTUz/mTmvldK85iTcuLxk3pYaNakbS2N1tz5t Uyg+i4Z1H9o6w== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 06/15] tick/sched: Unify idle cputime accounting Date: Fri, 16 Jan 2026 15:51:59 +0100 Message-ID: <20260116145208.87445-7-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" The non-vtime dynticks-idle cputime accounting is a big mess that accumulates within two concurrent statistics, each having their own shortcomings: * The accounting for online CPUs which is based on the delta between tick_nohz_start_idle() and tick_nohz_stop_idle(). Pros: - Works when the tick is off - Has nsecs granularity Cons: - Account idle steal time but doesn't substract it from idle cputime. - Assumes CONFIG_IRQ_TIME_ACCOUNTING by not accounting IRQs but the IRQ time is simply ignored when CONFIG_IRQ_TIME_ACCOUNTING=3Dn - The windows between 1) idle task scheduling and the first call to tick_nohz_start_idle() and 2) idle task between the last tick_nohz_stop_idle() and the rest of the idle time are blindspots wrt. cputime accounting (though mostly insignificant amount) - Relies on private fields outside of kernel stats, with specific accessors. * The accounting for offline CPUs which is based on ticks and the jiffies delta during which the tick was stopped. Pros: - Handles steal time correctly - Handle CONFIG_IRQ_TIME_ACCOUNTING=3Dy and CONFIG_IRQ_TIME_ACCOUNTING=3Dn correctly. - Handles the whole idle task - Accounts directly to kernel stats, without midlayer accumulator. Cons: - Doesn't elapse when the tick is off, which doesn't make it suitable for online CPUs. - Has TICK_NSEC granularity (jiffies) - Needs to track the dyntick-idle ticks that were accounted and substract them from the total jiffies time spent while the tick was stopped. This is an ugly workaround. Having two different accounting for a single context is not the only problem: since those accountings are of different natures, it is possible to observe the global idle time going backward after a CPU goes offline. Clean up the situation with introducing a hybrid approach that stays coherent and works for both online and offline CPUs: * Tick based or native vtime accounting operate before the idle loop is entered and resume once the idle loop prepares to exit. * When the idle loop starts, switch to dynticks-idle accounting as is done currently, except that the statistics accumulate directly to the relevant kernel stat fields. * Private dyntick cputime accounting fields are removed. * Works on both online and offline case. Further improvement will include: * Only switch to dynticks-idle cputime accounting when the tick actually goes in dynticks mode. * Handle CONFIG_IRQ_TIME_ACCOUNTING=3Dn correctly such that the dynticks-idle accounting still elapses while on IRQs. * Correctly substract idle steal cputime from idle time Reported-by: Xin Zhao Signed-off-by: Frederic Weisbecker --- include/linux/kernel_stat.h | 24 ++++++++++--- include/linux/vtime.h | 7 +++- kernel/sched/cputime.c | 62 ++++++++++++++++---------------- kernel/time/tick-sched.c | 72 +++++++++++-------------------------- 4 files changed, 77 insertions(+), 88 deletions(-) diff --git a/include/linux/kernel_stat.h b/include/linux/kernel_stat.h index dd020ecaf67b..ba65aad308a1 100644 --- a/include/linux/kernel_stat.h +++ b/include/linux/kernel_stat.h @@ -34,6 +34,9 @@ enum cpu_usage_stat { }; =20 struct kernel_cpustat { +#ifdef CONFIG_NO_HZ_COMMON + int idle_dyntick; +#endif u64 cpustat[NR_STATS]; }; =20 @@ -99,6 +102,20 @@ static inline unsigned long kstat_cpu_irqs_sum(unsigned= int cpu) return kstat_cpu(cpu).irqs_sum; } =20 +#ifdef CONFIG_NO_HZ_COMMON +extern void kcpustat_dyntick_start(void); +extern void kcpustat_dyntick_stop(void); +static inline bool kcpustat_idle_dyntick(void) +{ + return __this_cpu_read(kernel_cpustat.idle_dyntick); +} +#else +static inline bool kcpustat_idle_dyntick(void) +{ + return false; +} +#endif /* CONFIG_NO_HZ_COMMON */ + #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN extern u64 kcpustat_field(enum cpu_usage_stat usage, int cpu); extern void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu); @@ -113,7 +130,7 @@ static inline void kcpustat_cpu_fetch(struct kernel_cpu= stat *dst, int cpu) *dst =3D kcpustat_cpu(cpu); } =20 -#endif +#endif /* !CONFIG_VIRT_CPU_ACCOUNTING_GEN */ =20 extern void account_user_time(struct task_struct *, u64); extern void account_guest_time(struct task_struct *, u64); @@ -127,14 +144,13 @@ extern u64 get_idle_time(struct kernel_cpustat *kcs, = int cpu); #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE static inline void account_process_tick(struct task_struct *tsk, int user) { - vtime_flush(tsk); + if (!kcpustat_idle_dyntick()) + vtime_flush(tsk); } #else extern void account_process_tick(struct task_struct *, int user); #endif =20 -extern void account_idle_ticks(unsigned long ticks); - #ifdef CONFIG_SCHED_CORE extern void __account_forceidle_time(struct task_struct *tsk, u64 delta); #endif diff --git a/include/linux/vtime.h b/include/linux/vtime.h index 10cdb08f960b..43934ff20c4a 100644 --- a/include/linux/vtime.h +++ b/include/linux/vtime.h @@ -32,6 +32,11 @@ static inline bool vtime_generic_enabled_cpu(int cpu) return context_tracking_enabled_cpu(cpu); } =20 +static inline bool vtime_generic_enabled_this_cpu(void) +{ + return context_tracking_enabled_this_cpu(); +} + #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE extern void vtime_account_irq(struct task_struct *tsk, unsigned int offset= ); extern void vtime_account_softirq(struct task_struct *tsk); @@ -90,7 +95,7 @@ static inline bool vtime_accounting_enabled_cpu(int cpu) =20 static inline bool vtime_accounting_enabled_this_cpu(void) { - return context_tracking_enabled_this_cpu(); + return vtime_generic_enabled_this_cpu(); } =20 extern void vtime_task_switch_generic(struct task_struct *prev); diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index f32c169da11a..c10fcc3d65b3 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -400,16 +400,30 @@ static void irqtime_account_process_tick(struct task_= struct *p, int user_tick, } } =20 -static void irqtime_account_idle_ticks(int ticks) -{ - irqtime_account_process_tick(current, 0, ticks); -} #else /* !CONFIG_IRQ_TIME_ACCOUNTING: */ -static inline void irqtime_account_idle_ticks(int ticks) { } static inline void irqtime_account_process_tick(struct task_struct *p, int= user_tick, int nr_ticks) { } #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */ =20 +#ifdef CONFIG_NO_HZ_COMMON +void kcpustat_dyntick_start(void) +{ + if (!vtime_generic_enabled_this_cpu()) { + vtime_dyntick_start(); + __this_cpu_write(kernel_cpustat.idle_dyntick, 1); + } +} + +void kcpustat_dyntick_stop(void) +{ + if (!vtime_generic_enabled_this_cpu()) { + __this_cpu_write(kernel_cpustat.idle_dyntick, 0); + vtime_dyntick_stop(); + steal_account_process_time(ULONG_MAX); + } +} +#endif /* CONFIG_NO_HZ_COMMON */ + /* * Use precise platform statistics if available: */ @@ -423,11 +437,15 @@ void vtime_account_irq(struct task_struct *tsk, unsig= ned int offset) vtime_account_hardirq(tsk); } else if (pc & SOFTIRQ_OFFSET) { vtime_account_softirq(tsk); - } else if (!IS_ENABLED(CONFIG_HAVE_VIRT_CPU_ACCOUNTING_IDLE) && - is_idle_task(tsk)) { - vtime_account_idle(tsk); + } else if (!kcpustat_idle_dyntick()) { + if (!IS_ENABLED(CONFIG_HAVE_VIRT_CPU_ACCOUNTING_IDLE) && + is_idle_task(tsk)) { + vtime_account_idle(tsk); + } else { + vtime_account_kernel(tsk); + } } else { - vtime_account_kernel(tsk); + vtime_reset(); } } =20 @@ -469,6 +487,9 @@ void account_process_tick(struct task_struct *p, int us= er_tick) if (vtime_accounting_enabled_this_cpu()) return; =20 + if (kcpustat_idle_dyntick()) + return; + if (irqtime_enabled()) { irqtime_account_process_tick(p, user_tick, 1); return; @@ -490,29 +511,6 @@ void account_process_tick(struct task_struct *p, int u= ser_tick) account_idle_time(cputime); } =20 -/* - * Account multiple ticks of idle time. - * @ticks: number of stolen ticks - */ -void account_idle_ticks(unsigned long ticks) -{ - u64 cputime, steal; - - if (irqtime_enabled()) { - irqtime_account_idle_ticks(ticks); - return; - } - - cputime =3D ticks * TICK_NSEC; - steal =3D steal_account_process_time(ULONG_MAX); - - if (steal >=3D cputime) - return; - - cputime -=3D steal; - account_idle_time(cputime); -} - /* * Adjust tick based cputime random precision against scheduler runtime * accounting. diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index f1d07a0276a5..74c97ad75856 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -285,8 +285,6 @@ static void tick_sched_handle(struct tick_sched *ts, st= ruct pt_regs *regs) if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && tick_sched_flag_test(ts, TS_FLAG_STOPPED)) { touch_softlockup_watchdog_sched(); - if (is_idle_task(current)) - ts->idle_jiffies++; /* * In case the current tick fired too early past its expected * expiration, make sure we don't bypass the next clock reprogramming @@ -744,8 +742,12 @@ static void tick_nohz_update_jiffies(ktime_t now) =20 static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now) { + u64 *cpustat =3D kcpustat_this_cpu->cpustat; ktime_t delta; =20 + if (vtime_generic_enabled_this_cpu()) + return; + if (WARN_ON_ONCE(!tick_sched_flag_test(ts, TS_FLAG_IDLE_ACTIVE))) return; =20 @@ -753,9 +755,9 @@ static void tick_nohz_stop_idle(struct tick_sched *ts, = ktime_t now) =20 write_seqcount_begin(&ts->idle_sleeptime_seq); if (nr_iowait_cpu(smp_processor_id()) > 0) - ts->iowait_sleeptime =3D ktime_add(ts->iowait_sleeptime, delta); + cpustat[CPUTIME_IOWAIT] =3D ktime_add(cpustat[CPUTIME_IOWAIT], delta); else - ts->idle_sleeptime =3D ktime_add(ts->idle_sleeptime, delta); + cpustat[CPUTIME_IDLE] =3D ktime_add(cpustat[CPUTIME_IDLE], delta); =20 ts->idle_entrytime =3D now; tick_sched_flag_clear(ts, TS_FLAG_IDLE_ACTIVE); @@ -766,17 +768,21 @@ static void tick_nohz_stop_idle(struct tick_sched *ts= , ktime_t now) =20 static void tick_nohz_start_idle(struct tick_sched *ts) { + if (vtime_generic_enabled_this_cpu()) + return; + write_seqcount_begin(&ts->idle_sleeptime_seq); ts->idle_entrytime =3D ktime_get(); tick_sched_flag_set(ts, TS_FLAG_IDLE_ACTIVE); write_seqcount_end(&ts->idle_sleeptime_seq); - sched_clock_idle_sleep_event(); } =20 -static u64 get_cpu_sleep_time_us(struct tick_sched *ts, ktime_t *sleeptime, +static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usage_stat idx, bool compute_delta, u64 *last_update_time) { + struct tick_sched *ts =3D &per_cpu(tick_cpu_sched, cpu); + u64 *cpustat =3D kcpustat_cpu(cpu).cpustat; ktime_t now, idle; unsigned int seq; =20 @@ -793,9 +799,9 @@ static u64 get_cpu_sleep_time_us(struct tick_sched *ts,= ktime_t *sleeptime, if (tick_sched_flag_test(ts, TS_FLAG_IDLE_ACTIVE) && compute_delta) { ktime_t delta =3D ktime_sub(now, ts->idle_entrytime); =20 - idle =3D ktime_add(*sleeptime, delta); + idle =3D ktime_add(cpustat[idx], delta); } else { - idle =3D *sleeptime; + idle =3D cpustat[idx]; } } while (read_seqcount_retry(&ts->idle_sleeptime_seq, seq)); =20 @@ -822,9 +828,7 @@ static u64 get_cpu_sleep_time_us(struct tick_sched *ts,= ktime_t *sleeptime, */ u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time) { - struct tick_sched *ts =3D &per_cpu(tick_cpu_sched, cpu); - - return get_cpu_sleep_time_us(ts, &ts->idle_sleeptime, + return get_cpu_sleep_time_us(cpu, CPUTIME_IDLE, !nr_iowait_cpu(cpu), last_update_time); } EXPORT_SYMBOL_GPL(get_cpu_idle_time_us); @@ -848,9 +852,7 @@ EXPORT_SYMBOL_GPL(get_cpu_idle_time_us); */ u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time) { - struct tick_sched *ts =3D &per_cpu(tick_cpu_sched, cpu); - - return get_cpu_sleep_time_us(ts, &ts->iowait_sleeptime, + return get_cpu_sleep_time_us(cpu, CPUTIME_IOWAIT, nr_iowait_cpu(cpu), last_update_time); } EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us); @@ -1250,10 +1252,8 @@ void tick_nohz_idle_stop_tick(void) ts->idle_sleeps++; ts->idle_expires =3D expires; =20 - if (!was_stopped && tick_sched_flag_test(ts, TS_FLAG_STOPPED)) { - ts->idle_jiffies =3D ts->last_jiffies; + if (!was_stopped && tick_sched_flag_test(ts, TS_FLAG_STOPPED)) nohz_balance_enter_idle(cpu); - } } else { tick_nohz_retain_tick(ts); } @@ -1282,6 +1282,7 @@ void tick_nohz_idle_enter(void) WARN_ON_ONCE(ts->timer_expires_base); =20 tick_sched_flag_set(ts, TS_FLAG_INIDLE); + kcpustat_dyntick_start(); tick_nohz_start_idle(ts); =20 local_irq_enable(); @@ -1407,37 +1408,12 @@ unsigned long tick_nohz_get_idle_calls_cpu(int cpu) return ts->idle_calls; } =20 -static void tick_nohz_account_idle_time(struct tick_sched *ts, - ktime_t now) -{ - unsigned long ticks; - - ts->idle_exittime =3D now; - - if (vtime_accounting_enabled_this_cpu()) - return; - /* - * We stopped the tick in idle. update_process_times() would miss the - * time we slept, as it does only a 1 tick accounting. - * Enforce that this is accounted to idle ! - */ - ticks =3D jiffies - ts->idle_jiffies; - /* - * We might be one off. Do not randomly account a huge number of ticks! - */ - if (ticks && ticks < LONG_MAX) - account_idle_ticks(ticks); -} - void tick_nohz_idle_restart_tick(void) { struct tick_sched *ts =3D this_cpu_ptr(&tick_cpu_sched); =20 - if (tick_sched_flag_test(ts, TS_FLAG_STOPPED)) { - ktime_t now =3D ktime_get(); - tick_nohz_restart_sched_tick(ts, now); - tick_nohz_account_idle_time(ts, now); - } + if (tick_sched_flag_test(ts, TS_FLAG_STOPPED)) + tick_nohz_restart_sched_tick(ts, ktime_get()); } =20 static void tick_nohz_idle_update_tick(struct tick_sched *ts, ktime_t now) @@ -1446,8 +1422,6 @@ static void tick_nohz_idle_update_tick(struct tick_sc= hed *ts, ktime_t now) __tick_nohz_full_update_tick(ts, now); else tick_nohz_restart_sched_tick(ts, now); - - tick_nohz_account_idle_time(ts, now); } =20 /** @@ -1489,6 +1463,7 @@ void tick_nohz_idle_exit(void) =20 if (tick_stopped) tick_nohz_idle_update_tick(ts, now); + kcpustat_dyntick_stop(); =20 local_irq_enable(); } @@ -1625,20 +1600,15 @@ void tick_setup_sched_timer(bool hrtimer) void tick_sched_timer_dying(int cpu) { struct tick_sched *ts =3D &per_cpu(tick_cpu_sched, cpu); - ktime_t idle_sleeptime, iowait_sleeptime; unsigned long idle_calls, idle_sleeps; =20 /* This must happen before hrtimers are migrated! */ if (tick_sched_flag_test(ts, TS_FLAG_HIGHRES)) hrtimer_cancel(&ts->sched_timer); =20 - idle_sleeptime =3D ts->idle_sleeptime; - iowait_sleeptime =3D ts->iowait_sleeptime; idle_calls =3D ts->idle_calls; idle_sleeps =3D ts->idle_sleeps; memset(ts, 0, sizeof(*ts)); - ts->idle_sleeptime =3D idle_sleeptime; - ts->iowait_sleeptime =3D iowait_sleeptime; ts->idle_calls =3D idle_calls; ts->idle_sleeps =3D idle_sleeps; } --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 82294394469; Fri, 16 Jan 2026 14:53:11 +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=1768575191; cv=none; b=lvVUPb0CIzyF7pu9PaJxCMaCwIxrqSL5u7qZT3WRBLbvXlMvwfirzLkItc+jNQn5+c+DK9Duv3oeXYfw6lOlieqCINX2QQ5JmgeqJ+Vw9z8hvzO413rLlWGIQCYKMXMjusbNGGPSvUhWUKNSZqjWs7yZINm3OZBCL0Eqmn7labc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575191; c=relaxed/simple; bh=TzrKoRirKu2E+jAiuhlzsln8JBhROUV36dDGy9tKFsw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AoKRgsphjvgq1vvRKGW7EKP4fS904kY+Bcnm4m+bsXGEnNqw5Xrf4QI01cZwUN+Z6ykuPCZ9WFzm0ABA+Qw4deG2Kj+WPPW39TpGDWht8CoBURnsCRyWFMpa76OSzJUd7oKi85xolKthrbps3gNEcPH1xwfoR/Zy9loYU7+HxkM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AUnnY7SK; 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="AUnnY7SK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4A0FC19421; Fri, 16 Jan 2026 14:53:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575191; bh=TzrKoRirKu2E+jAiuhlzsln8JBhROUV36dDGy9tKFsw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AUnnY7SK3ALhVTftF+V//K5xP8H7lrjkEswZ+U2Y2mPJfJsD3b2iS1zkZM/21hkDb otcvgtr3VNOsAa5lRYT6msuhiOEdltnRqueCWPAs3RmlhuxjPkj0MoZRyQYXkCVkp+ EEa529KqpaVkr3RPuZTwr5AB9LQEXD1IOjZhct9kLv/VlIEdQtP5Pg0qGxOgAtdzuG zKl2D/2NbJb9yEWBpc2c6pyOL9cKpOdjPb072dxVwl4v4i2PACLu684MJTdcq35VeG yuc16NjatOamu0X8Rn+5hd5bF/GzfXEYSr+6rWj9yP9o2l3izkMwerxfrSQRHI6L2Y YEhsuz1tKeRPA== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 07/15] cpufreq: ondemand: Simplify idle cputime granularity test Date: Fri, 16 Jan 2026 15:52:00 +0100 Message-ID: <20260116145208.87445-8-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" cpufreq calls get_cpu_idle_time_us() just to know if idle cputime accounting has a nanoseconds granularity. Use the appropriate indicator instead to make that deduction. Signed-off-by: Frederic Weisbecker Acked-by: Rafael J. Wysocki (Intel) Acked-by: Viresh Kumar --- drivers/cpufreq/cpufreq_ondemand.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/drivers/cpufreq/cpufreq_ondemand.c b/drivers/cpufreq/cpufreq_o= ndemand.c index a6ecc203f7b7..2d52ee035702 100644 --- a/drivers/cpufreq/cpufreq_ondemand.c +++ b/drivers/cpufreq/cpufreq_ondemand.c @@ -334,17 +334,12 @@ static void od_free(struct policy_dbs_info *policy_db= s) static int od_init(struct dbs_data *dbs_data) { struct od_dbs_tuners *tuners; - u64 idle_time; - int cpu; =20 tuners =3D kzalloc(sizeof(*tuners), GFP_KERNEL); if (!tuners) return -ENOMEM; =20 - cpu =3D get_cpu(); - idle_time =3D get_cpu_idle_time_us(cpu, NULL); - put_cpu(); - if (idle_time !=3D -1ULL) { + if (tick_nohz_enabled) { /* Idle micro accounting is supported. Use finer thresholds */ dbs_data->up_threshold =3D MICRO_FREQUENCY_UP_THRESHOLD; } else { --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 7979A13B293; Fri, 16 Jan 2026 14:53:18 +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=1768575198; cv=none; b=VYUixGEVpEe6oZY6tXABb/tvgditnXHgTDy/iz3vp2gCnhebCPieLr7KguTAj1dVJpG58/lcveCa1fTIZ2VaZGVQh//veN/SGJpdFULV70/5N2evMMXABmHDiNYWIjyRwOp42158sdRYPoiV8HfeSg9Od2EdygEDoyo2joRIcWM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575198; c=relaxed/simple; bh=6uFyuO5klJgHNENFOSF+x68f4iBYCkwgEv1Ti9TKsLg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JGvl0oF7FZq3bHCBXo6npfP0vGrJfqEDLmISoHCKeGF+kx5jLD7xHgCrNh2dDOkBu02R/jubkg7qPYQ+7m+nnUTevL21wAFPM2SgdYucvnw1OxBUzMeoxyEjd307BCOC8CT/PkLJepTURC+ajYAREbv6ClTHSxY9m8mC81DjfhE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=A2N15wgk; 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="A2N15wgk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CBD2FC19423; Fri, 16 Jan 2026 14:53:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575198; bh=6uFyuO5klJgHNENFOSF+x68f4iBYCkwgEv1Ti9TKsLg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A2N15wgkSJJg2WEIufXp2BjLHdQAxntSADU/uVCO6plXZBWOEQrHvHSfskOiYtVdZ UxQJQfexh2UyBLWfmd6knC/gADTgf/uNDot/hM9UNySIWksjgfM8Uon6dTZXfAxUqk o2SsSUTNxj4vS2+MN7ybX1APmYSWrVvDtZhtVpi7Zk7KqoeFq6GjRtCdwXHMRfj7CN gfJHbzwyqtlWn/fJkjFXq/VXs5H1iHdh8FveDF8UnXBwY5lXC11DN7cAGvK+kVEuoo Nm9knjTpgNpfzWUnTRfgWhubz6NT+6JMku2ChA1QU7igMB2u3mgddBsFNKKrO/GCOi dbvxfaysBrppg== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 08/15] tick/sched: Remove nohz disabled special case in cputime fetch Date: Fri, 16 Jan 2026 15:52:01 +0100 Message-ID: <20260116145208.87445-9-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" Even when nohz is not runtime enabled, the dynticks idle cputime accounting can run and the common idle cputime accessors are still relevant. Remove the nohz disabled special case accordingly. Signed-off-by: Frederic Weisbecker --- kernel/time/tick-sched.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index 74c97ad75856..f0b79e876997 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -786,7 +786,7 @@ static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usag= e_stat idx, ktime_t now, idle; unsigned int seq; =20 - if (!tick_nohz_active || vtime_generic_enabled_cpu(cpu)) + if (vtime_generic_enabled_cpu(cpu)) return -1; =20 now =3D ktime_get(); @@ -824,7 +824,7 @@ static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usag= e_stat idx, * This time is measured via accounting rather than sampling, * and is as accurate as ktime_get() is. * - * Return: -1 if NOHZ is not enabled, else total idle time of the @cpu + * Return: -1 if generic vtime is enabled, else total idle time of the @cpu */ u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time) { @@ -848,7 +848,7 @@ EXPORT_SYMBOL_GPL(get_cpu_idle_time_us); * This time is measured via accounting rather than sampling, * and is as accurate as ktime_get() is. * - * Return: -1 if NOHZ is not enabled, else total iowait time of @cpu + * Return: -1 if generic vtime is enabled, else total iowait time of @cpu */ u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time) { --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 3707E393419; Fri, 16 Jan 2026 14:53:25 +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=1768575206; cv=none; b=fndZ8aN9RLEexCQxkdsGJrJc7qTAmo03p746Wg5OX1Uzn19ojkh4bF11S8fk0wZzlr3vy4dBvXyMCR5wbMWmKEs/g2mbWkGnDk5SWZnObwK2ZNxlPSDnMAcD5EZ8bqLLzundpxcqCjujJTaOrY+GMS2iLIOAeeIcVoQAY6n8hLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575206; c=relaxed/simple; bh=Q7B1RTOaC9Z34bM1w/cMiGiolpdcg9oH38EWCVtHmKA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EIdAiJKMvcpNT4T4w4eLOzCRLQJ9M0XxQoLE3x+/CyvsBVXKeZ1eGx55hp6qFnkdnFuUeXPHiAl3YwOGIy8Mg8ZL10X8qv+gFxuCNAsc4OB1E/hyTcyElf4mNCqqyt1Hlf5pADDIyE7Rurt/LQf/+ueMKzUhPDpp01Q2j8UJ7Bs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qV8YITrN; 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="qV8YITrN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3D0AC19423; Fri, 16 Jan 2026 14:53:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575205; bh=Q7B1RTOaC9Z34bM1w/cMiGiolpdcg9oH38EWCVtHmKA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qV8YITrNeHN1vzLsOndn+0+dbQjazVhfx2VbJ8jT7YriJsx2nx46iWEwW2gVt8yJH tlcV7Nvibp89UbiZf4+uzqei2NDTs6Av3wRVIUZ0ChSuzQA4P8i70E9QdxjGp/sDV4 sPPaH8XUHZdUeGTLdfuHY25+SaecJH1HVmYWOfWeUmtH1c3Fzcr27NkNcjOElxtvyI /UekxTAhDLERlBhNC1Jqi8ZOKR4x8emiNSm3/+jh596LRKlBluOsrpDbZMoN/v4HvB CrZEXv9NEpsuIt0BGc55kRrn1tQHgKiOUYtcDq+YTfG12iRAWDEV4ffM7ZKfPYjSyk QTckhtBL1OPvw== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 09/15] tick/sched: Move dyntick-idle cputime accounting to cputime code Date: Fri, 16 Jan 2026 15:52:02 +0100 Message-ID: <20260116145208.87445-10-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" Although the dynticks-idle cputime accounting is necessarily tied to the tick subsystem, the actual related accounting code has no business residing there and should be part of the scheduler cputime code. Move away the relevant pieces and state machine to where they belong. Signed-off-by: Frederic Weisbecker --- include/linux/kernel_stat.h | 14 +++- kernel/sched/cputime.c | 145 ++++++++++++++++++++++++++++++-- kernel/time/tick-sched.c | 161 +++++++----------------------------- 3 files changed, 180 insertions(+), 140 deletions(-) diff --git a/include/linux/kernel_stat.h b/include/linux/kernel_stat.h index ba65aad308a1..a906492eb680 100644 --- a/include/linux/kernel_stat.h +++ b/include/linux/kernel_stat.h @@ -35,9 +35,12 @@ enum cpu_usage_stat { =20 struct kernel_cpustat { #ifdef CONFIG_NO_HZ_COMMON - int idle_dyntick; + bool idle_dyntick; + bool idle_elapse; + seqcount_t idle_sleeptime_seq; + ktime_t idle_entrytime; #endif - u64 cpustat[NR_STATS]; + u64 cpustat[NR_STATS]; }; =20 struct kernel_stat { @@ -103,8 +106,11 @@ static inline unsigned long kstat_cpu_irqs_sum(unsigne= d int cpu) } =20 #ifdef CONFIG_NO_HZ_COMMON -extern void kcpustat_dyntick_start(void); -extern void kcpustat_dyntick_stop(void); +extern void kcpustat_dyntick_start(ktime_t now); +extern void kcpustat_dyntick_stop(ktime_t now); +extern void kcpustat_irq_enter(ktime_t now); +extern void kcpustat_irq_exit(ktime_t now); + static inline bool kcpustat_idle_dyntick(void) { return __this_cpu_read(kernel_cpustat.idle_dyntick); diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index c10fcc3d65b3..16d6730efe6d 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -406,22 +406,153 @@ static inline void irqtime_account_process_tick(stru= ct task_struct *p, int user_ #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */ =20 #ifdef CONFIG_NO_HZ_COMMON -void kcpustat_dyntick_start(void) +static void kcpustat_idle_stop(struct kernel_cpustat *kc, ktime_t now) { - if (!vtime_generic_enabled_this_cpu()) { - vtime_dyntick_start(); - __this_cpu_write(kernel_cpustat.idle_dyntick, 1); - } + u64 *cpustat =3D kc->cpustat; + ktime_t delta; + + if (!kc->idle_elapse) + return; + + delta =3D ktime_sub(now, kc->idle_entrytime); + + write_seqcount_begin(&kc->idle_sleeptime_seq); + if (nr_iowait_cpu(smp_processor_id()) > 0) + cpustat[CPUTIME_IOWAIT] =3D ktime_add(cpustat[CPUTIME_IOWAIT], delta); + else + cpustat[CPUTIME_IDLE] =3D ktime_add(cpustat[CPUTIME_IDLE], delta); + + kc->idle_entrytime =3D now; + kc->idle_elapse =3D false; + write_seqcount_end(&kc->idle_sleeptime_seq); } =20 -void kcpustat_dyntick_stop(void) +static void kcpustat_idle_start(struct kernel_cpustat *kc, ktime_t now) { + write_seqcount_begin(&kc->idle_sleeptime_seq); + kc->idle_entrytime =3D now; + kc->idle_elapse =3D true; + write_seqcount_end(&kc->idle_sleeptime_seq); +} + +void kcpustat_dyntick_stop(ktime_t now) +{ + struct kernel_cpustat *kc =3D kcpustat_this_cpu; + if (!vtime_generic_enabled_this_cpu()) { - __this_cpu_write(kernel_cpustat.idle_dyntick, 0); + WARN_ON_ONCE(!kc->idle_dyntick); + kcpustat_idle_stop(kc, now); + kc->idle_dyntick =3D false; vtime_dyntick_stop(); steal_account_process_time(ULONG_MAX); } } + +void kcpustat_dyntick_start(ktime_t now) +{ + struct kernel_cpustat *kc =3D kcpustat_this_cpu; + + if (!vtime_generic_enabled_this_cpu()) { + vtime_dyntick_start(); + kc->idle_dyntick =3D true; + kcpustat_idle_start(kc, now); + } +} + +void kcpustat_irq_enter(ktime_t now) +{ + struct kernel_cpustat *kc =3D kcpustat_this_cpu; + + if (!vtime_generic_enabled_this_cpu()) + kcpustat_idle_stop(kc, now); +} + +void kcpustat_irq_exit(ktime_t now) +{ + struct kernel_cpustat *kc =3D kcpustat_this_cpu; + + if (!vtime_generic_enabled_this_cpu()) + kcpustat_idle_start(kc, now); +} + +static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usage_stat idx, + bool compute_delta, u64 *last_update_time) +{ + struct kernel_cpustat *kc =3D &kcpustat_cpu(cpu); + u64 *cpustat =3D kc->cpustat; + ktime_t now, idle; + unsigned int seq; + + if (vtime_generic_enabled_cpu(cpu)) + return -1; + + now =3D ktime_get(); + if (last_update_time) + *last_update_time =3D ktime_to_us(now); + + do { + seq =3D read_seqcount_begin(&kc->idle_sleeptime_seq); + + if (kc->idle_elapse && compute_delta) { + ktime_t delta =3D ktime_sub(now, kc->idle_entrytime); + + idle =3D ktime_add(cpustat[idx], delta); + } else { + idle =3D cpustat[idx]; + } + } while (read_seqcount_retry(&kc->idle_sleeptime_seq, seq)); + + return ktime_to_us(idle); +} + +/** + * get_cpu_idle_time_us - get the total idle time of a CPU + * @cpu: CPU number to query + * @last_update_time: variable to store update time in. Do not update + * counters if NULL. + * + * Return the cumulative idle time (since boot) for a given + * CPU, in microseconds. Note that this is partially broken due to + * the counter of iowait tasks that can be remotely updated without + * any synchronization. Therefore it is possible to observe backward + * values within two consecutive reads. + * + * This time is measured via accounting rather than sampling, + * and is as accurate as ktime_get() is. + * + * Return: -1 if generic vtime is enabled, else total idle time of the @cpu + */ +u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time) +{ + return get_cpu_sleep_time_us(cpu, CPUTIME_IDLE, + !nr_iowait_cpu(cpu), last_update_time); +} +EXPORT_SYMBOL_GPL(get_cpu_idle_time_us); + +/** + * get_cpu_iowait_time_us - get the total iowait time of a CPU + * @cpu: CPU number to query + * @last_update_time: variable to store update time in. Do not update + * counters if NULL. + * + * Return the cumulative iowait time (since boot) for a given + * CPU, in microseconds. Note this is partially broken due to + * the counter of iowait tasks that can be remotely updated without + * any synchronization. Therefore it is possible to observe backward + * values within two consecutive reads. + * + * This time is measured via accounting rather than sampling, + * and is as accurate as ktime_get() is. + * + * Return: -1 if generic vtime is enabled, else total iowait time of @cpu + */ +u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time) +{ + return get_cpu_sleep_time_us(cpu, CPUTIME_IOWAIT, + nr_iowait_cpu(cpu), last_update_time); +} +EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us); + #endif /* CONFIG_NO_HZ_COMMON */ =20 /* diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index f0b79e876997..cbd645fb8df6 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -740,123 +740,6 @@ static void tick_nohz_update_jiffies(ktime_t now) touch_softlockup_watchdog_sched(); } =20 -static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now) -{ - u64 *cpustat =3D kcpustat_this_cpu->cpustat; - ktime_t delta; - - if (vtime_generic_enabled_this_cpu()) - return; - - if (WARN_ON_ONCE(!tick_sched_flag_test(ts, TS_FLAG_IDLE_ACTIVE))) - return; - - delta =3D ktime_sub(now, ts->idle_entrytime); - - write_seqcount_begin(&ts->idle_sleeptime_seq); - if (nr_iowait_cpu(smp_processor_id()) > 0) - cpustat[CPUTIME_IOWAIT] =3D ktime_add(cpustat[CPUTIME_IOWAIT], delta); - else - cpustat[CPUTIME_IDLE] =3D ktime_add(cpustat[CPUTIME_IDLE], delta); - - ts->idle_entrytime =3D now; - tick_sched_flag_clear(ts, TS_FLAG_IDLE_ACTIVE); - write_seqcount_end(&ts->idle_sleeptime_seq); - - sched_clock_idle_wakeup_event(); -} - -static void tick_nohz_start_idle(struct tick_sched *ts) -{ - if (vtime_generic_enabled_this_cpu()) - return; - - write_seqcount_begin(&ts->idle_sleeptime_seq); - ts->idle_entrytime =3D ktime_get(); - tick_sched_flag_set(ts, TS_FLAG_IDLE_ACTIVE); - write_seqcount_end(&ts->idle_sleeptime_seq); - sched_clock_idle_sleep_event(); -} - -static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usage_stat idx, - bool compute_delta, u64 *last_update_time) -{ - struct tick_sched *ts =3D &per_cpu(tick_cpu_sched, cpu); - u64 *cpustat =3D kcpustat_cpu(cpu).cpustat; - ktime_t now, idle; - unsigned int seq; - - if (vtime_generic_enabled_cpu(cpu)) - return -1; - - now =3D ktime_get(); - if (last_update_time) - *last_update_time =3D ktime_to_us(now); - - do { - seq =3D read_seqcount_begin(&ts->idle_sleeptime_seq); - - if (tick_sched_flag_test(ts, TS_FLAG_IDLE_ACTIVE) && compute_delta) { - ktime_t delta =3D ktime_sub(now, ts->idle_entrytime); - - idle =3D ktime_add(cpustat[idx], delta); - } else { - idle =3D cpustat[idx]; - } - } while (read_seqcount_retry(&ts->idle_sleeptime_seq, seq)); - - return ktime_to_us(idle); - -} - -/** - * get_cpu_idle_time_us - get the total idle time of a CPU - * @cpu: CPU number to query - * @last_update_time: variable to store update time in. Do not update - * counters if NULL. - * - * Return the cumulative idle time (since boot) for a given - * CPU, in microseconds. Note that this is partially broken due to - * the counter of iowait tasks that can be remotely updated without - * any synchronization. Therefore it is possible to observe backward - * values within two consecutive reads. - * - * This time is measured via accounting rather than sampling, - * and is as accurate as ktime_get() is. - * - * Return: -1 if generic vtime is enabled, else total idle time of the @cpu - */ -u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time) -{ - return get_cpu_sleep_time_us(cpu, CPUTIME_IDLE, - !nr_iowait_cpu(cpu), last_update_time); -} -EXPORT_SYMBOL_GPL(get_cpu_idle_time_us); - -/** - * get_cpu_iowait_time_us - get the total iowait time of a CPU - * @cpu: CPU number to query - * @last_update_time: variable to store update time in. Do not update - * counters if NULL. - * - * Return the cumulative iowait time (since boot) for a given - * CPU, in microseconds. Note this is partially broken due to - * the counter of iowait tasks that can be remotely updated without - * any synchronization. Therefore it is possible to observe backward - * values within two consecutive reads. - * - * This time is measured via accounting rather than sampling, - * and is as accurate as ktime_get() is. - * - * Return: -1 if generic vtime is enabled, else total iowait time of @cpu - */ -u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time) -{ - return get_cpu_sleep_time_us(cpu, CPUTIME_IOWAIT, - nr_iowait_cpu(cpu), last_update_time); -} -EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us); - static void tick_nohz_restart(struct tick_sched *ts, ktime_t now) { hrtimer_cancel(&ts->sched_timer); @@ -1264,6 +1147,20 @@ void tick_nohz_idle_retain_tick(void) tick_nohz_retain_tick(this_cpu_ptr(&tick_cpu_sched)); } =20 +static void tick_nohz_clock_sleep(struct tick_sched *ts) +{ + tick_sched_flag_set(ts, TS_FLAG_IDLE_ACTIVE); + sched_clock_idle_sleep_event(); +} + +static void tick_nohz_clock_wakeup(struct tick_sched *ts) +{ + if (tick_sched_flag_test(ts, TS_FLAG_IDLE_ACTIVE)) { + tick_sched_flag_clear(ts, TS_FLAG_IDLE_ACTIVE); + sched_clock_idle_wakeup_event(); + } +} + /** * tick_nohz_idle_enter - prepare for entering idle on the current CPU * @@ -1278,12 +1175,10 @@ void tick_nohz_idle_enter(void) local_irq_disable(); =20 ts =3D this_cpu_ptr(&tick_cpu_sched); - WARN_ON_ONCE(ts->timer_expires_base); - - tick_sched_flag_set(ts, TS_FLAG_INIDLE); - kcpustat_dyntick_start(); - tick_nohz_start_idle(ts); + ts->idle_entrytime =3D ktime_get(); + kcpustat_dyntick_start(ts->idle_entrytime); + tick_nohz_clock_sleep(ts); =20 local_irq_enable(); } @@ -1311,10 +1206,13 @@ void tick_nohz_irq_exit(void) { struct tick_sched *ts =3D this_cpu_ptr(&tick_cpu_sched); =20 - if (tick_sched_flag_test(ts, TS_FLAG_INIDLE)) - tick_nohz_start_idle(ts); - else + if (tick_sched_flag_test(ts, TS_FLAG_INIDLE)) { + ts->idle_entrytime =3D ktime_get(); + kcpustat_irq_exit(ts->idle_entrytime); + tick_nohz_clock_sleep(ts); + } else { tick_nohz_full_update_tick(ts); + } } =20 /** @@ -1459,11 +1357,11 @@ void tick_nohz_idle_exit(void) now =3D ktime_get(); =20 if (idle_active) - tick_nohz_stop_idle(ts, now); + tick_nohz_clock_wakeup(ts); =20 if (tick_stopped) tick_nohz_idle_update_tick(ts, now); - kcpustat_dyntick_stop(); + kcpustat_dyntick_stop(now); =20 local_irq_enable(); } @@ -1519,9 +1417,14 @@ static inline void tick_nohz_irq_enter(void) =20 if (!tick_sched_flag_test(ts, TS_FLAG_STOPPED | TS_FLAG_IDLE_ACTIVE)) return; + now =3D ktime_get(); - if (tick_sched_flag_test(ts, TS_FLAG_IDLE_ACTIVE)) - tick_nohz_stop_idle(ts, now); + + if (tick_sched_flag_test(ts, TS_FLAG_IDLE_ACTIVE)) { + tick_nohz_clock_wakeup(ts); + kcpustat_irq_enter(now); + } + /* * If all CPUs are idle we may need to update a stale jiffies value. * Note nohz_full is a special case: a timekeeper is guaranteed to stay --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 AF8D323D2B2; Fri, 16 Jan 2026 14:53:33 +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=1768575213; cv=none; b=Wu1x5ol+glsgsv5QE3YjoaB1fjxQZM05D4JiCkVuBs2bhlknTsfm2P2oQj40OeVyOYeISyeeTk2zGQXv/OePM1iPd3o596bY5SP1zALKLrzChXhkIbM+na75yc6XZYfB9p/nn9mE1PgM4DwR9gjc358v0dLp4OtI40rIj3BTTQ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575213; c=relaxed/simple; bh=+EcaS9wpjAI5nYExsBELcaZu8W8ohxG84dL93BxRlIw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rFW7q0ke2g4PJfABMXC+3oxbGTZDPCbb2b+gapTPLMz/INX9vpu/OWH6gKPegEixZS2Lby3vvIqneTkmhPruXCJlOJpbhJfoiW8X1d3RLAWRxFDL8h4e/0TVCSItpIxidMSMMlZ/r+PdOj33VITumvI1wkoTXoXB/ba8tnVpT8k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=U7f1kh7r; 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="U7f1kh7r" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4247FC19425; Fri, 16 Jan 2026 14:53:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575213; bh=+EcaS9wpjAI5nYExsBELcaZu8W8ohxG84dL93BxRlIw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U7f1kh7rJjODodXqRraFZwOOQ0AX4fWSIf9o/E9fD1XYzZBx+5fe236YW0A3Ve8O1 Rz04lUtX6W0Gw6C314tLM0ft3an+9Nd8rmqrWtM0Vhg6x0+EvQe2+e6aDyUNfLVhon 0A/oWIFSINFiw5FCD1JK/pclfTftfMl9sh3tJos2b6nVCiOf19VU4C3AZuhhFqDtjQ QwaQZu0Nnu2URB700+D7zJp6yNwtJJ9uRVarOFgNrqtd0i5eO4zzPyC1Hmdi8COYRc vyQapnbNGGxOsmIOJu1LHgQBI29sqN/KUWMmFR8J2STLF9hH/rT/6h1EPP7gcz39KZ 0Pk5QK2wl08WQ== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 10/15] tick/sched: Remove unused fields Date: Fri, 16 Jan 2026 15:52:03 +0100 Message-ID: <20260116145208.87445-11-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" Remove fields after the dyntick-idle cputime migration to scheduler code. Signed-off-by: Frederic Weisbecker --- kernel/time/tick-sched.h | 12 ------------ kernel/time/timer_list.c | 6 +----- scripts/gdb/linux/timerlist.py | 4 ---- 3 files changed, 1 insertion(+), 21 deletions(-) diff --git a/kernel/time/tick-sched.h b/kernel/time/tick-sched.h index b4a7822f495d..79b9252047b1 100644 --- a/kernel/time/tick-sched.h +++ b/kernel/time/tick-sched.h @@ -44,9 +44,7 @@ struct tick_device { * to resume the tick timer operation in the timeline * when the CPU returns from nohz sleep. * @next_tick: Next tick to be fired when in dynticks mode. - * @idle_jiffies: jiffies at the entry to idle for idle time accounting * @idle_waketime: Time when the idle was interrupted - * @idle_sleeptime_seq: sequence counter for data consistency * @idle_entrytime: Time when the idle call was entered * @last_jiffies: Base jiffies snapshot when next event was last computed * @timer_expires_base: Base time clock monotonic for @timer_expires @@ -55,9 +53,6 @@ struct tick_device { * @idle_expires: Next tick in idle, for debugging purpose only * @idle_calls: Total number of idle calls * @idle_sleeps: Number of idle calls, where the sched tick was stopped - * @idle_exittime: Time when the idle state was left - * @idle_sleeptime: Sum of the time slept in idle with sched tick stopped - * @iowait_sleeptime: Sum of the time slept in idle with sched tick stoppe= d, with IO outstanding * @tick_dep_mask: Tick dependency mask - is set, if someone needs the tick * @check_clocks: Notification mechanism about clocksource changes */ @@ -73,12 +68,10 @@ struct tick_sched { struct hrtimer sched_timer; ktime_t last_tick; ktime_t next_tick; - unsigned long idle_jiffies; ktime_t idle_waketime; unsigned int got_idle_tick; =20 /* Idle entry */ - seqcount_t idle_sleeptime_seq; ktime_t idle_entrytime; =20 /* Tick stop */ @@ -90,11 +83,6 @@ struct tick_sched { unsigned long idle_calls; unsigned long idle_sleeps; =20 - /* Idle exit */ - ktime_t idle_exittime; - ktime_t idle_sleeptime; - ktime_t iowait_sleeptime; - /* Full dynticks handling */ atomic_t tick_dep_mask; =20 diff --git a/kernel/time/timer_list.c b/kernel/time/timer_list.c index 488e47e96e93..e77b512e8597 100644 --- a/kernel/time/timer_list.c +++ b/kernel/time/timer_list.c @@ -154,14 +154,10 @@ static void print_cpu(struct seq_file *m, int cpu, u6= 4 now) P_flag(highres, TS_FLAG_HIGHRES); P_ns(last_tick); P_flag(tick_stopped, TS_FLAG_STOPPED); - P(idle_jiffies); P(idle_calls); P(idle_sleeps); P_ns(idle_entrytime); P_ns(idle_waketime); - P_ns(idle_exittime); - P_ns(idle_sleeptime); - P_ns(iowait_sleeptime); P(last_jiffies); P(next_timer); P_ns(idle_expires); @@ -258,7 +254,7 @@ static void timer_list_show_tickdevices_header(struct s= eq_file *m) =20 static inline void timer_list_header(struct seq_file *m, u64 now) { - SEQ_printf(m, "Timer List Version: v0.10\n"); + SEQ_printf(m, "Timer List Version: v0.11\n"); SEQ_printf(m, "HRTIMER_MAX_CLOCK_BASES: %d\n", HRTIMER_MAX_CLOCK_BASES); SEQ_printf(m, "now at %Ld nsecs\n", (unsigned long long)now); SEQ_printf(m, "\n"); diff --git a/scripts/gdb/linux/timerlist.py b/scripts/gdb/linux/timerlist.py index ccc24d30de80..c14ce55674c9 100644 --- a/scripts/gdb/linux/timerlist.py +++ b/scripts/gdb/linux/timerlist.py @@ -90,14 +90,10 @@ def print_cpu(hrtimer_bases, cpu, max_clock_bases): text +=3D f" .{'nohz':15s}: {int(bool(ts['flags'] & TS_FLAG_N= OHZ))}\n" text +=3D f" .{'last_tick':15s}: {ts['last_tick']}\n" text +=3D f" .{'tick_stopped':15s}: {int(bool(ts['flags'] & T= S_FLAG_STOPPED))}\n" - text +=3D f" .{'idle_jiffies':15s}: {ts['idle_jiffies']}\n" text +=3D f" .{'idle_calls':15s}: {ts['idle_calls']}\n" text +=3D f" .{'idle_sleeps':15s}: {ts['idle_sleeps']}\n" text +=3D f" .{'idle_entrytime':15s}: {ts['idle_entrytime']} = nsecs\n" text +=3D f" .{'idle_waketime':15s}: {ts['idle_waketime']} ns= ecs\n" - text +=3D f" .{'idle_exittime':15s}: {ts['idle_exittime']} ns= ecs\n" - text +=3D f" .{'idle_sleeptime':15s}: {ts['idle_sleeptime']} = nsecs\n" - text +=3D f" .{'iowait_sleeptime':15s}: {ts['iowait_sleeptime= ']} nsecs\n" text +=3D f" .{'last_jiffies':15s}: {ts['last_jiffies']}\n" text +=3D f" .{'next_timer':15s}: {ts['next_timer']}\n" text +=3D f" .{'idle_expires':15s}: {ts['idle_expires']} nsec= s\n" --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 4354E3933FD; Fri, 16 Jan 2026 14:53:41 +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=1768575221; cv=none; b=ry4z+0nAaItYBkQm4bpkk81gnoLO1P7QJhFFk6y6BVV39biKDXPg94b0S9Tvqxmyo5tLvA97fucfhFuy1DBAE3AMB9Hgb4GMIxTt06XPmyoutvXxruBexCvv3qrUqP3yla7HszrW7ce9GQG4KkMXuFY5Z6S0Xd/YYtM2TZUb4k8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575221; c=relaxed/simple; bh=cektvtI2SQiWO5ottxWjn0y5YqwpxHFc7QfJqdyaGjA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lvNm9nXMOtEm4w610cQzwCsxnTJ1Dd+/1Mh5/cuNEQt/MYOBTsYFMkdio5IZJaZlgeTDIzapef1jRMyaAw9JYtFQPuym16p+4TmTfghG+F71W+X+AcXnHjEa67jk/f3o0Xw/V7EnOaREe+Ge5C9qtMkqB4Mi5po3S4ueIy4CSx8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=majYxb9z; 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="majYxb9z" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD3A9C116C6; Fri, 16 Jan 2026 14:53:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575220; bh=cektvtI2SQiWO5ottxWjn0y5YqwpxHFc7QfJqdyaGjA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=majYxb9zCy1nbQRA1636ysfFV2EK0/qpH4WyUbSc4F11FOMivnoCU9dWsqRzdXQgO 0MLmU7Iotug0Svmin6qEo2i1OFpka0D0pxed3kZHXuclBjeyOb9jqgwcmlGfY4sDbp gTbntQVtC6lzNf/gQvADwtKPpq2oBLDumuiuXSG3Kg2h0ar+0eIT8+KEN/d045w8+6 82dvl0WaoB4Ywhlw/3Uf6eUdwdbcpYIAG4ZCujx8X6rcyhTTcP/60iw490KEUqeb80 dw8qTk9m3iTx/4o116/7uPT8R2t7A6lLVjoZoB8HZ9lBMYbZRP+h+TxnFcGco+/gcK ynzL8m/vHtxdg== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 11/15] tick/sched: Account tickless idle cputime only when tick is stopped Date: Fri, 16 Jan 2026 15:52:04 +0100 Message-ID: <20260116145208.87445-12-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" There is no real point in switching to dyntick-idle cputime accounting mode if the tick is not actually stopped. This just adds overhead, notably fetching the GTOD, on each idle exit and each idle IRQ entry for no reason during short idle trips. Signed-off-by: Frederic Weisbecker --- kernel/time/tick-sched.c | 44 ++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index cbd645fb8df6..05da130d257a 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -1135,8 +1135,10 @@ void tick_nohz_idle_stop_tick(void) ts->idle_sleeps++; ts->idle_expires =3D expires; =20 - if (!was_stopped && tick_sched_flag_test(ts, TS_FLAG_STOPPED)) + if (!was_stopped && tick_sched_flag_test(ts, TS_FLAG_STOPPED)) { + kcpustat_dyntick_start(ts->idle_entrytime); nohz_balance_enter_idle(cpu); + } } else { tick_nohz_retain_tick(ts); } @@ -1177,7 +1179,6 @@ void tick_nohz_idle_enter(void) ts =3D this_cpu_ptr(&tick_cpu_sched); WARN_ON_ONCE(ts->timer_expires_base); ts->idle_entrytime =3D ktime_get(); - kcpustat_dyntick_start(ts->idle_entrytime); tick_nohz_clock_sleep(ts); =20 local_irq_enable(); @@ -1207,9 +1208,10 @@ void tick_nohz_irq_exit(void) struct tick_sched *ts =3D this_cpu_ptr(&tick_cpu_sched); =20 if (tick_sched_flag_test(ts, TS_FLAG_INIDLE)) { - ts->idle_entrytime =3D ktime_get(); - kcpustat_irq_exit(ts->idle_entrytime); tick_nohz_clock_sleep(ts); + ts->idle_entrytime =3D ktime_get(); + if (tick_sched_flag_test(ts, TS_FLAG_STOPPED)) + kcpustat_irq_exit(ts->idle_entrytime); } else { tick_nohz_full_update_tick(ts); } @@ -1310,8 +1312,11 @@ void tick_nohz_idle_restart_tick(void) { struct tick_sched *ts =3D this_cpu_ptr(&tick_cpu_sched); =20 - if (tick_sched_flag_test(ts, TS_FLAG_STOPPED)) - tick_nohz_restart_sched_tick(ts, ktime_get()); + if (tick_sched_flag_test(ts, TS_FLAG_STOPPED)) { + ktime_t now =3D ktime_get(); + kcpustat_dyntick_stop(now); + tick_nohz_restart_sched_tick(ts, now); + } } =20 static void tick_nohz_idle_update_tick(struct tick_sched *ts, ktime_t now) @@ -1341,7 +1346,6 @@ static void tick_nohz_idle_update_tick(struct tick_sc= hed *ts, ktime_t now) void tick_nohz_idle_exit(void) { struct tick_sched *ts =3D this_cpu_ptr(&tick_cpu_sched); - bool idle_active, tick_stopped; ktime_t now; =20 local_irq_disable(); @@ -1350,18 +1354,13 @@ void tick_nohz_idle_exit(void) WARN_ON_ONCE(ts->timer_expires_base); =20 tick_sched_flag_clear(ts, TS_FLAG_INIDLE); - idle_active =3D tick_sched_flag_test(ts, TS_FLAG_IDLE_ACTIVE); - tick_stopped =3D tick_sched_flag_test(ts, TS_FLAG_STOPPED); + tick_nohz_clock_wakeup(ts); =20 - if (idle_active || tick_stopped) + if (tick_sched_flag_test(ts, TS_FLAG_STOPPED)) { now =3D ktime_get(); - - if (idle_active) - tick_nohz_clock_wakeup(ts); - - if (tick_stopped) + kcpustat_dyntick_stop(now); tick_nohz_idle_update_tick(ts, now); - kcpustat_dyntick_stop(now); + } =20 local_irq_enable(); } @@ -1415,15 +1414,13 @@ static inline void tick_nohz_irq_enter(void) struct tick_sched *ts =3D this_cpu_ptr(&tick_cpu_sched); ktime_t now; =20 - if (!tick_sched_flag_test(ts, TS_FLAG_STOPPED | TS_FLAG_IDLE_ACTIVE)) + tick_nohz_clock_wakeup(ts); + + if (!tick_sched_flag_test(ts, TS_FLAG_STOPPED)) return; =20 now =3D ktime_get(); - - if (tick_sched_flag_test(ts, TS_FLAG_IDLE_ACTIVE)) { - tick_nohz_clock_wakeup(ts); - kcpustat_irq_enter(now); - } + kcpustat_irq_enter(now); =20 /* * If all CPUs are idle we may need to update a stale jiffies value. @@ -1432,8 +1429,7 @@ static inline void tick_nohz_irq_enter(void) * rare case (typically stop machine). So we must make sure we have a * last resort. */ - if (tick_sched_flag_test(ts, TS_FLAG_STOPPED)) - tick_nohz_update_jiffies(now); + tick_nohz_update_jiffies(now); } =20 #else --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 8334B274FC1; Fri, 16 Jan 2026 14:53:48 +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=1768575228; cv=none; b=DiUGYfEkE7m+tiLC+iBrLNm97BmpCpY1rR/23xw3VzUZ/8+sDcWZ8tG5+FOUh0ILJIrLxd7ajeT+DQtH7RohnhM9lWRavl/AaI0WC9oFvrh3ChvuaV5bpqTWqSD0QopLLBMp0Lv82G0w9ML99LSTua8FI7AaFyqXLVBDO6tzU4s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575228; c=relaxed/simple; bh=6rMuEpbQZXvwjKA1HjWhJVfT/gG1/xfSb0ElvkTOWjQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ofVqpmgH1ExCiWdNm1j/WveUa9b6NdqfjKpFZDnDKvf3a7aF4b5AYCVRCLaz3et7NAxtg6pB01NjV7w2RbsQIwrea0zlw8Kz09WusTazw8qTO4ujOgv4RkmFy9wNTvX3WJG3/SD/Vpnn4nFxeRHjg43aWHVDi69GgUdAnibmi/Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CNHyGy9x; 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="CNHyGy9x" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4FE2AC16AAE; Fri, 16 Jan 2026 14:53:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575228; bh=6rMuEpbQZXvwjKA1HjWhJVfT/gG1/xfSb0ElvkTOWjQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CNHyGy9x0ZCCX3hcPWuzKQoZdn2unDE3tiZ3OHWATurUeRfzrcyer0Bizt3PhEHNs VeXOLM91VS3T7jU6c+Oomdzra6apJuw5lvulqLk2RNJM8W2atJwfXU3bmOQrRhcMGK dhLpJHmLnLCrzmHDvtyJNh5lG2FqU6eGSaVbvu6dpyoc99Q46h6YEkbnB8NxttweMt 4+T3RxEeQPIACQs50+Rr0IclsAjLeekt5UsMdBaVhxEwjWRULX/BeE9qy/w1yrQ3ET 3arl4YDBDpRDM6J9kZ5pl1ddZb/l311wmJrjTSkyvPPaYoij16nZ7tekNqE3TrLhNP zte6143jEN7qg== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 12/15] tick/sched: Consolidate idle time fetching APIs Date: Fri, 16 Jan 2026 15:52:05 +0100 Message-ID: <20260116145208.87445-13-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" Fetching the idle cputime is available through a variety of accessors all over the place depending on the different accounting flavours and needs: - idle vtime generic accounting can be accessed by kcpustat_field(), kcpustat_cpu_fetch(), or get_idle/iowait_time() but not by get_cpu_idle/iowait_time_us() - dynticks-idle accounting can only be accessed by get_idle/iowait_time() or get_cpu_idle/iowait_time_us() - CONFIG_NO_HZ_COMMON=3Dn idle accounting can be accessed by kcpustat_field= () kcpustat_cpu_fetch(), or get_idle/iowait_time() but not by get_cpu_idle/iowait_time_us() Moreover get_idle/iowait_time() relies on get_cpu_idle/iowait_time_us() with a non-sensical conversion to microseconds and back to nanoseconds on the way. Start consolidating the APIs with removing get_idle/iowait_time() and make kcpustat_field() and kcpustat_cpu_fetch() work for all cases. Signed-off-by: Frederic Weisbecker --- fs/proc/stat.c | 40 +++----------------------- fs/proc/uptime.c | 8 ++---- include/linux/kernel_stat.h | 34 +++++++++++++++++++--- kernel/sched/cputime.c | 57 ++++++++++++++++++++++++++----------- 4 files changed, 76 insertions(+), 63 deletions(-) diff --git a/fs/proc/stat.c b/fs/proc/stat.c index 6ac2a13b8be5..c00468a83f64 100644 --- a/fs/proc/stat.c +++ b/fs/proc/stat.c @@ -22,38 +22,6 @@ #define arch_irq_stat() 0 #endif =20 -u64 get_idle_time(struct kernel_cpustat *kcs, int cpu) -{ - u64 idle, idle_usecs =3D -1ULL; - - if (cpu_online(cpu)) - idle_usecs =3D get_cpu_idle_time_us(cpu, NULL); - - if (idle_usecs =3D=3D -1ULL) - /* !NO_HZ or cpu offline or vtime so we can rely on cpustat.idle */ - idle =3D kcpustat_field(CPUTIME_IDLE, cpu); - else - idle =3D idle_usecs * NSEC_PER_USEC; - - return idle; -} - -static u64 get_iowait_time(struct kernel_cpustat *kcs, int cpu) -{ - u64 iowait, iowait_usecs =3D -1ULL; - - if (cpu_online(cpu)) - iowait_usecs =3D get_cpu_iowait_time_us(cpu, NULL); - - if (iowait_usecs =3D=3D -1ULL) - /* !NO_HZ or cpu offline or vtime so we can rely on cpustat.iowait */ - iowait =3D kcpustat_field(CPUTIME_IOWAIT, cpu); - else - iowait =3D iowait_usecs * NSEC_PER_USEC; - - return iowait; -} - static void show_irq_gap(struct seq_file *p, unsigned int gap) { static const char zeros[] =3D " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"; @@ -105,8 +73,8 @@ static int show_stat(struct seq_file *p, void *v) user +=3D cpustat[CPUTIME_USER]; nice +=3D cpustat[CPUTIME_NICE]; system +=3D cpustat[CPUTIME_SYSTEM]; - idle +=3D get_idle_time(&kcpustat, i); - iowait +=3D get_iowait_time(&kcpustat, i); + idle +=3D cpustat[CPUTIME_IDLE]; + iowait +=3D cpustat[CPUTIME_IOWAIT]; irq +=3D cpustat[CPUTIME_IRQ]; softirq +=3D cpustat[CPUTIME_SOFTIRQ]; steal +=3D cpustat[CPUTIME_STEAL]; @@ -146,8 +114,8 @@ static int show_stat(struct seq_file *p, void *v) user =3D cpustat[CPUTIME_USER]; nice =3D cpustat[CPUTIME_NICE]; system =3D cpustat[CPUTIME_SYSTEM]; - idle =3D get_idle_time(&kcpustat, i); - iowait =3D get_iowait_time(&kcpustat, i); + idle =3D cpustat[CPUTIME_IDLE]; + iowait =3D cpustat[CPUTIME_IOWAIT]; irq =3D cpustat[CPUTIME_IRQ]; softirq =3D cpustat[CPUTIME_SOFTIRQ]; steal =3D cpustat[CPUTIME_STEAL]; diff --git a/fs/proc/uptime.c b/fs/proc/uptime.c index b5343d209381..433aa947cd57 100644 --- a/fs/proc/uptime.c +++ b/fs/proc/uptime.c @@ -18,12 +18,8 @@ static int uptime_proc_show(struct seq_file *m, void *v) int i; =20 idle_nsec =3D 0; - for_each_possible_cpu(i) { - struct kernel_cpustat kcs; - - kcpustat_cpu_fetch(&kcs, i); - idle_nsec +=3D get_idle_time(&kcs, i); - } + for_each_possible_cpu(i) + idle_nsec +=3D kcpustat_field(CPUTIME_IDLE, i); =20 ktime_get_boottime_ts64(&uptime); timens_add_boottime(&uptime); diff --git a/include/linux/kernel_stat.h b/include/linux/kernel_stat.h index a906492eb680..e1efd26e56f0 100644 --- a/include/linux/kernel_stat.h +++ b/include/linux/kernel_stat.h @@ -110,32 +110,59 @@ extern void kcpustat_dyntick_start(ktime_t now); extern void kcpustat_dyntick_stop(ktime_t now); extern void kcpustat_irq_enter(ktime_t now); extern void kcpustat_irq_exit(ktime_t now); +extern u64 kcpustat_field_idle(int cpu); +extern u64 kcpustat_field_iowait(int cpu); =20 static inline bool kcpustat_idle_dyntick(void) { return __this_cpu_read(kernel_cpustat.idle_dyntick); } #else +static inline u64 kcpustat_field_idle(int cpu) +{ + return kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE]; +} +static inline u64 kcpustat_field_iowait(int cpu) +{ + return kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT]; +} + static inline bool kcpustat_idle_dyntick(void) { return false; } #endif /* CONFIG_NO_HZ_COMMON */ =20 +/* Fetch cputime values when vtime is disabled on a CPU */ +static inline u64 kcpustat_field_default(enum cpu_usage_stat usage, int cp= u) +{ + if (usage =3D=3D CPUTIME_IDLE) + return kcpustat_field_idle(cpu); + if (usage =3D=3D CPUTIME_IOWAIT) + return kcpustat_field_iowait(cpu); + return kcpustat_cpu(cpu).cpustat[usage]; +} + +static inline void kcpustat_cpu_fetch_default(struct kernel_cpustat *dst, = int cpu) +{ + *dst =3D kcpustat_cpu(cpu); + dst->cpustat[CPUTIME_IDLE] =3D kcpustat_field_idle(cpu); + dst->cpustat[CPUTIME_IOWAIT] =3D kcpustat_field_iowait(cpu); +} + #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN extern u64 kcpustat_field(enum cpu_usage_stat usage, int cpu); extern void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu); #else static inline u64 kcpustat_field(enum cpu_usage_stat usage, int cpu) { - return kcpustat_cpu(cpu).cpustat[usage]; + return kcpustat_field_default(usage, cpu); } =20 static inline void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu) { - *dst =3D kcpustat_cpu(cpu); + kcpustat_cpu_fetch_default(dst, cpu); } - #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_GEN */ =20 extern void account_user_time(struct task_struct *, u64); @@ -145,7 +172,6 @@ extern void account_system_index_time(struct task_struc= t *, u64, enum cpu_usage_stat); extern void account_steal_time(u64); extern void account_idle_time(u64); -extern u64 get_idle_time(struct kernel_cpustat *kcs, int cpu); =20 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE static inline void account_process_tick(struct task_struct *tsk, int user) diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index 16d6730efe6d..9906abe5d7bc 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -475,21 +475,14 @@ void kcpustat_irq_exit(ktime_t now) kcpustat_idle_start(kc, now); } =20 -static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usage_stat idx, - bool compute_delta, u64 *last_update_time) +static u64 kcpustat_field_dyntick(int cpu, enum cpu_usage_stat idx, + bool compute_delta, ktime_t now) { struct kernel_cpustat *kc =3D &kcpustat_cpu(cpu); u64 *cpustat =3D kc->cpustat; - ktime_t now, idle; + ktime_t idle; unsigned int seq; =20 - if (vtime_generic_enabled_cpu(cpu)) - return -1; - - now =3D ktime_get(); - if (last_update_time) - *last_update_time =3D ktime_to_us(now); - do { seq =3D read_seqcount_begin(&kc->idle_sleeptime_seq); =20 @@ -502,7 +495,38 @@ static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usa= ge_stat idx, } } while (read_seqcount_retry(&kc->idle_sleeptime_seq, seq)); =20 - return ktime_to_us(idle); + return idle; +} + +u64 kcpustat_field_idle(int cpu) +{ + return kcpustat_field_dyntick(cpu, CPUTIME_IDLE, + !nr_iowait_cpu(cpu), ktime_get()); +} +EXPORT_SYMBOL_GPL(kcpustat_field_idle); + +u64 kcpustat_field_iowait(int cpu) +{ + return kcpustat_field_dyntick(cpu, CPUTIME_IOWAIT, + nr_iowait_cpu(cpu), ktime_get()); +} +EXPORT_SYMBOL_GPL(kcpustat_field_iowait); + +static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usage_stat idx, + bool compute_delta, u64 *last_update_time) +{ + ktime_t now =3D ktime_get(); + u64 res; + + if (vtime_generic_enabled_cpu(cpu)) + return -1; + else + res =3D kcpustat_field_dyntick(cpu, idx, compute_delta, now); + + if (last_update_time) + *last_update_time =3D ktime_to_us(now); + + return ktime_to_us(res); } =20 /** @@ -552,7 +576,6 @@ u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_ti= me) nr_iowait_cpu(cpu), last_update_time); } EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us); - #endif /* CONFIG_NO_HZ_COMMON */ =20 /* @@ -1110,8 +1133,8 @@ u64 kcpustat_field(enum cpu_usage_stat usage, int cpu) struct rq *rq; int err; =20 - if (!vtime_accounting_enabled_cpu(cpu)) - return val; + if (!vtime_generic_enabled_cpu(cpu)) + return kcpustat_field_default(usage, cpu); =20 rq =3D cpu_rq(cpu); =20 @@ -1206,8 +1229,8 @@ void kcpustat_cpu_fetch(struct kernel_cpustat *dst, i= nt cpu) struct rq *rq; int err; =20 - if (!vtime_accounting_enabled_cpu(cpu)) { - *dst =3D *src; + if (!vtime_generic_enabled_cpu(cpu)) { + kcpustat_cpu_fetch_default(dst, cpu); return; } =20 @@ -1220,7 +1243,7 @@ void kcpustat_cpu_fetch(struct kernel_cpustat *dst, i= nt cpu) curr =3D rcu_dereference(rq->curr); if (WARN_ON_ONCE(!curr)) { rcu_read_unlock(); - *dst =3D *src; + kcpustat_cpu_fetch_default(dst, cpu); return; } =20 --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 EBA6438A2AE; Fri, 16 Jan 2026 14:53:55 +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=1768575236; cv=none; b=YKCI9iAYy/1FxVEi5ZWiNZYd+TggcFmohKVv6ZuysmWmJux0te0AA3NoQlul79EE2cSxYtXaLzPG6j8maeYQQzs/IkmUZDv50I623X7YjZp4fktuyt7vAgY2MA64IaWDj8QklKKnxGTFFUKCCMd3mE7EK/3msLbNeTrKavNhzT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575236; c=relaxed/simple; bh=8v3ULYDqt0dF0OoyYvjCFXVF3r/R8dp2K5PMXJFm6yY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jl3B62ObX2V5KqqnZ5NY9iTQGpGNP8WPAImGcr66qVOX0GWqzKsBbBPIZlWEIp2YQqmKpofoKp04jenXYh/fkZKfOiMkpixiCObwSspmUGF363k3HUtRB6NpD2zO+is9sKhOzzofpnKoO2XHuMYB5vJToRCHPyW/2otrRx6f7iE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QTuE+pev; 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="QTuE+pev" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8BFCFC116C6; Fri, 16 Jan 2026 14:53:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575235; bh=8v3ULYDqt0dF0OoyYvjCFXVF3r/R8dp2K5PMXJFm6yY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QTuE+pevi7Ewtc5J4XzS2UyPIDX6nKym9hfu473HaWWIdWlledSc2OUe2hDm9GjOu aIpXiBUIscS+O2fSFPLXX3Tag46bUkGLCfL75SiNLjoAITlPVWYZuanFHF6VjbaKdw daTCh/g4e/qMOdWH3BpJWKAuEzWjK/sDbXfsXBnjZ5mVW6FI5Z33aH80Afp37MySD8 Jxi8QkRD+ZeXuYuXyAgdatTG1bSK6XRmNw9+ZaYjhhSMzGB8vaMxhspoZJfxHBSPo3 CkHOTOCFZB2yfJWRjUQfoFmcssxDFrE+v0AAlOiNVBOdxgzmUMTuR4lKnN+oaWQQoG mNnXatX/ElVkg== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 13/15] sched/cputime: Consolidate get_cpu_[idle|iowait]_time_us() Date: Fri, 16 Jan 2026 15:52:06 +0100 Message-ID: <20260116145208.87445-14-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" get_cpu_idle/iowait_time_us() may ultimately fail if generic vtime accounting is enabled. The ad-hoc replacement solution by cpufreq is to compute jiffies minus the whole busy cputime. Although the intention should provide a coherent low resolution estimation of the idle and iowait time, the implementation is buggy because jiffies don't start at 0. Enhance instead get_cpu_[idle|iowait]_time_us() to provide support for vtime generic accounting. Signed-off-by: Frederic Weisbecker --- drivers/cpufreq/cpufreq.c | 29 +---------------------------- include/linux/kernel_stat.h | 3 +++ include/linux/tick.h | 4 ---- kernel/sched/cputime.c | 14 ++++++++++---- 4 files changed, 14 insertions(+), 36 deletions(-) diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index 4472bb1ec83c..ecb9634cd06b 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -130,38 +130,11 @@ struct kobject *get_governor_parent_kobj(struct cpufr= eq_policy *policy) } EXPORT_SYMBOL_GPL(get_governor_parent_kobj); =20 -static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) -{ - struct kernel_cpustat kcpustat; - u64 cur_wall_time; - u64 idle_time; - u64 busy_time; - - cur_wall_time =3D jiffies64_to_nsecs(get_jiffies_64()); - - kcpustat_cpu_fetch(&kcpustat, cpu); - - busy_time =3D kcpustat.cpustat[CPUTIME_USER]; - busy_time +=3D kcpustat.cpustat[CPUTIME_SYSTEM]; - busy_time +=3D kcpustat.cpustat[CPUTIME_IRQ]; - busy_time +=3D kcpustat.cpustat[CPUTIME_SOFTIRQ]; - busy_time +=3D kcpustat.cpustat[CPUTIME_STEAL]; - busy_time +=3D kcpustat.cpustat[CPUTIME_NICE]; - - idle_time =3D cur_wall_time - busy_time; - if (wall) - *wall =3D div_u64(cur_wall_time, NSEC_PER_USEC); - - return div_u64(idle_time, NSEC_PER_USEC); -} - u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) { u64 idle_time =3D get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); =20 - if (idle_time =3D=3D -1ULL) - return get_cpu_idle_time_jiffy(cpu, wall); - else if (!io_busy) + if (!io_busy) idle_time +=3D get_cpu_iowait_time_us(cpu, wall); =20 return idle_time; diff --git a/include/linux/kernel_stat.h b/include/linux/kernel_stat.h index e1efd26e56f0..e59916477075 100644 --- a/include/linux/kernel_stat.h +++ b/include/linux/kernel_stat.h @@ -133,6 +133,9 @@ static inline bool kcpustat_idle_dyntick(void) } #endif /* CONFIG_NO_HZ_COMMON */ =20 +extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time); +extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time); + /* Fetch cputime values when vtime is disabled on a CPU */ static inline u64 kcpustat_field_default(enum cpu_usage_stat usage, int cp= u) { diff --git a/include/linux/tick.h b/include/linux/tick.h index ac76ae9fa36d..1296cba67bee 100644 --- a/include/linux/tick.h +++ b/include/linux/tick.h @@ -138,8 +138,6 @@ extern bool tick_nohz_idle_got_tick(void); extern ktime_t tick_nohz_get_next_hrtimer(void); extern ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next); extern unsigned long tick_nohz_get_idle_calls_cpu(int cpu); -extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time); -extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time); #else /* !CONFIG_NO_HZ_COMMON */ #define tick_nohz_enabled (0) static inline int tick_nohz_tick_stopped(void) { return 0; } @@ -160,8 +158,6 @@ static inline ktime_t tick_nohz_get_sleep_length(ktime_= t *delta_next) *delta_next =3D TICK_NSEC; return *delta_next; } -static inline u64 get_cpu_idle_time_us(int cpu, u64 *unused) { return -1; } -static inline u64 get_cpu_iowait_time_us(int cpu, u64 *unused) { return -1= ; } #endif /* !CONFIG_NO_HZ_COMMON */ =20 /* diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index 9906abe5d7bc..f0620b429698 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -511,6 +511,13 @@ u64 kcpustat_field_iowait(int cpu) nr_iowait_cpu(cpu), ktime_get()); } EXPORT_SYMBOL_GPL(kcpustat_field_iowait); +#else +static u64 kcpustat_field_dyntick(int cpu, enum cpu_usage_stat idx, + bool compute_delta, ktime_t now) +{ + return kcpustat_cpu(cpu).cpustat[idx]; +} +#endif /* CONFIG_NO_HZ_COMMON */ =20 static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usage_stat idx, bool compute_delta, u64 *last_update_time) @@ -519,7 +526,7 @@ static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usag= e_stat idx, u64 res; =20 if (vtime_generic_enabled_cpu(cpu)) - return -1; + res =3D kcpustat_field(idx, cpu); else res =3D kcpustat_field_dyntick(cpu, idx, compute_delta, now); =20 @@ -544,7 +551,7 @@ static u64 get_cpu_sleep_time_us(int cpu, enum cpu_usag= e_stat idx, * This time is measured via accounting rather than sampling, * and is as accurate as ktime_get() is. * - * Return: -1 if generic vtime is enabled, else total idle time of the @cpu + * Return: total idle time of the @cpu */ u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time) { @@ -568,7 +575,7 @@ EXPORT_SYMBOL_GPL(get_cpu_idle_time_us); * This time is measured via accounting rather than sampling, * and is as accurate as ktime_get() is. * - * Return: -1 if generic vtime is enabled, else total iowait time of @cpu + * Return: total iowait time of @cpu */ u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time) { @@ -576,7 +583,6 @@ u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_ti= me) nr_iowait_cpu(cpu), last_update_time); } EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us); -#endif /* CONFIG_NO_HZ_COMMON */ =20 /* * Use precise platform statistics if available: --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 4C51D394493; Fri, 16 Jan 2026 14:54:02 +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=1768575243; cv=none; b=e2RorPuM4sCBOAc9KIlw8rMmoe6lieWgX640wm0w6kv0pj9ulnUsY6uQVpnWY3rjy7YYhnFIScbGz7MCpZST9CIQCByoN0OBcDnVVcHJF+P7T+niybFnTRX1cuRkyHt73UKtkAPa/sQiPYirXBeLnjKKe15Ijf8FPqfBZdT4PiM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575243; c=relaxed/simple; bh=mk05EtvK6jmV7aIMR9OssU/GyByeNnVfxkVqMJUomLs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FdIBfTPNhyavZfVtjBw+z7R0MkELpApEiN2JdGxuJgzI/U8+79RaiqaDOoNm4uftAfjCsfGUoABojmyvcWZ+X8n0hsDpbkpApnbuxg8mVkMhDmPhDPsd95JxrhwZ+xDWi2AOiwg20wqXbC18DLeTDNGmsAm47LIsz+7Jd2bn38Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jcKqW+8Y; 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="jcKqW+8Y" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E67EAC16AAE; Fri, 16 Jan 2026 14:53:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575242; bh=mk05EtvK6jmV7aIMR9OssU/GyByeNnVfxkVqMJUomLs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jcKqW+8YV5y1IBAUOSldpi6lBEuvnxsszdxaFzWfPn7SddeH5Def9WFC2Py2Rf3s1 kqIvancp1XfneOrmmW7MKk5yddG/PHjbkzUFSorJymu0aooz5hahZ1Lh0+T6opdkOE fJfDF5W5QOx+FeE7+vrYNsd3pwntG6OxOiuiLUZ5aXcRrjlaLw3h0n9HQaRPsIFyAy ZStxpfLCZ61SMsXAucgtCaUGybpk84IKlSdjQeYajSIeOCaD6y1U0Bt/xDBqdj687t DS12t/MQtuMmnyzcmHSN6oYAiLgwKh3WvZLzWugRIROVvRXaE3Cs/JV4cuHcNtZK/R uvWyiDRhINhcg== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 14/15] sched/cputime: Handle idle irqtime gracefully Date: Fri, 16 Jan 2026 15:52:07 +0100 Message-ID: <20260116145208.87445-15-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" The dyntick-idle cputime accounting always assumes that IRQ time accounting is enabled and consequently stops elapsing the idle time during dyntick-idle IRQs. This doesn't mix up well with disabled IRQ time accounting because then idle IRQs become a cputime blind-spot. Also this feature is disabled on most configurations and the overhead of pausing dyntick-idle accounting while in idle IRQs could then be avoided. Fix the situation with conditionally pausing dyntick-idle accounting during idle IRQs only if neither native vtime (which does IRQ time accounting) nor generic IRQ time accounting are enabled. Also make sure that the accumulated IRQ time is not accidentally substracted from later accounting. Signed-off-by: Frederic Weisbecker --- kernel/sched/cputime.c | 24 +++++++++++++++++++++--- kernel/sched/sched.h | 1 + 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index f0620b429698..3dadfaa92b27 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -45,7 +45,8 @@ static void irqtime_account_delta(struct irqtime *irqtime= , u64 delta, u64_stats_update_begin(&irqtime->sync); cpustat[idx] +=3D delta; irqtime->total +=3D delta; - irqtime->tick_delta +=3D delta; + if (!irqtime->idle_dyntick) + irqtime->tick_delta +=3D delta; u64_stats_update_end(&irqtime->sync); } =20 @@ -80,6 +81,16 @@ void irqtime_account_irq(struct task_struct *curr, unsig= ned int offset) irqtime_account_delta(irqtime, delta, CPUTIME_SOFTIRQ); } =20 +static inline void irqtime_dyntick_start(void) +{ + __this_cpu_write(cpu_irqtime.idle_dyntick, true); +} + +static inline void irqtime_dyntick_stop(void) +{ + __this_cpu_write(cpu_irqtime.idle_dyntick, false); +} + static u64 irqtime_tick_accounted(u64 maxtime) { struct irqtime *irqtime =3D this_cpu_ptr(&cpu_irqtime); @@ -93,6 +104,9 @@ static u64 irqtime_tick_accounted(u64 maxtime) =20 #else /* !CONFIG_IRQ_TIME_ACCOUNTING: */ =20 +static inline void irqtime_dyntick_start(void) { } +static inline void irqtime_dyntick_stop(void) { } + static u64 irqtime_tick_accounted(u64 dummy) { return 0; @@ -443,6 +457,7 @@ void kcpustat_dyntick_stop(ktime_t now) WARN_ON_ONCE(!kc->idle_dyntick); kcpustat_idle_stop(kc, now); kc->idle_dyntick =3D false; + irqtime_dyntick_stop(); vtime_dyntick_stop(); steal_account_process_time(ULONG_MAX); } @@ -454,6 +469,7 @@ void kcpustat_dyntick_start(ktime_t now) =20 if (!vtime_generic_enabled_this_cpu()) { vtime_dyntick_start(); + irqtime_dyntick_start(); kc->idle_dyntick =3D true; kcpustat_idle_start(kc, now); } @@ -463,7 +479,8 @@ void kcpustat_irq_enter(ktime_t now) { struct kernel_cpustat *kc =3D kcpustat_this_cpu; =20 - if (!vtime_generic_enabled_this_cpu()) + if (!vtime_generic_enabled_this_cpu() && + (irqtime_enabled() || vtime_accounting_enabled_this_cpu())) kcpustat_idle_stop(kc, now); } =20 @@ -471,7 +488,8 @@ void kcpustat_irq_exit(ktime_t now) { struct kernel_cpustat *kc =3D kcpustat_this_cpu; =20 - if (!vtime_generic_enabled_this_cpu()) + if (!vtime_generic_enabled_this_cpu() && + (irqtime_enabled() || vtime_accounting_enabled_this_cpu())) kcpustat_idle_start(kc, now); } =20 diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index d30cca6870f5..cf677ff12b10 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3307,6 +3307,7 @@ static inline void sched_core_tick(struct rq *rq) { } #ifdef CONFIG_IRQ_TIME_ACCOUNTING =20 struct irqtime { + bool idle_dyntick; u64 total; u64 tick_delta; u64 irq_start_time; --=20 2.51.1 From nobody Sun Feb 8 05:27:48 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 7B1833939B9; Fri, 16 Jan 2026 14:54:10 +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=1768575250; cv=none; b=MdrcWGyUOLcDdu6d4KdglETnztOseyJfDN3gP5sYw+mcZKO0Q9llPno40e9h0ZxR73JEx45BPvRXAJLUkanUxPAUoHY3WxiblIE7gCDSJ8QSyVr3j4B2di1ZzkLqeRx1qfpwdnw5t1tk8qkkD3Q7HK9nwQkW9Qgjmpuy8nSSpW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768575250; c=relaxed/simple; bh=NiFNP1dNEv/R0O+iuBotsMn8keb9Y+JzQXP3O6DHJSw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sVlpf1DtfKJ3mqV7Z1DQI47GgGOEwqjnpmv4j67EE9vVjWRJWmwd+EhteHnjtvrAowkCUuJPExnzQQMVTUC71Wmuvms08KBdI9GHa5HhsmQv8LthF2M59nzjsQDzaBa6Y8JYr2w9F8KNlSHXb1TaMTJbpFXPWswTVyxrQeA4650= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=P58I4pON; 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="P58I4pON" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4568DC19423; Fri, 16 Jan 2026 14:54:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1768575250; bh=NiFNP1dNEv/R0O+iuBotsMn8keb9Y+JzQXP3O6DHJSw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P58I4pONEqgRKgQ76ZkWUDt5XWzZMMI1w+pz+lcYFsRcbcsZHQupqMSvLSprbGueN b+pk/yo1pIdVpFUg3JXsmxcb/PGfRQU9pNdfd8Jr9hk0A1q6noGtXzxgWyURE9ouC3 1APMRpVzRm8n/zG3SHUGHD64MLVTPwa7TIMgoVeMS6BoBYJPpxS8Mw6kww+qUJDUgN uiIDVVVHWgH05pom4hl7O6l7IccpprH56k6WxOcl/M06cBnf3pySTKttXVjeZktmJd jPd5Cm2EqpHdhEMCmACSzFve7xNaBHvoUtLunX2kvWz82JJJyBOVA8xHbLW4Ftq6Hh ouLpAwlTIoQ0g== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , "Christophe Leroy (CS GROUP)" , "Rafael J. Wysocki" , Alexander Gordeev , Anna-Maria Behnsen , Ben Segall , Boqun Feng , Christian Borntraeger , Dietmar Eggemann , Heiko Carstens , Ingo Molnar , Jan Kiszka , Joel Fernandes , Juri Lelli , Kieran Bingham , Madhavan Srinivasan , Mel Gorman , Michael Ellerman , Neeraj Upadhyay , Nicholas Piggin , "Paul E . McKenney" , Peter Zijlstra , Steven Rostedt , Sven Schnelle , Thomas Gleixner , Uladzislau Rezki , Valentin Schneider , Vasily Gorbik , Vincent Guittot , Viresh Kumar , Xin Zhao , linux-pm@vger.kernel.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH 15/15] sched/cputime: Handle dyntick-idle steal time correctly Date: Fri, 16 Jan 2026 15:52:08 +0100 Message-ID: <20260116145208.87445-16-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260116145208.87445-1-frederic@kernel.org> References: <20260116145208.87445-1-frederic@kernel.org> 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" The dyntick-idle steal time is currently accounted when the tick restarts but the stolen idle time is not substracted from the idle time that was already accounted. This is to avoid observing the idle time going backward as the dyntick-idle cputime accessors can't reliably know in advance the stolen idle time. In order to maintain a forward progressing idle cputime while substracting idle steal time from it, keep track of the previously accounted idle stolen time and substract it from _later_ idle cputime accounting. Signed-off-by: Frederic Weisbecker --- include/linux/kernel_stat.h | 1 + kernel/sched/cputime.c | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/linux/kernel_stat.h b/include/linux/kernel_stat.h index e59916477075..a5b5a25c3cc1 100644 --- a/include/linux/kernel_stat.h +++ b/include/linux/kernel_stat.h @@ -39,6 +39,7 @@ struct kernel_cpustat { bool idle_elapse; seqcount_t idle_sleeptime_seq; ktime_t idle_entrytime; + u64 idle_steal; #endif u64 cpustat[NR_STATS]; }; diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index 3dadfaa92b27..749a6ed4d2fa 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -424,18 +424,25 @@ static void kcpustat_idle_stop(struct kernel_cpustat = *kc, ktime_t now) { u64 *cpustat =3D kc->cpustat; ktime_t delta; + u64 steal, steal_delta; =20 if (!kc->idle_elapse) return; =20 delta =3D ktime_sub(now, kc->idle_entrytime); + steal =3D steal_account_process_time(delta); =20 write_seqcount_begin(&kc->idle_sleeptime_seq); + steal_delta =3D min_t(u64, kc->idle_steal, delta); + delta -=3D steal_delta; + kc->idle_steal -=3D steal_delta; + if (nr_iowait_cpu(smp_processor_id()) > 0) cpustat[CPUTIME_IOWAIT] =3D ktime_add(cpustat[CPUTIME_IOWAIT], delta); else cpustat[CPUTIME_IDLE] =3D ktime_add(cpustat[CPUTIME_IDLE], delta); =20 + kc->idle_steal +=3D steal; kc->idle_entrytime =3D now; kc->idle_elapse =3D false; write_seqcount_end(&kc->idle_sleeptime_seq); @@ -459,7 +466,6 @@ void kcpustat_dyntick_stop(ktime_t now) kc->idle_dyntick =3D false; irqtime_dyntick_stop(); vtime_dyntick_stop(); - steal_account_process_time(ULONG_MAX); } } =20 @@ -507,6 +513,7 @@ static u64 kcpustat_field_dyntick(int cpu, enum cpu_usa= ge_stat idx, if (kc->idle_elapse && compute_delta) { ktime_t delta =3D ktime_sub(now, kc->idle_entrytime); =20 + delta -=3D min_t(u64, kc->idle_steal, (u64)delta); idle =3D ktime_add(cpustat[idx], delta); } else { idle =3D cpustat[idx]; --=20 2.51.1