hugetlb: Add hugetlb.*.numa_stat file
Subsystems (4)
PATCH v4 1/1 hugetlb: Add hugetlb.*.numa_stat file
For hugetlb backed jobs/VMs it's critical to understand the numa information for the memory backing these jobs to deliver optimal performance.
Currently this technically can be queried from /proc/self/numa_maps, but there are significant issues with that. Namely:
1. Memory can be mapped on unmapped.
2. numa_maps are per process and need to be aggregated across all processes in the cgroup. For shared memory this is more involved as the userspace needs to make sure it doesn't double count shared mappings.
3. I believe querying numa_maps needs to hold the mmap_lock which adds to the contention on this lock.
For these reasons I propose simply adding hugetlb.*.numa_stat file, which shows the numa information of the cgroup similarly to memory.numa_stat.
On cgroup-v2:
cat /sys/fs/cgroup/unified/test/hugetlb.2MB.numa_stat total=2097152 N0=2097152 N1=0
On cgroup-v1:
cat /sys/fs/cgroup/hugetlb/test/hugetlb.2MB.numa_stat total=2097152 N0=2097152 N1=0 hierarichal_total=2097152 N0=2097152 N1=0
This patch was tested manually by allocating hugetlb memory and querying the hugetlb.*.numa_stat file of the cgroup and its parents. 
Cc: Mike Kravetz <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: Miaohe Lin <[email protected]>
Cc: Oscar Salvador <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Muchun Song <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Shakeel Butt <[email protected]>
Cc: Jue Wang <[email protected]>
Cc: Yang Yao <[email protected]>
Cc: Joanna Li <[email protected]>
Cc: Cannon Matthews <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
Changes in v4:
- Removed unnecessary braces.
- usage is now counted in pages instead of bytes.
- Reverted unneeded changes to write_to_hugetlbfs.c
Changes in v3:
- Fixed typos (sorry!)
- Used conventional locations for cgroups mount points in docs/commit message.
- Updated docs.
- Handle kzalloc_node failure, and proper deallocation of per node data.
- Use struct_size() to calculate the struct size.
- Use nr_node_ids instead of MAX_NUMNODES.
- Updated comments per multi-line comment pattern.
Changes in v2:
- Fix warning Reported-by: kernel test robot <[email protected]>
--- .../admin-guide/cgroup-v1/hugetlb.rst | 4 + Documentation/admin-guide/cgroup-v2.rst | 5 + include/linux/hugetlb.h | 4 +- include/linux/hugetlb_cgroup.h | 7 ++ mm/hugetlb_cgroup.c | 113 ++++++++++++++++-- 5 files changed, 122 insertions(+), 11 deletions(-)
Documentation/admin-guide/cgroup-v1/hugetlb.rst +4 -0
@@ -29,12 +29,14 @@ Brief summary of control files:: hugetlb.<hugepagesize>.max_usage_in_bytes # show max "hugepagesize" hugetlb usage recorded hugetlb.<hugepagesize>.usage_in_bytes # show current usage for "hugepagesize" hugetlb hugetlb.<hugepagesize>.failcnt # show the number of allocation failure due to HugeTLB usage limit + hugetlb.<hugepagesize>.numa_stat # show the numa information of the hugetlb memory charged to this cgroup For a system supporting three hugepage sizes (64k, 32M and 1G), the control files include:: hugetlb.1GB.limit_in_bytes hugetlb.1GB.max_usage_in_bytes + hugetlb.1GB.numa_stat hugetlb.1GB.usage_in_bytes hugetlb.1GB.failcnt hugetlb.1GB.rsvd.limit_in_bytes
@@ -43,6 +45,7 @@ files include:: hugetlb.1GB.rsvd.failcnt hugetlb.64KB.limit_in_bytes hugetlb.64KB.max_usage_in_bytes + hugetlb.64KB.numa_stat hugetlb.64KB.usage_in_bytes hugetlb.64KB.failcnt hugetlb.64KB.rsvd.limit_in_bytes
@@ -51,6 +54,7 @@ files include:: hugetlb.64KB.rsvd.failcnt hugetlb.32MB.limit_in_bytes hugetlb.32MB.max_usage_in_bytes + hugetlb.32MB.numa_stat hugetlb.32MB.usage_in_bytes hugetlb.32MB.failcnt hugetlb.32MB.rsvd.limit_in_bytes
Documentation/admin-guide/cgroup-v2.rst +5 -0
@@ -2252,6 +2252,11 @@ HugeTLB Interface Files are local to the cgroup i.e. not hierarchical. The file modified event generated on this file reflects only the local events. + hugetlb.<hugepagesize>.numa_stat + Similar to memory.numa_stat, it shows the numa information of the + hugetlb pages of <hugepagesize> in this cgroup. Only active in + use hugetlb pages are included. The per-node values are in bytes. + Misc ----
include/linux/hugetlb.h +2 -2
@@ -613,8 +613,8 @@ struct hstate {#endif #ifdef CONFIG_CGROUP_HUGETLB /* cgroup control files */ -struct cftype cgroup_files_dfl[7 ]; -struct cftype cgroup_files_legacy[9 ]; +struct cftype cgroup_files_dfl[8 ]; +struct cftype cgroup_files_legacy[10 ];#endif char name[HSTATE_NAME_LEN ]; };
include/linux/hugetlb_cgroup.h +7 -0
@@ -36,6 +36,11 @@ enum hugetlb_memory_event { HUGETLB_NR_MEMORY_EVENTS, }; +struct hugetlb_cgroup_per_node { +/* hugetlb usage in bytes over all hstates. */ +unsigned long usage[HUGE_MAX_HSTATE ]; +}; +struct hugetlb_cgroup {struct cgroup_subsys_state css;
@@ -57,6 +62,8 @@ struct hugetlb_cgroup {/* Handle for "hugetlb.events.local" */ struct cgroup_file events_local_file[HUGE_MAX_HSTATE ]; + +struct hugetlb_cgroup_per_node *nodeinfo[]; }; static inline struct hugetlb_cgroup *
mm/hugetlb_cgroup.c +104 -9
169 lines not on this page · raw message
--
2.34.0.rc0.344.g81b53c2807-goog
Quoted 8 lines · Mina Almasry
For hugetlb backed jobs/VMs it's critical to understand the numa information for the memory backing these jobs to deliver optimal performance.
Currently this technically can be queried from
/proc/self/numa_maps, but there are significant issues with that. Namely:
1. Memory can be mapped on unmapped.
or*
Quoted 41 lines
2.
numa_mapsare per process and need to be aggregated across all processes in the cgroup. For shared memory this is more involved as the userspace needs to make sure it doesn't double count shared mappings.
3. I believe queryingnuma_mapsneeds to hold themmap_lockwhich adds to the contention on this lock.For these reasons I propose simply adding hugetlb.*.numa_stat file, which shows the numa information of the cgroup similarly to memory.numa_stat.
On cgroup-v2:
cat /sys/fs/cgroup/unified/test/hugetlb.2MB.numa_stat total=2097152 N0=2097152 N1=0On cgroup-v1:
cat /sys/fs/cgroup/hugetlb/test/hugetlb.2MB.numa_stat total=2097152 N0=2097152 N1=0 hierarichal_total=2097152 N0=2097152 N1=0This patch was tested manually by allocating hugetlb memory and querying the hugetlb.*.numa_stat file of the cgroup and its parents. 
Cc: Mike Kravetz <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: Miaohe Lin <[email protected]>
Cc: Oscar Salvador <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Muchun Song <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Shakeel Butt <[email protected]>
Cc: Jue Wang <[email protected]>
Cc: Yang Yao <[email protected]>
Cc: Joanna Li <[email protected]>
Cc: Cannon Matthews <[email protected]>
Cc: [email protected]
Cc: [email protected]Signed-off-by Mina Almasry <[email protected]>
[...]
include/linux/hugetlb_cgroup.h enum hugetlb_memory_event { · line 41
Quoted 11 lines
include/linux/hugetlb_cgroup.h +3
-0@@ -36,6 +36,11 @@ enum hugetlb_memory_event { HUGETLB_NR_MEMORY_EVENTS, }; +struct hugetlb_cgroup_per_node { +/* hugetlb usage in bytes over all hstates. */ +unsigned long usage[HUGE_MAX_HSTATE ];
Should we use atomic here? Is this safe for all archs?
[...]
include/linux/hugetlb_cgroup.h static void __hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages, · line 324
Quoted 8 lines
include/linux/hugetlb_cgroup.h +1
-1/* @@ -292,7 +321,8 @@ static void __hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,return ;__set_hugetlb_cgroup (page, h_cg, rsvd); -return ; +if (!rsvd && h_cg)
No need to check h_cg.
Quoted 47 lines
include/linux/hugetlb_cgroup.h +32
-1+ h_cg->nodeinfo[page_to_nid (page)]->usage[idx] += nr_pages; } void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,@@ -331,7 +361,8 @@ static void __hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,if (rsvd)css_put (&h_cg->css); - +else + h_cg->nodeinfo[page_to_nid (page)]->usage[idx] -= nr_pages;return ; }@@ -421,6 +452,58 @@ enum { RES_RSVD_FAILCNT, }; +static int hugetlb_cgroup_read_numa_stat (struct seq_file *seq,void *dummy) +{ +int nid; +struct cftype *cft =seq_cft (seq); +int idx =MEMFILE_IDX (cft->private); +bool legacy =MEMFILE_ATTR (cft->private); +struct hugetlb_cgroup *h_cg =hugetlb_cgroup_from_css (seq_css (seq)); +struct cgroup_subsys_state *css; +unsigned long usage; + +if (legacy) { +/* Add up usage across all nodes for the non-hierarchical total. */ + usage =0 ; +for_each_node_state (nid,N_MEMORY ) + usage += h_cg->nodeinfo[nid]->usage[idx]; +seq_printf (seq,"total=%lu" , usage *PAGE_SIZE ); + +/* Simply print the per-node usage for the non-hierarchical total. */ +for_each_node_state (nid,N_MEMORY ) +seq_printf (seq," N%d=%lu" , nid, + h_cg->nodeinfo[nid]->usage[idx] *PAGE_SIZE ); +seq_putc (seq,'\n' ); + } + +/* +* The hierarchical total is pretty much the value recorded by the +* counter, so use that. +*/ + seq_printf(seq, "%stotal=%lu", legacy ? "hierarichal_" : "",
include/linux/hugetlb_cgroup.h enum { · line 493
Quoted 10 lines
include/linux/hugetlb_cgroup.h +10
-0+ page_counter_read(&h_cg->hugepage[idx]) * PAGE_SIZE); + +/* +* For each node, transverse the css tree to obtain the hierarichal +* node usage. +*/ +for_each_node_state (nid,N_MEMORY ) { + usage =0 ; +rcu_read_lock (); +css_for_each_descendant_pre (css, &h_cg->css) {
This will be slow. Do you really want to traverse the hugetlb-cgroup tree that many times? We had similar issues with memcg stats as well but got resolved with rstat.
Though I feel like hugetlb-cgroup is not that worried about performance. There is no batching in the charging and uncharging codepaths.
Quoted 12 lines
include/linux/hugetlb_cgroup.h +12
-0+ usage +=hugetlb_cgroup_from_css (css) + ->nodeinfo[nid] + ->usage[idx]; + } +rcu_read_unlock (); +seq_printf (seq," N%d=%lu" , nid, usage *PAGE_SIZE ); + } + +seq_putc (seq,'\n' ); + +return 0 ; +}
Quoted 145 lines · Shakeel Butt
Quoted 8 lines · Mina Almasry
For hugetlb backed jobs/VMs it's critical to understand the numa information for the memory backing these jobs to deliver optimal performance.
Currently this technically can be queried from
/proc/self/numa_maps, but there are significant issues with that. Namely:
1. Memory can be mapped on unmapped.or*
Quoted 41 lines
2.
numa_mapsare per process and need to be aggregated across all processes in the cgroup. For shared memory this is more involved as the userspace needs to make sure it doesn't double count shared mappings.
3. I believe queryingnuma_mapsneeds to hold themmap_lockwhich adds to the contention on this lock.For these reasons I propose simply adding hugetlb.*.numa_stat file, which shows the numa information of the cgroup similarly to memory.numa_stat.
On cgroup-v2:
cat /sys/fs/cgroup/unified/test/hugetlb.2MB.numa_stat total=2097152 N0=2097152 N1=0On cgroup-v1:
cat /sys/fs/cgroup/hugetlb/test/hugetlb.2MB.numa_stat total=2097152 N0=2097152 N1=0 hierarichal_total=2097152 N0=2097152 N1=0This patch was tested manually by allocating hugetlb memory and querying the hugetlb.*.numa_stat file of the cgroup and its parents. 
Cc: Mike Kravetz <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: Miaohe Lin <[email protected]>
Cc: Oscar Salvador <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Muchun Song <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Shakeel Butt <[email protected]>
Cc: Jue Wang <[email protected]>
Cc: Yang Yao <[email protected]>
Cc: Joanna Li <[email protected]>
Cc: Cannon Matthews <[email protected]>
Cc: [email protected]
Cc: [email protected]Signed-off-by Mina Almasry <[email protected]>[...]
include/linux/hugetlb_cgroup.h enum hugetlb_memory_event { · line 41
Quoted 11 lines
include/linux/hugetlb_cgroup.h +3
-0@@ -36,6 +36,11 @@ enum hugetlb_memory_event { HUGETLB_NR_MEMORY_EVENTS, }; +struct hugetlb_cgroup_per_node { +/* hugetlb usage in bytes over all hstates. */ +unsigned long usage[HUGE_MAX_HSTATE ];Should we use atomic here? Is this safe for all archs?
[...]
include/linux/hugetlb_cgroup.h static void __hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages, · line 324
Quoted 8 lines
include/linux/hugetlb_cgroup.h +1
-1/* @@ -292,7 +321,8 @@ static void __hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,return ;__set_hugetlb_cgroup (page, h_cg, rsvd); -return ; +if (!rsvd && h_cg)No need to check
h_cg.Quoted 47 lines
include/linux/hugetlb_cgroup.h +32
-1+ h_cg->nodeinfo[page_to_nid (page)]->usage[idx] += nr_pages; } void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,@@ -331,7 +361,8 @@ static void __hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages,if (rsvd)css_put (&h_cg->css); - +else + h_cg->nodeinfo[page_to_nid (page)]->usage[idx] -= nr_pages;return ; }@@ -421,6 +452,58 @@ enum { RES_RSVD_FAILCNT, }; +static int hugetlb_cgroup_read_numa_stat (struct seq_file *seq,void *dummy) +{ +int nid; +struct cftype *cft =seq_cft (seq); +int idx =MEMFILE_IDX (cft->private); +bool legacy =MEMFILE_ATTR (cft->private); +struct hugetlb_cgroup *h_cg =hugetlb_cgroup_from_css (seq_css (seq)); +struct cgroup_subsys_state *css; +unsigned long usage; + +if (legacy) { +/* Add up usage across all nodes for the non-hierarchical total. */ + usage =0 ; +for_each_node_state (nid,N_MEMORY ) + usage += h_cg->nodeinfo[nid]->usage[idx]; +seq_printf (seq,"total=%lu" , usage *PAGE_SIZE ); + +/* Simply print the per-node usage for the non-hierarchical total. */ +for_each_node_state (nid,N_MEMORY ) +seq_printf (seq," N%d=%lu" , nid, + h_cg->nodeinfo[nid]->usage[idx] *PAGE_SIZE ); +seq_putc (seq,'\n' ); + } + +/* +* The hierarchical total is pretty much the value recorded by the +* counter, so use that. +*/ + seq_printf(seq, "%stotal=%lu", legacy ? "hierarichal_" : "",include/linux/hugetlb_cgroup.h enum { · line 493
Quoted 10 lines
include/linux/hugetlb_cgroup.h +10
-0+ page_counter_read(&h_cg->hugepage[idx]) * PAGE_SIZE); + +/* +* For each node, transverse the css tree to obtain the hierarichal +* node usage. +*/ +for_each_node_state (nid,N_MEMORY ) { + usage =0 ; +rcu_read_lock (); +css_for_each_descendant_pre (css, &h_cg->css) {This will be slow. Do you really want to traverse the hugetlb-cgroup tree that many times? We had similar issues with memcg stats as well but got resolved with rstat.
Quoted 4 lines
Though I feel like hugetlb-cgroup is not that worried about performance. There is no batching in the charging and uncharging codepaths.
I'm not seeing a way to get this info without paying the cost to transverse the tree somewhere (either at charge time or at collection time). I think maybe we can wait and see if there are issues with the performance and if so we can adopt the memcg solutions or something else.
Thank you for reviewing. I'm about to upload v5 with fixes.
Quoted 12 lines
Quoted 12 lines
include/linux/hugetlb_cgroup.h +12
-0+ usage +=hugetlb_cgroup_from_css (css) + ->nodeinfo[nid] + ->usage[idx]; + } +rcu_read_unlock (); +seq_printf (seq," N%d=%lu" , nid, usage *PAGE_SIZE ); + } + +seq_putc (seq,'\n' ); + +return 0 ; +}
Quoted 147 lines · Mina Almasry
For hugetlb backed jobs/VMs it's critical to understand the numa information for the memory backing these jobs to deliver optimal performance.
Currently this technically can be queried from
/proc/self/numa_maps, but there are significant issues with that. Namely:
1. Memory can be mapped on unmapped.
2.numa_mapsare per process and need to be aggregated across all processes in the cgroup. For shared memory this is more involved as the userspace needs to make sure it doesn't double count shared mappings.
3. I believe queryingnuma_mapsneeds to hold themmap_lockwhich adds to the contention on this lock.For these reasons I propose simply adding hugetlb.*.numa_stat file, which shows the numa information of the cgroup similarly to memory.numa_stat.
On cgroup-v2:
cat /sys/fs/cgroup/unified/test/hugetlb.2MB.numa_stat total=2097152 N0=2097152 N1=0On cgroup-v1:
cat /sys/fs/cgroup/hugetlb/test/hugetlb.2MB.numa_stat total=2097152 N0=2097152 N1=0 hierarichal_total=2097152 N0=2097152 N1=0This patch was tested manually by allocating hugetlb memory and querying the hugetlb.*.numa_stat file of the cgroup and its parents. 
Cc: Mike Kravetz <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: Miaohe Lin <[email protected]>
Cc: Oscar Salvador <[email protected]>
Cc: Michal Hocko <[email protected]>
Cc: Muchun Song <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Shakeel Butt <[email protected]>
Cc: Jue Wang <[email protected]>
Cc: Yang Yao <[email protected]>
Cc: Joanna Li <[email protected]>
Cc: Cannon Matthews <[email protected]>
Cc: [email protected]
Cc: [email protected]Signed-off-by Mina Almasry <[email protected]>---
Changes in v4:
- Removed unnecessary braces.
- usage is now counted in pages instead of bytes.
- Reverted unneeded changes towrite_to_hugetlbfs.cChanges in v3:
- Fixed typos (sorry!)
- Used conventional locations for cgroups mount points in docs/commit message.
- Updated docs.
- Handlekzalloc_nodefailure, and proper deallocation of per node data.
- Usestruct_size()to calculate the struct size.
- Usenr_node_idsinstead ofMAX_NUMNODES.
- Updated comments per multi-line comment pattern.Changes in v2:
- Fix warning Reported-by: kernel test robot <[email protected]>--- .../admin-guide/cgroup-v1/hugetlb.rst | 4 + Documentation/admin-guide/cgroup-v2.rst | 5 + include/linux/hugetlb.h | 4 +- include/linux/hugetlb_cgroup.h | 7 ++ mm/hugetlb_cgroup.c | 113 ++++++++++++++++-- 5 files changed, 122 insertions(+), 11 deletions(-)Documentation/admin-guide/cgroup-v1/hugetlb.rst +4
-0@@ -29,12 +29,14 @@ Brief summary of control files:: hugetlb.<hugepagesize>.max_usage_in_bytes # show max "hugepagesize" hugetlb usage recorded hugetlb.<hugepagesize>.usage_in_bytes # show current usage for "hugepagesize" hugetlb hugetlb.<hugepagesize>.failcnt # show the number of allocation failure due to HugeTLB usage limit + hugetlb.<hugepagesize>.numa_stat # show the numa information of the hugetlb memory charged to this cgroup For a system supporting three hugepage sizes (64k, 32M and 1G), the control files include:: hugetlb.1GB.limit_in_bytes hugetlb.1GB.max_usage_in_bytes + hugetlb.1GB.numa_stat hugetlb.1GB.usage_in_bytes hugetlb.1GB.failcnt hugetlb.1GB.rsvd.limit_in_bytes@@ -43,6 +45,7 @@ files include:: hugetlb.1GB.rsvd.failcnt hugetlb.64KB.limit_in_bytes hugetlb.64KB.max_usage_in_bytes + hugetlb.64KB.numa_stat hugetlb.64KB.usage_in_bytes hugetlb.64KB.failcnt hugetlb.64KB.rsvd.limit_in_bytes@@ -51,6 +54,7 @@ files include:: hugetlb.64KB.rsvd.failcnt hugetlb.32MB.limit_in_bytes hugetlb.32MB.max_usage_in_bytes + hugetlb.32MB.numa_stat hugetlb.32MB.usage_in_bytes hugetlb.32MB.failcnt hugetlb.32MB.rsvd.limit_in_bytesDocumentation/admin-guide/cgroup-v2.rst +5
-0@@ -2252,6 +2252,11 @@ HugeTLB Interface Files are local to the cgroup i.e. not hierarchical. The file modified event generated on this file reflects only the local events. + hugetlb.<hugepagesize>.numa_stat + Similar to memory.numa_stat, it shows the numa information of the + hugetlb pages of <hugepagesize> in this cgroup. Only active in + use hugetlb pages are included. The per-node values are in bytes. + Misc ----include/linux/hugetlb.h +2
-2@@ -613,8 +613,8 @@ struct hstate {#endif #ifdef CONFIG_CGROUP_HUGETLB /* cgroup control files */ -struct cftype cgroup_files_dfl[7 ]; -struct cftype cgroup_files_legacy[9 ]; +struct cftype cgroup_files_dfl[8 ]; +struct cftype cgroup_files_legacy[10 ];#endif char name[HSTATE_NAME_LEN ]; };include/linux/hugetlb_cgroup.h +0
-0
include/linux/hugetlb_cgroup.h enum hugetlb_memory_event { · line 40
Quoted 5 lines
include/linux/hugetlb_cgroup.h +2
-0@@ -36,6 +36,11 @@ enum hugetlb_memory_event { HUGETLB_NR_MEMORY_EVENTS, }; +struct hugetlb_cgroup_per_node { +/* hugetlb usage in bytes over all hstates. */
The accounting unit is pages not bytes here.
Thanks.