Skip to contentSkip to conversation

mm: switch THP shrinker to list_lru

PATCH Applied Johannes Weiner 8 participants 36 messages May 27 – Jun 20

Subsystems (9)
lore
Share & tools
lore

Johannes Weiner

author maintainer
#0 lore raw

v5 cover mm: switch THP shrinker to list_lru

This is version 5 of switching the THP shrinker to list_lru.

Core of the new version is the list_lru/set_shrinker_bit fix up front, which minimally affects later patches; and a rebase onto the latest mm-unstable - replaced alloc_swap_folio() with __swap_cache_alloc().

The changes seemed small enough that *I chose to keep the review tags from v4*. Please shout if you object to this!

Changes in v5:
- patch 1 is a new fix for a very old, pre-existing set_shrinker_bit() problem in list_lru, where the bit can be set on a dying child memcg instead of the ancestor that actually received the item. Pointed out by Usama Arif and Sashiko; fix it first to make it minimally backportable and so the conversion is safe.
- patches 6 and 9 adapt to that fix's new memcg-by-reference lock_list_lru_of_memcg() signature
- collapse_huge_page(): propagate folio_memcg_alloc_deferred() failure as SCAN_ALLOC_HUGE_PAGE_FAIL instead of leaking SCAN_SUCCEED and falsely reporting a successful MADV_COLLAPSE (Usama Arif, Sashiko)
- deferred_split_isolate(): fix a UAF by reading folio state before list_lru_isolate(); once removed, a racing folio_put() frees the folio via the lockless list_empty() check while we still touch its flags and stats (Sashiko)
- rebased to mm-unstable of 2026-05-27, which simplifies the flatten prep patch (now anon-only, as alloc_swap_folio() was folded into the new __swap_cache_alloc()) and moves the swap-side folio_memcg_alloc_deferred() hook into __swap_cache_alloc(). Kairui, I would appreciate an eyeball on that.

Changes in v4:
- guard folio_memcg_alloc_deferred() with mem_cgroup_disabled() to fix NULL deref in __memcg_list_lru_alloc() when booting with cgroup_disable=memory (e.g., kdump capture kernel) -- reported and tested by Mikhail Zaslonko on s390 and x86
- flatten if (folio) branches in alloc_swap_folio() and alloc_anon_folio() in a prep patch so the list_lru allocation additions are a clean minimal diff (Lorenzo)
- folio_memcg_alloc_deferred() moved out of alloc_charge_folio() into the anon-only collapse_huge_page() path; collapse_file() shares that helper but its pages don't go on the THP shrinker queue (David)
- guard folio_memcg_alloc_deferred() with order > 1; mTHPs below order-2 can't be queued on the deferred split list (David)
- make deferred_split_lru static, hide behind folio_memcg_alloc_deferred() wrapper with GFP_KERNEL (Lorenzo)
- rename l -> lru throughout huge_memory.c (Lorenzo)
- kdoc for folio_memcg_list_lru_alloc() (Lorenzo)
- list_lru_lock_irq()/unlock_irq()/add_irq() irq-disabling variants; use list_lru_add_irq() in deferred_split_scan() (Lorenzo)
- reorder shrinker_free() before list_lru_destroy() (Lorenzo)

Changes in v3:
- dedicated lockdep_key for irqsafe deferred_split_lru.lock (syzbot)
- conditional list_lru ops in __folio_freeze_and_split_unmapped() (syzbot)
- annotate runs of inscrutable false, NULL, false function arguments (David)
- rename to folio_memcg_list_lru_alloc() (David)

Changes in v2:
- explicit rcu_read_lock() in __folio_freeze_and_split_unmapped() (Usama)
- split out list_lru prep bits (Dave)

The open-coded deferred split queue has issues. It's not NUMA-aware (when cgroup is enabled), and it's more complicated in the callsites interacting with it. Switching to list_lru fixes the NUMA problem and streamlines things. It also simplifies planned shrinker work.

Patch 1 fixes a pre-existing list_lru bug where the shrinker bit is set on the caller's memcg rather than the ancestor whose sublist the item actually lands on after a walk-up. Standalone, backportable; the rest of the series depends on it.

Patches 2-5 are cleanups and small refactors in list_lru code. They're basically independent, but make the THP shrinker conversion easier.

Patch 6 extends the list_lru API to allow the caller to control the locking scope. The THP shrinker has private state it needs to keep synchronized with the LRU state.

Patch 7 extends the list_lru API with a convenience helper to do list_lru head allocation (memcg_list_lru_alloc) when coming from a folio. Anon THPs are instantiated in several places, and with the folio reparenting patches pending, folio_memcg() access is now a more delicate dance. This avoids having to replicate that dance everywhere.

Patch 8 flattens the alloc_anon_folio() retry loop so the next patch's list_lru hook lands as a clean addition rather than nested deep inside an if (folio) block.

Patch 9 finally switches the deferred_split_queue to list_lru.

Based on mm-unstable.

 include/linux/huge_mm.h    |   7 +-
 include/linux/list_lru.h   |  70 +++++++++
 include/linux/memcontrol.h |   4 -
 include/linux/mmzone.h     |  12 --
 mm/huge_memory.c           | 364 +++++++++++++++------------------------------
 mm/internal.h              |   2 +-
 mm/khugepaged.c            |   5 +
 mm/list_lru.c              | 238 +++++++++++++++++++----------
 mm/memcontrol.c            |  12 +-
 mm/memory.c                |  38 ++---
 mm/mm_init.c               |  15 --
 mm/swap_state.c            |  10 ++
 12 files changed, 399 insertions(+), 378 deletions(-)

