From mpatocka at redhat.com Mon Jul 14 17:12:48 2025 From: mpatocka at redhat.com (Mikulas Patocka) Date: Mon, 14 Jul 2025 19:12:48 +0200 (CEST) Subject: [PATCH] wireguard: omit need_resched() before cond_resched() Message-ID: <5da9fced-67f4-3e32-76ca-b8a5be3b962a@redhat.com> There's no need to call need_resched() because cond_resched() will do nothing if need_resched() returns false. Signed-off-by: Mikulas Patocka --- drivers/net/wireguard/receive.c | 3 +-- drivers/net/wireguard/send.c | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) Index: linux-2.6/drivers/net/wireguard/receive.c =================================================================== --- linux-2.6.orig/drivers/net/wireguard/receive.c 2024-03-30 20:07:03.000000000 +0100 +++ linux-2.6/drivers/net/wireguard/receive.c 2025-07-14 19:09:52.000000000 +0200 @@ -501,8 +501,7 @@ void wg_packet_decrypt_worker(struct wor likely(decrypt_packet(skb, PACKET_CB(skb)->keypair)) ? PACKET_STATE_CRYPTED : PACKET_STATE_DEAD; wg_queue_enqueue_per_peer_rx(skb, state); - if (need_resched()) - cond_resched(); + cond_resched(); } } Index: linux-2.6/drivers/net/wireguard/send.c =================================================================== --- linux-2.6.orig/drivers/net/wireguard/send.c 2024-07-21 17:40:39.000000000 +0200 +++ linux-2.6/drivers/net/wireguard/send.c 2025-07-14 19:10:03.000000000 +0200 @@ -279,8 +279,7 @@ void wg_packet_tx_worker(struct work_str wg_noise_keypair_put(keypair, false); wg_peer_put(peer); - if (need_resched()) - cond_resched(); + cond_resched(); } } @@ -303,8 +302,7 @@ void wg_packet_encrypt_worker(struct wor } } wg_queue_enqueue_per_peer_tx(first, state); - if (need_resched()) - cond_resched(); + cond_resched(); } } From yury.norov at gmail.com Sat Jul 19 22:44:41 2025 From: yury.norov at gmail.com (Yury Norov) Date: Sat, 19 Jul 2025 18:44:41 -0400 Subject: [PATCH v2 0/2] rework wg_cpumask_next_online() Message-ID: <20250719224444.411074-1-yury.norov@gmail.com> From: Yury Norov (NVIDIA) 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 From yury.norov at gmail.com Sat Jul 19 22:44:42 2025 From: yury.norov at gmail.com (Yury Norov) Date: Sat, 19 Jul 2025 18:44:42 -0400 Subject: [PATCH v2 1/2] wireguard: queueing: simplify wg_cpumask_next_online() In-Reply-To: <20250719224444.411074-1-yury.norov@gmail.com> References: <20250719224444.411074-1-yury.norov@gmail.com> Message-ID: <20250719224444.411074-2-yury.norov@gmail.com> From: "Yury Norov (NVIDIA)" 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 Signed-off-by: Yury Norov (NVIDIA) --- 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 From yury.norov at gmail.com Sat Jul 19 22:44:43 2025 From: yury.norov at gmail.com (Yury Norov) Date: Sat, 19 Jul 2025 18:44:43 -0400 Subject: [PATCH 2/2] wireguard: queueing: always return valid online CPU in wg_cpumask_choose_online() In-Reply-To: <20250719224444.411074-1-yury.norov@gmail.com> References: <20250719224444.411074-1-yury.norov@gmail.com> Message-ID: <20250719224444.411074-3-yury.norov@gmail.com> From: Yury Norov (NVIDIA) 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 Signed-off-by: Yury Norov (NVIDIA) --- 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 From kees at kernel.org Tue Jul 22 17:18:30 2025 From: kees at kernel.org (Kees Cook) Date: Tue, 22 Jul 2025 10:18:30 -0700 Subject: [PATCH net-next 0/3] net: Add sockaddr_inet unified address structure Message-ID: <20250722171528.work.209-kees@kernel.org> Hi! Repeating patch 1, as it has the rationale: There are cases in networking (e.g. wireguard, sctp) where a union is used to provide coverage for either IPv4 or IPv6 network addresses, and they include an embedded "struct sockaddr" as well (for "sa_family" and raw "sa_data" access). The current struct sockaddr contains a flexible array, which means these unions should not be further embedded in other structs because they do not technically have a fixed size (and are generating warnings for the coming -Wflexible-array-not-at-end flag addition). But the future changes to make struct sockaddr a fixed size (i.e. with a 14 byte sa_data member) make the "sa_data" uses with an IPv6 address a potential place for the compiler to get upset about object size mismatches. Therefore, we need a sockaddr that cleanly provides both an sa_family member and an appropriately fixed-sized sa_data member that does not bloat member usage via the potential alternative of sockaddr_storage to cover both IPv4 and IPv6, to avoid unseemly churn in the affected code bases. Introduce sockaddr_inet as a unified structure for holding both IPv4 and IPv6 addresses (i.e. large enough to accommodate sockaddr_in6). The structure is defined in linux/in6.h since its max size is sized based on sockaddr_in6 and provides a more specific alternative to the generic sockaddr_storage for IPv4 with IPv6 address family handling. The "sa_family" member doesn't use the sa_family_t type to avoid needing layer violating header inclusions. Also includes the replacements for wireguard and sctp. Thanks, -Kees Kees Cook (3): ipv6: Add sockaddr_inet unified address structure wireguard: peer: Replace sockaddr with sockaddr_inet sctp: Replace sockaddr with sockaddr_inet in sctp_addr union drivers/net/wireguard/peer.h | 2 +- include/linux/in6.h | 7 +++++++ include/net/sctp/structs.h | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) -- 2.34.1 From kees at kernel.org Tue Jul 22 17:18:33 2025 From: kees at kernel.org (Kees Cook) Date: Tue, 22 Jul 2025 10:18:33 -0700 Subject: [PATCH net-next 3/3] sctp: Replace sockaddr with sockaddr_inet in sctp_addr union In-Reply-To: <20250722171528.work.209-kees@kernel.org> References: <20250722171528.work.209-kees@kernel.org> Message-ID: <20250722171836.1078436-3-kees@kernel.org> As part of the removal of the variably-sized sockaddr for kernel internals, replace struct sockaddr with sockaddr_inet in the sctp_addr union. No binary changes; the union size remains unchanged due to sockaddr_inet matching the size of sockaddr_in6. Signed-off-by: Kees Cook --- Cc: Marcelo Ricardo Leitner Cc: Xin Long Cc: "David S. Miller" Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni Cc: Simon Horman Cc: Cc: --- include/net/sctp/structs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/net/sctp/structs.h b/include/net/sctp/structs.h index 1ad7ce71d0a7..8a540ad9b509 100644 --- a/include/net/sctp/structs.h +++ b/include/net/sctp/structs.h @@ -51,9 +51,9 @@ * We should wean ourselves off this. */ union sctp_addr { + struct sockaddr_inet sa; /* Large enough for both address families */ struct sockaddr_in v4; struct sockaddr_in6 v6; - struct sockaddr sa; }; /* Forward declarations for data structures. */ -- 2.34.1 From kees at kernel.org Tue Jul 22 17:18:31 2025 From: kees at kernel.org (Kees Cook) Date: Tue, 22 Jul 2025 10:18:31 -0700 Subject: [PATCH net-next 1/3] ipv6: Add sockaddr_inet unified address structure In-Reply-To: <20250722171528.work.209-kees@kernel.org> References: <20250722171528.work.209-kees@kernel.org> Message-ID: <20250722171836.1078436-1-kees@kernel.org> There are cases in networking (e.g. wireguard, sctp) where a union is used to provide coverage for either IPv4 or IPv6 network addresses, and they include an embedded "struct sockaddr" as well (for "sa_family" and raw "sa_data" access). The current struct sockaddr contains a flexible array, which means these unions should not be further embedded in other structs because they do not technically have a fixed size (and are generating warnings for the coming -Wflexible-array-not-at-end flag addition). But the future changes to make struct sockaddr a fixed size (i.e. with a 14 byte sa_data member) make the "sa_data" uses with an IPv6 address a potential place for the compiler to get upset about object size mismatches. Therefore, we need a sockaddr that cleanly provides both an sa_family member and an appropriately fixed-sized sa_data member that does not bloat member usage via the potential alternative of sockaddr_storage to cover both IPv4 and IPv6, to avoid unseemly churn in the affected code bases. Introduce sockaddr_inet as a unified structure for holding both IPv4 and IPv6 addresses (i.e. large enough to accommodate sockaddr_in6). The structure is defined in linux/in6.h since its max size is sized based on sockaddr_in6 and provides a more specific alternative to the generic sockaddr_storage for IPv4 with IPv6 address family handling. The "sa_family" member doesn't use the sa_family_t type to avoid needing layer violating header inclusions. Signed-off-by: Kees Cook --- include/linux/in6.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/linux/in6.h b/include/linux/in6.h index 0777a21cbf86..403f926d33d8 100644 --- a/include/linux/in6.h +++ b/include/linux/in6.h @@ -18,6 +18,13 @@ #include +/* Large enough to hold both sockaddr_in and sockaddr_in6. */ +struct sockaddr_inet { + unsigned short sa_family; + char sa_data[sizeof(struct sockaddr_in6) - + sizeof(unsigned short)]; +}; + /* IPv6 Wildcard Address (::) and Loopback Address (::1) defined in RFC2553 * NOTE: Be aware the IN6ADDR_* constants and in6addr_* externals are defined * in network byte order, not in host byte order as are the IPv4 equivalents -- 2.34.1 From kees at kernel.org Tue Jul 22 17:18:32 2025 From: kees at kernel.org (Kees Cook) Date: Tue, 22 Jul 2025 10:18:32 -0700 Subject: [PATCH net-next 2/3] wireguard: peer: Replace sockaddr with sockaddr_inet In-Reply-To: <20250722171528.work.209-kees@kernel.org> References: <20250722171528.work.209-kees@kernel.org> Message-ID: <20250722171836.1078436-2-kees@kernel.org> As part of the removal of the variably-sized sockaddr for kernel internals, replace struct sockaddr with sockaddr_inet in the endpoint union. No binary changes; the union size remains unchanged due to sockaddr_inet matching the size of sockaddr_in6. Signed-off-by: Kees Cook --- Cc: "Jason A. Donenfeld" Cc: Andrew Lunn Cc: "David S. Miller" Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni Cc: Cc: --- drivers/net/wireguard/peer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/net/wireguard/peer.h b/drivers/net/wireguard/peer.h index 76e4d3128ad4..718fb42bdac7 100644 --- a/drivers/net/wireguard/peer.h +++ b/drivers/net/wireguard/peer.h @@ -20,7 +20,7 @@ struct wg_device; struct endpoint { union { - struct sockaddr addr; + struct sockaddr_inet addr; /* Large enough for both address families */ struct sockaddr_in addr4; struct sockaddr_in6 addr6; }; -- 2.34.1 From horms at kernel.org Wed Jul 23 15:46:47 2025 From: horms at kernel.org (Simon Horman) Date: Wed, 23 Jul 2025 16:46:47 +0100 Subject: [PATCH net-next 0/3] net: Add sockaddr_inet unified address structure In-Reply-To: <20250722171528.work.209-kees@kernel.org> References: <20250722171528.work.209-kees@kernel.org> Message-ID: <20250723154647.GI1036606@horms.kernel.org> + Iwashima-san and Willem This series looks like something you should review On Tue, Jul 22, 2025 at 10:18:30AM -0700, Kees Cook wrote: > Hi! > > Repeating patch 1, as it has the rationale: > > There are cases in networking (e.g. wireguard, sctp) where a union is > used to provide coverage for either IPv4 or IPv6 network addresses, > and they include an embedded "struct sockaddr" as well (for "sa_family" > and raw "sa_data" access). The current struct sockaddr contains a > flexible array, which means these unions should not be further embedded > in other structs because they do not technically have a fixed size (and > are generating warnings for the coming -Wflexible-array-not-at-end flag > addition). But the future changes to make struct sockaddr a fixed size > (i.e. with a 14 byte sa_data member) make the "sa_data" uses with an IPv6 > address a potential place for the compiler to get upset about object size > mismatches. Therefore, we need a sockaddr that cleanly provides both an > sa_family member and an appropriately fixed-sized sa_data member that does > not bloat member usage via the potential alternative of sockaddr_storage > to cover both IPv4 and IPv6, to avoid unseemly churn in the affected code > bases. > > Introduce sockaddr_inet as a unified structure for holding both IPv4 and > IPv6 addresses (i.e. large enough to accommodate sockaddr_in6). > > The structure is defined in linux/in6.h since its max size is sized > based on sockaddr_in6 and provides a more specific alternative to the > generic sockaddr_storage for IPv4 with IPv6 address family handling. > > The "sa_family" member doesn't use the sa_family_t type to avoid needing > layer violating header inclusions. > > Also includes the replacements for wireguard and sctp. > > Thanks, > > -Kees > > Kees Cook (3): > ipv6: Add sockaddr_inet unified address structure > wireguard: peer: Replace sockaddr with sockaddr_inet > sctp: Replace sockaddr with sockaddr_inet in sctp_addr union > > drivers/net/wireguard/peer.h | 2 +- > include/linux/in6.h | 7 +++++++ > include/net/sctp/structs.h | 2 +- > 3 files changed, 9 insertions(+), 2 deletions(-) > > -- > 2.34.1 > From patchwork-bot+netdevbpf at kernel.org Fri Jul 25 23:26:07 2025 From: patchwork-bot+netdevbpf at kernel.org (patchwork-bot+netdevbpf at kernel.org) Date: Fri, 25 Jul 2025 23:26:07 +0000 Subject: [PATCH net-next 0/3] net: Add sockaddr_inet unified address structure In-Reply-To: <20250722171528.work.209-kees@kernel.org> References: <20250722171528.work.209-kees@kernel.org> Message-ID: <175348596749.3366157.11889906791238903563.git-patchwork-notify@kernel.org> Hello: This series was applied to netdev/net-next.git (main) by Jakub Kicinski : On Tue, 22 Jul 2025 10:18:30 -0700 you wrote: > Hi! > > Repeating patch 1, as it has the rationale: > > There are cases in networking (e.g. wireguard, sctp) where a union is > used to provide coverage for either IPv4 or IPv6 network addresses, > and they include an embedded "struct sockaddr" as well (for "sa_family" > and raw "sa_data" access). The current struct sockaddr contains a > flexible array, which means these unions should not be further embedded > in other structs because they do not technically have a fixed size (and > are generating warnings for the coming -Wflexible-array-not-at-end flag > addition). But the future changes to make struct sockaddr a fixed size > (i.e. with a 14 byte sa_data member) make the "sa_data" uses with an IPv6 > address a potential place for the compiler to get upset about object size > mismatches. Therefore, we need a sockaddr that cleanly provides both an > sa_family member and an appropriately fixed-sized sa_data member that does > not bloat member usage via the potential alternative of sockaddr_storage > to cover both IPv4 and IPv6, to avoid unseemly churn in the affected code > bases. > > [...] Here is the summary with links: - [net-next,1/3] ipv6: Add sockaddr_inet unified address structure https://git.kernel.org/netdev/net-next/c/463deed51796 - [net-next,2/3] wireguard: peer: Replace sockaddr with sockaddr_inet https://git.kernel.org/netdev/net-next/c/9203e0a82c0b - [net-next,3/3] sctp: Replace sockaddr with sockaddr_inet in sctp_addr union https://git.kernel.org/netdev/net-next/c/511d10b4c2f9 You are awesome, thank you! -- Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html