Skip to contentSkip to conversation

hugetlb: Add hugetlb.*.numa_stat file

PATCH Superseded Mina Almasry 2 participants 2 messages Oct 19, 2021 – Oct 20, 2021

Subsystems (5)

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 techinically 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 aggregaged across all proceses 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 /dev/cgroup/memory/test/hugetlb.2MB.numa_stat total=2097152 N0=2097152 N1=0

On cgroup-v1:
cat /dev/cgroup/memory/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: 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]

---
 .../admin-guide/cgroup-v1/hugetlb.rst         |  4 +
 Documentation/admin-guide/cgroup-v2.rst       |  7 ++
 include/linux/hugetlb.h                       |  4 +-
 include/linux/hugetlb_cgroup.h                |  7 ++
 mm/hugetlb_cgroup.c                           | 93 +++++++++++++++++--
 .../testing/selftests/vm/write_to_hugetlbfs.c |  9 +-
 6 files changed, 113 insertions(+), 11 deletions(-)
Documentation/admin-guide/cgroup-v1/hugetlb.rst +4 -0
index 338f2c7d7a1c..0fa724d82abb 100644
@@ -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 +7 -0
index 4d8c27eca96b..8ba0d6aadd2c 100644
@@ -2252,6 +2252,13 @@ 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
+	memory in this cgroup:
+
+	/dev/cgroup/memory/test # cat hugetlb.2MB.numa_stat
+	total=0 N0=0 N1=0
+
 Misc
 ----
 
include/linux/hugetlb.h +2 -2
index 1faebe1cd0ed..0445faaa636e 100644
@@ -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
index c137396129db..54ff6ec68ed3 100644
@@ -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 +86 -7

155 lines not on this page · raw message

tools/testing/selftests/vm/write_to_hugetlbfs.c +7 -2
index 6a2caba19ee1..d2da6315a40c 100644
@@ -37,8 +37,8 @@ static int shmid;
 static void exit_usage(void)
 {
 	printf("Usage: %s -p <path to hugetlbfs file> -s <size to map> "
-	       "[-m <0=hugetlbfs | 1=mmap(MAP_HUGETLB)>] [-l] [-r] "
-	       "[-o] [-w] [-n]\n",
+	       "[-m <0=hugetlbfs | 1=mmap(MAP_HUGETLB)>] [-l(sleep)] [-r(private)] "
+	       "[-o(populate)] [-w(rite)] [-n(o-reserve)]\n",
 	       self);
 	exit(EXIT_FAILURE);
 }
@@ -161,6 +161,11 @@ int main(int argc, char **argv)
 	else
 		printf("RESERVE mapping.\n");
 
+	if (want_sleep)
+		printf("Sleeping\n");
+	else
+		printf("Not sleeping\n");
+
 	switch (method) {
 	case HUGETLBFS:
 		printf("Allocating using HUGETLBFS.\n");

--
2.33.0.1079.g6e70778dc9-goog

#1 lore raw
Thank you for the patch! Perhaps something to improve:

Hi Mina,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on hnaz-mm/master]
[also build test WARNING on tj-cgroup/for-next linus/master v5.15-rc6 next-20211020] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Mina-Almasry/hugetlb-Add-hugetlb-numa_stat-file/20211020-055543
base:   https://github.com/hnaz/linux-mm master
config: x86_64-randconfig-a004-20211019 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 9660563950aaed54020bfdf0be07e7096a9553e4)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/cdac71b7be0126a6f559110105cb7baff1b6552b
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Mina-Almasry/hugetlb-Add-hugetlb-numa_stat-file/20211020-055543 git checkout cdac71b7be0126a6f559110105cb7baff1b6552b
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> mm/hugetlb_cgroup.c:469:6: warning: format specifies type 'unsigned long' but the argument has type 'unsigned long long' [-Wformat]
                      (u64)page_counter_read(&h_cg->hugepage[idx]) * PAGE_SIZE);
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 warning generated.

vim +469 mm/hugetlb_cgroup.c

440
441 static int hugetlb_cgroup_read_numa_stat(struct seq_file *seq, void *dummy)
442 {
443 int nid;
444 struct cftype *cft = seq_cft(seq);
445 int idx = MEMFILE_IDX(cft->private);
446 bool legacy = MEMFILE_ATTR(cft->private);
447 struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq));
448 struct cgroup_subsys_state *css;
449 unsigned long usage;
450
451 if (legacy) {
452 /* Add up usage across all nodes for the non-hierarchical total. */
453 usage = 0;
454 for_each_node_state(nid, N_MEMORY)
455 usage += h_cg->nodeinfo[nid]->usage[idx];
456 seq_printf(seq, "total=%lu", usage);
457
458 /* Simply print the per-node usage for the non-hierarchical total. */
459 for_each_node_state(nid, N_MEMORY)
460 seq_printf(seq, " N%d=%lu", nid,
461 h_cg->nodeinfo[nid]->usage[idx]);
462 seq_putc(seq, '\n');
463 }
464
465 /* The hierarchical total is pretty much the value recorded by the
466 * counter, so use that.
467 */
468 seq_printf(seq, "%stotal=%lu", legacy ? "hierarichal_" : "",

 > 469			   (u64)page_counter_read(&h_cg->hugepage[idx]) * PAGE_SIZE);

470
471 /* For each node, transverse the css tree to obtain the hierarichal
472 * node usage.
473 */
474 for_each_node_state(nid, N_MEMORY) {
475 usage = 0;
476 rcu_read_lock();
477 css_for_each_descendant_pre(css, &h_cg->css) {
478 usage += hugetlb_cgroup_from_css(css)
479 ->nodeinfo[nid]
480 ->usage[idx];
481 }
482 rcu_read_unlock();
483 seq_printf(seq, " N%d=%lu", nid, usage);
484 }
485
486 seq_putc(seq, '\n');
487
488 return 0;
489 }
490

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org