Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp8049703rwb; Tue, 6 Dec 2022 13:19:01 -0800 (PST) X-Google-Smtp-Source: AA0mqf4sQVIcvC9yAdb+Wump1knYe9Ct5oANF9OQlrw6C9t5g6zIZZvyIHhmAE6aQjHdaRixGK5R X-Received: by 2002:a17:906:6b86:b0:7c0:eeae:10c8 with SMTP id l6-20020a1709066b8600b007c0eeae10c8mr10315341ejr.68.1670361541176; Tue, 06 Dec 2022 13:19:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670361541; cv=none; d=google.com; s=arc-20160816; b=E78VPT6Sd7U1Jj0Q9LcUcMBk+WKklESM9H482ZjZFbG/839lFJ+p42I2VKvqcBDybQ u5Jrvg2l88sHKhO+Hp553YWOBU3bv1OOi1oAwaOpe68OKKKyk6uzg0sZFLlvwgI0+Rix xon9KVWi4FJ2c0tc1i2e4VJo7//GJqTJVr23K90o8rL8j8NUdyCouWXW2hwY3Z1m3cUj a9TdXQH2BCUwQ+XwDGMzwmPOoTFWyvOFHeywfjxY281QPWL9s/awNUFbYcR1cENF/Idd S+UF/Qs0nQPTQpNZRrV6f8ScEEwgcFDnMuuQ9HbZyKzbhMtC8ISZE/L/Et6Xt/Wb1j3y ZdGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=MhNNBuU3zhWHic7xb6ZOwSKSyK5OVL6db6+6pmE+Q3M=; b=vyh03bQSeTrAmBp/3X6625yfypeDwtA8gbemgsgcDAzCCOIkjEVSeytj3uRfiUiP80 1qjpgpdCpfcYqAgDH7eFBoSyUE7j6xVdO9+xzcMXzb0GrSZqO0JhxC8Z3udh5f65Lusr bSEWQJwSM/MMXC7QQSqG0DubW/Bc8SVB9gtsrUqEpaX8x/iTGBo/0e1g+9BLdc0lQWiX I++CKpz0RHbFldeBR4DOYuL6roysoOaxdYxs44cbFKO3Td5gLDrfjoFVr8I6RKatNxIW vHKczmMXoOw90o4gqv7Tqkevcx8YUeMLbS4W9VrTZxByaRYAUpZ4LqPI29FyYqeEBMo3 4Jxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=ew9hSEgO; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m15-20020a50930f000000b0046762f4cc79si2708810eda.137.2022.12.06.13.18.42; Tue, 06 Dec 2022 13:19:01 -0800 (PST) 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=@cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=ew9hSEgO; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229730AbiLFUva (ORCPT + 77 others); Tue, 6 Dec 2022 15:51:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229600AbiLFUv2 (ORCPT ); Tue, 6 Dec 2022 15:51:28 -0500 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6B55442F6 for ; Tue, 6 Dec 2022 12:51:26 -0800 (PST) Received: by mail-ej1-x635.google.com with SMTP id b2so9281897eja.7 for ; Tue, 06 Dec 2022 12:51:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20210112.gappssmtp.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=MhNNBuU3zhWHic7xb6ZOwSKSyK5OVL6db6+6pmE+Q3M=; b=ew9hSEgOp7zxS5LHySeYnEzHgmaD+v212Cb4Fd4PwkYBPj5pH62iZWOulKd0ei+VIF hG4aPrHYGGD2WjWCwTeiUbs4eJpgiBWAD11E9kH3ukMi930ryVJILwhbma6Z6EzrMM05 mLbvFWbLYmMuszo96rfDjQSt3bGw11HgQ5uhXrEPsPukevWYu9Se1bCczpK+/vleLV5K AGPm4VSO4i06JLwAL1yP1lr+XAgD8QZhihWIylphT7Vy0PwUMDgG1DNbLcgxqTsHQe/q icM80qePTJzH/mDe7I8K1001EPbcfyl2/qvXn1BCmCuF8HDKBXbdkhqjrLXdTuledxeF tJ2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=MhNNBuU3zhWHic7xb6ZOwSKSyK5OVL6db6+6pmE+Q3M=; b=Pn8iuJ6eEZQJR3xKKeykVL6yjtzEiRU+Mr7SpXLYA8JcysO3JdnoMXApkomMDcnxe6 Oe01hlSpI2pFXcvdEVyvzZYQHR/5JfP/oU1gsF3vIAmwcCofSX2CX4R5DZO+MCjwoD3F hTXzHHjWs/z+gg7zIQ11F0frY4OyCZn/Oix72cZJunyPEcLyue/uDByNiKPnJiYe4HkT dMxat/i6KeB1LsBhgMlLudBG42FNfO3CilhdthuBQXvSGHrv2sl/8oziYemg6jWe2jPd wwavb+yqRj6TgAmx9DX8kFc56O4NG3RMBmstq70R0L4eJiFKpy+jY2EGA7nWVizzbRBk KXGQ== X-Gm-Message-State: ANoB5pmyEgQcmm5I/y4W/+BSj8QDtw2ULHSQhJ8maoq3xPTDaHY1KNi1 6Y4wuQQuZJMnjNQOhzpch0SQmQ== X-Received: by 2002:a17:906:fa11:b0:7c0:d94c:f9f with SMTP id lo17-20020a170906fa1100b007c0d94c0f9fmr12177439ejb.542.1670359885503; Tue, 06 Dec 2022 12:51:25 -0800 (PST) Received: from localhost ([2a02:8070:6387:ab20:15aa:3c87:c206:d15e]) by smtp.gmail.com with ESMTPSA id fi22-20020a1709073ad600b007c0d4d3a0c1sm4576925ejc.32.2022.12.06.12.51.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 12:51:25 -0800 (PST) Date: Tue, 6 Dec 2022 21:51:01 +0100 From: Johannes Weiner To: Eric Dumazet Cc: Ivan Babrou , Linux MM , Linux Kernel Network Developers , linux-kernel , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , "David S. Miller" , Hideaki YOSHIFUJI , David Ahern , Jakub Kicinski , Paolo Abeni , cgroups@vger.kernel.org, kernel-team Subject: Re: Low TCP throughput due to vmpressure with swap enabled Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 On Tue, Dec 06, 2022 at 08:13:50PM +0100, Eric Dumazet wrote: > On Tue, Dec 6, 2022 at 8:00 PM Johannes Weiner wrote: > > @@ -1701,10 +1701,10 @@ void mem_cgroup_sk_alloc(struct sock *sk); > > void mem_cgroup_sk_free(struct sock *sk); > > static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) > > { > > - if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure) > > + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->socket_pressure) > > && READ_ONCE(memcg->socket_pressure)) > > > return true; > > do { > > - if (time_before(jiffies, READ_ONCE(memcg->socket_pressure))) > > + if (memcg->socket_pressure) > > if (READ_ONCE(...)) Good point, I'll add those. > > @@ -7195,10 +7194,10 @@ bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, > > struct page_counter *fail; > > > > if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) { > > - memcg->tcpmem_pressure = 0; > > Orthogonal to your patch, but: > > Maybe avoid touching this cache line too often and use READ/WRITE_ONCE() ? > > if (READ_ONCE(memcg->socket_pressure)) > WRITE_ONCE(memcg->socket_pressure, false); Ah, that's a good idea. I think it'll be fine in the failure case, since that's associated with OOM and total performance breakdown anyway. But certainly, in the common case of the charge succeeding, we should not keep hammering false into that variable over and over. How about the delta below? I also flipped the branches around to keep the common path at the first indentation level, hopefully making that a bit clearer too. Thanks for taking a look, Eric! diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index ef1c388be5b3..13ae10116895 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1701,10 +1701,11 @@ void mem_cgroup_sk_alloc(struct sock *sk); void mem_cgroup_sk_free(struct sock *sk); static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) { - if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->socket_pressure) + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && + READ_ONCE(memcg->socket_pressure)) return true; do { - if (memcg->socket_pressure) + if (READ_ONCE(memcg->socket_pressure)) return true; } while ((memcg = parent_mem_cgroup(memcg))); return false; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 0d4b9dbe775a..96c4ec0f11ca 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -7193,31 +7193,29 @@ bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { struct page_counter *fail; - if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) { - memcg->socket_pressure = false; - return true; + if (!page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) { + WRITE_ONCE(memcg->socket_pressure, true); + if (gfp_mask & __GFP_FAIL) { + page_counter_charge(&memcg->tcpmem, nr_pages); + return true; + } + return false; } - memcg->socket_pressure = true; + if (unlikely(READ_ONCE(memcg->socket_pressure))) + WRITE_ONCE(memcg->socket_pressure, false); + } + + if (try_charge(memcg, gfp_mask & ~__GFP_NOFAIL, nr_pages) < 0) { + WRITE_ONCE(memcg->socket_pressure, true); if (gfp_mask & __GFP_NOFAIL) { - page_counter_charge(&memcg->tcpmem, nr_pages); + try_charge(memcg, gfp_mask, nr_pages); + mod_memcg_state(memcg, MEMCG_SOCK, nr_pages); return true; } return false; } - - if (try_charge(memcg, gfp_mask & ~__GFP_NOFAIL, nr_pages) == 0) { - memcg->socket_pressure = false; - goto success; - } - memcg->socket_pressure = true; - if (gfp_mask & __GFP_NOFAIL) { - try_charge(memcg, gfp_mask, nr_pages); - goto success; - } - - return false; - -success: + if (unlikely(READ_ONCE(memcg->socket_pressure))) + WRITE_ONCE(memcg->socket_pressure, false); mod_memcg_state(memcg, MEMCG_SOCK, nr_pages); return true; }