Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next] 6lowpan: nhc: use RCU instead of lowpan_nhc_lock
@ 2026-11-18 22:18 Eric Dumazet
  0 siblings, 0 replies; only message in thread
From: Eric Dumazet @ 2026-11-18 22:18 UTC (permalink / raw)
  To: David S . Miller, Jakub Kicinski, Paolo Abeni, Willem de Bruijn
  Cc: Simon Horman, netdev, Eric Dumazet, Alexander Aring

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-18 22:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 22:18 [PATCH net-next] 6lowpan: nhc: use RCU instead of lowpan_nhc_lock Eric Dumazet

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