From nobody Sat Feb 7 21:14:50 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 1AE0F3EF0A3; Fri, 6 Feb 2026 14:24:56 +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=1770387896; cv=none; b=aGH+jJKADAl89IEyQTGeazgUs0DP8aKOFyXQM2y6LWbg5icKUeDQG+VwRqDiPBSSh89Bts4WAyhIaaeZqHdcmqE/t8GFAhEdcdjk0OttOysE8MkQ4YYwNtsNZ/7TFkilnGLxxGlXBXZj3R/b/OMKVmo+gIcKddTWANRLRvzMFM4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770387896; c=relaxed/simple; bh=axD30NXxtXM5SUHtedxFjg90A77tUvhavW17z9RBWt4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pkfLzW+GA/kWg+7lUVYrLH83pbn9ncwl5NVrql2WQ+v3jVODoh9z++9P0IGfPzP+nyJWRwHNUY8m24tuc7sZhhlVrp1AkRTR+ZUWOObNr3mU6o3NM4fY0sI2rHrKY7U7eiG71+9XvLpuIzi7HxKPtsJz6Gc9v1dvHl+SPiHvcY8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JnGDPW3W; 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="JnGDPW3W" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 684F4C116C6; Fri, 6 Feb 2026 14:24:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770387896; bh=axD30NXxtXM5SUHtedxFjg90A77tUvhavW17z9RBWt4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JnGDPW3WKQhZalgvDsfBR/ftmpCbHIg1kQ0jgLm0KnWWKT+zYa23crSRw7f5YRXCM btDzqBLYR0Y0L4Cokn+/iY4Hvkk1LkJUyVU9RhKCw56WiwHT/UxGr31MDM0RACUhrV YlzTAPXCA4NQtRJKJWqG1kMqJAvuoV7QdX7PlPuOIxgYZxAAilXOiHkCS39U7SyzkR 6gVTqKVllraCVZZi/Pl0sOAUW9n76CxeszJ1hzlDS8iF/uHHjHHoHKmekWABTOsAVz eX8l3T5Ax5VY6BHzJDpKsCH3geoZS801SZSmFMGC/Ebc1IKNLQCaeHmaINBKfiIT72 /v4G2qyFhFU9A== 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, Shrikanth Hegde Subject: [PATCH 15/15] sched/cputime: Handle dyntick-idle steal time correctly Date: Fri, 6 Feb 2026 15:22:45 +0100 Message-ID: <20260206142245.58987-16-frederic@kernel.org> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20260206142245.58987-1-frederic@kernel.org> References: <20260206142245.58987-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 | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/linux/kernel_stat.h b/include/linux/kernel_stat.h index 512104b0ff49..24a54a6151ba 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; u64 idle_entrytime; + u64 idle_stealtime; #endif u64 cpustat[NR_STATS]; }; diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index 92fa2f037b6e..7e79288eb327 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -424,19 +424,25 @@ static inline void irqtime_account_process_tick(struc= t task_struct *p, int user_ static void kcpustat_idle_stop(struct kernel_cpustat *kc, u64 now) { u64 *cpustat =3D kc->cpustat; - u64 delta; + u64 delta, steal, steal_delta; =20 if (!kc->idle_elapse) return; =20 delta =3D 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_stealtime, delta); + delta -=3D steal_delta; + kc->idle_stealtime -=3D steal_delta; + if (nr_iowait_cpu(smp_processor_id()) > 0) cpustat[CPUTIME_IOWAIT] +=3D delta; else cpustat[CPUTIME_IDLE] +=3D delta; =20 + kc->idle_stealtime +=3D steal; kc->idle_entrytime =3D now; kc->idle_elapse =3D false; write_seqcount_end(&kc->idle_sleeptime_seq); @@ -460,7 +466,6 @@ void kcpustat_dyntick_stop(u64 now) kc->idle_dyntick =3D false; irqtime_dyntick_stop(); vtime_dyntick_stop(); - steal_account_process_time(ULONG_MAX); } } =20 @@ -505,10 +510,14 @@ static u64 kcpustat_field_dyntick(int cpu, enum cpu_u= sage_stat idx, do { seq =3D read_seqcount_begin(&kc->idle_sleeptime_seq); =20 - if (kc->idle_elapse && compute_delta) - idle =3D cpustat[idx] + (now - kc->idle_entrytime); - else - idle =3D cpustat[idx]; + idle =3D cpustat[idx]; + + if (kc->idle_elapse && compute_delta) { + u64 delta =3D now - kc->idle_entrytime; + + delta -=3D min_t(u64, kc->idle_stealtime, delta); + idle +=3D delta; + } } while (read_seqcount_retry(&kc->idle_sleeptime_seq, seq)); =20 return idle; --=20 2.51.1