Re: [RFC PATCH v2 01/18] mm: free PG_rcu_free folios only after an RCU grace period

From: Kiryl Shutsemau

Date: Fri Nov 13 2026 - 17:40:56 EST


On Thu, Nov 12, 2026 at 01:32:02PM +0100, David Hildenbrand (Arm) wrote:
> On 11/12/26 13:26, Kiryl Shutsemau wrote:
> > On Wed, Nov 11, 2026 at 11:03:38AM +0100, David Hildenbrand (Arm) wrote:
> >> So I might be completely off, but what I imagine is that the memdescs will get
> >> RCU-freed (these are not pages), and once they are then getting freed, they will
> >> probably take the physical page with them.
> >>
> >> And freeing memdescs through RCU will likely be memdesc specific.
> >>
> >> I really don't know, Willy might know more about these plans.
> >
> > Right: once the folio is allocated separately, the lockless read
> > dereferences it too, so it has to wait for the grace period along with
> > its pages. The per-CPU batching and the waits in the allocator later
> > in the series do not depend on where the check sits.
> >
> > Until then the folio is the struct page, and __free_pages_prepare() is
> > the one place every page cache folio passes, so v3 keeps the check
> > there.
>
> If we go that way, don't make it effectively folio-centric in the
> implementation. That's what triggered my response in the first place.

Is something like this what you mean?

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index d3862d68a8d9..5b9ac91d25ae 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -1314,7 +1314,7 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)

/*
* Each CPU has at most one grace period in flight (@queued), covering
- * @batch. Folios freed meanwhile wait on @pending for the next one.
+ * @batch. Pages freed meanwhile wait on @pending for the next one.
*/
struct rcu_free_pcpu {
spinlock_t lock;
@@ -1354,28 +1354,26 @@ static void rcu_free_take(struct rcu_free_pcpu *r, struct list_head *list)

static unsigned long rcu_free_pages(struct list_head *list, bool direct)
{
- struct folio_batch fbatch;
- struct folio *folio, *next;
+ struct page *page, *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));
+ list_for_each_entry_safe(page, next, list, lru) {
+ unsigned int order = page_private(page);
+
+ set_page_private(page, 0);
+ nr += 1 << order;
+ mod_zone_page_state(page_zone(page), NR_FREE_RCU_PAGES,
+ -(1 << order));
/* Atomic: memory_failure() may set PG_hwpoison meanwhile */
- folio_clear_rcu_free(folio);
+ folio_clear_rcu_free(page_folio(page));
if (direct) {
- __free_pages_ok(&folio->page, folio_order(folio),
- FPI_NONE);
+ __free_pages_ok(page, order, FPI_NONE);
cond_resched();
- } else if (!folio_batch_add(&fbatch, folio)) {
- free_unref_folios(&fbatch);
+ } else {
+ free_frozen_pages(page, order);
cond_resched_tasks_rcu_qs();
}
}
- if (folio_batch_count(&fbatch))
- free_unref_folios(&fbatch);
return nr;
}

@@ -1421,17 +1419,20 @@ static void rcu_free_expedite_workfn(struct work_struct *work)
count_vm_event(PGFREE_RCU_EXPEDITE);
}

-static void rcu_free_defer(struct folio *folio)
+static void rcu_free_defer(struct page *page, unsigned int order)
{
struct rcu_free_pcpu *r = raw_cpu_ptr(&rcu_free_pcpu);
- struct zone *zone = folio_zone(folio);
+ struct zone *zone = page_zone(page);
unsigned long free, waiting;
unsigned long flags;

+ /* For rcu_free_pages(): not every high-order page is compound */
+ set_page_private(page, order);
+
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);
+ __mod_zone_page_state(zone, NR_FREE_RCU_PAGES, 1 << order);
+ list_add_tail(&page->lru, &r->pending);
+ r->nr_pending += 1 << order;
rcu_free_start(r);
spin_unlock_irqrestore(&r->lock, flags);

@@ -1452,7 +1453,7 @@ static void rcu_free_defer(struct folio *folio)
}

/*
- * Free every waiting folio now, straight to the buddy lists, where the
+ * Free every waiting page now, straight to the buddy lists, where the
* callers look for the pages next. A concurrent drain holds the lock
* until it has freed what it took.
*/
@@ -1528,7 +1529,7 @@ static __always_inline bool __free_pages_prepare(struct page *page,
!(PageHWPoison(page) && !order)) {
VM_WARN_ON_ONCE(fpi_flags & FPI_NOLOCK);
count_vm_events(PGFREE_RCU, 1 << order);
- rcu_free_defer(folio);
+ rcu_free_defer(page, order);
return false;
}

--
Kiryl Shutsemau / Kirill A. Shutemov