Netdev List
 help / color / mirror / Atom feed
From: Eric Dumazet <edumazet@kernel.org>
To: "David S . Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>
Cc: Simon Horman <horms@kernel.org>,
	netdev@vger.kernel.org, Eric Dumazet <edumazet@kernel.org>,
	Alexander Aring <alex.aring@gmail.com>
Subject: [PATCH net-next] 6lowpan: nhc: use RCU instead of lowpan_nhc_lock
Date: Wed, 18 Nov 2026 23:18:20 +0100	[thread overview]
Message-ID: <20261118221820.1601273-1-edumazet@kernel.org> (raw)

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


                 reply	other threads:[~2026-11-18 22:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118221820.1601273-1-edumazet@kernel.org \
    --to=edumazet@kernel.org \
    --cc=alex.aring@gmail.com \
    --cc=davem@davemloft.net \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox