Skip to contentSkip to conversation

powerpc: A few misc cpumask refactors

PATCH Superseded Shrikanth Hegde 2 participants 6 messages Mar 27 – Mar 29 PowerPC

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Shrikanth Hegde

author reviewer
#0 lore raw

cover powerpc: A few misc cpumask refactors

While going through the code, spotted these cpumask miscellaneous changes. These are pure code refactors, they dont affect functionality or performance. They just use the updated cpumask api and remove some checks which are not necessary now.

Major one being:
cpu = cpumask_next(cpu, mask)
if (cpu >= nr_cpu_ids)
cpu = cpumask_first(mask)
Above block can be replaced by cpu = cpumask_next_wrap(cpu, mask)

Note that exact same blocks are still present in:
arch/x86/kernel/tsc_sync.c
drivers/char/random.c
drivers/net/wireguard/queueing.h
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c
drivers/net/ethernet/sfc/siena/efx_channels.c
drivers/net/ethernet/sfc/efx_channels.c
kernel/rcu/rcutorture.c

Yury, Rasmus,
Do you think it makes sense to send for the above files too?

Shrikanth Hegde (2):
powerpc: Use cpumask_next_wrap instead
powerpc: Few misc cpumask code refactors

 arch/powerpc/kernel/irq.c             |  5 +----
 arch/powerpc/kernel/setup-common.c    |  7 ++-----
 arch/powerpc/mm/book3s64/hash_utils.c |  4 +---
 arch/powerpc/sysdev/xive/common.c     | 12 ++++--------
 4 files changed, 8 insertions(+), 20 deletions(-)
Signature · 1 line
2.47.3
author reviewer
#1 lore raw

PATCH 1/2 powerpc: Use cpumask_next_wrap instead

cpu = cpumask_next(cpu, mask)
if (cpu >= nr_cpu_ids)
cpu = cpumask_first(mask)

Above block is identical to:
cpu = cpumask_next_wrap(cpu, mask)

Replace it, No change in functionality or performance. Slightly simpler code.

---
 arch/powerpc/kernel/irq.c             | 5 +----
 arch/powerpc/mm/book3s64/hash_utils.c | 4 +---
 arch/powerpc/sysdev/xive/common.c     | 5 +----
 3 files changed, 3 insertions(+), 11 deletions(-)
arch/powerpc/kernel/irq.c +1 -4
index a0e8b998c9b5..f69de08ad347 100644
@@ -370,10 +370,7 @@ int irq_choose_cpu(const struct cpumask *mask)
 do_round_robin:
 		raw_spin_lock_irqsave(&irq_rover_lock, flags);
 
-		irq_rover = cpumask_next(irq_rover, cpu_online_mask);
-		if (irq_rover >= nr_cpu_ids)
-			irq_rover = cpumask_first(cpu_online_mask);
-
+		irq_rover = cpumask_next_wrap(irq_rover, cpu_online_mask);
 		cpuid = irq_rover;
 
 		raw_spin_unlock_irqrestore(&irq_rover_lock, flags);
arch/powerpc/mm/book3s64/hash_utils.c +1 -3
index 9dc5889d6ecb..e4fcf929cb33 100644
@@ -1299,9 +1299,7 @@ static void stress_hpt_timer_fn(struct timer_list *timer)
 	if (!firmware_has_feature(FW_FEATURE_LPAR))
 		tlbiel_all();
 
-	next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
-	if (next_cpu >= nr_cpu_ids)
-		next_cpu = cpumask_first(cpu_online_mask);
+	next_cpu = cpumask_next_wrap(raw_smp_processor_id(), cpu_online_mask);
 	stress_hpt_timer.expires = jiffies + msecs_to_jiffies(10);
 	add_timer_on(&stress_hpt_timer, next_cpu);
 }
arch/powerpc/sysdev/xive/common.c +1 -4
index e1a4f8a97393..b6446abe29a6 100644
@@ -577,10 +577,7 @@ static int xive_find_target_in_mask(const struct cpumask *mask,
 		 */
 		if (cpu_online(cpu) && xive_try_pick_target(cpu))
 			return cpu;
-		cpu = cpumask_next(cpu, mask);
-		/* Wrap around */
-		if (cpu >= nr_cpu_ids)
-			cpu = cpumask_first(mask);
+		cpu = cpumask_next_wrap(cpu, mask);
 	} while (cpu != first);
 
 	return -1;
Signature · 1 line
2.47.3
author reviewer
#4 lore raw

PATCH 2/2 powerpc: Few misc cpumask code refactors

In reply to Shrikanth Hegde

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.

---
 arch/powerpc/kernel/setup-common.c | 7 ++-----
 arch/powerpc/sysdev/xive/common.c  | 7 +++----
 2 files changed, 5 insertions(+), 9 deletions(-)
arch/powerpc/kernel/setup-common.c +2 -5
index b1761909c23f..47215eab296b 100644
@@ -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;
arch/powerpc/sysdev/xive/common.c +3 -4
index b6446abe29a6..91dd3b364d7d 100644
@@ -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);
Signature · 1 line
2.47.3
Reviewed-by on v1 2/2
#5 lore raw

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

Quoted 49 lines · Shrikanth Hegde

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.

---
 arch/powerpc/kernel/setup-common.c | 7 ++-----
 arch/powerpc/sysdev/xive/common.c  | 7 +++----
 2 files changed, 5 insertions(+), 9 deletions(-)
arch/powerpc/kernel/setup-common.c +2 -5
index b1761909c23f..47215eab296b 100644
@@ -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;
arch/powerpc/sysdev/xive/common.c +0 -0
index b6446abe29a6..91dd3b364d7d 100644
@@ -548,11 +548,10 @@ static void xive_dec_target_count(int cpu)
 static int xive_find_target_in_mask(const struct cpumask *mask,

arch/powerpc/sysdev/xive/common.c static void xive_dec_target_count(int cpu) · line 554

Quoted 10 lines
arch/powerpc/sysdev/xive/common.c +3 -4
 				    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/[email protected]/

For the rest:

Quoted 5 lines
arch/powerpc/sysdev/xive/common.c +0 -0
 	/* Locate it */
 	cpu = cpumask_first(mask);
Signature · 1 line
2.47.3
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] powerpc: Use cpumask_next_wrap instead

In reply to Shrikanth Hegde
Quoted 53 lines · Shrikanth Hegde

cpu = cpumask_next(cpu, mask)
if (cpu >= nr_cpu_ids)
cpu = cpumask_first(mask)

Above block is identical to:
cpu = cpumask_next_wrap(cpu, mask)

Replace it, No change in functionality or performance. Slightly simpler code.

---
 arch/powerpc/kernel/irq.c             | 5 +----
 arch/powerpc/mm/book3s64/hash_utils.c | 4 +---
 arch/powerpc/sysdev/xive/common.c     | 5 +----
 3 files changed, 3 insertions(+), 11 deletions(-)
arch/powerpc/kernel/irq.c +1 -4
index a0e8b998c9b5..f69de08ad347 100644
@@ -370,10 +370,7 @@ int irq_choose_cpu(const struct cpumask *mask)
 do_round_robin:
 		raw_spin_lock_irqsave(&irq_rover_lock, flags);
 
-		irq_rover = cpumask_next(irq_rover, cpu_online_mask);
-		if (irq_rover >= nr_cpu_ids)
-			irq_rover = cpumask_first(cpu_online_mask);
-
+		irq_rover = cpumask_next_wrap(irq_rover, cpu_online_mask);
 		cpuid = irq_rover;
 
 		raw_spin_unlock_irqrestore(&irq_rover_lock, flags);
arch/powerpc/mm/book3s64/hash_utils.c +1 -3
index 9dc5889d6ecb..e4fcf929cb33 100644
@@ -1299,9 +1299,7 @@ static void stress_hpt_timer_fn(struct timer_list *timer)
 	if (!firmware_has_feature(FW_FEATURE_LPAR))
 		tlbiel_all();
 
-	next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
-	if (next_cpu >= nr_cpu_ids)
-		next_cpu = cpumask_first(cpu_online_mask);
+	next_cpu = cpumask_next_wrap(raw_smp_processor_id(), cpu_online_mask);
 	stress_hpt_timer.expires = jiffies + msecs_to_jiffies(10);
 	add_timer_on(&stress_hpt_timer, next_cpu);
 }
arch/powerpc/sysdev/xive/common.c +0 -0
index e1a4f8a97393..b6446abe29a6 100644

