Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp630065pxv; Thu, 15 Jul 2021 11:58:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyIAD+fdVcrN0Oqr/VaRFauNN3Hfb89v1vHqZ/L2mhncaFSc1e5orsVEXxRSIfcs4evBwSf X-Received: by 2002:a17:906:b2d5:: with SMTP id cf21mr7260262ejb.514.1626375524946; Thu, 15 Jul 2021 11:58:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626375524; cv=none; d=google.com; s=arc-20160816; b=MPhNTJajszXiWyMEWHewYuKRUNba6JmkIlnICHclxO/eZiv18UNmzNj3W3i8kWp45c pWKzApR4/rjSmvXivO2oKnFTf0Mu6Xo+PdLEK06yDzmDsVcrPWxRc7TddkBWJaA4i/Xf SOkNPC4c127VZIqqWi86Kyiq8MWl5ThAFk3CvJjWRCMjoK5FMYeGaORierLc2mt/ekI0 TpjsPf7DbyEj82Vc+8vcT9oRnOIz2pSoxim8vuHDV8E1og503256JQc0HV0tA9djyBbe 1B5vejViE0/MRr7x7EZfZYU2j5H8EBIj73l8gKwW7PvgHASUWMOOIk+AYk9se2yISCdP bvGQ== 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=NSkSpTyH2AMd+1m0SKaWmK+4D3jq+lQRYld54MU7HrQ=; b=W2B/E9VPncviM3/1G0pPGs5y1xRVfZ8ODpWWHRcH/DeegRI4tSLfrm+8RvlqeNkmO4 8t7KqTnb8QQOHR6RsvyO7esgMOG99txHzFPwTXnd5F4JqaP5oleHMBciCMfw3j6b2UL6 y5oWKBbOnhm3OK/dsSgCZBh++nxPJtcoRXNrSST6gHsS4DgSuRUtopl8i4u46AJbtSD5 ENVBn9c5UaQQdtrIgrSW0dprQz1aobBF0aYHLOUZpBhXxaWkO1MkTPtnDXFO5CyVv5WZ VDNUURwTlnrT2m6ZdbOkOG3Tc/P/rU/5c/7boB2MuP2L+tTS9cJnoJvZHUWmkX2A5xjC 57lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Mb4Cd53F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l14si8235871eji.305.2021.07.15.11.58.21; Thu, 15 Jul 2021 11:58:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Mb4Cd53F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S242536AbhGOS7p (ORCPT + 99 others); Thu, 15 Jul 2021 14:59:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:52350 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240088AbhGOSwq (ORCPT ); Thu, 15 Jul 2021 14:52:46 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D3CBF613D8; Thu, 15 Jul 2021 18:49:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626374992; bh=dkS18s5cDxGv15dyZ3WKlW9c2mmTEejltcA51Aq8ebw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mb4Cd53F7My1YCinweDpswF8ovhkcbNReAK7khbklVCw9BW4QzGFsgtQ/en3dgOxc k4Jhi9UpMnu2kWTD3IZrAylBcb5P9jBrLL9KK1bNbfwlqqrLZJl5Et18HN9HIJ/x5o PEKvqdJlg6OT/zyKZ0pfr6xeGd3b4zGvjssKRKGs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dave Jones , Jakub Kicinski , "David S. Miller" , Sasha Levin Subject: [PATCH 5.10 114/215] net: ip: avoid OOM kills with large UDP sends over loopback Date: Thu, 15 Jul 2021 20:38:06 +0200 Message-Id: <20210715182619.702719256@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182558.381078833@linuxfoundation.org> References: <20210715182558.381078833@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jakub Kicinski [ Upstream commit 6d123b81ac615072a8525c13c6c41b695270a15d ] Dave observed number of machines hitting OOM on the UDP send path. The workload seems to be sending large UDP packets over loopback. Since loopback has MTU of 64k kernel will try to allocate an skb with up to 64k of head space. This has a good chance of failing under memory pressure. What's worse if the message length is <32k the allocation may trigger an OOM killer. This is entirely avoidable, we can use an skb with page frags. af_unix solves a similar problem by limiting the head length to SKB_MAX_ALLOC. This seems like a good and simple approach. It means that UDP messages > 16kB will now use fragments if underlying device supports SG, if extra allocator pressure causes regressions in real workloads we can switch to trying the large allocation first and falling back. v4: pre-calculate all the additions to alloclen so we can be sure it won't go over order-2 Reported-by: Dave Jones Signed-off-by: Jakub Kicinski Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/ipv4/ip_output.c | 32 ++++++++++++++++++-------------- net/ipv6/ip6_output.c | 32 +++++++++++++++++--------------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c index 97975bed491a..560d5dc43562 100644 --- a/net/ipv4/ip_output.c +++ b/net/ipv4/ip_output.c @@ -1053,7 +1053,7 @@ static int __ip_append_data(struct sock *sk, unsigned int datalen; unsigned int fraglen; unsigned int fraggap; - unsigned int alloclen; + unsigned int alloclen, alloc_extra; unsigned int pagedlen; struct sk_buff *skb_prev; alloc_new_skb: @@ -1073,35 +1073,39 @@ alloc_new_skb: fraglen = datalen + fragheaderlen; pagedlen = 0; + alloc_extra = hh_len + 15; + alloc_extra += exthdrlen; + + /* The last fragment gets additional space at tail. + * Note, with MSG_MORE we overallocate on fragments, + * because we have no idea what fragment will be + * the last. + */ + if (datalen == length + fraggap) + alloc_extra += rt->dst.trailer_len; + if ((flags & MSG_MORE) && !(rt->dst.dev->features&NETIF_F_SG)) alloclen = mtu; - else if (!paged) + else if (!paged && + (fraglen + alloc_extra < SKB_MAX_ALLOC || + !(rt->dst.dev->features & NETIF_F_SG))) alloclen = fraglen; else { alloclen = min_t(int, fraglen, MAX_HEADER); pagedlen = fraglen - alloclen; } - alloclen += exthdrlen; - - /* The last fragment gets additional space at tail. - * Note, with MSG_MORE we overallocate on fragments, - * because we have no idea what fragment will be - * the last. - */ - if (datalen == length + fraggap) - alloclen += rt->dst.trailer_len; + alloclen += alloc_extra; if (transhdrlen) { - skb = sock_alloc_send_skb(sk, - alloclen + hh_len + 15, + skb = sock_alloc_send_skb(sk, alloclen, (flags & MSG_DONTWAIT), &err); } else { skb = NULL; if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <= 2 * sk->sk_sndbuf) - skb = alloc_skb(alloclen + hh_len + 15, + skb = alloc_skb(alloclen, sk->sk_allocation); if (unlikely(!skb)) err = -ENOBUFS; diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c index 077d43af8226..e889655ca0e2 100644 --- a/net/ipv6/ip6_output.c +++ b/net/ipv6/ip6_output.c @@ -1554,7 +1554,7 @@ emsgsize: unsigned int datalen; unsigned int fraglen; unsigned int fraggap; - unsigned int alloclen; + unsigned int alloclen, alloc_extra; unsigned int pagedlen; alloc_new_skb: /* There's no room in the current skb */ @@ -1581,17 +1581,28 @@ alloc_new_skb: fraglen = datalen + fragheaderlen; pagedlen = 0; + alloc_extra = hh_len; + alloc_extra += dst_exthdrlen; + alloc_extra += rt->dst.trailer_len; + + /* We just reserve space for fragment header. + * Note: this may be overallocation if the message + * (without MSG_MORE) fits into the MTU. + */ + alloc_extra += sizeof(struct frag_hdr); + if ((flags & MSG_MORE) && !(rt->dst.dev->features&NETIF_F_SG)) alloclen = mtu; - else if (!paged) + else if (!paged && + (fraglen + alloc_extra < SKB_MAX_ALLOC || + !(rt->dst.dev->features & NETIF_F_SG))) alloclen = fraglen; else { alloclen = min_t(int, fraglen, MAX_HEADER); pagedlen = fraglen - alloclen; } - - alloclen += dst_exthdrlen; + alloclen += alloc_extra; if (datalen != length + fraggap) { /* @@ -1601,30 +1612,21 @@ alloc_new_skb: datalen += rt->dst.trailer_len; } - alloclen += rt->dst.trailer_len; fraglen = datalen + fragheaderlen; - /* - * We just reserve space for fragment header. - * Note: this may be overallocation if the message - * (without MSG_MORE) fits into the MTU. - */ - alloclen += sizeof(struct frag_hdr); - copy = datalen - transhdrlen - fraggap - pagedlen; if (copy < 0) { err = -EINVAL; goto error; } if (transhdrlen) { - skb = sock_alloc_send_skb(sk, - alloclen + hh_len, + skb = sock_alloc_send_skb(sk, alloclen, (flags & MSG_DONTWAIT), &err); } else { skb = NULL; if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <= 2 * sk->sk_sndbuf) - skb = alloc_skb(alloclen + hh_len, + skb = alloc_skb(alloclen, sk->sk_allocation); if (unlikely(!skb)) err = -ENOBUFS; -- 2.30.2