mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] wireguard: Mark concurrent accesses to reused indices
@ 2026-11-12  2:30 Zhu Jun
  2026-11-12  2:37 ` netdev-bot+sinfo
  0 siblings, 1 reply; 3+ messages in thread
From: Zhu Jun @ 2026-11-12  2:30 UTC (permalink / raw)
  To: Jason
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, wireguard, netdev,
	linux-kernel, Zhu Jun

Removing a handshake entry from the index table does not wait for RCU
readers that already found it. Reinserting the entry assigns a new index
while those readers may still compare the old entry's index, which KCSAN
reports as a data race.

Use WRITE_ONCE() for index reassignment and READ_ONCE() for index
comparisons in the table walks. Keep the existing RCU lifetime protection
and locked collision check.

OpenAI Codex assisted with analysis and patch preparation.

Fixes: e7096c131e51 ("net: WireGuard secure network tunnel")
Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>
---
 drivers/net/wireguard/peerlookup.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/net/wireguard/peerlookup.c b/drivers/net/wireguard/peerlookup.c
index 99651a86ac95..c8d943711a6a 100644
--- a/drivers/net/wireguard/peerlookup.c
+++ b/drivers/net/wireguard/peerlookup.c
@@ -129,11 +129,11 @@ __le32 wg_index_hashtable_insert(struct index_hashtable *table,
 
 search_unused_slot:
 	/* First we try to find an unused slot, randomly, while unlocked. */
-	entry->index = (__force __le32)get_random_u32();
+	WRITE_ONCE(entry->index, (__force __le32)get_random_u32());
 	hlist_for_each_entry_rcu_bh(existing_entry,
 				    index_bucket(table, entry->index),
 				    index_hash) {
-		if (existing_entry->index == entry->index)
+		if (READ_ONCE(existing_entry->index) == entry->index)
 			/* If it's already in use, we continue searching. */
 			goto search_unused_slot;
 	}
@@ -145,7 +145,7 @@ __le32 wg_index_hashtable_insert(struct index_hashtable *table,
 	hlist_for_each_entry_rcu_bh(existing_entry,
 				    index_bucket(table, entry->index),
 				    index_hash) {
-		if (existing_entry->index == entry->index) {
+		if (READ_ONCE(existing_entry->index) == entry->index) {
 			spin_unlock_bh(&table->lock);
 			/* If it was stolen, we start over. */
 			goto search_unused_slot;
@@ -208,7 +208,7 @@ wg_index_hashtable_lookup(struct index_hashtable *table,
 	rcu_read_lock_bh();
 	hlist_for_each_entry_rcu_bh(iter_entry, index_bucket(table, index),
 				    index_hash) {
-		if (iter_entry->index == index) {
+		if (READ_ONCE(iter_entry->index) == index) {
 			if (likely(iter_entry->type & type_mask))
 				entry = iter_entry;
 			break;
-- 
2.51.0




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

end of thread, other threads:[~2026-11-12  3:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-12  2:30 [PATCH] wireguard: Mark concurrent accesses to reused indices Zhu Jun
2026-11-12  2:37 ` netdev-bot+sinfo
2026-11-12  3:05   ` Zhu Jun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®