Skip to contentSkip to conversation

mm: stop recalculating min_free_kbytes once set by userspace

PATCH Applied Nimrod Oren 6 participants 9 messages Oct 14 – Oct 16

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: stop recalculating min_free_kbytes once set by userspace

min_free_kbytes is recalculated on memory hotplug and on THP sysfs writes. The new value comes from calculate_min_free_kbytes() or, while THP is enabled, from set_recommended_min_free_kbytes(). A value written to the vm.min_free_kbytes sysctl is replaced whenever the new value is higher, so a lower value does not stick. This matters most when THP is enabled on configurations with large pageblocks, where the new value can be excessive [1].

Stop recalculating min_free_kbytes once a value has been written to the sysctl. Systems where the sysctl is never written keep the current behavior.

---
 Documentation/admin-guide/sysctl/vm.rst | 7 +++++++
 mm/khugepaged.c                         | 7 +------
 mm/page_alloc.c                         | 6 +-----
 3 files changed, 9 insertions(+), 11 deletions(-)
Documentation/admin-guide/sysctl/vm.rst +7 -0
index 5b318d17aa4b..189fc57dac5f 100644
@@ -539,6 +539,13 @@ watermark[WMARK_MIN] value for each lowmem zone in the system.
 Each lowmem zone gets a number of reserved free pages based
 proportionally on its size.
 
+By default, the kernel sets this value based on the amount of lowmem
+and recalculates it on memory hotplug.  When Transparent Hugepage (THP)
+support is enabled through global or per-size controls, the kernel may
+also raise it to help keep pageblocks free and reduce fragmentation for
+THP allocations.  Once a value is written to this file, the kernel no
+longer adjusts it automatically.
+
 Some minimal amount of memory is needed to satisfy PF_MEMALLOC
 allocations; if you set this to lower than 1024KB, your system will
 become subtly broken, and prone to deadlock under high loads.
mm/khugepaged.c +1 -6
index dbae78b68dd8..3fd209f9377d 100644
@@ -3148,13 +3148,8 @@ void set_recommended_min_free_kbytes(void)
 			      (unsigned long) nr_free_buffer_pages() / 20);
 	recommended_min <<= (PAGE_SHIFT-10);
 
-	if (recommended_min > min_free_kbytes) {
-		if (user_min_free_kbytes >= 0)
-			pr_info_ratelimited("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
-					    min_free_kbytes, recommended_min);
-
+	if (user_min_free_kbytes < 0 && recommended_min > min_free_kbytes)
 		min_free_kbytes = recommended_min;
-	}
 
 update_wmarks:
 	setup_per_zone_wmarks();
mm/page_alloc.c +1 -5
index 6ed36ba4d055..57a33da2562d 100644
@@ -6737,12 +6737,8 @@ void calculate_min_free_kbytes(void)
 	lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
 	new_min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
 
-	if (new_min_free_kbytes > user_min_free_kbytes)
+	if (user_min_free_kbytes < 0)
 		min_free_kbytes = clamp(new_min_free_kbytes, 128, 262144);
-	else
-		pr_warn_ratelimited("min_free_kbytes is not updated to %d because user defined value %d is preferred\n",
-				    new_min_free_kbytes, user_min_free_kbytes);
-
 }
 
 int __meminit init_per_zone_wmark_min(void)
Signature · 1 line
2.45.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Nimrod Oren

min_free_kbytes is recalculated on memory hotplug and on THP sysfs writes. The new value comes from calculate_min_free_kbytes() or, while THP is enabled, from set_recommended_min_free_kbytes(). A value written to the vm.min_free_kbytes sysctl is replaced whenever the new value is higher, so a lower value does not stick. This matters most when THP is enabled on configurations with large pageblocks, where the new value can be excessive [1].

Quoted 8 lines

Stop recalculating min_free_kbytes once a value has been written to the sysctl. Systems where the sysctl is never written keep the current behavior.

