From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70CA9C369DC for ; Tue, 29 Apr 2025 12:13:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A46B16B0007; Tue, 29 Apr 2025 08:13:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9CDF26B0008; Tue, 29 Apr 2025 08:13:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 847B96B000A; Tue, 29 Apr 2025 08:13:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 630DC6B0007 for ; Tue, 29 Apr 2025 08:13:21 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 33C991C83AF for ; Tue, 29 Apr 2025 12:13:21 +0000 (UTC) X-FDA: 83386971402.10.30B4AF0 Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) by imf04.hostedemail.com (Postfix) with ESMTP id 2E76C40010 for ; Tue, 29 Apr 2025 12:13:18 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=suse.com header.s=google header.b=UBayS79F; dmarc=pass (policy=quarantine) header.from=suse.com; spf=pass (imf04.hostedemail.com: domain of mhocko@suse.com designates 209.85.208.52 as permitted sender) smtp.mailfrom=mhocko@suse.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745928799; a=rsa-sha256; cv=none; b=IcsyinhNuAPRCju0ZBPgWOSalp9EWaKZdWA4q9axJPF03X511rApBhYHsgN06CViP+CWQb 1ewU/oLk5Se1jMgPbqCDR/V7aScxneQvTNthkYi4cgCwM5delvvCSGS4IjMoTbMyNqJd1+ YwiqIgws++lG93g+hg1hGzolzY9v6Ys= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1745928799; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XGwtrgjfssbRJnnLGAioENkMU7oIKogOgkQ9Ev2GRas=; b=sSauO1rHDY2cykLSW8HLm0Nie25y81bJTIZd/XOZ4SNtHxwWynoHr37Hr2GuLk/m2vCtgQ GwAytoMpzOX23PP8ygPwehLlz1OdAYFk/huhv9FHJPvajHFqFYf9ho/CsXRDZ1I9yIG9HX 6fwaerrqic0kQErG9sgvp7u76qjYt9A= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=suse.com header.s=google header.b=UBayS79F; dmarc=pass (policy=quarantine) header.from=suse.com; spf=pass (imf04.hostedemail.com: domain of mhocko@suse.com designates 209.85.208.52 as permitted sender) smtp.mailfrom=mhocko@suse.com Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-5f63ac6ef0fso7457049a12.1 for ; Tue, 29 Apr 2025 05:13:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1745928798; x=1746533598; darn=kvack.org; 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=XGwtrgjfssbRJnnLGAioENkMU7oIKogOgkQ9Ev2GRas=; b=UBayS79FxHYN4srObHl6EFkU7nn3NBF29+Yck3UQ27pwuRSR1/ibIldZtA3N4kq0sR LfScnGa7NClUVK0hE6gk8s9/wDdCD8srUW5SsWwfqTcdnYRlERPujZd3cH58+N5jQdyj wqBOk29oTZ+ruU2VDcYRdOgGCdnCxNWhPtQlZTpiOAPcf4xiIl3V5uSSWGUAUCZnPyk7 ZlMT1sLG5O8zmOrj4tzhNYS6nWwpaq4wFnxB6k4wD/jCuiHPJmIfWVSOb6yeIw2p4OcP cYEYg8ARol4NLWbBTJNj0B66c+G/+nAg89cAADOsMKAf1u8Q3jYqVm54bC80JUaUJPCj vb/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745928798; x=1746533598; 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=XGwtrgjfssbRJnnLGAioENkMU7oIKogOgkQ9Ev2GRas=; b=A9YEx+BeN/jNfJS07v9IpCm3n0nqJv9T7y8YRC3pBEGr1IZpjyTPHpNki0kSlv5AF2 BkmdIvNkdZrMQQKVdnSFyGZU+goxIYgk3mKu8X+mz+dTi63B3ebkGQrpHgTzIKOnUvRS doDT684zjR4qNRXU1NXBJ/1jl0bU3FqgabCb0fV0u9V8u3GZrzkS3NUpMAfsclq9BGNO 8u6Yi9rPkxNPx2mSc9uEp+b40+2oNKE6Erl71kzizfSR4BaYKVXcHe73PtIDxOGF0NDf AOsxwfX6zhfueGI1mlUuAr0a2nKmnvE2uhxnGAbzhPYRZygW/gAhD3GUKSLqiqfoP/J0 OZHQ== X-Forwarded-Encrypted: i=1; AJvYcCVO4MgJOCCN2i82iesg8cdaxHVhLBPjmZAr/u5pwyd9WG6iUsHhH2Zg3jkQH25VHWPiiBWCBCDgdw==@kvack.org X-Gm-Message-State: AOJu0YwAK9Fh9LXku2D9Eia7AD3IdsvC9gA2SAQjuCwVIstqbPLyD+Sr S7Vf6RCG6gMQoV1v3lg/2s0z4ytOjfiEJtRVMkwF4CBTyhDBgOkBkkK6mEAaXNY= X-Gm-Gg: ASbGnctr5Fls+QPKm5nXRlUIMevf+C6CW7/xkgFayJOmjUQ0UDf+CZ1nPw+6iLGG2ZY ZEJRGpQrd+P2e5XLYpaZM+5DlCuIbH1h4VPg9NcaR5ccKPT1b3BjRj/3O5r2BHJXRGI/e7ZvDtk mZceWAL9tGpVx5m2xnIksu3LpM1QwXLZC0ffgAn4o9Wq8FL3UeHlIyP0U0A5oR6ytdpjoiFv+/P 3QhOqQmQ7eFeoOvNFXtGxNVMeFkRVfgXA5oRdQQ5OVB8zm+biaTh+0Nl1bV4qOld16sl/Q8JpEc ngNOZzd9UhtCMpImwVPMWJCA3v5fxzVCnqsfN49S33MoGAN9JfA8CA== X-Google-Smtp-Source: AGHT+IEo5qlskE04ChdYwUfeG14jGRzrfDGg7ko0hloQlCXfAEEdIv4iz53wIbhKxTpKz7/q/ZoqMg== X-Received: by 2002:a05:6402:5112:b0:5ed:bab5:3093 with SMTP id 4fb4d7f45d1cf-5f839b24397mr2697613a12.16.1745928797630; Tue, 29 Apr 2025 05:13:17 -0700 (PDT) Received: from localhost (109-81-85-148.rct.o2.cz. [109.81.85.148]) by smtp.gmail.com with UTF8SMTPSA id 4fb4d7f45d1cf-5f7038328f1sm7319317a12.70.2025.04.29.05.13.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Apr 2025 05:13:17 -0700 (PDT) Date: Tue, 29 Apr 2025 14:13:16 +0200 From: Michal Hocko To: Shakeel Butt Cc: Andrew Morton , Johannes Weiner , Roman Gushchin , Muchun Song , Vlastimil Babka , Jakub Kicinski , Eric Dumazet , Soheil Hassas Yeganeh , linux-mm@kvack.org, cgroups@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Meta kernel team Subject: Re: [PATCH] memcg: multi-memcg percpu charge cache Message-ID: References: <20250416180229.2902751-1-shakeel.butt@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250416180229.2902751-1-shakeel.butt@linux.dev> X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 2E76C40010 X-Stat-Signature: 5c9834jrzrxbisjrk63bzowfb8o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Buqq+9UlVa/ZQxB2YhrwCeigPyRwZ3kaYazx1/gWDj/gWKjUjFFKgjiOAGVGWHJ5nD4CMxj0dN6plChttgU6nCiLvbQATBcROUuByFg7Z2jVBFB0b1kE5ubpDDHZGQYMCu1BJUrgi0oIHy9t7bvqHeKlwT82yLPFcfvowv+2V5Dxr5fTld5nV2uKcjqq9qW5I6sTYGK6Un09kR2O7PPq6GkzGCxzisL/1WX6JAR03ezI/nUQzi96G5+DzKuZ+gw/8ezEDt6JRLwsLyupmCGCixaXUZ3GcCV9iryZDF3L5jTmkFwpBajwpa1ygqVD6yK7tHH5HhGQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed 16-04-25 11:02:29, Shakeel Butt wrote: > Memory cgroup accounting is expensive and to reduce the cost, the kernel > maintains per-cpu charge cache for a single memcg. So, if a charge > request comes for a different memcg, the kernel will flush the old > memcg's charge cache and then charge the newer memcg a fixed amount (64 > pages), subtracts the charge request amount and stores the remaining in > the per-cpu charge cache for the newer memcg. > > This mechanism is based on the assumption that the kernel, for locality, > keep a process on a CPU for long period of time and most of the charge > requests from that process will be served by that CPU's local charge > cache. > > However this assumption breaks down for incoming network traffic in a > multi-tenant machine. We are in the process of running multiple > workloads on a single machine and if such workloads are network heavy, > we are seeing very high network memory accounting cost. We have observed > multiple CPUs spending almost 100% of their time in net_rx_action and > almost all of that time is spent in memcg accounting of the network > traffic. > > More precisely, net_rx_action is serving packets from multiple workloads > and is observing/serving mix of packets of these workloads. The memcg > switch of per-cpu cache is very expensive and we are observing a lot of > memcg switches on the machine. Almost all the time is being spent on > charging new memcg and flushing older memcg cache. So, definitely we > need per-cpu cache that support multiple memcgs for this scenario. > > This patch implements a simple (and dumb) multiple memcg percpu charge > cache. Actually we started with more sophisticated LRU based approach but > the dumb one was always better than the sophisticated one by 1% to 3%, > so going with the simple approach. Makes sense to start simple and go for a more sophisticated (has table appraoch maybe) later when a clear gain could be demonstrated. > Some of the design choices are: > > 1. Fit all caches memcgs in a single cacheline. Could you be more specific about the reasoning? I suspect it is for the network receive path you are mentioning above, right? > 2. The cache array can be mix of empty slots or memcg charged slots, so > the kernel has to traverse the full array. > 3. The cache drain from the reclaim will drain all cached memcgs to keep > things simple. > > To evaluate the impact of this optimization, on a 72 CPUs machine, we > ran the following workload where each netperf client runs in a different > cgroup. The next-20250415 kernel is used as base. > > $ netserver -6 > $ netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K > > number of clients | Without patch | With patch > 6 | 42584.1 Mbps | 48603.4 Mbps (14.13% improvement) > 12 | 30617.1 Mbps | 47919.7 Mbps (56.51% improvement) > 18 | 25305.2 Mbps | 45497.3 Mbps (79.79% improvement) > 24 | 20104.1 Mbps | 37907.7 Mbps (88.55% improvement) > 30 | 14702.4 Mbps | 30746.5 Mbps (109.12% improvement) > 36 | 10801.5 Mbps | 26476.3 Mbps (145.11% improvement) > > The results show drastic improvement for network intensive workloads. > > Signed-off-by: Shakeel Butt Just a minor suggestion below. Other than that looks good to me (with follow up fixes) in this thread. Acked-by: Michal Hocko Thanks! > --- > mm/memcontrol.c | 128 ++++++++++++++++++++++++++++++++++-------------- > 1 file changed, 91 insertions(+), 37 deletions(-) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 1ad326e871c1..0a02ba07561e 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -1769,10 +1769,11 @@ void mem_cgroup_print_oom_group(struct mem_cgroup *memcg) > pr_cont(" are going to be killed due to memory.oom.group set\n"); > } > /* Make sure nr_pages and cached fit into a single cache line */ > +#define NR_MEMCG_STOCK 7 > struct memcg_stock_pcp { > local_trylock_t stock_lock; > - struct mem_cgroup *cached; /* this never be root cgroup */ > - unsigned int nr_pages; > + uint8_t nr_pages[NR_MEMCG_STOCK]; > + struct mem_cgroup *cached[NR_MEMCG_STOCK]; > > struct obj_cgroup *cached_objcg; > struct pglist_data *cached_pgdat; [...] -- Michal Hocko SUSE Labs