Skip to contentSkip to conversation

Control folio sizes used for page cache memory

RFC PATCH In review Ryan Roberts 7 participants 26 messages Jul 17, 2024 – Dec 18, 2025

Subsystems (5)
lore
Share & tools
lore

Ryan Roberts

author reviewer
#0 lore raw

cover Control folio sizes used for page cache memory

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

I'm trying to solve 2 separate problems with this series:

1. Reduce pressure in iTLB and improve performance on arm64: This is a modified approach for the change at [1]. Instead of hardcoding the preferred executable folio size into the arch, user space can now select it. This decouples the arch code and also makes the mechanism more generic; it can be bypassed (the default) or any folio size can be set. For my use case, 64K is preferred, but I've also heard from Willy of a use case where putting all text into 2M PMD-sized folios is preferred. This approach avoids the need for synchonous MADV_COLLAPSE (and therefore faulting in all text ahead of time) to achieve that.

2. Reduce memory fragmentation in systems under high memory pressure (e.g. Android): The theory goes that if all folios are 64K, then failure to allocate a 64K folio should become unlikely. But if the page cache is allocating lots of different orders, with most allocations having an order below 64K (as is the case today) then ability to allocate 64K folios diminishes. By providing control over the allowed set of folio sizes, we can tune to avoid crucial 64K folio allocation failure. Additionally I've heard (second hand) of the need to disable large folios in the page cache entirely due to latency concerns in some settings. These controls allow all of this without kernel changes.

The value of (1) is clear and the performance improvements are documented in patch 2. I don't yet have any data demonstrating the theory for (2) since I can't reproduce the setup that Barry had at [2]. But my view is that by adding these controls we will enable the community to explore further, in the same way that the anon mTHP controls helped harden the understanding for anonymous memory.

---
This series depends on the "mTHP allocation stats for file-backed memory" series at [3], which itself applies on top of yesterday's mm-unstable (650b6752c8a3). All mm selftests have been run; no regressions were observed.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://www.youtube.com/watch?v=ht7eGWqwmNs&list=PLbzoR-pLrL6oj1rVTXLnV7cOuetvjKn9q&index=4
[3] https://lore.kernel.org/linux-mm/[email protected]/

Thanks,
Ryan

Ryan Roberts (4):
mm: mTHP user controls to configure pagecache large folio sizes
mm: Introduce "always+exec" for mTHP file_enabled control
mm: Override mTHP "enabled" defaults at kernel cmdline
mm: Override mTHP "file_enabled" defaults at kernel cmdline

 .../admin-guide/kernel-parameters.txt         |  16 ++
 Documentation/admin-guide/mm/transhuge.rst    |  66 +++++++-
 include/linux/huge_mm.h                       |  61 ++++---
 mm/filemap.c                                  |  26 ++-
 mm/huge_memory.c                              | 158 +++++++++++++++++-
 mm/readahead.c                                |  43 ++++-
 6 files changed, 329 insertions(+), 41 deletions(-)

--
2.43.0

author reviewer
#1 lore raw

RFC PATCH 1/4 mm: mTHP user controls to configure pagecache large folio sizes

Add mTHP controls to sysfs to allow user space to configure the folio sizes that can be considered for allocation of file-backed memory:

/sys/kernel/mm/transparent_hugepage/hugepages-*kB/file_enable

For now, the control can be set to either always or never to enable or disable that size. More options may be added in future.

By default, at boot, all folio sizes are enabled, and the algorithm used to select a folio size remains conceptually unchanged; increase by 2 enabled orders each time a readahead marker is hit then reduce to the closest enabled order to fit within bounds of ra size, index alignment and EOF. So when all folio sizes are enabled, behavior should be unchanged. When folio sizes are disabled, the algorithm will never select them.

Systems such as Android are always under extreme memory pressure and as a result fragmentation often causes attempts to allocate large folios to fail and fallback to smaller folios. By fixing the pagecache to one large folio size (e.g. 64K) plus fallback to small folios, a large source of this fragmentation can be removed and 64K mTHP allocations succeed more often, allowing the system to benefit from improved performance on arm64 and other arches that support "contpte".

---
 Documentation/admin-guide/mm/transhuge.rst | 21 +++++++++
 include/linux/huge_mm.h                    | 50 +++++++++++++---------
 mm/filemap.c                               | 15 ++++---
 mm/huge_memory.c                           | 43 +++++++++++++++++++
 mm/readahead.c                             | 43 +++++++++++++++----
 5 files changed, 138 insertions(+), 34 deletions(-)
Documentation/admin-guide/mm/transhuge.rst +21 -0
index d4857e457add..9f3ed504c646 100644
@@ -284,6 +284,27 @@ that THP is shared. Exceeding the number would block the collapse::
 
 A higher value may increase memory footprint for some workloads.
 
+File-Backed Hugepages
+---------------------
+
+The kernel will automatically select an appropriate THP size for file-backed
+memory from a set of allowed sizes. By default all THP sizes that the page cache
+supports are allowed, but this set can be modified with one of::
+
+	echo always >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/file_enabled
+	echo never >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/file_enabled
+
+where <size> is the hugepage size being addressed, the available sizes for which
+vary by system. ``always`` adds the hugepage size to the set of allowed sizes,
+and ``never`` removes the hugepage size from the set of allowed sizes.
+
+In some situations, constraining the allowed sizes can reduce memory
+fragmentation, resulting in fewer allocation fallbacks and improved system
+performance.
+
+Note that any changes to the allowed set of sizes only applies to future
+file-backed THP allocations.
+
 Boot parameter
 ==============
 
include/linux/huge_mm.h +29 -21

80 lines not on this page · raw message

