Skip to content

mm/fbatch: drain lru_add_drain() and _all()

Hugh Dickins linux-block linux-fsdevel linux-mm lkml 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest26 patches 26 repliesIn review
  2. v125 patches 29 repliesSuperseded

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/26mm/fbatch: remove !CONFIG_SMP special case of folio_activate()mm/folio.c+6 -341 reply
2/26mm/fbatch: allow folios_put_refs() to skip xa_is_value() entriesinclude/linux/folio_batch.h mm/folio.c mm/shmem.c +1 more+15 -331 reply
3/26mm/fbatch: temporarily disable lazyfree and mlock+munlock batchingmm/folio.c mm/mlock.c+5 -00 replies
4/26mm/fbatch: lru bit set, no extra ref, while folio on per-cpu fbatchinclude/linux/mm_inline.h include/linux/mm_types.h mm/folio.c +1 more+74 -822 replies
5/26mm/fbatch: lru_add_del_folio()+folio_add_lru() after clear_lru()mm/compaction.c mm/vmscan.c+20 -92 replies
6/26mm/fbatch: fbatch_drain_lazyfree(onstack fbatch) before ptl unlockinclude/linux/huge_mm.h mm/folio.c mm/huge_memory.c +2 more+38 -202 replies
7/26mm/fbatch: LRU_NEXT_ACTIVATE bit to optimize folio_activate()include/linux/mm_inline.h mm/folio.c+24 -37 replies
8/26mm/fbatch: replace mlock_new_folio() by __folio_add_lru(,mlockit)Documentation/mm/unevictable-lru.rst include/linux/mm_types.h include/linux/swap.h +4 more+53 -971 reply
9/26mm/fbatch: restore mlock+munlock batching, without extra refmm/mlock.c+86 -613 replies
10/26mm/fbatch: remove several uses of mlock_drain_local()mm/huge_memory.c mm/migrate.c mm/rmap.c+0 -80 replies
11/26mm/fbatch: remove migration's PAGE_WAS_MLOCKED lru_add_drain()mm/compaction.c mm/migrate.c+2 -80 replies
12/26mm/fbatch: remove percpu_pvec_drained and folios_put()include/linux/folio_batch.h include/linux/mm.h mm/folio.c +1 more+4 -350 replies
13/26mm/fbatch: no lru_add_drain to collect_longterm_unpinnable_folios()include/linux/swap.h mm/folio.c mm/gup.c+5 -610 replies
14/26mm/fbatch: no lru_add_drain() nor _all() for memfd_wait_for_pins()mm/memfd.c+1 -50 replies
15/26mm/fbatch: remove shake_folio() shake_page() from memory-failuremm/hwpoison-inject.c mm/internal.h mm/memory-failure.c+1 -390 replies
16/26mm/fbatch: remove lru_cache_disable() from NUMA folio migrationmm/mempolicy.c mm/migrate.c+0 -100 replies
17/26mm/fbatch: no lru_cache_disable() in __alloc_contig_migrate_range()mm/page_alloc.c+0 -32 replies
18/26mm/fbatch: remove lru_add_drain() and _all() calls from variousmm/compaction.c mm/fadvise.c mm/khugepaged.c +5 more+4 -730 replies
19/26mm/fbatch: vm/stat_refresh include lru_add_drain() on each cpumm/vmstat.c+1 -00 replies
20/26s390/fbatch: no lru_add_drain_all() in s390_wiggle_split_folio()arch/s390/kernel/uv.c+0 -10 replies
21/26block/fbatch: no lru_add_drain_all() in invalidate_bdev()block/bdev.c+0 -10 replies
22/26fs/fbatch: drop_caches invalidate_bh_lrus() not lru_add_drain_all()fs/drop_caches.c+2 -20 replies
23/26fs,mm/fbatch: use invalidate_bh_lrus() not invalidate_bh_lrus_cpu()fs/buffer.c include/linux/buffer_head.h mm/folio.c+7 -380 replies
24/26fs,mm/fbatch: lru_cache_disable() keep off buffer_head lrus onlyfs/buffer.c include/linux/buffer_head.h include/linux/swap.h +4 more+36 -620 replies
25/26mm/fbatch: move lru_add_drain_all() declaration to mm/internal.hinclude/linux/swap.h mm/internal.h+1 -10 replies
26/26mm/fbatch: drop reference inside the loop when drainingmm/folio.c mm/mlock.c+38 -120 replies

Reviews trailers per patch

7 of 26 patches have a Reviewed-by or Acked-by.

PatchDavid Hildenbrand (Arm)Vlastimil Babka (SUSE)Miaohe LinAlen Karnil
SeriesT-b
1/26A-b R-b R-b
2/26A-b R-b R-b
3/26R-b
4/26
5/26R-b
6/26R-b
7/26
8/26R-b
9/26
10/26
11/26
12/26
13/26
14/26
15/26A-b
16/26
17/26
18/26
19/26
20/26
21/26
22/26
23/26
24/26
25/26
26/26

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 4 reviewers.

Read the v2 thread

Cover letter [PATCH v2 00/26] mm/fbatch: drain lru_add_drain() and _all()

[PATCH v2 00/26] mm/fbatch: drain lru_add_drain() and _all()

This series was prompted by lru_add_drain_all() appearing in watchdog
backtraces: not to blame, but blocked on an unresponsive CPU to run its workqueue. lru_add_drain_all() is a heavyweight operation, which we often hope to avoid by a much lighter lru_add_drain(); but even those local drains can aggravate the lruvec lock contention which per-cpu fbatches are intended to ease.

Now let the folios on the lru_add and other per-cpu fbatches remain there, isolatable, with PG_lru set and refcount unraised, just like when on an actual LRU. Then many calls to lru_add_drain() and _all() can be removed.

It's something I've wanted to do for years, tried several times, but only hit on a good way to do it a few weeks ago: now it's uncomfortable to watch others wrestling with the drainage, while I'm sitting on this.

The key idea came from the "if (!folio_test_clear_lru(folio)) continue;". If we don't mind missing to take an action on rare occasions, then maybe we won't mind taking an action on the wrong folio on rare occasions, so long as it is a folio consenting to PG_lru rules.

No new locking, but relies on folio_try_get() and folio_test_clear_lru() even on the lru_add fbatch; with use of bits not set in aligned pointers, and some try_cmpxchg()ing. Speculative references to folios are already
accepted: this adds another source of them.

Performance? You (and the bots) tell me. I'm considering this as a cleanup, to make life easier for developers. I expect that some loads will show improvement, but also expect some disappointments (perhaps I go too far against lru_cache_disable()? or not far enough).

The timing is not good: I'm taking three weeks off today; but it's best to get this out in the open early. If it's welcome in principle for 7.4, but changes needed, maybe someone else can step up to shepherd it through; otherwise, I can take it up again for 7.5.

Rebased and retested on Linus's v7.3-rc2 tree, after adding two mm.git dependencies which did not get into rc2, but can be expected in rc3:
Shakeel's "mm/mlock: use the IRQ-safe accessor for NR_MLOCK in __munlock_folio()" and Ackerley's "mm/folio: EXPORT_SYMBOL_FOR_KVM( lru_cache_drain_for_folio)". Those two are now in mm-hotfixes-stable (e14a34548064 and 7891fbb9512f respectively: thanks to Andrew) as of mm-everything-2026-09-09-06-32, and the series applied and briefly tested over 09-09's earlier mm-everything, so I'm giving a hopeful but not entirely honest base-commit below.

v1 posted on 24 August had 25 patches, with 26/25 added later:

