Re: [PATCH v10 01/12] sched/cputime: Add kcpustat_field_total helper

Yury Norov <[email protected]> Wed, 12 Aug 2026 14:44:54 -0400
Newsgroups dev.linux.lists.virtualization,org.kernel.vger.linux-doc,org.kernel.vger.linux-kernel
Message-ID <any_Jv9y6a8WKoL9@yury>
On Wed, Aug 12, 2026 at 11:10:22AM +0530, Shrikanth Hegde wrote:
> 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.
> 
> Suggested-by: Yury Norov <[email protected]>
> Signed-off-by: Shrikanth Hegde <[email protected]>

Reviewed-by: Yury Norov <[email protected]>

> ---
>  arch/s390/kernel/hiperdispatch.c |  8 ++------
>  fs/proc/uptime.c                 |  6 +-----
>  include/linux/kernel_stat.h      | 11 +++++++++++
>  3 files changed, 14 insertions(+), 11 deletions(-)
> 
> diff --git a/arch/s390/kernel/hiperdispatch.c b/arch/s390/kernel/hiperdispatch.c
> index 217206522266..e5c7c818c178 100644
> --- a/arch/s390/kernel/hiperdispatch.c
> +++ b/arch/s390/kernel/hiperdispatch.c
> @@ -210,13 +210,9 @@ static unsigned long hd_calculate_steal_percentage(void)
>  	int cpus, cpu;
>  	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 433aa947cd57..53143c66cbe1 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 9ca6c2259dfe..c1e85550bf12 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);
> -- 
> 2.47.3