tcp: fix negative sk_forward_alloc after MSG_ZEROCOPY append
Subsystems (3)
PATCH 1/1 tcp: fix negative sk_forward_alloc after MSG_ZEROCOPY append
Our local syzbot reported the following warning:
WARNING: ./include/linux/ucopysize.h:57 at tcp_sendmsg_locked+0x1e13/0x1f20 Modules linked in: CPU: 3 UID: 0 PID: 377 Comm: syz.2.2248 Not tainted 7.4.0-rc2+ #398 PREEMPT RIP: 0010:tcp_sendmsg_locked+0x1e13/0x1f20 Call Trace: <TASK> tcp_sendmsg+0x2c/0x50 inet_sendmsg+0x62/0xb0 __sys_sendto+0x2fe/0x350 __x64_sys_sendto+0x7f/0xb0 x64_sys_call+0x1375/0x27e0 do_syscall_64+0xe4/0x470 entry_SYSCALL_64_after_hwframe+0x76/0x7e </TASK>
The problem is here:
if (!skb_zcopy_pure(skb)) {
copy = tcp_wmem_schedule(sk, copy);
if (!copy)
goto wait_for_space;
}When MSG_ZEROCOPY data is appended to a skb that already holds copied data, only the user length is scheduled, but the pinned pages are then charged by their truesize:
skb_zerocopy_iter_stream
__zerocopy_sg_from_iter
zerocopy_fill_skb_from_iter //truesize += PAGE_ALIGN(copied + start) sk_mem_charge(sk, truesize) //sk_forward_alloc goes negative
For example, sending 8 iovecs of 1 byte each schedules 8 bytes and charges 32768 with 4K pages.
On a later sendmsg() without MSG_ZEROCOPY, if sk_wmem_schedule() fails (e.g. tcp_mem hard limit is reached), tcp_wmem_schedule() returns min(copy, sk->sk_forward_alloc), which is negative. The negative copy is passed to skb_copy_to_page_nocache() and triggers the warning, and sendmsg() fails with -EFAULT.
A few options were considered:
1) Schedule the truesize up front. It is not known before the pages are pinned, it depends on the iovec layout.
2) Do not append zerocopy data to a non pure skb. This splits a small header and its zerocopy payload into two skbs.
3) Schedule the missing part right after the pages are charged. They are already in the skb at this point, so this can not fail.
Go with 3).
--- net/ipv4/tcp.c | 2 ++ 1 file changed, 2 insertions(+)
net/ipv4/tcp.c +2 -0
@@ -1359,6 +1359,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)if (err <0 ) goto do_error; copy = err; +/* Pages were charged by truesize, not by copy. */ +sk_forced_mem_schedule (sk,0 ); }else if (zc ==MSG_SPLICE_PAGES ) {/* Splice in data if we can; copy if we can't. */ if (tcp_downgrade_zcopy_pure (sk, skb))
Signature · 1 line
2.43.0
Eric Dumazet
maintainerQuoted 30 lines · Jiayuan Chen
Our local syzbot reported the following warning:
WARNING: ./include/linux/ucopysize.h:57 at tcp_sendmsg_locked+0x1e13/0x1f20 Modules linked in: CPU: 3 UID: 0 PID: 377 Comm: syz.2.2248 Not tainted 7.4.0-rc2+ #398 PREEMPT RIP: 0010:tcp_sendmsg_locked+0x1e13/0x1f20 Call Trace: <TASK> tcp_sendmsg+0x2c/0x50 inet_sendmsg+0x62/0xb0 __sys_sendto+0x2fe/0x350 __x64_sys_sendto+0x7f/0xb0 x64_sys_call+0x1375/0x27e0 do_syscall_64+0xe4/0x470 entry_SYSCALL_64_after_hwframe+0x76/0x7e </TASK>The problem is here:
if (!skb_zcopy_pure(skb)) { copy = tcp_wmem_schedule(sk, copy); if (!copy) goto wait_for_space; }When
MSG_ZEROCOPYdata is appended to a skb that already holds copied data, only the user length is scheduled, but the pinned pages are then charged by their truesize:
Quoted 7 lines
skb_zerocopy_iter_stream
__zerocopy_sg_from_iter
zerocopy_fill_skb_from_iter//truesize +=PAGE_ALIGN(copied + start)sk_mem_charge(sk, truesize)//sk_forward_alloc goes negativeFor example, sending 8 iovecs of 1 byte each schedules 8 bytes and charges 32768 with 4K pages.
This part is a separate bug: these 8 bytes sit in a single page.
zerocopy_fill_skb_from_iter() adds PAGE_ALIGN(copied + start) to skb->truesize for each iov_iter_get_pages2() call, even when the chunk is merged into the previous frag and the page reference is released.
With a corked socket, one copied byte, then one MSG_ZEROCOPY sendmsg() of 1024 bytes from a single page:
1 iovec sk_wmem_queued += 4096
1024 iovecs sk_wmem_queued += 4194304 (sk_sndbuf is 2626560)
With your patch, this over-estimation would become a forced charge, above tcp_mem and memcg limits, with an amount controlled by the user.
I have a fix [1] for this accounting (only charge the pages a frag did not already cover), I will send it shortly. With it, both cases above charge 4096 bytes, and sk_forward_alloc is still negative after the append, so your patch is still needed.
Thanks.
Quoted 9 lines
On a later
sendmsg()withoutMSG_ZEROCOPY, ifsk_wmem_schedule()fails (e.g.tcp_memhard limit is reached),tcp_wmem_schedule()returnsmin(copy, sk->sk_forward_alloc), which is negative. The negative copy is passed toskb_copy_to_page_nocache()and triggers the warning, andsendmsg()fails with-EFAULT.A few options were considered:
Quoted 8 lines
1) Schedule the truesize up front. It is not known before the pages are pinned, it depends on the iovec layout.
2) Do not append zerocopy data to a non pure skb. This splits a small header and its zerocopy payload into two skbs.
3) Schedule the missing part right after the pages are charged. They are already in the skb at this point, so this can not fail.Go with 3).
Analysis looks correct, and 3) is consistent with what we did for tcp_collapse() recently.
Quoted 12 lines
Fixes 849b425cd091 ("tcp: fix possible freeze in tx path under memory pressure")Signed-off-by Jiayuan Chen <[email protected]>--- net/ipv4/tcp.c | 2 ++ 1 file changed, 2 insertions(+)net/ipv4/tcp.c +0
-0
net/ipv4/tcp.c int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) · line 1363
Quoted 5 lines
net/ipv4/tcp.c +2
-0@@ -1359,6 +1359,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)if (err <0 ) goto do_error; copy = err; +/* Pages were charged by truesize, not by copy. */ +sk_forced_mem_schedule (sk,0 );
Pure zerocopy skbs are not charged, so there is nothing to fix for them. Please avoid the extra work in this case:
if (!skb_zcopy_pure(skb))
sk_forced_mem_schedule(sk, 0);Quoted 6 lines
net/ipv4/tcp.c +0
-1}else if (zc ==MSG_SPLICE_PAGES ) {/* Splice in data if we can; copy if we can't. */ if (tcp_downgrade_zcopy_pure (sk, skb)) --2.43.0
[1]
net/core/datagram.c +0 -0
100644
net/core/datagram.c +0 -1
@@ -659,7 +659,6 @@ int zerocopy_fill_skb_from_iter(struct sk_buff *skb, skb->data_len += copied; skb->len += copied; - skb->truesize +=PAGE_ALIGN (copied + start); head =compound_head (pages[n]); order =compound_order (head);
@@ -680,6 +679,11 @@ int zerocopy_fill_skb_from_iter(struct sk_buff *skb,if (head ==skb_frag_page (last) && start == skb_frag_off(last) +
skb_frag_size(last)) {
+ /* Only account the pages this frag
+ * did not already cover.
+ */
+ skb->truesize +=PAGE_ALIGN(start + size) -
+ PAGE_ALIGN(start);
skb_frag_size_add(last, size);
/* We combined this page, weneed to release
* a reference. Since compound
pages refcount
@@ -695,6 +699,7 @@ int zerocopy_fill_skb_from_iter(struct sk_buff *skb,
page_ref_sub(last_head, refs);
refs = 0;
}
+ skb->truesize += PAGE_SIZE;
skb_fill_page_desc_noacc(skb, frag++, head,
start, size);
}
if (refs)Quoted 50 lines · Eric Dumazet
Quoted 29 lines · Jiayuan Chen
Our local syzbot reported the following warning:
WARNING: ./include/linux/ucopysize.h:57 at tcp_sendmsg_locked+0x1e13/0x1f20 Modules linked in: CPU: 3 UID: 0 PID: 377 Comm: syz.2.2248 Not tainted 7.4.0-rc2+ #398 PREEMPT RIP: 0010:tcp_sendmsg_locked+0x1e13/0x1f20 Call Trace: <TASK> tcp_sendmsg+0x2c/0x50 inet_sendmsg+0x62/0xb0 __sys_sendto+0x2fe/0x350 __x64_sys_sendto+0x7f/0xb0 x64_sys_call+0x1375/0x27e0 do_syscall_64+0xe4/0x470 entry_SYSCALL_64_after_hwframe+0x76/0x7e </TASK>The problem is here:
if (!skb_zcopy_pure(skb)) { copy = tcp_wmem_schedule(sk, copy); if (!copy) goto wait_for_space; }When
MSG_ZEROCOPYdata is appended to a skb that already holds copied data, only the user length is scheduled, but the pinned pages are then charged by their truesize:Quoted 7 lines
skb_zerocopy_iter_stream
__zerocopy_sg_from_iter
zerocopy_fill_skb_from_iter//truesize +=PAGE_ALIGN(copied + start)sk_mem_charge(sk, truesize)//sk_forward_alloc goes negativeFor example, sending 8 iovecs of 1 byte each schedules 8 bytes and charges 32768 with 4K pages.
This part is a separate bug: these 8 bytes sit in a single page.
zerocopy_fill_skb_from_iter()addsPAGE_ALIGN(copied + start)toskb->truesizefor eachiov_iter_get_pages2()call, even when the chunk is merged into the previous frag and the page reference is released.With a corked socket, one copied byte, then one
MSG_ZEROCOPYsendmsg()of 1024 bytes from a single page:1 iovec
sk_wmem_queued+= 4096
1024 iovecssk_wmem_queued+= 4194304 (sk_sndbufis 2626560)
Quoted 7 lines
With your patch, this over-estimation would become a forced charge, above
tcp_memand memcg limits, with an amount controlled by the user.I have a fix [1] for this accounting (only charge the pages a frag did not already cover), I will send it shortly. With it, both cases above charge 4096 bytes, and
sk_forward_allocis still negative after the append, so your patch is still needed.
Thanks for the fix.
Quoted 40 lines
Thanks.
Quoted 8 lines
On a later
sendmsg()withoutMSG_ZEROCOPY, ifsk_wmem_schedule()fails (e.g.tcp_memhard limit is reached),tcp_wmem_schedule()returnsmin(copy, sk->sk_forward_alloc), which is negative. The negative copy is passed toskb_copy_to_page_nocache()and triggers the warning, andsendmsg()fails with-EFAULT.A few options were considered:
Quoted 8 lines
1) Schedule the truesize up front. It is not known before the pages are pinned, it depends on the iovec layout.
2) Do not append zerocopy data to a non pure skb. This splits a small header and its zerocopy payload into two skbs.
3) Schedule the missing part right after the pages are charged. They are already in the skb at this point, so this can not fail.Go with 3).
Analysis looks correct, and 3) is consistent with what we did for
tcp_collapse()recently.Quoted 11 lines
Fixes 849b425cd091 ("tcp: fix possible freeze in tx path under memory pressure")Signed-off-by Jiayuan Chen <[email protected]>--- net/ipv4/tcp.c | 2 ++ 1 file changed, 2 insertions(+)net/ipv4/tcp.c +0
-0net/ipv4/tcp.c int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) · line 1363
Quoted 5 lines
net/ipv4/tcp.c +2
-0@@ -1359,6 +1359,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)if (err <0 ) goto do_error; copy = err; +/* Pages were charged by truesize, not by copy. */ +sk_forced_mem_schedule (sk,0 );Pure zerocopy skbs are not charged, so there is nothing to fix for them. Please avoid the extra work in this case:
Quoted 2 lines
if (!skb_zcopy_pure(skb))
sk_forced_mem_schedule(sk, 0);Will do in v2.
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · 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).
- [Medium] The new sk_forced_mem_schedule(sk, 0) only runs on the success path of skb_zerocopy_iter_stream().
- [Low] In tcp_sendmsg_locked() (net/ipv4/tcp.c), the MSG_ZEROCOPY branch only pre-schedules memory when the skb is not pure: `if…
--
tcp: fix negative sk_forward_alloc after MSG_ZEROCOPY append
The MSG_ZEROCOPY path in tcp_sendmsg_locked() can append to a skb that already holds copied data. In that case only the user length is scheduled, but the pinned pages are charged by truesize. This commit adds sk_forced_mem_schedule(sk, 0) after a successful skb_zerocopy_iter_stream() so that sk_forward_alloc does not go negative.
net/ipv4/tcp.c int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) · line 1363
Quoted 10 lines
net/ipv4/tcp.c +2
-0@@ -1359,6 +1359,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)if (err <0 ) goto do_error; copy = err; +/* Pages were charged by truesize, not by copy. */ +sk_forced_mem_schedule (sk,0 );
[Severity: Medium]
Can sk_forward_alloc still go negative on the error path? The new call only runs when skb_zerocopy_iter_stream() succeeds, because the "if (err < 0) goto do_error" check comes first.
In skb_zerocopy_iter_stream(), the truesize charge made by __zerocopy_sg_from_iter() is undone on -EFAULT only if ___pskb_trim() succeeds:
net/core/skbuff.c:skb_zerocopy_iter_stream() {
...
if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
...
if (!___pskb_trim(skb, orig_len)) {
skb->truesize -= delta_truesize;
sk_wmem_queued_add(sk, -delta_truesize);
if (!skb_zcopy_pure(skb))
sk_mem_uncharge(sk, delta_truesize);
}
skb->sk = save_sk;
return err;
}
...
}___pskb_trim() fails when the skb is cloned andpskb_expand_head(skb, 0, 0, GFP_ATOMIC) fails.
Suppose the write queue tail skb is not pure, so only copy was scheduled, and it is cloned. If the user iovec faults after some pages are pinned and the atomic allocation fails, those pages stay charged by truesize. The caller then jumps to do_error without running the fix-up.
A later non-zerocopy sendmsg() under tcp_mem pressure would then get a negative copy back from tcp_wmem_schedule(). That is the same case the commit message describes.
Would it work to call sk_forced_mem_schedule(sk, 0) right after skb_zerocopy_iter_stream() returns, before the error checks? On the paths that do undo the charge, the call would do nothing.
[Severity: Low]
This isn't a bug, but does the new comment hold for pure zerocopy skbs? Earlier in this branch, the pre-schedule is guarded:
if (!skb_zcopy_pure(skb)) {
copy = tcp_wmem_schedule(sk, copy);
if (!copy)
goto wait_for_space;
}__zerocopy_sg_from_iter() also skips the charge for pure skbs:
net/core/datagram.c:__zerocopy_sg_from_iter() {
...
sk_wmem_queued_add(sk, truesize);
if (!skb_zcopy_pure(skb))
sk_mem_charge(sk, truesize);
...
}So when SKBFL_PURE_ZEROCOPY is set, no pages were charged to sk_forward_alloc. sk_forced_mem_schedule(sk, 0) is still called anyway.
In that case it does nothing, unless sk_forward_alloc is already negative for some unrelated reason. Then it would force-charge without any warning and hide that imbalance.
Could the call use the same !skb_zcopy_pure(skb) check as the pre-schedule? Alternatively, the comment could say it covers the non-pure case.
Quoted 3 lines
net/ipv4/tcp.c +0
-0}else if (zc ==MSG_SPLICE_PAGES ) {/* Splice in data if we can; copy if we can't. */ if (tcp_downgrade_zcopy_pure (sk, skb))
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119090828.343748-1-jiayuan.chen%40linux.dev