Re: [PATCH] mm/lruvec: preemptively free dead folios during lru_add drain

From: Matthew Wilcox

Date: Thu Apr 23 2026 - 13:15:53 EST


On Thu, Apr 23, 2026 at 09:43:07AM -0700, JP Kobryn (Meta) wrote:
> Of all observable lruvec lock contention in our fleet, we find that ~24%
> occurs when dead folios are present in lru_add batches at drain time. This
> is wasteful in the sense that the folio is added to the LRU just to be
> immediately removed via folios_put_refs(), incurring two unnecessary lock
> acquisitions.

Well, this is a lovely patch with no obvious downsides. Nicely done.

> Eliminate this overhead by preemptively cleaning up dead folios before they
> make it into the LRU. Use folio_ref_freeze() to filter folios whose only
> remaining refcount is the batch ref. When dead folios are found, move them
> off the add batch and onto a temporary batch to be freed.
>
> During A/B testing on one of our prod instagram workloads (high-frequency
> short-lived requests), the patch intercepted almost all dead folios before
> they entered the LRU. Data collected using the mm_lru_insertion tracepoint
> shows the effectiveness of the patch:
>
> Per-host LRU add averages at 95% CPU load
> (60 hosts each side, 3 x 60s intervals)
>
> dead folios/min total folios/min dead %
> unpatched: 1,297,785 19,341,986 6.7097%
> patched: 14 19,039,996 0.0001%
>
> Within this workload, we save ~2.6M lock acquisitions per minute per host
> as a result.
>
> System-wide memory stats improved on the patched side also at 95% CPU load:
> - direct reclaim scanning reduced 7%
> - allocation stalls reduced 5.2%
> - compaction stalls reduced 12.3%
> - page frees reduced 4.9%
>
> No regressions were observed in requests served per second or request tail
> latency (p99). Both metrics showed directional improvement at higher CPU
> utilization (comparing 85% to 95%).
>
> Signed-off-by: JP Kobryn (Meta) <jp.kobryn@xxxxxxxxx>
> ---
> mm/swap.c | 36 +++++++++++++++++++++++++++++++++++-
> 1 file changed, 35 insertions(+), 1 deletion(-)
>
> diff --git a/mm/swap.c b/mm/swap.c
> index 5cc44f0de9877..71607b0ce3d18 100644
> --- a/mm/swap.c
> +++ b/mm/swap.c
> @@ -160,13 +160,36 @@ 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 (move_fn != lru_add && !folio_test_clear_lru(folio))
> + 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.
> + *
> + * 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_unqueue_deferred_split(folio);

Would it be better to do this outside the lru lock; it's just that we
don't have a convenient batched version to do it? It seems like
there are a few places that could use a batched version in vmscan.c and
swap.c. Not that I think we should hold up this patch to investigate
that micro-optimisation! Just something you couldlook at as a
follow-up.

Reviewed-by: Matthew Wilcox (Oracle) <willy@xxxxxxxxxxxxx>