Skip to contentSkip to conversation

mm, bpf: BPF based THP order selection

PATCH In review Yafang Shao 7 participants 61 messages Sep 10, 2025 – Sep 25, 2025

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

v7 cover mm, bpf: BPF based THP order selection

Background
==========

Our production servers consistently configure THP to "never" due to historical incidents caused by its behavior. Key issues include:
- Increased Memory Consumption
THP significantly raises overall memory usage, reducing available memory for workloads.

- Latency Spikes
Random latency spikes occur due to frequent memory compaction triggered by THP.

- Lack of Fine-Grained Control
THP tuning is globally configured, making it unsuitable for containerized environments. When multiple workloads share a host, enabling THP without per-workload control leads to unpredictable behavior.

Due to these issues, administrators avoid switching to madvise or always modes—unless per-workload THP control is implemented.

To address this, we propose BPF-based THP policy for flexible adjustment. Additionally, as David mentioned, this mechanism can also serve as a policy prototyping tool (test policies via BPF before upstreaming them).

Proposed Solution
=================

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:

- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The new interface for the BPF program is as follows:

/**
* @thp_get_order: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE
* if neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/

int thp_get_order(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task

**WARNING**
- This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled.
- The interface may change
- Behavior may differ in future kernel versions
- We might remove it in the future

Selftests
=========

BPF CI
------

Patch #7: Implements a basic BPF THP policy that restricts THP allocation
via khugepaged to tasks within a specified memory cgroup.
Patch #8: Provides tests for dynamic BPF program updates and replacement. Patch #9: Includes negative tests for invalid BPF helper usage, verifying
proper verification by the BPF verifier.

Currently, several dependency patches reside in mm-new but haven't been merged into bpf-next. To enable BPF CI testing, these dependencies were manually applied to bpf-next. All selftests in this series pass successfully [0].

Performance Evaluation
----------------------

Performance impact was measured given the page fault handler modifications. The standard perf bench mem memset benchmark was employed to assess page fault performance.

Testing was conducted on an AMD EPYC 7W83 64-Core Processor (single NUMA node). Due to variance between individual test runs, a script executed 10000 iterations to calculate meaningful averages.

- Baseline (without this patch series)
- With patch series but no BPF program attached
- With patch series and BPF program attached

The results across three configurations show negligible performance impact:

Number of runs: 10,000
Average throughput: 40-41 GB/sec

Production verification
-----------------------

We have successfully deployed a variant of this approach across numerous Kubernetes production servers. The implementation enables THP for specific workloads (such as applications utilizing ZGC [1]) while disabling it for others. This selective deployment has operated flawlessly, with no regression reports to date.

For ZGC-based applications, our verification demonstrates that shmem THP delivers significant improvements:
- Reduced CPU utilization
- Lower average latencies

We are continuously extending its support to more workloads, such as TCMalloc-based services. [2]

Deployment Steps in our production servers are as follows,

1. Initial Setup:
- Set THP mode to "never" (disabling THP by default).
- Attach the BPF program and pin the BPF maps and links.
- Pinning ensures persistence (like a kernel module), preventing disruption under system pressure.
- A THP whitelist map tracks allowed cgroups (initially empty -> no THP allocations).

2. Enable THP Control:
- Switch THP mode to "always" or "madvise" (BPF now governs actual allocations).

3. Dynamic Management:
- To permit THP for a cgroup, add its ID to the whitelist map.
- To revoke permission, remove the cgroup ID from the map.
- The BPF program can be updated live (policy adjustments require no task interruption).

4. To roll back, disable THP and remove this BPF program.

**WARNING**
Be aware that the maintainers do not suggest this use case, as the BPF hook interface is unstable and might be removed from the upstream kernel—unless you have your own kernel team to maintain it ;-)

Future work
===========

file-backed THP policy
----------------------

Based on our validation with production workloads, we observed mixed results with XFS large folios (also known as file-backed THP):

- Performance Benefits
Some workloads demonstrated significant improvements with XFS large folios enabled
- Performance Regression
Some workloads experienced degradation when using XFS large folios

These results demonstrate that File THP, similar to anonymous THP, requires a more granular approach instead of a uniform implementation.

We will extend the BPF-based order selection mechanism to support file-backed THP allocation policies.

Hooking fork() with BPF for Task Configuration
----------------------------------------------

The current method for controlling a newly fork()-ed task involves calling prctl() (e.g., with PR_SET_THP_DISABLE) to set flags in its mm->flags. This requires explicit userspace modification.

A more efficient alternative is to implement a new BPF hook within the fork() path. This hook would allow a BPF program to set the task's mm->flags directly after mm initialization, leveraging BPF helpers for a solution that is transparent to userspace. This is particularly valuable in data center environments for fleet-wide management.

Changes:
=======:

v6->v7:
Key Changes Implemented Based on Feedback:
From Lorenzo:
- Rename the hook from get_suggested_order() to bpf_hook_get_thp_order().
- Rename bpf_thp.c to huge_memory_bpf.c
- Focuse the current patchset on THP order selection
- Add the BPF hook into thp_vma_allowable_orders()
- Make the hook VMA-based and remove the mm parameter
- Modify the BPF program to return a single order
- Stop passing vma_flags directly to BPF programs
- Mark vma->vm_mm as trusted_or_null
- Change the MAINTAINER file
From Andrii:
- Mark mm->owner as rcu_or_null to avoid introducing new helpers
From Barry:
- decouple swap from the normal page fault path
kernel test robot:
- Fix a sparse warning
Shakeel helped clarify the implementation.

RFC v5-> v6: https://lwn.net/Articles/1035116/
- Code improvement around the RCU usage (Usama)
- Add selftests for khugepaged fork (Usama)
- Add performance data for page fault (Usama)
- Remove the RFC tag

RFC v4->v5: https://lwn.net/Articles/1034265/
- Add support for vma (David)
- Add mTHP support in khugepaged (Zi)
- Use bitmask of all allowed orders instead (Zi)
- Retrieve the page size and PMD order rather than hardcoding them (Zi)

RFC v3->v4: https://lwn.net/Articles/1031829/
- Use a new interface get_suggested_order() (David)
- Mark it as experimental (David, Lorenzo)
- Code improvement in THP (Usama)
- Code improvement in BPF struct ops (Amery)

RFC v2->v3: https://lwn.net/Articles/1024545/
- Finer-graind tuning based on madvise or always mode (David, Lorenzo)
- Use BPF to write more advanced policies logic (David, Lorenzo)

RFC v1->v2: https://lwn.net/Articles/1021783/
The main changes are as follows,
- Use struct_ops instead of fmod_ret (Alexei)
- Introduce a new THP mode (Johannes)
- Introduce new helpers for BPF hook (Zi)
- Refine the commit log

RFC v1: https://lwn.net/Articles/1019290/

Yafang Shao (10):
mm: thp: remove disabled task from khugepaged_mm_slot
mm: thp: add support for BPF based THP order selection
mm: thp: decouple THP allocation between swap and page fault paths
mm: thp: enable THP allocation exclusively through khugepaged
bpf: mark mm->owner as __safe_rcu_or_null
bpf: mark vma->vm_mm as __safe_trusted_or_null selftests/bpf: add a simple BPF based THP policy selftests/bpf: add test case to update THP policy selftests/bpf: add test cases for invalid thp_adjust usage
Documentation: add BPF-based THP policy management

 Documentation/admin-guide/mm/transhuge.rst    |  46 +++
 MAINTAINERS                                   |   3 +
 include/linux/huge_mm.h                       |  29 +-
 include/linux/khugepaged.h                    |   1 +
 kernel/bpf/verifier.c                         |   8 +
 kernel/sys.c                                  |   6 +
 mm/Kconfig                                    |  12 +
 mm/Makefile                                   |   1 +
 mm/huge_memory.c                              |   3 +-
 mm/huge_memory_bpf.c                          | 243 +++++++++++++++
 mm/khugepaged.c                               |  19 +-
 mm/memory.c                                   |  15 +-
 tools/testing/selftests/bpf/config            |   3 +
 .../selftests/bpf/prog_tests/thp_adjust.c     | 284 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/lsm.c       |   8 +-
 .../selftests/bpf/progs/test_thp_adjust.c     | 114 +++++++
 .../bpf/progs/test_thp_adjust_sleepable.c     |  22 ++
 .../bpf/progs/test_thp_adjust_trusted_owner.c |  30 ++
 .../bpf/progs/test_thp_adjust_trusted_vma.c   |  27 ++
 19 files changed, 849 insertions(+), 25 deletions(-)
 create mode 100644 mm/huge_memory_bpf.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/thp_adjust.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_sleepable.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_owner.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_vma.c
Signature · 1 line
2.47.3
#1 lore raw

PATCH v7 1/10 mm: thp: remove disabled task from khugepaged_mm_slot

Since a task with MMF_DISABLE_THP_COMPLETELY cannot use THP, remove it from the khugepaged_mm_slot to stop khugepaged from processing it.

After this change, the following semantic relationship always holds:

MMF_VM_HUGEPAGE is set == task is in khugepaged mm_slot
MMF_VM_HUGEPAGE is not set == task is not in khugepaged mm_slot

---
 include/linux/khugepaged.h |  1 +
 kernel/sys.c               |  6 ++++++
 mm/khugepaged.c            | 19 +++++++++----------
 3 files changed, 16 insertions(+), 10 deletions(-)
include/linux/khugepaged.h +1 -0
index eb1946a70cff..6cb9107f1006 100644
@@ -19,6 +19,7 @@ extern void khugepaged_min_free_kbytes_update(void);
 extern bool current_is_khugepaged(void);
 extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 				   bool install_pmd);
+bool hugepage_pmd_enabled(void);
 
 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {
kernel/sys.c +6 -0
index a46d9b75880b..a1c1e8007f2d 100644
@@ -8,6 +8,7 @@
 #include <linux/export.h>
 #include <linux/mm.h>
 #include <linux/mm_inline.h>
+#include <linux/khugepaged.h>
 #include <linux/utsname.h>
 #include <linux/mman.h>
 #include <linux/reboot.h>
@@ -2493,6 +2494,11 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
 		mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
 		mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
 	}
+
+	if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
+	    !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
+	    hugepage_pmd_enabled())
+		__khugepaged_enter(mm);
 	mmap_write_unlock(current->mm);
 	return 0;
 }
mm/khugepaged.c +9 -10
index 4ec324a4c1fe..88ac482fb3a0 100644
@@ -413,7 +413,7 @@ static inline int hpage_collapse_test_exit_or_disable(struct mm_struct *mm)
 		mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm);
 }
 
-static bool hugepage_pmd_enabled(void)
+bool hugepage_pmd_enabled(void)
 {
 	/*
 	 * We cover the anon, shmem and the file-backed case here; file-backed
@@ -445,6 +445,7 @@ void __khugepaged_enter(struct mm_struct *mm)
 
 	/* __khugepaged_exit() must not run from under us */
 	VM_BUG_ON_MM(hpage_collapse_test_exit(mm), mm);
+	WARN_ON_ONCE(mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm));
 	if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm)))
 		return;
 
@@ -472,7 +473,8 @@ void __khugepaged_enter(struct mm_struct *mm)
 void khugepaged_enter_vma(struct vm_area_struct *vma,
 			  vm_flags_t vm_flags)
 {
-	if (!mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
+	if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, vma->vm_mm) &&
+	    !mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
 	    hugepage_pmd_enabled()) {
 		if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
 			__khugepaged_enter(vma->vm_mm);
@@ -1451,16 +1453,13 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
 
 	lockdep_assert_held(&khugepaged_mm_lock);
 
-	if (hpage_collapse_test_exit(mm)) {
+	if (hpage_collapse_test_exit_or_disable(mm)) {
 		/* free mm_slot */
 		hash_del(&slot->hash);
 		list_del(&slot->mm_node);
 
-		/*
-		 * Not strictly needed because the mm exited already.
-		 *
-		 * mm_flags_clear(MMF_VM_HUGEPAGE, mm);
-		 */
+		/* If the mm is disabled, this flag must be cleared. */
+		mm_flags_clear(MMF_VM_HUGEPAGE, mm);
 
 		/* khugepaged_mm_lock actually not necessary for the below */
 		mm_slot_free(mm_slot_cache, mm_slot);
@@ -2507,9 +2506,9 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 	VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
 	/*
 	 * Release the current mm_slot if this mm is about to die, or
-	 * if we scanned all vmas of this mm.
+	 * if we scanned all vmas of this mm, or if this mm is disabled.
 	 */
-	if (hpage_collapse_test_exit(mm) || !vma) {
+	if (hpage_collapse_test_exit_or_disable(mm) || !vma) {
 		/*
 		 * Make sure that if mm_users is reaching zero while
 		 * khugepaged runs here, khugepaged_exit will find
Signature · 1 line
2.47.3
reviewer
#2 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

Hey Yafang,

Quoted 41 lines · Yafang Shao

Since a task with MMF_DISABLE_THP_COMPLETELY cannot use THP, remove it from the khugepaged_mm_slot to stop khugepaged from processing it.

After this change, the following semantic relationship always holds:

MMF_VM_HUGEPAGE is set == task is in khugepaged mm_slot
MMF_VM_HUGEPAGE is not set == task is not in khugepaged mm_slot

---
  include/linux/khugepaged.h |  1 +
  kernel/sys.c               |  6 ++++++
  mm/khugepaged.c            | 19 +++++++++----------
  3 files changed, 16 insertions(+), 10 deletions(-)
include/linux/khugepaged.h +1 -0
index eb1946a70cff..6cb9107f1006 100644
@@ -19,6 +19,7 @@ extern void khugepaged_min_free_kbytes_update(void);
  extern bool current_is_khugepaged(void);
  extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
  				   bool install_pmd);
+bool hugepage_pmd_enabled(void);
 
  static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
  {
kernel/sys.c +1 -0
index a46d9b75880b..a1c1e8007f2d 100644
@@ -8,6 +8,7 @@
  #include <linux/export.h>
  #include <linux/mm.h>
  #include <linux/mm_inline.h>
+#include <linux/khugepaged.h>
  #include <linux/utsname.h>
  #include <linux/mman.h>
  #include <linux/reboot.h>

kernel/sys.c static int prctl_set_thp_disable(bool thp_disable, unsigned long flags, · line 2502

Quoted 9 lines
kernel/sys.c +5 -0
@@ -2493,6 +2494,11 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
  		mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
  		mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
  	}
+
+	if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
+	    !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
+	    hugepage_pmd_enabled())
+		__khugepaged_enter(mm);
  	mmap_write_unlock(current->mm);

One minor style suggestion for prctl_set_thp_disable():

static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
unsigned long arg4, unsigned long arg5)

{
	struct mm_struct *mm = current->mm;

	[...]
	if (mmap_write_lock_killable(current->mm))
		return -EINTR;
	[...]
	mmap_write_unlock(current->mm);
	return 0;
}

It initializes struct mm_struct *mm = current->mm; at the beginning, but then uses both mm and current->mm. Could you change the calls using current->mm to use the local mm variable for consistency? Just a nit ;)

Cheers,
Lance

Quoted 65 lines
kernel/sys.c +0 -0
  	return 0;
  }
mm/khugepaged.c +9 -10
index 4ec324a4c1fe..88ac482fb3a0 100644
@@ -413,7 +413,7 @@ static inline int hpage_collapse_test_exit_or_disable(struct mm_struct *mm)
  		mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm);
  }
 
-static bool hugepage_pmd_enabled(void)
+bool hugepage_pmd_enabled(void)
  {
  	/*
  	 * We cover the anon, shmem and the file-backed case here; file-backed
@@ -445,6 +445,7 @@ void __khugepaged_enter(struct mm_struct *mm)
 
  	/* __khugepaged_exit() must not run from under us */
  	VM_BUG_ON_MM(hpage_collapse_test_exit(mm), mm);
+	WARN_ON_ONCE(mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm));
  	if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm)))
  		return;
 
@@ -472,7 +473,8 @@ void __khugepaged_enter(struct mm_struct *mm)
  void khugepaged_enter_vma(struct vm_area_struct *vma,
  			  vm_flags_t vm_flags)
  {
-	if (!mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
+	if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, vma->vm_mm) &&
+	    !mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
  	    hugepage_pmd_enabled()) {
  		if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
  			__khugepaged_enter(vma->vm_mm);
@@ -1451,16 +1453,13 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
 
  	lockdep_assert_held(&khugepaged_mm_lock);
 
-	if (hpage_collapse_test_exit(mm)) {
+	if (hpage_collapse_test_exit_or_disable(mm)) {
  		/* free mm_slot */
  		hash_del(&slot->hash);
  		list_del(&slot->mm_node);
 
-		/*
-		 * Not strictly needed because the mm exited already.
-		 *
-		 * mm_flags_clear(MMF_VM_HUGEPAGE, mm);
-		 */
+		/* If the mm is disabled, this flag must be cleared. */
+		mm_flags_clear(MMF_VM_HUGEPAGE, mm);
 
  		/* khugepaged_mm_lock actually not necessary for the below */
  		mm_slot_free(mm_slot_cache, mm_slot);
@@ -2507,9 +2506,9 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
  	VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
  	/*
  	 * Release the current mm_slot if this mm is about to die, or
-	 * if we scanned all vmas of this mm.
+	 * if we scanned all vmas of this mm, or if this mm is disabled.
  	 */
-	if (hpage_collapse_test_exit(mm) || !vma) {
+	if (hpage_collapse_test_exit_or_disable(mm) || !vma) {
  		/*
  		 * Make sure that if mm_users is reaching zero while
  		 * khugepaged runs here, khugepaged_exit will find
#3 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

Quoted 70 lines · Lance Yang

Hey Yafang,

Quoted 41 lines · Yafang Shao

Since a task with MMF_DISABLE_THP_COMPLETELY cannot use THP, remove it from the khugepaged_mm_slot to stop khugepaged from processing it.

After this change, the following semantic relationship always holds:

MMF_VM_HUGEPAGE is set == task is in khugepaged mm_slot
MMF_VM_HUGEPAGE is not set == task is not in khugepaged mm_slot

---
  include/linux/khugepaged.h |  1 +
  kernel/sys.c               |  6 ++++++
  mm/khugepaged.c            | 19 +++++++++----------
  3 files changed, 16 insertions(+), 10 deletions(-)
include/linux/khugepaged.h +1 -0
index eb1946a70cff..6cb9107f1006 100644
@@ -19,6 +19,7 @@ extern void khugepaged_min_free_kbytes_update(void);
  extern bool current_is_khugepaged(void);
  extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
                                 bool install_pmd);
+bool hugepage_pmd_enabled(void);
 
  static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
  {
kernel/sys.c +1 -0
index a46d9b75880b..a1c1e8007f2d 100644
@@ -8,6 +8,7 @@
  #include <linux/export.h>
  #include <linux/mm.h>
  #include <linux/mm_inline.h>
+#include <linux/khugepaged.h>
  #include <linux/utsname.h>
  #include <linux/mman.h>
  #include <linux/reboot.h>

kernel/sys.c static int prctl_set_thp_disable(bool thp_disable, unsigned long flags, · line 2502

Quoted 9 lines
kernel/sys.c +5 -0
@@ -2493,6 +2494,11 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
              mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
              mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
      }
+
+     if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
+         !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
+         hugepage_pmd_enabled())
+             __khugepaged_enter(mm);
      mmap_write_unlock(current->mm);

One minor style suggestion for prctl_set_thp_disable():

static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
unsigned long arg4, unsigned long arg5)

{
        struct mm_struct *mm = current->mm;

        [...]
        if (mmap_write_lock_killable(current->mm))
                return -EINTR;
        [...]
        mmap_write_unlock(current->mm);
        return 0;
}
Quoted 3 lines

It initializes struct mm_struct *mm = current->mm; at the beginning, but then uses both mm and current->mm. Could you change the calls using current->mm to use the local mm variable for consistency? Just a nit ;)

Nice catch

Hello Andrew, David,

The original commit "prctl: extend PR_SET_THP_DISABLE to optionally exclude VM_HUGEPAGE" is still in mm-new branch. The change below is a minor cleanup for it.

Could we please fold this change directly into the original commit to keep the history clean?

kernel/sys.c +0 -0
index a46d9b7..2250a32 100644
@@ -2479,7 +2479,7 @@ static int prctl_set_thp_disable(bool

thp_disable, unsigned long flags,

        /* Flags are only allowed when disabling. */
        if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
                return -EINVAL;
-       if (mmap_write_lock_killable(current->mm))
+       if (mmap_write_lock_killable(mm))
                return -EINTR;
        if (thp_disable) {
                if (flags & PR_THP_DISABLE_EXCEPT_ADVISED) {
@@ -2493,7 +2493,7 @@ static int prctl_set_thp_disable(bool
thp_disable, unsigned long flags,
                mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
                mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
        }
-       mmap_write_unlock(current->mm);
+       mmap_write_unlock(mm);
        return 0;

}

Signature · 2 lines
Regards
Yafang
reviewer Acked-by on v7 1/10
#4 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

In reply to Yafang Shao
Quoted 9 lines · Yafang Shao

Since a task with MMF_DISABLE_THP_COMPLETELY cannot use THP, remove it from the khugepaged_mm_slot to stop khugepaged from processing it.

After this change, the following semantic relationship always holds:

MMF_VM_HUGEPAGE is set == task is in khugepaged mm_slot
MMF_VM_HUGEPAGE is not set == task is not in khugepaged mm_slot

Cheers,
Lance

#5 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

In reply to Yafang Shao
kernel test robot noticed the following build errors:

Hi Yafang,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/mm-thp-remove-disabled-task-from-khugepaged_mm_slot/20250910-144850
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250910024447.64788-2-laoar.shao%40gmail.com
patch subject: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20250911/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250911/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> kernel/sys.c:2500:6: error: call to undeclared function 'hugepage_pmd_enabled'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    2500 |             hugepage_pmd_enabled())
         |             ^
>> kernel/sys.c:2501:3: error: call to undeclared function '__khugepaged_enter'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    2501 |                 __khugepaged_enter(mm);
         |                 ^

2 errors generated.

vim +/hugepage_pmd_enabled +2500 kernel/sys.c

2471
2472 static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
2473 unsigned long arg4, unsigned long arg5)
2474 {
2475 struct mm_struct *mm = current->mm;
2476
2477 if (arg4 || arg5)
2478 return -EINVAL;
2479
2480 /* Flags are only allowed when disabling. */
2481 if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
2482 return -EINVAL;
2483 if (mmap_write_lock_killable(current->mm))
2484 return -EINTR;
2485 if (thp_disable) {
2486 if (flags & PR_THP_DISABLE_EXCEPT_ADVISED) {
2487 mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
2488 mm_flags_set(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
2489 } else {
2490 mm_flags_set(MMF_DISABLE_THP_COMPLETELY, mm);
2491 mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
2492 }
2493 } else {
2494 mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
2495 mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
2496 }
2497
2498 if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
2499 !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&

> 2500		    hugepage_pmd_enabled())
> 2501			__khugepaged_enter(mm);

2502 mmap_write_unlock(current->mm);
2503 return 0;
2504 }
2505

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
reviewer
#6 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

Hi Yafang,

Quoted 21 lines · kernel test robot

Hi Yafang,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/mm-thp-remove-disabled-task-from-khugepaged_mm_slot/20250910-144850
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250910024447.64788-2-laoar.shao%40gmail.com
patch subject: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20250911/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250911/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

Quoted 7 lines
Quoted 1 line
Quoted 1 line

kernel/sys.c:2500:6: error: call to undeclared function 'hugepage_pmd_enabled'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

     2500 |             hugepage_pmd_enabled())
          |             ^
Quoted 1 line
Quoted 1 line

kernel/sys.c:2501:3: error: call to undeclared function '__khugepaged_enter'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

     2501 |                 __khugepaged_enter(mm);
          |                 ^

2 errors generated.

Oops, seems like hugepage_pmd_enabled() and __khugepaged_enter() are only available when CONFIG_TRANSPARENT_HUGEPAGE is enabled ;)

Quoted 33 lines

vim +/hugepage_pmd_enabled +2500 kernel/sys.c

   2471	
   2472	static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
   2473					 unsigned long arg4, unsigned long arg5)
   2474	{
   2475		struct mm_struct *mm = current->mm;
   2476	
   2477		if (arg4 || arg5)
   2478			return -EINVAL;
   2479	
   2480		/* Flags are only allowed when disabling. */
   2481		if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
   2482			return -EINVAL;
   2483		if (mmap_write_lock_killable(current->mm))
   2484			return -EINTR;
   2485		if (thp_disable) {
   2486			if (flags & PR_THP_DISABLE_EXCEPT_ADVISED) {
   2487				mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
   2488				mm_flags_set(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2489			} else {
   2490				mm_flags_set(MMF_DISABLE_THP_COMPLETELY, mm);
   2491				mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2492			}
   2493		} else {
   2494			mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
   2495			mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2496		}
   2497	
   2498		if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
   2499		    !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
Quoted 6 lines
Quoted 2 lines

2500 hugepage_pmd_enabled())
2501 __khugepaged_enter(mm);

   2502		mmap_write_unlock(current->mm);
   2503		return 0;
   2504	}
   2505	

So, let's wrap the new logic in an #ifdef CONFIG_TRANSPARENT_HUGEPAGE block.

kernel/sys.c +3 -0
index a1c1e8007f2d..c8600e017933 100644
@@ -2495,10 +2495,13 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
                mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
        }
 
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
        if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
            !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
            hugepage_pmd_enabled())
                __khugepaged_enter(mm);
+#endif
+
        mmap_write_unlock(current->mm);
        return 0;
 }

Cheers,
Lance

reviewer
#7 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

Quoted 85 lines · Lance Yang

Hi Yafang,

Quoted 21 lines · kernel test robot

Hi Yafang,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/mm-thp-remove-disabled-task-from-khugepaged_mm_slot/20250910-144850
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250910024447.64788-2-laoar.shao%40gmail.com
patch subject: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20250911/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250911/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

Quoted 7 lines
Quoted 1 line
Quoted 1 line

kernel/sys.c:2500:6: error: call to undeclared function 'hugepage_pmd_enabled'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

     2500 |             hugepage_pmd_enabled())
          |             ^
Quoted 1 line
Quoted 1 line

kernel/sys.c:2501:3: error: call to undeclared function '__khugepaged_enter'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

     2501 |                 __khugepaged_enter(mm);
          |                 ^

2 errors generated.

Oops, seems like hugepage_pmd_enabled() and __khugepaged_enter() are only available when CONFIG_TRANSPARENT_HUGEPAGE is enabled ;)

Quoted 33 lines

vim +/hugepage_pmd_enabled +2500 kernel/sys.c

   2471	
   2472	static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
   2473					 unsigned long arg4, unsigned long arg5)
   2474	{
   2475		struct mm_struct *mm = current->mm;
   2476	
   2477		if (arg4 || arg5)
   2478			return -EINVAL;
   2479	
   2480		/* Flags are only allowed when disabling. */
   2481		if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
   2482			return -EINVAL;
   2483		if (mmap_write_lock_killable(current->mm))
   2484			return -EINTR;
   2485		if (thp_disable) {
   2486			if (flags & PR_THP_DISABLE_EXCEPT_ADVISED) {
   2487				mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
   2488				mm_flags_set(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2489			} else {
   2490				mm_flags_set(MMF_DISABLE_THP_COMPLETELY, mm);
   2491				mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2492			}
   2493		} else {
   2494			mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
   2495			mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2496		}
   2497	
   2498		if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
   2499		    !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
Quoted 6 lines
Quoted 2 lines

2500 hugepage_pmd_enabled())
2501 __khugepaged_enter(mm);

   2502		mmap_write_unlock(current->mm);
   2503		return 0;
   2504	}
   2505	

So, let's wrap the new logic in an #ifdef CONFIG_TRANSPARENT_HUGEPAGE block.

kernel/sys.c +0 -0
index a1c1e8007f2d..c8600e017933 100644
@@ -2495,10 +2495,13 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
                mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
        }
 

kernel/sys.c static int prctl_set_thp_disable(bool thp_disable, unsigned long flags, · line 2507

Quoted 10 lines
kernel/sys.c +3 -0
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
        if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
            !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
            hugepage_pmd_enabled())
                __khugepaged_enter(mm);
+#endif
+
        mmap_write_unlock(current->mm);
        return 0;
 }

Or in the header file,

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
...
#else
bool hugepage_pmd_enabled()

{
	return false;
}

int __khugepaged_enter(struct mm_struct *mm)

{
	return 0;
}

#endif

Best Regards,
Yan, Zi

#8 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

Quoted 109 lines · Zi Yan
Quoted 85 lines · Lance Yang

Hi Yafang,

Quoted 21 lines · kernel test robot

Hi Yafang,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/mm-thp-remove-disabled-task-from-khugepaged_mm_slot/20250910-144850
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250910024447.64788-2-laoar.shao%40gmail.com
patch subject: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20250911/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250911/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

