From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796425833; b=aoSMmkyK14iIDwPoQicNhzcGEmf4WnW2W1g0iYbOXm5Vtzxe3glVHhVLfdzSatRTYIy/ dLuBfv05BR2ODDsHTlzzBq8c/5qilY8Xsb5rRuVh79jXykLTbUe4GVY2SPlmymtvAN37Q Xs6cL99/2ODLVgS9zGtBrT/7UIAN9PGmDd/wP3KUazuUfdvjLa+ekOLN4yhAu25jYCMEX CvNUz39FcpAN+DX9YXZHupxxfUliqMjVBitZCBkPMAKGJsS6AjxYOfOAFjoyOnJvwkDxw r4mur6dAngG9KEJAAPsSrFMxuKmYsvBCZ5hxSFFuAcMgCy4gc5Ywl4OUBInbTXJaaRg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796425833; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Received:Date:In-Reply-To:Mime-Version: References:X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=RzmZPPyu4z3dsCWdUOO45/WFPtIqiUIzd5Ng0IyoQPc=; b=B51MeyWGocc57364DIU4F8rPwbNen1HRylwGZc5Y1yMnGWXtWfFURh56+7LsEnVve0sK MUnMVQwp9eNndJKhYsyKKTRNjDKY5z0SCtR73h/H2fmWNqoA1R8C7KTLCMIbUBtOy/4r/ VSD5cWcEuXDxzFKmNEFGFsHtbFPmQqrepG7bZDCiw4jUAgYm8E7snrRuJS+bjlXfQl+Ft W26tsSFZJXK/8OXOYi3R/Jj4v1lOxO3ZnSSjr602h3QVXhg+s25Tg9SIVUNT1Pqg/IQi9 O4znmt5HKbafV850ZFH6XgLLXdX4qf7yyZYcH6cP2/B2mPGuRLruMPmxX0mbEqWI4sA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=LZPnjWOj; arc=none smtp.remote-ip=74.125.82.200 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42A60606A337 for ; Fri, 4 Dec 2026 23:10:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 42A60606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=LZPnjWOj Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-33c35f5ca6cso2273907eec.1 for ; Fri, 04 Dec 2026 15:10:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796425832; x=1797030632; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=RzmZPPyu4z3dsCWdUOO45/WFPtIqiUIzd5Ng0IyoQPc=; b=LZPnjWOjT5DjGr2GgvFPa7Ul/MLT2VZ8IQFuTN1b5WD2oF0+SsWN42WoCCuYHhlN9b vy++Xnfanejvrj9Ip5qAt9VEY+Nb3jogQBfTbP5aJhEvCYB1Ug5n9Tqp+811K4gowVuU quVItUvs6l6R6KgilJDQ/RCjx+oECVyQpLFV+pUqP4J5NoBYGk8bi+f4RNjvyAvQTMc1 EJ87tTxOo6LSdMSzhCFviR5v3+34+Cg7IkWa4D9WtWVNkM6cUmg6pYAKCvpr9YXVA5lR ImZDp+evt0+nzNsSfnhBO2hijjD5yU1kY379wtCnRwFeOjxo4Rn3aFm5RW1ryF0y9ofN ulbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796425832; x=1797030632; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=RzmZPPyu4z3dsCWdUOO45/WFPtIqiUIzd5Ng0IyoQPc=; b=0/zDJh1Usgp7hciuw7pYEfjBjeagTa5F5Y0iHH6sv+aWUqhaNqeFrflozSOubcdKeh O6qP1YjLRElRtzPP64zX6nwmyCsgBKCWZlGYOra2I5idRADa4UhkFqeO9vDEwVEV1/tV e2WbaOv8cIDe/p6EO8TIdiPE3s5NfwU2uvzv7fjBWwsn9XQjdwl5X8UoBklzocVcpoQj SN9EzrPQTdBq1IZiURkpEGTpnlJIyeHtDKhbS2UqPIu6K50FKXLDnYyOHNM2b2tJ1rwT GjQkS6M7Vyi9i9COsJiKUtsx06peJ2osTb2R6blDd16CKD7hx+KYjllkX0aI7icb2e9V ABOg== X-Gm-Message-State: AB4q+uFQjH0+qG4w3HZ9cas9dCfATSMQjWAolJYeGWCyvdS7XJPEWvw1 oYAMjxXWu8zOpMRiOoN+4aAAe1cZwQUNZ+x5eM7z5dhIaVvhW6Ylx3AwSfSwrNJFtZPbC0VZIQW G+Jjx08ehJXeyIMOgDLow3ocdLU7MWDW83B6B30BiI6s/uH8Wt+wP1gjSzcO3qbt5EnpgVZUHwQ 4ll0EiQdfDK1fHUD1wZUmofhOKWsEMk2WVfjEtMjmomm5/J/nKeNcSMjzMmBympy8= X-Received: from dybnj9.prod.google.com ([2002:a05:7300:d089:b0:386:33c7:a7de]) (user=almasrymina job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:1402:b0:36e:b3e6:ebe7 with SMTP id 5a478bee46e88-387521c9ec2mr3336975eec.26.1796425831904; Fri, 04 Dec 2026 15:10:31 -0800 (PST) Date: Fri, 4 Dec 2026 23:10:25 +0000 In-Reply-To: <20261204231030.3815575-1-almasrymina@google.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261204231030.3815575-1-almasrymina@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261204231030.3815575-2-almasrymina@google.com> Subject: [PATCH net-next v5 1/6] net: guard leased queue ops against physical netdev unregister From: Mina Almasry To: netdev@vger.kernel.org Cc: Mina Almasry , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Simon Horman , Shuah Khan , linux-kselftest@vger.kernel.org, Stanislav Fomichev , Bobby Eshleman , Nikolay Aleksandrov , Daniel Borkmann , David Wei , Pavel Begunkov , Jens Axboe , Weiming Shi , io-uring@vger.kernel.org, Jesper Dangaard Brouer , Ilias Apalodimas Content-Type: text/plain; charset="UTF-8" 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 --- 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