Netdev List
 help / color / mirror / Atom feed
From: Mina Almasry <almasrymina@google.com>
To: netdev@vger.kernel.org
Cc: Mina Almasry <almasrymina@google.com>,
	"David S. Miller" <davem@davemloft.net>,
	 Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	 Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>, Shuah Khan <shuah@kernel.org>,
	 linux-kselftest@vger.kernel.org,
	Stanislav Fomichev <sdf.kernel@gmail.com>,
	 Bobby Eshleman <bobbyeshleman@meta.com>,
	Nikolay Aleksandrov <razor@blackwall.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	David Wei <dw@davidwei.uk>,
	 Pavel Begunkov <asml.silence@gmail.com>,
	Jens Axboe <axboe@kernel.dk>,  Weiming Shi <bestswngs@gmail.com>,
	io-uring@vger.kernel.org,
	 Jesper Dangaard Brouer <hawk@kernel.org>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>
Subject: [PATCH net-next v5 1/6] net: guard leased queue ops against physical netdev unregister
Date: Fri,  4 Dec 2026 23:10:25 +0000	[thread overview]
Message-ID: <20261204231030.3815575-2-almasrymina@google.com> (raw)
In-Reply-To: <20261204231030.3815575-1-almasrymina@google.com>

When a virtual device (e.g. netkit) leases an RX queue from a physical
device, netdev_rx_queue_lease() holds a reference on the physical device
until netkit's ndo_uninit() runs. During unregister_netdevice_many_notify()
on the physical device, operations traversing the lease from the virtual
device can race with teardown after Loop 1 marks the physical device
NETREG_UNREGISTERING:

  CPU0 (unregister phys_dev)          CPU1 (leasee path)
  --------------------------          ------------------
  dev_close(phys_dev)
  reg_state = NETREG_UNREGISTERING
                                      netdev_lock(virt_dev)
                                      netdev_lock(phys_dev)
                                        -> binds/opens/queries dying dev
                                        -> or reconfigs closed phys_dev
  dev_memory_provider_uninstall(phys)
  NETDEV_UNREGISTER(phys)
  cascaded netkit_uninit() -> unlease

In addition, in a same-batch unregister_netdevice_many([netkit, phys_dev]),
Loop 1 sets NETREG_UNREGISTERING on both devices and netkit's ndo_uninit()
-> netif_rxq_cleanup_unlease() can call __netif_mp_close_rxq(phys_dev)
before phys_dev's dev_memory_provider_uninstall() runs, attempting queue
reconfiguration on an unregistering device.

Add netdev_lock_if_alive() to atomically lock a net_device only if it has
not entered unregistration, and use it in netif_mp_open_rxq(),
netdev_queue_get_dma_dev(), and netdev_nl_bind_tx_doit() (also re-checking
netif_device_present() under the physical device's lock).

In __netif_mp_close_rxq(), when !netif_is_alive(dev) and mp_params is
still installed, validate and clear rxq->mp_params directly without
invoking netdev_rx_queue_reconfig(), since the device has already been
closed.

These races were identified by code inspection while adding leased-queue
unregister support for devmem; they have not been observed in production
or tested on real hardware. Verified with netdevsim and netkit selftests
in nl_netdev.py and nk_qlease.py.

Signed-off-by: Mina Almasry <almasrymina@google.com>
---
v4 -> v5:
- Replace netif_lease_valid() with netdev_lock_if_alive() and guard
  netdev_queue_get_dma_dev(), netif_mp_open_rxq(), and
  netdev_nl_bind_tx_doit() (Paolo).

---
 include/net/netdev_lock.h  |  9 +++++++++
 net/core/netdev-genl.c     | 14 ++++++++++++--
 net/core/netdev_queues.c   |  3 ++-
 net/core/netdev_rx_queue.c | 19 ++++++++++++++-----
 4 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/include/net/netdev_lock.h b/include/net/netdev_lock.h
index edd8cc2e2b36e..71e6f26b0d971 100644
--- a/include/net/netdev_lock.h
+++ b/include/net/netdev_lock.h
@@ -12,6 +12,15 @@ static inline bool netdev_trylock(struct net_device *dev)
 	return mutex_trylock(&dev->lock);
 }
 
+static inline bool __must_check netdev_lock_if_alive(struct net_device *dev)
+{
+	netdev_lock(dev);
+	if (netif_is_alive(dev))
+		return true;
+	netdev_unlock(dev);
+	return false;
+}
+
 static inline void netdev_assert_locked(const struct net_device *dev)
 {
 	lockdep_assert_held(&dev->lock);
diff --git a/net/core/netdev-genl.c b/net/core/netdev-genl.c
index d2ccb1090496d..87dac9c5e84c3 100644
--- a/net/core/netdev-genl.c
+++ b/net/core/netdev-genl.c
@@ -1253,8 +1253,18 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info)
 		goto err_unlock_netdev;
 	}
 
