From nobody Mon May 25 01:13:05 2026 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DFF5C304BDF for ; Wed, 20 May 2026 05:31:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779255117; cv=none; b=PZ4AlVlUJt9cE2CdWVfEB1a0hLCBbiDomBSn7Vu6eFHfUPhA3gfzfM7NrUZ7g1x27tIDYukYv5IFIagwPTVukaJWdQYhHTGPYhAe24oA+3VU+kDCAIjN2sGu7P13ejAvC/v6Y5IRiRLJMmmP/eaw7w5BQ/djW4YG/IeokBaI59c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779255117; c=relaxed/simple; bh=K/PLv88eqYnhm3qPrn875AQJAdvkBQzK3awXGxOfqnk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b5nsjtKzqCOdVc/0PxI4hR3IyyDdouE1KtYKZmMy2soY4XX7E/gcmXXNvWv8jTc8/yUdwOlKNXY5+FSFlSouLaMr9AAZEDahzau9L1Kq/19i+c4b97TKgrWujxcZDWMhdcBEMCfKMhpCEGetTezvWraCZfcKqFmOVpfjSrvZeYw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=nO/4iKEn; arc=none smtp.client-ip=95.215.58.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="nO/4iKEn" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1779255104; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kz9eWjLv9LTU/HF3o4z9RuRje/Iqp2isOufx/xVO/40=; b=nO/4iKEnnLh8low2bw0UgwHeEbtkmaOlRQ+C++sFKPF63vRHCjQSN5ZF3YTTufyJaWf/q7 IgbNV3MrXSXH78XAJcadPHFaU4LQY5sPjCuNFD/t1hHD01syJQEA5ViS72qvQiHHFo0Nyq 8rQ92WUHjXLj/FUoAvLQklEam49OUyA= From: Shakeel Butt To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Qi Zheng , Alexandre Ghiti , Joshua Hahn , Harry Yoo , Meta kernel team , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel test robot Subject: [PATCH 1/4] memcg: store node_id instead of pglist_data pointer Date: Tue, 19 May 2026 22:31:19 -0700 Message-ID: <20260520053123.2709959-2-shakeel.butt@linux.dev> In-Reply-To: <20260520053123.2709959-1-shakeel.butt@linux.dev> References: <20260520053123.2709959-1-shakeel.butt@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" The struct obj_stock_pcp stores a pointer to pglist_data for the slab stats cached on the cpu. On 64-bit machines, this costs 8 bytes. The pointer is not strictly required: NODE_DATA() can recover it from the node id. Replace cached_pgdat with int16_t node_id and use NUMA_NO_NODE as the "no stats cached" sentinel. At the moment all the archs limit MAX_NUMNODES to 1024 so int16_t is plenty; a BUILD_BUG_ON() makes sure we notice if that ever changes. Signed-off-by: Shakeel Butt Tested-by: kernel test robot Acked-by: Muchun Song Reported-by: kernel test robot Reviewed-by: Harry Yoo (Oracle) --- mm/memcontrol.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b8caeb7ccaa3..d7c162946719 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2021,7 +2021,7 @@ struct obj_stock_pcp { local_trylock_t lock; unsigned int nr_bytes; struct obj_cgroup *cached_objcg; - struct pglist_data *cached_pgdat; + int16_t node_id; int nr_slab_reclaimable_b; int nr_slab_unreclaimable_b; =20 @@ -2031,6 +2031,7 @@ struct obj_stock_pcp { =20 static DEFINE_PER_CPU_ALIGNED(struct obj_stock_pcp, obj_stock) =3D { .lock =3D INIT_LOCAL_TRYLOCK(lock), + .node_id =3D NUMA_NO_NODE, }; =20 static DEFINE_MUTEX(percpu_charge_mutex); @@ -3159,6 +3160,13 @@ static void __account_obj_stock(struct obj_cgroup *o= bjcg, { int *bytes; =20 + /* + * Though at the moment MAX_NUMNODES <=3D 1024 in all archs but let's make + * sure it does not exceed S16_MAX otherwise we need to fix node_id type + * in struct obj_stock_pcp. + */ + BUILD_BUG_ON(MAX_NUMNODES >=3D S16_MAX); + if (!stock || READ_ONCE(stock->cached_objcg) !=3D objcg) goto direct; =20 @@ -3166,9 +3174,11 @@ static void __account_obj_stock(struct obj_cgroup *o= bjcg, * Save vmstat data in stock and skip vmstat array update unless * accumulating over a page of vmstat data or when pgdat changes. */ - if (stock->cached_pgdat !=3D pgdat) { + if (stock->node_id =3D=3D NUMA_NO_NODE) { + stock->node_id =3D pgdat->node_id; + } else if (stock->node_id !=3D pgdat->node_id) { /* Flush the existing cached vmstat data */ - struct pglist_data *oldpg =3D stock->cached_pgdat; + struct pglist_data *oldpg =3D NODE_DATA(stock->node_id); =20 if (stock->nr_slab_reclaimable_b) { mod_objcg_mlstate(objcg, oldpg, NR_SLAB_RECLAIMABLE_B, @@ -3180,7 +3190,7 @@ static void __account_obj_stock(struct obj_cgroup *ob= jcg, stock->nr_slab_unreclaimable_b); stock->nr_slab_unreclaimable_b =3D 0; } - stock->cached_pgdat =3D pgdat; + stock->node_id =3D pgdat->node_id; } =20 bytes =3D (idx =3D=3D NR_SLAB_RECLAIMABLE_B) ? &stock->nr_slab_reclaimabl= e_b @@ -3276,19 +3286,21 @@ static void drain_obj_stock(struct obj_stock_pcp *s= tock) * Flush the vmstat data in current stock */ if (stock->nr_slab_reclaimable_b || stock->nr_slab_unreclaimable_b) { + struct pglist_data *oldpg =3D NODE_DATA(stock->node_id); + if (stock->nr_slab_reclaimable_b) { - mod_objcg_mlstate(old, stock->cached_pgdat, + mod_objcg_mlstate(old, oldpg, NR_SLAB_RECLAIMABLE_B, stock->nr_slab_reclaimable_b); stock->nr_slab_reclaimable_b =3D 0; } if (stock->nr_slab_unreclaimable_b) { - mod_objcg_mlstate(old, stock->cached_pgdat, + mod_objcg_mlstate(old, oldpg, NR_SLAB_UNRECLAIMABLE_B, stock->nr_slab_unreclaimable_b); stock->nr_slab_unreclaimable_b =3D 0; } - stock->cached_pgdat =3D NULL; + stock->node_id =3D NUMA_NO_NODE; } =20 WRITE_ONCE(stock->cached_objcg, NULL); --=20 2.53.0-Meta From nobody Mon May 25 01:13:05 2026 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D6F962F8EB8 for ; Wed, 20 May 2026 05:31:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779255113; cv=none; b=a5HsnE3D57lyFLfzijY7bxZ93aznweDoMa0qsrudG8L4Wf1fWiYi5UNOrsrlBXMm5lWF8Xh6bedV+sEDv1NoTmj/9hrDKt6LBdW2/gvkeJihF8LX6FSSnrCqFGemhzJHJ6Klqg5DddkXbWzGI7SA8a21oD5CxF039f2nC2YI2IM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779255113; c=relaxed/simple; bh=hh+ZsmGobXMaY0Ip8QsYs3UUrV387b2EJ80uC+55muE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Fc2dwOWOUzblmvEEutEQAanE9mzqtvcZa9nO5+choTS2xJ0Xx3GzOmbBUdypSmCS82r4LRnrk4ffnXfkJe934fXgpKZec2JWQlcP3rTc6G1hwh9RbSYtr3MQQZDezhyMv4ghuKthzEa1ug+6Rtxl0v6XSWB8xu+l/tcbLlebfWQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=MjfF36VE; arc=none smtp.client-ip=91.218.175.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="MjfF36VE" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1779255109; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7lMrdf7YGJu8aP4LNTUgpxJU2AI9pB1PVPoOsH0nIso=; b=MjfF36VE6N0qJIroEBs3c8RWjCvifrIFRf2W+FK3y8LIJRXWDxnQC/v/jUJFBuKam/L0zx bzI80G/MdOX7cufTWl3IuwEU71x0Cw4SCh6A6yXRuXXfrGSpy5QNgEMwDVFhQ06NK4oPx+ hSoRJGXnisy5W5ZAcSXHPP2vrm3quXg= From: Shakeel Butt To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Qi Zheng , Alexandre Ghiti , Joshua Hahn , Harry Yoo , Meta kernel team , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel test robot Subject: [PATCH 2/4] memcg: uint16_t for nr_bytes in obj_stock_pcp Date: Tue, 19 May 2026 22:31:20 -0700 Message-ID: <20260520053123.2709959-3-shakeel.butt@linux.dev> In-Reply-To: <20260520053123.2709959-1-shakeel.butt@linux.dev> References: <20260520053123.2709959-1-shakeel.butt@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Currently struct obj_stock_pcp stores nr_bytes in an 'unsigned int' which is 4 bytes on 64-bit machines. Switch the field to uint16_t to shrink the per-CPU cache. The kernel supports PAGE_SIZE_4KB, _8KB, _16KB, _32KB, _64KB and _256KB (see HAVE_PAGE_SIZE_* in arch/Kconfig). After the PAGE_SIZE-aligned flush in __refill_obj_stock(), the sub-page remainder fits in uint16_t up through 64KiB pages where PAGE_SIZE - 1 =3D=3D U16_MAX, but on 256KiB pages PAGE_SIZE - 1 =3D=3D 0x3FFFF exceeds U16_MAX. The accumulator also needs to stay within uint16_t between page-aligned flushes on 64KiB pages where PAGE_SIZE itself is U16_MAX + 1. Accumulate the new total in an 'unsigned int' local, then: 1. Flush whenever the accumulator would hit U16_MAX. Together with the existing allow_uncharge flush at PAGE_SIZE, this keeps the uint16_t safe on PAGE_SIZE <=3D 64KiB. 2. On configs with PAGE_SHIFT > 16 (PAGE_SIZE_256KB on hexagon and powerpc 44x), push any sub-page remainder above U16_MAX into objcg->nr_charged_bytes via atomic_add before storing back, so the store cannot silently truncate. The PAGE_SHIFT > 16 guard folds the branch out at compile time on smaller page sizes. Signed-off-by: Shakeel Butt Tested-by: kernel test robot Reported-by: kernel test robot Reviewed-by: Harry Yoo (Oracle) --- mm/memcontrol.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index d7c162946719..b3d63d9f267c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2019,7 +2019,7 @@ static DEFINE_PER_CPU_ALIGNED(struct memcg_stock_pcp,= memcg_stock) =3D { =20 struct obj_stock_pcp { local_trylock_t lock; - unsigned int nr_bytes; + uint16_t nr_bytes; struct obj_cgroup *cached_objcg; int16_t node_id; int nr_slab_reclaimable_b; @@ -3331,6 +3331,7 @@ static void __refill_obj_stock(struct obj_cgroup *obj= cg, bool allow_uncharge) { unsigned int nr_pages =3D 0; + unsigned int stock_nr_bytes; =20 if (!stock) { nr_pages =3D nr_bytes >> PAGE_SHIFT; @@ -3339,21 +3340,41 @@ static void __refill_obj_stock(struct obj_cgroup *o= bjcg, goto out; } =20 + stock_nr_bytes =3D stock->nr_bytes; if (READ_ONCE(stock->cached_objcg) !=3D objcg) { /* reset if necessary */ drain_obj_stock(stock); obj_cgroup_get(objcg); - stock->nr_bytes =3D atomic_read(&objcg->nr_charged_bytes) + stock_nr_bytes =3D atomic_read(&objcg->nr_charged_bytes) ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0; WRITE_ONCE(stock->cached_objcg, objcg); =20 allow_uncharge =3D true; /* Allow uncharge when objcg changes */ } - stock->nr_bytes +=3D nr_bytes; + stock_nr_bytes +=3D nr_bytes; + + /* Since stock->nr_bytes is uint16_t, don't refill >=3D U16_MAX */ + if ((allow_uncharge && (stock_nr_bytes > PAGE_SIZE)) || + stock_nr_bytes >=3D U16_MAX) { + nr_pages =3D stock_nr_bytes >> PAGE_SHIFT; + stock_nr_bytes &=3D (PAGE_SIZE - 1); + + /* + * On configs with PAGE_SHIFT > 16 (PAGE_SIZE_256KB on + * hexagon and powerpc 44x), the sub-page remainder can + * still exceed U16_MAX. Push the excess back to + * objcg->nr_charged_bytes so the store into uint16_t + * cannot silently truncate; folded out at compile time + * on smaller page sizes. + */ + if (PAGE_SHIFT > 16 && stock_nr_bytes > U16_MAX) { + unsigned int kept =3D stock_nr_bytes & U16_MAX; =20 - if (allow_uncharge && (stock->nr_bytes > PAGE_SIZE)) { - nr_pages =3D stock->nr_bytes >> PAGE_SHIFT; - stock->nr_bytes &=3D (PAGE_SIZE - 1); + atomic_add(stock_nr_bytes - kept, + &objcg->nr_charged_bytes); + stock_nr_bytes =3D kept; + } } + stock->nr_bytes =3D stock_nr_bytes; =20 out: if (nr_pages) --=20 2.53.0-Meta From nobody Mon May 25 01:13:05 2026 Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A48D53019D8 for ; Wed, 20 May 2026 05:32:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779255127; cv=none; b=GCBTkih8s2JVepa5ASUuvHCMYI4UuFlN2n2p/0gKI59KmfqpLNMeWLUcQIQsBpfRtuNVcdGRXneLIUiKYteWuW4qZgX0NNOzV8qyPmWh3//TcS//KDRH48Q0uAUcvG/Bzuk+pNCD8CRgFMOYTvuEtN8RabP5Ox74qVbvlvdp/KI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779255127; c=relaxed/simple; bh=SzehFdc2LM/qe24HkIFFk6y9UM5HSB4TRz02KugGNu0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=etSewtObNSjA8Eh/8+/MR6YnXFUneRyq1kLALMXMT7Un1ni34j7rET241tcC08sjYgOu9UauJxFQkkfaILrKtOoZr6l09hx9nbwLYUBEawXLoLOFKqSG4ABRTncYngJlN++LCFGKkj6AWPAFe1ice+2qw9QMcFGlMavsd2B3wuo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=nh0efrQN; arc=none smtp.client-ip=95.215.58.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="nh0efrQN" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1779255123; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vmA/uDHx5nJVlFD8/rulPbhnYG25SCWOMR7+eU0w1XU=; b=nh0efrQN6xF7hr28/JCnR3ccyOfMRqcb/ECQDM+qqDdSWCyLv9oq21aX54yJBDmmHEk7nr Dzdts2JOIWvCWjuQlOfTkR4i5zYbYE02WhohsRdHxr+tjOxpv1wIBM18+NHw9Fhs+Gx7aM +HiHuQlXDuEpvY69ImnWCS3MR3rgrF8= From: Shakeel Butt To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Qi Zheng , Alexandre Ghiti , Joshua Hahn , Harry Yoo , Meta kernel team , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel test robot Subject: [PATCH 3/4] memcg: int16_t for cached slab stats Date: Tue, 19 May 2026 22:31:21 -0700 Message-ID: <20260520053123.2709959-4-shakeel.butt@linux.dev> In-Reply-To: <20260520053123.2709959-1-shakeel.butt@linux.dev> References: <20260520053123.2709959-1-shakeel.butt@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Currently struct obj_stock_pcp stores cached slab stats in 'int' which is 4 bytes per counter on 64-bit machines. Switch them to int16_t to shrink the cached metadata. The existing PAGE_SIZE flush in __account_obj_stock() bounds *bytes at PAGE_SIZE on 4KiB and 16KiB page archs, well within int16_t. On 64KiB pages PAGE_SIZE is well above S16_MAX so that flush never fires, and a sufficiently long run of accumulations would overflow the cache. Add an explicit S16_MAX guard before each add: when the next add would push abs(*bytes) past S16_MAX, fold the cached value into @nr and flush directly via mod_objcg_mlstate() before the accumulation. Signed-off-by: Shakeel Butt Tested-by: kernel test robot Reported-by: kernel test robot Reviewed-by: Harry Yoo (Oracle) --- mm/memcontrol.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b3d63d9f267c..1ed27fd06850 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2022,8 +2022,8 @@ struct obj_stock_pcp { uint16_t nr_bytes; struct obj_cgroup *cached_objcg; int16_t node_id; - int nr_slab_reclaimable_b; - int nr_slab_unreclaimable_b; + int16_t nr_slab_reclaimable_b; + int16_t nr_slab_unreclaimable_b; =20 struct work_struct work; unsigned long flags; @@ -3158,7 +3158,7 @@ static void __account_obj_stock(struct obj_cgroup *ob= jcg, struct obj_stock_pcp *stock, int nr, struct pglist_data *pgdat, enum node_stat_item idx) { - int *bytes; + int16_t *bytes; =20 /* * Though at the moment MAX_NUMNODES <=3D 1024 in all archs but let's make @@ -3195,6 +3195,16 @@ static void __account_obj_stock(struct obj_cgroup *o= bjcg, =20 bytes =3D (idx =3D=3D NR_SLAB_RECLAIMABLE_B) ? &stock->nr_slab_reclaimabl= e_b : &stock->nr_slab_unreclaimable_b; + /* + * To avoid overflow or underflow, flush directly if accumulating @nr + * would push the cached value past S16_MAX. + */ + if (abs(nr + *bytes) >=3D S16_MAX) { + nr +=3D *bytes; + *bytes =3D 0; + goto direct; + } + /* * Even for large object >=3D PAGE_SIZE, the vmstat data will still be * cached locally at least once before pushing it out. --=20 2.53.0-Meta From nobody Mon May 25 01:13:05 2026 Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84BA82F8EB8 for ; Wed, 20 May 2026 05:32:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779255138; cv=none; b=KP7fcx73WsboXKJ1FQ11TCqkpC1MF3AkAkKaWPcxcpG/Lk71r2jOO12PZZh1HcTVOGQyRhzEV+j1+agd34rQT8nLB2WibsqHaHW+vY1k1tRPmkbl11lDhlJ13KAa/gIh+VrBJy6qgGtH0SJGdqfmjihN/xyQE1J0WW0t38KduQE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779255138; c=relaxed/simple; bh=r1OKmPJykDl3nsgWMXXU9YSyK+/oVa4WOsla/TLp/Dc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=t6CCb5jpu44b/SIstlSElP/VTOD+k3aRBJZfZXnk3MgpGORIVwVxR/tQaFFIYIF7U7kEP5ardlOMa9tAUbVAK6JMctzINEmCihcciuThCR4PcbT09FcKGVKyuZ1H9OvdsPE4oMI/NQ/KX6+5f9vb7CQF57dKrQj2dFPzKAb7x6s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=aKP1eQ2O; arc=none smtp.client-ip=95.215.58.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="aKP1eQ2O" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1779255134; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WYHpRVqTIOTPs8poaWY6JWzoP4/vBp/W0Ib884JadBM=; b=aKP1eQ2OWIiH+OkE2FsHyXPXO2I9NnQh9i6MNR0MBo9a3tA6SU413D11muMhrpD+zRoW1h ceLNN5KZkOUGqUSTUuHTVm8U5FlhPwCplfeyVwu+Zq/PCjg0YnZiA99esR4Ygx9eRtr+DG +1CFOdllAn4deiMztZV53eSWPUxJWs0= From: Shakeel Butt To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Qi Zheng , Alexandre Ghiti , Joshua Hahn , Harry Yoo , Meta kernel team , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel test robot Subject: [PATCH 4/4] memcg: multi objcg charge support Date: Tue, 19 May 2026 22:31:22 -0700 Message-ID: <20260520053123.2709959-5-shakeel.butt@linux.dev> In-Reply-To: <20260520053123.2709959-1-shakeel.butt@linux.dev> References: <20260520053123.2709959-1-shakeel.butt@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Commit 01b9da291c49 ("mm: memcontrol: convert objcg to be per-memcg per-node type") split a memcg's single obj_cgroup into one per NUMA node so that reparenting LRU folios can take per-node lru locks. As a side effect, the per-CPU obj_stock_pcp -- which caches exactly one cached_objcg -- thrashes on workloads where threads of the same memcg run on different NUMA nodes. The kernel test robot reported a 67.7% regression on stress-ng.switch.ops_per_sec from this pattern. Mirror the multi-slot pattern already used by memcg_stock_pcp: turn nr_bytes and cached_objcg into NR_OBJ_STOCK-element arrays, scan all slots on consume/refill/account, prefer empty slots when inserting, and evict a random slot only when full. With multiple slots a CPU can hold the per-node objcg variants of one memcg plus a few siblings without ever forcing a drain. A single int8_t index records which slot the cached slab stats belong to; the stats are flushed on slot or pgdat change. With NR_OBJ_STOCK =3D 5 the layout (verified with pahole) is: offset 0 : lock(1) + index(1) + node_id(2) + slab stats(4) =3D 8B offset 8 : nr_bytes[5] =3D 10B offset 18 : padding =3D 6B offset 24 : cached[5] =3D 40B offset 64 : (line 2) work_struct + flags (cold) so consume_obj_stock, refill_obj_stock and the slab account path each touch exactly one 64-byte cache line on non-debug 64-bit builds. Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-lkp/202605121641.b6a60cb0-lkp@intel.com Fixes: 01b9da291c49 ("mm: memcontrol: convert objcg to be per-memcg per-nod= e type") Signed-off-by: Shakeel Butt Tested-by: kernel test robot --- mm/memcontrol.c | 185 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 133 insertions(+), 52 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 1ed27fd06850..52104cbb8e7c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -150,14 +150,14 @@ static void obj_cgroup_release(struct percpu_ref *ref) * However, it can be PAGE_SIZE or (x * PAGE_SIZE). * * The following sequence can lead to it: - * 1) CPU0: objcg =3D=3D stock->cached_objcg + * 1) CPU0: objcg cached in one of stock->cached[i] * 2) CPU1: we do a small allocation (e.g. 92 bytes), * PAGE_SIZE bytes are charged * 3) CPU1: a process from another memcg is allocating something, * the stock if flushed, * objcg->nr_charged_bytes =3D PAGE_SIZE - 92 * 5) CPU0: we do release this object, - * 92 bytes are added to stock->nr_bytes + * 92 bytes are added to stock->nr_bytes[i] * 6) CPU0: stock is flushed, * 92 bytes are added to objcg->nr_charged_bytes * @@ -2017,13 +2017,25 @@ static DEFINE_PER_CPU_ALIGNED(struct memcg_stock_pc= p, memcg_stock) =3D { .lock =3D INIT_LOCAL_TRYLOCK(lock), }; =20 +/* + * NR_OBJ_STOCK is sized so the entire hot path of obj_stock_pcp + * (lock, accounting metadata, nr_bytes[] and cached[]) fits within a + * single 64-byte cache line on non-debug 64-bit builds. With 5 slots: + * lock(1) + index(1) + node_id(2) + slab stats(4) + nr_bytes(10) + * + pad(6) + cached(40) =3D=3D 64 bytes. + * A CPU can thus consume/refill/account against five different objcgs + * (typically per-node variants of the same memcg) while incurring at + * most one cache miss on the stock. + */ +#define NR_OBJ_STOCK 5 struct obj_stock_pcp { local_trylock_t lock; - uint16_t nr_bytes; - struct obj_cgroup *cached_objcg; + int8_t index; int16_t node_id; int16_t nr_slab_reclaimable_b; int16_t nr_slab_unreclaimable_b; + uint16_t nr_bytes[NR_OBJ_STOCK]; + struct obj_cgroup *cached[NR_OBJ_STOCK]; =20 struct work_struct work; unsigned long flags; @@ -2031,11 +2043,13 @@ struct obj_stock_pcp { =20 static DEFINE_PER_CPU_ALIGNED(struct obj_stock_pcp, obj_stock) =3D { .lock =3D INIT_LOCAL_TRYLOCK(lock), + .index =3D -1, .node_id =3D NUMA_NO_NODE, }; =20 static DEFINE_MUTEX(percpu_charge_mutex); =20 +static void drain_obj_stock_slot(struct obj_stock_pcp *stock, int i); static void drain_obj_stock(struct obj_stock_pcp *stock); static bool obj_stock_flush_required(struct obj_stock_pcp *stock, struct mem_cgroup *root_memcg); @@ -3153,12 +3167,13 @@ static void unlock_stock(struct obj_stock_pcp *stoc= k) local_unlock(&obj_stock.lock); } =20 -/* Call after __refill_obj_stock() to ensure stock->cached_objg =3D=3D obj= cg */ +/* Call after __refill_obj_stock() so a slot for objcg exists in the stock= */ static void __account_obj_stock(struct obj_cgroup *objcg, struct obj_stock_pcp *stock, int nr, struct pglist_data *pgdat, enum node_stat_item idx) { int16_t *bytes; + int i; =20 /* * Though at the moment MAX_NUMNODES <=3D 1024 in all archs but let's make @@ -3167,29 +3182,39 @@ static void __account_obj_stock(struct obj_cgroup *= objcg, */ BUILD_BUG_ON(MAX_NUMNODES >=3D S16_MAX); =20 - if (!stock || READ_ONCE(stock->cached_objcg) !=3D objcg) + if (!stock) + goto direct; + + for (i =3D 0; i < NR_OBJ_STOCK; ++i) { + if (READ_ONCE(stock->cached[i]) =3D=3D objcg) + break; + } + if (i =3D=3D NR_OBJ_STOCK) goto direct; =20 /* * Save vmstat data in stock and skip vmstat array update unless - * accumulating over a page of vmstat data or when pgdat changes. + * accumulating over a page of vmstat data or when the objcg slot or + * pgdat the stats belong to changes. */ - if (stock->node_id =3D=3D NUMA_NO_NODE) { + if (stock->index < 0) { + stock->index =3D i; stock->node_id =3D pgdat->node_id; - } else if (stock->node_id !=3D pgdat->node_id) { - /* Flush the existing cached vmstat data */ + } else if (stock->index !=3D i || stock->node_id !=3D pgdat->node_id) { + struct obj_cgroup *old =3D READ_ONCE(stock->cached[stock->index]); struct pglist_data *oldpg =3D NODE_DATA(stock->node_id); =20 if (stock->nr_slab_reclaimable_b) { - mod_objcg_mlstate(objcg, oldpg, NR_SLAB_RECLAIMABLE_B, + mod_objcg_mlstate(old, oldpg, NR_SLAB_RECLAIMABLE_B, stock->nr_slab_reclaimable_b); stock->nr_slab_reclaimable_b =3D 0; } if (stock->nr_slab_unreclaimable_b) { - mod_objcg_mlstate(objcg, oldpg, NR_SLAB_UNRECLAIMABLE_B, + mod_objcg_mlstate(old, oldpg, NR_SLAB_UNRECLAIMABLE_B, stock->nr_slab_unreclaimable_b); stock->nr_slab_unreclaimable_b =3D 0; } + stock->index =3D i; stock->node_id =3D pgdat->node_id; } =20 @@ -3230,10 +3255,16 @@ static bool __consume_obj_stock(struct obj_cgroup *= objcg, struct obj_stock_pcp *stock, unsigned int nr_bytes) { - if (objcg =3D=3D READ_ONCE(stock->cached_objcg) && - stock->nr_bytes >=3D nr_bytes) { - stock->nr_bytes -=3D nr_bytes; - return true; + int i; + + for (i =3D 0; i < NR_OBJ_STOCK; ++i) { + if (READ_ONCE(stock->cached[i]) !=3D objcg) + continue; + if (stock->nr_bytes[i] >=3D nr_bytes) { + stock->nr_bytes[i] -=3D nr_bytes; + return true; + } + return false; } =20 return false; @@ -3254,16 +3285,42 @@ static bool consume_obj_stock(struct obj_cgroup *ob= jcg, unsigned int nr_bytes) return ret; } =20 -static void drain_obj_stock(struct obj_stock_pcp *stock) +/* Flush the cached slab stats (if any) back to their owning objcg/pgdat. = */ +static void drain_obj_stock_stats(struct obj_stock_pcp *stock) { - struct obj_cgroup *old =3D READ_ONCE(stock->cached_objcg); + struct obj_cgroup *old; + struct pglist_data *oldpg; + + if (stock->index < 0) + return; + + old =3D READ_ONCE(stock->cached[stock->index]); + oldpg =3D NODE_DATA(stock->node_id); + + if (stock->nr_slab_reclaimable_b) { + mod_objcg_mlstate(old, oldpg, NR_SLAB_RECLAIMABLE_B, + stock->nr_slab_reclaimable_b); + stock->nr_slab_reclaimable_b =3D 0; + } + if (stock->nr_slab_unreclaimable_b) { + mod_objcg_mlstate(old, oldpg, NR_SLAB_UNRECLAIMABLE_B, + stock->nr_slab_unreclaimable_b); + stock->nr_slab_unreclaimable_b =3D 0; + } + stock->index =3D -1; + stock->node_id =3D NUMA_NO_NODE; +} + +static void drain_obj_stock_slot(struct obj_stock_pcp *stock, int i) +{ + struct obj_cgroup *old =3D READ_ONCE(stock->cached[i]); =20 if (!old) return; =20 - if (stock->nr_bytes) { - unsigned int nr_pages =3D stock->nr_bytes >> PAGE_SHIFT; - unsigned int nr_bytes =3D stock->nr_bytes & (PAGE_SIZE - 1); + if (stock->nr_bytes[i]) { + unsigned int nr_pages =3D stock->nr_bytes[i] >> PAGE_SHIFT; + unsigned int nr_bytes =3D stock->nr_bytes[i] & (PAGE_SIZE - 1); =20 if (nr_pages) { struct mem_cgroup *memcg; @@ -3289,46 +3346,43 @@ static void drain_obj_stock(struct obj_stock_pcp *s= tock) * so it might be changed in the future. */ atomic_add(nr_bytes, &old->nr_charged_bytes); - stock->nr_bytes =3D 0; + stock->nr_bytes[i] =3D 0; } =20 - /* - * Flush the vmstat data in current stock - */ - if (stock->nr_slab_reclaimable_b || stock->nr_slab_unreclaimable_b) { - struct pglist_data *oldpg =3D NODE_DATA(stock->node_id); - - if (stock->nr_slab_reclaimable_b) { - mod_objcg_mlstate(old, oldpg, - NR_SLAB_RECLAIMABLE_B, - stock->nr_slab_reclaimable_b); - stock->nr_slab_reclaimable_b =3D 0; - } - if (stock->nr_slab_unreclaimable_b) { - mod_objcg_mlstate(old, oldpg, - NR_SLAB_UNRECLAIMABLE_B, - stock->nr_slab_unreclaimable_b); - stock->nr_slab_unreclaimable_b =3D 0; - } - stock->node_id =3D NUMA_NO_NODE; - } + /* Flush vmstat data when its owning slot is being drained. */ + if (stock->index =3D=3D i) + drain_obj_stock_stats(stock); =20 - WRITE_ONCE(stock->cached_objcg, NULL); + WRITE_ONCE(stock->cached[i], NULL); obj_cgroup_put(old); } =20 +static void drain_obj_stock(struct obj_stock_pcp *stock) +{ + int i; + + for (i =3D 0; i < NR_OBJ_STOCK; ++i) + drain_obj_stock_slot(stock, i); +} + static bool obj_stock_flush_required(struct obj_stock_pcp *stock, struct mem_cgroup *root_memcg) { - struct obj_cgroup *objcg =3D READ_ONCE(stock->cached_objcg); + struct obj_cgroup *objcg; struct mem_cgroup *memcg; bool flush =3D false; + int i; =20 rcu_read_lock(); - if (objcg) { + for (i =3D 0; i < NR_OBJ_STOCK; ++i) { + objcg =3D READ_ONCE(stock->cached[i]); + if (!objcg) + continue; memcg =3D obj_cgroup_memcg(objcg); - if (memcg && mem_cgroup_is_descendant(memcg, root_memcg)) + if (memcg && mem_cgroup_is_descendant(memcg, root_memcg)) { flush =3D true; + break; + } } rcu_read_unlock(); =20 @@ -3342,6 +3396,7 @@ static void __refill_obj_stock(struct obj_cgroup *obj= cg, { unsigned int nr_pages =3D 0; unsigned int stock_nr_bytes; + int i, slot =3D -1, empty_slot =3D -1; =20 if (!stock) { nr_pages =3D nr_bytes >> PAGE_SHIFT; @@ -3350,19 +3405,45 @@ static void __refill_obj_stock(struct obj_cgroup *o= bjcg, goto out; } =20 - stock_nr_bytes =3D stock->nr_bytes; - if (READ_ONCE(stock->cached_objcg) !=3D objcg) { /* reset if necessary */ - drain_obj_stock(stock); + for (i =3D 0; i < NR_OBJ_STOCK; ++i) { + struct obj_cgroup *cached =3D READ_ONCE(stock->cached[i]); + + if (!cached) { + if (empty_slot =3D=3D -1) + empty_slot =3D i; + continue; + } + if (cached =3D=3D objcg) { + slot =3D i; + break; + } + } + + if (slot =3D=3D -1) { + slot =3D empty_slot; + if (slot =3D=3D -1) { + slot =3D get_random_u32_below(NR_OBJ_STOCK); + drain_obj_stock_slot(stock, slot); + } obj_cgroup_get(objcg); + /* + * Keep the xchg result in the unsigned int local; storing + * it directly into stock->nr_bytes[slot] (uint16_t) would + * silently truncate values >=3D U16_MAX and bypass the flush + * guard below, leaking page-counter charges. + */ stock_nr_bytes =3D atomic_read(&objcg->nr_charged_bytes) ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0; - WRITE_ONCE(stock->cached_objcg, objcg); + WRITE_ONCE(stock->cached[slot], objcg); =20 allow_uncharge =3D true; /* Allow uncharge when objcg changes */ + } else { + stock_nr_bytes =3D stock->nr_bytes[slot]; } + stock_nr_bytes +=3D nr_bytes; =20 - /* Since stock->nr_bytes is uint16_t, don't refill >=3D U16_MAX */ + /* nr_bytes[] is uint16_t; flush if we would refill >=3D U16_MAX. */ if ((allow_uncharge && (stock_nr_bytes > PAGE_SIZE)) || stock_nr_bytes >=3D U16_MAX) { nr_pages =3D stock_nr_bytes >> PAGE_SHIFT; @@ -3384,7 +3465,7 @@ static void __refill_obj_stock(struct obj_cgroup *obj= cg, stock_nr_bytes =3D kept; } } - stock->nr_bytes =3D stock_nr_bytes; + stock->nr_bytes[slot] =3D stock_nr_bytes; =20 out: if (nr_pages) --=20 2.53.0-Meta