Skip to contentSkip to conversation

scsi/fcoe: simplify fcoe_select_cpu()

PATCH In review Yury Norov 3 participants 4 messages Jun 4, 2025 – Jun 5, 2025

Subsystems (2)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

PATCH 1/1 scsi/fcoe: simplify fcoe_select_cpu()

cpumask_next() followed by cpumask_first() opencodes cpumask_next_wrap(). Fix it.

---
 drivers/scsi/fcoe/fcoe.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/scsi/fcoe/fcoe.c +1 -4
index b911fdb387f3..07eddafe52ff 100644
@@ -1312,10 +1312,7 @@ static inline unsigned int fcoe_select_cpu(void)
 {
 	static unsigned int selected_cpu;
 
-	selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
-	if (selected_cpu >= nr_cpu_ids)
-		selected_cpu = cpumask_first(cpu_online_mask);
-
+	selected_cpu = cpumask_next_wrap(selected_cpu, cpu_online_mask);
 	return selected_cpu;
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · Yury Norov
drivers/scsi/fcoe/fcoe.c +0 -0
index b911fdb387f3..07eddafe52ff 100644

drivers/scsi/fcoe/fcoe.c static inline unsigned int fcoe_select_cpu(void) · line 1317

Quoted 10 lines
drivers/scsi/fcoe/fcoe.c +1 -4
@@ -1312,10 +1312,7 @@ static inline unsigned int fcoe_select_cpu(void)
  {
  	static unsigned int selected_cpu;
 
-	selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
-	if (selected_cpu >= nr_cpu_ids)
-		selected_cpu = cpumask_first(cpu_online_mask);
-
+	selected_cpu = cpumask_next_wrap(selected_cpu, cpu_online_mask);
  	return selected_cpu;
  }

Why does this algorithm occur in the FCoE driver? Isn't WORK_CPU_UNBOUND good enough for this driver? And if it isn't good enough, shouldn't this kind of functionality be integrated in kernel/workqueue.c rather than having the above algorithm in a kernel driver?

Thanks,

Bart.

author
#2 lore raw

+ Tejun, Lai

Quoted 13 lines · Bart Van Assche
Quoted 12 lines · Yury Norov
drivers/scsi/fcoe/fcoe.c +1 -4
index b911fdb387f3..07eddafe52ff 100644
@@ -1312,10 +1312,7 @@ static inline unsigned int fcoe_select_cpu(void)
  {
  	static unsigned int selected_cpu;
-	selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
-	if (selected_cpu >= nr_cpu_ids)
-		selected_cpu = cpumask_first(cpu_online_mask);
-
+	selected_cpu = cpumask_next_wrap(selected_cpu, cpu_online_mask);
Quoted 8 lines

drivers/scsi/fcoe/fcoe.c static inline unsigned int fcoe_select_cpu(void) · line 1317

Quoted 2 lines
drivers/scsi/fcoe/fcoe.c +0 -0
  	return selected_cpu;
  }

Why does this algorithm occur in the FCoE driver? Isn't WORK_CPU_UNBOUND good enough for this driver? And if it isn't good enough, shouldn't this kind of functionality be integrated in kernel/workqueue.c rather than having the above algorithm in a kernel driver?

(I'm obviously not an expert in this driver, and just wanted to cleanup the cpumask API usage.)

It looks like the intention is to distribute the workload among CPUs sequentially. If you move this function out of the driver, someone else may call the function, and sequential distribution may get broken.

If sequential distribution doesn't matter here, and the real intention is just to distribute workload more or less evenly, we already have cpumask_any_distribute() for this.

Thanks,
Yury

maintainer
#3 lore raw
Quoted 28 lines · Yury Norov

+ Tejun, Lai

Quoted 13 lines · Bart Van Assche
Quoted 12 lines · Yury Norov
drivers/scsi/fcoe/fcoe.c +1 -4
index b911fdb387f3..07eddafe52ff 100644
@@ -1312,10 +1312,7 @@ static inline unsigned int fcoe_select_cpu(void)
   {
   	static unsigned int selected_cpu;
-	selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
-	if (selected_cpu >= nr_cpu_ids)
-		selected_cpu = cpumask_first(cpu_online_mask);
-
+	selected_cpu = cpumask_next_wrap(selected_cpu, cpu_online_mask);
Quoted 8 lines

drivers/scsi/fcoe/fcoe.c static inline unsigned int fcoe_select_cpu(void) · line 1317

Quoted 2 lines
drivers/scsi/fcoe/fcoe.c +0 -0
   	return selected_cpu;
   }

Why does this algorithm occur in the FCoE driver? Isn't WORK_CPU_UNBOUND good enough for this driver? And if it isn't good enough, shouldn't this kind of functionality be integrated in kernel/workqueue.c rather than having the above algorithm in a kernel driver?

(I'm obviously not an expert in this driver, and just wanted to cleanup the cpumask API usage.)

Quoted 9 lines

It looks like the intention is to distribute the workload among CPUs sequentially. If you move this function out of the driver, someone else may call the function, and sequential distribution may get broken.

If sequential distribution doesn't matter here, and the real intention is just to distribute workload more or less evenly, we already have cpumask_any_distribute() for this.

This function is used to distribute incoming skbs onto a work cpu. And it's actually quite pointless, as the skb already has a field (skb->sk->sk_incoming_cpu) which tells you exactly on which CPU this skb was received, so we should use that here.

I'll send a patch.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich