mm/fbatch: drain lru_add_drain() and _all()
Subsystems (24)
Hugh Dickins
maintainerv2 cover 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
Hugh Dickins
maintainerPATCH v2 1/26 mm/fbatch: remove !CONFIG_SMP special case of folio_activate()
3.0 commit eb709b0d062e ("mm: batch activate_page() to reduce lock contention") brought in an ifdef CONFIG_SMP around activate batching:
https://lore.kernel.org/linux-mm/[email protected]/
shows a sensitivity to bloat that day, not any incompatibility with UP.
No other batching here has a UP alternative, and it's a bit confusing:
simplify mm/folio.c a little by removing it now.
Certainly we can reduce UP bloat (and/or 32-bit bloat) by, say, lowering FOLIO_BATCH_SIZE from 31: traditionally 16, 14, 15, then raised to 31 by 6.9 commit 9cecde80aae0 ("mm: increase folio batch size"); or by giving just the static per-cpu folio batches a type of their own with a smaller array size on UP (1? or a little batching worthwhile even on UP?). But not right now, it's orthogonal to this series.
And I suspect that the old ifdef led to lru_activate being placed last, whereas it's usually the second most popular fbatch: move it there, to match cpu_needs_drain() comment "Check these in order of likelihood that they're not zero".
--- mm/folio.c | 40 ++++++---------------------------------- 1 file changed, 6 insertions(+), 34 deletions(-)
mm/folio.c +6 -34
@@ -48,12 +48,10 @@ struct cpu_fbatches {*/ local_lock_t lock;struct folio_batch lru_add; +struct folio_batch lru_activate;struct folio_batch lru_deactivate_file;struct folio_batch lru_deactivate;struct folio_batch lru_lazyfree; -#ifdef CONFIG_SMP -struct folio_batch lru_activate; -#endif /* Protecting the following batches which require disabling interrupts */ local_lock_t lock_irq;struct folio_batch lru_move_tail;
@@ -283,15 +281,6 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio)count_memcg_events (lruvec_memcg (lruvec),PGACTIVATE , nr_pages); } -#ifdef CONFIG_SMP -static void folio_activate_drain (int cpu) -{ -struct folio_batch *fbatch = &per_cpu (cpu_fbatches.lru_activate, cpu); - -if (folio_batch_count (fbatch)) -folio_batch_move_lru (fbatch, lru_activate); -} -void folio_activate (struct folio *folio) { if (folio_test_active(folio) || folio_test_unevictable(folio) ||
@@ -301,25 +290,6 @@ void folio_activate(struct folio *folio)folio_batch_add_and_move (folio, lru_activate); } -#else -static inline void folio_activate_drain (int cpu) -{ -} - -void folio_activate (struct folio *folio) -{ -struct lruvec *lruvec; - -if (!folio_test_clear_lru (folio)) -return ; - - lruvec =folio_lruvec_lock_irq (folio); -lru_activate (lruvec, folio); -lruvec_unlock_irq (lruvec); -folio_set_lru (folio); -} -#endif -static void __lru_cache_activate_folio (struct folio *folio) {struct folio_batch *fbatch;
@@ -628,6 +598,10 @@ void lru_add_drain_cpu(int cpu)trace_mm_lru_add_drain_tp (cpu, nr_folios); } + fbatch = &fbatches->lru_activate; +if (folio_batch_count (fbatch)) +folio_batch_move_lru (fbatch, lru_activate); + fbatch = &fbatches->lru_move_tail;/* Disabling interrupts below acts as a compiler barrier. */ if (data_race (folio_batch_count (fbatch))) {
@@ -650,8 +624,6 @@ void lru_add_drain_cpu(int cpu) fbatch = &fbatches->lru_lazyfree;if (folio_batch_count (fbatch))folio_batch_move_lru (fbatch, lru_lazyfree); - -folio_activate_drain (cpu); }/**
@@ -759,11 +731,11 @@ static bool cpu_needs_drain(unsigned int cpu)/* Check these in order of likelihood that they're not zero */ return data_race (folio_batch_count (&fbatches->lru_add) || +folio_batch_count (&fbatches->lru_activate) ||folio_batch_count (&fbatches->lru_move_tail) ||folio_batch_count (&fbatches->lru_deactivate_file) ||folio_batch_count (&fbatches->lru_deactivate) ||folio_batch_count (&fbatches->lru_lazyfree) || -folio_batch_count (&fbatches->lru_activate) ||need_mlock_drain (cpu)) ||has_bh_in_lru (cpu,NULL ); }
Signature · 1 line
2.51.0
Re: [PATCH v2 01/26] mm/fbatch: remove !CONFIG_SMP special case of folio_activate()
Quoted 9 lines · Hugh Dickins
3.0 commit
eb709b0d062e("mm: batchactivate_page()to reduce lock contention") brought in an ifdefCONFIG_SMParound activate batching:
https://lore.kernel.org/linux-mm/[email protected]/
shows a sensitivity to bloat that day, not any incompatibility with UP.No other batching here has a UP alternative, and it's a bit confusing:
simplifymm/folio.ca little by removing it now.[...]
Signature · 1 line
Alen Karnil <[email protected]>
Hugh Dickins
maintainerPATCH v2 2/26 mm/fbatch: allow folios_put_refs() to skip xa_is_value() entries
In reply to Hugh DickinsLet folios_put_refs() (hence folio_batch_release()) skip xa_is_value() entries, and therefore remove unneeded folio_batch_remove_exceptionals().
It made some sense when introduced in 3.1 for shmem swap entries only, but workingset shadows popularized exceptional entries in 3.15, and it's silly for so many sites to be squashing exceptionals out of the fbatch, merely to suit an inadequacy in folios_put_refs().
But remove exceptionals on leaving truncate_folio_batch_exceptionals(), one of whose callers then passes the fbatch on to others less tolerant.
No longer essential to this series, since 7.2 commit 9669b87065a6 ("mm/lruvec: preemptively free dead folios during lru_add drain") allowed folios_put_refs() to skip NULLs; but still an improvement.
--- include/linux/folio_batch.h | 5 +---- mm/folio.c | 25 ++++--------------------- mm/shmem.c | 2 -- mm/truncate.c | 16 ++++++++++------ 4 files changed, 15 insertions(+), 33 deletions(-)
include/linux/folio_batch.h +1 -4
@@ -22,8 +22,7 @@ struct folio;* The folio_batch is used to amortise the cost of retrieving and * operating on a set of folios. The order of folios in the batch may be * significant (eg delete_from_page_cache_batch()). Some users of the -* folio_batch store "exceptional" entries in it which can be removed -* by calling folio_batch_remove_exceptionals(). +* folio_batch store "exceptional" (xa_is_value) entries in it too. */ struct folio_batch {unsigned char nr;
@@ -100,6 +99,4 @@ static inline void folio_batch_release(struct folio_batch *fbatch)if (folio_batch_count (fbatch)) __folio_batch_release(fbatch); } - -void folio_batch_remove_exceptionals (struct folio_batch *fbatch);#endif /* _LINUX_FOLIO_BATCH_H */
mm/folio.c +4 -21
@@ -964,6 +964,10 @@ void folios_put_refs(struct folio_batch *folios, unsigned int *refs)if (!folio)continue ; +/* Skip any "exceptional" (workingset or shmem swap) entry. */ +if (xa_is_value (folio)) +continue ; +if (is_huge_zero_folio (folio))continue ;
@@ -1069,27 +1073,6 @@ void __folio_batch_release(struct folio_batch *fbatch) }EXPORT_SYMBOL (__folio_batch_release); -/** -* folio_batch_remove_exceptionals() - Prune non-folios from a batch. -* @fbatch: The batch to prune -* -* find_get_entries() fills a batch with both folios and shadow/swap/DAX -* entries. This function prunes all the non-folio entries from @fbatch -* without leaving holes, so that it can be passed on to folio-only batch -* operations. -*/ -void folio_batch_remove_exceptionals (struct folio_batch *fbatch) -{ -unsigned int i, j; - -for (i =0 , j =0 ; i <folio_batch_count (fbatch); i++) { -struct folio *folio = fbatch->folios[i]; -if (!xa_is_value (folio)) - fbatch->folios[j++] = folio; - } - fbatch->nr = j; -} -#ifdef CONFIG_MEMCG static void lruvec_reparent_lru(struct lruvec *child_lruvec, struct lruvec *parent_lruvec,
mm/shmem.c +0 -2
@@ -1157,7 +1157,6 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend,truncate_inode_folio (mapping, folio);folio_unlock (folio); } -folio_batch_remove_exceptionals (&fbatch);folio_batch_release (&fbatch);cond_resched (); }
@@ -1277,7 +1276,6 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend, }folio_unlock (folio); } -folio_batch_remove_exceptionals (&fbatch);folio_batch_release (&fbatch); }
mm/truncate.c +10 -6
@@ -53,7 +53,7 @@ static void clear_shadow_entries(struct address_space *mapping,/* * Unconditionally remove exceptional entries. Usually called from truncate * path. Note that the folio_batch may be altered by this function by removing -* exceptional entries similar to what folio_batch_remove_exceptionals() does. +* exceptional entries. * Please note that indices[] has entries in ascending order as guaranteed by * either find_get_entries() or find_lock_entries(). */
@@ -95,7 +95,7 @@ static void truncate_folio_batch_exceptionals(struct address_space *mapping,dax_delete_mapping_entry (mapping, indices[i]); } } - goto out; + goto squash; }xas_set (&xas, indices[j]);
@@ -113,8 +113,14 @@ static void truncate_folio_batch_exceptionals(struct address_space *mapping,if (mapping_shrinkable (mapping))inode_lru_list_add (mapping->host);spin_unlock (&mapping->host->i_lock); -out: -folio_batch_remove_exceptionals (fbatch); + +squash: +for (i = j +1 ; i < nr; i++) { + folio = fbatch->folios[i]; +if (!xa_is_value (folio)) + fbatch->folios[j++] = folio; + } + fbatch->nr = j; }/**
@@ -575,7 +581,6 @@ unsigned long mapping_try_invalidate(struct address_space *mapping,if (xa_has_values)clear_shadow_entries (mapping, indices[0 ], indices[nr-1 ]); -folio_batch_remove_exceptionals (&fbatch);folio_batch_release (&fbatch);cond_resched (); }
@@ -732,7 +737,6 @@ int invalidate_inode_pages2_range(struct address_space *mapping,if (xa_has_values)clear_shadow_entries (mapping, indices[0 ], indices[nr-1 ]); -folio_batch_remove_exceptionals (&fbatch);folio_batch_release (&fbatch);cond_resched (); }
Signature · 1 line
2.51.0
Re: [PATCH v2 02/26] mm/fbatch: allow folios_put_refs() to skip xa_is_value() entries
Quoted 9 lines · Hugh Dickins
Let
folios_put_refs()(hencefolio_batch_release()) skipxa_is_value()entries, and therefore remove unneededfolio_batch_remove_exceptionals().It made some sense when introduced in 3.1 for shmem swap entries only, but workingset shadows popularized exceptional entries in 3.15, and it's silly for so many sites to be squashing exceptionals out of the fbatch, merely to suit an inadequacy in
folios_put_refs().[...]
Signature · 1 line
Alen Karnil <[email protected]>
Hugh Dickins
maintainerPATCH v2 3/26 mm/fbatch: temporarily disable lazyfree and mlock+munlock batching
In reply to Hugh DickinsIt will not matter if we occasionally get activation or deactivation wrong; but a mistaken lazyfree (MADV_FREE) would likely cause dataloss. So disable its batching while reworking the per-cpu fbatch handling, then re-enable it with more thought afterwards.
Similarly disable mlock+munlock batching temporarily: they will need some redesign before re-enabling. Just insert one disabling line for now, leaving the rest of the code as it was, for consideration later.
--- mm/folio.c | 2 ++ mm/mlock.c | 3 +++ 2 files changed, 5 insertions(+)
mm/folio.c +2 -0
@@ -219,6 +219,8 @@ static void __folio_batch_add_and_move(struct folio_batch __percpu *fbatch,local_lock (&cpu_fbatches.lock);if (!folio_batch_add (this_cpu_ptr (fbatch), folio) || +/* XXX Temporarily disable lazyfree batching */ + fbatch == &cpu_fbatches.lru_lazyfree || !folio_may_be_lru_cached (folio) ||lru_cache_disabled ())folio_batch_move_lru (this_cpu_ptr (fbatch), move_fn);
mm/mlock.c +3 -0
@@ -255,6 +255,7 @@ void mlock_folio(struct folio *folio)folio_get (folio);if (!folio_batch_add (fbatch,mlock_lru (folio)) || + true ||/* XXX Temporarily disable mlock batching */ !folio_may_be_lru_cached (folio) ||lru_cache_disabled ())mlock_folio_batch (fbatch);local_unlock (&mlock_fbatch.lock);
@@ -278,6 +279,7 @@ void mlock_new_folio(struct folio *folio)folio_get (folio);if (!folio_batch_add (fbatch,mlock_new (folio)) || + true ||/* XXX Temporarily disable mlock_new batching */ !folio_may_be_lru_cached (folio) ||lru_cache_disabled ())mlock_folio_batch (fbatch);local_unlock (&mlock_fbatch.lock);
@@ -299,6 +301,7 @@ void munlock_folio(struct folio *folio)*/ folio_get (folio);if (!folio_batch_add (fbatch, folio) || + true ||/* XXX Temporarily disable munlock batching */ !folio_may_be_lru_cached (folio) ||lru_cache_disabled ())mlock_folio_batch (fbatch);local_unlock (&mlock_fbatch.lock);
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 4/26 mm/fbatch: lru bit set, no extra ref, while folio on per-cpu fbatch
In reply to Hugh DickinsTreat folios on a per-cpu fbatch as if they were already on the lruvec:
with PG_lru set, without holding an extra reference. This will enable the removal of most lru_add_drain() and lru_add_drain_all() calls soon.
Recognize such a folio by 0x02 set in the folio->lru.next pointer by folio_add_lru(). Then lruvec_del_folio() (aided by "lru_add_del_folio") can pretend to unlink it, and folio_batch_move_lru()'s lru_add case can check whether one of the others has already moved it to lruvec.
(That bit is also used in a transient way by set_page_pfmemalloc(), to inform interested callers whether page_is_pfmemalloc(): but those callers are in networking, not putting folios on LRU; and accept that any use of the page->lru field already erases page_is_pfmemalloc() information.)
Let folio->lru.next point to the lru_add fbatch entry, but this is now just for debugging: it seemed to be important for folio_batch_move_lru() to distinguish fresh from stale entries, but then it turned out that it has to processs them identically.
Activate, deactivates and move_tail, holding no reference on the folio, might come to act on a stale folio when the fbatch is drained: but it's acquired by try_get and test_clear_lru, so safe even when suboptimal.
Reclaim is not an exact science, and there have been no complaints of missed actions since 5.11 commit fc574c23558c ("mm/swap.c: serialize memcg changes in pagevec_lru_move_fn") introduced the TestClearPageLRU
protocol: so don't expect complaints of a few surprisingly taken actions.
--- include/linux/mm_inline.h | 25 ++++++++ include/linux/mm_types.h | 6 +- mm/folio.c | 119 +++++++++++++------------------------- mm/huge_memory.c | 6 +- 4 files changed, 74 insertions(+), 82 deletions(-)
include/linux/mm_inline.h +25 -0
@@ -343,6 +343,29 @@ static inline void folio_migrate_refs(struct folio *new, const struct folio *old }#endif /* CONFIG_LRU_GEN */ +enum { +LRU_NEXT_NEVER_TAIL =0 ,/* Used by a tail's compound_head */ +LRU_NEXT_BATCHED =1 ,/* Not used by any aligned pointer */ +NR_LRU_NEXT_FLAGS +}; + +static __always_inline +bool lru_add_del_folio(struct folio *folio) +{ +unsigned long lru_next =READ_ONCE (folio->lru_next); + +/* BUG_ON(folio_test_lru(folio) && folio_ref_count(folio)); */ +if (!(lru_next &BIT (LRU_NEXT_BATCHED ))) +return false; + +WRITE_ONCE (folio->lru.next,LIST_POISON1 ); +/* BUG_ON(folio->lru_next & BIT(LRU_NEXT_BATCHED)); */ + +/* Ensure folio->lru_next visible when folio_set_lru() called later */ +smp_mb__before_atomic (); +return true; +} +static __always_inline void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio) {
@@ -384,6 +407,8 @@ void lruvec_del_folio(struct lruvec *lruvec, struct folio *folio)if (lru_gen_del_folio (lruvec, folio, false))return ; +if (lru_add_del_folio (folio)) +return ;if (lru !=LRU_UNEVICTABLE )list_del (&folio->lru);
include/linux/mm_types.h +3 -3
@@ -85,6 +85,8 @@ struct page {* WARNING: bit 0 of the first word is used for PageTail(). That * means the other users of this union MUST NOT use the bit to * avoid collision and false-positive PageTail(). +* Bit 1 of the first word is used by page_is_pfmemalloc(). +* Bit 1 of the first word (lru_next) is also used by folio_add_lru(). */ union { struct { /* Page cache and anonymous pages */
@@ -410,10 +412,8 @@ struct folio {union {struct list_head lru;/* private: avoid cluttering the output */ -/* For the Unevictable "LRU list" slot */ struct { -/* Avoid compound_info */ -void *__filler; +unsigned long lru_next;/* public: */ unsigned int mlock_count;/* private: */
mm/folio.c +41 -78
179 lines not on this page · raw message
mm/huge_memory.c +5 -1
@@ -3995,8 +3995,12 @@ static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n }/* lock lru list/PageCompound, ref frozen by page_ref_freeze */ -if (do_lru) +if (do_lru) { lruvec =folio_lruvec_lock (folio); +/* Move from fbatch to lruvec before lru_add_split_folio()s */ +if (lru_add_del_folio (folio)) +lruvec_add_folio (lruvec, folio); + } ret =__split_unmapped_folio (folio, new_order, split_at, xas, mapping, split_type);
Signature · 1 line
2.51.0
Vlastimil Babka (SUSE)
maintainerRe: [PATCH v2 04/26] mm/fbatch: lru bit set, no extra ref, while folio on per-cpu fbatch
Quoted 6 lines · Hugh Dickins
Treat folios on a per-cpu fbatch as if they were already on the lruvec:
withPG_lruset, without holding an extra reference. This will enable the removal of mostlru_add_drain()andlru_add_drain_all()calls soon.Recognize such a folio by
0x02set in thefolio->lru.nextpointer byfolio_add_lru(). Thenlruvec_del_folio()(aided by "lru_add_del_folio")
I admit I find the name lru_add_del_folio() quite confusing. What it does is AFAIU try to delete from a folio_batch, which is not necessarily the lru_add one?
I don't have an alternative proposal ready though, naming is hard.
Quoted 17 lines
can pretend to unlink it, and folio_batch_move_lru()'s
lru_addcase can check whether one of the others has already moved it to lruvec.(That bit is also used in a transient way by
set_page_pfmemalloc(), to inform interested callers whetherpage_is_pfmemalloc(): but those callers are in networking, not putting folios on LRU; and accept that any use of thepage->lrufield already erasespage_is_pfmemalloc()information.)Let
folio->lru.nextpoint to thelru_addfbatch entry, but this is now just for debugging: it seemed to be important forfolio_batch_move_lru()to distinguish fresh from stale entries, but then it turned out that it has to processs them identically.Activate, deactivates and
move_tail, holding no reference on the folio, might come to act on a stale folio when the fbatch is drained: but it's acquired bytry_getandtest_clear_lru, so safe even when suboptimal.
Quoted 6 lines
Reclaim is not an exact science, and there have been no complaints of missed actions since 5.11 commit
fc574c23558c("mm/swap.c: serialize memcg changes inpagevec_lru_move_fn") introduced the TestClearPageLRU
protocol: so don't expect complaints of a few surprisingly taken actions.Signed-off-by Hugh Dickins <[email protected]>
Tricky, and I guess my questions below will betray I didn't grasp all the nuances...
Quoted 17 lines
--- include/linux/mm_inline.h | 25 ++++++++ include/linux/mm_types.h | 6 +- mm/folio.c | 119 +++++++++++++------------------------- mm/huge_memory.c | 6 +- 4 files changed, 74 insertions(+), 82 deletions(-)include/linux/mm_inline.h +2
-0@@ -343,6 +343,29 @@ static inline void folio_migrate_refs(struct folio *new, const struct folio *old }#endif /* CONFIG_LRU_GEN */ +enum { +LRU_NEXT_NEVER_TAIL =0 ,/* Used by a tail's compound_head */
include/linux/mm_inline.h static inline void folio_migrate_refs(struct folio *new, const struct folio *old · line 357
Quoted 10 lines
include/linux/mm_inline.h +10
-0+LRU_NEXT_BATCHED =1 ,/* Not used by any aligned pointer */ +NR_LRU_NEXT_FLAGS +}; + +static __always_inline +bool lru_add_del_folio(struct folio *folio) +{ +unsigned long lru_next =READ_ONCE (folio->lru_next); + +/* BUG_ON(folio_test_lru(folio) && folio_ref_count(folio)); */
Maybe do it as VM_WARN_ON_ONCE() and then it's acceptable for mainline and gets excercised by bots?
include/linux/mm_inline.h static inline void folio_migrate_refs(struct folio *new, const struct folio *old · line 362
Quoted 5 lines
include/linux/mm_inline.h +5
-0+if (!(lru_next &BIT (LRU_NEXT_BATCHED ))) +return false; + +WRITE_ONCE (folio->lru.next,LIST_POISON1 ); +/* BUG_ON(folio->lru_next & BIT(LRU_NEXT_BATCHED)); */
Is this a test for unexpected LIST_POISON1 definitions (in which case BUILD_BUG_ON would work?) or a test for a race that should not happen, however with a very tiny detection window?
Quoted 91 lines
include/linux/mm_inline.h +8
-0+ +/* Ensure folio->lru_next visible when folio_set_lru() called later */ +smp_mb__before_atomic (); +return true; +} +static __always_inline void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio) {@@ -384,6 +407,8 @@ void lruvec_del_folio(struct lruvec *lruvec, struct folio *folio)if (lru_gen_del_folio (lruvec, folio, false))return ; +if (lru_add_del_folio (folio)) +return ;if (lru !=LRU_UNEVICTABLE )list_del (&folio->lru);include/linux/mm_types.h +3
-3@@ -85,6 +85,8 @@ struct page {* WARNING: bit 0 of the first word is used for PageTail(). That * means the other users of this union MUST NOT use the bit to * avoid collision and false-positive PageTail(). +* Bit 1 of the first word is used by page_is_pfmemalloc(). +* Bit 1 of the first word (lru_next) is also used by folio_add_lru(). */ union { struct { /* Page cache and anonymous pages */@@ -410,10 +412,8 @@ struct folio {union {struct list_head lru;/* private: avoid cluttering the output */ -/* For the Unevictable "LRU list" slot */ struct { -/* Avoid compound_info */ -void *__filler; +unsigned long lru_next;/* public: */ unsigned int mlock_count;/* private: */ mm/folio.c +4
-28@@ -152,57 +152,34 @@ static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn)int i;struct lruvec *lruvec =NULL ;unsigned long flags =0 ; -struct folio_batch free_fbatch; -bool is_lru_add = (move_fn == lru_add); - -/* -* If we're adding to the LRU, preemptively filter dead folios. Use -* this dedicated folio batch for temp storage and deferred cleanup. -*/ -if (is_lru_add) -folio_batch_init (&free_fbatch);for (i =0 ; i <folio_batch_count (fbatch); i++) {struct folio *folio = fbatch->folios[i]; -/* block memcg migration while the folio moves between lru */ -if (!is_lru_add && !folio_test_clear_lru (folio)) -continue ; - -/* -* Filter dead folios by moving them from the add batch to the temp -* batch for freeing after this loop. -* -* We're bypassing normal cleanup. Clear flags that are not -* applicable to dead folios. -* -* Since the folio may be part of a huge page, unqueue from -* deferred split list to avoid a dangling list entry. -*/ -if (is_lru_add &&folio_ref_freeze (folio,1 )) { -__folio_clear_active (folio); -__folio_clear_unevictable (folio); -folio_unqueue_deferred_split (folio); +if (!folio_try_get (folio)) { fbatch->folios[i] =NULL ; -folio_batch_add (&free_fbatch, folio);continue ; } +if (!folio_test_clear_lru (folio)) +continue ; +
mm/folio.c static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn) · line 176
Quoted 10 lines
mm/folio.c +7
-0+/* Do not add to LRU if it has already been added */ +if (move_fn == lru_add && !lru_add_del_folio (folio)) + goto restore_lru; +folio_lruvec_relock_irqsave (folio, &lruvec, &flags);move_fn (lruvec, folio); +/* Do add to LRU if not already there (move_fn skipped) */ +if (lru_add_del_folio (folio)) +lruvec_add_folio (lruvec, folio);
AFAICS the previous code didn't do anything like this (and move_fn could skip or not all the same?) , and I wonder if it's now (sorry) load-bearing, or an optimization?
Quoted 137 lines
mm/folio.c +30
-50+restore_lru:folio_set_lru (folio); }if (lruvec)lruvec_unlock_irqrestore (lruvec, flags); - -/* Cleanup filtered dead folios. */ -if (is_lru_add) { -mem_cgroup_uncharge_folios (&free_fbatch); -free_unref_folios (&free_fbatch); - } -folios_put (fbatch); }@@ -211,8 +188,6 @@ static void __folio_batch_add_and_move(struct folio_batch __percpu *fbatch, {unsigned long flags; -folio_get (folio); -if (disable_irq)local_lock_irqsave (&cpu_fbatches.lock_irq, flags);else @@ -273,7 +248,6 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio)if (folio_test_active (folio) ||folio_test_unevictable (folio))return ; -lruvec_del_folio (lruvec, folio);folio_set_active (folio);lruvec_add_folio (lruvec, folio);@@ -289,37 +263,12 @@ void folio_activate(struct folio *folio) !folio_test_lru(folio))return ; -folio_batch_add_and_move (folio, lru_activate); -} - -static void __lru_cache_activate_folio (struct folio *folio) -{ -struct folio_batch *fbatch; -int i; - -local_lock (&cpu_fbatches.lock); - fbatch =this_cpu_ptr (&cpu_fbatches.lru_add); -/* -* Search backwards on the optimistic assumption that the folio being -* activated has just been added to this batch. Note that only -* the local batch is examined as a !LRU folio could be in the -* process of being released, reclaimed, migrated or on a remote -* batch that is currently being drained. Furthermore, marking -* a remote batch's folio active potentially hits a race where -* a folio is marked active just after it is added to the inactive -* list causing accounting errors and BUG_ON checks to trigger. +* XXX: It is curiously difficult to recreate safely the old +* __lru_cache_activate_folio() optimization (folio_set_active() +* directly if it's on the local lru_add fbatch): revisit later. */ -for (i =folio_batch_count (fbatch) -1 ; i >=0 ; i--) { -struct folio *batch_folio = fbatch->folios[i]; - -if (batch_folio == folio) { -folio_set_active (folio); -break ; - } - } - -local_unlock (&cpu_fbatches.lock); +folio_batch_add_and_move (folio, lru_activate); }#ifdef CONFIG_LRU_GEN @@ -410,16 +359,7 @@ void folio_mark_accessed(struct folio *folio)* unevictable page accessed has no effect. */ }else if (!folio_test_active (folio)) { -/* -* If the folio is on the LRU, queue it for activation via -* cpu_fbatches.lru_activate. Otherwise, assume the folio is in a -* folio_batch, mark it active and it'll be moved to the active -* LRU on the next drain. -*/ -if (folio_test_lru (folio)) -folio_activate (folio); -else -__lru_cache_activate_folio (folio); +folio_activate (folio);folio_clear_referenced (folio);workingset_activation (folio); }@@ -439,6 +379,10 @@ EXPORT_SYMBOL(folio_mark_accessed);*/ void folio_add_lru (struct folio *folio) { +struct folio_batch *fbatch; +unsigned long lru_next; +bool full; +VM_BUG_ON_FOLIO (folio_test_active (folio) &&folio_test_unevictable (folio), folio);VM_BUG_ON_FOLIO (folio_test_lru (folio), folio);@@ -458,7 +402,26 @@ void folio_add_lru(struct folio *folio)folio_mark_accessed (folio); } -folio_batch_add_and_move (folio, lru_add); +local_lock (&cpu_fbatches.lock); + fbatch =this_cpu_ptr (&cpu_fbatches.lru_add); + +/* Storing this address is only for debugging */ + lru_next = (unsigned long )&fbatch->folios[fbatch->nr]; +/* This mask will do nothing on 64-bit */ + lru_next &= ~(BIT (NR_LRU_NEXT_FLAGS ) -1 ); + lru_next |=BIT (LRU_NEXT_BATCHED ); + folio->lru_next = lru_next; + + full = !folio_batch_add (fbatch, folio); + +/* Ensure folio->lru_next visible to folio_test_clear_lru() callers */ +smp_mb__before_atomic (); +folio_set_lru (folio); + +if (full || !folio_may_be_lru_cached (folio) ||lru_cache_disabled ()) +folio_batch_move_lru (fbatch, lru_add); + +local_unlock (&cpu_fbatches.lock); }EXPORT_SYMBOL (folio_add_lru);mm/huge_memory.c +0
-0
mm/huge_memory.c static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n · line 4002
Quoted 9 lines
mm/huge_memory.c +4
-1@@ -3995,8 +3995,12 @@ static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n }/* lock lru list/PageCompound, ref frozen by page_ref_freeze */ -if (do_lru) +if (do_lru) { lruvec =folio_lruvec_lock (folio); +/* Move from fbatch to lruvec before lru_add_split_folio()s */ +if (lru_add_del_folio (folio)) +lruvec_add_folio (lruvec, folio);
This is not mentioned in the changelog. Why is it necessary now?
Quoted 4 lines
mm/huge_memory.c +1
-0+ } ret =__split_unmapped_folio (folio, new_order, split_at, xas, mapping, split_type);
Hugh Dickins
maintainerRe: [PATCH v2 04/26] mm/fbatch: lru bit set, no extra ref, while folio on per-cpu fbatch
Quoted 11 lines · Vlastimil Babka (SUSE)
Quoted 6 lines · Hugh Dickins
Treat folios on a per-cpu fbatch as if they were already on the lruvec:
withPG_lruset, without holding an extra reference. This will enable the removal of mostlru_add_drain()andlru_add_drain_all()calls soon.Recognize such a folio by
0x02set in thefolio->lru.nextpointer byfolio_add_lru(). Thenlruvec_del_folio()(aided by "lru_add_del_folio")I admit I find the name
lru_add_del_folio()quite confusing. What it does is AFAIU try to delete from afolio_batch, which is not necessarily thelru_addone?
It is to delete from the lru_add fbatch: that's the one where a folio is put into the LRU system initially, and it's the one which has this special LRU_NEXT_BATCHED linkage to the fbatch, not any proper next,prev linkage into a real LRU list.
Which is not to say that that folio could not also occur in some of the other fbatches (for this or other CPUs) at the same time: it might, because of the un-refcoount-raised folio getting freed and reused; and it might, as the same instance of the folio, be acted upon by one of the other LRU manipulation functions. Those latter tend to have "!folio_test_lru" checks in, but now the lru_add ones do have lru set; they tend to be reached through a real LRU list of folios (e.g. deciding to deactivate something found on an active list), but I don't think that's necessarily the case.
As to be name: I did try out several alternatives, but was most satisfied by "lru_add_del_folio". I'll certainly grant that it's amusing, and maybe too confusing if it were to be a widely-used API; but it mostly ends up just nestling inside lruvec_del_folio().
Quoted 24 lines
I don't have an alternative proposal ready though, naming is hard.
Quoted 22 lines
can pretend to unlink it, and folio_batch_move_lru()'s
lru_addcase can check whether one of the others has already moved it to lruvec.(That bit is also used in a transient way by
set_page_pfmemalloc(), to inform interested callers whetherpage_is_pfmemalloc(): but those callers are in networking, not putting folios on LRU; and accept that any use of thepage->lrufield already erasespage_is_pfmemalloc()information.)Let
folio->lru.nextpoint to thelru_addfbatch entry, but this is now just for debugging: it seemed to be important forfolio_batch_move_lru()to distinguish fresh from stale entries, but then it turned out that it has to processs them identically.Activate, deactivates and
move_tail, holding no reference on the folio, might come to act on a stale folio when the fbatch is drained: but it's acquired bytry_getandtest_clear_lru, so safe even when suboptimal.Reclaim is not an exact science, and there have been no complaints of missed actions since 5.11 commit
fc574c23558c("mm/swap.c: serialize memcg changes inpagevec_lru_move_fn") introduced the TestClearPageLRU
protocol: so don't expect complaints of a few surprisingly taken actions.
Quoted 4 lines
Quoted 1 line
Signed-off-by Hugh Dickins <[email protected]>Tricky, and I guess my questions below will betray I didn't grasp all the nuances...
Many thanks for looking: it is the core, and the hardest to review.
Quoted 16 lines
Quoted 16 lines
--- include/linux/mm_inline.h | 25 ++++++++ include/linux/mm_types.h | 6 +- mm/folio.c | 119 +++++++++++++------------------------- mm/huge_memory.c | 6 +- 4 files changed, 74 insertions(+), 82 deletions(-)include/linux/mm_inline.h +0
-0@@ -343,6 +343,29 @@ static inline void folio_migrate_refs(struct folio *new, const struct folio *old }#endif /* CONFIG_LRU_GEN */
Quoted 15 lines
include/linux/mm_inline.h static inline void folio_migrate_refs(struct folio *new, const struct folio *old · line 357
Quoted 12 lines
include/linux/mm_inline.h +12
-0+enum { +LRU_NEXT_NEVER_TAIL =0 ,/* Used by a tail's compound_head */ +LRU_NEXT_BATCHED =1 ,/* Not used by any aligned pointer */ +NR_LRU_NEXT_FLAGS +}; + +static __always_inline +bool lru_add_del_folio(struct folio *folio) +{ +unsigned long lru_next =READ_ONCE (folio->lru_next); + +/* BUG_ON(folio_test_lru(folio) && folio_ref_count(folio)); */ Maybe do it as
VM_WARN_ON_ONCE()and then it's acceptable for mainline and gets excercised by bots?
All I want there is the comment; but I thought the most concise and helpful way to write the comment would be to write it as a BUG_ON(). I'm not keen on bloating inlines with assertions, and _ONCEs have their cost on top - all mitigated by the VM_, true. I can change it if asked, but all I want to put there myself is the comment.
Quoted 10 lines
include/linux/mm_inline.h static inline void folio_migrate_refs(struct folio *new, const struct folio *old · line 362
Quoted 6 lines
include/linux/mm_inline.h +5
-0+if (!(lru_next &BIT (LRU_NEXT_BATCHED ))) +return false; + +WRITE_ONCE (folio->lru.next,LIST_POISON1 ); +/* BUG_ON(folio->lru_next & BIT(LRU_NEXT_BATCHED)); */ Is this a test for unexpected
LIST_POISON1definitions (in which caseBUILD_BUG_ONwould work?) or a test for a race that should not happen, however with a very tiny detection window?
Again, it's just a comment, that things will go wrong if someone one day decides to redefine LIST_POISON1 in a way which conflicts. Yes, if it were uncommented, then I'd prefer to put a BUILD_BUG_ON instead.
Quoted 53 lines
Quoted 53 lines
mm/folio.c +8
-28@@ -152,57 +152,34 @@ static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn)int i;struct lruvec *lruvec =NULL ;unsigned long flags =0 ; -struct folio_batch free_fbatch; -bool is_lru_add = (move_fn == lru_add); - -/* -* If we're adding to the LRU, preemptively filter dead folios. Use -* this dedicated folio batch for temp storage and deferred cleanup. -*/ -if (is_lru_add) -folio_batch_init (&free_fbatch);for (i =0 ; i <folio_batch_count (fbatch); i++) {struct folio *folio = fbatch->folios[i]; -/* block memcg migration while the folio moves between lru */ -if (!is_lru_add && !folio_test_clear_lru (folio)) -continue ; - -/* -* Filter dead folios by moving them from the add batch to the temp -* batch for freeing after this loop. -* -* We're bypassing normal cleanup. Clear flags that are not -* applicable to dead folios. -* -* Since the folio may be part of a huge page, unqueue from -* deferred split list to avoid a dangling list entry. -*/ -if (is_lru_add &&folio_ref_freeze (folio,1 )) { -__folio_clear_active (folio); -__folio_clear_unevictable (folio); -folio_unqueue_deferred_split (folio); +if (!folio_try_get (folio)) { fbatch->folios[i] =NULL ; -folio_batch_add (&free_fbatch, folio);continue ; } +if (!folio_test_clear_lru (folio)) +continue ; + +/* Do not add to LRU if it has already been added */ +if (move_fn == lru_add && !lru_add_del_folio (folio)) + goto restore_lru; +folio_lruvec_relock_irqsave (folio, &lruvec, &flags);move_fn (lruvec, folio);
Quoted 7 lines
mm/folio.c static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn) · line 176
Quoted 3 lines
mm/folio.c +3
-0+/* Do add to LRU if not already there (move_fn skipped) */ +if (lru_add_del_folio (folio)) +lruvec_add_folio (lruvec, folio);AFAICS the previous code didn't do anything like this (and
move_fncould skip or not all the same?) , and I wonder if it's now (sorry) load-bearing, or an optimization?
Good question, an optimization or essential? I'd have to ask for more time, to give you a definitive answer on that. What I can easily do is give the thinking behind it. We're the lucky one who got to clear lru bit, it's conceivable that a racing task (particularly or necessarily? when the folio has got freed and reused meanwhile) wanting to put this folio on real LRU failed to clear lru bit and so skipped this entry, and in that case it is our responsibilty to do so (usually the move_fn does an lruvec del and add which accomplishes that, but not when it skipped the folio as having unsuitable flags).
Those lines were not in my initial attempt, and I never noticed any badness from not having them (folio unreclaimable and unmigratable until freed, I presume); but when working on the mlock+munlock I became more aware of this need for the lru-bit-clearer to do the work others are expecting. (And I do not pretend that munlock is complete in that respect yet: no more imperfect than before, I think, but more work to do if perfection is needed.)
Quoted 17 lines
Quoted 5 lines
mm/huge_memory.c +0
-0mm/huge_memory.c static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n · line 4002
Quoted 9 lines
mm/huge_memory.c +4
-1@@ -3995,8 +3995,12 @@ static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n }/* lock lru list/PageCompound, ref frozen by page_ref_freeze */ -if (do_lru) +if (do_lru) { lruvec =folio_lruvec_lock (folio); +/* Move from fbatch to lruvec before lru_add_split_folio()s */ +if (lru_add_del_folio (folio)) +lruvec_add_folio (lruvec, folio);This is not mentioned in the changelog. Why is it necessary now?
Quoted 4 lines
mm/huge_memory.c static int __folio_freeze_and_split_unmapped(struct folio *folio, unsigned int n · line 4006
Quoted 4 lines
mm/huge_memory.c +1
-0+ } ret =__split_unmapped_folio (folio, new_order, split_at, xas, mapping, split_type);
It caught me by surprise too,
I was very lucky that our internal testing crashed without that, it's a narrow race when we're not batching large folios. But you only have to look at lru_add_split_folio() to see, that it does (very reasonably) expect a folio with the lru bit set to have sensible next,prev. (WHereas most others isolate a folio before working on it, hugepage splitting is peculiar in having a mode where folio is left on lru, but frozen to refcount zero while the work is done).
Thanks! More replies later...
Hugh
Hugh Dickins
maintainerPATCH v2 5/26 mm/fbatch: lru_add_del_folio()+folio_add_lru() after clear_lru()
In reply to Hugh DickinsMost callers of folio_test_clear_lru() then proceed to remove the folio from its lru, and add it back at the end when they're done (if still in use). But isolate_migratepages_block() and check_move_unevictable_pages() sometimes decide against, and release immediately with a folio_set_lru().
Which usually works fine: but there's now a small chance that while they held the folio with lru bit cleared, an lru_add fbatch drain came along, and had to skip that folio because its lru bit was transiently cleared (previously, the lru_add fbatch drain relied on finding lru bit never yet set). This risks leaving that folio off lru, unreclaimable until freed.
Fix such cases by trying lru_add_del_folio() (which only takes action and returns true if the folio was on an lru_add fbatch), then folio_add_lru() if it succeeded: invalidating the old fbatch slot, appending in a new one.
--- mm/compaction.c | 13 +++++++++++-- mm/vmscan.c | 16 +++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-)
mm/compaction.c +11 -2
@@ -1204,7 +1204,13 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, !cc->alloc_contig)) { low_pfn +=folio_nr_pages (folio) -1 ; nr_scanned +=folio_nr_pages (folio) -1 ; -folio_set_lru (folio); +if (lru_add_del_folio (folio)) { +lruvec_unlock_irqrestore (locked, flags); +folio_add_lru (folio); + locked =NULL ; + }else { +folio_set_lru (folio); + } goto isolate_fail_put; } }
@@ -1293,7 +1299,10 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (locked)lruvec_unlock_irqrestore (locked, flags);if (folio) { -folio_set_lru (folio); +if (lru_add_del_folio (folio)) +folio_add_lru (folio); +else +folio_set_lru (folio);folio_put (folio); }
mm/vmscan.c +9 -7
@@ -8093,17 +8093,19 @@ void check_move_unevictable_folios(struct folio_batch *fbatch)folio_clear_unevictable (folio);lruvec_add_folio (lruvec, folio); pgrescued += nr_pages; + }else if (lru_add_del_folio (folio)) { +lruvec_unlock_irq (lruvec); +folio_add_lru (folio); + lruvec =NULL ; } -folio_set_lru (folio); +if (lruvec) +folio_set_lru (folio); } -if (lruvec) { -__count_vm_events (UNEVICTABLE_PGRESCUED , pgrescued); -__count_vm_events (UNEVICTABLE_PGSCANNED , pgscanned); +if (lruvec)lruvec_unlock_irq (lruvec); - }else if (pgscanned) { -count_vm_events (UNEVICTABLE_PGSCANNED , pgscanned); - } +count_vm_events (UNEVICTABLE_PGRESCUED , pgrescued); +count_vm_events (UNEVICTABLE_PGSCANNED , pgscanned); }EXPORT_SYMBOL_GPL (check_move_unevictable_folios);
Signature · 1 line
2.51.0
Re: [PATCH v2 05/26] mm/fbatch: lru_add_del_folio()+folio_add_lru() after clear_lru()
Quoted 10 lines · Hugh Dickins
Most callers of
folio_test_clear_lru()then proceed to remove the folio from its lru, and add it back at the end when they're done (if still in use). Butisolate_migratepages_block()andcheck_move_unevictable_pages()sometimes decide against, and release immediately with afolio_set_lru().Which usually works fine: but there's now a small chance that while they held the folio with lru bit cleared, an
lru_addfbatch drain came along, and had to skip that folio because its lru bit was transiently cleared (previously, thelru_addfbatch drain relied on finding lru bit never yet set). This risks leaving that folio off lru, unreclaimable until freed.
So this makes the previous patch a somewhat bisection hazard? I guess it's acceptable given it's not fatal.
Quoted 2 lines
Fix such cases by trying
lru_add_del_folio()(which only takes action and returns true if the folio was on anlru_addfbatch), thenfolio_add_lru()
Oh ok, that's one detail I didn't realize on the previous patch, and explains the name of the function. But it's still IMHO confusing.
Quoted 3 lines
if it succeeded: invalidating the old fbatch slot, appending in a new one.
Signed-off-by Hugh Dickins <[email protected]>
In general, LGTM.
Nit below:
Quoted 54 lines
--- mm/compaction.c | 13 +++++++++++-- mm/vmscan.c | 16 +++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-)mm/compaction.c +11
-2@@ -1204,7 +1204,13 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, !cc->alloc_contig)) { low_pfn +=folio_nr_pages (folio) -1 ; nr_scanned +=folio_nr_pages (folio) -1 ; -folio_set_lru (folio); +if (lru_add_del_folio (folio)) { +lruvec_unlock_irqrestore (locked, flags); +folio_add_lru (folio); + locked =NULL ; + }else { +folio_set_lru (folio); + } goto isolate_fail_put; } }@@ -1293,7 +1299,10 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (locked)lruvec_unlock_irqrestore (locked, flags);if (folio) { -folio_set_lru (folio); +if (lru_add_del_folio (folio)) +folio_add_lru (folio); +else +folio_set_lru (folio);folio_put (folio); }mm/vmscan.c +6
-1@@ -8093,17 +8093,19 @@ void check_move_unevictable_folios(struct folio_batch *fbatch)folio_clear_unevictable (folio);lruvec_add_folio (lruvec, folio); pgrescued += nr_pages; + }else if (lru_add_del_folio (folio)) { +lruvec_unlock_irq (lruvec); +folio_add_lru (folio); + lruvec =NULL ; } -folio_set_lru (folio); +if (lruvec) +folio_set_lru (folio); }
mm/vmscan.c void check_move_unevictable_folios(struct folio_batch *fbatch) · line 8108
Quoted 10 lines
mm/vmscan.c +3
-6-if (lruvec) { -__count_vm_events (UNEVICTABLE_PGRESCUED , pgrescued); -__count_vm_events (UNEVICTABLE_PGSCANNED , pgscanned); +if (lruvec)lruvec_unlock_irq (lruvec); - }else if (pgscanned) { -count_vm_events (UNEVICTABLE_PGSCANNED , pgscanned); - } +count_vm_events (UNEVICTABLE_PGRESCUED , pgrescued); +count_vm_events (UNEVICTABLE_PGSCANNED , pgscanned);
AFAIU this is done because we can no longer rule out that !lruvec means pgrescued is 0.
But we can still distinguish the cheaper __count_vm_events vs count_vm_events? Probably all the same on x86, but I hear on arm64 this_cpu* ops have a cost worth proposing rather elaborate schemes to deal with...
Quoted 3 lines
mm/vmscan.c +0
-0}EXPORT_SYMBOL_GPL (check_move_unevictable_folios);
Hugh Dickins
maintainerRe: [PATCH v2 05/26] mm/fbatch: lru_add_del_folio()+folio_add_lru() after clear_lru()
Quoted 6 lines · Vlastimil Babka (SUSE)
Quoted 5 lines · Hugh Dickins
Most callers of
folio_test_clear_lru()then proceed to remove the folio from its lru, and add it back at the end when they're done (if still in use). Butisolate_migratepages_block()andcheck_move_unevictable_pages()sometimes decide against, and release immediately with afolio_set_lru().
Quoted 8 lines
Quoted 5 lines
Which usually works fine: but there's now a small chance that while they held the folio with lru bit cleared, an
lru_addfbatch drain came along, and had to skip that folio because its lru bit was transiently cleared (previously, thelru_addfbatch drain relied on finding lru bit never yet set). This risks leaving that folio off lru, unreclaimable until freed.So this makes the previous patch a somewhat bisection hazard? I guess it's acceptable given it's not fatal.
Not what I would call a bisection hazard. Yes, the preceding patch is not perfect, but more reviewable that way, and then come corrections to edge cases best considered by themselves. Nobody bisecting unrelated issues would get held up by this gap, and it won't crash any bisections.
Quoted 7 lines
Quoted 3 lines
Fix such cases by trying
lru_add_del_folio()(which only takes action and returns true if the folio was on anlru_addfbatch), thenfolio_add_lru()Oh ok, that's one detail I didn't realize on the previous patch, and explains the name of the function. But it's still IMHO confusing.
Quoted 6 lines
Quoted 3 lines
if it succeeded: invalidating the old fbatch slot, appending in a new one.
Signed-off-by Hugh Dickins <[email protected]>In general, LGTM.
Reviewed-by Vlastimil Babka (SUSE) <[email protected]>
Thanks.
Quoted 21 lines
Nit below:
Quoted 18 lines
mm/vmscan.c +6
-1@@ -8093,17 +8093,19 @@ void check_move_unevictable_folios(struct folio_batch *fbatch)folio_clear_unevictable (folio);lruvec_add_folio (lruvec, folio); pgrescued += nr_pages; + }else if (lru_add_del_folio (folio)) { +lruvec_unlock_irq (lruvec); +folio_add_lru (folio); + lruvec =NULL ; } -folio_set_lru (folio); +if (lruvec) +folio_set_lru (folio); }
Quoted 16 lines
mm/vmscan.c void check_move_unevictable_folios(struct folio_batch *fbatch) · line 8108
Quoted 10 lines
mm/vmscan.c +3
-6-if (lruvec) { -__count_vm_events (UNEVICTABLE_PGRESCUED , pgrescued); -__count_vm_events (UNEVICTABLE_PGSCANNED , pgscanned); +if (lruvec)lruvec_unlock_irq (lruvec); - }else if (pgscanned) { -count_vm_events (UNEVICTABLE_PGSCANNED , pgscanned); - } +count_vm_events (UNEVICTABLE_PGRESCUED , pgrescued); +count_vm_events (UNEVICTABLE_PGSCANNED , pgscanned);AFAIU this is done because we can no longer rule out that !lruvec means pgrescued is 0.
But we can still distinguish the cheaper__count_vm_eventsvscount_vm_events? Probably all the same on x86, but I hear on arm64 this_cpu* ops have a cost worth proposing rather elaborate schemes to deal with...
Yes, it was just looking a bit baroque to still be deciding whether to use the __count or the count there. Could be done of course, and with "if (pgrescued)" and "if (pgscanned)"; but I haven't noticed anywhere else in the source where we go to such lengths to use __count versus count, and I don't think this is on anyone's hotpath (IIRC this is just SHM_UNLOCK).
Now you've got me worried, no, fractionally worried, about Shakeel's recent __count to count fix to NR_MLOCK.
I am much more familiar with x86, and have noticed the recent tussles over improving arm64 this_cpus, so that confirms you're right; but I'd look for juicier low-hanging fruit than this, if we're going to embark on an "if (x) __count() else count()" spree.
Hugh
Hugh Dickins
maintainerPATCH v2 6/26 mm/fbatch: fbatch_drain_lazyfree(onstack fbatch) before ptl unlock
In reply to Hugh DickinsRe-enable lazyfree batching for MADV_FREE. But it's not safe now to leave potentially stale (then reused) folios in a per-cpu fbatch for lazyfree. Instead, madvise_free_pte_range() keep an fbatch on its stack, and drain it each time before dropping pagetable lock, while the folios are secure.
Ignore folio_may_be_lru_cached() and lru_cache_disabled(): limitations irrelevant to this fbatch drained under spinlock (even if RT); though in practice madvise_free_huge_pmd() does have to drain every time.
--- include/linux/huge_mm.h | 6 ++++-- mm/folio.c | 34 +++++++++++++++++++++------------- mm/huge_memory.c | 6 ++++-- mm/internal.h | 3 ++- mm/madvise.c | 9 +++++++-- 5 files changed, 38 insertions(+), 20 deletions(-)
include/linux/huge_mm.h +4 -2
@@ -24,9 +24,11 @@ static inline void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud) }#endif -vm_fault_t do_huge_pmd_wp_page (struct vm_fault *vmf); +struct folio_batch ;bool madvise_free_huge_pmd (struct mmu_gather *tlb,struct vm_area_struct *vma, -pmd_t *pmd,unsigned long addr,unsigned long next); +pmd_t *pmd,unsigned long addr,unsigned long next, +struct folio_batch *fbatch); +vm_fault_t do_huge_pmd_wp_page (struct vm_fault *vmf);bool zap_huge_pmd (struct mmu_gather *tlb,struct vm_area_struct *vma,pmd_t *pmd,unsigned long addr); int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, pud_t *pud,
mm/folio.c +21 -13
@@ -51,7 +51,6 @@ struct cpu_fbatches {struct folio_batch lru_activate;struct folio_batch lru_deactivate_file;struct folio_batch lru_deactivate; -struct folio_batch lru_lazyfree;/* Protecting the following batches which require disabling interrupts */ local_lock_t lock_irq;struct folio_batch lru_move_tail;
@@ -194,8 +193,6 @@ static void __folio_batch_add_and_move(struct folio_batch __percpu *fbatch,local_lock (&cpu_fbatches.lock);if (!folio_batch_add (this_cpu_ptr (fbatch), folio) || -/* XXX Temporarily disable lazyfree batching */ - fbatch == &cpu_fbatches.lru_lazyfree || !folio_may_be_lru_cached (folio) ||lru_cache_disabled ())folio_batch_move_lru (this_cpu_ptr (fbatch), move_fn);
@@ -585,10 +582,6 @@ void lru_add_drain_cpu(int cpu) fbatch = &fbatches->lru_deactivate;if (folio_batch_count (fbatch))folio_batch_move_lru (fbatch, lru_deactivate); - - fbatch = &fbatches->lru_lazyfree; -if (folio_batch_count (fbatch)) -folio_batch_move_lru (fbatch, lru_lazyfree); }/**
@@ -634,19 +627,35 @@ void folio_deactivate(struct folio *folio)/** * folio_mark_lazyfree - make an anon folio lazyfree -* @folio: folio to deactivate +* @fbatch: batch to which folio will be added +* @folio: folio to be lazily freed * -* folio_mark_lazyfree() moves @folio to the inactive file list. -* This is done to accelerate the reclaim of @folio. +* folio_mark_lazyfree() moves @folio to the inactive file list +* via @fbatch. This is done to accelerate the reclaim of @folio. */ -void folio_mark_lazyfree (struct folio *folio) +void folio_mark_lazyfree (struct folio_batch *fbatch,struct folio *folio) {if (!folio_test_anon (folio) || !folio_test_swapbacked (folio) || !folio_test_lru (folio) ||folio_test_swapcache (folio) ||folio_test_unevictable (folio))return ; -folio_batch_add_and_move (folio, lru_lazyfree); +if (!folio_batch_add (fbatch, folio)) +folio_batch_move_lru (fbatch, lru_lazyfree); +} + +/** +* fbatch_drain_lazyfree - drain the caller's folio batch +* @fbatch: batch of folios to be lazily freed +* +* Must be called before caller drops the page table lock: that is, +* before dropping the last certain reference to the folios in @fbatch. +* It would be very bad to lazyfree a folio after it was freed and reused. +*/ +void fbatch_drain_lazyfree (struct folio_batch *fbatch) +{ +if (folio_batch_count (fbatch)) +folio_batch_move_lru (fbatch, lru_lazyfree); }void lru_add_drain (void )
@@ -700,7 +709,6 @@ static bool cpu_needs_drain(unsigned int cpu)folio_batch_count (&fbatches->lru_move_tail) ||folio_batch_count (&fbatches->lru_deactivate_file) ||folio_batch_count (&fbatches->lru_deactivate) || -folio_batch_count (&fbatches->lru_lazyfree) || need_mlock_drain(cpu)) ||has_bh_in_lru (cpu,NULL ); }
mm/huge_memory.c +4 -2
@@ -2356,7 +2356,8 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)* Otherwise, return false. */ bool madvise_free_huge_pmd (struct mmu_gather *tlb,struct vm_area_struct *vma, -pmd_t *pmd,unsigned long addr,unsigned long next) +pmd_t *pmd,unsigned long addr,unsigned long next, +struct folio_batch *fbatch) {spinlock_t *ptl;pmd_t orig_pmd;
@@ -2417,7 +2418,8 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,tlb_remove_pmd_tlb_entry (tlb, pmd, addr); } -folio_mark_lazyfree (folio); +folio_mark_lazyfree (fbatch, folio); +fbatch_drain_lazyfree (fbatch); ret = true; out:spin_unlock (ptl);
mm/internal.h +2 -1
@@ -63,7 +63,8 @@ void lru_add_drain(void);void lru_add_drain_cpu (int cpu);void lru_add_drain_cpu_zone (struct zone *zone);void folio_deactivate (struct folio *folio); -void folio_mark_lazyfree (struct folio *folio); +void folio_mark_lazyfree (struct folio_batch *fbatch,struct folio *folio); +void fbatch_drain_lazyfree (struct folio_batch *fbatch);/* mm/vmscan.c */ unsigned long zone_reclaimable_pages (struct zone *zone);
mm/madvise.c +7 -2
@@ -27,6 +27,7 @@#include <linux/file.h> #include <linux/blk_plug.h> #include <linux/backing-dev.h> +#include <linux/folio_batch.h> #include <linux/pagewalk.h> #include <linux/swap.h> #include <linux/leafops.h>
@@ -666,6 +667,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,struct mmu_gather *tlb = walk->private;struct mm_struct *mm = tlb->mm;struct vm_area_struct *vma = walk->vma; +struct folio_batch fbatch;spinlock_t *ptl;pte_t *start_pte, *pte, ptent;struct folio *folio;
@@ -673,9 +675,10 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,unsigned long next;int nr, max_nr; +folio_batch_init (&fbatch); next =pmd_addr_end (addr, end);if (pmd_trans_huge (*pmd)) -if (madvise_free_huge_pmd (tlb, vma, pmd, addr, next)) +if (madvise_free_huge_pmd (tlb, vma, pmd, addr, next, &fbatch))return 0 ;tlb_change_page_size (tlb,PAGE_SIZE );
@@ -733,6 +736,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,continue ;folio_get (folio);lazy_mmu_mode_disable (); +fbatch_drain_lazyfree (&fbatch);pte_unmap_unlock (start_pte, ptl); start_pte =NULL ; err =split_folio (folio);
@@ -777,13 +781,14 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,clear_young_dirty_ptes (vma, addr, pte, nr, cydp_flags);tlb_remove_tlb_entries (tlb, pte, nr, addr); } -folio_mark_lazyfree (folio); +folio_mark_lazyfree (&fbatch, folio); }if (nr_swap)add_mm_counter (mm,MM_SWAPENTS , nr_swap);if (start_pte) {lazy_mmu_mode_disable (); +fbatch_drain_lazyfree (&fbatch);pte_unmap_unlock (start_pte, ptl); }cond_resched ();
Signature · 1 line
2.51.0
Re: [PATCH v2 06/26] mm/fbatch: fbatch_drain_lazyfree(onstack fbatch) before ptl unlock
Quoted 10 lines · Hugh Dickins
Re-enable lazyfree batching for
MADV_FREE. But it's not safe now to leave potentially stale (then reused) folios in a per-cpu fbatch for lazyfree. Instead,madvise_free_pte_range()keep an fbatch on its stack, and drain it each time before dropping pagetable lock, while the folios are secure.Ignore
folio_may_be_lru_cached()andlru_cache_disabled(): limitations irrelevant to this fbatch drained under spinlock (even if RT); though in practicemadvise_free_huge_pmd()does have to drain every time.Signed-off-by Hugh Dickins <[email protected]>
It seems correct to me, so:
Might be that something regresses performance though. Guess we'll see.
Also seems to me that if this patch was preparatory, the batching wouldn't have to be temporarily disabled. Doesn't matter ultimately though.
Hugh Dickins
maintainerRe: [PATCH v2 06/26] mm/fbatch: fbatch_drain_lazyfree(onstack fbatch) before ptl unlock
Quoted 10 lines · Vlastimil Babka (SUSE)
Quoted 9 lines · Hugh Dickins
Re-enable lazyfree batching for
MADV_FREE. But it's not safe now to leave potentially stale (then reused) folios in a per-cpu fbatch for lazyfree. Instead,madvise_free_pte_range()keep an fbatch on its stack, and drain it each time before dropping pagetable lock, while the folios are secure.Ignore
folio_may_be_lru_cached()andlru_cache_disabled(): limitations irrelevant to this fbatch drained under spinlock (even if RT); though in practicemadvise_free_huge_pmd()does have to drain every time.
Quoted 5 lines
Quoted 1 line
Signed-off-by Hugh Dickins <[email protected]>It seems correct to me, so:
Reviewed-by Vlastimil Babka (SUSE) <[email protected]>
Thanks.
Quoted 5 lines
Might be that something regresses performance though. Guess we'll see.
Also seems to me that if this patch was preparatory, the batching wouldn't have to be temporarily disabled. Doesn't matter ultimately though.
That's true, the order of patches does rather reflect the order in which I got to think about things - but I thought it might be easiest to review in this way too. I was anxious to get to the core patch (04/26) as quickly as possible, to find out whether the idea would fly at all; I could see lazyfree was a problem, but wanted to put off its solution.
Hugh
Hugh Dickins
maintainerPATCH v2 7/26 mm/fbatch: LRU_NEXT_ACTIVATE bit to optimize folio_activate()
In reply to Hugh DickinsImplement an equivalent to the old __lru_cache_activate_folio() optimization, to activate a folio recently put in the lru_add fbatch, without having to put it through the lru_activate fbatch too. Neither lruvec lock nor lru bit can guard this safely and efficiently, so resort to try_cmpxchg() on a further, LRU_NEXT_ACTIVATE bit in folio->lru_next.
--- include/linux/mm_inline.h | 4 ++++ mm/folio.c | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-)
include/linux/mm_inline.h +4 -0
@@ -346,6 +346,7 @@ static inline void folio_migrate_refs(struct folio *new, const struct folio *oldenum {LRU_NEXT_NEVER_TAIL =0 ,/* Used by a tail's compound_head */ LRU_NEXT_BATCHED =1 ,/* Not used by any aligned pointer */ +LRU_NEXT_ACTIVATE ,NR_LRU_NEXT_FLAGS };
@@ -358,6 +359,9 @@ bool lru_add_del_folio(struct folio *folio)if (!(lru_next &BIT (LRU_NEXT_BATCHED )))return false; +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +folio_set_active (folio); +WRITE_ONCE (folio->lru.next,LIST_POISON1 );/* BUG_ON(folio->lru_next & BIT(LRU_NEXT_BATCHED)); */
mm/folio.c +20 -3
@@ -256,15 +256,32 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio)void folio_activate (struct folio *folio) { +unsigned long lru_next; +if (folio_test_active (folio) ||folio_test_unevictable (folio) || !folio_test_lru (folio))return ;/* -* XXX: It is curiously difficult to recreate safely the old -* __lru_cache_activate_folio() optimization (folio_set_active() -* directly if it's on the local lru_add fbatch): revisit later. +* This optimization is intended for the common case of folio +* having been recently added to this CPU's lru_add fbatch. +* But since other CPUs can now take it at any instant (after +* a folio_test_clear_lru()), and we may be migrated to another +* CPU, it is simplest just to extend the optimization to all CPUs. +* +* folio_set_active() would be unsafe without the lruvec lock, and +* a folio_test_clear_lru() here might cause a racing drain of the +* lru_add fbatch to skip its lru_add(): so use try_cmpxchg(). */ + lru_next =READ_ONCE (folio->lru_next); +while (lru_next &BIT (LRU_NEXT_BATCHED )) { +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +return ; +if (try_cmpxchg (&folio->lru_next, &lru_next, + lru_next |BIT (LRU_NEXT_ACTIVATE ))) +return ; + } +folio_batch_add_and_move (folio, lru_activate); }
Signature · 1 line
2.51.0
Vlastimil Babka (SUSE)
maintainerRe: [PATCH v2 07/26] mm/fbatch: LRU_NEXT_ACTIVATE bit to optimize folio_activate()
Quoted 17 lines · Hugh Dickins
Implement an equivalent to the old
__lru_cache_activate_folio()optimization, to activate a folio recently put in thelru_addfbatch, without having to put it through thelru_activatefbatch too. Neither lruvec lock nor lru bit can guard this safely and efficiently, so resort totry_cmpxchg()on a further,LRU_NEXT_ACTIVATEbit infolio->lru_next.Signed-off-by Hugh Dickins <[email protected]>--- include/linux/mm_inline.h | 4 ++++ mm/folio.c | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-)include/linux/mm_inline.h +0
-0
include/linux/mm_inline.h static inline void folio_migrate_refs(struct folio *new, const struct folio *old · line 351
Quoted 6 lines
include/linux/mm_inline.h +1
-0@@ -346,6 +346,7 @@ static inline void folio_migrate_refs(struct folio *new, const struct folio *oldenum {LRU_NEXT_NEVER_TAIL =0 ,/* Used by a tail's compound_head */ LRU_NEXT_BATCHED =1 ,/* Not used by any aligned pointer */ +LRU_NEXT_ACTIVATE ,NR_LRU_NEXT_FLAGS };
This addition, and the comment "/* This mask will do nothing on 64-bit */" in folio_add_lru()... does it mean that now this is breaking 32-bit? Should we make this optimization, or perhaps all of the cpu fbatch, 64-bit only?
Quoted 50 lines
include/linux/mm_inline.h +3
-0@@ -358,6 +359,9 @@ bool lru_add_del_folio(struct folio *folio)if (!(lru_next &BIT (LRU_NEXT_BATCHED )))return false; +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +folio_set_active (folio); +WRITE_ONCE (folio->lru.next,LIST_POISON1 );/* BUG_ON(folio->lru_next & BIT(LRU_NEXT_BATCHED)); */ mm/folio.c +20
-3@@ -256,15 +256,32 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio)void folio_activate (struct folio *folio) { +unsigned long lru_next; +if (folio_test_active (folio) ||folio_test_unevictable (folio) || !folio_test_lru (folio))return ;/* -* XXX: It is curiously difficult to recreate safely the old -* __lru_cache_activate_folio() optimization (folio_set_active() -* directly if it's on the local lru_add fbatch): revisit later. +* This optimization is intended for the common case of folio +* having been recently added to this CPU's lru_add fbatch. +* But since other CPUs can now take it at any instant (after +* a folio_test_clear_lru()), and we may be migrated to another +* CPU, it is simplest just to extend the optimization to all CPUs. +* +* folio_set_active() would be unsafe without the lruvec lock, and +* a folio_test_clear_lru() here might cause a racing drain of the +* lru_add fbatch to skip its lru_add(): so use try_cmpxchg(). */ + lru_next =READ_ONCE (folio->lru_next); +while (lru_next &BIT (LRU_NEXT_BATCHED )) { +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +return ; +if (try_cmpxchg (&folio->lru_next, &lru_next, + lru_next |BIT (LRU_NEXT_ACTIVATE ))) +return ; + } +folio_batch_add_and_move (folio, lru_activate); }
Hugh Dickins
maintainerRe: [PATCH v2 07/26] mm/fbatch: LRU_NEXT_ACTIVATE bit to optimize folio_activate()
Quoted 13 lines · Vlastimil Babka (SUSE)
Quoted 12 lines · Hugh Dickins
Implement an equivalent to the old
__lru_cache_activate_folio()optimization, to activate a folio recently put in thelru_addfbatch, without having to put it through thelru_activatefbatch too. Neither lruvec lock nor lru bit can guard this safely and efficiently, so resort totry_cmpxchg()on a further,LRU_NEXT_ACTIVATEbit infolio->lru_next.Signed-off-by Hugh Dickins <[email protected]>--- include/linux/mm_inline.h | 4 ++++ mm/folio.c | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-)
Quoted 15 lines
include/linux/mm_inline.h static inline void folio_migrate_refs(struct folio *new, const struct folio *old · line 351
Quoted 11 lines
include/linux/mm_inline.h +1
-0@@ -346,6 +346,7 @@ static inline void folio_migrate_refs(struct folio *new, const struct folio *oldenum {LRU_NEXT_NEVER_TAIL =0 ,/* Used by a tail's compound_head */ LRU_NEXT_BATCHED =1 ,/* Not used by any aligned pointer */ +LRU_NEXT_ACTIVATE ,NR_LRU_NEXT_FLAGS };This addition, and the comment "/* This mask will do nothing on 64-bit */" in
folio_add_lru()... does it mean that now this is breaking 32-bit? Should we make this optimization, or perhaps all of the cpu fbatch, 64-bit only?
Answering second question first: those could have been options, to disable the activation optimization or all per-cpu fbatching, on 32-bit; but I much preferred them not to diverge, and tried hard to keep 32-bit.
First Q: This does not, in the end, break 32-bit at all. But while I was developing, I thought it did, and reluctantly switched the encoding away from holding lru_add entry pointer in lru_next, to holding lru_add cpu and lru_add index in lru_next: from those the lru_add entry pointer can easily be calculated, and there's lots of space left over for more flag bits (which I thought might come to be needed).
But then I had that marvellous realization in folio_batch_move_lru(), that it needed to service "wrong" entries in exactly the same way as "right" entries, therefore didn't need to distinguish them, therefore didn't need any address there at all.
I have kept the address for debugging, and do put it to some use in the paranoid vmstats patch 27/26. On 32-bit, with 4-byte pointers, this LRU_NEXT_ACTIVATE bit does then leave one pointer covering two adjacent entries, when it's deciding whether the folio points back to this lru_add entry. Is it possible for two adjacent entries to hold the same folio (and so perhaps miscount the stat)? Yes, it is possible (if that folio is freed and reused and readded immediately); but so unlikely that it's of no importance when gathering stats.
Hugh
Hugh Dickins
maintainerRe: [PATCH v2 07/26] mm/fbatch: LRU_NEXT_ACTIVATE bit to optimize folio_activate()
On Sat, 12 Sep 2026, Hugh Dickins wrote:
...
Quoted 8 lines
I have kept the address for debugging, and do put it to some use in the paranoid vmstats patch 27/26. On 32-bit, with 4-byte pointers, this
LRU_NEXT_ACTIVATEbit does then leave one pointer covering two adjacent entries, when it's deciding whether the folio points back to thislru_addentry. Is it possible for two adjacent entries to hold the same folio (and so perhaps miscount the stat)? Yes, it is possible (if that folio is freed and reused and readded immediately); but so unlikely that it's of no importance when gathering stats.
It doesn't matter at all, it doesn't affect the choices made here, but I do want to correct my estimation of the likelihood of identical adjacent entries. Perhaps the most common occurrence would be, not when freed+reused+readded, but when isolate_migratepages_block() does that little lru_add_del_folio() + folio_add_lru() dance. It could be that the entry it "deletes" (by breaking the linkage from folio to fbatch) is the one most recently added to that fbatch, then folio_add_lru() will fill the next slot with that same folio.
Hugh
Kiryl Shutsemau
reviewerRe: [PATCH v2 07/26] mm/fbatch: LRU_NEXT_ACTIVATE bit to optimize folio_activate()
In reply to Hugh DickinsQuoted 59 lines · Hugh Dickins
Implement an equivalent to the old
__lru_cache_activate_folio()optimization, to activate a folio recently put in thelru_addfbatch, without having to put it through thelru_activatefbatch too. Neither lruvec lock nor lru bit can guard this safely and efficiently, so resort totry_cmpxchg()on a further,LRU_NEXT_ACTIVATEbit infolio->lru_next.Signed-off-by Hugh Dickins <[email protected]>--- include/linux/mm_inline.h | 4 ++++ mm/folio.c | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-)include/linux/mm_inline.h +4
-0@@ -346,6 +346,7 @@ static inline void folio_migrate_refs(struct folio *new, const struct folio *oldenum {LRU_NEXT_NEVER_TAIL =0 ,/* Used by a tail's compound_head */ LRU_NEXT_BATCHED =1 ,/* Not used by any aligned pointer */ +LRU_NEXT_ACTIVATE ,NR_LRU_NEXT_FLAGS };@@ -358,6 +359,9 @@ bool lru_add_del_folio(struct folio *folio)if (!(lru_next &BIT (LRU_NEXT_BATCHED )))return false; +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +folio_set_active (folio); +WRITE_ONCE (folio->lru.next,LIST_POISON1 );/* BUG_ON(folio->lru_next & BIT(LRU_NEXT_BATCHED)); */ mm/folio.c +9
-3@@ -256,15 +256,32 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio)void folio_activate (struct folio *folio) { +unsigned long lru_next; +if (folio_test_active (folio) ||folio_test_unevictable (folio) || !folio_test_lru (folio))return ;/* -* XXX: It is curiously difficult to recreate safely the old -* __lru_cache_activate_folio() optimization (folio_set_active() -* directly if it's on the local lru_add fbatch): revisit later. +* This optimization is intended for the common case of folio +* having been recently added to this CPU's lru_add fbatch. +* But since other CPUs can now take it at any instant (after +* a folio_test_clear_lru()), and we may be migrated to another +* CPU, it is simplest just to extend the optimization to all CPUs. +* +* folio_set_active() would be unsafe without the lruvec lock, and
mm/folio.c static void lru_activate(struct lruvec *lruvec, struct folio *folio) · line 282
Quoted 10 lines
mm/folio.c +9
-0+* a folio_test_clear_lru() here might cause a racing drain of the +* lru_add fbatch to skip its lru_add(): so use try_cmpxchg(). */ + lru_next =READ_ONCE (folio->lru_next); +while (lru_next &BIT (LRU_NEXT_BATCHED )) { +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +return ; +if (try_cmpxchg (&folio->lru_next, &lru_next, + lru_next |BIT (LRU_NEXT_ACTIVATE ))) +return ;
Hm. What prevents the folio from becoming unevictable under us here? I don't see anything.
__folio_add_lru() wouldn't like it:
VM_BUG_ON_FOLIO(folio_test_active(folio) && folio_test_unevictable(folio), folio);
folio_lru_list() has the VM_BUG() too.
I am not sure what the right fix is.
Maybe lru_add_del_folio() should only call folio_set_active() on !folio_test_unevictable() folios?
Or should we allow occasional active+unevictable so if they are munlocked, they will go directly to active list?
Quoted 8 lines
mm/folio.c +2
-0+ } +folio_batch_add_and_move (folio, lru_activate); }Signature · 1 line
2.51.0
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Hugh Dickins
maintainerRe: [PATCH v2 07/26] mm/fbatch: LRU_NEXT_ACTIVATE bit to optimize folio_activate()
Quoted 74 lines · Kiryl Shutsemau
Quoted 59 lines · Hugh Dickins
Implement an equivalent to the old
__lru_cache_activate_folio()optimization, to activate a folio recently put in thelru_addfbatch, without having to put it through thelru_activatefbatch too. Neither lruvec lock nor lru bit can guard this safely and efficiently, so resort totry_cmpxchg()on a further,LRU_NEXT_ACTIVATEbit infolio->lru_next.Signed-off-by Hugh Dickins <[email protected]>--- include/linux/mm_inline.h | 4 ++++ mm/folio.c | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-)include/linux/mm_inline.h +4
-0@@ -346,6 +346,7 @@ static inline void folio_migrate_refs(struct folio *new, const struct folio *oldenum {LRU_NEXT_NEVER_TAIL =0 ,/* Used by a tail's compound_head */ LRU_NEXT_BATCHED =1 ,/* Not used by any aligned pointer */ +LRU_NEXT_ACTIVATE ,NR_LRU_NEXT_FLAGS };@@ -358,6 +359,9 @@ bool lru_add_del_folio(struct folio *folio)if (!(lru_next &BIT (LRU_NEXT_BATCHED )))return false; +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +folio_set_active (folio); +WRITE_ONCE (folio->lru.next,LIST_POISON1 );/* BUG_ON(folio->lru_next & BIT(LRU_NEXT_BATCHED)); */ mm/folio.c +9
-3@@ -256,15 +256,32 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio)void folio_activate (struct folio *folio) { +unsigned long lru_next; +if (folio_test_active (folio) ||folio_test_unevictable (folio) || !folio_test_lru (folio))return ;/* -* XXX: It is curiously difficult to recreate safely the old -* __lru_cache_activate_folio() optimization (folio_set_active() -* directly if it's on the local lru_add fbatch): revisit later. +* This optimization is intended for the common case of folio +* having been recently added to this CPU's lru_add fbatch. +* But since other CPUs can now take it at any instant (after +* a folio_test_clear_lru()), and we may be migrated to another +* CPU, it is simplest just to extend the optimization to all CPUs. +* +* folio_set_active() would be unsafe without the lruvec lock, and mm/folio.c static void lru_activate(struct lruvec *lruvec, struct folio *folio) · line 282
Quoted 10 lines
mm/folio.c +9
-0+* a folio_test_clear_lru() here might cause a racing drain of the +* lru_add fbatch to skip its lru_add(): so use try_cmpxchg(). */ + lru_next =READ_ONCE (folio->lru_next); +while (lru_next &BIT (LRU_NEXT_BATCHED )) { +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +return ; +if (try_cmpxchg (&folio->lru_next, &lru_next, + lru_next |BIT (LRU_NEXT_ACTIVATE ))) +return ;Hm. What prevents the folio from becoming unevictable under us here? I don't see anything.
Quoted 6 lines
__folio_add_lru()wouldn't like it:VM_BUG_ON_FOLIO(folio_test_active(folio) && folio_test_unevictable(folio), folio);
folio_lru_list()has theVM_BUG()too.
You're right, thank you. I thought I had deleted all such VM_BUG_ONs:
and indeed I had, but only in a patch I later decided was too much for this series (removing PG_unevictable, using !folio_evictable() in some places, or folio_test_unevictable() testing another POISON in lru_next).
That excuse is not enough for this series! Yes, I must send a fixup, but not today.
Quoted 7 lines
I am not sure what the right fix is.
Maybe
lru_add_del_folio()should only callfolio_set_active()on !folio_test_unevictable() folios?Or should we allow occasional active+unevictable
Yes, that's what I did, just removed the VM_BUG_ONs: but I'll need to check again whether that other patch also had to fix any ordering of checks. Offhand, probably not: once the "Unevictable LRU" became an oopsing fiction, it was important to check unevictable first:
unevictable must take precedence, and then it really doesn't matter whether active is set or not.
Quoted 2 lines
so if they are
munlocked, they will go directly to active list?
I didn't think of that, but I don't think that "active", set racily back when the folio was assigned "unevictable", bears much relation to whether it ought to be put on active or inactive list when later made evictable again. We should probably be consistent, and consistent with existing behaviour, that they go to inactive when made evictable. (I'm not looking at that other patch at present, I don't recall where active got cleared in it.)
Hugh
Hugh Dickins
maintainerRe: [PATCH v2 07/26] mm/fbatch: LRU_NEXT_ACTIVATE bit to optimize folio_activate()
Quoted 2 lines · Hugh Dickins
Quoted 1 line · Kiryl Shutsemau
On Wed, Sep 09, 2026 at 02:55:41AM -0700, Hugh Dickins wrote:
...
Quoted 68 lines
Quoted 61 lines
Quoted 47 lines
include/linux/mm_inline.h +4
-0@@ -346,6 +346,7 @@ static inline void folio_migrate_refs(struct folio *new, const struct folio *oldenum {LRU_NEXT_NEVER_TAIL =0 ,/* Used by a tail's compound_head */ LRU_NEXT_BATCHED =1 ,/* Not used by any aligned pointer */ +LRU_NEXT_ACTIVATE ,NR_LRU_NEXT_FLAGS };@@ -358,6 +359,9 @@ bool lru_add_del_folio(struct folio *folio)if (!(lru_next &BIT (LRU_NEXT_BATCHED )))return false; +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +folio_set_active (folio); +WRITE_ONCE (folio->lru.next,LIST_POISON1 );/* BUG_ON(folio->lru_next & BIT(LRU_NEXT_BATCHED)); */ mm/folio.c +9
-3@@ -256,15 +256,32 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio)void folio_activate (struct folio *folio) { +unsigned long lru_next; +if (folio_test_active (folio) ||folio_test_unevictable (folio) || !folio_test_lru (folio))return ;/* -* XXX: It is curiously difficult to recreate safely the old -* __lru_cache_activate_folio() optimization (folio_set_active() -* directly if it's on the local lru_add fbatch): revisit later. +* This optimization is intended for the common case of folio +* having been recently added to this CPU's lru_add fbatch. +* But since other CPUs can now take it at any instant (after +* a folio_test_clear_lru()), and we may be migrated to another +* CPU, it is simplest just to extend the optimization to all CPUs. +* +* folio_set_active() would be unsafe without the lruvec lock, and mm/folio.c static void lru_activate(struct lruvec *lruvec, struct folio *folio) · line 282
Quoted 10 lines
mm/folio.c +9
-0+* a folio_test_clear_lru() here might cause a racing drain of the +* lru_add fbatch to skip its lru_add(): so use try_cmpxchg(). */ + lru_next =READ_ONCE (folio->lru_next); +while (lru_next &BIT (LRU_NEXT_BATCHED )) { +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +return ; +if (try_cmpxchg (&folio->lru_next, &lru_next, + lru_next |BIT (LRU_NEXT_ACTIVATE ))) +return ;Hm. What prevents the folio from becoming unevictable under us here? I don't see anything.
Quoted 6 lines
__folio_add_lru()wouldn't like it:VM_BUG_ON_FOLIO(folio_test_active(folio) && folio_test_unevictable(folio), folio);
folio_lru_list()has theVM_BUG()too.
Quoted 13 lines
You're right, thank you. I thought I had deleted all such
VM_BUG_ONs:
and indeed I had, but only in a patch I later decided was too much for this series (removingPG_unevictable, using !folio_evictable() in some places, orfolio_test_unevictable()testing another POISON inlru_next).That excuse is not enough for this series! Yes, I must send a fixup, but not today.
Quoted 5 lines
I am not sure what the right fix is.
Maybe
lru_add_del_folio()should only callfolio_set_active()on !folio_test_unevictable() folios?
I was writing the commit message to a 7.1/26 fixup patch, when I found I just could not describe any possible race here.
(And I was using your first suggestion, above: in the longer term I prefer what I chose below, but decided it was better not to get into that now: deleting various VM_BUG_ON_FOLIOs is better argued elsewhere. There's another of them in folio_migrate_flags().)
folio_activate() has just checked !folio_test_unevictable(), so it would have to be a race with something which sets the unevictable flag on this folio at the same time as we find it's LRU_NEXT_BATCHED.
!folio_evictable() might become true at any instant, but folio_test_unevictable()?
I cannot see what the racer could be: can you? I can see lru_add() making it unevictable afterwards; and I can see folio migration (successful or not) carrying unevictable forwards (or setting it on a freshly allocated folio). But I cannot see any risky race for folio_activate() or folio_mark_accessed() here.
Hugh
Quoted 22 lines
Quoted 2 lines
Or should we allow occasional active+unevictable
Yes, that's what I did, just removed the
VM_BUG_ONs: but I'll need to check again whether that other patch also had to fix any ordering of checks. Offhand, probably not: once the "Unevictable LRU" became an oopsing fiction, it was important to check unevictable first:
unevictable must take precedence, and then it really doesn't matter whether active is set or not.Quoted 2 lines
so if they are
munlocked, they will go directly to active list?I didn't think of that, but I don't think that "active", set racily back when the folio was assigned "unevictable", bears much relation to whether it ought to be put on active or inactive list when later made evictable again. We should probably be consistent, and consistent with existing behaviour, that they go to inactive when made evictable. (I'm not looking at that other patch at present, I don't recall where active got cleared in it.)
Hugh
Kiryl Shutsemau
reviewerRe: [PATCH v2 07/26] mm/fbatch: LRU_NEXT_ACTIVATE bit to optimize folio_activate()
Quoted 98 lines · Hugh Dickins
Quoted 2 lines · Hugh Dickins
Quoted 1 line · Kiryl Shutsemau
On Wed, Sep 09, 2026 at 02:55:41AM -0700, Hugh Dickins wrote:
...
Quoted 68 lines
Quoted 61 lines
Quoted 47 lines
include/linux/mm_inline.h +4
-0@@ -346,6 +346,7 @@ static inline void folio_migrate_refs(struct folio *new, const struct folio *oldenum {LRU_NEXT_NEVER_TAIL =0 ,/* Used by a tail's compound_head */ LRU_NEXT_BATCHED =1 ,/* Not used by any aligned pointer */ +LRU_NEXT_ACTIVATE ,NR_LRU_NEXT_FLAGS };@@ -358,6 +359,9 @@ bool lru_add_del_folio(struct folio *folio)if (!(lru_next &BIT (LRU_NEXT_BATCHED )))return false; +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +folio_set_active (folio); +WRITE_ONCE (folio->lru.next,LIST_POISON1 );/* BUG_ON(folio->lru_next & BIT(LRU_NEXT_BATCHED)); */ mm/folio.c +9
-3@@ -256,15 +256,32 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio)void folio_activate (struct folio *folio) { +unsigned long lru_next; +if (folio_test_active (folio) ||folio_test_unevictable (folio) || !folio_test_lru (folio))return ;/* -* XXX: It is curiously difficult to recreate safely the old -* __lru_cache_activate_folio() optimization (folio_set_active() -* directly if it's on the local lru_add fbatch): revisit later. +* This optimization is intended for the common case of folio +* having been recently added to this CPU's lru_add fbatch. +* But since other CPUs can now take it at any instant (after +* a folio_test_clear_lru()), and we may be migrated to another +* CPU, it is simplest just to extend the optimization to all CPUs. +* +* folio_set_active() would be unsafe without the lruvec lock, and mm/folio.c static void lru_activate(struct lruvec *lruvec, struct folio *folio) · line 282
Quoted 10 lines
mm/folio.c +9
-0+* a folio_test_clear_lru() here might cause a racing drain of the +* lru_add fbatch to skip its lru_add(): so use try_cmpxchg(). */ + lru_next =READ_ONCE (folio->lru_next); +while (lru_next &BIT (LRU_NEXT_BATCHED )) { +if (lru_next &BIT (LRU_NEXT_ACTIVATE )) +return ; +if (try_cmpxchg (&folio->lru_next, &lru_next, + lru_next |BIT (LRU_NEXT_ACTIVATE ))) +return ;Hm. What prevents the folio from becoming unevictable under us here? I don't see anything.
Quoted 6 lines
__folio_add_lru()wouldn't like it:VM_BUG_ON_FOLIO(folio_test_active(folio) && folio_test_unevictable(folio), folio);
folio_lru_list()has theVM_BUG()too.Quoted 13 lines
You're right, thank you. I thought I had deleted all such
VM_BUG_ONs:
and indeed I had, but only in a patch I later decided was too much for this series (removingPG_unevictable, using !folio_evictable() in some places, orfolio_test_unevictable()testing another POISON inlru_next).That excuse is not enough for this series! Yes, I must send a fixup, but not today.
Quoted 5 lines
I am not sure what the right fix is.
Maybe
lru_add_del_folio()should only callfolio_set_active()on !folio_test_unevictable() folios?I was writing the commit message to a 7.1/26 fixup patch, when I found I just could not describe any possible race here.
(And I was using your first suggestion, above: in the longer term I prefer what I chose below, but decided it was better not to get into that now: deleting various
VM_BUG_ON_FOLIOsis better argued elsewhere. There's another of them infolio_migrate_flags().)
folio_activate()has just checked !folio_test_unevictable(), so it would have to be a race with something which sets the unevictable flag on this folio at the same time as we find it'sLRU_NEXT_BATCHED.
Quoted 8 lines
!folio_evictable() might become true at any instant, but
folio_test_unevictable()?I cannot see what the racer could be: can you? I can see
lru_add()making it unevictable afterwards; and I can see folio migration (successful or not) carrying unevictable forwards (or setting it on a freshly allocated folio). But I cannot see any risky race forfolio_activate()orfolio_mark_accessed()here.
I was thinking of race with mlock() plus a failed compaction (assuming compact_unevictable_allowed is 1):
CPU0 CPU1 folio_activate() folio_test_unevictable() == false folio_test_lru() == true __mlock_folio() folio_set_unevictable() <moves to LRU_UNEVICTABLE> ... compact_zone() isolate_migratepages() isolate_migratepages_block() folio_test_clear_lru() lruvec_del_folio() <migration fails> putback_movable_pages() folio_putback_lru() __folio_add_lru() // !active, unevictable lru_next |= BIT(LRU_NEXT_BATCHED); folio_set_lru()
lru_next = READ_ONCE(lru_next)
// lru_next has LRU_NEXT_BATCHED try_cmpxchg(lru_next | LRU_NEXT_ACTIVATE) == true
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Hugh Dickins
maintainerPATCH v2 8/26 mm/fbatch: replace mlock_new_folio() by __folio_add_lru(,mlockit)
In reply to Hugh DickinsReplace mlock_new_folio(), working on mm/mlock.c's mlock_fbatch, by __folio_add_lru(,mlockit), working on mm/folio.c's lru_add fbatch:
folio moved to lruvec by lru_add(), with its mlocking incidental.
Remove old comment about not needing smp_mb__after_atomic() from lru_add(): but that is a detail which will need to be reconsidered.
Initialize mlock_count earlier, when adding to fbatch rather than when adding to lruvec. mlock_count count in 2s, with the low bit set to distinguish it from lru.prev. This helps when an mlocked folio is put back early by compaction, but will enable further optimization next.
Change mlock_count from unsigned int to long: long to match pointer without endian concerns, signed for better treatment of those rare cases when final munlocks precede their still batched mlocks.
This is an intermediate, poorly tested review stage: mlock_new_folio() code removed from mm/mlock.c, remaining code there updated to respect the new mlock_count accounting, but not considered beyond that.
--- Documentation/mm/unevictable-lru.rst | 2 +- include/linux/mm_types.h | 10 +++-- include/linux/swap.h | 6 ++- mm/folio.c | 59 ++++++++++++------------ mm/huge_memory.c | 2 +- mm/internal.h | 4 +- mm/mlock.c | 67 ++++------------------------ 7 files changed, 53 insertions(+), 97 deletions(-)
Documentation/mm/unevictable-lru.rst +1 -1
@@ -314,7 +314,7 @@ For each PTE (or PMD) being faulted into a VMA, the page add rmap function calls mlock_vma_folio(), which calls mlock_folio() when the VMA is VM_LOCKED (unless it is a PTE mapping of a part of a transparent huge page). Or when it is a newly allocated anonymous page, folio_add_lru_vma() calls -mlock_new_folio() instead: similar to mlock_folio(), but can make better +__folio_add_lru(mlockit) instead: similar to mlock_folio(), but can make better judgments, since this page is held exclusively and known not to be on LRU yet. mlock_folio() sets PG_mlocked immediately, then places the page on the CPU's
include/linux/mm_types.h +7 -3
@@ -86,7 +86,7 @@ struct page {* means the other users of this union MUST NOT use the bit to * avoid collision and false-positive PageTail(). * Bit 1 of the first word is used by page_is_pfmemalloc(). -* Bit 1 of the first word (lru_next) is also used by folio_add_lru(). +* Bit 1 of the first word (lru_next) is also used by __folio_add_lru(). */ union { struct { /* Page cache and anonymous pages */
@@ -361,7 +361,7 @@ typedef unsigned short mm_id_t;* struct folio - Represents a contiguous set of bytes. * @flags: Identical to the page flags. * @lru: Least Recently Used list; tracks how recently this folio was used. -* @mlock_count: Number of times this folio has been pinned by mlock(). +* @mlock_count: Number of times this folio has been pinned by mlock() *2 +1 * @mapping: The file this page belongs to, or refers to the anon_vma for * anonymous memory. * @index: Offset within the file, in units of pages. For anonymous memory,
@@ -415,7 +415,7 @@ struct folio {struct {unsigned long lru_next;/* public: */ -unsigned int mlock_count; +long mlock_count;/* private: */ };/* public: */
@@ -509,6 +509,10 @@ struct folio { }; }; +/* folio's mlock_count is doubled, low bit set to distinguish from lru.prev */ +#define MLOCK_COUNT_0 1/* Bit not set in any aligned pointer */ +#define MLOCK_COUNT_1 2/* Increment or decrement mlock_count */ +#define FOLIO_MATCH (pg, fl) \ static_assert(offsetof(struct page, pg) == offsetof(struct folio, fl))FOLIO_MATCH (flags, flags);
include/linux/swap.h +5 -1
@@ -294,7 +294,11 @@ extern unsigned long totalreserve_pages;#define nr_free_pages () global_zone_page_state(NR_FREE_PAGES)/* linux/mm/folio.c */ -void folio_add_lru (struct folio *folio); +void __folio_add_lru (struct folio *folio,bool mlockit); +static inline void folio_add_lru (struct folio *folio) +{ +__folio_add_lru (folio, false); +}void folio_mark_accessed (struct folio *folio);void lru_add_drain_all (void );
mm/folio.c +29 -30
105 lines not on this page · raw message
mm/huge_memory.c +1 -1
@@ -3623,7 +3623,7 @@ static void lru_add_split_folio(struct folio *folio, struct folio *new_folio,/* head is still on lru (and we have it frozen) */ VM_WARN_ON (!folio_test_lru (folio));if (folio_test_unevictable (folio)) - new_folio->mlock_count =0 ; + new_folio->mlock_count =MLOCK_COUNT_0 ;else list_add_tail (&new_folio->lru, &folio->lru);folio_set_lru (new_folio);
mm/internal.h +1 -3
@@ -974,7 +974,7 @@ folio_within_vma(struct folio *folio, struct vm_area_struct *vma)* * mlock is usually called at the end of folio_add_*_rmap_*(), munlock at * the end of folio_remove_rmap_*(); but new anon folios are managed by -* folio_add_lru_vma() calling mlock_new_folio(). +* folio_add_lru_vma() calling __folio_add_lru(). */ void mlock_folio (struct folio *folio); static inline void mlock_vma_folio(struct folio *folio,
@@ -1009,7 +1009,6 @@ static inline void munlock_vma_folio(struct folio *folio,munlock_folio (folio); } -void mlock_new_folio (struct folio *folio);bool need_mlock_drain (int cpu);void mlock_drain_local (void );void mlock_drain_remote (int cpu);
@@ -1136,7 +1135,6 @@ static inline bool vma_supports_mlock(const struct vm_area_struct *vma)#else /* !CONFIG_MMU */ static inline void unmap_mapping_folio (struct folio *folio) { } -static inline void mlock_new_folio (struct folio *folio) { }static inline bool need_mlock_drain (int cpu) {return false; }static inline void mlock_drain_local (void ) { }static inline void mlock_drain_remote (int cpu) { }
mm/mlock.c +9 -58
118 lines not on this page · raw message
Signature · 1 line
2.51.0
Re: [PATCH v2 08/26] mm/fbatch: replace mlock_new_folio() by __folio_add_lru(,mlockit)
Quoted 16 lines · Hugh Dickins
Replace
mlock_new_folio(), working on mm/mlock.c'smlock_fbatch, by__folio_add_lru(,mlockit), working on mm/folio.c'slru_addfbatch:
folio moved to lruvec bylru_add(), with its mlocking incidental.Remove old comment about not needing
smp_mb__after_atomic()fromlru_add(): but that is a detail which will need to be reconsidered.Initialize
mlock_countearlier, when adding to fbatch rather than when adding to lruvec.mlock_countcount in 2s, with the low bit set to distinguish it from lru.prev. This helps when an mlocked folio is put back early by compaction, but will enable further optimization next.Change
mlock_countfrom unsigned int to long: long to match pointer without endian concerns, signed for better treatment of those rare cases when final munlocks precede their still batched mlocks.
Quoted 5 lines
This is an intermediate, poorly tested review stage:
mlock_new_folio()code removed frommm/mlock.c, remaining code there updated to respect the newmlock_countaccounting, but not considered beyond that.Signed-off-by Hugh Dickins <[email protected]>
Hugh Dickins
maintainerPATCH v2 9/26 mm/fbatch: restore mlock+munlock batching, without extra ref
In reply to Hugh DickinsUpdate mlock_folio(), munlock_folio() and their fbatch callouts and helpers, to do folio_try_get()s at batch processing time, instead of holding a folio reference all the while in mlock_fbatch: as in folio.c.
But more interesting is the use of mod_mlock_count(), using try_cmpxchg() to update folio->mlock_count safely when possible (now when on lru_add fbatch as well as when unevictable). While __mlock_folio() is as hard to think about as before, __munlock_folio() simpler because munlock_folio() can adjust mlock_count itself without clear_lru() or lruvec lock, and so do the folio_test_clear_mlocked() immediately for itself (without which unevictable_pgs_cleared was likely to appear high, when it should be 0 or low to indicate good mlock health).
__munlock_folio() is safe for use even when the unreferenced folio has been freed and reused. It appears that __mlock_folio() could affect a folio which has been freed and reused, but only if it is reused as an mlocked folio, in which case its mlock_count is spuriously incremented (but usually a spurious munlock decrement will follow). How grave is this? If unevictable_pgs_cleared remains low, not so bad.
I've gone back and forth on whether to move mlock_fbatch and these functions into mm/folio.c: for now they stay here in mm/mlock.c.
--- mm/mlock.c | 147 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 86 insertions(+), 61 deletions(-)
mm/mlock.c +86 -61
@@ -58,6 +58,20 @@ EXPORT_SYMBOL(can_do_mlock);* indicate the unevictable state. */ +static long mod_mlock_count (struct folio *folio,long incdec) +{ +long mlock_count =READ_ONCE (folio->mlock_count); + +while (mlock_count &MLOCK_COUNT_0 ) { +if (mlock_count + incdec <MLOCK_COUNT_0 ) +return MLOCK_COUNT_0 ; +if (try_cmpxchg (&folio->mlock_count, &mlock_count, + mlock_count + incdec)) +return mlock_count + incdec; + } +return 0 ; +} +static struct lruvec *__mlock_folio (struct folio *folio,struct lruvec *lruvec) {/* There is nothing more we can do while it's off LRU */
@@ -65,6 +79,7 @@ static struct lruvec *__mlock_folio(struct folio *folio, struct lruvec *lruvec)return lruvec; lruvec =folio_lruvec_relock_irq (folio, lruvec); +lruvec_del_folio (lruvec, folio);if (unlikely (folio_evictable (folio))) {/*
@@ -73,92 +88,82 @@ static struct lruvec *__mlock_folio(struct folio *folio, struct lruvec *lruvec)* folio be unevictable? I'm not sure, but move it now if so. */ if (folio_test_unevictable (folio)) { -lruvec_del_folio (lruvec, folio);folio_clear_unevictable (folio); -lruvec_add_folio (lruvec, folio); -__count_vm_events (UNEVICTABLE_PGRESCUED ,folio_nr_pages (folio)); } goto out; } +/* +* Something to keep in mind when studying the arithmetic here: +* we only come to __mlock_folio() when mlock_folio() could not +* mod_mlock_count() itself; but by the time this is processed, +* the folio may have already been munlocked, or another mlock +* already marked it as unevictable and so mod_mlock_countable. +* And don't forget that a folio may be unevictable for reasons +* other than mlocked (hence the folio_evictable() check above). +*/ +if (folio_test_unevictable (folio)) {if (folio_test_mlocked (folio)) - folio->mlock_count +=MLOCK_COUNT_1 ; +mod_mlock_count (folio,MLOCK_COUNT_1 ); goto out; } -lruvec_del_folio (lruvec, folio);folio_clear_active (folio);folio_set_unevictable (folio); - folio->mlock_count =MLOCK_COUNT_0 ; -if (folio_test_mlocked (folio)) - folio->mlock_count +=MLOCK_COUNT_1 ; -lruvec_add_folio (lruvec, folio);__count_vm_events (UNEVICTABLE_PGCULLED ,folio_nr_pages (folio)); + +if (!folio_test_mlocked (folio)) + folio->mlock_count =MLOCK_COUNT_0 ; +else if (!mod_mlock_count (folio,MLOCK_COUNT_1 )) + folio->mlock_count =MLOCK_COUNT_0 +MLOCK_COUNT_1 ; out: +lruvec_add_folio (lruvec, folio);folio_set_lru (folio);return lruvec; }static struct lruvec *__munlock_folio (struct folio *folio,struct lruvec *lruvec) { -int nr_pages =folio_nr_pages (folio); -bool isolated = false; +long nr_pages =folio_nr_pages (folio); -if (!folio_test_clear_lru (folio)) - goto munlock; - - isolated = true; - lruvec =folio_lruvec_relock_irq (folio, lruvec); - -if (folio_test_unevictable (folio)) { -/* Then mlock_count is maintained, but might undercount */ -if (folio->mlock_count >MLOCK_COUNT_0 ) - folio->mlock_count -=MLOCK_COUNT_1 ; -if (folio->mlock_count >MLOCK_COUNT_0 ) - goto out; - } -/* else assume that was the last mlock: reclaim will fix it if not */ - -munlock: -if (folio_test_clear_mlocked (folio)) { -zone_stat_mod_folio (folio,NR_MLOCK , -nr_pages); -if (isolated || !folio_test_unevictable (folio)) -__count_vm_events (UNEVICTABLE_PGMUNLOCKED , nr_pages); -else +/* There is nothing more we can do while it's off LRU */ +if (!folio_test_clear_lru (folio)) { +if (folio_test_unevictable (folio) &&folio_evictable (folio))__count_vm_events (UNEVICTABLE_PGSTRANDED , nr_pages); +/* But whoever puts it back on LRU should rescue it */ +return lruvec; } -/* folio_evictable() has to be checked *after* clearing Mlocked */ -if (isolated &&folio_test_unevictable (folio) &&folio_evictable (folio)) { -lruvec_del_folio (lruvec, folio); + lruvec =folio_lruvec_relock_irq (folio, lruvec); +lruvec_del_folio (lruvec, folio); + +if (folio_test_unevictable (folio) &&folio_evictable (folio)) {folio_clear_unevictable (folio); -lruvec_add_folio (lruvec, folio);__count_vm_events (UNEVICTABLE_PGRESCUED , nr_pages); } -out: -if (isolated) -folio_set_lru (folio); + +lruvec_add_folio (lruvec, folio); +folio_set_lru (folio);return lruvec; }/* -* Flags held in the low bits of a struct folio pointer on the mlock_fbatch. +* Flag held in the low bits of a struct folio pointer on the mlock_fbatch. */ -#define LRU_FOLIO 0x1 -static inline struct folio *mlock_lru (struct folio *folio) +#define MLOCK_FLAG 0x1 +static inline struct folio *mlock_flagged (struct folio *folio) { -return (struct folio *)((unsigned long )folio +LRU_FOLIO ); +return (struct folio *)((unsigned long )folio +MLOCK_FLAG ); }/* * mlock_folio_batch() is derived from folio_batch_move_lru(): perhaps that can * make use of such folio pointer flags in future, but for now just keep it for -* mlock. We could use three separate folio batches instead, but one feels -* better (munlocking a full folio batch does not need to drain mlocking folio -* batches first). +* mlock. We could use separate folio batches instead, but one feels better +* (munlocking a full folio batch does not need to drain mlocking batch first). */ static void mlock_folio_batch (struct folio_batch *fbatch) {
@@ -169,10 +174,15 @@ static void mlock_folio_batch(struct folio_batch *fbatch)for (i =0 ; i <folio_batch_count (fbatch); i++) { folio = fbatch->folios[i]; - mlock = (unsigned long )folio &LRU_FOLIO ; + mlock = (unsigned long )folio &MLOCK_FLAG ; folio = (struct folio *)((unsigned long )folio - mlock); fbatch->folios[i] = folio; +if (!folio_try_get (folio)) { + fbatch->folios[i] =NULL ; +continue ; + } +if (mlock) lruvec =__mlock_folio (folio, lruvec);else
@@ -218,19 +228,25 @@ void mlock_folio(struct folio *folio) {struct folio_batch *fbatch; -local_lock (&mlock_fbatch.lock); - fbatch =this_cpu_ptr (&mlock_fbatch.fbatch); -if (!folio_test_set_mlocked (folio)) { -int nr_pages =folio_nr_pages (folio); +long nr_pages =folio_nr_pages (folio);zone_stat_mod_folio (folio,NR_MLOCK , nr_pages); -__count_vm_events (UNEVICTABLE_PGMLOCKED , nr_pages); +count_vm_events (UNEVICTABLE_PGMLOCKED , nr_pages); } -folio_get (folio); -if (!folio_batch_add (fbatch,mlock_lru (folio)) || - true ||/* XXX Temporarily disable mlock batching */ +/* +* No more to do if mlock_count is maintained: either the folio +* is on an lru_add fbatch, and will be moved to unevictable in +* due course, or it's already counted as unevictable: no need +* for an mlock_fbatch entry below. +*/ +if (mod_mlock_count (folio,MLOCK_COUNT_1 )) +return ; + +local_lock (&mlock_fbatch.lock); + fbatch =this_cpu_ptr (&mlock_fbatch.fbatch); +if (!folio_batch_add (fbatch,mlock_flagged (folio)) || !folio_may_be_lru_cached (folio) ||lru_cache_disabled ())mlock_folio_batch (fbatch);local_unlock (&mlock_fbatch.lock);
@@ -244,15 +260,24 @@ void munlock_folio(struct folio *folio) {struct folio_batch *fbatch; +/* +* No more to do if mlock_count is maintained and still raised. +* But if mlock_count is unmaintained, we might need to queue an +* munlock fbatch entry, just to cancel an undequeued mlock entry? +*/ +if (mod_mlock_count (folio, -MLOCK_COUNT_1 ) >MLOCK_COUNT_0 ) +return ; + +if (folio_test_clear_mlocked (folio)) { +long nr_pages =folio_nr_pages (folio); + +zone_stat_mod_folio (folio,NR_MLOCK , -nr_pages); +count_vm_events (UNEVICTABLE_PGMUNLOCKED , nr_pages); + } +local_lock (&mlock_fbatch.lock); fbatch =this_cpu_ptr (&mlock_fbatch.fbatch); -/* -* folio_test_clear_mlocked(folio) must be left to __munlock_folio(), -* which will check whether the folio is multiply mlocked. -*/ -folio_get (folio);if (!folio_batch_add (fbatch, folio) || - true ||/* XXX Temporarily disable munlock batching */ !folio_may_be_lru_cached (folio) ||lru_cache_disabled ())mlock_folio_batch (fbatch);local_unlock (&mlock_fbatch.lock);
Signature · 1 line
2.51.0
Vlastimil Babka (SUSE)
maintainerRe: [PATCH v2 09/26] mm/fbatch: restore mlock+munlock batching, without extra ref
Quoted 37 lines · Hugh Dickins
Update
mlock_folio(),munlock_folio()and their fbatch callouts and helpers, to do folio_try_get()s at batch processing time, instead of holding a folio reference all the while inmlock_fbatch: as infolio.c.But more interesting is the use of
mod_mlock_count(), usingtry_cmpxchg()to updatefolio->mlock_countsafely when possible (now when onlru_addfbatch as well as when unevictable). While__mlock_folio()is as hard to think about as before,__munlock_folio()simpler becausemunlock_folio()can adjustmlock_countitself withoutclear_lru()or lruvec lock, and so do thefolio_test_clear_mlocked()immediately for itself (without whichunevictable_pgs_clearedwas likely to appear high, when it should be 0 or low to indicate good mlock health).
__munlock_folio()is safe for use even when the unreferenced folio has been freed and reused. It appears that__mlock_folio()could affect a folio which has been freed and reused, but only if it is reused as an mlocked folio, in which case itsmlock_countis spuriously incremented (but usually a spurious munlock decrement will follow). How grave is this? Ifunevictable_pgs_clearedremains low, not so bad.I've gone back and forth on whether to move
mlock_fbatchand these functions intomm/folio.c: for now they stay here inmm/mlock.c.Signed-off-by Hugh Dickins <[email protected]>--- mm/mlock.c | 147 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 86 insertions(+), 61 deletions(-)mm/mlock.c +1
-0@@ -58,6 +58,20 @@ EXPORT_SYMBOL(can_do_mlock);* indicate the unevictable state. */ +static long mod_mlock_count (struct folio *folio,long incdec)
mm/mlock.c EXPORT_SYMBOL(can_do_mlock); · line 71
Quoted 10 lines
mm/mlock.c +10
-0+{ +long mlock_count =READ_ONCE (folio->mlock_count); + +while (mlock_count &MLOCK_COUNT_0 ) { +if (mlock_count + incdec <MLOCK_COUNT_0 ) +return MLOCK_COUNT_0 ; +if (try_cmpxchg (&folio->mlock_count, &mlock_count, + mlock_count + incdec)) +return mlock_count + incdec; + }
This never rereads folio->mlock_count to mlock_count inside the loop, so it can spin forever?
(too late here for me to understand the rest today)
Quoted 3 lines
mm/mlock.c +3
-0+return 0 ; +} +
Hugh Dickins
maintainerRe: [PATCH v2 09/26] mm/fbatch: restore mlock+munlock batching, without extra ref
Quoted 37 lines · Vlastimil Babka (SUSE)
Quoted 36 lines · Hugh Dickins
Update
mlock_folio(),munlock_folio()and their fbatch callouts and helpers, to do folio_try_get()s at batch processing time, instead of holding a folio reference all the while inmlock_fbatch: as infolio.c.But more interesting is the use of
mod_mlock_count(), usingtry_cmpxchg()to updatefolio->mlock_countsafely when possible (now when onlru_addfbatch as well as when unevictable). While__mlock_folio()is as hard to think about as before,__munlock_folio()simpler becausemunlock_folio()can adjustmlock_countitself withoutclear_lru()or lruvec lock, and so do thefolio_test_clear_mlocked()immediately for itself (without whichunevictable_pgs_clearedwas likely to appear high, when it should be 0 or low to indicate good mlock health).
__munlock_folio()is safe for use even when the unreferenced folio has been freed and reused. It appears that__mlock_folio()could affect a folio which has been freed and reused, but only if it is reused as an mlocked folio, in which case itsmlock_countis spuriously incremented (but usually a spurious munlock decrement will follow). How grave is this? Ifunevictable_pgs_clearedremains low, not so bad.I've gone back and forth on whether to move
mlock_fbatchand these functions intomm/folio.c: for now they stay here inmm/mlock.c.Signed-off-by Hugh Dickins <[email protected]>--- mm/mlock.c | 147 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 86 insertions(+), 61 deletions(-)mm/mlock.c +0
-0@@ -58,6 +58,20 @@ EXPORT_SYMBOL(can_do_mlock);* indicate the unevictable state. */
Quoted 14 lines
mm/mlock.c EXPORT_SYMBOL(can_do_mlock); · line 71
Quoted 11 lines
mm/mlock.c +11
-0+static long mod_mlock_count (struct folio *folio,long incdec) +{ +long mlock_count =READ_ONCE (folio->mlock_count); + +while (mlock_count &MLOCK_COUNT_0 ) { +if (mlock_count + incdec <MLOCK_COUNT_0 ) +return MLOCK_COUNT_0 ; +if (try_cmpxchg (&folio->mlock_count, &mlock_count, + mlock_count + incdec)) +return mlock_count + incdec; + }This never rereads
folio->mlock_counttomlock_countinside the loop, so it can spin forever?
You give me a nasty moment, have I misunderstood? Isn't it part of the try_cmpxchg() contract, that it reads folio->mlock_count into mlock_count when it fails? Hence the "&mlock_count" rather than just "mlock_count"?
Quoted 2 lines
(too late here for me to understand the rest today)
But that I understand very weil: thanks for all that you have managed.
Hugh
Vlastimil Babka (SUSE)
maintainerRe: [PATCH v2 09/26] mm/fbatch: restore mlock+munlock batching, without extra ref
Quoted 53 lines · Hugh Dickins
Quoted 37 lines · Vlastimil Babka (SUSE)
Quoted 36 lines · Hugh Dickins
Update
mlock_folio(),munlock_folio()and their fbatch callouts and helpers, to do folio_try_get()s at batch processing time, instead of holding a folio reference all the while inmlock_fbatch: as infolio.c.But more interesting is the use of
mod_mlock_count(), usingtry_cmpxchg()to updatefolio->mlock_countsafely when possible (now when onlru_addfbatch as well as when unevictable). While__mlock_folio()is as hard to think about as before,__munlock_folio()simpler becausemunlock_folio()can adjustmlock_countitself withoutclear_lru()or lruvec lock, and so do thefolio_test_clear_mlocked()immediately for itself (without whichunevictable_pgs_clearedwas likely to appear high, when it should be 0 or low to indicate good mlock health).
__munlock_folio()is safe for use even when the unreferenced folio has been freed and reused. It appears that__mlock_folio()could affect a folio which has been freed and reused, but only if it is reused as an mlocked folio, in which case itsmlock_countis spuriously incremented (but usually a spurious munlock decrement will follow). How grave is this? Ifunevictable_pgs_clearedremains low, not so bad.I've gone back and forth on whether to move
mlock_fbatchand these functions intomm/folio.c: for now they stay here inmm/mlock.c.Signed-off-by Hugh Dickins <[email protected]>--- mm/mlock.c | 147 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 86 insertions(+), 61 deletions(-)mm/mlock.c +0
-0@@ -58,6 +58,20 @@ EXPORT_SYMBOL(can_do_mlock);* indicate the unevictable state. */ Quoted 14 lines
mm/mlock.c EXPORT_SYMBOL(can_do_mlock); · line 71
Quoted 11 lines
mm/mlock.c +11
-0+static long mod_mlock_count (struct folio *folio,long incdec) +{ +long mlock_count =READ_ONCE (folio->mlock_count); + +while (mlock_count &MLOCK_COUNT_0 ) { +if (mlock_count + incdec <MLOCK_COUNT_0 ) +return MLOCK_COUNT_0 ; +if (try_cmpxchg (&folio->mlock_count, &mlock_count, + mlock_count + incdec)) +return mlock_count + incdec; + }This never rereads
folio->mlock_counttomlock_countinside the loop, so it can spin forever?
Quoted 3 lines
You give me a nasty moment, have I misunderstood? Isn't it part of the
try_cmpxchg()contract, that it readsfolio->mlock_countintomlock_countwhen it fails? Hence the "&mlock_count" rather than just "mlock_count"?
Indeed, sorry! It would have helped if the contract was spelled out somewhere. But now I found it only for the atomic_try_cmpxchg() variant. Guess I'm too used to the slab_update_freelist() wrappers which don't follow this... but maybe should? Hmm.
Quoted 6 lines
Quoted 2 lines
(too late here for me to understand the rest today)
But that I understand very weil: thanks for all that you have managed.
Hugh
Hugh Dickins
maintainerPATCH v2 10/26 mm/fbatch: remove several uses of mlock_drain_local()
In reply to Hugh DickinsRemove several uses of mlock_drain_local(): they were workarounds for when that sequence of operations tended to leave folios in mlock_fbatch with debilitating raised refcount (and no good if the task changed CPU).
--- mm/huge_memory.c | 2 -- mm/migrate.c | 2 -- mm/rmap.c | 4 ---- 3 files changed, 8 deletions(-)
mm/huge_memory.c +0 -2
@@ -3570,8 +3570,6 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,folio_remove_rmap_pmd (folio,pmd_page (orig_pmd), vma);zap_deposited_table (mm, pmdp);add_mm_counter (mm,MM_ANONPAGES , -HPAGE_PMD_NR ); -if (vma->vm_flags &VM_LOCKED ) -mlock_drain_local ();folio_put (folio);return true;
mm/migrate.c +0 -2
@@ -447,8 +447,6 @@ static bool remove_migration_pte(struct folio *folio,folio_add_file_rmap_pte (folio, new, vma);set_pte_at (vma->vm_mm, pvmw.address, pvmw.pte, pte); } -if (READ_ONCE (vma->vm_flags) &VM_LOCKED ) -mlock_drain_local ();trace_remove_migration_pte (pvmw.address,pte_val (pte),compound_order (new));
mm/rmap.c +0 -4
@@ -2396,8 +2396,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, } finish_unmap:folio_remove_rmap_ptes (folio, page, nr_pages, vma); -if (vma->vm_flags &VM_LOCKED ) -mlock_drain_local ();folio_put_refs (folio, nr_pages);/*
@@ -2771,8 +2769,6 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,hugetlb_remove_rmap (folio); elsefolio_remove_rmap_pte (folio ,subpage ,vma ); -if (vma->vm_flags &VM_LOCKED ) -mlock_drain_local ();folio_put (folio); }
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 11/26 mm/fbatch: remove migration's PAGE_WAS_MLOCKED lru_add_drain()
In reply to Hugh DickinsA welcome side-effect of mm/mlock.c's mod_mlock_count() succeeding on folios on the per-cpu lru_add fbatch, is that migrate_folio_move() no longer has to lru_add_drain() before remove_migration_ptes() restores a PAGE_WAS_MLOCKED mlock_count: so remove PAGE_WAS_MLOCKED altogether.
(Re the "We would like to do something similar for the old page, when unsuccessful" comment above it: that may be easier now, but involve some rearrangement: not researched, so just leave the comment as is.)
However: it was uncommon before, for isolate_migratepages_block() to meet a folio on LRU, with suitable refcount, marked mlocked but not yet
unevictable: so that got missed when sysctl_compact_unevictable_allowed should not allow it. But now it's a more common case, so check if the folio will become unevictable, by using !folio_evictable() instead.
That is more expensive than testing mlocked and unevictable bits: but it is consistent with the decision made once the fbatch gets drained, and is_inaccessible further down depends on the right is_unevictable. Optimize those checks better if they show a regression in practice.
--- mm/compaction.c | 2 +- mm/migrate.c | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-)
mm/compaction.c +1 -1
@@ -1113,7 +1113,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (!folio_test_lru (folio)) goto isolate_fail_put; - is_unevictable =folio_test_unevictable (folio); + is_unevictable = !folio_evictable (folio);/* Compaction might skip unevictable pages but CMA takes them */ if (!(mode &ISOLATE_UNEVICTABLE ) && is_unevictable)
mm/migrate.c +1 -7
@@ -1147,8 +1147,7 @@ static int move_to_new_folio(struct folio *dst, struct folio *src,*/ enum {FOLIO_WAS_MAPPED =BIT (0 ), -FOLIO_WAS_MLOCKED =BIT (1 ), -FOLIO_OLD_STATES =FOLIO_WAS_MAPPED |FOLIO_WAS_MLOCKED , +FOLIO_OLD_STATES =FOLIO_WAS_MAPPED , }; static void __migrate_folio_record(struct folio *dst,
@@ -1258,8 +1257,6 @@ static int migrate_folio_unmap(new_folio_t get_new_folio,folio_lock (src); } locked = true; -if (folio_test_mlocked (src)) - old_folio_state |=FOLIO_WAS_MLOCKED ;if (folio_test_writeback (src)) {/*
@@ -1410,9 +1407,6 @@ static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private,* isolated from the unevictable LRU: but this case is the easiest. */ folio_add_lru (dst); -if (old_folio_state &FOLIO_WAS_MLOCKED ) -lru_add_drain (); -if (old_folio_state &FOLIO_WAS_MAPPED )remove_migration_ptes (src, dst,0 );
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 12/26 mm/fbatch: remove percpu_pvec_drained and folios_put()
In reply to Hugh DickinsRemove the percpu_pvec_drained field from folio_batch, and its only use in __folio_batch_release(): remove that now pointless lru_add_drain().
Which leaves __folio_batch_release() as an exported name for folios_put() which is itself just a wrapper for folios_put_refs(): mm/mlock.c and mm/folio.c don't need such a wrapper, just say folios_put_refs(,NULL).
Or should folios_put() be the export? But __folio_batch_release() is what drivers/gpu and net/sunrpc are using: don't change them in this series.
--- include/linux/folio_batch.h | 2 -- include/linux/mm.h | 18 ------------------ mm/folio.c | 17 +++-------------- mm/mlock.c | 2 +- 4 files changed, 4 insertions(+), 35 deletions(-)
include/linux/folio_batch.h +0 -2
@@ -27,7 +27,6 @@ struct folio;struct folio_batch {unsigned char nr;unsigned char i; -bool percpu_pvec_drained;struct folio *folios[FOLIO_BATCH_SIZE ]; };
@@ -41,7 +40,6 @@ static inline void folio_batch_init(struct folio_batch *fbatch) { fbatch->nr =0 ; fbatch->i =0 ; - fbatch->percpu_pvec_drained = false; }static inline void folio_batch_reinit (struct folio_batch *fbatch)
include/linux/mm.h +0 -18
@@ -2201,24 +2201,6 @@ typedef union {void release_pages (release_pages_arg ,int nr); -/** -* folios_put - Decrement the reference count on an array of folios. -* @folios: The folios. -* -* Like folio_put(), but for a batch of folios. This is more efficient -* than writing the loop yourself as it will optimise the locks which need -* to be taken if the folios are freed. The folios batch is returned -* empty and ready to be reused for another batch; there is no need to -* reinitialise it. -* -* Context: May be called in process or interrupt context, but not in NMI -* context. May be called while holding a spinlock. -*/ -static inline void folios_put (struct folio_batch *folios) -{ -folios_put_refs (folios,NULL ); -} -static inline void put_page (struct page *page) {struct folio *folio =page_folio (page);
mm/folio.c +3 -14
@@ -160,7 +160,7 @@ static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn)if (lruvec)lruvec_unlock_irqrestore (lruvec, flags); -folios_put (fbatch); +folios_put_refs (fbatch,NULL ); } static void __folio_batch_add_and_move(struct folio_batch __percpu *fbatch,
@@ -1043,22 +1043,11 @@ void release_pages(release_pages_arg arg, int nr)EXPORT_SYMBOL (release_pages);/* -* The folios which we're about to release may be in the deferred lru-addition -* queues. That would prevent them from really being freed right now. That's -* OK from a correctness point of view but is inefficient - those folios may be -* cache-warm and we want to give them back to the page allocator ASAP. -* -* So __folio_batch_release() will drain those queues here. -* folio_batch_move_lru() calls folios_put() directly to avoid -* mutual recursion. +* This used to optimize with a drain before putting: no longer helpful. */ void __folio_batch_release (struct folio_batch *fbatch) { -if (!fbatch->percpu_pvec_drained) { -lru_add_drain (); - fbatch->percpu_pvec_drained = true; - } -folios_put (fbatch); +folios_put_refs (fbatch,NULL ); }EXPORT_SYMBOL (__folio_batch_release);
mm/mlock.c +1 -1
@@ -191,7 +191,7 @@ static void mlock_folio_batch(struct folio_batch *fbatch)if (lruvec)lruvec_unlock_irq (lruvec); -folios_put (fbatch); +folios_put_refs (fbatch,NULL ); }void mlock_drain_local (void )
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 13/26 mm/fbatch: no lru_add_drain to collect_longterm_unpinnable_folios()
In reply to Hugh Dickinscollect_longterm_unpinnable_folios() has no use for lru_add_drain() nor lru_add_drain_all(), now that the per-cpu fbatch references are gone.
Replace the recently added lru_cache_drain_for_folio() by a temporary stub, so any callers outside mm may build independently of this series.
--- include/linux/swap.h | 9 +++++---- mm/folio.c | 48 -------------------------------------------- mm/gup.c | 9 --------- 3 files changed, 5 insertions(+), 61 deletions(-)
include/linux/swap.h +5 -4
@@ -304,11 +304,12 @@ void lru_add_drain_all(void);enum lru_cache_drained {LRU_CACHE_NOT_DRAINED , -LRU_CACHE_DRAINED , -LRU_CACHE_DRAINED_ALL , }; -void lru_cache_drain_for_folio (const struct folio *folio, -unsigned int extra_refs,enum lru_cache_drained *drained); +static inline void lru_cache_drain_for_folio (const struct folio *folio, +unsigned int extra_refs,enum lru_cache_drained *drained) +{ +/* Temporary stub for callers' build independent of mm/fbatch series */ +}/* linux/mm/folio-compat.c */ void mark_page_accessed (struct page *page);
mm/folio.c +0 -48
60 lines not on this page · raw message
mm/gup.c +0 -9
@@ -2266,14 +2266,12 @@ static unsigned long collect_longterm_unpinnable_folios(struct list_head *movable_folio_list, struct pages_or_folios *pofs) { - enum lru_cache_drained drained = LRU_CACHE_NOT_DRAINED; unsigned long collected = 0;struct folio *folio;long i =0 ;for (folio =pofs_get_folio (pofs, i); folio; folio =pofs_next_folio (folio, pofs, &i)) { -const int pin_refs =folio_has_pincount (folio) ?1 :GUP_PIN_COUNTING_BIAS ;if (folio_is_longterm_pinnable (folio))continue ;
@@ -2288,13 +2286,6 @@ static unsigned long collect_longterm_unpinnable_folios(continue ; } -/* -* We drain not only to make the folio_isolate_lru() succeed, -* but also to remove any other folio references from LRU -* caches. -*/ -lru_cache_drain_for_folio (folio, pin_refs, &drained); -if (!folio_isolate_lru (folio))continue ;
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 14/26 mm/fbatch: no lru_add_drain() nor _all() for memfd_wait_for_pins()
In reply to Hugh Dickinsmemfd_tag_pins() used lru_add_drain(), memfd_wait_for_pins() then used lru_add_drain_all(): remove those calls, no longer useful now that the per-cpu fbatch references are gone.
--- mm/memfd.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
mm/memfd.c +1 -5
@@ -40,8 +40,6 @@ static void memfd_tag_pins(struct xa_state *xas)struct folio *folio;int latency =0 ; -lru_add_drain (); -xas_lock_irq (xas);xas_for_each (xas, folio,ULONG_MAX ) {if (!xa_is_value (folio) &&memfd_folio_has_extra_refs (folio))
@@ -168,9 +166,7 @@ static int memfd_wait_for_pins(struct address_space *mapping)if (!xas_marked (&xas,MEMFD_TAG_PINNED ))break ; -if (!scan) -lru_add_drain_all (); -else if (schedule_timeout_killable ((HZ << scan) /200 )) +if (scan &&schedule_timeout_killable ((HZ << scan) /200 )) scan =LAST_SCAN ;xas_set (&xas,0 );
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 15/26 mm/fbatch: remove shake_folio() shake_page() from memory-failure
In reply to Hugh Dickinsshake_folio()'s lru_add_drain_all() no longer serves a purpose, now that the per-cpu fbatch references are gone. Are the retries in get_any_page() then still useful? Not obvious, so keep them.
--- mm/hwpoison-inject.c | 1 - mm/internal.h | 1 - mm/memory-failure.c | 38 +------------------------------------- 3 files changed, 1 insertion(+), 39 deletions(-)
mm/hwpoison-inject.c +0 -1
@@ -118,7 +118,6 @@ static int hwpoison_inject(void *data, u64 val)if (!hwpoison_filter_enable) goto inject; -shake_folio (folio);/* * This implies unable to support non-LRU pages except free page. */
mm/internal.h +0 -1
@@ -1171,7 +1171,6 @@ static inline bool node_reclaim_enabled(void)*/ #ifdef CONFIG_MEMORY_FAILURE int unmap_poisoned_folio (struct folio *folio,unsigned long pfn,bool must_kill); -void shake_folio (struct folio *folio);typedef int hwpoison_filter_func_t (struct page *p);void hwpoison_filter_register (hwpoison_filter_func_t *filter);void hwpoison_filter_unregister (void );
mm/memory-failure.c +1 -37
@@ -310,30 +310,6 @@ static int kill_proc(struct to_kill *tk, unsigned long pfn, int flags)return ret; } -/* -* Unknown page type encountered. Try to check whether it can turn PageLRU by -* lru_add_drain_all. -*/ -void shake_folio (struct folio *folio) -{ -if (folio_test_hugetlb (folio)) -return ; -/* -* TODO: Could shrink slab caches here if a lightweight range-based -* shrinker will be available. -*/ -if (folio_test_slab (folio)) -return ; - -lru_add_drain_all (); -} -EXPORT_SYMBOL_GPL (shake_folio); - -static void shake_page (struct page *page) -{ -shake_folio (page_folio (page)); -} -static unsigned long dev_pagemap_mapping_shift (struct vm_area_struct *vma,unsigned long address) {
@@ -1459,10 +1435,8 @@ static int get_any_page(struct page *p, unsigned long flags)* We raced with (possibly temporary) unhandlable * page, retry. */ -if (pass++ <GET_PAGE_MAX_RETRY_NUM ) { -shake_page (p); +if (pass++ <GET_PAGE_MAX_RETRY_NUM ) goto try_again; - } ret = -EIO ; goto out; }
@@ -1477,7 +1451,6 @@ static int get_any_page(struct page *p, unsigned long flags)*/ if (pass++ <GET_PAGE_MAX_RETRY_NUM ) {put_page (p); -shake_page (p); count_increased = false; goto try_again; }
@@ -1627,7 +1600,6 @@ static bool hwpoison_user_mappings(struct folio *folio, struct page *p,LIST_HEAD (tokill);bool unmap_success;bool forcekill; -bool mlocked =folio_test_mlocked (folio);/* * Here we are interested only in user-mapped pages, so skip any
@@ -1658,13 +1630,6 @@ static bool hwpoison_user_mappings(struct folio *folio, struct page *p,pr_err ("%#lx: failed to unmap page (folio mapcount=%d)\n" , pfn,folio_mapcount (folio)); -/* -* try_to_unmap() might put mlocked page in lru cache, so call -* shake_page() again to ensure that it's flushed. -*/ -if (mlocked) -shake_folio (folio); -/* * Now that the dirty bit has been propagated to the * struct page and all unmaps done we can decide if
@@ -2554,7 +2519,6 @@ int memory_failure(unsigned long pfn, int flags)* The check (unnecessarily) ignores LRU pages being isolated and * walked by the page reclaim code, however that's not a big loss. */ -shake_folio (folio);folio_lock (folio);
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 16/26 mm/fbatch: remove lru_cache_disable() from NUMA folio migration
In reply to Hugh DickinsRemove lru_cache_disable() from mbind(MOVE), migrate_pages and move_pages syscall handling. They do not now benefit from lru_add_drain_all() first; they gain little benefit from invalidating buffer head LRUs first, since 5.0 commit 80409c65e2c6 ("mm: migrate: make buffer_migrate_page_norefs() actually succeed"); and it's a shame that (even without CAP_SYS_NICE) they can prevent concurrent tasks from enjoying the use of those caches.
--- mm/mempolicy.c | 7 ------- mm/migrate.c | 3 --- 2 files changed, 10 deletions(-)
mm/mempolicy.c +0 -7
@@ -1343,8 +1343,6 @@ int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,long err =0 ;nodemask_t tmp; -lru_cache_disable (); -/* * Find a 'source' bit set in 'tmp' whose corresponding 'dest' * bit in 'to' is not also set in 'tmp'. Clear the found 'source'
@@ -1425,7 +1423,6 @@ int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,break ; } -lru_cache_enable ();if (err <0 )return err;return (nr_failed <INT_MAX ) ? nr_failed :INT_MAX ;
@@ -1530,8 +1527,6 @@ static long do_mbind(unsigned long start, unsigned long len,if (!new) flags |=MPOL_MF_DISCONTIG_OK ; -if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) -lru_cache_disable (); {NODEMASK_SCRATCH (scratch);if (scratch) {
@@ -1626,8 +1621,6 @@ static long do_mbind(unsigned long start, unsigned long len,putback_movable_pages (&pagelist); mpol_out:mpol_put (new); -if (flags & (MPOL_MF_MOVE |MPOL_MF_MOVE_ALL )) -lru_cache_enable ();return err; }
mm/migrate.c +0 -3
@@ -2359,8 +2359,6 @@ static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,int start, i;int err =0 , err1; -lru_cache_disable (); -for (i = start =0 ; i < nr_pages; i++) {const void __user *p;int node;
@@ -2439,7 +2437,6 @@ static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,if (err >=0 ) err = err1; out: -lru_cache_enable ();return err; }
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 17/26 mm/fbatch: no lru_cache_disable() in __alloc_contig_migrate_range()
In reply to Hugh DickinsRemove lru_cache_disable() from __alloc_contig_migrate_range(). It does not now benefit from lru_add_drain_all() first; and gains little benefit from invalidating buffer head LRUs first, since 5.0 commit 80409c65e2c6 ("mm: migrate: make buffer_migrate_page_norefs() actually succeed").
This will be more controversial. lru_cache_disable()+lru_cache_enable() were brought in for CMA page migration, see 5.13 commit d479960e44f2 ("mm: disable LRU pagevec during the migration temporarily") through 8cc621d2f45d ("mm: fs: invalidate BH LRU during page migration") - I guess the testing there must have been on a 4.19-based Android kernel, without 5.0's buffer_migrate_page_norefs().
It's possible that invalidating BH LRUs perhaps 0 times, perhaps N times, will average out worse than invalidating 1 time and stopping everyone else; or that folio_test_clear_lru() failures manifest more than before (note how folio migration has retries on raised refcount, but isolate_migratepages_block() no retry on failed test_clear_lru). But let's give this a try and look out for regressions.
Don't delete lru_cache_disable() yet: leaving stale folio pointers in the per-cpu fbatches, with folio_try_get() yet to come on them, would be bad for memory hotremoval: the lru_cache_disable() in offline_pages() protects from that.
--- mm/page_alloc.c | 3 --- 1 file changed, 3 deletions(-)
mm/page_alloc.c +0 -3
@@ -7147,8 +7147,6 @@ static int __alloc_contig_migrate_range(struct compact_control *cc, .reason = MR_CONTIG_RANGE, }; -lru_cache_disable (); -while (pfn < end || !list_empty (&cc->migratepages)) {if (fatal_signal_pending (current)) { ret = -EINTR ;
@@ -7182,7 +7180,6 @@ static int __alloc_contig_migrate_range(struct compact_control *cc,break ; } -lru_cache_enable ();if (ret <0 ) {if (!(cc->gfp_mask & __GFP_NOWARN) && ret == -EBUSY )alloc_contig_dump_pages (&cc->migratepages);
Signature · 1 line
2.51.0
Re: [PATCH v2 17/26] mm/fbatch: no lru_cache_disable() in __alloc_contig_migrate_range()
Hi Hugh,
I've come across your series because of a usecase I have, namely this patch.
Not to bore you with details, I got an IMX8MPlus with some cameras.
Due to the cameras (and other associated HW), it ends up allocating multiple framebuffers using CMA (currently 16 per camera) which is quite slow (~200ms for a single camera).
I've spent some time profiling and found that the biggest
stall is when lru_cache_disable() is called. Just applying the patch on its own, I saw greatly improved performance (cutting the delay almost 5x).
This is on a 7.2 kernel.
Quoted 19 lines · Hugh Dickins
Remove
lru_cache_disable()from__alloc_contig_migrate_range(). It does not now benefit fromlru_add_drain_all()first; and gains little benefit from invalidating buffer head LRUs first, since 5.0 commit80409c65e2c6("mm: migrate: makebuffer_migrate_page_norefs()actually succeed").This will be more controversial. lru_cache_disable()+lru_cache_enable() were brought in for CMA page migration, see 5.13 commit
d479960e44f2("mm: disable LRU pagevec during the migration temporarily") through8cc621d2f45d("mm: fs: invalidate BH LRU during page migration") - I guess the testing there must have been on a 4.19-based Android kernel, without 5.0'sbuffer_migrate_page_norefs().It's possible that invalidating BH LRUs perhaps 0 times, perhaps N times, will average out worse than invalidating 1 time and stopping everyone else; or that
folio_test_clear_lru()failures manifest more than before (note how folio migration has retries on raised refcount, butisolate_migratepages_block()no retry on failedtest_clear_lru). But let's give this a try and look out for regressions.
Quoted 5 lines
Don't delete
lru_cache_disable()yet: leaving stale folio pointers in the per-cpu fbatches, withfolio_try_get()yet to come on them, would be bad for memory hotremoval: thelru_cache_disable()inoffline_pages()protects from that.
Can the patch stand on its own?> Signed-off-by: Hugh Dickins <[email protected]>
Quoted 25 lines
--- mm/page_alloc.c | 3 --- 1 file changed, 3 deletions(-)mm/page_alloc.c +0
-3@@ -7147,8 +7147,6 @@ static int __alloc_contig_migrate_range(struct compact_control *cc, .reason = MR_CONTIG_RANGE, }; -lru_cache_disable (); -while (pfn < end || !list_empty (&cc->migratepages)) {if (fatal_signal_pending (current)) { ret = -EINTR ;@@ -7182,7 +7180,6 @@ static int __alloc_contig_migrate_range(struct compact_control *cc,break ; } -lru_cache_enable ();if (ret <0 ) {if (!(cc->gfp_mask & __GFP_NOWARN) && ret == -EBUSY )alloc_contig_dump_pages (&cc->migratepages);
Signature · 2 lines
Thanks, Alen Karnil
Hugh Dickins
maintainerRe: [PATCH v2 17/26] mm/fbatch: no lru_cache_disable() in __alloc_contig_migrate_range()
Quoted 4 lines · Alen Karnil
Hi Hugh,
I've come across your series because of a usecase I have, namely this patch.
Quoted 8 lines
Not to bore you with details, I got an IMX8MPlus with some cameras. Due to the cameras (and other associated HW), it ends up allocating multiple framebuffers using CMA (currently 16 per camera)
which is quite slow (~200ms for a single camera).I've spent some time profiling and found that the biggest
stall is whenlru_cache_disable()is called. Just applying the patch on its own, I saw greatly improved performance (cutting the delay almost 5x).
That's useful information, thanks for trying it and reporting back.
Quoted 3 lines
This is on a 7.2 kernel.
...
Quoted 1 line
Can the patch stand on its own?
Yes and no. Yes, you can include it in your own builds if you're finding it useful: it's not going to cause any corruption or crashes or suchlike.
But without patches 1-9, it does increase the likelihood that, once the system has been running for a while and memory has become fragmented, compaction for CMA allocations may fail. It's a temporary failure which the lru_cache_disable() protects from, and maybe the retries and system activity would work out, but there is this increased likelihood that your camera will not work when you need it to. (And I don't know how long the camera's memory buffer is kept, between uses of the camera.)
So no, I would never recommend this patch for a release, without the preceding patches of the series.
Hugh
Quoted 29 lines
Quoted 25 lines
--- mm/page_alloc.c | 3 --- 1 file changed, 3 deletions(-)mm/page_alloc.c +0
-1@@ -7147,8 +7147,6 @@ static int __alloc_contig_migrate_range(structcompact_control *cc, .reason = MR_CONTIG_RANGE, }; -lru_cache_disable (); -while (pfn < end || !list_empty (&cc->migratepages)) {if (fatal_signal_pending (current)) { ret = -EINTR ;@@ -7182,7 +7180,6 @@ static int __alloc_contig_migrate_range(structcompact_control *cc, break; } -lru_cache_enable ();if (ret <0 ) {if (!(cc->gfp_mask & __GFP_NOWARN) && ret == -EBUSY )alloc_contig_dump_pages (&cc->migratepages);Signature · 2 lines
Thanks, Alen Karnil
Hugh Dickins
maintainerPATCH v2 18/26 mm/fbatch: remove lru_add_drain() and _all() calls from various
In reply to Hugh DickinsSplitting into little patches gets tedious: now that folios on per-cpu fbatches no longer hold an extra reference (and have the lru flag set), most calls to lru_add_drain(), and more importantly lru_add_drain_all(), should be removed.
Remove lru_add_drain() from compact_zone().
Remove lru_add_drain_all() from compact_nodes() and compact_store(). Keep lru_add_drain_cpu_zone() in compact_zone(): it was always a bit of a hack, a cheap way to get a local_lock() around pcp drain_pages_zone().
Remove lru_add_drain() and lru_add_drain_all() from generic_fadvise( POSIX_FADV_DONTNEED), but still retry once on failure; remove outdated comment line from mapping_try_invalidate().
Remove every lru_add_drain() and lru_add_drain_all() from khugepaged.c.
Remove lru_add_drain_all() from KSM's scan_get_next_rmap_item() restart.
Remove lru_add_drain() from wp_can_reuse_anon_folio() and do_swap_page().
Remove lru_add_drain(), lru_add_drain_all() from migrate_device_unmap().
Keep lru_add_drain()s in mm/gup.c populate_vma_page_range() and faultin_page_range(): good housekeeping after a bulk operation.
Keep lru_add_drain()s in mm/madvise.c: the ones after a bulk op probably do want to "Push any new pages onto the LRU now", and update the stats; the ones before a bulk op may be trying to stabilize initial conditions, or to minimize draining under ptlock.
Keep lru_add_drain_all() in memcg-v1's mem_cgroup_force_empty():
it probably does want to push onto the LRU and update the stats.
Keep lru_add_drain() after bulk op in mlock_vma_pages_range(), partly for "Unevictable kB" accuracy; but remove the lru_add_drain() before it.
Keep lru_add_drain()s in swap_cluster_readahead(), swap_vma_readahead():
they do want to "Push any new pages onto the LRU now" (akpm 2.5.46).
Keep lru_add_drain()s and lru_add_drain_all() throughout mm/vmscan.c:
it works on LRUs, so it does need folios to be on an actual LRU.
--- mm/compaction.c | 12 +----------- mm/fadvise.c | 17 +---------------- mm/khugepaged.c | 11 ----------- mm/ksm.c | 12 ------------ mm/memory.c | 14 ++------------ mm/migrate_device.c | 9 --------- mm/mlock.c | 1 - mm/truncate.c | 1 - 8 files changed, 4 insertions(+), 73 deletions(-)
mm/compaction.c +1 -11
@@ -2652,9 +2652,6 @@ compact_zone(struct compact_control *cc, struct capture_control *capc)trace_mm_compaction_begin (cc, start_pfn, end_pfn, sync); -/* lru_add_drain_all could be expensive with involving other CPUs */ -lru_add_drain (); -while ((ret =compact_finished (cc)) ==COMPACT_CONTINUE ) {int err;unsigned long iteration_start_pfn = cc->migrate_pfn;
@@ -2969,9 +2966,6 @@ static int compact_nodes(void) {int ret, nid; -/* Flush pending updates to the LRU lists */ -lru_add_drain_all (); -for_each_online_node (nid) { ret =compact_node (NODE_DATA (nid), false);if (ret)
@@ -3036,12 +3030,8 @@ static ssize_t compact_store(struct device *dev, {int nid = dev->id; -if (nid >=0 && nid < nr_node_ids &&node_online (nid)) { -/* Flush pending updates to the LRU lists */ -lru_add_drain_all (); - +if (nid >=0 && nid < nr_node_ids &&node_online (nid))compact_node (NODE_DATA (nid), false); - }return count; }
mm/fadvise.c +1 -16
@@ -143,27 +143,12 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)if (end_index >= start_index) {unsigned long nr_failed =0 ; -/* -* It's common to FADV_DONTNEED right after -* the read or write that instantiates the -* pages, in which case there will be some -* sitting on the local LRU cache. Try to -* avoid the expensive remote drain and the -* second cache tree walk below by flushing -* them out right away. -*/ -lru_add_drain (); -mapping_try_invalidate (mapping, start_index, end_index, &nr_failed); -/* -* The failures may be due to the folio being -* in the LRU cache of a remote CPU. Drain all -* caches and try again. +* Retry if any failures, in case they were transient. */ if (nr_failed) { -lru_add_drain_all ();invalidate_mapping_pages (mapping, start_index, end_index); }
mm/khugepaged.c +0 -11
@@ -1236,10 +1236,6 @@ static enum scan_result __collapse_huge_page_swapin(struct mm_struct *mm,if (pte)pte_unmap (pte); -/* Drain LRU cache to remove extra pin on the swapped in pages */ -if (swapped_in) -lru_add_drain (); - result =SCAN_SUCCEED ; out: trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, result,
@@ -2320,8 +2316,6 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, result =SCAN_FAIL ; goto xa_unlocked; } -/* drain lru cache to help folio_isolate_lru() */ -lru_add_drain (); }else if (folio_trylock (folio)) {folio_get (folio);xas_unlock_irq (&xas);
@@ -2335,8 +2329,6 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,page_cache_sync_readahead (mapping, &file->f_ra, file, index, end - index); -/* drain lru cache to help folio_isolate_lru() */ -lru_add_drain (); folio =filemap_lock_folio (mapping, index);if (IS_ERR (folio)) { result =SCAN_FAIL ;
@@ -2975,8 +2967,6 @@ static void khugepaged_do_scan(struct collapse_control *cc)bool wait = true;enum scan_result result =SCAN_SUCCEED ; -lru_add_drain_all (); - cc->progress =0 ;while (true) {cond_resched ();
@@ -3208,7 +3198,6 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start, cc->progress =0 ;mmgrab (mm); -lru_add_drain_all ();for (addr = hstart; addr < hend; addr +=HPAGE_PMD_SIZE ) {enum scan_result result =SCAN_FAIL ;
mm/ksm.c +0 -12
@@ -2625,18 +2625,6 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)advisor_start_scan ();trace_ksm_start_scan (ksm_scan.seqnr, ksm_rmap_items); -/* -* A number of pages can hang around indefinitely in per-cpu -* LRU cache, raised page count preventing write_protect_page -* from merging them. Though it doesn't really matter much, -* it is puzzling to see some stuck in pages_volatile until -* other activity jostles them out, and they also prevented -* LTP's KSM test from succeeding deterministically; so drain -* them here (here rather than on entry to ksm_do_scan(), -* so we don't IPI too often when pages_to_scan is set low). -*/ -lru_add_drain_all (); -/* * Whereas stale stable_nodes on the stable_tree itself * get pruned in the regular course of stable_tree_search(),
mm/memory.c +2 -12
@@ -4313,9 +4313,6 @@ static bool __wp_can_reuse_large_anon_folio(struct folio *folio,static bool wp_can_reuse_anon_folio (struct folio *folio,struct vm_area_struct *vma) { -const bool maybe_in_lru_cache = !folio_test_lru (folio); -const bool in_swapcache =folio_test_swapcache (folio); -if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE ) &&folio_test_large (folio))return __wp_can_reuse_large_anon_folio (folio, vma);
@@ -4326,16 +4323,9 @@ static bool wp_can_reuse_anon_folio(struct folio *folio,* * KSM doesn't necessarily raise the folio refcount. */ -if (folio_test_ksm (folio) || -folio_ref_count (folio) >1 + maybe_in_lru_cache + in_swapcache) +if (folio_test_ksm (folio))return false; -if (maybe_in_lru_cache) -/* -* We cannot easily detect+handle references from -* remote LRU caches or references to LRU folios. -*/ -lru_add_drain (); -if (folio_ref_count (folio) >1 + in_swapcache) +if (folio_ref_count (folio) >1 +folio_test_swapcache (folio))return false;if (!folio_trylock (folio))return false;
mm/migrate_device.c +0 -9
@@ -575,11 +575,8 @@ static unsigned long migrate_device_unmap(unsigned long *src_pfns,struct folio *fault_folio = fault_page ?page_folio (fault_page) :NULL ;unsigned long i, restore =0 ; -bool allow_drain = true;unsigned long unmapped =0 ; -lru_add_drain (); -for (i =0 ; i < npages; ) {struct page *page =migrate_pfn_to_page (src_pfns[i]);struct folio *folio;
@@ -600,12 +597,6 @@ static unsigned long migrate_device_unmap(unsigned long *src_pfns,/* ZONE_DEVICE folios are not on LRU */ if (!folio_is_zone_device (folio)) { -if (!folio_test_lru (folio) && allow_drain) { -/* Drain CPU's lru cache */ -lru_add_drain_all (); - allow_drain = false; - } -if (!folio_isolate_lru (folio)) { src_pfns[i] &= ~MIGRATE_PFN_MIGRATE ; restore++;
mm/mlock.c +0 -1
@@ -424,7 +424,6 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma,vma_start_write (vma);vma_flags_reset_once (vma, new_vma_flags); -lru_add_drain ();walk_page_range_vma (vma, start, end, &mlock_walk_ops,NULL );lru_add_drain ();
mm/truncate.c +0 -1
@@ -571,7 +571,6 @@ unsigned long mapping_try_invalidate(struct address_space *mapping,*/ if (!ret) {deactivate_file_folio (folio); -/* Likely in the lru cache of a remote CPU */ if (nr_failed) (*nr_failed)++; }
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 19/26 mm/fbatch: vm/stat_refresh include lru_add_drain() on each cpu
In reply to Hugh DickinsReading or writing /proc/sys/vm/stat_refresh has long been a way for testing to force an immediate refresh of /proc/vmstat numbers: it seems appropriate that it should also call lru_add_drain() on each cpu that it visits, before collecting that processor's diffs.
Add that in refresh_vm_stats(). I wanted it in refresh_cpu_vm_stats(), which would include it in the regular (default HZ) vmstat_update() too:
which might be a wise precaution when proposing to eliminate almost all the old calls to lru_add_drain_all(). However, that would go against its "strives to only access node local memory" cacheline care, and bring it into lruvec lock contention: which would probably displease its authors.
--- mm/vmstat.c | 1 + 1 file changed, 1 insertion(+)
mm/vmstat.c +1 -0
@@ -1990,6 +1990,7 @@ static int vmstat_late_init_done;#ifdef CONFIG_PROC_FS static void refresh_vm_stats (struct work_struct *work) { +lru_add_drain ();refresh_cpu_vm_stats (true); }
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 20/26 s390/fbatch: no lru_add_drain_all() in s390_wiggle_split_folio()
In reply to Hugh Dickinss390_wiggle_split_folio() has no good reason to lru_add_drain_all(), now that the per-cpu fbatch references are gone.
--- arch/s390/kernel/uv.c | 1 - 1 file changed, 1 deletion(-)
arch/s390/kernel/uv.c +0 -1
@@ -364,7 +364,6 @@ int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio)lockdep_assert_not_held (&mm->mmap_lock);folio_wait_writeback (folio); -lru_add_drain_all ();if (!folio_test_large (folio))return 0 ;
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 21/26 block/fbatch: no lru_add_drain_all() in invalidate_bdev()
In reply to Hugh Dickinsinvalidate_bdev() has no good reason to lru_add_drain_all(), now that the per-cpu fbatch references are gone; but keep doing invalidate_bh_lrus().
--- block/bdev.c | 1 - 1 file changed, 1 deletion(-)
block/bdev.c +0 -1
@@ -97,7 +97,6 @@ void invalidate_bdev(struct block_device *bdev)if (mapping->nrpages) {invalidate_bh_lrus (); -lru_add_drain_all ();/* make sure all lru add caches are flushed */ invalidate_mapping_pages (mapping,0 ,-1 ); } }
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 22/26 fs/fbatch: drop_caches invalidate_bh_lrus() not lru_add_drain_all()
In reply to Hugh Dickinsdrop_caches has no good reason to lru_add_drain_all(), now that the per-cpu fbatch references are gone; but it may have good reason to do the invalidate_bh_lrus() part of it.
--- fs/drop_caches.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/drop_caches.c +2 -2
@@ -10,7 +10,7 @@#include <linux/writeback.h> #include <linux/sysctl.h> #include <linux/gfp.h> -#include <linux/swap.h> +#include <linux/buffer_head.h> #include "internal.h" /* A global variable is a bit ugly, but it keeps the code simple */
@@ -60,7 +60,7 @@ static int drop_caches_sysctl_handler(const struct ctl_table *table, int write,static int stfu;if (sysctl_drop_caches &1 ) { -lru_add_drain_all (); +invalidate_bh_lrus ();iterate_supers (drop_pagecache_sb,NULL );count_vm_event (DROP_PAGECACHE ); }
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 23/26 fs,mm/fbatch: use invalidate_bh_lrus() not invalidate_bh_lrus_cpu()
In reply to Hugh DickinsWhilst there can be a case for including an invalidate_bh_lrus_cpu() in lru_add_drain_all()'s workqueued visits to remote CPUs, wouldn't it now be a preferable cleanup to remove that alternative, and stick with the smp_call_function_many_cond()-based invalidate_bh_lrus() throughout?
And fix the UP lru_add_drain_all() to include an invalidate_bh_lrus(), which went missing when 5.15 commit 243418e3925d ("mm: fs: invalidate bh_lrus for only cold path") took that out of lru_add_drain().
--- fs/buffer.c | 16 +--------------- include/linux/buffer_head.h | 4 ---- mm/folio.c | 25 ++++++------------------- 3 files changed, 7 insertions(+), 38 deletions(-)
fs/buffer.c +1 -15
@@ -1427,7 +1427,7 @@ static void invalidate_bh_lru(void *arg)put_cpu_var (bh_lrus); } -bool has_bh_in_lru (int cpu,void *dummy) +static bool has_bh_in_lru (int cpu,void *dummy) {struct bh_lru *b =per_cpu_ptr (&bh_lrus, cpu);int i;
@@ -1446,20 +1446,6 @@ void invalidate_bh_lrus(void) }EXPORT_SYMBOL_GPL (invalidate_bh_lrus); -/* -* It's called from workqueue context so we need a bh_lru_lock to close -* the race with preemption/irq. -*/ -void invalidate_bh_lrus_cpu (void ) -{ -struct bh_lru *b; - -bh_lru_lock (); - b =this_cpu_ptr (&bh_lrus); -__invalidate_bh_lrus (b); -bh_lru_unlock (); -} -void folio_set_bh (struct buffer_head *bh,struct folio *folio,unsigned long offset) {
include/linux/buffer_head.h +0 -4
@@ -518,8 +518,6 @@ bool mmb_has_buffers(struct mapping_metadata_bhs *mmb);void mmb_invalidate (struct mapping_metadata_bhs *mmb);int mmb_sync (struct mapping_metadata_bhs *mmb);void invalidate_bh_lrus (void ); -void invalidate_bh_lrus_cpu (void ); -bool has_bh_in_lru (int cpu,void *dummy);extern int buffer_heads_over_limit;#else /* CONFIG_BUFFER_HEAD */
@@ -528,8 +526,6 @@ static inline void buffer_init(void) {}static inline bool try_to_free_buffers (struct folio *folio) {return true; }static inline int mmb_sync (struct mapping_metadata_bhs *mmb) {return 0 ; }static inline void invalidate_bh_lrus (void ) {} -static inline void invalidate_bh_lrus_cpu (void ) {} -static inline bool has_bh_in_lru (int cpu,void *dummy) {return false; }#define buffer_heads_over_limit 0#endif /* CONFIG_BUFFER_HEAD */
mm/folio.c +6 -19
@@ -681,21 +681,6 @@ void lru_add_drain(void)mlock_drain_local (); } -/* -* It's called from per-cpu workqueue context in SMP case so -* lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on -* the same cpu. It shouldn't be a problem in !SMP case since -* the core is only one and the locks will disable preemption. -*/ -static void lru_add_and_bh_lrus_drain (void ) -{ -local_lock (&cpu_fbatches.lock); -lru_add_drain_cpu (smp_processor_id ()); -local_unlock (&cpu_fbatches.lock); -invalidate_bh_lrus_cpu (); -mlock_drain_local (); -} -void lru_add_drain_cpu_zone (struct zone *zone) {local_lock (&cpu_fbatches.lock);
@@ -711,7 +696,7 @@ static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);static void lru_add_drain_per_cpu (struct work_struct *dummy) { -lru_add_and_bh_lrus_drain (); +lru_add_drain (); }static bool cpu_needs_drain (unsigned int cpu)
@@ -724,8 +709,7 @@ static bool cpu_needs_drain(unsigned int cpu)folio_batch_count (&fbatches->lru_move_tail) ||folio_batch_count (&fbatches->lru_deactivate_file) ||folio_batch_count (&fbatches->lru_deactivate) || - need_mlock_drain(cpu)) || -has_bh_in_lru (cpu,NULL ); + need_mlock_drain(cpu)); }/*
@@ -824,6 +808,8 @@ static inline void __lru_add_drain_all(bool force_all_cpus) } } +invalidate_bh_lrus (); +for_each_cpu (cpu, &has_work)flush_work (&per_cpu (lru_add_drain_work, cpu));
@@ -839,6 +825,7 @@ void lru_add_drain_all(void)void lru_add_drain_all (void ) {lru_add_drain (); +invalidate_bh_lrus (); }#endif /* CONFIG_SMP */
@@ -872,7 +859,7 @@ void lru_cache_disable(void)#ifdef CONFIG_SMP __lru_add_drain_all (true);#else -lru_add_and_bh_lrus_drain (); +lru_add_drain_all ();#endif }
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 24/26 fs,mm/fbatch: lru_cache_disable() keep off buffer_head lrus only
In reply to Hugh DickinsNow that the per-cpu fbatch folio references are gone, there seems to be no excuse for lru_cache_disable() there - other than offline_pages() needing to lru_add_drain_all() to erase stale pointers from the fbatches.
Remove lru_cache_disabled() checks from all except bh_lru_install():
assuming that lru_cache_disable() might have value in preventing repeated calls to invalidate_bh_lrus() when migrating folios in memory hotremoval.
So move all that from mm/folio.c to fs/buffer.c: but I can't see how any of the paranoid synchronize_rcu_expedited() business is needed now (or even before) - leave it out. And back at the mm end, lru_add_drain_all() does not need that force_all_cpus either - it stopped forcing all cpus in 5.18 commit ff042f4a9b05 ("mm: lru_cache_disable: replace work queue synchronization with synchronize_rcu").
--- fs/buffer.c | 24 +++++++++++++++--- include/linux/buffer_head.h | 4 +++ include/linux/swap.h | 7 ------ mm/folio.c | 49 ++++--------------------------------- mm/internal.h | 6 ----- mm/memory_hotplug.c | 4 +++ mm/mlock.c | 4 +-- 7 files changed, 36 insertions(+), 62 deletions(-)
fs/buffer.c +21 -3
@@ -1200,6 +1200,24 @@ static inline void check_irqs_on(void)#endif } +static atomic_t lru_disable_count =ATOMIC_INIT (0 ); + +void lru_cache_disable (void ) +{ +if (atomic_inc_return (&lru_disable_count) ==1 ) +invalidate_bh_lrus (); +} + +static inline bool lru_cache_disabled (void ) +{ +return atomic_read (&lru_disable_count); +} + +void lru_cache_enable (void ) +{ +atomic_dec (&lru_disable_count); +} +/* * Install a buffer_head into this cpu's LRU. If not already in the LRU, it is * inserted at the front, and the buffer_head at the back if any is evicted.
@@ -1215,9 +1233,9 @@ static void bh_lru_install(struct buffer_head *bh)bh_lru_lock ();/* -* the refcount of buffer_head in bh_lru prevents dropping the -* attached page(i.e., try_to_free_buffers) so it could cause -* failing page migration. +* The refcount of buffer_head in bh_lru prevents dropping the +* attached page (i.e., try_to_free_buffers), so it could cause +* repeated calls to invalidate_bh_lrus() during page migration. * Skip putting upcoming bh into bh_lru until migration is done. */ if (lru_cache_disabled () ||cpu_is_isolated (smp_processor_id ())) {
include/linux/buffer_head.h +4 -0
@@ -517,6 +517,8 @@ void mmb_init(struct mapping_metadata_bhs *mmb, struct address_space *mapping);bool mmb_has_buffers (struct mapping_metadata_bhs *mmb);void mmb_invalidate (struct mapping_metadata_bhs *mmb);int mmb_sync (struct mapping_metadata_bhs *mmb); +void lru_cache_disable (void ); +void lru_cache_enable (void );void invalidate_bh_lrus (void );extern int buffer_heads_over_limit;
@@ -525,6 +527,8 @@ extern int buffer_heads_over_limit;static inline void buffer_init (void ) {}static inline bool try_to_free_buffers (struct folio *folio) {return true; }static inline int mmb_sync (struct mapping_metadata_bhs *mmb) {return 0 ; } +static inline void lru_cache_disable (void ) {} +static inline void lru_cache_enable (void ) {}static inline void invalidate_bh_lrus (void ) {}#define buffer_heads_over_limit 0
include/linux/swap.h +0 -7
@@ -314,13 +314,6 @@ static inline void lru_cache_drain_for_folio(const struct folio *folio,/* linux/mm/folio-compat.c */ void mark_page_accessed (struct page *page); -extern atomic_t lru_disable_count; - -static inline bool lru_cache_disabled (void ) -{ -return atomic_read (&lru_disable_count); -} -extern unsigned long shrink_all_memory (unsigned long nr_pages);long remove_mapping (struct address_space *mapping,struct folio *folio);
mm/folio.c +5 -44
91 lines not on this page · raw message
mm/internal.h +0 -6
@@ -53,12 +53,6 @@ static inline bool folio_may_be_lru_cached(const struct folio *folio)return !folio_test_large (folio); } -static inline void lru_cache_enable (void ) -{ -atomic_dec (&lru_disable_count); -} - -void lru_cache_disable (void );void lru_add_drain (void );void lru_add_drain_cpu (int cpu);void lru_add_drain_cpu_zone (struct zone *zone);
mm/memory_hotplug.c +4 -0
@@ -24,6 +24,7 @@#include <linux/ioport.h> #include <linux/delay.h> #include <linux/migrate.h> +#include <linux/buffer_head.h> #include <linux/page-isolation.h> #include <linux/pfn.h> #include <linux/suspend.h>
@@ -2095,6 +2096,9 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages, }while (ret); +/* Remove any instances of the freed pages from per-cpu fbatches. */ +lru_add_drain_all (); +/* Mark all sections offline and remove free pages from the buddy. */ managed_pages =__offline_isolated_pages (start_pfn, end_pfn);pr_debug ("Offlined Pages %ld\n" , nr_pages);
mm/mlock.c +2 -2
@@ -247,7 +247,7 @@ void mlock_folio(struct folio *folio)local_lock (&mlock_fbatch.lock); fbatch =this_cpu_ptr (&mlock_fbatch.fbatch);if (!folio_batch_add (fbatch,mlock_flagged (folio)) || - !folio_may_be_lru_cached (folio) ||lru_cache_disabled ()) + !folio_may_be_lru_cached (folio))mlock_folio_batch (fbatch);local_unlock (&mlock_fbatch.lock); }
@@ -278,7 +278,7 @@ void munlock_folio(struct folio *folio)local_lock (&mlock_fbatch.lock); fbatch =this_cpu_ptr (&mlock_fbatch.fbatch);if (!folio_batch_add (fbatch, folio) || - !folio_may_be_lru_cached (folio) ||lru_cache_disabled ()) + !folio_may_be_lru_cached (folio))mlock_folio_batch (fbatch);local_unlock (&mlock_fbatch.lock); }
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 25/26 mm/fbatch: move lru_add_drain_all() declaration to mm/internal.h
In reply to Hugh DickinsOnly mm now uses lru_add_drain_all(), in forced reclaim and in memory
hotremoval: help to keep it that way by moving its declaration alongside that of lru_add_drain() in mm/internal.h.
--- include/linux/swap.h | 1 - mm/internal.h | 1 + 2 files changed, 1 insertion(+), 1 deletion(-)
include/linux/swap.h +0 -1
@@ -300,7 +300,6 @@ static inline void folio_add_lru(struct folio *folio)__folio_add_lru (folio, false); }void folio_mark_accessed (struct folio *folio); -void lru_add_drain_all (void );enum lru_cache_drained {LRU_CACHE_NOT_DRAINED ,
mm/internal.h +1 -0
@@ -54,6 +54,7 @@ static inline bool folio_may_be_lru_cached(const struct folio *folio) }void lru_add_drain (void ); +void lru_add_drain_all (void );void lru_add_drain_cpu (int cpu);void lru_add_drain_cpu_zone (struct zone *zone);void folio_deactivate (struct folio *folio);
Signature · 1 line
2.51.0
Hugh Dickins
maintainerPATCH v2 26/26 mm/fbatch: drop reference inside the loop when draining
In reply to Hugh Dickinsfolio_batch_move_lru() and mlock_folio_batch() used folios_put_refs() after their loop: but that's counter-productive, to batch up dropping all the references acquired within the loop. Now folio_put_testzero() inside the loop, where we also already hold (bar races) the right lock to remove the folio from its lruvec.
This should be much friendlier to compaction, in the common case when the folio is still in use, to bring it back to expected refs sooner; but I suspect that when the folio is freed, compaction won't accept it until it gets to be PageBuddy later on?
Respect the comment in mm/vmscan.c move_folios_to_lru(): could be done differently, but it's not worth optimizing the case when we cleared lru, since it also has to handle the case when we failed to clear it.
--- mm/folio.c | 23 +++++++++++++++++------ mm/mlock.c | 27 +++++++++++++++++++++------ 2 files changed, 38 insertions(+), 12 deletions(-)
mm/folio.c +17 -6
@@ -128,20 +128,18 @@ static void lru_add(struct lruvec *lruvec, struct folio *folio)static void folio_batch_move_lru (struct folio_batch *fbatch,move_fn_t move_fn) { -int i; +int i, j =0 ;struct lruvec *lruvec =NULL ;unsigned long flags =0 ;for (i =0 ; i <folio_batch_count (fbatch); i++) {struct folio *folio = fbatch->folios[i]; -if (!folio_try_get (folio)) { - fbatch->folios[i] =NULL ; +if (!folio_try_get (folio))continue ; - }if (!folio_test_clear_lru (folio)) -continue ; + goto restored_lru;/* Do not add to LRU if it has already been added */ if (move_fn == lru_add && !lru_add_del_folio (folio))
@@ -155,11 +153,24 @@ static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn)lruvec_add_folio (lruvec, folio); restore_lru:folio_set_lru (folio); +/* See mm/vmscan.c move_folios_to_lru() comment on ordering */ +restored_lru: +if (unlikely (folio_put_testzero (folio))) { +folio_unqueue_deferred_split (folio); +__page_cache_release (folio, &lruvec, &flags); + fbatch->folios[j++] = folio; + } }if (lruvec)lruvec_unlock_irqrestore (lruvec, flags); -folios_put_refs (fbatch,NULL ); +if (unlikely (j)) { + fbatch->nr = j; +mem_cgroup_uncharge_folios (fbatch); +free_unref_folios (fbatch); + }else { +folio_batch_reinit (fbatch); + } } static void __folio_batch_add_and_move(struct folio_batch __percpu *fbatch,
mm/mlock.c +21 -6
@@ -27,6 +27,7 @@#include <linux/secretmem.h> #include "internal.h" +#include "page_alloc.h" struct mlock_fbatch {local_lock_t lock;
@@ -170,28 +171,42 @@ static void mlock_folio_batch(struct folio_batch *fbatch)struct lruvec *lruvec =NULL ;unsigned long mlock;struct folio *folio; -int i; +int i, j =0 ;for (i =0 ; i <folio_batch_count (fbatch); i++) { folio = fbatch->folios[i]; mlock = (unsigned long )folio &MLOCK_FLAG ; folio = (struct folio *)((unsigned long )folio - mlock); - fbatch->folios[i] = folio; -if (!folio_try_get (folio)) { - fbatch->folios[i] =NULL ; +if (!folio_try_get (folio))continue ; - }if (mlock) lruvec =__mlock_folio (folio, lruvec);else lruvec =__munlock_folio (folio, lruvec); + +if (unlikely (folio_put_testzero (folio))) { +folio_unqueue_deferred_split (folio); +/* __page_cache_release() without irqflags */ +if (folio_test_lru (folio)) { + lruvec =folio_lruvec_relock_irq (folio, lruvec); +lruvec_del_folio (lruvec, folio); +__folio_clear_lru_flags (folio); + } + fbatch->folios[j++] = folio; + } }if (lruvec)lruvec_unlock_irq (lruvec); -folios_put_refs (fbatch,NULL ); +if (unlikely (j)) { + fbatch->nr = j; +mem_cgroup_uncharge_folios (fbatch); +free_unref_folios (fbatch); + }else { +folio_batch_reinit (fbatch); + } }void mlock_drain_local (void )
Signature · 1 line
2.51.0
Hugh Dickins
maintainer[PATCH v2 27/26] mm/fbatch: paranoid folio vmstats in folio_batch_move_lru()
In reply to Hugh DickinsSave mapping and index when adding folio to per-cpu fbatch, check them when draining the fbatch, show folio counts at the end of /proc/vmstat when CONFIG_DEBUG_VM=y.
This is a work-in-progress hack, not intended for kernel release:
hence the 27/26 leaving it out of the series; but a useful framework for study and reassurance on stale fbatch entries.
Could be extended to the mlock_fbatch, but probably nothing new to learn there. May be more useful if separate stats kept for the lru_add fbatch and (together) the other four - their characteristics are different.
Needs refinement: the current numbers raise more questions than give answers. The folio_hoisted, folio_changed, folio_already numbers mostly record nothing worse than LRU-eligible folio pages getting freed before drain, and soon recycled for fresh LRU-eligible folios: no harm there; but these stats do not verify that.
For example, after hours of kernel builds on tmpfs while swapping:
folio_not_got 65750323folio_off_lru 357827folio_hoisted 10458363folio_matched 2888574439folio_swapped 113626282folio_changed 11558942folio_already 12717475folio_skipped 705
--- include/linux/vm_event_item.h | 10 ++++ mm/folio.c | 101 +++++++++++++++++++++++++++++++--- mm/vmstat.c | 10 ++++ 3 files changed, 112 insertions(+), 9 deletions(-)
include/linux/vm_event_item.h +10 -0
@@ -179,6 +179,16 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, NRSWPIN, NRSWPOUT,#endif /* CONFIG_SWAP */ +#ifdef CONFIG_DEBUG_VM +FOLIO_NOT_GOT , +FOLIO_OFF_LRU , +FOLIO_HOISTED , +FOLIO_MATCHED , +FOLIO_SWAPPED , +FOLIO_CHANGED , +FOLIO_ALREADY , +FOLIO_SKIPPED , +#endif /* CONFIG_DEBUG_VM */NR_VM_EVENT_ITEMS };
mm/folio.c +92 -9
161 lines not on this page · raw message
mm/vmstat.c +10 -0
@@ -1507,6 +1507,16 @@ const char * const vmstat_text[] = { [I(NRSWPIN)] = "nrswpin", [I(NRSWPOUT)] = "nrswpout", #endif /* CONFIG_SWAP */ +#ifdef CONFIG_DEBUG_VM + [I(FOLIO_NOT_GOT)] = "folio_not_got", + [I(FOLIO_OFF_LRU)] = "folio_off_lru", + [I(FOLIO_HOISTED)] = "folio_hoisted", + [I(FOLIO_MATCHED)] = "folio_matched", + [I(FOLIO_SWAPPED)] = "folio_swapped", + [I(FOLIO_CHANGED)] = "folio_changed", + [I(FOLIO_ALREADY)] = "folio_already", + [I(FOLIO_SKIPPED)] = "folio_skipped", +#endif /* CONFIG_DEBUG_VM */ #undef I #endif /* CONFIG_VM_EVENT_COUNTERS */ };
Signature · 1 line
2.51.0
Hi Hugh,
Quoted 28 lines · Hugh Dickins
[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 lighterlru_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_addand other per-cpu fbatches remain there, isolatable, withPG_lruset and refcount unraised, just like when on an actual LRU. Then many calls tolru_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_lrurules.No new locking, but relies on
folio_try_get()andfolio_test_clear_lru()even on thelru_addfbatch; 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.
Quoted 5 lines
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).
I found one source of regression but I think it's fixable.
Using a test of 4 processes repeatedly write faulting anon pages then discarding with MADV_DONTNEED, the perf A/B below shows that the series (minus patch 27) spends more time in __page_cache_release due to new lru_lock traffic.
baseline (7891fbb9512f):
0.03% folio-churn [kernel.kallsyms] [k] queued_spin_lock_slowpath 0.26% folio-churn [kernel.kallsyms] [k] _raw_spin_lock_irqsave 0.01% folio-churn [kernel.kallsyms] [k] folio_lruvec_lock_irqsave 0.00% folio-churn [kernel.kallsyms] [k] folio_lruvec_relock_irqsave 0.03% folio-churn [kernel.kallsyms] [k] __page_cache_release
series (without patch 27):
6.97% folio-churn [kernel.kallsyms] [k] _raw_spin_lock_irqsave
|
---_raw_spin_lock_irqsave
|
--6.96%--folio_lruvec_lock_irqsave
folio_lruvec_relock_irqsave
|
--6.72%--__page_cache_release
folios_put_refs
free_pages_and_swap_cache
tlb_flush_mmu
tlb_finish_mmu
do_madvise
__x64_sys_madvise
do_syscall_64
entry_SYSCALL_64_after_hwframe
__madvise
6.62% folio-churn [kernel.kallsyms] [k] queued_spin_lock_slowpath
|
---queued_spin_lock_slowpath
_raw_spin_lock_irqsave
folio_lruvec_lock_irqsave
folio_lruvec_relock_irqsave
|
--6.43%--__page_cache_release
folios_put_refs
free_pages_and_swap_cache
tlb_flush_mmu
tlb_finish_mmu
do_madvise
__x64_sys_madvise
do_syscall_64
entry_SYSCALL_64_after_hwframe
__madviseThere was a pre-existing dead folio optimization [0] which allowed caller-released folios remaining on batches to be cleaned up at drain time without lru_lock acquisition. This series removes it which makes sense since the batch reference concept is gone. But now that callers effectively invoke the cleanup when refcounts go directly to zero, there's a point in which an lru_lock will be taken for pending folios that are dead (refcount zero but still on batch). That's seen in the call graphs above and is unnecessary since these folios haven't been inserted into the LRU. Luckily It looks like the new API call lru_add_del_folio() can be used to avoid this, since it returns true after cleaning up the pending LRU batch state. I applied this diff and re-ran the same benchmark. The new locking overhead was nearly all eliminated. The perf output afterward shows the fractional amount that remains.
@@ -64,8 +64,10 @@ static void __page_cache_release(struct folio *folio, struct lruvec **lruvecp,
unsigned long *flagsp)
{
if (folio_test_lru(folio)) {
- folio_lruvec_relock_irqsave(folio, lruvecp, flagsp);
- lruvec_del_folio(*lruvecp, folio);
+ if (!lru_add_del_folio(folio)) {
+ folio_lruvec_relock_irqsave(folio, lruvecp, flagsp);
+ lruvec_del_folio(*lruvecp, folio);
+ }
__folio_clear_lru_flags(folio);
}}
series modified with patch above (without patch 27):
0.77% folio-churn [kernel.kallsyms] [k] queued_spin_lock_slowpath
|
---queued_spin_lock_slowpath
_raw_spin_lock_irqsave
|
--0.77%--folio_lruvec_lock_irqsave
folio_lruvec_relock_irqsave
|
--0.52%--__page_cache_release
folios_put_refs
free_pages_and_swap_cache
tlb_flush_mmu
tlb_finish_mmu
do_madvise
__x64_sys_madvise
do_syscall_64
entry_SYSCALL_64_after_hwframe
__madvise
0.37% folio-churn [kernel.kallsyms] [k] _raw_spin_lock_irqsave
0.04% folio-churn [kernel.kallsyms] [k] folio_lruvec_lock_irqsave
0.01% folio-churn [kernel.kallsyms] [k] folio_lruvec_relock_irqsaveHi Hugh,
Quoted 38 lines · Hugh Dickins
[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 lighterlru_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_addand other per-cpu fbatches remain there, isolatable, withPG_lruset and refcount unraised, just like when on an actual LRU. Then many calls tolru_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_lrurules.No new locking, but relies on
folio_try_get()andfolio_test_clear_lru()even on thelru_addfbatch; 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.
Quoted 9 lines
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 forNR_MLOCKin__munlock_folio()" and Ackerley's "mm/folio:EXPORT_SYMBOL_FOR_KVM( lru_cache_drain_for_folio)". Those two are now in mm-hotfixes-stable (e14a34548064and7891fbb9512frespectively: 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.
Can confirm it cleanly applies and runs on 7.3 ('c03c1a14507dfaf8b856c2608f9ac216021fc222')>
Quoted 84 lines
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: allowfolios_put_refs()to skipxa_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 ofpage->lru.nextper Kiryl and Matthew
READ/WRITE_ONCEsmp_mb__before_atomicper 07/25 discussion with Kiryl fixed (commented out)BUG_ONinlru_add_del_folio()
05/26 mm/fbatch: lru_add_del_folio()+folio_add_lru() afterclear_lru()06/26 mm/fbatch:fbatch_drain_lazyfree(onstack fbatch)before ptl unlock 07/26 mm/fbatch:LRU_NEXT_ACTIVATEbit to optimizefolio_activate()
v2: hunk inlru_add_del_folio()updated according to mods in 04/26
08/26 mm/fbatch: replacemlock_new_folio()by__folio_add_lru(,mlockit)
v2: updated 04/26 Bit 1 comment onfolio_add_lru()to__folio_add_lru()
09/26 mm/fbatch: restore mlock+munlock batching, without extra ref
v2: rediffed around Shakeel'sNR_MLOCKfix, fixed unknowingly in v1
10/26 mm/fbatch: remove several uses ofmlock_drain_local()11/26 mm/fbatch: remove migration'sPAGE_WAS_MLOCKEDlru_add_drain()
v2: conversely, use !folio_evictable() inisolate_migratepages_block()
12/26 mm/fbatch: removepercpu_pvec_drainedandfolios_put()13/26 mm/fbatch: nolru_adddrain tocollect_longterm_unpinnable_folios()
v2: revert David's rc1lru_cache_drain_for_folio()frommm/folio.c,
and Ackerley's EXPORT for KVM; but keep an inline stub inswap.h
14/26 mm/fbatch: nolru_add_drain()nor_all()formemfd_wait_for_pins()15/26 mm/fbatch: removeshake_folio()shake_page()from memory-failure
v2: added Ack from Miaohe
16/26 mm/fbatch: removelru_cache_disable()from NUMA folio migration 17/26 mm/fbatch: nolru_cache_disable()in__alloc_contig_migrate_range()18/26 mm/fbatch: removelru_add_drain()and_all()calls from various 19/26 mm/fbatch: vm/stat_refresh includelru_add_drain()on each cpu 20/26 s390/fbatch: nolru_add_drain_all()ins390_wiggle_split_folio()21/26 block/fbatch: nolru_add_drain_all()ininvalidate_bdev()22/26 fs/fbatch:drop_cachesinvalidate_bh_lrus()notlru_add_drain_all()23/26 fs,mm/fbatch: useinvalidate_bh_lrus()notinvalidate_bh_lrus_cpu()24/26 fs,mm/fbatch:lru_cache_disable()keep offbuffer_headlrus only 25/26 mm/fbatch: movelru_add_drain_all()declaration tomm/internal.h26/26 mm/fbatch: drop reference inside the loop when drainingDocumentation/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:
7891fbb9512f127826e1d5dbf380ee212bd15eb0Hugh
Signature · 2 lines
Thanks, Alen Karnil
Quoted 4 lines · Hugh Dickins
mm/fbatch: drain
lru_add_drain()and_all()[PATCH v2 00/26] mm/fbatch: drain
lru_add_drain()and_all()
Quoted 8 lines
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 lighterlru_add_drain(); but even those local drains can aggravate the lruvec lock contention which per-cpu fbatches are intended to ease.[...]
Signature · 1 line
Alen Karnil <[email protected]>
Hugh Dickins
maintainerQuoted 12 lines · Alen Karnil
Quoted 11 lines · Hugh Dickins
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 lighterlru_add_drain(); but even those local drains can aggravate the lruvec lock contention which per-cpu fbatches are intended to ease.
Quoted 3 lines
Quoted 1 line
[...]
Tested-by Alen Karnil <[email protected]>
Thank you, Alen, that's very encouraging and helpful - though I think it would be too presumptuous of me to add your Tested-by tag to each of the 26: I'll just add your Reviewed-bys to 01 and 02, thanks.
You may be wondering what became of the series: it's good, and I did rebase it to 7.4-rc2, folding in JP Kobryn's fine improvement (thank you) to __page_cache_release(), and Kiryl's !folio_test_unevictable() safety in lru_add_del_folio().
But found there was already a conflict in folio_batch_move_lru() in mm/core.git, which would make several of the rc2-based patches not directly applicable there; then saw David's "MM: Firefighting mode" mail, which confirmed what was becoming apparent, that it would be more helpful not to repost the lru_add_drain() series this cycle, but rather attempt to review the current fixing activity (I've tried, but just cannot keep up with all that David, Lorenzo and others have been achieving).
I intend to post again around 7.5-rc1 or -rc2: I'm probably naive to hope that the fixing frenzy will be less by then, but I'd better go ahead with a repost then, whatever the situation.
Hugh