Quoted 7 lines
Quoted 1 line
Quoted 1 line

kernel/sys.c:2500:6: error: call to undeclared function 'hugepage_pmd_enabled'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

     2500 |             hugepage_pmd_enabled())
          |             ^
Quoted 1 line
Quoted 1 line

kernel/sys.c:2501:3: error: call to undeclared function '__khugepaged_enter'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

     2501 |                 __khugepaged_enter(mm);
          |                 ^

2 errors generated.

Oops, seems like hugepage_pmd_enabled() and __khugepaged_enter() are only available when CONFIG_TRANSPARENT_HUGEPAGE is enabled ;)

Quoted 33 lines

vim +/hugepage_pmd_enabled +2500 kernel/sys.c

   2471
   2472      static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
   2473                                       unsigned long arg4, unsigned long arg5)
   2474      {
   2475              struct mm_struct *mm = current->mm;
   2476
   2477              if (arg4 || arg5)
   2478                      return -EINVAL;
   2479
   2480              /* Flags are only allowed when disabling. */
   2481              if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
   2482                      return -EINVAL;
   2483              if (mmap_write_lock_killable(current->mm))
   2484                      return -EINTR;
   2485              if (thp_disable) {
   2486                      if (flags & PR_THP_DISABLE_EXCEPT_ADVISED) {
   2487                              mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
   2488                              mm_flags_set(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2489                      } else {
   2490                              mm_flags_set(MMF_DISABLE_THP_COMPLETELY, mm);
   2491                              mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2492                      }
   2493              } else {
   2494                      mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
   2495                      mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2496              }
   2497
   2498              if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
   2499                  !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
Quoted 6 lines
Quoted 2 lines

2500 hugepage_pmd_enabled())
2501 __khugepaged_enter(mm);

   2502              mmap_write_unlock(current->mm);
   2503              return 0;
   2504      }
   2505

So, let's wrap the new logic in an #ifdef CONFIG_TRANSPARENT_HUGEPAGE block.

kernel/sys.c +0 -0
index a1c1e8007f2d..c8600e017933 100644
@@ -2495,10 +2495,13 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
                mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
        }
 

kernel/sys.c static int prctl_set_thp_disable(bool thp_disable, unsigned long flags, · line 2507

Quoted 10 lines
kernel/sys.c +3 -0
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
        if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
            !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
            hugepage_pmd_enabled())
                __khugepaged_enter(mm);
+#endif
+
        mmap_write_unlock(current->mm);
        return 0;
 }

Or in the header file,

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
...
#else
bool hugepage_pmd_enabled()

{
        return false;
}
Quoted 5 lines

int __khugepaged_enter(struct mm_struct *mm)

{
        return 0;
}

#endif

Thank you, both. I will address this in the next version.

Signature · 2 lines
Regards
Yafang
reviewer
#9 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

In reply to Zi Yan
Quoted 108 lines · Zi Yan
Quoted 85 lines · Lance Yang

Hi Yafang,

Quoted 21 lines · kernel test robot

Hi Yafang,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/mm-thp-remove-disabled-task-from-khugepaged_mm_slot/20250910-144850
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250910024447.64788-2-laoar.shao%40gmail.com
patch subject: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20250911/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250911/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

Quoted 7 lines
Quoted 1 line
Quoted 1 line

kernel/sys.c:2500:6: error: call to undeclared function 'hugepage_pmd_enabled'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

      2500 |             hugepage_pmd_enabled())
           |             ^
Quoted 1 line
Quoted 1 line

kernel/sys.c:2501:3: error: call to undeclared function '__khugepaged_enter'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

      2501 |                 __khugepaged_enter(mm);
           |                 ^

2 errors generated.

Oops, seems like hugepage_pmd_enabled() and __khugepaged_enter() are only available when CONFIG_TRANSPARENT_HUGEPAGE is enabled ;)

Quoted 33 lines

vim +/hugepage_pmd_enabled +2500 kernel/sys.c

    2471	
    2472	static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
    2473					 unsigned long arg4, unsigned long arg5)
    2474	{
    2475		struct mm_struct *mm = current->mm;
    2476	
    2477		if (arg4 || arg5)
    2478			return -EINVAL;
    2479	
    2480		/* Flags are only allowed when disabling. */
    2481		if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
    2482			return -EINVAL;
    2483		if (mmap_write_lock_killable(current->mm))
    2484			return -EINTR;
    2485		if (thp_disable) {
    2486			if (flags & PR_THP_DISABLE_EXCEPT_ADVISED) {
    2487				mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
    2488				mm_flags_set(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
    2489			} else {
    2490				mm_flags_set(MMF_DISABLE_THP_COMPLETELY, mm);
    2491				mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
    2492			}
    2493		} else {
    2494			mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
    2495			mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
    2496		}
    2497	
    2498		if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
    2499		    !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
Quoted 6 lines
Quoted 2 lines

2500 hugepage_pmd_enabled())
2501 __khugepaged_enter(mm);

    2502		mmap_write_unlock(current->mm);
    2503		return 0;
    2504	}
    2505	

So, let's wrap the new logic in an #ifdef CONFIG_TRANSPARENT_HUGEPAGE block.

kernel/sys.c +0 -0
index a1c1e8007f2d..c8600e017933 100644
@@ -2495,10 +2495,13 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
                 mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
         }
 

kernel/sys.c static int prctl_set_thp_disable(bool thp_disable, unsigned long flags, · line 2507

Quoted 10 lines
kernel/sys.c +3 -0
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
         if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
             !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
             hugepage_pmd_enabled())
                 __khugepaged_enter(mm);
+#endif
+
         mmap_write_unlock(current->mm);
         return 0;
  }

Or in the header file,

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
...
#else
bool hugepage_pmd_enabled()

{
	return false;
}
Quoted 5 lines

int __khugepaged_enter(struct mm_struct *mm)

{
	return 0;
}

#endif

Nice. That's a much better approach.

#10 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

In reply to Zi Yan
Quoted 104 lines · Zi Yan
Quoted 85 lines · Lance Yang

Hi Yafang,

Quoted 21 lines · kernel test robot

Hi Yafang,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/mm-thp-remove-disabled-task-from-khugepaged_mm_slot/20250910-144850
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250910024447.64788-2-laoar.shao%40gmail.com
patch subject: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20250911/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250911/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

Quoted 7 lines
Quoted 1 line
Quoted 1 line

kernel/sys.c:2500:6: error: call to undeclared function 'hugepage_pmd_enabled'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

     2500 |             hugepage_pmd_enabled())
          |             ^
Quoted 1 line
Quoted 1 line

kernel/sys.c:2501:3: error: call to undeclared function '__khugepaged_enter'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

     2501 |                 __khugepaged_enter(mm);
          |                 ^

2 errors generated.

Oops, seems like hugepage_pmd_enabled() and __khugepaged_enter() are only available when CONFIG_TRANSPARENT_HUGEPAGE is enabled ;)

Quoted 33 lines

vim +/hugepage_pmd_enabled +2500 kernel/sys.c

   2471
   2472	static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
   2473					 unsigned long arg4, unsigned long arg5)
   2474	{
   2475		struct mm_struct *mm = current->mm;
   2476
   2477		if (arg4 || arg5)
   2478			return -EINVAL;
   2479
   2480		/* Flags are only allowed when disabling. */
   2481		if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
   2482			return -EINVAL;
   2483		if (mmap_write_lock_killable(current->mm))
   2484			return -EINTR;
   2485		if (thp_disable) {
   2486			if (flags & PR_THP_DISABLE_EXCEPT_ADVISED) {
   2487				mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
   2488				mm_flags_set(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2489			} else {
   2490				mm_flags_set(MMF_DISABLE_THP_COMPLETELY, mm);
   2491				mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2492			}
   2493		} else {
   2494			mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
   2495			mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2496		}
   2497
   2498		if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
   2499		    !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
Quoted 6 lines
Quoted 2 lines

2500 hugepage_pmd_enabled())
2501 __khugepaged_enter(mm);

   2502		mmap_write_unlock(current->mm);
   2503		return 0;
   2504	}
   2505

So, let's wrap the new logic in an #ifdef CONFIG_TRANSPARENT_HUGEPAGE block.

kernel/sys.c +0 -0
index a1c1e8007f2d..c8600e017933 100644
@@ -2495,10 +2495,13 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
                mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
        }
 

kernel/sys.c static int prctl_set_thp_disable(bool thp_disable, unsigned long flags, · line 2507

Quoted 10 lines
kernel/sys.c +3 -0
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
        if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
            !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
            hugepage_pmd_enabled())
                __khugepaged_enter(mm);
+#endif
+
        mmap_write_unlock(current->mm);
        return 0;
 }

Or in the header file,

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
...
#else
bool hugepage_pmd_enabled()

Quoted 8 lines
{
	return false;
}

int __khugepaged_enter(struct mm_struct *mm)

{
	return 0;
}

It seems we have a convention of just not implementing things here if they're necessarily used in core code paths (and _with my suggested change_) it's _just_ khugepaged that's invoking them).

Anyway with my suggestion we can fix this entirely with:

#ifdef CONFIG_TRANSPARENT_HUGEPAGE

void khugepaged_enter_mm(struct mm_struct *mm);

#else

void khugepaged_enter_mm(struct mm_struct *mm)
{
}

#endif

Cheers, Lorenzo

Quoted 4 lines

#endif

Best Regards,
Yan, Zi

#11 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

Quoted 125 lines · Lorenzo Stoakes
Quoted 104 lines · Zi Yan
Quoted 85 lines · Lance Yang

Hi Yafang,

Quoted 21 lines · kernel test robot

Hi Yafang,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/mm-thp-remove-disabled-task-from-khugepaged_mm_slot/20250910-144850
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250910024447.64788-2-laoar.shao%40gmail.com
patch subject: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20250911/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250911/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

Quoted 7 lines
Quoted 1 line
Quoted 1 line

kernel/sys.c:2500:6: error: call to undeclared function 'hugepage_pmd_enabled'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

     2500 |             hugepage_pmd_enabled())
          |             ^
Quoted 1 line
Quoted 1 line

kernel/sys.c:2501:3: error: call to undeclared function '__khugepaged_enter'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

     2501 |                 __khugepaged_enter(mm);
          |                 ^

2 errors generated.

Oops, seems like hugepage_pmd_enabled() and __khugepaged_enter() are only available when CONFIG_TRANSPARENT_HUGEPAGE is enabled ;)

Quoted 33 lines

vim +/hugepage_pmd_enabled +2500 kernel/sys.c

   2471
   2472    static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
   2473                                     unsigned long arg4, unsigned long arg5)
   2474    {
   2475            struct mm_struct *mm = current->mm;
   2476
   2477            if (arg4 || arg5)
   2478                    return -EINVAL;
   2479
   2480            /* Flags are only allowed when disabling. */
   2481            if ((!thp_disable && flags) || (flags & ~PR_THP_DISABLE_EXCEPT_ADVISED))
   2482                    return -EINVAL;
   2483            if (mmap_write_lock_killable(current->mm))
   2484                    return -EINTR;
   2485            if (thp_disable) {
   2486                    if (flags & PR_THP_DISABLE_EXCEPT_ADVISED) {
   2487                            mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
   2488                            mm_flags_set(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2489                    } else {
   2490                            mm_flags_set(MMF_DISABLE_THP_COMPLETELY, mm);
   2491                            mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2492                    }
   2493            } else {
   2494                    mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
   2495                    mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
   2496            }
   2497
   2498            if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
   2499                !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
Quoted 6 lines
Quoted 2 lines

2500 hugepage_pmd_enabled())
2501 __khugepaged_enter(mm);

   2502            mmap_write_unlock(current->mm);
   2503            return 0;
   2504    }
   2505

So, let's wrap the new logic in an #ifdef CONFIG_TRANSPARENT_HUGEPAGE block.

kernel/sys.c +0 -0
index a1c1e8007f2d..c8600e017933 100644
@@ -2495,10 +2495,13 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
                mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
        }
 

kernel/sys.c static int prctl_set_thp_disable(bool thp_disable, unsigned long flags, · line 2507

Quoted 10 lines
kernel/sys.c +3 -0
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
        if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
            !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
            hugepage_pmd_enabled())
                __khugepaged_enter(mm);
+#endif
+
        mmap_write_unlock(current->mm);
        return 0;
 }

Or in the header file,

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
...
#else
bool hugepage_pmd_enabled()

Quoted 8 lines
{
      return false;
}

int __khugepaged_enter(struct mm_struct *mm)

{
      return 0;
}

It seems we have a convention of just not implementing things here if they're necessarily used in core code paths (and _with my suggested change_) it's _just_ khugepaged that's invoking them).

Anyway with my suggestion we can fix this entirely with:

#ifdef CONFIG_TRANSPARENT_HUGEPAGE

void khugepaged_enter_mm(struct mm_struct *mm);

Quoted 7 lines

#else

void khugepaged_enter_mm(struct mm_struct *mm)
{
}

#endif

ack

Thanks for all the suggestions.

Signature · 2 lines
Regards
Yafang
#12 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

In reply to Yafang Shao
Quoted 10 lines · Yafang Shao

Since a task with MMF_DISABLE_THP_COMPLETELY cannot use THP, remove it from the khugepaged_mm_slot to stop khugepaged from processing it.

After this change, the following semantic relationship always holds:

MMF_VM_HUGEPAGE is set == task is in khugepaged mm_slot
MMF_VM_HUGEPAGE is not set == task is not in khugepaged mm_slot

(Obviously on basis of fixing issue bot reported).

Quoted 11 lines
---
 include/linux/khugepaged.h |  1 +
 kernel/sys.c               |  6 ++++++
 mm/khugepaged.c            | 19 +++++++++----------
 3 files changed, 16 insertions(+), 10 deletions(-)
include/linux/khugepaged.h +0 -0
index eb1946a70cff..6cb9107f1006 100644

include/linux/khugepaged.h extern void khugepaged_min_free_kbytes_update(void); · line 22

Quoted 4 lines
include/linux/khugepaged.h +1 -0
@@ -19,6 +19,7 @@ extern void khugepaged_min_free_kbytes_update(void);
 extern bool current_is_khugepaged(void);
 extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 				   bool install_pmd);
+bool hugepage_pmd_enabled(void);

Need to provide a !CONFIG_TRANSPARENT_HUGEPAGE version, or to not invoke this in a context where CONFIG_TRANSPARENT_HUGEPAGE is specified.

Quoted 16 lines
include/linux/khugepaged.h +0 -0
 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {
kernel/sys.c +1 -0
index a46d9b75880b..a1c1e8007f2d 100644
@@ -8,6 +8,7 @@
 #include <linux/export.h>
 #include <linux/mm.h>
 #include <linux/mm_inline.h>
+#include <linux/khugepaged.h>
 #include <linux/utsname.h>
 #include <linux/mman.h>
 #include <linux/reboot.h>

kernel/sys.c static int prctl_set_thp_disable(bool thp_disable, unsigned long flags, · line 2501

Quoted 8 lines
kernel/sys.c +5 -0
@@ -2493,6 +2494,11 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
 		mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
 		mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
 	}
+
+	if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
+	    !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
+	    hugepage_pmd_enabled())
+		__khugepaged_enter(mm);

Let's refactor this so it's not open-coded.

We can have:

void khugepaged_enter_mm(struct mm_struct *mm)

{
	if (mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm))
		return;
	if (mm_flags_test(MMF_VM_HUGEPAGE, mm))
		return;
	if (!hugepage_pmd_enabled())
		return;

	__khugepaged_enter(mm);
}

void khugepaged_enter_vma(struct vm_area_struct *vma,
vm_flags_t vm_flags)

{
	if (!thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
		return;

	khugepaged_enter_mm(vma->vm_mm);
}

Then just invoke khugepaged_enter_mm() here.

Quoted 17 lines
kernel/sys.c +0 -0
 	mmap_write_unlock(current->mm);
 	return 0;
 }
mm/khugepaged.c +1 -1
index 4ec324a4c1fe..88ac482fb3a0 100644
@@ -413,7 +413,7 @@ static inline int hpage_collapse_test_exit_or_disable(struct mm_struct *mm)
 		mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm);
 }
 
-static bool hugepage_pmd_enabled(void)
+bool hugepage_pmd_enabled(void)
 {
 	/*
 	 * We cover the anon, shmem and the file-backed case here; file-backed

mm/khugepaged.c void __khugepaged_enter(struct mm_struct *mm) · line 448

Quoted 4 lines
mm/khugepaged.c +1 -0
@@ -445,6 +445,7 @@ void __khugepaged_enter(struct mm_struct *mm)
 
 	/* __khugepaged_exit() must not run from under us */
 	VM_BUG_ON_MM(hpage_collapse_test_exit(mm), mm);
+	WARN_ON_ONCE(mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm));

Not sure why this needs to be a naked WARN_ON_ONCE()? Seems that'd be a programmatic eror, so VM_WARN_ON_ONCE() more appropriate?

Can also change the VM_BUG_ON_MM() to VM_WARN_ON_ONCE_MM() while we're here.

mm/khugepaged.c void __khugepaged_enter(struct mm_struct *mm) · line 480

Quoted 13 lines
mm/khugepaged.c +2 -1
 	if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm)))
 		return;
 
@@ -472,7 +473,8 @@ void __khugepaged_enter(struct mm_struct *mm)
 void khugepaged_enter_vma(struct vm_area_struct *vma,
 			  vm_flags_t vm_flags)
 {
-	if (!mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
+	if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, vma->vm_mm) &&
+	    !mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
 	    hugepage_pmd_enabled()) {
 		if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
 			__khugepaged_enter(vma->vm_mm);

See above, we can refactor this.

Quoted 22 lines
mm/khugepaged.c +3 -6
@@ -1451,16 +1453,13 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
 
 	lockdep_assert_held(&khugepaged_mm_lock);
 
-	if (hpage_collapse_test_exit(mm)) {
+	if (hpage_collapse_test_exit_or_disable(mm)) {
 		/* free mm_slot */
 		hash_del(&slot->hash);
 		list_del(&slot->mm_node);
 
-		/*
-		 * Not strictly needed because the mm exited already.
-		 *
-		 * mm_flags_clear(MMF_VM_HUGEPAGE, mm);
-		 */
+		/* If the mm is disabled, this flag must be cleared. */
+		mm_flags_clear(MMF_VM_HUGEPAGE, mm);
 
 		/* khugepaged_mm_lock actually not necessary for the below */
 		mm_slot_free(mm_slot_cache, mm_slot);
@@ -2507,9 +2506,9 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 	VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);

mm/khugepaged.c static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, · line 2514

Quoted 10 lines
mm/khugepaged.c +2 -2
 	/*
 	 * Release the current mm_slot if this mm is about to die, or
-	 * if we scanned all vmas of this mm.
+	 * if we scanned all vmas of this mm, or if this mm is disabled.
 	 */
-	if (hpage_collapse_test_exit(mm) || !vma) {
+	if (hpage_collapse_test_exit_or_disable(mm) || !vma) {
 		/*
 		 * Make sure that if mm_users is reaching zero while
 		 * khugepaged runs here, khugepaged_exit will find

Seems reasonable, but makes me wonder if we actually always want to invoke hpage_collapse_test_exit_or_disable()?

I guess the VM_BUG_ON() (though it should be a VM_WARN_ON_ONCE()) in __khugepaged_enter() is a legit use, but the only other case is retract_page_tables().

I wonder if we should change this also? Seems reasonable to.

Quoted 3 lines

--
2.47.3

#13 lore raw

Re: [PATCH v7 mm-new 01/10] mm: thp: remove disabled task from khugepaged_mm_slot

Quoted 77 lines · Lorenzo Stoakes
Quoted 10 lines · Yafang Shao

Since a task with MMF_DISABLE_THP_COMPLETELY cannot use THP, remove it from the khugepaged_mm_slot to stop khugepaged from processing it.

After this change, the following semantic relationship always holds:

MMF_VM_HUGEPAGE is set == task is in khugepaged mm_slot
MMF_VM_HUGEPAGE is not set == task is not in khugepaged mm_slot

(Obviously on basis of fixing issue bot reported).

Quoted 11 lines
---
 include/linux/khugepaged.h |  1 +
 kernel/sys.c               |  6 ++++++
 mm/khugepaged.c            | 19 +++++++++----------
 3 files changed, 16 insertions(+), 10 deletions(-)
include/linux/khugepaged.h +0 -0
index eb1946a70cff..6cb9107f1006 100644

include/linux/khugepaged.h extern void khugepaged_min_free_kbytes_update(void); · line 22

Quoted 4 lines
include/linux/khugepaged.h +1 -0
@@ -19,6 +19,7 @@ extern void khugepaged_min_free_kbytes_update(void);
 extern bool current_is_khugepaged(void);
 extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
                                 bool install_pmd);
+bool hugepage_pmd_enabled(void);

Need to provide a !CONFIG_TRANSPARENT_HUGEPAGE version, or to not invoke this in a context where CONFIG_TRANSPARENT_HUGEPAGE is specified.

Quoted 16 lines
include/linux/khugepaged.h +0 -0
 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {
kernel/sys.c +1 -0
index a46d9b75880b..a1c1e8007f2d 100644
@@ -8,6 +8,7 @@
 #include <linux/export.h>
 #include <linux/mm.h>
 #include <linux/mm_inline.h>
+#include <linux/khugepaged.h>
 #include <linux/utsname.h>
 #include <linux/mman.h>
 #include <linux/reboot.h>

kernel/sys.c static int prctl_set_thp_disable(bool thp_disable, unsigned long flags, · line 2501

Quoted 8 lines
kernel/sys.c +5 -0
@@ -2493,6 +2494,11 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
              mm_flags_clear(MMF_DISABLE_THP_COMPLETELY, mm);
              mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, mm);
      }
+
+     if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) &&
+         !mm_flags_test(MMF_VM_HUGEPAGE, mm) &&
+         hugepage_pmd_enabled())
+             __khugepaged_enter(mm);

Let's refactor this so it's not open-coded.

We can have:

void khugepaged_enter_mm(struct mm_struct *mm)

{
        if (mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm))
                return;
        if (mm_flags_test(MMF_VM_HUGEPAGE, mm))
                return;
        if (!hugepage_pmd_enabled())
                return;

        __khugepaged_enter(mm);
}

void khugepaged_enter_vma(struct vm_area_struct *vma,
vm_flags_t vm_flags)

Quoted 8 lines
{
        if (!thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
                return;

        khugepaged_enter_mm(vma->vm_mm);
}

Then just invoke khugepaged_enter_mm() here.

That's better.

Quoted 20 lines
Quoted 20 lines
kernel/sys.c +0 -0
      mmap_write_unlock(current->mm);
      return 0;
 }
mm/khugepaged.c +1 -1
index 4ec324a4c1fe..88ac482fb3a0 100644
@@ -413,7 +413,7 @@ static inline int hpage_collapse_test_exit_or_disable(struct mm_struct *mm)
              mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm);
 }
 
-static bool hugepage_pmd_enabled(void)
+bool hugepage_pmd_enabled(void)
 {
      /*
       * We cover the anon, shmem and the file-backed case here; file-backed
@@ -445,6 +445,7 @@ void __khugepaged_enter(struct mm_struct *mm)
 
Quoted 6 lines

mm/khugepaged.c void __khugepaged_enter(struct mm_struct *mm) · line 448

Quoted 3 lines
mm/khugepaged.c +1 -0
      /* __khugepaged_exit() must not run from under us */
      VM_BUG_ON_MM(hpage_collapse_test_exit(mm), mm);
+     WARN_ON_ONCE(mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm));

Not sure why this needs to be a naked WARN_ON_ONCE()? Seems that'd be a programmatic eror, so VM_WARN_ON_ONCE() more appropriate?

ack

Quoted 2 lines

Can also change the VM_BUG_ON_MM() to VM_WARN_ON_ONCE_MM() while we're here.

ack

Quoted 4 lines
Quoted 4 lines
mm/khugepaged.c +0 -0
      if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm)))
              return;
 
Quoted 12 lines

mm/khugepaged.c void __khugepaged_enter(struct mm_struct *mm) · line 480

Quoted 10 lines
mm/khugepaged.c +2 -1
@@ -472,7 +473,8 @@ void __khugepaged_enter(struct mm_struct *mm)
 void khugepaged_enter_vma(struct vm_area_struct *vma,
                        vm_flags_t vm_flags)
 {
-     if (!mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
+     if (!mm_flags_test(MMF_DISABLE_THP_COMPLETELY, vma->vm_mm) &&
+         !mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
          hugepage_pmd_enabled()) {
              if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
                      __khugepaged_enter(vma->vm_mm);

See above, we can refactor this.

ack

Quoted 37 lines
Quoted 23 lines
mm/khugepaged.c +3 -6
@@ -1451,16 +1453,13 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
 
      lockdep_assert_held(&khugepaged_mm_lock);
 
-     if (hpage_collapse_test_exit(mm)) {
+     if (hpage_collapse_test_exit_or_disable(mm)) {
              /* free mm_slot */
              hash_del(&slot->hash);
              list_del(&slot->mm_node);
 
-             /*
-              * Not strictly needed because the mm exited already.
-              *
-              * mm_flags_clear(MMF_VM_HUGEPAGE, mm);
-              */
+             /* If the mm is disabled, this flag must be cleared. */
+             mm_flags_clear(MMF_VM_HUGEPAGE, mm);
 
              /* khugepaged_mm_lock actually not necessary for the below */
              mm_slot_free(mm_slot_cache, mm_slot);
@@ -2507,9 +2506,9 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
      VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);

mm/khugepaged.c static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result, · line 2514

Quoted 10 lines
mm/khugepaged.c +2 -2
      /*
       * Release the current mm_slot if this mm is about to die, or
-      * if we scanned all vmas of this mm.
+      * if we scanned all vmas of this mm, or if this mm is disabled.
       */
-     if (hpage_collapse_test_exit(mm) || !vma) {
+     if (hpage_collapse_test_exit_or_disable(mm) || !vma) {
              /*
               * Make sure that if mm_users is reaching zero while
               * khugepaged runs here, khugepaged_exit will find

Seems reasonable, but makes me wonder if we actually always want to invoke hpage_collapse_test_exit_or_disable()?

Quoted 5 lines

I guess the VM_BUG_ON() (though it should be a VM_WARN_ON_ONCE()) in __khugepaged_enter() is a legit use, but the only other case is retract_page_tables().

I wonder if we should change this also? Seems reasonable to.

Right, we can change it also. Thanks for your suggestion.

Signature · 2 lines
Regards
Yafang
#14 lore raw

PATCH v7 2/10 mm: thp: add support for BPF based THP order selection

In reply to Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

---
 MAINTAINERS             |   1 +
 include/linux/huge_mm.h |  26 ++++-
 mm/Kconfig              |  12 ++
 mm/Makefile             |   1 +
 mm/huge_memory_bpf.c    | 243 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 280 insertions(+), 3 deletions(-)
 create mode 100644 mm/huge_memory_bpf.c
MAINTAINERS +1 -0
index 8fef05bc2224..d055a3c95300 100644
@@ -16252,6 +16252,7 @@ F:	include/linux/huge_mm.h
 F:	include/linux/khugepaged.h
 F:	include/trace/events/huge_memory.h
 F:	mm/huge_memory.c
+F:	mm/huge_memory_bpf.c
 F:	mm/khugepaged.c
 F:	mm/mm_slot.h
 F:	tools/testing/selftests/mm/khugepaged.c
include/linux/huge_mm.h +23 -3
index 23f124493c47..f72a5fd04e4f 100644
@@ -56,6 +56,7 @@ enum transparent_hugepage_flag {
 	TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
 	TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
 	TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
+	TRANSPARENT_HUGEPAGE_BPF_ATTACHED,      /* BPF prog is attached */
 };
 
 struct kobject;
@@ -270,6 +271,19 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 					 enum tva_type type,
 					 unsigned long orders);
 
+#ifdef CONFIG_BPF_GET_THP_ORDER
+unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+			enum tva_type type, unsigned long orders);
+#else
+static inline unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+			enum tva_type tva_flags, unsigned long orders)
+{
+	return orders;
+}
+#endif
+
 /**
  * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma
  * @vma:  the vm area to check
@@ -291,6 +305,12 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
 				       enum tva_type type,
 				       unsigned long orders)
 {
+	unsigned long bpf_orders;
+
+	bpf_orders = bpf_hook_thp_get_orders(vma, vm_flags, type, orders);
+	if (!bpf_orders)
+		return 0;
+
 	/*
 	 * Optimization to check if required orders are enabled early. Only
 	 * forced collapse ignores sysfs configs.
@@ -304,12 +324,12 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
 		    ((vm_flags & VM_HUGEPAGE) && hugepage_global_enabled()))
 			mask |= READ_ONCE(huge_anon_orders_inherit);
 
-		orders &= mask;
-		if (!orders)
+		bpf_orders &= mask;
+		if (!bpf_orders)
 			return 0;
 	}
 
-	return __thp_vma_allowable_orders(vma, vm_flags, type, orders);
+	return __thp_vma_allowable_orders(vma, vm_flags, type, bpf_orders);
 }
 
 struct thpsize {
mm/Kconfig +12 -0
index d1ed839ca710..4d89d2158f10 100644
@@ -896,6 +896,18 @@ config NO_PAGE_MAPCOUNT
 
 	  EXPERIMENTAL because the impact of some changes is still unclear.
 
+config BPF_GET_THP_ORDER
+	bool "BPF-based THP order selection (EXPERIMENTAL)"
+	depends on TRANSPARENT_HUGEPAGE && BPF_SYSCALL
+
+	help
+	  Enable dynamic THP order selection using BPF programs. This
+	  experimental feature allows custom BPF logic to determine optimal
+	  transparent hugepage allocation sizes at runtime.
+
+	  WARNING: This feature is unstable and may change in future kernel
+	  versions.
+
 endif # TRANSPARENT_HUGEPAGE
 
 # simple helper to make the code a bit easier to read
mm/Makefile +1 -0
index 21abb3353550..f180332f2ad0 100644
@@ -99,6 +99,7 @@ obj-$(CONFIG_MIGRATION) += migrate.o
 obj-$(CONFIG_NUMA) += memory-tiers.o
 obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o
 obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o
+obj-$(CONFIG_BPF_GET_THP_ORDER) += huge_memory_bpf.o
 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
 obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
mm/huge_memory_bpf.c new file +243 -0

243 lines not on this page · raw message

Signature · 1 line
2.47.3
reviewer
#15 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Hey Yafang,

Quoted 54 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

---

[...]

Quoted 28 lines
mm/huge_memory_bpf.c new file +22 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+enum bpf_thp_vma_type {
+       BPF_THP_VM_NONE = 0,
+       BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+       BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set

mm/huge_memory_bpf.c line 32

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.

A minor documentation nit: the comment says "Return -1 to indicate that the original requested @orders should remain unchanged". It might be slightly clearer to say "Return a negative value to fall back to the original behavior". This would cover all error codes as well ;)

mm/huge_memory_bpf.c line 37

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                          enum bpf_thp_vma_type vma_type,
+                          enum tva_type tva_type,
+                          unsigned long orders);

Sorry if I'm missing some context here since I haven't tracked the whole series closely.

Regarding the return value for thp_order_fn_t: right now it returns a single int order. I was thinking, what if we let it return an unsigned long bitmask of orders instead? This seems like it would be more flexible down the road, especially if we get more mTHP sizes to choose from. It would also make the API more consistent, as bpf_hook_thp_get_orders() itself returns an unsigned long ;)

Also, for future extensions, it might be a good idea to add a reserved flags argument to the thp_order_fn_t signature.

For example thp_order_fn_t(..., unsigned long flags).

This would give us aforward-compatible way to add new semantics later without breaking the ABI and needing a v2. We could just require it to be 0 for now.

Thanks for the great work!
Lance

reviewer
#16 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 112 lines · Lance Yang

Hey Yafang,

Quoted 54 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

---

[...]

Quoted 28 lines
mm/huge_memory_bpf.c new file +22 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+enum bpf_thp_vma_type {
+       BPF_THP_VM_NONE = 0,
+       BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+       BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set

mm/huge_memory_bpf.c line 32

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.

A minor documentation nit: the comment says "Return -1 to indicate that the original requested @orders should remain unchanged". It might be slightly clearer to say "Return a negative value to fall back to the original behavior". This would cover all error codes as well ;)

mm/huge_memory_bpf.c line 37

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                          enum bpf_thp_vma_type vma_type,
+                          enum tva_type tva_type,
+                          unsigned long orders);

Sorry if I'm missing some context here since I haven't tracked the whole series closely.

Quoted 6 lines

Regarding the return value for thp_order_fn_t: right now it returns a single int order. I was thinking, what if we let it return an unsigned long bitmask of orders instead? This seems like it would be more flexible down the road, especially if we get more mTHP sizes to choose from. It would also make the API more consistent, as bpf_hook_thp_get_orders() itself returns an unsigned long ;)

I just realized a flaw in my previous suggestion :(

Changing the return type of thp_order_fn_t to unsigned long for consistency and flexibility. However, I completely overlooked that this would prevent the BPF program from returning negative error codes ...

Thanks,
Lance

Quoted 12 lines

Also, for future extensions, it might be a good idea to add a reserved flags argument to the thp_order_fn_t signature.

For example thp_order_fn_t(..., unsigned long flags).

This would give us aforward-compatible way to add new semantics later without breaking the ABI and needing a v2. We could just require it to be 0 for now.

Thanks for the great work!
Lance

reviewer
#17 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 120 lines · Lance Yang
Quoted 112 lines · Lance Yang

Hey Yafang,

Quoted 54 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

---

[...]

Quoted 28 lines
mm/huge_memory_bpf.c new file +22 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+enum bpf_thp_vma_type {
+       BPF_THP_VM_NONE = 0,
+       BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+       BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set

mm/huge_memory_bpf.c line 32

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.

A minor documentation nit: the comment says "Return -1 to indicate that the original requested @orders should remain unchanged". It might be slightly clearer to say "Return a negative value to fall back to the original behavior". This would cover all error codes as well ;)

mm/huge_memory_bpf.c line 37

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                          enum bpf_thp_vma_type vma_type,
+                          enum tva_type tva_type,
+                          unsigned long orders);

Sorry if I'm missing some context here since I haven't tracked the whole series closely.

Quoted 6 lines

Regarding the return value for thp_order_fn_t: right now it returns a single int order. I was thinking, what if we let it return an unsigned long bitmask of orders instead? This seems like it would be more flexible down the road, especially if we get more mTHP sizes to choose from. It would also make the API more consistent, as bpf_hook_thp_get_orders() itself returns an unsigned long ;)

Quoted 21 lines

I just realized a flaw in my previous suggestion :(

Changing the return type of thp_order_fn_t to unsigned long for consistency and flexibility. However, I completely overlooked that this would prevent the BPF program from returning negative error codes ...

Thanks,
Lance

Quoted 12 lines

Also, for future extensions, it might be a good idea to add a reserved flags argument to the thp_order_fn_t signature.

For example thp_order_fn_t(..., unsigned long flags).

This would give us aforward-compatible way to add new semantics later without breaking the ABI and needing a v2. We could just require it to be 0 for now.

Thanks for the great work!
Lance

Forgot to add:

Noticed that if the hook returns 0, bpf_hook_thp_get_orders() falls back to 'orders', preventing us from dynamically disabling mTHP allocations.

Honoring a return of 0 is critical for our use case, which is to dynamically disable mTHP for low-priority containers when memory gets low in mixed workloads.

And then re-enable it for them when memory is back above the low watermark.

#18 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 99 lines · Lance Yang
Quoted 95 lines · Lance Yang
Quoted 94 lines · Lance Yang

Hey Yafang,

Quoted 54 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

---

[...]

Quoted 35 lines
mm/huge_memory_bpf.c new file +29 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+enum bpf_thp_vma_type {
+       BPF_THP_VM_NONE = 0,
+       BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+       BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise
+ *
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

mm/huge_memory_bpf.c line 32

Quoted 3 lines
mm/huge_memory_bpf.c new file +3 -0
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.

A minor documentation nit: the comment says "Return -1 to indicate that the original requested @orders should remain unchanged". It might be slightly clearer to say "Return a negative value to fall back to the original behavior". This would cover all error codes as well ;)

will change it.

Quoted 27 lines
Quoted 27 lines
Quoted 10 lines

mm/huge_memory_bpf.c line 37

Quoted 6 lines
mm/huge_memory_bpf.c new file +5 -0
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                          enum bpf_thp_vma_type vma_type,
+                          enum tva_type tva_type,
+                          unsigned long orders);

Sorry if I'm missing some context here since I haven't tracked the whole series closely.

Quoted 6 lines

Regarding the return value for thp_order_fn_t: right now it returns a single int order. I was thinking, what if we let it return an unsigned long bitmask of orders instead? This seems like it would be more flexible down the road, especially if we get more mTHP sizes to choose from. It would also make the API more consistent, as bpf_hook_thp_get_orders() itself returns an unsigned long ;)

I just realized a flaw in my previous suggestion :(

Changing the return type of thp_order_fn_t to unsigned long for consistency and flexibility. However, I completely overlooked that this would prevent the BPF program from returning negative error codes ...

Thanks,
Lance

Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Also, for future extensions, it might be a good idea to add a reserved flags argument to the thp_order_fn_t signature.

For example thp_order_fn_t(..., unsigned long flags).

This would give us aforward-compatible way to add new semantics later without breaking the ABI and needing a v2. We could just require it to be 0 for now.

That makes sense. However, as Lorenzo mentioned previously, we should keep the interface as minimal as possible.

Quoted 10 lines
Quoted 3 lines
Quoted 3 lines

Thanks for the great work!
Lance

Forgot to add:

Noticed that if the hook returns 0, bpf_hook_thp_get_orders() falls back to 'orders', preventing us from dynamically disabling mTHP allocations.

Could you please clarify what you mean by that?

+ thp_order = bpf_hook_thp_get_order(vma, vma_type, tva_type, orders);
+ if (thp_order < 0)
+ goto out;

In my implementation, it only falls back to @orders if the return value is negative. If the return value is 0, it uses BIT(0):

+ if (thp_order <= highest_order(orders))
+ thp_orders = BIT(thp_order);

Quoted 7 lines

Honoring a return of 0 is critical for our use case, which is to dynamically disable mTHP for low-priority containers when memory gets low in mixed workloads.

And then re-enable it for them when memory is back above the low watermark.

Thank you for detailing your use case; that context is very helpful.

Signature · 2 lines
Regards
Yafang
reviewer
#19 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 1 line · Yafang Shao

On Wed, Sep 10, 2025 at 9:57 PM Lance Yang <[email protected]> wrote:

[...]

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
+ * BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ * neither is set.
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation
+ * - PMD-mapped allocation if PMD_ORDER is set
+ * - mTHP allocation otherwise
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ * not exceed the highest requested order in @orders. Return -1 to
+ * indicate that the original requested @orders should remain unchanged.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

A minor documentation nit: the comment says "Return -1 to indicate that the original requested @orders should remain unchanged". It might be slightly clearer to say "Return a negative value to fall back to the original behavior". This would cover all error codes as well ;)

will change it.

Please feel free to change it ;)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[...]

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Also, for future extensions, it might be a good idea to add a reserved flags argument to the thp_order_fn_t signature.

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

For example thp_order_fn_t(..., unsigned long flags).

This would give us aforward-compatible way to add new semantics later without breaking the ABI and needing a v2. We could just require it to be 0 for now.

That makes sense. However, as Lorenzo mentioned previously, we should keep the interface as minimal as possible.

Got it.

[...]

Quoted 12 lines
Quoted 5 lines

Forgot to add:

Noticed that if the hook returns 0, bpf_hook_thp_get_orders() falls back to 'orders', preventing us from dynamically disabling mTHP allocations.

Could you please clarify what you mean by that?

mm/huge_memory_bpf.c new file +3 -0
+       thp_order = bpf_hook_thp_get_order(vma, vma_type, tva_type, orders);
+       if (thp_order < 0)
+               goto out;
Quoted 2 lines

In my implementation, it only falls back to @orders if the return value is negative. If the return value is 0, it uses BIT(0):

My bad, I completely misread the code last night ...

I see now that returning 0 forces a base page (order-0)

mm/huge_memory_bpf.c line 89

Quoted 3 lines
mm/huge_memory_bpf.c new file +2 -0
+       if (thp_order <= highest_order(orders))
+               thp_orders = BIT(thp_order);

Yes, this is exactly the behavior we need. It will allow us to dynamically disable mTHP for low-priority containers when we need to, which is perfect for our use case!

Quoted 10 lines
Quoted 8 lines

Honoring a return of 0 is critical for our use case, which is to dynamically disable mTHP for low-priority containers when memory gets low in mixed workloads.

And then re-enable it for them when memory is back above the low watermark.

Thank you for detailing your use case; that context is very helpful.

Cheers,
Lance

#20 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

In reply to Lance Yang
Quoted 148 lines · Lance Yang
Quoted 120 lines · Lance Yang
Quoted 112 lines · Lance Yang

Hey Yafang,

Quoted 54 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

---

[...]

Quoted 28 lines
mm/huge_memory_bpf.c new file +22 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+enum bpf_thp_vma_type {
+       BPF_THP_VM_NONE = 0,
+       BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+       BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set

mm/huge_memory_bpf.c line 32

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.

A minor documentation nit: the comment says "Return -1 to indicate that the original requested @orders should remain unchanged". It might be slightly clearer to say "Return a negative value to fall back to the original behavior". This would cover all error codes as well ;)

mm/huge_memory_bpf.c line 37

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                          enum bpf_thp_vma_type vma_type,
+                          enum tva_type tva_type,
+                          unsigned long orders);

Sorry if I'm missing some context here since I haven't tracked the whole series closely.

Quoted 6 lines

Regarding the return value for thp_order_fn_t: right now it returns a single int order. I was thinking, what if we let it return an unsigned long bitmask of orders instead? This seems like it would be more flexible down the road, especially if we get more mTHP sizes to choose from. It would also make the API more consistent, as bpf_hook_thp_get_orders() itself returns an unsigned long ;)

Quoted 21 lines

I just realized a flaw in my previous suggestion :(

Changing the return type of thp_order_fn_t to unsigned long for consistency and flexibility. However, I completely overlooked that this would prevent the BPF program from returning negative error codes ...

Thanks,
Lance

Quoted 12 lines

Also, for future extensions, it might be a good idea to add a reserved flags argument to the thp_order_fn_t signature.

For example thp_order_fn_t(..., unsigned long flags).

This would give us aforward-compatible way to add new semantics later without breaking the ABI and needing a v2. We could just require it to be 0 for now.

Thanks for the great work!
Lance

Forgot to add:

Quoted 7 lines

Noticed that if the hook returns 0, bpf_hook_thp_get_orders() falls back to 'orders', preventing us from dynamically disabling mTHP allocations.

Honoring a return of 0 is critical for our use case, which is to dynamically disable mTHP for low-priority containers when memory gets low in mixed workloads.

Right, yeah we shouldn't just default back to orders should we. The user _knows_ what orders are available and should select one.

Actually that logic is a bit weird overall let me reply again to patch...

Quoted 3 lines

And then re-enable it for them when memory is back above the low watermark.

#21 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

In reply to Lance Yang
Quoted 111 lines · Lance Yang

Hey Yafang,

Quoted 54 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

---

[...]

Quoted 28 lines
mm/huge_memory_bpf.c new file +22 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+enum bpf_thp_vma_type {
+       BPF_THP_VM_NONE = 0,
+       BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+       BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set

mm/huge_memory_bpf.c line 32

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.

A minor documentation nit: the comment says "Return -1 to indicate that the original requested @orders should remain unchanged". It might be slightly clearer to say "Return a negative value to fall back to the original behavior". This would cover all error codes as well ;)

mm/huge_memory_bpf.c line 37

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                          enum bpf_thp_vma_type vma_type,
+                          enum tva_type tva_type,
+                          unsigned long orders);

Sorry if I'm missing some context here since I haven't tracked the whole series closely.

Quoted 6 lines

Regarding the return value for thp_order_fn_t: right now it returns a single int order. I was thinking, what if we let it return an unsigned long bitmask of orders instead? This seems like it would be more flexible down the road, especially if we get more mTHP sizes to choose from. It would also make the API more consistent, as bpf_hook_thp_get_orders() itself returns an unsigned long ;)

I think that adds confusion - as in how an order might be chosen from those. Also we have _received_ a bitmap of available orders - and the intent here is to select _which one we should use_.

And this is an experimental feature, behind a flag explicitly labelled as experimental (and thus subject to change) so if we found we needed to change things in the future we can.

Quoted 3 lines

Also, for future extensions, it might be a good idea to add a reserved flags argument to the thp_order_fn_t signature.

We don't need to do anything like this, as we are behind an experimental flag and in no way guarantee that this will be used this way going forwards.

Quoted 6 lines

For example thp_order_fn_t(..., unsigned long flags).

This would give us aforward-compatible way to add new semantics later without breaking the ABI and needing a v2. We could just require it to be 0 for now.

There is no ABI.

I mean again to emphasise, this is an _experimental_ feature not to be relied upon in production.

Quoted 3 lines

Thanks for the great work!
Lance

Perhaps we need to put a 'EXPERIMENTAL_' prefix on the config flag too to really bring this home, as it's perhaps not all that clear :)

Cheers, Lorenzo

reviewer
#22 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 119 lines · Lorenzo Stoakes
Quoted 111 lines · Lance Yang

Hey Yafang,

Quoted 54 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

---

[...]

Quoted 28 lines
mm/huge_memory_bpf.c new file +22 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+enum bpf_thp_vma_type {
+       BPF_THP_VM_NONE = 0,
+       BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+       BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set

mm/huge_memory_bpf.c line 32

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.

A minor documentation nit: the comment says "Return -1 to indicate that the original requested @orders should remain unchanged". It might be slightly clearer to say "Return a negative value to fall back to the original behavior". This would cover all error codes as well ;)

mm/huge_memory_bpf.c line 37

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                          enum bpf_thp_vma_type vma_type,
+                          enum tva_type tva_type,
+                          unsigned long orders);

Sorry if I'm missing some context here since I haven't tracked the whole series closely.

Quoted 6 lines

Regarding the return value for thp_order_fn_t: right now it returns a single int order. I was thinking, what if we let it return an unsigned long bitmask of orders instead? This seems like it would be more flexible down the road, especially if we get more mTHP sizes to choose from. It would also make the API more consistent, as bpf_hook_thp_get_orders() itself returns an unsigned long ;)

Quoted 3 lines

I think that adds confusion - as in how an order might be chosen from those. Also we have _received_ a bitmap of available orders - and the intent here is to select _which one we should use_.

Yep. Makes sense to me ;)

Quoted 4 lines

And this is an experimental feature, behind a flag explicitly labelled as experimental (and thus subject to change) so if we found we needed to change things in the future we can.

You're right, I didn't pay enough attention to the fact that this is an experimental feature. So my suggestions were based on a lack of context ...

Quoted 20 lines
Quoted 4 lines

Also, for future extensions, it might be a good idea to add a reserved flags argument to the thp_order_fn_t signature.

We don't need to do anything like this, as we are behind an experimental flag and in no way guarantee that this will be used this way going forwards.

Quoted 6 lines

For example thp_order_fn_t(..., unsigned long flags).

This would give us aforward-compatible way to add new semantics later without breaking the ABI and needing a v2. We could just require it to be 0 for now.

There is no ABI.

I mean again to emphasise, this is an _experimental_ feature not to be relied upon in production.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

Thanks for the great work!
Lance

Perhaps we need to put a 'EXPERIMENTAL_' prefix on the config flag too to really bring this home, as it's perhaps not all that clear :)

No need for a 'EXPERIMENTAL_' prefix, it was just me missing the background. Appreciate you clarifying this!

Cheers,
Lance

#23 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 122 lines · Lance Yang
Quoted 119 lines · Lorenzo Stoakes
Quoted 117 lines · Lance Yang

Hey Yafang,

Quoted 54 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

---

[...]

Quoted 28 lines
mm/huge_memory_bpf.c new file +22 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+enum bpf_thp_vma_type {
+       BPF_THP_VM_NONE = 0,
+       BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+       BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set

mm/huge_memory_bpf.c line 32

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.

A minor documentation nit: the comment says "Return -1 to indicate that the original requested @orders should remain unchanged". It might be slightly clearer to say "Return a negative value to fall back to the original behavior". This would cover all error codes as well ;)

mm/huge_memory_bpf.c line 37

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                          enum bpf_thp_vma_type vma_type,
+                          enum tva_type tva_type,
+                          unsigned long orders);

Sorry if I'm missing some context here since I haven't tracked the whole series closely.

Regarding the return value for thp_order_fn_t: right now it returns a single int order. I was thinking, what if we let it return an unsigned long bitmask of orders instead? This seems like it would be more flexible down the road, especially if we get more mTHP sizes to choose from. It would also make the API more consistent, as bpf_hook_thp_get_orders() itself returns an unsigned long ;)

Quoted 5 lines
Quoted 3 lines

I think that adds confusion - as in how an order might be chosen from those. Also we have _received_ a bitmap of available orders - and the intent here is to select _which one we should use_.

Yep. Makes sense to me ;)

Thanks :)

Quoted 9 lines
Quoted 5 lines

And this is an experimental feature, behind a flag explicitly labelled as experimental (and thus subject to change) so if we found we needed to change things in the future we can.

You're right, I didn't pay enough attention to the fact that this is an experimental feature. So my suggestions were based on a lack of context ...

It's fine, don't worry :) these are sensible suggestions - it to me highlights that we haven't been clear enough perhaps.

Quoted 21 lines
Quoted 21 lines
Quoted 5 lines

Also, for future extensions, it might be a good idea to add a reserved flags argument to the thp_order_fn_t signature.

We don't need to do anything like this, as we are behind an experimental flag and in no way guarantee that this will be used this way going forwards.

Quoted 6 lines

For example thp_order_fn_t(..., unsigned long flags).

This would give us aforward-compatible way to add new semantics later without breaking the ABI and needing a v2. We could just require it to be 0 for now.

There is no ABI.

I mean again to emphasise, this is an _experimental_ feature not to be relied upon in production.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Thanks for the great work!
Lance

Perhaps we need to put a 'EXPERIMENTAL_' prefix on the config flag too to really bring this home, as it's perhaps not all that clear :)

No need for a 'EXPERIMENTAL_' prefix, it was just me missing the background. Appreciate you clarifying this!

Don't worry about it, but also it suggests that we probably need to be ultra-super clear to users in general. So I think an _EXPERIMENTAL suffix is probably pretty valid here just to _hammer home_ that - hey - we might break you! :)

Quoted 4 lines

Cheers,
Lance

Cheers, Lorenzo

#24 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 172 lines · Lorenzo Stoakes
Quoted 122 lines · Lance Yang
Quoted 119 lines · Lorenzo Stoakes
Quoted 117 lines · Lance Yang

Hey Yafang,

Quoted 54 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

---

[...]

Quoted 28 lines
mm/huge_memory_bpf.c new file +22 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+enum bpf_thp_vma_type {
+       BPF_THP_VM_NONE = 0,
+       BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+       BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set

mm/huge_memory_bpf.c line 32

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.

A minor documentation nit: the comment says "Return -1 to indicate that the original requested @orders should remain unchanged". It might be slightly clearer to say "Return a negative value to fall back to the original behavior". This would cover all error codes as well ;)

mm/huge_memory_bpf.c line 37

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                          enum bpf_thp_vma_type vma_type,
+                          enum tva_type tva_type,
+                          unsigned long orders);

Sorry if I'm missing some context here since I haven't tracked the whole series closely.

Regarding the return value for thp_order_fn_t: right now it returns a single int order. I was thinking, what if we let it return an unsigned long bitmask of orders instead? This seems like it would be more flexible down the road, especially if we get more mTHP sizes to choose from. It would also make the API more consistent, as bpf_hook_thp_get_orders() itself returns an unsigned long ;)

Quoted 5 lines
Quoted 3 lines

I think that adds confusion - as in how an order might be chosen from those. Also we have _received_ a bitmap of available orders - and the intent here is to select _which one we should use_.

Yep. Makes sense to me ;)

Thanks :)

Quoted 9 lines
Quoted 5 lines

And this is an experimental feature, behind a flag explicitly labelled as experimental (and thus subject to change) so if we found we needed to change things in the future we can.

You're right, I didn't pay enough attention to the fact that this is an experimental feature. So my suggestions were based on a lack of context ...

It's fine, don't worry :) these are sensible suggestions - it to me highlights that we haven't been clear enough perhaps.

Quoted 27 lines
Quoted 26 lines
Quoted 5 lines

Also, for future extensions, it might be a good idea to add a reserved flags argument to the thp_order_fn_t signature.

We don't need to do anything like this, as we are behind an experimental flag and in no way guarantee that this will be used this way going forwards.

Quoted 6 lines

For example thp_order_fn_t(..., unsigned long flags).

This would give us aforward-compatible way to add new semantics later without breaking the ABI and needing a v2. We could just require it to be 0 for now.

There is no ABI.

I mean again to emphasise, this is an _experimental_ feature not to be relied upon in production.

Quoted 3 lines

Thanks for the great work!
Lance

Perhaps we need to put a 'EXPERIMENTAL_' prefix on the config flag too to really bring this home, as it's perhaps not all that clear :)

Quoted 7 lines
Quoted 2 lines

No need for a 'EXPERIMENTAL_' prefix, it was just me missing the background. Appreciate you clarifying this!

Don't worry about it, but also it suggests that we probably need to be ultra-super clear to users in general. So I think an _EXPERIMENTAL suffix is probably pretty valid here just to _hammer home_ that - hey - we might break you! :)

I will add it. Thanks for the reminder.

Signature · 2 lines
Regards
Yafang
#25 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 7 lines · Yafang Shao
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Perhaps we need to put a 'EXPERIMENTAL_' prefix on the config flag too to really bring this home, as it's perhaps not all that clear :)

No need for a 'EXPERIMENTAL_' prefix, it was just me missing the background. Appreciate you clarifying this!

Quoted 6 lines
Quoted 4 lines

Don't worry about it, but also it suggests that we probably need to be ultra-super clear to users in general. So I think an _EXPERIMENTAL suffix is probably pretty valid here just to _hammer home_ that - hey - we might break you! :)

I will add it. Thanks for the reminder.

Thanks!

Quoted 4 lines

--
Regards
Yafang

Cheers, Lorenzo

#26 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

In reply to Yafang Shao
Quoted 40 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

Quoted 8 lines

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

Thanks for highlighting.

Quoted 18 lines
---
 MAINTAINERS             |   1 +
 include/linux/huge_mm.h |  26 ++++-
 mm/Kconfig              |  12 ++
 mm/Makefile             |   1 +
 mm/huge_memory_bpf.c    | 243 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 280 insertions(+), 3 deletions(-)
 create mode 100644 mm/huge_memory_bpf.c
MAINTAINERS +0 -0
index 8fef05bc2224..d055a3c95300 100644

MAINTAINERS F: include/linux/huge_mm.h · line 16255

Quoted 4 lines
MAINTAINERS +1 -0
@@ -16252,6 +16252,7 @@ F:	include/linux/huge_mm.h
 F:	include/linux/khugepaged.h
 F:	include/trace/events/huge_memory.h
 F:	mm/huge_memory.c
+F:	mm/huge_memory_bpf.c

THanks!

Quoted 16 lines
MAINTAINERS +0 -0
 F:	mm/khugepaged.c
 F:	mm/mm_slot.h
 F:	tools/testing/selftests/mm/khugepaged.c
include/linux/huge_mm.h +1 -0
index 23f124493c47..f72a5fd04e4f 100644
@@ -56,6 +56,7 @@ enum transparent_hugepage_flag {
 	TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
 	TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
 	TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
+	TRANSPARENT_HUGEPAGE_BPF_ATTACHED,      /* BPF prog is attached */
 };
 
 struct kobject;

include/linux/huge_mm.h unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, · line 277

Quoted 7 lines
include/linux/huge_mm.h +4 -0
@@ -270,6 +271,19 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 					 enum tva_type type,
 					 unsigned long orders);
 
+#ifdef CONFIG_BPF_GET_THP_ORDER
+unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+			enum tva_type type, unsigned long orders);

Thanks for renaming!

Quoted 13 lines
include/linux/huge_mm.h +9 -0
+#else
+static inline unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+			enum tva_type tva_flags, unsigned long orders)
+{
+	return orders;
+}
+#endif
+
 /**
  * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma
  * @vma:  the vm area to check

include/linux/huge_mm.h unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, · line 312

Quoted 8 lines
include/linux/huge_mm.h +5 -0
@@ -291,6 +305,12 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
 				       enum tva_type type,
 				       unsigned long orders)
 {
+	unsigned long bpf_orders;
+
+	bpf_orders = bpf_hook_thp_get_orders(vma, vm_flags, type, orders);
+	if (!bpf_orders)
+		return 0;

I think it'd be easier to just do:

	/* The BPF-specified order overrides which order is selected. */
	orders &= bpf_hook_thp_get_orders(vma, vm_flags, type, orders);
	if (!orders)
		return 0;

include/linux/huge_mm.h unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, · line 329