The base moved substantially since v4 (the swap allocation rework in particular reshuffled the alloc_swap_folio() landing spot), so the patch-level diff between v4 and v5 is non-obvious from a tree diff alone. For ease of review, here is the range-diff:

 -:  ------------ >  1:  f4f3933599b9 mm: list_lru: set shrinker bit on the memcg that owns the locked sublist
 1:  846dafe02e8b !  2:  e7b8f8bce2ec mm: list_lru: lock_list_lru_of_memcg() cannot return NULL if !skip_empty
    @@ mm/list_lru.c
     @@ mm/list_lru.c: bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
      	struct list_lru_one *l;
      
    - 	l = lock_list_lru_of_memcg(lru, nid, memcg, false, false);
    + 	l = lock_list_lru_of_memcg(lru, nid, &memcg, false, false);
     -	if (!l)
     -		return false;
      	if (list_empty(item)) {
      		list_add_tail(item, &l->list);
    - 		/* Set shrinker bit if the first element was added */
    + 		/*
     @@ mm/list_lru.c: bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid,
      {
      	struct list_lru_node *nlru = &lru->node[nid];
      	struct list_lru_one *l;
     +
    - 	l = lock_list_lru_of_memcg(lru, nid, memcg, false, false);
    + 	l = lock_list_lru_of_memcg(lru, nid, &memcg, false, false);
     -	if (!l)
     -		return false;
      	if (!list_empty(item)) {
 2:  afe28e645aff !  3:  f1e34640dff9 mm: list_lru: deduplicate unlock_list_lru()
    @@ mm/list_lru.c: static inline bool lock_list_lru(struct list_lru_one *l, bool irq
      		return false;
      	}
      	return true;
    -@@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
    - 	memcg = parent_mem_cgroup(memcg);
    +@@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid,
    + 	*memcg = parent_mem_cgroup(*memcg);
      	goto again;
      }
     -
    @@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_
      #else
      static void list_lru_register(struct list_lru *lru)
      {
    -@@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
    +@@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid,
      
      	return l;
      }

3: 9e5499facfb1 ! 4: 2612b71187ea mm: list_lru: move list dead check to lock_list_lru_of_memcg()
@@ mm/list_lru.c: list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
}

static inline struct list_lru_one *
-@@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg, +@@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid,
rcu_read_lock();
again:
- l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg));
+ l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(*memcg));
- if (likely(l) && lock_list_lru(l, irq)) {
- rcu_read_unlock();
- return l;
4: 855b908bfb82 ! 5: cc2819362f07 mm: list_lru: deduplicate lock_list_lru()
@@ mm/list_lru.c: list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
-}
-
static inline struct list_lru_one *
- lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
- bool irq, bool skip_empty)
-@@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
+ lock_list_lru_of_memcg(struct list_lru *lru, int nid,
+ struct mem_cgroup **memcg, bool irq, bool skip_empty)
+@@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid,
{
struct list_lru_one *l = &lru->node[nid].lru;

5: b8a70f1016f3 ! 6: 08c4561616df mm: list_lru: introduce caller locking for additions and deletions

    @@ include/linux/list_lru.h: int memcg_list_lru_alloc(struct mem_cgroup *memcg, str
     + * list_lru_lock: lock the sublist for the given node and memcg
     + * @lru: the lru pointer
     + * @nid: the node id of the sublist to lock.
    -+ * @memcg: the cgroup of the sublist to lock.
    ++ * @memcg: pointer to the cgroup of the sublist to lock. On return,
    ++ *         updated to the cgroup whose sublist was actually locked,
    ++ *         which may be an ancestor if the original memcg was dying.
     + *
     + * Returns the locked list_lru_one sublist. The caller must call
     + * list_lru_unlock() when done.
    @@ include/linux/list_lru.h: int memcg_list_lru_alloc(struct mem_cgroup *memcg, str
     + * Return: the locked list_lru_one, or NULL on failure
     + */
     +struct list_lru_one *list_lru_lock(struct list_lru *lru, int nid,
    -+		struct mem_cgroup *memcg);
    ++		struct mem_cgroup **memcg);
     +
     +/**
     + * list_lru_unlock: unlock a sublist locked by list_lru_lock()
    @@ include/linux/list_lru.h: int memcg_list_lru_alloc(struct mem_cgroup *memcg, str
     +void list_lru_unlock(struct list_lru_one *l);
     +
     +struct list_lru_one *list_lru_lock_irq(struct list_lru *lru, int nid,
    -+		struct mem_cgroup *memcg);
    ++		struct mem_cgroup **memcg);
     +void list_lru_unlock_irq(struct list_lru_one *l);
     +
     +struct list_lru_one *list_lru_lock_irqsave(struct list_lru *lru, int nid,
    -+		struct mem_cgroup *memcg, unsigned long *irq_flags);
    ++		struct mem_cgroup **memcg, unsigned long *irq_flags);
     +void list_lru_unlock_irqrestore(struct list_lru_one *l,
     +		unsigned long *irq_flags);
     +
    @@ mm/list_lru.c
     @@ mm/list_lru.c: list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
      
      static inline struct list_lru_one *
    - lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
    --		       bool irq, bool skip_empty)
    -+		       bool irq, unsigned long *irq_flags, bool skip_empty)
    + lock_list_lru_of_memcg(struct list_lru *lru, int nid,
    +-		       struct mem_cgroup **memcg, bool irq, bool skip_empty)
    ++		       struct mem_cgroup **memcg, bool irq,
    ++		       unsigned long *irq_flags, bool skip_empty)
      {
      	struct list_lru_one *l;
      
    -@@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
    +@@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid,
      again:
    - 	l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg));
    + 	l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(*memcg));
      	if (likely(l)) {
     -		lock_list_lru(l, irq);
     +		lock_list_lru(l, irq, irq_flags);
    @@ mm/list_lru.c: lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_
     @@ mm/list_lru.c: list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
      
      static inline struct list_lru_one *
    - lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
    --		       bool irq, bool skip_empty)
    -+		       bool irq, unsigned long *irq_flags, bool skip_empty)
    + lock_list_lru_of_memcg(struct list_lru *lru, int nid,
    +-		       struct mem_cgroup **memcg, bool irq, bool skip_empty)
    ++		       struct mem_cgroup **memcg, bool irq,
    ++		       unsigned long *irq_flags, bool skip_empty)
      {
      	struct list_lru_one *l = &lru->node[nid].lru;
      
    @@ mm/list_lru.c: list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
     -bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
     -		  struct mem_cgroup *memcg)
     +struct list_lru_one *list_lru_lock(struct list_lru *lru, int nid,
    -+				   struct mem_cgroup *memcg)
    ++				   struct mem_cgroup **memcg)
      {
     -	struct list_lru_node *nlru = &lru->node[nid];
     -	struct list_lru_one *l;
    @@ mm/list_lru.c: list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
     +}
     +
     +struct list_lru_one *list_lru_lock_irq(struct list_lru *lru, int nid,
    -+				       struct mem_cgroup *memcg)
    ++				       struct mem_cgroup **memcg)
     +{
     +	return lock_list_lru_of_memcg(lru, nid, memcg, /*irq=*/true,
     +				      /*irq_flags=*/NULL, /*skip_empty=*/false);
    @@ mm/list_lru.c: list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
     +	unlock_list_lru(l, /*irq_off=*/true, /*irq_flags=*/NULL);
     +}
      
    --	l = lock_list_lru_of_memcg(lru, nid, memcg, false, false);
    +-	l = lock_list_lru_of_memcg(lru, nid, &memcg, false, false);
     +struct list_lru_one *list_lru_lock_irqsave(struct list_lru *lru, int nid,
    -+					   struct mem_cgroup *memcg,
    ++					   struct mem_cgroup **memcg,
     +					   unsigned long *flags)
     +{
     +	return lock_list_lru_of_memcg(lru, nid, memcg, /*irq=*/true,
    @@ mm/list_lru.c: list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
     +{
      	if (list_empty(item)) {
      		list_add_tail(item, &l->list);
    - 		/* Set shrinker bit if the first element was added */
    + 		/*
    +@@ mm/list_lru.c: bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
    + 		 */
      		if (!l->nr_items++)
      			set_shrinker_bit(memcg, nid, lru_shrinker_id(lru));
     -		unlock_list_lru(l, false);
    @@ mm/list_lru.c: list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
     +	struct list_lru_one *l;
     +	bool ret;
     +
    -+	l = list_lru_lock(lru, nid, memcg);
    ++	l = list_lru_lock(lru, nid, &memcg);
     +	ret = __list_lru_add(lru, l, item, nid, memcg);
     +	list_lru_unlock(l);
     +	return ret;
    @@ mm/list_lru.c: list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
     +	struct list_lru_one *l;
     +	bool ret;
     +
    -+	l = list_lru_lock_irq(lru, nid, memcg);
    ++	l = list_lru_lock_irq(lru, nid, &memcg);
     +	ret = __list_lru_add(lru, l, item, nid, memcg);
     +	list_lru_unlock_irq(l);
     +	return ret;
    @@ mm/list_lru.c: EXPORT_SYMBOL_GPL(list_lru_add_obj);
      	struct list_lru_one *l;
     +	bool ret;
      
    --	l = lock_list_lru_of_memcg(lru, nid, memcg, false, false);
    +-	l = lock_list_lru_of_memcg(lru, nid, &memcg, false, false);
     -	if (!list_empty(item)) {
     -		list_del_init(item);
     -		l->nr_items--;
    @@ mm/list_lru.c: EXPORT_SYMBOL_GPL(list_lru_add_obj);
     -	}
     -	unlock_list_lru(l, false);
     -	return false;
    -+	l = list_lru_lock(lru, nid, memcg);
    ++	l = list_lru_lock(lru, nid, &memcg);
     +	ret = __list_lru_del(lru, l, item, nid);
     +	list_lru_unlock(l);
     +	return ret;
    @@ mm/list_lru.c: __list_lru_walk_one(struct list_lru *lru, int nid, struct mem_cgr
      	unsigned long isolated = 0;
      
      restart:
    --	l = lock_list_lru_of_memcg(lru, nid, memcg, irq_off, true);
    -+	l = lock_list_lru_of_memcg(lru, nid, memcg, /*irq=*/irq_off,
    +-	l = lock_list_lru_of_memcg(lru, nid, &memcg, irq_off, true);
    ++	l = lock_list_lru_of_memcg(lru, nid, &memcg, /*irq=*/irq_off,
     +				   /*irq_flags=*/NULL, /*skip_empty=*/true);
      	if (!l)
      		return isolated;
 6:  0bf8cd5bc205 =  7:  9b1b9ab5e749 mm: list_lru: introduce folio_memcg_list_lru_alloc()
 7:  a26656c1c0a5 !  8:  fd4e1d364dc2 mm: memory: flatten folio allocation retry loops

@@ Metadata
Author: Johannes Weiner <[email protected]>

## Commit message ##
- mm: memory: flatten folio allocation retry loops
+ mm: memory: flatten alloc_anon_folio() retry loop

- alloc_swap_folio() and alloc_anon_folio() use a top-level if (folio)
- that buries the success path four levels deep. This makes for awkward
- long lines and wrapping. The next patch will add more code here, so
- flatten this now to keep things clean and simple.
+ alloc_anon_folio() uses a top-level if (folio) that buries the success
+ path four levels deep. This makes for awkward long lines and wrapping.
+ The next patch will add more code here, so flatten this now to keep
+ things clean and simple.

- alloc_anon_folio() already has a next label, use it for !folio. Add
- the equivalent to alloc_swap_folio().
+ The next label is already there, use it for !folio.

No functional change intended.

@@ Commit message

## mm/memory.c ##
-@@ mm/memory.c: static struct folio *alloc_swap_folio(struct vm_fault *vmf)
- while (orders) {
- addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order);
- folio = vma_alloc_folio(gfp, order, vma, addr);
-- if (folio) {
-- if (!mem_cgroup_swapin_charge_folio(folio, vma->vm_mm,
-- gfp, entry))
-- return folio;
-+ if (!folio)
-+ goto next;
-+ if (mem_cgroup_swapin_charge_folio(folio, vma->vm_mm, gfp, entry)) {
- count_mthp_stat(order, MTHP_STAT_SWPIN_FALLBACK_CHARGE);
- folio_put(folio);
-+ goto next;
- }
-+ return folio;
-+next:
- count_mthp_stat(order, MTHP_STAT_SWPIN_FALLBACK);
- order = next_order(&orders, order);
- }
@@ mm/memory.c: static struct folio *alloc_anon_folio(struct vm_fault *vmf)
while (orders) {
addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order);
8: e454696ab1b7 ! 9: 70fe768450de mm: switch deferred split shrinker to list_lru

    @@ mm/huge_memory.c: static int __folio_freeze_and_split_unmapped(struct folio *fol
     +	 */
     +	dequeue_deferred = folio_test_anon(folio) && old_order > 1;
     +	if (dequeue_deferred) {
    ++		struct mem_cgroup *memcg;
    ++
     +		rcu_read_lock();
    ++		memcg = folio_memcg(folio);
     +		lru = list_lru_lock(&deferred_split_lru,
    -+				    folio_nid(folio), folio_memcg(folio));
    ++				    folio_nid(folio), &memcg);
     +	}
      	if (folio_ref_freeze(folio, folio_cache_ref_count(folio) + 1)) {
      		struct swap_cluster_info *ci = NULL;
    @@ mm/huge_memory.c: int split_folio_to_list(struct folio *folio, struct list_head
      bool __folio_unqueue_deferred_split(struct folio *folio)
      {
     -	struct deferred_split *ds_queue;
    ++	struct mem_cgroup *memcg;
     +	struct list_lru_one *lru;
     +	int nid = folio_nid(folio);
      	unsigned long flags;
    @@ mm/huge_memory.c: int split_folio_to_list(struct folio *folio, struct list_head
     -	if (!list_empty(&folio->_deferred_list)) {
     -		ds_queue->split_queue_len--;
     +	rcu_read_lock();
    -+	lru = list_lru_lock_irqsave(&deferred_split_lru, nid, folio_memcg(folio), &flags);
    ++	memcg = folio_memcg(folio);
    ++	lru = list_lru_lock_irqsave(&deferred_split_lru, nid, &memcg, &flags);
     +	if (__list_lru_del(&deferred_split_lru, lru, &folio->_deferred_list, nid)) {
      		if (folio_test_partially_mapped(folio)) {
      			folio_clear_partially_mapped(folio);
    @@ mm/huge_memory.c: void deferred_split_folio(struct folio *folio, bool partially_
     +
     +	rcu_read_lock();
     +	memcg = folio_memcg(folio);
    -+	lru = list_lru_lock_irqsave(&deferred_split_lru, nid, memcg, &flags);
    ++	lru = list_lru_lock_irqsave(&deferred_split_lru, nid, &memcg, &flags);
      	if (partially_mapped) {
      		if (!folio_test_partially_mapped(folio)) {
      			folio_set_partially_mapped(folio);
    @@ mm/huge_memory.c: static bool thp_underused(struct folio *folio)
     +		return LRU_REMOVED;
     +	}
     +
    -+	/* We lost race with folio_put() */
    -+	list_lru_isolate(lru, item);
    ++	/*
    ++	 * We lost race with folio_put(). Read folio state before the
    ++	 * isolate: folio_unqueue_deferred_split() checks list_empty()
    ++	 * locklessly, so once removed the folio can be freed any time.
    ++	 */
     +	if (folio_test_partially_mapped(folio)) {
     +		folio_clear_partially_mapped(folio);
     +		mod_mthp_stat(folio_order(folio),
     +			      MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1);
     +	}
    ++	list_lru_isolate(lru, item);
     +	return LRU_REMOVED;
     +}
     +
    @@ mm/huge_memory.c: static bool thp_underused(struct folio *folio)
      	struct folio *folio, *next;
     -	int split = 0, i;
     -	struct folio_batch fbatch;
    +-
    +-	folio_batch_init(&fbatch);
     +	int split = 0;
     +	unsigned long isolated;
      
    --	folio_batch_init(&fbatch);
    -+	isolated = list_lru_shrink_walk_irq(&deferred_split_lru, sc,
    -+					    deferred_split_isolate, &dispose);
    - 
     -retry:
     -	ds_queue = split_queue_lock_irqsave(sc->nid, sc->memcg, &flags);
     -	/* Take pin on all head pages to avoid freeing them under us */
    @@ mm/huge_memory.c: static bool thp_underused(struct folio *folio)
     -			break;
     -	}
     -	split_queue_unlock_irqrestore(ds_queue, flags);
    --
    ++	isolated = list_lru_shrink_walk_irq(&deferred_split_lru, sc,
    ++					    deferred_split_isolate, &dispose);
    + 
     -	for (i = 0; i < folio_batch_count(&fbatch); i++) {
     +	list_for_each_entry_safe(folio, next, &dispose, _deferred_list) {
      		bool did_split = false;
    @@ mm/khugepaged.c: static enum scan_result collapse_huge_page(struct mm_struct *mm
      	if (result != SCAN_SUCCEED)
      		goto out_nolock;
      
    -+	if (folio_memcg_alloc_deferred(folio))
    ++	if (folio_memcg_alloc_deferred(folio)) {
    ++		result = SCAN_ALLOC_HUGE_PAGE_FAIL;
     +		goto out_nolock;
    ++	}
     +
      	mmap_read_lock(mm);
      	result = hugepage_vma_revalidate(mm, pmd_addr, /*expect_anon=*/ true,
    @@ mm/memcontrol.c: static void mem_cgroup_css_offline(struct cgroup_subsys_state *
      	reparent_shrinker_deferred(memcg);
     
      ## mm/memory.c ##
    -@@ mm/memory.c: static struct folio *alloc_swap_folio(struct vm_fault *vmf)
    - 			folio_put(folio);
    - 			goto next;
    - 		}
    -+		if (order > 1 && folio_memcg_alloc_deferred(folio)) {
    -+			folio_put(folio);
    -+			goto fallback;
    -+		}
    - 		return folio;
    - next:
    - 		count_mthp_stat(order, MTHP_STAT_SWPIN_FALLBACK);
     @@ mm/memory.c: static struct folio *alloc_anon_folio(struct vm_fault *vmf)
      			folio_put(folio);
      			goto next;
    @@ mm/mm_init.c: static void __meminit pgdat_init_internals(struct pglist_data *pgd
      	pgdat_init_kcompactd(pgdat);
      
      	init_waitqueue_head(&pgdat->kswapd_wait);
    +
    + ## mm/swap_state.c ##
    +@@ mm/swap_state.c: static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
    + 		return ERR_PTR(-ENOMEM);
    + 	}
    + 
    ++	if (order > 1 && folio_memcg_alloc_deferred(folio)) {
    ++		spin_lock(&ci->lock);
    ++		__swap_cache_do_del_folio(ci, folio, entry, shadow);
    ++		spin_unlock(&ci->lock);
    ++		folio_unlock(folio);
    ++		/* nr_pages refs from swap cache, 1 from allocation */
    ++		folio_put_refs(folio, nr_pages + 1);
    ++		return ERR_PTR(-ENOMEM);
    ++	}
    ++
    + 	/* memsw uncharges swap when folio is added to swap cache */
    + 	memcg1_swapin(folio);
    + 	if (shadow)
author maintainer
#1 lore raw

PATCH v5 1/9 mm: list_lru: fix set_shrinker_bit() call during race with cgroup deletion

When list_lru_add() races with cgroup deletion, the shrinker bit is set on the wrong group and lost. This can cause a shrinker run to miss the cgroup that actually has the object.

When the passed in memcg is dead, the function finds the first non-dead parent from the passed in memcg and adds the object there; but the shrinker bit is set on the memcg that was passed in.

This bug is as old as the shrinker bitmap itself.

Fix it by returning the "effective" memcg from the locking function, and have the caller use that.

Fixes fae91d6d8be5 ("mm/list_lru.c: set bit in memcg shrinker bitmap on first list_lru item appearance")
Reported-by Sashiko
---
 mm/list_lru.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)
mm/list_lru.c +15 -11
index dd29bcf8eb5f..45d1b97737ea 100644
@@ -77,14 +77,14 @@ static inline bool lock_list_lru(struct list_lru_one *l, bool irq)
 }
 
 static inline struct list_lru_one *
-lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
-		       bool irq, bool skip_empty)
+lock_list_lru_of_memcg(struct list_lru *lru, int nid,
+		       struct mem_cgroup **memcg, bool irq, bool skip_empty)
 {
 	struct list_lru_one *l;
 
 	rcu_read_lock();
 again:
-	l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg));
+	l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(*memcg));
 	if (likely(l) && lock_list_lru(l, irq)) {
 		rcu_read_unlock();
 		return l;
@@ -97,8 +97,8 @@ lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
 		rcu_read_unlock();
 		return NULL;
 	}
-	VM_WARN_ON(!css_is_dying(&memcg->css));
-	memcg = parent_mem_cgroup(memcg);
+	VM_WARN_ON(!css_is_dying(&(*memcg)->css));
+	*memcg = parent_mem_cgroup(*memcg);
 	goto again;
 }
 
@@ -135,8 +135,8 @@ list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
 }
 
 static inline struct list_lru_one *
-lock_list_lru_of_memcg(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
-		       bool irq, bool skip_empty)
+lock_list_lru_of_memcg(struct list_lru *lru, int nid,
+		       struct mem_cgroup **memcg, bool irq, bool skip_empty)
 {
 	struct list_lru_one *l = &lru->node[nid].lru;
 
@@ -164,12 +164,16 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
 	struct list_lru_node *nlru = &lru->node[nid];
 	struct list_lru_one *l;
 
-	l = lock_list_lru_of_memcg(lru, nid, memcg, false, false);
+	l = lock_list_lru_of_memcg(lru, nid, &memcg, false, false);
 	if (!l)
 		return false;
 	if (list_empty(item)) {
 		list_add_tail(item, &l->list);
-		/* Set shrinker bit if the first element was added */
+		/*
+		 * Set shrinker bit on the memcg that owns the locked
+		 * sublist - lock_list_lru_of_memcg() may have walked up
+		 * past a dying memcg, and the bit must be set there.
+		 */
 		if (!l->nr_items++)
 			set_shrinker_bit(memcg, nid, lru_shrinker_id(lru));
 		unlock_list_lru(l, false);
@@ -204,7 +208,7 @@ bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid,
 {
 	struct list_lru_node *nlru = &lru->node[nid];
 	struct list_lru_one *l;
-	l = lock_list_lru_of_memcg(lru, nid, memcg, false, false);
+	l = lock_list_lru_of_memcg(lru, nid, &memcg, false, false);
 	if (!l)
 		return false;
 	if (!list_empty(item)) {
@@ -288,7 +292,7 @@ __list_lru_walk_one(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
 	unsigned long isolated = 0;
 
 restart:
-	l = lock_list_lru_of_memcg(lru, nid, memcg, irq_off, true);
+	l = lock_list_lru_of_memcg(lru, nid, &memcg, irq_off, true);
 	if (!l)
 		return isolated;
 	list_for_each_safe(item, n, &l->list) {
Signature · 1 line
2.54.0
reviewer Acked-by on v5 1/9
#2 lore raw

Re: [PATCH v5 1/9] mm: list_lru: fix set_shrinker_bit() call during race with cgroup deletion

Quoted 13 lines · Johannes Weiner

When list_lru_add() races with cgroup deletion, the shrinker bit is set on the wrong group and lost. This can cause a shrinker run to miss the cgroup that actually has the object.

When the passed in memcg is dead, the function finds the first non-dead parent from the passed in memcg and adds the object there; but the shrinker bit is set on the memcg that was passed in.

This bug is as old as the shrinker bitmap itself.

Fix it by returning the "effective" memcg from the locking function, and have the caller use that.

Quoted 8 lines
Fixes fae91d6d8be5 ("mm/list_lru.c: set bit in memcg shrinker bitmap on first list_lru item appearance")
Reported-by Sashiko
---
 mm/list_lru.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)
Reviewed-by on v5 1/9
#3 lore raw

Re: [PATCH v5 1/9] mm: list_lru: fix set_shrinker_bit() call during race with cgroup deletion

In reply to Johannes Weiner
Quoted 8 lines · Johannes Weiner

When list_lru_add() races with cgroup deletion, the shrinker bit is set on the wrong group and lost. This can cause a shrinker run to miss the cgroup that actually has the object.

When the passed in memcg is dead, the function finds the first non-dead parent from the passed in memcg and adds the object there; but the shrinker bit is set on the memcg that was passed in.

This means we just miss to reclaim some obj, but won't crash the kernel.

Quoted 9 lines

This bug is as old as the shrinker bitmap itself.

Fix it by returning the "effective" memcg from the locking function, and have the caller use that.

Fixes fae91d6d8be5 ("mm/list_lru.c: set bit in memcg shrinker bitmap on first list_lru item appearance")
Reported-by Sashiko

So we don't want to cc stable, right?

The fix looks right, so

Signature · 2 lines
Wei Yang
Help you, Help me
author maintainer
#4 lore raw

PATCH v5 2/9 mm: list_lru: lock_list_lru_of_memcg() cannot return NULL if !skip_empty

In reply to Johannes Weiner

skip_empty is only for the shrinker to abort and skip a list that's empty or whose cgroup is being deleted.

For list additions and deletions, the cgroup hierarchy is walked upwards until a valid list_lru head is found, or it will fall back to the node list. Acquiring the lock won't fail. Remove the NULL checks in those callers.

---
 mm/list_lru.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
mm/list_lru.c +1 -4
index 45d1b97737ea..77999ed78fa5 100644
@@ -165,8 +165,6 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
 	struct list_lru_one *l;
 
 	l = lock_list_lru_of_memcg(lru, nid, &memcg, false, false);
-	if (!l)
-		return false;
 	if (list_empty(item)) {
 		list_add_tail(item, &l->list);
 		/*
@@ -208,9 +206,8 @@ bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid,
 {
 	struct list_lru_node *nlru = &lru->node[nid];
 	struct list_lru_one *l;
+
 	l = lock_list_lru_of_memcg(lru, nid, &memcg, false, false);
-	if (!l)
-		return false;
 	if (!list_empty(item)) {
 		list_del_init(item);
 		l->nr_items--;
Signature · 1 line
2.54.0
author maintainer
#5 lore raw

PATCH v5 3/9 mm: list_lru: deduplicate unlock_list_lru()

In reply to Johannes Weiner

The MEMCG and !MEMCG variants are the same. lock_list_lru() has the same pattern when bailing. Consolidate into a common implementation.

---
 mm/list_lru.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)
mm/list_lru.c +9 -20
index 77999ed78fa5..5497034e80f3 100644
@@ -15,6 +15,14 @@
 #include "slab.h"
 #include "internal.h"
 
+static inline void unlock_list_lru(struct list_lru_one *l, bool irq_off)
+{
+	if (irq_off)
+		spin_unlock_irq(&l->lock);
+	else
+		spin_unlock(&l->lock);
+}
+
 #ifdef CONFIG_MEMCG
 static LIST_HEAD(memcg_list_lrus);
 static DEFINE_MUTEX(list_lrus_mutex);
@@ -67,10 +75,7 @@ static inline bool lock_list_lru(struct list_lru_one *l, bool irq)
 	else
 		spin_lock(&l->lock);
 	if (unlikely(READ_ONCE(l->nr_items) == LONG_MIN)) {
-		if (irq)
-			spin_unlock_irq(&l->lock);
-		else
-			spin_unlock(&l->lock);
+		unlock_list_lru(l, irq);
 		return false;
 	}
 	return true;
@@ -101,14 +106,6 @@ lock_list_lru_of_memcg(struct list_lru *lru, int nid,
 	*memcg = parent_mem_cgroup(*memcg);
 	goto again;
 }
-
-static inline void unlock_list_lru(struct list_lru_one *l, bool irq_off)
-{
-	if (irq_off)
-		spin_unlock_irq(&l->lock);
-	else
-		spin_unlock(&l->lock);
-}
 #else
 static void list_lru_register(struct list_lru *lru)
 {
@@ -147,14 +144,6 @@ lock_list_lru_of_memcg(struct list_lru *lru, int nid,
 
 	return l;
 }
-
-static inline void unlock_list_lru(struct list_lru_one *l, bool irq_off)
-{
-	if (irq_off)
-		spin_unlock_irq(&l->lock);
-	else
-		spin_unlock(&l->lock);
-}
 #endif /* CONFIG_MEMCG */
 
 /* The caller must ensure the memcg lifetime. */
Signature · 1 line
2.54.0
author maintainer
#6 lore raw

PATCH v5 4/9 mm: list_lru: move list dead check to lock_list_lru_of_memcg()

In reply to Johannes Weiner

Only the MEMCG variant of lock_list_lru() needs to check if there is a race with cgroup deletion and list reparenting. Move the check to the caller, so that the next patch can unify the lock_list_lru() variants.

---
 mm/list_lru.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
mm/list_lru.c +8 -9
index 5497034e80f3..7d0523e44010 100644
@@ -68,17 +68,12 @@ list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
 	return &lru->node[nid].lru;
 }
 
-static inline bool lock_list_lru(struct list_lru_one *l, bool irq)
+static inline void lock_list_lru(struct list_lru_one *l, bool irq)
 {
 	if (irq)
 		spin_lock_irq(&l->lock);
 	else
 		spin_lock(&l->lock);
-	if (unlikely(READ_ONCE(l->nr_items) == LONG_MIN)) {
-		unlock_list_lru(l, irq);
-		return false;
-	}
-	return true;
 }
 
 static inline struct list_lru_one *
@@ -90,9 +85,13 @@ lock_list_lru_of_memcg(struct list_lru *lru, int nid,
 	rcu_read_lock();
 again:
 	l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(*memcg));
-	if (likely(l) && lock_list_lru(l, irq)) {
-		rcu_read_unlock();
-		return l;
+	if (likely(l)) {
+		lock_list_lru(l, irq);
+		if (likely(READ_ONCE(l->nr_items) != LONG_MIN)) {
+			rcu_read_unlock();
+			return l;
+		}
+		unlock_list_lru(l, irq);
 	}
 	/*
 	 * Caller may simply bail out if raced with reparenting or
Signature · 1 line
2.54.0
author maintainer
#7 lore raw

PATCH v5 5/9 mm: list_lru: deduplicate lock_list_lru()

In reply to Johannes Weiner

The MEMCG and !MEMCG paths have the same pattern. Share the code.

---
 mm/list_lru.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)
mm/list_lru.c +9 -12
index 7d0523e44010..fdb3fe2ea64f 100644
@@ -15,6 +15,14 @@
 #include "slab.h"
 #include "internal.h"
 
+static inline void lock_list_lru(struct list_lru_one *l, bool irq)
+{
+	if (irq)
+		spin_lock_irq(&l->lock);
+	else
+		spin_lock(&l->lock);
+}
+
 static inline void unlock_list_lru(struct list_lru_one *l, bool irq_off)
 {
 	if (irq_off)
@@ -68,14 +76,6 @@ list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
 	return &lru->node[nid].lru;
 }
 
-static inline void lock_list_lru(struct list_lru_one *l, bool irq)
-{
-	if (irq)
-		spin_lock_irq(&l->lock);
-	else
-		spin_lock(&l->lock);
-}
-
 static inline struct list_lru_one *
 lock_list_lru_of_memcg(struct list_lru *lru, int nid,
 		       struct mem_cgroup **memcg, bool irq, bool skip_empty)
@@ -136,10 +136,7 @@ lock_list_lru_of_memcg(struct list_lru *lru, int nid,
 {
 	struct list_lru_one *l = &lru->node[nid].lru;
 
-	if (irq)
-		spin_lock_irq(&l->lock);
-	else
-		spin_lock(&l->lock);
+	lock_list_lru(l, irq);
 
 	return l;
 }
Signature · 1 line
2.54.0
#8 lore raw

Re: [PATCH v5 5/9] mm: list_lru: deduplicate lock_list_lru()

Quoted 16 lines · Johannes Weiner

The MEMCG and !MEMCG paths have the same pattern. Share the code.

---
mm/list_lru.c | 21 +++++++++------------

1 file changed, 9 insertions(+), 12 deletions(-)
mm/list_lru.c +0 -0
index 7d0523e44010..fdb3fe2ea64f 100644

mm/list_lru.c line 16

Quoted 2 lines
mm/list_lru.c +0 -0
@@ -15,6 +15,14 @@
 #include "slab.h"
 #include "internal.h"

Hi, Johannes

One very tiny nit below.

mm/list_lru.c line 18

Quoted 2 lines
mm/list_lru.c +1 -0
+static inline void lock_list_lru(struct list_lru_one *l, bool irq)

Here we use @irq.

mm/list_lru.c line 26

Quoted 8 lines
mm/list_lru.c +7 -0
+{
+	if (irq)
+		spin_lock_irq(&l->lock);
+	else
+		spin_lock(&l->lock);
+}
+
 static inline void unlock_list_lru(struct list_lru_one *l, bool irq_off)

Here we use @irq_off.

Do you think it would be nicer to unify the parameter name?

Also the name in callsite and the argument annotation. Would it be cleaner readers?

Signature · 2 lines
Wei Yang
Help you, Help me
author maintainer
#9 lore raw

Re: [PATCH v5 5/9] mm: list_lru: deduplicate lock_list_lru()

Quoted 40 lines · Wei Yang
Quoted 16 lines · Johannes Weiner

The MEMCG and !MEMCG paths have the same pattern. Share the code.

---
mm/list_lru.c | 21 +++++++++------------

1 file changed, 9 insertions(+), 12 deletions(-)
mm/list_lru.c +0 -0
index 7d0523e44010..fdb3fe2ea64f 100644

mm/list_lru.c line 16

Quoted 2 lines
mm/list_lru.c +0 -0
@@ -15,6 +15,14 @@
 #include "slab.h"
 #include "internal.h"

Hi, Johannes

One very tiny nit below.

mm/list_lru.c line 18

Quoted 2 lines
mm/list_lru.c +1 -0
+static inline void lock_list_lru(struct list_lru_one *l, bool irq)

Here we use @irq.

mm/list_lru.c line 26

Quoted 8 lines
mm/list_lru.c +7 -0
+{
+	if (irq)
+		spin_lock_irq(&l->lock);
+	else
+		spin_lock(&l->lock);
+}
+
 static inline void unlock_list_lru(struct list_lru_one *l, bool irq_off)

Here we use @irq_off.

Quoted 1 line

Do you think it would be nicer to unify the parameter name?

Yes, I think it would be nicer.

Note that I inherited this - we had irq on the lock and irq_off on the unlock before already. I didn't want to mix even more yak shaving prep patches into this series.

Mind sending a follow-up patch on top of mm-unstable?

#10 lore raw

Re: [PATCH v5 5/9] mm: list_lru: deduplicate lock_list_lru()

Quoted 41 lines · Johannes Weiner
Quoted 40 lines · Wei Yang
Quoted 16 lines · Johannes Weiner

The MEMCG and !MEMCG paths have the same pattern. Share the code.

---
mm/list_lru.c | 21 +++++++++------------

1 file changed, 9 insertions(+), 12 deletions(-)
mm/list_lru.c +0 -0
index 7d0523e44010..fdb3fe2ea64f 100644

mm/list_lru.c line 16

Quoted 2 lines
mm/list_lru.c +0 -0
@@ -15,6 +15,14 @@
 #include "slab.h"
 #include "internal.h"

Hi, Johannes

One very tiny nit below.

mm/list_lru.c line 18

Quoted 2 lines
mm/list_lru.c +1 -0
+static inline void lock_list_lru(struct list_lru_one *l, bool irq)

Here we use @irq.

mm/list_lru.c line 26

Quoted 8 lines
mm/list_lru.c +7 -0
+{
+	if (irq)
+		spin_lock_irq(&l->lock);
+	else
+		spin_lock(&l->lock);
+}
+
 static inline void unlock_list_lru(struct list_lru_one *l, bool irq_off)

Here we use @irq_off.

Quoted 7 lines
Quoted 1 line

Do you think it would be nicer to unify the parameter name?

Yes, I think it would be nicer.

Note that I inherited this - we had irq on the lock and irq_off on the unlock before already. I didn't want to mix even more yak shaving prep patches into this series.

Reasonable.

Quoted 2 lines

Mind sending a follow-up patch on top of mm-unstable?

Thanks, I am glad to.

Since this is trivial, I would wait until everything is settle down.

Looks good to you?

Signature · 2 lines
Wei Yang
Help you, Help me
author maintainer
#11 lore raw

PATCH v5 6/9 mm: list_lru: introduce caller locking for additions and deletions

In reply to Johannes Weiner

Locking is currently internal to the list_lru API. However, a caller might want to keep auxiliary state synchronized with the LRU state.

For example, the THP shrinker uses the lock of its custom LRU to keep PG_partially_mapped and vmstats consistent.

To allow the THP shrinker to switch to list_lru, provide normal and irqsafe locking primitives as well as caller-locked variants of the addition and deletion functions.

---
 include/linux/list_lru.h |  43 +++++++++++++
 mm/list_lru.c            | 133 ++++++++++++++++++++++++++++++---------
 2 files changed, 145 insertions(+), 31 deletions(-)
include/linux/list_lru.h +43 -0
index fe739d35a864..134cb3e5652a 100644
@@ -83,6 +83,46 @@ int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
 			 gfp_t gfp);
 void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent);
 
+/**
+ * list_lru_lock: lock the sublist for the given node and memcg
+ * @lru: the lru pointer
+ * @nid: the node id of the sublist to lock.
+ * @memcg: pointer to the cgroup of the sublist to lock. On return,
+ *         updated to the cgroup whose sublist was actually locked,
+ *         which may be an ancestor if the original memcg was dying.
+ *
+ * Returns the locked list_lru_one sublist. The caller must call
+ * list_lru_unlock() when done.
+ *
+ * You must ensure that the memcg is not freed during this call (e.g., with
+ * rcu or by taking a css refcnt).
+ *
+ * Return: the locked list_lru_one, or NULL on failure
+ */
+struct list_lru_one *list_lru_lock(struct list_lru *lru, int nid,
+		struct mem_cgroup **memcg);
+
+/**
+ * list_lru_unlock: unlock a sublist locked by list_lru_lock()
+ * @l: the list_lru_one to unlock
+ */
+void list_lru_unlock(struct list_lru_one *l);
+
+struct list_lru_one *list_lru_lock_irq(struct list_lru *lru, int nid,
+		struct mem_cgroup **memcg);
+void list_lru_unlock_irq(struct list_lru_one *l);
+
+struct list_lru_one *list_lru_lock_irqsave(struct list_lru *lru, int nid,
+		struct mem_cgroup **memcg, unsigned long *irq_flags);
+void list_lru_unlock_irqrestore(struct list_lru_one *l,
+		unsigned long *irq_flags);
+
+/* Caller-locked variants, see list_lru_add() etc for documentation */
+bool __list_lru_add(struct list_lru *lru, struct list_lru_one *l,
+		struct list_head *item, int nid, struct mem_cgroup *memcg);
+bool __list_lru_del(struct list_lru *lru, struct list_lru_one *l,
+		struct list_head *item, int nid);
+
 /**
  * list_lru_add: add an element to the lru list's tail
  * @lru: the lru pointer
@@ -115,6 +155,9 @@ void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *paren
 bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
 		    struct mem_cgroup *memcg);
 
+bool list_lru_add_irq(struct list_lru *lru, struct list_head *item, int nid,
+		      struct mem_cgroup *memcg);
+
 /**
  * list_lru_add_obj: add an element to the lru list's tail
  * @lru: the lru pointer
mm/list_lru.c +102 -31

210 lines not on this page · raw message

Signature · 1 line
2.54.0
author maintainer
#12 lore raw

PATCH v5 7/9 mm: list_lru: introduce folio_memcg_list_lru_alloc()

In reply to Johannes Weiner

memcg_list_lru_alloc() is called every time an object that may end up on the list_lru is created. It needs to quickly check if the list_lru heads for the memcg already exist, and allocate them when they don't.

Doing this with folio objects is tricky: folio_memcg() is not stable and requires either RCU protection or pinning the cgroup. But it's desirable to make the existence check lightweight under RCU, and only pin the memcg when we need to allocate list_lru heads and may block.

In preparation for switching the THP shrinker to list_lru, add a helper function for allocating list_lru heads coming from a folio.

---
 include/linux/list_lru.h | 27 +++++++++++++++++++++++++++
 mm/list_lru.c            | 39 ++++++++++++++++++++++++++++++++++-----
 2 files changed, 61 insertions(+), 5 deletions(-)
include/linux/list_lru.h +27 -0
index 134cb3e5652a..a450fffe1550 100644
@@ -81,6 +81,33 @@ static inline int list_lru_init_memcg_key(struct list_lru *lru, struct shrinker
 
 int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
 			 gfp_t gfp);
+
+#ifdef CONFIG_MEMCG
+/**
+ * folio_memcg_list_lru_alloc - allocate list_lru heads for shrinkable folio
+ * @folio: the newly allocated & charged folio
+ * @lru: the list_lru this might be queued on
+ * @gfp: gfp mask
+ *
+ * Allocate list_lru heads (per-memcg, per-node) needed to queue this
+ * particular folio down the line.
+ *
+ * This does memcg_list_lru_alloc(), but on the memcg that @folio is
+ * associated with. Handles folio_memcg() access rules in the fast
+ * path (list_lru heads allocated) and the allocation slowpath.
+ *
+ * Returns 0 on success, a negative error value otherwise.
+ */
+int folio_memcg_list_lru_alloc(struct folio *folio, struct list_lru *lru,
+			       gfp_t gfp);
+#else
+static inline int folio_memcg_list_lru_alloc(struct folio *folio,
+					     struct list_lru *lru, gfp_t gfp)
+{
+	return 0;
+}
+#endif
+
 void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent);
 
 /**
mm/list_lru.c +34 -5
index 402bb028114d..41a811966063 100644
@@ -568,17 +568,14 @@ static inline bool memcg_list_lru_allocated(struct mem_cgroup *memcg,
 	return idx < 0 || xa_load(&lru->xa, idx);
 }
 
-int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
-			 gfp_t gfp)
+static int __memcg_list_lru_alloc(struct mem_cgroup *memcg,
+				  struct list_lru *lru, gfp_t gfp)
 {
 	unsigned long flags;
 	struct list_lru_memcg *mlru = NULL;
 	struct mem_cgroup *pos, *parent;
 	XA_STATE(xas, &lru->xa, 0);
 
-	if (!list_lru_memcg_aware(lru) || memcg_list_lru_allocated(memcg, lru))
-		return 0;
-
 	gfp &= GFP_RECLAIM_MASK;
 	/*
 	 * Because the list_lru can be reparented to the parent cgroup's
@@ -619,6 +616,38 @@ int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
 
 	return xas_error(&xas);
 }
+
+int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
+			 gfp_t gfp)
+{
+	if (!list_lru_memcg_aware(lru) || memcg_list_lru_allocated(memcg, lru))
+		return 0;
+	return __memcg_list_lru_alloc(memcg, lru, gfp);
+}
+
+int folio_memcg_list_lru_alloc(struct folio *folio, struct list_lru *lru,
+			       gfp_t gfp)
+{
+	struct mem_cgroup *memcg;
+	int res;
+
+	if (!list_lru_memcg_aware(lru))
+		return 0;
+
+	/* Fast path when list_lru heads already exist */
+	rcu_read_lock();
+	memcg = folio_memcg(folio);
+	res = memcg_list_lru_allocated(memcg, lru);
+	rcu_read_unlock();
+	if (likely(res))
+		return 0;
+
+	/* Allocation may block, pin the memcg */
+	memcg = get_mem_cgroup_from_folio(folio);
+	res = __memcg_list_lru_alloc(memcg, lru, gfp);
+	mem_cgroup_put(memcg);
+	return res;
+}
 #else
 static inline void memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
 {
Signature · 1 line
2.54.0
author maintainer
#13 lore raw

PATCH v5 8/9 mm: memory: flatten alloc_anon_folio() retry loop

In reply to Johannes Weiner

alloc_anon_folio() uses a top-level if (folio) that buries the success path four levels deep. This makes for awkward long lines and wrapping. The next patch will add more code here, so flatten this now to keep things clean and simple.

The next label is already there, use it for !folio.

No functional change intended.

---
 mm/memory.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
mm/memory.c +17 -17
index 7c020995eafc..135f5c0f57bd 100644
@@ -5215,24 +5215,24 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 	while (orders) {
 		addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order);
 		folio = vma_alloc_folio(gfp, order, vma, addr);
-		if (folio) {
-			if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) {
-				count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
-				folio_put(folio);
-				goto next;
-			}
-			folio_throttle_swaprate(folio, gfp);
-			/*
-			 * When a folio is not zeroed during allocation
-			 * (__GFP_ZERO not used) or user folios require special
-			 * handling, folio_zero_user() is used to make sure
-			 * that the page corresponding to the faulting address
-			 * will be hot in the cache after zeroing.
-			 */
-			if (user_alloc_needs_zeroing())
-				folio_zero_user(folio, vmf->address);
-			return folio;
+		if (!folio)
+			goto next;
+		if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) {
+			count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
+			folio_put(folio);
+			goto next;
 		}
+		folio_throttle_swaprate(folio, gfp);
+		/*
+		 * When a folio is not zeroed during allocation
+		 * (__GFP_ZERO not used) or user folios require special
+		 * handling, folio_zero_user() is used to make sure
+		 * that the page corresponding to the faulting address
+		 * will be hot in the cache after zeroing.
+		 */
+		if (user_alloc_needs_zeroing())
+			folio_zero_user(folio, vmf->address);
+		return folio;
 next:
 		count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);
 		order = next_order(&orders, order);
Signature · 1 line
2.54.0
reviewer Reviewed-by on v5 8/9
#14 lore raw

Re: [PATCH v5 8/9] mm: memory: flatten alloc_anon_folio() retry loop

Quoted 7 lines · Johannes Weiner

alloc_anon_folio() uses a top-level if (folio) that buries the success path four levels deep. This makes for awkward long lines and wrapping. The next patch will add more code here, so flatten this now to keep things clean and simple.

The next label is already there, use it for !folio.

Quoted 7 lines

No functional change intended.

---

Quoted 49 lines
 mm/memory.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
mm/memory.c +17 -17
index 7c020995eafc..135f5c0f57bd 100644
@@ -5215,24 +5215,24 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 	while (orders) {
 		addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order);
 		folio = vma_alloc_folio(gfp, order, vma, addr);
-		if (folio) {
-			if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) {
-				count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
-				folio_put(folio);
-				goto next;
-			}
-			folio_throttle_swaprate(folio, gfp);
-			/*
-			 * When a folio is not zeroed during allocation
-			 * (__GFP_ZERO not used) or user folios require special
-			 * handling, folio_zero_user() is used to make sure
-			 * that the page corresponding to the faulting address
-			 * will be hot in the cache after zeroing.
-			 */
-			if (user_alloc_needs_zeroing())
-				folio_zero_user(folio, vmf->address);
-			return folio;
+		if (!folio)
+			goto next;
+		if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) {
+			count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
+			folio_put(folio);
+			goto next;
 		}
+		folio_throttle_swaprate(folio, gfp);
+		/*
+		 * When a folio is not zeroed during allocation
+		 * (__GFP_ZERO not used) or user folios require special
+		 * handling, folio_zero_user() is used to make sure
+		 * that the page corresponding to the faulting address
+		 * will be hot in the cache after zeroing.
+		 */
+		if (user_alloc_needs_zeroing())
+			folio_zero_user(folio, vmf->address);
+		return folio;
 next:
 		count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);
 		order = next_order(&orders, order);
author maintainer
#15 lore raw

PATCH v5 9/9 mm: switch deferred split shrinker to list_lru

In reply to Johannes Weiner

The deferred split queue handles cgroups in a suboptimal fashion. The queue is per-NUMA node or per-cgroup, not the intersection. That means on a cgrouped system, a node-restricted allocation entering reclaim can end up splitting large pages on other nodes:

alloc/unmap
deferred_split_folio()
list_add_tail(memcg->split_queue) set_shrinker_bit(memcg, node, deferred_shrinker_id)

for_each_zone_zonelist_nodemask(restricted_nodes)
mem_cgroup_iter()
shrink_slab(node, memcg)
shrink_slab_memcg(node, memcg)
if test_shrinker_bit(memcg, node, deferred_shrinker_id)
deferred_split_scan()
walks memcg->split_queue

The shrinker bit adds an imperfect guard rail. As soon as the cgroup has a single large page on the node of interest, all large pages owned by that memcg, including those on other nodes, will be split.

list_lru properly sets up per-node, per-cgroup lists. As a bonus, it streamlines a lot of the list operations and reclaim walks. It's used widely by other major shrinkers already. Convert the deferred split queue as well.

The list_lru per-memcg heads are instantiated on demand when the first object of interest is allocated for a cgroup, by calling folio_memcg_alloc_deferred(). Add calls to where splittable pages are
created: anon faults, swapin faults, khugepaged collapse.

These calls create all possible node heads for the cgroup at once, so the migration code (between nodes) doesn't need any special care.

---
 include/linux/huge_mm.h    |   7 +-
 include/linux/memcontrol.h |   4 -
 include/linux/mmzone.h     |  12 --
 mm/huge_memory.c           | 364 +++++++++++++------------------------
 mm/internal.h              |   2 +-
 mm/khugepaged.c            |   5 +
 mm/memcontrol.c            |  12 +-
 mm/memory.c                |   4 +
 mm/mm_init.c               |  15 --
 mm/swap_state.c            |  10 +
 10 files changed, 150 insertions(+), 285 deletions(-)
include/linux/huge_mm.h +3 -4
index edece3e26985..f6c2531a27a3 100644
@@ -423,10 +423,10 @@ static inline int split_huge_page(struct page *page)
 {
 	return split_huge_page_to_list_to_order(page, NULL, 0);
 }
+
+int folio_memcg_alloc_deferred(struct folio *folio);
+
 void deferred_split_folio(struct folio *folio, bool partially_mapped);
-#ifdef CONFIG_MEMCG
-void reparent_deferred_split_queue(struct mem_cgroup *memcg);
-#endif
 
 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
 		unsigned long address, bool freeze);
@@ -664,7 +664,6 @@ static inline int folio_split(struct folio *folio, unsigned int new_order,
 }
 
 static inline void deferred_split_folio(struct folio *folio, bool partially_mapped) {}
-static inline void reparent_deferred_split_queue(struct mem_cgroup *memcg) {}
 #define split_huge_pmd(__vma, __pmd, __address)	\
 	do { } while (0)
 
include/linux/memcontrol.h +0 -4
index bf1a6e131eca..20404e59fb3b 100644
@@ -278,10 +278,6 @@ struct mem_cgroup {
 	struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
 #endif
 
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-	struct deferred_split deferred_split_queue;
-#endif
-
 #ifdef CONFIG_LRU_GEN_WALKS_MMU
 	/* per-memcg mm_struct list */
 	struct lru_gen_mm_list mm_list;
include/linux/mmzone.h +0 -12
index 1331a7b93f33..8e449f524f26 100644
@@ -1431,14 +1431,6 @@ struct zonelist {
  */
 extern struct page *mem_map;
 
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-struct deferred_split {
-	spinlock_t split_queue_lock;
-	struct list_head split_queue;
-	unsigned long split_queue_len;
-};
-#endif
-
 #ifdef CONFIG_MEMORY_FAILURE
 /*
  * Per NUMA node memory failure handling statistics.
@@ -1564,10 +1556,6 @@ typedef struct pglist_data {
 	unsigned long first_deferred_pfn;
 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
 
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-	struct deferred_split deferred_split_queue;
-#endif
-
 #ifdef CONFIG_NUMA_BALANCING
 	/* start time in ms of current promote rate limit period */
 	unsigned int nbp_rl_start;
mm/huge_memory.c +124 -240

517 lines not on this page · raw message

mm/internal.h +1 -1
index 5602393054f3..181e79f1d6a2 100644
@@ -852,7 +852,7 @@ static inline bool folio_unqueue_deferred_split(struct folio *folio)
 	/*
 	 * At this point, there is no one trying to add the folio to
 	 * deferred_list. If folio is not in deferred_list, it's safe
-	 * to check without acquiring the split_queue_lock.
+	 * to check without acquiring the list_lru lock.
 	 */
 	if (data_race(list_empty(&folio->_deferred_list)))
 		return false;
mm/khugepaged.c +5 -0
index 35a5f8c44c18..8ffb47f1e845 100644
@@ -1306,6 +1306,11 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long s
 	if (result != SCAN_SUCCEED)
 		goto out_nolock;
 
+	if (folio_memcg_alloc_deferred(folio)) {
+		result = SCAN_ALLOC_HUGE_PAGE_FAIL;
+		goto out_nolock;
+	}
+
 	mmap_read_lock(mm);
 	result = hugepage_vma_revalidate(mm, pmd_addr, /*expect_anon=*/ true,
 					 &vma, cc, order);
mm/memcontrol.c +3 -9
index 92269740eef1..d93564af82b5 100644
@@ -4035,11 +4035,6 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)
 	for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
 		memcg->cgwb_frn[i].done =
 			__WB_COMPLETION_INIT(&memcg_cgwb_frn_waitq);
-#endif
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-	spin_lock_init(&memcg->deferred_split_queue.split_queue_lock);
-	INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
-	memcg->deferred_split_queue.split_queue_len = 0;
 #endif
 	lru_gen_init_memcg(memcg);
 	return memcg;
@@ -4191,11 +4186,10 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
 	zswap_memcg_offline_cleanup(memcg);
 
 	memcg_offline_kmem(memcg);
-	reparent_deferred_split_queue(memcg);
 	/*
-	 * The reparenting of objcg must be after the reparenting of the
-	 * list_lru and deferred_split_queue above, which ensures that they will
-	 * not mistakenly get the parent list_lru and deferred_split_queue.
+	 * The reparenting of objcg must be after the reparenting of
+	 * the list_lru in memcg_offline_kmem(), which ensures that
+	 * they will not mistakenly get the parent list_lru.
 	 */
 	memcg_reparent_objcgs(memcg);
 	reparent_shrinker_deferred(memcg);
mm/memory.c +4 -0
index 135f5c0f57bd..f22e61d8c8de 100644
@@ -5222,6 +5222,10 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 			folio_put(folio);
 			goto next;
 		}
+		if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+			folio_put(folio);
+			goto fallback;
+		}
 		folio_throttle_swaprate(folio, gfp);
 		/*
 		 * When a folio is not zeroed during allocation
mm/mm_init.c +0 -15
index db5568cf36e1..c0a7f1cf6fef 100644
@@ -1373,19 +1373,6 @@ static void __init calculate_node_totalpages(struct pglist_data *pgdat,
 	pr_debug("On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
 }
 
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-static void pgdat_init_split_queue(struct pglist_data *pgdat)
-{
-	struct deferred_split *ds_queue = &pgdat->deferred_split_queue;
-
-	spin_lock_init(&ds_queue->split_queue_lock);
-	INIT_LIST_HEAD(&ds_queue->split_queue);
-	ds_queue->split_queue_len = 0;
-}
-#else
-static void pgdat_init_split_queue(struct pglist_data *pgdat) {}
-#endif
-
 #ifdef CONFIG_COMPACTION
 static void pgdat_init_kcompactd(struct pglist_data *pgdat)
 {
@@ -1401,8 +1388,6 @@ static void __meminit pgdat_init_internals(struct pglist_data *pgdat)
 
 	pgdat_resize_init(pgdat);
 	pgdat_kswapd_lock_init(pgdat);
-
-	pgdat_init_split_queue(pgdat);
 	pgdat_init_kcompactd(pgdat);
 
 	init_waitqueue_head(&pgdat->kswapd_wait);
mm/swap_state.c +10 -0
index 04f5ce992401..9c3a5cf99778 100644
@@ -465,6 +465,16 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
 		return ERR_PTR(-ENOMEM);
 	}
 
+	if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+		spin_lock(&ci->lock);
+		__swap_cache_do_del_folio(ci, folio, entry, shadow);
+		spin_unlock(&ci->lock);
+		folio_unlock(folio);
+		/* nr_pages refs from swap cache, 1 from allocation */
+		folio_put_refs(folio, nr_pages + 1);
+		return ERR_PTR(-ENOMEM);
+	}
+
 	/* memsw uncharges swap when folio is added to swap cache */
 	memcg1_swapin(folio);
 	if (shadow)
Signature · 1 line
2.54.0
#16 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

Hi Johannes,

Quoted 72 lines · On Wed, 27 May 2026 16:45:16 -0400 Johannes Weiner <[email protected]> wrote:

The deferred split queue handles cgroups in a suboptimal fashion. The queue is per-NUMA node or per-cgroup, not the intersection. That means on a cgrouped system, a node-restricted allocation entering reclaim can end up splitting large pages on other nodes:

alloc/unmap
deferred_split_folio()
list_add_tail(memcg->split_queue) set_shrinker_bit(memcg, node, deferred_shrinker_id)

for_each_zone_zonelist_nodemask(restricted_nodes)
mem_cgroup_iter()
shrink_slab(node, memcg)
shrink_slab_memcg(node, memcg)
if test_shrinker_bit(memcg, node, deferred_shrinker_id)
deferred_split_scan()
walks memcg->split_queue

The shrinker bit adds an imperfect guard rail. As soon as the cgroup has a single large page on the node of interest, all large pages owned by that memcg, including those on other nodes, will be split.

list_lru properly sets up per-node, per-cgroup lists. As a bonus, it streamlines a lot of the list operations and reclaim walks. It's used widely by other major shrinkers already. Convert the deferred split queue as well.

The list_lru per-memcg heads are instantiated on demand when the first object of interest is allocated for a cgroup, by calling folio_memcg_alloc_deferred(). Add calls to where splittable pages are
created: anon faults, swapin faults, khugepaged collapse.

These calls create all possible node heads for the cgroup at once, so the migration code (between nodes) doesn't need any special care.

---
 include/linux/huge_mm.h    |   7 +-
 include/linux/memcontrol.h |   4 -
 include/linux/mmzone.h     |  12 --
 mm/huge_memory.c           | 364 +++++++++++++------------------------
 mm/internal.h              |   2 +-
 mm/khugepaged.c            |   5 +
 mm/memcontrol.c            |  12 +-
 mm/memory.c                |   4 +
 mm/mm_init.c               |  15 --
 mm/swap_state.c            |  10 +
 10 files changed, 150 insertions(+), 285 deletions(-)
include/linux/huge_mm.h +3 -3
index edece3e26985..f6c2531a27a3 100644
@@ -423,10 +423,10 @@ static inline int split_huge_page(struct page *page)
 {
 	return split_huge_page_to_list_to_order(page, NULL, 0);
 }
+
+int folio_memcg_alloc_deferred(struct folio *folio);
+
 void deferred_split_folio(struct folio *folio, bool partially_mapped);
-#ifdef CONFIG_MEMCG
-void reparent_deferred_split_queue(struct mem_cgroup *memcg);
-#endif
 
 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
 		unsigned long address, bool freeze);

include/linux/huge_mm.h static inline int folio_split(struct folio *folio, unsigned int new_order, · line 668

Quoted 6 lines
include/linux/huge_mm.h +0 -1
@@ -664,7 +664,6 @@ static inline int folio_split(struct folio *folio, unsigned int new_order,
 }
 
 static inline void deferred_split_folio(struct folio *folio, bool partially_mapped) {}
-static inline void reparent_deferred_split_queue(struct mem_cgroup *memcg) {}
 #define split_huge_pmd(__vma, __pmd, __address)	\
 	do { } while (0)

I found this patch is now in mm-new and it makes UM mode kunit fails like below.

    $ ./tools/testing/kunit/kunit.py run --kunitconfig mm/damon/tests/
    [00:00:02] Configuring KUnit Kernel ...
    [00:00:02] Building KUnit Kernel ...
    Populating config with:
    $ make ARCH=um O=.kunit olddefconfig
    Building with:
    $ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=8
    ERROR:root:../mm/swap_state.c: In function ‘__swap_cache_alloc’:
    ../mm/swap_state.c:468:26: error: implicit declaration of function ‘folio_memcg_alloc_deferred’ [-Wimplicit-function-declaration]
      468 |         if (order > 1 && folio_memcg_alloc_deferred(folio)) {
          |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
    make[4]: *** [../scripts/Makefile.build:289: mm/swap_state.o] Error 1
    make[4]: *** Waiting for unfinished jobs....
    make[3]: *** [../scripts/Makefile.build:548: mm] Error 2
    make[3]: *** Waiting for unfinished jobs....
    make[2]: *** [/home/lkhack/linux/Makefile:2143: .] Error 2
    make[1]: *** [/home/lkhack/linux/Makefile:248: __sub-make] Error 2
    make: *** [Makefile:248: __sub-make] Error 2

Maybe we can define the function for CONFIG_TRANSPARENT_HUGEPAGE unset case? I confirmed the below attaching temporal fix works for at least kunit.

Thanks,
SJ

[...]
=== >8 ===
From 23b5800dd49085707baee5774b74782c3e424f24 Mon Sep 17 00:00:00 2001
From: SeongJae Park <[email protected]>
Date: Wed, 27 May 2026 23:58:07 -0700
Subject: [PATCH] mm/huge_mm: define memcg_alloc_deferred() for
!CONFIG_TRANSPARENT_HUGEPPAGE
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Without this, UM mode kunit fails like below.

    $ ./tools/testing/kunit/kunit.py run --kunitconfig mm/damon/tests/
    [00:00:02] Configuring KUnit Kernel ...
    [00:00:02] Building KUnit Kernel ...
    Populating config with:
    $ make ARCH=um O=.kunit olddefconfig
    Building with:
    $ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=8
    ERROR:root:../mm/swap_state.c: In function ‘__swap_cache_alloc’:
    ../mm/swap_state.c:468:26: error: implicit declaration of function ‘folio_memcg_alloc_deferred’ [-Wimplicit-function-declaration]
      468 |         if (order > 1 && folio_memcg_alloc_deferred(folio)) {
          |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
    make[4]: *** [../scripts/Makefile.build:289: mm/swap_state.o] Error 1
    make[4]: *** Waiting for unfinished jobs....
    make[3]: *** [../scripts/Makefile.build:548: mm] Error 2
    make[3]: *** Waiting for unfinished jobs....
    make[2]: *** [/home/lkhack/linux/Makefile:2143: .] Error 2
    make[1]: *** [/home/lkhack/linux/Makefile:248: __sub-make] Error 2
    make: *** [Makefile:248: __sub-make] Error 2

Fix by implementing the function for CONFIG_TRANSPARENT_HUGEPPAGE unset case.

---
 include/linux/huge_mm.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux/huge_mm.h +5 -0
index f6c2531a27a35..055de7b8ed487 100644
@@ -663,6 +663,11 @@ static inline int folio_split(struct folio *folio, unsigned int new_order,
 	return -EINVAL;
 }
 
+static inline int folio_memcg_alloc_deferred(struct folio *folio)
+{
+	return 0;
+}
+
 static inline void deferred_split_folio(struct folio *folio, bool partially_mapped) {}
 #define split_huge_pmd(__vma, __pmd, __address)	\
 	do { } while (0)
Signature · 1 line
2.47.3
author maintainer Acked-by
#17 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

Quoted 30 lines · SeongJae Park

From 23b5800dd49085707baee5774b74782c3e424f24 Mon Sep 17 00:00:00 2001
From: SeongJae Park <[email protected]>
Date: Wed, 27 May 2026 23:58:07 -0700
Subject: [PATCH] mm/huge_mm: define memcg_alloc_deferred() for
!CONFIG_TRANSPARENT_HUGEPPAGE
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Without this, UM mode kunit fails like below.

    $ ./tools/testing/kunit/kunit.py run --kunitconfig mm/damon/tests/
    [00:00:02] Configuring KUnit Kernel ...
    [00:00:02] Building KUnit Kernel ...
    Populating config with:
    $ make ARCH=um O=.kunit olddefconfig
    Building with:
    $ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=8
    ERROR:root:../mm/swap_state.c: In function ‘__swap_cache_alloc’:
    ../mm/swap_state.c:468:26: error: implicit declaration of function ‘folio_memcg_alloc_deferred’ [-Wimplicit-function-declaration]
      468 |         if (order > 1 && folio_memcg_alloc_deferred(folio)) {
          |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
    make[4]: *** [../scripts/Makefile.build:289: mm/swap_state.o] Error 1
    make[4]: *** Waiting for unfinished jobs....
    make[3]: *** [../scripts/Makefile.build:548: mm] Error 2
    make[3]: *** Waiting for unfinished jobs....
    make[2]: *** [/home/lkhack/linux/Makefile:2143: .] Error 2
    make[1]: *** [/home/lkhack/linux/Makefile:248: __sub-make] Error 2
    make: *** [Makefile:248: __sub-make] Error 2
Quoted 5 lines

Fix by implementing the function for CONFIG_TRANSPARENT_HUGEPPAGE unset case.

Whoops, thanks for the fix, SJ. I'll incorporate UM builds into my final compile test before sending.

reviewer
#18 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

In reply to Johannes Weiner
Quoted 53 lines · Johannes Weiner

The deferred split queue handles cgroups in a suboptimal fashion. The queue is per-NUMA node or per-cgroup, not the intersection. That means on a cgrouped system, a node-restricted allocation entering reclaim can end up splitting large pages on other nodes:

alloc/unmap
deferred_split_folio()
list_add_tail(memcg->split_queue) set_shrinker_bit(memcg, node, deferred_shrinker_id)

for_each_zone_zonelist_nodemask(restricted_nodes)
mem_cgroup_iter()
shrink_slab(node, memcg)
shrink_slab_memcg(node, memcg)
if test_shrinker_bit(memcg, node, deferred_shrinker_id)
deferred_split_scan()
walks memcg->split_queue

The shrinker bit adds an imperfect guard rail. As soon as the cgroup has a single large page on the node of interest, all large pages owned by that memcg, including those on other nodes, will be split.

list_lru properly sets up per-node, per-cgroup lists. As a bonus, it streamlines a lot of the list operations and reclaim walks. It's used widely by other major shrinkers already. Convert the deferred split queue as well.

The list_lru per-memcg heads are instantiated on demand when the first object of interest is allocated for a cgroup, by calling folio_memcg_alloc_deferred(). Add calls to where splittable pages are
created: anon faults, swapin faults, khugepaged collapse.

These calls create all possible node heads for the cgroup at once, so the migration code (between nodes) doesn't need any special care.

---
 include/linux/huge_mm.h    |   7 +-
 include/linux/memcontrol.h |   4 -
 include/linux/mmzone.h     |  12 --
 mm/huge_memory.c           | 364 +++++++++++++------------------------
 mm/internal.h              |   2 +-
 mm/khugepaged.c            |   5 +
 mm/memcontrol.c            |  12 +-
 mm/memory.c                |   4 +
 mm/mm_init.c               |  15 --
 mm/swap_state.c            |  10 +
 10 files changed, 150 insertions(+), 285 deletions(-)

[...]

mm/memory.c static struct folio *alloc_anon_folio(struct vm_fault *vmf) · line 5226

Quoted 10 lines
mm/memory.c +2 -0
index 135f5c0f57bd..f22e61d8c8de 100644
@@ -5222,6 +5222,10 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 			folio_put(folio);
 			goto next;
 		}
+		if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+			folio_put(folio);

Ah sorry, should have caught this in the previous version, do we need

count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);

here?

or maybe we just goto next instead of goto fallback and trty next viable order?

Quoted 5 lines
mm/memory.c +2 -0
+			goto fallback;
+		}
 		folio_throttle_swaprate(folio, gfp);
 		/*
 		 * When a folio is not zeroed during allocation
author maintainer
#19 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

Quoted 72 lines · Usama Arif
Quoted 53 lines · Johannes Weiner

The deferred split queue handles cgroups in a suboptimal fashion. The queue is per-NUMA node or per-cgroup, not the intersection. That means on a cgrouped system, a node-restricted allocation entering reclaim can end up splitting large pages on other nodes:

alloc/unmap
deferred_split_folio()
list_add_tail(memcg->split_queue) set_shrinker_bit(memcg, node, deferred_shrinker_id)

for_each_zone_zonelist_nodemask(restricted_nodes)
mem_cgroup_iter()
shrink_slab(node, memcg)
shrink_slab_memcg(node, memcg)
if test_shrinker_bit(memcg, node, deferred_shrinker_id)
deferred_split_scan()
walks memcg->split_queue

The shrinker bit adds an imperfect guard rail. As soon as the cgroup has a single large page on the node of interest, all large pages owned by that memcg, including those on other nodes, will be split.

list_lru properly sets up per-node, per-cgroup lists. As a bonus, it streamlines a lot of the list operations and reclaim walks. It's used widely by other major shrinkers already. Convert the deferred split queue as well.

The list_lru per-memcg heads are instantiated on demand when the first object of interest is allocated for a cgroup, by calling folio_memcg_alloc_deferred(). Add calls to where splittable pages are
created: anon faults, swapin faults, khugepaged collapse.

These calls create all possible node heads for the cgroup at once, so the migration code (between nodes) doesn't need any special care.

---
 include/linux/huge_mm.h    |   7 +-
 include/linux/memcontrol.h |   4 -
 include/linux/mmzone.h     |  12 --
 mm/huge_memory.c           | 364 +++++++++++++------------------------
 mm/internal.h              |   2 +-
 mm/khugepaged.c            |   5 +
 mm/memcontrol.c            |  12 +-
 mm/memory.c                |   4 +
 mm/mm_init.c               |  15 --
 mm/swap_state.c            |  10 +
 10 files changed, 150 insertions(+), 285 deletions(-)

[...]

mm/memory.c static struct folio *alloc_anon_folio(struct vm_fault *vmf) · line 5226

Quoted 10 lines
mm/memory.c +2 -0
index 135f5c0f57bd..f22e61d8c8de 100644
@@ -5222,6 +5222,10 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 			folio_put(folio);
 			goto next;
 		}
+		if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+			folio_put(folio);

Ah sorry, should have caught this in the previous version, do we need

Quoted 3 lines

count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);

here?

This isn't an allocation we expect to fail with any sort of routine that we'd need to capture it in the event counter. It would warn in dmesg if it did. But in practice it can't happen at all, since it's a sub-costly-order slab allocation and the allocator would loop and OOM kill stuff until it succeeds.

Quoted 2 lines

or maybe we just goto next instead of goto fallback and trty next viable order?

Again I don't think it matters, but fallback seems a bit more correct because the size of the list_lru allocation doesn't change with lower orders (until we hit 0).

So I think we can just leave it as is.

reviewer Acked-by
#20 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

Quoted 83 lines · Johannes Weiner
Quoted 72 lines · Usama Arif
Quoted 53 lines · Johannes Weiner

The deferred split queue handles cgroups in a suboptimal fashion. The queue is per-NUMA node or per-cgroup, not the intersection. That means on a cgrouped system, a node-restricted allocation entering reclaim can end up splitting large pages on other nodes:

alloc/unmap
deferred_split_folio()
list_add_tail(memcg->split_queue) set_shrinker_bit(memcg, node, deferred_shrinker_id)

for_each_zone_zonelist_nodemask(restricted_nodes)
mem_cgroup_iter()
shrink_slab(node, memcg)
shrink_slab_memcg(node, memcg)
if test_shrinker_bit(memcg, node, deferred_shrinker_id)
deferred_split_scan()
walks memcg->split_queue

The shrinker bit adds an imperfect guard rail. As soon as the cgroup has a single large page on the node of interest, all large pages owned by that memcg, including those on other nodes, will be split.

list_lru properly sets up per-node, per-cgroup lists. As a bonus, it streamlines a lot of the list operations and reclaim walks. It's used widely by other major shrinkers already. Convert the deferred split queue as well.

The list_lru per-memcg heads are instantiated on demand when the first object of interest is allocated for a cgroup, by calling folio_memcg_alloc_deferred(). Add calls to where splittable pages are
created: anon faults, swapin faults, khugepaged collapse.

These calls create all possible node heads for the cgroup at once, so the migration code (between nodes) doesn't need any special care.

---
 include/linux/huge_mm.h    |   7 +-
 include/linux/memcontrol.h |   4 -
 include/linux/mmzone.h     |  12 --
 mm/huge_memory.c           | 364 +++++++++++++------------------------
 mm/internal.h              |   2 +-
 mm/khugepaged.c            |   5 +
 mm/memcontrol.c            |  12 +-
 mm/memory.c                |   4 +
 mm/mm_init.c               |  15 --
 mm/swap_state.c            |  10 +
 10 files changed, 150 insertions(+), 285 deletions(-)

[...]

mm/memory.c static struct folio *alloc_anon_folio(struct vm_fault *vmf) · line 5226

Quoted 10 lines
mm/memory.c +2 -0
index 135f5c0f57bd..f22e61d8c8de 100644
@@ -5222,6 +5222,10 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 			folio_put(folio);
 			goto next;
 		}
+		if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+			folio_put(folio);

Ah sorry, should have caught this in the previous version, do we need

Quoted 3 lines

count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);

here?

This isn't an allocation we expect to fail with any sort of routine that we'd need to capture it in the event counter. It would warn in dmesg if it did. But in practice it can't happen at all, since it's a sub-costly-order slab allocation and the allocator would loop and OOM kill stuff until it succeeds.

Quoted 8 lines
Quoted 2 lines

or maybe we just goto next instead of goto fallback and trty next viable order?

Again I don't think it matters, but fallback seems a bit more correct because the size of the list_lru allocation doesn't change with lower orders (until we hit 0).

So I think we can just leave it as is.

Ack!

Reviewed-by on v5 9/9
#21 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

In reply to Johannes Weiner
Quoted 53 lines · Johannes Weiner

The deferred split queue handles cgroups in a suboptimal fashion. The queue is per-NUMA node or per-cgroup, not the intersection. That means on a cgrouped system, a node-restricted allocation entering reclaim can end up splitting large pages on other nodes:

alloc/unmap
deferred_split_folio()
list_add_tail(memcg->split_queue) set_shrinker_bit(memcg, node, deferred_shrinker_id)

for_each_zone_zonelist_nodemask(restricted_nodes)
mem_cgroup_iter()
shrink_slab(node, memcg)
shrink_slab_memcg(node, memcg)
if test_shrinker_bit(memcg, node, deferred_shrinker_id)
deferred_split_scan()
walks memcg->split_queue

The shrinker bit adds an imperfect guard rail. As soon as the cgroup has a single large page on the node of interest, all large pages owned by that memcg, including those on other nodes, will be split.

list_lru properly sets up per-node, per-cgroup lists. As a bonus, it streamlines a lot of the list operations and reclaim walks. It's used widely by other major shrinkers already. Convert the deferred split queue as well.

The list_lru per-memcg heads are instantiated on demand when the first object of interest is allocated for a cgroup, by calling folio_memcg_alloc_deferred(). Add calls to where splittable pages are
created: anon faults, swapin faults, khugepaged collapse.

These calls create all possible node heads for the cgroup at once, so the migration code (between nodes) doesn't need any special care.

---
 include/linux/huge_mm.h    |   7 +-
 include/linux/memcontrol.h |   4 -
 include/linux/mmzone.h     |  12 --
 mm/huge_memory.c           | 364 +++++++++++++------------------------
 mm/internal.h              |   2 +-
 mm/khugepaged.c            |   5 +
 mm/memcontrol.c            |  12 +-
 mm/memory.c                |   4 +
 mm/mm_init.c               |  15 --
 mm/swap_state.c            |  10 +
 10 files changed, 150 insertions(+), 285 deletions(-)

...

Quoted 8 lines
mm/swap_state.c +0 -0
index 04f5ce992401..9c3a5cf99778 100644
@@ -465,6 +465,16 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
                return ERR_PTR(-ENOMEM);
        }
 

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 477

Quoted 10 lines
mm/swap_state.c +10 -0
+       if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+               spin_lock(&ci->lock);
+               __swap_cache_do_del_folio(ci, folio, entry, shadow);
+               spin_unlock(&ci->lock);
+               folio_unlock(folio);
+               /* nr_pages refs from swap cache, 1 from allocation */
+               folio_put_refs(folio, nr_pages + 1);
+               return ERR_PTR(-ENOMEM);
+       }
+

Thanks!

Nit: I think it would be better if we move the error handling under a label to be shared with the charge failure above, but it's fine this way, can be simplified later.

#22 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

In reply to Johannes Weiner
Quoted 53 lines · Johannes Weiner

The deferred split queue handles cgroups in a suboptimal fashion. The queue is per-NUMA node or per-cgroup, not the intersection. That means on a cgrouped system, a node-restricted allocation entering reclaim can end up splitting large pages on other nodes:

alloc/unmap
deferred_split_folio()
list_add_tail(memcg->split_queue) set_shrinker_bit(memcg, node, deferred_shrinker_id)

for_each_zone_zonelist_nodemask(restricted_nodes)
mem_cgroup_iter()
shrink_slab(node, memcg)
shrink_slab_memcg(node, memcg)
if test_shrinker_bit(memcg, node, deferred_shrinker_id)
deferred_split_scan()
walks memcg->split_queue

The shrinker bit adds an imperfect guard rail. As soon as the cgroup has a single large page on the node of interest, all large pages owned by that memcg, including those on other nodes, will be split.

list_lru properly sets up per-node, per-cgroup lists. As a bonus, it streamlines a lot of the list operations and reclaim walks. It's used widely by other major shrinkers already. Convert the deferred split queue as well.

The list_lru per-memcg heads are instantiated on demand when the first object of interest is allocated for a cgroup, by calling folio_memcg_alloc_deferred(). Add calls to where splittable pages are
created: anon faults, swapin faults, khugepaged collapse.

These calls create all possible node heads for the cgroup at once, so the migration code (between nodes) doesn't need any special care.

---

include/linux/huge_mm.h    |   7 +-
include/linux/memcontrol.h |   4 -
include/linux/mmzone.h     |  12 --
mm/huge_memory.c           | 364 +++++++++++++------------------------
mm/internal.h              |   2 +-
mm/khugepaged.c            |   5 +
mm/memcontrol.c            |  12 +-
mm/memory.c                |   4 +
mm/mm_init.c               |  15 --
mm/swap_state.c            |  10 +
10 files changed, 150 insertions(+), 285 deletions(-)

[...]

mm/huge_memory.c static struct folio *vma_alloc_anon_folio_pmd(struct vm_area_struct *vma, · line 1295

Quoted 12 lines
mm/huge_memory.c +8 -0
@@ -1379,6 +1285,14 @@ static struct folio *vma_alloc_anon_folio_pmd(struct vm_area_struct *vma,
 		count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
 		return NULL;
 	}
+
+	if (folio_memcg_alloc_deferred(folio)) {
+		folio_put(folio);
+		count_vm_event(THP_FAULT_FALLBACK);
+		count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK);
+		return NULL;
+	}
+

Nit: we have three possible failure point, and some duplicate count_xxx_event/state().

Maybe we can have a followup cleanup for it.

Others, looks good. Thanks.

Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#23 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

In reply to Johannes Weiner

On Wed, May 27, 2026 at 04:45:16PM -0400, Johannes Weiner wrote:
[...]

mm/memory.c static struct folio *alloc_anon_folio(struct vm_fault *vmf) · line 5226

Quoted 10 lines
mm/memory.c +2 -0
index 135f5c0f57bd..f22e61d8c8de 100644
@@ -5222,6 +5222,10 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 			folio_put(folio);
 			goto next;
 		}
+		if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+			folio_put(folio);

Missing a MTHP_STAT_ANON_FAULT_FALLBACK bump here?

Since we jump straight to fallback and end up at order-0 :)

mm/memory.c static struct folio *alloc_anon_folio(struct vm_fault *vmf) · line 5231

Quoted 5 lines
mm/memory.c +2 -0
+			goto fallback;
+		}
 		folio_throttle_swaprate(folio, gfp);
 		/*
 		 * When a folio is not zeroed during allocation

[...]

Cheers, Lance

reviewer
#24 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

Quoted 16 lines · Lance Yang

On Wed, May 27, 2026 at 04:45:16PM -0400, Johannes Weiner wrote:
[...]

mm/memory.c static struct folio *alloc_anon_folio(struct vm_fault *vmf) · line 5226

Quoted 10 lines
mm/memory.c +2 -0
index 135f5c0f57bd..f22e61d8c8de 100644
@@ -5222,6 +5222,10 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 			folio_put(folio);
 			goto next;
 		}
+		if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+			folio_put(folio);

Missing a MTHP_STAT_ANON_FAULT_FALLBACK bump here?

Quoted 1 line

Since we jump straight to fallback and end up at order-0 :)

Sorry for the noise, I missed the earlier discussion. Never mind this one, please.

Quoted 9 lines

mm/memory.c static struct folio *alloc_anon_folio(struct vm_fault *vmf) · line 5231

Quoted 6 lines
mm/memory.c +2 -0
+			goto fallback;
+		}
 		folio_throttle_swaprate(folio, gfp);
 		/*
 		 * When a folio is not zeroed during allocation

[...]

Cheers, Lance

reviewer
#25 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

In reply to Johannes Weiner

On Wed, May 27, 2026 at 04:45:16PM -0400, Johannes Weiner wrote:
[...]

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 467

Quoted 8 lines
mm/swap_state.c +0 -0
index 04f5ce992401..9c3a5cf99778 100644
@@ -465,6 +465,16 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
 		return ERR_PTR(-ENOMEM);
 	}
 

Shouldn't this be limited to anon swapin?

e.g. vmf && vma_is_anonymous(vmf->vma)

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 468

Quoted 1 line
mm/swap_state.c +1 -0
+	if (order > 1 && folio_memcg_alloc_deferred(folio)) {

__swap_cache_alloc() is also used by shmem direct swapin, so shmem can get here too when handling a large swap entry:

shmem_get_folio_gfp()
shmem_swapin_folio()

    shmem_swap_alloc_folio()
      swapin_sync()
        swap_cache_alloc_folio()
          __swap_cache_alloc()
            folio_memcg_alloc_deferred()

@Baolin please correct me if I got it wrong :)

folio_memcg_alloc_deferred() itself doesn't filter shmem out either; it only allocates the memcg list_lru metadata for deferred_split_lru:

int folio_memcg_alloc_deferred(struct folio *folio)

{
	if (mem_cgroup_disabled())
		return 0;
	return folio_memcg_list_lru_alloc(folio, &deferred_split_lru, GFP_KERNEL);
}

Since deferred_split_lru only queues anon large folios, doing this for shmem swapin doesn't buy us anything :)

Maybe I'm missing something, just wanted to raise it.

Quoted 16 lines
mm/swap_state.c +9 -0
+		spin_lock(&ci->lock);
+		__swap_cache_do_del_folio(ci, folio, entry, shadow);
+		spin_unlock(&ci->lock);
+		folio_unlock(folio);
+		/* nr_pages refs from swap cache, 1 from allocation */
+		folio_put_refs(folio, nr_pages + 1);
+		return ERR_PTR(-ENOMEM);
+	}
+
 	/* memsw uncharges swap when folio is added to swap cache */
 	memcg1_swapin(folio);
 	if (shadow)
Signature · 1 line
2.54.0
author maintainer
#26 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

Quoted 21 lines · Lance Yang

On Wed, May 27, 2026 at 04:45:16PM -0400, Johannes Weiner wrote:
[...]

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 467

Quoted 8 lines
mm/swap_state.c +0 -0
index 04f5ce992401..9c3a5cf99778 100644
@@ -465,6 +465,16 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
 		return ERR_PTR(-ENOMEM);
 	}
 

Shouldn't this be limited to anon swapin?

e.g. vmf && vma_is_anonymous(vmf->vma)

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 468

Quoted 1 line
mm/swap_state.c +1 -0
+	if (order > 1 && folio_memcg_alloc_deferred(folio)) {

__swap_cache_alloc() is also used by shmem direct swapin, so shmem can get here too when handling a large swap entry:

Quoted 7 lines

shmem_get_folio_gfp()
shmem_swapin_folio()

    shmem_swap_alloc_folio()
      swapin_sync()
        swap_cache_alloc_folio()
          __swap_cache_alloc()
            folio_memcg_alloc_deferred()

Good catch, I think you're right. I shouldn't have dismissed that branch due to "/* Direct swapin skipping swap cache & readahead */"

Quoted 6 lines

@Baolin please correct me if I got it wrong :)

folio_memcg_alloc_deferred() itself doesn't filter shmem out either; it only allocates the memcg list_lru metadata for deferred_split_lru:

int folio_memcg_alloc_deferred(struct folio *folio)

Quoted 8 lines
{
	if (mem_cgroup_disabled())
		return 0;
	return folio_memcg_list_lru_alloc(folio, &deferred_split_lru, GFP_KERNEL);
}

Since deferred_split_lru only queues anon large folios, doing this for shmem swapin doesn't buy us anything :)

Yes, agreed. I don't think it's a big deal / show stopper in terms of user-visible effect, but of course still worth fixing.

I'll send a follow-up patch.

reviewer
#27 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

Quoted 45 lines · Johannes Weiner
Quoted 21 lines · Lance Yang

On Wed, May 27, 2026 at 04:45:16PM -0400, Johannes Weiner wrote:
[...]

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 467

Quoted 8 lines
mm/swap_state.c +0 -0
index 04f5ce992401..9c3a5cf99778 100644
@@ -465,6 +465,16 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
 		return ERR_PTR(-ENOMEM);
 	}
 

Shouldn't this be limited to anon swapin?

e.g. vmf && vma_is_anonymous(vmf->vma)

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 468

Quoted 1 line
mm/swap_state.c +1 -0
+	if (order > 1 && folio_memcg_alloc_deferred(folio)) {

__swap_cache_alloc() is also used by shmem direct swapin, so shmem can get here too when handling a large swap entry:

Quoted 7 lines

shmem_get_folio_gfp()
shmem_swapin_folio()

    shmem_swap_alloc_folio()
      swapin_sync()
        swap_cache_alloc_folio()
          __swap_cache_alloc()
            folio_memcg_alloc_deferred()

Good catch, I think you're right. I shouldn't have dismissed that branch due to "/* Direct swapin skipping swap cache & readahead */"

Quoted 12 lines

@Baolin please correct me if I got it wrong :)

folio_memcg_alloc_deferred() itself doesn't filter shmem out either; it only allocates the memcg list_lru metadata for deferred_split_lru:

int folio_memcg_alloc_deferred(struct folio *folio)

{
	if (mem_cgroup_disabled())
		return 0;
	return folio_memcg_list_lru_alloc(folio, &deferred_split_lru, GFP_KERNEL);
}
Quoted 7 lines
Quoted 2 lines

Since deferred_split_lru only queues anon large folios, doing this for shmem swapin doesn't buy us anything :)

Yes, agreed. I don't think it's a big deal / show stopper in terms of user-visible effect, but of course still worth fixing.

I'll send a follow-up patch.

Thanks.

Looks like this has already landed in mm-stable. If you're okay with it, I can send the follow-up.

From: Lance Yang <[email protected]>
Date: Tue, 9 Jun 2026 10:56:45 +0800
Subject: [PATCH] mm: prepare deferred split metadata only for anon swapin

__swap_cache_alloc() prepares deferred split metadata for large swapcache folios.

That also covers shmem swapin, because shmem_swap_alloc_folio() can call swapin_sync() with a large order[1]. But shmem folios are not queued on the deferred split queue, so preparing the metadata doesn't buy us anything there.

So let's limit it to anon swapin.

[1] https://lore.kernel.org/all/[email protected]/

---
 mm/swap_state.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/swap_state.c +2 -1
index 9c3a5cf99778..7adac957c2b8 100644
@@ -465,7 +465,8 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
 		return ERR_PTR(-ENOMEM);
 	}
 