-	if (bind_dev != netdev)
-		netdev_lock(bind_dev);
+	if (bind_dev != netdev) {
+		if (!netdev_lock_if_alive(bind_dev)) {
+			err = -ENODEV;
+			NL_SET_ERR_MSG(info->extack,
+				       "Device is being unregistered");
+			goto err_unlock_netdev;
+		}
+		if (!netif_device_present(bind_dev)) {
+			err = -ENODEV;
+			goto err_unlock_bind_dev;
+		}
+	}
 
 	dma_dev = netdev_queue_get_dma_dev(bind_dev, 0, NETDEV_QUEUE_TYPE_TX);
 
diff --git a/net/core/netdev_queues.c b/net/core/netdev_queues.c
index 27cb6397bdd4e..c796957c07f4b 100644
--- a/net/core/netdev_queues.c
+++ b/net/core/netdev_queues.c
@@ -50,7 +50,8 @@ struct device *netdev_queue_get_dma_dev(struct net_device *dev,
 
 	hw_rxq = __netif_get_rx_queue(dev, idx)->lease;
 
-	netdev_lock(hw_rxq->dev);
+	if (!netdev_lock_if_alive(hw_rxq->dev))
+		return NULL;
 	idx = get_netdev_rx_queue_index(hw_rxq);
 	dma_dev = __netdev_queue_get_dma_dev(hw_rxq->dev, idx);
 	netdev_unlock(hw_rxq->dev);
diff --git a/net/core/netdev_rx_queue.c b/net/core/netdev_rx_queue.c
index a7261fedd5e76..e3ae99210caeb 100644
--- a/net/core/netdev_rx_queue.c
+++ b/net/core/netdev_rx_queue.c
@@ -269,7 +269,10 @@ int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
 		return -EOPNOTSUPP;
 	}
 
-	netdev_lock(dev);
+	if (!netdev_lock_if_alive(dev)) {
+		NL_SET_ERR_MSG(extack, "Device is being unregistered");
+		return -ENODEV;
+	}
 	ret = __netif_mp_open_rxq(dev, rxq_idx, p, extack);
 	netdev_unlock(dev);
 	return ret;
@@ -284,17 +287,23 @@ static int __netif_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx,
 
 	rxq = __netif_get_rx_queue(dev, rxq_idx);
 
-	/* Callers holding a netdev ref may get here after we already
-	 * went thru shutdown via dev_memory_provider_uninstall().
+	/* Callers holding a netdev ref may get here after the device entered
+	 * unregistration, either before or after
+	 * dev_memory_provider_uninstall() ran. The device is already closed,
+	 * so clear mp_params without reconfiguring the queue.
 	 */
-	if (dev->reg_state > NETREG_REGISTERED &&
-	    !rxq->mp_params.mp_ops)
+	if (!netif_is_alive(dev) && !rxq->mp_params.mp_ops)
 		return 0;
 
 	if (WARN_ON_ONCE(rxq->mp_params.mp_ops != old_p->mp_ops ||
 			 rxq->mp_params.mp_priv != old_p->mp_priv))
 		return -EINVAL;
 
+	if (!netif_is_alive(dev)) {
+		memset(&rxq->mp_params, 0, sizeof(rxq->mp_params));
+		return 0;
+	}
+
 	netdev_queue_config(dev, rxq_idx, &qcfg[0]);
 	memset(&rxq->mp_params, 0, sizeof(rxq->mp_params));
 	netdev_queue_config(dev, rxq_idx, &qcfg[1]);
-- 
2.56.1.915.g69d5ec7298-goog


  reply	other threads:[~2026-12-04 23:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 23:10 [PATCH net-next v5 0/6] net: devmem: support leased RX and TX queues on netdev unregister Mina Almasry
2026-12-04 23:10 ` Mina Almasry [this message]
2026-12-04 23:10 ` [PATCH net-next v5 2/6] net: devmem: fold net_devmem_lookup_dmabuf() into net_devmem_get_binding() Mina Almasry
2026-12-04 23:10 ` [PATCH net-next v5 3/6] net: devmem: resolve leased RX queues to physical device on bind Mina Almasry
2026-12-04 23:10 ` [PATCH net-next v5 4/6] net: devmem: synchronize and unmap all device bindings on unregister Mina Almasry
2026-12-04 23:10 ` [PATCH net-next v5 5/6] net: propagate busy status on memory provider uninstall Mina Almasry
2026-12-04 23:10 ` [PATCH net-next v5 6/6] selftests: net: add leased devmem RX and TX netdev unregister tests Mina Almasry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204231030.3815575-2-almasrymina@google.com \
    --to=almasrymina@google.com \
    --cc=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=bestswngs@gmail.com \
    --cc=bobbyeshleman@meta.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dw@davidwei.uk \
    --cc=edumazet@kernel.org \
    --cc=hawk@kernel.org \
    --cc=horms@kernel.org \
    --cc=ilias.apalodimas@linaro.org \
    --cc=io-uring@vger.kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=razor@blackwall.org \
    --cc=sdf.kernel@gmail.com \
    --cc=shuah@kernel.org \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox