casaroli opened a new pull request, #22424:
URL: https://github.com/apache/nuttx/pull/22424

   ## Summary
   
   `ifdown` of a network device that uses `netdev_upperhalf` can deadlock with 
the worker of the device.
   
   `SIOCSIFFLAGS` holds the device lock (`d_lock`) while `netdev_ifdown()` runs 
(`net/netdev/netdev_ioctl.c`). `netdev_upper_ifdown()` then waits for the 
worker with `work_cancel_sync()` (or for the RX threads). The worker also takes 
`d_lock` (`netdev_upper_txavail_work()`, `netdev_upper_rxpoll_work()`). A send 
on another thread during the `ifdown` cannot get `d_lock` in 
`netdev_upper_txavail()`, so it queues the work, and the worker starts and 
waits for `d_lock`. Then the `ifdown` waits for the worker, and the worker 
waits for the `ifdown`. On a work queue with one thread (HPWORK), all other 
work of that queue stops too.
   
   The fix stops the worker the way Linux does: mark the device as going down 
under `d_lock`, release `d_lock` while it waits for the worker, take `d_lock` 
again, then call the `ifdown` of the lower half. The worker does no `receive()` 
and no transmit while the flag is set. `netdev_upper_ifup()` clears the flag 
after the `ifup` of the lower half. The commit message has the details and the 
cases (a second `ifdown` during the wait gets `-EBUSY`; a failed `ifup` or 
`ifdown` restores the worker).
   
   ## Impact
   
   `drivers/net/netdev_upperhalf.c` only. Devices with `NETDEV_RX_WORK`, 
`NETDEV_RX_THREAD` and `NETDEV_RX_THREAD_RSS`; `NETDEV_RX_DIRECT` has no worker 
and does not change. No API, ABI or Kconfig change. The same code in flat, 
protected and kernel builds and on all architectures.
   
   ## Testing
   
   `qemu-armv8a:netnsh_smp` (flat, 4 CPUs, QEMU TCG, virtio-net, 
`CONFIG_NET_UDP_WRITE_BUFFERS=y`): a test program takes `eth0` down and up in a 
loop while three threads send (two UDP sockets, one packet socket). The window 
is short, so a test-only change (not in this PR) sleeps 20 ms at the start of 
`netdev_upper_ifdown()`, with `d_lock` held.
   
   - Before: the first `ifdown` does not return; nsh does not respond; all CPUs 
are idle.
   - After: 1000 rounds; nsh responds.
   - Without the test-only sleep: 3000 rounds before and after (the race did 
not occur).
   
   Builds with `-Werror` (CI image): `qemu-armv8a:netnsh_smp` (flat), 
`qemu-armv8a:knsh` with networking and virtio-net added (kernel), 
`rv-virt:netnsh64` (flat), `rv-virt:knetnsh64` (kernel), each with packet 
sockets, netlink route and netfilter, and the DNS client enabled. No new 
warnings (the only warning is the RWX linker warning of `rv-virt:netnsh64`, 
also on master). checkpatch (`-c -u -m -g`) passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

  • [PR] drivers/net: Wait for th... casaroli (via GitHub) via commits

Reply via email to