-	if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+	if (order > 1 && vma && vma_is_anonymous(vma) &&
+	    folio_memcg_alloc_deferred(folio)) {
 		spin_lock(&ci->lock);
 		__swap_cache_do_del_folio(ci, folio, entry, shadow);
 		spin_unlock(&ci->lock);
Signature · 1 line
2.39.3 (Apple Git-146)
#28 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

Quoted 86 lines · Lance Yang
Quoted 45 lines · Johannes Weiner
Quoted 21 lines · Lance Yang

On Wed, May 27, 2026 at 04:45:16PM -0400, Johannes Weiner wrote:
[...]

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 467

Quoted 8 lines
mm/swap_state.c +0 -0
index 04f5ce992401..9c3a5cf99778 100644
@@ -465,6 +465,16 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
 		return ERR_PTR(-ENOMEM);
 	}
 

Shouldn't this be limited to anon swapin?

e.g. vmf && vma_is_anonymous(vmf->vma)

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 468

Quoted 1 line
mm/swap_state.c +1 -0
+	if (order > 1 && folio_memcg_alloc_deferred(folio)) {

__swap_cache_alloc() is also used by shmem direct swapin, so shmem can get here too when handling a large swap entry:

Quoted 7 lines

shmem_get_folio_gfp()
shmem_swapin_folio()

    shmem_swap_alloc_folio()
      swapin_sync()
        swap_cache_alloc_folio()
          __swap_cache_alloc()
            folio_memcg_alloc_deferred()

Good catch, I think you're right. I shouldn't have dismissed that branch due to "/* Direct swapin skipping swap cache & readahead */"

Quoted 12 lines

@Baolin please correct me if I got it wrong :)

