Skip to contentSkip to conversation

wireguard: queueing: simplify wg_cpumask_next_online()

PATCH Superseded Yury Norov 3 participants 10 messages Jun 19, 2025 – Jun 30, 2025

Subsystems (4)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

PATCH v2 1/1 wireguard: queueing: simplify wg_cpumask_next_online()

From: Yury Norov [NVIDIA] <[email protected]>

wg_cpumask_choose_online() opencodes cpumask_nth(). Use it and make the function significantly simpler. While there, fix opencoded cpu_online() too.

---
v1: https://lore.kernel.org/all/[email protected]/
v2:
- fix 'cpu' undeclared;
- change subject (Jason);
- keep the original function structure (Jason);

 drivers/net/wireguard/queueing.h | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/net/wireguard/queueing.h +4 -9
index 7eb76724b3ed..56314f98b6ba 100644
@@ -104,16 +104,11 @@ static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating)
 
 static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
 {
-	unsigned int cpu = *stored_cpu, cpu_index, i;
+	unsigned int cpu = *stored_cpu;
+
+	if (unlikely(cpu >= nr_cpu_ids || !cpu_online(cpu)))
+		cpu = *stored_cpu = cpumask_nth(id % num_online_cpus(), cpu_online_mask);
 
-	if (unlikely(cpu >= nr_cpu_ids ||
-		     !cpumask_test_cpu(cpu, cpu_online_mask))) {
-		cpu_index = id % cpumask_weight(cpu_online_mask);
-		cpu = cpumask_first(cpu_online_mask);
-		for (i = 0; i < cpu_index; ++i)
-			cpu = cpumask_next(cpu, cpu_online_mask);
-		*stored_cpu = cpu;
-	}
 	return cpu;
 }
 
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Yury Norov

From: Yury Norov [NVIDIA] <[email protected]>

wg_cpumask_choose_online() opencodes cpumask_nth(). Use it and make the function significantly simpler. While there, fix opencoded cpu_online() too.

Quoted 7 lines

---
v1: https://lore.kernel.org/all/[email protected]/
v2:
- fix 'cpu' undeclared;
- change subject (Jason);
- keep the original function structure (Jason);

#2 lore raw
In reply to Yury Norov
Quoted 22 lines · Yury Norov

From: Yury Norov [NVIDIA] <[email protected]>

wg_cpumask_choose_online() opencodes cpumask_nth(). Use it and make the function significantly simpler. While there, fix opencoded cpu_online() too.

---
v1: https://lore.kernel.org/all/[email protected]/
v2:
- fix 'cpu' undeclared;
- change subject (Jason);
- keep the original function structure (Jason);

 drivers/net/wireguard/queueing.h | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/net/wireguard/queueing.h +0 -0
index 7eb76724b3ed..56314f98b6ba 100644

drivers/net/wireguard/queueing.h static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating) · line 110

Quoted 8 lines
drivers/net/wireguard/queueing.h +4 -1
@@ -104,16 +104,11 @@ static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating)
 
 static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
 {
-	unsigned int cpu = *stored_cpu, cpu_index, i;
+	unsigned int cpu = *stored_cpu;
+
+	if (unlikely(cpu >= nr_cpu_ids || !cpu_online(cpu)))
+		cpu = *stored_cpu = cpumask_nth(id % num_online_cpus(), cpu_online_mask);

I was about to apply this but then it occurred to me: what happens if cpu_online_mask changes (shrinks) after num_online_cpus() is evaluated? cpumask_nth() will then return nr_cpu_ids?

Jason

author
#3 lore raw
Quoted 24 lines · Jason A. Donenfeld
Quoted 23 lines · Yury Norov

From: Yury Norov [NVIDIA] <[email protected]>

wg_cpumask_choose_online() opencodes cpumask_nth(). Use it and make the function significantly simpler. While there, fix opencoded cpu_online() too.

---
v1: https://lore.kernel.org/all/[email protected]/
v2:
- fix 'cpu' undeclared;
- change subject (Jason);
- keep the original function structure (Jason);

 drivers/net/wireguard/queueing.h | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/net/wireguard/queueing.h +0 -0
index 7eb76724b3ed..56314f98b6ba 100644
@@ -104,16 +104,11 @@ static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating)
 