mm/filemap.c +10 -5
index 131d514fca29..870016fcfdde 100644
@@ -1922,6 +1922,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
 no_page:
 	if (!folio && (fgp_flags & FGP_CREAT)) {
 		unsigned order = FGF_GET_ORDER(fgp_flags);
+		unsigned long orders;
 		int err;
 
 		if ((fgp_flags & FGP_WRITE) && mapping_can_writeback(mapping))
@@ -1937,13 +1938,15 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
 
 		if (!mapping_large_folio_support(mapping))
 			order = 0;
-		if (order > MAX_PAGECACHE_ORDER)
-			order = MAX_PAGECACHE_ORDER;
+
+		orders = file_orders_always() | BIT(0);
+		orders &= BIT(order + 1) - 1;
 		/* If we're not aligned, allocate a smaller folio */
 		if (index & ((1UL << order) - 1))
-			order = __ffs(index);
+			orders &= BIT(__ffs(index) + 1) - 1;
+		order = highest_order(orders);
 
-		do {
+		while (orders) {
 			gfp_t alloc_gfp = gfp;
 
 			err = -ENOMEM;
@@ -1962,7 +1965,9 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
 				break;
 			folio_put(folio);
 			folio = NULL;
-		} while (order-- > 0);
+
+			order = next_order(&orders, order);
+		};
 
 		if (err == -EEXIST)
 			goto repeat;
mm/huge_memory.c +43 -0

68 lines not on this page · raw message

mm/readahead.c +35 -8

67 lines not on this page · raw message

Signature · 1 line
2.43.0
author reviewer
#2 lore raw

RFC PATCH 2/4 mm: Introduce "always+exec" for mTHP file_enabled control

In reply to Ryan Roberts

In addition to always and never, add always+exec as an option for:

/sys/kernel/mm/transparent_hugepage/hugepages-*kB/file_enabled

always+exec acts like always but additionally marks the hugepage size as the preferred hugepage size for sections of any file mapped with execute permission. A maximum of one hugepage size can be marked as exec at a time, so applying it to a new size implicitly removes it from any size it was previously set for.

Change readahead to use this flagged exec size; when a request is made for an executable mapping, do a synchronous read of the size in a naturally aligned manner.

On arm64 if memory is physically contiguous and naturally aligned to the "contpte" size, we can use contpte mappings, which improves utilization of the TLB. When paired with the "multi-size THP" changes, this works well to reduce dTLB pressure. However iTLB pressure is still high due to executable mappings having a low liklihood of being in the required folio size and mapping alignment, even when the filesystem supports readahead into large folios (e.g. XFS).

The reason for the low liklihood is that the current readahead algorithm starts with an order-2 folio and increases the folio order by 2 every time the readahead mark is hit. But most executable memory is faulted in fairly randomly and so the readahead mark is rarely hit and most executable folios remain order-2. This is observed impirically and confirmed from discussion with a gnu linker expert; in general, the linker does nothing to group temporally accessed text together spacially. Additionally, with the current read-around approach there are no alignment guarrantees between the file and folio. This is insufficient for arm64's contpte mapping requirement (order-4 for 4K base pages).

So it seems reasonable to special-case the read(ahead) logic for executable mappings. The trade-off is performance improvement (due to more efficient storage of the translations in iTLB) vs potential read amplification (due to reading too much data around the fault which won't be used), and the latter is independent of base page size.

Of course if no hugepage size is marked as always+exec the old behaviour is maintained.

Performance Benchmarking
------------------------

The below shows kernel compilation and speedometer javascript benchmarks on Ampere Altra arm64 system. When the patch is applied, always+exec is set for 64K folios.

First, confirmation that this patch causes more memory to be contained in 64K folios (this is for all file-backed memory so includes non-executable too):

| File-backed folios      |   Speedometer   |  Kernel Compile |
| by size as percentage   |-----------------|-----------------|
| of all mapped file mem  | before |  after | before |  after |
|=========================|========|========|========|========|
|file-thp-aligned-16kB    |    45% |     9% |    46% |     7% |
|file-thp-aligned-32kB    |     2% |     0% |     3% |     1% |
|file-thp-aligned-64kB    |     3% |    63% |     5% |    80% |
|file-thp-aligned-128kB   |    11% |    11% |     0% |     0% |
|file-thp-unaligned-16kB  |     1% |     0% |     3% |     1% |
|file-thp-unaligned-128kB |     1% |     0% |     0% |     0% |
|file-thp-partial         |     0% |     0% |     0% |     0% |
|-------------------------|--------|--------|--------|--------|
|file-cont-aligned-64kB   |    16% |    75% |     5% |    80% |

The above shows that for both use cases, the amount of file memory backed by 16K folios reduces and the amount backed by 64K folios increases significantly. And the amount of memory that is contpte-mapped significantly increases (last line).

And this is reflected in performance improvement:

Kernel Compilation (smaller is faster):

| kernel   |   real-time |   kern-time |   user-time |   peak memory |
|----------|-------------|-------------|-------------|---------------|
| before   |        0.0% |        0.0% |        0.0% |          0.0% |
| after    |       -1.6% |       -2.1% |       -1.7% |          0.0% |

Speedometer (bigger is faster):

| kernel   |   runs_per_min |   peak memory |
|----------|----------------|---------------|
| before   |           0.0% |          0.0% |
| after    |           1.3% |          1.0% |

Both benchmarks show a ~1.5% improvement once the patch is applied.

---
 Documentation/admin-guide/mm/transhuge.rst |  6 +++++
 include/linux/huge_mm.h                    | 11 ++++++++
 mm/filemap.c                               | 11 ++++++++
 mm/huge_memory.c                           | 31 +++++++++++++++++-----
 4 files changed, 52 insertions(+), 7 deletions(-)
Documentation/admin-guide/mm/transhuge.rst +6 -0
index 9f3ed504c646..1aaf8e3a0b5a 100644
@@ -292,12 +292,18 @@ memory from a set of allowed sizes. By default all THP sizes that the page cache
 supports are allowed, but this set can be modified with one of::
 
 	echo always >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/file_enabled
+	echo always+exec >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/file_enabled
 	echo never >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/file_enabled
 
 where <size> is the hugepage size being addressed, the available sizes for which
 vary by system. ``always`` adds the hugepage size to the set of allowed sizes,
 and ``never`` removes the hugepage size from the set of allowed sizes.
 
+``always+exec`` acts like ``always`` but additionally marks the hugepage size as
+the preferred hugepage size for sections of any file mapped executable. A
+maximum of one hugepage size can be marked as ``exec`` at a time, so applying it
+to a new size implicitly removes it from any size it was previously set for.
+
 In some situations, constraining the allowed sizes can reduce memory
 fragmentation, resulting in fewer allocation fallbacks and improved system
 performance.
include/linux/huge_mm.h +11 -0
index 19ced8192d39..3571ea0c3d8c 100644
@@ -177,12 +177,18 @@ extern unsigned long huge_anon_orders_always;
 extern unsigned long huge_anon_orders_madvise;
 extern unsigned long huge_anon_orders_inherit;
 extern unsigned long huge_file_orders_always;
+extern int huge_file_exec_order;
 
 static inline unsigned long file_orders_always(void)
 {
 	return READ_ONCE(huge_file_orders_always);
 }
 
+static inline int file_exec_order(void)
+{
+	return READ_ONCE(huge_file_exec_order);
+}
+
 static inline bool hugepage_global_enabled(void)
 {
 	return transparent_hugepage_flags &
@@ -453,6 +459,11 @@ static inline unsigned long file_orders_always(void)
 	return 0;
 }
 
+static inline int file_exec_order(void)
+{
+	return -1;
+}
+
 static inline bool folio_test_pmd_mappable(struct folio *folio)
 {
 	return false;
mm/filemap.c +11 -0
index 870016fcfdde..c4a3cc6a2e46 100644
@@ -3128,6 +3128,7 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
 	struct file *fpin = NULL;
 	unsigned long vm_flags = vmf->vma->vm_flags;
 	unsigned int mmap_miss;
+	int exec_order = file_exec_order();
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 	/* Use the readahead code, even if readahead is disabled */
@@ -3147,6 +3148,16 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
 	}
 #endif
 
+	/* If explicit order is set for exec mappings, use it. */
+	if ((vm_flags & VM_EXEC) && exec_order >= 0) {
+		fpin = maybe_unlock_mmap_for_io(vmf, fpin);
+		ra->size = 1UL << exec_order;
+		ra->async_size = 0;
+		ractl._index &= ~((unsigned long)ra->size - 1);
+		page_cache_ra_order(&ractl, ra, exec_order);
+		return fpin;
+	}
+
 	/* If we don't want any read-ahead, don't bother */
 	if (vm_flags & VM_RAND_READ)
 		return fpin;
mm/huge_memory.c +24 -7
index e8fe28fe9cf9..4249c0bc9388 100644
@@ -81,6 +81,7 @@ unsigned long huge_anon_orders_always __read_mostly;
 unsigned long huge_anon_orders_madvise __read_mostly;
 unsigned long huge_anon_orders_inherit __read_mostly;
 unsigned long huge_file_orders_always __read_mostly;
+int huge_file_exec_order __read_mostly = -1;
 
 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 					 unsigned long vm_flags,
@@ -462,6 +463,7 @@ static const struct attribute_group hugepage_attr_group = {
 static void hugepage_exit_sysfs(struct kobject *hugepage_kobj);
 static void thpsize_release(struct kobject *kobj);
 static DEFINE_SPINLOCK(huge_anon_orders_lock);
+static DEFINE_SPINLOCK(huge_file_orders_lock);
 static LIST_HEAD(thpsize_list);
 
 static ssize_t anon_enabled_show(struct kobject *kobj,
@@ -531,11 +533,15 @@ static ssize_t file_enabled_show(struct kobject *kobj,
 {
 	int order = to_thpsize(kobj)->order;
 	const char *output;
+	bool exec;
 
-	if (test_bit(order, &huge_file_orders_always))
-		output = "[always] never";
-	else
-		output = "always [never]";
+	if (test_bit(order, &huge_file_orders_always)) {
+		exec = READ_ONCE(huge_file_exec_order) == order;
+		output = exec ? "always [always+exec] never" :
+				"[always] always+exec never";
+	} else {
+		output = "always always+exec [never]";
+	}
 
 	return sysfs_emit(buf, "%s\n", output);
 }
@@ -547,13 +553,24 @@ static ssize_t file_enabled_store(struct kobject *kobj,
 	int order = to_thpsize(kobj)->order;
 	ssize_t ret = count;
 
-	if (sysfs_streq(buf, "always"))
+	spin_lock(&huge_file_orders_lock);
+
+	if (sysfs_streq(buf, "always")) {
 		set_bit(order, &huge_file_orders_always);
-	else if (sysfs_streq(buf, "never"))
+		if (huge_file_exec_order == order)
+			huge_file_exec_order = -1;
+	} else if (sysfs_streq(buf, "always+exec")) {
+		set_bit(order, &huge_file_orders_always);
+		huge_file_exec_order = order;
+	} else if (sysfs_streq(buf, "never")) {
 		clear_bit(order, &huge_file_orders_always);
-	else
+		if (huge_file_exec_order == order)
+			huge_file_exec_order = -1;
+	} else {
 		ret = -EINVAL;
+	}
 
+	spin_unlock(&huge_file_orders_lock);
 	return ret;
 }
 

--
2.43.0

author reviewer
#3 lore raw

Re: [RFC PATCH v1 2/4] mm: Introduce "always+exec" for mTHP file_enabled control

Quoted 9 lines · Ryan Roberts

In addition to always and never, add always+exec as an option for:

/sys/kernel/mm/transparent_hugepage/hugepages-*kB/file_enabled

always+exec acts like always but additionally marks the hugepage size as the preferred hugepage size for sections of any file mapped with execute permission. A maximum of one hugepage size can be marked as exec at a time, so applying it to a new size implicitly removes it from any size it was previously set for.

Just to document discussion around this which happened in the THP Cabal meeting, there was a proposal to avoid user controls by always always allocating the largest folio that we can for an exec mapping, given the bounds of the VMA. But we concluded that this would likely cause latency concerns for some workloads (e.g. increased app start up time). We then thought about setting a ceiling on the folio size to allocate (e.g. 64K). But in that case, who decides what the right ceiling value is? So, in my opinion, we are back to user controls.

Thanks,
Ryan

Quoted 246 lines

Change readahead to use this flagged exec size; when a request is made for an executable mapping, do a synchronous read of the size in a naturally aligned manner.

On arm64 if memory is physically contiguous and naturally aligned to the "contpte" size, we can use contpte mappings, which improves utilization of the TLB. When paired with the "multi-size THP" changes, this works well to reduce dTLB pressure. However iTLB pressure is still high due to executable mappings having a low liklihood of being in the required folio size and mapping alignment, even when the filesystem supports readahead into large folios (e.g. XFS).

The reason for the low liklihood is that the current readahead algorithm starts with an order-2 folio and increases the folio order by 2 every time the readahead mark is hit. But most executable memory is faulted in fairly randomly and so the readahead mark is rarely hit and most executable folios remain order-2. This is observed impirically and confirmed from discussion with a gnu linker expert; in general, the linker does nothing to group temporally accessed text together spacially. Additionally, with the current read-around approach there are no alignment guarrantees between the file and folio. This is insufficient for arm64's contpte mapping requirement (order-4 for 4K base pages).

So it seems reasonable to special-case the read(ahead) logic for executable mappings. The trade-off is performance improvement (due to more efficient storage of the translations in iTLB) vs potential read amplification (due to reading too much data around the fault which won't be used), and the latter is independent of base page size.

Of course if no hugepage size is marked as always+exec the old behaviour is maintained.

Performance Benchmarking
------------------------

The below shows kernel compilation and speedometer javascript benchmarks on Ampere Altra arm64 system. When the patch is applied, always+exec is set for 64K folios.

First, confirmation that this patch causes more memory to be contained in 64K folios (this is for all file-backed memory so includes non-executable too):

| File-backed folios      |   Speedometer   |  Kernel Compile |
| by size as percentage   |-----------------|-----------------|
| of all mapped file mem  | before |  after | before |  after |
|=========================|========|========|========|========|
|file-thp-aligned-16kB    |    45% |     9% |    46% |     7% |
|file-thp-aligned-32kB    |     2% |     0% |     3% |     1% |
|file-thp-aligned-64kB    |     3% |    63% |     5% |    80% |
|file-thp-aligned-128kB   |    11% |    11% |     0% |     0% |
|file-thp-unaligned-16kB  |     1% |     0% |     3% |     1% |
|file-thp-unaligned-128kB |     1% |     0% |     0% |     0% |
|file-thp-partial         |     0% |     0% |     0% |     0% |
|-------------------------|--------|--------|--------|--------|
|file-cont-aligned-64kB   |    16% |    75% |     5% |    80% |

The above shows that for both use cases, the amount of file memory backed by 16K folios reduces and the amount backed by 64K folios increases significantly. And the amount of memory that is contpte-mapped significantly increases (last line).

And this is reflected in performance improvement:

Kernel Compilation (smaller is faster):

| kernel   |   real-time |   kern-time |   user-time |   peak memory |
|----------|-------------|-------------|-------------|---------------|
| before   |        0.0% |        0.0% |        0.0% |          0.0% |
| after    |       -1.6% |       -2.1% |       -1.7% |          0.0% |

Speedometer (bigger is faster):

| kernel   |   runs_per_min |   peak memory |
|----------|----------------|---------------|
| before   |           0.0% |          0.0% |
| after    |           1.3% |          1.0% |

Both benchmarks show a ~1.5% improvement once the patch is applied.

---
 Documentation/admin-guide/mm/transhuge.rst |  6 +++++
 include/linux/huge_mm.h                    | 11 ++++++++
 mm/filemap.c                               | 11 ++++++++
 mm/huge_memory.c                           | 31 +++++++++++++++++-----
 4 files changed, 52 insertions(+), 7 deletions(-)
Documentation/admin-guide/mm/transhuge.rst +6 -0
index 9f3ed504c646..1aaf8e3a0b5a 100644
@@ -292,12 +292,18 @@ memory from a set of allowed sizes. By default all THP sizes that the page cache
 supports are allowed, but this set can be modified with one of::
 
 	echo always >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/file_enabled
+	echo always+exec >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/file_enabled
 	echo never >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/file_enabled
 
 where <size> is the hugepage size being addressed, the available sizes for which
 vary by system. ``always`` adds the hugepage size to the set of allowed sizes,
 and ``never`` removes the hugepage size from the set of allowed sizes.
 
+``always+exec`` acts like ``always`` but additionally marks the hugepage size as
+the preferred hugepage size for sections of any file mapped executable. A
+maximum of one hugepage size can be marked as ``exec`` at a time, so applying it
+to a new size implicitly removes it from any size it was previously set for.
+
 In some situations, constraining the allowed sizes can reduce memory
 fragmentation, resulting in fewer allocation fallbacks and improved system
 performance.
include/linux/huge_mm.h +11 -0
index 19ced8192d39..3571ea0c3d8c 100644
@@ -177,12 +177,18 @@ extern unsigned long huge_anon_orders_always;
 extern unsigned long huge_anon_orders_madvise;
 extern unsigned long huge_anon_orders_inherit;
 extern unsigned long huge_file_orders_always;
+extern int huge_file_exec_order;
 
 static inline unsigned long file_orders_always(void)
 {
 	return READ_ONCE(huge_file_orders_always);
 }
 
+static inline int file_exec_order(void)
+{
+	return READ_ONCE(huge_file_exec_order);
+}
+
 static inline bool hugepage_global_enabled(void)
 {
 	return transparent_hugepage_flags &
@@ -453,6 +459,11 @@ static inline unsigned long file_orders_always(void)
 	return 0;
 }
 
+static inline int file_exec_order(void)
+{
+	return -1;
+}
+
 static inline bool folio_test_pmd_mappable(struct folio *folio)
 {
 	return false;
mm/filemap.c +11 -0
index 870016fcfdde..c4a3cc6a2e46 100644
@@ -3128,6 +3128,7 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
 	struct file *fpin = NULL;
 	unsigned long vm_flags = vmf->vma->vm_flags;
 	unsigned int mmap_miss;
+	int exec_order = file_exec_order();
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 	/* Use the readahead code, even if readahead is disabled */
@@ -3147,6 +3148,16 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
 	}
 #endif
 
+	/* If explicit order is set for exec mappings, use it. */
+	if ((vm_flags & VM_EXEC) && exec_order >= 0) {
+		fpin = maybe_unlock_mmap_for_io(vmf, fpin);
+		ra->size = 1UL << exec_order;
+		ra->async_size = 0;
+		ractl._index &= ~((unsigned long)ra->size - 1);
+		page_cache_ra_order(&ractl, ra, exec_order);
+		return fpin;
+	}
+
 	/* If we don't want any read-ahead, don't bother */
 	if (vm_flags & VM_RAND_READ)
 		return fpin;
mm/huge_memory.c +24 -8
index e8fe28fe9cf9..4249c0bc9388 100644
@@ -81,6 +81,7 @@ unsigned long huge_anon_orders_always __read_mostly;
 unsigned long huge_anon_orders_madvise __read_mostly;
 unsigned long huge_anon_orders_inherit __read_mostly;
 unsigned long huge_file_orders_always __read_mostly;
+int huge_file_exec_order __read_mostly = -1;
 
 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 					 unsigned long vm_flags,
@@ -462,6 +463,7 @@ static const struct attribute_group hugepage_attr_group = {
 static void hugepage_exit_sysfs(struct kobject *hugepage_kobj);
 static void thpsize_release(struct kobject *kobj);
 static DEFINE_SPINLOCK(huge_anon_orders_lock);
+static DEFINE_SPINLOCK(huge_file_orders_lock);
 static LIST_HEAD(thpsize_list);
 
 static ssize_t anon_enabled_show(struct kobject *kobj,
@@ -531,11 +533,15 @@ static ssize_t file_enabled_show(struct kobject *kobj,
 {
 	int order = to_thpsize(kobj)->order;
 	const char *output;
+	bool exec;
 
-	if (test_bit(order, &huge_file_orders_always))
-		output = "[always] never";
-	else
-		output = "always [never]";
+	if (test_bit(order, &huge_file_orders_always)) {
+		exec = READ_ONCE(huge_file_exec_order) == order;
+		output = exec ? "always [always+exec] never" :
+				"[always] always+exec never";
+	} else {
+		output = "always always+exec [never]";
+	}
 
 	return sysfs_emit(buf, "%s\n", output);
 }
@@ -547,13 +553,24 @@ static ssize_t file_enabled_store(struct kobject *kobj,
 	int order = to_thpsize(kobj)->order;
 	ssize_t ret = count;
 
-	if (sysfs_streq(buf, "always"))
+	spin_lock(&huge_file_orders_lock);
+
+	if (sysfs_streq(buf, "always")) {
 		set_bit(order, &huge_file_orders_always);
-	else if (sysfs_streq(buf, "never"))
+		if (huge_file_exec_order == order)
+			huge_file_exec_order = -1;
+	} else if (sysfs_streq(buf, "always+exec")) {
+		set_bit(order, &huge_file_orders_always);
+		huge_file_exec_order = order;
+	} else if (sysfs_streq(buf, "never")) {
 		clear_bit(order, &huge_file_orders_always);
-	else
+		if (huge_file_exec_order == order)
+			huge_file_exec_order = -1;
+	} else {
 		ret = -EINVAL;
+	}
 
+	spin_unlock(&huge_file_orders_lock);
 	return ret;
 }
 
--

2.43.0

author reviewer
#4 lore raw

RFC PATCH 3/4 mm: Override mTHP "enabled" defaults at kernel cmdline

In reply to Ryan Roberts

Add thp_anon= cmdline parameter to allow specifying the default enablement of each supported anon THP size. The parameter accepts the following format and can be provided multiple times to configure each size:

thp_anon=<size>[KMG]:<value>

See Documentation/admin-guide/mm/transhuge.rst for more details.

Configuring the defaults at boot time is useful to allow early user space to take advantage of mTHP before its been configured through sysfs.

---
 .../admin-guide/kernel-parameters.txt         |  8 +++
 Documentation/admin-guide/mm/transhuge.rst    | 26 +++++++--
 mm/huge_memory.c                              | 55 ++++++++++++++++++-
 3 files changed, 82 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index bc55fb55cd26..48443ad12e3f 100644
@@ -6592,6 +6592,14 @@
 			<deci-seconds>: poll all this frequency
 			0: no polling (default)
 
+	thp_anon=	[KNL]
+			Format: <size>[KMG]:always|madvise|never|inherit
+			Can be used to control the default behavior of the
+			system with respect to anonymous transparent hugepages.
+			Can be used multiple times for multiple anon THP sizes.
+			See Documentation/admin-guide/mm/transhuge.rst for more
+			details.
+
 	threadirqs	[KNL,EARLY]
 			Force threading of all interrupt handlers except those
 			marked explicitly IRQF_NO_THREAD.
Documentation/admin-guide/mm/transhuge.rst +20 -6
index 1aaf8e3a0b5a..f53d43d986e2 100644
@@ -311,13 +311,27 @@ performance.
 Note that any changes to the allowed set of sizes only applies to future
 file-backed THP allocations.
 
-Boot parameter
-==============
+Boot parameters
+===============
 
-You can change the sysfs boot time defaults of Transparent Hugepage
-Support by passing the parameter ``transparent_hugepage=always`` or
-``transparent_hugepage=madvise`` or ``transparent_hugepage=never``
-to the kernel command line.
+You can change the sysfs boot time default for the top-level "enabled"
+control by passing the parameter ``transparent_hugepage=always`` or
+``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the
+kernel command line.
+
+Alternatively, each supported anonymous THP size can be controlled by
+passing ``thp_anon=<size>[KMG]:<state>``, where ``<size>`` is the THP size
+and ``<state>`` is one of ``always``, ``madvise``, ``never`` or
+``inherit``.
+
+For example, the following will set 64K THP to ``always``::
+
+	thp_anon=64K:always
+
+``thp_anon=`` may be specified multiple times to configure all THP sizes as
+required. If ``thp_anon=`` is specified at least once, any anon THP sizes
+not explicitly configured on the command line are implicitly set to
+``never``.
 
 Hugepages in tmpfs/shmem
 ========================
mm/huge_memory.c +54 -1

73 lines not on this page · raw message

Signature · 1 line
2.43.0
reviewer
#5 lore raw

Re: [RFC PATCH v1 3/4] mm: Override mTHP "enabled" defaults at kernel cmdline

Quoted 6 lines · Ryan Roberts

Add thp_anon= cmdline parameter to allow specifying the default enablement of each supported anon THP size. The parameter accepts the following format and can be provided multiple times to configure each size:

Quoted 7 lines

thp_anon=<size>[KMG]:<value>

See Documentation/admin-guide/mm/transhuge.rst for more details.

Configuring the defaults at boot time is useful to allow early user space to take advantage of mTHP before its been configured through sysfs.

This is exactly what I need and want to implement, as the current behavior is problematic. We need to boot up the system and reach the point where we can set up the sys interfaces to enable mTHP. Many processes miss the opportunity to use mTHP.

On the other hand, userspace might have been tuned to detect that mTHP is enabled, such as a .so library. However, it turns out we have had inconsistent settings between the two stages - before and after setting mTHP enabled by sys interfaces.

Quoted 146 lines
---
 .../admin-guide/kernel-parameters.txt         |  8 +++
 Documentation/admin-guide/mm/transhuge.rst    | 26 +++++++--
 mm/huge_memory.c                              | 55 ++++++++++++++++++-
 3 files changed, 82 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index bc55fb55cd26..48443ad12e3f 100644
@@ -6592,6 +6592,14 @@
                        <deci-seconds>: poll all this frequency
                        0: no polling (default)
 
+       thp_anon=       [KNL]
+                       Format: <size>[KMG]:always|madvise|never|inherit
+                       Can be used to control the default behavior of the
+                       system with respect to anonymous transparent hugepages.
+                       Can be used multiple times for multiple anon THP sizes.
+                       See Documentation/admin-guide/mm/transhuge.rst for more
+                       details.
+
        threadirqs      [KNL,EARLY]
                        Force threading of all interrupt handlers except those
                        marked explicitly IRQF_NO_THREAD.
Documentation/admin-guide/mm/transhuge.rst +20 -6
index 1aaf8e3a0b5a..f53d43d986e2 100644
@@ -311,13 +311,27 @@ performance.
 Note that any changes to the allowed set of sizes only applies to future
 file-backed THP allocations.
 
-Boot parameter
-==============
+Boot parameters
+===============
 
-You can change the sysfs boot time defaults of Transparent Hugepage
-Support by passing the parameter ``transparent_hugepage=always`` or
-``transparent_hugepage=madvise`` or ``transparent_hugepage=never``
-to the kernel command line.
+You can change the sysfs boot time default for the top-level "enabled"
+control by passing the parameter ``transparent_hugepage=always`` or
+``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the
+kernel command line.
+
+Alternatively, each supported anonymous THP size can be controlled by
+passing ``thp_anon=<size>[KMG]:<state>``, where ``<size>`` is the THP size
+and ``<state>`` is one of ``always``, ``madvise``, ``never`` or
+``inherit``.
+
+For example, the following will set 64K THP to ``always``::
+
+       thp_anon=64K:always
+
+``thp_anon=`` may be specified multiple times to configure all THP sizes as
+required. If ``thp_anon=`` is specified at least once, any anon THP sizes
+not explicitly configured on the command line are implicitly set to
+``never``.
 
 Hugepages in tmpfs/shmem
 ========================
mm/huge_memory.c +54 -2
index 4249c0bc9388..794d2790d90d 100644
@@ -82,6 +82,7 @@ unsigned long huge_anon_orders_madvise __read_mostly;
 unsigned long huge_anon_orders_inherit __read_mostly;
 unsigned long huge_file_orders_always __read_mostly;
 int huge_file_exec_order __read_mostly = -1;
+static bool anon_orders_configured;
 
 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
                                         unsigned long vm_flags,
@@ -763,7 +764,10 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
         * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time
         * constant so we have to do this here.
         */
-       huge_anon_orders_inherit = BIT(PMD_ORDER);
+       if (!anon_orders_configured) {
+               huge_anon_orders_inherit = BIT(PMD_ORDER);
+               anon_orders_configured = true;
+       }
 
        /*
         * For pagecache, default to enabling all orders. powerpc's PMD_ORDER
@@ -955,6 +959,55 @@ static int __init setup_transparent_hugepage(char *str)
 }
 __setup("transparent_hugepage=", setup_transparent_hugepage);
 
+static int __init setup_thp_anon(char *str)
+{
+       unsigned long size;
+       char *state;
+       int order;
+       int ret = 0;
+
+       if (!str)
+               goto out;
+
+       size = (unsigned long)memparse(str, &state);
+       order = ilog2(size >> PAGE_SHIFT);
+       if (*state != ':' || !is_power_of_2(size) || size <= PAGE_SIZE ||
+           !(BIT(order) & THP_ORDERS_ALL_ANON))
+               goto out;
+
+       state++;
+
+       if (!strcmp(state, "always")) {
+               clear_bit(order, &huge_anon_orders_inherit);
+               clear_bit(order, &huge_anon_orders_madvise);
+               set_bit(order, &huge_anon_orders_always);
+               ret = 1;
+       } else if (!strcmp(state, "inherit")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_madvise);
+               set_bit(order, &huge_anon_orders_inherit);
+               ret = 1;
+       } else if (!strcmp(state, "madvise")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_inherit);
+               set_bit(order, &huge_anon_orders_madvise);
+               ret = 1;
+       } else if (!strcmp(state, "never")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_inherit);
+               clear_bit(order, &huge_anon_orders_madvise);
+               ret = 1;
+       }
+
+       if (ret)
+               anon_orders_configured = true;
+out:
+       if (!ret)
+               pr_warn("thp_anon=%s: cannot parse, ignored\n", str);
+       return ret;
+}
+__setup("thp_anon=", setup_thp_anon);
+
 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
 {
        if (likely(vma->vm_flags & VM_WRITE))
--
Quoted 2 lines

2.43.0

Thanks
Barry

author reviewer
#6 lore raw

Re: [RFC PATCH v1 3/4] mm: Override mTHP "enabled" defaults at kernel cmdline

Quoted 20 lines · Barry Song
Quoted 6 lines · Ryan Roberts

Add thp_anon= cmdline parameter to allow specifying the default enablement of each supported anon THP size. The parameter accepts the following format and can be provided multiple times to configure each size:

Quoted 7 lines

thp_anon=<size>[KMG]:<value>

See Documentation/admin-guide/mm/transhuge.rst for more details.

Configuring the defaults at boot time is useful to allow early user space to take advantage of mTHP before its been configured through sysfs.

This is exactly what I need and want to implement, as the current behavior is problematic. We need to boot up the system and reach the point where we can set up the sys interfaces to enable mTHP. Many processes miss the opportunity to use mTHP.

Quoted 4 lines

On the other hand, userspace might have been tuned to detect that mTHP is enabled, such as a .so library. However, it turns out we have had inconsistent settings between the two stages - before and after setting mTHP enabled by sys interfaces.

Good feedback - sounds like I should separate out this patch from the rest of the series to get it reviewed and merged faster?

Quoted 152 lines
Quoted 147 lines
---
 .../admin-guide/kernel-parameters.txt         |  8 +++
 Documentation/admin-guide/mm/transhuge.rst    | 26 +++++++--
 mm/huge_memory.c                              | 55 ++++++++++++++++++-
 3 files changed, 82 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index bc55fb55cd26..48443ad12e3f 100644
@@ -6592,6 +6592,14 @@
                        <deci-seconds>: poll all this frequency
                        0: no polling (default)
 
+       thp_anon=       [KNL]
+                       Format: <size>[KMG]:always|madvise|never|inherit
+                       Can be used to control the default behavior of the
+                       system with respect to anonymous transparent hugepages.
+                       Can be used multiple times for multiple anon THP sizes.
+                       See Documentation/admin-guide/mm/transhuge.rst for more
+                       details.
+
        threadirqs      [KNL,EARLY]
                        Force threading of all interrupt handlers except those
                        marked explicitly IRQF_NO_THREAD.
Documentation/admin-guide/mm/transhuge.rst +20 -6
index 1aaf8e3a0b5a..f53d43d986e2 100644
@@ -311,13 +311,27 @@ performance.
 Note that any changes to the allowed set of sizes only applies to future
 file-backed THP allocations.
 
-Boot parameter
-==============
+Boot parameters
+===============
 
-You can change the sysfs boot time defaults of Transparent Hugepage
-Support by passing the parameter ``transparent_hugepage=always`` or
-``transparent_hugepage=madvise`` or ``transparent_hugepage=never``
-to the kernel command line.
+You can change the sysfs boot time default for the top-level "enabled"
+control by passing the parameter ``transparent_hugepage=always`` or
+``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the
+kernel command line.
+
+Alternatively, each supported anonymous THP size can be controlled by
+passing ``thp_anon=<size>[KMG]:<state>``, where ``<size>`` is the THP size
+and ``<state>`` is one of ``always``, ``madvise``, ``never`` or
+``inherit``.
+
+For example, the following will set 64K THP to ``always``::
+
+       thp_anon=64K:always
+
+``thp_anon=`` may be specified multiple times to configure all THP sizes as
+required. If ``thp_anon=`` is specified at least once, any anon THP sizes
+not explicitly configured on the command line are implicitly set to
+``never``.
 
 Hugepages in tmpfs/shmem
 ========================
mm/huge_memory.c +54 -2
index 4249c0bc9388..794d2790d90d 100644
@@ -82,6 +82,7 @@ unsigned long huge_anon_orders_madvise __read_mostly;
 unsigned long huge_anon_orders_inherit __read_mostly;
 unsigned long huge_file_orders_always __read_mostly;
 int huge_file_exec_order __read_mostly = -1;
+static bool anon_orders_configured;
 
 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
                                         unsigned long vm_flags,
@@ -763,7 +764,10 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
         * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time
         * constant so we have to do this here.
         */
-       huge_anon_orders_inherit = BIT(PMD_ORDER);
+       if (!anon_orders_configured) {
+               huge_anon_orders_inherit = BIT(PMD_ORDER);
+               anon_orders_configured = true;
+       }
 
        /*
         * For pagecache, default to enabling all orders. powerpc's PMD_ORDER
@@ -955,6 +959,55 @@ static int __init setup_transparent_hugepage(char *str)
 }
 __setup("transparent_hugepage=", setup_transparent_hugepage);
 
+static int __init setup_thp_anon(char *str)
+{
+       unsigned long size;
+       char *state;
+       int order;
+       int ret = 0;
+
+       if (!str)
+               goto out;
+
+       size = (unsigned long)memparse(str, &state);
+       order = ilog2(size >> PAGE_SHIFT);
+       if (*state != ':' || !is_power_of_2(size) || size <= PAGE_SIZE ||
+           !(BIT(order) & THP_ORDERS_ALL_ANON))
+               goto out;
+
+       state++;
+
+       if (!strcmp(state, "always")) {
+               clear_bit(order, &huge_anon_orders_inherit);
+               clear_bit(order, &huge_anon_orders_madvise);
+               set_bit(order, &huge_anon_orders_always);
+               ret = 1;
+       } else if (!strcmp(state, "inherit")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_madvise);
+               set_bit(order, &huge_anon_orders_inherit);
+               ret = 1;
+       } else if (!strcmp(state, "madvise")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_inherit);
+               set_bit(order, &huge_anon_orders_madvise);
+               ret = 1;
+       } else if (!strcmp(state, "never")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_inherit);
+               clear_bit(order, &huge_anon_orders_madvise);
+               ret = 1;
+       }
+
+       if (ret)
+               anon_orders_configured = true;
+out:
+       if (!ret)
+               pr_warn("thp_anon=%s: cannot parse, ignored\n", str);
+       return ret;
+}
+__setup("thp_anon=", setup_thp_anon);
+
 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
 {
        if (likely(vma->vm_flags & VM_WRITE))
--
Quoted 2 lines

2.43.0

Thanks
Barry

reviewer
#7 lore raw

Re: [RFC PATCH v1 3/4] mm: Override mTHP "enabled" defaults at kernel cmdline

Quoted 22 lines · Ryan Roberts
Quoted 20 lines · Barry Song
Quoted 6 lines · Ryan Roberts

Add thp_anon= cmdline parameter to allow specifying the default enablement of each supported anon THP size. The parameter accepts the following format and can be provided multiple times to configure each size:

Quoted 7 lines

thp_anon=<size>[KMG]:<value>

See Documentation/admin-guide/mm/transhuge.rst for more details.

Configuring the defaults at boot time is useful to allow early user space to take advantage of mTHP before its been configured through sysfs.

This is exactly what I need and want to implement, as the current behavior is problematic. We need to boot up the system and reach the point where we can set up the sys interfaces to enable mTHP. Many processes miss the opportunity to use mTHP.

Quoted 7 lines
Quoted 4 lines

On the other hand, userspace might have been tuned to detect that mTHP is enabled, such as a .so library. However, it turns out we have had inconsistent settings between the two stages - before and after setting mTHP enabled by sys interfaces.

Good feedback - sounds like I should separate out this patch from the rest of the series to get it reviewed and merged faster?

+1

Quoted 155 lines
Quoted 153 lines
Quoted 148 lines
---
 .../admin-guide/kernel-parameters.txt         |  8 +++
 Documentation/admin-guide/mm/transhuge.rst    | 26 +++++++--
 mm/huge_memory.c                              | 55 ++++++++++++++++++-
 3 files changed, 82 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index bc55fb55cd26..48443ad12e3f 100644
@@ -6592,6 +6592,14 @@
                        <deci-seconds>: poll all this frequency
                        0: no polling (default)
 
+       thp_anon=       [KNL]
+                       Format: <size>[KMG]:always|madvise|never|inherit
+                       Can be used to control the default behavior of the
+                       system with respect to anonymous transparent hugepages.
+                       Can be used multiple times for multiple anon THP sizes.
+                       See Documentation/admin-guide/mm/transhuge.rst for more
+                       details.
+
        threadirqs      [KNL,EARLY]
                        Force threading of all interrupt handlers except those
                        marked explicitly IRQF_NO_THREAD.
Documentation/admin-guide/mm/transhuge.rst +20 -6
index 1aaf8e3a0b5a..f53d43d986e2 100644
@@ -311,13 +311,27 @@ performance.
 Note that any changes to the allowed set of sizes only applies to future
 file-backed THP allocations.
 
-Boot parameter
-==============
+Boot parameters
+===============
 
-You can change the sysfs boot time defaults of Transparent Hugepage
-Support by passing the parameter ``transparent_hugepage=always`` or
-``transparent_hugepage=madvise`` or ``transparent_hugepage=never``
-to the kernel command line.
+You can change the sysfs boot time default for the top-level "enabled"
+control by passing the parameter ``transparent_hugepage=always`` or
+``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the
+kernel command line.
+
+Alternatively, each supported anonymous THP size can be controlled by
+passing ``thp_anon=<size>[KMG]:<state>``, where ``<size>`` is the THP size
+and ``<state>`` is one of ``always``, ``madvise``, ``never`` or
+``inherit``.
+
+For example, the following will set 64K THP to ``always``::
+
+       thp_anon=64K:always
+
+``thp_anon=`` may be specified multiple times to configure all THP sizes as
+required. If ``thp_anon=`` is specified at least once, any anon THP sizes
+not explicitly configured on the command line are implicitly set to
+``never``.
 
 Hugepages in tmpfs/shmem
 ========================
mm/huge_memory.c +54 -2
index 4249c0bc9388..794d2790d90d 100644
@@ -82,6 +82,7 @@ unsigned long huge_anon_orders_madvise __read_mostly;
 unsigned long huge_anon_orders_inherit __read_mostly;
 unsigned long huge_file_orders_always __read_mostly;
 int huge_file_exec_order __read_mostly = -1;
+static bool anon_orders_configured;
 
 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
                                         unsigned long vm_flags,
@@ -763,7 +764,10 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
         * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time
         * constant so we have to do this here.
         */
-       huge_anon_orders_inherit = BIT(PMD_ORDER);
+       if (!anon_orders_configured) {
+               huge_anon_orders_inherit = BIT(PMD_ORDER);
+               anon_orders_configured = true;
+       }
 
        /*
         * For pagecache, default to enabling all orders. powerpc's PMD_ORDER
@@ -955,6 +959,55 @@ static int __init setup_transparent_hugepage(char *str)
 }
 __setup("transparent_hugepage=", setup_transparent_hugepage);
 
+static int __init setup_thp_anon(char *str)
+{
+       unsigned long size;
+       char *state;
+       int order;
+       int ret = 0;
+
+       if (!str)
+               goto out;
+
+       size = (unsigned long)memparse(str, &state);
+       order = ilog2(size >> PAGE_SHIFT);
+       if (*state != ':' || !is_power_of_2(size) || size <= PAGE_SIZE ||
+           !(BIT(order) & THP_ORDERS_ALL_ANON))
+               goto out;
+
+       state++;
+
+       if (!strcmp(state, "always")) {
+               clear_bit(order, &huge_anon_orders_inherit);
+               clear_bit(order, &huge_anon_orders_madvise);
+               set_bit(order, &huge_anon_orders_always);
+               ret = 1;
+       } else if (!strcmp(state, "inherit")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_madvise);
+               set_bit(order, &huge_anon_orders_inherit);
+               ret = 1;
+       } else if (!strcmp(state, "madvise")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_inherit);
+               set_bit(order, &huge_anon_orders_madvise);
+               ret = 1;
+       } else if (!strcmp(state, "never")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_inherit);
+               clear_bit(order, &huge_anon_orders_madvise);
+               ret = 1;
+       }
+
+       if (ret)
+               anon_orders_configured = true;
+out:
+       if (!ret)
+               pr_warn("thp_anon=%s: cannot parse, ignored\n", str);
+       return ret;
+}
+__setup("thp_anon=", setup_thp_anon);
+
 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
 {
        if (likely(vma->vm_flags & VM_WRITE))
--
Quoted 2 lines

2.43.0

Thanks
Barry

author reviewer
#8 lore raw

Re: [RFC PATCH v1 3/4] mm: Override mTHP "enabled" defaults at kernel cmdline

Quoted 28 lines · Barry Song
Quoted 27 lines · Ryan Roberts
Quoted 24 lines · Barry Song
Quoted 6 lines · Ryan Roberts

Add thp_anon= cmdline parameter to allow specifying the default enablement of each supported anon THP size. The parameter accepts the following format and can be provided multiple times to configure each size:

Quoted 7 lines

thp_anon=<size>[KMG]:<value>

See Documentation/admin-guide/mm/transhuge.rst for more details.

Configuring the defaults at boot time is useful to allow early user space to take advantage of mTHP before its been configured through sysfs.

This is exactly what I need and want to implement, as the current behavior is problematic. We need to boot up the system and reach the point where we can set up the sys interfaces to enable mTHP. Many processes miss the opportunity to use mTHP.

On the other hand, userspace might have been tuned to detect that mTHP is enabled, such as a .so library. However, it turns out we have had inconsistent settings between the two stages - before and after setting mTHP enabled by sys interfaces.

Quoted 4 lines
Quoted 2 lines

Good feedback - sounds like I should separate out this patch from the rest of the series to get it reviewed and merged faster?

+1

OK I'll wait a couple of days to see if anyone has any feedback against this version, then I'll re-post this on its own.

Quoted 156 lines
Quoted 156 lines
Quoted 154 lines
Quoted 149 lines
---
 .../admin-guide/kernel-parameters.txt         |  8 +++
 Documentation/admin-guide/mm/transhuge.rst    | 26 +++++++--
 mm/huge_memory.c                              | 55 ++++++++++++++++++-
 3 files changed, 82 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index bc55fb55cd26..48443ad12e3f 100644
@@ -6592,6 +6592,14 @@
                        <deci-seconds>: poll all this frequency
                        0: no polling (default)
 
+       thp_anon=       [KNL]
+                       Format: <size>[KMG]:always|madvise|never|inherit
+                       Can be used to control the default behavior of the
+                       system with respect to anonymous transparent hugepages.
+                       Can be used multiple times for multiple anon THP sizes.
+                       See Documentation/admin-guide/mm/transhuge.rst for more
+                       details.
+
        threadirqs      [KNL,EARLY]
                        Force threading of all interrupt handlers except those
                        marked explicitly IRQF_NO_THREAD.
Documentation/admin-guide/mm/transhuge.rst +20 -6
index 1aaf8e3a0b5a..f53d43d986e2 100644
@@ -311,13 +311,27 @@ performance.
 Note that any changes to the allowed set of sizes only applies to future
 file-backed THP allocations.
 
-Boot parameter
-==============
+Boot parameters
+===============
 
-You can change the sysfs boot time defaults of Transparent Hugepage
-Support by passing the parameter ``transparent_hugepage=always`` or
-``transparent_hugepage=madvise`` or ``transparent_hugepage=never``
-to the kernel command line.
+You can change the sysfs boot time default for the top-level "enabled"
+control by passing the parameter ``transparent_hugepage=always`` or
+``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the
+kernel command line.
+
+Alternatively, each supported anonymous THP size can be controlled by
+passing ``thp_anon=<size>[KMG]:<state>``, where ``<size>`` is the THP size
+and ``<state>`` is one of ``always``, ``madvise``, ``never`` or
+``inherit``.
+
+For example, the following will set 64K THP to ``always``::
+
+       thp_anon=64K:always
+
+``thp_anon=`` may be specified multiple times to configure all THP sizes as
+required. If ``thp_anon=`` is specified at least once, any anon THP sizes
+not explicitly configured on the command line are implicitly set to
+``never``.
 
 Hugepages in tmpfs/shmem
 ========================
mm/huge_memory.c +54 -2
index 4249c0bc9388..794d2790d90d 100644
@@ -82,6 +82,7 @@ unsigned long huge_anon_orders_madvise __read_mostly;
 unsigned long huge_anon_orders_inherit __read_mostly;
 unsigned long huge_file_orders_always __read_mostly;
 int huge_file_exec_order __read_mostly = -1;
+static bool anon_orders_configured;
 
 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
                                         unsigned long vm_flags,
@@ -763,7 +764,10 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
         * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time
         * constant so we have to do this here.
         */
-       huge_anon_orders_inherit = BIT(PMD_ORDER);
+       if (!anon_orders_configured) {
+               huge_anon_orders_inherit = BIT(PMD_ORDER);
+               anon_orders_configured = true;
+       }
 
        /*
         * For pagecache, default to enabling all orders. powerpc's PMD_ORDER
@@ -955,6 +959,55 @@ static int __init setup_transparent_hugepage(char *str)
 }
 __setup("transparent_hugepage=", setup_transparent_hugepage);
 
+static int __init setup_thp_anon(char *str)
+{
+       unsigned long size;
+       char *state;
+       int order;
+       int ret = 0;
+
+       if (!str)
+               goto out;
+
+       size = (unsigned long)memparse(str, &state);
+       order = ilog2(size >> PAGE_SHIFT);
+       if (*state != ':' || !is_power_of_2(size) || size <= PAGE_SIZE ||
+           !(BIT(order) & THP_ORDERS_ALL_ANON))
+               goto out;
+
+       state++;
+
+       if (!strcmp(state, "always")) {
+               clear_bit(order, &huge_anon_orders_inherit);
+               clear_bit(order, &huge_anon_orders_madvise);
+               set_bit(order, &huge_anon_orders_always);
+               ret = 1;
+       } else if (!strcmp(state, "inherit")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_madvise);
+               set_bit(order, &huge_anon_orders_inherit);
+               ret = 1;
+       } else if (!strcmp(state, "madvise")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_inherit);
+               set_bit(order, &huge_anon_orders_madvise);
+               ret = 1;
+       } else if (!strcmp(state, "never")) {
+               clear_bit(order, &huge_anon_orders_always);
+               clear_bit(order, &huge_anon_orders_inherit);
+               clear_bit(order, &huge_anon_orders_madvise);
+               ret = 1;
+       }
+
+       if (ret)
+               anon_orders_configured = true;
+out:
+       if (!ret)
+               pr_warn("thp_anon=%s: cannot parse, ignored\n", str);
+       return ret;
+}
+__setup("thp_anon=", setup_thp_anon);
+
 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
 {
        if (likely(vma->vm_flags & VM_WRITE))
--
Quoted 2 lines

2.43.0

Thanks
Barry

#9 lore raw

Re: [RFC PATCH v1 3/4] mm: Override mTHP "enabled" defaults at kernel cmdline

In reply to Barry Song
Quoted 28 lines · Barry Song
Quoted 27 lines · Ryan Roberts
Quoted 24 lines · Barry Song
Quoted 6 lines · Ryan Roberts

Add thp_anon= cmdline parameter to allow specifying the default enablement of each supported anon THP size. The parameter accepts the following format and can be provided multiple times to configure each size:

Quoted 7 lines

thp_anon=<size>[KMG]:<value>

See Documentation/admin-guide/mm/transhuge.rst for more details.

Configuring the defaults at boot time is useful to allow early user space to take advantage of mTHP before its been configured through sysfs.

This is exactly what I need and want to implement, as the current behavior is problematic. We need to boot up the system and reach the point where we can set up the sys interfaces to enable mTHP. Many processes miss the opportunity to use mTHP.

On the other hand, userspace might have been tuned to detect that mTHP is enabled, such as a .so library. However, it turns out we have had inconsistent settings between the two stages - before and after setting mTHP enabled by sys interfaces.

Quoted 4 lines
Quoted 2 lines

Good feedback - sounds like I should separate out this patch from the rest of the series to get it reviewed and merged faster?

+1

Agreed, this is reasonable to have.

Signature · 3 lines
Cheers,

David / dhildenb
#10 lore raw

Re: [RFC PATCH v1 3/4] mm: Override mTHP "enabled" defaults at kernel cmdline

In reply to Ryan Roberts
Quoted 6 lines · Ryan Roberts

Add thp_anon= cmdline parameter to allow specifying the default enablement of each supported anon THP size. The parameter accepts the following format and can be provided multiple times to configure each size:

thp_anon=<size>[KMG]:<value>

Minor suggestion. Should this be renamed to hp_anon= or hugepages_anon= instead? This would align with the values under /sys/kernel/mm/transparent_hugepage/ hugepages-*kB.

Quoted 84 lines

See Documentation/admin-guide/mm/transhuge.rst for more details.

Configuring the defaults at boot time is useful to allow early user space to take advantage of mTHP before its been configured through sysfs.

---
 .../admin-guide/kernel-parameters.txt         |  8 +++
 Documentation/admin-guide/mm/transhuge.rst    | 26 +++++++--
 mm/huge_memory.c                              | 55 ++++++++++++++++++-
 3 files changed, 82 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index bc55fb55cd26..48443ad12e3f 100644
@@ -6592,6 +6592,14 @@
 			<deci-seconds>: poll all this frequency
 			0: no polling (default)
 
+	thp_anon=	[KNL]
+			Format: <size>[KMG]:always|madvise|never|inherit
+			Can be used to control the default behavior of the
+			system with respect to anonymous transparent hugepages.
+			Can be used multiple times for multiple anon THP sizes.
+			See Documentation/admin-guide/mm/transhuge.rst for more
+			details.
+
 	threadirqs	[KNL,EARLY]
 			Force threading of all interrupt handlers except those
 			marked explicitly IRQF_NO_THREAD.
Documentation/admin-guide/mm/transhuge.rst +20 -6
index 1aaf8e3a0b5a..f53d43d986e2 100644
@@ -311,13 +311,27 @@ performance.
 Note that any changes to the allowed set of sizes only applies to future
 file-backed THP allocations.
 
-Boot parameter
-==============
+Boot parameters
+===============
 
-You can change the sysfs boot time defaults of Transparent Hugepage
-Support by passing the parameter ``transparent_hugepage=always`` or
-``transparent_hugepage=madvise`` or ``transparent_hugepage=never``
-to the kernel command line.
+You can change the sysfs boot time default for the top-level "enabled"
+control by passing the parameter ``transparent_hugepage=always`` or
+``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the
+kernel command line.
+
+Alternatively, each supported anonymous THP size can be controlled by
+passing ``thp_anon=<size>[KMG]:<state>``, where ``<size>`` is the THP size
+and ``<state>`` is one of ``always``, ``madvise``, ``never`` or
+``inherit``.
+
+For example, the following will set 64K THP to ``always``::
+
+	thp_anon=64K:always
+
+``thp_anon=`` may be specified multiple times to configure all THP sizes as
+required. If ``thp_anon=`` is specified at least once, any anon THP sizes
+not explicitly configured on the command line are implicitly set to
+``never``.
 
 Hugepages in tmpfs/shmem
 ========================
mm/huge_memory.c +1 -0
index 4249c0bc9388..794d2790d90d 100644
@@ -82,6 +82,7 @@ unsigned long huge_anon_orders_madvise __read_mostly;
 unsigned long huge_anon_orders_inherit __read_mostly;
 unsigned long huge_file_orders_always __read_mostly;
 int huge_file_exec_order __read_mostly = -1;
+static bool anon_orders_configured;
 
 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 					 unsigned long vm_flags,

mm/huge_memory.c static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) · line 768

Quoted 6 lines
mm/huge_memory.c +2 -1
@@ -763,7 +764,10 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
 	 * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time
 	 * constant so we have to do this here.
 	 */
-	huge_anon_orders_inherit = BIT(PMD_ORDER);
+	if (!anon_orders_configured) {
+		huge_anon_orders_inherit = BIT(PMD_ORDER);

PMD_ORDER for 64k base PS systems would result in a 512M value, which exceeds the xarray limit [1]. Therefore, I think we need to avoid PMD-size orders by checking if PMD_ORDER > MAX_PAGECACHE_ORDER.

[1] https://lore.kernel.org/all/[email protected]/

Quoted 64 lines
mm/huge_memory.c +51 -0
+		anon_orders_configured = true;
+	}
 
 	/*
 	 * For pagecache, default to enabling all orders. powerpc's PMD_ORDER
@@ -955,6 +959,55 @@ static int __init setup_transparent_hugepage(char *str)
 }
 __setup("transparent_hugepage=", setup_transparent_hugepage);
 
+static int __init setup_thp_anon(char *str)
+{
+	unsigned long size;
+	char *state;
+	int order;
+	int ret = 0;
+
+	if (!str)
+		goto out;
+
+	size = (unsigned long)memparse(str, &state);
+	order = ilog2(size >> PAGE_SHIFT);
+	if (*state != ':' || !is_power_of_2(size) || size <= PAGE_SIZE ||
+	    !(BIT(order) & THP_ORDERS_ALL_ANON))
+		goto out;
+
+	state++;
+
+	if (!strcmp(state, "always")) {
+		clear_bit(order, &huge_anon_orders_inherit);
+		clear_bit(order, &huge_anon_orders_madvise);
+		set_bit(order, &huge_anon_orders_always);
+		ret = 1;
+	} else if (!strcmp(state, "inherit")) {
+		clear_bit(order, &huge_anon_orders_always);
+		clear_bit(order, &huge_anon_orders_madvise);
+		set_bit(order, &huge_anon_orders_inherit);
+		ret = 1;
+	} else if (!strcmp(state, "madvise")) {
+		clear_bit(order, &huge_anon_orders_always);
+		clear_bit(order, &huge_anon_orders_inherit);
+		set_bit(order, &huge_anon_orders_madvise);
+		ret = 1;
+	} else if (!strcmp(state, "never")) {
+		clear_bit(order, &huge_anon_orders_always);
+		clear_bit(order, &huge_anon_orders_inherit);
+		clear_bit(order, &huge_anon_orders_madvise);
+		ret = 1;
+	}
+
+	if (ret)
+		anon_orders_configured = true;
+out:
+	if (!ret)
+		pr_warn("thp_anon=%s: cannot parse, ignored\n", str);
+	return ret;
+}
+__setup("thp_anon=", setup_thp_anon);
+
 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
 {
 	if (likely(vma->vm_flags & VM_WRITE))
Signature · 1 line
2.43.0
author reviewer
#11 lore raw

Re: [RFC PATCH v1 3/4] mm: Override mTHP "enabled" defaults at kernel cmdline

Quoted 11 lines · Daniel Gomez
Quoted 6 lines · Ryan Roberts

Add thp_anon= cmdline parameter to allow specifying the default enablement of each supported anon THP size. The parameter accepts the following format and can be provided multiple times to configure each size:

thp_anon=<size>[KMG]:<value>

Minor suggestion. Should this be renamed to hp_anon= or hugepages_anon= instead? This would align with the values under /sys/kernel/mm/transparent_hugepage/ hugepages-*kB.

"hp" doesn't feel right; that's not an abreviation we use today to my knowledge. But I'd be happy to change it to "hugepages_anon", if that's the concensus.

Quoted 82 lines
Quoted 82 lines

See Documentation/admin-guide/mm/transhuge.rst for more details.

Configuring the defaults at boot time is useful to allow early user space to take advantage of mTHP before its been configured through sysfs.

---
 .../admin-guide/kernel-parameters.txt         |  8 +++
 Documentation/admin-guide/mm/transhuge.rst    | 26 +++++++--
 mm/huge_memory.c                              | 55 ++++++++++++++++++-
 3 files changed, 82 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index bc55fb55cd26..48443ad12e3f 100644
@@ -6592,6 +6592,14 @@
 			<deci-seconds>: poll all this frequency
 			0: no polling (default)
 
+	thp_anon=	[KNL]
+			Format: <size>[KMG]:always|madvise|never|inherit
+			Can be used to control the default behavior of the
+			system with respect to anonymous transparent hugepages.
+			Can be used multiple times for multiple anon THP sizes.
+			See Documentation/admin-guide/mm/transhuge.rst for more
+			details.
+
 	threadirqs	[KNL,EARLY]
 			Force threading of all interrupt handlers except those
 			marked explicitly IRQF_NO_THREAD.
Documentation/admin-guide/mm/transhuge.rst +20 -6
index 1aaf8e3a0b5a..f53d43d986e2 100644
@@ -311,13 +311,27 @@ performance.
 Note that any changes to the allowed set of sizes only applies to future
 file-backed THP allocations.
 
-Boot parameter
-==============
+Boot parameters
+===============
 
-You can change the sysfs boot time defaults of Transparent Hugepage
-Support by passing the parameter ``transparent_hugepage=always`` or
-``transparent_hugepage=madvise`` or ``transparent_hugepage=never``
-to the kernel command line.
+You can change the sysfs boot time default for the top-level "enabled"
+control by passing the parameter ``transparent_hugepage=always`` or
+``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the
+kernel command line.
+
+Alternatively, each supported anonymous THP size can be controlled by
+passing ``thp_anon=<size>[KMG]:<state>``, where ``<size>`` is the THP size
+and ``<state>`` is one of ``always``, ``madvise``, ``never`` or
+``inherit``.
+
+For example, the following will set 64K THP to ``always``::
+
+	thp_anon=64K:always
+
+``thp_anon=`` may be specified multiple times to configure all THP sizes as
+required. If ``thp_anon=`` is specified at least once, any anon THP sizes
+not explicitly configured on the command line are implicitly set to
+``never``.
 
 Hugepages in tmpfs/shmem
 ========================
mm/huge_memory.c +1 -0
index 4249c0bc9388..794d2790d90d 100644
@@ -82,6 +82,7 @@ unsigned long huge_anon_orders_madvise __read_mostly;
 unsigned long huge_anon_orders_inherit __read_mostly;
 unsigned long huge_file_orders_always __read_mostly;
 int huge_file_exec_order __read_mostly = -1;
+static bool anon_orders_configured;
 
Quoted 13 lines

mm/huge_memory.c static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) · line 768

Quoted 9 lines
mm/huge_memory.c +2 -1
 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 					 unsigned long vm_flags,
@@ -763,7 +764,10 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
 	 * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time
 	 * constant so we have to do this here.
 	 */
-	huge_anon_orders_inherit = BIT(PMD_ORDER);
+	if (!anon_orders_configured) {
+		huge_anon_orders_inherit = BIT(PMD_ORDER);

PMD_ORDER for 64k base PS systems would result in a 512M value, which exceeds the xarray limit [1]. Therefore, I think we need to avoid PMD-size orders by checking if PMD_ORDER > MAX_PAGECACHE_ORDER.

This is for anon memory, which isn't installed in the page cache so its independent of MAX_PAGECACHE_ORDER. I don't believe there is a problem here.

Quoted 66 lines

[1] https://lore.kernel.org/all/[email protected]/

Quoted 63 lines
mm/huge_memory.c +51 -0
+		anon_orders_configured = true;
+	}
 
 	/*
 	 * For pagecache, default to enabling all orders. powerpc's PMD_ORDER
@@ -955,6 +959,55 @@ static int __init setup_transparent_hugepage(char *str)
 }
 __setup("transparent_hugepage=", setup_transparent_hugepage);
 
+static int __init setup_thp_anon(char *str)
+{
+	unsigned long size;
+	char *state;
+	int order;
+	int ret = 0;
+
+	if (!str)
+		goto out;
+
+	size = (unsigned long)memparse(str, &state);
+	order = ilog2(size >> PAGE_SHIFT);
+	if (*state != ':' || !is_power_of_2(size) || size <= PAGE_SIZE ||
+	    !(BIT(order) & THP_ORDERS_ALL_ANON))
+		goto out;
+
+	state++;
+
+	if (!strcmp(state, "always")) {
+		clear_bit(order, &huge_anon_orders_inherit);
+		clear_bit(order, &huge_anon_orders_madvise);
+		set_bit(order, &huge_anon_orders_always);
+		ret = 1;
+	} else if (!strcmp(state, "inherit")) {
+		clear_bit(order, &huge_anon_orders_always);
+		clear_bit(order, &huge_anon_orders_madvise);
+		set_bit(order, &huge_anon_orders_inherit);
+		ret = 1;
+	} else if (!strcmp(state, "madvise")) {
+		clear_bit(order, &huge_anon_orders_always);
+		clear_bit(order, &huge_anon_orders_inherit);
+		set_bit(order, &huge_anon_orders_madvise);
+		ret = 1;
+	} else if (!strcmp(state, "never")) {
+		clear_bit(order, &huge_anon_orders_always);
+		clear_bit(order, &huge_anon_orders_inherit);
+		clear_bit(order, &huge_anon_orders_madvise);
+		ret = 1;
+	}
+
+	if (ret)
+		anon_orders_configured = true;
+out:
+	if (!ret)
+		pr_warn("thp_anon=%s: cannot parse, ignored\n", str);
+	return ret;
+}
+__setup("thp_anon=", setup_thp_anon);
+
 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
 {
 	if (likely(vma->vm_flags & VM_WRITE))
Signature · 1 line
2.43.0
author reviewer
#12 lore raw

Re: [RFC PATCH v1 3/4] mm: Override mTHP "enabled" defaults at kernel cmdline

Quoted 7 lines · Ryan Roberts
Quoted 6 lines · Daniel Gomez
Quoted 5 lines · Ryan Roberts

Add thp_anon= cmdline parameter to allow specifying the default enablement of each supported anon THP size. The parameter accepts the following format and can be provided multiple times to configure each size:

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

thp_anon=<size>[KMG]:<value>

Minor suggestion. Should this be renamed to hp_anon= or hugepages_anon= instead? This would align with the values under /sys/kernel/mm/transparent_hugepage/ hugepages-*kB.

"hp" doesn't feel right; that's not an abreviation we use today to my knowledge. But I'd be happy to change it to "hugepages_anon", if that's the concensus.

Thinking about this a bit more, "hugepages=" is already a cmdline parameter used to reserve hugepages for use with HugeTLB. So I think that could get confusing.

transparent_hugepage= is the existing cmdline parameter for the top-level (anon) control. I considered "transparent_hugepage_anon=" or even just extending to use the same parameter for both the top level and the per-size controls (with optional size):

transparent_hugepage=[<size>[KMG]:]<value>

But given they likely need to be provided multiple times, both of those options seem too long. Which is how I settled on thp_anon= (and in the next patch, thp_file=).

Quoted 167 lines
Quoted 83 lines
Quoted 83 lines

See Documentation/admin-guide/mm/transhuge.rst for more details.

Configuring the defaults at boot time is useful to allow early user space to take advantage of mTHP before its been configured through sysfs.

---
 .../admin-guide/kernel-parameters.txt         |  8 +++
 Documentation/admin-guide/mm/transhuge.rst    | 26 +++++++--
 mm/huge_memory.c                              | 55 ++++++++++++++++++-
 3 files changed, 82 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index bc55fb55cd26..48443ad12e3f 100644
@@ -6592,6 +6592,14 @@
 			<deci-seconds>: poll all this frequency
 			0: no polling (default)
 
+	thp_anon=	[KNL]
+			Format: <size>[KMG]:always|madvise|never|inherit
+			Can be used to control the default behavior of the
+			system with respect to anonymous transparent hugepages.
+			Can be used multiple times for multiple anon THP sizes.
+			See Documentation/admin-guide/mm/transhuge.rst for more
+			details.
+
 	threadirqs	[KNL,EARLY]
 			Force threading of all interrupt handlers except those
 			marked explicitly IRQF_NO_THREAD.
Documentation/admin-guide/mm/transhuge.rst +20 -6
index 1aaf8e3a0b5a..f53d43d986e2 100644
@@ -311,13 +311,27 @@ performance.
 Note that any changes to the allowed set of sizes only applies to future
 file-backed THP allocations.
 
-Boot parameter
-==============
+Boot parameters
+===============
 
-You can change the sysfs boot time defaults of Transparent Hugepage
-Support by passing the parameter ``transparent_hugepage=always`` or
-``transparent_hugepage=madvise`` or ``transparent_hugepage=never``
-to the kernel command line.
+You can change the sysfs boot time default for the top-level "enabled"
+control by passing the parameter ``transparent_hugepage=always`` or
+``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the
+kernel command line.
+
+Alternatively, each supported anonymous THP size can be controlled by
+passing ``thp_anon=<size>[KMG]:<state>``, where ``<size>`` is the THP size
+and ``<state>`` is one of ``always``, ``madvise``, ``never`` or
+``inherit``.
+
+For example, the following will set 64K THP to ``always``::
+
+	thp_anon=64K:always
+
+``thp_anon=`` may be specified multiple times to configure all THP sizes as
+required. If ``thp_anon=`` is specified at least once, any anon THP sizes
+not explicitly configured on the command line are implicitly set to
+``never``.
 
 Hugepages in tmpfs/shmem
 ========================
mm/huge_memory.c +1 -0
index 4249c0bc9388..794d2790d90d 100644
@@ -82,6 +82,7 @@ unsigned long huge_anon_orders_madvise __read_mostly;
 unsigned long huge_anon_orders_inherit __read_mostly;
 unsigned long huge_file_orders_always __read_mostly;
 int huge_file_exec_order __read_mostly = -1;
+static bool anon_orders_configured;
 
Quoted 13 lines

mm/huge_memory.c static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) · line 768

Quoted 9 lines
mm/huge_memory.c +2 -1
 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 					 unsigned long vm_flags,
@@ -763,7 +764,10 @@ static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
 	 * disable all other sizes. powerpc's PMD_ORDER isn't a compile-time
 	 * constant so we have to do this here.
 	 */
-	huge_anon_orders_inherit = BIT(PMD_ORDER);
+	if (!anon_orders_configured) {
+		huge_anon_orders_inherit = BIT(PMD_ORDER);

PMD_ORDER for 64k base PS systems would result in a 512M value, which exceeds the xarray limit [1]. Therefore, I think we need to avoid PMD-size orders by checking if PMD_ORDER > MAX_PAGECACHE_ORDER.

This is for anon memory, which isn't installed in the page cache so its independent of MAX_PAGECACHE_ORDER. I don't believe there is a problem here.

Quoted 66 lines

[1] https://lore.kernel.org/all/[email protected]/

Quoted 63 lines
mm/huge_memory.c +51 -0
+		anon_orders_configured = true;
+	}
 
 	/*
 	 * For pagecache, default to enabling all orders. powerpc's PMD_ORDER
@@ -955,6 +959,55 @@ static int __init setup_transparent_hugepage(char *str)
 }
 __setup("transparent_hugepage=", setup_transparent_hugepage);
 
+static int __init setup_thp_anon(char *str)
+{
+	unsigned long size;
+	char *state;
+	int order;
+	int ret = 0;
+
+	if (!str)
+		goto out;
+
+	size = (unsigned long)memparse(str, &state);
+	order = ilog2(size >> PAGE_SHIFT);
+	if (*state != ':' || !is_power_of_2(size) || size <= PAGE_SIZE ||
+	    !(BIT(order) & THP_ORDERS_ALL_ANON))
+		goto out;
+
+	state++;
+
+	if (!strcmp(state, "always")) {
+		clear_bit(order, &huge_anon_orders_inherit);
+		clear_bit(order, &huge_anon_orders_madvise);
+		set_bit(order, &huge_anon_orders_always);
+		ret = 1;
+	} else if (!strcmp(state, "inherit")) {
+		clear_bit(order, &huge_anon_orders_always);
+		clear_bit(order, &huge_anon_orders_madvise);
+		set_bit(order, &huge_anon_orders_inherit);
+		ret = 1;
+	} else if (!strcmp(state, "madvise")) {
+		clear_bit(order, &huge_anon_orders_always);
+		clear_bit(order, &huge_anon_orders_inherit);
+		set_bit(order, &huge_anon_orders_madvise);
+		ret = 1;
+	} else if (!strcmp(state, "never")) {
+		clear_bit(order, &huge_anon_orders_always);
+		clear_bit(order, &huge_anon_orders_inherit);
+		clear_bit(order, &huge_anon_orders_madvise);
+		ret = 1;
+	}
+
+	if (ret)
+		anon_orders_configured = true;
+out:
+	if (!ret)
+		pr_warn("thp_anon=%s: cannot parse, ignored\n", str);
+	return ret;
+}
+__setup("thp_anon=", setup_thp_anon);
+
 pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
 {
 	if (likely(vma->vm_flags & VM_WRITE))
Signature · 1 line
2.43.0
author reviewer
#13 lore raw

RFC PATCH 4/4 mm: Override mTHP "file_enabled" defaults at kernel cmdline

In reply to Ryan Roberts

Add thp_file= cmdline parameter to allow specifying the default enablement of each supported file-backed THP size. The parameter accepts the following format and can be provided multiple times to configure each size:

thp_file=<size>[KMG]:<value>

See Documentation/admin-guide/mm/transhuge.rst for more details.

Configuring the defaults at boot time is often necessary because its not always possible to drop active executable pages from the page cache, especially if they are well used like libc. The command line parameter allows configuring the values before the first page is installed in the page cache.

---
 .../admin-guide/kernel-parameters.txt         |  8 ++++
 Documentation/admin-guide/mm/transhuge.rst    | 13 ++++++
 mm/huge_memory.c                              | 45 ++++++++++++++++++-
 3 files changed, 65 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index 48443ad12e3f..e3e99def5691 100644
@@ -6600,6 +6600,14 @@
 			See Documentation/admin-guide/mm/transhuge.rst for more
 			details.
 
+	thp_file=	[KNL]
+			Format: <size>[KMG]:always|always+exec|never
+			Can be used to control the default behavior of the
+			system with respect to file-backed transparent hugepages.
+			Can be used multiple times for multiple file-backed THP
+			sizes. See Documentation/admin-guide/mm/transhuge.rst
+			for more details.
+
 	threadirqs	[KNL,EARLY]
 			Force threading of all interrupt handlers except those
 			marked explicitly IRQF_NO_THREAD.
Documentation/admin-guide/mm/transhuge.rst +13 -0
index f53d43d986e2..2379ed4ad085 100644
@@ -333,6 +333,19 @@ required. If ``thp_anon=`` is specified at least once, any anon THP sizes
 not explicitly configured on the command line are implicitly set to
 ``never``.
 
+Each supported file-backed THP size can be controlled by passing
+``thp_file=<size>[KMG]:<state>``, where ``<size>`` is the THP size and
+``<state>`` is one of ``always``, ``always+exec`` or ``never``.
+
+For example, the following will set 64K THP to ``always+exec``::
+
+	thp_file=64K:always+exec
+
+``thp_file=`` may be specified multiple times to configure all THP sizes as
+required. If ``thp_file=`` is specified at least once, any file-backed THP
+sizes not explicitly configured on the command line are implicitly set to
+``never``.
+
 Hugepages in tmpfs/shmem
 ========================
 
mm/huge_memory.c +44 -1

63 lines not on this page · raw message

Signature · 1 line
2.43.0
#14 lore raw
In reply to Ryan Roberts
Quoted 28 lines · Ryan Roberts

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

I'm trying to solve 2 separate problems with this series:

1. Reduce pressure in iTLB and improve performance on arm64: This is a modified approach for the change at [1]. Instead of hardcoding the preferred executable folio size into the arch, user space can now select it. This decouples the arch code and also makes the mechanism more generic; it can be bypassed (the default) or any folio size can be set. For my use case, 64K is preferred, but I've also heard from Willy of a use case where putting all text into 2M PMD-sized folios is preferred. This approach avoids the need for synchonous MADV_COLLAPSE (and therefore faulting in all text ahead of time) to achieve that.

2. Reduce memory fragmentation in systems under high memory pressure (e.g. Android): The theory goes that if all folios are 64K, then failure to allocate a 64K folio should become unlikely. But if the page cache is allocating lots of different orders, with most allocations having an order below 64K (as is the case today) then ability to allocate 64K folios diminishes. By providing control over the allowed set of folio sizes, we can tune to avoid crucial 64K folio allocation failure. Additionally I've heard (second hand) of the need to disable large folios in the page cache entirely due to latency concerns in some settings. These controls allow all of this without kernel changes.

Quoted 8 lines

The value of (1) is clear and the performance improvements are documented in patch 2. I don't yet have any data demonstrating the theory for (2) since I can't reproduce the setup that Barry had at [2]. But my view is that by adding these controls we will enable the community to explore further, in the same way that the anon mTHP controls helped harden the understanding for anonymous memory.

---

How would this interact with other requirements we get from the filesystem (for example, because of the device) [1].

Assuming a device has a filesystem has a min order of X, but we disable anything >= X, how would we combine that configuration/information?

[1] https://lore.kernel.org/all/[email protected]/T/#u

Signature · 3 lines
Cheers,

David / dhildenb
author reviewer
#15 lore raw
Quoted 36 lines · David Hildenbrand
Quoted 35 lines · Ryan Roberts

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

I'm trying to solve 2 separate problems with this series:

1. Reduce pressure in iTLB and improve performance on arm64: This is a modified approach for the change at [1]. Instead of hardcoding the preferred executable folio size into the arch, user space can now select it. This decouples the arch code and also makes the mechanism more generic; it can be bypassed (the default) or any folio size can be set. For my use case, 64K is preferred, but I've also heard from Willy of a use case where putting all text into 2M PMD-sized folios is preferred. This approach avoids the need for synchonous MADV_COLLAPSE (and therefore faulting in all text ahead of time) to achieve that.

2. Reduce memory fragmentation in systems under high memory pressure (e.g. Android): The theory goes that if all folios are 64K, then failure to allocate a 64K folio should become unlikely. But if the page cache is allocating lots of different orders, with most allocations having an order below 64K (as is the case today) then ability to allocate 64K folios diminishes. By providing control over the allowed set of folio sizes, we can tune to avoid crucial 64K folio allocation failure. Additionally I've heard (second hand) of the need to disable large folios in the page cache entirely due to latency concerns in some settings. These controls allow all of this without kernel changes.

The value of (1) is clear and the performance improvements are documented in patch 2. I don't yet have any data demonstrating the theory for (2) since I can't reproduce the setup that Barry had at [2]. But my view is that by adding these controls we will enable the community to explore further, in the same way that the anon mTHP controls helped harden the understanding for anonymous memory.

Quoted 7 lines
Quoted 1 line

---

How would this interact with other requirements we get from the filesystem (for example, because of the device) [1].

Assuming a device has a filesystem has a min order of X, but we disable anything

Quoted 1 line

= X, how would we combine that configuration/information?

Currently order-0 is implicitly the "always-on" fallback order. My thinking was that with [1], the specified min order just becomes that "always-on" fallback order.

Today:

  orders = file_orders_always() | BIT(0);

Tomorrow:

  orders = (file_orders_always() & ~(BIT(min_order) - 1)) | BIT(min_order);

That does mean that in this case, a user-disabled order could still be used. So the controls are really hints rather than definitive commands.

Quoted 5 lines

[1]
https://lore.kernel.org/all/[email protected]/T/#u

#16 lore raw
Quoted 50 lines · Ryan Roberts
Quoted 36 lines · David Hildenbrand
Quoted 35 lines · Ryan Roberts

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

I'm trying to solve 2 separate problems with this series:

1. Reduce pressure in iTLB and improve performance on arm64: This is a modified approach for the change at [1]. Instead of hardcoding the preferred executable folio size into the arch, user space can now select it. This decouples the arch code and also makes the mechanism more generic; it can be bypassed (the default) or any folio size can be set. For my use case, 64K is preferred, but I've also heard from Willy of a use case where putting all text into 2M PMD-sized folios is preferred. This approach avoids the need for synchonous MADV_COLLAPSE (and therefore faulting in all text ahead of time) to achieve that.

2. Reduce memory fragmentation in systems under high memory pressure (e.g. Android): The theory goes that if all folios are 64K, then failure to allocate a 64K folio should become unlikely. But if the page cache is allocating lots of different orders, with most allocations having an order below 64K (as is the case today) then ability to allocate 64K folios diminishes. By providing control over the allowed set of folio sizes, we can tune to avoid crucial 64K folio allocation failure. Additionally I've heard (second hand) of the need to disable large folios in the page cache entirely due to latency concerns in some settings. These controls allow all of this without kernel changes.

The value of (1) is clear and the performance improvements are documented in patch 2. I don't yet have any data demonstrating the theory for (2) since I can't reproduce the setup that Barry had at [2]. But my view is that by adding these controls we will enable the community to explore further, in the same way that the anon mTHP controls helped harden the understanding for anonymous memory.

Quoted 7 lines
Quoted 1 line

---

How would this interact with other requirements we get from the filesystem (for example, because of the device) [1].

Assuming a device has a filesystem has a min order of X, but we disable anything

Quoted 1 line

= X, how would we combine that configuration/information?

Currently order-0 is implicitly the "always-on" fallback order. My thinking was that with [1], the specified min order just becomes that "always-on" fallback order.

Today:

Quoted 8 lines
   orders = file_orders_always() | BIT(0);

Tomorrow:

   orders = (file_orders_always() & ~(BIT(min_order) - 1)) | BIT(min_order);

That does mean that in this case, a user-disabled order could still be used. So the controls are really hints rather than definitive commands.

Okay, because that's a difference to order-0, which is -- as you note -- always-on (not even a toggle).

Staring at patch #1, you use the name "file_enable". That might indeed cause some confusion. Thinking out loud, I wonder if a different terminology could better express the semantics. Hm ... but maybe it only would have to be documented.

Thanks for the details.

Signature · 3 lines
Cheers,

David / dhildenb
#17 lore raw
In reply to Ryan Roberts
Quoted 50 lines · Ryan Roberts
Quoted 36 lines · David Hildenbrand
Quoted 35 lines · Ryan Roberts

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

I'm trying to solve 2 separate problems with this series:

1. Reduce pressure in iTLB and improve performance on arm64: This is a modified approach for the change at [1]. Instead of hardcoding the preferred executable folio size into the arch, user space can now select it. This decouples the arch code and also makes the mechanism more generic; it can be bypassed (the default) or any folio size can be set. For my use case, 64K is preferred, but I've also heard from Willy of a use case where putting all text into 2M PMD-sized folios is preferred. This approach avoids the need for synchonous MADV_COLLAPSE (and therefore faulting in all text ahead of time) to achieve that.

2. Reduce memory fragmentation in systems under high memory pressure (e.g. Android): The theory goes that if all folios are 64K, then failure to allocate a 64K folio should become unlikely. But if the page cache is allocating lots of different orders, with most allocations having an order below 64K (as is the case today) then ability to allocate 64K folios diminishes. By providing control over the allowed set of folio sizes, we can tune to avoid crucial 64K folio allocation failure. Additionally I've heard (second hand) of the need to disable large folios in the page cache entirely due to latency concerns in some settings. These controls allow all of this without kernel changes.

The value of (1) is clear and the performance improvements are documented in patch 2. I don't yet have any data demonstrating the theory for (2) since I can't reproduce the setup that Barry had at [2]. But my view is that by adding these controls we will enable the community to explore further, in the same way that the anon mTHP controls helped harden the understanding for anonymous memory.

Quoted 7 lines
Quoted 1 line

---

How would this interact with other requirements we get from the filesystem (for example, because of the device) [1].

Assuming a device has a filesystem has a min order of X, but we disable anything

Quoted 1 line

= X, how would we combine that configuration/information?

Currently order-0 is implicitly the "always-on" fallback order. My thinking was that with [1], the specified min order just becomes that "always-on" fallback order.

Today:

Quoted 8 lines
  orders = file_orders_always() | BIT(0);

Tomorrow:

  orders = (file_orders_always() & ~(BIT(min_order) - 1)) | BIT(min_order);

That does mean that in this case, a user-disabled order could still be used. So the controls are really hints rather than definitive commands.

In the scenario where a min order is not enabled in hugepages-<size>kB/ file_enabled, will the user still be allowed to automatically mkfs/mount with blocksize=min_order, and will sysfs reflect this? Or, since it's a hint, will it remain hidden but still allow mkfs/mount to proceed?

Quoted 8 lines
Quoted 7 lines

[1]
https://lore.kernel.org/all/[email protected]/T/#u

author reviewer
#18 lore raw
Quoted 57 lines · Daniel Gomez
Quoted 56 lines · Ryan Roberts
Quoted 36 lines · David Hildenbrand
Quoted 35 lines · Ryan Roberts

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

I'm trying to solve 2 separate problems with this series:

1. Reduce pressure in iTLB and improve performance on arm64: This is a modified approach for the change at [1]. Instead of hardcoding the preferred executable folio size into the arch, user space can now select it. This decouples the arch code and also makes the mechanism more generic; it can be bypassed (the default) or any folio size can be set. For my use case, 64K is preferred, but I've also heard from Willy of a use case where putting all text into 2M PMD-sized folios is preferred. This approach avoids the need for synchonous MADV_COLLAPSE (and therefore faulting in all text ahead of time) to achieve that.

2. Reduce memory fragmentation in systems under high memory pressure (e.g. Android): The theory goes that if all folios are 64K, then failure to allocate a 64K folio should become unlikely. But if the page cache is allocating lots of different orders, with most allocations having an order below 64K (as is the case today) then ability to allocate 64K folios diminishes. By providing control over the allowed set of folio sizes, we can tune to avoid crucial 64K folio allocation failure. Additionally I've heard (second hand) of the need to disable large folios in the page cache entirely due to latency concerns in some settings. These controls allow all of this without kernel changes.

The value of (1) is clear and the performance improvements are documented in patch 2. I don't yet have any data demonstrating the theory for (2) since I can't reproduce the setup that Barry had at [2]. But my view is that by adding these controls we will enable the community to explore further, in the same way that the anon mTHP controls helped harden the understanding for anonymous memory.

Quoted 7 lines
Quoted 1 line

---

How would this interact with other requirements we get from the filesystem (for example, because of the device) [1].

Assuming a device has a filesystem has a min order of X, but we disable anything

Quoted 1 line

= X, how would we combine that configuration/information?

Currently order-0 is implicitly the "always-on" fallback order. My thinking was that with [1], the specified min order just becomes that "always-on" fallback order.

Today:

  orders = file_orders_always() | BIT(0);

Tomorrow:

  orders = (file_orders_always() & ~(BIT(min_order) - 1)) | BIT(min_order);
Quoted 7 lines
Quoted 2 lines

That does mean that in this case, a user-disabled order could still be used. So the controls are really hints rather than definitive commands.

In the scenario where a min order is not enabled in hugepages-<size>kB/ file_enabled, will the user still be allowed to automatically mkfs/mount with blocksize=min_order, and will sysfs reflect this? Or, since it's a hint, will it remain hidden but still allow mkfs/mount to proceed?

My proposal is that the controls are hints, and they would not block mounting a file system.

As an example, the user may set /sys/kernel/mm/transparent_hugepage/hugepages-16kB/file_enable to never. In this case the kernel would never pick a 16K folio to back a file who's minimum folio size is not 16K. If the file's minimum folio size is 16K then it would still allocate that folio size in the fallback case, after trying any appropriate bigger folio sizes that are set to always.

Thanks,
Ryan

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

[1]
https://lore.kernel.org/all/[email protected]/T/#u

author reviewer
#19 lore raw
In reply to Ryan Roberts
Quoted 9 lines · Ryan Roberts

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

I'm trying to solve 2 separate problems with this series:

Quoted 8 lines

1. Reduce pressure in iTLB and improve performance on arm64: This is a modified approach for the change at [1]. Instead of hardcoding the preferred executable folio size into the arch, user space can now select it. This decouples the arch code and also makes the mechanism more generic; it can be bypassed (the default) or any folio size can be set. For my use case, 64K is preferred, but I've also heard from Willy of a use case where putting all text into 2M PMD-sized folios is preferred. This approach avoids the need for synchonous MADV_COLLAPSE (and therefore faulting in all text ahead of time) to achieve that.

Just a polite bump on this; I'd really like to get something like this merged to help reduce iTLB pressure. We had a discussion at the THP Cabal meeting a few weeks back without solid conclusion. I haven't heard any concrete objections yet, but also only a luke-warm reception. How can I move this forwards?

Thanks,
Ryan

Quoted 47 lines

2. Reduce memory fragmentation in systems under high memory pressure (e.g. Android): The theory goes that if all folios are 64K, then failure to allocate a 64K folio should become unlikely. But if the page cache is allocating lots of different orders, with most allocations having an order below 64K (as is the case today) then ability to allocate 64K folios diminishes. By providing control over the allowed set of folio sizes, we can tune to avoid crucial 64K folio allocation failure. Additionally I've heard (second hand) of the need to disable large folios in the page cache entirely due to latency concerns in some settings. These controls allow all of this without kernel changes.

The value of (1) is clear and the performance improvements are documented in patch 2. I don't yet have any data demonstrating the theory for (2) since I can't reproduce the setup that Barry had at [2]. But my view is that by adding these controls we will enable the community to explore further, in the same way that the anon mTHP controls helped harden the understanding for anonymous memory.

---
This series depends on the "mTHP allocation stats for file-backed memory" series at [3], which itself applies on top of yesterday's mm-unstable (650b6752c8a3). All mm selftests have been run; no regressions were observed.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://www.youtube.com/watch?v=ht7eGWqwmNs&list=PLbzoR-pLrL6oj1rVTXLnV7cOuetvjKn9q&index=4
[3] https://lore.kernel.org/linux-mm/[email protected]/

Thanks,
Ryan

Ryan Roberts (4):
mm: mTHP user controls to configure pagecache large folio sizes
mm: Introduce "always+exec" for mTHP file_enabled control
mm: Override mTHP "enabled" defaults at kernel cmdline
mm: Override mTHP "file_enabled" defaults at kernel cmdline

 .../admin-guide/kernel-parameters.txt         |  16 ++
 Documentation/admin-guide/mm/transhuge.rst    |  66 +++++++-
 include/linux/huge_mm.h                       |  61 ++++---
 mm/filemap.c                                  |  26 ++-
 mm/huge_memory.c                              | 158 +++++++++++++++++-
 mm/readahead.c                                |  43 ++++-
 6 files changed, 329 insertions(+), 41 deletions(-)

--
2.43.0

reviewer
#20 lore raw
Quoted 20 lines · Ryan Roberts
Quoted 9 lines · Ryan Roberts

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

I'm trying to solve 2 separate problems with this series:

Quoted 8 lines

1. Reduce pressure in iTLB and improve performance on arm64: This is a modified approach for the change at [1]. Instead of hardcoding the preferred executable folio size into the arch, user space can now select it. This decouples the arch code and also makes the mechanism more generic; it can be bypassed (the default) or any folio size can be set. For my use case, 64K is preferred, but I've also heard from Willy of a use case where putting all text into 2M PMD-sized folios is preferred. This approach avoids the need for synchonous MADV_COLLAPSE (and therefore faulting in all text ahead of time) to achieve that.

Quoted 4 lines

Just a polite bump on this; I'd really like to get something like this merged to help reduce iTLB pressure. We had a discussion at the THP Cabal meeting a few weeks back without solid conclusion. I haven't heard any concrete objections yet, but also only a luke-warm reception. How can I move this forwards?

Hi Ryan,

These requirements seem to apply to anon, swap, pagecache, and shmem to some extent. While the swapin_enabled knob was rejected, the shmem_enabled option is already in place.

I wonder if it's possible to use the existing 'enabled' setting across all cases, as
from an architectural perspective with cont-pte, pagecache may not differ from anon. The demand for reducing page faults, LRU overhead, etc., also seems quite similar.

I imagine that once Android's file systems support mTHP, we’ll uniformly enable 64KB for anon, swap, shmem, and page cache. It should then be sufficient to enable all of them using a single knob:
'/sys/kernel/mm/transparent_hugepage/hugepages-xxkB/enabled'.

Is there anything that makes pagecache and shmem significantly different from anon? In my Android case, they all seem the same. However, I assume there might be other use cases where differentiating them is necessary?

Quoted 53 lines

Thanks,
Ryan

Quoted 47 lines

2. Reduce memory fragmentation in systems under high memory pressure (e.g. Android): The theory goes that if all folios are 64K, then failure to allocate a 64K folio should become unlikely. But if the page cache is allocating lots of different orders, with most allocations having an order below 64K (as is the case today) then ability to allocate 64K folios diminishes. By providing control over the allowed set of folio sizes, we can tune to avoid crucial 64K folio allocation failure. Additionally I've heard (second hand) of the need to disable large folios in the page cache entirely due to latency concerns in some settings. These controls allow all of this without kernel changes.

The value of (1) is clear and the performance improvements are documented in patch 2. I don't yet have any data demonstrating the theory for (2) since I can't reproduce the setup that Barry had at [2]. But my view is that by adding these controls we will enable the community to explore further, in the same way that the anon mTHP controls helped harden the understanding for anonymous memory.

---
This series depends on the "mTHP allocation stats for file-backed memory" series at [3], which itself applies on top of yesterday's mm-unstable (650b6752c8a3). All mm selftests have been run; no regressions were observed.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://www.youtube.com/watch?v=ht7eGWqwmNs&list=PLbzoR-pLrL6oj1rVTXLnV7cOuetvjKn9q&index=4
[3] https://lore.kernel.org/linux-mm/[email protected]/

Thanks,
Ryan

Ryan Roberts (4):
mm: mTHP user controls to configure pagecache large folio sizes
mm: Introduce "always+exec" for mTHP file_enabled control
mm: Override mTHP "enabled" defaults at kernel cmdline
mm: Override mTHP "file_enabled" defaults at kernel cmdline

 .../admin-guide/kernel-parameters.txt         |  16 ++
 Documentation/admin-guide/mm/transhuge.rst    |  66 +++++++-
 include/linux/huge_mm.h                       |  61 ++++---
 mm/filemap.c                                  |  26 ++-
 mm/huge_memory.c                              | 158 +++++++++++++++++-
 mm/readahead.c                                |  43 ++++-
 6 files changed, 329 insertions(+), 41 deletions(-)

--
2.43.0

Thanks
Barry

author reviewer
#21 lore raw
Quoted 38 lines · Barry Song
Quoted 20 lines · Ryan Roberts
Quoted 9 lines · Ryan Roberts

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

I'm trying to solve 2 separate problems with this series:

Quoted 8 lines

1. Reduce pressure in iTLB and improve performance on arm64: This is a modified approach for the change at [1]. Instead of hardcoding the preferred executable folio size into the arch, user space can now select it. This decouples the arch code and also makes the mechanism more generic; it can be bypassed (the default) or any folio size can be set. For my use case, 64K is preferred, but I've also heard from Willy of a use case where putting all text into 2M PMD-sized folios is preferred. This approach avoids the need for synchonous MADV_COLLAPSE (and therefore faulting in all text ahead of time) to achieve that.

Quoted 4 lines

Just a polite bump on this; I'd really like to get something like this merged to help reduce iTLB pressure. We had a discussion at the THP Cabal meeting a few weeks back without solid conclusion. I haven't heard any concrete objections yet, but also only a luke-warm reception. How can I move this forwards?

Hi Ryan,

These requirements seem to apply to anon, swap, pagecache, and shmem to some extent. While the swapin_enabled knob was rejected, the shmem_enabled option is already in place.

I wonder if it's possible to use the existing 'enabled' setting across all cases, as
from an architectural perspective with cont-pte, pagecache may not differ from anon. The demand for reducing page faults, LRU overhead, etc., also seems quite similar.

Quoted 8 lines

I imagine that once Android's file systems support mTHP, we’ll uniformly enable 64KB for anon, swap, shmem, and page cache. It should then be sufficient to enable all of them using a single knob:
'/sys/kernel/mm/transparent_hugepage/hugepages-xxkB/enabled'.

Is there anything that makes pagecache and shmem significantly different from anon? In my Android case, they all seem the same. However, I assume there might be other use cases where differentiating them is necessary?

For anon vs shmem, we were just following the precedent set by the legacy PMD controls, which separated them. I vaguely recall David explaining why there are separate controls but don't recall the exact reason; I beleive there was some use case where anon THP made sense, but shmem THP was problematic for some reason. Note too, that the controls expose different options; anon has {always never, madvise}, shmem has {always, never, advise (no m; it applies to fadvise too), within_size, force, deny}. So I guess if the extra shmem options are important then it makes sense to have a separate control.

For pagecache vs anon, I'm not sure it makes sense to tie these to the same control. We have readahead information to help us make an educated guess at the folio size we should use (currently we start at order-2 and increase by 2 orders every time we hit the readahead marker) and it's much easier to drop pagecache folios under memory pressure. So by default, I think most/all orders would be enabled for pagecahce. But for anon, things are harder. In the common case, likely we only want 2M when madvised, and 64K always (and possibly 16K always).

Talking with Willy today, his preference is to not expose any controls for pagecache at all, and let the architecture hint the preferred folio size for code - basically how I did it at [1] - linked in the original post. This is very simple and exposes no user controls so could be easily modified over time as we get more data.

Trouble is nobody seemed willing to R-b the first approach. So perhaps we're stuck waiting for Android's FSs to support large folios so we can start benchmarking the real-world gains?

Thanks,
Ryan

Quoted 57 lines
Quoted 54 lines

Thanks,
Ryan

Quoted 47 lines

2. Reduce memory fragmentation in systems under high memory pressure (e.g. Android): The theory goes that if all folios are 64K, then failure to allocate a 64K folio should become unlikely. But if the page cache is allocating lots of different orders, with most allocations having an order below 64K (as is the case today) then ability to allocate 64K folios diminishes. By providing control over the allowed set of folio sizes, we can tune to avoid crucial 64K folio allocation failure. Additionally I've heard (second hand) of the need to disable large folios in the page cache entirely due to latency concerns in some settings. These controls allow all of this without kernel changes.

The value of (1) is clear and the performance improvements are documented in patch 2. I don't yet have any data demonstrating the theory for (2) since I can't reproduce the setup that Barry had at [2]. But my view is that by adding these controls we will enable the community to explore further, in the same way that the anon mTHP controls helped harden the understanding for anonymous memory.

---
This series depends on the "mTHP allocation stats for file-backed memory" series at [3], which itself applies on top of yesterday's mm-unstable (650b6752c8a3). All mm selftests have been run; no regressions were observed.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://www.youtube.com/watch?v=ht7eGWqwmNs&list=PLbzoR-pLrL6oj1rVTXLnV7cOuetvjKn9q&index=4
[3] https://lore.kernel.org/linux-mm/[email protected]/

Thanks,
Ryan

Ryan Roberts (4):
mm: mTHP user controls to configure pagecache large folio sizes
mm: Introduce "always+exec" for mTHP file_enabled control
mm: Override mTHP "enabled" defaults at kernel cmdline
mm: Override mTHP "file_enabled" defaults at kernel cmdline

 .../admin-guide/kernel-parameters.txt         |  16 ++
 Documentation/admin-guide/mm/transhuge.rst    |  66 +++++++-
 include/linux/huge_mm.h                       |  61 ++++---
 mm/filemap.c                                  |  26 ++-
 mm/huge_memory.c                              | 158 +++++++++++++++++-
 mm/readahead.c                                |  43 ++++-
 6 files changed, 329 insertions(+), 41 deletions(-)

--
2.43.0

Thanks
Barry

#22 lore raw
In reply to Barry Song

It's unusual that many emails sent days ago are resurfacing on LKML. Please ignore them.
By the way, does anyone know what happened?

Quoted 105 lines · Barry Song
Quoted 20 lines · Ryan Roberts
Quoted 9 lines · Ryan Roberts

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

I'm trying to solve 2 separate problems with this series:

Quoted 8 lines

1. Reduce pressure in iTLB and improve performance on arm64: This is a modified approach for the change at [1]. Instead of hardcoding the preferred executable folio size into the arch, user space can now select it. This decouples the arch code and also makes the mechanism more generic; it can be bypassed (the default) or any folio size can be set. For my use case, 64K is preferred, but I've also heard from Willy of a use case where putting all text into 2M PMD-sized folios is preferred. This approach avoids the need for synchonous MADV_COLLAPSE (and therefore faulting in all text ahead of time) to achieve that.

Quoted 4 lines

Just a polite bump on this; I'd really like to get something like this merged to help reduce iTLB pressure. We had a discussion at the THP Cabal meeting a few weeks back without solid conclusion. I haven't heard any concrete objections yet, but also only a luke-warm reception. How can I move this forwards?

Hi Ryan,

These requirements seem to apply to anon, swap, pagecache, and shmem to some extent. While the swapin_enabled knob was rejected, the shmem_enabled option is already in place.

I wonder if it's possible to use the existing 'enabled' setting across all cases, as
from an architectural perspective with cont-pte, pagecache may not differ from anon. The demand for reducing page faults, LRU overhead, etc., also seems quite similar.

I imagine that once Android's file systems support mTHP, we’ll uniformly enable 64KB for anon, swap, shmem, and page cache. It should then be sufficient to enable all of them using a single knob:
'/sys/kernel/mm/transparent_hugepage/hugepages-xxkB/enabled'.

Is there anything that makes pagecache and shmem significantly different from anon? In my Android case, they all seem the same. However, I assume there might be other use cases where differentiating them is necessary?

Quoted 53 lines

Thanks,
Ryan

Quoted 47 lines

2. Reduce memory fragmentation in systems under high memory pressure (e.g. Android): The theory goes that if all folios are 64K, then failure to allocate a 64K folio should become unlikely. But if the page cache is allocating lots of different orders, with most allocations having an order below 64K (as is the case today) then ability to allocate 64K folios diminishes. By providing control over the allowed set of folio sizes, we can tune to avoid crucial 64K folio allocation failure. Additionally I've heard (second hand) of the need to disable large folios in the page cache entirely due to latency concerns in some settings. These controls allow all of this without kernel changes.

The value of (1) is clear and the performance improvements are documented in patch 2. I don't yet have any data demonstrating the theory for (2) since I can't reproduce the setup that Barry had at [2]. But my view is that by adding these controls we will enable the community to explore further, in the same way that the anon mTHP controls helped harden the understanding for anonymous memory.

---
This series depends on the "mTHP allocation stats for file-backed memory" series at [3], which itself applies on top of yesterday's mm-unstable (650b6752c8a3). All mm selftests have been run; no regressions were observed.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://www.youtube.com/watch?v=ht7eGWqwmNs&list=PLbzoR-pLrL6oj1rVTXLnV7cOuetvjKn9q&index=4
[3] https://lore.kernel.org/linux-mm/[email protected]/

Thanks,
Ryan

Ryan Roberts (4):
mm: mTHP user controls to configure pagecache large folio sizes
mm: Introduce "always+exec" for mTHP file_enabled control
mm: Override mTHP "enabled" defaults at kernel cmdline
mm: Override mTHP "file_enabled" defaults at kernel cmdline

 .../admin-guide/kernel-parameters.txt         |  16 ++
 Documentation/admin-guide/mm/transhuge.rst    |  66 +++++++-
 include/linux/huge_mm.h                       |  61 ++++---
 mm/filemap.c                                  |  26 ++-
 mm/huge_memory.c                              | 158 +++++++++++++++++-
 mm/readahead.c                                |  43 ++++-
 6 files changed, 329 insertions(+), 41 deletions(-)

--
2.43.0

Thanks
Barry

#24 lore raw
In reply to Ryan Roberts
Quoted 9 lines · Ryan Roberts

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

I'm trying to solve 2 separate problems with this series:

What happened to this patchkit? I was looking into how to efficiently get larger pages for text transparently, and there doesn't seem to be anything better than such a heuristic?

Thanks,
-Andi

#25 lore raw
Quoted 10 lines · Andi Kleen
Quoted 7 lines · Ryan Roberts

Hi All,

This series is an RFC that adds sysfs and kernel cmdline controls to configure the set of allowed large folio sizes that can be used when allocating file-memory for the page cache. As part of the control mechanism, it provides for a special-case "preferred folio size for executable mappings" marker.

Quoted 6 lines
Quoted 2 lines

I'm trying to solve 2 separate problems with this series:

What happened to this patchkit? I was looking into how to efficiently get larger pages for text transparently, and there doesn't seem to be anything better than such a heuristic?

I do feel that we need some controls over file folio sizes.

It could also be implemented using an eBPF-based approach, such as [1]?

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

Thanks
Barry