[PATCH v5 5/7] mm/page_counter: introduce an asynchronous drainer
From: Joshua Hahn
Date: Mon Aug 31 2026 - 15:46:32 EST
The existing percpu memcg stock drainer schedules a stock drain worker
per-cpu, one for every CPU containing the target memcg's stock.
One issue with this design is the coarseness of the drain; when a worker
runs on a CPU, it drains not only the target memcg's stock, but all
other (up to 6) memcgs who are stocked on that CPU.
Instead, use a per-page_counter drainer that iterates through each CPU
and flushes any existing charges, leaving other unrelated memcgs' stock
alone. Since that walks every possible CPU, the per-cpu helper now skips
the remote lock when a stock looks empty.
One benefit of having one worker flush through all CPUs is that
duplicate drain requests when a worker is already queued are coalesced,
since the asynchronous drainer takes no arguments and flushes all CPUs.
We use the system_dfl_wq for this asynchronous drain worker, since
memcg_wq is a percpu workqueue. Note that neither workqueue has
WQ_MEM_RECLAIM.
Previously the local CPU was drained inline because local_lock made it
the only reachable stock. The lock is now a per-cpu raw_spinlock_t that
any CPU can take, so drain any CPU's stock to return pages immediately.
Suggested-by: Johannes Weiner <hannes@xxxxxxxxxxx>
Signed-off-by: Joshua Hahn <joshua.hahnjy@xxxxxxxxx>
---
include/linux/page_counter.h | 1 +
mm/page_counter.c | 55 ++++++++++++++++++++++++++++++++----
2 files changed, 50 insertions(+), 6 deletions(-)
diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h
index 428ca8e7b2da5..b10ef785f06de 100644
--- a/include/linux/page_counter.h
+++ b/include/linux/page_counter.h
@@ -114,6 +114,7 @@ static inline void page_counter_reset_watermark(struct page_counter *counter)
}
void page_counter_drain_cpu_stock(struct page_counter *counter, int cpu);
+void page_counter_drain_stock_async(struct page_counter *counter);
void page_counter_alloc_stock(struct page_counter *counter, unsigned long batch);
void page_counter_free_stock(struct page_counter *counter);
diff --git a/mm/page_counter.c b/mm/page_counter.c
index a76949abf04e7..e6cfb5865ba75 100644
--- a/mm/page_counter.c
+++ b/mm/page_counter.c
@@ -12,6 +12,7 @@
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
+#include <linux/workqueue.h>
#include <linux/bug.h>
#include <asm/page.h>
@@ -135,7 +136,7 @@ static bool page_counter_consume_stock(struct page_counter *counter,
return false;
if (pcp_stock->nr_pages >= nr_pages) {
- pcp_stock->nr_pages -= nr_pages;
+ WRITE_ONCE(pcp_stock->nr_pages, pcp_stock->nr_pages - nr_pages);
charged = true;
}
@@ -183,10 +184,10 @@ unsigned long page_counter_refill_stock(struct page_counter *counter,
*/
stocked = pcp_stock->nr_pages + overage;
if (stocked > high) {
- pcp_stock->nr_pages = low;
+ WRITE_ONCE(pcp_stock->nr_pages, low);
to_flush = stocked - low;
} else {
- pcp_stock->nr_pages = stocked;
+ WRITE_ONCE(pcp_stock->nr_pages, stocked);
to_flush = 0;
}
raw_spin_unlock_irqrestore(&pcp_stock->lock, flags);
@@ -429,15 +430,53 @@ void page_counter_drain_cpu_stock(struct page_counter *counter, int cpu)
return;
pcp_stock = per_cpu_ptr(stock, cpu);
+
+ /*
+ * Skip the remote lock when empty. Racing with the charge path is why
+ * nr_pages uses WRITE_ONCE(); a stale read defers to the next drain.
+ */
+ if (!READ_ONCE(pcp_stock->nr_pages))
+ return;
+
raw_spin_lock_irqsave(&pcp_stock->lock, flags);
nr_pages = pcp_stock->nr_pages;
- pcp_stock->nr_pages = 0;
+ WRITE_ONCE(pcp_stock->nr_pages, 0);
raw_spin_unlock_irqrestore(&pcp_stock->lock, flags);
if (nr_pages)
page_counter_uncharge(counter, nr_pages);
}
+static void page_counter_drain_work_fn(struct work_struct *work)
+{
+ struct page_counter *counter = container_of(work, struct page_counter,
+ drain_work);
+ int cpu;
+
+ for_each_possible_cpu(cpu)
+ page_counter_drain_cpu_stock(counter, cpu);
+}
+
+/**
+ * page_counter_drain_stock_async - schedule a page_counter stock drain
+ * @counter: page_counter to drain
+ *
+ * Drains any CPU's stock inline, then schedules a drain over every CPU and
+ * returns. Concurrent requests coalesce onto the same queued work. @counter
+ * must outlive that work, which page_counter_free_stock() cancels.
+ * Must not be called from NMI context.
+ */
+void page_counter_drain_stock_async(struct page_counter *counter)
+{
+ /* Pairs with the smp_store_release() in page_counter_alloc_stock() */
+ if (!smp_load_acquire(&counter->stock))
+ return;
+
+ /* Drain any CPU's stock to immediately return pages; migrating is OK */
+ page_counter_drain_cpu_stock(counter, raw_smp_processor_id());
+ queue_work(system_dfl_wq, &counter->drain_work);
+}
+
/**
* page_counter_alloc_stock - allocate the percpu stock for a page_counter
* @counter: counter to allocate percpu stock for
@@ -469,6 +508,7 @@ void page_counter_alloc_stock(struct page_counter *counter, unsigned long batch)
}
counter->batch = batch;
+ INIT_WORK(&counter->drain_work, page_counter_drain_work_fn);
/* Publish stock only after percpu allocs / inits are finished */
smp_store_release(&counter->stock, stock);
}
@@ -477,8 +517,9 @@ void page_counter_alloc_stock(struct page_counter *counter, unsigned long batch)
* page_counter_free_stock - free @counter's percpu cached charge
* @counter: page_counter whose stock to free
*
- * Caller must guarantee no (un)charge or drain of @counter is in flight or can
- * start. memcg only calls this once the cgroup is dead and unreachable.
+ * Caller must guarantee no (un)charge or drain of @counter can start, and must
+ * be ordered against the last CPU to touch the stock, since the drain peeks at
+ * nr_pages unlocked. memcg's RCU grace period before css_free provides both.
*/
void page_counter_free_stock(struct page_counter *counter)
{
@@ -490,6 +531,8 @@ void page_counter_free_stock(struct page_counter *counter)
/* Stop greedy over-charging before the stock goes away */
counter->batch = 0;
+ /* Make sure pending drainers don't run on freed page_counters */
+ cancel_work_sync(&counter->drain_work);
for_each_possible_cpu(cpu)
page_counter_drain_cpu_stock(counter, cpu);
--
2.53.0-Meta