Skip to content

Commit 991e767

Browse files
shakeelbtorvalds
authored andcommitted
mm: memcontrol: account kernel stack per node
Currently the kernel stack is being accounted per-zone. There is no need to do that. In addition due to being per-zone, memcg has to keep a separate MEMCG_KERNEL_STACK_KB. Make the stat per-node and deprecate MEMCG_KERNEL_STACK_KB as memcg_stat_item is an extension of node_stat_item. In addition localize the kernel stack stats updates to account_kernel_stack(). Signed-off-by: Shakeel Butt <shakeelb@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reviewed-by: Roman Gushchin <guro@fb.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@kernel.org> Link: http://lkml.kernel.org/r/20200630161539.1759185-1-shakeelb@google.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fbc1ac9 commit 991e767

9 files changed

Lines changed: 55 additions & 61 deletions

File tree

‎drivers/base/node.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,9 @@ static ssize_t node_read_meminfo(struct device *dev,
440440
nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
441441
nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
442442
nid, K(i.sharedram),
443-
nid, sum_zone_node_page_state(nid, NR_KERNEL_STACK_KB),
443+
nid, node_page_state(pgdat, NR_KERNEL_STACK_KB),
444444
#ifdef CONFIG_SHADOW_CALL_STACK
445-
nid, sum_zone_node_page_state(nid, NR_KERNEL_SCS_KB),
445+
nid, node_page_state(pgdat, NR_KERNEL_SCS_KB),
446446
#endif
447447
nid, K(sum_zone_node_page_state(nid, NR_PAGETABLE)),
448448
nid, 0UL,

‎fs/proc/meminfo.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
101101
show_val_kb(m, "SReclaimable: ", sreclaimable);
102102
show_val_kb(m, "SUnreclaim: ", sunreclaim);
103103
seq_printf(m, "KernelStack: %8lu kB\n",
104-
global_zone_page_state(NR_KERNEL_STACK_KB));
104+
global_node_page_state(NR_KERNEL_STACK_KB));
105105
#ifdef CONFIG_SHADOW_CALL_STACK
106106
seq_printf(m, "ShadowCallStack:%8lu kB\n",
107-
global_zone_page_state(NR_KERNEL_SCS_KB));
107+
global_node_page_state(NR_KERNEL_SCS_KB));
108108
#endif
109109
show_val_kb(m, "PageTables: ",
110110
global_zone_page_state(NR_PAGETABLE));

‎include/linux/memcontrol.h‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ struct kmem_cache;
3232
enum memcg_stat_item {
3333
MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS,
3434
MEMCG_SOCK,
35-
/* XXX: why are these zone and not node counters? */
36-
MEMCG_KERNEL_STACK_KB,
3735
MEMCG_NR_STAT,
3836
};
3937

@@ -729,8 +727,19 @@ void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
729727
void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
730728
int val);
731729
void __mod_lruvec_slab_state(void *p, enum node_stat_item idx, int val);
730+
732731
void mod_memcg_obj_state(void *p, int idx, int val);
733732

