Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: memcg/percpu: per-memcg percpu memory statistics

Percpu memory can represent a noticeable chunk of the total memory
consumption, especially on big machines with many CPUs. Let's track
percpu memory usage for each memcg and display it in memory.stat.

A percpu allocation is usually scattered over multiple pages (and nodes),
and can be significantly smaller than a page. So let's add a byte-sized
counter on the memcg level: MEMCG_PERCPU_B. Byte-sized vmstat infra
created for slabs can be perfectly reused for percpu case.

[guro@fb.com: v3]
Link: http://lkml.kernel.org/r/20200623184515.4132564-4-guro@fb.com

Signed-off-by: Roman Gushchin <guro@fb.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Dennis Zhou <dennis@kernel.org>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Christoph Lameter <cl@linux.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Mel Gorman <mgorman@techsingularity.net>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: Tobin C. Harding <tobin@kernel.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Waiman Long <longman@redhat.com>
Cc: Bixuan Cui <cuibixuan@huawei.com>
Cc: Michal Koutný <mkoutny@suse.com>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Link: http://lkml.kernel.org/r/20200608230819.832349-4-guro@fb.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Roman Gushchin and committed by
Linus Torvalds
772616b0 3c7be18a

+25 -1
+4
Documentation/admin-guide/cgroup-v2.rst
··· 1274 1274 Amount of memory used for storing in-kernel data 1275 1275 structures. 1276 1276 1277 + percpu 1278 + Amount of memory used for storing per-cpu kernel 1279 + data structures. 1280 + 1277 1281 sock 1278 1282 Amount of memory used in network transmission buffers 1279 1283
+8
include/linux/memcontrol.h
··· 32 32 enum memcg_stat_item { 33 33 MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS, 34 34 MEMCG_SOCK, 35 + MEMCG_PERCPU_B, 35 36 MEMCG_NR_STAT, 36 37 }; 37 38 ··· 339 338 #define MEMCG_CHARGE_BATCH 32U 340 339 341 340 extern struct mem_cgroup *root_mem_cgroup; 341 + 342 + static __always_inline bool memcg_stat_item_in_bytes(int idx) 343 + { 344 + if (idx == MEMCG_PERCPU_B) 345 + return true; 346 + return vmstat_item_in_bytes(idx); 347 + } 342 348 343 349 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) 344 350 {
+3 -1
mm/memcontrol.c
··· 781 781 if (mem_cgroup_disabled()) 782 782 return; 783 783 784 - if (vmstat_item_in_bytes(idx)) 784 + if (memcg_stat_item_in_bytes(idx)) 785 785 threshold <<= PAGE_SHIFT; 786 786 787 787 x = val + __this_cpu_read(memcg->vmstats_percpu->stat[idx]); ··· 1488 1488 seq_buf_printf(&s, "slab %llu\n", 1489 1489 (u64)(memcg_page_state(memcg, NR_SLAB_RECLAIMABLE_B) + 1490 1490 memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE_B))); 1491 + seq_buf_printf(&s, "percpu %llu\n", 1492 + (u64)memcg_page_state(memcg, MEMCG_PERCPU_B)); 1491 1493 seq_buf_printf(&s, "sock %llu\n", 1492 1494 (u64)memcg_page_state(memcg, MEMCG_SOCK) * 1493 1495 PAGE_SIZE);
+10
mm/percpu.c
··· 1610 1610 1611 1611 if (chunk) { 1612 1612 chunk->obj_cgroups[off >> PCPU_MIN_ALLOC_SHIFT] = objcg; 1613 + 1614 + rcu_read_lock(); 1615 + mod_memcg_state(obj_cgroup_memcg(objcg), MEMCG_PERCPU_B, 1616 + size * num_possible_cpus()); 1617 + rcu_read_unlock(); 1613 1618 } else { 1614 1619 obj_cgroup_uncharge(objcg, size * num_possible_cpus()); 1615 1620 obj_cgroup_put(objcg); ··· 1632 1627 chunk->obj_cgroups[off >> PCPU_MIN_ALLOC_SHIFT] = NULL; 1633 1628 1634 1629 obj_cgroup_uncharge(objcg, size * num_possible_cpus()); 1630 + 1631 + rcu_read_lock(); 1632 + mod_memcg_state(obj_cgroup_memcg(objcg), MEMCG_PERCPU_B, 1633 + -(size * num_possible_cpus())); 1634 + rcu_read_unlock(); 1635 1635 1636 1636 obj_cgroup_put(objcg); 1637 1637 }