Netdev List
 help / color / mirror / Atom feed
* [PATCH net] usbnet: don't lose rx mode updates that arrive during set_rx_mode()
@ 2026-12-02  6:05 Mahdi Yari
  2026-12-02  9:14 ` Oliver Neukum
  2026-12-03  6:08 ` netdev-bot+sashiko
  0 siblings, 2 replies; 4+ messages in thread
From: Mahdi Yari @ 2026-12-02  6:05 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.
test_and_clear_bit() is fully ordered, so set_rx_mode() reads the
netdev flags only after the bit is cleared. No retry is lost: the bit
was already cleared whether or not set_rx_mode() succeeded.

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. 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>
---
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 | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/net/usb/usbnet.c b/drivers/net/usb/usbnet.c
index 97a30dd..74c100c 100644
--- a/drivers/net/usb/usbnet.c
+++ b/drivers/net/usb/usbnet.c
@@ -1184,8 +1184,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 +1293,7 @@ skip_reset:
 	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 related	[flat|nested] 4+ messages in thread

* Re: [PATCH net] usbnet: don't lose rx mode updates that arrive during set_rx_mode()
  2026-12-02  6:05 [PATCH net] usbnet: don't lose rx mode updates that arrive during set_rx_mode() Mahdi Yari
@ 2026-12-02  9:14 ` Oliver Neukum
  2026-12-03  6:08 ` netdev-bot+sashiko
  1 sibling, 0 replies; 4+ messages in thread
From: Oliver Neukum @ 2026-12-02  9:14 UTC (permalink / raw)
  To: Mahdi Yari, andrew+netdev, davem, edumazet, kuba, pabeni
  Cc: netdev, linux-usb, linux-kernel, stable



On 02.12.26 07:05, Mahdi Yari wrote:
> 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.
> test_and_clear_bit() is fully ordered, so set_rx_mode() reads the
> netdev flags only after the bit is cleared. No retry is lost: the bit
> was already cleared whether or not set_rx_mode() succeeded.
> 
> 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. 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>Acked-by: Oliver Neukum <oneukum@suse.com>

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

