memcg: add pagetable comsumption to memory.stat
Subsystems (9)
cover memcg: add pagetable comsumption to memory.stat
Many workloads consumes significant amount of memory in pagetables. This patch series exposes the pagetable comsumption for each memory cgroup.
Shakeel Butt (2):
mm: move lruvec stats update functions to vmstat.h
mm: memcontrol: account pagetables per node
Documentation/admin-guide/cgroup-v2.rst | 3 + arch/nds32/mm/mm-nds32.c | 6 +- drivers/base/node.c | 2 +- fs/proc/meminfo.c | 2 +- include/linux/memcontrol.h | 112 ------------------------ include/linux/mm.h | 11 ++- include/linux/mmzone.h | 2 +- include/linux/vmstat.h | 104 ++++++++++++++++++++++ mm/memcontrol.c | 19 ++++ mm/page_alloc.c | 6 +- 10 files changed, 142 insertions(+), 125 deletions(-)
Signature · 1 line
2.29.2.454.gaff20da3a2-goog
PATCH 1/2 mm: move lruvec stats update functions to vmstat.h
This patch does not change any functionality and only move the functions which update the lruvec stats to vmstat.h from memcontrol.h. The main reason for this patch is to be able to use these functions in the page table contructor function which is defined in mm.h and we can not include the memcontrol.h in that file.
--- include/linux/memcontrol.h | 112 ------------------------------------- include/linux/vmstat.h | 104 ++++++++++++++++++++++++++++++++++ mm/memcontrol.c | 18 ++++++ 3 files changed, 122 insertions(+), 112 deletions(-)
include/linux/memcontrol.h +0 -112
@@ -947,8 +947,6 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,void __mod_memcg_lruvec_state (struct lruvec *lruvec,enum node_stat_item idx,int val); -void __mod_lruvec_state (struct lruvec *lruvec,enum node_stat_item idx, -int val);void __mod_lruvec_kmem_state (void *p,enum node_stat_item idx,int val); static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
@@ -971,44 +969,6 @@ static inline void mod_memcg_lruvec_state(struct lruvec *lruvec,local_irq_restore (flags); } -static inline void mod_lruvec_state (struct lruvec *lruvec, -enum node_stat_item idx,int val) -{ -unsigned long flags; - -local_irq_save (flags); -__mod_lruvec_state (lruvec, idx, val); -local_irq_restore (flags); -} - -static inline void __mod_lruvec_page_state (struct page *page, -enum node_stat_item idx,int val) -{ -struct page *head =compound_head (page);/* rmap on tail pages */ -struct mem_cgroup *memcg =page_memcg (head); -pg_data_t *pgdat =page_pgdat (page); -struct lruvec *lruvec; - -/* Untracked pages have no memcg, no lruvec. Update only the node */ -if (!memcg) { -__mod_node_page_state (pgdat, idx, val); -return ; - } - - lruvec =mem_cgroup_lruvec (memcg, pgdat); -__mod_lruvec_state (lruvec, idx, val); -} - -static inline void mod_lruvec_page_state (struct page *page, -enum node_stat_item idx,int val) -{ -unsigned long flags; - -local_irq_save (flags); -__mod_lruvec_page_state (page, idx, val); -local_irq_restore (flags); -} -unsigned long mem_cgroup_soft_limit_reclaim (pg_data_t *pgdat,int order,gfp_t gfp_mask,unsigned long *total_scanned);
@@ -1411,30 +1371,6 @@ static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec, { } -static inline void __mod_lruvec_state (struct lruvec *lruvec, -enum node_stat_item idx,int val) -{ -__mod_node_page_state (lruvec_pgdat (lruvec), idx, val); -} - -static inline void mod_lruvec_state (struct lruvec *lruvec, -enum node_stat_item idx,int val) -{ -mod_node_page_state (lruvec_pgdat (lruvec), idx, val); -} - -static inline void __mod_lruvec_page_state (struct page *page, -enum node_stat_item idx,int val) -{ -__mod_node_page_state (page_pgdat (page), idx, val); -} - -static inline void mod_lruvec_page_state (struct page *page, -enum node_stat_item idx,int val) -{ -mod_node_page_state (page_pgdat (page), idx, val); -} -static inline void __mod_lruvec_kmem_state (void *p,enum node_stat_item idx,int val) {
@@ -1490,30 +1426,6 @@ static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page) }#endif /* CONFIG_MEMCG */ -static inline void __inc_lruvec_state (struct lruvec *lruvec, -enum node_stat_item idx) -{ -__mod_lruvec_state (lruvec, idx,1 ); -} - -static inline void __dec_lruvec_state (struct lruvec *lruvec, -enum node_stat_item idx) -{ -__mod_lruvec_state (lruvec, idx,-1 ); -} - -static inline void __inc_lruvec_page_state (struct page *page, -enum node_stat_item idx) -{ -__mod_lruvec_page_state (page, idx,1 ); -} - -static inline void __dec_lruvec_page_state (struct page *page, -enum node_stat_item idx) -{ -__mod_lruvec_page_state (page, idx,-1 ); -} -static inline void __inc_lruvec_kmem_state (void *p,enum node_stat_item idx) {__mod_lruvec_kmem_state (p, idx,1 );
@@ -1524,30 +1436,6 @@ static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx)__mod_lruvec_kmem_state (p, idx,-1 ); } -static inline void inc_lruvec_state (struct lruvec *lruvec, -enum node_stat_item idx) -{ -mod_lruvec_state (lruvec, idx,1 ); -} - -static inline void dec_lruvec_state (struct lruvec *lruvec, -enum node_stat_item idx) -{ -mod_lruvec_state (lruvec, idx,-1 ); -} - -static inline void inc_lruvec_page_state (struct page *page, -enum node_stat_item idx) -{ -mod_lruvec_page_state (page, idx,1 ); -} - -static inline void dec_lruvec_page_state (struct page *page, -enum node_stat_item idx) -{ -mod_lruvec_page_state (page, idx,-1 ); -} -static inline struct lruvec *parent_lruvec (struct lruvec *lruvec) {struct mem_cgroup *memcg;
include/linux/vmstat.h +104 -0
108 lines not on this page · raw message
mm/memcontrol.c +18 -0
@@ -847,6 +847,24 @@ void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,__mod_memcg_lruvec_state (lruvec, idx, val); } +void __mod_lruvec_page_state (struct page *page,enum node_stat_item idx, +int val) +{ +struct page *head =compound_head (page);/* rmap on tail pages */ +struct mem_cgroup *memcg =page_memcg (head); +pg_data_t *pgdat =page_pgdat (page); +struct lruvec *lruvec; + +/* Untracked pages have no memcg, no lruvec. Update only the node */ +if (!memcg) { +__mod_node_page_state (pgdat, idx, val); +return ; + } + + lruvec =mem_cgroup_lruvec (memcg, pgdat); +__mod_lruvec_state (lruvec, idx, val); +} +void __mod_lruvec_kmem_state (void *p,enum node_stat_item idx,int val) {pg_data_t *pgdat =page_pgdat (virt_to_page (p));
Signature · 1 line
2.29.2.454.gaff20da3a2-goog
Re: [PATCH 1/2] mm: move lruvec stats update functions to vmstat.h
Quoted 7 lines · Shakeel Butt
This patch does not change any functionality and only move the functions which update the lruvec stats to
vmstat.hfrommemcontrol.h. The main reason for this patch is to be able to use these functions in the page table contructor function which is defined inmm.hand we can not include thememcontrol.hin that file.Signed-off-by Shakeel Butt <[email protected]>
This is a better place for this interface in general. The lruvec abstraction, while invented for memcg, isn't specific to memcg at all.
PATCH 2/2 mm: memcontrol: account pagetables per node
In reply to Shakeel ButtFor many workloads, pagetable consumption is significant and it makes sense to expose it in the memory.stat for the memory cgroups. However at the moment, the pagetables are accounted per-zone. Converting them to per-node and using the right interface will correctly account for the memory cgroups as well.
---
This patch was posted at [1] and [2] but more work was needed to make it build for all archs.
[1] http://lkml.kernel.org/r/[email protected]
[2] http://lkml.kernel.org/r/[email protected]
Documentation/admin-guide/cgroup-v2.rst | 3 +++ arch/nds32/mm/mm-nds32.c | 6 +++--- drivers/base/node.c | 2 +- fs/proc/meminfo.c | 2 +- include/linux/mm.h | 8 ++++---- include/linux/mmzone.h | 2 +- mm/memcontrol.c | 1 + mm/page_alloc.c | 6 +++--- 8 files changed, 17 insertions(+), 13 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +3 -0
@@ -1274,6 +1274,9 @@ PAGE_SIZE multiple when read back. kernel_stack Amount of memory allocated to kernel stacks. + pagetables + Amount of memory allocated for page tables. + percpu(npn) Amount of memory used for storing per-cpu kernel data structures.
arch/nds32/mm/mm-nds32.c +3 -3
@@ -34,8 +34,8 @@ pgd_t *pgd_alloc(struct mm_struct *mm)cpu_dcache_wb_range ((unsigned long )new_pgd, (unsigned long )new_pgd +PTRS_PER_PGD *sizeof (pgd_t)); -inc_zone_page_state (virt_to_page ((unsigned long *)new_pgd), -NR_PAGETABLE ); +inc_lruvec_page_state (virt_to_page ((unsigned long *)new_pgd), +NR_PAGETABLE );return new_pgd; }
@@ -59,7 +59,7 @@ void pgd_free(struct mm_struct *mm, pgd_t * pgd) pte =pmd_page (*pmd);pmd_clear (pmd); -dec_zone_page_state (virt_to_page ((unsigned long *)pgd),NR_PAGETABLE ); +dec_lruvec_page_state (virt_to_page ((unsigned long *)pgd),NR_PAGETABLE );pte_free (mm, pte);mm_dec_nr_ptes (mm);pmd_free (mm, pmd);
drivers/base/node.c +1 -1
@@ -450,7 +450,7 @@ static ssize_t node_read_meminfo(struct device *dev,#ifdef CONFIG_SHADOW_CALL_STACK nid,node_page_state (pgdat,NR_KERNEL_SCS_KB ), #endif - nid,K (sum_zone_node_page_state (nid,NR_PAGETABLE )), + nid,K (node_page_state (pgdat,NR_PAGETABLE )), nid,0UL , nid,K (sum_zone_node_page_state (nid,NR_BOUNCE )), nid, K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
fs/proc/meminfo.c +1 -1
@@ -107,7 +107,7 @@ static int meminfo_proc_show(struct seq_file *m, void *v) global_node_page_state(NR_KERNEL_SCS_KB));#endif show_val_kb (m,"PageTables: " , -global_zone_page_state (NR_PAGETABLE )); +global_node_page_state (NR_PAGETABLE ));show_val_kb (m,"NFS_Unstable: " ,0 ); show_val_kb(m, "Bounce: ",
include/linux/mm.h +4 -4
@@ -2199,7 +2199,7 @@ static inline bool pgtable_pte_page_ctor(struct page *page)if (!ptlock_init (page))return false;__SetPageTable (page); -inc_zone_page_state (page,NR_PAGETABLE ); +inc_lruvec_page_state (page,NR_PAGETABLE );return true; }
@@ -2207,7 +2207,7 @@ static inline void pgtable_pte_page_dtor(struct page *page) {ptlock_free (page);__ClearPageTable (page); -dec_zone_page_state (page,NR_PAGETABLE ); +dec_lruvec_page_state (page,NR_PAGETABLE ); }#define pte_offset_map_lock (mm, pmd, address, ptlp) \
@@ -2294,7 +2294,7 @@ static inline bool pgtable_pmd_page_ctor(struct page *page)if (!pmd_ptlock_init (page))return false;__SetPageTable (page); -inc_zone_page_state (page,NR_PAGETABLE ); +inc_lruvec_page_state (page,NR_PAGETABLE );return true; }
@@ -2302,7 +2302,7 @@ static inline void pgtable_pmd_page_dtor(struct page *page) {pmd_ptlock_free (page);__ClearPageTable (page); -dec_zone_page_state (page,NR_PAGETABLE ); +dec_lruvec_page_state (page,NR_PAGETABLE ); }/*
include/linux/mmzone.h +1 -1
@@ -151,7 +151,6 @@ enum zone_stat_item {NR_ZONE_UNEVICTABLE ,NR_ZONE_WRITE_PENDING ,/* Count of dirty, writeback and unstable pages */ NR_MLOCK ,/* mlock()ed pages found and moved off LRU */ -NR_PAGETABLE ,/* used for pagetables */ /* Second 128 byte cacheline */ NR_BOUNCE , #if IS_ENABLED(CONFIG_ZSMALLOC)
@@ -206,6 +205,7 @@ enum node_stat_item {#if IS_ENABLED (CONFIG_SHADOW_CALL_STACK )NR_KERNEL_SCS_KB ,/* measured in KiB */ #endif +NR_PAGETABLE ,/* used for pagetables */ NR_VM_NODE_STAT_ITEMS };
mm/memcontrol.c +1 -0
@@ -1519,6 +1519,7 @@ static struct memory_stat memory_stats[] = { { "anon", PAGE_SIZE, NR_ANON_MAPPED }, { "file", PAGE_SIZE, NR_FILE_PAGES }, { "kernel_stack", 1024, NR_KERNEL_STACK_KB }, + { "pagetables", PAGE_SIZE, NR_PAGETABLE }, { "percpu", 1, MEMCG_PERCPU_B }, { "sock", PAGE_SIZE, MEMCG_SOCK }, { "shmem", PAGE_SIZE, NR_SHMEM },
mm/page_alloc.c +3 -3
@@ -5520,7 +5520,7 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)global_node_page_state_pages (NR_SLAB_UNRECLAIMABLE_B ),global_node_page_state (NR_FILE_MAPPED ),global_node_page_state (NR_SHMEM ), -global_zone_page_state (NR_PAGETABLE ), +global_node_page_state (NR_PAGETABLE ),global_zone_page_state (NR_BOUNCE ),global_zone_page_state (NR_FREE_PAGES ), free_pcp,
@@ -5552,6 +5552,7 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)#ifdef CONFIG_SHADOW_CALL_STACK " shadow_call_stack:%lukB" #endif +" pagetables:%lukB" " all_unreclaimable? %s" "\n" , pgdat->node_id,
@@ -5577,6 +5578,7 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)#ifdef CONFIG_SHADOW_CALL_STACK node_page_state (pgdat,NR_KERNEL_SCS_KB ), #endif +K (node_page_state (pgdat,NR_PAGETABLE )), pgdat->kswapd_failures >=MAX_RECLAIM_RETRIES ? "yes" : "no"); }
@@ -5608,7 +5610,6 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)" present:%lukB" " managed:%lukB" " mlocked:%lukB" -" pagetables:%lukB" " bounce:%lukB" " free_pcp:%lukB" " local_pcp:%ukB"
@@ -5629,7 +5630,6 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)K (zone->present_pages),K (zone_managed_pages (zone)),K (zone_page_state (zone,NR_MLOCK )), -K (zone_page_state (zone,NR_PAGETABLE )),K (zone_page_state (zone,NR_BOUNCE )),K (free_pcp), K(this_cpu_read(zone->pageset->pcp.count)),
Signature · 1 line
2.29.2.454.gaff20da3a2-goog
Re: [PATCH 2/2] mm: memcontrol: account pagetables per node
Quoted 7 lines · Shakeel Butt
For many workloads, pagetable consumption is significant and it makes sense to expose it in the memory.stat for the memory cgroups. However at the moment, the pagetables are accounted per-zone. Converting them to per-node and using the right interface will correctly account for the memory cgroups as well.
Signed-off-by Shakeel Butt <[email protected]>
Agreed, this is a useful stat item to have.
Just one trivial issue:
Quoted 11 lines
include/linux/mmzone.h +0
-1@@ -151,7 +151,6 @@ enum zone_stat_item {NR_ZONE_UNEVICTABLE ,NR_ZONE_WRITE_PENDING ,/* Count of dirty, writeback and unstable pages */ NR_MLOCK ,/* mlock()ed pages found and moved off LRU */ -NR_PAGETABLE ,/* used for pagetables */ /* Second 128 byte cacheline */ NR_BOUNCE , #if IS_ENABLED(CONFIG_ZSMALLOC)
include/linux/mmzone.h enum node_stat_item { · line 210
Quoted 6 lines
include/linux/mmzone.h +1
-0@@ -206,6 +205,7 @@ enum node_stat_item {#if IS_ENABLED (CONFIG_SHADOW_CALL_STACK )NR_KERNEL_SCS_KB ,/* measured in KiB */ #endif +NR_PAGETABLE ,/* used for pagetables */ NR_VM_NODE_STAT_ITEMS };
You need to update mm/vmstat.c::vmstat_text accordingly or /proc/vmstat output will be bogus.
With that fixed, please feel free to add:
Re: [PATCH 2/2] mm: memcontrol: account pagetables per node
Quoted 26 lines · Johannes Weiner
Quoted 7 lines · Shakeel Butt
For many workloads, pagetable consumption is significant and it makes sense to expose it in the memory.stat for the memory cgroups. However at the moment, the pagetables are accounted per-zone. Converting them to per-node and using the right interface will correctly account for the memory cgroups as well.
Signed-off-by Shakeel Butt <[email protected]>Agreed, this is a useful stat item to have.
Just one trivial issue:
Quoted 12 lines
include/linux/mmzone.h +0
-1@@ -151,7 +151,6 @@ enum zone_stat_item {NR_ZONE_UNEVICTABLE ,NR_ZONE_WRITE_PENDING ,/* Count of dirty, writeback and unstable pages */ NR_MLOCK ,/* mlock()ed pages found and moved off LRU */ -NR_PAGETABLE ,/* used for pagetables */ /* Second 128 byte cacheline */ NR_BOUNCE , #if IS_ENABLED(CONFIG_ZSMALLOC)@@ -206,6 +205,7 @@ enum node_stat_item {#if IS_ENABLED (CONFIG_SHADOW_CALL_STACK )
Quoted 8 lines
include/linux/mmzone.h enum node_stat_item { · line 210
Quoted 5 lines
include/linux/mmzone.h +1
-0NR_KERNEL_SCS_KB ,/* measured in KiB */ #endif +NR_PAGETABLE ,/* used for pagetables */ NR_VM_NODE_STAT_ITEMS };You need to update mm/vmstat.c::vmstat_text accordingly or
/proc/vmstatoutput will be bogus.
Oh I missed that. Thanks for catching.
Quoted 3 lines
With that fixed, please feel free to add:
Acked-by Johannes Weiner <[email protected]>
Thanks.
Quoted 2 lines · Shakeel Butt
Many workloads consumes significant amount of memory in pagetables. This patch series exposes the pagetable comsumption for each memory cgroup.
Hi Shakeel!
The code looks good to me. However I'm not sure I understand what's the use case for the new statistics? Can you, please, elaborate a bit more here?
From a very first glance, the size of pagetables should be _roughly_ equal to the size_of(pte)/PAGE_SIZE*(size of a cgroup) and should not exceed 1% of the cgroup size. So for all but very large cgroups the value will be in the noise of per-cpu counters. Perhaps I'm missing some important cases?
Thanks!
Quoted 20 lines
Shakeel Butt (2):
mm: move lruvec stats update functions tovmstat.h
mm: memcontrol: account pagetables per nodeDocumentation/admin-guide/cgroup-v2.rst | 3 + arch/nds32/mm/mm-nds32.c | 6 +- drivers/base/node.c | 2 +- fs/proc/meminfo.c | 2 +- include/linux/memcontrol.h | 112 ------------------------ include/linux/mm.h | 11 ++- include/linux/mmzone.h | 2 +- include/linux/vmstat.h | 104 ++++++++++++++++++++++ mm/memcontrol.c | 19 ++++ mm/page_alloc.c | 6 +- 10 files changed, 142 insertions(+), 125 deletions(-)Signature · 1 line
2.29.2.454.gaff20da3a2-goog
Quoted 7 lines · Roman Gushchin
Quoted 2 lines · Shakeel Butt
Many workloads consumes significant amount of memory in pagetables. This patch series exposes the pagetable comsumption for each memory cgroup.
Hi Shakeel!
Quoted 8 lines
The code looks good to me. However I'm not sure I understand what's the use case for the new statistics? Can you, please, elaborate a bit more here?
From a very first glance, the size of pagetables should be _roughly_ equal to the size_of(pte)/PAGE_SIZE*(size of a cgroup) and should not exceed 1% of the cgroup size. So for all but very large cgroups the value will be in the noise of per-cpu counters. Perhaps I'm missing some important cases?
I think this is in general a good metric to have but one specific use-case we have is the user space network driver which mmaps the memory of the applications for zero copy data transfers. This driver can consume a large amount of memory in page tables. So, this metric becomes really useful here.
Shakeel
Roman Gushchin
Acked-byQuoted 17 lines · Shakeel Butt
Quoted 7 lines · Roman Gushchin
Quoted 2 lines · Shakeel Butt
Many workloads consumes significant amount of memory in pagetables. This patch series exposes the pagetable comsumption for each memory cgroup.
Hi Shakeel!
Quoted 8 lines
The code looks good to me. However I'm not sure I understand what's the use case for the new statistics? Can you, please, elaborate a bit more here?
From a very first glance, the size of pagetables should be _roughly_ equal to the size_of(pte)/PAGE_SIZE*(size of a cgroup) and should not exceed 1% of the cgroup size. So for all but very large cgroups the value will be in the noise of per-cpu counters. Perhaps I'm missing some important cases?
Quoted 5 lines
I think this is in general a good metric to have but one specific use-case we have is the user space network driver which mmaps the memory of the applications for zero copy data transfers. This driver can consume a large amount of memory in page tables. So, this metric becomes really useful here.
Got it, thank you for the explanation!
Would you mind to add this text to the cover letter as an example?
for the series.
Thanks!
Quoted 19 lines · Roman Gushchin
Quoted 17 lines · Shakeel Butt
Quoted 15 lines · Roman Gushchin
Quoted 2 lines · Shakeel Butt
Many workloads consumes significant amount of memory in pagetables. This patch series exposes the pagetable comsumption for each memory cgroup.
Hi Shakeel!
The code looks good to me. However I'm not sure I understand what's the use case for the new statistics? Can you, please, elaborate a bit more here?
From a very first glance, the size of pagetables should be _roughly_ equal to the size_of(pte)/PAGE_SIZE*(size of a cgroup) and should not exceed 1% of the cgroup size. So for all but very large cgroups the value will be in the noise of per-cpu counters. Perhaps I'm missing some important cases?
Quoted 8 lines
Quoted 5 lines
I think this is in general a good metric to have but one specific use-case we have is the user space network driver which mmaps the memory of the applications for zero copy data transfers. This driver can consume a large amount of memory in page tables. So, this metric becomes really useful here.
Got it, thank you for the explanation!
Would you mind to add this text to the cover letter as an example?
Sure.
Quoted 3 lines
Acked-by Roman Gushchin <[email protected]>for the series.
Thanks for the review.