From: Mina Almasry <almasrymina@google.com>
To: netdev@vger.kernel.org
Cc: "Mina Almasry" <almasrymina@google.com>,
"Jesper Dangaard Brouer" <hawk@kernel.org>,
"Ilias Apalodimas" <ilias.apalodimas@linaro.org>,
"David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@kernel.org>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Simon Horman" <horms@kernel.org>,
"Neal Cardwell" <ncardwell@google.com>,
"Kuniyuki Iwashima" <kuniyu@google.com>,
"Pavel Begunkov" <asml.silence@gmail.com>,
"Bobby Eshleman" <bobbyeshleman@meta.com>,
"Stanislav Fomichev" <sdf@fomichev.me>,
linux-kernel@vger.kernel.org,
"Steffen Klassert" <steffen.klassert@secunet.com>,
"Herbert Xu" <herbert@gondor.apana.org.au>,
"Willem de Bruijn" <willemb@google.com>,
"John Fastabend" <john.fastabend@gmail.com>,
"Sabrina Dubroca" <sd@queasysnail.net>,
"Andrew Lunn" <andrew+netdev@lunn.ch>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Jason Wang" <jasowangio@gmail.com>,
"Eugenio Pérez" <eperezma@redhat.com>,
"Xuan Zhuo" <xuanzhuo@linux.alibaba.com>,
"Andrew Morton" <akpm@linux-foundation.org>,
"David Hildenbrand" <david@kernel.org>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Mike Rapoport" <rppt@kernel.org>,
"Suren Baghdasaryan" <surenb@google.com>,
"Michal Hocko" <mhocko@suse.com>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Johannes Berg" <johannes@sipsolutions.net>,
"Byungchul Park" <byungchul@sk.com>, "Zi Yan" <ziy@nvidia.com>,
virtualization@lists.linux.dev, linux-mm@kvack.org,
bpf@vger.kernel.org, linux-wireless@vger.kernel.org,
"Jonathan Corbet" <corbet@lwn.net>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Lorenzo Bianconi" <lorenzo@kernel.org>,
"Wen Gu" <guwen@linux.alibaba.com>,
"Philo Lu" <lulie@linux.alibaba.com>,
"Justin Chen" <justin.chen@broadcom.com>,
"Florian Fainelli" <florian.fainelli@broadcom.com>,
"Vikas Gupta" <vikas.gupta@broadcom.com>,
"Michael Chan" <michael.chan@broadcom.com>,
"Pavan Chebbi" <pavan.chebbi@broadcom.com>,
"Doug Berger" <opendmb@gmail.com>,
"Nicolai Buchwitz" <nb@tipi-net.de>,
"Broadcom internal kernel review list"
<bcm-kernel-feedback-list@broadcom.com>,
"Satish Kharat" <satishkh@cisco.com>,
"Wei Fang" <wei.fang@nxp.com>, "Frank Li" <frank.li@nxp.com>,
"Shenwei Wang" <shenwei.wang@nxp.com>,
"Joshua Washington" <joshwash@google.com>,
"Harshitha Ramamurthy" <hramamurthy@google.com>,
"Jijie Shao" <shaojijie@huawei.com>,
"Jian Shen" <shenjian15@huawei.com>,
"Fan Gong" <gongfan1@huawei.com>,
"Tony Nguyen" <anthony.l.nguyen@intel.com>,
"Przemek Kitszel" <przemyslaw.kitszel@intel.com>,
"Marcin Wojtas" <marcin.s.wojtas@gmail.com>,
"Russell King" <linux@armlinux.org.uk>,
"Sunil Goutham" <sgoutham@marvell.com>,
"Geetha sowjanya" <gakula@marvell.com>,
"Ratheesh Kannoth" <rkannoth@marvell.com>,
"Subbaraya Sundeep" <sbhatta@marvell.com>,
"Bharat Bhushan" <bbhushan2@marvell.com>,
"Felix Fietkau" <nbd@nbd.name>,
"Matthias Brugger" <matthias.bgg@gmail.com>,
"AngeloGioacchino Del Regno"
<angelogioacchino.delregno@collabora.com>,
"Tariq Toukan" <tariqt@nvidia.com>,
"Saeed Mahameed" <saeedm@nvidia.com>,
"Mark Bloch" <mbloch@nvidia.com>,
"Leon Romanovsky" <leon@kernel.org>,
"Ido Schimmel" <idosch@nvidia.com>,
"Petr Machata" <petrm@nvidia.com>,
"Horatiu Vultur" <horatiu.vultur@microchip.com>,
UNGLinuxDriver@microchip.com,
"Daniel Machon" <daniel.machon@microchip.com>,
"Steen Hegelund" <Steen.Hegelund@microchip.com>,
"Haiyang Zhang" <haiyangz@microsoft.com>,
"Wei Liu" <wei.liu@kernel.org>,
"Dexuan Cui" <decui@microsoft.com>,
"Yibo Dong" <dong100@mucse.com>,
"Brett Creeley" <brett.creeley@amd.com>,
"Justin Lai" <justinlai0215@realtek.com>,
"Larry Chiu" <larry.chiu@realtek.com>,
"Niklas Söderlund" <niklas.soderlund@ragnatech.se>,
"Paul Barker" <paul@pbarker.dev>,
"Masahisa Kojima" <kojima.masahisa@socionext.com>,
"Maxime Chevallier" <maxime.chevallier@bootlin.com>,
"Maxime Coquelin" <mcoquelin.stm32@gmail.com>,
"Alexandre Torgue" <alexandre.torgue@foss.st.com>,
"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
"Siddharth Vadapalli" <s-vadapalli@ti.com>,
"Roger Quadros" <rogerq@kernel.org>,
"MD Danish Anwar" <danishanwar@ti.com>,
"Jiawen Wu" <jiawenwu@trustnetic.com>,
"Mengyuan Lou" <mengyuanlou@net-swift.com>,
"Ronak Doshi" <ronak.doshi@broadcom.com>,
"Ryder Lee" <ryder.lee@mediatek.com>,
"Shayne Chen" <shayne.chen@mediatek.com>,
"Sean Wang" <sean.wang@mediatek.com>,
"Juergen Gross" <jgross@suse.com>,
"Stefano Stabellini" <sstabellini@kernel.org>,
"Oleksandr Tyshchenko" <oleksandr_tyshchenko@epam.com>,
"Jordan Rhee" <jordanrhee@google.com>,
"Ankit Garg" <nktgrg@google.com>,
"Tim Hostetler" <thostet@google.com>,
"Eric Joyner" <eric.joyner@amd.com>,
"Prabu Thayalan" <prabu.ponrajthayalan@amd.com>,
"Nikhil P. Rao" <nikhil.rao@amd.com>,
"Chintan Vankar" <c-vankar@ti.com>,
"Nora Schiffer" <nora.schiffer@ew.tq-group.com>,
"Alexander Sverdlin" <alex@sverdlin.org>,
"Kevin Hao" <haokexin@gmail.com>,
"Karl Mehltretter" <kmehltretter@gmail.com>,
"Aleksandr Loktionov" <aleksandr.loktionov@intel.com>,
"Meghana Malladi" <m-malladi@ti.com>,
"Jacob Keller" <jacob.e.keller@intel.com>,
"StanleyYP Wang" <StanleyYP.Wang@mediatek.com>,
linux-doc@vger.kernel.org, imx@lists.linux.dev,
linux-rdma@vger.kernel.org, linux-hyperv@vger.kernel.org,
linux-renesas-soc@vger.kernel.org, linux-omap@vger.kernel.org
Subject: [PATCH net-next v2 3/5] tcp: detach page_pool pages before mapping in tcp_zerocopy_receive()
Date: Sun, 8 Nov 2026 06:00:14 +0000 [thread overview]
Message-ID: <20261108060016.2131509-4-almasrymina@google.com> (raw)
In-Reply-To: <20261108060016.2131509-1-almasrymina@google.com>
Once tcp_zerocopy_receive() maps an order-0 RX fragment page into a
userspace VMA via vm_insert_pages(), vm_insert_pages() increments
page->_refcount, which prevents the page from being recycled back into
its page_pool when the RX SKB is freed. However, while the RX SKB still
holds its fragment reference, the page remains marked as a page_pool
page (page_pool_page_is_pp(page) == true) inside the userspace VMA, so
subsequent GUP callers such as sendmsg(MSG_ZEROCOPY) or vmsplice() can
encounter a page_pool page while holding only a raw struct page
reference.
Establish the invariant that pp_ref_count is only incremented by callers
that already hold an active pp_ref_count (>= 1) on that netmem, and a
raw page_ref_count is never converted into a pp_ref_count:
- In can_map_frag(), require !skb_cloned(skb), page_pool_ref_count == 1,
and page_ref_count == 1 for page_pool pages so the fragment being
mapped holds the sole reference to the page; cloned SKBs or page_pool
pages with additional references (including drivers holding an
undrained fragment bias) fall back to copying.
- In tcp_zerocopy_receive(), convert the fragment's sole pp_ref_count
into a regular page reference and detach the page from its page_pool
via get_page(page) + napi_pp_put_page(page_to_netmem(page)) before
queueing the page for vm_insert_pages(). If vm_insert_pages()
subsequently fails, the detached page remains referenced by the RX SKB
via its regular page reference and is freed back to the page allocator
when the SKB is consumed.
Signed-off-by: Mina Almasry <almasrymina@google.com>
---
include/net/page_pool/helpers.h | 5 ++++
net/core/datagram.c | 2 ++
net/ipv4/tcp.c | 44 ++++++++++++++++++++++++++++-----
3 files changed, 45 insertions(+), 6 deletions(-)
diff --git a/include/net/page_pool/helpers.h b/include/net/page_pool/helpers.h
index 28635ce9454e8..c814ce95282eb 100644
--- a/include/net/page_pool/helpers.h
+++ b/include/net/page_pool/helpers.h
@@ -242,6 +242,11 @@ page_pool_get_dma_dir(const struct page_pool *pool)
return pool->p.dma_dir;
}
+static inline long page_pool_ref_count(netmem_ref netmem)
+{
+ return atomic_long_read(netmem_get_pp_ref_count_ref(netmem));
+}
+
static inline void page_pool_fragment_netmem(netmem_ref netmem, long nr)
{
atomic_long_set(netmem_get_pp_ref_count_ref(netmem), nr);
diff --git a/net/core/datagram.c b/net/core/datagram.c
index ecc30a6f83023..808eda4f7efb5 100644
--- a/net/core/datagram.c
+++ b/net/core/datagram.c
@@ -34,6 +34,7 @@
*
*/
+#include <kunit/visibility.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
@@ -702,6 +703,7 @@ int zerocopy_fill_skb_from_iter(struct sk_buff *skb,
}
return 0;
}
+EXPORT_SYMBOL_IF_KUNIT(zerocopy_fill_skb_from_iter);
static int
zerocopy_fill_skb_from_devmem(struct sk_buff *skb, struct iov_iter *from,
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index b9de7c302ba83..a984e7e93e4c9 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -286,6 +286,7 @@
#include <asm/ioctls.h>
#include <net/busy_poll.h>
#include <net/hotdata.h>
+#include <net/page_pool/helpers.h>
#include <trace/events/tcp.h>
#include <net/rps.h>
@@ -1921,7 +1922,7 @@ static skb_frag_t *skb_advance_to_frag(struct sk_buff *skb, u32 offset_skb,
return frag;
}
-static bool can_map_frag(const skb_frag_t *frag)
+static bool can_map_frag(const struct sk_buff *skb, const skb_frag_t *frag)
{
struct page *page;
@@ -1935,18 +1936,32 @@ static bool can_map_frag(const skb_frag_t *frag)
if (PageCompound(page) || page->mapping)
return false;
+ /* Before mapping a page_pool page into userspace,
+ * tcp_zerocopy_receive() converts this fragment's pp_ref_count into a
+ * regular page reference and detaches the page from its page_pool via
+ * napi_pp_put_page(). Require this skb_frag_t to hold the sole
+ * reference to the page so no other fragment or SKB clone expects
+ * pp_ref_count on it after detachment.
+ */
+ if (page_pool_page_is_pp(page) &&
+ (skb_cloned(skb) ||
+ page_pool_ref_count(page_to_netmem(page)) != 1 ||
+ page_ref_count(page) != 1))
+ return false;
+
return true;
}
-static int find_next_mappable_frag(const skb_frag_t *frag,
+static int find_next_mappable_frag(const struct sk_buff *skb,
+ const skb_frag_t *frag,
int remaining_in_skb)
{
int offset = 0;
- if (likely(can_map_frag(frag)))
+ if (likely(can_map_frag(skb, frag)))
return 0;
- while (offset < remaining_in_skb && !can_map_frag(frag)) {
+ while (offset < remaining_in_skb && !can_map_frag(skb, frag)) {
offset += skb_frag_size(frag);
++frag;
}
@@ -1986,7 +2001,8 @@ static void tcp_zerocopy_set_hint_for_skb(struct sock *sk,
* mappable_offset: Bytes till next mappable frag, *not* counting bytes
* in partial_frag_remainder.
*/
- mappable_offset = find_next_mappable_frag(frag, zc->recv_skip_hint);
+ mappable_offset = find_next_mappable_frag(skb, frag,
+ zc->recv_skip_hint);
zc->recv_skip_hint = mappable_offset + partial_frag_remainder;
}
@@ -2290,7 +2306,7 @@ static int tcp_zerocopy_receive(struct sock *sk,
break;
}
- mappable_offset = find_next_mappable_frag(frags,
+ mappable_offset = find_next_mappable_frag(skb, frags,
zc->recv_skip_hint);
if (mappable_offset) {
zc->recv_skip_hint = mappable_offset;
@@ -2300,6 +2316,22 @@ static int tcp_zerocopy_receive(struct sock *sk,
if (WARN_ON_ONCE(!page))
break;
+ /* Convert this fragment's sole pp_ref_count into a regular
+ * page reference and detach the page from its page_pool before
+ * mapping it into userspace. Once vm_insert_pages() elevates
+ * page_ref_count the page cannot be recycled into the
+ * page_pool anyway (and if vm_insert_pages() fails, the
+ * detached page is freed to the page allocator when the skb is
+ * consumed); detaching it up front ensures that a page_pool
+ * page is never mapped into a userspace VMA (where GUP callers
+ * such as MSG_ZEROCOPY or vmsplice() would otherwise observe a
+ * page_pool page while holding only a raw page_ref_count).
+ */
+ if (page_pool_page_is_pp(page)) {
+ get_page(page);
+ napi_pp_put_page(page_to_netmem(page));
+ }
+
prefetchw(page);
pages[pages_to_map++] = page;
length += PAGE_SIZE;
--
2.56.0.455.g6c5a503b38-goog
next prev parent reply other threads:[~2026-11-08 6:00 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-08 6:00 [PATCH net-next v2 0/5] net: deprecate skb->pp_recycle and skb_mark_for_recycle() Mina Almasry
2026-11-08 6:00 ` [PATCH net-next v2 1/5] net: skbuff: skip managed zerocopy frags in skb_frag_ref() Mina Almasry
2026-11-08 6:00 ` [PATCH net-next v2 2/5] net: skbuff: refcount spliced page_pool frags via skb_pipe_buf_ops Mina Almasry
2026-11-08 6:00 ` Mina Almasry [this message]
2026-11-08 6:00 ` [PATCH net-next v2 4/5] net: remove skb->pp_recycle and consolidate netmem refcounting Mina Almasry
2026-11-08 6:00 ` [PATCH net-next v2 5/5] net: remove skb_mark_for_recycle() 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=20261108060016.2131509-4-almasrymina@google.com \
--to=almasrymina@google.com \
--cc=StanleyYP.Wang@mediatek.com \
--cc=Steen.Hegelund@microchip.com \
--cc=UNGLinuxDriver@microchip.com \
--cc=akpm@linux-foundation.org \
--cc=aleksandr.loktionov@intel.com \
--cc=alex@sverdlin.org \
--cc=alexandre.torgue@foss.st.com \
--cc=andrew+netdev@lunn.ch \
--cc=angelogioacchino.delregno@collabora.com \
--cc=anthony.l.nguyen@intel.com \
--cc=asml.silence@gmail.com \
--cc=ast@kernel.org \
--cc=bbhushan2@marvell.com \
--cc=bcm-kernel-feedback-list@broadcom.com \
--cc=bobbyeshleman@meta.com \
--cc=bpf@vger.kernel.org \
--cc=brett.creeley@amd.com \
--cc=byungchul@sk.com \
--cc=c-vankar@ti.com \
--cc=corbet@lwn.net \
--cc=daniel.machon@microchip.com \
--cc=daniel@iogearbox.net \
--cc=danishanwar@ti.com \
--cc=davem@davemloft.net \
--cc=david@kernel.org \
--cc=decui@microsoft.com \
--cc=dong100@mucse.com \
--cc=edumazet@kernel.org \
--cc=eperezma@redhat.com \
--cc=eric.joyner@amd.com \
--cc=florian.fainelli@broadcom.com \
--cc=frank.li@nxp.com \
--cc=fujita.tomonori@gmail.com \
--cc=gakula@marvell.com \
--cc=gongfan1@huawei.com \
--cc=guwen@linux.alibaba.com \
--cc=haiyangz@microsoft.com \
--cc=haokexin@gmail.com \
--cc=hawk@kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=horatiu.vultur@microchip.com \
--cc=horms@kernel.org \
--cc=hramamurthy@google.com \
--cc=idosch@nvidia.com \
--cc=ilias.apalodimas@linaro.org \
--cc=imx@lists.linux.dev \
--cc=jacob.e.keller@intel.com \
--cc=jasowangio@gmail.com \
--cc=jgross@suse.com \
--cc=jiawenwu@trustnetic.com \
--cc=johannes@sipsolutions.net \
--cc=john.fastabend@gmail.com \
--cc=jordanrhee@google.com \
--cc=joshwash@google.com \
--cc=justin.chen@broadcom.com \
--cc=justinlai0215@realtek.com \
--cc=kmehltretter@gmail.com \
--cc=kojima.masahisa@socionext.com \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=larry.chiu@realtek.com \
--cc=leon@kernel.org \
--cc=liam@infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hyperv@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-omap@vger.kernel.org \
--cc=linux-rdma@vger.kernel.org \
--cc=linux-renesas-soc@vger.kernel.org \
--cc=linux-wireless@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=ljs@kernel.org \
--cc=lorenzo@kernel.org \
--cc=lulie@linux.alibaba.com \
--cc=m-malladi@ti.com \
--cc=marcin.s.wojtas@gmail.com \
--cc=matthias.bgg@gmail.com \
--cc=maxime.chevallier@bootlin.com \
--cc=mbloch@nvidia.com \
--cc=mcoquelin.stm32@gmail.com \
--cc=mengyuanlou@net-swift.com \
--cc=mhocko@suse.com \
--cc=michael.chan@broadcom.com \
--cc=mst@redhat.com \
--cc=nb@tipi-net.de \
--cc=nbd@nbd.name \
--cc=ncardwell@google.com \
--cc=netdev@vger.kernel.org \
--cc=nikhil.rao@amd.com \
--cc=niklas.soderlund@ragnatech.se \
--cc=nktgrg@google.com \
--cc=nora.schiffer@ew.tq-group.com \
--cc=oleksandr_tyshchenko@epam.com \
--cc=opendmb@gmail.com \
--cc=pabeni@redhat.com \
--cc=paul@pbarker.dev \
--cc=pavan.chebbi@broadcom.com \
--cc=petrm@nvidia.com \
--cc=prabu.ponrajthayalan@amd.com \
--cc=przemyslaw.kitszel@intel.com \
--cc=rdunlap@infradead.org \
--cc=rkannoth@marvell.com \
--cc=rogerq@kernel.org \
--cc=ronak.doshi@broadcom.com \
--cc=rppt@kernel.org \
--cc=ryder.lee@mediatek.com \
--cc=s-vadapalli@ti.com \
--cc=saeedm@nvidia.com \
--cc=satishkh@cisco.com \
--cc=sbhatta@marvell.com \
--cc=sd@queasysnail.net \
--cc=sdf@fomichev.me \
--cc=sean.wang@mediatek.com \
--cc=sgoutham@marvell.com \
--cc=shaojijie@huawei.com \
--cc=shayne.chen@mediatek.com \
--cc=shenjian15@huawei.com \
--cc=shenwei.wang@nxp.com \
--cc=skhan@linuxfoundation.org \
--cc=sstabellini@kernel.org \
--cc=steffen.klassert@secunet.com \
--cc=surenb@google.com \
--cc=tariqt@nvidia.com \
--cc=thostet@google.com \
--cc=vbabka@kernel.org \
--cc=vikas.gupta@broadcom.com \
--cc=virtualization@lists.linux.dev \
--cc=wei.fang@nxp.com \
--cc=wei.liu@kernel.org \
--cc=willemb@google.com \
--cc=xuanzhuo@linux.alibaba.com \
--cc=ziy@nvidia.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
all inboxes | Powered by JetHome®