* [PATCH net 1/4] net: skbuff: limit folio refcount when copying skb frag references
2026-11-18 8:32 [PATCH net 0/4] net: skbuff: limit pagecache folio refcount David Hildenbrand (Arm)
@ 2026-11-18 8:32 ` David Hildenbrand (Arm)
2026-11-19 13:53 ` David Hildenbrand (Arm)
` (2 more replies)
2026-11-18 8:32 ` [PATCH net 2/4] net: skbuff: limit folio refcount when rearranging skb frags David Hildenbrand (Arm)
` (2 subsequent siblings)
3 siblings, 3 replies; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-18 8:32 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Andrew Morton, Kiryl Shutsemau, Song Liu,
Johannes Weiner, Rik van Riel, Matthew Wilcox (Oracle),
Neal Cardwell, Kuniyuki Iwashima, Pedro Falcato
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..447099b39deeb 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.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH net 1/4] net: skbuff: limit folio refcount when copying skb frag references
2026-11-18 8:32 ` [PATCH net 1/4] net: skbuff: limit folio refcount when copying skb frag references David Hildenbrand (Arm)
@ 2026-11-19 13:53 ` David Hildenbrand (Arm)
2026-11-20 16:21 ` Stanislav Fomichev
2026-11-22 9:19 ` netdev-bot+sashiko
2 siblings, 0 replies; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-19 13:53 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Andrew Morton, Kiryl Shutsemau, Song Liu,
Johannes Weiner, Rik van Riel, Matthew Wilcox (Oracle),
Neal Cardwell, Kuniyuki Iwashima, Pedro Falcato
Cc: netdev, linux-kernel, linux-mm, stable
> *
> * 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
To avoid a html docs warning we might have to drop the spaces for indenting this
line.
--
Cheers,
David
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH net 1/4] net: skbuff: limit folio refcount when copying skb frag references
2026-11-18 8:32 ` [PATCH net 1/4] net: skbuff: limit folio refcount when copying skb frag references David Hildenbrand (Arm)
2026-11-19 13:53 ` David Hildenbrand (Arm)
@ 2026-11-20 16:21 ` Stanislav Fomichev
2026-11-20 21:01 ` David Hildenbrand (Arm)
2026-11-22 9:19 ` netdev-bot+sashiko
2 siblings, 1 reply; 12+ messages in thread
From: Stanislav Fomichev @ 2026-11-20 16:21 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Andrew Morton, Kiryl Shutsemau, Song Liu,
Johannes Weiner, Rik van Riel, Matthew Wilcox (Oracle),
Neal Cardwell, Kuniyuki Iwashima, Pedro Falcato, netdev,
linux-kernel, linux-mm, stable
On 11/18, David Hildenbrand (Arm) wrote:
> 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));
> +}
I don't see you removing original skb_frag_ref implementations after
the conversion, any specific reason? Do we still have any skb_frag_ref
callers left? Maybe at least you can make skb_frag_ref do 'return
(void)skb_frag_try_ref' to avoid duplicate code?
(optional) Also, an easier way to migrate might be to make the existing
skb_frag_ref return bool, convert everything, and then add __must_check.
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [PATCH net 1/4] net: skbuff: limit folio refcount when copying skb frag references
2026-11-20 16:21 ` Stanislav Fomichev
@ 2026-11-20 21:01 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-20 21:01 UTC (permalink / raw)
To: Stanislav Fomichev
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Andrew Morton, Kiryl Shutsemau, Song Liu,
Johannes Weiner, Rik van Riel, Matthew Wilcox (Oracle),
Neal Cardwell, Kuniyuki Iwashima, Pedro Falcato, netdev,
linux-kernel, linux-mm, stable
On 11/20/26 17:21, Stanislav Fomichev wrote:
> On 11/18, David Hildenbrand (Arm) wrote:
>> 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));
>> +}
>
Hi,
thanks for taking a look!
> I don't see you removing original skb_frag_ref implementations after
> the conversion, any specific reason?
As explained in the cover letter, I want to get these fixes here upstream asap,
to backport them alongside the core fixes that already landed.
The focus is on the skb users that allow for multiplying folio references easily.
> Do we still have any skb_frag_ref
> callers left?
Yes, after this series there are still some left. Primarily ones that are not
considered harmful for overflowing the refcount. And some of these require a bit
more work to convert, like skb_split().
> Maybe at least you can make skb_frag_ref do 'return
> (void)skb_frag_try_ref' to avoid duplicate code?
That would be bad: calling code would assume that it obtained a reference but it
actually didn't.
There is some headroom above the folio refcount limit to allow for such harmless
users that are not converted yet. The immediate problems are the users that
allow for unbounded folio references.
>
> (optional) Also, an easier way to migrate might be to make the existing
> skb_frag_ref return bool, convert everything, and then add __must_check.
That would require teaching all callers about handling errors in one go, because
callers would otherwise silently ignore failures. That's nothing we can
backport, really.
I agree that as a follow-up, all code should be converted to use a single
function that can fail.
But that's nothing for this series that focuses on fixing folio refcounts in a
way that can be backported.
Thanks!
--
Cheers,
David
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH net 1/4] net: skbuff: limit folio refcount when copying skb frag references
2026-11-18 8:32 ` [PATCH net 1/4] net: skbuff: limit folio refcount when copying skb frag references David Hildenbrand (Arm)
2026-11-19 13:53 ` David Hildenbrand (Arm)
2026-11-20 16:21 ` Stanislav Fomichev
@ 2026-11-22 9:19 ` netdev-bot+sashiko
2 siblings, 0 replies; 12+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22 9:19 UTC (permalink / raw)
To: david
Cc: davem, edumazet, kuba, pabeni, horms, akpm, kas, songliubraving,
hannes, riel, willy, ncardwell, kuniyu, pfalcato, netdev,
linux-kernel, linux-mm, stable
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
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] skb_frag_try_ref() (include/linux/skbuff_ref.h) sends every frag
page that is neither page_pool nor net_iov through try_get_page() ->…
- [Low] The patch is tagged Cc: stable with Fixes tags going back to v4.8
(800d8c63b2e9).
--
net: skbuff: limit folio refcount when copying skb frag references
This patch adds skb_frag_try_ref() and skb_frags_try_ref(). They take
non-page_pool frag page references through try_get_page(), so the folio
refcount limit is enforced. pskb_expand_head(), __pskb_copy_fclone(),
skb_zerocopy() and pskb_carve_inside_*() now use them and fail when a
reference is refused.
> 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.
[Severity: Low]
This isn't a bug in mainline, but the refusal described here only works
because try_get_page() enforces the refcount limit. That check was added
by commit c6c9a4c1c9a1 ("mm: limit pagecache folio refcount when splicing
into pipes"), which builds on 26b8f029ce52 ("mm: limit pagecache folio
refcount when mapping pages").
Before those commits, try_get_page() had no upper bound:
page = compound_head(page);
if (WARN_ON_ONCE(page_ref_count(page) <= 0))
return false;
page_ref_inc(page);
return true;
This patch has Cc: stable and Fixes tags back to 800d8c63b2e9. A backport
to a tree without the prerequisites would still compile. There,
skb_frag_try_ref() would never refuse a reference and would only warn
after the refcount has wrapped.
Could the commit message state this dependency, for example with a
"# depends on" note on the stable tag?
> + *
> + * 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));
[Severity: High]
Can this make the converted paths fail on every packet for drivers that
deliberately keep RX page refcounts near INT_MAX?
Every frag that is neither page_pool nor net_iov now goes through
try_get_page()->folio_get_checked()->__folio_ref_count_over_limit(). That
check applies to any folio, not just pagecache folios.
The gve driver uses GQI RDA/QPL and DQO-QPL, and none of these use
page_pool. It biases its RX pages up to INT_MAX:
drivers/net/ethernet/google/gve/gve_rx.c:gve_setup_rx_buffer() {
...
/* The page already has 1 ref */
page_ref_add(page, INT_MAX - 1);
page_info->pagecnt_bias = INT_MAX;
}
The qpl_copy_pool setup in gve_rx.c and gve_alloc_qpl_page_dqo() do the
same thing.
Handing a frag to the stack only decrements the private bias. When the
bias runs out, the page count is raised back to INT_MAX:
drivers/net/ethernet/google/gve/gve_utils.c:gve_dec_pagecnt_bias() {
page_info->pagecnt_bias--;
if (page_info->pagecnt_bias == 0) {
int pagecount = page_count(page_info->page);
...
page_ref_add(page_info->page, INT_MAX - pagecount);
}
}
So for roughly the first 33M frees of each ring page, page_count() stays
at or above FOLIO_REF_COUNT_ADMIN_LIMIT. For roughly the first 67M it
stays at or above FOLIO_REF_COUNT_LIMIT.
In that range folio_ref_count_over_limit_slowpath() refuses the reference:
if (ref_count >= FOLIO_REF_COUNT_ADMIN_LIMIT)
goto over_limit;
...
if (!in_task())
goto over_limit;
It refuses unconditionally above the admin limit, and always in softirq.
Each refusal also prints the ratelimited "reached refcount limit" warning.
If that's right, these paths would fail every time for gve RX skbs that
carry page frags, i.e. packets larger than rx_copybreak:
nfqnl_build_packet_message()->skb_zerocopy()
(NFQUEUE copy-packet mode, the packet is dropped)
queue_userspace_packet()->skb_zerocopy()
(OVS upcall fails)
___pskb_trim()->pskb_expand_head() on a cloned skb
(AF_PACKET with snaplen < len)
skb_unclone()/skb_cow()->pskb_expand_head() after a tap cloned the skb
__pskb_copy_fclone()
(pskb_copy users such as nft dup)
pskb_carve_inside_header()/pskb_carve_inside_nonlinear()
Before this patch these paths only failed when a GFP_ATOMIC allocation
failed, because get_page() kept incrementing without a check.
Later patches in the series apply the same check to skb_segment(),
skb_try_coalesce(), skb_shift(), tcp_clone_payload() and spd_fill_page().
That would also affect software GSO of forwarded GRO packets. splice()
from TCP and AF_UNIX sockets would return -EFAULT on gve-received data.
The kernel-doc for __folio_ref_count_over_limit() in include/linux/mm.h
says "Checking other folios does no harm.". That doesn't seem to hold for
drivers that bias refcounts this way.
There is also a more speculative issue. folio_get_checked() has an
unconditional WARN_ON_ONCE(ref_count <= 0). If any remaining unchecked
get_page() wraps such a page to INT_MIN, a later skb_frag_try_ref() on it
would fire a real WARN. One example is iptfs_skb_add_frags() in
xfrm_iptfs on RX outer-packet frags. Under panic_on_warn, a remote sender
could trigger that WARN.
Since this is Cc: stable, would it be better to restrict the check to
pagecache folios in these net paths? Alternatively, gve could stop biasing
to INT_MAX, with that change backported as a prerequisite.
> +}
> +
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118-skb_refcount_overflow-v1-0-5551b4954c3f%40kernel.org
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH net 2/4] net: skbuff: limit folio refcount when rearranging skb frags
2026-11-18 8:32 [PATCH net 0/4] net: skbuff: limit pagecache folio refcount David Hildenbrand (Arm)
2026-11-18 8:32 ` [PATCH net 1/4] net: skbuff: limit folio refcount when copying skb frag references David Hildenbrand (Arm)
@ 2026-11-18 8:32 ` David Hildenbrand (Arm)
2026-11-22 9:19 ` netdev-bot+sashiko
2026-11-18 8:32 ` [PATCH net 3/4] net: skbuff: limit folio refcount when splicing pages into skbs David Hildenbrand (Arm)
2026-11-18 8:32 ` [PATCH net 4/4] net: skbuff: limit folio refcount when splicing from sockets David Hildenbrand (Arm)
3 siblings, 1 reply; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-18 8:32 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Andrew Morton, Kiryl Shutsemau, Song Liu,
Johannes Weiner, Rik van Riel, Matthew Wilcox (Oracle),
Neal Cardwell, Kuniyuki Iwashima, Pedro Falcato
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 447099b39deeb..bed1c8c4c6b99 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.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH net 2/4] net: skbuff: limit folio refcount when rearranging skb frags
2026-11-18 8:32 ` [PATCH net 2/4] net: skbuff: limit folio refcount when rearranging skb frags David Hildenbrand (Arm)
@ 2026-11-22 9:19 ` netdev-bot+sashiko
0 siblings, 0 replies; 12+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22 9:19 UTC (permalink / raw)
To: david
Cc: davem, edumazet, kuba, pabeni, horms, akpm, kas, songliubraving,
hannes, riel, willy, ncardwell, kuniyu, pfalcato, 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] The commit message explains why skb_split() keeps its
unconditional skb_frag_ref() by saying that "each split also allocates…
--
net: skbuff: limit folio refcount when rearranging skb frags
This patch replaces unconditional skb_frag_ref() calls with
skb_frag_try_ref() and skb_frags_try_ref() in skb_segment(),
skb_try_coalesce(), skb_shift() and tcp_clone_payload(). The goal is to
stop skbs from multiplying folio references until the folio refcount limit
is exceeded. skb_split() is deliberately left alone.
> 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.
[Severity: Low]
Is this reasoning true for all skb_split() callers?
Socket charging and dropping the references on ACK both describe the TCP
callers, tcp_fragment() and tso_fragment() in net/ipv4/tcp_output.c.
skb_split() also has a non-TCP caller in batadv_frag_create() in
net/batman-adv/fragmentation.c:
net/batman-adv/fragmentation.c:batadv_frag_create() {
...
skb_fragment = dev_alloc_skb(ll_reserved + mtu + tailroom);
...
skb_split(skb, skb_fragment, skb->len - fragment_size);
...
}
The fragment skb there comes from dev_alloc_skb(), so it is not charged to
any socket. The data is not acked either.
The path is batadv_frag_send_packet()->batadv_frag_create()->skb_split()->
skb_split_no_header(). skb_split_no_header() still calls
skb_frag_ref(skb1, k) unconditionally whenever the split point falls inside
a frag.
The KUnit tests in net/core/net_test.c also call skb_split() on skbs that
have no socket.
The conclusion probably still holds for batman-adv. A packet is limited to
BATADV_FRAG_MAX_FRAGMENTS fragments, and the fragments are transmitted and
freed. Only the stated reason is TCP-specific.
Could the commit message say that the socket memory argument covers only
the TCP callers, and mention why the batman-adv path is also bounded?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118-skb_refcount_overflow-v1-0-5551b4954c3f%40kernel.org
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH net 3/4] net: skbuff: limit folio refcount when splicing pages into skbs
2026-11-18 8:32 [PATCH net 0/4] net: skbuff: limit pagecache folio refcount David Hildenbrand (Arm)
2026-11-18 8:32 ` [PATCH net 1/4] net: skbuff: limit folio refcount when copying skb frag references David Hildenbrand (Arm)
2026-11-18 8:32 ` [PATCH net 2/4] net: skbuff: limit folio refcount when rearranging skb frags David Hildenbrand (Arm)
@ 2026-11-18 8:32 ` David Hildenbrand (Arm)
2026-11-22 9:19 ` netdev-bot+sashiko
2026-11-18 8:32 ` [PATCH net 4/4] net: skbuff: limit folio refcount when splicing from sockets David Hildenbrand (Arm)
3 siblings, 1 reply; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-18 8:32 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Andrew Morton, Kiryl Shutsemau, Song Liu,
Johannes Weiner, Rik van Riel, Matthew Wilcox (Oracle),
Neal Cardwell, Kuniyuki Iwashima, Pedro Falcato
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.
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 bed1c8c4c6b99..c937d74eed6ac 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.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH net 3/4] net: skbuff: limit folio refcount when splicing pages into skbs
2026-11-18 8:32 ` [PATCH net 3/4] net: skbuff: limit folio refcount when splicing pages into skbs David Hildenbrand (Arm)
@ 2026-11-22 9:19 ` netdev-bot+sashiko
0 siblings, 0 replies; 12+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22 9:19 UTC (permalink / raw)
To: david
Cc: davem, edumazet, kuba, pabeni, horms, akpm, kas, songliubraving,
hannes, riel, willy, ncardwell, kuniyu, pfalcato, netdev,
linux-kernel, linux-mm, stable
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · 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 fix leaves the kTLS MSG_SPLICE_PAGES paths unchecked.
- [Medium] The new -EFAULT from skb_append_pagefrags() can turn a
refcount near the limit into a persistent loop of NFS connection…
--
net: skbuff: limit folio refcount when splicing pages into skbs
The patch changes skb_append_pagefrags() to take its page reference with
try_get_page() instead of get_page(). If the folio refcount limit has been
reached it returns -EFAULT, so in-kernel MSG_SPLICE_PAGES users such as
sunrpc can no longer push pagecache folios past the limit.
> diff --git a/net/core/skbuff.c b/net/core/skbuff.c
> index bed1c8c4c6b99..c937d74eed6ac 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;
[Severity: High]
Does this cover all of the in-kernel MSG_SPLICE_PAGES users that the commit
message is aimed at? Both kTLS sendmsg paths handle MSG_SPLICE_PAGES without
ever calling skb_append_pagefrags(). They still take an unchecked reference
on every spliced page.
For software kTLS the path is
tls_sw_sendmsg_locked()->tls_sw_sendmsg_splice()->sk_msg_page_add():
include/linux/skmsg.h:sk_msg_page_add() {
...
get_page(page);
sge = sk_msg_elem(msg, msg->sg.end);
sg_set_page(sge, page, len, offset);
...
}
The only check in tls_sw_sendmsg_splice() is this one, and it does not look
at the refcount limit:
if (WARN_ON_ONCE(!sendpage_ok(page))) {
For device offload kTLS the path is tls_push_data()->tls_append_frag():
net/tls/tls_device.c:tls_append_frag() {
...
++record->num_frags;
get_page(pfrag->page);
...
}
These references stay in offload_ctx->records_list until
tls_tcp_clean_acked() runs, so they last until the peer ACKs.
The sunrpc example from the commit message appears to take this route.
svc_tcp_sendmsg() sets MSG_SPLICE_PAGES and calls sock_sendmsg(). On
XPT_TLS_SESSION transports, set up by
svc_tcp_handshake()->tls_server_hello_x509(), that socket is a kTLS socket.
So nfsd pagecache pages would reach sk_msg_page_add() or tls_append_frag()
and never skb_append_pagefrags().
No later patch in the series touches net/tls/ or include/linux/skmsg.h,
including "net: skbuff: limit folio refcount when splicing from sockets".
Should the spliced-page references in sk_msg_page_add() and
tls_append_frag() also go through try_get_page()?
[Severity: Medium]
Can this new -EFAULT cause nfsd connections to be torn down over and over?
nfsd threads are kthreads, and folio_ref_count_over_limit_slowpath()
refuses them at FOLIO_REF_COUNT_LIMIT, with no admin headroom:
/* Background threads must not consume admin reserves. */
if (current->flags & PF_KTHREAD)
goto over_limit;
On the read side the limit is checked only once, when
filemap_splice_read()->splice_folio_into_pipe() calls folio_get_checked().
After that, nfsd_splice_actor()->svc_rqst_replace_page() takes an unchecked
get_page() for rq_respages, and the pipe reference is dropped. With this
patch, sending the reply needs a second, checked reference per page here.
Let k be roughly the number of pages from the folio in the reply. Suppose a
local user holds the folio refcount between FOLIO_REF_COUNT_LIMIT - k and
FOLIO_REF_COUNT_LIMIT. The read stage succeeds and fills the refcount up to
the limit. The send then fails:
skb_append_pagefrags()
try_get_page() fails, returns -EFAULT
skb_splice_from_iter()
iov_iter_revert(), returns -EFAULT or a short count
tcp_sendmsg_locked()
returns a short count
svc_tcp_sendto()
if (sent < 0 || sent != (xdr->len + sizeof(marker)))
goto out_close;
...
svc_xprt_deferred_close(xprt);
This closes the connection together with every other RPC in flight on it.
The client reconnects and retransmits the same READ. Nothing clears the
condition, so the cycle repeats for as long as the refcount stays in that
window.
Mappings are checked against the same limit. An unprivileged user can
therefore put the count in that window by mapping until refused and then
unmapping a few pages.
Before this patch the send succeeded. At or above FOLIO_REF_COUNT_LIMIT, the
pipe-stage check makes nfsd return an NFS read error rather than close the
connection.
The commit message says:
The only caller, skb_splice_from_iter(), handles that.
For sunrpc, though, the error ends in connection teardown, and nothing falls
back to copying the data into frags. Would a copy fallback be better here,
or is the teardown intended?
> skb_zcopy_downgrade_managed(skb);
> - get_page(page);
> skb_fill_page_desc_noacc(skb, i, page, offset, size);
> } else {
> return -EMSGSIZE;
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118-skb_refcount_overflow-v1-0-5551b4954c3f%40kernel.org
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH net 4/4] net: skbuff: limit folio refcount when splicing from sockets
2026-11-18 8:32 [PATCH net 0/4] net: skbuff: limit pagecache folio refcount David Hildenbrand (Arm)
` (2 preceding siblings ...)
2026-11-18 8:32 ` [PATCH net 3/4] net: skbuff: limit folio refcount when splicing pages into skbs David Hildenbrand (Arm)
@ 2026-11-18 8:32 ` David Hildenbrand (Arm)
2026-11-22 9:19 ` netdev-bot+sashiko
3 siblings, 1 reply; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-18 8:32 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Andrew Morton, Kiryl Shutsemau, Song Liu,
Johannes Weiner, Rik van Riel, Matthew Wilcox (Oracle),
Neal Cardwell, Kuniyuki Iwashima, Pedro Falcato
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, let spd_fill_page(),
__splice_segment() and __skb_splice_bits() return an int instead of a
bool. Keep returning true/false instead of 1/0 to reduce the churn in
this fix.
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
Acked-by: Pedro Falcato <pfalcato@suse.de>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
net/core/skbuff.c | 90 +++++++++++++++++++++++++++++++------------------------
1 file changed, 51 insertions(+), 39 deletions(-)
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index c937d74eed6ac..b73fef7a1b460 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -3162,11 +3162,13 @@ 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. Returns non-zero to stop, or a negative error
+ * if we cannot 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)
+static int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
+ unsigned int *len, unsigned int offset, bool linear,
+ struct sock *sk)
{
if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
return true;
@@ -3180,7 +3182,8 @@ 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))
+ return -EFAULT;
spd->pages[spd->nr_pages] = page;
spd->partial[spd->nr_pages].len = *len;
spd->partial[spd->nr_pages].offset = offset;
@@ -3189,12 +3192,14 @@ static bool spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
return false;
}
-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)
+static int __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)
{
+ int ret;
+
if (!*len)
return true;
@@ -3212,8 +3217,9 @@ 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))
- return true;
+ ret = spd_fill_page(spd, page, &flen, poff, linear, sk);
+ if (ret)
+ return ret;
poff += flen;
plen -= flen;
*len -= flen;
@@ -3225,28 +3231,30 @@ 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.
+ * Map linear and fragment data from the skb to spd. It returns non-zero to
+ * stop: true if the pipe is full or if we already spliced the requested
+ * length, or a negative error if we cannot take a page reference.
*/
-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)
+static int __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 sk_buff *iter;
- int seg;
+ int seg, ret;
/* map the linear part :
* If skb->head_frag is set, this 'linear' part is backed by a
* fragment, and if the head is not shared with any clones then
* we can avoid a copy since we own the head portion of this page.
*/
- if (__splice_segment(virt_to_page(skb->data),
- (unsigned long) skb->data & (PAGE_SIZE - 1),
- skb_headlen(skb),
- offset, len, spd,
- skb_head_is_locked(skb),
- sk))
- return true;
+ ret = __splice_segment(virt_to_page(skb->data),
+ (unsigned long)skb->data & (PAGE_SIZE - 1),
+ skb_headlen(skb),
+ offset, len, spd,
+ skb_head_is_locked(skb),
+ sk);
+ if (ret)
+ return ret;
/*
* then map the fragments
@@ -3260,10 +3268,11 @@ static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
if (WARN_ON_ONCE(!skb_frag_page(f)))
return false;
- if (__splice_segment(skb_frag_page(f),
- skb_frag_off(f), skb_frag_size(f),
- offset, len, spd, false, sk))
- return true;
+ ret = __splice_segment(skb_frag_page(f),
+ skb_frag_off(f), skb_frag_size(f),
+ offset, len, spd, false, sk);
+ if (ret)
+ return ret;
}
skb_walk_frags(skb, iter) {
@@ -3272,11 +3281,12 @@ 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))
- return true;
+ ret = __skb_splice_bits(iter, pipe, offset, len, spd, sk);
+ if (ret)
+ return ret;
}
return false;
@@ -3284,7 +3294,9 @@ 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 we could not splice anything because we could not take
+ * a page reference.
*/
int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
struct pipe_inode_info *pipe, unsigned int tlen,
@@ -3299,14 +3311,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;
- __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
+ ret = __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
if (spd.nr_pages)
- ret = splice_to_pipe(pipe, &spd);
-
- return ret;
+ return splice_to_pipe(pipe, &spd);
+ /* Returning 0 would make callers report EOF. */
+ return ret < 0 ? ret : 0;
}
EXPORT_SYMBOL_GPL(skb_splice_bits);
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH net 4/4] net: skbuff: limit folio refcount when splicing from sockets
2026-11-18 8:32 ` [PATCH net 4/4] net: skbuff: limit folio refcount when splicing from sockets David Hildenbrand (Arm)
@ 2026-11-22 9:19 ` netdev-bot+sashiko
0 siblings, 0 replies; 12+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22 9:19 UTC (permalink / raw)
To: david
Cc: davem, edumazet, kuba, pabeni, horms, akpm, kas, songliubraving,
hannes, riel, willy, ncardwell, kuniyu, pfalcato, 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] The new comment above skb_splice_bits()
(net/core/skbuff.c:3295-3300) says the function "Returns the number of…
Pre-existing issues:
- [Medium] skb_splice_bits() still returns 0, which callers report as
EOF, when nothing was spliced because linear_to_page() failed to…
--
net: skbuff: limit folio refcount when splicing from sockets
This patch uses try_get_page() in spd_fill_page() so that splicing from
sockets into pipes cannot push a folio refcount past its limit. On
failure it returns -EFAULT, and the error is propagated as an int
through __splice_segment() and __skb_splice_bits(). skb_splice_bits()
returns the error when nothing was spliced, so the callers don't report
EOF.
> diff --git a/net/core/skbuff.c b/net/core/skbuff.c
> index c937d74eed6ac..b73fef7a1b460 100644
> --- a/net/core/skbuff.c
> +++ b/net/core/skbuff.c
[ ... ]
> @@ -3284,7 +3294,9 @@ 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 we could not splice anything because we could not take
> + * a page reference.
> */
[Severity: Low]
This isn't a bug, but is this return description complete?
When spd.nr_pages is non-zero, skb_splice_bits() returns whatever
splice_to_pipe() returns. splice_to_pipe() in fs/splice.c can fail for
reasons that have nothing to do with page references:
if (unlikely(!pipe->readers)) {
send_sig(SIGPIPE, current, 0);
ret = -EPIPE;
goto out;
}
...
if (!ret)
ret = -EAGAIN;
The reverse case also exists. If linear_to_page() fails in
spd_fill_page(), nothing gets spliced, yet skb_splice_bits() returns 0
and not an error.
Could the comment also mention the -EPIPE and -EAGAIN cases that come
from splice_to_pipe()?
> int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
> struct pipe_inode_info *pipe, unsigned int tlen,
> @@ -3299,14 +3311,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;
>
> - __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
> + ret = __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
>
> if (spd.nr_pages)
> - ret = splice_to_pipe(pipe, &spd);
> -
> - return ret;
> + return splice_to_pipe(pipe, &spd);
> + /* Returning 0 would make callers report EOF. */
> + return ret < 0 ? ret : 0;
[Severity: Medium]
This is a pre-existing issue, not one introduced by this patch. Can the
spurious EOF that this new comment describes still happen when
linear_to_page() fails?
When skb_head_is_locked() is true (a kmalloc'ed head or a cloned skb),
spd_fill_page() goes through linear_to_page(). An allocation failure
there returns a positive value, not a negative errno:
net/core/skbuff.c:linear_to_page() {
...
if (!sk_page_frag_refill(sk, pfrag))
return NULL;
...
}
net/core/skbuff.c:spd_fill_page() {
...
if (linear) {
page = linear_to_page(page, len, &offset, sk);
if (!page)
return true;
}
...
}
That 1 is passed up through __splice_segment() and __skb_splice_bits().
With spd.nr_pages == 0, skb_splice_bits() then returns 0 here.
The callers treat the 0 as end of stream even though data is still
queued:
- unix_stream_read_generic() adds a chunk of 0, breaks because
unix_skb_len(skb) is non-zero, and returns 0.
- tcp_splice_read() breaks with ret 0 although the receive queue is
not empty.
- kcm_splice_read() and tls_sw_splice_read() return 0 directly.
The old code also returned 0 in this case. Hitting it needs severe
memory pressure, where sk_page_frag_refill() fails both the high-order
and the order-0 allocation.
Would it make sense for spd_fill_page() to return a negative errno such
as -ENOMEM when linear_to_page() fails? That would send it down the same
error path as the try_get_page() failure.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118-skb_refcount_overflow-v1-0-5551b4954c3f%40kernel.org
^ permalink raw reply [flat|nested] 12+ messages in thread