From: Shakeel Butt <shakeel.butt@linux.dev>
To: Andrew Morton <akpm@linux-foundation.org>,
Johannes Weiner <hannes@cmpxchg.org>,
David Hildenbrand <david@kernel.org>
Cc: Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Muchun Song <muchun.song@linux.dev>,
Qi Zheng <qi.zheng@linux.dev>, Zi Yan <ziy@nvidia.com>,
Lorenzo Stoakes <ljs@kernel.org>,
Vlastimil Babka <vbabka@kernel.org>,
Kairui Song <kasong@tencent.com>, Nhat Pham <nphamcs@gmail.com>,
Usama Arif <usama.arif@linux.dev>,
Hugh Dickins <hughd@google.com>,
Meta kernel team <kernel-team@meta.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: [PATCH 0/4] mm, memcg: stop orphaned swap cache from using up memcg IDs
Date: Mon, 30 Nov 2026 10:27:51 -0800 [thread overview]
Message-ID: <20261130182755.666042-1-shakeel.butt@linux.dev> (raw)
On Meta hosts that run many short-lived cgroups, about 13,000 new ones
per hour, creating a memory cgroup started to fail with -EBUSY. All
65,535 memcg IDs were in use, nearly all by deleted cgroups, so systemd
could not start units and container setup failed. drgn showed what held
the IDs:
- a 7.1 host: 58,431 IDs in use; 57,803 of the 57,806 dead ones held
only by orphaned swap cache, 262K slots (1 GB)
- a 6.16 host: 22,777 IDs in use; all 22,284 dead ones held only by
orphaned swap cache, 1M slots (4 GB)
An orphan is a swap cache folio that nothing maps and whose swap slots
have no references. It can never be swapped back in, but it keeps its
memory and its slots until LRU reclaim frees it. Each slot records the
ID of its memcg, and the ID stays allocated while any slot records it,
even after the cgroup is deleted.
On these hosts, reclaim did not free the orphans for days. memory.low
protects the parent cgroups, and since LRU reparenting (f1cf8d2f36dc)
the deleted cgroups no longer hold the folios, so reclaim of those
cgroups does not find them either. That makes orphans pile up faster on
7.1 than on 6.16, but both leak.
On 7.1, the leak still shows up in nr_dying_subsys_memory in
cgroup.stat, because a deleted memcg stays around while its ID is held.
Since commit 8290f45995f5 ("memcg: move memcg private ID refcount to
objcg") in 7.4, the memcg is freed and only its ID stays, so that count
can be 0 while the IDs run out. Then the only sign the kernel gives is
the warning from "memcg: warn when memory cgroup IDs run out", which is
already in mm-unstable.
The kernel normally removes such a folio from the swap cache when the
last reference to its slots, or its last mapping, goes away. Each patch
fixes a case where that does not happen:
- Patch 1: the folio is locked or under writeback at that moment. Retry
from a delayed work, and sweep all swap clusters if the retry queue
overflows.
- Patch 2: the process exits while the folio is unmapped for migration
or a split, or a split hands back an orphan.
- Patch 3: do_swap_page() maps a KSM copy and drops the last swap
reference of the original.
- Patch 4: reclaim gives swap to a folio whose process has already
exited. This was the largest source in a VM test.
Swap slots can also outlive their cgroup for reasons that are not bugs,
for example a tmpfs file left behind. This series does not change that.
The series is based on mm-everything-2026-11-30-07-23, which already has
two related patches: "memcg: free the private ID of a memcg that never
comes online" [1] and "memcg: warn when memory cgroup IDs run out" [2].
Testing in a VM with 8 CPUs, 4 GB of memory and 8 GB of swap:
- 1,200 short-lived cgroups, 8 at a time, that each swap under
memory.high and exit, under global memory pressure and with
memory.low on their parent. Dead memcg IDs left at the end:
without the series with it
v7.4-rc3 132, 27, 21, 39, 0 in 5 runs
15, 108
mm-everything-2026-11-30 51, 17 0 in 5 runs
Four of the runs with the series used KASAN, lockdep and DEBUG_VM. A
KCSAN run reported nothing in the new code. In traced runs without
patches 3 and 4, 53 of the 64 orphans left got their swap from
reclaim, 51 of them from kswapd, after their process had exited.
- A 1 GB process killed three times while thousands of its folios were
under slow swap writeback: the retry queue overflowed, the sweeps
freed every orphan, and a sweep took about 63 ms with 3 GB of swap in
use.
- Two processes sharing 4,096 KSM pages: without patch 3, 4,096 orphans
and the ID of their dead cgroup stay; with it, none.
- A THP swapped out whole, then partly unmapped and split through
debugfs: without patch 2 one orphan stays; with it, none.
- swapoff and swapon in a loop under KASAN and lockdep: no reports.
Each patch builds with W=1, and the series also builds with
CONFIG_SWAP=n, CONFIG_MEMCG=n and for i386.
[1] https://lore.kernel.org/all/20261124000439.967403-1-shakeel.butt@linux.dev/
[2] https://lore.kernel.org/all/20261124020800.2313930-1-shakeel.butt@linux.dev/
Shakeel
Shakeel Butt (4):
mm/swap: free orphaned swap cache instead of leaving it to reclaim
mm: free swap cache that migration or a split leaves unmapped
mm/memory: free the swap cache folio that a KSM copy leaves behind
mm/vmscan: don't give swap to an anon folio that nothing maps
mm/huge_memory.c | 10 +-
mm/memory.c | 6 ++
mm/migrate.c | 9 ++
mm/swap.h | 17 +++
mm/swap_state.c | 12 ++-
mm/swapfile.c | 263 ++++++++++++++++++++++++++++++++++++++++++++++-
mm/vmscan.c | 26 +++++
7 files changed, 334 insertions(+), 9 deletions(-)
base-commit: f05a21e03b2fa69a4fc3130e148349985d62ff8e
--
2.53.1-Meta
next reply other threads:[~2026-11-30 18:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 18:27 Shakeel Butt [this message]
2026-11-30 18:27 ` [PATCH 1/4] mm/swap: free orphaned swap cache instead of leaving it to reclaim Shakeel Butt
2026-11-30 18:27 ` [PATCH 2/4] mm: free swap cache that migration or a split leaves unmapped Shakeel Butt
2026-11-30 18:27 ` [PATCH 3/4] mm/memory: free the swap cache folio that a KSM copy leaves behind Shakeel Butt
2026-11-30 18:27 ` [PATCH 4/4] mm/vmscan: don't give swap to an anon folio that nothing maps Shakeel Butt
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=20261130182755.666042-1-shakeel.butt@linux.dev \
--to=shakeel.butt@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=kasong@tencent.com \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=nphamcs@gmail.com \
--cc=qi.zheng@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=ziy@nvidia.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