Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next] net: dsa: report a deleted FDB entry as no longer offloaded
@ 2026-11-22 17:44 Julius Bairaktaris
  2026-11-23 17:44 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Julius Bairaktaris @ 2026-11-22 17:44 UTC (permalink / raw)
  To: Andrew Lunn, Vladimir Oltean
  Cc: Nikolay Aleksandrov, Ido Schimmel, netdev, bridge,
	David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman

dsa_user_switchdev_event_work() reports SWITCHDEV_FDB_OFFLOADED with
offloaded=true once it has added an FDB entry to the switch, and reports
nothing once it has deleted one. The bridge sets BR_FDB_OFFLOADED on the
entry when it gets the report, so an entry that the bridge keeps after
DSA deleted it from the switch is still shown as offloaded, and matched
as such by "bridge fdb flush ... offloaded", while the switch no longer
holds it.

The bridge keeps such an entry when the address moves: an address
learned on a foreign port of the bridge, which DSA installs as a host
address on the CPU port when the switch asks for assisted learning, is
deleted from the CPU port when it is then learned on a user port, where
DSA does not install it. The flag is also left set when an addition is
still queued while the bridge withdraws the entry: the work items run in
order on the ordered DSA workqueue, so the addition reports the entry
as offloaded after the bridge withdrew it, and the deletion queued
behind it reports nothing.

Report offloaded=false when a deletion the bridge asked for succeeds.
Such a deletion reaches every DSA port that can hold the entry: the
user port it is on, each port of a LAG, or for a host address each user
port in the bridge. The reports come from the work items in the order
the switch was changed, so the last one for an entry gives the state
the switch was left in. A deletion replayed for one port that leaves
the bridge or a LAG is not reported, since a host address or a LAG
entry can still be held for the other ports. When the last port of the
switch leaves, the flag of the entries it held stays set, as before.
DSA's reports are ordered against each other, not against those of
another switchdev driver in the same bridge: a deletion still queued
here can clear the flag that driver set on an entry re-created on one
of its ports, as a queued addition could already set it.

The addition has been reported since commit c9eb3e0f8701 ("net: dsa: Add
support for learning FDB through notification"); a deletion never was.

Found by an LLM review of v1 of "net: bridge: withdraw the
switchdev entry of an fdb entry made dynamic".

In QEMU with KASAN, with dsa_loop given FDB operations and assisted
learning on the CPU port, and a tc mirred redirect that delivers frames
to a dsa_loop user port (test-only setup): an address learned on a veth
port of the bridge and then on the user port, and a static entry on the
user port that is deleted while its addition is still queued (dsa_loop
delays it by 500 ms) and whose address is then learned on that port, are
both still shown as offloaded without this change, in 3 of 3 boots
each. With it, neither is, in 3 of 3 boots each, and a host address
that stays on the CPU port when one of two user ports leaves the bridge
is still shown as offloaded, in 3 of 3 boots.

Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de>
---
This depends on "[PATCH net] net: dsa: hold the device a queued FDB event
is reported to" (https://lore.kernel.org/netdev/20261122173347.791590-1-julius@bairaktaris.de/,
the prerequisite-patch-id below), which keeps the device alive that the
report added here goes to.

Trigger: an address that DSA installed as a host address moves to a
user port, or an entry is withdrawn while its addition is still queued.
Impact: a wrong "offload" flag in the bridge's view only. CVSS 3.1 0.0.
Until the fix: the flag goes when the entry is deleted or ages out.

I used Claude (Anthropic) to help write these patches.

 net/dsa/user.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/net/dsa/user.c b/net/dsa/user.c
index 5e72fb34e308..7bcf8d3b5fcd 100644
--- a/net/dsa/user.c
+++ b/net/dsa/user.c
@@ -44,6 +44,7 @@ struct dsa_switchdev_event_work {
 	unsigned char addr[ETH_ALEN];
 	u16 vid;
 	bool host_addr;
+	bool replayed;
 };
 
 enum dsa_standalone_event {
@@ -3629,13 +3630,14 @@ static int dsa_user_netdevice_event(struct notifier_block *nb,
 }
 
 static void
-dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
+dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work,
+		       bool offloaded)
 {
 	struct switchdev_notifier_fdb_info info = {};
 
 	info.addr = switchdev_work->addr;
 	info.vid = switchdev_work->vid;
-	info.offloaded = true;
+	info.offloaded = offloaded;
 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
 				 switchdev_work->orig_dev, &info.info, NULL);
 }
@@ -3668,7 +3670,7 @@ static void dsa_user_switchdev_event_work(struct work_struct *work)
 				dp->index, addr, vid, err);
 			break;
 		}
-		dsa_fdb_offload_notify(switchdev_work);
+		dsa_fdb_offload_notify(switchdev_work, true);
 		break;
 
 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
