Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2929015rwb; Mon, 15 Aug 2022 14:14:42 -0700 (PDT) X-Google-Smtp-Source: AA6agR56BWZ9wgkRA6e0km5syT6D5yUzSa35kQ9Wah3MapD9C1vHIldZ+PtPjD0Ssue/V81DD1mP X-Received: by 2002:a17:907:d88:b0:730:d0bc:977a with SMTP id go8-20020a1709070d8800b00730d0bc977amr11889918ejc.664.1660598082092; Mon, 15 Aug 2022 14:14:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660598082; cv=none; d=google.com; s=arc-20160816; b=f8DJwoOnGWGjGVhgPKoFD0OO8Wbi68W2fxV1oeEDmYaKPvSdv3X+5dT2Po8A3yqiyt JcKpixA74P5jIIkNsvItUMg8gtMbCAF5s3yR6UrFF++6N0sayA+w3N/ZP4KpDubTfdt8 euRW5ieR6QnCd6q6sk+o1aNYZ+ScCNPrlm5Amh3Cz2rPmZsDaQnNvvgoOGh0ZueFZqlc fIbi/nL1nF9czPhobyC0bjzcJajYM1yEQLFuEySTH3CxAKV/D17MuJuBBftLB+Pjl5CF VzaLVp7ncPyHiSDfFmQrM+IuhgXLmBy0pVDLgPu1/IbU9VVW3wXxllCMgneJE15a6Ggf oRaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=QtOWEBH0izid3maOLDOb9McV0kaUAYFxdBGvAvAdl8Y=; b=HZuXt5l7tgRrx2JlqlCNvwYd4/jjcSSBUbvJ7ir6i9FtTi18WihfiUNK8Cnzgoo0RI o2KfAdpuxYcV/fRf7oWfIEYwFaC9UyU24hFpB/cfkE2lCllkRDGhxdWSBnAWGzod0kHb kYV1o1D0G8DEW8H731MP3nqny6SGOr5md39TPjGb5/nB6d+PJGkdBxUME9mgDinvz8YP T4PZLO42xF9i5R1kXGPSpVn1IzVVJ6KacB4X2Op3ydpqbRICIbRLRWvHl7vj5rYbpp0R 72Qj6OkXmfG7vgSnRu3yj4+8hxaMWduq+ZqD7alFSRFyI2mUtdZudwBPT9pH6lOAGPvY licQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2fCMLLoQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ds20-20020a170907725400b0072b4afac56fsi9285048ejc.327.2022.08.15.14.14.15; Mon, 15 Aug 2022 14:14:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2fCMLLoQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245508AbiHOU5Y (ORCPT + 99 others); Mon, 15 Aug 2022 16:57:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346917AbiHOU4C (ORCPT ); Mon, 15 Aug 2022 16:56:02 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C555A48E82; Mon, 15 Aug 2022 12:11:34 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 81C35B81109; Mon, 15 Aug 2022 19:11:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BD6B4C433D7; Mon, 15 Aug 2022 19:11:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660590692; bh=rG5TxObyUf1LId3LHy8gMxxLmgk02vTuSArN1/uPQjw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2fCMLLoQL95FIwCZWLkLHmvjxeD1SLbDvpOmle2lt7JttbhF3yGuUJYnrcN8jWlzP widQzy8sM9hRuzKAyI4N3tLaqdB0064xv+Ad8S74zmCBFJaH7GJ2eWJ++2A+jdw2Fr z60ZOhVAyNDgi/RswppCVrui+JieidCTLk+PMlRM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , Soheil Hassas Yeganeh , Wei Wang , Shakeel Butt , "David S. Miller" , Sasha Levin Subject: [PATCH 5.18 0341/1095] tcp: fix possible freeze in tx path under memory pressure Date: Mon, 15 Aug 2022 19:55:40 +0200 Message-Id: <20220815180443.908916463@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Dumazet [ Upstream commit 849b425cd091e1804af964b771761cfbefbafb43 ] Blamed commit only dealt with applications issuing small writes. Issue here is that we allow to force memory schedule for the sk_buff allocation, but we have no guarantee that sendmsg() is able to copy some payload in it. In this patch, I make sure the socket can use up to tcp_wmem[0] bytes. For example, if we consider tcp_wmem[0] = 4096 (default on x86), and initial skb->truesize being 1280, tcp_sendmsg() is able to copy up to 2816 bytes under memory pressure. Before this patch a sendmsg() sending more than 2816 bytes would either block forever (if persistent memory pressure), or return -EAGAIN. For bigger MTU networks, it is advised to increase tcp_wmem[0] to avoid sending too small packets. v2: deal with zero copy paths. Fixes: 8e4d980ac215 ("tcp: fix behavior for epoll edge trigger") Signed-off-by: Eric Dumazet Acked-by: Soheil Hassas Yeganeh Reviewed-by: Wei Wang Reviewed-by: Shakeel Butt Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/ipv4/tcp.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 91735d631a28..51116166e3d2 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -953,6 +953,23 @@ static int tcp_downgrade_zcopy_pure(struct sock *sk, struct sk_buff *skb) return 0; } +static int tcp_wmem_schedule(struct sock *sk, int copy) +{ + int left; + + if (likely(sk_wmem_schedule(sk, copy))) + return copy; + + /* We could be in trouble if we have nothing queued. + * Use whatever is left in sk->sk_forward_alloc and tcp_wmem[0] + * to guarantee some progress. + */ + left = sock_net(sk)->ipv4.sysctl_tcp_wmem[0] - sk->sk_wmem_queued; + if (left > 0) + sk_forced_mem_schedule(sk, min(left, copy)); + return min(copy, sk->sk_forward_alloc); +} + static struct sk_buff *tcp_build_frag(struct sock *sk, int size_goal, int flags, struct page *page, int offset, size_t *size) { @@ -988,7 +1005,11 @@ static struct sk_buff *tcp_build_frag(struct sock *sk, int size_goal, int flags, tcp_mark_push(tp, skb); goto new_segment; } - if (tcp_downgrade_zcopy_pure(sk, skb) || !sk_wmem_schedule(sk, copy)) + if (tcp_downgrade_zcopy_pure(sk, skb)) + return NULL; + + copy = tcp_wmem_schedule(sk, copy); + if (!copy) return NULL; if (can_coalesce) { @@ -1337,8 +1358,11 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) copy = min_t(int, copy, pfrag->size - pfrag->offset); - if (tcp_downgrade_zcopy_pure(sk, skb) || - !sk_wmem_schedule(sk, copy)) + if (tcp_downgrade_zcopy_pure(sk, skb)) + goto wait_for_space; + + copy = tcp_wmem_schedule(sk, copy); + if (!copy) goto wait_for_space; err = skb_copy_to_page_nocache(sk, &msg->msg_iter, skb, @@ -1365,7 +1389,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) skb_shinfo(skb)->flags |= SKBFL_PURE_ZEROCOPY; if (!skb_zcopy_pure(skb)) { - if (!sk_wmem_schedule(sk, copy)) + copy = tcp_wmem_schedule(sk, copy); + if (!copy) goto wait_for_space; } -- 2.35.1