Quoted 11 lines

drivers/net/wireguard/queueing.h static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating) · line 110

Quoted 7 lines
drivers/net/wireguard/queueing.h +4 -1
 static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
 {
-	unsigned int cpu = *stored_cpu, cpu_index, i;
+	unsigned int cpu = *stored_cpu;
+
+	if (unlikely(cpu >= nr_cpu_ids || !cpu_online(cpu)))
+		cpu = *stored_cpu = cpumask_nth(id % num_online_cpus(), cpu_online_mask);

I was about to apply this but then it occurred to me: what happens if cpu_online_mask changes (shrinks) after num_online_cpus() is evaluated? cpumask_nth() will then return nr_cpu_ids?

It will return >= nd_cpu_ids. The original version based a for-loop does the same, so I decided that the caller is safe against it.

If not, I can send a v3. But, what should we do - retry, or return a local cpu? Or something else?

Thanks,
Yury

#4 lore raw
Quoted 37 lines · Yury Norov
Quoted 24 lines · Jason A. Donenfeld
Quoted 23 lines · Yury Norov

From: Yury Norov [NVIDIA] <[email protected]>

wg_cpumask_choose_online() opencodes cpumask_nth(). Use it and make the function significantly simpler. While there, fix opencoded cpu_online() too.

---
v1: https://lore.kernel.org/all/[email protected]/
v2:
- fix 'cpu' undeclared;
- change subject (Jason);
- keep the original function structure (Jason);

 drivers/net/wireguard/queueing.h | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/net/wireguard/queueing.h +0 -0
index 7eb76724b3ed..56314f98b6ba 100644
@@ -104,16 +104,11 @@ static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating)
 
Quoted 11 lines

drivers/net/wireguard/queueing.h static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating) · line 110

Quoted 7 lines
drivers/net/wireguard/queueing.h +4 -1
 static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
 {
-	unsigned int cpu = *stored_cpu, cpu_index, i;
+	unsigned int cpu = *stored_cpu;
+
+	if (unlikely(cpu >= nr_cpu_ids || !cpu_online(cpu)))
+		cpu = *stored_cpu = cpumask_nth(id % num_online_cpus(), cpu_online_mask);

I was about to apply this but then it occurred to me: what happens if cpu_online_mask changes (shrinks) after num_online_cpus() is evaluated? cpumask_nth() will then return nr_cpu_ids?

Quoted 2 lines

It will return >= nd_cpu_ids. The original version based a for-loop does the same, so I decided that the caller is safe against it.

Good point. I just checked... This goes into queue_work_on() which eventually hits:

        /* pwq which will be used unless @work is executing elsewhere */
        if (req_cpu == WORK_CPU_UNBOUND) {

And it turns out WORK_CPU_UNBOUND is the same as nr_cpu_ids. So I guess that's a fine failure mode.

I'll queue this patch up.

Jason

author
#5 lore raw
Quoted 41 lines · Jason A. Donenfeld
Quoted 37 lines · Yury Norov
Quoted 24 lines · Jason A. Donenfeld
Quoted 23 lines · Yury Norov

From: Yury Norov [NVIDIA] <[email protected]>

wg_cpumask_choose_online() opencodes cpumask_nth(). Use it and make the function significantly simpler. While there, fix opencoded cpu_online() too.

---
v1: https://lore.kernel.org/all/[email protected]/
v2:
- fix 'cpu' undeclared;
- change subject (Jason);
- keep the original function structure (Jason);

 drivers/net/wireguard/queueing.h | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/net/wireguard/queueing.h +0 -0
index 7eb76724b3ed..56314f98b6ba 100644
@@ -104,16 +104,11 @@ static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating)
 
Quoted 11 lines

drivers/net/wireguard/queueing.h static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating) · line 110

