rework wg_cpumask_next_online()
Subsystems (3)
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
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
@@ -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
PATCH v2 2/2 wireguard: queueing: always return valid online CPU in wg_cpumask_choose_online()
In reply to Yury NorovFrom: 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
@@ -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
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_idsreturn (Jason).Yury Norov (NVIDIA) (2):
wireguard: queueing: simplifywg_cpumask_next_online()
wireguard: queueing: always return valid online CPU inwg_cpumask_choose_online()drivers/net/wireguard/queueing.h | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)Signature · 1 line
2.43.0
Applied this series, thanks.