Skip to content

Commit a8c49af

Browse files
yosrym93torvalds
authored andcommitted
memcg: add per-memcg total kernel memory stat
Currently memcg stats show several types of kernel memory: kernel stack, page tables, sock, vmalloc, and slab. However, there are other allocations with __GFP_ACCOUNT (or supersets such as GFP_KERNEL_ACCOUNT) that are not accounted in any of those stats, a few examples are: - various kvm allocations (e.g. allocated pages to create vcpus) - io_uring - tmp_page in pipes during pipe_write() - bpf ringbuffers - unix sockets Keeping track of the total kernel memory is essential for the ease of migration from cgroup v1 to v2 as there are large discrepancies between v1's kmem.usage_in_bytes and the sum of the available kernel memory stats in v2. Adding separate memcg stats for all __GFP_ACCOUNT kernel allocations is an impractical maintenance burden as there a lot of those all over the kernel code, with more use cases likely to show up in the future. Therefore, add a "kernel" memcg stat that is analogous to kmem page counter, with added benefits such as using rstat infrastructure which aggregates stats more efficiently. Additionally, this provides a lighter alternative in case the legacy kmem is deprecated in the future [[email protected]: v2] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Yosry Ahmed <[email protected]> Acked-by: Shakeel Butt <[email protected]> Acked-by: Johannes Weiner <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Muchun Song <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 086f694 commit a8c49af

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

Documentation/admin-guide/cgroup-v2.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,11 @@ PAGE_SIZE multiple when read back.
13011301
Amount of memory used to cache filesystem data,
13021302
including tmpfs and shared memory.
13031303

1304+
kernel (npn)
1305+
Amount of total kernel memory, including
1306+
(kernel_stack, pagetables, percpu, vmalloc, slab) in
1307+
addition to other kernel memory use cases.
1308+
13041309
kernel_stack
13051310
Amount of memory allocated to kernel stacks.
13061311

include/linux/memcontrol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum memcg_stat_item {
3434
MEMCG_SOCK,
3535
MEMCG_PERCPU_B,
3636
MEMCG_VMALLOC,
37+
MEMCG_KMEM,
3738
MEMCG_NR_STAT,
3839
};
3940

mm/memcontrol.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ struct memory_stat {
13711371
static const struct memory_stat memory_stats[] = {
13721372
{ "anon", NR_ANON_MAPPED },
13731373
{ "file", NR_FILE_PAGES },
1374+
{ "kernel", MEMCG_KMEM },
13741375
{ "kernel_stack", NR_KERNEL_STACK_KB },
13751376
{ "pagetables", NR_PAGETABLE },
13761377
{ "percpu", MEMCG_PERCPU_B },
@@ -2114,6 +2115,7 @@ static DEFINE_MUTEX(percpu_charge_mutex);
21142115
static void drain_obj_stock(struct obj_stock *stock);
21152116
static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
21162117
struct mem_cgroup *root_memcg);
2118+
static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages);
21172119

21182120
#else
21192121
static inline void drain_obj_stock(struct obj_stock *stock)
@@ -2124,6 +2126,9 @@ static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
21242126
{
21252127
return false;
21262128
}
2129+
static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages)
2130+
{
2131+
}
21272132
#endif
21282133

21292134
/**
@@ -2979,6 +2984,18 @@ static void memcg_free_cache_id(int id)
29792984
ida_simple_remove(&memcg_cache_ida, id);
29802985
}
29812986

2987+
static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages)
2988+
{
2989+
mod_memcg_state(memcg, MEMCG_KMEM, nr_pages);
2990+
if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
2991+
if (nr_pages > 0)
2992+
page_counter_charge(&memcg->kmem, nr_pages);
2993+
else
2994+
page_counter_uncharge(&memcg->kmem, -nr_pages);
2995+
}
2996+
}
2997+
2998+
29822999
/*
29833000
* obj_cgroup_uncharge_pages: uncharge a number of kernel pages from a objcg
29843001
* @objcg: object cgroup to uncharge
@@ -2991,8 +3008,7 @@ static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
29913008

29923009
memcg = get_mem_cgroup_from_objcg(objcg);
29933010

2994-
if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
2995-
page_counter_uncharge(&memcg->kmem, nr_pages);
3011+
memcg_account_kmem(memcg, -nr_pages);
29963012
refill_stock(memcg, nr_pages);
29973013

29983014
css_put(&memcg->css);
@@ -3018,8 +3034,7 @@ static int obj_cgroup_charge_pages(struct obj_cgroup *objcg, gfp_t gfp,
30183034
if (ret)
30193035
goto out;
30203036

3021-
if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
3022-
page_counter_charge(&memcg->kmem, nr_pages);
3037+
memcg_account_kmem(memcg, nr_pages);
30233038
out:
30243039
css_put(&memcg->css);
30253040

@@ -6801,8 +6816,8 @@ static void uncharge_batch(const struct uncharge_gather *ug)
68016816
page_counter_uncharge(&ug->memcg->memory, ug->nr_memory);
68026817
if (do_memsw_account())
68036818
page_counter_uncharge(&ug->memcg->memsw, ug->nr_memory);
6804-
if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && ug->nr_kmem)
6805-
page_counter_uncharge(&ug->memcg->kmem, ug->nr_kmem);
6819+
if (ug->nr_kmem)
6820+
memcg_account_kmem(ug->memcg, -ug->nr_kmem);
68066821
memcg_oom_recover(ug->memcg);
68076822
}
68086823

0 commit comments

Comments
 (0)