@@ -3682,8 +3684,13 @@ static void dsa_user_switchdev_event_work(struct work_struct *work)
 			dev_err(ds->dev,
 				"port %d failed to delete %pM vid %d from fdb: %d\n",
 				dp->index, addr, vid, err);
+			break;
 		}
-
+		/* A deletion replayed for this port alone, as it leaves the
+		 * bridge or a LAG, can leave the entry held for other ports
+		 */
+		if (!switchdev_work->replayed)
+			dsa_fdb_offload_notify(switchdev_work, false);
 		break;
 	}
 
@@ -3774,6 +3781,7 @@ static int dsa_user_fdb_event(struct net_device *dev,
 	ether_addr_copy(switchdev_work->addr, fdb_info->addr);
 	switchdev_work->vid = fdb_info->vid;
 	switchdev_work->host_addr = host_addr;
+	switchdev_work->replayed = !!ctx;
 
 	dsa_schedule_work(&switchdev_work->work);
 

base-commit: eb6d6c80d35449c27471b1874f13066c09a8964d
prerequisite-patch-id: 7d26d57478287c871bcb5eb5837534cf4a2b48a3
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net-next] net: dsa: report a deleted FDB entry as no longer offloaded
  2026-11-22 17:44 [PATCH net-next] net: dsa: report a deleted FDB entry as no longer offloaded Julius Bairaktaris
@ 2026-11-23 17:44 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 17:44 UTC (permalink / raw)
  To: julius
  Cc: andrew, olteanv, razor, idosch, netdev, bridge, davem, edumazet,
	kuba, pabeni, horms

Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] Use-after-free of a foreign bridge port's net_device in the new
  deletion report.
- [Low] The commit message describes a user-visible defect: 'bridge fdb
  show' keeps the offload flag, and 'bridge fdb flush ... offloaded'…
- [Low] The new offloaded=false report is a delayed, per-port message
  meaning 'DSA's copy of (addr, vid) towards orig_dev is gone'.
- [Low] The new DEL path reports offloaded=false whenever the deletion
  returns 0, but for refcounted entries 0 does not mean the entry left…

--

net: dsa: report a deleted FDB entry as no longer offloaded

After a successful FDB deletion requested by the bridge,
dsa_user_switchdev_event_work() now reports SWITCHDEV_FDB_OFFLOADED with
offloaded=false. The bridge then clears BR_FDB_OFFLOADED on entries the
switch no longer holds. Deletions replayed for a single port that leaves a
bridge or LAG (ctx != NULL) are not reported.

> The addition has been reported since commit c9eb3e0f8701 ("net: dsa: Add
> support for learning FDB through notification"); a deletion never was.

[ ... ]

> Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de>

[Severity: Low]
This isn't a bug, but should this have a Fixes: tag?

The message names the origin commit in the text. It also describes a
defect users can see in "bridge fdb show" and
"bridge fdb flush ... offloaded". The prerequisite is sent as
[PATCH net]. However, the only trailer is Signed-off-by.

> diff --git a/net/dsa/user.c b/net/dsa/user.c
> index 041f9060c8ef0..fdeb1c68c2f4b 100644
> --- a/net/dsa/user.c
> +++ b/net/dsa/user.c

[ ... ]

> @@ -3628,13 +3629,14 @@ static int dsa_user_netdevice_event(struct notifier_block *nb,
>  }
>  
>  static void
> -dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
> +dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work,
> +		       bool offloaded)
>  {
>  	struct switchdev_notifier_fdb_info info = {};
>  
>  	info.addr = switchdev_work->addr;
>  	info.vid = switchdev_work->vid;
> -	info.offloaded = true;
> +	info.offloaded = offloaded;
>  	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
>  				 switchdev_work->orig_dev, &info.info, NULL);
>  }

[ ... ]

> @@ -3681,8 +3683,13 @@ static void dsa_user_switchdev_event_work(struct work_struct *work)
>  			dev_err(ds->dev,
>  				"port %d failed to delete %pM vid %d from fdb: %d\n",
>  				dp->index, addr, vid, err);
> +			break;
>  		}
> -
> +		/* A deletion replayed for this port alone, as it leaves the
> +		 * bridge or a LAG, can leave the entry held for other ports
> +		 */
> +		if (!switchdev_work->replayed)
> +			dsa_fdb_offload_notify(switchdev_work, false);

[Severity: High]
Can this call use a freed net_device?

Before this patch, the DEL branch only used switchdev_work->dev.
dsa_flush_workqueue() in dsa_port_pre_bridge_leave() keeps that device
alive.

The DEL branch now calls dsa_fdb_offload_notify(), which passes
switchdev_work->orig_dev to call_switchdev_notifiers().
dsa_user_fdb_event() stores orig_dev without netdev_hold() or dev_hold():

    switchdev_work->orig_dev = orig_dev;