reviewer Acked-by on v1 1/1
#2 lore raw
In reply to Nimrod Oren
Quoted 16 lines · Nimrod Oren

min_free_kbytes is recalculated on memory hotplug and on THP sysfs writes. The new value comes from calculate_min_free_kbytes() or, while THP is enabled, from set_recommended_min_free_kbytes(). A value written to the vm.min_free_kbytes sysctl is replaced whenever the new value is higher, so a lower value does not stick. This matters most when THP is enabled on configurations with large pageblocks, where the new value can be excessive [1].

Stop recalculating min_free_kbytes once a value has been written to the sysctl. Systems where the sysctl is never written keep the current behavior.

Quoted 6 lines
---
 Documentation/admin-guide/sysctl/vm.rst | 7 +++++++
 mm/khugepaged.c                         | 7 +------
 mm/page_alloc.c                         | 6 +-----
 3 files changed, 9 insertions(+), 11 deletions(-)

Best Regards,
Yan, Zi

maintainer
#3 lore raw
In reply to Nimrod Oren
Quoted 11 lines · On Wed, 14 Oct 2026 17:08:59 +0300 Nimrod Oren <[email protected]> wrote:

min_free_kbytes is recalculated on memory hotplug and on THP sysfs writes. The new value comes from calculate_min_free_kbytes() or, while THP is enabled, from set_recommended_min_free_kbytes(). A value written to the vm.min_free_kbytes sysctl is replaced whenever the new value is higher, so a lower value does not stick. This matters most when THP is enabled on configurations with large pageblocks, where the new value can be excessive [1].

Stop recalculating min_free_kbytes once a value has been written to the sysctl. Systems where the sysctl is never written keep the current behavior.

This problem lies somewhere in the range of "minor annoyance" to "catastrophically horrible", but it isn't clear where in that range it falls.

So can we please can we have a paragraph which explains the real-world userspace-visible runtime effects of this problem?

#4 lore raw
Quoted 14 lines · Andrew Morton
Quoted 11 lines · On Wed, 14 Oct 2026 17:08:59 +0300 Nimrod Oren <[email protected]> wrote:

min_free_kbytes is recalculated on memory hotplug and on THP sysfs writes. The new value comes from calculate_min_free_kbytes() or, while THP is enabled, from set_recommended_min_free_kbytes(). A value written to the vm.min_free_kbytes sysctl is replaced whenever the new value is higher, so a lower value does not stick. This matters most when THP is enabled on configurations with large pageblocks, where the new value can be excessive [1].

Stop recalculating min_free_kbytes once a value has been written to the sysctl. Systems where the sysctl is never written keep the current behavior.

Quoted 6 lines

This problem lies somewhere in the range of "minor annoyance" to "catastrophically horrible", but it isn't clear where in that range it falls.

So can we please can we have a paragraph which explains the real-world userspace-visible runtime effects of this problem?

Sure. Can we add the following after the first paragraph?

For example, with default arm64 pageblock sizes, the THP recommendation can reach 5.5 GiB per eligible zone with 64 KiB pages vs 22 MiB with 4 KiB pages. This keeps a significant amount of memory free, reducing what applications can use and triggering reclaim much earlier. Users can lower vm.min_free_kbytes to avoid this, but the lower value only sticks until the next recalculation.

Thanks

maintainer
#5 lore raw
Quoted 22 lines · On Thu, 15 Oct 2026 19:16:40 +0300 Nimrod Oren <[email protected]> wrote:
Quoted 14 lines · Andrew Morton
Quoted 11 lines · On Wed, 14 Oct 2026 17:08:59 +0300 Nimrod Oren <[email protected]> wrote:

min_free_kbytes is recalculated on memory hotplug and on THP sysfs writes. The new value comes from calculate_min_free_kbytes() or, while THP is enabled, from set_recommended_min_free_kbytes(). A value written to the vm.min_free_kbytes sysctl is replaced whenever the new value is higher, so a lower value does not stick. This matters most when THP is enabled on configurations with large pageblocks, where the new value can be excessive [1].

