Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3090720rwb; Mon, 15 Aug 2022 17:54:04 -0700 (PDT) X-Google-Smtp-Source: AA6agR7vdBcNzVlCNdtXrA1t9nM/Joz4x00dGU3LUtSdSHuBzID+znOjzbcc3rMpYrCUM/Qyrhfn X-Received: by 2002:a05:6402:e86:b0:440:d1be:20c7 with SMTP id h6-20020a0564020e8600b00440d1be20c7mr16773172eda.349.1660611244491; Mon, 15 Aug 2022 17:54:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660611244; cv=none; d=google.com; s=arc-20160816; b=LHh4rw6mCjfYJD0CeDMS4O8H/Fk2HGFH5ZV0Ms4Qw3O0xegyHRz6/oIyyKA2Vw+Rv0 EiPK/uc0NV3+wTPR7BF9b8wHA+oLRlu4vOwM0M75GEf6qljUi920J9CclEQQZGIwbOqW IArI73JeHUw0A5KfrT/QAv8An0PxpKzosp+iZsnOk5jYOR1zPr2z3Jln3TskcOEEFEHn RK1FIQM0lJsUI/pm/hFBCTf9BOW3VjSCwXIF+qVNm4pGanSu3WN2Y8dXvXpQvqIVrqqK XpXy0DbUx7MFBj6q2Cix7WzghPvpVOLIS2Y01bSvPclwblA0uel3VRKDCTZHPqoIGvOW Uv1w== 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=gLUJ64gUD4AeFVhizBdTx4KrlJAviMmXh/IQ5xpH3ak=; b=IM/goZ/uLMNub3CVbS8AWoAqbKg66SN9BSlIjYA3ip4hCU2P7vfhsUfcIvH59GJxBx qQwVMi/U1wCJ+lCOnO0RnPJGvOpwADFPlxfGNDQTHb1d88K9atRCG9/XcNDv2lsLEcmW BFhH44pegWOo8I5v6LDTvJOcYXnSYiDCqOMmp8yROIchwjkzQLDrzP6G5+mgHIRdV8Kq fQ2TSyMF52XOMqwDYp5IBvwdM85GN0mpTrqqUGHGA+L4253DqXrORGtz5c4xDiPIUbPN tdWgsbBcU+TSqJ3Kf8Yr8qGA5mmseNRTUKHIQ+VvKUfa94RNvED4TS+U2ZOwTkUZBsfN tMww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=I94Oj4O5; 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 j4-20020a508a84000000b0043ba4ea320esi8242163edj.171.2022.08.15.17.53.39; Mon, 15 Aug 2022 17:54:04 -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=I94Oj4O5; 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 S240408AbiHOXeJ (ORCPT + 99 others); Mon, 15 Aug 2022 19:34:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346702AbiHOX1Z (ORCPT ); Mon, 15 Aug 2022 19:27:25 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 770A182847; Mon, 15 Aug 2022 13:07:13 -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 CFCEEB810C5; Mon, 15 Aug 2022 20:07:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19FA8C433D6; Mon, 15 Aug 2022 20:07:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660594030; bh=bNA+L8ZtPWK90hlTA0ywsViACbAwVY8ybVjhT9qqLuk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I94Oj4O5KQfKgfg20bAzWefJgwiammlyoeucnC0MI9a8TQN4X7QGVpPRYov7FxeCB LvPy5Ro2vHzMhOwnIEmdCxGzJuirpnSOwo+bOodr1vorFGfV1UsU31X53haWksdltV 4kHSXiMY/EDlEfMnaah0eQRdmeLwvdYZfbvYxMoQ= 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.19 0364/1157] tcp: fix possible freeze in tx path under memory pressure Date: Mon, 15 Aug 2022 19:55:20 +0200 Message-Id: <20220815180454.279576717@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@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 766881775abb..3ae2ea048883 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -952,6 +952,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) { @@ -987,7 +1004,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) { @@ -1336,8 +1357,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, @@ -1364,7 +1388,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