Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp89779iog; Thu, 23 Jun 2022 23:17:04 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tRjNkI07o5GfQBT2yy/WSpZOoPYs0zTFGpKOdmGW7VcTcrcQC90pcFGF86sa/JU7Mvo6z7 X-Received: by 2002:a62:6411:0:b0:50a:81df:bfa6 with SMTP id y17-20020a626411000000b0050a81dfbfa6mr44861293pfb.26.1656051423867; Thu, 23 Jun 2022 23:17:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656051423; cv=none; d=google.com; s=arc-20160816; b=IzQGhYla0rlsrbPqPkQbuDBpxwXf/4v9a0uVVkAZn9cXGm9l2FobKnWVJ1bPRYz1KR mYo83SCCyskxxpQYP3C7oNqNn0bXaU2S38VABeqzf/bvqq4X/Y4DIoOK7XaaP/TCMlBA R40y1WA8y4+Gvbp2XDJbb2U+lXEwtBruB99rc6j5LWf0/MZcGZEkNDHG8G8HeJzE4ixr Xox+GNC5oR8I2F65kk6aucthODc/wwMCzqTEMK+KnQZ8B/wqX1BS5KpV+X7mA9H5EpJw 8J4TIt6jdxsv850q6FNH8XuMkTyTYlOsaDHIOFPZP98fZAYmM144KKaqJNAeKV4oDV/W ggSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=SeaKrMM2DGf9AmKtqHuEPGi8mQgc3169/28hZsaIXsU=; b=zmk0AYIEuR5f4WjSuVNSUoWt8wua6JuarBIOIyKzIExzHPFPAUDeyIdbUw9ve+0gIK b9ebjjkzVnMcIr2k2K0WO6sUJWLddRw/vGex17NGKKledyi2ldWOXbcIrVnNSc3Ljknf Xuc4CuEJN6fOWDJC42IhQ5R+0DiJUVXNLG4bi/+fjCuWx+QKkOIyJ6B0d5ijLGh29gsY G7z93w6htAUO/q009pvQSUnV+rjuGImfqmkBML5WCX6CtJedYj2orQ6skkvV3iNSIFx0 jm24Gnw5M2vN5ELLH32aZcwCl89VVQWgUvWFIUnZ/wqaeIoRYdBmbo+R8MeM7jQzdOLF HABQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=FqiQk1iw; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y65-20020a638a44000000b0040d46709b85si1591985pgd.738.2022.06.23.23.16.43; Thu, 23 Jun 2022 23:17:03 -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=@google.com header.s=20210112 header.b=FqiQk1iw; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230280AbiFXGIU (ORCPT + 99 others); Fri, 24 Jun 2022 02:08:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229641AbiFXGIH (ORCPT ); Fri, 24 Jun 2022 02:08:07 -0400 Received: from mail-yb1-xb29.google.com (mail-yb1-xb29.google.com [IPv6:2607:f8b0:4864:20::b29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6F6BDFDA for ; Thu, 23 Jun 2022 23:08:05 -0700 (PDT) Received: by mail-yb1-xb29.google.com with SMTP id r3so2838628ybr.6 for ; Thu, 23 Jun 2022 23:08:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=SeaKrMM2DGf9AmKtqHuEPGi8mQgc3169/28hZsaIXsU=; b=FqiQk1iwQhSOXqbIhgVqSjXUfZlndtlBhMmS3D2jY6aNk61b+pcUKRo2ByWWbfcn6g 8X+pd75XhEdBKwAdwg+alHdpfKkV43RstivYkdaY/WlCeljzGFN2LKxz9cDqpvJ1vpPZ aIPQMNcoLoHdyUMr+VcjSHUisnUSwFJkjg9tNvVR1ORlao3PzRO0ovn7NTponI3t9vXl tg2ddeMBpINdPGMnBEQLUuloASNfsn3mg0iymryzg7WWRzL9+xdZbxu6/LoaLNP+8P9i dC/SKDmyeAehjCmCvWYSUSEZv2xgCYyIOxMZIemDJX2tXHPrPJdcx7UAIJOmJo3UeP9f LpFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=SeaKrMM2DGf9AmKtqHuEPGi8mQgc3169/28hZsaIXsU=; b=qadbAO/psIi6DDpxpCm+01yev3bJZLHSOPw84wOy153JkfwKwBMnBo2qvG/fl2WAsd 3W+f2a6mRbdRx7vgPwf79v1M9hep/pWis25NHq1srOWQtrjair8npHLlXuututlLKb88 or6hHFXqsCWjP2UgOPnAXxhGIWDw1w/dkWxQeZ8No9LLXoIQ1E5mXg2dtGnrMJLrtb85 0S5b5Lx1FMi7VQvaa5BgcQYsrTNtJJYHd18fRaovAtjE+n2dyFfU/R3gbWm3fM0zADkW 6WEUErNVbvIpD8wXorxR61Di3bZo4yaVJFAVFAD5BjKfjwLmECVMgg9JLHjhG4Rv9Bzk RZMA== X-Gm-Message-State: AJIora9l7ad7BAoG431qXHLLMUPOHQMxPdHNSErI0CB4KgdAThEWy2z6 q7J07PtjDrc9wvkpexdLrS+fJyYGKOE1Y0SOGDW+KQ== X-Received: by 2002:a25:8181:0:b0:668:c835:eb7c with SMTP id p1-20020a258181000000b00668c835eb7cmr13491867ybk.598.1656050884788; Thu, 23 Jun 2022 23:08:04 -0700 (PDT) MIME-Version: 1.0 References: <20220619150456.GB34471@xsang-OptiPlex-9020> <20220622172857.37db0d29@kernel.org> <20220623185730.25b88096@kernel.org> <20220624051351.GA72171@shbuild999.sh.intel.com> <20220624060053.GD79500@shbuild999.sh.intel.com> In-Reply-To: <20220624060053.GD79500@shbuild999.sh.intel.com> From: Eric Dumazet Date: Fri, 24 Jun 2022 08:07:53 +0200 Message-ID: Subject: Re: [net] 4890b686f4: netperf.Throughput_Mbps -69.4% regression To: Feng Tang Cc: Jakub Kicinski , Xin Long , Marcelo Ricardo Leitner , kernel test robot , Shakeel Butt , Soheil Hassas Yeganeh , LKML , Linux Memory Management List , network dev , linux-s390@vger.kernel.org, MPTCP Upstream , "linux-sctp @ vger . kernel . org" , lkp@lists.01.org, kbuild test robot , Huang Ying , zhengjun.xing@linux.intel.com, fengwei.yin@intel.com, Ying Xu Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Fri, Jun 24, 2022 at 8:01 AM Feng Tang wrote: > > On Fri, Jun 24, 2022 at 07:45:00AM +0200, Eric Dumazet wrote: > > On Fri, Jun 24, 2022 at 7:14 AM Feng Tang wrote: > > > > > > Hi Eric, > > > > > > On Fri, Jun 24, 2022 at 06:13:51AM +0200, Eric Dumazet wrote: > > > > On Fri, Jun 24, 2022 at 3:57 AM Jakub Kicinski wrote: > > > > > > > > > > On Thu, 23 Jun 2022 18:50:07 -0400 Xin Long wrote: > > > > > > From the perf data, we can see __sk_mem_reduce_allocated() is the one > > > > > > using CPU the most more than before, and mem_cgroup APIs are also > > > > > > called in this function. It means the mem cgroup must be enabled in > > > > > > the test env, which may explain why I couldn't reproduce it. > > > > > > > > > > > > The Commit 4890b686f4 ("net: keep sk->sk_forward_alloc as small as > > > > > > possible") uses sk_mem_reclaim(checking reclaimable >= PAGE_SIZE) to > > > > > > reclaim the memory, which is *more frequent* to call > > > > > > __sk_mem_reduce_allocated() than before (checking reclaimable >= > > > > > > SK_RECLAIM_THRESHOLD). It might be cheap when > > > > > > mem_cgroup_sockets_enabled is false, but I'm not sure if it's still > > > > > > cheap when mem_cgroup_sockets_enabled is true. > > > > > > > > > > > > I think SCTP netperf could trigger this, as the CPU is the bottleneck > > > > > > for SCTP netperf testing, which is more sensitive to the extra > > > > > > function calls than TCP. > > > > > > > > > > > > Can we re-run this testing without mem cgroup enabled? > > > > > > > > > > FWIW I defer to Eric, thanks a lot for double checking the report > > > > > and digging in! > > > > > > > > I did tests with TCP + memcg and noticed a very small additional cost > > > > in memcg functions, > > > > because of suboptimal layout: > > > > > > > > Extract of an internal Google bug, update from June 9th: > > > > > > > > -------------------------------- > > > > I have noticed a minor false sharing to fetch (struct > > > > mem_cgroup)->css.parent, at offset 0xc0, > > > > because it shares the cache line containing struct mem_cgroup.memory, > > > > at offset 0xd0 > > > > > > > > Ideally, memcg->socket_pressure and memcg->parent should sit in a read > > > > mostly cache line. > > > > ----------------------- > > > > > > > > But nothing that could explain a "-69.4% regression" > > > > > > We can double check that. > > > > > > > memcg has a very similar strategy of per-cpu reserves, with > > > > MEMCG_CHARGE_BATCH being 32 pages per cpu. > > > > > > We have proposed patch to increase the batch numer for stats > > > update, which was not accepted as it hurts the accuracy and > > > the data is used by many tools. > > > > > > > It is not clear why SCTP with 10K writes would overflow this reserve constantly. > > > > > > > > Presumably memcg experts will have to rework structure alignments to > > > > make sure they can cope better > > > > with more charge/uncharge operations, because we are not going back to > > > > gigantic per-socket reserves, > > > > this simply does not scale. > > > > > > Yes, the memcg statitics and charge/unchage update is very sensitive > > > with the data alignemnt layout, and can easily trigger peformance > > > changes, as we've seen quite some similar cases in the past several > > > years. > > > > > > One pattern we've seen is, even if a memcg stats updating or charge > > > function only takes about 2%~3% of the CPU cycles in perf-profile data, > > > once it got affected, the peformance change could be amplified to up to > > > 60% or more. > > > > > > > Reorganizing "struct mem_cgroup" to put "struct page_counter memory" > > in a separate cache line would be beneficial. > > That may help. > > And I also want to say the benchmarks(especially micro one) are very > sensitive to the layout of mem_cgroup. As the 'page_counter' is 112 > bytes in size, I recently made a patch to make it cacheline aligned > (take 2 cachelines), which improved some hackbench/netperf test > cases, but caused huge (49%) drop for some vm-scalability tests. > > > Many low hanging fruits, assuming nobody will use __randomize_layout on it ;) > > > > Also some fields are written even if their value is not changed. > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > index abec50f31fe64100f4be5b029c7161b3a6077a74..53d9c1e581e78303ef73942e2b34338567987b74 > > 100644 > > --- a/mm/memcontrol.c > > +++ b/mm/memcontrol.c > > @@ -7037,10 +7037,12 @@ 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; > > + if (READ_ONCE(memcg->tcpmem_pressure)) > > + WRITE_ONCE(memcg->tcpmem_pressure, 0); > > return true; > > } > > - memcg->tcpmem_pressure = 1; > > + if (!READ_ONCE(memcg->tcpmem_pressure)) > > + WRITE_ONCE(memcg->tcpmem_pressure, 1); > > if (gfp_mask & __GFP_NOFAIL) { > > page_counter_charge(&memcg->tcpmem, nr_pages); > > return true; > > I will also try this patch, which may take some time. Note that applications can opt-in reserving memory for one socket, using SO_RESERVE_MEM This can be used for jobs with a controlled number of sockets, as this will avoid many charge/uncharge operations.