Quoted 13 lines
include/linux/huge_mm.h +3 -2
+
 	/*
 	 * Optimization to check if required orders are enabled early. Only
 	 * forced collapse ignores sysfs configs.
@@ -304,12 +324,12 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
 		    ((vm_flags & VM_HUGEPAGE) && hugepage_global_enabled()))
 			mask |= READ_ONCE(huge_anon_orders_inherit);
 
-		orders &= mask;
-		if (!orders)
+		bpf_orders &= mask;
+		if (!bpf_orders)
 			return 0

With my suggeted change this would remain the same.

include/linux/huge_mm.h unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, · line 332

Quoted 4 lines
include/linux/huge_mm.h +1 -1
 	}
 
-	return __thp_vma_allowable_orders(vma, vm_flags, type, orders);
+	return __thp_vma_allowable_orders(vma, vm_flags, type, bpf_orders);

With my suggeted change this would remain the same.

mm/Kconfig config NO_PAGE_MAPCOUNT · line 899

Quoted 12 lines
include/linux/huge_mm.h +0 -0
 }
 
 struct thpsize {
mm/Kconfig +1 -0
index d1ed839ca710..4d89d2158f10 100644
@@ -896,6 +896,18 @@ config NO_PAGE_MAPCOUNT
 
 	  EXPERIMENTAL because the impact of some changes is still unclear.
 
+config BPF_GET_THP_ORDER

Yeah, I think we maybe need to sledgehammer this as already Lance was confused as to the permenancy of this, and I feel that users might be too, even with the '(EXPERIMENTAL)' bit.

So maybe

config BPF_GET_THP_ORDER_EXPERIMENTAL

Just to hammer it home?

Quoted 39 lines
mm/Kconfig +11 -0
+	bool "BPF-based THP order selection (EXPERIMENTAL)"
+	depends on TRANSPARENT_HUGEPAGE && BPF_SYSCALL
+
+	help
+	  Enable dynamic THP order selection using BPF programs. This
+	  experimental feature allows custom BPF logic to determine optimal
+	  transparent hugepage allocation sizes at runtime.
+
+	  WARNING: This feature is unstable and may change in future kernel
+	  versions.
+
 endif # TRANSPARENT_HUGEPAGE
 
 # simple helper to make the code a bit easier to read
mm/Makefile +1 -0
index 21abb3353550..f180332f2ad0 100644
@@ -99,6 +99,7 @@ obj-$(CONFIG_MIGRATION) += migrate.o
 obj-$(CONFIG_NUMA) += memory-tiers.o
 obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o
 obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o
+obj-$(CONFIG_BPF_GET_THP_ORDER) += huge_memory_bpf.o
 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
 obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
mm/huge_memory_bpf.c new file +7 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+

mm/huge_memory_bpf.c line 17

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+enum bpf_thp_vma_type {
+	BPF_THP_VM_NONE = 0,
+	BPF_THP_VM_HUGEPAGE,	/* VM_HUGEPAGE */
+	BPF_THP_VM_NOHUGEPAGE,	/* VM_NOHUGEPAGE */
+};

I'm really not so sure how useful this is - can't a user just ascertain this from the VMA flags themselves?

Let's keep the interface as minimal as possible.

mm/huge_memory_bpf.c line 20

Quoted 3 lines
mm/huge_memory_bpf.c new file +3 -0
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation

orders -> order?

mm/huge_memory_bpf.c line 24

Quoted 4 lines
mm/huge_memory_bpf.c new file +4 -0
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.

Obv as above let's drop this probably :)

mm/huge_memory_bpf.c line 26

Quoted 2 lines
mm/huge_memory_bpf.c new file +2 -0
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation

Shouldn't requested = available?

mm/huge_memory_bpf.c line 28

Quoted 2 lines
mm/huge_memory_bpf.c new file +2 -0
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise

Not sure these 2 points are super useful.

Quoted 32 lines
mm/huge_memory_bpf.c new file +32 -0
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+			   enum bpf_thp_vma_type vma_type,
+			   enum tva_type tva_type,
+			   unsigned long orders);
+
+struct bpf_thp_ops {
+	thp_order_fn_t __rcu *thp_get_order;
+};
+
+static struct bpf_thp_ops bpf_thp;
+static DEFINE_SPINLOCK(thp_ops_lock);
+
+/*
+ * Returns the original @orders if no BPF program is attached or if the
+ * suggested order is invalid.
+ */
+unsigned long bpf_hook_thp_get_orders(struct vm_area_struct *vma,
+				      vm_flags_t vma_flags,
+				      enum tva_type tva_type,
+				      unsigned long orders)
+{
+	thp_order_fn_t *bpf_hook_thp_get_order;
+	unsigned long thp_orders = orders;
+	enum bpf_thp_vma_type vma_type;
+	int thp_order;
+
+	/* No BPF program is attached */

mm/huge_memory_bpf.c line 70

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+	if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+		      &transparent_hugepage_flags))
+		return orders;
+
+	if (vma_flags & VM_HUGEPAGE)
+		vma_type = BPF_THP_VM_HUGEPAGE;
+	else if (vma_flags & VM_NOHUGEPAGE)
+		vma_type = BPF_THP_VM_NOHUGEPAGE;
+	else
+		vma_type = BPF_THP_VM_NONE;

As per above, not sure this is all that useful.

mm/huge_memory_bpf.c line 83

Quoted 13 lines
mm/huge_memory_bpf.c new file +13 -0
+
+	rcu_read_lock();
+	bpf_hook_thp_get_order = rcu_dereference(bpf_thp.thp_get_order);
+	if (!bpf_hook_thp_get_order)
+		goto out;
+
+	thp_order = bpf_hook_thp_get_order(vma, vma_type, tva_type, orders);
+	if (thp_order < 0)
+		goto out;
+	/*
+	 * The maximum requested order is determined by the callsite. E.g.:
+	 * - PMD-mapped THP uses PMD_ORDER
+	 * - mTHP uses (PMD_ORDER - 1)

I don't think this is quite right, highest_order() figures out the highest set bit, so mTHP can be PMD_ORDER - 1 or less (in theory ofc).

I think we can just replace this with something simpler like - 'depending on where the BPF hook is invoked, we check for either PMD order or mTHP orders (less than PMD order)' or something.

mm/huge_memory_bpf.c line 87

Quoted 4 lines
mm/huge_memory_bpf.c new file +4 -0
+	 *
+	 * We must respect this upper bound to avoid undefined behavior. So the
+	 * highest suggested order can't exceed the highest requested order.
+	 */

I think this sentence is also unnecessary.

Quoted 159 lines
mm/huge_memory_bpf.c new file +156 -1
+	if (thp_order <= highest_order(orders))
+		thp_orders = BIT(thp_order);
+
+out:
+	rcu_read_unlock();
+	return thp_orders;
+}
+
+static bool bpf_thp_ops_is_valid_access(int off, int size,
+					enum bpf_access_type type,
+					const struct bpf_prog *prog,
+					struct bpf_insn_access_aux *info)
+{
+	return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
+}
+
+static const struct bpf_func_proto *
+bpf_thp_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
+{
+	return bpf_base_func_proto(func_id, prog);
+}
+
+static const struct bpf_verifier_ops thp_bpf_verifier_ops = {
+	.get_func_proto = bpf_thp_get_func_proto,
+	.is_valid_access = bpf_thp_ops_is_valid_access,
+};
+
+static int bpf_thp_init(struct btf *btf)
+{
+	return 0;
+}
+
+static int bpf_thp_check_member(const struct btf_type *t,
+				const struct btf_member *member,
+				const struct bpf_prog *prog)
+{
+	/* The call site operates under RCU protection. */
+	if (prog->sleepable)
+		return -EINVAL;
+	return 0;
+}
+
+static int bpf_thp_init_member(const struct btf_type *t,
+			       const struct btf_member *member,
+			       void *kdata, const void *udata)
+{
+	return 0;
+}
+
+static int bpf_thp_reg(void *kdata, struct bpf_link *link)
+{
+	struct bpf_thp_ops *ops = kdata;
+
+	spin_lock(&thp_ops_lock);
+	if (test_and_set_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+			     &transparent_hugepage_flags)) {
+		spin_unlock(&thp_ops_lock);
+		return -EBUSY;
+	}
+	WARN_ON_ONCE(rcu_access_pointer(bpf_thp.thp_get_order));
+	rcu_assign_pointer(bpf_thp.thp_get_order, ops->thp_get_order);
+	spin_unlock(&thp_ops_lock);
+	return 0;
+}
+
+static void bpf_thp_unreg(void *kdata, struct bpf_link *link)
+{
+	thp_order_fn_t *old_fn;
+
+	spin_lock(&thp_ops_lock);
+	clear_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED, &transparent_hugepage_flags);
+	old_fn = rcu_replace_pointer(bpf_thp.thp_get_order, NULL,
+				     lockdep_is_held(&thp_ops_lock));
+	WARN_ON_ONCE(!old_fn);
+	spin_unlock(&thp_ops_lock);
+
+	synchronize_rcu();
+}
+
+static int bpf_thp_update(void *kdata, void *old_kdata, struct bpf_link *link)
+{
+	thp_order_fn_t *old_fn, *new_fn;
+	struct bpf_thp_ops *old = old_kdata;
+	struct bpf_thp_ops *ops = kdata;
+	int ret = 0;
+
+	if (!ops || !old)
+		return -EINVAL;
+
+	spin_lock(&thp_ops_lock);
+	/* The prog has aleady been removed. */
+	if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+		      &transparent_hugepage_flags)) {
+		ret = -ENOENT;
+		goto out;
+	}
+
+	new_fn = rcu_dereference(ops->thp_get_order);
+	old_fn = rcu_replace_pointer(bpf_thp.thp_get_order, new_fn,
+				     lockdep_is_held(&thp_ops_lock));
+	WARN_ON_ONCE(!old_fn || !new_fn);
+
+out:
+	spin_unlock(&thp_ops_lock);
+	if (!ret)
+		synchronize_rcu();
+	return ret;
+}
+
+static int bpf_thp_validate(void *kdata)
+{
+	struct bpf_thp_ops *ops = kdata;
+
+	if (!ops->thp_get_order) {
+		pr_err("bpf_thp: required ops isn't implemented\n");
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static int bpf_thp_get_order(struct vm_area_struct *vma,
+			     enum bpf_thp_vma_type vma_type,
+			     enum tva_type tva_type,
+			     unsigned long orders)
+{
+	return -1;
+}
+
+static struct bpf_thp_ops __bpf_thp_ops = {
+	.thp_get_order = (thp_order_fn_t __rcu *)bpf_thp_get_order,
+};
+
+static struct bpf_struct_ops bpf_bpf_thp_ops = {
+	.verifier_ops = &thp_bpf_verifier_ops,
+	.init = bpf_thp_init,
+	.check_member = bpf_thp_check_member,
+	.init_member = bpf_thp_init_member,
+	.reg = bpf_thp_reg,
+	.unreg = bpf_thp_unreg,
+	.update = bpf_thp_update,
+	.validate = bpf_thp_validate,
+	.cfi_stubs = &__bpf_thp_ops,
+	.owner = THIS_MODULE,
+	.name = "bpf_thp_ops",
+};
+
+static int __init bpf_thp_ops_init(void)
+{
+	int err;
+
+	err = register_bpf_struct_ops(&bpf_bpf_thp_ops, bpf_thp_ops);
+	if (err)
+		pr_err("bpf_thp: Failed to register struct_ops (%d)\n", err);
+	return err;
+}
+late_initcall(bpf_thp_ops_init);
--

2.47.3

#27 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 128 lines · Lorenzo Stoakes
Quoted 40 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

Quoted 8 lines

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

Thanks for highlighting.

Quoted 18 lines
---
 MAINTAINERS             |   1 +
 include/linux/huge_mm.h |  26 ++++-
 mm/Kconfig              |  12 ++
 mm/Makefile             |   1 +
 mm/huge_memory_bpf.c    | 243 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 280 insertions(+), 3 deletions(-)
 create mode 100644 mm/huge_memory_bpf.c
MAINTAINERS +0 -0
index 8fef05bc2224..d055a3c95300 100644

MAINTAINERS F: include/linux/huge_mm.h · line 16255

Quoted 4 lines
MAINTAINERS +1 -0
@@ -16252,6 +16252,7 @@ F:    include/linux/huge_mm.h
 F:   include/linux/khugepaged.h
 F:   include/trace/events/huge_memory.h
 F:   mm/huge_memory.c
+F:   mm/huge_memory_bpf.c

THanks!

Quoted 16 lines
MAINTAINERS +0 -0
 F:   mm/khugepaged.c
 F:   mm/mm_slot.h
 F:   tools/testing/selftests/mm/khugepaged.c
include/linux/huge_mm.h +1 -0
index 23f124493c47..f72a5fd04e4f 100644
@@ -56,6 +56,7 @@ enum transparent_hugepage_flag {
      TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
      TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
      TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
+     TRANSPARENT_HUGEPAGE_BPF_ATTACHED,      /* BPF prog is attached */
 };
 
 struct kobject;

include/linux/huge_mm.h unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, · line 277

Quoted 7 lines
include/linux/huge_mm.h +4 -0
@@ -270,6 +271,19 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
                                       enum tva_type type,
                                       unsigned long orders);
 
+#ifdef CONFIG_BPF_GET_THP_ORDER
+unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+                     enum tva_type type, unsigned long orders);

Thanks for renaming!

Quoted 13 lines
include/linux/huge_mm.h +9 -0
+#else
+static inline unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+                     enum tva_type tva_flags, unsigned long orders)
+{
+     return orders;
+}
+#endif
+
 /**
  * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma
  * @vma:  the vm area to check

include/linux/huge_mm.h unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, · line 312

Quoted 8 lines
include/linux/huge_mm.h +5 -0
@@ -291,6 +305,12 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
                                     enum tva_type type,
                                     unsigned long orders)
 {
+     unsigned long bpf_orders;
+
+     bpf_orders = bpf_hook_thp_get_orders(vma, vm_flags, type, orders);
+     if (!bpf_orders)
+             return 0;

I think it'd be easier to just do:

Quoted 4 lines
        /* The BPF-specified order overrides which order is selected. */
        orders &= bpf_hook_thp_get_orders(vma, vm_flags, type, orders);
        if (!orders)
                return 0;

good suggestion!

Quoted 41 lines
Quoted 6 lines
include/linux/huge_mm.h +1 -0
+
      /*
       * Optimization to check if required orders are enabled early. Only
       * forced collapse ignores sysfs configs.

include/linux/huge_mm.h unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, · line 329

Quoted 8 lines
include/linux/huge_mm.h +2 -2
@@ -304,12 +324,12 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
                  ((vm_flags & VM_HUGEPAGE) && hugepage_global_enabled()))
                      mask |= READ_ONCE(huge_anon_orders_inherit);
 
-             orders &= mask;
-             if (!orders)
+             bpf_orders &= mask;
+             if (!bpf_orders)
                      return 0

With my suggeted change this would remain the same.

include/linux/huge_mm.h unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, · line 332

Quoted 4 lines
include/linux/huge_mm.h +1 -1
      }
 
-     return __thp_vma_allowable_orders(vma, vm_flags, type, orders);
+     return __thp_vma_allowable_orders(vma, vm_flags, type, bpf_orders);

With my suggeted change this would remain the same.

mm/Kconfig config NO_PAGE_MAPCOUNT · line 899

Quoted 12 lines
include/linux/huge_mm.h +0 -0
 }
 
 struct thpsize {
mm/Kconfig +1 -0
index d1ed839ca710..4d89d2158f10 100644
@@ -896,6 +896,18 @@ config NO_PAGE_MAPCOUNT
 
        EXPERIMENTAL because the impact of some changes is still unclear.
 
+config BPF_GET_THP_ORDER

Yeah, I think we maybe need to sledgehammer this as already Lance was confused as to the permenancy of this, and I feel that users might be too, even with the '(EXPERIMENTAL)' bit.

Quoted 5 lines

So maybe

config BPF_GET_THP_ORDER_EXPERIMENTAL

Just to hammer it home?

ack

Quoted 45 lines
Quoted 45 lines
mm/Kconfig +11 -0
+     bool "BPF-based THP order selection (EXPERIMENTAL)"
+     depends on TRANSPARENT_HUGEPAGE && BPF_SYSCALL
+
+     help
+       Enable dynamic THP order selection using BPF programs. This
+       experimental feature allows custom BPF logic to determine optimal
+       transparent hugepage allocation sizes at runtime.
+
+       WARNING: This feature is unstable and may change in future kernel
+       versions.
+
 endif # TRANSPARENT_HUGEPAGE
 
 # simple helper to make the code a bit easier to read
mm/Makefile +1 -0
index 21abb3353550..f180332f2ad0 100644
@@ -99,6 +99,7 @@ obj-$(CONFIG_MIGRATION) += migrate.o
 obj-$(CONFIG_NUMA) += memory-tiers.o
 obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o
 obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o
+obj-$(CONFIG_BPF_GET_THP_ORDER) += huge_memory_bpf.o
 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
 obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
mm/huge_memory_bpf.c new file +12 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
Quoted 8 lines

mm/huge_memory_bpf.c line 17

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+enum bpf_thp_vma_type {
+     BPF_THP_VM_NONE = 0,
+     BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+     BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};

I'm really not so sure how useful this is - can't a user just ascertain this from the VMA flags themselves?

I assume you are referring to checking flags from vma->vm_flags. There is an exception where we cannot use vma->vm_flags: in hugepage_madvise(), which calls khugepaged_enter_vma(vma, *vm_flags).

At this point, the VM_HUGEPAGE flag has not been set in vma->vm_flags yet. Therefore, we must pass the separate *vm_flags variable. Perhaps we can simplify the logic with the following change?

mm/madvise.c +0 -0
index 35ed4ab0d7c5..5755de80a4d7 100644
@@ -1425,6 +1425,8 @@ static int madvise_vma_behavior(struct

madvise_behavior *madv_behavior)

        VM_WARN_ON_ONCE(madv_behavior->lock_mode != MADVISE_MMAP_WRITE_LOCK);

        error = madvise_update_vma(new_flags, madv_behavior);

+ if (new_flags & VM_HUGEPAGE)
+ khugepaged_enter_vma(vma);
out:
/*
* madvise() returns EAGAIN if kernel resources, such as

Quoted 8 lines

Let's keep the interface as minimal as possible.

mm/huge_memory_bpf.c line 20

Quoted 3 lines
mm/huge_memory_bpf.c new file +3 -0
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation

orders -> order?

ack

Quoted 6 lines

mm/huge_memory_bpf.c line 24

Quoted 5 lines
mm/huge_memory_bpf.c new file +4 -0
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
Quoted 6 lines

Obv as above let's drop this probably :)

mm/huge_memory_bpf.c line 26

Quoted 2 lines
mm/huge_memory_bpf.c new file +2 -0
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation

Shouldn't requested = available?

ack

Quoted 5 lines

mm/huge_memory_bpf.c line 28

Quoted 3 lines
mm/huge_memory_bpf.c new file +2 -0
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise

Not sure these 2 points are super useful.

will remove it.

Quoted 63 lines
Quoted 33 lines
mm/huge_memory_bpf.c new file +32 -0
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                        enum bpf_thp_vma_type vma_type,
+                        enum tva_type tva_type,
+                        unsigned long orders);
+
+struct bpf_thp_ops {
+     thp_order_fn_t __rcu *thp_get_order;
+};
+
+static struct bpf_thp_ops bpf_thp;
+static DEFINE_SPINLOCK(thp_ops_lock);
+
+/*
+ * Returns the original @orders if no BPF program is attached or if the
+ * suggested order is invalid.
+ */
+unsigned long bpf_hook_thp_get_orders(struct vm_area_struct *vma,
+                                   vm_flags_t vma_flags,
+                                   enum tva_type tva_type,
+                                   unsigned long orders)
+{
+     thp_order_fn_t *bpf_hook_thp_get_order;
+     unsigned long thp_orders = orders;
+     enum bpf_thp_vma_type vma_type;
+     int thp_order;
+
+     /* No BPF program is attached */

mm/huge_memory_bpf.c line 70

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+     if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+                   &transparent_hugepage_flags))
+             return orders;
+
+     if (vma_flags & VM_HUGEPAGE)
+             vma_type = BPF_THP_VM_HUGEPAGE;
+     else if (vma_flags & VM_NOHUGEPAGE)
+             vma_type = BPF_THP_VM_NOHUGEPAGE;
+     else
+             vma_type = BPF_THP_VM_NONE;

As per above, not sure this is all that useful.

mm/huge_memory_bpf.c line 83

Quoted 13 lines
mm/huge_memory_bpf.c new file +13 -0
+
+     rcu_read_lock();
+     bpf_hook_thp_get_order = rcu_dereference(bpf_thp.thp_get_order);
+     if (!bpf_hook_thp_get_order)
+             goto out;
+
+     thp_order = bpf_hook_thp_get_order(vma, vma_type, tva_type, orders);
+     if (thp_order < 0)
+             goto out;
+     /*
+      * The maximum requested order is determined by the callsite. E.g.:
+      * - PMD-mapped THP uses PMD_ORDER
+      * - mTHP uses (PMD_ORDER - 1)

I don't think this is quite right, highest_order() figures out the highest set bit, so mTHP can be PMD_ORDER - 1 or less (in theory ofc).

Quoted 3 lines

I think we can just replace this with something simpler like - 'depending on where the BPF hook is invoked, we check for either PMD order or mTHP orders (less than PMD order)' or something.

ack

Quoted 7 lines

mm/huge_memory_bpf.c line 87

Quoted 5 lines
mm/huge_memory_bpf.c new file +4 -0
+      *
+      * We must respect this upper bound to avoid undefined behavior. So the
+      * highest suggested order can't exceed the highest requested order.
+      */

I think this sentence is also unnecessary.

will remove it.

Signature · 2 lines
Regards
Yafang
#28 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 130 lines · Yafang Shao
Quoted 128 lines · Lorenzo Stoakes
Quoted 40 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

Quoted 8 lines

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

Thanks for highlighting.

Quoted 18 lines
---
 MAINTAINERS             |   1 +
 include/linux/huge_mm.h |  26 ++++-
 mm/Kconfig              |  12 ++
 mm/Makefile             |   1 +
 mm/huge_memory_bpf.c    | 243 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 280 insertions(+), 3 deletions(-)
 create mode 100644 mm/huge_memory_bpf.c
MAINTAINERS +0 -0
index 8fef05bc2224..d055a3c95300 100644

MAINTAINERS F: include/linux/huge_mm.h · line 16255

Quoted 4 lines
MAINTAINERS +1 -0
@@ -16252,6 +16252,7 @@ F:    include/linux/huge_mm.h
 F:   include/linux/khugepaged.h
 F:   include/trace/events/huge_memory.h
 F:   mm/huge_memory.c
+F:   mm/huge_memory_bpf.c

THanks!

Quoted 16 lines
MAINTAINERS +0 -0
 F:   mm/khugepaged.c
 F:   mm/mm_slot.h
 F:   tools/testing/selftests/mm/khugepaged.c
include/linux/huge_mm.h +1 -0
index 23f124493c47..f72a5fd04e4f 100644
@@ -56,6 +56,7 @@ enum transparent_hugepage_flag {
      TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
      TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
      TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
+     TRANSPARENT_HUGEPAGE_BPF_ATTACHED,      /* BPF prog is attached */
 };
 
 struct kobject;

include/linux/huge_mm.h unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, · line 277

Quoted 7 lines
include/linux/huge_mm.h +4 -0
@@ -270,6 +271,19 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
                                       enum tva_type type,
                                       unsigned long orders);
 
+#ifdef CONFIG_BPF_GET_THP_ORDER
+unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+                     enum tva_type type, unsigned long orders);

Thanks for renaming!

Quoted 13 lines
include/linux/huge_mm.h +9 -0
+#else
+static inline unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+                     enum tva_type tva_flags, unsigned long orders)
+{
+     return orders;
+}
+#endif
+
 /**
  * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma
  * @vma:  the vm area to check

include/linux/huge_mm.h unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, · line 312

Quoted 8 lines
include/linux/huge_mm.h +5 -0
@@ -291,6 +305,12 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
                                     enum tva_type type,
                                     unsigned long orders)
 {
+     unsigned long bpf_orders;
+
+     bpf_orders = bpf_hook_thp_get_orders(vma, vm_flags, type, orders);
+     if (!bpf_orders)
+             return 0;

I think it'd be easier to just do:

Quoted 6 lines
Quoted 4 lines
        /* The BPF-specified order overrides which order is selected. */
        orders &= bpf_hook_thp_get_orders(vma, vm_flags, type, orders);
        if (!orders)
                return 0;

good suggestion!

Thanks, though this does come back to 'are we masking on orders' or not.

Obviously this is predicated on that being the case.

Quoted 15 lines
Quoted 15 lines

mm/Kconfig config NO_PAGE_MAPCOUNT · line 899

