Skip to contentSkip to conversation

rework wg_cpumask_next_online()

PATCH Applied Yury Norov 2 participants 5 messages Jul 19, 2025 – Sep 8, 2025

Subsystems (3)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

v2 cover rework wg_cpumask_next_online()

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

Simplify the function and fix possible out-of-boundary condition.

v2:
- fix possible >= nr_cpu_ids return (Jason).

Yury Norov (NVIDIA) (2):
wireguard: queueing: simplify wg_cpumask_next_online()
wireguard: queueing: always return valid online CPU in wg_cpumask_choose_online()

 drivers/net/wireguard/queueing.h | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/2 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.

---
 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
author
#2 lore raw

PATCH v2 2/2 wireguard: queueing: always return valid online CPU in wg_cpumask_choose_online()

In reply to Yury Norov

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

The function gets number of online CPUS, and uses it to search for Nth cpu in cpu_online_mask.

If id == num_online_cpus() - 1, and one CPU gets offlined between calling num_online_cpus() -> cpumask_nth(), there's a chance for cpumask_nth() to find nothing and return >= nr_cpu_ids.

The caller code in __queue_work() tries to avoid that by checking the returned CPU against WORK_CPU_UNBOUND, which is NR_CPUS. It's not the same as '>= nr_cpu_ids'. On a typical Ubuntu desktop, NR_CPUS is 8192, while nr_cpu_ids is the actual number of possible CPUs, say 8.

The non-existing cpu may later be passed to rcu_dereference() and corrupt the logic. Fix it by switching from 'if' to 'while'.

---
 drivers/net/wireguard/queueing.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireguard/queueing.h +1 -1
index 56314f98b6ba..79b6d70de236 100644
@@ -106,7 +106,7 @@ static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
 {
 	unsigned int cpu = *stored_cpu;
 
-	if (unlikely(cpu >= nr_cpu_ids || !cpu_online(cpu)))
+	while (unlikely(cpu >= nr_cpu_ids || !cpu_online(cpu)))
 		cpu = *stored_cpu = cpumask_nth(id % num_online_cpus(), cpu_online_mask);
 
 	return cpu;
Signature · 1 line
2.43.0
author
#3 lore raw
In reply to Yury Norov

Ping?

Quoted 16 lines · Yury Norov

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

Simplify the function and fix possible out-of-boundary condition.

v2:
- fix possible >= nr_cpu_ids return (Jason).

Yury Norov (NVIDIA) (2):
wireguard: queueing: simplify wg_cpumask_next_online()
wireguard: queueing: always return valid online CPU in wg_cpumask_choose_online()

 drivers/net/wireguard/queueing.h | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.0