mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v2] usbnet: don't lose rx mode updates that arrive during set_rx_mode()
@ 2026-12-03 11:13 Mahdi Yari
  0 siblings, 0 replies; only message in thread
From: Mahdi Yari @ 2026-12-03 11:13 UTC (permalink / raw)
  To: andrew+netdev, davem, edumazet, kuba, pabeni
  Cc: oneukum, netdev, linux-usb, linux-kernel, Mahdi Yari, stable

usbnet_deferred_kevent() tests EVENT_SET_RX_MODE, calls the
minidriver's set_rx_mode() and only then clears the bit. set_rx_mode()
may sleep in a control transfer. If the rx mode changes meanwhile,
usbnet_set_rx_mode() sets the bit and requeues the kevent, but the
trailing clear_bit() wipes that request, so the rerun finds the bit
clear and the new filter is never sent.

This was observed on a machine in real use: an OpenWrt router whose LAN
port, an ASIX AX88179A (0b95:1790) on cdc_ncm, is bridged with a WiFi
AP. After boot or a USB replug, WiFi clients could not reach hosts
behind the USB port. The bridge enables allmulti and promisc while the
first SET_ETHERNET_PACKET_FILTER is in flight, and usbmon shows that
request as the only one, without promisc:

  S Co:1:009:0 s 21 43 000e 0000 0000 0

so the adapter keeps dropping unicast for other MACs. Setting promisc
by hand afterwards (ip link set eth1 promisc on) sends 000f and fixes
it. The cause was found by reading usbmon traces and the code, with
the help of an AI coding assistant (Claude), which also helped write
and test this patch.

Reproducing it needs no kernel changes: add the cdc_ncm port to a
bridge, replug the adapter (or reload the usbnet modules) and watch
usbmon for "s 21 43" requests. The race does not hit every time.

Clear the bit with test_and_clear_bit() before calling set_rx_mode(),
so a request made while it runs is handled by the requeued kevent.
No retry is lost: the bit was already cleared whether or not
set_rx_mode() succeeded.

test_and_clear_bit() is fully ordered, but the set_bit() in
usbnet_defer_kevent() is not. On weakly ordered CPUs a kevent that is
already running could see the bit before the rx mode change behind it,
such as a new multicast address, clear the bit and send the old
filter, and the requeued kevent would then skip the update. Add
smp_mb__before_atomic() in usbnet_set_rx_mode() to pair with
test_and_clear_bit(). It goes there rather than in
usbnet_defer_kevent() so the other events are unchanged.
usbnet_defer_kevent() starts with the set_bit(), so no access sits
between the barrier and the atomic.

Tested with that reproducer on the same hardware (Intel Atom x5-Z8350,
AX88179A on cdc_ncm) running OpenWrt 25.12.5, kernel 6.12.94, with
this change backported. In runs where the race hit, the unpatched
kernel never sent promisc (4 of 4 module reloads) and bridged traffic
failed. With the fix the requeued kevent sent it (3 of 3 reloads, 2 of
2 replugs) and bridged traffic worked. Those runs were without the
barrier, which compiles to nothing on x86. Not tested on a
weakly ordered CPU, not run-tested on net and not tested with kernel
selftests. On net, usbnet.o builds with W=1 for allmodconfig and
allyesconfig without new warnings.

Fixes: 1efed2d06c70 ("usbnet: add a callback for set_rx_mode")
Cc: stable@vger.kernel.org
Signed-off-by: Mahdi Yari <m.yary16@gmail.com>
---
v2:
- Add smp_mb__before_atomic() in usbnet_set_rx_mode() so the fix also
  holds on weakly ordered CPUs (Sashiko review). herd7 with the LKMM
  allows the lost update without the barrier and forbids it with it.
- Dropped Oliver's Acked-by because of that code change.
v1: https://lore.kernel.org/netdev/20261202060513.660741-1-m.yary16@gmail.com/

Note: in net-next, commit 7d573278f9a5 ("ax88179_178a: Add HW support
for AX179A-based chips") moves the AX88179A off cdc_ncm. The race
still affects every usbnet minidriver using set_rx_mode: cdc_ether,
cdc_ncm and int51x1.

 drivers/net/usb/usbnet.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/net/usb/usbnet.c b/drivers/net/usb/usbnet.c
index 97a30dd22..5f39e54cc 100644
--- a/drivers/net/usb/usbnet.c
+++ b/drivers/net/usb/usbnet.c
@@ -1176,6 +1176,10 @@ void usbnet_set_rx_mode(struct net_device *net)
 {
 	struct usbnet		*dev = netdev_priv(net);
 
+	/* order the rx mode change before the bit, pairs with
+	 * test_and_clear_bit() in usbnet_deferred_kevent()
+	 */
+	smp_mb__before_atomic();
 	usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
 }
 EXPORT_SYMBOL_GPL(usbnet_set_rx_mode);
@@ -1184,8 +1188,6 @@ static void __handle_set_rx_mode(struct usbnet *dev)
 {
 	if (dev->driver_info->set_rx_mode)
 		(dev->driver_info->set_rx_mode)(dev);
-
-	clear_bit(EVENT_SET_RX_MODE, &dev->flags);
 }
 
 /* work that cannot be done in interrupt context uses keventd.
@@ -1295,7 +1297,7 @@ usbnet_deferred_kevent(struct work_struct *work)
 	if (test_bit(EVENT_LINK_CHANGE, &dev->flags))
 		__handle_link_change(dev);
 
-	if (test_bit(EVENT_SET_RX_MODE, &dev->flags))
+	if (test_and_clear_bit(EVENT_SET_RX_MODE, &dev->flags))
 		__handle_set_rx_mode(dev);
 
 

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

only message in thread, other threads:[~2026-12-03 11:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03 11:13 [PATCH net v2] usbnet: don't lose rx mode updates that arrive during set_rx_mode() Mahdi Yari

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®