Quoted 10 lines
include/linux/huge_mm.h +0 -0
 struct thpsize {
mm/Kconfig +1 -0
index d1ed839ca710..4d89d2158f10 100644
@@ -896,6 +896,18 @@ config NO_PAGE_MAPCOUNT
 
        EXPERIMENTAL because the impact of some changes is still unclear.
 
+config BPF_GET_THP_ORDER

Yeah, I think we maybe need to sledgehammer this as already Lance was confused as to the permenancy of this, and I feel that users might be too, even with the '(EXPERIMENTAL)' bit.

Quoted 7 lines
Quoted 5 lines

So maybe

config BPF_GET_THP_ORDER_EXPERIMENTAL

Just to hammer it home?

ack

Thanks!

Quoted 55 lines
Quoted 46 lines
Quoted 46 lines
mm/Kconfig +11 -0
+     bool "BPF-based THP order selection (EXPERIMENTAL)"
+     depends on TRANSPARENT_HUGEPAGE && BPF_SYSCALL
+
+     help
+       Enable dynamic THP order selection using BPF programs. This
+       experimental feature allows custom BPF logic to determine optimal
+       transparent hugepage allocation sizes at runtime.
+
+       WARNING: This feature is unstable and may change in future kernel
+       versions.
+
 endif # TRANSPARENT_HUGEPAGE
 
 # simple helper to make the code a bit easier to read
mm/Makefile +1 -0
index 21abb3353550..f180332f2ad0 100644
@@ -99,6 +99,7 @@ obj-$(CONFIG_MIGRATION) += migrate.o
 obj-$(CONFIG_NUMA) += memory-tiers.o
 obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o
 obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o
+obj-$(CONFIG_BPF_GET_THP_ORDER) += huge_memory_bpf.o
 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
 obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
mm/huge_memory_bpf.c new file +12 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
Quoted 8 lines

mm/huge_memory_bpf.c line 17

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+enum bpf_thp_vma_type {
+     BPF_THP_VM_NONE = 0,
+     BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+     BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};

I'm really not so sure how useful this is - can't a user just ascertain this from the VMA flags themselves?

Quoted 7 lines

I assume you are referring to checking flags from vma->vm_flags. There is an exception where we cannot use vma->vm_flags: in hugepage_madvise(), which calls khugepaged_enter_vma(vma, *vm_flags).

At this point, the VM_HUGEPAGE flag has not been set in vma->vm_flags yet. Therefore, we must pass the separate *vm_flags variable. Perhaps we can simplify the logic with the following change?

Ugh god.

I guess this is the workaround for the vm_flags thing right.

mm/madvise.c static int madvise_vma_behavior(struct · line 1430

Quoted 12 lines
mm/madvise.c +2 -0
index 35ed4ab0d7c5..5755de80a4d7 100644
@@ -1425,6 +1425,8 @@ static int madvise_vma_behavior(struct
 madvise_behavior *madv_behavior)
        VM_WARN_ON_ONCE(madv_behavior->lock_mode != MADVISE_MMAP_WRITE_LOCK);
 
        error = madvise_update_vma(new_flags, madv_behavior);
+       if (new_flags & VM_HUGEPAGE)
+               khugepaged_enter_vma(vma);

Hm ok, that's not such a bad idea, though ofc this should be something like:

	if (!error && (new_flags & VM_HUGEPAGE))
		khugepaged_enter_vma(vma);

And obviously dropping this khugepaged_enter_vma() from hugepage_madvise().

Quoted 7 lines
mm/madvise.c +0 -0
 out:
        /*

* madvise() returns EAGAIN if kernel resources, such as

Quoted 3 lines

Let's keep the interface as minimal as possible.

Quoted 7 lines
Quoted 5 lines

mm/huge_memory_bpf.c line 20

Quoted 3 lines
mm/huge_memory_bpf.c new file +3 -0
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation

orders -> order?

ack

Thanks!

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
mm/huge_memory_bpf.c new file +4 -0
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
Quoted 8 lines
Quoted 6 lines

Obv as above let's drop this probably :)

mm/huge_memory_bpf.c line 26

Quoted 2 lines
mm/huge_memory_bpf.c new file +2 -0
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation

Shouldn't requested = available?

ack

Thanks!

Quoted 8 lines
Quoted 6 lines

mm/huge_memory_bpf.c line 28

Quoted 4 lines
mm/huge_memory_bpf.c new file +2 -0
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise

Not sure these 2 points are super useful.

will remove it.

Thanks!

Quoted 64 lines
Quoted 64 lines
Quoted 34 lines
mm/huge_memory_bpf.c new file +32 -0
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                        enum bpf_thp_vma_type vma_type,
+                        enum tva_type tva_type,
+                        unsigned long orders);
+
+struct bpf_thp_ops {
+     thp_order_fn_t __rcu *thp_get_order;
+};
+
+static struct bpf_thp_ops bpf_thp;
+static DEFINE_SPINLOCK(thp_ops_lock);
+
+/*
+ * Returns the original @orders if no BPF program is attached or if the
+ * suggested order is invalid.
+ */
+unsigned long bpf_hook_thp_get_orders(struct vm_area_struct *vma,
+                                   vm_flags_t vma_flags,
+                                   enum tva_type tva_type,
+                                   unsigned long orders)
+{
+     thp_order_fn_t *bpf_hook_thp_get_order;
+     unsigned long thp_orders = orders;
+     enum bpf_thp_vma_type vma_type;
+     int thp_order;
+
+     /* No BPF program is attached */

mm/huge_memory_bpf.c line 70

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+     if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+                   &transparent_hugepage_flags))
+             return orders;
+
+     if (vma_flags & VM_HUGEPAGE)
+             vma_type = BPF_THP_VM_HUGEPAGE;
+     else if (vma_flags & VM_NOHUGEPAGE)
+             vma_type = BPF_THP_VM_NOHUGEPAGE;
+     else
+             vma_type = BPF_THP_VM_NONE;

As per above, not sure this is all that useful.

mm/huge_memory_bpf.c line 83

Quoted 13 lines
mm/huge_memory_bpf.c new file +13 -0
+
+     rcu_read_lock();
+     bpf_hook_thp_get_order = rcu_dereference(bpf_thp.thp_get_order);
+     if (!bpf_hook_thp_get_order)
+             goto out;
+
+     thp_order = bpf_hook_thp_get_order(vma, vma_type, tva_type, orders);
+     if (thp_order < 0)
+             goto out;
+     /*
+      * The maximum requested order is determined by the callsite. E.g.:
+      * - PMD-mapped THP uses PMD_ORDER
+      * - mTHP uses (PMD_ORDER - 1)

I don't think this is quite right, highest_order() figures out the highest set bit, so mTHP can be PMD_ORDER - 1 or less (in theory ofc).

Quoted 5 lines
Quoted 3 lines

I think we can just replace this with something simpler like - 'depending on where the BPF hook is invoked, we check for either PMD order or mTHP orders (less than PMD order)' or something.

ack

Thanks!

Quoted 10 lines
Quoted 8 lines

mm/huge_memory_bpf.c line 87

Quoted 6 lines
mm/huge_memory_bpf.c new file +4 -0
+      *
+      * We must respect this upper bound to avoid undefined behavior. So the
+      * highest suggested order can't exceed the highest requested order.
+      */

I think this sentence is also unnecessary.

will remove it.

Thanks!

Quoted 4 lines

--
Regards
Yafang

Cheers, Lorenzo

#29 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 244 lines · Lorenzo Stoakes
Quoted 130 lines · Yafang Shao
Quoted 128 lines · Lorenzo Stoakes
Quoted 40 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

Quoted 8 lines

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

Thanks for highlighting.

Quoted 18 lines
---
 MAINTAINERS             |   1 +
 include/linux/huge_mm.h |  26 ++++-
 mm/Kconfig              |  12 ++
 mm/Makefile             |   1 +
 mm/huge_memory_bpf.c    | 243 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 280 insertions(+), 3 deletions(-)
 create mode 100644 mm/huge_memory_bpf.c
MAINTAINERS +0 -0
index 8fef05bc2224..d055a3c95300 100644

MAINTAINERS F: include/linux/huge_mm.h · line 16255

Quoted 4 lines
MAINTAINERS +1 -0
@@ -16252,6 +16252,7 @@ F:    include/linux/huge_mm.h
 F:   include/linux/khugepaged.h
 F:   include/trace/events/huge_memory.h
 F:   mm/huge_memory.c
+F:   mm/huge_memory_bpf.c

THanks!

Quoted 16 lines
MAINTAINERS +0 -0
 F:   mm/khugepaged.c
 F:   mm/mm_slot.h
 F:   tools/testing/selftests/mm/khugepaged.c
include/linux/huge_mm.h +1 -0
index 23f124493c47..f72a5fd04e4f 100644
@@ -56,6 +56,7 @@ enum transparent_hugepage_flag {
      TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
      TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
      TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
+     TRANSPARENT_HUGEPAGE_BPF_ATTACHED,      /* BPF prog is attached */
 };
 
 struct kobject;

include/linux/huge_mm.h unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, · line 277

Quoted 7 lines
include/linux/huge_mm.h +4 -0
@@ -270,6 +271,19 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
                                       enum tva_type type,
                                       unsigned long orders);
 
+#ifdef CONFIG_BPF_GET_THP_ORDER
+unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+                     enum tva_type type, unsigned long orders);

Thanks for renaming!

Quoted 13 lines
include/linux/huge_mm.h +9 -0
+#else
+static inline unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+                     enum tva_type tva_flags, unsigned long orders)
+{
+     return orders;
+}
+#endif
+
 /**
  * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma
  * @vma:  the vm area to check

include/linux/huge_mm.h unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, · line 312

Quoted 8 lines
include/linux/huge_mm.h +5 -0
@@ -291,6 +305,12 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
                                     enum tva_type type,
                                     unsigned long orders)
 {
+     unsigned long bpf_orders;
+
+     bpf_orders = bpf_hook_thp_get_orders(vma, vm_flags, type, orders);
+     if (!bpf_orders)
+             return 0;

I think it'd be easier to just do:

Quoted 6 lines
Quoted 4 lines
        /* The BPF-specified order overrides which order is selected. */
        orders &= bpf_hook_thp_get_orders(vma, vm_flags, type, orders);
        if (!orders)
                return 0;

good suggestion!

Thanks, though this does come back to 'are we masking on orders' or not.

Obviously this is predicated on that being the case.

Quoted 15 lines
Quoted 15 lines

mm/Kconfig config NO_PAGE_MAPCOUNT · line 899

Quoted 10 lines
include/linux/huge_mm.h +0 -0
 struct thpsize {
mm/Kconfig +1 -0
index d1ed839ca710..4d89d2158f10 100644
@@ -896,6 +896,18 @@ config NO_PAGE_MAPCOUNT
 
        EXPERIMENTAL because the impact of some changes is still unclear.
 
+config BPF_GET_THP_ORDER

Yeah, I think we maybe need to sledgehammer this as already Lance was confused as to the permenancy of this, and I feel that users might be too, even with the '(EXPERIMENTAL)' bit.

Quoted 7 lines
Quoted 5 lines

So maybe

config BPF_GET_THP_ORDER_EXPERIMENTAL

Just to hammer it home?

ack

Thanks!

Quoted 55 lines
Quoted 46 lines
Quoted 46 lines
mm/Kconfig +11 -0
+     bool "BPF-based THP order selection (EXPERIMENTAL)"
+     depends on TRANSPARENT_HUGEPAGE && BPF_SYSCALL
+
+     help
+       Enable dynamic THP order selection using BPF programs. This
+       experimental feature allows custom BPF logic to determine optimal
+       transparent hugepage allocation sizes at runtime.
+
+       WARNING: This feature is unstable and may change in future kernel
+       versions.
+
 endif # TRANSPARENT_HUGEPAGE
 
 # simple helper to make the code a bit easier to read
mm/Makefile +1 -0
index 21abb3353550..f180332f2ad0 100644
@@ -99,6 +99,7 @@ obj-$(CONFIG_MIGRATION) += migrate.o
 obj-$(CONFIG_NUMA) += memory-tiers.o
 obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o
 obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o
+obj-$(CONFIG_BPF_GET_THP_ORDER) += huge_memory_bpf.o
 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
 obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
mm/huge_memory_bpf.c new file +12 -0
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
Quoted 8 lines

mm/huge_memory_bpf.c line 17

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+enum bpf_thp_vma_type {
+     BPF_THP_VM_NONE = 0,
+     BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+     BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};

I'm really not so sure how useful this is - can't a user just ascertain this from the VMA flags themselves?

Quoted 7 lines

I assume you are referring to checking flags from vma->vm_flags. There is an exception where we cannot use vma->vm_flags: in hugepage_madvise(), which calls khugepaged_enter_vma(vma, *vm_flags).

At this point, the VM_HUGEPAGE flag has not been set in vma->vm_flags yet. Therefore, we must pass the separate *vm_flags variable. Perhaps we can simplify the logic with the following change?

Ugh god.

I guess this is the workaround for the vm_flags thing right.

Quoted 9 lines
mm/madvise.c +0 -0
index 35ed4ab0d7c5..5755de80a4d7 100644
@@ -1425,6 +1425,8 @@ static int madvise_vma_behavior(struct
 madvise_behavior *madv_behavior)
        VM_WARN_ON_ONCE(madv_behavior->lock_mode != MADVISE_MMAP_WRITE_LOCK);
 
Quoted 8 lines

mm/madvise.c static int madvise_vma_behavior(struct · line 1430

Quoted 3 lines
mm/madvise.c +2 -0
        error = madvise_update_vma(new_flags, madv_behavior);
+       if (new_flags & VM_HUGEPAGE)
+               khugepaged_enter_vma(vma);

Hm ok, that's not such a bad idea, though ofc this should be something like:

        if (!error && (new_flags & VM_HUGEPAGE))
                khugepaged_enter_vma(vma);

ack

Quoted 2 lines

And obviously dropping this khugepaged_enter_vma() from hugepage_madvise().

Thanks for the reminder.

Signature · 2 lines
Regards
Yafang
#30 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

In reply to Yafang Shao
Quoted 5 lines · Yafang Shao
mm/huge_memory_bpf.c new file +0 -0
new file mode 100644
index 000000000000..525ee22ab598

[snip]

Quoted 30 lines
mm/huge_memory_bpf.c new file +30 -0
+unsigned long bpf_hook_thp_get_orders(struct vm_area_struct *vma,
+				      vm_flags_t vma_flags,
+				      enum tva_type tva_type,
+				      unsigned long orders)
+{
+	thp_order_fn_t *bpf_hook_thp_get_order;
+	unsigned long thp_orders = orders;
+	enum bpf_thp_vma_type vma_type;
+	int thp_order;
+
+	/* No BPF program is attached */
+	if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+		      &transparent_hugepage_flags))
+		return orders;
+
+	if (vma_flags & VM_HUGEPAGE)
+		vma_type = BPF_THP_VM_HUGEPAGE;
+	else if (vma_flags & VM_NOHUGEPAGE)
+		vma_type = BPF_THP_VM_NOHUGEPAGE;
+	else
+		vma_type = BPF_THP_VM_NONE;
+
+	rcu_read_lock();
+	bpf_hook_thp_get_order = rcu_dereference(bpf_thp.thp_get_order);
+	if (!bpf_hook_thp_get_order)
+		goto out;
+
+	thp_order = bpf_hook_thp_get_order(vma, vma_type, tva_type, orders);
+	if (thp_order < 0)
+		goto out;

mm/huge_memory_bpf.c line 89

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+	/*
+	 * The maximum requested order is determined by the callsite. E.g.:
+	 * - PMD-mapped THP uses PMD_ORDER
+	 * - mTHP uses (PMD_ORDER - 1)
+	 *
+	 * We must respect this upper bound to avoid undefined behavior. So the
+	 * highest suggested order can't exceed the highest requested order.
+	 */
+	if (thp_order <= highest_order(orders))
+		thp_orders = BIT(thp_order);

OK so looking at Lance's reply re: setting 0 and what we're doing here in general - this seems a bit weird to me.

Shouldn't orders be specifying a _mask_ as to which orders are _available_, rather than allowing a user to specify an arbitrary order?

So if you're a position where the only possible order is PMD sized, now this would let you arbitrarily select an mTHP right? That does no seem correct.

And as per Lance, if we cannot satisfy the requested order, we shouldn't fall back to available orders, we should take that as a signal that we cannot have THP at all.

So shouldn't this just be:

	thp_orders = orders & BIT(thp_order);

? Or am I missing something here?

mm/huge_memory_bpf.c line 94

Quoted 5 lines
mm/huge_memory_bpf.c new file +5 -0
+
+out:
+	rcu_read_unlock();
+	return thp_orders;
+}

Cheers, Lorenzo

#31 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 60 lines · Lorenzo Stoakes
Quoted 5 lines · Yafang Shao
mm/huge_memory_bpf.c new file +0 -0
new file mode 100644
index 000000000000..525ee22ab598

[snip]

Quoted 30 lines
mm/huge_memory_bpf.c new file +30 -0
+unsigned long bpf_hook_thp_get_orders(struct vm_area_struct *vma,
+                                   vm_flags_t vma_flags,
+                                   enum tva_type tva_type,
+                                   unsigned long orders)
+{
+     thp_order_fn_t *bpf_hook_thp_get_order;
+     unsigned long thp_orders = orders;
+     enum bpf_thp_vma_type vma_type;
+     int thp_order;
+
+     /* No BPF program is attached */
+     if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+                   &transparent_hugepage_flags))
+             return orders;
+
+     if (vma_flags & VM_HUGEPAGE)
+             vma_type = BPF_THP_VM_HUGEPAGE;
+     else if (vma_flags & VM_NOHUGEPAGE)
+             vma_type = BPF_THP_VM_NOHUGEPAGE;
+     else
+             vma_type = BPF_THP_VM_NONE;
+
+     rcu_read_lock();
+     bpf_hook_thp_get_order = rcu_dereference(bpf_thp.thp_get_order);
+     if (!bpf_hook_thp_get_order)
+             goto out;
+
+     thp_order = bpf_hook_thp_get_order(vma, vma_type, tva_type, orders);
+     if (thp_order < 0)
+             goto out;

mm/huge_memory_bpf.c line 89

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+     /*
+      * The maximum requested order is determined by the callsite. E.g.:
+      * - PMD-mapped THP uses PMD_ORDER
+      * - mTHP uses (PMD_ORDER - 1)
+      *
+      * We must respect this upper bound to avoid undefined behavior. So the
+      * highest suggested order can't exceed the highest requested order.
+      */
+     if (thp_order <= highest_order(orders))
+             thp_orders = BIT(thp_order);

OK so looking at Lance's reply re: setting 0 and what we're doing here in general - this seems a bit weird to me.

Shouldn't orders be specifying a _mask_ as to which orders are _available_, rather than allowing a user to specify an arbitrary order?

So if you're a position where the only possible order is PMD sized, now this would let you arbitrarily select an mTHP right? That does no seem correct.

Quoted 7 lines

And as per Lance, if we cannot satisfy the requested order, we shouldn't fall back to available orders, we should take that as a signal that we cannot have THP at all.

So shouldn't this just be:

        thp_orders = orders & BIT(thp_order);

That's better. I will change it.

Signature · 2 lines
Regards
Yafang
#32 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 66 lines · Yafang Shao
Quoted 64 lines · Lorenzo Stoakes
Quoted 5 lines · Yafang Shao
mm/huge_memory_bpf.c new file +0 -0
new file mode 100644
index 000000000000..525ee22ab598

[snip]

Quoted 30 lines
mm/huge_memory_bpf.c new file +30 -0
+unsigned long bpf_hook_thp_get_orders(struct vm_area_struct *vma,
+                                   vm_flags_t vma_flags,
+                                   enum tva_type tva_type,
+                                   unsigned long orders)
+{
+     thp_order_fn_t *bpf_hook_thp_get_order;
+     unsigned long thp_orders = orders;
+     enum bpf_thp_vma_type vma_type;
+     int thp_order;
+
+     /* No BPF program is attached */
+     if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+                   &transparent_hugepage_flags))
+             return orders;
+
+     if (vma_flags & VM_HUGEPAGE)
+             vma_type = BPF_THP_VM_HUGEPAGE;
+     else if (vma_flags & VM_NOHUGEPAGE)
+             vma_type = BPF_THP_VM_NOHUGEPAGE;
+     else
+             vma_type = BPF_THP_VM_NONE;
+
+     rcu_read_lock();
+     bpf_hook_thp_get_order = rcu_dereference(bpf_thp.thp_get_order);
+     if (!bpf_hook_thp_get_order)
+             goto out;
+
+     thp_order = bpf_hook_thp_get_order(vma, vma_type, tva_type, orders);
+     if (thp_order < 0)
+             goto out;

mm/huge_memory_bpf.c line 89

Quoted 10 lines
mm/huge_memory_bpf.c new file +10 -0
+     /*
+      * The maximum requested order is determined by the callsite. E.g.:
+      * - PMD-mapped THP uses PMD_ORDER
+      * - mTHP uses (PMD_ORDER - 1)
+      *
+      * We must respect this upper bound to avoid undefined behavior. So the
+      * highest suggested order can't exceed the highest requested order.
+      */
+     if (thp_order <= highest_order(orders))
+             thp_orders = BIT(thp_order);

OK so looking at Lance's reply re: setting 0 and what we're doing here in general - this seems a bit weird to me.

Shouldn't orders be specifying a _mask_ as to which orders are _available_, rather than allowing a user to specify an arbitrary order?

So if you're a position where the only possible order is PMD sized, now this would let you arbitrarily select an mTHP right? That does no seem correct.

And as per Lance, if we cannot satisfy the requested order, we shouldn't fall back to available orders, we should take that as a signal that we cannot have THP at all.

Quoted 5 lines
Quoted 3 lines

So shouldn't this just be:

        thp_orders = orders & BIT(thp_order);

That's better. I will change it.

Great I agree (obviously :P)

Thanks!

Quoted 4 lines

--
Regards
Yafang

Cheers, Lorenzo

reviewer Tested-by on v7 2/10
#33 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

In reply to Yafang Shao
Quoted 46 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

Quoted 7 lines

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

I've tested this patch on my machine, and it works as expected. Using BPF hooks to control THP is a great step forward!

This work also inspires some ideas for another useful hook for THP that I might propose in the future, once this series is settled and merged ;)

Cheers,
Lance

Quoted 370 lines
---
 MAINTAINERS             |   1 +
 include/linux/huge_mm.h |  26 ++++-
 mm/Kconfig              |  12 ++
 mm/Makefile             |   1 +
 mm/huge_memory_bpf.c    | 243 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 280 insertions(+), 3 deletions(-)
 create mode 100644 mm/huge_memory_bpf.c
MAINTAINERS +1 -0
index 8fef05bc2224..d055a3c95300 100644
@@ -16252,6 +16252,7 @@ F:      include/linux/huge_mm.h
 F:     include/linux/khugepaged.h
 F:     include/trace/events/huge_memory.h
 F:     mm/huge_memory.c
+F:     mm/huge_memory_bpf.c
 F:     mm/khugepaged.c
 F:     mm/mm_slot.h
 F:     tools/testing/selftests/mm/khugepaged.c
include/linux/huge_mm.h +23 -3
index 23f124493c47..f72a5fd04e4f 100644
@@ -56,6 +56,7 @@ enum transparent_hugepage_flag {
        TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
        TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
        TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
+       TRANSPARENT_HUGEPAGE_BPF_ATTACHED,      /* BPF prog is attached */
 };
 
 struct kobject;
@@ -270,6 +271,19 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
                                         enum tva_type type,
                                         unsigned long orders);
 
+#ifdef CONFIG_BPF_GET_THP_ORDER
+unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+                       enum tva_type type, unsigned long orders);
+#else
+static inline unsigned long
+bpf_hook_thp_get_orders(struct vm_area_struct *vma, vm_flags_t vma_flags,
+                       enum tva_type tva_flags, unsigned long orders)
+{
+       return orders;
+}
+#endif
+
 /**
  * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma
  * @vma:  the vm area to check
@@ -291,6 +305,12 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
                                       enum tva_type type,
                                       unsigned long orders)
 {
+       unsigned long bpf_orders;
+
+       bpf_orders = bpf_hook_thp_get_orders(vma, vm_flags, type, orders);
+       if (!bpf_orders)
+               return 0;
+
        /*
         * Optimization to check if required orders are enabled early. Only
         * forced collapse ignores sysfs configs.
@@ -304,12 +324,12 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
                    ((vm_flags & VM_HUGEPAGE) && hugepage_global_enabled()))
                        mask |= READ_ONCE(huge_anon_orders_inherit);
 
-               orders &= mask;
-               if (!orders)
+               bpf_orders &= mask;
+               if (!bpf_orders)
                        return 0;
        }
 
-       return __thp_vma_allowable_orders(vma, vm_flags, type, orders);
+       return __thp_vma_allowable_orders(vma, vm_flags, type, bpf_orders);
 }
 
 struct thpsize {
mm/Kconfig +12 -0
index d1ed839ca710..4d89d2158f10 100644
@@ -896,6 +896,18 @@ config NO_PAGE_MAPCOUNT
 
          EXPERIMENTAL because the impact of some changes is still unclear.
 
+config BPF_GET_THP_ORDER
+       bool "BPF-based THP order selection (EXPERIMENTAL)"
+       depends on TRANSPARENT_HUGEPAGE && BPF_SYSCALL
+
+       help
+         Enable dynamic THP order selection using BPF programs. This
+         experimental feature allows custom BPF logic to determine optimal
+         transparent hugepage allocation sizes at runtime.
+
+         WARNING: This feature is unstable and may change in future kernel
+         versions.
+
 endif # TRANSPARENT_HUGEPAGE
 
 # simple helper to make the code a bit easier to read
mm/Makefile +1 -0
index 21abb3353550..f180332f2ad0 100644
@@ -99,6 +99,7 @@ obj-$(CONFIG_MIGRATION) += migrate.o
 obj-$(CONFIG_NUMA) += memory-tiers.o
 obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o
 obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o
+obj-$(CONFIG_BPF_GET_THP_ORDER) += huge_memory_bpf.o
 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
 obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
mm/huge_memory_bpf.c new file +243 -1
new file mode 100644
index 000000000000..525ee22ab598
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF-based THP policy management
+ *
+ * Author: Yafang Shao <[email protected]>
+ */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+enum bpf_thp_vma_type {
+       BPF_THP_VM_NONE = 0,
+       BPF_THP_VM_HUGEPAGE,    /* VM_HUGEPAGE */
+       BPF_THP_VM_NOHUGEPAGE,  /* VM_NOHUGEPAGE */
+};
+
+/**
+ * @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
+ * @vma: vm_area_struct associated with the THP allocation
+ * @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
+ *            BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
+ *            neither is set.
+ * @tva_type: TVA type for current @vma
+ * @orders: Bitmask of requested THP orders for this allocation
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise
+ *
+ * Return: The suggested THP order from the BPF program for allocation. It will
+ *         not exceed the highest requested order in @orders. Return -1 to
+ *         indicate that the original requested @orders should remain unchanged.
+ */
+typedef int thp_order_fn_t(struct vm_area_struct *vma,
+                          enum bpf_thp_vma_type vma_type,
+                          enum tva_type tva_type,
+                          unsigned long orders);
+
+struct bpf_thp_ops {
+       thp_order_fn_t __rcu *thp_get_order;
+};
+
+static struct bpf_thp_ops bpf_thp;
+static DEFINE_SPINLOCK(thp_ops_lock);
+
+/*
+ * Returns the original @orders if no BPF program is attached or if the
+ * suggested order is invalid.
+ */
+unsigned long bpf_hook_thp_get_orders(struct vm_area_struct *vma,
+                                     vm_flags_t vma_flags,
+                                     enum tva_type tva_type,
+                                     unsigned long orders)
+{
+       thp_order_fn_t *bpf_hook_thp_get_order;
+       unsigned long thp_orders = orders;
+       enum bpf_thp_vma_type vma_type;
+       int thp_order;
+
+       /* No BPF program is attached */
+       if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+                     &transparent_hugepage_flags))
+               return orders;
+
+       if (vma_flags & VM_HUGEPAGE)
+               vma_type = BPF_THP_VM_HUGEPAGE;
+       else if (vma_flags & VM_NOHUGEPAGE)
+               vma_type = BPF_THP_VM_NOHUGEPAGE;
+       else
+               vma_type = BPF_THP_VM_NONE;
+
+       rcu_read_lock();
+       bpf_hook_thp_get_order = rcu_dereference(bpf_thp.thp_get_order);
+       if (!bpf_hook_thp_get_order)
+               goto out;
+
+       thp_order = bpf_hook_thp_get_order(vma, vma_type, tva_type, orders);
+       if (thp_order < 0)
+               goto out;
+       /*
+        * The maximum requested order is determined by the callsite. E.g.:
+        * - PMD-mapped THP uses PMD_ORDER
+        * - mTHP uses (PMD_ORDER - 1)
+        *
+        * We must respect this upper bound to avoid undefined behavior. So the
+        * highest suggested order can't exceed the highest requested order.
+        */
+       if (thp_order <= highest_order(orders))
+               thp_orders = BIT(thp_order);
+
+out:
+       rcu_read_unlock();
+       return thp_orders;
+}
+
+static bool bpf_thp_ops_is_valid_access(int off, int size,
+                                       enum bpf_access_type type,
+                                       const struct bpf_prog *prog,
+                                       struct bpf_insn_access_aux *info)
+{
+       return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
+}
+
+static const struct bpf_func_proto *
+bpf_thp_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
+{
+       return bpf_base_func_proto(func_id, prog);
+}
+
+static const struct bpf_verifier_ops thp_bpf_verifier_ops = {
+       .get_func_proto = bpf_thp_get_func_proto,
+       .is_valid_access = bpf_thp_ops_is_valid_access,
+};
+
+static int bpf_thp_init(struct btf *btf)
+{
+       return 0;
+}
+
+static int bpf_thp_check_member(const struct btf_type *t,
+                               const struct btf_member *member,
+                               const struct bpf_prog *prog)
+{
+       /* The call site operates under RCU protection. */
+       if (prog->sleepable)
+               return -EINVAL;
+       return 0;
+}
+
+static int bpf_thp_init_member(const struct btf_type *t,
+                              const struct btf_member *member,
+                              void *kdata, const void *udata)
+{
+       return 0;
+}
+
+static int bpf_thp_reg(void *kdata, struct bpf_link *link)
+{
+       struct bpf_thp_ops *ops = kdata;
+
+       spin_lock(&thp_ops_lock);
+       if (test_and_set_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+                            &transparent_hugepage_flags)) {
+               spin_unlock(&thp_ops_lock);
+               return -EBUSY;
+       }
+       WARN_ON_ONCE(rcu_access_pointer(bpf_thp.thp_get_order));
+       rcu_assign_pointer(bpf_thp.thp_get_order, ops->thp_get_order);
+       spin_unlock(&thp_ops_lock);
+       return 0;
+}
+
+static void bpf_thp_unreg(void *kdata, struct bpf_link *link)
+{
+       thp_order_fn_t *old_fn;
+
+       spin_lock(&thp_ops_lock);
+       clear_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED, &transparent_hugepage_flags);
+       old_fn = rcu_replace_pointer(bpf_thp.thp_get_order, NULL,
+                                    lockdep_is_held(&thp_ops_lock));
+       WARN_ON_ONCE(!old_fn);
+       spin_unlock(&thp_ops_lock);
+
+       synchronize_rcu();
+}
+
+static int bpf_thp_update(void *kdata, void *old_kdata, struct bpf_link *link)
+{
+       thp_order_fn_t *old_fn, *new_fn;
+       struct bpf_thp_ops *old = old_kdata;
+       struct bpf_thp_ops *ops = kdata;
+       int ret = 0;
+
+       if (!ops || !old)
+               return -EINVAL;
+
+       spin_lock(&thp_ops_lock);
+       /* The prog has aleady been removed. */
+       if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+                     &transparent_hugepage_flags)) {
+               ret = -ENOENT;
+               goto out;
+       }
+
+       new_fn = rcu_dereference(ops->thp_get_order);
+       old_fn = rcu_replace_pointer(bpf_thp.thp_get_order, new_fn,
+                                    lockdep_is_held(&thp_ops_lock));
+       WARN_ON_ONCE(!old_fn || !new_fn);
+
+out:
+       spin_unlock(&thp_ops_lock);
+       if (!ret)
+               synchronize_rcu();
+       return ret;
+}
+
+static int bpf_thp_validate(void *kdata)
+{
+       struct bpf_thp_ops *ops = kdata;
+
+       if (!ops->thp_get_order) {
+               pr_err("bpf_thp: required ops isn't implemented\n");
+               return -EINVAL;
+       }
+       return 0;
+}
+
+static int bpf_thp_get_order(struct vm_area_struct *vma,
+                            enum bpf_thp_vma_type vma_type,
+                            enum tva_type tva_type,
+                            unsigned long orders)
+{
+       return -1;
+}
+
+static struct bpf_thp_ops __bpf_thp_ops = {
+       .thp_get_order = (thp_order_fn_t __rcu *)bpf_thp_get_order,
+};
+
+static struct bpf_struct_ops bpf_bpf_thp_ops = {
+       .verifier_ops = &thp_bpf_verifier_ops,
+       .init = bpf_thp_init,
+       .check_member = bpf_thp_check_member,
+       .init_member = bpf_thp_init_member,
+       .reg = bpf_thp_reg,
+       .unreg = bpf_thp_unreg,
+       .update = bpf_thp_update,
+       .validate = bpf_thp_validate,
+       .cfi_stubs = &__bpf_thp_ops,
+       .owner = THIS_MODULE,
+       .name = "bpf_thp_ops",
+};
+
+static int __init bpf_thp_ops_init(void)
+{
+       int err;
+
+       err = register_bpf_struct_ops(&bpf_bpf_thp_ops, bpf_thp_ops);
+       if (err)
+               pr_err("bpf_thp: Failed to register struct_ops (%d)\n", err);
+       return err;
+}
+late_initcall(bpf_thp_ops_init);
--