folio_memcg_alloc_deferred() itself doesn't filter shmem out either; it only allocates the memcg list_lru metadata for deferred_split_lru:

int folio_memcg_alloc_deferred(struct folio *folio)

{
	if (mem_cgroup_disabled())
		return 0;
	return folio_memcg_list_lru_alloc(folio, &deferred_split_lru, GFP_KERNEL);
}
Quoted 7 lines
Quoted 2 lines

Since deferred_split_lru only queues anon large folios, doing this for shmem swapin doesn't buy us anything :)

Yes, agreed. I don't think it's a big deal / show stopper in terms of user-visible effect, but of course still worth fixing.

I'll send a follow-up patch.

Thanks.

Looks like this has already landed in mm-stable. If you're okay with it, I can send the follow-up.

From: Lance Yang <[email protected]>
Date: Tue, 9 Jun 2026 10:56:45 +0800
Subject: [PATCH] mm: prepare deferred split metadata only for anon swapin

__swap_cache_alloc() prepares deferred split metadata for large swapcache folios.

That also covers shmem swapin, because shmem_swap_alloc_folio() can call swapin_sync() with a large order[1]. But shmem folios are not queued on the deferred split queue, so preparing the metadata doesn't buy us anything there.

So let's limit it to anon swapin.

[1] https://lore.kernel.org/all/[email protected]/

---
 mm/swap_state.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/swap_state.c +0 -0
index 9c3a5cf99778..7adac957c2b8 100644

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 468

Quoted 5 lines
mm/swap_state.c +1 -1
@@ -465,7 +465,8 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
 		return ERR_PTR(-ENOMEM);
 	}
 
-	if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+	if (order > 1 && vma && vma_is_anonymous(vma) &&

A folio can be anon for a non-shmem file-backed VMA though? E.g. MAP_PRIVATE-mapped file-backed mappings?

Not sure if that's something that'd be a factor here/meaningful though.

Quoted 6 lines
mm/swap_state.c +1 -1
+	    folio_memcg_alloc_deferred(folio)) {
 		spin_lock(&ci->lock);
 		__swap_cache_do_del_folio(ci, folio, entry, shadow);
 		spin_unlock(&ci->lock);
--

2.39.3 (Apple Git-146)

Cheers, Lorenzo

reviewer
#29 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

Quoted 90 lines · Lorenzo Stoakes
Quoted 89 lines · Lance Yang
Quoted 45 lines · Johannes Weiner
Quoted 21 lines · Lance Yang

On Wed, May 27, 2026 at 04:45:16PM -0400, Johannes Weiner wrote:
[...]

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 467

Quoted 8 lines
mm/swap_state.c +0 -0
index 04f5ce992401..9c3a5cf99778 100644
@@ -465,6 +465,16 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
 		return ERR_PTR(-ENOMEM);
 	}
 

Shouldn't this be limited to anon swapin?

e.g. vmf && vma_is_anonymous(vmf->vma)

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 468

Quoted 1 line
mm/swap_state.c +1 -0
+	if (order > 1 && folio_memcg_alloc_deferred(folio)) {

__swap_cache_alloc() is also used by shmem direct swapin, so shmem can get here too when handling a large swap entry:

Quoted 7 lines

shmem_get_folio_gfp()
shmem_swapin_folio()

     shmem_swap_alloc_folio()
       swapin_sync()
         swap_cache_alloc_folio()
           __swap_cache_alloc()
             folio_memcg_alloc_deferred()

Good catch, I think you're right. I shouldn't have dismissed that branch due to "/* Direct swapin skipping swap cache & readahead */"

Quoted 12 lines

@Baolin please correct me if I got it wrong :)

folio_memcg_alloc_deferred() itself doesn't filter shmem out either; it only allocates the memcg list_lru metadata for deferred_split_lru:

int folio_memcg_alloc_deferred(struct folio *folio)

{
	if (mem_cgroup_disabled())
		return 0;
	return folio_memcg_list_lru_alloc(folio, &deferred_split_lru, GFP_KERNEL);
}
Quoted 7 lines
Quoted 2 lines

Since deferred_split_lru only queues anon large folios, doing this for shmem swapin doesn't buy us anything :)

