Re: [PATCH v4] hugetlb: Add hugetlb.*.numa_stat file

From: Muchun Song
Date: Wed Nov 10 2021 - 01:28:25 EST


On Tue, Nov 9, 2021 at 5:04 AM Mina Almasry <almasrymina@xxxxxxxxxx> wrote:
>
> 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 <mike.kravetz@xxxxxxxxxx>
> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> Cc: Shuah Khan <shuah@xxxxxxxxxx>
> Cc: Miaohe Lin <linmiaohe@xxxxxxxxxx>
> Cc: Oscar Salvador <osalvador@xxxxxxx>
> Cc: Michal Hocko <mhocko@xxxxxxxx>
> Cc: Muchun Song <songmuchun@xxxxxxxxxxxxx>
> Cc: David Rientjes <rientjes@xxxxxxxxxx>
> Cc: Shakeel Butt <shakeelb@xxxxxxxxxx>
> Cc: Jue Wang <juew@xxxxxxxxxx>
> Cc: Yang Yao <ygyao@xxxxxxxxxx>
> Cc: Joanna Li <joannali@xxxxxxxxxx>
> Cc: Cannon Matthews <cannonmatthews@xxxxxxxxxx>
> Cc: linux-mm@xxxxxxxxx
> Cc: linux-kernel@xxxxxxxxxxxxxxx
>
> Signed-off-by: Mina Almasry <almasrymina@xxxxxxxxxx>
>
> ---
>
> 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 <lkp@xxxxxxxxx>
> ---
> .../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(-)
>
> diff --git a/Documentation/admin-guide/cgroup-v1/hugetlb.rst b/Documentation/admin-guide/cgroup-v1/hugetlb.rst
> index 338f2c7d7a1c..0fa724d82abb 100644
> --- a/Documentation/admin-guide/cgroup-v1/hugetlb.rst
> +++ b/Documentation/admin-guide/cgroup-v1/hugetlb.rst
> @@ -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
> diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
> index 4d8c27eca96b..356847f8f008 100644
> --- a/Documentation/admin-guide/cgroup-v2.rst
> +++ b/Documentation/admin-guide/cgroup-v2.rst
> @@ -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
> ----
>
> diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
> index 1faebe1cd0ed..0445faaa636e 100644
> --- a/include/linux/hugetlb.h
> +++ b/include/linux/hugetlb.h
> @@ -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];
> };
> diff --git a/include/linux/hugetlb_cgroup.h b/include/linux/hugetlb_cgroup.h
> index c137396129db..54ff6ec68ed3 100644
> --- a/include/linux/hugetlb_cgroup.h
> +++ b/include/linux/hugetlb_cgroup.h
> @@ -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.