01/26 mm/fbatch: remove !CONFIG_SMP special case of folio_activate()
v2: added Ack from David, R-b Vlastimil
02/26 mm/fbatch: allow folios_put_refs() to skip xa_is_value() entries
v2: added Ack from David, R-b Vlastimil
03/26 mm/fbatch: temporarily disable lazyfree and mlock+munlock batching
v2: added R-b Vlastimil
04/26 mm/fbatch: lru bit set, no extra ref, while folio on per-cpu fbatch
v2: added comment on Bit 1 of page->lru.next per Kiryl and Matthew
READ/WRITE_ONCE smp_mb__before_atomic per 07/25 discussion with Kiryl fixed (commented out) BUG_ON in lru_add_del_folio()
05/26 mm/fbatch: lru_add_del_folio()+folio_add_lru() after clear_lru() 06/26 mm/fbatch: fbatch_drain_lazyfree(onstack fbatch) before ptl unlock 07/26 mm/fbatch: LRU_NEXT_ACTIVATE bit to optimize folio_activate()
v2: hunk in lru_add_del_folio() updated according to mods in 04/26
08/26 mm/fbatch: replace mlock_new_folio() by __folio_add_lru(,mlockit)
v2: updated 04/26 Bit 1 comment on folio_add_lru() to __folio_add_lru()
09/26 mm/fbatch: restore mlock+munlock batching, without extra ref
v2: rediffed around Shakeel's NR_MLOCK fix, fixed unknowingly in v1
10/26 mm/fbatch: remove several uses of mlock_drain_local() 11/26 mm/fbatch: remove migration's PAGE_WAS_MLOCKED lru_add_drain()
v2: conversely, use !folio_evictable() in isolate_migratepages_block()
12/26 mm/fbatch: remove percpu_pvec_drained and folios_put() 13/26 mm/fbatch: no lru_add drain to collect_longterm_unpinnable_folios()
v2: revert David's rc1 lru_cache_drain_for_folio() from mm/folio.c,
and Ackerley's EXPORT for KVM; but keep an inline stub in swap.h
14/26 mm/fbatch: no lru_add_drain() nor _all() for memfd_wait_for_pins() 15/26 mm/fbatch: remove shake_folio() shake_page() from memory-failure
v2: added Ack from Miaohe
16/26 mm/fbatch: remove lru_cache_disable() from NUMA folio migration 17/26 mm/fbatch: no lru_cache_disable() in __alloc_contig_migrate_range() 18/26 mm/fbatch: remove lru_add_drain() and _all() calls from various 19/26 mm/fbatch: vm/stat_refresh include lru_add_drain() on each cpu 20/26 s390/fbatch: no lru_add_drain_all() in s390_wiggle_split_folio() 21/26 block/fbatch: no lru_add_drain_all() in invalidate_bdev() 22/26 fs/fbatch: drop_caches invalidate_bh_lrus() not lru_add_drain_all() 23/26 fs,mm/fbatch: use invalidate_bh_lrus() not invalidate_bh_lrus_cpu() 24/26 fs,mm/fbatch: lru_cache_disable() keep off buffer_head lrus only 25/26 mm/fbatch: move lru_add_drain_all() declaration to mm/internal.h 26/26 mm/fbatch: drop reference inside the loop when draining

 Documentation/mm/unevictable-lru.rst |   2 +-
 arch/s390/kernel/uv.c                |   1 -
 block/bdev.c                         |   1 -
 fs/buffer.c                          |  40 +--
 fs/drop_caches.c                     |   4 +-
 include/linux/buffer_head.h          |   8 +-
 include/linux/folio_batch.h          |   7 +-
 include/linux/huge_mm.h              |   6 +-
 include/linux/mm.h                   |  18 --
 include/linux/mm_inline.h            |  29 ++
 include/linux/mm_types.h             |  14 +-
 include/linux/swap.h                 |  23 +-
 mm/compaction.c                      |  27 +-
 mm/fadvise.c                         |  17 +-
 mm/folio.c                           | 436 +++++++++------------------
 mm/gup.c                             |   9 -
 mm/huge_memory.c                     |  16 +-
 mm/hwpoison-inject.c                 |   1 -
 mm/internal.h                        |  15 +-
 mm/khugepaged.c                      |  11 -
 mm/ksm.c                             |  12 -
 mm/madvise.c                         |   9 +-
 mm/memfd.c                           |   6 +-
 mm/memory-failure.c                  |  38 +--
 mm/memory.c                          |  14 +-
 mm/memory_hotplug.c                  |   4 +
 mm/mempolicy.c                       |   7 -
 mm/migrate.c                         |  13 +-
 mm/migrate_device.c                  |   9 -
 mm/mlock.c                           | 211 +++++++------
 mm/page_alloc.c                      |   3 -
 mm/rmap.c                            |   4 -
 mm/shmem.c                           |   2 -
 mm/truncate.c                        |  17 +-
 mm/vmscan.c                          |  16 +-
 mm/vmstat.c                          |   1 +
 36 files changed, 388 insertions(+), 663 deletions(-)

base-commit: 7891fbb9512f127826e1d5dbf380ee212bd15eb0

Hugh