Yes, agreed. I don't think it's a big deal / show stopper in terms of user-visible effect, but of course still worth fixing.

I'll send a follow-up patch.

Thanks.

Looks like this has already landed in mm-stable. If you're okay with it, I can send the follow-up.

From: Lance Yang <[email protected]>
Date: Tue, 9 Jun 2026 10:56:45 +0800
Subject: [PATCH] mm: prepare deferred split metadata only for anon swapin

__swap_cache_alloc() prepares deferred split metadata for large swapcache folios.

That also covers shmem swapin, because shmem_swap_alloc_folio() can call swapin_sync() with a large order[1]. But shmem folios are not queued on the deferred split queue, so preparing the metadata doesn't buy us anything there.

So let's limit it to anon swapin.

[1] https://lore.kernel.org/all/[email protected]/

---
  mm/swap_state.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
mm/swap_state.c +0 -0
index 9c3a5cf99778..7adac957c2b8 100644
@@ -465,7 +465,8 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
  		return ERR_PTR(-ENOMEM);
  	}
 
Quoted 5 lines

mm/swap_state.c static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, · line 468

Quoted 2 lines
mm/swap_state.c +1 -1
-	if (order > 1 && folio_memcg_alloc_deferred(folio)) {
+	if (order > 1 && vma && vma_is_anonymous(vma) &&

A folio can be anon for a non-shmem file-backed VMA though? E.g. MAP_PRIVATE-mapped file-backed mappings?

Ah, good point! vma_is_anonymous() is too strong here ...

Maybe use !vma_is_shmem(vma) instead?

Quoted 10 lines

Not sure if that's something that'd be a factor here/meaningful though.

Quoted 6 lines
mm/swap_state.c +1 -1
+	    folio_memcg_alloc_deferred(folio)) {
  		spin_lock(&ci->lock);
  		__swap_cache_do_del_folio(ci, folio, entry, shadow);
  		spin_unlock(&ci->lock);
--

2.39.3 (Apple Git-146)

Cheers, Lorenzo

#30 lore raw

Re: [PATCH v5 9/9] mm: switch deferred split shrinker to list_lru

In reply to Johannes Weiner

On Wed, May 27, 2026 at 04:45:16PM -0400, Johannes Weiner wrote:
[...]

Quoted 10 lines
mm/huge_memory.c +2 -1
@@ -3890,34 +3804,43 @@ static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n
 	struct folio *end_folio = folio_next(folio);
 	struct folio *new_folio, *next;
 	int old_order = folio_order(folio);
+	struct list_lru_one *lru;
+	bool dequeue_deferred;
 	int ret = 0;
-	struct deferred_split *ds_queue;
 
 	VM_WARN_ON_ONCE(!mapping && end);

mm/huge_memory.c static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n · line 3819

Quoted 10 lines
mm/huge_memory.c +8 -2
-	/* Prevent deferred_split_scan() touching ->_refcount */
-	ds_queue = folio_split_queue_lock(folio);
+	/*
+	 * If this folio can be on the deferred split queue, lock out
+	 * the shrinker before freezing the ref. If the shrinker sees
+	 * a 0-ref folio, it assumes it beat folio_put() to the list
+	 * lock and must clean up the LRU state - the same dequeue we
+	 * will do below as part of the split.
+	 */
+	dequeue_deferred = folio_test_anon(folio) && old_order > 1;

Looking at __folio_remove_rmap(), we check !folio_is_device_private() before deferred_split_folio(). __folio_freeze_and_split_unmapped() is used in folio_split_unmapped(). According to its comment, it could take device-private folio.

This means for device-private folio, we still lock lru_list and try to remove it from deferred_split_lru. The good news is this doesn't harm the system, but does some extra work.

Would it be better to add !folio_is_device_private() here?

The purpose to lock here is to prevent shrinker seeing ref-0 folio. Since device-private folio is not on deferred_split_lru, shrink won't see it.

Quoted 51 lines
mm/huge_memory.c +18 -13
+	if (dequeue_deferred) {
+		struct mem_cgroup *memcg;
+
+		rcu_read_lock();
+		memcg = folio_memcg(folio);
+		lru = list_lru_lock(&deferred_split_lru,
+				    folio_nid(folio), &memcg);
+	}
 	if (folio_ref_freeze(folio, folio_cache_ref_count(folio) + 1)) {
 		struct swap_cluster_info *ci = NULL;
 		struct lruvec *lruvec;
 
-		if (old_order > 1) {
-			if (!list_empty(&folio->_deferred_list)) {
-				ds_queue->split_queue_len--;
-				/*
-				 * Reinitialize page_deferred_list after removing the
-				 * page from the split_queue, otherwise a subsequent
-				 * split will see list corruption when checking the
-				 * page_deferred_list.
-				 */
-				list_del_init(&folio->_deferred_list);
-			}
+		if (dequeue_deferred) {
+			__list_lru_del(&deferred_split_lru, lru,
+				       &folio->_deferred_list, folio_nid(folio));
 			if (folio_test_partially_mapped(folio)) {
 				folio_clear_partially_mapped(folio);
 				mod_mthp_stat(old_order,
 					MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1);
 			}
+			list_lru_unlock(lru);
+			rcu_read_unlock();
 		}
-		split_queue_unlock(ds_queue);
+
 		if (mapping) {
 			int nr = folio_nr_pages(folio);
 
@@ -4017,7 +3940,10 @@ static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n
 		if (ci)
 			swap_cluster_unlock(ci);
 	} else {
-		split_queue_unlock(ds_queue);
+		if (dequeue_deferred) {
+			list_lru_unlock(lru);
+			rcu_read_unlock();
+		}
 		return -EAGAIN;
 	}
 
Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#31 lore raw
In reply to Johannes Weiner

Hi Johannes,

Quoted 84 lines · Johannes Weiner

This is version 5 of switching the THP shrinker to list_lru.

Core of the new version is the list_lru/set_shrinker_bit fix up front, which minimally affects later patches; and a rebase onto the latest mm-unstable - replaced alloc_swap_folio() with __swap_cache_alloc().

The changes seemed small enough that *I chose to keep the review tags from v4*. Please shout if you object to this!

Changes in v5:
- patch 1 is a new fix for a very old, pre-existing set_shrinker_bit() problem in list_lru, where the bit can be set on a dying child memcg instead of the ancestor that actually received the item. Pointed out by Usama Arif and Sashiko; fix it first to make it minimally backportable and so the conversion is safe.
- patches 6 and 9 adapt to that fix's new memcg-by-reference lock_list_lru_of_memcg() signature
- collapse_huge_page(): propagate folio_memcg_alloc_deferred() failure as SCAN_ALLOC_HUGE_PAGE_FAIL instead of leaking SCAN_SUCCEED and falsely reporting a successful MADV_COLLAPSE (Usama Arif, Sashiko)
- deferred_split_isolate(): fix a UAF by reading folio state before list_lru_isolate(); once removed, a racing folio_put() frees the folio via the lockless list_empty() check while we still touch its flags and stats (Sashiko)
- rebased to mm-unstable of 2026-05-27, which simplifies the flatten prep patch (now anon-only, as alloc_swap_folio() was folded into the new __swap_cache_alloc()) and moves the swap-side folio_memcg_alloc_deferred() hook into __swap_cache_alloc(). Kairui, I would appreciate an eyeball on that.

Changes in v4:
- guard folio_memcg_alloc_deferred() with mem_cgroup_disabled() to fix NULL deref in __memcg_list_lru_alloc() when booting with cgroup_disable=memory (e.g., kdump capture kernel) -- reported and tested by Mikhail Zaslonko on s390 and x86
- flatten if (folio) branches in alloc_swap_folio() and alloc_anon_folio() in a prep patch so the list_lru allocation additions are a clean minimal diff (Lorenzo)
- folio_memcg_alloc_deferred() moved out of alloc_charge_folio() into the anon-only collapse_huge_page() path; collapse_file() shares that helper but its pages don't go on the THP shrinker queue (David)
- guard folio_memcg_alloc_deferred() with order > 1; mTHPs below order-2 can't be queued on the deferred split list (David)
- make deferred_split_lru static, hide behind folio_memcg_alloc_deferred() wrapper with GFP_KERNEL (Lorenzo)
- rename l -> lru throughout huge_memory.c (Lorenzo)
- kdoc for folio_memcg_list_lru_alloc() (Lorenzo)
- list_lru_lock_irq()/unlock_irq()/add_irq() irq-disabling variants; use list_lru_add_irq() in deferred_split_scan() (Lorenzo)
- reorder shrinker_free() before list_lru_destroy() (Lorenzo)

Changes in v3:
- dedicated lockdep_key for irqsafe deferred_split_lru.lock (syzbot)
- conditional list_lru ops in __folio_freeze_and_split_unmapped() (syzbot)
- annotate runs of inscrutable false, NULL, false function arguments (David)
- rename to folio_memcg_list_lru_alloc() (David)

Changes in v2:
- explicit rcu_read_lock() in __folio_freeze_and_split_unmapped() (Usama)
- split out list_lru prep bits (Dave)

The open-coded deferred split queue has issues. It's not NUMA-aware (when cgroup is enabled), and it's more complicated in the callsites interacting with it. Switching to list_lru fixes the NUMA problem and streamlines things. It also simplifies planned shrinker work.

Patch 1 fixes a pre-existing list_lru bug where the shrinker bit is set on the caller's memcg rather than the ancestor whose sublist the item actually lands on after a walk-up. Standalone, backportable; the rest of the series depends on it.

Patches 2-5 are cleanups and small refactors in list_lru code. They're basically independent, but make the THP shrinker conversion easier.

Patch 6 extends the list_lru API to allow the caller to control the locking scope. The THP shrinker has private state it needs to keep synchronized with the LRU state.

Patch 7 extends the list_lru API with a convenience helper to do list_lru head allocation (memcg_list_lru_alloc) when coming from a folio. Anon THPs are instantiated in several places, and with the folio reparenting patches pending, folio_memcg() access is now a more delicate dance. This avoids having to replicate that dance everywhere.

Quoted 5 lines

Patch 8 flattens the alloc_anon_folio() retry loop so the next patch's list_lru hook lands as a clean addition rather than nested deep inside an if (folio) block.

Patch 9 finally switches the deferred_split_queue to list_lru.

As the changelog above says, the old queue is per-memcg only, rather than per-memcg-per-node. So reclaim on one node can still walk the whole memcg queue and split underused THPs from other nodes in the same memcg.

But I think the new one can lose reclaim in the cgroup.memory=nokmem case ...

With nokmem, the deferred shrinker can still run from memcg reclaim, because it is SHRINKER_NONSLAB. But the list_lru is no longer per-memcg:

__list_lru_init() clears memcg_aware,

	if (mem_cgroup_kmem_disabled())
		memcg_aware = false;

so list_lru_from_memcg_idx() falls back to the shared node list:

static inline struct list_lru_one * list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)

{
	if (list_lru_memcg_aware(lru) && idx >= 0) {
[...]
	}
	return &lru->node[nid].lru;
}

That makes the shrinker bit unreliable. __list_lru_add() still sets the bit on the memcg passed in, but only when the list goes from empty to non-empty:

bool __list_lru_add(struct list_lru *lru, struct list_lru_one *l,
struct list_head *item, int nid, struct mem_cgroup *memcg)

{
	if (list_empty(item)) {
[...]
		if (!l->nr_items++)
			set_shrinker_bit(memcg, nid, lru_shrinker_id(lru));
[...]
		return true;
	}
	return false;
}

If memcg A adds the first folio, A gets the bit. If memcg B later adds a folio to the same shared list, B does not get a bit, because the list was already non-empty.

So in the A-first/B-later case, reclaim from B may not call the deferred shrinker at all. The shared list is scanned from memcg reclaim only if reclaim runs from the memcg that has the bit, such as A here, or from global reclaim :)

Anyway, only after the shared list is emptied does the next memcg to add a folio get to be the one with the bit, IIUC :)

Hopefully I didn't miss somthing important ...

Cheers, Lance

author maintainer
#32 lore raw
Quoted 48 lines · Lance Yang

As the changelog above says, the old queue is per-memcg only, rather than per-memcg-per-node. So reclaim on one node can still walk the whole memcg queue and split underused THPs from other nodes in the same memcg.

But I think the new one can lose reclaim in the cgroup.memory=nokmem case ...

With nokmem, the deferred shrinker can still run from memcg reclaim, because it is SHRINKER_NONSLAB. But the list_lru is no longer per-memcg:

__list_lru_init() clears memcg_aware,

	if (mem_cgroup_kmem_disabled())
		memcg_aware = false;

so list_lru_from_memcg_idx() falls back to the shared node list:

static inline struct list_lru_one * list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)

{
	if (list_lru_memcg_aware(lru) && idx >= 0) {
[...]
	}
	return &lru->node[nid].lru;
}

That makes the shrinker bit unreliable. __list_lru_add() still sets the bit on the memcg passed in, but only when the list goes from empty to non-empty:

bool __list_lru_add(struct list_lru *lru, struct list_lru_one *l,
struct list_head *item, int nid, struct mem_cgroup *memcg)

{
	if (list_empty(item)) {
[...]
		if (!l->nr_items++)
			set_shrinker_bit(memcg, nid, lru_shrinker_id(lru));
[...]
		return true;
	}
	return false;
}

If memcg A adds the first folio, A gets the bit. If memcg B later adds a folio to the same shared list, B does not get a bit, because the list was already non-empty.

