When unregister_netdevice_many_notify() sets NETREG_UNREGISTERING and
calls dev_xdp_uninstall(), a driver that shrinks real_num_rx_queues
(such as virtio_net) hits netif_set_real_num_rx_queues(). Because it
only checks NETREG_REGISTERED, it skips net_rx_queue_update_kobjects()
while still lowering dev->real_num_rx_queues. remove_queue_kobjects()
then leaks the extra RX queue kobjects and their netdev references,
hanging unregister with "waiting for eth0 to become free. Usage count
= 2".
Update RX queue kobjects under NETREG_UNREGISTERING too, matching
netif_set_real_num_tx_queues() from
commit 5c56580b74e5 ("net: Adjust TX queue kobjects if number of queues
changes during unregister").
Like netdev_queue_update_kobjects(), warn in
net_rx_queue_update_kobjects() if new_num > old_num during
NETREG_UNREGISTERING. Also return -EINVAL there: once
remove_queue_kobjects() has released dev->queues_kset and set
real_num_rx_queues to 0, any later call would add kobjects to the
released kset and take netdev references that are never dropped.
No in-tree caller is known to grow RX queues during unregister.
Found and verified by unbinding virtio_net with XDP attached in an
x86_64 KVM guest (KASAN + NET_DEV_REFCNT_TRACKER).
Fixes: bd0b2e7fe611 ("net: xdp: make the stack take care of the tear down")
Signed-off-by: Haishuang Yan <yanh…@cmss.chinamobile.com>
---
v2:
- warn and return -EINVAL in net_rx_queue_update_kobjects() if
new_num > old_num during NETREG_UNREGISTERING (Sashiko review)
- add missing BPF/XDP maintainers to Cc (cc_maintainers)
v1: https://lore.kernel.org/netdev/2026…@cmss.chinamobile.com/
The root cause was analyzed with the help of an AI coding assistant.
Testing on x86_64 KVM (net e1055cbfd6eb, KASAN + NET_DEV_REFCNT_TRACKER;
net/core/dev.c and net-sysfs.c are unchanged up to ef3e21c61baa):
- before: unbinding virtio_net with XDP hangs ("Usage count = 2");
- v2: unbind with XDP, without XDP, and with an AF_XDP socket on the XDP
queue all succeed with no KASAN, WARN or ref_tracker reports;
- W=1 net/core/{dev,net-sysfs}.o clean on x86_64 and arm64;
- not tested: the new grow-during-unregister guard itself (no in-tree
caller reaches it), other drivers, CONFIG_SYSFS=n.
net/core/dev.c | 3 ++-
net/core/net-sysfs.c | 8 ++++++++
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/net/core/dev.c b/net/core/dev.c
index cd0796a1dbfdf..09047ae78da76 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -3341,7 +3341,8 @@ int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
if (rxq < 1 || rxq > dev->num_rx_queues)
return -EINVAL;
- if (dev->reg_state == NETREG_REGISTERED) {
+ if (dev->reg_state == NETREG_REGISTERED ||
+ dev->reg_state == NETREG_UNREGISTERING) {
netdev_assert_locked_ops_compat(dev);
rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index 5c0402389c6d1..69e9966069b1f 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -1328,6 +1328,14 @@ net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num)
int i;
int error = 0;
+ /* Rx queue kobjects are allowed to be updated when a device is being
+ * unregistered, but solely to remove queues. Any path adding queues
+ * should be fixed.
+ */
+ if (WARN(dev->reg_state == NETREG_UNREGISTERING && new_num > old_num,
+ "New queues can't be registered after device unregistration."))
+ return -EINVAL;
+
#ifndef CONFIG_RPS
if (!dev->sysfs_rx_queue_group)
return 0;
base-commit: ef3e21c61baa74e39a49b75318bd4541562ec43f
--
2.43.0