[RFC PATCH 05/18] mm: expedite the grace period when a CPU has many deferred frees

From: Kiryl Shutsemau (Meta)

Date: Mon Oct 19 2026 - 12:46:25 EST


From: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>

A deferred folio waits one to two grace periods on its CPU's list, where
the page allocator cannot use it. A CPU that frees many flagged folios
at once, for example when a file is truncated, can hold a lot of memory
there.

When more than 4 MiB is pending on a CPU, a work takes both of the CPU's
lists and frees them after an expedited grace period. That grace period
starts after the take, so it covers every reader that found one of the
folios. The task that frees them does not wait.

An expedited grace period interrupts every CPU, so keep it for when the
memory matters. That is when the zone is below its high watermark, or
when the pages waiting in it are more than an eighth of its free pages.
The watermark alone comes too late for a bulk free. A truncate or
fadvise(POSIX_FADV_DONTNEED) of a hot range defers all of it while the
zone still has memory. When the readers that refill the range then run
short, nothing is freed that would check again.

With rcu_normal, as on PREEMPT_RT, the expedited grace period is a
normal one.

pgfree_rcu_expedite in /proc/vmstat counts the expedite runs that freed
something.

Assisted-by: LLM
Signed-off-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
[kas: take both lists, expedite only when the memory matters, rewrote
the changelog]
Signed-off-by: Kiryl Shutsemau (Meta) <kas@xxxxxxxxxx>
---
include/linux/vm_event_item.h | 2 +-
mm/page_alloc.c | 61 ++++++++++++++++++++++++++++++++++-
mm/vmstat.c | 1 +
3 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h
index cbe849701653..45527fa7d0c1 100644
--- a/include/linux/vm_event_item.h
+++ b/include/linux/vm_event_item.h
@@ -36,7 +36,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,
FOR_ALL_ZONES(ALLOCSTALL)
FOR_ALL_ZONES(PGSCAN_SKIP)
PGFREE, PGACTIVATE, PGDEACTIVATE, PGLAZYFREE,
- PGFREE_RCU,
+ PGFREE_RCU, PGFREE_RCU_EXPEDITE,
PGFAULT, PGMAJFAULT,
PGLAZYFREED,
PGREUSE,
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 15bb66e36304..a9058d414f92 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -1313,6 +1313,8 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)

#endif /* CONFIG_MEM_ALLOC_PROFILING */

+#define RCU_FREE_EXPEDITE (SZ_4M >> PAGE_SHIFT)
+
/*
* Each CPU has at most one grace period in flight (@queued), covering
* @batch. Folios freed meanwhile wait on @pending for the next one.
@@ -1322,7 +1324,9 @@ struct rcu_free_pcpu {
bool queued;
struct list_head pending;
struct list_head batch;
+ unsigned long nr_pending;
struct rcu_work rwork;
+ struct work_struct expedite;
};

static DEFINE_PER_CPU(struct rcu_free_pcpu, rcu_free_pcpu);
@@ -1337,17 +1341,29 @@ static void rcu_free_start(struct rcu_free_pcpu *r)
return;

list_splice_init(&r->pending, &r->batch);
+ r->nr_pending = 0;
r->queued = true;
WARN_ON_ONCE(!queue_rcu_work(rcu_free_wq, &r->rwork));
}

-static void rcu_free_pages(struct list_head *list)
+static void rcu_free_take(struct rcu_free_pcpu *r, struct list_head *list)
+{
+ lockdep_assert_held(&r->lock);
+
+ list_splice_tail_init(&r->batch, list);
+ list_splice_tail_init(&r->pending, list);
+ r->nr_pending = 0;
+}
+
+static unsigned long rcu_free_pages(struct list_head *list)
{
struct folio_batch fbatch;
struct folio *folio, *next;
+ unsigned long nr = 0;

folio_batch_init(&fbatch);
list_for_each_entry_safe(folio, next, list, lru) {
+ nr += folio_nr_pages(folio);
mod_zone_page_state(folio_zone(folio), NR_FREE_RCU_PAGES,
-folio_nr_pages(folio));
if (!folio_batch_add(&fbatch, folio)) {
@@ -1357,6 +1373,17 @@ static void rcu_free_pages(struct list_head *list)
}
if (folio_batch_count(&fbatch))
free_unref_folios(&fbatch);
+ return nr;
+}
+
+/* Free what rcu_free_take() took: the grace period starts after that */
+static unsigned long rcu_free_flush(struct list_head *list)
+{
+ if (list_empty(list))
+ return 0;
+
+ synchronize_rcu_expedited();
+ return rcu_free_pages(list);
}

static void rcu_free_workfn(struct work_struct *work)
@@ -1374,17 +1401,48 @@ static void rcu_free_workfn(struct work_struct *work)
rcu_free_pages(&done);
}

+static void rcu_free_expedite_workfn(struct work_struct *work)
+{
+ struct rcu_free_pcpu *r = container_of(work, struct rcu_free_pcpu,
+ expedite);
+ LIST_HEAD(list);
+
+ spin_lock_irq(&r->lock);
+ rcu_free_take(r, &list);
+ spin_unlock_irq(&r->lock);
+
+ if (rcu_free_flush(&list))
+ count_vm_event(PGFREE_RCU_EXPEDITE);
+}
+
static void rcu_free_defer(struct folio *folio)
{
struct rcu_free_pcpu *r = raw_cpu_ptr(&rcu_free_pcpu);
struct zone *zone = folio_zone(folio);
+ unsigned long free, waiting;
unsigned long flags;

spin_lock_irqsave(&r->lock, flags);
__mod_zone_page_state(zone, NR_FREE_RCU_PAGES, folio_nr_pages(folio));
list_add_tail(&folio->lru, &r->pending);
+ r->nr_pending += folio_nr_pages(folio);
rcu_free_start(r);
spin_unlock_irqrestore(&r->lock, flags);
+
+ /* Unlocked: a stale count delays the expedite by one free */
+ if (data_race(r->nr_pending) <= RCU_FREE_EXPEDITE ||
+ work_pending(&r->expedite))
+ return;
+
+ /*
+ * An expedited grace period interrupts every CPU: only while the
+ * zone is short of memory, or while more than an eighth of what it
+ * has free waits here.
+ */
+ free = zone_page_state(zone, NR_FREE_PAGES);
+ waiting = zone_page_state(zone, NR_FREE_RCU_PAGES);
+ if (free < high_wmark_pages(zone) || waiting > free / 8)
+ queue_work(rcu_free_wq, &r->expedite);
}

static void __init rcu_free_init(void)
@@ -1404,6 +1462,7 @@ static void __init rcu_free_init(void)
INIT_LIST_HEAD(&r->pending);
INIT_LIST_HEAD(&r->batch);
INIT_RCU_WORK(&r->rwork, rcu_free_workfn);
+ INIT_WORK(&r->expedite, rcu_free_expedite_workfn);
}
}

diff --git a/mm/vmstat.c b/mm/vmstat.c
index 64068d73d6d2..aa7280b5e69c 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1345,6 +1345,7 @@ const char * const vmstat_text[] = {
[I(PGDEACTIVATE)] = "pgdeactivate",
[I(PGLAZYFREE)] = "pglazyfree",
[I(PGFREE_RCU)] = "pgfree_rcu",
+ [I(PGFREE_RCU_EXPEDITE)] = "pgfree_rcu_expedite",

[I(PGFAULT)] = "pgfault",
[I(PGMAJFAULT)] = "pgmajfault",
--
2.54.0