Re: [PATCH] mm: use per-numa-node atomics instead of percpu_counters

From: Mathieu Desnoyers
Date: Wed Mar 26 2025 - 15:56:31 EST


On 2025-03-25 18:15, Sweet Tea Dorminy wrote:
From: Sweet Tea Dorminy <sweettea@xxxxxxxxxx>

Recently, several internal services had an RSS usage regression as part of a
kernel upgrade. Previously, they were on a pre-6.2 kernel and were able to
read RSS statistics in a backup watchdog process to monitor and decide if
they'd overrun their memory budget. Now, however, a representative service
with five threads, expected to use about a hundred MB of memory, on a 250-cpu
machine had memory usage tens of megabytes different from the expected amount
-- this constituted a significant percentage of inaccuracy, causing the
watchdog to act.


I suspect the culprit sits here:

int percpu_counter_batch __read_mostly = 32;
EXPORT_SYMBOL(percpu_counter_batch);

static int compute_batch_value(unsigned int cpu)
{
int nr = num_online_cpus();

percpu_counter_batch = max(32, nr*2);
return 0;
}

So correct me if I'm wrong, but in this case the worse-case
inaccuracy for a 256 cpu machine would be
"+/- percpu_counter_batch" within each percpu counter,
thus globally:

+/- (256 * 2) * 256, or 131072 pages, meaning an inaccuracy
of +/- 512MB with 4kB pages. This is quite significant.

So I understand that the batch size is scaled up as the
number of CPUs increases to minimize contention on the
percpu_counter lock. Unfortunately, as the number of CPUs
increases, the inaccuracy increases with the square of the
number of cpus.

Have you tried decreasing this percpu_counter_batch value on
larger machines to see if it helps ?

Thanks,

Mathieu

[...]


base-commit: b0cb56cbbdb4754918c28d6d7c294d56e28a3dd5
prerequisite-patch-id: b7b47d1b9aa3fd887dd718ab276581f120e516e6

[...][ cutting the gazillions prerequisite-patch-id, please review you use
of tooling when preparing/sending patches. ]

--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com