2.47.3

#34 lore raw

Re: [PATCH v7 mm-new 02/10] mm: thp: add support for BPF based THP order selection

Quoted 52 lines · Lance Yang
Quoted 50 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook bpf_hook_thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:
- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The kernel API of this new BPF hook is as follows,

/**
* @thp_order_fn_t: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE if
* neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/
typedef int thp_order_fn_t(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task.

This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled. Note that this capability is currently unstable and may undergo significant changes—including potential removal—in future kernel versions.

Quoted 8 lines
Quoted 3 lines

I've tested this patch on my machine, and it works as expected. Using BPF hooks to control THP is a great step forward!

Thanks for your test. I will post a new version ASAP.

Quoted 3 lines

This work also inspires some ideas for another useful hook for THP that I might propose in the future, once this series is settled and merged ;)

Excited to see it!

Signature · 2 lines
Regards
Yafang
#35 lore raw

PATCH v7 3/10 mm: thp: decouple THP allocation between swap and page fault paths

In reply to Yafang Shao

The new BPF capability enables finer-grained THP policy decisions by introducing separate handling for swap faults versus normal page faults.

As highlighted by Barry:

We’ve observed that swapping in large folios can lead to more swap thrashing for some workloads- e.g. kernel build. Consequently, some workloads might prefer swapping in smaller folios than those allocated by alloc_anon_folio().

While prtcl() could potentially be extended to leverage this new policy, doing so would require modifications to the uAPI.

---
 include/linux/huge_mm.h | 3 ++-
 mm/huge_memory.c        | 2 +-
 mm/memory.c             | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
include/linux/huge_mm.h +2 -1
index f72a5fd04e4f..b9742453806f 100644
@@ -97,9 +97,10 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr;
 
 enum tva_type {
 	TVA_SMAPS,		/* Exposing "THPeligible:" in smaps. */
-	TVA_PAGEFAULT,		/* Serving a page fault. */
+	TVA_PAGEFAULT,		/* Serving a non-swap page fault. */
 	TVA_KHUGEPAGED,		/* Khugepaged collapse. */
 	TVA_FORCED_COLLAPSE,	/* Forced collapse (e.g. MADV_COLLAPSE). */
+	TVA_SWAP,		/* Serving a swap */
 };
 
 #define thp_vma_allowable_order(vma, vm_flags, type, order) \
mm/huge_memory.c +1 -1
index 26cedfcd7418..523153d21a41 100644
@@ -103,7 +103,7 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 					 unsigned long orders)
 {
 	const bool smaps = type == TVA_SMAPS;
-	const bool in_pf = type == TVA_PAGEFAULT;
+	const bool in_pf = (type == TVA_PAGEFAULT || type == TVA_SWAP);
 	const bool forced_collapse = type == TVA_FORCED_COLLAPSE;
 	unsigned long supported_orders;
 
mm/memory.c +1 -1
index d9de6c056179..d8819cac7930 100644
@@ -4515,7 +4515,7 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 	 * Get a list of all the (large) orders below PMD_ORDER that are enabled
 	 * and suitable for swapping THP.
 	 */
-	orders = thp_vma_allowable_orders(vma, vma->vm_flags, TVA_PAGEFAULT,
+	orders = thp_vma_allowable_orders(vma, vma->vm_flags, TVA_SWAP,
 					  BIT(PMD_ORDER) - 1);
 	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
 	orders = thp_swap_suitable_orders(swp_offset(entry),
Signature · 1 line
2.47.3
Reviewed-by on v7 3/10
#36 lore raw

Re: [PATCH v7 mm-new 03/10] mm: thp: decouple THP allocation between swap and page fault paths

Quoted 10 lines · Yafang Shao

The new BPF capability enables finer-grained THP policy decisions by introducing separate handling for swap faults versus normal page faults.

As highlighted by Barry:

We’ve observed that swapping in large folios can lead to more swap thrashing for some workloads- e.g. kernel build. Consequently, some workloads might prefer swapping in smaller folios than those allocated by alloc_anon_folio().

Quoted 4 lines

While prtcl() could potentially be extended to leverage this new policy, doing so would require modifications to the uAPI.

Other than nits, these seems fine, so:

Quoted 12 lines
Cc Barry Song <[email protected]>
---
 include/linux/huge_mm.h | 3 ++-
 mm/huge_memory.c        | 2 +-
 mm/memory.c             | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
include/linux/huge_mm.h +0 -0
index f72a5fd04e4f..b9742453806f 100644

include/linux/huge_mm.h extern struct kobj_attribute thpsize_shmem_enabled_attr; · line 103

Quoted 8 lines
include/linux/huge_mm.h +2 -1
@@ -97,9 +97,10 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr;
 
 enum tva_type {
 	TVA_SMAPS,		/* Exposing "THPeligible:" in smaps. */
-	TVA_PAGEFAULT,		/* Serving a page fault. */
+	TVA_PAGEFAULT,		/* Serving a non-swap page fault. */
 	TVA_KHUGEPAGED,		/* Khugepaged collapse. */
 	TVA_FORCED_COLLAPSE,	/* Forced collapse (e.g. MADV_COLLAPSE). */
+	TVA_SWAP,		/* Serving a swap */

Serving a swap what? :) I think TVA_SWAP_PAGEFAULT would be better here right? And 'serving a swap page fault'.

Quoted 32 lines
include/linux/huge_mm.h +0 -0
 };
 
 #define thp_vma_allowable_order(vma, vm_flags, type, order) \
mm/huge_memory.c +1 -1
index 26cedfcd7418..523153d21a41 100644
@@ -103,7 +103,7 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 					 unsigned long orders)
 {
 	const bool smaps = type == TVA_SMAPS;
-	const bool in_pf = type == TVA_PAGEFAULT;
+	const bool in_pf = (type == TVA_PAGEFAULT || type == TVA_SWAP);
 	const bool forced_collapse = type == TVA_FORCED_COLLAPSE;
 	unsigned long supported_orders;
 
mm/memory.c +1 -2
index d9de6c056179..d8819cac7930 100644
@@ -4515,7 +4515,7 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 	 * Get a list of all the (large) orders below PMD_ORDER that are enabled
 	 * and suitable for swapping THP.
 	 */
-	orders = thp_vma_allowable_orders(vma, vma->vm_flags, TVA_PAGEFAULT,
+	orders = thp_vma_allowable_orders(vma, vma->vm_flags, TVA_SWAP,
 					  BIT(PMD_ORDER) - 1);
 	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
 	orders = thp_swap_suitable_orders(swp_offset(entry),
--

2.47.3

#37 lore raw

Re: [PATCH v7 mm-new 03/10] mm: thp: decouple THP allocation between swap and page fault paths

Quoted 34 lines · Lorenzo Stoakes
Quoted 10 lines · Yafang Shao

The new BPF capability enables finer-grained THP policy decisions by introducing separate handling for swap faults versus normal page faults.

As highlighted by Barry:

We’ve observed that swapping in large folios can lead to more swap thrashing for some workloads- e.g. kernel build. Consequently, some workloads might prefer swapping in smaller folios than those allocated by alloc_anon_folio().

Quoted 4 lines

While prtcl() could potentially be extended to leverage this new policy, doing so would require modifications to the uAPI.

Other than nits, these seems fine, so:

Quoted 13 lines
Cc Barry Song <[email protected]>
---
 include/linux/huge_mm.h | 3 ++-
 mm/huge_memory.c        | 2 +-
 mm/memory.c             | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
include/linux/huge_mm.h +0 -0
index f72a5fd04e4f..b9742453806f 100644
@@ -97,9 +97,10 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr;
 
Quoted 10 lines

include/linux/huge_mm.h extern struct kobj_attribute thpsize_shmem_enabled_attr; · line 103

Quoted 7 lines
include/linux/huge_mm.h +2 -1
 enum tva_type {
      TVA_SMAPS,              /* Exposing "THPeligible:" in smaps. */
-     TVA_PAGEFAULT,          /* Serving a page fault. */
+     TVA_PAGEFAULT,          /* Serving a non-swap page fault. */
      TVA_KHUGEPAGED,         /* Khugepaged collapse. */
      TVA_FORCED_COLLAPSE,    /* Forced collapse (e.g. MADV_COLLAPSE). */
+     TVA_SWAP,               /* Serving a swap */

Serving a swap what? :) I think TVA_SWAP_PAGEFAULT would be better here right? And 'serving a swap page fault'.

will change it. Thanks for your suggestion.

Signature · 2 lines
Regards
Yafang
#38 lore raw

Re: [PATCH v7 mm-new 03/10] mm: thp: decouple THP allocation between swap and page fault paths

Quoted 47 lines · Yafang Shao
Quoted 34 lines · Lorenzo Stoakes
Quoted 10 lines · Yafang Shao

The new BPF capability enables finer-grained THP policy decisions by introducing separate handling for swap faults versus normal page faults.

As highlighted by Barry:

We’ve observed that swapping in large folios can lead to more swap thrashing for some workloads- e.g. kernel build. Consequently, some workloads might prefer swapping in smaller folios than those allocated by alloc_anon_folio().

Quoted 4 lines

While prtcl() could potentially be extended to leverage this new policy, doing so would require modifications to the uAPI.

Other than nits, these seems fine, so:

Quoted 13 lines
Cc Barry Song <[email protected]>
---
 include/linux/huge_mm.h | 3 ++-
 mm/huge_memory.c        | 2 +-
 mm/memory.c             | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
include/linux/huge_mm.h +0 -0
index f72a5fd04e4f..b9742453806f 100644
@@ -97,9 +97,10 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr;
 
Quoted 10 lines

include/linux/huge_mm.h extern struct kobj_attribute thpsize_shmem_enabled_attr; · line 103

Quoted 7 lines
include/linux/huge_mm.h +2 -1
 enum tva_type {
      TVA_SMAPS,              /* Exposing "THPeligible:" in smaps. */
-     TVA_PAGEFAULT,          /* Serving a page fault. */
+     TVA_PAGEFAULT,          /* Serving a non-swap page fault. */
      TVA_KHUGEPAGED,         /* Khugepaged collapse. */
      TVA_FORCED_COLLAPSE,    /* Forced collapse (e.g. MADV_COLLAPSE). */
+     TVA_SWAP,               /* Serving a swap */

Serving a swap what? :) I think TVA_SWAP_PAGEFAULT would be better here right? And 'serving a swap page fault'.

Quoted 1 line

will change it. Thanks for your suggestion.

Thanks!

Quoted 4 lines

--
Regards
Yafang

Cheers, Lorenzo

#39 lore raw

PATCH v7 4/10 mm: thp: enable THP allocation exclusively through khugepaged

In reply to Yafang Shao

Currently, THP allocation cannot be restricted to khugepaged alone while being disabled in the page fault path. This limitation exists because disabling THP allocation during page faults also prevents the execution of khugepaged_enter_vma() in that path.

With the introduction of BPF, we can now implement THP policies based on different TVA types. This patch adjusts the logic to support this new capability.

While we could also extend prtcl() to utilize this new policy, such a change would require a uAPI modification.

---
 mm/huge_memory.c |  1 -
 mm/memory.c      | 13 ++++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)
mm/huge_memory.c +0 -1
index 523153d21a41..1e9e7b32e2cf 100644
@@ -1346,7 +1346,6 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
 	ret = vmf_anon_prepare(vmf);
 	if (ret)
 		return ret;
-	khugepaged_enter_vma(vma, vma->vm_flags);
 
 	if (!(vmf->flags & FAULT_FLAG_WRITE) &&
 			!mm_forbids_zeropage(vma->vm_mm) &&
mm/memory.c +8 -5
index d8819cac7930..d0609dc1e371 100644
@@ -6289,11 +6289,14 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
 	if (pud_trans_unstable(vmf.pud))
 		goto retry_pud;
 
-	if (pmd_none(*vmf.pmd) &&
-	    thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PMD_ORDER)) {
-		ret = create_huge_pmd(&vmf);
-		if (!(ret & VM_FAULT_FALLBACK))
-			return ret;
+	if (pmd_none(*vmf.pmd)) {
+		if (vma_is_anonymous(vma))
+			khugepaged_enter_vma(vma, vm_flags);
+		if (thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PMD_ORDER)) {
+			ret = create_huge_pmd(&vmf);
+			if (!(ret & VM_FAULT_FALLBACK))
+				return ret;
+		}
 	} else {
 		vmf.orig_pmd = pmdp_get_lockless(vmf.pmd);
 
Signature · 1 line
2.47.3
reviewer Acked-by on v7 4/10
#40 lore raw

Re: [PATCH v7 mm-new 04/10] mm: thp: enable THP allocation exclusively through khugepaged

Quoted 37 lines · Yafang Shao

Currently, THP allocation cannot be restricted to khugepaged alone while being disabled in the page fault path. This limitation exists because disabling THP allocation during page faults also prevents the execution of khugepaged_enter_vma() in that path.

With the introduction of BPF, we can now implement THP policies based on different TVA types. This patch adjusts the logic to support this new capability.

While we could also extend prtcl() to utilize this new policy, such a change would require a uAPI modification.

---
 mm/huge_memory.c |  1 -
 mm/memory.c      | 13 ++++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)
mm/huge_memory.c +0 -1
index 523153d21a41..1e9e7b32e2cf 100644
@@ -1346,7 +1346,6 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
        ret = vmf_anon_prepare(vmf);
        if (ret)
                return ret;
-       khugepaged_enter_vma(vma, vma->vm_flags);
 
        if (!(vmf->flags & FAULT_FLAG_WRITE) &&
                        !mm_forbids_zeropage(vma->vm_mm) &&
mm/memory.c +0 -0
index d8819cac7930..d0609dc1e371 100644
@@ -6289,11 +6289,14 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
        if (pud_trans_unstable(vmf.pud))

mm/memory.c static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, · line 6294

Quoted 10 lines
mm/memory.c +3 -5
                goto retry_pud;
 
-       if (pmd_none(*vmf.pmd) &&
-           thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PMD_ORDER)) {
-               ret = create_huge_pmd(&vmf);
-               if (!(ret & VM_FAULT_FALLBACK))
-                       return ret;
+       if (pmd_none(*vmf.pmd)) {
+               if (vma_is_anonymous(vma))
+                       khugepaged_enter_vma(vma, vm_flags);

Hmm... I'm a bit confused about the different conditions for calling khugepaged_enter_vma(). It's sometimes called for anonymous VMAs, other times ONLY for non-anonymous, and sometimes unconditionally ;)

Anyway, this isn't a blocker, just something I noticed. I might try to simplify that down the road.

Cheers,
Lance

Quoted 12 lines
mm/memory.c +5 -1
+               if (thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PMD_ORDER)) {
+                       ret = create_huge_pmd(&vmf);
+                       if (!(ret & VM_FAULT_FALLBACK))
+                               return ret;
+               }
        } else {
                vmf.orig_pmd = pmdp_get_lockless(vmf.pmd);
 
--

2.47.3

#41 lore raw

Re: [PATCH v7 mm-new 04/10] mm: thp: enable THP allocation exclusively through khugepaged

Quoted 41 lines · Lance Yang
Quoted 39 lines · Yafang Shao

Currently, THP allocation cannot be restricted to khugepaged alone while being disabled in the page fault path. This limitation exists because disabling THP allocation during page faults also prevents the execution of khugepaged_enter_vma() in that path.

With the introduction of BPF, we can now implement THP policies based on different TVA types. This patch adjusts the logic to support this new capability.

While we could also extend prtcl() to utilize this new policy, such a change would require a uAPI modification.

---
 mm/huge_memory.c |  1 -
 mm/memory.c      | 13 ++++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)
mm/huge_memory.c +0 -1
index 523153d21a41..1e9e7b32e2cf 100644
@@ -1346,7 +1346,6 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
        ret = vmf_anon_prepare(vmf);
        if (ret)
                return ret;
-       khugepaged_enter_vma(vma, vma->vm_flags);
 
        if (!(vmf->flags & FAULT_FLAG_WRITE) &&
                        !mm_forbids_zeropage(vma->vm_mm) &&
mm/memory.c +0 -0
index d8819cac7930..d0609dc1e371 100644
@@ -6289,11 +6289,14 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
        if (pud_trans_unstable(vmf.pud))
                goto retry_pud;
 
Quoted 12 lines

mm/memory.c static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, · line 6294

Quoted 8 lines
mm/memory.c +3 -5
-       if (pmd_none(*vmf.pmd) &&
-           thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PMD_ORDER)) {
-               ret = create_huge_pmd(&vmf);
-               if (!(ret & VM_FAULT_FALLBACK))
-                       return ret;
+       if (pmd_none(*vmf.pmd)) {
+               if (vma_is_anonymous(vma))
+                       khugepaged_enter_vma(vma, vm_flags);

Hmm... I'm a bit confused about the different conditions for calling khugepaged_enter_vma(). It's sometimes called for anonymous VMAs, other times ONLY for non-anonymous, and sometimes unconditionally ;)

Right, it is really confusing.

Quoted 3 lines

Anyway, this isn't a blocker, just something I noticed. I might try to simplify that down the road.

please do it when you have a moment.

Quoted 2 lines

Thanks for the review.

Signature · 2 lines
Regards
Yafang
#42 lore raw

Re: [PATCH v7 mm-new 04/10] mm: thp: enable THP allocation exclusively through khugepaged

In reply to Yafang Shao
Quoted 4 lines · Yafang Shao

Currently, THP allocation cannot be restricted to khugepaged alone while being disabled in the page fault path. This limitation exists because disabling THP allocation during page faults also prevents the execution of khugepaged_enter_vma() in that path.

This is quite confusing, I see what you mean - you want to be able to disable page fault THP but not khugepaged THP _at the point of possibly faulting in a THP aligned VMA_.

It seems this patch makes khugepaged_enter_vma() unconditional for an anonymous VMA, rather than depending on the return value specified by thp_vma_allowable_order().

So I think a clearer explanation is:

khugepaged_enter_vma() ultimately invokes any attached BPF function with the TVA_KHUGEPAGED flag set when determining whether or not to enable khugepaged THP for a freshly faulted in VMA.

Currently, on fault, we invoke this in do_huge_pmd_anonymous_page(), as invoked by create_huge_pmd() and only when we have already checked to see if an allowable TVA_PAGEFAULT order is specified.

Since we might want to disallow THP on fault-in but allow it via khugepaged, we move things around so we always attempt to enter khugepaged upon fault.

Having said all this, I'm very confused.

Why are we doing this?

We only enable khugepaged _early_ when we know we're faulting in a huge PMD here.

I guess we do this because, if we are allowed to do the pagefault, maybe something changed that might have previously disallowed khugepaged to run for the mm.

But now we're just checking unconditionally for... no reason?

if BPF disables page fault but not khugepaged, then surely the mm would already be under be khugepaged if it could be?

It's sort of immaterial if we get a pmd_none() that is not-faultable for whatever reason but BPF might say is khugepaged'able, because it'd have already set this.

This is because if we just map a new VMA, we already let khugepaged have it via khugepaged_enter_vma() in __mmap_new_vma() and in the merge paths.

I mean maybe I'm missing something here :)

Quoted 6 lines

With the introduction of BPF, we can now implement THP policies based on different TVA types. This patch adjusts the logic to support this new capability.

While we could also extend prtcl() to utilize this new policy, such a

Typo: prtcl -> prctl

Quoted 1 line

change would require a uAPI modification.

Hm, in what respect? PR_SET_THP_DISABLE?

Quoted 46 lines
---
 mm/huge_memory.c |  1 -
 mm/memory.c      | 13 ++++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)
mm/huge_memory.c +0 -1
index 523153d21a41..1e9e7b32e2cf 100644
@@ -1346,7 +1346,6 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
 	ret = vmf_anon_prepare(vmf);
 	if (ret)
 		return ret;
-	khugepaged_enter_vma(vma, vma->vm_flags);
 
 	if (!(vmf->flags & FAULT_FLAG_WRITE) &&
 			!mm_forbids_zeropage(vma->vm_mm) &&
mm/memory.c +8 -6
index d8819cac7930..d0609dc1e371 100644
@@ -6289,11 +6289,14 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
 	if (pud_trans_unstable(vmf.pud))
 		goto retry_pud;
 
-	if (pmd_none(*vmf.pmd) &&
-	    thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PMD_ORDER)) {
-		ret = create_huge_pmd(&vmf);
-		if (!(ret & VM_FAULT_FALLBACK))
-			return ret;
+	if (pmd_none(*vmf.pmd)) {
+		if (vma_is_anonymous(vma))
+			khugepaged_enter_vma(vma, vm_flags);
+		if (thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PMD_ORDER)) {
+			ret = create_huge_pmd(&vmf);
+			if (!(ret & VM_FAULT_FALLBACK))
+				return ret;
+		}
 	} else {
 		vmf.orig_pmd = pmdp_get_lockless(vmf.pmd);
 
--

2.47.3

#43 lore raw

Re: [PATCH v7 mm-new 04/10] mm: thp: enable THP allocation exclusively through khugepaged

Quoted 7 lines · Lorenzo Stoakes
Quoted 4 lines · Yafang Shao

Currently, THP allocation cannot be restricted to khugepaged alone while being disabled in the page fault path. This limitation exists because disabling THP allocation during page faults also prevents the execution of khugepaged_enter_vma() in that path.

Quoted 7 lines

This is quite confusing, I see what you mean - you want to be able to disable page fault THP but not khugepaged THP _at the point of possibly faulting in a THP aligned VMA_.

It seems this patch makes khugepaged_enter_vma() unconditional for an anonymous VMA, rather than depending on the return value specified by thp_vma_allowable_order().

The functions thp_vma_allowable_order(TVA_PAGEFAULT) and thp_vma_allowable_order(TVA_KHUGEPAGED) are functionally equivalent within the page fault handler; they always yield the same result. Consequently, their execution order is irrelevant.

The change reorders these two calls and, in doing so, also moves the call to vmf_anon_prepare(vmf). This alters the control flow:
- before this change: The logic checked the return value of vmf_anon_prepare() between the two thp_vma_allowable_order() calls.

    thp_vma_allowable_order(TVA_PAGEFAULT);
    ret = vmf_anon_prepare(vmf);
    if (ret)
        return ret;
    thp_vma_allowable_order(TVA_KHUGEPAGED);

- after this change: The logic now executes both
thp_vma_allowable_order() calls first and does not check the return value of vmf_anon_prepare().

    thp_vma_allowable_order(TVA_KHUGEPAGED);
    thp_vma_allowable_order(TVA_PAGEFAULT);
    ret = vmf_anon_prepare(vmf); // Return value 'ret' is ignored.

This change is safe because the return value of vmf_anon_prepare() can be safely ignored. This function checks for transient system-level conditions (e.g., memory pressure, THP availability) that might prevent an immediate THP allocation. It does not guarantee that a subsequent allocation will succeed.

This behavior is consistent with the policy in hugepage_madvise(), where a VMA is queued for khugepaged before a definitive allocation check. If the system is under pressure, khugepaged will simply retry the allocation at a more opportune time.

Quoted 7 lines

So I think a clearer explanation is:

khugepaged_enter_vma() ultimately invokes any attached BPF function with the TVA_KHUGEPAGED flag set when determining whether or not to enable khugepaged THP for a freshly faulted in VMA.

Quoted 7 lines

Currently, on fault, we invoke this in do_huge_pmd_anonymous_page(), as invoked by create_huge_pmd() and only when we have already checked to see if an allowable TVA_PAGEFAULT order is specified.

Since we might want to disallow THP on fault-in but allow it via khugepaged, we move things around so we always attempt to enter khugepaged upon fault.

Thanks for the clarification.

Quoted 5 lines

Having said all this, I'm very confused.

Why are we doing this?

Quoted 8 lines

We only enable khugepaged _early_ when we know we're faulting in a huge PMD here.

I guess we do this because, if we are allowed to do the pagefault, maybe something changed that might have previously disallowed khugepaged to run for the mm.

But now we're just checking unconditionally for... no reason?

I have blamed the change history of do_huge_pmd_anonymous_page() but was unable to find any rationale for placing khugepaged_enter_vma() after the vmf_anon_prepare() check. I therefore believe this ordering is likely unintentional.

Quoted 3 lines

if BPF disables page fault but not khugepaged, then surely the mm would already be under be khugepaged if it could be?

The behavior you describe applies to the madvise mode, not the always mode. To reiterate: the hugepage_madvise() function unconditionally adds the memory mm to the khugepaged queue, whereas the page fault handler employs conditional logic.

Quoted 11 lines

It's sort of immaterial if we get a pmd_none() that is not-faultable for whatever reason but BPF might say is khugepaged'able, because it'd have already set this.

This is because if we just map a new VMA, we already let khugepaged have it via khugepaged_enter_vma() in __mmap_new_vma() and in the merge paths.

I mean maybe I'm missing something here :)

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

With the introduction of BPF, we can now implement THP policies based on different TVA types. This patch adjusts the logic to support this new capability.

While we could also extend prtcl() to utilize this new policy, such a

Typo: prtcl -> prctl

thanks

Quoted 4 lines
Quoted 2 lines

change would require a uAPI modification.

Hm, in what respect? PR_SET_THP_DISABLE?

Right, when can extend PR_SET_THP_DISABLE() to support this logic as well.

Signature · 2 lines
Regards
Yafang
#44 lore raw

Re: [PATCH v7 mm-new 04/10] mm: thp: enable THP allocation exclusively through khugepaged

Quoted 13 lines · Yafang Shao
Quoted 11 lines · Lorenzo Stoakes
Quoted 4 lines · Yafang Shao

Currently, THP allocation cannot be restricted to khugepaged alone while being disabled in the page fault path. This limitation exists because disabling THP allocation during page faults also prevents the execution of khugepaged_enter_vma() in that path.

This is quite confusing, I see what you mean - you want to be able to disable page fault THP but not khugepaged THP _at the point of possibly faulting in a THP aligned VMA_.

Quoted 8 lines
Quoted 3 lines

It seems this patch makes khugepaged_enter_vma() unconditional for an anonymous VMA, rather than depending on the return value specified by thp_vma_allowable_order().

The functions thp_vma_allowable_order(TVA_PAGEFAULT) and thp_vma_allowable_order(TVA_KHUGEPAGED) are functionally equivalent within the page fault handler; they always yield the same result. Consequently, their execution order is irrelevant.

It seems hard to definitely demonstrate that by checking !in_pf vs not in this situation :) but it seems broadly true afaict.

So they differ only in that one starts khugepaged, the other tries to establish a THP on fault via create_huge_pmd().

Quoted 11 lines

The change reorders these two calls and, in doing so, also moves the call to vmf_anon_prepare(vmf). This alters the control flow:
- before this change: The logic checked the return value of vmf_anon_prepare() between the two thp_vma_allowable_order() calls.

    thp_vma_allowable_order(TVA_PAGEFAULT);
    ret = vmf_anon_prepare(vmf);
    if (ret)
        return ret;
    thp_vma_allowable_order(TVA_KHUGEPAGED);

I mean it's also _only if_ the TVA_PAGEFAULT invocation succeeds that the TVA_KHUGEPAGED one happens.

Quoted 8 lines

- after this change: The logic now executes both
thp_vma_allowable_order() calls first and does not check the return value of vmf_anon_prepare().

    thp_vma_allowable_order(TVA_KHUGEPAGED);
    thp_vma_allowable_order(TVA_PAGEFAULT);
    ret = vmf_anon_prepare(vmf); // Return value 'ret' is ignored.

Hm this is confusing, your code does:

+ if (pmd_none(*vmf.pmd)) {
+ if (vma_is_anonymous(vma))
+ khugepaged_enter_vma(vma, vm_flags);
+ if (thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PMD_ORDER)) {
+ ret = create_huge_pmd(&vmf);
+ if (!(ret & VM_FAULT_FALLBACK))
+ return ret;
+ }

So the ret is absolutely not ignored, but whether it succeeds or not, we still invoke khugepaged_enter_vma().

Previously we would not have one this had vmf_anon_prepare() failed in do_huge_pmd_anonymous_page().

Which I guess is what you mean?

Quoted 11 lines

This change is safe because the return value of vmf_anon_prepare() can be safely ignored. This function checks for transient system-level conditions (e.g., memory pressure, THP availability) that might prevent an immediate THP allocation. It does not guarantee that a subsequent allocation will succeed.

This behavior is consistent with the policy in hugepage_madvise(), where a VMA is queued for khugepaged before a definitive allocation check. If the system is under pressure, khugepaged will simply retry the allocation at a more opportune time.

OK. I do note though that the khugepaged being kicked off is at mm_struct level.

So us trying to invoke khugepaged on the mm again is about.. something having changed that would previously have prevented us but now doesn't?

That is, a product of thp_vma_allowable_order() right?

So probably a sysfs change or similar?

But I guess it makes sense to hook in BPF whenever this is the case because this _could_ be the point at which khugepaged enters the mm, and we want to select the allowable order at this time.

So on basis of the two checks being effectively equivalent (on assumption this is always the case) then the change is fairly reasonable.

Though I would put this information, that the checks are equivalent, in the commit message so it's really clear.

Quoted 12 lines
Quoted 12 lines

So I think a clearer explanation is:

khugepaged_enter_vma() ultimately invokes any attached BPF function with the TVA_KHUGEPAGED flag set when determining whether or not to enable khugepaged THP for a freshly faulted in VMA.

Currently, on fault, we invoke this in do_huge_pmd_anonymous_page(), as invoked by create_huge_pmd() and only when we have already checked to see if an allowable TVA_PAGEFAULT order is specified.

Quoted 5 lines
Quoted 3 lines

Since we might want to disallow THP on fault-in but allow it via khugepaged, we move things around so we always attempt to enter khugepaged upon fault.

Thanks for the clarification.

Thanks

Quoted 13 lines
Quoted 13 lines

Having said all this, I'm very confused.

Why are we doing this?

We only enable khugepaged _early_ when we know we're faulting in a huge PMD here.

I guess we do this because, if we are allowed to do the pagefault, maybe something changed that might have previously disallowed khugepaged to run for the mm.

Quoted 6 lines
Quoted 1 line

But now we're just checking unconditionally for... no reason?

I have blamed the change history of do_huge_pmd_anonymous_page() but was unable to find any rationale for placing khugepaged_enter_vma() after the vmf_anon_prepare() check. I therefore believe this ordering is likely unintentional.

Right, yeah.

Quoted 9 lines
Quoted 4 lines

if BPF disables page fault but not khugepaged, then surely the mm would already be under be khugepaged if it could be?

The behavior you describe applies to the madvise mode, not the always mode. To reiterate: the hugepage_madvise() function unconditionally adds the memory mm to the khugepaged queue, whereas the page fault handler employs conditional logic.

Right, so we suggest the order

Quoted 16 lines
Quoted 16 lines

It's sort of immaterial if we get a pmd_none() that is not-faultable for whatever reason but BPF might say is khugepaged'able, because it'd have already set this.

This is because if we just map a new VMA, we already let khugepaged have it via khugepaged_enter_vma() in __mmap_new_vma() and in the merge paths.

I mean maybe I'm missing something here :)

Quoted 5 lines

With the introduction of BPF, we can now implement THP policies based on different TVA types. This patch adjusts the logic to support this new capability.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

While we could also extend prtcl() to utilize this new policy, such a

Typo: prtcl -> prctl

thanks

Cheers

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

change would require a uAPI modification.

Hm, in what respect? PR_SET_THP_DISABLE?

Right, when can extend PR_SET_THP_DISABLE() to support this logic as well.

Yeah let's not touch that please :)

Quoted 4 lines

--
Regards
Yafang

Cheers, Lorenzo

#45 lore raw

Re: [PATCH v7 mm-new 04/10] mm: thp: enable THP allocation exclusively through khugepaged

Quoted 24 lines · Lorenzo Stoakes
Quoted 13 lines · Yafang Shao
Quoted 11 lines · Lorenzo Stoakes
Quoted 4 lines · Yafang Shao

Currently, THP allocation cannot be restricted to khugepaged alone while being disabled in the page fault path. This limitation exists because disabling THP allocation during page faults also prevents the execution of khugepaged_enter_vma() in that path.

This is quite confusing, I see what you mean - you want to be able to disable page fault THP but not khugepaged THP _at the point of possibly faulting in a THP aligned VMA_.

Quoted 8 lines
Quoted 3 lines

It seems this patch makes khugepaged_enter_vma() unconditional for an anonymous VMA, rather than depending on the return value specified by thp_vma_allowable_order().

The functions thp_vma_allowable_order(TVA_PAGEFAULT) and thp_vma_allowable_order(TVA_KHUGEPAGED) are functionally equivalent within the page fault handler; they always yield the same result. Consequently, their execution order is irrelevant.

Quoted 5 lines

It seems hard to definitely demonstrate that by checking !in_pf vs not in this situation :) but it seems broadly true afaict.

So they differ only in that one starts khugepaged, the other tries to establish a THP on fault via create_huge_pmd().

right

Quoted 51 lines
Quoted 7 lines

The change reorders these two calls and, in doing so, also moves the call to vmf_anon_prepare(vmf). This alters the control flow:
- before this change: The logic checked the return value of vmf_anon_prepare() between the two thp_vma_allowable_order() calls.

Quoted 5 lines
    thp_vma_allowable_order(TVA_PAGEFAULT);
    ret = vmf_anon_prepare(vmf);
    if (ret)
        return ret;
    thp_vma_allowable_order(TVA_KHUGEPAGED);

I mean it's also _only if_ the TVA_PAGEFAULT invocation succeeds that the TVA_KHUGEPAGED one happens.

Quoted 8 lines

- after this change: The logic now executes both
thp_vma_allowable_order() calls first and does not check the return value of vmf_anon_prepare().

    thp_vma_allowable_order(TVA_KHUGEPAGED);
    thp_vma_allowable_order(TVA_PAGEFAULT);
    ret = vmf_anon_prepare(vmf); // Return value 'ret' is ignored.

Hm this is confusing, your code does:

mm/memory.c +8 -0
+       if (pmd_none(*vmf.pmd)) {
+               if (vma_is_anonymous(vma))
+                       khugepaged_enter_vma(vma, vm_flags);
+               if (thp_vma_allowable_order(vma, vm_flags, TVA_PAGEFAULT, PMD_ORDER)) {
+                       ret = create_huge_pmd(&vmf);
+                       if (!(ret & VM_FAULT_FALLBACK))
+                               return ret;
+               }

So the ret is absolutely not ignored, but whether it succeeds or not, we still invoke khugepaged_enter_vma().

Previously we would not have one this had vmf_anon_prepare() failed in do_huge_pmd_anonymous_page().

Which I guess is what you mean?

Quoted 7 lines

This change is safe because the return value of vmf_anon_prepare() can be safely ignored. This function checks for transient system-level conditions (e.g., memory pressure, THP availability) that might prevent an immediate THP allocation. It does not guarantee that a subsequent allocation will succeed.

Quoted 6 lines
Quoted 4 lines

This behavior is consistent with the policy in hugepage_madvise(), where a VMA is queued for khugepaged before a definitive allocation check. If the system is under pressure, khugepaged will simply retry the allocation at a more opportune time.

OK. I do note though that the khugepaged being kicked off is at mm_struct level.

The unit of operation for khugepaged is the mm_struct itself. It processes the entire mm even when only a single VMA within it is a candidate for a THP.

Quoted 6 lines

So us trying to invoke khugepaged on the mm again is about.. something having changed that would previously have prevented us but now doesn't?

That is, a product of thp_vma_allowable_order() right?

Quoted 8 lines

So probably a sysfs change or similar?

But I guess it makes sense to hook in BPF whenever this is the case because this _could_ be the point at which khugepaged enters the mm, and we want to select the allowable order at this time.

So on basis of the two checks being effectively equivalent (on assumption this is always the case) then the change is fairly reasonable.

Yes, that is exactly what I mean.

Quoted 3 lines

Though I would put this information, that the checks are equivalent, in the commit message so it's really clear.

will add it.

Signature · 2 lines
Regards
Yafang
#46 lore raw

PATCH v7 5/10 bpf: mark mm->owner as __safe_rcu_or_null

In reply to Yafang Shao

When CONFIG_MEMCG is enabled, we can access mm->owner under RCU. The owner can be NULL. With this change, BPF helpers can safely access mm->owner to retrieve the associated task from the mm. We can then make policy decision based on the task attribute.

The typical use case is as follows,

bpf_rcu_read_lock(); // rcu lock must be held for rcu trusted field @owner = @mm->owner; // mm_struct::owner is rcu trusted or null if (!@owner)

      goto out;

/* Do something based on the task attribute */

out:
bpf_rcu_read_unlock();

---
 kernel/bpf/verifier.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/bpf/verifier.c +3 -0
index c4f69a9e9af6..d400e18ee31e 100644
@@ -7123,6 +7123,9 @@ BTF_TYPE_SAFE_RCU(struct cgroup_subsys_state) {
 /* RCU trusted: these fields are trusted in RCU CS and can be NULL */
 BTF_TYPE_SAFE_RCU_OR_NULL(struct mm_struct) {
 	struct file __rcu *exe_file;
+#ifdef CONFIG_MEMCG
+	struct task_struct __rcu *owner;
+#endif
 };
 
 /* skb->sk, req->sk are not RCU protected, but we mark them as such
Signature · 1 line
2.47.3
Acked-by on v7 5/10
#47 lore raw

Re: [PATCH v7 mm-new 05/10] bpf: mark mm->owner as __safe_rcu_or_null

Quoted 10 lines · Yafang Shao

When CONFIG_MEMCG is enabled, we can access mm->owner under RCU. The owner can be NULL. With this change, BPF helpers can safely access mm->owner to retrieve the associated task from the mm. We can then make policy decision based on the task attribute.

The typical use case is as follows,

bpf_rcu_read_lock(); // rcu lock must be held for rcu trusted field @owner = @mm->owner; // mm_struct::owner is rcu trusted or null if (!@owner)

Quoted 8 lines
      goto out;

/* Do something based on the task attribute */

out:
bpf_rcu_read_unlock();

This is one for the BPF people, but this seems reasonable afaict so:

Quoted 22 lines
---
 kernel/bpf/verifier.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/bpf/verifier.c +3 -1
index c4f69a9e9af6..d400e18ee31e 100644
@@ -7123,6 +7123,9 @@ BTF_TYPE_SAFE_RCU(struct cgroup_subsys_state) {
 /* RCU trusted: these fields are trusted in RCU CS and can be NULL */
 BTF_TYPE_SAFE_RCU_OR_NULL(struct mm_struct) {
 	struct file __rcu *exe_file;
+#ifdef CONFIG_MEMCG
+	struct task_struct __rcu *owner;
+#endif
 };
 
 /* skb->sk, req->sk are not RCU protected, but we mark them as such
--

2.47.3

#48 lore raw

PATCH v7 6/10 bpf: mark vma->vm_mm as __safe_trusted_or_null

In reply to Yafang Shao

The vma->vm_mm might be NULL and it can be accessed outside of RCU. Thus, we can mark it as trusted_or_null. With this change, BPF helpers can safely access vma->vm_mm to retrieve the associated mm_struct from the VMA. Then we can make policy decision from the VMA.

The lsm selftest must be modified because it directly accesses vma->vm_mm without a NULL pointer check; otherwise it will break due to this change.

For the VMA based THP policy, the use case is as follows,

@mm = @vma->vm_mm; // vm_area_struct::vm_mm is trusted or null if (!@mm)

      return;
  bpf_rcu_read_lock(); // rcu lock must be held to dereference the owner
  @owner = @mm->owner; // mm_struct::owner is rcu trusted or null
  if (!@owner)
    goto out;

@cgroup1 = bpf_task_get_cgroup1(@owner, MEMCG_HIERARCHY_ID);

/* make the decision based on the @cgroup1 attribute */

bpf_cgroup_release(@cgroup1); // release the associated cgroup
out:
bpf_rcu_read_unlock();

PSI memory information can be obtained from the associated cgroup to inform policy decisions. Since upstream PSI support is currently limited to cgroup v2, the following example demonstrates cgroup v2 implementation:

  @owner = @mm->owner;
  if (@owner) {
      // @ancestor_cgid is user-configured
      @ancestor = bpf_cgroup_from_id(@ancestor_cgid);
      if (bpf_task_under_cgroup(@owner, @ancestor)) {
          @psi_group = @ancestor->psi;
        /* Extract PSI metrics from @psi_group and
         * implement policy logic based on the values
         */

      }

}

Cc Lorenzo Stoakes <[email protected]>
---
 kernel/bpf/verifier.c                   | 5 +++++
 tools/testing/selftests/bpf/progs/lsm.c | 8 +++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +5 -0
index d400e18ee31e..b708b98f796c 100644
@@ -7165,6 +7165,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {
 	struct sock *sk;
 };
 
+BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct) {
+	struct mm_struct *vm_mm;
+};
+
 static bool type_is_rcu(struct bpf_verifier_env *env,
 			struct bpf_reg_state *reg,
 			const char *field_name, u32 btf_id)
@@ -7206,6 +7210,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
 {
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry));
+	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct));
 
 	return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
 					  "__safe_trusted_or_null");