orig_dev can be a foreign bridge port such as a veth. Suppose that port
has FDB entries DSA offloaded, either static or learned with
ds->assisted_learning_on_cpu_port, and it is then deleted:

ip link del veth0
  br_del_if()
    del_nbp()
      br_fdb_cleanup_by_dst()
        br_switchdev_fdb_notify()     /* SWITCHDEV_FDB_DEL_TO_DEVICE */
          __switchdev_handle_fdb_event_to_device()
            dsa_user_fdb_event()      /* ctx == NULL, replayed == false */
              dsa_schedule_work()
      netdev_upper_dev_unlink()

Nothing flushes dsa_owq when a foreign device unregisters.
dsa_user_netdevice_event() has no NETDEV_UNREGISTER case, and
dsa_flush_workqueue() only runs when DSA ports or LAGs leave the bridge.

So netdev_run_todo() and free_netdev() can free veth0 while these works
are still queued, for example behind slow MDIO or SPI port_fdb_del calls.
The work then runs:

dsa_user_switchdev_event_work()
  dsa_fdb_offload_notify(switchdev_work, false)
    call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, orig_dev, ...)
      br_switchdev_event()
        p = br_port_get_rtnl_rcu(dev);     /* dev already freed */

The existing ADD report has the same lifetime gap, but only through a
narrow race with unregistration. Here, the teardown of orig_dev is what
generates the DEL report.

The note below the '---' line says this depends on "net: dsa: hold the
device a queued FDB event is reported to". That patch is not in this
tree, and git am drops the note.

Could the two go in as a series, or could the dependency be recorded in
the commit message itself? Otherwise this patch could be applied or
backported without it.

[Severity: Low]
Does a return value of 0 here mean the entry has left the switch?

Host addresses on CPU and DSA ports are refcounted, and so are LAG
entries. dsa_port_do_fdb_del() returns 0 when only the refcount drops:

net/dsa/switch.c:dsa_port_do_fdb_del() {
    ...
	if (!refcount_dec_and_test(&a->refcount)) {
		trace_dsa_fdb_del_drop(dp, addr, vid, &db, &a->refcount);
		goto out;
	}

	err = ds->ops->port_fdb_del(ds, port, addr, vid, db);
	...
	if (err) {
		refcount_set(&a->refcount, 1);
		goto out;
	}
    ...
}

Take a host address X added for DSA user ports A and B, giving a refcount
of 2 on the CPU port:

- A's DEL drops the refcount to 1 and reports false.
- B's DEL then fails in port_fdb_del(). The refcount is restored to 1 and
  nothing is reported.

The last report the bridge received is false, but the switch still holds
X.

The same thing can happen if the GFP_ATOMIC allocation in
dsa_user_fdb_event() fails for B. __switchdev_handle_fdb_event_to_device()
stops at the first error other than -EOPNOTSUPP:

	if (err && err != -EOPNOTSUPP)
		return err;

Doesn't that contradict this line in the commit message: "the last one
for an entry gives the state the switch was left in"?

[Severity: Low]
Can this report reach a different bridge than the one the deletion was
for?

The report goes to the orig_dev captured at queue time. However,
br_switchdev_event() finds the bridge from orig_dev's current bridge port
when the work runs. br_fdb_offloaded_set() also ignores p and matches
only addr and vid:

net/bridge/br_fdb.c:br_fdb_offloaded_set() {
    ...
	fdb = br_fdb_find(br, addr, vid);
	if (fdb && offloaded != test_bit(BR_FDB_OFFLOADED, &fdb->flags))
		change_bit(BR_FDB_OFFLOADED, &fdb->flags);
    ...
}

Suppose foreign port V leaves bridge B1, so del_nbp() queues DSA DEL
works with orig_dev = V. V then joins bridge B2 before dsa_owq runs those
works.

Would the report then clear BR_FDB_OFFLOADED on B2's (addr, vid) entry?
One example is V's own local address added again in B2, even if another
switchdev driver in B2 offloaded it.

The commit message covers the same-bridge case ("a deletion still queued
here can clear the flag that driver set on an entry re-created on one of
its ports"). It does not cover this cross-bridge case.

br_fdb_cleanup_by_dst() keeps ext-learned entries only while
BR_FDB_OFFLOADED is clear:

			if (test_bit(BR_FDB_STATIC, &f->flags) ||
			    (test_bit(BR_FDB_ADDED_BY_EXT_LEARN, &f->flags) &&
			     !test_bit(BR_FDB_OFFLOADED, &f->flags))) {

So a wrongly cleared flag in either case also changes which entries those
flushes delete.

>  		break;
>  	}
>  

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122174434.975426-1-julius%40bairaktaris.de

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-23 17:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 17:44 [PATCH net-next] net: dsa: report a deleted FDB entry as no longer offloaded Julius Bairaktaris
2026-11-23 17:44 ` netdev-bot+sashiko

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