From: Ionut Nechita Since commit be5e015d107d ("vmstat: skip periodic vmstat update for isolated CPUs"), vmstat_shepherd() never queues vmstat_update() on isolated CPUs, and quiet_vmstat() only folds the counters with refresh_cpu_vm_stats(false). Commit 51a755c56dc0 ("mm: tune PCP high automatically") later made pcp->high grow on demand, up to roughly 1/8 of the zone's managed memory spread over the node's CPUs, and relies on decay_pcp_high(), called from refresh_cpu_vm_stats(true), to shrink it again and to give back the pages cached above it. That path never runs on isolated CPUs. As a result, once a workload on an isolated (nohz_full) CPU has frees that push pcp->high up, both pcp->high and the pages on the pcp lists stay there for good, even after the workload is gone. Those pages are neither counted in MemFree/MemAvailable nor attributed in /proc/allocinfo, so the memory looks lost. On a single-node, 64-CPU Sapphire Rapids system (256 GB) running a PREEMPT_RT kernel with nohz_full=1-31,33-63 and a containerized telco workload, about 28 GiB ended up on the pcp lists of the isolated CPUs (pcp->count close to pcp->high_max on most of them) after 8 days of uptime and showed up as unaccounted kernel memory. Lowering vm.percpu_pagelist_high_fraction lowered pcp->high but left idle isolated CPUs holding 300-450 MiB each; only drain_all_pages() gave the memory back (MemFree went from 126.5 to 154.5 GiB, MemAvailable from 157 to 185 GiB). Do the decay remotely: for isolated CPUs, let vmstat_shepherd(), which runs on a housekeeping CPU, call decay_pcp_high() on their pagesets. decay_pcp_high() now takes pcp->lock while it adjusts pcp->high, as the pageset may belong to a remote CPU; the draining part already took it. Remote pcp access under pcp->lock is already done by drain_pages_zone(). To keep the interference on isolated CPUs minimal, the remote pcp->lock is only taken when pcp->high is above high_min or pcp->count is above pcp->high, which is only the case after auto-tuning raised pcp->high. Fixes: 51a755c56dc0 ("mm: tune PCP high automatically") Signed-off-by: Ionut Nechita --- mm/page_alloc.c | 43 +++++++++++++++++++++++++++++++++++++++++-- mm/page_alloc.h | 1 + mm/vmstat.c | 14 ++++++++++---- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index ce44b8fc4..aa9904eaf 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -2586,8 +2586,13 @@ bool decay_pcp_high(struct zone *zone, struct per_cpu_pages *pcp) int high_min, to_drain, to_drain_batched, batch; bool todo = false; - high_min = READ_ONCE(pcp->high_min); - batch = READ_ONCE(pcp->batch); + /* + * pcp->lock is taken because the pageset may belong to a remote CPU, + * see decay_pcp_high_cpu(). + */ + pcp_spin_lock_nopin(pcp); + high_min = pcp->high_min; + batch = pcp->batch; /* * Decrease pcp->high periodically to try to free possible * idle PCP pages. And, avoid to free too many pages to @@ -2601,6 +2606,7 @@ bool decay_pcp_high(struct zone *zone, struct per_cpu_pages *pcp) } to_drain = pcp->count - pcp->high; + pcp_spin_unlock_nopin(pcp); while (to_drain > 0) { to_drain_batched = min(to_drain, batch); pcp_spin_lock_nopin(pcp); @@ -2614,6 +2620,39 @@ bool decay_pcp_high(struct zone *zone, struct per_cpu_pages *pcp) return todo; } +/* + * Isolated CPUs are skipped by vmstat_shepherd() and therefore never run + * vmstat_update(), which is the only caller of decay_pcp_high() for the + * local pagesets. Once PCP auto-tuning has raised pcp->high on such a CPU, + * neither pcp->high nor the pages cached above high_min are ever given back, + * and they are not accounted as free memory either. + * + * Decay the pagesets of @cpu remotely instead. This is called from the + * housekeeping CPU running vmstat_shepherd() and only takes the remote + * pcp->lock when there is something to do, so an isolated CPU whose + * pagesets are at or below high_min sees no interference. + */ +void decay_pcp_high_cpu(unsigned int cpu) +{ + struct zone *zone; + + for_each_populated_zone(zone) { + struct per_cpu_pages *pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); + + /* + * Lockless peek to avoid taking the remote pcp->lock when + * there is nothing to decay; a stale value only delays the + * decay until the next vmstat_shepherd() run. + */ + if (data_race(pcp->high <= pcp->high_min && + pcp->count <= pcp->high)) + continue; + + decay_pcp_high(zone, pcp); + cond_resched(); + } +} + #ifdef CONFIG_NUMA /* * Called from the vmstat counter updater to drain pagesets of this diff --git a/mm/page_alloc.h b/mm/page_alloc.h index 58386512e..85cd958cc 100644 --- a/mm/page_alloc.h +++ b/mm/page_alloc.h @@ -334,6 +334,7 @@ static inline int gfp_migratetype(const gfp_t gfp_flags) #undef GFP_MOVABLE_SHIFT bool decay_pcp_high(struct zone *zone, struct per_cpu_pages *pcp); +void decay_pcp_high_cpu(unsigned int cpu); void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp); void drain_all_pages(struct zone *zone); diff --git a/mm/vmstat.c b/mm/vmstat.c index c6beac6be..5742ca508 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -2148,6 +2148,7 @@ static void vmstat_shepherd(struct work_struct *w) /* Check processors whose vmstat worker threads have been disabled */ for_each_online_cpu(cpu) { struct delayed_work *dw = &per_cpu(vmstat_work, cpu); + bool isolated; /* * In kernel users of vmstat counters either require the precise value and @@ -2161,13 +2162,18 @@ static void vmstat_shepherd(struct work_struct *w) * for all isolated CPUs to avoid interference with the isolated workload. */ scoped_guard(rcu) { - if (cpu_is_isolated(cpu)) - continue; - - if (!work_busy(&dw->work) && need_update(cpu)) + isolated = cpu_is_isolated(cpu); + if (!isolated && !work_busy(&dw->work) && need_update(cpu)) queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0); } + /* + * Isolated CPUs never run vmstat_update(), so the pcp->high + * decay it performs has to be done remotely from here. + */ + if (isolated) + decay_pcp_high_cpu(cpu); + cond_resched(); } cpus_read_unlock(); -- 2.55.0