* [PATCH net v2 1/4] net: skbuff: limit folio refcount when copying skb frag references
2026-11-26 21:18 [PATCH net v2 0/4] net: skbuff: limit pagecache folio refcount David Hildenbrand (Arm)
@ 2026-11-26 21:18 ` David Hildenbrand (Arm)
2026-11-27 21:21 ` netdev-bot+sashiko
2026-11-26 21:18 ` [PATCH net v2 2/4] net: skbuff: limit folio refcount when rearranging skb frags David Hildenbrand (Arm)
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-26 21:18 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Stanislav Fomichev, Willem de Bruijn, Simon Horman, Andrew Morton,
Kiryl Shutsemau, Song Liu, Johannes Weiner, Rik van Riel,
Matthew Wilcox (Oracle), Neal Cardwell, Kuniyuki Iwashima,
Pedro Falcato, Chuck Lever, Jeff Layton
Cc: netdev, linux-kernel, linux-mm, David Hildenbrand (Arm), stable
Various networking features copy frag references of an skb, for example
when unsharing a cloned skb (AF_PACKET trimming a clone), when copying an
skb (nft dup) or when building NFQUEUE messages. This is an effective
way for multiplying folio references held by an skb, allowing for
exceeding folio refcount limits.
This is not specific to large pagecache folios, but with them it is a
lot easier to reach the refcount limit, as page table mappings can cheaply
create many references.
Let's introduce skb_frag_try_ref() and skb_frags_try_ref(), which use
try_get_page(), and use them in pskb_expand_head(), __pskb_copy_fclone(),
skb_zerocopy() and pskb_carve_inside_*(). All of these can already fail,
and their callers handle that.
Fixes: 800d8c63b2e9 ("shmem: add huge pages support")
Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS")
Fixes: 793917d997df ("mm/readahead: Add large folio readahead")
Cc: stable@vger.kernel.org
Acked-by: Pedro Falcato <pfalcato@suse.de>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
include/linux/skbuff_ref.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++
net/core/skbuff.c | 35 +++++++++++++++++++-----------
2 files changed, 76 insertions(+), 12 deletions(-)
diff --git a/include/linux/skbuff_ref.h b/include/linux/skbuff_ref.h
index b6f6da556ee90..560ddc863bbec 100644
--- a/include/linux/skbuff_ref.h
+++ b/include/linux/skbuff_ref.h
@@ -44,6 +44,33 @@ static __always_inline void skb_frag_ref(struct sk_buff *skb, int f)
skb->pp_recycle);
}
+/**
+ * skb_frag_try_ref - try to acquire a reference on a paged fragment of an skb
+ * @skb: the buffer
+ * @f: the fragment offset
+ *
+ * Like skb_frag_ref(), but acquires non-page_pool page references using
+ * try_get_page(), refusing additional references to a page whose refcount is
+ * at its limit.
+ *
+ * Return: True if a reference was acquired, otherwise false.
+ */
+static __always_inline __must_check bool skb_frag_try_ref(struct sk_buff *skb,
+ int f)
+{
+ netmem_ref netmem = skb_frag_netmem(&skb_shinfo(skb)->frags[f]);
+
+#ifdef CONFIG_PAGE_POOL
+ if (skb->pp_recycle && napi_pp_get_page(netmem))
+ return true;
+#endif
+ if (netmem_is_net_iov(netmem)) {
+ net_iov_inc_ref(netmem_to_net_iov(netmem));
+ return true;
+ }
+ return try_get_page(netmem_to_page(netmem));
+}
+
bool napi_pp_put_page(netmem_ref netmem);
/**
@@ -83,4 +110,30 @@ static __always_inline void skb_frag_unref(struct sk_buff *skb, int f)
skb->pp_recycle);
}
+/**
+ * skb_frags_try_ref - try to acquire references on all paged fragments
+ * @skb: the buffer
+ *
+ * Try to acquire a reference on each paged fragment of @skb using
+ * skb_frag_try_ref(). On failure, acquired references are dropped.
+ *
+ * Must not be used on skbs with managed frag references.
+ *
+ * Return: True if all references were acquired, otherwise false.
+ */
+static inline __must_check bool skb_frags_try_ref(struct sk_buff *skb)
+{
+ int i;
+
+ DEBUG_NET_WARN_ON_ONCE(skb_zcopy_managed(skb));
+ for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
+ if (unlikely(!skb_frag_try_ref(skb, i))) {
+ while (i--)
+ skb_frag_unref(skb, i);
+ return false;
+ }
+ }
+ return true;
+}
+
#endif /* _LINUX_SKBUFF_REF_H */
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 1ac1e327ac4c6..05626d9622b3d 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -2266,7 +2266,13 @@ struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
n->pp_recycle = skb->pp_recycle;
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
- skb_frag_ref(n, i);
+ if (!skb_frag_try_ref(n, i)) {
+ /* Drop the references acquired so far. */
+ skb_shinfo(n)->nr_frags = i;
+ kfree_skb(n);
+ n = NULL;
+ goto out;
+ }
}
skb_shinfo(n)->nr_frags = i;
skb_shinfo(n)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
@@ -2310,7 +2316,6 @@ int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
unsigned int size = osize + nhead + ntail;
long off;
u8 *data;
- int i;
if (unlikely(nhead < 0)) {
DEBUG_NET_WARN_ON_ONCE(1);
@@ -2347,12 +2352,10 @@ int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
* be since all we did is relocate the values
*/
if (skb_cloned(skb)) {
- if (skb_orphan_frags(skb, gfp_mask))
+ if (skb_orphan_frags(skb, gfp_mask) || !skb_frags_try_ref(skb))
goto nofrags;
if (skb_zcopy(skb))
net_zcopy_get(skb_uarg(skb));
- for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
- skb_frag_ref(skb, i);
if (skb_has_frag_list(skb))
skb_clone_fraglist(skb);
@@ -3908,7 +3911,8 @@ EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
*
* Return value:
* 0: everything is OK
- * -ENOMEM: couldn't orphan frags of @from due to lack of memory
+ * -ENOMEM: couldn't orphan frags of @from due to lack of memory, or
+ * couldn't take additional references to the frags of @from
* -EFAULT: skb_copy_bits() found some problem with skb geometry, or readable head
* payload would be mixed with unreadable frags.
*/
@@ -3973,7 +3977,11 @@ skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
len);
skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
len -= size;
- skb_frag_ref(to, j);
+ if (!skb_frag_try_ref(to, j)) {
+ /* The caller frees @to, dropping the acquired refs. */
+ skb_shinfo(to)->nr_frags = j;
+ return -ENOMEM;
+ }
j++;
}
skb_shinfo(to)->nr_frags = j;
@@ -7004,7 +7012,6 @@ static void skb_carve_reset_headers(struct sk_buff *skb)
static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
const int headlen, gfp_t gfp_mask)
{
- int i;
unsigned int size = skb_end_offset(skb);
int new_hlen = headlen - off;
u8 *data;
@@ -7032,14 +7039,12 @@ static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
frags[skb_shinfo(skb)->nr_frags]));
if (skb_cloned(skb)) {
/* drop the old head gracefully */
- if (skb_orphan_frags(skb, gfp_mask)) {
+ if (skb_orphan_frags(skb, gfp_mask) || !skb_frags_try_ref(skb)) {
skb_kfree_head(data);
return -ENOMEM;
}
if (skb_zcopy(skb))
net_zcopy_get(skb_uarg(skb));
- for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
- skb_frag_ref(skb, i);
if (skb_has_frag_list(skb))
skb_clone_fraglist(skb);
skb_release_data(skb, SKB_CONSUMED);
@@ -7169,7 +7174,13 @@ static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
skb_frag_off_add(&shinfo->frags[0], off - pos);
skb_frag_size_sub(&shinfo->frags[0], off - pos);
}
- skb_frag_ref(skb, i);
+ if (!skb_frag_try_ref(skb, i)) {
+ /* We referenced frags i - k ... i - 1 so far. */
+ while (k--)
+ skb_frag_unref(skb, --i);
+ skb_kfree_head(data);
+ return -ENOMEM;
+ }
k++;
}
pos += fsize;
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH net v2 1/4] net: skbuff: limit folio refcount when copying skb frag references
2026-11-26 21:18 ` [PATCH net v2 1/4] net: skbuff: limit folio refcount when copying skb frag references David Hildenbrand (Arm)
@ 2026-11-27 21:21 ` netdev-bot+sashiko
0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 21:21 UTC (permalink / raw)
To: david
Cc: davem, edumazet, kuba, pabeni, sdf.kernel, willemb, horms, akpm,
kas, songliubraving, hannes, riel, willy, ncardwell, kuniyu,
pfalcato, cel, jlayton, netdev, linux-kernel, linux-mm, stable
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] Concern: this commit converts only pskb_expand_head(),
__pskb_copy_fclone(), skb_zerocopy() and pskb_carve_inside_*() to the…
--
net: skbuff: limit folio refcount when copying skb frag references
This commit adds skb_frag_try_ref() and skb_frags_try_ref(). Both take
frag page references with try_get_page(), so they refuse a new reference
when the folio refcount is at its limit. The commit converts
pskb_expand_head(), __pskb_copy_fclone(), skb_zerocopy() and
pskb_carve_inside_*() to these helpers, and those functions now fail when
a reference can't be taken.
> Let's introduce skb_frag_try_ref() and skb_frags_try_ref(), which use
> try_get_page(), and use them in pskb_expand_head(), __pskb_copy_fclone(),
> skb_zerocopy() and pskb_carve_inside_*(). All of these can already fail,
> and their callers handle that.
[Severity: Low]
This isn't a bug, but could the commit message mention the frag reference
sites that still use plain skb_frag_ref() at this point in the series?
In net/core/skbuff.c, these still take unchecked references:
skb_segment():
skb_frag_ref(nskb, skb_shinfo(nskb)->nr_frags);
skb_try_coalesce():
skb_frag_ref(from, i);
skb_shift():
skb_frag_ref(tgt, to);
skb_split_no_header():
skb_frag_ref(skb1, k);
Outside skbuff.c, the same applies to tcp_clone_payload() in
net/ipv4/tcp_output.c, and to iptfs_skb_add_frags() and
iptfs_consume_frags() in net/xfrm/xfrm_iptfs.c.
The next patch in the series, "net: skbuff: limit folio refcount when
rearranging skb frags", converts skb_segment(), skb_try_coalesce(),
skb_shift() and tcp_clone_payload().
That patch also explains why skb_split() is left unchanged. Each split
allocates a new skb that is charged to the socket, and the references are
dropped once the data is acked.
The cover letter (merge 25f5c92e1ac0) leaves IP-TFS reassembly out on
purpose, because it needs one new inner skb per additional reference.
Would a short note saying that the remaining callers are converted, or
skipped on purpose, in the follow-up patch be worth adding here?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126-skb_refcount_overflow-v2-0-a6f600a5634a%40kernel.org
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH net v2 2/4] net: skbuff: limit folio refcount when rearranging skb frags
2026-11-26 21:18 [PATCH net v2 0/4] net: skbuff: limit pagecache folio refcount David Hildenbrand (Arm)
2026-11-26 21:18 ` [PATCH net v2 1/4] net: skbuff: limit folio refcount when copying skb frag references David Hildenbrand (Arm)
@ 2026-11-26 21:18 ` David Hildenbrand (Arm)
2026-11-26 21:18 ` [PATCH net v2 3/4] net: skbuff: limit folio refcount when splicing pages into skbs David Hildenbrand (Arm)
2026-11-26 21:18 ` [PATCH net v2 4/4] net: skbuff: limit folio refcount when splicing from sockets David Hildenbrand (Arm)
3 siblings, 0 replies; 7+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-26 21:18 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Stanislav Fomichev, Willem de Bruijn, Simon Horman, Andrew Morton,
Kiryl Shutsemau, Song Liu, Johannes Weiner, Rik van Riel,
Matthew Wilcox (Oracle), Neal Cardwell, Kuniyuki Iwashima,
Pedro Falcato, Chuck Lever, Jeff Layton
Cc: netdev, linux-kernel, linux-mm, David Hildenbrand (Arm), stable
Various networking features take additional references to frag pages
that are still referenced elsewhere when rearranging skb frags, for
example software GSO segmentation, TCP receive coalescing of clones, SACK
shifting and MTU probing. This is an effective way for multiplying folio
references held by an skb, allowing for exceeding folio refcount limits.
This is not specific to large pagecache folios, but with them it is a
lot easier to reach the refcount limit, as page table mappings can cheaply
create many references.
Let's use skb_frag_try_ref() and skb_frags_try_ref() in skb_segment(),
skb_try_coalesce(), skb_shift() and tcp_clone_payload(). All of these can
already fail, and their callers handle that.
skb_split() can still add one reference when splitting a frag. But each
split also allocates a new skb that is charged to the socket, reaching
the refcount limit that way would require a lot of socket memory, and the
references go away once the data was acked, so it's not that relevant in
practice. Making skb_split() fail is more involved, so let's leave it alone
for now.
Fixes: 800d8c63b2e9 ("shmem: add huge pages support")
Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS")
Fixes: 793917d997df ("mm/readahead: Add large folio readahead")
Cc: stable@vger.kernel.org
Acked-by: Pedro Falcato <pfalcato@suse.de>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
net/core/skbuff.c | 32 +++++++++++++++++++++-----------
net/ipv4/tcp_output.c | 8 +++++---
2 files changed, 26 insertions(+), 14 deletions(-)
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 05626d9622b3d..6da51ad8fd440 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -4338,7 +4338,8 @@ static int skb_prepare_for_shift(struct sk_buff *skb)
* the length of the skb, from skb to tgt. Returns number bytes shifted.
* It's up to caller to free skb if everything was shifted.
*
- * If @tgt runs out of frags, the whole operation is aborted.
+ * If @tgt runs out of frags, or we cannot take an additional frag reference,
+ * the whole operation is aborted.
*
* Skb cannot include anything else but paged data while tgt is allowed
* to have non-paged data as well.
@@ -4421,7 +4422,9 @@ int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
} else {
skb_frag_page_copy(fragto, fragfrom);
- skb_frag_ref(tgt, to);
+ /* Nothing has been committed yet. */
+ if (!skb_frag_try_ref(tgt, to))
+ return 0;
skb_frag_off_copy(fragto, fragfrom);
skb_frag_size_set(fragto, todo);
@@ -5160,7 +5163,10 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
if (!skb_shinfo(nskb)->nr_frags)
nskb->pp_recycle = frag_skb->pp_recycle;
*nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
- skb_frag_ref(nskb, skb_shinfo(nskb)->nr_frags);
+ if (!skb_frag_try_ref(nskb, skb_shinfo(nskb)->nr_frags)) {
+ err = -ENOMEM;
+ goto err;
+ }
size = skb_frag_size(nskb_frag);
if (pos < offset) {
@@ -6301,7 +6307,8 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
bool *fragstolen, int *delta_truesize)
{
struct skb_shared_info *to_shinfo, *from_shinfo;
- int i, delta, len = from->len;
+ int delta, len = from->len;
+ bool cloned;
*fragstolen = false;
@@ -6374,6 +6381,15 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
WARN_ON_ONCE(delta < len);
+ /*
+ * A clone keeps referencing its frags, so we need our own references.
+ * Nothing was added to @to yet: we only steal the head of @from above
+ * if it is not locked, and the head of a clone is always locked.
+ */
+ cloned = skb_cloned(from);
+ if (cloned && !skb_frags_try_ref(from))
+ return false;
+
memcpy(to_shinfo->frags + to_shinfo->nr_frags,
from_shinfo->frags,
from_shinfo->nr_frags * sizeof(skb_frag_t));
@@ -6381,15 +6397,9 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
if (from_shinfo->nr_frags)
to_shinfo->flags |= from_shinfo->flags & SKBFL_SHARED_FRAG;
- if (!skb_cloned(from))
+ if (!cloned)
from_shinfo->nr_frags = 0;
- /* if the skb is not cloned this does nothing
- * since we set nr_frags to 0.
- */
- for (i = 0; i < from_shinfo->nr_frags; i++)
- skb_frag_ref(from, i);
-
to->truesize += delta;
to->len += len;
to->data_len += len;
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index cf3ddc055d48d..043838d996404 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -2710,10 +2710,12 @@ static int tcp_clone_payload(struct sock *sk, struct sk_buff *to,
}
commit:
WARN_ON_ONCE(len != probe_size);
- for (i = 0; i < nr_frags; i++)
- skb_frag_ref(to, i);
-
skb_shinfo(to)->nr_frags = nr_frags;
+ if (!skb_frags_try_ref(to)) {
+ skb_shinfo(to)->nr_frags = 0;
+ return -ENOMEM;
+ }
+
to->truesize += probe_size;
to->len += probe_size;
to->data_len += probe_size;
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net v2 3/4] net: skbuff: limit folio refcount when splicing pages into skbs
2026-11-26 21:18 [PATCH net v2 0/4] net: skbuff: limit pagecache folio refcount David Hildenbrand (Arm)
2026-11-26 21:18 ` [PATCH net v2 1/4] net: skbuff: limit folio refcount when copying skb frag references David Hildenbrand (Arm)
2026-11-26 21:18 ` [PATCH net v2 2/4] net: skbuff: limit folio refcount when rearranging skb frags David Hildenbrand (Arm)
@ 2026-11-26 21:18 ` David Hildenbrand (Arm)
2026-11-27 21:21 ` netdev-bot+sashiko
2026-11-26 21:18 ` [PATCH net v2 4/4] net: skbuff: limit folio refcount when splicing from sockets David Hildenbrand (Arm)
3 siblings, 1 reply; 7+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-26 21:18 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Stanislav Fomichev, Willem de Bruijn, Simon Horman, Andrew Morton,
Kiryl Shutsemau, Song Liu, Johannes Weiner, Rik van Riel,
Matthew Wilcox (Oracle), Neal Cardwell, Kuniyuki Iwashima,
Pedro Falcato, Chuck Lever, Jeff Layton
Cc: netdev, linux-kernel, linux-mm, David Hildenbrand (Arm), stable
Various networking features use MSG_SPLICE_PAGES to add pages to skbs,
whereby skb_append_pagefrags() takes a page reference for each fragment it
adds. Pages spliced from pipes were already checked when entering the
pipe, but in-kernel users like sunrpc pass pagecache pages directly. This
allows for exceeding folio refcount limits.
This is not specific to large pagecache folios, but with them it is a
lot easier to reach the refcount limit, as page table mappings can cheaply
create many references.
Let's use try_get_page() in skb_append_pagefrags() and fail with -EFAULT.
Don't use -EMSGSIZE, as TCP would just retry with a new skb. The only
caller, skb_splice_from_iter(), handles that.
For nfsd, such a failure while sending a reply tears down the
connection. That can only happen if someone pushed the refcount of an
exported folio close to the limit, so it's not relevant in practice;
nfsd could handle that more gracefully in the future.
Fixes: 800d8c63b2e9 ("shmem: add huge pages support")
Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS")
Fixes: 793917d997df ("mm/readahead: Add large folio readahead")
Cc: stable@vger.kernel.org
Acked-by: Pedro Falcato <pfalcato@suse.de>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
net/core/skbuff.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 6da51ad8fd440..957335bcc5961 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -4714,8 +4714,13 @@ int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
if (skb_can_coalesce(skb, i, page, offset)) {
skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
} else if (i < max_frags) {
+ /*
+ * Don't use -EMSGSIZE, as callers would just retry with a new
+ * skb.
+ */
+ if (!try_get_page(page))
+ return -EFAULT;
skb_zcopy_downgrade_managed(skb);
- get_page(page);
skb_fill_page_desc_noacc(skb, i, page, offset, size);
} else {
return -EMSGSIZE;
@@ -7613,8 +7618,9 @@ static void skb_splice_csum_page(struct sk_buff *skb, struct page *page,
* possible, copying them to fragments if not possible (such as if they're slab
* pages).
*
- * Returns the amount of data spliced/copied or -EMSGSIZE if there's
- * insufficient space in the buffer to transfer anything.
+ * Returns the amount of data spliced/copied, -EMSGSIZE if there's
+ * insufficient space in the buffer to transfer anything, or -EFAULT if we
+ * could not take a page reference.
*/
ssize_t skb_splice_from_iter(struct sk_buff *skb, struct iov_iter *iter,
ssize_t maxsize)
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH net v2 3/4] net: skbuff: limit folio refcount when splicing pages into skbs
2026-11-26 21:18 ` [PATCH net v2 3/4] net: skbuff: limit folio refcount when splicing pages into skbs David Hildenbrand (Arm)
@ 2026-11-27 21:21 ` netdev-bot+sashiko
0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 21:21 UTC (permalink / raw)
To: david
Cc: davem, edumazet, kuba, pabeni, sdf.kernel, willemb, horms, akpm,
kas, songliubraving, hannes, riel, willy, ncardwell, kuniyu,
pfalcato, cel, jlayton, netdev, linux-kernel, linux-mm, stable
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The refcount-limit fix does not cover kernel TLS, so the case
the commit message uses as its example (sunrpc/nfsd sending pagecache…
--
net: skbuff: limit folio refcount when splicing pages into skbs
This commit changes skb_append_pagefrags() to use try_get_page() instead
of get_page(). In-kernel MSG_SPLICE_PAGES senders that pass pagecache
pages directly, such as sunrpc, can then no longer push a folio's
refcount past the limit. On failure it returns -EFAULT rather than
-EMSGSIZE, so TCP does not just retry with a new skb.
> diff --git a/net/core/skbuff.c b/net/core/skbuff.c
> index 6da51ad8fd440..957335bcc5961 100644
> --- a/net/core/skbuff.c
> +++ b/net/core/skbuff.c
> @@ -4714,8 +4714,13 @@ int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
> if (skb_can_coalesce(skb, i, page, offset)) {
> skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
> } else if (i < max_frags) {
> + /*
> + * Don't use -EMSGSIZE, as callers would just retry with a new
> + * skb.
> + */
> + if (!try_get_page(page))
> + return -EFAULT;
> skb_zcopy_downgrade_managed(skb);
> - get_page(page);
> skb_fill_page_desc_noacc(skb, i, page, offset, size);
> } else {
> return -EMSGSIZE;
[Severity: High]
Does this cover sockets that have kTLS installed? The commit message uses
sunrpc/nfsd as its example. With kTLS, though, MSG_SPLICE_PAGES never
reaches skb_append_pagefrags().
With software kTLS the call chain is:
tls_sw_sendmsg()
tls_sw_sendmsg_splice()
sk_msg_page_add()
sk_msg_page_add() in include/linux/skmsg.h still takes the reference
without a check, and it has no way to return an error:
static inline void sk_msg_page_add(struct sk_msg *msg, struct page *page,
u32 len, u32 offset)
{
struct scatterlist *sge;
get_page(page);
...
}
With TLS device offload, the MSG_SPLICE_PAGES branch in tls_push_data()
passes the extracted page to tls_append_frag(). That function also uses
plain get_page(), and the reference is held until the record is ACKed:
net/tls/tls_device.c:tls_append_frag() {
...
} else {
++frag;
skb_frag_fill_page_desc(frag, pfrag->page, pfrag->offset,
size);
++record->num_frags;
get_page(pfrag->page);
}
...
}
nfsd reaches these paths with xprtsec=tls:
- nfsd_read_splice_ok() only turns off splice reads for krb5i and krb5p.
- So nfsd_splice_read() still puts pagecache pages into rq_res.
- svc_tcp_sendmsg() then sends them with MSG_SPLICE_PAGES through
sock_sendmsg(), which goes to tls_sw_sendmsg() or tls_device_sendmsg().
nvme-tcp over TLS also sends pages with MSG_SPLICE_PAGES and goes through
the same paths.
The sunrpc client does not seem to use MSG_SPLICE_PAGES, since
XS_SENDMSG_FLAGS is MSG_DONTWAIT | MSG_NOSIGNAL. The server side is the
case that matters here.
Should sk_msg_page_add() (or tls_sw_sendmsg_splice()) and the zero-copy
tls_append_frag() path also use try_get_page() and fail with -EFAULT in
the same way? No other patch in the series appears to touch net/tls or
include/linux/skmsg.h.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126-skb_refcount_overflow-v2-0-a6f600a5634a%40kernel.org
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH net v2 4/4] net: skbuff: limit folio refcount when splicing from sockets
2026-11-26 21:18 [PATCH net v2 0/4] net: skbuff: limit pagecache folio refcount David Hildenbrand (Arm)
` (2 preceding siblings ...)
2026-11-26 21:18 ` [PATCH net v2 3/4] net: skbuff: limit folio refcount when splicing pages into skbs David Hildenbrand (Arm)
@ 2026-11-26 21:18 ` David Hildenbrand (Arm)
3 siblings, 0 replies; 7+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-26 21:18 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Stanislav Fomichev, Willem de Bruijn, Simon Horman, Andrew Morton,
Kiryl Shutsemau, Song Liu, Johannes Weiner, Rik van Riel,
Matthew Wilcox (Oracle), Neal Cardwell, Kuniyuki Iwashima,
Pedro Falcato, Chuck Lever, Jeff Layton
Cc: netdev, linux-kernel, linux-mm, David Hildenbrand (Arm), stable
Various networking features support splicing data from sockets into
pipes (TCP, AF_UNIX, KCM, kTLS), whereby spd_fill_page() takes an
additional page reference for each fragment, while the skb keeps its own
reference until it was fully consumed. This allows for exceeding folio
refcount limits.
This is not specific to large pagecache folios, but with them it is a
lot easier to reach the refcount limit, as page table mappings can cheaply
create many references.
Let's use try_get_page() in spd_fill_page() and stop splicing with
-EFAULT if that fails. To propagate the error, add an "err" output
parameter to spd_fill_page(), __splice_segment() and __skb_splice_bits().
If we could not splice anything, return the error from skb_splice_bits():
returning 0 would make the callers (TCP, AF_UNIX, KCM, kTLS) report EOF,
although data is still queued. The callers already handle errors.
Use -EFAULT, consistent with splice_folio_into_pipe().
Fixes: 800d8c63b2e9 ("shmem: add huge pages support")
Fixes: 99cb0dbd47a1 ("mm,thp: add read-only THP support for (non-shmem) FS")
Fixes: 793917d997df ("mm/readahead: Add large folio readahead")
Cc: stable@vger.kernel.org
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
net/core/skbuff.c | 38 +++++++++++++++++++++++---------------
1 file changed, 23 insertions(+), 15 deletions(-)
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 957335bcc5961..ca5e27545b46b 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -3162,11 +3162,12 @@ static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
}
/*
- * Fill page/offset/length into spd, if it can hold more pages.
+ * Fill page/offset/length into spd, if it can hold more pages and we can
+ * take a page reference.
*/
static bool spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
unsigned int *len, unsigned int offset, bool linear,
- struct sock *sk)
+ struct sock *sk, int *err)
{
if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
return true;
@@ -3180,7 +3181,10 @@ static bool spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
spd->partial[spd->nr_pages - 1].len += *len;
return false;
}
- get_page(page);
+ if (!try_get_page(page)) {
+ *err = -EFAULT;
+ return true;
+ }
spd->pages[spd->nr_pages] = page;
spd->partial[spd->nr_pages].len = *len;
spd->partial[spd->nr_pages].offset = offset;
@@ -3193,7 +3197,7 @@ static bool __splice_segment(struct page *page, unsigned int poff,
unsigned int plen, unsigned int *off,
unsigned int *len,
struct splice_pipe_desc *spd, bool linear,
- struct sock *sk)
+ struct sock *sk, int *err)
{
if (!*len)
return true;
@@ -3212,7 +3216,7 @@ static bool __splice_segment(struct page *page, unsigned int poff,
do {
unsigned int flen = min(*len, plen);
- if (spd_fill_page(spd, page, &flen, poff, linear, sk))
+ if (spd_fill_page(spd, page, &flen, poff, linear, sk, err))
return true;
poff += flen;
plen -= flen;
@@ -3226,11 +3230,12 @@ static bool __splice_segment(struct page *page, unsigned int poff,
/*
* Map linear and fragment data from the skb to spd. It reports true if the
- * pipe is full or if we already spliced the requested length.
+ * pipe is full, if we already spliced the requested length, or on error.
*/
static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
unsigned int *offset, unsigned int *len,
- struct splice_pipe_desc *spd, struct sock *sk)
+ struct splice_pipe_desc *spd, struct sock *sk,
+ int *err)
{
struct sk_buff *iter;
int seg;
@@ -3245,7 +3250,7 @@ static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
skb_headlen(skb),
offset, len, spd,
skb_head_is_locked(skb),
- sk))
+ sk, err))
return true;
/*
@@ -3262,7 +3267,7 @@ static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
if (__splice_segment(skb_frag_page(f),
skb_frag_off(f), skb_frag_size(f),
- offset, len, spd, false, sk))
+ offset, len, spd, false, sk, err))
return true;
}
@@ -3272,10 +3277,10 @@ static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
continue;
}
/* __skb_splice_bits() only fails if the output has no room
- * left, so no point in going over the frag_list for the error
- * case.
+ * left or on error, so no point in going over the frag_list
+ * for the error case.
*/
- if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
+ if (__skb_splice_bits(iter, pipe, offset, len, spd, sk, err))
return true;
}
@@ -3284,7 +3289,8 @@ static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
/*
* Map data from the skb to a pipe. Should handle both the linear part,
- * the fragments, and the frag list.
+ * the fragments, and the frag list. Returns the number of bytes spliced,
+ * or an error if splicing failed.
*/
int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
struct pipe_inode_info *pipe, unsigned int tlen,
@@ -3299,12 +3305,14 @@ int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
.ops = &default_pipe_buf_ops,
.spd_release = sock_spd_release,
};
- int ret = 0;
+ int ret = 0, err = 0;
- __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
+ __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk, &err);
if (spd.nr_pages)
ret = splice_to_pipe(pipe, &spd);
+ else if (err)
+ ret = err;
return ret;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread