net: skbuff: limit pagecache folio refcount
Subsystems (5)
v2 cover net: skbuff: limit pagecache folio refcount
Hi,
This is v2 of the remaining folio refcount overflow fixes that should go through the net tree, ideally soon. They address the known cases where networking can still overflow the folio refcount by taking unbounded references.
The series is based on Linus' tree, which includes the MM prerequisite c6c9a4c1c9a16 ("mm: limit pagecache folio refcount when splicing into pipes") that adds the refcount limit checks to try_get_page().
More details in [1], but in essence:
There are ways to overflow the folio refcount and cause trouble either by letting it go negative or letting it go all the way back to 0.
This is particularly easy with large pagecache folios, where page table mappings can "easily" create many references. The MM changes limit references from page tables, GUP and pipes, and leave some headroom for races, other bounded refcount users etc.
However, skbs can easily multiply folio references, so any such skb path that can take unbounded folio references must also obey the new limits: we must use try_get_page() to obtain an additional folio reference and handle failures accordingly.
Patch #1 introduces skb_frag_try_ref() and skb_frags_try_ref() and uses them when copying skb frag references. Patch #2 covers rearranging frags. Patches #3 and #4 cover splicing pages into skbs and splicing data from sockets into pipes.
We use existing failure paths where possible. Socket splicing returns the bytes already spliced, or -EFAULT if no data could be spliced because try_get_page() failed.
Net paths that take folio references that I don't touch because the references are either bounded or the paths are not relevant in practice:
- skb_split(): each reference needs a new socket-charged skb.
- IP-TFS reassembly: each reference needs a new inner skb.
- io_uring zerocopy send: each piece is charged >= 4 KiB of TCP memory.
- kTLS software splicing: only transfers references from the pipe.
- kTLS device offload: pushing the record fails in skb_append_pagefrags() (patch #3), so a socket can exceed the limit by about one record.
- AF_XDP zerocopy TX, nfsd splice reads, sockmap / sk_msg, espintcp:
bounded, or require privileges / special setups.
- Other driver-specific paths: not looked into for now, as I can neither test them nor see if they actually trigger unbounded references.
- page_pool and net_iov frags: not pagecache pages.
The original refcount overflow problem was reported by Anthropic; we knew that it was possible in theory, but didn't expect to have easy ways to trigger it. After fixing the splice issue, I used AI to identify other unbounded folio refcount users, and it flagged various skb-related paths. I made it actually reproduce unbounded references. These patches address the paths that were actually reproduced.
I ran tests before and after applying the changes, using a kernel with reduced folio refcount limits. These tests focused on exercising the new error paths and checking that the refcount limits were respected.
These tests covered AF_PACKET, nft dup, NFQUEUE, software GSO segmentation, TCP receive coalescing with dropped ACKs, and socket splicing.
I also ran the following networking selftests successfully:
- net:tls
- net:udp_splice_checksum
- net:udpgso.sh
- net:msg_zerocopy.sh
If there are any further tests I can run, please let me know; help with testing would be appreciated as well, as I only have access to limited hardware and have limited net experience in general.
[1] https://lore.kernel.org/linux-mm/[email protected]/
---
Changes in v2:
- "net: skbuff: limit folio refcount when copying skb frag references"
- Fix skb_zerocopy() kernel-doc indentation
- "net: skbuff: limit folio refcount when splicing pages into skbs"
- Mention nfsd connection teardown in the description
- "net: skbuff: limit folio refcount when splicing from sockets"
- Propagate the error through an "err" pointer
- Document the skb_splice_bits() return value
- Update the description accordingly
- Drop Pedro's Ack
- Extend the cover letter
- The GVE issue sashiko reported has already been fixed in commit
3946899dbeac ("gve: fix RX page refcount overflow from INT_MAX
pagecnt_bias").
- Link to v1: https://patch.msgid.link/[email protected]
---
David Hildenbrand (Arm) (4):
net: skbuff: limit folio refcount when copying skb frag references
net: skbuff: limit folio refcount when rearranging skb frags
net: skbuff: limit folio refcount when splicing pages into skbs
net: skbuff: limit folio refcount when splicing from sockets
include/linux/skbuff_ref.h | 53 ++++++++++++++++++++ net/core/skbuff.c | 117 +++++++++++++++++++++++++++++---------------- net/ipv4/tcp_output.c | 8 ++-- 3 files changed, 134 insertions(+), 44 deletions(-)
---
base-commit: 63fdb511c27afcebce797e3db580b3534ddfa639
change-id: 20261117-skb_refcount_overflow-75a0034e469e
Best regards,
Signature · 1 line
David Hildenbrand (Arm) <[email protected]>
PATCH v2 1/4 net: skbuff: limit folio refcount when copying skb frag references
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.
--- include/linux/skbuff_ref.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++ net/core/skbuff.c | 35 +++++++++++++++++++----------- 2 files changed, 76 insertions(+), 12 deletions(-)
include/linux/skbuff_ref.h +53 -0
@@ -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 */
net/core/skbuff.c +23 -12
@@ -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;
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH net v2 1/4] net: skbuff: limit folio refcount when copying skb frag references
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…
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.
Quoted 4 lines
Let's introduce
skb_frag_try_ref()andskb_frags_try_ref(), which usetry_get_page(), and use them inpskb_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?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126-skb_refcount_overflow-v2-0-a6f600a5634a%40kernel.org
PATCH v2 2/4 net: skbuff: limit folio refcount when rearranging skb frags
In reply to David Hildenbrand (Arm)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.
--- net/core/skbuff.c | 32 +++++++++++++++++++++----------- net/ipv4/tcp_output.c | 8 +++++--- 2 files changed, 26 insertions(+), 14 deletions(-)
net/core/skbuff.c +21 -11
@@ -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;
net/ipv4/tcp_output.c +5 -3
@@ -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;
Signature · 1 line
2.56.1
PATCH v2 3/4 net: skbuff: limit folio refcount when splicing pages into skbs
In reply to David Hildenbrand (Arm)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.
--- net/core/skbuff.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
net/core/skbuff.c +9 -3
@@ -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)
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH net v2 3/4] net: skbuff: limit folio refcount when splicing pages into skbs
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
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.
Quoted 9 lines
net/core/skbuff.c +1
-0@@ -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) { +/*
net/core/skbuff.c int skb_append_pagefrags(struct sk_buff *skb, struct page *page, · line 4726
Quoted 10 lines
net/core/skbuff.c +5
-1+* 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.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126-skb_refcount_overflow-v2-0-a6f600a5634a%40kernel.org
PATCH v2 4/4 net: skbuff: limit folio refcount when splicing from sockets
In reply to David Hildenbrand (Arm)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().
--- net/core/skbuff.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-)
net/core/skbuff.c +23 -15
@@ -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; }
Signature · 1 line
2.56.1