Skip to content

mm, memcg: stop orphaned swap cache from using up memcg IDs

Shakeel Butt linux-mm lkml sashiko-reviews 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest4 patches 1 replyIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4mm/swap: free orphaned swap cache instead of leaving it to reclaimmm/swap.h mm/swap_state.c mm/swapfile.c+285 -70 replies
2/4mm: free swap cache that migration or a split leaves unmappedmm/huge_memory.c mm/migrate.c+17 -20 replies
3/4mm/memory: free the swap cache folio that a KSM copy leaves behindmm/memory.c+6 -00 replies
4/4mm/vmscan: don't give swap to an anon folio that nothing mapsmm/vmscan.c+26 -01 reply

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/4] mm, memcg: stop orphaned swap cache from using up memcg IDs

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/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

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

Signature · 1 line
2.53.1-Meta