Every memory cgroup allocates, per NUMA node, one shrinker_info_unit for every SHRINKER_UNIT_BITS shrinker ids up to shrinker_nr_max. The unit holds BITS_PER_LONG atomic_long_t of deferred work, a power of two, plus the one-word bitmap of shrinkers with objects charged to the cgroup. That word takes the unit just past the power of two, to 520 bytes on 64-bit, so every unit comes from kmalloc-1k and almost half of it is padding. The units are allocated when the cgroup goes online and freed when it is freed, so a dying cgroup keeps them until the last reference to it is gone. Their number is the number of memory cgroups, times the NUMA nodes, times shrinker_nr_max / SHRINKER_UNIT_BITS. shrinker_nr_max is the peak number of memcg-aware shrinkers registered at once, and every superblock registers one, so a host that mounts many filesystems and runs many cgroups carries the padding as unreclaimable slab, and keeps paying it for new cgroups after the mounts are gone. Let a unit cover one id less than a word holds. The bitmap still fits one word, and nr_deferred[] plus that word add up to BITS_PER_LONG longs, so the unit fills a power-of-two kmalloc slot exactly and, on 64-bit, the memory taken by units halves, for about one unit more in BITS_PER_LONG. round_up() in expand_shrinker_info() needs a power-of-two step and becomes roundup(); the other uses of SHRINKER_UNIT_BITS are divisions, modulos and multiplications that do not care. Assert the unit size at build time, since the layout depends on it. Assisted-by: LLM Signed-off-by: Konstantin Khorenko --- include/linux/shrinker.h | 6 +++++- mm/shrinker.c | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h index 1a00be90d93a9..f1cf6096a7a06 100644 --- a/include/linux/shrinker.h +++ b/include/linux/shrinker.h @@ -7,7 +7,11 @@ #include #include -#define SHRINKER_UNIT_BITS BITS_PER_LONG +/* + * One id less than a word holds, so that nr_deferred[] and the map word add + * up to BITS_PER_LONG longs: a power of two, which fills a kmalloc slot. + */ +#define SHRINKER_UNIT_BITS (BITS_PER_LONG - 1) /* * Bitmap and deferred work of shrinker::id corresponding to memcg-aware diff --git a/mm/shrinker.c b/mm/shrinker.c index 7ec2a9704f6f2..5e4a5ce231868 100644 --- a/mm/shrinker.c +++ b/mm/shrinker.c @@ -46,6 +46,8 @@ static inline int shrinker_unit_alloc(struct shrinker_info *new, int start = old ? DIV_ROUND_UP(old->map_nr_max, SHRINKER_UNIT_BITS) : 0; int i; + BUILD_BUG_ON(!is_power_of_2(sizeof(struct shrinker_info_unit))); + for (i = start; i < nr; i++) { unit = kzalloc_node(sizeof(*unit), GFP_KERNEL, nid); if (!unit) { @@ -159,7 +161,7 @@ static int expand_one_shrinker_info(struct mem_cgroup *memcg, int new_size, static int expand_shrinker_info(int new_id) { int ret = 0; - int new_nr_max = round_up(new_id + 1, SHRINKER_UNIT_BITS); + int new_nr_max = roundup(new_id + 1, SHRINKER_UNIT_BITS); int new_size, old_size = 0; struct mem_cgroup *memcg; -- 2.43.0