tools/testing/selftests/bpf/progs/lsm.c +5 -3
index 0c13b7409947..7de173daf27b 100644
@@ -89,14 +89,16 @@ SEC("lsm/file_mprotect")
 int BPF_PROG(test_int_hook, struct vm_area_struct *vma,
 	     unsigned long reqprot, unsigned long prot, int ret)
 {
-	if (ret != 0)
+	struct mm_struct *mm = vma->vm_mm;
+
+	if (ret != 0 || !mm)
 		return ret;
 
 	__s32 pid = bpf_get_current_pid_tgid() >> 32;
 	int is_stack = 0;
 
-	is_stack = (vma->vm_start <= vma->vm_mm->start_stack &&
-		    vma->vm_end >= vma->vm_mm->start_stack);
+	is_stack = (vma->vm_start <= mm->start_stack &&
+		    vma->vm_end >= mm->start_stack);
 
 	if (is_stack && monitored_pid == pid) {
 		mprotect_count++;
Signature · 1 line
2.47.3
Acked-by on v7 6/10
#49 lore raw

Re: [PATCH v7 mm-new 06/10] bpf: mark vma->vm_mm as __safe_trusted_or_null

Quoted 37 lines · Yafang Shao

The vma->vm_mm might be NULL and it can be accessed outside of RCU. Thus, we can mark it as trusted_or_null. With this change, BPF helpers can safely access vma->vm_mm to retrieve the associated mm_struct from the VMA. Then we can make policy decision from the VMA.

The lsm selftest must be modified because it directly accesses vma->vm_mm without a NULL pointer check; otherwise it will break due to this change.

For the VMA based THP policy, the use case is as follows,

@mm = @vma->vm_mm; // vm_area_struct::vm_mm is trusted or null if (!@mm)

      return;
  bpf_rcu_read_lock(); // rcu lock must be held to dereference the owner
  @owner = @mm->owner; // mm_struct::owner is rcu trusted or null
  if (!@owner)
    goto out;

@cgroup1 = bpf_task_get_cgroup1(@owner, MEMCG_HIERARCHY_ID);

/* make the decision based on the @cgroup1 attribute */

bpf_cgroup_release(@cgroup1); // release the associated cgroup
out:
bpf_rcu_read_unlock();

PSI memory information can be obtained from the associated cgroup to inform policy decisions. Since upstream PSI support is currently limited to cgroup v2, the following example demonstrates cgroup v2 implementation:

  @owner = @mm->owner;
  if (@owner) {
      // @ancestor_cgid is user-configured
      @ancestor = bpf_cgroup_from_id(@ancestor_cgid);
      if (bpf_task_under_cgroup(@owner, @ancestor)) {
          @psi_group = @ancestor->psi;
Quoted 8 lines
        /* Extract PSI metrics from @psi_group and
         * implement policy logic based on the values
         */

      }

}

Again more for BPF guys, but seems sensible, so:

Quoted 56 lines
Cc Lorenzo Stoakes <[email protected]>
---
 kernel/bpf/verifier.c                   | 5 +++++
 tools/testing/selftests/bpf/progs/lsm.c | 8 +++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +5 -0
index d400e18ee31e..b708b98f796c 100644
@@ -7165,6 +7165,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {
 	struct sock *sk;
 };
 
+BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct) {
+	struct mm_struct *vm_mm;
+};
+
 static bool type_is_rcu(struct bpf_verifier_env *env,
 			struct bpf_reg_state *reg,
 			const char *field_name, u32 btf_id)
@@ -7206,6 +7210,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
 {
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry));
+	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct));
 
 	return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
 					  "__safe_trusted_or_null");
tools/testing/selftests/bpf/progs/lsm.c +5 -4
index 0c13b7409947..7de173daf27b 100644
@@ -89,14 +89,16 @@ SEC("lsm/file_mprotect")
 int BPF_PROG(test_int_hook, struct vm_area_struct *vma,
 	     unsigned long reqprot, unsigned long prot, int ret)
 {
-	if (ret != 0)
+	struct mm_struct *mm = vma->vm_mm;
+
+	if (ret != 0 || !mm)
 		return ret;
 
 	__s32 pid = bpf_get_current_pid_tgid() >> 32;
 	int is_stack = 0;
 
-	is_stack = (vma->vm_start <= vma->vm_mm->start_stack &&
-		    vma->vm_end >= vma->vm_mm->start_stack);
+	is_stack = (vma->vm_start <= mm->start_stack &&
+		    vma->vm_end >= mm->start_stack);
 
 	if (is_stack && monitored_pid == pid) {
 		mprotect_count++;
--

2.47.3

#50 lore raw

Re: [PATCH v7 mm-new 06/10] bpf: mark vma->vm_mm as __safe_trusted_or_null

In reply to Yafang Shao
Quoted 4 lines · Yafang Shao

The vma->vm_mm might be NULL and it can be accessed outside of RCU. Thus, we can mark it as trusted_or_null. With this change, BPF helpers can safely access vma->vm_mm to retrieve the associated mm_struct from the VMA. Then we can make policy decision from the VMA.

I don't agree with any of that statement.

How are you getting a vma outside an rcu lock safely?

vmas are RCU type safe so I don't think you can make the statement of null or trusted. You can get a vma that has moved to another mm if you are not careful.

What am I missing? Surely there is more context to add to this commit message.

Quoted 98 lines

The lsm selftest must be modified because it directly accesses vma->vm_mm without a NULL pointer check; otherwise it will break due to this change.

For the VMA based THP policy, the use case is as follows,

@mm = @vma->vm_mm; // vm_area_struct::vm_mm is trusted or null if (!@mm)

      return;
  bpf_rcu_read_lock(); // rcu lock must be held to dereference the owner
  @owner = @mm->owner; // mm_struct::owner is rcu trusted or null
  if (!@owner)
    goto out;

@cgroup1 = bpf_task_get_cgroup1(@owner, MEMCG_HIERARCHY_ID);

/* make the decision based on the @cgroup1 attribute */

bpf_cgroup_release(@cgroup1); // release the associated cgroup
out:
bpf_rcu_read_unlock();

PSI memory information can be obtained from the associated cgroup to inform policy decisions. Since upstream PSI support is currently limited to cgroup v2, the following example demonstrates cgroup v2 implementation:

  @owner = @mm->owner;
  if (@owner) {
      // @ancestor_cgid is user-configured
      @ancestor = bpf_cgroup_from_id(@ancestor_cgid);
      if (bpf_task_under_cgroup(@owner, @ancestor)) {
          @psi_group = @ancestor->psi;
        /* Extract PSI metrics from @psi_group and
         * implement policy logic based on the values
         */

      }

}

Cc Lorenzo Stoakes <[email protected]>
---
 kernel/bpf/verifier.c                   | 5 +++++
 tools/testing/selftests/bpf/progs/lsm.c | 8 +++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +5 -0
index d400e18ee31e..b708b98f796c 100644
@@ -7165,6 +7165,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {
 	struct sock *sk;
 };
 
+BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct) {
+	struct mm_struct *vm_mm;
+};
+
 static bool type_is_rcu(struct bpf_verifier_env *env,
 			struct bpf_reg_state *reg,
 			const char *field_name, u32 btf_id)
@@ -7206,6 +7210,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
 {
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry));
+	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct));
 
 	return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
 					  "__safe_trusted_or_null");
tools/testing/selftests/bpf/progs/lsm.c +5 -3
index 0c13b7409947..7de173daf27b 100644
@@ -89,14 +89,16 @@ SEC("lsm/file_mprotect")
 int BPF_PROG(test_int_hook, struct vm_area_struct *vma,
 	     unsigned long reqprot, unsigned long prot, int ret)
 {
-	if (ret != 0)
+	struct mm_struct *mm = vma->vm_mm;
+
+	if (ret != 0 || !mm)
 		return ret;
 
 	__s32 pid = bpf_get_current_pid_tgid() >> 32;
 	int is_stack = 0;
 
-	is_stack = (vma->vm_start <= vma->vm_mm->start_stack &&
-		    vma->vm_end >= vma->vm_mm->start_stack);
+	is_stack = (vma->vm_start <= mm->start_stack &&
+		    vma->vm_end >= mm->start_stack);
 
 	if (is_stack && monitored_pid == pid) {
 		mprotect_count++;
Signature · 1 line
2.47.3
#51 lore raw

Re: [PATCH v7 mm-new 06/10] bpf: mark vma->vm_mm as __safe_trusted_or_null

Quoted 9 lines · Liam R. Howlett
Quoted 4 lines · Yafang Shao

The vma->vm_mm might be NULL and it can be accessed outside of RCU. Thus, we can mark it as trusted_or_null. With this change, BPF helpers can safely access vma->vm_mm to retrieve the associated mm_struct from the VMA. Then we can make policy decision from the VMA.

I don't agree with any of that statement.

How are you getting a vma outside an rcu lock safely?

I'm guessing he means that kernel code might access it outside of RCU?

vma->vm_mm can be NULL for 'special' mappings, no not that special, not the other special, the VDSO special, yeah that one.

get_vma_name() in fs/proc/task_mmu.c does:

	if (!vma->vm_mm) {
		*name = "[vdso]";
		return;
	}

Not sure you'd ever find a way to bump into that in THP code paths though ofc.

I was reassured in the last version of the series that the MM is definitely safe to access safe to access

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

And it _seems_ BPF can already access VMA's.

I think everything's under RCU, and there's automatically an RCU lock applied for anything BPF-ish.

So my A-b was all baed on this kind of hand waving...

Quoted 7 lines

vmas are RCU type safe so I don't think you can make the statement of null or trusted. You can get a vma that has moved to another mm if you are not careful.

What am I missing? Surely there is more context to add to this commit message.

Suspect it's the BPF-magic that's the confusing bit...

Quoted 99 lines
Quoted 99 lines

The lsm selftest must be modified because it directly accesses vma->vm_mm without a NULL pointer check; otherwise it will break due to this change.

For the VMA based THP policy, the use case is as follows,

@mm = @vma->vm_mm; // vm_area_struct::vm_mm is trusted or null if (!@mm)

      return;
  bpf_rcu_read_lock(); // rcu lock must be held to dereference the owner
  @owner = @mm->owner; // mm_struct::owner is rcu trusted or null
  if (!@owner)
    goto out;

@cgroup1 = bpf_task_get_cgroup1(@owner, MEMCG_HIERARCHY_ID);

/* make the decision based on the @cgroup1 attribute */

bpf_cgroup_release(@cgroup1); // release the associated cgroup
out:
bpf_rcu_read_unlock();

PSI memory information can be obtained from the associated cgroup to inform policy decisions. Since upstream PSI support is currently limited to cgroup v2, the following example demonstrates cgroup v2 implementation:

  @owner = @mm->owner;
  if (@owner) {
      // @ancestor_cgid is user-configured
      @ancestor = bpf_cgroup_from_id(@ancestor_cgid);
      if (bpf_task_under_cgroup(@owner, @ancestor)) {
          @psi_group = @ancestor->psi;
        /* Extract PSI metrics from @psi_group and
         * implement policy logic based on the values
         */

      }

}

Cc Lorenzo Stoakes <[email protected]>
---
 kernel/bpf/verifier.c                   | 5 +++++
 tools/testing/selftests/bpf/progs/lsm.c | 8 +++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +5 -0
index d400e18ee31e..b708b98f796c 100644
@@ -7165,6 +7165,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) {
 	struct sock *sk;
 };
 
+BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct) {
+	struct mm_struct *vm_mm;
+};
+
 static bool type_is_rcu(struct bpf_verifier_env *env,
 			struct bpf_reg_state *reg,
 			const char *field_name, u32 btf_id)
@@ -7206,6 +7210,7 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env,
 {
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket));
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry));
+	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct vm_area_struct));
 
 	return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id,
 					  "__safe_trusted_or_null");
tools/testing/selftests/bpf/progs/lsm.c +5 -4
index 0c13b7409947..7de173daf27b 100644
@@ -89,14 +89,16 @@ SEC("lsm/file_mprotect")
 int BPF_PROG(test_int_hook, struct vm_area_struct *vma,
 	     unsigned long reqprot, unsigned long prot, int ret)
 {
-	if (ret != 0)
+	struct mm_struct *mm = vma->vm_mm;
+
+	if (ret != 0 || !mm)
 		return ret;
 
 	__s32 pid = bpf_get_current_pid_tgid() >> 32;
 	int is_stack = 0;
 
-	is_stack = (vma->vm_start <= vma->vm_mm->start_stack &&
-		    vma->vm_end >= vma->vm_mm->start_stack);
+	is_stack = (vma->vm_start <= mm->start_stack &&
+		    vma->vm_end >= mm->start_stack);
 
 	if (is_stack && monitored_pid == pid) {
 		mprotect_count++;
--

2.47.3

#52 lore raw

Re: [PATCH v7 mm-new 06/10] bpf: mark vma->vm_mm as __safe_trusted_or_null

Quoted 29 lines · Lorenzo Stoakes
Quoted 9 lines · Liam R. Howlett
Quoted 4 lines · Yafang Shao

The vma->vm_mm might be NULL and it can be accessed outside of RCU. Thus, we can mark it as trusted_or_null. With this change, BPF helpers can safely access vma->vm_mm to retrieve the associated mm_struct from the VMA. Then we can make policy decision from the VMA.

I don't agree with any of that statement.

How are you getting a vma outside an rcu lock safely?

I'm guessing he means that kernel code might access it outside of RCU?

vma->vm_mm can be NULL for 'special' mappings, no not that special, not the other special, the VDSO special, yeah that one.

get_vma_name() in fs/proc/task_mmu.c does:

        if (!vma->vm_mm) {
                *name = "[vdso]";
                return;
        }

Not sure you'd ever find a way to bump into that in THP code paths though ofc.

I was reassured in the last version of the series that the MM is definitely safe to access safe to access

Quoted 7 lines

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

And it _seems_ BPF can already access VMA's.

I think everything's under RCU, and there's automatically an RCU lock applied for anything BPF-ish.

This is true for non-sleepable BPF programs. However, for sleepable BPF programs, only SRCU protection is held.

Quoted 4 lines

So my A-b was all baed on this kind of hand waving...

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

vmas are RCU type safe so I don't think you can make the statement of null or trusted. You can get a vma that has moved to another mm if you are not careful.

What am I missing? Surely there is more context to add to this commit message.

Suspect it's the BPF-magic that's the confusing bit...

Absolutely. BPF has a lot of magic under the hood ;-)

Signature · 2 lines
Regards
Yafang
#53 lore raw

Re: [PATCH v7 mm-new 06/10] bpf: mark vma->vm_mm as __safe_trusted_or_null

In reply to Liam R. Howlett
Quoted 10 lines · Liam R. Howlett
Quoted 4 lines · Yafang Shao

The vma->vm_mm might be NULL and it can be accessed outside of RCU. Thus, we can mark it as trusted_or_null. With this change, BPF helpers can safely access vma->vm_mm to retrieve the associated mm_struct from the VMA. Then we can make policy decision from the VMA.

I don't agree with any of that statement.

How are you getting a vma outside an rcu lock safely?

The callers of this BPF hook guarantee that the provided vm_area_struct pointer is safe to read. This means your BPF program can safely access its members, though it cannot write to them.

You might question how code in lsm.c can access vma->vm_mm->start_stack without an explicit NULL check. This is because the BPF verifier has a safety feature: if vma->vm_mm is NULL, it will substitute the value 0 instead of actually performing the dereference, preventing a crash.

However, while this prevents a kernel panic, it doesn't guarantee correct logic. If your program uses the value 0 for start_stack without knowing it came from a NULL pointer, it might behave incorrectly. Therefore, you must still explicitly check for NULL to ensure your program's logic is sound.

The __safe_trusted_or_null marker enforces this requirement. It is a restriction that ensures program correctness, not a loosening of the rules.

Alex, Andrii, please correct me if my understanding is wrong.

Quoted 7 lines

vmas are RCU type safe so I don't think you can make the statement of null or trusted. You can get a vma that has moved to another mm if you are not careful.

What am I missing? Surely there is more context to add to this commit message.

According to the definition of struct vm_area_struct, the comment on vm_mm states: "Unstable RCU readers are allowed to read this." This confirms that we can safely read vm_mm without holding the RCU read lock. If this were not the case, the comment would need to be corrected.

  struct vm_area_struct {
         /*
         * The address space we belong to.
         * Unstable RCU readers are allowed to read this.
         */
         struct mm_struct *vm_mm;

};

As a minor, unrelated note: Non-sleepable BPF programs always run within an RCU read-side critical section. Therefore, you do not need to explicitly acquire the RCU read lock in such programs.

Signature · 2 lines
Regards
Yafang
#54 lore raw

PATCH v7 7/10 selftests/bpf: add a simple BPF based THP policy

In reply to Yafang Shao

This selftest verifies that PMD-mapped THP allocation is restricted in page faults for tasks within a specific cgroup, while still permitting THP allocation via khugepaged.

Since THP allocation depends on various factors (e.g., system memory pressure), using the actual allocated THP size for validation is unreliable. Instead, we check the return value of get_suggested_order(), which indicates whether the system intends to allocate a THP, regardless of whether the allocation ultimately succeeds.

This test case defines a simple THP policy. The policy permits PMD-mapped THP allocation through khugepaged for tasks in a designated cgroup, but prohibits it for all other tasks and contexts, including the page fault handler.

---
 MAINTAINERS                                   |   2 +
 tools/testing/selftests/bpf/config            |   3 +
 .../selftests/bpf/prog_tests/thp_adjust.c     | 254 ++++++++++++++++++
 .../selftests/bpf/progs/test_thp_adjust.c     | 100 +++++++
 4 files changed, 359 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/thp_adjust.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust.c
MAINTAINERS +2 -0
index d055a3c95300..6aa5543963d1 100644
@@ -16255,6 +16255,8 @@ F:	mm/huge_memory.c
 F:	mm/huge_memory_bpf.c
 F:	mm/khugepaged.c
 F:	mm/mm_slot.h
+F:	tools/testing/selftests/bpf/prog_tests/thp_adjust.c
+F:	tools/testing/selftests/bpf/progs/test_thp_adjust*
 F:	tools/testing/selftests/mm/khugepaged.c
 F:	tools/testing/selftests/mm/split_huge_page_test.c
 F:	tools/testing/selftests/mm/transhuge-stress.c
tools/testing/selftests/bpf/config +3 -0
index 8916ab814a3e..b2c73cfae14e 100644
@@ -26,6 +26,7 @@ CONFIG_DMABUF_HEAPS=y
 CONFIG_DMABUF_HEAPS_SYSTEM=y
 CONFIG_DUMMY=y
 CONFIG_DYNAMIC_FTRACE=y
+CONFIG_BPF_GET_THP_ORDER=y
 CONFIG_FPROBE=y
 CONFIG_FTRACE_SYSCALLS=y
 CONFIG_FUNCTION_ERROR_INJECTION=y
@@ -51,6 +52,7 @@ CONFIG_IPV6_TUNNEL=y
 CONFIG_KEYS=y
 CONFIG_LIRC=y
 CONFIG_LWTUNNEL=y
+CONFIG_MEMCG=y
 CONFIG_MODULE_SIG=y
 CONFIG_MODULE_SRCVERSION_ALL=y
 CONFIG_MODULE_UNLOAD=y
@@ -114,6 +116,7 @@ CONFIG_SECURITY=y
 CONFIG_SECURITYFS=y
 CONFIG_SYN_COOKIES=y
 CONFIG_TEST_BPF=m
+CONFIG_TRANSPARENT_HUGEPAGE=y
 CONFIG_UDMABUF=y
 CONFIG_USERFAULTFD=y
 CONFIG_VSOCKETS=y
tools/testing/selftests/bpf/prog_tests/thp_adjust.c new file +254 -0

254 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_thp_adjust.c new file +100 -0

100 lines not on this page · raw message

Signature · 1 line
2.47.3
#55 lore raw

Re: [PATCH v7 mm-new 07/10] selftests/bpf: add a simple BPF based THP policy

Quoted 10 lines · Yafang Shao
tools/testing/selftests/bpf/progs/test_thp_adjust.c new file +9 -0
+/* Detecting whether a task can successfully allocate THP is unreliable because
+ * it may be influenced by system memory pressure. Instead of making the result
+ * dependent on unpredictable factors, we should simply check
+ * bpf_hook_thp_get_orders()'s return value, which is deterministic.
+ */
+SEC("fexit/bpf_hook_thp_get_orders")
+int BPF_PROG(thp_run, struct vm_area_struct *vma, u64 vma_flags, enum tva_type tva_type,
+            unsigned long orders, int retval)
+{

...

tools/testing/selftests/bpf/progs/test_thp_adjust.c line 54

Quoted 4 lines
tools/testing/selftests/bpf/progs/test_thp_adjust.c new file +4 -0
+SEC("struct_ops/thp_get_order")
+int BPF_PROG(alloc_in_khugepaged, struct vm_area_struct *vma, enum bpf_thp_vma_type vma_type,
+            enum tva_type tva_type, unsigned long orders)
+{

This is a bad idea to mix struct_ops logic with fentry/fexit style. struct_ops hook will not be affected by compiler optimizations, while fentry depends on a whim of compilers.
struct_ops can be scoped, while fentry is always global. sched-ext already struggles with the later, since some scheds need tracing data from other parts of the kernel and they cannot be grouped together. All sorts of workarounds were proposed, but no good solution in sight. So don't go this route for THP. Make everything you need to be struct_ops based and/or pass whatever extra data into these ops.

Also think of scoping for bpf-thp from the start.
Currently st_ops/thp_get_order is only one and it's global. It's ok for prototypes and experiments, but not ok for landing upstream. I think cgroup would a natural scope and different cgroups might want their own bpf based THP hints. Once you do that, think through how delegation of suggested order will propagate through hierarchy.

bpf-oom seems to be aligning toward the same design principles, so don't reinvent the wheel.

#56 lore raw

Re: [PATCH v7 mm-new 07/10] selftests/bpf: add a simple BPF based THP policy

Quoted 15 lines · Alexei Starovoitov
Quoted 10 lines · Yafang Shao
tools/testing/selftests/bpf/progs/test_thp_adjust.c new file +9 -0
+/* Detecting whether a task can successfully allocate THP is unreliable because
+ * it may be influenced by system memory pressure. Instead of making the result
+ * dependent on unpredictable factors, we should simply check
+ * bpf_hook_thp_get_orders()'s return value, which is deterministic.
+ */
+SEC("fexit/bpf_hook_thp_get_orders")
+int BPF_PROG(thp_run, struct vm_area_struct *vma, u64 vma_flags, enum tva_type tva_type,
+            unsigned long orders, int retval)
+{

...

Quoted 15 lines

tools/testing/selftests/bpf/progs/test_thp_adjust.c line 54

Quoted 4 lines
tools/testing/selftests/bpf/progs/test_thp_adjust.c new file +4 -0
+SEC("struct_ops/thp_get_order")
+int BPF_PROG(alloc_in_khugepaged, struct vm_area_struct *vma, enum bpf_thp_vma_type vma_type,
+            enum tva_type tva_type, unsigned long orders)
+{

This is a bad idea to mix struct_ops logic with fentry/fexit style. struct_ops hook will not be affected by compiler optimizations, while fentry depends on a whim of compilers.
struct_ops can be scoped, while fentry is always global. sched-ext already struggles with the later, since some scheds need tracing data from other parts of the kernel and they cannot be grouped together. All sorts of workarounds were proposed, but no good solution in sight. So don't go this route for THP. Make everything you need to be struct_ops based and/or pass whatever extra data into these ops.

will change it.

Quoted 7 lines

Also think of scoping for bpf-thp from the start.
Currently st_ops/thp_get_order is only one and it's global. It's ok for prototypes and experiments, but not ok for landing upstream. I think cgroup would a natural scope and different cgroups might want their own bpf based THP hints. Once you do that, think through how delegation of suggested order will propagate through hierarchy.

+ Tejun

As Johannes Weiner previously explained [[0]], cgroups are designed as nested hierarchies for partitioning resources. They are a poor fit for enforcing arbitrary, non-hierarchical policies.

: Cgroups are for nested trees dividing up resources. They're not a good : fit for arbitrary, non-hierarchical policy settings.

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

The THP policy is a quintessential example of such an arbitrary setting. Even within a single cgroup, it is often necessary to enable THP for performance-critical tasks while disabling it for others to avoid latency spikes. Implementing this policy through a cgroup interface that propagates hierarchically would eliminate the crucial ability to configure it on a per-task basis.

While the bpf-thp mechanism has a global scope, this does not limit its application to a single system-wide policy. In contrast to a hierarchical cgroup-based setting, bpf-thp offers the flexibility to set policies per-task, per-cgroup, or globally. Fundamentally, it is a more powerful variant of prctl(), not a variant of cgroup interface file.

Quoted 3 lines

bpf-oom seems to be aligning toward the same design principles, so don't reinvent the wheel.

Since bpf-oom's role is to select a task to kill from within **a defined group of tasks**, it is inherently well-suited for cgroup-based management.

Signature · 2 lines
Regards
Yafang
#57 lore raw

PATCH v7 8/10 selftests/bpf: add test case to update THP policy

In reply to Yafang Shao

EBUSY is returned when attempting to install a new BPF program while one is already running, though updates to existing programs are permitted.

---
 .../selftests/bpf/prog_tests/thp_adjust.c     | 23 +++++++++++++++++++
 .../selftests/bpf/progs/test_thp_adjust.c     | 14 +++++++++++
 2 files changed, 37 insertions(+)
tools/testing/selftests/bpf/prog_tests/thp_adjust.c +23 -0
index a4a34ee28301..30172f2ee5d5 100644
@@ -170,6 +170,27 @@ static void subtest_thp_policy(void)
 	bpf_link__destroy(ops_link);
 }
 
+static void subtest_thp_policy_update(void)
+{
+	struct bpf_link *old_link, *new_link;
+	int err;
+
+	old_link = bpf_map__attach_struct_ops(skel->maps.swap_ops);
+	if (!ASSERT_OK_PTR(old_link, "attach_old_link"))
+		return;
+
+	new_link = bpf_map__attach_struct_ops(skel->maps.khugepaged_ops);
+	if (!ASSERT_NULL(new_link, "attach_new_link"))
+		goto destory_old;
+	ASSERT_EQ(errno, EBUSY, "attach_new_link");
+
+	err = bpf_link__update_map(old_link, skel->maps.khugepaged_ops);
+	ASSERT_EQ(err, 0, "update_old_link");
+
+destory_old:
+	bpf_link__destroy(old_link);
+}
+
 static int thp_adjust_setup(void)
 {
 	int err, cgrp_fd, cgrp_id, pmd_order;
@@ -249,6 +270,8 @@ void test_thp_adjust(void)
 
 	if (test__start_subtest("alloc_in_khugepaged"))
 		subtest_thp_policy();
+	if (test__start_subtest("policy_update"))
+		subtest_thp_policy_update();
 
 	thp_adjust_destroy();
 }
tools/testing/selftests/bpf/progs/test_thp_adjust.c +14 -0
index 93c7927e827a..175e65c5899f 100644
@@ -98,3 +98,17 @@ SEC(".struct_ops.link")
 struct bpf_thp_ops khugepaged_ops = {
 	.thp_get_order = (void *)alloc_in_khugepaged,
 };
+
+SEC("struct_ops/thp_get_order")
+int BPF_PROG(alloc_not_in_swap, struct vm_area_struct *vma, enum bpf_thp_vma_type vma_type,
+	     enum tva_type tva_type, unsigned long orders)
+{
+	if (tva_type == TVA_SWAP)
+		return 0;
+	return -1;
+}
+
+SEC(".struct_ops.link")
+struct bpf_thp_ops swap_ops = {
+	.thp_get_order = (void *)alloc_not_in_swap,
+};
Signature · 1 line
2.47.3
#58 lore raw

PATCH v7 9/10 selftests/bpf: add test cases for invalid thp_adjust usage

In reply to Yafang Shao

1. The trusted vma->vm_mm pointer can be null and must be checked before dereferencing.
2. The trusted mm->owner pointer can be null and must be checked before dereferencing.
3. Sleepable programs are prohibited because the call site operates under RCU protection.

---
 .../selftests/bpf/prog_tests/thp_adjust.c     |  7 +++++
 .../bpf/progs/test_thp_adjust_sleepable.c     | 22 ++++++++++++++
 .../bpf/progs/test_thp_adjust_trusted_owner.c | 30 +++++++++++++++++++
 .../bpf/progs/test_thp_adjust_trusted_vma.c   | 27 +++++++++++++++++
 4 files changed, 86 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_sleepable.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_owner.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_vma.c
tools/testing/selftests/bpf/prog_tests/thp_adjust.c +7 -0
index 30172f2ee5d5..bbe1a82345ef 100644
@@ -5,6 +5,9 @@
 #include <test_progs.h>
 #include "cgroup_helpers.h"
 #include "test_thp_adjust.skel.h"
+#include "test_thp_adjust_sleepable.skel.h"
+#include "test_thp_adjust_trusted_vma.skel.h"
+#include "test_thp_adjust_trusted_owner.skel.h"
 
 #define LEN (16 * 1024 * 1024) /* 16MB */
 #define THP_ENABLED_FILE "/sys/kernel/mm/transparent_hugepage/enabled"
@@ -274,4 +277,8 @@ void test_thp_adjust(void)
 		subtest_thp_policy_update();
 
 	thp_adjust_destroy();
+
+	RUN_TESTS(test_thp_adjust_trusted_vma);
+	RUN_TESTS(test_thp_adjust_trusted_owner);
+	RUN_TESTS(test_thp_adjust_sleepable);
 }
tools/testing/selftests/bpf/progs/test_thp_adjust_sleepable.c new file +22 -0
new file mode 100644
index 000000000000..9b92359f9789
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("struct_ops.s/thp_get_order")
+__failure __msg("attach to unsupported member thp_get_order of struct bpf_thp_ops")
+int BPF_PROG(thp_sleepable, struct vm_area_struct *vma, enum bpf_thp_vma_type vma_type,
+	     enum tva_type tva_type, unsigned long orders)
+{
+	return -1;
+}
+
+SEC(".struct_ops.link")
+struct bpf_thp_ops vma_ops = {
+	.thp_get_order = (void *)thp_sleepable,
+};
tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_owner.c new file +30 -0
new file mode 100644
index 000000000000..b3f98c2a9b43
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("struct_ops/thp_get_order")
+__failure __msg("R3 pointer arithmetic on rcu_ptr_or_null_ prohibited, null-check it first")
+int BPF_PROG(thp_trusted_owner, struct vm_area_struct *vma, enum bpf_thp_vma_type vma_type,
+	     enum tva_type tva_type, unsigned long orders)
+{
+	struct mm_struct *mm = vma->vm_mm;
+	struct task_struct *p;
+
+	if (!mm)
+		return 0;
+
+	p = mm->owner;
+	bpf_printk("The task name is %s\n", p->comm);
+	return -1;
+}
+
+SEC(".struct_ops.link")
+struct bpf_thp_ops vma_ops = {
+	.thp_get_order = (void *)thp_trusted_owner,
+};
tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_vma.c new file +27 -0
new file mode 100644
index 000000000000..5ce100348714
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("struct_ops/thp_get_order")
+__failure __msg("R1 invalid mem access 'trusted_ptr_or_null_'")
+int BPF_PROG(thp_trusted_vma, struct vm_area_struct *vma, enum bpf_thp_vma_type vma_type,
+	     enum tva_type tva_type, unsigned long orders)
+{
+	struct mm_struct *mm = vma->vm_mm;
+	struct task_struct *p = mm->owner;
+
+	if (!p)
+		return 0;
+	return -1;
+}
+
+SEC(".struct_ops.link")
+struct bpf_thp_ops vma_ops = {
+	.thp_get_order = (void *)thp_trusted_vma,
+};
Signature · 1 line
2.47.3
#59 lore raw

PATCH v7 10/10 Documentation: add BPF-based THP policy management

In reply to Yafang Shao

Add the documentation.

---
 Documentation/admin-guide/mm/transhuge.rst | 46 ++++++++++++++++++++++
 1 file changed, 46 insertions(+)
Documentation/admin-guide/mm/transhuge.rst +46 -0
index 1654211cc6cf..1e072eaacf65 100644
@@ -738,3 +738,49 @@ support enabled just fine as always. No difference can be noted in
 hugetlbfs other than there will be less overall fragmentation. All
 usual features belonging to hugetlbfs are preserved and
 unaffected. libhugetlbfs will also work fine as usual.
+
+BPF-based THP adjustment
+========================
+
+Overview
+--------
+
+When the system is configured with "always" or "madvise" THP mode, a BPF program
+can be used to adjust THP allocation policies dynamically. This enables
+fine-grained control over THP decisions based on various factors including
+workload identity, allocation context, and system memory pressure.
+
+Program Interface
+-----------------
+
+This feature implements a struct_ops BPF program with the following interface::
+
+  int thp_get_order(struct vm_area_struct *vma,
+                    enum bpf_thp_vma_type vma_type,
+                    enum tva_type tva_type,
+                    unsigned long orders);
+
+Parameters::
+
+  @vma: vm_area_struct associated with the THP allocation
+  @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
+             BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE
+             if neither is set.
+  @tva_type: TVA type for current @vma
+  @orders: Bitmask of requested THP orders for this allocation
+           - PMD-mapped allocation if PMD_ORDER is set
+           - mTHP allocation otherwise
+
+Return value::
+
+  The suggested THP order from the BPF program for allocation. It will not
+  exceed the highest requested order in @orders. Return -1 to indicate that the
+  original requested @orders should remain unchanged.
+
+Implementation Notes
+--------------------
+
+This is currently an experimental feature.
+CONFIG_BPF_GET_THP_ORDER must be enabled to use it.
+Only one BPF program can be attached at a time, but the program can be updated
+dynamically to adjust policies without requiring affected tasks to be restarted.
Signature · 1 line
2.47.3
reviewer
#60 lore raw
In reply to Yafang Shao

Seems like we forgot to CC [email protected] ;p

Quoted 298 lines · Yafang Shao

Background
==========

Our production servers consistently configure THP to "never" due to historical incidents caused by its behavior. Key issues include:
- Increased Memory Consumption
THP significantly raises overall memory usage, reducing available memory for workloads.

- Latency Spikes
Random latency spikes occur due to frequent memory compaction triggered by THP.

- Lack of Fine-Grained Control
THP tuning is globally configured, making it unsuitable for containerized environments. When multiple workloads share a host, enabling THP without per-workload control leads to unpredictable behavior.

Due to these issues, administrators avoid switching to madvise or always modes—unless per-workload THP control is implemented.

To address this, we propose BPF-based THP policy for flexible adjustment. Additionally, as David mentioned, this mechanism can also serve as a policy prototyping tool (test policies via BPF before upstreaming them).

Proposed Solution
=================

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook thp_get_order(), allowing BPF programs to influence THP order selection based on factors such as:

- Workload identity
For example, workloads running in specific containers or cgroups.
- Allocation context
Whether the allocation occurs during a page fault, khugepaged, swap or other paths.
- VMA's memory advice settings
MADV_HUGEPAGE or MADV_NOHUGEPAGE
- Memory pressure
PSI system data or associated cgroup PSI metrics

The new interface for the BPF program is as follows:

/**
* @thp_get_order: Get the suggested THP orders from a BPF program for allocation
* @vma: vm_area_struct associated with the THP allocation
* @vma_type: The VMA type, such as BPF_THP_VM_HUGEPAGE if VM_HUGEPAGE is set
* BPF_THP_VM_NOHUGEPAGE if VM_NOHUGEPAGE is set, or BPF_THP_VM_NONE
* if neither is set.
* @tva_type: TVA type for current @vma
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Return: The suggested THP order from the BPF program for allocation. It will
* not exceed the highest requested order in @orders. Return -1 to
* indicate that the original requested @orders should remain unchanged.
*/

int thp_get_order(struct vm_area_struct *vma,
enum bpf_thp_vma_type vma_type,
enum tva_type tva_type,
unsigned long orders);

Only a single BPF program can be attached at any given time, though it can be dynamically updated to adjust the policy. The implementation supports anonymous THP, shmem THP, and mTHP, with future extensions planned for file-backed THP.

This functionality is only active when system-wide THP is configured to madvise or always mode. It remains disabled in never mode. Additionally, if THP is explicitly disabled for a specific task via prctl(), this BPF functionality will also be unavailable for that task

**WARNING**
- This feature requires CONFIG_BPF_GET_THP_ORDER (marked EXPERIMENTAL) to be enabled.
- The interface may change
- Behavior may differ in future kernel versions
- We might remove it in the future

Selftests
=========

BPF CI
------

Patch #7: Implements a basic BPF THP policy that restricts THP allocation
via khugepaged to tasks within a specified memory cgroup.
Patch #8: Provides tests for dynamic BPF program updates and replacement. Patch #9: Includes negative tests for invalid BPF helper usage, verifying
proper verification by the BPF verifier.

Currently, several dependency patches reside in mm-new but haven't been merged into bpf-next. To enable BPF CI testing, these dependencies were manually applied to bpf-next. All selftests in this series pass successfully [0].

Performance Evaluation
----------------------

Performance impact was measured given the page fault handler modifications. The standard perf bench mem memset benchmark was employed to assess page fault performance.

Testing was conducted on an AMD EPYC 7W83 64-Core Processor (single NUMA node). Due to variance between individual test runs, a script executed 10000 iterations to calculate meaningful averages.

- Baseline (without this patch series)
- With patch series but no BPF program attached
- With patch series and BPF program attached

The results across three configurations show negligible performance impact:

Number of runs: 10,000
Average throughput: 40-41 GB/sec

Production verification
-----------------------

We have successfully deployed a variant of this approach across numerous Kubernetes production servers. The implementation enables THP for specific workloads (such as applications utilizing ZGC [1]) while disabling it for others. This selective deployment has operated flawlessly, with no regression reports to date.

For ZGC-based applications, our verification demonstrates that shmem THP delivers significant improvements:
- Reduced CPU utilization
- Lower average latencies

We are continuously extending its support to more workloads, such as TCMalloc-based services. [2]

Deployment Steps in our production servers are as follows,

1. Initial Setup:
- Set THP mode to "never" (disabling THP by default).
- Attach the BPF program and pin the BPF maps and links.
- Pinning ensures persistence (like a kernel module), preventing disruption under system pressure.
- A THP whitelist map tracks allowed cgroups (initially empty -> no THP allocations).

2. Enable THP Control:
- Switch THP mode to "always" or "madvise" (BPF now governs actual allocations).

3. Dynamic Management:
- To permit THP for a cgroup, add its ID to the whitelist map.
- To revoke permission, remove the cgroup ID from the map.
- The BPF program can be updated live (policy adjustments require no task interruption).

4. To roll back, disable THP and remove this BPF program.

**WARNING**
Be aware that the maintainers do not suggest this use case, as the BPF hook interface is unstable and might be removed from the upstream kernel—unless you have your own kernel team to maintain it ;-)

Future work
===========

file-backed THP policy
----------------------

Based on our validation with production workloads, we observed mixed results with XFS large folios (also known as file-backed THP):

- Performance Benefits
Some workloads demonstrated significant improvements with XFS large folios enabled
- Performance Regression
Some workloads experienced degradation when using XFS large folios

These results demonstrate that File THP, similar to anonymous THP, requires a more granular approach instead of a uniform implementation.

We will extend the BPF-based order selection mechanism to support file-backed THP allocation policies.

Hooking fork() with BPF for Task Configuration
----------------------------------------------

The current method for controlling a newly fork()-ed task involves calling prctl() (e.g., with PR_SET_THP_DISABLE) to set flags in its mm->flags. This requires explicit userspace modification.

A more efficient alternative is to implement a new BPF hook within the fork() path. This hook would allow a BPF program to set the task's mm->flags directly after mm initialization, leveraging BPF helpers for a solution that is transparent to userspace. This is particularly valuable in data center environments for fleet-wide management.

Changes:
=======:

v6->v7:
Key Changes Implemented Based on Feedback:
From Lorenzo:
- Rename the hook from get_suggested_order() to bpf_hook_get_thp_order().
- Rename bpf_thp.c to huge_memory_bpf.c
- Focuse the current patchset on THP order selection
- Add the BPF hook into thp_vma_allowable_orders()
- Make the hook VMA-based and remove the mm parameter
- Modify the BPF program to return a single order
- Stop passing vma_flags directly to BPF programs
- Mark vma->vm_mm as trusted_or_null
- Change the MAINTAINER file
From Andrii:
- Mark mm->owner as rcu_or_null to avoid introducing new helpers
From Barry:
- decouple swap from the normal page fault path
kernel test robot:
- Fix a sparse warning
Shakeel helped clarify the implementation.

RFC v5-> v6: https://lwn.net/Articles/1035116/
- Code improvement around the RCU usage (Usama)
- Add selftests for khugepaged fork (Usama)
- Add performance data for page fault (Usama)
- Remove the RFC tag

RFC v4->v5: https://lwn.net/Articles/1034265/
- Add support for vma (David)
- Add mTHP support in khugepaged (Zi)
- Use bitmask of all allowed orders instead (Zi)
- Retrieve the page size and PMD order rather than hardcoding them (Zi)

RFC v3->v4: https://lwn.net/Articles/1031829/
- Use a new interface get_suggested_order() (David)
- Mark it as experimental (David, Lorenzo)
- Code improvement in THP (Usama)
- Code improvement in BPF struct ops (Amery)

RFC v2->v3: https://lwn.net/Articles/1024545/
- Finer-graind tuning based on madvise or always mode (David, Lorenzo)
- Use BPF to write more advanced policies logic (David, Lorenzo)

RFC v1->v2: https://lwn.net/Articles/1021783/
The main changes are as follows,
- Use struct_ops instead of fmod_ret (Alexei)
- Introduce a new THP mode (Johannes)
- Introduce new helpers for BPF hook (Zi)
- Refine the commit log

RFC v1: https://lwn.net/Articles/1019290/

Yafang Shao (10):
mm: thp: remove disabled task from khugepaged_mm_slot
mm: thp: add support for BPF based THP order selection
mm: thp: decouple THP allocation between swap and page fault paths
mm: thp: enable THP allocation exclusively through khugepaged
bpf: mark mm->owner as __safe_rcu_or_null
bpf: mark vma->vm_mm as __safe_trusted_or_null selftests/bpf: add a simple BPF based THP policy selftests/bpf: add test case to update THP policy selftests/bpf: add test cases for invalid thp_adjust usage
Documentation: add BPF-based THP policy management

 Documentation/admin-guide/mm/transhuge.rst    |  46 +++
 MAINTAINERS                                   |   3 +
 include/linux/huge_mm.h                       |  29 +-
 include/linux/khugepaged.h                    |   1 +
 kernel/bpf/verifier.c                         |   8 +
 kernel/sys.c                                  |   6 +
 mm/Kconfig                                    |  12 +
 mm/Makefile                                   |   1 +
 mm/huge_memory.c                              |   3 +-
 mm/huge_memory_bpf.c                          | 243 +++++++++++++++
 mm/khugepaged.c                               |  19 +-
 mm/memory.c                                   |  15 +-
 tools/testing/selftests/bpf/config            |   3 +
 .../selftests/bpf/prog_tests/thp_adjust.c     | 284 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/lsm.c       |   8 +-
 .../selftests/bpf/progs/test_thp_adjust.c     | 114 +++++++
 .../bpf/progs/test_thp_adjust_sleepable.c     |  22 ++
 .../bpf/progs/test_thp_adjust_trusted_owner.c |  30 ++
 .../bpf/progs/test_thp_adjust_trusted_vma.c   |  27 ++
 19 files changed, 849 insertions(+), 25 deletions(-)
 create mode 100644 mm/huge_memory_bpf.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/thp_adjust.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_sleepable.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_owner.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_vma.c

--
2.47.3