733+
static inline void mod_lruvec_slab_state(void *p, enum node_stat_item idx,
734+
int val)
735+
{
736+
unsigned long flags;
737+
738+
local_irq_save(flags);
739+
__mod_lruvec_slab_state(p, idx, val);
740+
local_irq_restore(flags);
741+
}
742+
734743
static inline void mod_memcg_lruvec_state(struct lruvec *lruvec,
735744
enum node_stat_item idx, int val)
736745
{
@@ -1151,6 +1160,14 @@ static inline void __mod_lruvec_slab_state(void *p, enum node_stat_item idx,
11511160
__mod_node_page_state(page_pgdat(page), idx, val);
11521161
}
11531162

1163+
static inline void mod_lruvec_slab_state(void *p, enum node_stat_item idx,
1164+
int val)
1165+
{
1166+
struct page *page = virt_to_head_page(p);
1167+
1168+
mod_node_page_state(page_pgdat(page), idx, val);
1169+
}
1170+
11541171
static inline void mod_memcg_obj_state(void *p, int idx, int val)
11551172
{
11561173
}

‎include/linux/mmzone.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,6 @@ enum zone_stat_item {
155155
NR_ZONE_WRITE_PENDING, /* Count of dirty, writeback and unstable pages */
156156
NR_MLOCK, /* mlock()ed pages found and moved off LRU */
157157
NR_PAGETABLE, /* used for pagetables */
158-
NR_KERNEL_STACK_KB, /* measured in KiB */
159-
#if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
160-
NR_KERNEL_SCS_KB, /* measured in KiB */
161-
#endif
162158
/* Second 128 byte cacheline */
163159
NR_BOUNCE,
164160
#if IS_ENABLED(CONFIG_ZSMALLOC)
@@ -203,6 +199,10 @@ enum node_stat_item {
203199
NR_KERNEL_MISC_RECLAIMABLE, /* reclaimable non-slab kernel pages */
204200
NR_FOLL_PIN_ACQUIRED, /* via: pin_user_page(), gup flag: FOLL_PIN */
205201
NR_FOLL_PIN_RELEASED, /* pages returned via unpin_user_page() */
202+
NR_KERNEL_STACK_KB, /* measured in KiB */
203+
#if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
204+
NR_KERNEL_SCS_KB, /* measured in KiB */
205+
#endif
206206
NR_VM_NODE_STAT_ITEMS
207207
};
208208

‎kernel/fork.c‎

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,8 @@ static inline void free_thread_stack(struct task_struct *tsk)
276276
if (vm) {
277277
int i;
278278

279-
for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
280-
mod_memcg_page_state(vm->pages[i],
281-
MEMCG_KERNEL_STACK_KB,
282-
-(int)(PAGE_SIZE / 1024));
283-
279+
for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
284280
memcg_kmem_uncharge_page(vm->pages[i], 0);
285-
}
286281

287282
for (i = 0; i < NR_CACHED_STACKS; i++) {
288283
if (this_cpu_cmpxchg(cached_stacks[i],
@@ -382,31 +377,14 @@ static void account_kernel_stack(struct task_struct *tsk, int account)
382377
void *stack = task_stack_page(tsk);
383378
struct vm_struct *vm = task_stack_vm_area(tsk);
384379

385-
BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0);
386-
387-
if (vm) {
388-
int i;
389-
390-
BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);
391380

392-
for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
393-
mod_zone_page_state(page_zone(vm->pages[i]),
394-
NR_KERNEL_STACK_KB,
395-
PAGE_SIZE / 1024 * account);
396-
}
397-
} else {
398-
/*
399-
* All stack pages are in the same zone and belong to the
400-
* same memcg.
401-
*/
402-
struct page *first_page = virt_to_page(stack);
403-
404-
mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB,
405-
THREAD_SIZE / 1024 * account);
406-
407-
mod_memcg_obj_state(stack, MEMCG_KERNEL_STACK_KB,
408-
account * (THREAD_SIZE / 1024));
409-
}
381+
/* All stack pages are in the same node. */
382+
if (vm)
383+
mod_lruvec_page_state(vm->pages[0], NR_KERNEL_STACK_KB,
384+
account * (THREAD_SIZE / 1024));
385+
else
386+
mod_lruvec_slab_state(stack, NR_KERNEL_STACK_KB,
387+
account * (THREAD_SIZE / 1024));
410388
}
411389

412390
static int memcg_charge_kernel_stack(struct task_struct *tsk)
@@ -415,24 +393,23 @@ static int memcg_charge_kernel_stack(struct task_struct *tsk)
415393
struct vm_struct *vm = task_stack_vm_area(tsk);
416394
int ret;
417395

396+
BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0);
397+
418398
if (vm) {
419399
int i;
420400

401+
BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);
402+
421403
for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
422404
/*
423405
* If memcg_kmem_charge_page() fails, page->mem_cgroup
424-
* pointer is NULL, and both memcg_kmem_uncharge_page()
425-
* and mod_memcg_page_state() in free_thread_stack()
426-
* will ignore this page. So it's safe.
406+
* pointer is NULL, and memcg_kmem_uncharge_page() in
407+
* free_thread_stack() will ignore this page.
427408
*/
428409
ret = memcg_kmem_charge_page(vm->pages[i], GFP_KERNEL,
429410
0);
430411
if (ret)
431412
return ret;
432-
433-
mod_memcg_page_state(vm->pages[i],
434-
MEMCG_KERNEL_STACK_KB,
435-
PAGE_SIZE / 1024);
436413
}
437414
}
438415
#endif

‎kernel/scs.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static void __scs_account(void *s, int account)
1717
{
1818
struct page *scs_page = virt_to_page(s);
1919

20-
mod_zone_page_state(page_zone(scs_page), NR_KERNEL_SCS_KB,
20+
mod_node_page_state(page_pgdat(scs_page), NR_KERNEL_SCS_KB,
2121
account * (SCS_SIZE / SZ_1K));
2222
}
2323

‎mm/memcontrol.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ static char *memory_stat_format(struct mem_cgroup *memcg)
14851485
(u64)memcg_page_state(memcg, NR_FILE_PAGES) *
14861486
PAGE_SIZE);
14871487
seq_buf_printf(&s, "kernel_stack %llu\n",
1488-
(u64)memcg_page_state(memcg, MEMCG_KERNEL_STACK_KB) *
1488+
(u64)memcg_page_state(memcg, NR_KERNEL_STACK_KB) *
14891489
1024);
14901490
seq_buf_printf(&s, "slab %llu\n",
14911491
(u64)(memcg_page_state(memcg, NR_SLAB_RECLAIMABLE_B) +

‎mm/page_alloc.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5396,6 +5396,10 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)
53965396
" anon_thp: %lukB"
53975397
#endif
53985398
" writeback_tmp:%lukB"
5399+
" kernel_stack:%lukB"
5400+
#ifdef CONFIG_SHADOW_CALL_STACK
5401+
" shadow_call_stack:%lukB"
5402+
#endif
53995403
" all_unreclaimable? %s"
54005404
"\n",
54015405
pgdat->node_id,
@@ -5417,6 +5421,10 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)
54175421
K(node_page_state(pgdat, NR_ANON_THPS) * HPAGE_PMD_NR),
54185422
#endif
54195423
K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
5424+
node_page_state(pgdat, NR_KERNEL_STACK_KB),
5425+
#ifdef CONFIG_SHADOW_CALL_STACK
5426+
node_page_state(pgdat, NR_KERNEL_SCS_KB),
5427+
#endif
54205428
pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ?
54215429
"yes" : "no");
54225430
}
@@ -5448,10 +5456,6 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)
54485456
" present:%lukB"
54495457
" managed:%lukB"
54505458
" mlocked:%lukB"
5451-
" kernel_stack:%lukB"
5452-
#ifdef CONFIG_SHADOW_CALL_STACK
5453-
" shadow_call_stack:%lukB"
5454-
#endif
54555459
" pagetables:%lukB"
54565460
" bounce:%lukB"
54575461
" free_pcp:%lukB"
@@ -5473,10 +5477,6 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)
54735477
K(zone->present_pages),
54745478
K(zone_managed_pages(zone)),
54755479
K(zone_page_state(zone, NR_MLOCK)),
5476-
zone_page_state(zone, NR_KERNEL_STACK_KB),
5477-
#ifdef CONFIG_SHADOW_CALL_STACK
5478-
zone_page_state(zone, NR_KERNEL_SCS_KB),
5479-
#endif
54805480
K(zone_page_state(zone, NR_PAGETABLE)),
54815481
K(zone_page_state(zone, NR_BOUNCE)),
54825482
K(free_pcp),

‎mm/vmstat.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,10 +1140,6 @@ const char * const vmstat_text[] = {
11401140
"nr_zone_write_pending",
11411141
"nr_mlock",
11421142
"nr_page_table_pages",
1143-
"nr_kernel_stack",
1144-
#if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
1145-
"nr_shadow_call_stack",
1146-
#endif
11471143
"nr_bounce",
11481144
#if IS_ENABLED(CONFIG_ZSMALLOC)
11491145
"nr_zspages",
@@ -1194,6 +1190,10 @@ const char * const vmstat_text[] = {
11941190
"nr_kernel_misc_reclaimable",
11951191
"nr_foll_pin_acquired",
11961192
"nr_foll_pin_released",
1193+
"nr_kernel_stack",
1194+
#if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
1195+
"nr_shadow_call_stack",
1196+
#endif
11971197

11981198
/* enum writeback_stat_item counters */
11991199
"nr_dirty_threshold",

0 commit comments

Comments
 (0)