Skip to content

Commit f8d9d93

Browse files
edumazetkuba-moo
authored andcommitted
tcp: take care of mixed splice()/sendmsg(MSG_ZEROCOPY) case
syzbot found that mixing sendpage() and sendmsg(MSG_ZEROCOPY) calls over the same TCP socket would again trigger the infamous warning in inet_sock_destruct() WARN_ON(sk_forward_alloc_get(sk)); While Talal took into account a mix of regular copied data and MSG_ZEROCOPY one in the same skb, the sendpage() path has been forgotten. We want the charging to happen for sendpage(), because pages could be coming from a pipe. What is missing is the downgrading of pure zerocopy status to make sure sk_forward_alloc will stay synced. Add tcp_downgrade_zcopy_pure() helper so that we can use it from the two callers. Fixes: 9b65b17 ("net: avoid double accounting for pure zerocopy skbs") Signed-off-by: Eric Dumazet <edumazet@google.com> Reported-by: syzbot <syzkaller@googlegroups.com> Cc: Talal Ahmad <talalahmad@google.com> Cc: Arjun Roy <arjunroy@google.com> Cc: Willem de Bruijn <willemb@google.com> Acked-by: Soheil Hassas Yeganeh <soheil@google.com> Link: https://lore.kernel.org/r/20220203225547.665114-1-eric.dumazet@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 40106e0 commit f8d9d93

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

net/ipv4/tcp.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,22 @@ void tcp_remove_empty_skb(struct sock *sk)
937937
}
938938
}
939939

940+
/* skb changing from pure zc to mixed, must charge zc */
941+
static int tcp_downgrade_zcopy_pure(struct sock *sk, struct sk_buff *skb)
942+
{
943+
if (unlikely(skb_zcopy_pure(skb))) {
944+
u32 extra = skb->truesize -
945+
SKB_TRUESIZE(skb_end_offset(skb));
946+
947+
if (!sk_wmem_schedule(sk, extra))
948+
return -ENOMEM;
949+
950+
sk_mem_charge(sk, extra);
951+
skb_shinfo(skb)->flags &= ~SKBFL_PURE_ZEROCOPY;
952+
}
953+
return 0;
954+
}
955+
940956
static struct sk_buff *tcp_build_frag(struct sock *sk, int size_goal, int flags,
941957
struct page *page, int offset, size_t *size)
942958
{
@@ -972,7 +988,7 @@ static struct sk_buff *tcp_build_frag(struct sock *sk, int size_goal, int flags,
972988
tcp_mark_push(tp, skb);
973989
goto new_segment;
974990
}
975-
if (!sk_wmem_schedule(sk, copy))
991+
if (tcp_downgrade_zcopy_pure(sk, skb) || !sk_wmem_schedule(sk, copy))
976992
return NULL;
977993

978994
if (can_coalesce) {
@@ -1320,19 +1336,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
13201336

13211337
copy = min_t(int, copy, pfrag->size - pfrag->offset);
13221338

1323-
/* skb changing from pure zc to mixed, must charge zc */
1324-
if (unlikely(skb_zcopy_pure(skb))) {
1325-
u32 extra = skb->truesize -
1326-
SKB_TRUESIZE(skb_end_offset(skb));
1327-
1328-
if (!sk_wmem_schedule(sk, extra))
1329-
goto wait_for_space;
1330-
1331-
sk_mem_charge(sk, extra);
1332-
skb_shinfo(skb)->flags &= ~SKBFL_PURE_ZEROCOPY;
1333-
}
1334-
1335-
if (!sk_wmem_schedule(sk, copy))
1339+
if (tcp_downgrade_zcopy_pure(sk, skb) ||
1340+
!sk_wmem_schedule(sk, copy))
13361341
goto wait_for_space;
13371342

13381343
err = skb_copy_to_page_nocache(sk, &msg->msg_iter, skb,

0 commit comments

Comments
 (0)