Quoted 7 lines
drivers/net/wireguard/queueing.h +4 -1
 static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
 {
-	unsigned int cpu = *stored_cpu, cpu_index, i;
+	unsigned int cpu = *stored_cpu;
+
+	if (unlikely(cpu >= nr_cpu_ids || !cpu_online(cpu)))
+		cpu = *stored_cpu = cpumask_nth(id % num_online_cpus(), cpu_online_mask);

I was about to apply this but then it occurred to me: what happens if cpu_online_mask changes (shrinks) after num_online_cpus() is evaluated? cpumask_nth() will then return nr_cpu_ids?

Quoted 2 lines

It will return >= nd_cpu_ids. The original version based a for-loop does the same, so I decided that the caller is safe against it.

Quoted 8 lines

Good point. I just checked... This goes into queue_work_on() which eventually hits:

        /* pwq which will be used unless @work is executing elsewhere */
        if (req_cpu == WORK_CPU_UNBOUND) {

And it turns out WORK_CPU_UNBOUND is the same as nr_cpu_ids. So I guess that's a fine failure mode.

Actually, cpumask_nth_cpu may return >= nr_cpu_ids because of small_cpumask_nbits optimization. So it's safer to relax the condition.

Can you consider applying the following patch for that?

Thanks,
Yury

From fbdce972342437fb12703cae0c3a4f8f9e218a1b Mon Sep 17 00:00:00 2001
From: Yury Norov (NVIDIA) <[email protected]>
Date: Mon, 30 Jun 2025 13:47:49 -0400
Subject: [PATCH] workqueue: relax condition in __queue_work()

Some cpumask search functions may return a number greater than nr_cpu_ids when nothing is found. Adjust __queue_work() to it.

---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/workqueue.c +1 -1
index 9f9148075828..abacfe157fe6 100644
@@ -2261,7 +2261,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
 	rcu_read_lock();
 retry:
 	/* pwq which will be used unless @work is executing elsewhere */
-	if (req_cpu == WORK_CPU_UNBOUND) {
+	if (req_cpu >= WORK_CPU_UNBOUND) {
 		if (wq->flags & WQ_UNBOUND)
 			cpu = wq_select_unbound_cpu(raw_smp_processor_id());
 		else
Signature · 1 line
2.43.0
#6 lore raw

Hi Yury,

Quoted 59 lines
Quoted 21 lines
Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
drivers/net/wireguard/queueing.h +0 -0
index 7eb76724b3ed..56314f98b6ba 100644
@@ -104,16 +104,11 @@ static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating)
 
Quoted 11 lines

drivers/net/wireguard/queueing.h static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating) · line 110

Quoted 7 lines
drivers/net/wireguard/queueing.h +4 -1
 static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
 {
-       unsigned int cpu = *stored_cpu, cpu_index, i;
+       unsigned int cpu = *stored_cpu;
+
+       if (unlikely(cpu >= nr_cpu_ids || !cpu_online(cpu)))
+               cpu = *stored_cpu = cpumask_nth(id % num_online_cpus(), cpu_online_mask);

I was about to apply this but then it occurred to me: what happens if cpu_online_mask changes (shrinks) after num_online_cpus() is evaluated? cpumask_nth() will then return nr_cpu_ids?

Quoted 2 lines

It will return >= nd_cpu_ids. The original version based a for-loop does the same, so I decided that the caller is safe against it.

Quoted 8 lines

Good point. I just checked... This goes into queue_work_on() which eventually hits:

        /* pwq which will be used unless @work is executing elsewhere */
        if (req_cpu == WORK_CPU_UNBOUND) {

And it turns out WORK_CPU_UNBOUND is the same as nr_cpu_ids. So I guess that's a fine failure mode.

Actually, cpumask_nth_cpu may return >= nr_cpu_ids because of small_cpumask_nbits optimization. So it's safer to relax the condition.

Can you consider applying the following patch for that?

Thanks,
Yury

From fbdce972342437fb12703cae0c3a4f8f9e218a1b Mon Sep 17 00:00:00 2001
From: Yury Norov (NVIDIA) <[email protected]>
Date: Mon, 30 Jun 2025 13:47:49 -0400
Subject: [PATCH] workqueue: relax condition in __queue_work()

Some cpumask search functions may return a number greater than nr_cpu_ids when nothing is found. Adjust __queue_work() to it.

---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/workqueue.c +0 -0
index 9f9148075828..abacfe157fe6 100644

kernel/workqueue.c static void __queue_work(int cpu, struct workqueue_struct *wq, · line 2267

Quoted 8 lines
kernel/workqueue.c +1 -1
@@ -2261,7 +2261,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
        rcu_read_lock();
 retry:
        /* pwq which will be used unless @work is executing elsewhere */
-       if (req_cpu == WORK_CPU_UNBOUND) {
+       if (req_cpu >= WORK_CPU_UNBOUND) {
                if (wq->flags & WQ_UNBOUND)
                        cpu = wq_select_unbound_cpu(raw_smp_processor_id());
                else

Seems reasonable to me... Maybe submit this to Tejun and CC me?

Jason

author
#7 lore raw
Quoted 55 lines · Jason A. Donenfeld

Hi Yury,

Quoted 53 lines
Quoted 21 lines
Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
drivers/net/wireguard/queueing.h +0 -0
index 7eb76724b3ed..56314f98b6ba 100644
@@ -104,16 +104,11 @@ static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating)
 
Quoted 11 lines

drivers/net/wireguard/queueing.h static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating) · line 110

Quoted 7 lines
drivers/net/wireguard/queueing.h +4 -1
 static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
 {
-       unsigned int cpu = *stored_cpu, cpu_index, i;
+       unsigned int cpu = *stored_cpu;
+
+       if (unlikely(cpu >= nr_cpu_ids || !cpu_online(cpu)))
+               cpu = *stored_cpu = cpumask_nth(id % num_online_cpus(), cpu_online_mask);

I was about to apply this but then it occurred to me: what happens if cpu_online_mask changes (shrinks) after num_online_cpus() is evaluated? cpumask_nth() will then return nr_cpu_ids?

Quoted 2 lines

It will return >= nd_cpu_ids. The original version based a for-loop does the same, so I decided that the caller is safe against it.

Quoted 8 lines

Good point. I just checked... This goes into queue_work_on() which eventually hits:

        /* pwq which will be used unless @work is executing elsewhere */
        if (req_cpu == WORK_CPU_UNBOUND) {

And it turns out WORK_CPU_UNBOUND is the same as nr_cpu_ids. So I guess that's a fine failure mode.

Actually, cpumask_nth_cpu may return >= nr_cpu_ids because of small_cpumask_nbits optimization. So it's safer to relax the condition.

Can you consider applying the following patch for that?

Thanks,
Yury

From fbdce972342437fb12703cae0c3a4f8f9e218a1b Mon Sep 17 00:00:00 2001
From: Yury Norov (NVIDIA) <[email protected]>
Date: Mon, 30 Jun 2025 13:47:49 -0400
Subject: [PATCH] workqueue: relax condition in __queue_work()

Some cpumask search functions may return a number greater than nr_cpu_ids when nothing is found. Adjust __queue_work() to it.

---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines
Quoted 6 lines
kernel/workqueue.c +0 -0
index 9f9148075828..abacfe157fe6 100644

kernel/workqueue.c static void __queue_work(int cpu, struct workqueue_struct *wq, · line 2267

Quoted 8 lines
kernel/workqueue.c +1 -1
@@ -2261,7 +2261,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
        rcu_read_lock();
 retry:
        /* pwq which will be used unless @work is executing elsewhere */
-       if (req_cpu == WORK_CPU_UNBOUND) {
+       if (req_cpu >= WORK_CPU_UNBOUND) {
                if (wq->flags & WQ_UNBOUND)
                        cpu = wq_select_unbound_cpu(raw_smp_processor_id());
                else

Seems reasonable to me... Maybe submit this to Tejun and CC me?

Sure, no problem.

author
#8 lore raw
Quoted 30 lines
Quoted 13 lines
Quoted 13 lines

From fbdce972342437fb12703cae0c3a4f8f9e218a1b Mon Sep 17 00:00:00 2001
From: Yury Norov (NVIDIA) <[email protected]>
Date: Mon, 30 Jun 2025 13:47:49 -0400
Subject: [PATCH] workqueue: relax condition in __queue_work()

Some cpumask search functions may return a number greater than nr_cpu_ids when nothing is found. Adjust __queue_work() to it.

---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines
Quoted 6 lines
kernel/workqueue.c +0 -0
index 9f9148075828..abacfe157fe6 100644

kernel/workqueue.c static void __queue_work(int cpu, struct workqueue_struct *wq, · line 2267

Quoted 8 lines
kernel/workqueue.c +1 -1
@@ -2261,7 +2261,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
        rcu_read_lock();
 retry:
        /* pwq which will be used unless @work is executing elsewhere */
-       if (req_cpu == WORK_CPU_UNBOUND) {
+       if (req_cpu >= WORK_CPU_UNBOUND) {
                if (wq->flags & WQ_UNBOUND)
                        cpu = wq_select_unbound_cpu(raw_smp_processor_id());
                else

Seems reasonable to me... Maybe submit this to Tejun and CC me?

Quoted 1 line

Sure, no problem.

Hmm... So, actually WORK_CPU_UNBOUND is NR_CPUS, which is not the same as nr_cpu_ids. For example, on my Ubuntu machine, the CONFIG_NR_CPUS is 8192, and nr_cpu_ids is 8.

So, for the wg_cpumask_next_online() to work properly, we need to return the WORK_CPU_UNBOUND in case of nothing is found.

I think I need to send a v3...

Thanks,
Yury

#9 lore raw
Quoted 31 lines · Yury Norov
Quoted 31 lines
Quoted 30 lines
Quoted 20 lines

From fbdce972342437fb12703cae0c3a4f8f9e218a1b Mon Sep 17 00:00:00 2001
From: Yury Norov (NVIDIA) <[email protected]>
Date: Mon, 30 Jun 2025 13:47:49 -0400
Subject: [PATCH] workqueue: relax condition in __queue_work()

Some cpumask search functions may return a number greater than nr_cpu_ids when nothing is found. Adjust __queue_work() to it.

---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/workqueue.c +0 -0
index 9f9148075828..abacfe157fe6 100644

kernel/workqueue.c static void __queue_work(int cpu, struct workqueue_struct *wq, · line 2267

Quoted 8 lines
kernel/workqueue.c +1 -1
@@ -2261,7 +2261,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
        rcu_read_lock();
 retry:
        /* pwq which will be used unless @work is executing elsewhere */
-       if (req_cpu == WORK_CPU_UNBOUND) {
+       if (req_cpu >= WORK_CPU_UNBOUND) {
                if (wq->flags & WQ_UNBOUND)
                        cpu = wq_select_unbound_cpu(raw_smp_processor_id());
                else

Seems reasonable to me... Maybe submit this to Tejun and CC me?

Quoted 8 lines
Quoted 1 line

Sure, no problem.

Hmm... So, actually WORK_CPU_UNBOUND is NR_CPUS, which is not the same as nr_cpu_ids. For example, on my Ubuntu machine, the CONFIG_NR_CPUS is 8192, and nr_cpu_ids is 8.

So, for the wg_cpumask_next_online() to work properly, we need to return the WORK_CPU_UNBOUND in case of nothing is found.

Or just try again? Could just make your if into a while.