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=1796425836; b=VRTse3h1CCeefwM0++oplSteuP7lwteXVxS+RUgIBWtim6tY7XbRybR04aliCigu3L6/ +ALWQx6GPrNFGjuwwJqPBUjpUA3JQKKVbhaVCmbQd6Wc+bJw9WfwkWU87NPw7cQEPDvm0 jHGrZz2Y8plktv44XsX2+xiNdf4i9CZ1SOv0xb9AfESRsINJq3T/h62915R7CT2dgZd65 TZmpjUs2k2GayqctGkl4rEIZJogMZXIFrFzAAmZXYH86o8Vsp4K1er1rW9OtbO32myr9a hq/6QUOT26e9vVmT6Twtd/HGO5eGnEAV/4Gkml4dW2Q1Lx8aAxidubc41x6viwk8+HA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796425836; 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=v+fPT2ISJzs3hx4SGuo2HK2sUhcDgfHMeJf3isBf2MU=; b=dz4foy8dFlJmV1Dc8es/Xo9waJRRcH0BPmdP3G4/72qPt+c3RzyhIS0/LXS9wzGhXHpm JHa3PxvSF7BKeqHF7649xPv96xrPp0kqBkTMv96J8vSAo+J3RqUXxZs35/kijyL2gQFeg 9EUT05YOM19TjnBivUdavLjQukxlyMLh+/vMxF/yRBfPtsYx8+Y2JCjKrs7rU5WZj0zfC hbQ4OE92DYMMyoqSdbbujzlGdOYyNYdJyRvHcCge/8Tq9108RyX0hzvLszJUXp30/kOR9 CWi63+BIFVDmqiIIC5U8VuBL++3r3RP3EFxuJj3+obpihxQeK0GiTpud3KkOyF4hcHA== 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="d/iS7y7P"; 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 79F05606A689 for ; Fri, 4 Dec 2026 23:10:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 79F05606A689 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=d/iS7y7P Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-3875029077eso4515719eec.0 for ; Fri, 04 Dec 2026 15:10:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796425836; x=1797030636; 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=v+fPT2ISJzs3hx4SGuo2HK2sUhcDgfHMeJf3isBf2MU=; b=d/iS7y7PEjbcxwGM4HwKjX34kj4iIPDSyiMW5LsAMKdQM9E/J/vslNseXzhq1hH0Od LCZBaICHSOtTxFI7YDVysCoh/pW+UkgEJWEj4iimlJcXajy7tAU3nqIhVUr5l++GKZxu VOVCR21xIkoxIunmaoH+l5Btsx7GRO9dF5uDUky7xh397RSae1n9pYJvwwqnxhuAH9A8 oK2ubDSSAkKHjgomwYFMMGIdhD56YzKlrxBrDzC9SrCqa2VVYsT8VXv5jEMS2Qyu2tPf PDqedSuwaRWJR0hLN5eJX8YcXpqNpcypXunlsCwUT7es5Nct9iN0YiiGfgYfSZJKcy/o ha9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796425836; x=1797030636; 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=v+fPT2ISJzs3hx4SGuo2HK2sUhcDgfHMeJf3isBf2MU=; b=iaJGkazen2C60Ur+oYYmYbPDm2IvfNP7i2zX0M+RvUVxHl2Xhlj2porjtFNH8iwp5C XrQQwH/gdWsh0dqiBLQq4ZMQz6CxrJvTQWKQTFZYVqB7D2JhAqO7SqTXCn9I/jsfuJEU hDtwV1Lrv+mA322Lbp8cgepuEUiooIYaHKXBbg/1a0I1nBob28w7SQ7QfMeZNoVJuVTN lRMOgkdvZA35YJZyZykeL6Jf+XLGzgOahFpkYopwO6Dg8HgGNYG/2sQnyt78o8M+qdmp bUWhQSeaNxwMoXdYv80JiChVa0S8fwU/S4FxYfC+/zBXhM9feYevgps+TYduoqjBMCVe Ddig== X-Gm-Message-State: AB4q+uG9fUoqMn39e+abWIYyfdBdbcLGW4rJ+PuoYM6uqNohDGpLEdvt 6QC3qkIQMvMMWo3y0iy0pqj1cHqXIulAhWJlPq+m30Z0ea3H4md/v5bG80BzHE+EM/xVaO17DYz wzApe2xxU7yoi5hyaamSrno0W5FP/ulmQgkWGKbU+SHrzSqIIcmh/jKdU4Hj/75D1ZNNR5p6fzg 4HU9Xz4FITVKO6vfOc784akjd7yy8Cl6b/RKLuGyTvx25ie0LArdqTwRFjkJTPdJQ= X-Received: from dychz24.prod.google.com ([2002:a05:7301:8618:b0:36e:f67b:23ab]) (user=almasrymina job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:394a:b0:387:42b9:913e with SMTP id 5a478bee46e88-38752173a35mr4192900eec.31.1796425835117; Fri, 04 Dec 2026 15:10:35 -0800 (PST) Date: Fri, 4 Dec 2026 23:10:28 +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-5-almasrymina@google.com> Subject: [PATCH net-next v5 4/6] net: devmem: synchronize and unmap all device bindings on 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" net_devmem_dev_unregister() currently has three issues when tearing down devmem bindings: 1. For TX bindings, it clears NET_DEVMEM_BINDING_LIVE and immediately calls net_devmem_dmabuf_binding_unmap() without waiting for an RCU grace period, racing with in-flight net_devmem_get_binding() lookups (both direct on dev and leased via binding->vdev). 2. For RX bindings, it skips bindings that still have NET_DEVMEM_BINDING_LIVE set. If an RX queue fails to close while userspace still holds the netlink socket open and defers unmapping, net_devmem_dev_unregister() would skip it too, leaving the dmabuf mapped past physical device unregistration. 3. It checks !netif_is_queue_leasee(dev), which is now obsolete since binding->dev always points to the physical device for both RX and TX bindings. Split net_devmem_dev_unregister() into two passes separated by a single synchronize_net(): - First pass clears binding->vdev for any binding referencing dev (both when unregistering a virtual device and when unregistering the physical device) and clears NET_DEVMEM_BINDING_LIVE for all bindings with binding->dev == dev. - If any bindings match binding->dev == dev, call synchronize_net() once to wait for in-flight net_devmem_get_binding() RCU readers to finish. - Second pass unmaps every binding with binding->dev == dev (both TX and any deferred-unmap RX bindings, regardless of whether LIVE was set) and clears binding->dev to NULL. These issues were identified by code inspection while extending devmem unregister teardown to leased queues; 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: - Split out net_devmem_dev_unregister() cleanup into its own patch. - Add single synchronize_net() between clearing NET_DEVMEM_BINDING_LIVE and unmapping to close the TX net_devmem_get_binding() race (Paolo). - Unmap all remaining bindings with binding->dev == dev (including live deferred-unmap RX bindings) and drop the now-obsolete !netif_is_queue_leasee(dev) check. --- net/core/devmem.c | 36 ++++++++++++++++++++++-------------- net/core/devmem.h | 5 ++--- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/net/core/devmem.c b/net/core/devmem.c index 3a894ff06b32b..a395f100d2f33 100644 --- a/net/core/devmem.c +++ b/net/core/devmem.c @@ -246,6 +246,7 @@ void net_devmem_dev_unregister(struct net_device *dev) { struct net_devmem_dmabuf_binding *binding; unsigned long xa_idx; + bool unmap = false; netdev_assert_locked(dev); @@ -254,26 +255,33 @@ void net_devmem_dev_unregister(struct net_device *dev) xa_lock(&net_devmem_dmabuf_bindings); xa_for_each(&net_devmem_dmabuf_bindings, xa_idx, binding) { - if (binding->direction == DMA_FROM_DEVICE) { - if (READ_ONCE(binding->dev) != dev || - xa_get_mark(&net_devmem_dmabuf_bindings, xa_idx, - NET_DEVMEM_BINDING_LIVE)) - continue; - } else { - if (READ_ONCE(binding->vdev) == dev) - WRITE_ONCE(binding->vdev, NULL); - if (READ_ONCE(binding->dev) != dev) - continue; + if (READ_ONCE(binding->vdev) == dev) + WRITE_ONCE(binding->vdev, NULL); + if (READ_ONCE(binding->dev) == dev) { WRITE_ONCE(binding->vdev, NULL); __xa_clear_mark(&net_devmem_dmabuf_bindings, xa_idx, NET_DEVMEM_BINDING_LIVE); + unmap = true; } + } + xa_unlock(&net_devmem_dmabuf_bindings); + + if (!unmap) + return; + + /* Ensure no tx net_devmem_get_binding() are in flight after the + * mark is cleared. + */ + synchronize_net(); - xa_unlock(&net_devmem_dmabuf_bindings); - if (!netif_is_queue_leasee(dev)) + xa_lock(&net_devmem_dmabuf_bindings); + xa_for_each(&net_devmem_dmabuf_bindings, xa_idx, binding) { + if (READ_ONCE(binding->dev) == dev) { + xa_unlock(&net_devmem_dmabuf_bindings); net_devmem_dmabuf_binding_unmap(binding); - xa_lock(&net_devmem_dmabuf_bindings); - WRITE_ONCE(binding->dev, NULL); + xa_lock(&net_devmem_dmabuf_bindings); + WRITE_ONCE(binding->dev, NULL); + } } xa_unlock(&net_devmem_dmabuf_bindings); } diff --git a/net/core/devmem.h b/net/core/devmem.h index d3a5a482d99bf..4036aab286c4d 100644 --- a/net/core/devmem.h +++ b/net/core/devmem.h @@ -44,9 +44,8 @@ struct net_devmem_dmabuf_binding { * The binding undos itself and unmaps the underlying dmabuf once all * those refs are dropped and the binding is no longer desired or in * use. RX bindings are unmapped earlier when their queues are closed or - * uninstalled, unless a leased or busy queue may still use the binding. - * TX bindings are also unmapped earlier if their device is - * unregistered. + * uninstalled, unless a busy queue may still use the binding. Bindings + * are also unmapped earlier if their device is unregistered. * * net_devmem_net_iov_inc_ref() on dmabuf net_iovs will increment this * reference, making sure that the binding remains alive until all the -- 2.56.1.915.g69d5ec7298-goog