From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B490C606A322 for ; Fri, 4 Dec 2026 22:10:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Ym6GgNG9 Received: by kanga.kvack.org (Postfix) id 3A5936B05C4; Fri, 4 Dec 2026 17:09:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 37A4C6B05C5; Fri, 4 Dec 2026 17:09:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 26B226B05C6; Fri, 4 Dec 2026 17:09:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F35746B05C4 for ; Fri, 4 Dec 2026 17:09:34 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 951FB1605FB for ; Fri, 4 Dec 2026 22:09:34 +0000 (UTC) X-FDA: 85507693068.09.EED5DDB Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf20.hostedemail.com (Postfix) with ESMTP id E72B31C0008 for ; Fri, 4 Dec 2026 22:09:32 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=Ym6GgNG9; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf20.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796422173; b=hzTZIRNmtiwEZZ8tPDOepvWZ5Bvy6z2Ns/m0KiuxGJElUefrNQlZxUImKNd8+yHfH9hQJu GFWOCzCQykzQxGWz/zH3OKFuxMjldOWmAX1T4rQ4D+dXskeDwS7X33OMUtakCrIOU1FWxg 5OgRmrmdB+/JYML4Xa6iRBo5ATtel+8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796422173; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=UQd1CACgUf0um8TCDKpgFAYzVAX/6DdIyqw9BMMiigs=; b=gZz+/tVO5mytcUcEq7dThQwyeQAyncS58AvunYzsQls1CP+LjmrMjrpgMVd4UQ+DwXtaxm HpdDRABkIigSKrqEIhNzRxyRrcTmoRIoLpTIr5epv0mLxKD1zXxGkZxdZksUVoCVHgk3/r He9bWXZeih6ysv+gxDR6iQSNBG+UcDk= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=Ym6GgNG9; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf20.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 449E6453BA; Fri, 4 Dec 2026 22:09:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC9351F00936; Fri, 4 Dec 2026 22:09:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796422172; bh=UQd1CACgUf0um8TCDKpgFAYzVAX/6DdIyqw9BMMiigs=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=Ym6GgNG9JCtLJKI42j/3YNMDciSy6dOhKOCid7UTU+DT/Yhwpmz9owHQHNrMD2SlJ r4Ws/2A8K7NZo1B4j0MXu+vfIjy4Ce9LCWGoxCA0wYFh3FD2H2/SfTLmYM90oXvsLH s3ul8gPtxuP3cTpI440OBt7BujIrARSIu1Jl6ND4E0oKo8WaO7frE4YTZOoGsS8JR3 r7zJfB1UN25vx/ADCJ1qRNctAG0d2KuO8ZAMimu7uX3W7WXihSwpNL9dCVP2C6CAlc DWU0vPTkEPxt0bJOZwKnT7AS+GueMfSwGhLBD8KLdh/izS09gLp8aUyf83GcF/99jo rWiiqMcC6y7jw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v7 13/20] mm, swap: only charge physical swap entries Reply-To: sashiko-reviews@lists.linux.dev To: "Nhat Pham" , linux-mm@kvack.org Cc: "Andrew Morton" , "Chris Li" , "Kairui Song" , "Kemeng Shi" , "Baoquan He" , "Barry Song" , "Youngjun Park" , "Johannes Weiner" , "Yosry Ahmed" , "Chengming Zhou" , "Qi Zheng" , "Shakeel Butt" , "Axel Rasmussen" , "Yuanchu Xie" , "Wei Xu" , "Baolin Wang" , "Michal Hocko" , "David Hildenbrand" , "Liam R. Howlett" , "Vlastimil Babka" , "Mike Rapoport" , "Hugh Dickins" , "Jonathan Corbet" , "Shuah Khan" , "Randy Dunlap" , "Roman Gushchin" , "Muchun Song" , "Arnd Bergmann" , "Karl Mehltretter" , "Dapeng Mi" , "Marco Elver" , "Ethan Nelson-Moore" , =?utf-8?b?Qm9yaXNsYXYgUGV0a292IChBTUQp?= , "Jeff Layton" , "Tejun Heo" , =?utf-8?b?TWljaGFsIEtvdXRuw70=?= In-Reply-To: <20261203220010.1302207-14-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> <20261203220010.1302207-14-nphamcs@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 22:09:30 +0000 X-Rspamd-Server: rspam02 X-Rspam-User: X-Spam-Status: No, score=-11.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Stat-Signature: rk665qhbd8jwxsyudy9ydei1tgmb8md6 X-Rspamd-Queue-Id: E72B31C0008 X-HE-Tag: 1796422172-876891 X-HE-Meta: U2FsdGVkX1+RgPG46FVmkpRsUHjwRem8RLkash+SgB16fiJfHk8ON/WpERCtGA7Ln6YV/6i7VFSNy9WU6Ykpr1gKH687aBLKla6Im9gnBGjXc1nmjD+IAnYXB9IuSgA+kmIcETBKanU/dNEiFkPKJz1ecbBSiYFVnXS6MIYSBbnzCZTLJxO7o2kbA/DOuEGe/arl1HXG44FNj4VRizYTdOhqIW0pfXvXPfZz0FJ2VqVgBQQ36oQ1mNXSrbajiBCUeeuNAjfyw6PuX8ZDQlpQltONCV01UA4WU82ygDQgGAqYDASt3fcbx/vzN3ab9/hoV4trNDdRcAaHJTU/C9x3cGbxVSad95iR/2A7D1T7PAe9DJiC4KgqlgyvtLvRzbqPuDzSqTZVIR0YW520j9VDCm44WRuETLmihL9eZ1x35oG08XFHgaPPOXqNfPy/kT5e33IEeA8S4t38CejIatgG2tM+qp11TmApsytfH8PoQzXB9pQdfrkBBy1CL+a8FE5A57aUka2IDIHuck1u/k5o4FK5VsDquS3uuXuzyVYLA4oGoTwXxq4Aci5vYx0WDLL1Cc1op0bet4Pyj6UiaYMKOo7dCO3bQI1wabz6Ag7W8GLODYD5JaY6INCZGHAQT8umcTkeE9e87cx1Cq9CMnfYW4B+aRnosHsTEDeJmjt7IIWO//9wo64XIMkLp3lsYQ7pcJAVQ12oHfv67Doh1LQ9nnuxNmfhJ1DZ3R/V5dYmWg7/YVqVHMBmVCtqh7YwVY/jlvmHXdKNYV8d3dF+PCjd92Htl0pgQLsDZViWT4Hk6IDDwVHTO2SKB1TAaKTR4QKqK7SmeiExJj53pkcUF9Y153uTDBN4lMPe9z9ZdRkK101wkEd/n2J+ZGPWFavS0hiZILhFGC6vmDQNNW+NE4PtFtqOkhN71ZUaEBFFAK/UqzNCcpQ1Di/OESfWjXZwdBG0IX7nqJZPW5H3uQajQyq WVSxpyWV HLxHhFiMnqggE74EMmNF7cI1fZLevvHT7deOHv69t06ZMXTbovEM7UslGuKIp23sVgXk5GftaF+Y6HAETQ722IWBJ2VOKSwa8FibMWNuR5t/w5hR/edkwWUYPoKNyRmDfQDvw1yNoVyTq16AlsbnN3p0tr3tpnL/XDyhyiJP3tYgorNtzAY7Fvl7lYR/0BOY0qFkaX8BVKcTE8AwY1TJvt5sPrNNFx1HXhecNQhGrRCWpnB0vYXGidmo+3AftrIu1/63FzUlRA7rEI+YhmGrqRTFOJ1XzZbnyMUiRR/lO3vBPt+HupSBq9sFuU1LkhYoFB6NYFyTMN3x7ItaywxutDzIc9zPeA1VoQBpGkhR8ACh81ekjmLrNYL/NhAm9uy8ham3rbyVdbgCIsxT3uHFNV/0efWAkry2CFDE4OZAylD2wzUxMTA52UaiBXvjRF5L6jsS+zY4UVxVsOF5DFhpzex577CQsDKMaumVc2TI/1j/3M7MN1ZYQoJuyMPDyq4UkZhOH6injczN3tQ1nN1rCjOjA5qiSPgQg3bjCXhcdTAKGCDes80H48fq5ZkK5Ry7AH9IsQLij8Qem3ERChPa1UNsYnmWZG6XyAEnydSxVNRgy8CRoWDZBKXFvLKwYJHKvfCkISYO4EJ7jJ4KgFxd8nubx2Avse6NXg8Hp/OOjrv5nr+USxFd2hWjkULEqnn1IK2Ci8O94M6ktHJofF98rWTQ0OCuVyGyu8ldKHpKjcwSw62dc9f7fkZR1hgzsKTyTfoimMlaGRCHfE2/MGchzlXzzM/YNH15yMd8a76hrAwFQnz7J2SdfOl1RKJKs449/IDaE Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] mm: uncharged zero-filled vswap entries bypass memcg swap/memory limits - [Medium] mm, swap: folio_realloc_swap() allocates physical swap before memcg charge - [Medium] mm, zswap: shrinker keeps scanning after cgroup hits memory.swap.max -- commit a8ec36ff174da79cccd73f1c07e6ae7736522bb5 Author: Nhat Pham mm, swap: only charge physical swap entries This patch defers charging memcg->swap for virtual swap entries until physical swap backing is allocated, and updates the swap cgroup helpers and zswap shrinker checks accordingly. > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 978c47ad4b933..571f375b2e7f7 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c [ ... ] > @@ -6203,11 +6315,8 @@ long mem_cgroup_get_folio_swap_margin(const struct= folio *folio) > */ > bool mem_cgroup_can_swap(struct mem_cgroup *memcg, long nr_pages) > { > - if (!mem_cgroup_disabled() && !do_memsw_account() && > - page_counter_margin(&memcg->swap) < nr_pages) > - return false; > - > - return mem_cgroup_can_vswap(memcg) || get_nr_swap_pages() >=3D nr_pages; > + return mem_cgroup_can_vswap(memcg) || > + mem_cgroup_get_nr_swap_pages(memcg) >=3D nr_pages; > } [Severity: High] Can an unprivileged cgroup with memory.swap.max set to 0 now exhaust host memory with uncharged vswap cluster metadata? With mem_cgroup_can_swap() allowing reclaim when mem_cgroup_can_vswap() is true and folio_alloc_swap() skipping mem_cgroup_swap_charge() for vswap entries, zero-filled folios hit swap_zeromap_folio_set() in swap_writeout() and are evicted with 0 charge to memory, swap, or zswap. Each 512-slot cluster allocated by vswap_alloc_cluster() takes ~9.3 KiB of uncharged, unreclaimable kernel memory (GFP_ATOMIC | __GFP_NOWARN without __GFP_ACCOUNT), capped only at REFCOUNT_MAX pages in vswap_init(). An unprivileged task using memfd_create() (or anonymous mmap() with MADV_PAGEOUT) to swap out zero-filled pages can pin gigabytes of uncharged kernel memory, bypassing memory.max, memory.swap.max, and memory.zswap.max and triggering host OOM kills of unrelated workloads. > diff --git a/mm/swapfile.c b/mm/swapfile.c > index afc48d33cc40f..a1eef85b9eb6c 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c [ ... ] > @@ -2376,18 +2406,33 @@ swp_entry_t folio_realloc_swap(struct folio *foli= o) > VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio); > VM_WARN_ON(!is_vswap_entry(vswap_entry)); > =20 > - phys_entry =3D vswap_to_phys(vswap_entry); > - if (phys_entry.val) > - return phys_entry; > + voff =3D swp_cluster_offset(vswap_entry); > + ci =3D __swap_entry_to_cluster(vswap_entry); > + ci_dyn =3D container_of(ci, struct swap_cluster_info_dynamic, ci); > + > + spin_lock(&ci->lock); > + vt =3D __vtable_get(ci_dyn, voff); > + if (vtable_type(vt) =3D=3D VSWAP_SWAPFILE) { > + spin_unlock(&ci->lock); > + return vtable_to_phys(vt); > + } > + memcg_id =3D __swap_cgroup_get(ci, voff); > + spin_unlock(&ci->lock); > =20 > phys_entry =3D folio_alloc_phys_swap(folio); > - if (!phys_entry.val) > + if (!phys_entry.val) { > + mem_cgroup_swap_fail(memcg_id); > return (swp_entry_t){}; > + } > =20 > - voff =3D swp_cluster_offset(vswap_entry); > + if (mem_cgroup_swap_charge(memcg_id, nr)) { > + __swap_cluster_free_phys_backing(__swap_entry_to_info(phys_entry), > + __swap_entry_to_cluster(phys_entry), > + swp_cluster_offset(phys_entry), > + nr); > + return (swp_entry_t){}; > + } [Severity: Medium] Should mem_cgroup_swap_charge() run before folio_alloc_phys_swap() here? Both memcg_id and nr are already known after dropping ci->lock. When a cgroup at memory.swap.max fails zswap_store() in swap_writeout(), each rejected folio allocates physical swap slots and immediately rolls them back in __swap_cluster_free_phys_backing(). If the allocation isolated an empty cluster, dropping pci->count back to 0 enters free_cluster(), scheduling a 2 MiB discard on SWP_PAGE_DISCARD devices (or freeing pci->table via RCU) and forcing the next folio back through swap_alloc_slow(). Charging memcg->swap first avoids touching shared physical swap state when the cgroup has no swap margin. > diff --git a/mm/zswap.c b/mm/zswap.c > index 54b4ddcf7d733..f1f0fdf3945dc 100644 > --- a/mm/zswap.c > +++ b/mm/zswap.c [ ... ] > @@ -1372,7 +1378,7 @@ static unsigned long zswap_shrinker_count(struct sh= rinker *shrinker, > if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memc= g)) > return 0; > =20 > - if (!zswap_writeback_possible()) > + if (!zswap_writeback_possible(memcg)) > return 0; > =20 > /* > @@ -1459,7 +1465,7 @@ static struct shrinker *zswap_alloc_shrinker(void) > * writeback disabled, is a zombie cgroup, or has empty zswap LRUs. > * > * Also returns -ENOENT when vswap is enabled and there is no physical > - * swap to write back to. > + * swap for @memcg to write back to. > */ > static int shrink_memcg(struct mem_cgroup *memcg) > { > @@ -1470,7 +1476,7 @@ static int shrink_memcg(struct mem_cgroup *memcg) > if (!mem_cgroup_zswap_writeback_enabled(memcg)) > return -ENOENT; > =20 > - if (!zswap_writeback_possible()) > + if (!zswap_writeback_possible(memcg)) > return -ENOENT; [Severity: Medium] Can the zswap shrinker keep scanning entries after a cgroup exhausts its swap limit? do_shrink_slab() calls zswap_shrinker_count() once and then calls zswap_shrinker_scan() in SHRINK_BATCH chunks until SHRINK_STOP is returned, but zswap_shrinker_scan() does not check zswap_writeback_possible(sc->memcg= ). In addition, shrink_memcg_cb() returns LRU_RETRY rather than LRU_STOP when zswap_writeback_entry() fails with -ENOMEM, and shrink_memcg() only checks zswap_writeback_possible(memcg) once before iterating over all online NUMA nodes. Once a cgroup hits memory.swap.max partway through shrinking (or has it lowered between count and scan), the shrinker keeps walking remaining batches or nodes, rotating cold entries to the LRU tail and inflating MEMCG_SWAP_MAX, MEMCG_SWAP_FAIL, and zswap_reject_reclaim_fail. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203220010.1302= 207-1-nphamcs@gmail.com?part=3D13