wireguard.lists.zx2c4.com archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] rework wg_cpumask_next_online()
@ 2025-07-19 22:44 Yury Norov
  2025-07-19 22:44 ` [PATCH v2 1/2] wireguard: queueing: simplify wg_cpumask_next_online() Yury Norov
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Yury Norov @ 2025-07-19 22:44 UTC (permalink / raw)
  To: Jason A. Donenfeld, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, wireguard, netdev,
	linux-kernel
  Cc: Yury Norov

From: Yury Norov (NVIDIA) <yury.norov@gmail.com>

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(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] wireguard: queueing: simplify wg_cpumask_next_online()
  2025-07-19 22:44 [PATCH v2 0/2] rework wg_cpumask_next_online() Yury Norov
@ 2025-07-19 22:44 ` Yury Norov
  2025-07-19 22:44 ` [PATCH 2/2] wireguard: queueing: always return valid online CPU in wg_cpumask_choose_online() Yury Norov
  2025-08-09 13:24 ` [PATCH v2 0/2] rework wg_cpumask_next_online() Yury Norov
  2 siblings, 0 replies; 4+ messages in thread
From: Yury Norov @ 2025-07-19 22:44 UTC (permalink / raw)
  To: Jason A. Donenfeld, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, wireguard, netdev,
	linux-kernel
  Cc: Yury Norov

From: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>

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

Reviewed-by: Simon Horman <horms@kernel.org>
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
---
 drivers/net/wireguard/queueing.h | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/drivers/net/wireguard/queueing.h b/drivers/net/wireguard/queueing.h
index 7eb76724b3ed..56314f98b6ba 100644
--- a/drivers/net/wireguard/queueing.h
+++ b/drivers/net/wireguard/queueing.h
@@ -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;
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] wireguard: queueing: always return valid online CPU in wg_cpumask_choose_online()
  2025-07-19 22:44 [PATCH v2 0/2] rework wg_cpumask_next_online() Yury Norov
  2025-07-19 22:44 ` [PATCH v2 1/2] wireguard: queueing: simplify wg_cpumask_next_online() Yury Norov
@ 2025-07-19 22:44 ` Yury Norov
  2025-08-09 13:24 ` [PATCH v2 0/2] rework wg_cpumask_next_online() Yury Norov
  2 siblings, 0 replies; 4+ messages in thread
From: Yury Norov @ 2025-07-19 22:44 UTC (permalink / raw)
  To: Jason A. Donenfeld, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, wireguard, netdev,
	linux-kernel
  Cc: Yury Norov

From: Yury Norov (NVIDIA) <yury.norov@gmail.com>

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'.

Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
---
 drivers/net/wireguard/queueing.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/wireguard/queueing.h b/drivers/net/wireguard/queueing.h
index 56314f98b6ba..79b6d70de236 100644
--- a/drivers/net/wireguard/queueing.h
+++ b/drivers/net/wireguard/queueing.h
@@ -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;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] rework wg_cpumask_next_online()
  2025-07-19 22:44 [PATCH v2 0/2] rework wg_cpumask_next_online() Yury Norov
  2025-07-19 22:44 ` [PATCH v2 1/2] wireguard: queueing: simplify wg_cpumask_next_online() Yury Norov
  2025-07-19 22:44 ` [PATCH 2/2] wireguard: queueing: always return valid online CPU in wg_cpumask_choose_online() Yury Norov
@ 2025-08-09 13:24 ` Yury Norov
  2 siblings, 0 replies; 4+ messages in thread
From: Yury Norov @ 2025-08-09 13:24 UTC (permalink / raw)
  To: Jason A. Donenfeld, Andrew Lunn, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, wireguard, netdev,
	linux-kernel

Ping?

On Sat, Jul 19, 2025 at 06:44:41PM -0400, Yury Norov wrote:
> From: Yury Norov (NVIDIA) <yury.norov@gmail.com>
> 
> 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(-)
> 
> -- 
> 2.43.0

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-08-09 13:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-19 22:44 [PATCH v2 0/2] rework wg_cpumask_next_online() Yury Norov
2025-07-19 22:44 ` [PATCH v2 1/2] wireguard: queueing: simplify wg_cpumask_next_online() Yury Norov
2025-07-19 22:44 ` [PATCH 2/2] wireguard: queueing: always return valid online CPU in wg_cpumask_choose_online() Yury Norov
2025-08-09 13:24 ` [PATCH v2 0/2] rework wg_cpumask_next_online() Yury Norov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).