FDB events reach DSA from the atomic switchdev notifier and are handled from dsa_owq. The work builds the dsa_db from dp->bridge, which dsa_port_bridge_destroy() clears and frees when the port leaves the bridge or fails to join it. When switchdev_bridge_port_offload() fails in the FDB replay of dsa_port_bridge_join(), because dsa_user_fdb_event() could not allocate its work item, the entries replayed before the failure are still queued. The rollback clears dp->bridge, and the queued work dereferences it: KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] Workqueue: dsa_ordered dsa_user_switchdev_event_work RIP: 0010:dsa_port_bridge_host_fdb_add+0x141/0x5c0 A bridge leave has the same problem for an event that the notifier queues after dsa_port_pre_bridge_leave() flushed the workqueue: nothing orders the dp->bridge check in dsa_user_fdb_event() against the clear, and on a switch unbind the work can also run after dsa_user_destroy() freed the port's net_device. The notifier and the taggers read dp->bridge->dev and ->num without locks while the bridge is freed. Clear dp->bridge, wait with synchronize_net(), flush the workqueue, and only then drop the reference and free the bridge. Nothing in flight can take a reference meanwhile. The refcount is only raised in dsa_port_bridge_create(), on a bridge that dsa_tree_bridge_find() returned, and both run in the join path under RTNL, which dsa_port_bridge_destroy() holds too. The paths without RTNL only read dp->bridge, and all of them are RCU readers: the switchdev notifier is called under rcu_read_lock() by atomic_notifier_call_chain() and by the FDB replay, and the taggers run in the RCU sections of the transmit and receive paths. synchronize_net() waits for them. The work a notifier queued is on dsa_owq by then, and the flush waits for it. The cost is one expedited RCU grace period, under RTNL, each time a port leaves a bridge. The work reads dp->bridge once and drops the event when the port has left; the FDB helpers take the bridge from it. The join rollback destroys the bridge before it tells the switches that the port left, as dsa_port_bridge_leave() does, so a dropped event programs nothing after that. dsa_port_bridge_dev_get() and dsa_port_bridge_num_get() read the pointer once, and dsa_port_bridge_create() publishes it with smp_store_release(), so a reader that sees it also sees its fields. The other readers outside RTNL only test the pointer, with READ_ONCE(). Fixes: c26933639b54 ("net: dsa: request drivers to perform FDB isolation") Fixes: d3eed0e57d5d ("net: dsa: keep the bridge_dev and bridge_num as part of the same structure") Link: https://lore.kernel.org/all/179480421335.3136.15795220009190349154@kernel.org/ [1] Signed-off-by: Julius Bairaktaris --- The rollback oops was pointed out by the hosted Sashiko review of a DSA host FDB series [1]. In QEMU with KASAN, dsa_loop given FDB operations that take 15 ms, 40 static FDB entries to replay and the N-th allocation of "ip link set lan1 master br0" failing for N = 1 to 150, net oopses as above in 3 of 3 boots, and with this change in none of 4 boots. With the change, 2 to 5 host addresses per boot stay on the CPU port after those joins (5, 4, 5 and 2 more additions than deletions). They were added by replayed events that ran before the allocation failed: br_switchdev_port_offload() does not undo the part of the replay that succeeded, and this change does not alter that. v4: - No change; rebased on net 6902a2bb708c. v3: - The message says why no in-flight notification or tagger can take a reference to the bridge, and what the grace period costs, after Paolo Abeni's question on v1: https://lore.kernel.org/all/dfe749f5-83de-4802-bcff-7517ea6b7283@redhat.com/ - Code unchanged. Rebased on net e9d78fc6741a; the join sweep ran once more on it. v2: - The other readers of dp->bridge outside RTNL, dsa_port_to_bridge_port(), dsa_find_designated_bridge_port_by_vid(), dsa_default_offload_fwd_mark() and dsa_tag_8021q_find_port_by_vbid(), use READ_ONCE() (hosted Sashiko review of v1). - The comment in dsa_port_bridge_destroy() speaks of a tagger that read the bridge; a tagger that reads NULL is patch 3. v3: https://lore.kernel.org/all/20261204222045.214014-2-julius@bairaktaris.de/ v2: https://lore.kernel.org/all/20261128133503.3887987-2-julius@bairaktaris.de/ v1: https://lore.kernel.org/all/20261121185821.1471559-1-julius@bairaktaris.de/ include/net/dsa.h | 10 ++++++--- net/dsa/port.c | 50 ++++++++++++++++++++++++++++++--------------- net/dsa/port.h | 26 ++++++++++++----------- net/dsa/tag.h | 4 ++-- net/dsa/tag_8021q.c | 2 +- net/dsa/user.c | 25 ++++++++++++++++------- 6 files changed, 75 insertions(+), 42 deletions(-) diff --git a/include/net/dsa.h b/include/net/dsa.h index 5d12191b6f6f5..47e2162f1d7d3 100644 --- a/include/net/dsa.h +++ b/include/net/dsa.h @@ -758,7 +758,7 @@ static inline struct net_device *dsa_port_to_conduit(const struct dsa_port *dp) static inline struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp) { - if (!dp->bridge) + if (!READ_ONCE(dp->bridge)) return NULL; if (dp->lag) @@ -772,12 +772,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 1f5536c0dffc2..e41d3639f0f98 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; + /* Read without locks, see below */ + 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; + /* Read without locks from the switchdev notifiers and the taggers */ + smp_store_release(&dp->bridge, bridge); return 0; } @@ -457,7 +459,14 @@ static void dsa_port_bridge_destroy(struct dsa_port *dp, { struct dsa_bridge *bridge = dp->bridge; - dp->bridge = NULL; + WRITE_ONCE(dp->bridge, NULL); + + /* An FDB event from the atomic switchdev notifier that still sees the + * bridge can queue work for it, and a tagger that read it can still + * use it. Wait for them, then for the work they queued. + */ + synchronize_net(); + dsa_flush_workqueue(); if (!refcount_dec_and_test(&bridge->refcount)) return; @@ -528,7 +537,12 @@ int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br, &dsa_user_switchdev_blocking_notifier); dsa_flush_workqueue(); out_rollback_unbridge: + /* Drop the FDB events still queued from the replay before the + * switches are told that the port left + */ + dsa_port_bridge_destroy(dp, br); dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); + return err; out_rollback: dsa_port_bridge_destroy(dp, br); return err; @@ -973,8 +987,8 @@ int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu) return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info); } -int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, - u16 vid) +int dsa_port_fdb_add(struct dsa_port *dp, const struct dsa_bridge *bridge, + const unsigned char *addr, u16 vid) { struct dsa_notifier_fdb_info info = { .dp = dp, @@ -982,7 +996,7 @@ int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, .vid = vid, .db = { .type = DSA_DB_BRIDGE, - .bridge = *dp->bridge, + .bridge = *bridge, }, }; @@ -996,8 +1010,8 @@ int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info); } -int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, - u16 vid) +int dsa_port_fdb_del(struct dsa_port *dp, const struct dsa_bridge *bridge, + const unsigned char *addr, u16 vid) { struct dsa_notifier_fdb_info info = { .dp = dp, @@ -1005,7 +1019,7 @@ int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, .vid = vid, .db = { .type = DSA_DB_BRIDGE, - .bridge = *dp->bridge, + .bridge = *bridge, }, }; @@ -1041,12 +1055,13 @@ int dsa_port_standalone_host_fdb_add(struct dsa_port *dp, } int dsa_port_bridge_host_fdb_add(struct dsa_port *dp, + const struct dsa_bridge *bridge, const unsigned char *addr, u16 vid) { struct net_device *conduit = dsa_port_to_conduit(dp); struct dsa_db db = { .type = DSA_DB_BRIDGE, - .bridge = *dp->bridge, + .bridge = *bridge, }; int err; @@ -1092,12 +1107,13 @@ int dsa_port_standalone_host_fdb_del(struct dsa_port *dp, } int dsa_port_bridge_host_fdb_del(struct dsa_port *dp, + const struct dsa_bridge *bridge, const unsigned char *addr, u16 vid) { struct net_device *conduit = dsa_port_to_conduit(dp); struct dsa_db db = { .type = DSA_DB_BRIDGE, - .bridge = *dp->bridge, + .bridge = *bridge, }; int err; @@ -1113,8 +1129,8 @@ int dsa_port_bridge_host_fdb_del(struct dsa_port *dp, return dsa_port_host_fdb_del(dp, addr, vid, db); } -int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr, - u16 vid) +int dsa_port_lag_fdb_add(struct dsa_port *dp, const struct dsa_bridge *bridge, + const unsigned char *addr, u16 vid) { struct dsa_notifier_lag_fdb_info info = { .lag = dp->lag, @@ -1122,7 +1138,7 @@ int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr, .vid = vid, .db = { .type = DSA_DB_BRIDGE, - .bridge = *dp->bridge, + .bridge = *bridge, }, }; @@ -1132,8 +1148,8 @@ int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr, return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_ADD, &info); } -int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr, - u16 vid) +int dsa_port_lag_fdb_del(struct dsa_port *dp, const struct dsa_bridge *bridge, + const unsigned char *addr, u16 vid) { struct dsa_notifier_lag_fdb_info info = { .lag = dp->lag, @@ -1141,7 +1157,7 @@ int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr, .vid = vid, .db = { .type = DSA_DB_BRIDGE, - .bridge = *dp->bridge, + .bridge = *bridge, }, }; diff --git a/net/dsa/port.h b/net/dsa/port.h index 6bc3291573c02..e665baf7454a5 100644 --- a/net/dsa/port.h +++ b/net/dsa/port.h @@ -46,22 +46,24 @@ int dsa_port_mst_enable(struct dsa_port *dp, bool on, int dsa_port_vlan_msti(struct dsa_port *dp, const struct switchdev_vlan_msti *msti); int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu); -int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, - u16 vid); -int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, - u16 vid); +int dsa_port_fdb_add(struct dsa_port *dp, const struct dsa_bridge *bridge, + const unsigned char *addr, u16 vid); +int dsa_port_fdb_del(struct dsa_port *dp, const struct dsa_bridge *bridge, + const unsigned char *addr, u16 vid); int dsa_port_standalone_host_fdb_add(struct dsa_port *dp, const unsigned char *addr, u16 vid); int dsa_port_standalone_host_fdb_del(struct dsa_port *dp, const unsigned char *addr, u16 vid); -int dsa_port_bridge_host_fdb_add(struct dsa_port *dp, const unsigned char *addr, - u16 vid); -int dsa_port_bridge_host_fdb_del(struct dsa_port *dp, const unsigned char *addr, - u16 vid); -int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr, - u16 vid); -int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr, - u16 vid); +int dsa_port_bridge_host_fdb_add(struct dsa_port *dp, + const struct dsa_bridge *bridge, + const unsigned char *addr, u16 vid); +int dsa_port_bridge_host_fdb_del(struct dsa_port *dp, + const struct dsa_bridge *bridge, + const unsigned char *addr, u16 vid); +int dsa_port_lag_fdb_add(struct dsa_port *dp, const struct dsa_bridge *bridge, + const unsigned char *addr, u16 vid); +int dsa_port_lag_fdb_del(struct dsa_port *dp, const struct dsa_bridge *bridge, + const unsigned char *addr, u16 vid); int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data); int dsa_port_mdb_add(const struct dsa_port *dp, const struct switchdev_obj_port_mdb *mdb); diff --git a/net/dsa/tag.h b/net/dsa/tag.h index cf52283fe9df4..0e1be1da4537e 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 f91646e602465..de7e24f755055 100644 --- a/net/dsa/tag_8021q.c +++ b/net/dsa/tag_8021q.c @@ -482,7 +482,7 @@ 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) + if (!READ_ONCE(dp->bridge)) continue; if (dp->stp_state != BR_STATE_LEARNING && diff --git a/net/dsa/user.c b/net/dsa/user.c index 041f9060c8ef0..81f0d3901209f 100644 --- a/net/dsa/user.c +++ b/net/dsa/user.c @@ -3646,6 +3646,7 @@ static void dsa_user_switchdev_event_work(struct work_struct *work) const unsigned char *addr = switchdev_work->addr; struct net_device *dev = switchdev_work->dev; u16 vid = switchdev_work->vid; + const struct dsa_bridge *bridge; struct dsa_switch *ds; struct dsa_port *dp; int err; @@ -3653,14 +3654,22 @@ static void dsa_user_switchdev_event_work(struct work_struct *work) dp = dsa_user_to_port(dev); ds = dp->ds; + /* Gone if the port left the bridge after the event was queued: + * dsa_port_bridge_destroy() clears it before it flushes the workqueue + */ + bridge = READ_ONCE(dp->bridge); + if (!bridge) + goto out; + switch (switchdev_work->event) { case SWITCHDEV_FDB_ADD_TO_DEVICE: if (switchdev_work->host_addr) - err = dsa_port_bridge_host_fdb_add(dp, addr, vid); + err = dsa_port_bridge_host_fdb_add(dp, bridge, addr, + vid); else if (dp->lag) - err = dsa_port_lag_fdb_add(dp, addr, vid); + err = dsa_port_lag_fdb_add(dp, bridge, addr, vid); else - err = dsa_port_fdb_add(dp, addr, vid); + err = dsa_port_fdb_add(dp, bridge, addr, vid); if (err) { dev_err(ds->dev, "port %d failed to add %pM vid %d to fdb: %d\n", @@ -3672,11 +3681,12 @@ static void dsa_user_switchdev_event_work(struct work_struct *work) case SWITCHDEV_FDB_DEL_TO_DEVICE: if (switchdev_work->host_addr) - err = dsa_port_bridge_host_fdb_del(dp, addr, vid); + err = dsa_port_bridge_host_fdb_del(dp, bridge, addr, + vid); else if (dp->lag) - err = dsa_port_lag_fdb_del(dp, addr, vid); + err = dsa_port_lag_fdb_del(dp, bridge, addr, vid); else - err = dsa_port_fdb_del(dp, addr, vid); + err = dsa_port_fdb_del(dp, bridge, addr, vid); if (err) { dev_err(ds->dev, "port %d failed to delete %pM vid %d from fdb: %d\n", @@ -3686,6 +3696,7 @@ static void dsa_user_switchdev_event_work(struct work_struct *work) break; } +out: kfree(switchdev_work); } @@ -3718,7 +3729,7 @@ static int dsa_user_fdb_event(struct net_device *dev, if (ctx && ctx != dp) return 0; - if (!dp->bridge) + if (!READ_ONCE(dp->bridge)) return 0; if (switchdev_fdb_is_dynamically_learned(fdb_info)) { -- 2.53.0