🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by Eric Dumazet Wed, 18 Nov 2026 22:18
[PATCH net-next] 6lowpan: nhc: use RCU instead of lowpan_nhc_lock
#18780
Author: Eric Dumazet
Date: Wed, 18 Nov 2026 22:18
187 lines
12631 bytes
lowpan_nexthdr_nhcs[] is protected by lowpan_nhc_lock, a spinlock
that is acquired with BH disabled for every compressed or uncompressed
packet, even though the table only changes when an NHC module is
loaded or unloaded.

lowpan_nhc_del() already calls synchronize_net() after clearing
its slot, so readers only need to be in an RCU read-side critical
section to safely use a struct lowpan_nhc.

Convert the table to RCU:

- Mark lowpan_nexthdr_nhcs[] as __rcu.

- Readers use rcu_read_lock() and rcu_dereference().

- lowpan_nhc_add() uses cmpxchg() to install the new entry only if
  the slot is empty, and lowpan_nhc_del() uses RCU_INIT_POINTER()
  to clear it.

lowpan_nhc_lock is removed.

While at it, use a single exit path in lowpan_nhc_do_uncompression().

The ->compress() and ->uncompress() handlers are no longer called
with BH disabled by this file. None of the in-tree handlers depends
on it.

Compile tested only, sparse (C=2) reports no warning.

Signed-off-by: Eric Dumazet <edumazet@kernel.org>
Cc: Alexander Aring <alex.aring@gmail.com>
---
 net/6lowpan/nhc.c | 77 +++++++++++++++++++----------------------------
 1 file changed, 31 insertions(+), 46 deletions(-)

diff --git a/net/6lowpan/nhc.c b/net/6lowpan/nhc.c
index a4dde85664f2f8ec52a403a94f85422dccf63367..b996fd4a3ece03fbad8e6518faa8eaf21f6aaa82 100644
--- a/net/6lowpan/nhc.c
+++ b/net/6lowpan/nhc.c
@@ -12,8 +12,7 @@
 
 #include "nhc.h"
 
