Re: [PATCH net 4/4] net: skbuff: limit folio refcount when splicing from sockets
From: David Hildenbrand (Arm)
Date: Thu Nov 26 2026 - 09:44:34 EST
On 11/26/26 15:18, Paolo Abeni wrote:
> On 11/18/26 09:32, David Hildenbrand (Arm) wrote:
>> @@ -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;
>
Thanks for having a look.
> AFAICS the above introduces a change of behavior:
> caller with tlen==0 get 1 as return value, while they
> used to get EOF. I think the return code from
> `__skb_splice_bits` should be ignored, when not negative.
I think you are right.
>
> I think the churn here could be reduced re-using the `len`
> ptr argument to store the error code; something alike the
> (completely untested) following:
>
> ---
> diff --git a/net/core/skbuff.c b/net/core/skbuff.c
> index 85d5caa08887..9fd5116f33f3 100644
> --- a/net/core/skbuff.c
> +++ b/net/core/skbuff.c
> @@ -3177,6 +3177,10 @@ static bool spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
> spd->partial[spd->nr_pages - 1].len += *len;
> return false;
> }
> + if (!try_get_page(page)) {
> + *len = -EFAULT;
> + return true;
> + }
> get_page(page);
Double get, easily fixed.
> spd->pages[spd->nr_pages] = page;
> spd->partial[spd->nr_pages].len = *len;
> @@ -3299,6 +3303,8 @@ int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
> int ret = 0;
>
> __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
> + if (tlen < 0)
> + return tlen;
>
> if (spd.nr_pages)
> ret = splice_to_pipe(pipe, &spd);
> ---
>
> which is arguably ugly but IMHO no more than the tristate
> return value in the current version.
That doesn't work. "len" you write in the function is not tlen ...
__splice_segment() passes &flen.
Further all these lengths are unsigned int, not signed :/
Also, I think we have to respect spd.nr_pages.
If we want it minimal, a separate error pointer might be the easiest way to fix it. Still
ugly, but I tried to get it clean above and there is no way around some churn I'm afraid.
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index c937d74eed6ac..f471bea7fb26e 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -3166,7 +3166,7 @@ static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
*/
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 +3180,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 +3196,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 +3215,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;
@@ -3230,7 +3233,8 @@ static bool __splice_segment(struct page *page, unsigned int poff,
*/
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 +3249,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 +3266,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 +3276,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;
}
@@ -3299,9 +3303,11 @@ 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 (err && !spd.nr_pages)
+ return err;
if (spd.nr_pages)
ret = splice_to_pipe(pipe, &spd);
--
2.56.1
--
Cheers,
David