Stop recalculating min_free_kbytes once a value has been written to the sysctl. Systems where the sysctl is never written keep the current behavior.

Quoted 6 lines

This problem lies somewhere in the range of "minor annoyance" to "catastrophically horrible", but it isn't clear where in that range it falls.

So can we please can we have a paragraph which explains the real-world userspace-visible runtime effects of this problem?

Quoted 9 lines

Sure. Can we add the following after the first paragraph?

For example, with default arm64 pageblock sizes, the THP recommendation can reach 5.5 GiB per eligible zone with 64 KiB pages vs 22 MiB with 4 KiB pages. This keeps a significant amount of memory free, reducing what applications can use and triggering reclaim much earlier. Users can lower vm.min_free_kbytes to avoid this, but the lower value only sticks until the next recalculation.

Thanks.

Do you (and others) feel that we should backport this? Or is it just not applicable to real-world users of older kernels?

If we expect there to be non-zero users of 64k-page kernels on older versions then I think "yes, our users will benefit from this".

#6 lore raw
Quoted 5 lines · Andrew Morton

Do you (and others) feel that we should backport this? Or is it just not applicable to real-world users of older kernels?

If we expect there to be non-zero users of 64k-page kernels on older versions then I think "yes, our users will benefit from this".

This is a functional change, so I don't think a backport is appropriate. We add useful stuff in newer kernel versions all the time :) users who wish to benefit from them need to update their kernels.

--
Cheers, Lorenzo

maintainer Reviewed-by on v1 1/1
#7 lore raw
In reply to Nimrod Oren
Quoted 8 lines · Nimrod Oren

min_free_kbytes is recalculated on memory hotplug and on THP sysfs writes. The new value comes from calculate_min_free_kbytes() or, while THP is enabled, from set_recommended_min_free_kbytes(). A value written to the vm.min_free_kbytes sysctl is replaced whenever the new value is higher, so a lower value does not stick. This matters most when THP is enabled on configurations with large pageblocks, where the new value can be excessive [1].

Quoted 8 lines

Stop recalculating min_free_kbytes once a value has been written to the sysctl. Systems where the sysctl is never written keep the current behavior.

Thanks!

reviewer Reviewed-by on v1 1/1
#8 lore raw
In reply to Nimrod Oren
Quoted 8 lines · Nimrod Oren

min_free_kbytes is recalculated on memory hotplug and on THP sysfs writes. The new value comes from calculate_min_free_kbytes() or, while THP is enabled, from set_recommended_min_free_kbytes(). A value written to the vm.min_free_kbytes sysctl is replaced whenever the new value is higher, so a lower value does not stick. This matters most when THP is enabled on configurations with large pageblocks, where the new value can be excessive [1].

Quoted 8 lines

Stop recalculating min_free_kbytes once a value has been written to the sysctl. Systems where the sysctl is never written keep the current behavior.

With an added, focused, paragraph about user-visible effects as requested by Andrew (you can probably just reply to Andrew with something and he can fold it in for you), LGTM and:

Thanks for sticking with this! :)

Documentation/admin-guide/sysctl/vm.rst watermark[WMARK_MIN] value for each lowmem zone in the system. · line 548

Quoted 12 lines
Documentation/admin-guide/sysctl/vm.rst +7 -0
index 5b318d17aa4b..189fc57dac5f 100644
 
+By default, the kernel sets this value based on the amount of lowmem
+and recalculates it on memory hotplug.  When Transparent Hugepage (THP)
+support is enabled through global or per-size controls, the kernel may
+also raise it to help keep pageblocks free and reduce fragmentation for
+THP allocations.  Once a value is written to this file, the kernel no
+longer adjusts it automatically.
+

Ah nice to update this also!

--
Cheers, Lorenzo