arch/powerpc/sysdev/xive/common.c static int xive_find_target_in_mask(const struct cpumask *mask, · line 580

Quoted 8 lines
arch/powerpc/sysdev/xive/common.c +1 -4
@@ -577,10 +577,7 @@ static int xive_find_target_in_mask(const struct cpumask *mask,
 		 */
 		if (cpu_online(cpu) && xive_try_pick_target(cpu))
 			return cpu;
-		cpu = cpumask_next(cpu, mask);
-		/* Wrap around */
-		if (cpu >= nr_cpu_ids)
-			cpu = cpumask_first(mask);
+		cpu = cpumask_next_wrap(cpu, mask);

Again, for all except this:

Can you move my series for xive_find_target_in_mask() together with your patches?

Quoted 5 lines
arch/powerpc/sysdev/xive/common.c +0 -0
 	} while (cpu != first);
 
 	return -1;
Signature · 1 line
2.47.3
author reviewer
#3 lore raw

Re: [PATCH 1/2] powerpc: Use cpumask_next_wrap instead

Hi Yury.

Quoted 65 lines · Yury Norov
Quoted 53 lines · Shrikanth Hegde

cpu = cpumask_next(cpu, mask)
if (cpu >= nr_cpu_ids)
cpu = cpumask_first(mask)

Above block is identical to:
cpu = cpumask_next_wrap(cpu, mask)

Replace it, No change in functionality or performance. Slightly simpler code.

---
  arch/powerpc/kernel/irq.c             | 5 +----
  arch/powerpc/mm/book3s64/hash_utils.c | 4 +---
  arch/powerpc/sysdev/xive/common.c     | 5 +----
  3 files changed, 3 insertions(+), 11 deletions(-)
arch/powerpc/kernel/irq.c +1 -4
index a0e8b998c9b5..f69de08ad347 100644
@@ -370,10 +370,7 @@ int irq_choose_cpu(const struct cpumask *mask)
  do_round_robin:
  		raw_spin_lock_irqsave(&irq_rover_lock, flags);
 
-		irq_rover = cpumask_next(irq_rover, cpu_online_mask);
-		if (irq_rover >= nr_cpu_ids)
-			irq_rover = cpumask_first(cpu_online_mask);
-
+		irq_rover = cpumask_next_wrap(irq_rover, cpu_online_mask);
  		cpuid = irq_rover;
 
  		raw_spin_unlock_irqrestore(&irq_rover_lock, flags);
arch/powerpc/mm/book3s64/hash_utils.c +1 -3
index 9dc5889d6ecb..e4fcf929cb33 100644
@@ -1299,9 +1299,7 @@ static void stress_hpt_timer_fn(struct timer_list *timer)
  	if (!firmware_has_feature(FW_FEATURE_LPAR))
  		tlbiel_all();
 
-	next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
-	if (next_cpu >= nr_cpu_ids)
-		next_cpu = cpumask_first(cpu_online_mask);
+	next_cpu = cpumask_next_wrap(raw_smp_processor_id(), cpu_online_mask);
  	stress_hpt_timer.expires = jiffies + msecs_to_jiffies(10);
  	add_timer_on(&stress_hpt_timer, next_cpu);
  }
arch/powerpc/sysdev/xive/common.c +0 -0
index e1a4f8a97393..b6446abe29a6 100644

arch/powerpc/sysdev/xive/common.c static int xive_find_target_in_mask(const struct cpumask *mask, · line 580

Quoted 8 lines
arch/powerpc/sysdev/xive/common.c +1 -4
@@ -577,10 +577,7 @@ static int xive_find_target_in_mask(const struct cpumask *mask,
  		 */
  		if (cpu_online(cpu) && xive_try_pick_target(cpu))
  			return cpu;
-		cpu = cpumask_next(cpu, mask);
-		/* Wrap around */
-		if (cpu >= nr_cpu_ids)
-			cpu = cpumask_first(mask);
+		cpu = cpumask_next_wrap(cpu, mask);

Again, for all except this:

Quoted 2 lines

Thanks for the review.

Quoted 3 lines

Can you move my series for xive_find_target_in_mask() together with your patches?

Sure.
I can add your patch to this series and send it.

PS: Sorry, I hadn't seen your series.