-static const struct lowpan_nhc *lowpan_nexthdr_nhcs[NEXTHDR_MAX + 1];
-static DEFINE_SPINLOCK(lowpan_nhc_lock);
+static const struct lowpan_nhc __rcu *lowpan_nexthdr_nhcs[NEXTHDR_MAX + 1];
 
 static const struct lowpan_nhc *lowpan_nhc_by_nhcid(struct sk_buff *skb)
 {
@@ -27,7 +26,7 @@ static const struct lowpan_nhc *lowpan_nhc_by_nhcid(struct sk_buff *skb)
 	id = *skb->data;
 
 	for (i = 0; i < NEXTHDR_MAX + 1; i++) {
-		nhc = lowpan_nexthdr_nhcs[i];
+		nhc = rcu_dereference(lowpan_nexthdr_nhcs[i]);
 		if (!nhc)
 			continue;
 
@@ -44,13 +43,13 @@ int lowpan_nhc_check_compression(struct sk_buff *skb,
 	const struct lowpan_nhc *nhc;
 	int ret = 0;
 
-	spin_lock_bh(&lowpan_nhc_lock);
+	rcu_read_lock();
 
-	nhc = lowpan_nexthdr_nhcs[hdr->nexthdr];
+	nhc = rcu_dereference(lowpan_nexthdr_nhcs[hdr->nexthdr]);
 	if (!(nhc && nhc->compress))
 		ret = -ENOENT;
 
-	spin_unlock_bh(&lowpan_nhc_lock);
+	rcu_read_unlock();
 
 	return ret;
 }
@@ -61,9 +60,9 @@ int lowpan_nhc_do_compression(struct sk_buff *skb, const struct ipv6hdr *hdr,
 	int ret;
 	const struct lowpan_nhc *nhc;
 
-	spin_lock_bh(&lowpan_nhc_lock);
+	rcu_read_lock();
 
-	nhc = lowpan_nexthdr_nhcs[hdr->nexthdr];
+	nhc = rcu_dereference(lowpan_nexthdr_nhcs[hdr->nexthdr]);
 	/* check if the nhc module was removed in unlocked part.
 	 * TODO: this is a workaround we should prevent unloading
 	 * of nhc modules while unlocked part, this will always drop
@@ -93,7 +92,7 @@ int lowpan_nhc_do_compression(struct sk_buff *skb, const struct ipv6hdr *hdr,
 	skb_pull(skb, nhc->nexthdrlen);
 
 out:
-	spin_unlock_bh(&lowpan_nhc_lock);
+	rcu_read_unlock();
 
 	return ret;
 }
@@ -105,64 +104,50 @@ int lowpan_nhc_do_uncompression(struct sk_buff *skb,
 	const struct lowpan_nhc *nhc;
 	int ret;
 
-	spin_lock_bh(&lowpan_nhc_lock);
+	rcu_read_lock();
 
 	nhc = lowpan_nhc_by_nhcid(skb);
-	if (nhc) {
-		if (nhc->uncompress) {
-			ret = nhc->uncompress(skb, sizeof(struct ipv6hdr) +
-					      nhc->nexthdrlen);
-			if (ret < 0) {
-				spin_unlock_bh(&lowpan_nhc_lock);
-				return ret;
-			}
-		} else {
-			netdev_warn(dev, "received nhc id for %s which is not implemented.\n",
-				    nhc->name);
-			spin_unlock_bh(&lowpan_nhc_lock);
-			return -ENOTSUPP;
-		}
-	} else {
-		spin_unlock_bh(&lowpan_nhc_lock);
+	if (!nhc) {
 		netdev_warn(dev, "received unknown nhc id which was not found.\n");
-		return -ENOENT;
+		ret = -ENOENT;
+		goto out;
+	}
+
+	if (!nhc->uncompress) {
+		netdev_warn(dev, "received nhc id for %s which is not implemented.\n",
+			    nhc->name);
+		ret = -ENOTSUPP;
+		goto out;
 	}
 
+	ret = nhc->uncompress(skb, sizeof(struct ipv6hdr) + nhc->nexthdrlen);
+	if (ret < 0)
+		goto out;
+
 	hdr->nexthdr = nhc->nexthdr;
 	skb_reset_transport_header(skb);
 	raw_dump_table(__func__, "raw transport header dump",
 		       skb_transport_header(skb), nhc->nexthdrlen);
 
-	spin_unlock_bh(&lowpan_nhc_lock);
+out:
+	rcu_read_unlock();
 
-	return 0;
+	return ret;
 }
 
 int lowpan_nhc_add(const struct lowpan_nhc *nhc)
 {
-	int ret = 0;
-
-	spin_lock_bh(&lowpan_nhc_lock);
-
-	if (lowpan_nexthdr_nhcs[nhc->nexthdr]) {
-		ret = -EEXIST;
-		goto out;
-	}
+	if (cmpxchg(&lowpan_nexthdr_nhcs[nhc->nexthdr], NULL,
+		    RCU_INITIALIZER(nhc)))
+		return -EEXIST;
 
-	lowpan_nexthdr_nhcs[nhc->nexthdr] = nhc;
-out:
-	spin_unlock_bh(&lowpan_nhc_lock);
-	return ret;
+	return 0;
 }
 EXPORT_SYMBOL(lowpan_nhc_add);
 
 void lowpan_nhc_del(const struct lowpan_nhc *nhc)
 {
-	spin_lock_bh(&lowpan_nhc_lock);
-
-	lowpan_nexthdr_nhcs[nhc->nexthdr] = NULL;
-
-	spin_unlock_bh(&lowpan_nhc_lock);
+	RCU_INIT_POINTER(lowpan_nexthdr_nhcs[nhc->nexthdr], NULL);
 
 	synchronize_net();
 }
-- 
2.53.0
Re: [PATCH net-next] 6lowpan: nhc: use RCU instead of lowpan_nhc_lock
#20070
Author: patchwork-bot+netdevbpf
Date: Sat, 21 Nov 2026 02:00
26 lines
8649 bytes
Hello:

This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Wed, 18 Nov 2026 23:18:20 +0100 you wrote:
> lowpan_nexthdr_nhcs[] is protected by lowpan_nhc_lock, a spinlock
> that is acquired with BH disabled for every compressed or uncompressed
> packet, even though the table only changes when an NHC module is
> loaded or unloaded.
> 
> lowpan_nhc_del() already calls synchronize_net() after clearing
> its slot, so readers only need to be in an RCU read-side critical
> section to safely use a struct lowpan_nhc.
> 
> [...]

Here is the summary with links:
  - [net-next] 6lowpan: nhc: use RCU instead of lowpan_nhc_lock
    https://git.kernel.org/netdev/net-next/c/111e1b0b985e

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads