Skip to contentSkip to conversation

Shmem mTHP controls and stats improvements

PATCH In review Ryan Roberts 4 participants 14 messages Aug 8, 2024 – Sep 5, 2024

Subsystems (4)
lore
Share & tools
lore

Ryan Roberts

author reviewer
#0 lore raw

v3 cover Shmem mTHP controls and stats improvements

Hi All,

This is a small series to tidy up the way the shmem controls and stats are exposed. These patches were previously part of the series at [2], but I decided to split them out since they can go in independently.

---
This applies against today's mm-unstable (275d686abcb59). (although I had to fix a minor build bug in stackdepot.c due to MIN() not being defined in this tree). All mm selftests have been run; no regressions were observed.

Changes since v2 [2]
====================
- swpout stats are now exposed for all orders supported by anon or shmem.

[2] https://lore.kernel.org/linux-mm/[email protected]/

Thanks,
Ryan

Ryan Roberts (2):
mm: Cleanup count_mthp_stat() definition
mm: Tidy up shmem mTHP controls and stats

 include/linux/huge_mm.h |  70 +++++++++----------
 mm/huge_memory.c        | 144 +++++++++++++++++++++++++++++++---------
 mm/memory.c             |   2 -
 mm/shmem.c              |   6 --
 4 files changed, 149 insertions(+), 73 deletions(-)

--
2.43.0

author reviewer
#1 lore raw

PATCH v3 1/2 mm: Cleanup count_mthp_stat() definition

Let's move count_mthp_stat() so that it's always defined, even when THP is disabled. Previously uses of the function in files such as shmem.c, which are compiled even when THP is disabled, required ugly THP ifdeferry. With this cleanup, we can remove those ifdefs and the function resolves to a nop when THP is disabled.

I shortly plan to call count_mthp_stat() from more THP-invariant source files.

---
 include/linux/huge_mm.h | 70 ++++++++++++++++++++---------------------
 mm/memory.c             |  2 --
 mm/shmem.c              |  6 ----
 3 files changed, 35 insertions(+), 43 deletions(-)
include/linux/huge_mm.h +35 -35
index 2b5a175196e7a..969f11f360d29 100644
@@ -114,6 +114,41 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr;
 #define HPAGE_PUD_MASK	(~(HPAGE_PUD_SIZE - 1))
 #define HPAGE_PUD_SIZE	((1UL) << HPAGE_PUD_SHIFT)
 
+enum mthp_stat_item {
+	MTHP_STAT_ANON_FAULT_ALLOC,
+	MTHP_STAT_ANON_FAULT_FALLBACK,
+	MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE,
+	MTHP_STAT_SWPOUT,
+	MTHP_STAT_SWPOUT_FALLBACK,
+	MTHP_STAT_SHMEM_ALLOC,
+	MTHP_STAT_SHMEM_FALLBACK,
+	MTHP_STAT_SHMEM_FALLBACK_CHARGE,
+	MTHP_STAT_SPLIT,
+	MTHP_STAT_SPLIT_FAILED,
+	MTHP_STAT_SPLIT_DEFERRED,
+	__MTHP_STAT_COUNT
+};
+
+#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS)
+struct mthp_stat {
+	unsigned long stats[ilog2(MAX_PTRS_PER_PTE) + 1][__MTHP_STAT_COUNT];
+};
+
+DECLARE_PER_CPU(struct mthp_stat, mthp_stats);
+
+static inline void count_mthp_stat(int order, enum mthp_stat_item item)
+{
+	if (order <= 0 || order > PMD_ORDER)
+		return;
+
+	this_cpu_inc(mthp_stats.stats[order][item]);
+}
+#else
+static inline void count_mthp_stat(int order, enum mthp_stat_item item)
+{
+}
+#endif
+
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 
 extern unsigned long transparent_hugepage_flags;
@@ -269,41 +304,6 @@ struct thpsize {
 
 #define to_thpsize(kobj) container_of(kobj, struct thpsize, kobj)
 
-enum mthp_stat_item {
-	MTHP_STAT_ANON_FAULT_ALLOC,
-	MTHP_STAT_ANON_FAULT_FALLBACK,
-	MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE,
-	MTHP_STAT_SWPOUT,
-	MTHP_STAT_SWPOUT_FALLBACK,
-	MTHP_STAT_SHMEM_ALLOC,
-	MTHP_STAT_SHMEM_FALLBACK,
-	MTHP_STAT_SHMEM_FALLBACK_CHARGE,
-	MTHP_STAT_SPLIT,
-	MTHP_STAT_SPLIT_FAILED,
-	MTHP_STAT_SPLIT_DEFERRED,
-	__MTHP_STAT_COUNT
-};
-
-struct mthp_stat {
-	unsigned long stats[ilog2(MAX_PTRS_PER_PTE) + 1][__MTHP_STAT_COUNT];
-};
-
-#ifdef CONFIG_SYSFS
-DECLARE_PER_CPU(struct mthp_stat, mthp_stats);
-
-static inline void count_mthp_stat(int order, enum mthp_stat_item item)
-{
-	if (order <= 0 || order > PMD_ORDER)
-		return;
-
-	this_cpu_inc(mthp_stats.stats[order][item]);
-}
-#else
-static inline void count_mthp_stat(int order, enum mthp_stat_item item)
-{
-}
-#endif
-
 #define transparent_hugepage_use_zero_page()				\
 	(transparent_hugepage_flags &					\
 	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
mm/memory.c +0 -2
index 87a5c900c77f2..c96ac5b2464fa 100644
@@ -4761,9 +4761,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf)
 
 	folio_ref_add(folio, nr_pages - 1);
 	add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages);
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
 	count_mthp_stat(folio_order(folio), MTHP_STAT_ANON_FAULT_ALLOC);
-#endif
 	folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);
 	folio_add_lru_vma(folio, vma);
 setpte:
mm/shmem.c +0 -6
index 68c9a31bc763b..33a146843e4c0 100644
@@ -1791,9 +1791,7 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf,
 
 			if (pages == HPAGE_PMD_NR)
 				count_vm_event(THP_FILE_FALLBACK);
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
 			count_mthp_stat(order, MTHP_STAT_SHMEM_FALLBACK);
-#endif
 			order = next_order(&suitable_orders, order);
 		}
 	} else {
@@ -1818,10 +1816,8 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf,
 				count_vm_event(THP_FILE_FALLBACK);
 				count_vm_event(THP_FILE_FALLBACK_CHARGE);
 			}
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
 			count_mthp_stat(folio_order(folio), MTHP_STAT_SHMEM_FALLBACK);
 			count_mthp_stat(folio_order(folio), MTHP_STAT_SHMEM_FALLBACK_CHARGE);
-#endif
 		}
 		goto unlock;
 	}
@@ -2301,9 +2297,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index,
 		if (!IS_ERR(folio)) {
 			if (folio_test_pmd_mappable(folio))
 				count_vm_event(THP_FILE_ALLOC);
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
 			count_mthp_stat(folio_order(folio), MTHP_STAT_SHMEM_ALLOC);
-#endif
 			goto alloced;
 		}
 		if (PTR_ERR(folio) == -EEXIST)
Signature · 1 line
2.43.0
author reviewer
#2 lore raw

PATCH v3 2/2 mm: Tidy up shmem mTHP controls and stats

In reply to Ryan Roberts

Previously we had a situation where shmem mTHP controls and stats were not exposed for some supported sizes and were exposed for some unsupported sizes. So let's clean that up.

Anon mTHP can support all large orders [2, PMD_ORDER]. But shmem can support all large orders [1, MAX_PAGECACHE_ORDER]. However, per-size shmem controls and stats were previously being exposed for all the anon mTHP orders, meaning order-1 was not present, and for arm64 64K base pages, orders 12 and 13 were exposed but were not supported internally.

Tidy this all up by defining ctrl and stats attribute groups for anon and file separately. Anon ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_ANON and file ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_FILE_DEFAULT.

Additionally, create "any" ctrl and stats attribute groups which are populated for all orders in (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT). swpout stats use this since they apply to anon and shmem.

The side-effect of all this is that different hugepage-*kB directories contain different sets of controls and stats, depending on which memory types support that size. This approach is preferred over the alternative, which is to populate dummy controls and stats for memory types that do not support a given size.

---
 mm/huge_memory.c | 144 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 114 insertions(+), 30 deletions(-)
mm/huge_memory.c +114 -30
index 0c3075ee00012..082d86b7c6c2f 100644
@@ -482,8 +482,8 @@ static void thpsize_release(struct kobject *kobj);
 static DEFINE_SPINLOCK(huge_anon_orders_lock);
 static LIST_HEAD(thpsize_list);
 
-static ssize_t thpsize_enabled_show(struct kobject *kobj,
-				    struct kobj_attribute *attr, char *buf)
+static ssize_t anon_enabled_show(struct kobject *kobj,
+				 struct kobj_attribute *attr, char *buf)
 {
 	int order = to_thpsize(kobj)->order;
 	const char *output;
@@ -500,9 +500,9 @@ static ssize_t thpsize_enabled_show(struct kobject *kobj,
 	return sysfs_emit(buf, "%s\n", output);
 }
 
-static ssize_t thpsize_enabled_store(struct kobject *kobj,
-				     struct kobj_attribute *attr,
-				     const char *buf, size_t count)
+static ssize_t anon_enabled_store(struct kobject *kobj,
+				  struct kobj_attribute *attr,
+				  const char *buf, size_t count)
 {
 	int order = to_thpsize(kobj)->order;
 	ssize_t ret = count;
@@ -544,19 +544,35 @@ static ssize_t thpsize_enabled_store(struct kobject *kobj,
 	return ret;
 }
 
-static struct kobj_attribute thpsize_enabled_attr =
-	__ATTR(enabled, 0644, thpsize_enabled_show, thpsize_enabled_store);
+static struct kobj_attribute anon_enabled_attr =
+	__ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store);
 
-static struct attribute *thpsize_attrs[] = {
-	&thpsize_enabled_attr.attr,
+static struct attribute *anon_ctrl_attrs[] = {
+	&anon_enabled_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group anon_ctrl_attr_grp = {
+	.attrs = anon_ctrl_attrs,
+};
+
+static struct attribute *file_ctrl_attrs[] = {
 #ifdef CONFIG_SHMEM
 	&thpsize_shmem_enabled_attr.attr,
 #endif
 	NULL,
 };
 
-static const struct attribute_group thpsize_attr_group = {
-	.attrs = thpsize_attrs,
+static const struct attribute_group file_ctrl_attr_grp = {
+	.attrs = file_ctrl_attrs,
+};
+
+static struct attribute *any_ctrl_attrs[] = {
+	NULL,
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+	.attrs = any_ctrl_attrs,
 };
 
 static const struct kobj_type thpsize_ktype = {
@@ -595,64 +611,132 @@ DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, MTHP_STAT_ANON_FAULT_FALLBACK);
 DEFINE_MTHP_STAT_ATTR(anon_fault_fallback_charge, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
 DEFINE_MTHP_STAT_ATTR(swpout, MTHP_STAT_SWPOUT);
 DEFINE_MTHP_STAT_ATTR(swpout_fallback, MTHP_STAT_SWPOUT_FALLBACK);
+#ifdef CONFIG_SHMEM
 DEFINE_MTHP_STAT_ATTR(shmem_alloc, MTHP_STAT_SHMEM_ALLOC);
 DEFINE_MTHP_STAT_ATTR(shmem_fallback, MTHP_STAT_SHMEM_FALLBACK);
 DEFINE_MTHP_STAT_ATTR(shmem_fallback_charge, MTHP_STAT_SHMEM_FALLBACK_CHARGE);
+#endif
 DEFINE_MTHP_STAT_ATTR(split, MTHP_STAT_SPLIT);
 DEFINE_MTHP_STAT_ATTR(split_failed, MTHP_STAT_SPLIT_FAILED);
 DEFINE_MTHP_STAT_ATTR(split_deferred, MTHP_STAT_SPLIT_DEFERRED);
 
-static struct attribute *stats_attrs[] = {
+static struct attribute *anon_stats_attrs[] = {
 	&anon_fault_alloc_attr.attr,
 	&anon_fault_fallback_attr.attr,
 	&anon_fault_fallback_charge_attr.attr,
+#ifndef CONFIG_SHMEM
 	&swpout_attr.attr,
 	&swpout_fallback_attr.attr,
-	&shmem_alloc_attr.attr,
-	&shmem_fallback_attr.attr,
-	&shmem_fallback_charge_attr.attr,
+#endif
 	&split_attr.attr,
 	&split_failed_attr.attr,
 	&split_deferred_attr.attr,
 	NULL,
 };
 
-static struct attribute_group stats_attr_group = {
+static struct attribute_group anon_stats_attr_grp = {
+	.name = "stats",
+	.attrs = anon_stats_attrs,
+};
+
+static struct attribute *file_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+	&shmem_alloc_attr.attr,
+	&shmem_fallback_attr.attr,
+	&shmem_fallback_charge_attr.attr,
+#endif
+	NULL,
+};
+
+static struct attribute_group file_stats_attr_grp = {
+	.name = "stats",
+	.attrs = file_stats_attrs,
+};
+
+static struct attribute *any_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+	&swpout_attr.attr,
+	&swpout_fallback_attr.attr,
+#endif
+	NULL,
+};
+
+static struct attribute_group any_stats_attr_grp = {
 	.name = "stats",
-	.attrs = stats_attrs,
+	.attrs = any_stats_attrs,
 };
 
+static int sysfs_add_group(struct kobject *kobj,
+			   const struct attribute_group *grp)
+{
+	int ret = -ENOENT;
+
+	/*
+	 * If the group is named, try to merge first, assuming the subdirectory
+	 * was already created. This avoids the warning emitted by
+	 * sysfs_create_group() if the directory already exists.
+	 */
+	if (grp->name)
+		ret = sysfs_merge_group(kobj, grp);
+	if (ret)
+		ret = sysfs_create_group(kobj, grp);
+
+	return ret;
+}
+
 static struct thpsize *thpsize_create(int order, struct kobject *parent)
 {
 	unsigned long size = (PAGE_SIZE << order) / SZ_1K;
 	struct thpsize *thpsize;
-	int ret;
+	int ret = -ENOMEM;
 
 	thpsize = kzalloc(sizeof(*thpsize), GFP_KERNEL);
 	if (!thpsize)
-		return ERR_PTR(-ENOMEM);
+		goto err;
+
+	thpsize->order = order;
 
 	ret = kobject_init_and_add(&thpsize->kobj, &thpsize_ktype, parent,
 				   "hugepages-%lukB", size);
 	if (ret) {
 		kfree(thpsize);
-		return ERR_PTR(ret);
+		goto err;
 	}
 
-	ret = sysfs_create_group(&thpsize->kobj, &thpsize_attr_group);
-	if (ret) {
-		kobject_put(&thpsize->kobj);
-		return ERR_PTR(ret);
+
+	ret = sysfs_add_group(&thpsize->kobj, &any_ctrl_attr_grp);
+	if (ret)
+		goto err_put;
+
+	ret = sysfs_add_group(&thpsize->kobj, &any_stats_attr_grp);
+	if (ret)
+		goto err_put;
+
+	if (BIT(order) & THP_ORDERS_ALL_ANON) {
+		ret = sysfs_add_group(&thpsize->kobj, &anon_ctrl_attr_grp);
+		if (ret)
+			goto err_put;
+
+		ret = sysfs_add_group(&thpsize->kobj, &anon_stats_attr_grp);
+		if (ret)
+			goto err_put;
 	}
 
-	ret = sysfs_create_group(&thpsize->kobj, &stats_attr_group);
-	if (ret) {
-		kobject_put(&thpsize->kobj);
-		return ERR_PTR(ret);
+	if (BIT(order) & THP_ORDERS_ALL_FILE_DEFAULT) {
+		ret = sysfs_add_group(&thpsize->kobj, &file_ctrl_attr_grp);
+		if (ret)
+			goto err_put;
+
+		ret = sysfs_add_group(&thpsize->kobj, &file_stats_attr_grp);
+		if (ret)
+			goto err_put;
 	}
 
-	thpsize->order = order;
 	return thpsize;
+err_put:
+	kobject_put(&thpsize->kobj);
+err:
+	return ERR_PTR(ret);
 }
 
 static void thpsize_release(struct kobject *kobj)
@@ -692,7 +776,7 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
 		goto remove_hp_group;
 	}
 
-	orders = THP_ORDERS_ALL_ANON;
+	orders = THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT;
 	order = highest_order(orders);
 	while (orders) {
 		thpsize = thpsize_create(order, *hugepage_kobj);
Signature · 1 line
2.43.0
reviewer Tested-by on v3 2/2
#3 lore raw

Re: [PATCH v3 2/2] mm: Tidy up shmem mTHP controls and stats

Quoted 16 lines · Ryan Roberts

Previously we had a situation where shmem mTHP controls and stats were not exposed for some supported sizes and were exposed for some unsupported sizes. So let's clean that up.

Anon mTHP can support all large orders [2, PMD_ORDER]. But shmem can support all large orders [1, MAX_PAGECACHE_ORDER]. However, per-size shmem controls and stats were previously being exposed for all the anon mTHP orders, meaning order-1 was not present, and for arm64 64K base pages, orders 12 and 13 were exposed but were not supported internally.

Tidy this all up by defining ctrl and stats attribute groups for anon and file separately. Anon ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_ANON and file ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_FILE_DEFAULT.

Quoted 4 lines

Additionally, create "any" ctrl and stats attribute groups which are populated for all orders in (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT). swpout stats use this since they apply to anon and shmem.

This passage is a bit confusing to me. I'm not sure whether it's about creating any control and stats for both file and anon, or creating them separately depending on the situation. However, the previous passage mentions that file and anon create control and stats separately based on different orders, and the orders they create might differ.

But after running your patches, I understood it. For instance, I'm now seeing 8kB folder that didn't exist before:

/sys/kernel/mm/transparent_hugepage # ls -l
drwxr-xr-x 3 root root 0 Aug 8 22:01 hugepages-1024kB
...
drwxr-xr-x 3 root root 0 Aug 8 22:01 hugepages-8kB
drwxr-xr-x 2 root root 0 Aug 8 22:01 khugepaged
...

Then, when I entered the 8kB folder, I found 'shmem_enabled' but not 'enabled' for anon:
/sys/kernel/mm/transparent_hugepage/hugepages-8kB # ls shmem_enabled stats

However, in the 16kB folder, I found both:
/sys/kernel/mm/transparent_hugepage/hugepages-16kB # ls enabled shmem_enabled stats

In the 8kB 'stats,' I see 'shmem_alloc' but not 'anon_alloc.' Since both shmem and anon require swapout, I am seeing 'swpout' and 'swpout_fallback':

/sys/kernel/mm/transparent_hugepage/hugepages-8kB/stats # ls shmem_alloc shmem_fallback shmem_fallback_charge swpout
swpout_fallback

Everything observed seems to meet expectations, so:

Quoted 243 lines

The side-effect of all this is that different hugepage-*kB directories contain different sets of controls and stats, depending on which memory types support that size. This approach is preferred over the alternative, which is to populate dummy controls and stats for memory types that do not support a given size.

---
 mm/huge_memory.c | 144 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 114 insertions(+), 30 deletions(-)
mm/huge_memory.c +114 -31
index 0c3075ee00012..082d86b7c6c2f 100644
@@ -482,8 +482,8 @@ static void thpsize_release(struct kobject *kobj);
 static DEFINE_SPINLOCK(huge_anon_orders_lock);
 static LIST_HEAD(thpsize_list);
 
-static ssize_t thpsize_enabled_show(struct kobject *kobj,
-                                   struct kobj_attribute *attr, char *buf)
+static ssize_t anon_enabled_show(struct kobject *kobj,
+                                struct kobj_attribute *attr, char *buf)
 {
        int order = to_thpsize(kobj)->order;
        const char *output;
@@ -500,9 +500,9 @@ static ssize_t thpsize_enabled_show(struct kobject *kobj,
        return sysfs_emit(buf, "%s\n", output);
 }
 
-static ssize_t thpsize_enabled_store(struct kobject *kobj,
-                                    struct kobj_attribute *attr,
-                                    const char *buf, size_t count)
+static ssize_t anon_enabled_store(struct kobject *kobj,
+                                 struct kobj_attribute *attr,
+                                 const char *buf, size_t count)
 {
        int order = to_thpsize(kobj)->order;
        ssize_t ret = count;
@@ -544,19 +544,35 @@ static ssize_t thpsize_enabled_store(struct kobject *kobj,
        return ret;
 }
 
-static struct kobj_attribute thpsize_enabled_attr =
-       __ATTR(enabled, 0644, thpsize_enabled_show, thpsize_enabled_store);
+static struct kobj_attribute anon_enabled_attr =
+       __ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store);
 
-static struct attribute *thpsize_attrs[] = {
-       &thpsize_enabled_attr.attr,
+static struct attribute *anon_ctrl_attrs[] = {
+       &anon_enabled_attr.attr,
+       NULL,
+};
+
+static const struct attribute_group anon_ctrl_attr_grp = {
+       .attrs = anon_ctrl_attrs,
+};
+
+static struct attribute *file_ctrl_attrs[] = {
 #ifdef CONFIG_SHMEM
        &thpsize_shmem_enabled_attr.attr,
 #endif
        NULL,
 };
 
-static const struct attribute_group thpsize_attr_group = {
-       .attrs = thpsize_attrs,
+static const struct attribute_group file_ctrl_attr_grp = {
+       .attrs = file_ctrl_attrs,
+};
+
+static struct attribute *any_ctrl_attrs[] = {
+       NULL,
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+       .attrs = any_ctrl_attrs,
 };
 
 static const struct kobj_type thpsize_ktype = {
@@ -595,64 +611,132 @@ DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, MTHP_STAT_ANON_FAULT_FALLBACK);
 DEFINE_MTHP_STAT_ATTR(anon_fault_fallback_charge, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
 DEFINE_MTHP_STAT_ATTR(swpout, MTHP_STAT_SWPOUT);
 DEFINE_MTHP_STAT_ATTR(swpout_fallback, MTHP_STAT_SWPOUT_FALLBACK);
+#ifdef CONFIG_SHMEM
 DEFINE_MTHP_STAT_ATTR(shmem_alloc, MTHP_STAT_SHMEM_ALLOC);
 DEFINE_MTHP_STAT_ATTR(shmem_fallback, MTHP_STAT_SHMEM_FALLBACK);
 DEFINE_MTHP_STAT_ATTR(shmem_fallback_charge, MTHP_STAT_SHMEM_FALLBACK_CHARGE);
+#endif
 DEFINE_MTHP_STAT_ATTR(split, MTHP_STAT_SPLIT);
 DEFINE_MTHP_STAT_ATTR(split_failed, MTHP_STAT_SPLIT_FAILED);
 DEFINE_MTHP_STAT_ATTR(split_deferred, MTHP_STAT_SPLIT_DEFERRED);
 
-static struct attribute *stats_attrs[] = {
+static struct attribute *anon_stats_attrs[] = {
        &anon_fault_alloc_attr.attr,
        &anon_fault_fallback_attr.attr,
        &anon_fault_fallback_charge_attr.attr,
+#ifndef CONFIG_SHMEM
        &swpout_attr.attr,
        &swpout_fallback_attr.attr,
-       &shmem_alloc_attr.attr,
-       &shmem_fallback_attr.attr,
-       &shmem_fallback_charge_attr.attr,
+#endif
        &split_attr.attr,
        &split_failed_attr.attr,
        &split_deferred_attr.attr,
        NULL,
 };
 
-static struct attribute_group stats_attr_group = {
+static struct attribute_group anon_stats_attr_grp = {
+       .name = "stats",
+       .attrs = anon_stats_attrs,
+};
+
+static struct attribute *file_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+       &shmem_alloc_attr.attr,
+       &shmem_fallback_attr.attr,
+       &shmem_fallback_charge_attr.attr,
+#endif
+       NULL,
+};
+
+static struct attribute_group file_stats_attr_grp = {
+       .name = "stats",
+       .attrs = file_stats_attrs,
+};
+
+static struct attribute *any_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+       &swpout_attr.attr,
+       &swpout_fallback_attr.attr,
+#endif
+       NULL,
+};
+
+static struct attribute_group any_stats_attr_grp = {
        .name = "stats",
-       .attrs = stats_attrs,
+       .attrs = any_stats_attrs,
 };
 
+static int sysfs_add_group(struct kobject *kobj,
+                          const struct attribute_group *grp)
+{
+       int ret = -ENOENT;
+
+       /*
+        * If the group is named, try to merge first, assuming the subdirectory
+        * was already created. This avoids the warning emitted by
+        * sysfs_create_group() if the directory already exists.
+        */
+       if (grp->name)
+               ret = sysfs_merge_group(kobj, grp);
+       if (ret)
+               ret = sysfs_create_group(kobj, grp);
+
+       return ret;
+}
+
 static struct thpsize *thpsize_create(int order, struct kobject *parent)
 {
        unsigned long size = (PAGE_SIZE << order) / SZ_1K;
        struct thpsize *thpsize;
-       int ret;
+       int ret = -ENOMEM;
 
        thpsize = kzalloc(sizeof(*thpsize), GFP_KERNEL);
        if (!thpsize)
-               return ERR_PTR(-ENOMEM);
+               goto err;
+
+       thpsize->order = order;
 
        ret = kobject_init_and_add(&thpsize->kobj, &thpsize_ktype, parent,
                                   "hugepages-%lukB", size);
        if (ret) {
                kfree(thpsize);
-               return ERR_PTR(ret);
+               goto err;
        }
 
-       ret = sysfs_create_group(&thpsize->kobj, &thpsize_attr_group);
-       if (ret) {
-               kobject_put(&thpsize->kobj);
-               return ERR_PTR(ret);
+
+       ret = sysfs_add_group(&thpsize->kobj, &any_ctrl_attr_grp);
+       if (ret)
+               goto err_put;
+
+       ret = sysfs_add_group(&thpsize->kobj, &any_stats_attr_grp);
+       if (ret)
+               goto err_put;
+
+       if (BIT(order) & THP_ORDERS_ALL_ANON) {
+               ret = sysfs_add_group(&thpsize->kobj, &anon_ctrl_attr_grp);
+               if (ret)
+                       goto err_put;
+
+               ret = sysfs_add_group(&thpsize->kobj, &anon_stats_attr_grp);
+               if (ret)
+                       goto err_put;
        }
 
-       ret = sysfs_create_group(&thpsize->kobj, &stats_attr_group);
-       if (ret) {
-               kobject_put(&thpsize->kobj);
-               return ERR_PTR(ret);
+       if (BIT(order) & THP_ORDERS_ALL_FILE_DEFAULT) {
+               ret = sysfs_add_group(&thpsize->kobj, &file_ctrl_attr_grp);
+               if (ret)
+                       goto err_put;
+
+               ret = sysfs_add_group(&thpsize->kobj, &file_stats_attr_grp);
+               if (ret)
+                       goto err_put;
        }
 
-       thpsize->order = order;
        return thpsize;
+err_put:
+       kobject_put(&thpsize->kobj);
+err:
+       return ERR_PTR(ret);
 }
 
 static void thpsize_release(struct kobject *kobj)
@@ -692,7 +776,7 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
                goto remove_hp_group;
        }
 
-       orders = THP_ORDERS_ALL_ANON;
+       orders = THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT;
        order = highest_order(orders);
        while (orders) {
                thpsize = thpsize_create(order, *hugepage_kobj);
--
Quoted 2 lines

2.43.0

Thanks
Barry

author reviewer
#4 lore raw

Re: [PATCH v3 2/2] mm: Tidy up shmem mTHP controls and stats

Quoted 22 lines · Barry Song
Quoted 16 lines · Ryan Roberts

Previously we had a situation where shmem mTHP controls and stats were not exposed for some supported sizes and were exposed for some unsupported sizes. So let's clean that up.

Anon mTHP can support all large orders [2, PMD_ORDER]. But shmem can support all large orders [1, MAX_PAGECACHE_ORDER]. However, per-size shmem controls and stats were previously being exposed for all the anon mTHP orders, meaning order-1 was not present, and for arm64 64K base pages, orders 12 and 13 were exposed but were not supported internally.

Tidy this all up by defining ctrl and stats attribute groups for anon and file separately. Anon ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_ANON and file ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_FILE_DEFAULT.

Quoted 4 lines

Additionally, create "any" ctrl and stats attribute groups which are populated for all orders in (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT). swpout stats use this since they apply to anon and shmem.

Quoted 5 lines

This passage is a bit confusing to me. I'm not sure whether it's about creating any control and stats for both file and anon, or creating them separately depending on the situation. However, the previous passage mentions that file and anon create control and stats separately based on different orders, and the orders they create might differ.

Yes, I'll admit that I added this paragraph for the change I made since the last version, and it's not the clearest thing I've ever written on reflection.

The idea is that there are 3 buckets; "anon", "file" and "any". controls/stats in the "anon" bucket are populated for all orders supported by anon memory. Same for file. controls/stats in the "any" bucket are populated for all orders supported by either anon or file.

Quoted 24 lines

But after running your patches, I understood it. For instance, I'm now seeing 8kB folder that didn't exist before:

/sys/kernel/mm/transparent_hugepage # ls -l
drwxr-xr-x 3 root root 0 Aug 8 22:01 hugepages-1024kB
...
drwxr-xr-x 3 root root 0 Aug 8 22:01 hugepages-8kB
drwxr-xr-x 2 root root 0 Aug 8 22:01 khugepaged
...

Then, when I entered the 8kB folder, I found 'shmem_enabled' but not 'enabled' for anon:
/sys/kernel/mm/transparent_hugepage/hugepages-8kB # ls shmem_enabled stats

However, in the 16kB folder, I found both:
/sys/kernel/mm/transparent_hugepage/hugepages-16kB # ls enabled shmem_enabled stats

In the 8kB 'stats,' I see 'shmem_alloc' but not 'anon_alloc.' Since both shmem and anon require swapout, I am seeing 'swpout' and 'swpout_fallback':

Quoted 5 lines

/sys/kernel/mm/transparent_hugepage/hugepages-8kB/stats # ls shmem_alloc shmem_fallback shmem_fallback_charge swpout
swpout_fallback

Everything observed seems to meet expectations, so:

Yes, what you observe is exactly as intended. It gets a bit tricky if CONFIG_SHMEM is disabled; In this case, some ifdeffery ensures that the swpout stats are declared in the "anon" bucket instead of the "any" bucket.

Quoted 2 lines

Thanks!

Quoted 249 lines
Quoted 244 lines

The side-effect of all this is that different hugepage-*kB directories contain different sets of controls and stats, depending on which memory types support that size. This approach is preferred over the alternative, which is to populate dummy controls and stats for memory types that do not support a given size.

---
 mm/huge_memory.c | 144 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 114 insertions(+), 30 deletions(-)
mm/huge_memory.c +114 -31
index 0c3075ee00012..082d86b7c6c2f 100644
@@ -482,8 +482,8 @@ static void thpsize_release(struct kobject *kobj);
 static DEFINE_SPINLOCK(huge_anon_orders_lock);
 static LIST_HEAD(thpsize_list);
 
-static ssize_t thpsize_enabled_show(struct kobject *kobj,
-                                   struct kobj_attribute *attr, char *buf)
+static ssize_t anon_enabled_show(struct kobject *kobj,
+                                struct kobj_attribute *attr, char *buf)
 {
        int order = to_thpsize(kobj)->order;
        const char *output;
@@ -500,9 +500,9 @@ static ssize_t thpsize_enabled_show(struct kobject *kobj,
        return sysfs_emit(buf, "%s\n", output);
 }
 
-static ssize_t thpsize_enabled_store(struct kobject *kobj,
-                                    struct kobj_attribute *attr,
-                                    const char *buf, size_t count)
+static ssize_t anon_enabled_store(struct kobject *kobj,
+                                 struct kobj_attribute *attr,
+                                 const char *buf, size_t count)
 {
        int order = to_thpsize(kobj)->order;
        ssize_t ret = count;
@@ -544,19 +544,35 @@ static ssize_t thpsize_enabled_store(struct kobject *kobj,
        return ret;
 }
 
-static struct kobj_attribute thpsize_enabled_attr =
-       __ATTR(enabled, 0644, thpsize_enabled_show, thpsize_enabled_store);
+static struct kobj_attribute anon_enabled_attr =
+       __ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store);
 
-static struct attribute *thpsize_attrs[] = {
-       &thpsize_enabled_attr.attr,
+static struct attribute *anon_ctrl_attrs[] = {
+       &anon_enabled_attr.attr,
+       NULL,
+};
+
+static const struct attribute_group anon_ctrl_attr_grp = {
+       .attrs = anon_ctrl_attrs,
+};
+
+static struct attribute *file_ctrl_attrs[] = {
 #ifdef CONFIG_SHMEM
        &thpsize_shmem_enabled_attr.attr,
 #endif
        NULL,
 };
 
-static const struct attribute_group thpsize_attr_group = {
-       .attrs = thpsize_attrs,
+static const struct attribute_group file_ctrl_attr_grp = {
+       .attrs = file_ctrl_attrs,
+};
+
+static struct attribute *any_ctrl_attrs[] = {
+       NULL,
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+       .attrs = any_ctrl_attrs,
 };
 
 static const struct kobj_type thpsize_ktype = {
@@ -595,64 +611,132 @@ DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, MTHP_STAT_ANON_FAULT_FALLBACK);
 DEFINE_MTHP_STAT_ATTR(anon_fault_fallback_charge, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
 DEFINE_MTHP_STAT_ATTR(swpout, MTHP_STAT_SWPOUT);
 DEFINE_MTHP_STAT_ATTR(swpout_fallback, MTHP_STAT_SWPOUT_FALLBACK);
+#ifdef CONFIG_SHMEM
 DEFINE_MTHP_STAT_ATTR(shmem_alloc, MTHP_STAT_SHMEM_ALLOC);
 DEFINE_MTHP_STAT_ATTR(shmem_fallback, MTHP_STAT_SHMEM_FALLBACK);
 DEFINE_MTHP_STAT_ATTR(shmem_fallback_charge, MTHP_STAT_SHMEM_FALLBACK_CHARGE);
+#endif
 DEFINE_MTHP_STAT_ATTR(split, MTHP_STAT_SPLIT);
 DEFINE_MTHP_STAT_ATTR(split_failed, MTHP_STAT_SPLIT_FAILED);
 DEFINE_MTHP_STAT_ATTR(split_deferred, MTHP_STAT_SPLIT_DEFERRED);
 
-static struct attribute *stats_attrs[] = {
+static struct attribute *anon_stats_attrs[] = {
        &anon_fault_alloc_attr.attr,
        &anon_fault_fallback_attr.attr,
        &anon_fault_fallback_charge_attr.attr,
+#ifndef CONFIG_SHMEM
        &swpout_attr.attr,
        &swpout_fallback_attr.attr,
-       &shmem_alloc_attr.attr,
-       &shmem_fallback_attr.attr,
-       &shmem_fallback_charge_attr.attr,
+#endif
        &split_attr.attr,
        &split_failed_attr.attr,
        &split_deferred_attr.attr,
        NULL,
 };
 
-static struct attribute_group stats_attr_group = {
+static struct attribute_group anon_stats_attr_grp = {
+       .name = "stats",
+       .attrs = anon_stats_attrs,
+};
+
+static struct attribute *file_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+       &shmem_alloc_attr.attr,
+       &shmem_fallback_attr.attr,
+       &shmem_fallback_charge_attr.attr,
+#endif
+       NULL,
+};
+
+static struct attribute_group file_stats_attr_grp = {
+       .name = "stats",
+       .attrs = file_stats_attrs,
+};
+
+static struct attribute *any_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+       &swpout_attr.attr,
+       &swpout_fallback_attr.attr,
+#endif
+       NULL,
+};
+
+static struct attribute_group any_stats_attr_grp = {
        .name = "stats",
-       .attrs = stats_attrs,
+       .attrs = any_stats_attrs,
 };
 
+static int sysfs_add_group(struct kobject *kobj,
+                          const struct attribute_group *grp)
+{
+       int ret = -ENOENT;
+
+       /*
+        * If the group is named, try to merge first, assuming the subdirectory
+        * was already created. This avoids the warning emitted by
+        * sysfs_create_group() if the directory already exists.
+        */
+       if (grp->name)
+               ret = sysfs_merge_group(kobj, grp);
+       if (ret)
+               ret = sysfs_create_group(kobj, grp);
+
+       return ret;
+}
+
 static struct thpsize *thpsize_create(int order, struct kobject *parent)
 {
        unsigned long size = (PAGE_SIZE << order) / SZ_1K;
        struct thpsize *thpsize;
-       int ret;
+       int ret = -ENOMEM;
 
        thpsize = kzalloc(sizeof(*thpsize), GFP_KERNEL);
        if (!thpsize)
-               return ERR_PTR(-ENOMEM);
+               goto err;
+
+       thpsize->order = order;
 
        ret = kobject_init_and_add(&thpsize->kobj, &thpsize_ktype, parent,
                                   "hugepages-%lukB", size);
        if (ret) {
                kfree(thpsize);
-               return ERR_PTR(ret);
+               goto err;
        }
 
-       ret = sysfs_create_group(&thpsize->kobj, &thpsize_attr_group);
-       if (ret) {
-               kobject_put(&thpsize->kobj);
-               return ERR_PTR(ret);
+
+       ret = sysfs_add_group(&thpsize->kobj, &any_ctrl_attr_grp);
+       if (ret)
+               goto err_put;
+
+       ret = sysfs_add_group(&thpsize->kobj, &any_stats_attr_grp);
+       if (ret)
+               goto err_put;
+
+       if (BIT(order) & THP_ORDERS_ALL_ANON) {
+               ret = sysfs_add_group(&thpsize->kobj, &anon_ctrl_attr_grp);
+               if (ret)
+                       goto err_put;
+
+               ret = sysfs_add_group(&thpsize->kobj, &anon_stats_attr_grp);
+               if (ret)
+                       goto err_put;
        }
 
-       ret = sysfs_create_group(&thpsize->kobj, &stats_attr_group);
-       if (ret) {
-               kobject_put(&thpsize->kobj);
-               return ERR_PTR(ret);
+       if (BIT(order) & THP_ORDERS_ALL_FILE_DEFAULT) {
+               ret = sysfs_add_group(&thpsize->kobj, &file_ctrl_attr_grp);
+               if (ret)
+                       goto err_put;
+
+               ret = sysfs_add_group(&thpsize->kobj, &file_stats_attr_grp);
+               if (ret)
+                       goto err_put;
        }
 
-       thpsize->order = order;
        return thpsize;
+err_put:
+       kobject_put(&thpsize->kobj);
+err:
+       return ERR_PTR(ret);
 }
 
 static void thpsize_release(struct kobject *kobj)
@@ -692,7 +776,7 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
                goto remove_hp_group;
        }
 
-       orders = THP_ORDERS_ALL_ANON;
+       orders = THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT;
        order = highest_order(orders);
        while (orders) {
                thpsize = thpsize_create(order, *hugepage_kobj);
--
Quoted 2 lines

2.43.0

Thanks
Barry

reviewer
#5 lore raw

Re: [PATCH v3 2/2] mm: Tidy up shmem mTHP controls and stats

In reply to Ryan Roberts
Quoted 89 lines · Ryan Roberts

Previously we had a situation where shmem mTHP controls and stats were not exposed for some supported sizes and were exposed for some unsupported sizes. So let's clean that up.

Anon mTHP can support all large orders [2, PMD_ORDER]. But shmem can support all large orders [1, MAX_PAGECACHE_ORDER]. However, per-size shmem controls and stats were previously being exposed for all the anon mTHP orders, meaning order-1 was not present, and for arm64 64K base pages, orders 12 and 13 were exposed but were not supported internally.

Tidy this all up by defining ctrl and stats attribute groups for anon and file separately. Anon ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_ANON and file ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_FILE_DEFAULT.

Additionally, create "any" ctrl and stats attribute groups which are populated for all orders in (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT). swpout stats use this since they apply to anon and shmem.

The side-effect of all this is that different hugepage-*kB directories contain different sets of controls and stats, depending on which memory types support that size. This approach is preferred over the alternative, which is to populate dummy controls and stats for memory types that do not support a given size.

---
  mm/huge_memory.c | 144 +++++++++++++++++++++++++++++++++++++----------
  1 file changed, 114 insertions(+), 30 deletions(-)
mm/huge_memory.c +18 -11
index 0c3075ee00012..082d86b7c6c2f 100644
@@ -482,8 +482,8 @@ static void thpsize_release(struct kobject *kobj);
  static DEFINE_SPINLOCK(huge_anon_orders_lock);
  static LIST_HEAD(thpsize_list);
 
-static ssize_t thpsize_enabled_show(struct kobject *kobj,
-				    struct kobj_attribute *attr, char *buf)
+static ssize_t anon_enabled_show(struct kobject *kobj,
+				 struct kobj_attribute *attr, char *buf)
  {
  	int order = to_thpsize(kobj)->order;
  	const char *output;
@@ -500,9 +500,9 @@ static ssize_t thpsize_enabled_show(struct kobject *kobj,
  	return sysfs_emit(buf, "%s\n", output);
  }
 
-static ssize_t thpsize_enabled_store(struct kobject *kobj,
-				     struct kobj_attribute *attr,
-				     const char *buf, size_t count)
+static ssize_t anon_enabled_store(struct kobject *kobj,
+				  struct kobj_attribute *attr,
+				  const char *buf, size_t count)
  {
  	int order = to_thpsize(kobj)->order;
  	ssize_t ret = count;
@@ -544,19 +544,35 @@ static ssize_t thpsize_enabled_store(struct kobject *kobj,
  	return ret;
  }
 
-static struct kobj_attribute thpsize_enabled_attr =
-	__ATTR(enabled, 0644, thpsize_enabled_show, thpsize_enabled_store);
+static struct kobj_attribute anon_enabled_attr =
+	__ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store);
 
-static struct attribute *thpsize_attrs[] = {
-	&thpsize_enabled_attr.attr,
+static struct attribute *anon_ctrl_attrs[] = {
+	&anon_enabled_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group anon_ctrl_attr_grp = {
+	.attrs = anon_ctrl_attrs,
+};
+
+static struct attribute *file_ctrl_attrs[] = {
  #ifdef CONFIG_SHMEM
  	&thpsize_shmem_enabled_attr.attr,
  #endif
  	NULL,
  };
 
-static const struct attribute_group thpsize_attr_group = {
-	.attrs = thpsize_attrs,
+static const struct attribute_group file_ctrl_attr_grp = {

mm/huge_memory.c static ssize_t thpsize_enabled_store(struct kobject *kobj, · line 576

Quoted 10 lines
mm/huge_memory.c +9 -0
+	.attrs = file_ctrl_attrs,
+};
+
+static struct attribute *any_ctrl_attrs[] = {
+	NULL,
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+	.attrs = any_ctrl_attrs,
  };

I wonder why adding a NULL group?

Quoted 48 lines
mm/huge_memory.c +19 -5
  static const struct kobj_type thpsize_ktype = {
@@ -595,64 +611,132 @@ DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, MTHP_STAT_ANON_FAULT_FALLBACK);
  DEFINE_MTHP_STAT_ATTR(anon_fault_fallback_charge, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
  DEFINE_MTHP_STAT_ATTR(swpout, MTHP_STAT_SWPOUT);
  DEFINE_MTHP_STAT_ATTR(swpout_fallback, MTHP_STAT_SWPOUT_FALLBACK);
+#ifdef CONFIG_SHMEM
  DEFINE_MTHP_STAT_ATTR(shmem_alloc, MTHP_STAT_SHMEM_ALLOC);
  DEFINE_MTHP_STAT_ATTR(shmem_fallback, MTHP_STAT_SHMEM_FALLBACK);
  DEFINE_MTHP_STAT_ATTR(shmem_fallback_charge, MTHP_STAT_SHMEM_FALLBACK_CHARGE);
+#endif
  DEFINE_MTHP_STAT_ATTR(split, MTHP_STAT_SPLIT);
  DEFINE_MTHP_STAT_ATTR(split_failed, MTHP_STAT_SPLIT_FAILED);
  DEFINE_MTHP_STAT_ATTR(split_deferred, MTHP_STAT_SPLIT_DEFERRED);
 
-static struct attribute *stats_attrs[] = {
+static struct attribute *anon_stats_attrs[] = {
  	&anon_fault_alloc_attr.attr,
  	&anon_fault_fallback_attr.attr,
  	&anon_fault_fallback_charge_attr.attr,
+#ifndef CONFIG_SHMEM
  	&swpout_attr.attr,
  	&swpout_fallback_attr.attr,
-	&shmem_alloc_attr.attr,
-	&shmem_fallback_attr.attr,
-	&shmem_fallback_charge_attr.attr,
+#endif
  	&split_attr.attr,
  	&split_failed_attr.attr,
  	&split_deferred_attr.attr,
  	NULL,
  };
 
-static struct attribute_group stats_attr_group = {
+static struct attribute_group anon_stats_attr_grp = {
+	.name = "stats",
+	.attrs = anon_stats_attrs,
+};
+
+static struct attribute *file_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+	&shmem_alloc_attr.attr,
+	&shmem_fallback_attr.attr,
+	&shmem_fallback_charge_attr.attr,
+#endif
+	NULL,
+};
+

mm/huge_memory.c DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, MTHP_STAT_ANON_FAULT_FALLBACK); · line 660

Quoted 10 lines
mm/huge_memory.c +10 -0
+static struct attribute_group file_stats_attr_grp = {
+	.name = "stats",
+	.attrs = file_stats_attrs,
+};
+
+static struct attribute *any_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+	&swpout_attr.attr,
+	&swpout_fallback_attr.attr,
+#endif

Sorry I did not point it out in early version. I think file pages and shmem can also be split, while 'split_deferred' is only for anonymous page. So I think the any_stats_attrs should be:
static struct attribute *any_stats_attrs[] = {
#ifdef CONFIG_SHMEM
&swpout_attr.attr,
&swpout_fallback_attr.attr,
#endif
&split_attr.attr,
&split_failed_attr.attr,
NULL,
};

author reviewer
#6 lore raw

Re: [PATCH v3 2/2] mm: Tidy up shmem mTHP controls and stats

Hi Baolin,

Thanks for the review - I've been out on Paternity leave so only getting around to replying now...

Quoted 91 lines · Baolin Wang
Quoted 88 lines · Ryan Roberts

Previously we had a situation where shmem mTHP controls and stats were not exposed for some supported sizes and were exposed for some unsupported sizes. So let's clean that up.

Anon mTHP can support all large orders [2, PMD_ORDER]. But shmem can support all large orders [1, MAX_PAGECACHE_ORDER]. However, per-size shmem controls and stats were previously being exposed for all the anon mTHP orders, meaning order-1 was not present, and for arm64 64K base pages, orders 12 and 13 were exposed but were not supported internally.

Tidy this all up by defining ctrl and stats attribute groups for anon and file separately. Anon ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_ANON and file ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_FILE_DEFAULT.

Additionally, create "any" ctrl and stats attribute groups which are populated for all orders in (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT). swpout stats use this since they apply to anon and shmem.

The side-effect of all this is that different hugepage-*kB directories contain different sets of controls and stats, depending on which memory types support that size. This approach is preferred over the alternative, which is to populate dummy controls and stats for memory types that do not support a given size.

---
  mm/huge_memory.c | 144 +++++++++++++++++++++++++++++++++++++----------

  1 file changed, 114 insertions(+), 30 deletions(-)
mm/huge_memory.c +5 -3
index 0c3075ee00012..082d86b7c6c2f 100644
@@ -482,8 +482,8 @@ static void thpsize_release(struct kobject *kobj);
   static DEFINE_SPINLOCK(huge_anon_orders_lock);
   static LIST_HEAD(thpsize_list);
   -static ssize_t thpsize_enabled_show(struct kobject *kobj,
-                    struct kobj_attribute *attr, char *buf)
+static ssize_t anon_enabled_show(struct kobject *kobj,
+                 struct kobj_attribute *attr, char *buf)
   {
       int order = to_thpsize(kobj)->order;
       const char *output;
@@ -500,9 +500,9 @@ static ssize_t thpsize_enabled_show(struct kobject *kobj,
       return sysfs_emit(buf, "%s\n", output);
   }
   -static ssize_t thpsize_enabled_store(struct kobject *kobj,
-                     struct kobj_attribute *attr,
-                     const char *buf, size_t count)
+static ssize_t anon_enabled_store(struct kobject *kobj,
+                  struct kobj_attribute *attr,
+                  const char *buf, size_t count)
   {
       int order = to_thpsize(kobj)->order;
       ssize_t ret = count;
@@ -544,19 +544,35 @@ static ssize_t thpsize_enabled_store(struct kobject *kobj,
       return ret;
   }

  -static struct kobj_attribute thpsize_enabled_attr =
-    __ATTR(enabled, 0644, thpsize_enabled_show, thpsize_enabled_store); +static struct kobj_attribute anon_enabled_attr =
+    __ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store); -static struct attribute *thpsize_attrs[] = {
-    &thpsize_enabled_attr.attr,

mm/huge_memory.c +10 -0
+static struct attribute *anon_ctrl_attrs[] = {
+    &anon_enabled_attr.attr,
+    NULL,
+};
+
+static const struct attribute_group anon_ctrl_attr_grp = {
+    .attrs = anon_ctrl_attrs,
+};
+
+static struct attribute *file_ctrl_attrs[] = {

  #ifdef CONFIG_SHMEM &thpsize_shmem_enabled_attr.attr,
  #endif
      NULL,
  };
  -static const struct attribute_group thpsize_attr_group = { -    .attrs = thpsize_attrs,

mm/huge_memory.c +5 -0
+static const struct attribute_group file_ctrl_attr_grp = {
+    .attrs = file_ctrl_attrs,
+};
+
+static struct attribute *any_ctrl_attrs[] = {
Quoted 8 lines
Quoted 6 lines

+    NULL,

mm/huge_memory.c +4 -0
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+    .attrs = any_ctrl_attrs,

  };

I wonder why adding a NULL group?

It made everything a bit more generic and therefore extensible. Its my preference to leave it as is, but will remove it if you insist.

Quoted 63 lines
Quoted 48 lines

    static const struct kobj_type thpsize_ktype = {

mm/huge_memory.c +19 -3
@@ -595,64 +611,132 @@ DEFINE_MTHP_STAT_ATTR(anon_fault_fallback,
 MTHP_STAT_ANON_FAULT_FALLBACK);
   DEFINE_MTHP_STAT_ATTR(anon_fault_fallback_charge,
 MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
   DEFINE_MTHP_STAT_ATTR(swpout, MTHP_STAT_SWPOUT);
   DEFINE_MTHP_STAT_ATTR(swpout_fallback, MTHP_STAT_SWPOUT_FALLBACK);
+#ifdef CONFIG_SHMEM
   DEFINE_MTHP_STAT_ATTR(shmem_alloc, MTHP_STAT_SHMEM_ALLOC);
   DEFINE_MTHP_STAT_ATTR(shmem_fallback, MTHP_STAT_SHMEM_FALLBACK);
   DEFINE_MTHP_STAT_ATTR(shmem_fallback_charge, MTHP_STAT_SHMEM_FALLBACK_CHARGE);
+#endif
   DEFINE_MTHP_STAT_ATTR(split, MTHP_STAT_SPLIT);
   DEFINE_MTHP_STAT_ATTR(split_failed, MTHP_STAT_SPLIT_FAILED);
   DEFINE_MTHP_STAT_ATTR(split_deferred, MTHP_STAT_SPLIT_DEFERRED);
   -static struct attribute *stats_attrs[] = {
+static struct attribute *anon_stats_attrs[] = {
       &anon_fault_alloc_attr.attr,
       &anon_fault_fallback_attr.attr,
       &anon_fault_fallback_charge_attr.attr,
+#ifndef CONFIG_SHMEM
       &swpout_attr.attr,
       &swpout_fallback_attr.attr,
-    &shmem_alloc_attr.attr,
-    &shmem_fallback_attr.attr,
-    &shmem_fallback_charge_attr.attr,
+#endif
       &split_attr.attr,
       &split_failed_attr.attr,
       &split_deferred_attr.attr,
       NULL,
   };
   -static struct attribute_group stats_attr_group = {
+static struct attribute_group anon_stats_attr_grp = {
+    .name = "stats",
+    .attrs = anon_stats_attrs,
+};
+
+static struct attribute *file_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+    &shmem_alloc_attr.attr,
+    &shmem_fallback_attr.attr,
+    &shmem_fallback_charge_attr.attr,
+#endif
+    NULL,
+};
+

mm/huge_memory.c DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, · line 662

Quoted 10 lines
mm/huge_memory.c +10 -0
+static struct attribute_group file_stats_attr_grp = {
+    .name = "stats",
+    .attrs = file_stats_attrs,
+};
+
+static struct attribute *any_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+    &swpout_attr.attr,
+    &swpout_fallback_attr.attr,
+#endif

Sorry I did not point it out in early version. I think file pages and shmem can also be split, while 'split_deferred' is only for anonymous page. So I think the any_stats_attrs should be:
static struct attribute *any_stats_attrs[] = {

Quoted 8 lines

#ifdef CONFIG_SHMEM
    &swpout_attr.attr,
    &swpout_fallback_attr.attr,
#endif
    &split_attr.attr,
    &split_failed_attr.attr,
    NULL,
};

Ahh yes, good point. I'll fix this in the next version (I'm back in office on Wednesday so will send it then).

Thanks,
Ryan

reviewer
#7 lore raw

Re: [PATCH v3 2/2] mm: Tidy up shmem mTHP controls and stats

Quoted 4 lines · Ryan Roberts

Hi Baolin,

Thanks for the review - I've been out on Paternity leave so only getting around to replying now...

No worries :)

Quoted 101 lines · Baolin Wang
Quoted 91 lines · Ryan Roberts
Quoted 88 lines

Previously we had a situation where shmem mTHP controls and stats were not exposed for some supported sizes and were exposed for some unsupported sizes. So let's clean that up.

Anon mTHP can support all large orders [2, PMD_ORDER]. But shmem can support all large orders [1, MAX_PAGECACHE_ORDER]. However, per-size shmem controls and stats were previously being exposed for all the anon mTHP orders, meaning order-1 was not present, and for arm64 64K base pages, orders 12 and 13 were exposed but were not supported internally.

Tidy this all up by defining ctrl and stats attribute groups for anon and file separately. Anon ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_ANON and file ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_FILE_DEFAULT.

Additionally, create "any" ctrl and stats attribute groups which are populated for all orders in (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT). swpout stats use this since they apply to anon and shmem.

The side-effect of all this is that different hugepage-*kB directories contain different sets of controls and stats, depending on which memory types support that size. This approach is preferred over the alternative, which is to populate dummy controls and stats for memory types that do not support a given size.

---
   mm/huge_memory.c | 144 +++++++++++++++++++++++++++++++++++++----------
   1 file changed, 114 insertions(+), 30 deletions(-)
mm/huge_memory.c +22 -6
index 0c3075ee00012..082d86b7c6c2f 100644
@@ -482,8 +482,8 @@ static void thpsize_release(struct kobject *kobj);
   static DEFINE_SPINLOCK(huge_anon_orders_lock);
   static LIST_HEAD(thpsize_list);
   -static ssize_t thpsize_enabled_show(struct kobject *kobj,
-                    struct kobj_attribute *attr, char *buf)
+static ssize_t anon_enabled_show(struct kobject *kobj,
+                 struct kobj_attribute *attr, char *buf)
   {
       int order = to_thpsize(kobj)->order;
       const char *output;
@@ -500,9 +500,9 @@ static ssize_t thpsize_enabled_show(struct kobject *kobj,
       return sysfs_emit(buf, "%s\n", output);
   }
   -static ssize_t thpsize_enabled_store(struct kobject *kobj,
-                     struct kobj_attribute *attr,
-                     const char *buf, size_t count)
+static ssize_t anon_enabled_store(struct kobject *kobj,
+                  struct kobj_attribute *attr,
+                  const char *buf, size_t count)
   {
       int order = to_thpsize(kobj)->order;
       ssize_t ret = count;
@@ -544,19 +544,35 @@ static ssize_t thpsize_enabled_store(struct kobject *kobj,
       return ret;
   }
   -static struct kobj_attribute thpsize_enabled_attr =
-    __ATTR(enabled, 0644, thpsize_enabled_show, thpsize_enabled_store);
+static struct kobj_attribute anon_enabled_attr =
+    __ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store);
   -static struct attribute *thpsize_attrs[] = {
-    &thpsize_enabled_attr.attr,
+static struct attribute *anon_ctrl_attrs[] = {
+    &anon_enabled_attr.attr,
+    NULL,
+};
+
+static const struct attribute_group anon_ctrl_attr_grp = {
+    .attrs = anon_ctrl_attrs,
+};
+
+static struct attribute *file_ctrl_attrs[] = {
   #ifdef CONFIG_SHMEM
       &thpsize_shmem_enabled_attr.attr,
   #endif
       NULL,
   };
   -static const struct attribute_group thpsize_attr_group = {
-    .attrs = thpsize_attrs,
+static const struct attribute_group file_ctrl_attr_grp = {
+    .attrs = file_ctrl_attrs,
+};
+
+static struct attribute *any_ctrl_attrs[] = {
Quoted 8 lines

mm/huge_memory.c static ssize_t thpsize_enabled_store(struct kobject *kobj, · line 576

Quoted 6 lines
mm/huge_memory.c +5 -0
+    NULL,
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+    .attrs = any_ctrl_attrs,
   };

I wonder why adding a NULL group?

Quoted 2 lines

It made everything a bit more generic and therefore extensible. Its my preference to leave it as is, but will remove it if you insist.

My preference is we should add it when necessary, but but I don't have a strong opinion. Let's see what other guys prefer, David, Barry?

reviewer
#8 lore raw

Re: [PATCH v3 2/2] mm: Tidy up shmem mTHP controls and stats

Quoted 112 lines · Baolin Wang
Quoted 4 lines · Ryan Roberts

Hi Baolin,

Thanks for the review - I've been out on Paternity leave so only getting around to replying now...

No worries :)

Quoted 101 lines · Baolin Wang
Quoted 99 lines · Ryan Roberts
Quoted 84 lines

Previously we had a situation where shmem mTHP controls and stats were not exposed for some supported sizes and were exposed for some unsupported sizes. So let's clean that up.

Anon mTHP can support all large orders [2, PMD_ORDER]. But shmem can support all large orders [1, MAX_PAGECACHE_ORDER]. However, per-size shmem controls and stats were previously being exposed for all the anon mTHP orders, meaning order-1 was not present, and for arm64 64K base pages, orders 12 and 13 were exposed but were not supported internally.

Tidy this all up by defining ctrl and stats attribute groups for anon and file separately. Anon ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_ANON and file ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_FILE_DEFAULT.

Additionally, create "any" ctrl and stats attribute groups which are populated for all orders in (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT). swpout stats use this since they apply to anon and shmem.

The side-effect of all this is that different hugepage-*kB directories contain different sets of controls and stats, depending on which memory types support that size. This approach is preferred over the alternative, which is to populate dummy controls and stats for memory types that do not support a given size.

---
   mm/huge_memory.c | 144 +++++++++++++++++++++++++++++++++++++----------
   1 file changed, 114 insertions(+), 30 deletions(-)
mm/huge_memory.c +18 -6
index 0c3075ee00012..082d86b7c6c2f 100644
@@ -482,8 +482,8 @@ static void thpsize_release(struct kobject *kobj);
   static DEFINE_SPINLOCK(huge_anon_orders_lock);
   static LIST_HEAD(thpsize_list);
   -static ssize_t thpsize_enabled_show(struct kobject *kobj,
-                    struct kobj_attribute *attr, char *buf)
+static ssize_t anon_enabled_show(struct kobject *kobj,
+                 struct kobj_attribute *attr, char *buf)
   {
       int order = to_thpsize(kobj)->order;
       const char *output;
@@ -500,9 +500,9 @@ static ssize_t thpsize_enabled_show(struct kobject *kobj,
       return sysfs_emit(buf, "%s\n", output);
   }
   -static ssize_t thpsize_enabled_store(struct kobject *kobj,
-                     struct kobj_attribute *attr,
-                     const char *buf, size_t count)
+static ssize_t anon_enabled_store(struct kobject *kobj,
+                  struct kobj_attribute *attr,
+                  const char *buf, size_t count)
   {
       int order = to_thpsize(kobj)->order;
       ssize_t ret = count;
@@ -544,19 +544,35 @@ static ssize_t thpsize_enabled_store(struct kobject *kobj,
       return ret;
   }
   -static struct kobj_attribute thpsize_enabled_attr =
-    __ATTR(enabled, 0644, thpsize_enabled_show, thpsize_enabled_store);
+static struct kobj_attribute anon_enabled_attr =
+    __ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store);
   -static struct attribute *thpsize_attrs[] = {
-    &thpsize_enabled_attr.attr,
+static struct attribute *anon_ctrl_attrs[] = {
+    &anon_enabled_attr.attr,
+    NULL,
+};
+
+static const struct attribute_group anon_ctrl_attr_grp = {
+    .attrs = anon_ctrl_attrs,
+};
+
+static struct attribute *file_ctrl_attrs[] = {
   #ifdef CONFIG_SHMEM
       &thpsize_shmem_enabled_attr.attr,
   #endif
       NULL,
   };
   -static const struct attribute_group thpsize_attr_group = {
-    .attrs = thpsize_attrs,
+static const struct attribute_group file_ctrl_attr_grp = {

mm/huge_memory.c static ssize_t thpsize_enabled_store(struct kobject *kobj, · line 576

Quoted 10 lines
mm/huge_memory.c +9 -0
+    .attrs = file_ctrl_attrs,
+};
+
+static struct attribute *any_ctrl_attrs[] = {
+    NULL,
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+    .attrs = any_ctrl_attrs,
   };

I wonder why adding a NULL group?

Quoted 5 lines
Quoted 2 lines

It made everything a bit more generic and therefore extensible. Its my preference to leave it as is, but will remove it if you insist.

My preference is we should add it when necessary, but but I don't have a strong opinion. Let's see what other guys prefer, David, Barry?

I'm fine with either option. Adding a NULL control group makes it easier for lazy
people like me to understand the current status, as it clearly indicates that there
isn't a shared control group for file, shmem, and anon at the moment. :-)

Thanks
Barry

author reviewer
#9 lore raw

Re: [PATCH v3 2/2] mm: Tidy up shmem mTHP controls and stats

Quoted 117 lines · Barry Song
Quoted 115 lines · Baolin Wang
Quoted 4 lines · Ryan Roberts

Hi Baolin,

Thanks for the review - I've been out on Paternity leave so only getting around to replying now...

No worries :)

Quoted 103 lines · Baolin Wang
Quoted 91 lines · Ryan Roberts
Quoted 88 lines

Previously we had a situation where shmem mTHP controls and stats were not exposed for some supported sizes and were exposed for some unsupported sizes. So let's clean that up.

Anon mTHP can support all large orders [2, PMD_ORDER]. But shmem can support all large orders [1, MAX_PAGECACHE_ORDER]. However, per-size shmem controls and stats were previously being exposed for all the anon mTHP orders, meaning order-1 was not present, and for arm64 64K base pages, orders 12 and 13 were exposed but were not supported internally.

Tidy this all up by defining ctrl and stats attribute groups for anon and file separately. Anon ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_ANON and file ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_FILE_DEFAULT.

Additionally, create "any" ctrl and stats attribute groups which are populated for all orders in (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT). swpout stats use this since they apply to anon and shmem.

The side-effect of all this is that different hugepage-*kB directories contain different sets of controls and stats, depending on which memory types support that size. This approach is preferred over the alternative, which is to populate dummy controls and stats for memory types that do not support a given size.

---
   mm/huge_memory.c | 144 +++++++++++++++++++++++++++++++++++++----------
   1 file changed, 114 insertions(+), 30 deletions(-)
mm/huge_memory.c +22 -6
index 0c3075ee00012..082d86b7c6c2f 100644
@@ -482,8 +482,8 @@ static void thpsize_release(struct kobject *kobj);
   static DEFINE_SPINLOCK(huge_anon_orders_lock);
   static LIST_HEAD(thpsize_list);
   -static ssize_t thpsize_enabled_show(struct kobject *kobj,
-                    struct kobj_attribute *attr, char *buf)
+static ssize_t anon_enabled_show(struct kobject *kobj,
+                 struct kobj_attribute *attr, char *buf)
   {
       int order = to_thpsize(kobj)->order;
       const char *output;
@@ -500,9 +500,9 @@ static ssize_t thpsize_enabled_show(struct kobject *kobj,
       return sysfs_emit(buf, "%s\n", output);
   }
   -static ssize_t thpsize_enabled_store(struct kobject *kobj,
-                     struct kobj_attribute *attr,
-                     const char *buf, size_t count)
+static ssize_t anon_enabled_store(struct kobject *kobj,
+                  struct kobj_attribute *attr,
+                  const char *buf, size_t count)
   {
       int order = to_thpsize(kobj)->order;
       ssize_t ret = count;
@@ -544,19 +544,35 @@ static ssize_t thpsize_enabled_store(struct kobject *kobj,
       return ret;
   }
   -static struct kobj_attribute thpsize_enabled_attr =
-    __ATTR(enabled, 0644, thpsize_enabled_show, thpsize_enabled_store);
+static struct kobj_attribute anon_enabled_attr =
+    __ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store);
   -static struct attribute *thpsize_attrs[] = {
-    &thpsize_enabled_attr.attr,
+static struct attribute *anon_ctrl_attrs[] = {
+    &anon_enabled_attr.attr,
+    NULL,
+};
+
+static const struct attribute_group anon_ctrl_attr_grp = {
+    .attrs = anon_ctrl_attrs,
+};
+
+static struct attribute *file_ctrl_attrs[] = {
   #ifdef CONFIG_SHMEM
       &thpsize_shmem_enabled_attr.attr,
   #endif
       NULL,
   };
   -static const struct attribute_group thpsize_attr_group = {
-    .attrs = thpsize_attrs,
+static const struct attribute_group file_ctrl_attr_grp = {
+    .attrs = file_ctrl_attrs,
+};
+
+static struct attribute *any_ctrl_attrs[] = {
Quoted 8 lines

mm/huge_memory.c static ssize_t thpsize_enabled_store(struct kobject *kobj, · line 576

Quoted 6 lines
mm/huge_memory.c +5 -0
+    NULL,
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+    .attrs = any_ctrl_attrs,
   };

I wonder why adding a NULL group?

It made everything a bit more generic and therefore extensible. Its my preference to leave it as is, but will remove it if you insist.

Quoted 8 lines
Quoted 2 lines

My preference is we should add it when necessary, but but I don't have a strong opinion. Let's see what other guys prefer, David, Barry?

I'm fine with either option. Adding a NULL control group makes it easier for lazy
people like me to understand the current status, as it clearly indicates that there
isn't a shared control group for file, shmem, and anon at the moment. :-)

Thanks for the feedback, I'm going to leave it as is in the next version then.

Quoted 3 lines

Thanks
Barry

author reviewer
#10 lore raw

Re: [PATCH v3 2/2] mm: Tidy up shmem mTHP controls and stats

In reply to Baolin Wang

Hi Andrew,

Quoted 162 lines · Baolin Wang
Quoted 93 lines · Ryan Roberts

Previously we had a situation where shmem mTHP controls and stats were not exposed for some supported sizes and were exposed for some unsupported sizes. So let's clean that up.

Anon mTHP can support all large orders [2, PMD_ORDER]. But shmem can support all large orders [1, MAX_PAGECACHE_ORDER]. However, per-size shmem controls and stats were previously being exposed for all the anon mTHP orders, meaning order-1 was not present, and for arm64 64K base pages, orders 12 and 13 were exposed but were not supported internally.

Tidy this all up by defining ctrl and stats attribute groups for anon and file separately. Anon ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_ANON and file ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_FILE_DEFAULT.

Additionally, create "any" ctrl and stats attribute groups which are populated for all orders in (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT). swpout stats use this since they apply to anon and shmem.

The side-effect of all this is that different hugepage-*kB directories contain different sets of controls and stats, depending on which memory types support that size. This approach is preferred over the alternative, which is to populate dummy controls and stats for memory types that do not support a given size.

---
  mm/huge_memory.c | 144 +++++++++++++++++++++++++++++++++++++----------

  1 file changed, 114 insertions(+), 30 deletions(-)
mm/huge_memory.c +5 -3
index 0c3075ee00012..082d86b7c6c2f 100644
@@ -482,8 +482,8 @@ static void thpsize_release(struct kobject *kobj);
   static DEFINE_SPINLOCK(huge_anon_orders_lock);
   static LIST_HEAD(thpsize_list);
   -static ssize_t thpsize_enabled_show(struct kobject *kobj,
-                    struct kobj_attribute *attr, char *buf)
+static ssize_t anon_enabled_show(struct kobject *kobj,
+                 struct kobj_attribute *attr, char *buf)
   {
       int order = to_thpsize(kobj)->order;
       const char *output;
@@ -500,9 +500,9 @@ static ssize_t thpsize_enabled_show(struct kobject *kobj,
       return sysfs_emit(buf, "%s\n", output);
   }
   -static ssize_t thpsize_enabled_store(struct kobject *kobj,
-                     struct kobj_attribute *attr,
-                     const char *buf, size_t count)
+static ssize_t anon_enabled_store(struct kobject *kobj,
+                  struct kobj_attribute *attr,
+                  const char *buf, size_t count)
   {
       int order = to_thpsize(kobj)->order;
       ssize_t ret = count;
@@ -544,19 +544,35 @@ static ssize_t thpsize_enabled_store(struct kobject *kobj,
       return ret;
   }

  -static struct kobj_attribute thpsize_enabled_attr =
-    __ATTR(enabled, 0644, thpsize_enabled_show, thpsize_enabled_store); +static struct kobj_attribute anon_enabled_attr =
+    __ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store); -static struct attribute *thpsize_attrs[] = {
-    &thpsize_enabled_attr.attr,

mm/huge_memory.c +10 -0
+static struct attribute *anon_ctrl_attrs[] = {
+    &anon_enabled_attr.attr,
+    NULL,
+};
+
+static const struct attribute_group anon_ctrl_attr_grp = {
+    .attrs = anon_ctrl_attrs,
+};
+
+static struct attribute *file_ctrl_attrs[] = {

  #ifdef CONFIG_SHMEM &thpsize_shmem_enabled_attr.attr,
  #endif
      NULL,
  };
  -static const struct attribute_group thpsize_attr_group = { -    .attrs = thpsize_attrs,

mm/huge_memory.c +10 -0
+static const struct attribute_group file_ctrl_attr_grp = {
+    .attrs = file_ctrl_attrs,
+};
+
+static struct attribute *any_ctrl_attrs[] = {
+    NULL,
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+    .attrs = any_ctrl_attrs,
Quoted 1 line

  };

I wonder why adding a NULL group?

Quoted 47 lines

    static const struct kobj_type thpsize_ktype = {

mm/huge_memory.c +19 -3
@@ -595,64 +611,132 @@ DEFINE_MTHP_STAT_ATTR(anon_fault_fallback,
 MTHP_STAT_ANON_FAULT_FALLBACK);
   DEFINE_MTHP_STAT_ATTR(anon_fault_fallback_charge,
 MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
   DEFINE_MTHP_STAT_ATTR(swpout, MTHP_STAT_SWPOUT);
   DEFINE_MTHP_STAT_ATTR(swpout_fallback, MTHP_STAT_SWPOUT_FALLBACK);
+#ifdef CONFIG_SHMEM
   DEFINE_MTHP_STAT_ATTR(shmem_alloc, MTHP_STAT_SHMEM_ALLOC);
   DEFINE_MTHP_STAT_ATTR(shmem_fallback, MTHP_STAT_SHMEM_FALLBACK);
   DEFINE_MTHP_STAT_ATTR(shmem_fallback_charge, MTHP_STAT_SHMEM_FALLBACK_CHARGE);
+#endif
   DEFINE_MTHP_STAT_ATTR(split, MTHP_STAT_SPLIT);
   DEFINE_MTHP_STAT_ATTR(split_failed, MTHP_STAT_SPLIT_FAILED);
   DEFINE_MTHP_STAT_ATTR(split_deferred, MTHP_STAT_SPLIT_DEFERRED);
   -static struct attribute *stats_attrs[] = {
+static struct attribute *anon_stats_attrs[] = {
       &anon_fault_alloc_attr.attr,
       &anon_fault_fallback_attr.attr,
       &anon_fault_fallback_charge_attr.attr,
+#ifndef CONFIG_SHMEM
       &swpout_attr.attr,
       &swpout_fallback_attr.attr,
-    &shmem_alloc_attr.attr,
-    &shmem_fallback_attr.attr,
-    &shmem_fallback_charge_attr.attr,
+#endif
       &split_attr.attr,
       &split_failed_attr.attr,
       &split_deferred_attr.attr,
       NULL,
   };
   -static struct attribute_group stats_attr_group = {
+static struct attribute_group anon_stats_attr_grp = {
+    .name = "stats",
+    .attrs = anon_stats_attrs,
+};
+
+static struct attribute *file_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+    &shmem_alloc_attr.attr,
+    &shmem_fallback_attr.attr,
+    &shmem_fallback_charge_attr.attr,
+#endif
+    NULL,
+};
+

mm/huge_memory.c DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, · line 662

Quoted 10 lines
mm/huge_memory.c +10 -0
+static struct attribute_group file_stats_attr_grp = {
+    .name = "stats",
+    .attrs = file_stats_attrs,
+};
+
+static struct attribute *any_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+    &swpout_attr.attr,
+    &swpout_fallback_attr.attr,
+#endif

Sorry I did not point it out in early version. I think file pages and shmem can also be split, while 'split_deferred' is only for anonymous page. So I think the any_stats_attrs should be:
static struct attribute *any_stats_attrs[] = {

Quoted 8 lines

#ifdef CONFIG_SHMEM
    &swpout_attr.attr,
    &swpout_fallback_attr.attr,
#endif
    &split_attr.attr,
    &split_failed_attr.attr,
    NULL,
};

Could you please squash the following into this patch, which is already in mm-unstable? I'm hoping this sufficient and I don't need to send a whole new revision since there are changes on top of this in mm-unstable, which makes things tricky.

----8<-----

mm/huge_memory.c +2 -2
index 382938e46f96f..5905957b1f70d 100644
@@ -631,8 +631,6 @@ static struct attribute *anon_stats_attrs[] = {
        &swpout_attr.attr,
        &swpout_fallback_attr.attr,
 #endif
-       &split_attr.attr,
-       &split_failed_attr.attr,
        &split_deferred_attr.attr,
        &nr_anon_attr.attr,
        &nr_anon_partially_mapped_attr.attr,
@@ -663,6 +661,8 @@ static struct attribute *any_stats_attrs[] = {
        &swpout_attr.attr,
        &swpout_fallback_attr.attr,
 #endif
+       &split_attr.attr,
+       &split_failed_attr.attr,
        NULL,
 };
 

----8<-----

Thanks,
Ryan

maintainer
#11 lore raw

Re: [PATCH v3 2/2] mm: Tidy up shmem mTHP controls and stats

Quoted 9 lines · On Wed, 4 Sep 2024 11:47:29 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 9 lines
Quoted 7 lines
(quoted diff) +6 -0
+static struct attribute *any_ctrl_attrs[] = {
+    NULL,
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+    .attrs = any_ctrl_attrs,

  };

I wonder why adding a NULL group?

Was this review comment addressed?

Quoted 34 lines

...

Quoted 22 lines
Quoted 7 lines

+    &shmem_alloc_attr.attr,
+    &shmem_fallback_attr.attr,
+    &shmem_fallback_charge_attr.attr,

(quoted diff) +4 -0
+#endif
+    NULL,
+};
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+static struct attribute_group file_stats_attr_grp = {
+    .name = "stats",
+    .attrs = file_stats_attrs,
+};
+
+static struct attribute *any_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+    &swpout_attr.attr,
+    &swpout_fallback_attr.attr,
+#endif

Sorry I did not point it out in early version. I think file pages and shmem can also be split, while 'split_deferred' is only for anonymous page. So I think the any_stats_attrs should be:
static struct attribute *any_stats_attrs[] = {

Quoted 8 lines

#ifdef CONFIG_SHMEM
    &swpout_attr.attr,
    &swpout_fallback_attr.attr,
#endif
    &split_attr.attr,
    &split_failed_attr.attr,
    NULL,
};

Quoted 4 lines

Could you please squash the following into this patch, which is already in mm-unstable? I'm hoping this sufficient and I don't need to send a whole new revision since there are changes on top of this in mm-unstable, which makes things tricky.

I did that. Please send along a Signoff and a changelog?

author reviewer
#12 lore raw

Re: [PATCH v3 2/2] mm: Tidy up shmem mTHP controls and stats

Quoted 12 lines · Andrew Morton
Quoted 9 lines · On Wed, 4 Sep 2024 11:47:29 +0100 Ryan Roberts <[email protected]> wrote:
Quoted 9 lines
Quoted 7 lines
(quoted diff) +6 -0
+static struct attribute *any_ctrl_attrs[] = {
+    NULL,
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+    .attrs = any_ctrl_attrs,

  };

I wonder why adding a NULL group?

Quoted 1 line

Was this review comment addressed?

Yes, we had discussion at [1] and concluded to leave it as is; Baolin had a preference to remove it (but no strong opinion) and Barry preferred it as is to explicitly indicate that there are no controls shared by file/shmem/anon currently.

[1]
https://lore.kernel.org/linux-mm/CAGsJ_4z+yERPLwzm-8Mkx8MsNZAz0zZWycZfuGRjOc4kxS=HwQ@mail.gmail.com/

Quoted 35 lines
Quoted 35 lines

...

Quoted 30 lines
Quoted 7 lines

+    &shmem_alloc_attr.attr,
+    &shmem_fallback_attr.attr,
+    &shmem_fallback_charge_attr.attr,

(quoted diff) +4 -0
+#endif
+    NULL,
+};
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+static struct attribute_group file_stats_attr_grp = {
+    .name = "stats",
+    .attrs = file_stats_attrs,
+};
+
+static struct attribute *any_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+    &swpout_attr.attr,
+    &swpout_fallback_attr.attr,
+#endif

Sorry I did not point it out in early version. I think file pages and shmem can also be split, while 'split_deferred' is only for anonymous page. So I think the any_stats_attrs should be:
static struct attribute *any_stats_attrs[] = {
#ifdef CONFIG_SHMEM
    &swpout_attr.attr,
    &swpout_fallback_attr.attr,
#endif
    &split_attr.attr,
    &split_failed_attr.attr,
    NULL,
};

Quoted 6 lines
Quoted 4 lines

Could you please squash the following into this patch, which is already in mm-unstable? I'm hoping this sufficient and I don't need to send a whole new revision since there are changes on top of this in mm-unstable, which makes things tricky.

I did that. Please send along a Signoff and a changelog?

Ahh sorry, so even though you are squashing into an existing patch which has the change log and sign-off, the process still requires a separate one here? My bad.

----8<----

Fixup commit ("mm: Tidy up shmem mTHP controls and stats") by moving split_attr and split_failed_attr stats to the any_stats_attrs group. All memory types can have their folios split, so it was incorrect to only show this stat for anon memory.

----8<----

Thanks,
Ryan

reviewer Reviewed-by
#13 lore raw

Re: [PATCH v3 2/2] mm: Tidy up shmem mTHP controls and stats

In reply to Ryan Roberts
Quoted 196 lines · Ryan Roberts

Hi Andrew,

Quoted 162 lines · Baolin Wang
Quoted 84 lines · Ryan Roberts

Previously we had a situation where shmem mTHP controls and stats were not exposed for some supported sizes and were exposed for some unsupported sizes. So let's clean that up.

Anon mTHP can support all large orders [2, PMD_ORDER]. But shmem can support all large orders [1, MAX_PAGECACHE_ORDER]. However, per-size shmem controls and stats were previously being exposed for all the anon mTHP orders, meaning order-1 was not present, and for arm64 64K base pages, orders 12 and 13 were exposed but were not supported internally.

Tidy this all up by defining ctrl and stats attribute groups for anon and file separately. Anon ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_ANON and file ctrl and stats groups are populated for all orders in THP_ORDERS_ALL_FILE_DEFAULT.

Additionally, create "any" ctrl and stats attribute groups which are populated for all orders in (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE_DEFAULT). swpout stats use this since they apply to anon and shmem.

The side-effect of all this is that different hugepage-*kB directories contain different sets of controls and stats, depending on which memory types support that size. This approach is preferred over the alternative, which is to populate dummy controls and stats for memory types that do not support a given size.

---
   mm/huge_memory.c | 144 +++++++++++++++++++++++++++++++++++++----------
   1 file changed, 114 insertions(+), 30 deletions(-)
mm/huge_memory.c +18 -6
index 0c3075ee00012..082d86b7c6c2f 100644
@@ -482,8 +482,8 @@ static void thpsize_release(struct kobject *kobj);
   static DEFINE_SPINLOCK(huge_anon_orders_lock);
   static LIST_HEAD(thpsize_list);
   -static ssize_t thpsize_enabled_show(struct kobject *kobj,
-                    struct kobj_attribute *attr, char *buf)
+static ssize_t anon_enabled_show(struct kobject *kobj,
+                 struct kobj_attribute *attr, char *buf)
   {
       int order = to_thpsize(kobj)->order;
       const char *output;
@@ -500,9 +500,9 @@ static ssize_t thpsize_enabled_show(struct kobject *kobj,
       return sysfs_emit(buf, "%s\n", output);
   }
   -static ssize_t thpsize_enabled_store(struct kobject *kobj,
-                     struct kobj_attribute *attr,
-                     const char *buf, size_t count)
+static ssize_t anon_enabled_store(struct kobject *kobj,
+                  struct kobj_attribute *attr,
+                  const char *buf, size_t count)
   {
       int order = to_thpsize(kobj)->order;
       ssize_t ret = count;
@@ -544,19 +544,35 @@ static ssize_t thpsize_enabled_store(struct kobject *kobj,
       return ret;
   }
   -static struct kobj_attribute thpsize_enabled_attr =
-    __ATTR(enabled, 0644, thpsize_enabled_show, thpsize_enabled_store);
+static struct kobj_attribute anon_enabled_attr =
+    __ATTR(enabled, 0644, anon_enabled_show, anon_enabled_store);
   -static struct attribute *thpsize_attrs[] = {
-    &thpsize_enabled_attr.attr,
+static struct attribute *anon_ctrl_attrs[] = {
+    &anon_enabled_attr.attr,
+    NULL,
+};
+
+static const struct attribute_group anon_ctrl_attr_grp = {
+    .attrs = anon_ctrl_attrs,
+};
+
+static struct attribute *file_ctrl_attrs[] = {
   #ifdef CONFIG_SHMEM
       &thpsize_shmem_enabled_attr.attr,
   #endif
       NULL,
   };
   -static const struct attribute_group thpsize_attr_group = {
-    .attrs = thpsize_attrs,
+static const struct attribute_group file_ctrl_attr_grp = {

mm/huge_memory.c static ssize_t thpsize_enabled_store(struct kobject *kobj, · line 576

Quoted 10 lines
mm/huge_memory.c +9 -0
+    .attrs = file_ctrl_attrs,
+};
+
+static struct attribute *any_ctrl_attrs[] = {
+    NULL,
+};
+
+static const struct attribute_group any_ctrl_attr_grp = {
+    .attrs = any_ctrl_attrs,
   };

I wonder why adding a NULL group?

Quoted 47 lines
mm/huge_memory.c +19 -3
     static const struct kobj_type thpsize_ktype = {
@@ -595,64 +611,132 @@ DEFINE_MTHP_STAT_ATTR(anon_fault_fallback,
 MTHP_STAT_ANON_FAULT_FALLBACK);
   DEFINE_MTHP_STAT_ATTR(anon_fault_fallback_charge,
 MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
   DEFINE_MTHP_STAT_ATTR(swpout, MTHP_STAT_SWPOUT);
   DEFINE_MTHP_STAT_ATTR(swpout_fallback, MTHP_STAT_SWPOUT_FALLBACK);
+#ifdef CONFIG_SHMEM
   DEFINE_MTHP_STAT_ATTR(shmem_alloc, MTHP_STAT_SHMEM_ALLOC);
   DEFINE_MTHP_STAT_ATTR(shmem_fallback, MTHP_STAT_SHMEM_FALLBACK);
   DEFINE_MTHP_STAT_ATTR(shmem_fallback_charge, MTHP_STAT_SHMEM_FALLBACK_CHARGE);
+#endif
   DEFINE_MTHP_STAT_ATTR(split, MTHP_STAT_SPLIT);
   DEFINE_MTHP_STAT_ATTR(split_failed, MTHP_STAT_SPLIT_FAILED);
   DEFINE_MTHP_STAT_ATTR(split_deferred, MTHP_STAT_SPLIT_DEFERRED);
   -static struct attribute *stats_attrs[] = {
+static struct attribute *anon_stats_attrs[] = {
       &anon_fault_alloc_attr.attr,
       &anon_fault_fallback_attr.attr,
       &anon_fault_fallback_charge_attr.attr,
+#ifndef CONFIG_SHMEM
       &swpout_attr.attr,
       &swpout_fallback_attr.attr,
-    &shmem_alloc_attr.attr,
-    &shmem_fallback_attr.attr,
-    &shmem_fallback_charge_attr.attr,
+#endif
       &split_attr.attr,
       &split_failed_attr.attr,
       &split_deferred_attr.attr,
       NULL,
   };
   -static struct attribute_group stats_attr_group = {
+static struct attribute_group anon_stats_attr_grp = {
+    .name = "stats",
+    .attrs = anon_stats_attrs,
+};
+
+static struct attribute *file_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+    &shmem_alloc_attr.attr,
+    &shmem_fallback_attr.attr,
+    &shmem_fallback_charge_attr.attr,
+#endif
+    NULL,
+};
+

mm/huge_memory.c DEFINE_MTHP_STAT_ATTR(anon_fault_fallback, · line 662

Quoted 10 lines
mm/huge_memory.c +10 -0
+static struct attribute_group file_stats_attr_grp = {
+    .name = "stats",
+    .attrs = file_stats_attrs,
+};
+
+static struct attribute *any_stats_attrs[] = {
+#ifdef CONFIG_SHMEM
+    &swpout_attr.attr,
+    &swpout_fallback_attr.attr,
+#endif

Sorry I did not point it out in early version. I think file pages and shmem can also be split, while 'split_deferred' is only for anonymous page. So I think the any_stats_attrs should be:
static struct attribute *any_stats_attrs[] = {

Quoted 8 lines

#ifdef CONFIG_SHMEM
    &swpout_attr.attr,
    &swpout_fallback_attr.attr,
#endif
    &split_attr.attr,
    &split_failed_attr.attr,
    NULL,
};

Could you please squash the following into this patch, which is already in mm-unstable? I'm hoping this sufficient and I don't need to send a whole new revision since there are changes on top of this in mm-unstable, which makes things tricky.

----8<-----

mm/huge_memory.c +0 -2
index 382938e46f96f..5905957b1f70d 100644
@@ -631,8 +631,6 @@ static struct attribute *anon_stats_attrs[] = {
         &swpout_attr.attr,
         &swpout_fallback_attr.attr,
  #endif
-       &split_attr.attr,
-       &split_failed_attr.attr,
         &split_deferred_attr.attr,
         &nr_anon_attr.attr,
         &nr_anon_partially_mapped_attr.attr,

mm/huge_memory.c static struct attribute *any_stats_attrs[] = { · line 667

Quoted 7 lines
mm/huge_memory.c +2 -0
@@ -663,6 +661,8 @@ static struct attribute *any_stats_attrs[] = {
         &swpout_attr.attr,
         &swpout_fallback_attr.attr,
  #endif
+       &split_attr.attr,
+       &split_failed_attr.attr,
         NULL,
  };

The change looks good to me. Feel free to add