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 15/20] mm, swap: add vmstat and memcg counters for vswap
Date: Fri, 04 Dec 2026 22:09:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-250733@kernel.org> (raw)
In-Reply-To: <20261203220010.1302207-16-nphamcs@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mm: missing cgroup v1 swap-in rollback in
  swap_cache_del_folio() callers
- [Low] mm: redundant mem_cgroup_private_ids XArray lookup in
  __mem_cgroup_swap_put()
- [Low] mm: unnecessary mem_cgroup_private_ids XArray lookup in
  __mem_cgroup_swap_get()
--

commit d0f68463326459f383de74db00757c571c68f75f
Author: Nhat Pham <nphamcs@gmail.com>

mm, swap: add vmstat and memcg counters for vswap

This commit adds nr_vswap to /proc/vmstat and vswap to cgroup v1 and v2
memory.stat to report virtual swap usage.  It tracks MEMCG_VSWAP while
virtual swap slots hold a cgroup owner ID and restores it in
memcg1_swapin_abort().

> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index 571f375b2e7f7..5811ad3889dff 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c

[ ... ]

> @@ -6088,6 +6092,8 @@ void memcg1_swapin_abort(struct folio *folio, struct swap_cluster_info *ci)
>  		page_counter_charge(&memcg->memsw, nr);
>  	if (nr_swapfile)
>  		mod_memcg_state(memcg, MEMCG_SWAP, nr_swapfile);
> +	if (is_vswap_entry(folio->swap))
> +		mod_memcg_state(memcg, MEMCG_VSWAP, nr);
>  
>  	for (i = start; i < end; i++)
>  		if (__swp_tb_get_count(__swap_table_get(ci, i)))

[Severity: High]
Does memcg1_swapin_abort() miss the other paths that discard a clean
swapcache folio via swap_cache_del_folio()?

memcg1_swapin_abort() only runs from swap_cache_del_folio_rollback(), whose
sole caller is zswap_writeback_entry().  When do_swap_page() drops a freshly
swapped-in large folio after swap_cache_folio_mappable() fails (for example,
racing with MADV_DONTNEED), it calls swap_cache_del_folio() instead.

Because memcg1_swapin() already cleared the owner IDs and uncharged memsw,
MEMCG_SWAP, and MEMCG_VSWAP for the whole folio, skipping rollback leaves
remaining referenced slots with owner ID 0 and permanently drops their
cgroup v1 accounting while they stay counted in nr_vswap.

Repeating this lets an unprivileged cgroup v1 workload bypass
memory.memsw.limit_in_bytes and consume unbounded global swap or vswap
space.  unuse_pte(), shmem_swapin_folio(), and me_swapcache_clean() have the
same issue and should also use swap_cache_del_folio_rollback().

[ ... ]

> @@ -6126,7 +6133,12 @@ unsigned short __mem_cgroup_swap_get(struct folio *folio)
>  		return 0;
>  	}
>  
> -	return mem_cgroup_private_id_get(memcg, nr_pages);
> +	id = mem_cgroup_private_id_get(memcg, nr_pages);
> +	if (is_vswap_entry(folio->swap)) {
> +		memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
> +		mod_memcg_state(memcg, MEMCG_VSWAP, nr_pages);
> +	}
> +	return id;
>  }

[Severity: Low]
Is the obj_cgroup_from_private_id() lookup needed here?

__mem_cgroup_swap_get() already has memcg, and mod_memcg_state() walks past
dying cgroups via get_non_dying_memcg_start() to the same non-dying
ancestor.  Calling mod_memcg_state(memcg, MEMCG_VSWAP, nr_pages) directly
avoids a global XArray lookup on every vswap allocation during reclaim.

[ ... ]

> @@ -6266,10 +6278,18 @@ void __mem_cgroup_swap_uncharge(unsigned short id, unsigned int nr_pages,
>   * __mem_cgroup_swap_put - drop the private ID refs taken for swap slots
>   * @id: the pinned private ID
>   * @nr_pages: number of refs to drop
> + * @is_vswap: release virtual-swap accounting along with the refs
>   */
> -void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages)
> +void __mem_cgroup_swap_put(unsigned short id, unsigned int nr_pages,
> +			   bool is_vswap)
>  {
> +	struct mem_cgroup *memcg;
> +
>  	guard(rcu)();
> +	if (is_vswap) {
> +		memcg = obj_cgroup_memcg(obj_cgroup_from_private_id(id));
> +		mod_memcg_state(memcg, MEMCG_VSWAP, -nr_pages);
> +	}
>  	mem_cgroup_private_id_put(id, nr_pages);
>  }

[Severity: Low]
Can this avoid looking up id in mem_cgroup_private_ids twice when is_vswap
is true?

mem_cgroup_private_id_put() repeats obj_cgroup_from_private_id(id) under
ci->lock on every vswap free batch via memcg_swap_free().  Saving objcg and
calling __mem_cgroup_private_id_put(objcg, id, nr_pages) directly, as
__mem_cgroup_dir_phys_swap_free() does, avoids the second XArray lookup.

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


  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
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 [this message]
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-250733@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