Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Nhat Pham" <nphamcs@gmail.com>, linux-mm@kvack.org
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
	"Chris Li" <chrisl@kernel.org>,
	"Kairui Song" <kasong@tencent.com>,
	"Kemeng Shi" <shikemeng@huaweicloud.com>,
	"Baoquan He" <baoquan.he@linux.dev>,
	"Barry Song" <baohua@kernel.org>,
	"Youngjun Park" <youngjun.park@lge.com>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Yosry Ahmed" <yosry@kernel.org>,
	"Chengming Zhou" <chengming.zhou@linux.dev>,
	"Qi Zheng" <qi.zheng@linux.dev>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Axel Rasmussen" <axelrasmussen@google.com>,
	"Yuanchu Xie" <yuanchu@google.com>, "Wei Xu" <weixugc@google.com>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Michal Hocko" <mhocko@kernel.org>,
	"David Hildenbrand" <david@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Hugh Dickins" <hughd@google.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"Muchun Song" <muchun.song@linux.dev>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Karl Mehltretter" <kmehltretter@gmail.com>,
	"Dapeng Mi" <dapeng1.mi@linux.intel.com>,
	"Marco Elver" <elver@google.com>,
	"Ethan Nelson-Moore" <enelsonmoore@gmail.com>,
	"Borislav Petkov (AMD)" <bp@alien8.de>,
	"Jeff Layton" <jlayton@kernel.org>, "Tejun Heo" <tj@kernel.org>,
	"Michal Koutný" <mkoutny@suse.com>
Subject: Re: [PATCH v7 13/20] mm, swap: only charge physical swap entries
Date: Fri, 04 Dec 2026 22:09:30 +0000	[thread overview]
Message-ID: <sashiko-outbox-250731@kernel.org> (raw)
In-Reply-To: <20261203220010.1302207-14-nphamcs@gmail.com>

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 <nphamcs@gmail.com>

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() >= nr_pages;
> +	return mem_cgroup_can_vswap(memcg) ||
> +	       mem_cgroup_get_nr_swap_pages(memcg) >= 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 *folio)
>  	VM_BUG_ON_FOLIO(!folio_test_swapcache(folio), folio);
>  	VM_WARN_ON(!is_vswap_entry(vswap_entry));
>  
> -	phys_entry = vswap_to_phys(vswap_entry);
> -	if (phys_entry.val)
> -		return phys_entry;
> +	voff = swp_cluster_offset(vswap_entry);
> +	ci = __swap_entry_to_cluster(vswap_entry);
> +	ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci);
> +
> +	spin_lock(&ci->lock);
> +	vt = __vtable_get(ci_dyn, voff);
> +	if (vtable_type(vt) == VSWAP_SWAPFILE) {
> +		spin_unlock(&ci->lock);
> +		return vtable_to_phys(vt);
> +	}
> +	memcg_id = __swap_cgroup_get(ci, voff);
> +	spin_unlock(&ci->lock);
>  
>  	phys_entry = folio_alloc_phys_swap(folio);
> -	if (!phys_entry.val)
> +	if (!phys_entry.val) {
> +		mem_cgroup_swap_fail(memcg_id);
>  		return (swp_entry_t){};
> +	}
>  
> -	voff = 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 shrinker *shrinker,
>  	if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg))
>  		return 0;
>  
> -	if (!zswap_writeback_possible())
> +	if (!zswap_writeback_possible(memcg))
>  		return 0;
>  
>  	/*
> @@ -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;
>  
> -	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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203220010.1302207-1-nphamcs@gmail.com?part=13


  reply	other threads:[~2026-12-04 22:10 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 21:59 [PATCH v7 00/20] mm: mandatory virtual swap Nhat Pham
2026-12-03 21:59 ` [PATCH v7 01/20] mm/swap: prepare dynamic clusters for vswap Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 02/20] mm, swap: prepare the swap IO path " Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 03/20] mm/swap: prepare zswap and zero storage for virtual entries Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 04/20] mm/swap: allocate virtual swap slots Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 05/20] mm/swap: return physical entries from the swap allocator Nhat Pham
2026-12-03 21:59 ` [PATCH v7 06/20] mm/swap: resolve physical backing for reads and device flags Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 07/20] mm/swap: prepare physical swapoff for virtual backing Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 08/20] mm/swap: support physical swap as a vswap backend Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 21:59 ` [PATCH v7 09/20] mm, swap: enable THP swapin for vswap entries Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 10/20] mm, swap: write back vswap zswap entries to physical swap Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 11/20] mm, swap: reclaim physical slots backing cache-only vswap entries Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 12/20] mm, swap: add debugfs counters for vswap Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 13/20] mm, swap: only charge physical swap entries Nhat Pham
2026-12-04 22:09   ` sashiko-bot [this message]
2026-12-03 22:00 ` [PATCH v7 14/20] mm, swap: defer memcg_table allocation for physical swap clusters Nhat Pham
2026-12-03 22:00 ` [PATCH v7 15/20] mm, swap: add vmstat and memcg counters for vswap Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 16/20] mm/swap: make vswap mandatory Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 17/20] mm/swap: drop direct physical swapcache handling Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 18/20] mm/zswap: store compressed entries only in the vtable Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 19/20] mm/swap: reclaim physical backing through reverse mappings Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 22:00 ` [PATCH v7 20/20] mm/swap: move virtual swap metadata into vswap clusters Nhat Pham
2026-12-04 22:09   ` sashiko-bot
2026-12-03 22:14 ` [PATCH v7 00/20] mm: mandatory virtual swap Yosry Ahmed
2026-12-03 23:00   ` Nhat Pham
2026-12-03 23:05     ` Yosry Ahmed
2026-12-03 23:14       ` Nhat Pham

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-250731@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=axelrasmussen@google.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=baoquan.he@linux.dev \
    --cc=bp@alien8.de \
    --cc=chengming.zhou@linux.dev \
    --cc=chrisl@kernel.org \
    --cc=corbet@lwn.net \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=david@kernel.org \
    --cc=elver@google.com \
    --cc=enelsonmoore@gmail.com \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=jlayton@kernel.org \
    --cc=kasong@tencent.com \
    --cc=kmehltretter@gmail.com \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=nphamcs@gmail.com \
    --cc=qi.zheng@linux.dev \
    --cc=rdunlap@infradead.org \
    --cc=roman.gushchin@linux.dev \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=shikemeng@huaweicloud.com \
    --cc=skhan@linuxfoundation.org \
    --cc=tj@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=weixugc@google.com \
    --cc=yosry@kernel.org \
    --cc=youngjun.park@lge.com \
    --cc=yuanchu@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox