[PATCH 2/2] powerpc: Few misc cpumask code refactors

Shrikanth Hegde posted 2 patches 6 days, 10 hours ago
[PATCH 2/2] powerpc: Few misc cpumask code refactors
Posted by Shrikanth Hegde 6 days, 10 hours ago
A few miscellanous changes in handling cpumask api's in the order
they appear:

- cpumask_next(cpu, mask) >= nr_cpu_ids is used to determine if
  cpu is the last CPU. 

- cpumask_next can take -1 as valid argument. So simplify cpuinfo
  iterator. /proc/cpuinfo shows same info with patch.

- cpumask_weight can't be more than nr_cpu_ids. So remove the pointless
  comparison.

Signed-off-by: Shrikanth Hegde <sshegde@linux.ibm.com>
---
 arch/powerpc/kernel/setup-common.c | 7 ++-----
 arch/powerpc/sysdev/xive/common.c  | 7 +++----
 2 files changed, 5 insertions(+), 9 deletions(-)

diff --git a/arch/powerpc/kernel/setup-common.c b/arch/powerpc/kernel/setup-common.c
index b1761909c23f..47215eab296b 100644
--- a/arch/powerpc/kernel/setup-common.c
+++ b/arch/powerpc/kernel/setup-common.c
@@ -323,7 +323,7 @@ static int show_cpuinfo(struct seq_file *m, void *v)
 	seq_putc(m, '\n');
 
 	/* If this is the last cpu, print the summary */
-	if (cpumask_next(cpu_id, cpu_online_mask) >= nr_cpu_ids)
+	if (cpu_id == cpumask_last(cpu_online_mask))
 		show_cpuinfo_summary(m);
 
 	return 0;
@@ -331,10 +331,7 @@ static int show_cpuinfo(struct seq_file *m, void *v)
 
 static void *c_start(struct seq_file *m, loff_t *pos)
 {
-	if (*pos == 0)	/* just in case, cpu 0 is not the first */
-		*pos = cpumask_first(cpu_online_mask);
-	else
-		*pos = cpumask_next(*pos - 1, cpu_online_mask);
+	*pos = cpumask_next(*pos - 1, cpu_online_mask);
 	if ((*pos) < nr_cpu_ids)
 		return (void *)(unsigned long)(*pos + 1);
 	return NULL;
diff --git a/arch/powerpc/sysdev/xive/common.c b/arch/powerpc/sysdev/xive/common.c
index b6446abe29a6..91dd3b364d7d 100644
--- a/arch/powerpc/sysdev/xive/common.c
+++ b/arch/powerpc/sysdev/xive/common.c
@@ -548,11 +548,10 @@ static void xive_dec_target_count(int cpu)
 static int xive_find_target_in_mask(const struct cpumask *mask,
 				    unsigned int fuzz)
 {
-	int cpu, first, num, i;
+	int cpu, first, i;
 
-	/* Pick up a starting point CPU in the mask based on  fuzz */
-	num = min_t(int, cpumask_weight(mask), nr_cpu_ids);
-	first = fuzz % num;
+	/* Pick up a starting point CPU in the mask based on fuzz */
+	first = fuzz % cpumask_weight(mask);
 
 	/* Locate it */
 	cpu = cpumask_first(mask);
-- 
2.47.3
Re: [PATCH 2/2] powerpc: Few misc cpumask code refactors
Posted by Yury Norov 4 days, 20 hours ago
On Fri, Mar 27, 2026 at 12:01:29PM +0530, Shrikanth Hegde wrote:
> A few miscellanous changes in handling cpumask api's in the order
> they appear:
> 
> - cpumask_next(cpu, mask) >= nr_cpu_ids is used to determine if
>   cpu is the last CPU. 
> 
> - cpumask_next can take -1 as valid argument. So simplify cpuinfo
>   iterator. /proc/cpuinfo shows same info with patch.
> 
> - cpumask_weight can't be more than nr_cpu_ids. So remove the pointless
>   comparison.
> 
> Signed-off-by: Shrikanth Hegde <sshegde@linux.ibm.com>
> ---
>  arch/powerpc/kernel/setup-common.c | 7 ++-----
>  arch/powerpc/sysdev/xive/common.c  | 7 +++----
>  2 files changed, 5 insertions(+), 9 deletions(-)
> 
> diff --git a/arch/powerpc/kernel/setup-common.c b/arch/powerpc/kernel/setup-common.c
> index b1761909c23f..47215eab296b 100644
> --- a/arch/powerpc/kernel/setup-common.c
> +++ b/arch/powerpc/kernel/setup-common.c
> @@ -323,7 +323,7 @@ static int show_cpuinfo(struct seq_file *m, void *v)
>  	seq_putc(m, '\n');
>  
>  	/* If this is the last cpu, print the summary */
> -	if (cpumask_next(cpu_id, cpu_online_mask) >= nr_cpu_ids)
> +	if (cpu_id == cpumask_last(cpu_online_mask))
>  		show_cpuinfo_summary(m);
>  
>  	return 0;
> @@ -331,10 +331,7 @@ static int show_cpuinfo(struct seq_file *m, void *v)
>  
>  static void *c_start(struct seq_file *m, loff_t *pos)
>  {
> -	if (*pos == 0)	/* just in case, cpu 0 is not the first */
> -		*pos = cpumask_first(cpu_online_mask);
> -	else
> -		*pos = cpumask_next(*pos - 1, cpu_online_mask);
> +	*pos = cpumask_next(*pos - 1, cpu_online_mask);
>  	if ((*pos) < nr_cpu_ids)
>  		return (void *)(unsigned long)(*pos + 1);
>  	return NULL;
> diff --git a/arch/powerpc/sysdev/xive/common.c b/arch/powerpc/sysdev/xive/common.c
> index b6446abe29a6..91dd3b364d7d 100644
> --- a/arch/powerpc/sysdev/xive/common.c
> +++ b/arch/powerpc/sysdev/xive/common.c
> @@ -548,11 +548,10 @@ static void xive_dec_target_count(int cpu)
>  static int xive_find_target_in_mask(const struct cpumask *mask,
>  				    unsigned int fuzz)
>  {
> -	int cpu, first, num, i;
> +	int cpu, first, i;
>  
> -	/* Pick up a starting point CPU in the mask based on  fuzz */
> -	num = min_t(int, cpumask_weight(mask), nr_cpu_ids);
> -	first = fuzz % num;
> +	/* Pick up a starting point CPU in the mask based on fuzz */
> +	first = fuzz % cpumask_weight(mask);

For this function I've got a deeper rework.

https://lore.kernel.org/all/20260319033647.881246-3-ynorov@nvidia.com/

For the rest:
Reviewed-by: Yury Norov <ynorov@nvidia.com>

>  
>  	/* Locate it */
>  	cpu = cpumask_first(mask);
> -- 
> 2.47.3