Re: [PATCH net] usbnet: don't lose rx mode updates that arrive during set_rx_mode()

From: netdev-bot+sashiko

Date: Thu Dec 03 2026 - 01:08:37 EST


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