From nobody Thu Sep 24 20:37:49 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6B4F5348C66; Mon, 21 Sep 2026 06:16:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789971393; cv=none; b=WB/oilJumNoPfM4hsn6jtEya4aIVRNPSpAnvrsYxUSr0aJn8KSR4lhTH4bklgx3uvZahfvB/qzKEEpgjiHK7F80ku/va80NMkn54O74PJelgVqX+f/bRU5pziC64DWHgPZe5AsuGxp0Ci0gXOjJm2OqFIL+dAPloPEws4KdUm3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789971393; c=relaxed/simple; bh=ZUzMmbQpUdGk2onbydCh5L+k5zmd1xMeNqBbCinbBRE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NrJKnd2iP9/OaAhRbpQJ98kU3sHUzipne/Cq7x1J9kq5mnoqLC3wU1j2DDbfoHOkc5RQOiXsrI0DMCUkUEJLj/kC+MCWY3H1HgdVt/82SM2KWapOECEBBaMhVks7+QSvPfMcb2XUE8IpbsX98zZxtwBt0gr7rzjkyL+fGxwI0sM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mZdKqySA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mZdKqySA" Received: by smtp.kernel.org (Postfix) with ESMTPS id 015A4C2BCF6; Mon, 21 Sep 2026 06:16:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789971393; bh=ZUzMmbQpUdGk2onbydCh5L+k5zmd1xMeNqBbCinbBRE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=mZdKqySApf4RZ48SjtA/U8c78d7SzuJAICSaNwPSpR2V7q+yciJsXGTczr3qeSB/z tz+yv06lGv/Mud/zVSClnAPSJKMUnc4zPlpX2sU3R2cxOMABGrcNw2ljTIFaEBgKMP 53O7gdCM+1Rdh0D7kZ/ioerAMOD7jl56vm5BouH041+zTxFesvsvkKFIir816X9IY/ uSvzfp3Aj5cVnduVxELXRhx+42Taf5Lqaib3MsBucceJQx+d3KlF5dCdxc957YZnaw TY01X1pFNrfiLOgRALffMe8ls32QTkTU9ucge36Na6YoY7vIVnOefkOTZaYmUJnCVw vdYeQx+DFwW7w== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id D48F5C982E1; Mon, 21 Sep 2026 06:16:32 +0000 (UTC) From: Bingfang Guo via B4 Relay Date: Mon, 21 Sep 2026 14:16:24 +0800 Subject: [PATCH v2 1/4] memcg: keep swap charging under RCU protection Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260921-bingfangguo-memcgid-rework-v2-1-6c0637dc0edb@tencent.com> References: <20260921-bingfangguo-memcgid-rework-v2-0-6c0637dc0edb@tencent.com> In-Reply-To: <20260921-bingfangguo-memcgid-rework-v2-0-6c0637dc0edb@tencent.com> To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Dave Chinner , Qi Zheng , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , David Hildenbrand , Lorenzo Stoakes , Bingfang Guo Cc: cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Bingfang Guo X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1789971391; l=2959; i=bingfangguo@tencent.com; s=20260812; h=from:subject:message-id; bh=sE3zMklqkcFz3uR1w6qomhpryX2R8nExKsjfG+k30bI=; b=jYqZPOQzuqXt3gG9v8XXM4BalCcLyDqai9BvgC5xpefOPP5WMa23il4SGzRfiEbjc9+PIIj0H GvQ5nU6AAg/DDtdj1U5G90CxDuQBHe6GOo2QGChpb/L3eR4heGMLTma X-Developer-Key: i=bingfangguo@tencent.com; a=ed25519; pk=q7u9S7d5e7VijWrYF9O+vv9/XvqWduVGtwGtuCObAHE= X-Endpoint-Received: by B4 Relay for bingfangguo@tencent.com/20260812 with auth_id=942 X-Original-From: Bingfang Guo Reply-To: bingfangguo@tencent.com From: Bingfang Guo This is a preparatory work for unbinding memcgid from memcg. No functional change. The swap charging path currently drops its RCU read lock after acquiring a private ID reference. This is safe because the ID reference pins the memcg's CSS. After ID references are moved to pin objcgs, that lifetime guarantee will no longer hold. Keep the RCU read lock held while accessing the memcg for counter charging, statistics and failure handling. (This matches what __memcg1_swapout() already does). Save the private ID for swap memcg recording before dropping the RCU read lock. So the swap cluster locking remains outside the RCU read-side critical section. Signed-off-by: Bingfang Guo Acked-by: Muchun Song --- mm/memcontrol.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 791e536efaebe..981a231a76129 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5954,6 +5954,7 @@ int __mem_cgroup_try_charge_swap(struct folio *folio) struct page_counter *counter; struct mem_cgroup *memcg; struct obj_cgroup *objcg; + unsigned short private_id; =20 if (do_memsw_account()) return 0; @@ -5963,30 +5964,29 @@ int __mem_cgroup_try_charge_swap(struct folio *foli= o) if (!objcg) return 0; =20 - rcu_read_lock(); - memcg =3D obj_cgroup_memcg(objcg); - if (!folio_test_swapcache(folio)) { - memcg_memory_event(memcg, MEMCG_SWAP_FAIL); - rcu_read_unlock(); - return 0; - } + scoped_guard(rcu) { + memcg =3D obj_cgroup_memcg(objcg); + if (!folio_test_swapcache(folio)) { + memcg_memory_event(memcg, MEMCG_SWAP_FAIL); + return 0; + } =20 - memcg =3D mem_cgroup_private_id_get_online(memcg, nr_pages); - /* memcg is pined by memcg ID. */ - rcu_read_unlock(); + memcg =3D mem_cgroup_private_id_get_online(memcg, nr_pages); + /* memcg is pined by memcg ID. */ + private_id =3D mem_cgroup_private_id(memcg); =20 - if (!mem_cgroup_is_root(memcg) && - !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { - memcg_memory_event(memcg, MEMCG_SWAP_MAX); - memcg_memory_event(memcg, MEMCG_SWAP_FAIL); - mem_cgroup_private_id_put(memcg, nr_pages); - return -ENOMEM; + if (!mem_cgroup_is_root(memcg) && + !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { + memcg_memory_event(memcg, MEMCG_SWAP_MAX); + memcg_memory_event(memcg, MEMCG_SWAP_FAIL); + mem_cgroup_private_id_put(memcg, nr_pages); + return -ENOMEM; + } + mod_memcg_state(memcg, MEMCG_SWAP, nr_pages); } - mod_memcg_state(memcg, MEMCG_SWAP, nr_pages); =20 ci =3D swap_cluster_get_and_lock(folio); - __swap_cgroup_set(ci, swp_cluster_offset(folio->swap), nr_pages, - mem_cgroup_private_id(memcg)); + __swap_cgroup_set(ci, swp_cluster_offset(folio->swap), nr_pages, private_= id); swap_cluster_unlock(ci); =20 return 0; --=20 2.43.7 From nobody Thu Sep 24 20:37:49 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6B3E3331EC5; Mon, 21 Sep 2026 06:16:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789971393; cv=none; b=rB9iV0pBktULOlF+Zi5Eq1mN+wsBs2OfPcmI8/wzZvCIuyw+c7j7lp+mk2Nac5ilshTrHlDWqpyyYPBhXvFEJtbPhFeWf6qF+k8QZ9LQmUgbbbvWaCuRqIrMUOMlrEz3ogqLCIwVAaeRH59K/lZkssYtMqoAbaFNjSzOjkrHJIc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789971393; c=relaxed/simple; bh=Z7oPMtzmmdxZysH8NfprVZtdImWm4xoqdqjiYrOmKrM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pV8rKIDi8c5Dbn7vP04Eil66ZQM9zvS3QaezQ0+tjWdb6GiqGk/bysyMxzQmMNF0KyJol84mKxBpCMu71ieZpguqUSHtifc5ULd+kKITrstKr4nN1lPg2mgbI0S3qrZDTKfevh+hvqgLXjxaC3ZoETKFmQHNq0qfnFoBncg0tXY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=B8/S9+vD; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="B8/S9+vD" Received: by smtp.kernel.org (Postfix) with ESMTPS id 11EDDC2BCFB; Mon, 21 Sep 2026 06:16:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789971393; bh=Z7oPMtzmmdxZysH8NfprVZtdImWm4xoqdqjiYrOmKrM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=B8/S9+vDvxGDAjUWVRIfABjPEBAjAy9Nm1hRE6d4sRCbnzlFCxEyTR+AP8bFKK4SM /H7gQozKaAQVm5QxvCGbnbjJN8MMiOIB5hDooWErWbtjoS0xphpxWlR9cdIR9OjLzY Smu5gcZhrumLDeTHLljK29B98Jzw3RJX53SqYF0Rj9uXAKX0PHhb9o/AK3BhUhRnF2 B7uJQKBqnxMQTai4JIBz8C0qO2kLbfuqjY0U4KNxCWVgiaS/OG6r3vXJ0GQiYHuW/4 Vki2HKmPu4ZQyl23/wDJpvnMtfsV3fgD5Y6YxfuNPAGgYJeTMHrCJLgU5QliqNBPLB /qHVn30VZ6/4g== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7756C982E6; Mon, 21 Sep 2026 06:16:32 +0000 (UTC) From: Bingfang Guo via B4 Relay Date: Mon, 21 Sep 2026 14:16:25 +0800 Subject: [PATCH v2 2/4] memcg: base swap charge accounting on memcgid root status Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260921-bingfangguo-memcgid-rework-v2-2-6c0637dc0edb@tencent.com> References: <20260921-bingfangguo-memcgid-rework-v2-0-6c0637dc0edb@tencent.com> In-Reply-To: <20260921-bingfangguo-memcgid-rework-v2-0-6c0637dc0edb@tencent.com> To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Dave Chinner , Qi Zheng , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , David Hildenbrand , Lorenzo Stoakes , Bingfang Guo Cc: cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Bingfang Guo X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1789971391; l=4757; i=bingfangguo@tencent.com; s=20260812; h=from:subject:message-id; bh=9FAyfMKM+7unLAdMxjg2Op7yswvqwLDZxs1E3W/Lmyo=; b=DJXQvDM7Ki8+9464bK5faIcO6vt3cMKvvJ33Y66tpyyuAQyR1gRDHKvAv1MkCrl8UZpXxh0MQ BNC0qblHNNqC/SHLIKTKwIuhj7rHyNsStz7NfFvhohcYC5smbLkRYHS X-Developer-Key: i=bingfangguo@tencent.com; a=ed25519; pk=q7u9S7d5e7VijWrYF9O+vv9/XvqWduVGtwGtuCObAHE= X-Endpoint-Received: by B4 Relay for bingfangguo@tencent.com/20260812 with auth_id=942 X-Original-From: Bingfang Guo Reply-To: bingfangguo@tencent.com From: Bingfang Guo A private ID currently pins its original memcg, so testing whether the ID belongs to root is equivalent to testing whether the resolved memcg is root. That equivalence will no longer hold when IDs refer to objcgs. A non-root ID may resolve to root after reparenting, but its swap entries still carry counter charges inherited by root. Skipping their uncharge based on the resolved memcg would leave those charges behind. Use the private ID's root status to decide whether a swap entry carries a counter charge. A root-ID entry carries none, while a non-root-ID entry must release its charge even if its current accounting memcg has become root. This patch adds a new helper to check if the memcgid equals to that of the root memcg. For swap uncharging and v2 swap charging, simply decide whether to charge/uncharge memsw or swap counter based on the swap memcgid is root or not. For v1 swapout, don't recharge the memsw counter, just cancel the charge if the swap memcg ID refers to the root memcg. This makes memory and swap charging have similar semantics: one relys on the objcg's root status, and the other relys on the memcgid's root status. Signed-off-by: Bingfang Guo Acked-by: Muchun Song --- mm/memcontrol-v1.c | 16 ++++++++-------- mm/memcontrol-v1.h | 5 +++++ mm/memcontrol.c | 4 ++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c index bf2c7d53b01b1..ed015fdd95123 100644 --- a/mm/memcontrol-v1.c +++ b/mm/memcontrol-v1.c @@ -271,6 +271,7 @@ void __memcg1_swapout(struct folio *folio, struct swap_= cluster_info *ci) struct mem_cgroup *memcg, *swap_memcg; struct obj_cgroup *objcg; unsigned int nr_entries; + unsigned short private_id; =20 VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio); VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); @@ -293,25 +294,24 @@ void __memcg1_swapout(struct folio *folio, struct swa= p_cluster_info *ci) /* * In case the memcg owning these pages has been offlined and doesn't * have an ID allocated to it anymore, charge the closest online - * ancestor for the swap instead and transfer the memory+swap charge. + * ancestor for the swap instead and cancel the memory+swap charge + * if the ID refers to the root memcg. */ nr_entries =3D folio_nr_pages(folio); swap_memcg =3D mem_cgroup_private_id_get_online(memcg, nr_entries); + private_id =3D mem_cgroup_private_id(swap_memcg); mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries); =20 - __swap_cgroup_set(ci, swp_cluster_offset(folio->swap), nr_entries, - mem_cgroup_private_id(swap_memcg)); + __swap_cgroup_set(ci, swp_cluster_offset(folio->swap), nr_entries, privat= e_id); =20 folio_unqueue_deferred_split(folio); folio->memcg_data =3D 0; =20 - if (!obj_cgroup_is_root(objcg)) + if (!obj_cgroup_is_root(objcg)) { page_counter_uncharge(&memcg->memory, nr_entries); =20 - if (memcg !=3D swap_memcg) { - if (!mem_cgroup_is_root(swap_memcg)) - page_counter_charge(&swap_memcg->memsw, nr_entries); - page_counter_uncharge(&memcg->memsw, nr_entries); + if (mem_cgroup_private_id_is_root(private_id)) + page_counter_uncharge(&memcg->memsw, nr_entries); } =20 /* diff --git a/mm/memcontrol-v1.h b/mm/memcontrol-v1.h index 2cd37e1792d79..23be2512702dc 100644 --- a/mm/memcontrol-v1.h +++ b/mm/memcontrol-v1.h @@ -22,6 +22,11 @@ void drain_all_stock(struct mem_cgroup *root_memcg); =20 int memory_stat_show(struct seq_file *m, void *v); =20 +static inline bool mem_cgroup_private_id_is_root(unsigned short id) +{ + return id =3D=3D mem_cgroup_private_id(root_mem_cgroup); +} + struct mem_cgroup *mem_cgroup_private_id_get_online(struct mem_cgroup *mem= cg, unsigned int n); =20 diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 981a231a76129..aedac862fe254 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5975,7 +5975,7 @@ int __mem_cgroup_try_charge_swap(struct folio *folio) /* memcg is pined by memcg ID. */ private_id =3D mem_cgroup_private_id(memcg); =20 - if (!mem_cgroup_is_root(memcg) && + if (!mem_cgroup_private_id_is_root(private_id) && !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { memcg_memory_event(memcg, MEMCG_SWAP_MAX); memcg_memory_event(memcg, MEMCG_SWAP_FAIL); @@ -6004,7 +6004,7 @@ void __mem_cgroup_uncharge_swap(unsigned short id, un= signed int nr_pages) rcu_read_lock(); memcg =3D mem_cgroup_from_private_id(id); if (memcg) { - if (!mem_cgroup_is_root(memcg)) { + if (!mem_cgroup_private_id_is_root(id)) { if (do_memsw_account()) page_counter_uncharge(&memcg->memsw, nr_pages); else --=20 2.43.7 From nobody Thu Sep 24 20:37:49 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6B47D33D4E4; Mon, 21 Sep 2026 06:16:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789971393; cv=none; b=qFzeYgp1HIDCSCMGBsU6VqBC2PG0D0vhXCyHZkrK/IZrUTNosOakyL3Z9e09buftpwuZ50yZRYiDEQGjJ0uqzp67yvK3SnLNBj60wSyjnoKVlV3CpRKxyAQTj5nITGPvZ+8+mq8HGrPHLeduq5yqvdheQSYUkQvkbMLngzKg1Zk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789971393; c=relaxed/simple; bh=U6IJBz+i/+RpNC92aGMPddAzaye9fft8TYEREBbrLpI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bHZrVmxhRIwOQLdWM8Bl2jip/ZxKhzKNExr7ICS5YspGCyY+IVwjs+14Zkq41IFoQTO/TJxwUSU8KsiwvPm6268XksqyIcBbCIkDIWpDtBCrpr6bu7smS0ijMNfAwvb7Y90F762Asoz2WvOu4YObR5nBIsX1Av9i4TuCFL/IEWA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=R8nwYHGR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="R8nwYHGR" Received: by smtp.kernel.org (Postfix) with ESMTPS id 20A78C2BCFF; Mon, 21 Sep 2026 06:16:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789971393; bh=U6IJBz+i/+RpNC92aGMPddAzaye9fft8TYEREBbrLpI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=R8nwYHGRDyg8Spm/+MaxA2+4GfQH+EUu9wf2wHkkHlPBxp4MRLKBdEl+/Ahq2vqXU DlFcSIIuTasHG4dTPROTgqY2acGgTznEuF4fdx40QnFq8E1SGaEznRZxH14G0Dkx/T D50Uon7SgI+ST180fQsIkKi2HX4olrEz35cab/IJ+tEkLoPgfeJ2q1CfOooEPPPEUl ymoRRljLiewy1wqT5HDRrFST7AEFSd8qMnMEQO6+eb3Fnvdlg37jatYQQRapPn9Gwe IgONgo4RU8Khjow7KrhhSq0Nf3nxj9PKTjKKLGLTio7nmq2IyHM0SjPsRO3GOWxWTV YGyIt6nYuosAA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05422C982F1; Mon, 21 Sep 2026 06:16:33 +0000 (UTC) From: Bingfang Guo via B4 Relay Date: Mon, 21 Sep 2026 14:16:26 +0800 Subject: [PATCH v2 3/4] memcg: manipulate memcg private ID references by ID Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260921-bingfangguo-memcgid-rework-v2-3-6c0637dc0edb@tencent.com> References: <20260921-bingfangguo-memcgid-rework-v2-0-6c0637dc0edb@tencent.com> In-Reply-To: <20260921-bingfangguo-memcgid-rework-v2-0-6c0637dc0edb@tencent.com> To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Dave Chinner , Qi Zheng , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , David Hildenbrand , Lorenzo Stoakes , Bingfang Guo Cc: cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Bingfang Guo X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1789971391; l=5897; i=bingfangguo@tencent.com; s=20260812; h=from:subject:message-id; bh=E/d7cKYlZS695bYLUx8GLbGIAsbKGpTfoYLw2/gVq1I=; b=Wm3yHxcAKRDeuCKrXxNXOuYN8MX6LQ9zMhjOChRyOsyEUMDxBxGHZ7daRvab31sh43CXhE+II R2DnCOPlJJjByjCUTUIyA0SEyd8nnDOGHmLX8/mjtrbu7GFWOi6cDm7 X-Developer-Key: i=bingfangguo@tencent.com; a=ed25519; pk=q7u9S7d5e7VijWrYF9O+vv9/XvqWduVGtwGtuCObAHE= X-Endpoint-Received: by B4 Relay for bingfangguo@tencent.com/20260812 with auth_id=942 X-Original-From: Bingfang Guo Reply-To: bingfangguo@tencent.com From: Bingfang Guo This is a preparatory work for moving memcgid from memcg to objcg. Swap entries retain a private ID rather than a memcg pointer. Once private ID references are moved to objcgs, the ID can also outlive the memcg to which it was originally assigned. So it's better to make the get and put functions accept the ID itself instead of the memcg. Rename mem_cgroup_private_id_get_online() to mem_cgroup_private_id_get(), and make it return the ID only. If the memcg is already dying, the dying memcg will still be used for charging and stats accounting in v2 swap charging path. But they are hierarchical and will be reparented after offlining so it doesn't matter. Make mem_cgroup_private_id_put() take the ID and resolve the reference holder internally. Convert swap uncharge and charge rollback to release the reference using that ID. This introduces an extra xarray lookup for now, which will be removed in the final patch. Separate the online-state reference release into mem_cgroup_private_id_kill(). The offline path already has the memcg pointer and can call the underlying put helper directly. Signed-off-by: Bingfang Guo Acked-by: Muchun Song --- mm/memcontrol-v1.c | 7 +++---- mm/memcontrol-v1.h | 3 +-- mm/memcontrol.c | 32 +++++++++++++++++++++++--------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c index ed015fdd95123..b7f2868885071 100644 --- a/mm/memcontrol-v1.c +++ b/mm/memcontrol-v1.c @@ -268,7 +268,7 @@ void memcg1_commit_charge(struct folio *folio, struct m= em_cgroup *memcg) */ void __memcg1_swapout(struct folio *folio, struct swap_cluster_info *ci) { - struct mem_cgroup *memcg, *swap_memcg; + struct mem_cgroup *memcg; struct obj_cgroup *objcg; unsigned int nr_entries; unsigned short private_id; @@ -298,9 +298,8 @@ void __memcg1_swapout(struct folio *folio, struct swap_= cluster_info *ci) * if the ID refers to the root memcg. */ nr_entries =3D folio_nr_pages(folio); - swap_memcg =3D mem_cgroup_private_id_get_online(memcg, nr_entries); - private_id =3D mem_cgroup_private_id(swap_memcg); - mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries); + private_id =3D mem_cgroup_private_id_get(memcg, nr_entries); + mod_memcg_state(memcg, MEMCG_SWAP, nr_entries); =20 __swap_cgroup_set(ci, swp_cluster_offset(folio->swap), nr_entries, privat= e_id); =20 diff --git a/mm/memcontrol-v1.h b/mm/memcontrol-v1.h index 23be2512702dc..281425273ea97 100644 --- a/mm/memcontrol-v1.h +++ b/mm/memcontrol-v1.h @@ -27,8 +27,7 @@ static inline bool mem_cgroup_private_id_is_root(unsigned= short id) return id =3D=3D mem_cgroup_private_id(root_mem_cgroup); } =20 -struct mem_cgroup *mem_cgroup_private_id_get_online(struct mem_cgroup *mem= cg, - unsigned int n); +unsigned short mem_cgroup_private_id_get(struct mem_cgroup *memcg, unsigne= d int n); =20 void reparent_memcg_lruvec_state_local(struct mem_cgroup *memcg, struct mem_cgroup *parent, int idx); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index aedac862fe254..0dd398029057a 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4082,7 +4082,7 @@ static void mem_cgroup_private_id_remove(struct mem_c= group *memcg) } } =20 -static inline void mem_cgroup_private_id_put(struct mem_cgroup *memcg, uns= igned int n) +static void __mem_cgroup_private_id_put(struct mem_cgroup *memcg, unsigned= int n) { if (refcount_sub_and_test(n, &memcg->private_id_ref)) { mem_cgroup_private_id_remove(memcg); @@ -4092,7 +4092,22 @@ static inline void mem_cgroup_private_id_put(struct = mem_cgroup *memcg, unsigned } } =20 -struct mem_cgroup *mem_cgroup_private_id_get_online(struct mem_cgroup *mem= cg, unsigned int n) +static inline void mem_cgroup_private_id_put(unsigned short id, unsigned i= nt n) +{ + struct mem_cgroup *memcg; + + lockdep_assert_in_rcu_read_lock(); + + memcg =3D mem_cgroup_from_private_id(id); + __mem_cgroup_private_id_put(memcg, n); +} + +static void mem_cgroup_private_id_kill(struct mem_cgroup *memcg) +{ + __mem_cgroup_private_id_put(memcg, 1); +} + +unsigned short mem_cgroup_private_id_get(struct mem_cgroup *memcg, unsigne= d int n) { while (!refcount_add_not_zero(n, &memcg->private_id_ref)) { /* @@ -4105,7 +4120,8 @@ struct mem_cgroup *mem_cgroup_private_id_get_online(s= truct mem_cgroup *memcg, un } memcg =3D parent_mem_cgroup(memcg); } - return memcg; + + return mem_cgroup_private_id(memcg); } =20 /** @@ -4430,7 +4446,7 @@ static void mem_cgroup_css_offline(struct cgroup_subs= ys_state *css) =20 drain_all_stock(memcg); =20 - mem_cgroup_private_id_put(memcg, 1); + mem_cgroup_private_id_kill(memcg); } =20 static void mem_cgroup_css_released(struct cgroup_subsys_state *css) @@ -5971,15 +5987,13 @@ int __mem_cgroup_try_charge_swap(struct folio *foli= o) return 0; } =20 - memcg =3D mem_cgroup_private_id_get_online(memcg, nr_pages); - /* memcg is pined by memcg ID. */ - private_id =3D mem_cgroup_private_id(memcg); + private_id =3D mem_cgroup_private_id_get(memcg, nr_pages); =20 if (!mem_cgroup_private_id_is_root(private_id) && !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { memcg_memory_event(memcg, MEMCG_SWAP_MAX); memcg_memory_event(memcg, MEMCG_SWAP_FAIL); - mem_cgroup_private_id_put(memcg, nr_pages); + mem_cgroup_private_id_put(private_id, nr_pages); return -ENOMEM; } mod_memcg_state(memcg, MEMCG_SWAP, nr_pages); @@ -6011,7 +6025,7 @@ void __mem_cgroup_uncharge_swap(unsigned short id, un= signed int nr_pages) page_counter_uncharge(&memcg->swap, nr_pages); } mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages); - mem_cgroup_private_id_put(memcg, nr_pages); + mem_cgroup_private_id_put(id, nr_pages); } rcu_read_unlock(); } --=20 2.43.7 From nobody Thu Sep 24 20:37:49 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6E056361DA6; Mon, 21 Sep 2026 06:16:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789971393; cv=none; b=Lz6GNhLL6ZL+PUjfYK3CfmyvmOUz/0tYVrC45ldfiDOM3atIQluKv7Vhmvw/gZBJ2FzAKyoPCCvZBHC54g4fNIsn6UceK0MrSq5dfv68F8Bw4CZGLQ6dmop1zubRGzBrQxPBQ/dV3e4dWY3MBnUAVpsrL5Dqqke3Jb/QbgfjyOg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789971393; c=relaxed/simple; bh=goaapdMZ9AhxqIWhW85tY5V8jajiUFY3jjaPFkkKHjs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=O8cEqjmtJ/4SkpYjrFU1or9w0Z+ukyF1uTRu02/dtJ1gtlZiMpHTHH2CjXJgmQ87fa0WVz/u+n3JIL4xZ0qNs1mJn+JvxcgUUAlXFLAWOP0TlhzDezzsLZCJ+vi2LKsIWOVM92M/OiMk4wS17QWfWH65VtyOysamj/YuBlR2hf4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VsJSDQoj; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VsJSDQoj" Received: by smtp.kernel.org (Postfix) with ESMTPS id 2B0F5C2BCFC; Mon, 21 Sep 2026 06:16:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1789971393; bh=goaapdMZ9AhxqIWhW85tY5V8jajiUFY3jjaPFkkKHjs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=VsJSDQoj+V3FtURs2Y3RmxOPJs/Z/ypWeBw+BAs86L1lyJjXgLhmI5HJRDG5w5xMl iRaWJNBpdrdlvpK66LHn0kDrsY0cp9EGT659yoF/v3JddsbBuhRFHpSNxGELtSaM1F qaBpcl7MzSiz8WyvJUJyPHVmjO+94LoTguRSNWBNI1DVPQIW2y4Rj80zEC/KBmpiZ4 2hykgBPc7wgNZYRrexEzReOpXIYcKGGyp3TjTRZAyPmKH4q7IoQk4bjYbEl5ZFSBpI VwQDU6Uvu9EhZH2k/hFo51cSjaLKTzZLYF/vz1oITFroF7ZVc85NKT3xn++mmVybhC BjQg6G/Lzl4zQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16C0AC982ED; Mon, 21 Sep 2026 06:16:33 +0000 (UTC) From: Bingfang Guo via B4 Relay Date: Mon, 21 Sep 2026 14:16:27 +0800 Subject: [PATCH v2 4/4] memcg: move memcg private ID refcount to objcg Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260921-bingfangguo-memcgid-rework-v2-4-6c0637dc0edb@tencent.com> References: <20260921-bingfangguo-memcgid-rework-v2-0-6c0637dc0edb@tencent.com> In-Reply-To: <20260921-bingfangguo-memcgid-rework-v2-0-6c0637dc0edb@tencent.com> To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Dave Chinner , Qi Zheng , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , David Hildenbrand , Lorenzo Stoakes , Bingfang Guo Cc: cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Bingfang Guo X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1789971391; l=9479; i=bingfangguo@tencent.com; s=20260812; h=from:subject:message-id; bh=aXhbLZU24XSfkf7X99DsJOXPFhB18u8ETT9g48kLTKw=; b=3JlCLO6RLQE71Z0ZoU6+JAfNEztaKGqONZDV+zrVY+mgXwrXcyIgPcUVrzvAAzGdA2WgRMkhx mM54MPFHWGCAG8fzRWTHUb41Xj0wqUyuS2KJsOTU/2jkANE8ioRsyqk X-Developer-Key: i=bingfangguo@tencent.com; a=ed25519; pk=q7u9S7d5e7VijWrYF9O+vv9/XvqWduVGtwGtuCObAHE= X-Endpoint-Received: by B4 Relay for bingfangguo@tencent.com/20260812 with auth_id=942 X-Original-From: Bingfang Guo Reply-To: bingfangguo@tencent.com From: Bingfang Guo The memcg private ID is used by objects that can't afford storing a whole pointer and can outlive memcgs to track the memcg (notably swap entries). The current design holds a refcount to the css, preventing the memcg from being freed. This patch unbinds the lifetime of memcgid from the memcg so it can be freed. The idea is to move the refcount of memcgid to one of the memcg's objcg. The objcg is stored in the global memcgid xarray instead and used for retrieving the online memcg from it. So swapped out pages no longer pin the dying memcg. After the change, a memcgid can refer to a non present memcg. To handle this situation, when trying to get the original memcg from the id, compare the memcgid passed in with that of the memcg, and return NULL to indicate its death if they differ. NULL checks are added for list_lru_walk_node(), workingset_test_recent() to skip dead memcgs. For MGLRU recency test, mem_cgroup_lruvec() will substitute NULL with root_mem_cgroup. In the earlier patch, an extra xarray lookup was introduced in swap uncharging path. Now that we have the objcg pointer in the function, the extra overhead can be removed by using it for putting directly. Signed-off-by: Bingfang Guo Acked-by: Muchun Song --- include/linux/memcontrol.h | 7 +++-- mm/list_lru.c | 2 +- mm/memcontrol.c | 73 ++++++++++++++++++++++++++++++++++--------= ---- mm/workingset.c | 2 +- 4 files changed, 60 insertions(+), 24 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 46bf724cae7af..2041505a84ce2 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -180,6 +180,7 @@ struct obj_cgroup { struct percpu_ref refcnt; struct mem_cgroup *memcg; atomic_t nr_charged_bytes; + refcount_t private_id_ref; union { struct list_head list; /* protected by objcg_lock */ struct rcu_head rcu; @@ -225,9 +226,6 @@ struct mem_cgroup { /* vmpressure notifications. Written on every reclaim iteration. */ struct vmpressure vmpressure; =20 - /* Written on every swap charge and uncharge. */ - refcount_t private_id_ref; - #ifdef CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC /* MEMCG_KMEM for nmi context */ atomic_t kmem_stat; @@ -324,6 +322,9 @@ struct mem_cgroup { unsigned long zswap_max; #endif =20 + /* The objcg holding private memcg ID. */ + struct obj_cgroup *private_id_objcg; + /* Private memcg ID. Used to ID objects that outlive the cgroup */ int private_id; =20 diff --git a/mm/list_lru.c b/mm/list_lru.c index 8a6dd0a489e12..7edd79113cc56 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -428,7 +428,7 @@ unsigned long list_lru_walk_node(struct list_lru *lru, = int nid, xa_for_each(&lru->xa, index, mlru) { rcu_read_lock(); memcg =3D mem_cgroup_from_private_id(index); - if (!mem_cgroup_tryget(memcg)) { + if (!memcg || !mem_cgroup_tryget(memcg)) { rcu_read_unlock(); continue; } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 0dd398029057a..b74e50c7674f0 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4074,6 +4074,13 @@ static void memcg_wb_domain_size_changed(struct mem_= cgroup *memcg) #define MEM_CGROUP_ID_MAX ((1UL << MEM_CGROUP_ID_SHIFT) - 1) static DEFINE_XARRAY_ALLOC1(mem_cgroup_private_ids); =20 +static inline struct obj_cgroup *obj_cgroup_from_private_id(unsigned short= id) +{ + lockdep_assert_once(rcu_read_lock_held()); + + return xa_load(&mem_cgroup_private_ids, id); +} + static void mem_cgroup_private_id_remove(struct mem_cgroup *memcg) { if (memcg->private_id > 0) { @@ -4082,34 +4089,44 @@ static void mem_cgroup_private_id_remove(struct mem= _cgroup *memcg) } } =20 -static void __mem_cgroup_private_id_put(struct mem_cgroup *memcg, unsigned= int n) +static void __mem_cgroup_private_id_put(struct obj_cgroup *objcg, + unsigned short id, unsigned int n) { - if (refcount_sub_and_test(n, &memcg->private_id_ref)) { - mem_cgroup_private_id_remove(memcg); + struct obj_cgroup *objcg_free; =20 - /* Memcg ID pins CSS */ - css_put(&memcg->css); + if (refcount_sub_and_test(n, &objcg->private_id_ref)) { + objcg_free =3D xa_erase(&mem_cgroup_private_ids, id); + VM_WARN_ON(objcg_free !=3D objcg); + + /* Memcg ID pins the objcg */ + obj_cgroup_put(objcg); } } =20 static inline void mem_cgroup_private_id_put(unsigned short id, unsigned i= nt n) { - struct mem_cgroup *memcg; + struct obj_cgroup *objcg; =20 lockdep_assert_in_rcu_read_lock(); =20 - memcg =3D mem_cgroup_from_private_id(id); - __mem_cgroup_private_id_put(memcg, n); + objcg =3D obj_cgroup_from_private_id(id); + __mem_cgroup_private_id_put(objcg, id, n); } =20 static void mem_cgroup_private_id_kill(struct mem_cgroup *memcg) { - __mem_cgroup_private_id_put(memcg, 1); + __mem_cgroup_private_id_put(memcg->private_id_objcg, memcg->private_id, 1= ); } =20 unsigned short mem_cgroup_private_id_get(struct mem_cgroup *memcg, unsigne= d int n) { - while (!refcount_add_not_zero(n, &memcg->private_id_ref)) { + struct obj_cgroup *objcg; + + lockdep_assert_once(rcu_read_lock_held()); + + objcg =3D memcg->private_id_objcg; + + while (!refcount_add_not_zero(n, &objcg->private_id_ref)) { /* * The root cgroup cannot be destroyed, so it's refcount must * always be >=3D 1. @@ -4119,6 +4136,7 @@ unsigned short mem_cgroup_private_id_get(struct mem_c= group *memcg, unsigned int break; } memcg =3D parent_mem_cgroup(memcg); + objcg =3D memcg->private_id_objcg; } =20 return mem_cgroup_private_id(memcg); @@ -4129,11 +4147,25 @@ unsigned short mem_cgroup_private_id_get(struct mem= _cgroup *memcg, unsigned int * @id: the memcg id to look up * * Caller must hold rcu_read_lock(). + * + * @return: the memcg, or NULL if the memcg referred to is already dead. */ struct mem_cgroup *mem_cgroup_from_private_id(unsigned short id) { + struct obj_cgroup *objcg; + struct mem_cgroup *memcg; + WARN_ON_ONCE(!rcu_read_lock_held()); - return xa_load(&mem_cgroup_private_ids, id); + + objcg =3D obj_cgroup_from_private_id(id); + if (!objcg) + return NULL; + + memcg =3D obj_cgroup_memcg(objcg); + if (mem_cgroup_private_id(memcg) !=3D id) + return NULL; + + return memcg; } =20 struct mem_cgroup *mem_cgroup_get_from_id(u64 id) @@ -4380,9 +4412,10 @@ static int mem_cgroup_css_online(struct cgroup_subsy= s_state *css) FLUSH_TIME); lru_gen_online_memcg(memcg); =20 - /* Online state pins memcg ID, memcg ID pins CSS */ - refcount_set(&memcg->private_id_ref, 1); - css_get(css); + /* CSS pins memcg ID, memcg ID pins obj cgroup */ + memcg->private_id_objcg =3D objcg; + refcount_set(&memcg->private_id_objcg->private_id_ref, 1); + obj_cgroup_get(memcg->private_id_objcg); =20 /* * Ensure mem_cgroup_from_private_id() works once we're fully online. @@ -4394,7 +4427,7 @@ static int mem_cgroup_css_online(struct cgroup_subsys= _state *css) * publish it here at the end of onlining. This matches the * regular ID destruction during offlining. */ - xa_store(&mem_cgroup_private_ids, memcg->private_id, memcg, GFP_KERNEL); + xa_store(&mem_cgroup_private_ids, memcg->private_id, memcg->private_id_ob= jcg, GFP_KERNEL); =20 return 0; free_objcg: @@ -5832,8 +5865,6 @@ static void __init memcg_struct_check(void) memory_events_local); CACHELINE_ASSERT_GROUP_MEMBER(struct mem_cgroup, memcg_write_hot, vmpressure); - CACHELINE_ASSERT_GROUP_MEMBER(struct mem_cgroup, memcg_write_hot, - private_id_ref); #ifdef CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC CACHELINE_ASSERT_GROUP_MEMBER(struct mem_cgroup, memcg_write_hot, kmem_stat); @@ -5878,6 +5909,8 @@ static void __init memcg_struct_check(void) CACHELINE_ASSERT_GROUP_MEMBER(struct mem_cgroup, memcg_read_mostly, zswap_writeback); #endif + CACHELINE_ASSERT_GROUP_MEMBER(struct mem_cgroup, memcg_read_mostly, + private_id_objcg); CACHELINE_ASSERT_GROUP_MEMBER(struct mem_cgroup, memcg_read_mostly, private_id); CACHELINE_ASSERT_GROUP_MEMBER(struct mem_cgroup, memcg_read_mostly, @@ -6013,10 +6046,12 @@ int __mem_cgroup_try_charge_swap(struct folio *foli= o) */ void __mem_cgroup_uncharge_swap(unsigned short id, unsigned int nr_pages) { + struct obj_cgroup *objcg; struct mem_cgroup *memcg; =20 rcu_read_lock(); - memcg =3D mem_cgroup_from_private_id(id); + objcg =3D obj_cgroup_from_private_id(id); + memcg =3D obj_cgroup_memcg(objcg); if (memcg) { if (!mem_cgroup_private_id_is_root(id)) { if (do_memsw_account()) @@ -6025,7 +6060,7 @@ void __mem_cgroup_uncharge_swap(unsigned short id, un= signed int nr_pages) page_counter_uncharge(&memcg->swap, nr_pages); } mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages); - mem_cgroup_private_id_put(id, nr_pages); + __mem_cgroup_private_id_put(objcg, id, nr_pages); } rcu_read_unlock(); } diff --git a/mm/workingset.c b/mm/workingset.c index 8412f4840ae35..1504f91cdca5f 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -470,7 +470,7 @@ bool workingset_test_recent(void *shadow, bool file, bo= ol *workingset, * configurations instead. */ eviction_memcg =3D mem_cgroup_from_private_id(memcgid); - if (!mem_cgroup_tryget(eviction_memcg)) + if (eviction_memcg && !mem_cgroup_tryget(eviction_memcg)) eviction_memcg =3D NULL; rcu_read_unlock(); =20 --=20 2.43.7