From: Julius Bairaktaris <julius@bairaktaris.de>
To: Andrew Lunn <andrew@lunn.ch>, Vladimir Oltean <olteanv@gmail.com>,
netdev@vger.kernel.org
Cc: "David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@kernel.org>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Willem de Bruijn" <willemb@google.com>,
"Simon Horman" <horms@kernel.org>,
"Alvin Šipraga" <alvin.sipraga@analog.com>,
"Linus Walleij" <linusw@kernel.org>,
"Florian Fainelli" <florian.fainelli@broadcom.com>,
"Pawel Dembicki" <paweldembicki@gmail.com>,
"Tobias Waldekranz" <tobias@waldekranz.com>,
"Daniel Golle" <daniel@makrotopia.org>,
"Russell King" <linux@armlinux.org.uk>,
linux-kernel@vger.kernel.org
Subject: [PATCH net 1/6] net: dsa: free struct dsa_bridge after an RCU grace period
Date: Sat, 28 Nov 2026 17:52:00 +0100 [thread overview]
Message-ID: <20261128165206.3248835-2-julius@bairaktaris.de> (raw)
In-Reply-To: <20261128165206.3248835-1-julius@bairaktaris.de>
A tagger that offloads TX forwarding reads dp->bridge from its xmit
function, in an RCU read-side critical section with BH disabled, to find
the bridge number of a frame that the bridge marked with
offload_fwd_mark; the receive path reads it as well
(dsa_tag_8021q_find_port_by_vbid()). dsa_port_bridge_destroy() clears
dp->bridge under rtnl_lock and frees the structure with kfree() once the
last port has left. A frame that was marked before the leave can still
be on its way through the tagger at that point, and the tagger then
dereferences freed memory. dsa_port_bridge_dev_get() and
dsa_port_bridge_num_get() read the pointer twice, so a concurrent leave
also turns their NULL test into a NULL dereference.
Free the structure with kfree_rcu(), publish dp->bridge with
smp_store_release() and clear it with WRITE_ONCE(), and read it with
READ_ONCE() in the datapath: in both helpers, once each, and in
dsa_find_designated_bridge_port_by_vid(), dsa_default_offload_fwd_mark()
and dsa_tag_8021q_find_port_by_vbid(), which uses the bridge it read for
the number as well.
Found by an LLM review (Claude).
Fixes: d3eed0e57d5d ("net: dsa: keep the bridge_dev and bridge_num as part of the same structure")
Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de>
---
include/net/dsa.h | 14 ++++++++++++--
net/dsa/port.c | 11 +++++++----
net/dsa/tag.h | 4 ++--
net/dsa/tag_8021q.c | 6 ++++--
4 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/include/net/dsa.h b/include/net/dsa.h
index 5d12191b6f6..1076439e838 100644
--- a/include/net/dsa.h
+++ b/include/net/dsa.h
@@ -242,6 +242,7 @@ struct dsa_bridge {
unsigned int num;
bool tx_fwd_offload;
refcount_t refcount;
+ struct rcu_head rcu;
};
struct dsa_port {
@@ -302,6 +303,11 @@ struct dsa_port {
struct device_node *dn;
unsigned int ageing_time;
+ /* Written under rtnl_lock. The datapath reads it once, with
+ * READ_ONCE(), in an RCU read-side critical section: the port can
+ * leave its bridge while a frame is on its way through the tagger,
+ * and the structure is freed only after a grace period.
+ */
struct dsa_bridge *bridge;
struct devlink_port devlink_port;
struct phylink *pl;
@@ -772,12 +778,16 @@ struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp)
static inline struct net_device *
dsa_port_bridge_dev_get(const struct dsa_port *dp)
{
- return dp->bridge ? dp->bridge->dev : NULL;
+ const struct dsa_bridge *bridge = READ_ONCE(dp->bridge);
+
+ return bridge ? bridge->dev : NULL;
}
static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp)
{
- return dp->bridge ? dp->bridge->num : 0;
+ const struct dsa_bridge *bridge = READ_ONCE(dp->bridge);
+
+ return bridge ? bridge->num : 0;
}
static inline bool dsa_port_bridge_same(const struct dsa_port *a,
diff --git a/net/dsa/port.c b/net/dsa/port.c
index 1f5536c0dff..997e68cd215 100644
--- a/net/dsa/port.c
+++ b/net/dsa/port.c
@@ -427,7 +427,8 @@ static int dsa_port_bridge_create(struct dsa_port *dp,
bridge = dsa_tree_bridge_find(ds->dst, br);
if (bridge) {
refcount_inc(&bridge->refcount);
- dp->bridge = bridge;
+ /* Pairs with the READ_ONCE() of the datapath */
+ smp_store_release(&dp->bridge, bridge);
return 0;
}
@@ -447,7 +448,8 @@ static int dsa_port_bridge_create(struct dsa_port *dp,
return -EOPNOTSUPP;
}
- dp->bridge = bridge;
+ /* Pairs with the READ_ONCE() of the datapath */
+ smp_store_release(&dp->bridge, bridge);
return 0;
}
@@ -457,7 +459,7 @@ static void dsa_port_bridge_destroy(struct dsa_port *dp,
{
struct dsa_bridge *bridge = dp->bridge;
- dp->bridge = NULL;
+ WRITE_ONCE(dp->bridge, NULL);
if (!refcount_dec_and_test(&bridge->refcount))
return;
@@ -465,7 +467,8 @@ static void dsa_port_bridge_destroy(struct dsa_port *dp,
if (bridge->num)
dsa_bridge_num_put(br, bridge->num);
- kfree(bridge);
+ /* The datapath may still hold the pointer it read from dp->bridge */
+ kfree_rcu(bridge, rcu);
}
static bool dsa_port_supports_mst(struct dsa_port *dp)
diff --git a/net/dsa/tag.h b/net/dsa/tag.h
index cf52283fe9d..0e1be1da453 100644
--- a/net/dsa/tag.h
+++ b/net/dsa/tag.h
@@ -201,7 +201,7 @@ dsa_find_designated_bridge_port_by_vid(struct net_device *conduit, u16 vid)
if (dp->type != DSA_PORT_TYPE_USER)
continue;
- if (!dp->bridge)
+ if (!READ_ONCE(dp->bridge))
continue;
if (dp->stp_state != BR_STATE_LEARNING &&
@@ -238,7 +238,7 @@ static inline void dsa_default_offload_fwd_mark(struct sk_buff *skb)
{
struct dsa_port *dp = dsa_user_to_port(skb->dev);
- skb->offload_fwd_mark = !!(dp->bridge);
+ skb->offload_fwd_mark = !!READ_ONCE(dp->bridge);
}
/* Helper for removing DSA header tags from packets in the RX path.
diff --git a/net/dsa/tag_8021q.c b/net/dsa/tag_8021q.c
index f91646e6024..ac9ba1ee04f 100644
--- a/net/dsa/tag_8021q.c
+++ b/net/dsa/tag_8021q.c
@@ -482,7 +482,9 @@ dsa_tag_8021q_find_port_by_vbid(struct net_device *conduit, int vbid)
return NULL;
dsa_tree_for_each_user_port(dp, dst) {
- if (!dp->bridge)
+ const struct dsa_bridge *bridge = READ_ONCE(dp->bridge);
+
+ if (!bridge)
continue;
if (dp->stp_state != BR_STATE_LEARNING &&
@@ -492,7 +494,7 @@ dsa_tag_8021q_find_port_by_vbid(struct net_device *conduit, int vbid)
if (dp->cpu_dp != cpu_dp)
continue;
- if (dsa_port_bridge_num_get(dp) == vbid)
+ if (bridge->num == vbid)
return dp->user;
}
--
2.53.0
next prev parent reply other threads:[~2026-11-28 16:52 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 16:51 [PATCH net 0/6] net: dsa: offload_fwd_mark on unbridged ports, bridge numbers across switches Julius Bairaktaris
2026-11-28 16:52 ` Julius Bairaktaris [this message]
2026-11-28 16:52 ` [PATCH net 2/6] net: dsa: tag_sja1105, tag_vsc73xx_8021q: read the bridge once in xmit Julius Bairaktaris
2026-12-06 3:54 ` willemb
2026-11-28 16:52 ` [PATCH net 3/6] net: dsa: tag_dsa: send a marked frame of an unbridged port from the CPU Julius Bairaktaris
2026-12-06 3:55 ` willemb
2026-11-28 16:52 ` [PATCH net 4/6] net: dsa: do not hand a switch a bridge number above its limit Julius Bairaktaris
2026-12-06 3:55 ` willemb
2026-11-28 16:52 ` [PATCH net 5/6] net: dsa: do not take a bridge number past the end of the bitmap Julius Bairaktaris
2026-11-28 16:52 ` [PATCH net 6/6] net: dsa: keep a bridge number while another tree uses it Julius Bairaktaris
2026-12-06 3:57 ` willemb
2026-12-06 3:54 ` [PATCH net 0/6] net: dsa: offload_fwd_mark on unbridged ports, bridge numbers across switches willemb
2026-12-06 10:06 ` Julius Bairaktaris
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=20261128165206.3248835-2-julius@bairaktaris.de \
--to=julius@bairaktaris.de \
--cc=alvin.sipraga@analog.com \
--cc=andrew@lunn.ch \
--cc=daniel@makrotopia.org \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=florian.fainelli@broadcom.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linusw@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=netdev@vger.kernel.org \
--cc=olteanv@gmail.com \
--cc=pabeni@redhat.com \
--cc=paweldembicki@gmail.com \
--cc=tobias@waldekranz.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
all inboxes | Powered by JetHome®