[tip: sched/core] sched/cputime: Add kcpustat_field_total helper

From: tip-bot2 for Shrikanth Hegde

Date: Mon Sep 28 2026 - 03:07:30 EST


The following commit has been merged into the sched/core branch of tip:

Commit-ID: a8d0854a76a876ea474e17322acb680716a09da2
Gitweb: https://git.kernel.org/tip/a8d0854a76a876ea474e17322acb680716a09da2
Author: Shrikanth Hegde <sshegde@xxxxxxxxxxxxx>
AuthorDate: Mon, 28 Sep 2026 11:07:16 +05:30
Committer: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
CommitterDate: Mon, 28 Sep 2026 08:44:40 +02:00

sched/cputime: Add kcpustat_field_total helper

Provide a new helper function which sums up a given type of cpustat
over a specified cpumask.

This allows the caller's code to be simpler and avoids duplication.
For example, subsequent patch in the steal governor use this exact
same pattern when calculating steal time.

Number of cpus can be derived from cpumask_weight() where necessary.

Suggested-by: Yury Norov <yury.norov@xxxxxxxxx>
Signed-off-by: Shrikanth Hegde <sshegde@xxxxxxxxxxxxx>
Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
Reviewed-by: Yury Norov <ynorov@xxxxxxxxxx>
Reviewed-by: Mete Durlu <meted@xxxxxxxxxxxxx>
Acked-by: Frederic Weisbecker <frederic@xxxxxxxxxx>
Link: https://patch.msgid.link/20260928053728.797539-2-sshegde@xxxxxxxxxxxxx
---
arch/s390/kernel/hiperdispatch.c | 10 +++-------
fs/proc/uptime.c | 6 +-----
include/linux/kernel_stat.h | 11 +++++++++++
3 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/arch/s390/kernel/hiperdispatch.c b/arch/s390/kernel/hiperdispatch.c
index 2172065..c21496f 100644
--- a/arch/s390/kernel/hiperdispatch.c
+++ b/arch/s390/kernel/hiperdispatch.c
@@ -207,16 +207,12 @@ static unsigned long hd_calculate_steal_percentage(void)
{
unsigned long time_delta, steal_delta, steal, percentage;
static ktime_t prev;
- int cpus, cpu;
+ int cpus;
ktime_t now;

- cpus = 0;
- steal = 0;
percentage = 0;
- for_each_cpu(cpu, &hd_vmvl_cpumask) {
- steal += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
- cpus++;
- }
+ steal = kcpustat_field_total(CPUTIME_STEAL, &hd_vmvl_cpumask);
+ cpus = cpumask_weight(&hd_vmvl_cpumask);
/*
* If there is no vertical medium and low CPUs steal time
* is 0 as vertical high CPUs shouldn't experience steal time.
diff --git a/fs/proc/uptime.c b/fs/proc/uptime.c
index 433aa94..53143c6 100644
--- a/fs/proc/uptime.c
+++ b/fs/proc/uptime.c
@@ -15,12 +15,8 @@ static int uptime_proc_show(struct seq_file *m, void *v)
struct timespec64 idle;
u64 idle_nsec;
u32 rem;
- int i;
-
- idle_nsec = 0;
- for_each_possible_cpu(i)
- idle_nsec += kcpustat_field(CPUTIME_IDLE, i);

+ idle_nsec = kcpustat_field_total(CPUTIME_IDLE, cpu_possible_mask);
ktime_get_boottime_ts64(&uptime);
timens_add_boottime(&uptime);

diff --git a/include/linux/kernel_stat.h b/include/linux/kernel_stat.h
index 9ca6c22..c1e8555 100644
--- a/include/linux/kernel_stat.h
+++ b/include/linux/kernel_stat.h
@@ -196,6 +196,17 @@ static inline void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
}
#endif /* !CONFIG_VIRT_CPU_ACCOUNTING_GEN */

+static inline u64 kcpustat_field_total(enum cpu_usage_stat usage, const struct cpumask *cpus)
+{
+ u64 total = 0;
+ int cpu;
+
+ for_each_cpu(cpu, cpus)
+ total += kcpustat_field(usage, cpu);
+
+ return total;
+}
+
extern void account_user_time(struct task_struct *, u64);
extern void account_guest_time(struct task_struct *, u64);
extern void account_system_time(struct task_struct *, int, u64);