Quoted 7 lines

So in the A-first/B-later case, reclaim from B may not call the deferred shrinker at all. The shared list is scanned from memcg reclaim only if reclaim runs from the memcg that has the bit, such as A here, or from global reclaim :)

Anyway, only after the shared list is emptied does the next memcg to add a folio get to be the one with the bit, IIUC :)

Sorry for the delay, this took me a bit to think about. The shrinker code is a mess.

I read it the same way you do. And this is true for all list_lru users when nokmem is set: we just set random nonsense shrinker bits.

HOWEVER, the generic shrinker code fixes that up by IGNORING random shrinker bits like this when !memcg_kmem_online(). And shrinking correctly happens only against the shared root queue when the reclaim iterator walks root_mem_cgroup.

HOWEVER, the THP shrinker explicitly sets SHRINKER_NONSLAB, which in turn overrides the previous override. So yes there is a weirdness: we get the root cgroup invocation against the shared queue, and then one more time triggered by that random memcg bit.

The most direct fix is to just drop SHRINKER_NONSLAB. It declares independence from kmem, which is no longer true.

Cleaning up the shrinker code is left for another day.

Andrew, if there are no objections, can you please fold this?

---

From 6787efabb9584824c196bf01c517d93aae3764c3 Mon Sep 17 00:00:00 2001
From: Johannes Weiner <[email protected]>
Date: Tue, 2 Jun 2026 17:11:46 -0400
Subject: [PATCH] mm: switch deferred split shrinker to list_lru fix

Lance Yang points out a weirdness in the list_lru code with cgroup.memory=nokmem: in this mode, list_lru collapses to a shared per-node list that holds the folios, but __list_lru_add() still sets the shrinker bit on the owning memcg.

Usually this is fine, because the generic shrinker code ignores these random bits when !memcg_kmem_online(). But the THP shrinker still has the SHRINKER_NONSLAB flag set, which specifically declares an independence from kmem. As a result, the shrinker fires twice per reclaim cycle: one during the regular root cgroup scan, and then one more time triggered from whichever memcg got the shrinker bit.

Drop the flag, since it's no longer true. The deferred_split shrinker then behaves like every other list_lru-backed shrinker under nokmem, including the non-kmem ones (zswap, workingset shadow_nodes): skipped from memcg-internal reclaim, driven by global reclaim only.

This needs proper cleaning up on the shrinker and list_lru side, but that's scope for a follow-up series. Just make it consistent now.

---
 mm/huge_memory.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/huge_memory.c +1 -2
index 72f6caf0fec6..aef495891f8c 100644
@@ -956,8 +956,7 @@ int folio_memcg_alloc_deferred(struct folio *folio)
 static int __init thp_shrinker_init(void)
 {
 	deferred_split_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE |
-						 SHRINKER_MEMCG_AWARE |
-						 SHRINKER_NONSLAB,
+						 SHRINKER_MEMCG_AWARE,
 						 "thp-deferred_split");
 	if (!deferred_split_shrinker)
 		return -ENOMEM;
Signature · 1 line
2.54.0
reviewer
#33 lore raw
Quoted 73 lines · Johannes Weiner
Quoted 48 lines · Lance Yang

As the changelog above says, the old queue is per-memcg only, rather than per-memcg-per-node. So reclaim on one node can still walk the whole memcg queue and split underused THPs from other nodes in the same memcg.

But I think the new one can lose reclaim in the cgroup.memory=nokmem case ...

With nokmem, the deferred shrinker can still run from memcg reclaim, because it is SHRINKER_NONSLAB. But the list_lru is no longer per-memcg:

__list_lru_init() clears memcg_aware,

	if (mem_cgroup_kmem_disabled())
		memcg_aware = false;

so list_lru_from_memcg_idx() falls back to the shared node list:

static inline struct list_lru_one * list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)

{
	if (list_lru_memcg_aware(lru) && idx >= 0) {
[...]
	}
	return &lru->node[nid].lru;
}

That makes the shrinker bit unreliable. __list_lru_add() still sets the bit on the memcg passed in, but only when the list goes from empty to non-empty:

bool __list_lru_add(struct list_lru *lru, struct list_lru_one *l,
struct list_head *item, int nid, struct mem_cgroup *memcg)

{
	if (list_empty(item)) {
[...]
		if (!l->nr_items++)
			set_shrinker_bit(memcg, nid, lru_shrinker_id(lru));
[...]
		return true;
	}
	return false;
}

If memcg A adds the first folio, A gets the bit. If memcg B later adds a folio to the same shared list, B does not get a bit, because the list was already non-empty.

Quoted 7 lines

So in the A-first/B-later case, reclaim from B may not call the deferred shrinker at all. The shared list is scanned from memcg reclaim only if reclaim runs from the memcg that has the bit, such as A here, or from global reclaim :)

Anyway, only after the shared list is emptied does the next memcg to add a folio get to be the one with the bit, IIUC :)

Sorry for the delay, this took me a bit to think about. The shrinker code is a mess.

I read it the same way you do. And this is true for all list_lru users when nokmem is set: we just set random nonsense shrinker bits.

HOWEVER, the generic shrinker code fixes that up by IGNORING random shrinker bits like this when !memcg_kmem_online(). And shrinking correctly happens only against the shared root queue when the reclaim iterator walks root_mem_cgroup.

HOWEVER, the THP shrinker explicitly sets SHRINKER_NONSLAB, which in turn overrides the previous override. So yes there is a weirdness: we get the root cgroup invocation against the shared queue, and then one more time triggered by that random memcg bit.

Quoted 4 lines

The most direct fix is to just drop SHRINKER_NONSLAB. It declares independence from kmem, which is no longer true.

Cleaning up the shrinker code is left for another day.

Thanks for working on this!

Wondering if this fix trades one problem for another, though ...

Before this series, the deferred split shrinker had a real per-memcg queue. Even with cgroup.memory=nokmem, memcg reclaim could still scan that memcg's own deferred_split_queue:

memcg reclaim -> deferred split shrinker -> sc->memcg->deferred_split_queue

With the fix, nokmem + w/o SHRINKER_NONSLAB falls back to a non-memcg-aware shrinker:

memcg reclaim -> skip deferred split shrinker

root/global reclaim -> deferred split shrinker -> shared list_lru

Is that expected? There woud be no memcg-driven deferred split reclaim under nokmem, IIUC ...

Not sure what the right fix is, as I am not a memcg expert ...

Cheers, Lance

Quoted 53 lines

Andrew, if there are no objections, can you please fold this?

---

Quoted 1 line

From 6787efabb9584824c196bf01c517d93aae3764c3 Mon Sep 17 00:00:00 2001

From: Johannes Weiner <[email protected]>
Date: Tue, 2 Jun 2026 17:11:46 -0400
Subject: [PATCH] mm: switch deferred split shrinker to list_lru fix

Lance Yang points out a weirdness in the list_lru code with cgroup.memory=nokmem: in this mode, list_lru collapses to a shared per-node list that holds the folios, but __list_lru_add() still sets the shrinker bit on the owning memcg.

Usually this is fine, because the generic shrinker code ignores these random bits when !memcg_kmem_online(). But the THP shrinker still has the SHRINKER_NONSLAB flag set, which specifically declares an independence from kmem. As a result, the shrinker fires twice per reclaim cycle: one during the regular root cgroup scan, and then one more time triggered from whichever memcg got the shrinker bit.

Drop the flag, since it's no longer true. The deferred_split shrinker then behaves like every other list_lru-backed shrinker under nokmem, including the non-kmem ones (zswap, workingset shadow_nodes): skipped from memcg-internal reclaim, driven by global reclaim only.

This needs proper cleaning up on the shrinker and list_lru side, but that's scope for a follow-up series. Just make it consistent now.

---
mm/huge_memory.c | 3 +--

1 file changed, 1 insertion(+), 2 deletions(-)
mm/huge_memory.c +1 -2
index 72f6caf0fec6..aef495891f8c 100644
@@ -956,8 +956,7 @@ int folio_memcg_alloc_deferred(struct folio *folio)
 static int __init thp_shrinker_init(void)
 {
 	deferred_split_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE |
-						 SHRINKER_MEMCG_AWARE |
-						 SHRINKER_NONSLAB,
+						 SHRINKER_MEMCG_AWARE,
 						 "thp-deferred_split");
 	if (!deferred_split_shrinker)
 		return -ENOMEM;
Signature · 1 line
2.54.0
author maintainer
#34 lore raw
Quoted 93 lines · Lance Yang
Quoted 73 lines · Johannes Weiner
Quoted 48 lines · Lance Yang

As the changelog above says, the old queue is per-memcg only, rather than per-memcg-per-node. So reclaim on one node can still walk the whole memcg queue and split underused THPs from other nodes in the same memcg.

But I think the new one can lose reclaim in the cgroup.memory=nokmem case ...

With nokmem, the deferred shrinker can still run from memcg reclaim, because it is SHRINKER_NONSLAB. But the list_lru is no longer per-memcg:

__list_lru_init() clears memcg_aware,

	if (mem_cgroup_kmem_disabled())
		memcg_aware = false;

so list_lru_from_memcg_idx() falls back to the shared node list:

static inline struct list_lru_one * list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)

{
	if (list_lru_memcg_aware(lru) && idx >= 0) {
[...]
	}
	return &lru->node[nid].lru;
}

That makes the shrinker bit unreliable. __list_lru_add() still sets the bit on the memcg passed in, but only when the list goes from empty to non-empty:

bool __list_lru_add(struct list_lru *lru, struct list_lru_one *l,
struct list_head *item, int nid, struct mem_cgroup *memcg)

{
	if (list_empty(item)) {
[...]
		if (!l->nr_items++)
			set_shrinker_bit(memcg, nid, lru_shrinker_id(lru));
[...]
		return true;
	}
	return false;
}

If memcg A adds the first folio, A gets the bit. If memcg B later adds a folio to the same shared list, B does not get a bit, because the list was already non-empty.

Quoted 7 lines

So in the A-first/B-later case, reclaim from B may not call the deferred shrinker at all. The shared list is scanned from memcg reclaim only if reclaim runs from the memcg that has the bit, such as A here, or from global reclaim :)

Anyway, only after the shared list is emptied does the next memcg to add a folio get to be the one with the bit, IIUC :)

Sorry for the delay, this took me a bit to think about. The shrinker code is a mess.

I read it the same way you do. And this is true for all list_lru users when nokmem is set: we just set random nonsense shrinker bits.

HOWEVER, the generic shrinker code fixes that up by IGNORING random shrinker bits like this when !memcg_kmem_online(). And shrinking correctly happens only against the shared root queue when the reclaim iterator walks root_mem_cgroup.

HOWEVER, the THP shrinker explicitly sets SHRINKER_NONSLAB, which in turn overrides the previous override. So yes there is a weirdness: we get the root cgroup invocation against the shared queue, and then one more time triggered by that random memcg bit.

Quoted 4 lines

The most direct fix is to just drop SHRINKER_NONSLAB. It declares independence from kmem, which is no longer true.

Cleaning up the shrinker code is left for another day.

Thanks for working on this!

Wondering if this fix trades one problem for another, though ...

Before this series, the deferred split shrinker had a real per-memcg queue. Even with cgroup.memory=nokmem, memcg reclaim could still scan that memcg's own deferred_split_queue:

memcg reclaim -> deferred split shrinker -> sc->memcg->deferred_split_queue

With the fix, nokmem + w/o SHRINKER_NONSLAB falls back to a non-memcg-aware shrinker:

Quoted 6 lines

memcg reclaim -> skip deferred split shrinker

root/global reclaim -> deferred split shrinker -> shared list_lru

Is that expected? There woud be no memcg-driven deferred split reclaim under nokmem, IIUC ...

Yes, this is all correct. list_lru is still inherently tied to the kmem component of memcg (memcg_kmem_id()).

So without kmem, no isolation. But without kmem, no isolation *for a lot of stuff*. It's a legacy knob when slab accounting was new and expensive. But so many things depend on it now, disabling it just punches a nassive hole into memcg functionality and isolation coverage. It's not a sanctioned production use flag.

This change is negligible from a memcg semantics POV.

Quoted 1 line

Not sure what the right fix is, as I am not a memcg expert ...

reviewer
#35 lore raw
Quoted 104 lines · Johannes Weiner
Quoted 93 lines · Lance Yang
Quoted 73 lines · Johannes Weiner
Quoted 48 lines · Lance Yang

As the changelog above says, the old queue is per-memcg only, rather than per-memcg-per-node. So reclaim on one node can still walk the whole memcg queue and split underused THPs from other nodes in the same memcg.

But I think the new one can lose reclaim in the cgroup.memory=nokmem case ...

With nokmem, the deferred shrinker can still run from memcg reclaim, because it is SHRINKER_NONSLAB. But the list_lru is no longer per-memcg:

__list_lru_init() clears memcg_aware,

	if (mem_cgroup_kmem_disabled())
		memcg_aware = false;

so list_lru_from_memcg_idx() falls back to the shared node list:

static inline struct list_lru_one * list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)

{
	if (list_lru_memcg_aware(lru) && idx >= 0) {
[...]
	}
	return &lru->node[nid].lru;
}

That makes the shrinker bit unreliable. __list_lru_add() still sets the bit on the memcg passed in, but only when the list goes from empty to non-empty:

bool __list_lru_add(struct list_lru *lru, struct list_lru_one *l,
struct list_head *item, int nid, struct mem_cgroup *memcg)

{
	if (list_empty(item)) {
[...]
		if (!l->nr_items++)
			set_shrinker_bit(memcg, nid, lru_shrinker_id(lru));
[...]
		return true;
	}
	return false;
}

If memcg A adds the first folio, A gets the bit. If memcg B later adds a folio to the same shared list, B does not get a bit, because the list was already non-empty.

Quoted 7 lines

So in the A-first/B-later case, reclaim from B may not call the deferred shrinker at all. The shared list is scanned from memcg reclaim only if reclaim runs from the memcg that has the bit, such as A here, or from global reclaim :)

Anyway, only after the shared list is emptied does the next memcg to add a folio get to be the one with the bit, IIUC :)

Sorry for the delay, this took me a bit to think about. The shrinker code is a mess.

I read it the same way you do. And this is true for all list_lru users when nokmem is set: we just set random nonsense shrinker bits.

HOWEVER, the generic shrinker code fixes that up by IGNORING random shrinker bits like this when !memcg_kmem_online(). And shrinking correctly happens only against the shared root queue when the reclaim iterator walks root_mem_cgroup.

HOWEVER, the THP shrinker explicitly sets SHRINKER_NONSLAB, which in turn overrides the previous override. So yes there is a weirdness: we get the root cgroup invocation against the shared queue, and then one more time triggered by that random memcg bit.

Quoted 4 lines

The most direct fix is to just drop SHRINKER_NONSLAB. It declares independence from kmem, which is no longer true.

Cleaning up the shrinker code is left for another day.

Thanks for working on this!

Wondering if this fix trades one problem for another, though ...

Before this series, the deferred split shrinker had a real per-memcg queue. Even with cgroup.memory=nokmem, memcg reclaim could still scan that memcg's own deferred_split_queue:

memcg reclaim -> deferred split shrinker -> sc->memcg->deferred_split_queue

With the fix, nokmem + w/o SHRINKER_NONSLAB falls back to a non-memcg-aware shrinker:

Quoted 6 lines

memcg reclaim -> skip deferred split shrinker

root/global reclaim -> deferred split shrinker -> shared list_lru

Is that expected? There woud be no memcg-driven deferred split reclaim under nokmem, IIUC ...

Yes, this is all correct. list_lru is still inherently tied to the kmem component of memcg (memcg_kmem_id()).

Quoted 7 lines

So without kmem, no isolation. But without kmem, no isolation *for a lot of stuff*. It's a legacy knob when slab accounting was new and expensive. But so many things depend on it now, disabling it just punches a nassive hole into memcg functionality and isolation coverage. It's not a sanctioned production use flag.

This change is negligible from a memcg semantics POV.

Thanks for clarifying!

No strong objection from me. Just wanted to call out the nokmem behavior change and hear what folks think :D

Quoted 1 line
Quoted 1 line

Not sure what the right fix is, as I am not a memcg expert ...