* Re: [PATCH net] usbnet: don't lose rx mode updates that arrive during set_rx_mode()
  2026-12-02  6:05 [PATCH net] usbnet: don't lose rx mode updates that arrive during set_rx_mode() Mahdi Yari
  2026-12-02  9:14 ` Oliver Neukum
@ 2026-12-03  6:08 ` netdev-bot+sashiko
  2026-12-03 11:07   ` Mahdi Yari
  1 sibling, 1 reply; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03  6:08 UTC (permalink / raw)
  To: m.yary16
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, oneukum, netdev,
	linux-usb, linux-kernel, stable

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

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

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).

- [Medium] On weakly ordered CPUs this fix can still lose an rx-mode
  update, because only the reader side of the bit handoff has a barrier.

--

usbnet: don't lose rx mode updates that arrive during set_rx_mode()

This patch changes usbnet_deferred_kevent() to clear EVENT_SET_RX_MODE
with test_and_clear_bit() before it calls the minidriver's set_rx_mode().
Before this, the bit was cleared after set_rx_mode() returned, and that
could wipe a request made while set_rx_mode() was sleeping in a control
transfer.

> diff --git a/drivers/net/usb/usbnet.c b/drivers/net/usb/usbnet.c
> index 97a30dd22c545..74c100c0f0467 100644
> --- a/drivers/net/usb/usbnet.c
> +++ b/drivers/net/usb/usbnet.c

[ ... ]

> @@ -1295,7 +1293,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);

[Severity: Medium]

The commit message says:

  test_and_clear_bit() is fully ordered, so set_rx_mode() reads the
  netdev flags only after the bit is cleared.

Is a barrier on the reader side enough on weakly ordered architectures?
It seems to need a matching barrier on the writer side. The writer,
usbnet_defer_kevent(), sets the bit with a plain set_bit():

drivers/net/usb/usbnet.c:usbnet_defer_kevent() {
	set_bit (work, &dev->flags);
	if (!usbnet_going_away(dev)) {
		if (!schedule_work(&dev->kevent))
	...
}

Nothing orders the earlier store to the state that set_rx_mode() reads
before this set_bit(). The dev_mc_add() path is one example:

dev_mc_add()
  __dev_mc_add()
    __hw_addr_add_ex()
      list_add_tail_rcu(&ha->list, &list->list);
      __hw_addr_count_inc(list);
    __dev_set_rx_mode()
      usbnet_set_rx_mode()
        usbnet_defer_kevent()
          set_bit(EVENT_SET_RX_MODE, &dev->flags);

All of this runs under netif_addr_lock_bh(). A lock acquire does not
order the count store before the later set_bit().

On the reader side, usbnet_cdc_update_filter() reads the state without
the addr lock:

drivers/net/usb/cdc_ether.c:usbnet_cdc_update_filter() {
	...
	if (net->flags & IFF_PROMISC)
		cdc_filter |= USB_CDC_PACKET_TYPE_PROMISCUOUS;
	if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI))
		cdc_filter |= USB_CDC_PACKET_TYPE_ALL_MULTICAST;
	...
}

Suppose the kevent is already running, so PENDING has been cleared.
Could this sequence happen on arm64 or POWER?

CPU0                               CPU1 (kevent running)
__hw_addr_count_inc(list)
set_bit(EVENT_SET_RX_MODE)
  (visible before count store)
                                   test_and_clear_bit(EVENT_SET_RX_MODE)
                                     sees and clears the bit
                                   usbnet_cdc_update_filter()
                                     netdev_mc_empty() sees old count
schedule_work() succeeds,
kevent requeued
                                   rerun: EVENT_SET_RX_MODE is clear,
                                   __handle_set_rx_mode() is skipped

If so, the SET_ETHERNET_PACKET_FILTER request with ALL_MULTICAST for the
first multicast address would never be sent. That is the same lost update
this patch is meant to fix, and it would affect cdc_ether, cdc_ncm and
int51x1.

If schedule_work() fails because the work is still pending, ordering
holds. The fully ordered test_and_set_bit() on PENDING in queue_work()
pairs with the worker's barrier, so the gap seems limited to the case
where the kevent is already running. This can't happen on x86.

The IFF_PROMISC and IFF_ALLMULTI paths from the bridge scenario call
netdev_info() in __dev_set_promiscuity() and __dev_set_allmulti(). That
probably adds a full barrier as a side effect, so the mc list path looks
like the main remaining case.

Would adding smp_mb__before_atomic() before the set_bit() in
usbnet_defer_kevent() (or in usbnet_set_rx_mode()) pair with the barrier
in test_and_clear_bit() and close this gap?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202060513.660741-1-m.yary16%40gmail.com

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

* Re: [PATCH net] usbnet: don't lose rx mode updates that arrive during set_rx_mode()
  2026-12-03  6:08 ` netdev-bot+sashiko
@ 2026-12-03 11:07   ` Mahdi Yari
  0 siblings, 0 replies; 4+ messages in thread
From: Mahdi Yari @ 2026-12-03 11:07 UTC (permalink / raw)
  To: netdev
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, oneukum, linux-usb,
	linux-kernel, Mahdi Yari

> - [Medium] On weakly ordered CPUs this fix can still lose an rx-mode
>   update, because only the reader side of the bit handoff has a barrier.

Agreed. The set_bit() in usbnet_defer_kevent() is unordered, so on
arm64 or POWER a kevent that is already running can see the bit before
the mc list change, clear it and send the old filter, and the requeued
kevent then skips. x86 can't hit this, which is why my tests didn't.

v2 will add smp_mb__before_atomic() in usbnet_set_rx_mode() to pair with
the test_and_clear_bit(). I'm putting it there rather than in
usbnet_defer_kevent() so the other events are unchanged. herd7 with the
LKMM allows the lost update without the barrier and forbids it with it.

pw-bot: cr

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

end of thread, other threads:[~2026-12-03 11:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  6:05 [PATCH net] usbnet: don't lose rx mode updates that arrive during set_rx_mode() Mahdi Yari
2026-12-02  9:14 ` Oliver Neukum
2026-12-03  6:08 ` netdev-bot+sashiko
2026-12-03 11:07   ` Mahdi Yari

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