Skip to contentSkip to conversation

mm, bpf: BPF based THP order selection

PATCH Superseded Yafang Shao 8 participants 61 messages Aug 26, 2025 – Sep 3, 2025

Subsystems (10)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 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 [0], this mechanism can also serve as a policy prototyping tool (test policies via BPF before upstreaming them).

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

As suggested by David [0], we introduce a new BPF interface:

/**
* @get_suggested_order: Get the suggested THP orders for allocation
* @mm: mm_struct associated with the THP allocation
* @vma__nullable: vm_area_struct associated with the THP allocation (may be NULL)

 *                 When NULL, the decision should be based on @mm (i.e., when
 *                 triggered from an mm-scope hook rather than a VMA-specific
 *                 context).
 *                 Must belong to @mm (guaranteed by the caller).
 * @vma_flags: use these vm_flags instead of @vma->vm_flags (0 if @vma is NULL)
 * @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)
 * @orders: Bitmask of requested THP orders for this allocation
 *          - PMD-mapped allocation if PMD_ORDER is set
 *          - mTHP allocation otherwise
 *
 * Rerurn: Bitmask of suggested THP orders for allocation. The highest
 *         suggested order will not exceed the highest requested order
 *         in @orders.
 */
 int (*get_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
                            u64 vma_flags, enum tva_type tva_flags, int orders) __rcu;

This interface:
- Supports both use cases (per-workload tuning + policy prototyping).
- Can be extended with BPF helpers (e.g., for memory pressure awareness).

This is an experimental feature. To use it, you must enable CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION.

Warning:
- The interface may change
- Behavior may differ in future kernel versions
- We might remove it in the future

Selftests
=========

BPF selftests
-------------

Patch #5: Implements a basic BPF THP policy that restricts THP allocation
via khugepaged to tasks within a specified memory cgroup.
Patch #6: Contains test cases validating the khugepaged fork behavior. Patch #7: Provides tests for dynamic BPF program updates and replacement. Patch #8: 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:
mm: add bitmap mm->flags field
mm/huge_memory: convert "tva_flags" to "enum tva_type"
mm: convert core mm to mm_flags_*() accessors

To enable BPF CI testing, these dependencies were manually applied to bpf-next [1]. All selftests in this series pass successfully. The observed CI failures are unrelated to these changes.

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

As suggested by Usama [2], 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 and standard deviations.

The results across three configurations show negligible performance impact:
- Baseline (without this patch series)
- With patch series but no BPF program attached
- With patch series and BPF program attached

The result are as follows,

Number of runs: 10,000
Average throughput: 40-41 GB/sec
Standard deviation: 7-8 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 [3]) 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

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

Based on our validation with production workloads, we observed mixed results with XFS large folios (also known as File 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 THP allocation policies.

Changes:
=======

RFC v5-> v6:
- 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: add support for BPF based THP order selection
mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()
mm: thp: add a new kfunc bpf_mm_get_task()
bpf: mark vma->vm_mm as trusted
selftests/bpf: add a simple BPF based THP policy selftests/bpf: add test case for khugepaged fork selftests/bpf: add test case to update thp policy selftests/bpf: add test cases for invalid thp_adjust usage
Documentation: add BPF-based THP adjustment documentation
MAINTAINERS: add entry for BPF-based THP adjustment

 Documentation/admin-guide/mm/transhuge.rst    |  47 +++
 MAINTAINERS                                   |  10 +
 include/linux/huge_mm.h                       |  15 +
 include/linux/khugepaged.h                    |  12 +-
 kernel/bpf/verifier.c                         |   5 +
 mm/Kconfig                                    |  12 +
 mm/Makefile                                   |   1 +
 mm/bpf_thp.c                                  | 269 ++++++++++++++
 mm/huge_memory.c                              |  10 +
 mm/khugepaged.c                               |  26 +-
 mm/memory.c                                   |  18 +-
 tools/testing/selftests/bpf/config            |   3 +
 .../selftests/bpf/prog_tests/thp_adjust.c     | 343 ++++++++++++++++++
 .../selftests/bpf/progs/test_thp_adjust.c     | 115 ++++++
 .../bpf/progs/test_thp_adjust_trusted_vma.c   |  27 ++
 .../progs/test_thp_adjust_unreleased_memcg.c  |  24 ++
 .../progs/test_thp_adjust_unreleased_task.c   |  25 ++
 17 files changed, 955 insertions(+), 7 deletions(-)
 create mode 100644 mm/bpf_thp.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_trusted_vma.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_memcg.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_task.c
Signature · 1 line
2.47.3
#1 lore raw

PATCH v6 1/10 mm: thp: add support for BPF based THP order selection

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook get_suggested_order() [0], 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, or other paths.
- System memory pressure
(May require new BPF helpers to accurately assess memory pressure.)

Key Details:
- Only one BPF program can be attached at a time, but it can be updated dynamically to adjust the policy.
- Supports automatic mTHP order selection and per-workload THP policies.
- Only functional when THP is set to madise or always.

It requires CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION to enable. [1] This feature is unstable and may evolve in future kernel versions.

---
 include/linux/huge_mm.h    |  15 +++
 include/linux/khugepaged.h |  12 ++-
 mm/Kconfig                 |  12 +++
 mm/Makefile                |   1 +
 mm/bpf_thp.c               | 186 +++++++++++++++++++++++++++++++++++++
 mm/huge_memory.c           |  10 ++
 mm/khugepaged.c            |  26 +++++-
 mm/memory.c                |  18 +++-
 8 files changed, 273 insertions(+), 7 deletions(-)
 create mode 100644 mm/bpf_thp.c
include/linux/huge_mm.h +15 -0
index 1ac0d06fb3c1..f0c91d7bd267 100644
@@ -6,6 +6,8 @@
 
 #include <linux/fs.h> /* only for vma_is_dax() */
 #include <linux/kobject.h>
+#include <linux/pgtable.h>
+#include <linux/mm.h>
 
 vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf);
 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
@@ -56,6 +58,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;
@@ -195,6 +198,18 @@ static inline bool hugepage_global_always(void)
 			(1<<TRANSPARENT_HUGEPAGE_FLAG);
 }
 
+#ifdef CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION
+int get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+			u64 vma_flags, enum tva_type tva_flags, int orders);
+#else
+static inline int
+get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+		    u64 vma_flags, enum tva_type tva_flags, int orders)
+{
+	return orders;
+}
+#endif
+
 static inline int highest_order(unsigned long orders)
 {
 	return fls_long(orders) - 1;
include/linux/khugepaged.h +11 -1
index eb1946a70cff..d81c1228a21f 100644
@@ -4,6 +4,8 @@
 
 #include <linux/mm.h>
 
+#include <linux/huge_mm.h>
+
 extern unsigned int khugepaged_max_ptes_none __read_mostly;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 extern struct attribute_group khugepaged_attr_group;
@@ -22,7 +24,15 @@ extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 
 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {
-	if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm))
+	/*
+	 * THP allocation policy can be dynamically modified via BPF. Even if a
+	 * task was allowed to allocate THPs, BPF can decide whether its forked
+	 * child can allocate THPs.
+	 *
+	 * The MMF_VM_HUGEPAGE flag will be cleared by khugepaged.
+	 */
+	if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm) &&
+		get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER)))
 		__khugepaged_enter(mm);
 }
 
mm/Kconfig +12 -0
index 4108bcd96784..d10089e3f181 100644
@@ -924,6 +924,18 @@ config NO_PAGE_MAPCOUNT
 
 	  EXPERIMENTAL because the impact of some changes is still unclear.
 
+config EXPERIMENTAL_BPF_ORDER_SELECTION
+	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 ef54aa615d9d..cb55d1509be1 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_EXPERIMENTAL_BPF_ORDER_SELECTION) += bpf_thp.o
 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
 obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
mm/bpf_thp.c new file +186 -0

186 lines not on this page · raw message

mm/huge_memory.c +10 -0
index d89992b65acc..bd8f8f34ab3c 100644
@@ -1349,6 +1349,16 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
 		return ret;
 	khugepaged_enter_vma(vma, vma->vm_flags);
 
+	/*
+	 * This check must occur after khugepaged_enter_vma() because:
+	 * 1. We may permit THP allocation via khugepaged
+	 * 2. While simultaneously disallowing THP allocation
+	 *    during page fault handling
+	 */
+	if (get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_PAGEFAULT, BIT(PMD_ORDER)) !=
+				BIT(PMD_ORDER))
+		return VM_FAULT_FALLBACK;
+
 	if (!(vmf->flags & FAULT_FLAG_WRITE) &&
 			!mm_forbids_zeropage(vma->vm_mm) &&
 			transparent_hugepage_use_zero_page()) {
mm/khugepaged.c +24 -2
index d3d4f116e14b..935583626db6 100644
@@ -474,7 +474,9 @@ void khugepaged_enter_vma(struct vm_area_struct *vma,
 {
 	if (!mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
 	    hugepage_pmd_enabled()) {
-		if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
+		if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER) &&
+		    get_suggested_order(vma->vm_mm, vma, vm_flags, TVA_KHUGEPAGED,
+					BIT(PMD_ORDER)))
 			__khugepaged_enter(vma->vm_mm);
 	}
 }
@@ -934,6 +936,8 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
 		return SCAN_ADDRESS_RANGE;
 	if (!thp_vma_allowable_order(vma, vma->vm_flags, type, PMD_ORDER))
 		return SCAN_VMA_CHECK;
+	if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, type, BIT(PMD_ORDER)))
+		return SCAN_VMA_CHECK;
 	/*
 	 * Anon VMA expected, the address may be unmapped then
 	 * remapped to file after khugepaged reaquired the mmap_lock.
@@ -1465,6 +1469,11 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
 		/* khugepaged_mm_lock actually not necessary for the below */
 		mm_slot_free(mm_slot_cache, mm_slot);
 		mmdrop(mm);
+	} else if (!get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER))) {
+		hash_del(&slot->hash);
+		list_del(&slot->mm_node);
+		mm_flags_clear(MMF_VM_HUGEPAGE, mm);
+		mm_slot_free(mm_slot_cache, mm_slot);
 	}
 }
 
@@ -1538,6 +1547,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 	if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
 		return SCAN_VMA_CHECK;
 
+	if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_FORCED_COLLAPSE,
+				 BIT(PMD_ORDER)))
+		return SCAN_VMA_CHECK;
 	/* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
 	if (userfaultfd_wp(vma))
 		return SCAN_PTE_UFFD_WP;
@@ -2416,6 +2428,10 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 	 * the next mm on the list.
 	 */
 	vma = NULL;
+
+	/* If this mm is not suitable for the scan list, we should remove it. */
+	if (!get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER)))
+		goto breakouterloop_mmap_lock;
 	if (unlikely(!mmap_read_trylock(mm)))
 		goto breakouterloop_mmap_lock;
 
@@ -2432,7 +2448,9 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 			progress++;
 			break;
 		}
-		if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER)) {
+		if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER) ||
+		    !get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_KHUGEPAGED,
+					 BIT(PMD_ORDER))) {
 skip:
 			progress++;
 			continue;
@@ -2769,6 +2787,10 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 	if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
 		return -EINVAL;
 
+	if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_FORCED_COLLAPSE,
+				 BIT(PMD_ORDER)))
+		return -EINVAL;
+
 	cc = kmalloc(sizeof(*cc), GFP_KERNEL);
 	if (!cc)
 		return -ENOMEM;
mm/memory.c +14 -4
index d9de6c056179..0178857aa058 100644
@@ -4486,6 +4486,7 @@ static inline unsigned long thp_swap_suitable_orders(pgoff_t swp_offset,
 static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
+	int order, suggested_orders;
 	unsigned long orders;
 	struct folio *folio;
 	unsigned long addr;
@@ -4493,7 +4494,6 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 	spinlock_t *ptl;
 	pte_t *pte;
 	gfp_t gfp;
-	int order;
 
 	/*
 	 * If uffd is active for the vma we need per-page fault fidelity to
@@ -4510,13 +4510,18 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 	if (!zswap_never_enabled())
 		goto fallback;
 
+	suggested_orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags,
+					       TVA_PAGEFAULT,
+					       BIT(PMD_ORDER) - 1);
+	if (!suggested_orders)
+		goto fallback;
 	entry = pte_to_swp_entry(vmf->orig_pte);
 	/*
 	 * 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,
-					  BIT(PMD_ORDER) - 1);
+					  suggested_orders);
 	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
 	orders = thp_swap_suitable_orders(swp_offset(entry),
 					  vmf->address, orders);
@@ -5044,12 +5049,12 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	int order, suggested_orders;
 	unsigned long orders;
 	struct folio *folio;
 	unsigned long addr;
 	pte_t *pte;
 	gfp_t gfp;
-	int order;
 
 	/*
 	 * If uffd is active for the vma we need per-page fault fidelity to
@@ -5058,13 +5063,18 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 	if (unlikely(userfaultfd_armed(vma)))
 		goto fallback;
 
+	suggested_orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags,
+					       TVA_PAGEFAULT,
+					       BIT(PMD_ORDER) - 1);
+	if (!suggested_orders)
+		goto fallback;
 	/*
 	 * Get a list of all the (large) orders below PMD_ORDER that are enabled
 	 * for this vma. Then filter out the orders that can't be allocated over
 	 * the faulting address and still be fully contained in the vma.
 	 */
 	orders = thp_vma_allowable_orders(vma, vma->vm_flags, TVA_PAGEFAULT,
-					  BIT(PMD_ORDER) - 1);
+					  suggested_orders);
 	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
 
 	if (!orders)
Signature · 1 line
2.47.3
#2 lore raw

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

kernel test robot noticed the following build warnings:

Hi Yafang,

kernel test robot noticed the following build warnings:

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

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/mm-thp-add-support-for-BPF-based-THP-order-selection/20250826-152415
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250826071948.2618-2-laoar.shao%40gmail.com
patch subject: [PATCH v6 mm-new 01/10] mm: thp: add support for BPF based THP order selection
config: loongarch-randconfig-r113-20250827 (https://download.01.org/0day-ci/archive/20250827/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce: (https://download.01.org/0day-ci/archive/20250827/[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]/

sparse warnings: (new ones prefixed by >>)

>> mm/bpf_thp.c:47:31: sparse: sparse: incompatible types in comparison expression (different address spaces):
   mm/bpf_thp.c:47:31: sparse:    int ( [noderef] __rcu * )( ... )
   mm/bpf_thp.c:47:31: sparse:    int ( * )( ... )
   mm/bpf_thp.c:101:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   mm/bpf_thp.c:101:9: sparse:    int ( [noderef] __rcu * )( ... )
   mm/bpf_thp.c:101:9: sparse:    int ( * )( ... )
   mm/bpf_thp.c:102:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   mm/bpf_thp.c:102:9: sparse:    int ( [noderef] __rcu * )( ... )
   mm/bpf_thp.c:102:9: sparse:    int ( * )( ... )
   mm/bpf_thp.c:111:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   mm/bpf_thp.c:111:9: sparse:    int ( [noderef] __rcu * )( ... )
   mm/bpf_thp.c:111:9: sparse:    int ( * )( ... )
   mm/bpf_thp.c:112:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   mm/bpf_thp.c:112:9: sparse:    int ( [noderef] __rcu * )( ... )
   mm/bpf_thp.c:112:9: sparse:    int ( * )( ... )
   mm/bpf_thp.c:112:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   mm/bpf_thp.c:112:9: sparse:    int ( [noderef] __rcu * )( ... )
   mm/bpf_thp.c:112:9: sparse:    int ( * )( ... )
   mm/bpf_thp.c:133:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   mm/bpf_thp.c:133:9: sparse:    int ( [noderef] __rcu * )( ... )
   mm/bpf_thp.c:133:9: sparse:    int ( * )( ... )
   mm/bpf_thp.c:134:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   mm/bpf_thp.c:134:9: sparse:    int ( [noderef] __rcu * )( ... )
   mm/bpf_thp.c:134:9: sparse:    int ( * )( ... )
   mm/bpf_thp.c:134:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   mm/bpf_thp.c:134:9: sparse:    int ( [noderef] __rcu * )( ... )
   mm/bpf_thp.c:134:9: sparse:    int ( * )( ... )
>> mm/bpf_thp.c:102:9: sparse: sparse: dereference of noderef expression
>> mm/bpf_thp.c:102:9: sparse: sparse: dereference of noderef expression

mm/bpf_thp.c:112:9: sparse: sparse: dereference of noderef expression mm/bpf_thp.c:134:9: sparse: sparse: dereference of noderef expression mm/bpf_thp.c:134:9: sparse: sparse: dereference of noderef expression mm/bpf_thp.c:134:9: sparse: sparse: dereference of noderef expression mm/bpf_thp.c:148:14: sparse: sparse: dereference of noderef expression

vim +47 mm/bpf_thp.c

    33	
    34	int get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
    35				u64 vma_flags, enum tva_type tva_flags, int orders)
    36	{
    37		int (*bpf_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
    38					   u64 vma_flags, enum tva_type tva_flags, int orders);
    39		int suggested_orders = orders;
    40	
    41		/* No BPF program is attached */
    42		if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
    43			      &transparent_hugepage_flags))
    44			return suggested_orders;
    45	
    46		rcu_read_lock();
  > 47		bpf_suggested_order = rcu_dereference(bpf_thp.get_suggested_order);
    48		if (!bpf_suggested_order)
    49			goto out;
    50	
    51		suggested_orders = bpf_suggested_order(mm, vma__nullable, vma_flags, tva_flags, orders);
    52		if (highest_order(suggested_orders) > highest_order(orders))
    53			suggested_orders = orders;
    54	
    55	out:
    56		rcu_read_unlock();
    57		return suggested_orders;
    58	}
    59	
    60	static bool bpf_thp_ops_is_valid_access(int off, int size,
    61						enum bpf_access_type type,
    62						const struct bpf_prog *prog,
    63						struct bpf_insn_access_aux *info)
    64	{
    65		return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
    66	}
    67	
    68	static const struct bpf_func_proto *
    69	bpf_thp_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
    70	{
    71		return bpf_base_func_proto(func_id, prog);
    72	}
    73	
    74	static const struct bpf_verifier_ops thp_bpf_verifier_ops = {
    75		.get_func_proto = bpf_thp_get_func_proto,
    76		.is_valid_access = bpf_thp_ops_is_valid_access,
    77	};
    78	
    79	static int bpf_thp_init(struct btf *btf)
    80	{
    81		return 0;
    82	}
    83	
    84	static int bpf_thp_init_member(const struct btf_type *t,
    85				       const struct btf_member *member,
    86				       void *kdata, const void *udata)
    87	{
    88		return 0;
    89	}
    90	
    91	static int bpf_thp_reg(void *kdata, struct bpf_link *link)
    92	{
    93		struct bpf_thp_ops *ops = kdata;
    94	
    95		spin_lock(&thp_ops_lock);
    96		if (test_and_set_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
    97				     &transparent_hugepage_flags)) {
    98			spin_unlock(&thp_ops_lock);
    99			return -EBUSY;
   100		}
   101		WARN_ON_ONCE(rcu_access_pointer(bpf_thp.get_suggested_order));
 > 102		rcu_assign_pointer(bpf_thp.get_suggested_order, ops->get_suggested_order);

103 spin_unlock(&thp_ops_lock);
104 return 0;
105 }
106

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

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

Quoted 15 lines · kernel test robot

Hi Yafang,

kernel test robot noticed the following build warnings:

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

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/mm-thp-add-support-for-BPF-based-THP-order-selection/20250826-152415
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250826071948.2618-2-laoar.shao%40gmail.com
patch subject: [PATCH v6 mm-new 01/10] mm: thp: add support for BPF based THP order selection
config: loongarch-randconfig-r113-20250827 (https://download.01.org/0day-ci/archive/20250827/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce: (https://download.01.org/0day-ci/archive/20250827/[email protected]/reproduce)
Quoted 4 lines

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]/

Thanks for the report .
It seems this sparse warning can be fixed with the below additional change, would you please test it again?

mm/bpf_thp.c +4 -0
index 46b3bc96359e..b2f97f9e930d 100644
@@ -5,27 +5,32 @@
 #include <linux/huge_mm.h>
 #include <linux/khugepaged.h>
 
+/**
+ * @get_suggested_order: Get the suggested THP orders for allocation
+ * @mm: mm_struct associated with the THP allocation
+ * @vma__nullable: vm_area_struct associated with the THP allocation

(may be NULL)

+ *                 When NULL, the decision should be based on @mm (i.e., when
+ *                 triggered from an mm-scope hook rather than a VMA-specific
+ *                 context).
+ *                 Must belong to @mm (guaranteed by the caller).

+ * @vma_flags: use these vm_flags instead of @vma->vm_flags (0 if @vma is NULL)
+ * @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)
+ * @orders: Bitmask of requested THP orders for this allocation
+ * - PMD-mapped allocation if PMD_ORDER is set
+ * - mTHP allocation otherwise
+ *
+ * Rerurn: Bitmask of suggested THP orders for allocation. The highest
+ * suggested order will not exceed the highest requested order
+ * in @orders.
+ */
+typedef int suggested_order_fn_t(struct mm_struct *mm,

+                                struct vm_area_struct *vma__nullable,
+                                u64 vma_flags,
+                                enum tva_type tva_flags,
+                                int orders);
+
 struct bpf_thp_ops {
-       /**
-        * @get_suggested_order: Get the suggested THP orders for allocation
-        * @mm: mm_struct associated with the THP allocation
-        * @vma__nullable: vm_area_struct associated with the THP

allocation (may be NULL)
- * When NULL, the decision should be based on
@mm (i.e., when
- * triggered from an mm-scope hook rather than
a VMA-specific

-        *                 context).
-        *                 Must belong to @mm (guaranteed by the caller).
-        * @vma_flags: use these vm_flags instead of @vma->vm_flags (0
if @vma is NULL)
-        * @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)
-        * @orders: Bitmask of requested THP orders for this allocation
-        *          - PMD-mapped allocation if PMD_ORDER is set
-        *          - mTHP allocation otherwise
-        *
-        * Rerurn: Bitmask of suggested THP orders for allocation. The highest
-        *         suggested order will not exceed the highest requested order
-        *         in @orders.
-        */

- int (*get_suggested_order)(struct mm_struct *mm, struct
vm_area_struct *vma__nullable,
- u64 vma_flags, enum tva_type
tva_flags, int orders) __rcu;
+ suggested_order_fn_t __rcu *get_suggested_order;
};

static struct bpf_thp_ops bpf_thp;
@@ -34,8 +39,7 @@ static DEFINE_SPINLOCK(thp_ops_lock);
int get_suggested_order(struct mm_struct *mm, struct vm_area_struct
*vma__nullable,
u64 vma_flags, enum tva_type tva_flags, int orders)
{
- int (*bpf_suggested_order)(struct mm_struct *mm, struct
vm_area_struct *vma__nullable,
- u64 vma_flags, enum tva_type
tva_flags, int orders);

+       suggested_order_fn_t *bpf_suggested_order;
        int suggested_orders = orders;

        /* No BPF program is attached */

@@ -106,10 +110,12 @@ static int bpf_thp_reg(void *kdata, struct bpf_link *link)

static void bpf_thp_unreg(void *kdata, struct bpf_link *link) {
+ suggested_order_fn_t *old_fn;
+

        spin_lock(&thp_ops_lock);
        clear_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,

&transparent_hugepage_flags);
- WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
- rcu_replace_pointer(bpf_thp.get_suggested_order, NULL,
lockdep_is_held(&thp_ops_lock));
+ old_fn = rcu_replace_pointer(bpf_thp.get_suggested_order,
NULL, lockdep_is_held(&thp_ops_lock));

+       WARN_ON_ONCE(!old_fn);
        spin_unlock(&thp_ops_lock);

        synchronize_rcu();

@@ -117,8 +123,9 @@ static void bpf_thp_unreg(void *kdata, struct bpf_link *link)

static int bpf_thp_update(void *kdata, void *old_kdata, struct bpf_link *link) {
- struct bpf_thp_ops *ops = kdata;

+       suggested_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)
@@ -130,9 +137,10 @@ static int bpf_thp_update(void *kdata, void
*old_kdata, struct bpf_link *link)
                ret = -ENOENT;
                goto out;
        }

- WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
- rcu_replace_pointer(bpf_thp.get_suggested_order,
ops->get_suggested_order,
- lockdep_is_held(&thp_ops_lock));
+
+ new_fn = rcu_dereference(ops->get_suggested_order);
+ old_fn = rcu_replace_pointer(bpf_thp.get_suggested_order,
new_fn, lockdep_is_held(&thp_ops_lock));
+ WARN_ON_ONCE(!old_fn || !new_fn);

out:

        spin_unlock(&thp_ops_lock);

@@ -159,7 +167,7 @@ static int suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nul
}

static struct bpf_thp_ops __bpf_thp_ops = {
- .get_suggested_order = suggested_order,
+ .get_suggested_order = (suggested_order_fn_t __rcu *)suggested_order,
};

static struct bpf_struct_ops bpf_bpf_thp_ops = {

--
Regards

Yafang

#4 lore raw

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

Quoted 47 lines · Yafang Shao
Quoted 15 lines · kernel test robot

Hi Yafang,

kernel test robot noticed the following build warnings:

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

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/mm-thp-add-support-for-BPF-based-THP-order-selection/20250826-152415
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250826071948.2618-2-laoar.shao%40gmail.com
patch subject: [PATCH v6 mm-new 01/10] mm: thp: add support for BPF based THP order selection
config: loongarch-randconfig-r113-20250827 (https://download.01.org/0day-ci/archive/20250827/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce: (https://download.01.org/0day-ci/archive/20250827/[email protected]/reproduce)
Quoted 4 lines

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]/

Thanks for the report .
It seems this sparse warning can be fixed with the below additional change, would you please test it again?

mm/bpf_thp.c +13 -0
index 46b3bc96359e..b2f97f9e930d 100644
@@ -5,27 +5,32 @@
 #include <linux/huge_mm.h>
 #include <linux/khugepaged.h>
 
+/**
+ * @get_suggested_order: Get the suggested THP orders for allocation
+ * @mm: mm_struct associated with the THP allocation
+ * @vma__nullable: vm_area_struct associated with the THP allocation
 (may be NULL)
+ *                 When NULL, the decision should be based on @mm (i.e., when
+ *                 triggered from an mm-scope hook rather than a VMA-specific
+ *                 context).
+ *                 Must belong to @mm (guaranteed by the caller).
+ * @vma_flags: use these vm_flags instead of @vma->vm_flags (0 if @vma is NULL)
+ * @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)
+ * @orders: Bitmask of requested THP orders for this allocation
+ *          - PMD-mapped allocation if PMD_ORDER is set
+ *          - mTHP allocation otherwise

mm/bpf_thp.c line 31

Quoted 10 lines
mm/bpf_thp.c +10 -0
+ *
+ * Rerurn: Bitmask of suggested THP orders for allocation. The highest
+ *         suggested order will not exceed the highest requested order
+ *         in @orders.
+ */
+typedef int suggested_order_fn_t(struct mm_struct *mm,
+                                struct vm_area_struct *vma__nullable,
+                                u64 vma_flags,
+                                enum tva_type tva_flags,
+                                int orders);

Hm you are doing part of my review here as part of the fix :)

I think a respin is in order anyway so can tackle in future version.

Not sure the test bot can try out patches though? Not seen that before (nice if it or somebody on other end does though! :)

Quoted 108 lines
mm/bpf_thp.c +1 -5
+
 struct bpf_thp_ops {
-       /**
-        * @get_suggested_order: Get the suggested THP orders for allocation
-        * @mm: mm_struct associated with the THP allocation
-        * @vma__nullable: vm_area_struct associated with the THP
 allocation (may be NULL)
-        *                 When NULL, the decision should be based on

@mm (i.e., when
- * triggered from an mm-scope hook rather than
a VMA-specific

mm/bpf_thp.c +0 -3
-        *                 context).
-        *                 Must belong to @mm (guaranteed by the caller).
-        * @vma_flags: use these vm_flags instead of @vma->vm_flags (0

if @vma is NULL)

mm/bpf_thp.c +0 -10
-        * @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)
-        * @orders: Bitmask of requested THP orders for this allocation
-        *          - PMD-mapped allocation if PMD_ORDER is set
-        *          - mTHP allocation otherwise
-        *
-        * Rerurn: Bitmask of suggested THP orders for allocation. The highest
-        *         suggested order will not exceed the highest requested order
-        *         in @orders.
-        */
-       int (*get_suggested_order)(struct mm_struct *mm, struct

vm_area_struct *vma__nullable,
- u64 vma_flags, enum tva_type
tva_flags, int orders) __rcu;

mm/bpf_thp.c +2 -2
+       suggested_order_fn_t __rcu *get_suggested_order;
 };
 
 static struct bpf_thp_ops bpf_thp;
@@ -34,8 +39,7 @@ static DEFINE_SPINLOCK(thp_ops_lock);
 int get_suggested_order(struct mm_struct *mm, struct vm_area_struct
 *vma__nullable,
                        u64 vma_flags, enum tva_type tva_flags, int orders)
 {
-       int (*bpf_suggested_order)(struct mm_struct *mm, struct
 vm_area_struct *vma__nullable,
-                                  u64 vma_flags, enum tva_type
 tva_flags, int orders);
+       suggested_order_fn_t *bpf_suggested_order;
        int suggested_orders = orders;

        /* No BPF program is attached */
mm/bpf_thp.c +4 -2
@@ -106,10 +110,12 @@ static int bpf_thp_reg(void *kdata, struct bpf_link *link)
 
 static void bpf_thp_unreg(void *kdata, struct bpf_link *link)
 {
+       suggested_order_fn_t *old_fn;
+
        spin_lock(&thp_ops_lock);
        clear_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
 &transparent_hugepage_flags);
-       WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
-       rcu_replace_pointer(bpf_thp.get_suggested_order, NULL,
 lockdep_is_held(&thp_ops_lock));
+       old_fn = rcu_replace_pointer(bpf_thp.get_suggested_order,
 NULL, lockdep_is_held(&thp_ops_lock));
+       WARN_ON_ONCE(!old_fn);
        spin_unlock(&thp_ops_lock);

        synchronize_rcu();
mm/bpf_thp.c +2 -1
@@ -117,8 +123,9 @@ static void bpf_thp_unreg(void *kdata, struct
 bpf_link *link)
 
 static int bpf_thp_update(void *kdata, void *old_kdata, struct bpf_link *link)
 {
-       struct bpf_thp_ops *ops = kdata;
+       suggested_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)
mm/bpf_thp.c +4 -3
@@ -130,9 +137,10 @@ static int bpf_thp_update(void *kdata, void
 *old_kdata, struct bpf_link *link)
                ret = -ENOENT;
                goto out;
        }
-       WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
-       rcu_replace_pointer(bpf_thp.get_suggested_order,
 ops->get_suggested_order,
-                           lockdep_is_held(&thp_ops_lock));
+
+       new_fn = rcu_dereference(ops->get_suggested_order);
+       old_fn = rcu_replace_pointer(bpf_thp.get_suggested_order,
 new_fn, lockdep_is_held(&thp_ops_lock));
+       WARN_ON_ONCE(!old_fn || !new_fn);

out:

        spin_unlock(&thp_ops_lock);
mm/bpf_thp.c +0 -0
@@ -159,7 +167,7 @@ static int suggested_order(struct mm_struct *mm,

struct vm_area_struct *vma__nul
}

static struct bpf_thp_ops __bpf_thp_ops = {

mm/bpf_thp.c +1 -2
-       .get_suggested_order = suggested_order,
+       .get_suggested_order = (suggested_order_fn_t __rcu *)suggested_order,
 };
 
 static struct bpf_struct_ops bpf_bpf_thp_ops = {
 
 
--

Regards

Yafang

#5 lore raw

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

In reply to Yafang Shao
Quoted 26 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook get_suggested_order() [0], 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, or other paths.
- System memory pressure
(May require new BPF helpers to accurately assess memory pressure.)

Key Details:
- Only one BPF program can be attached at a time, but it can be updated dynamically to adjust the policy.
- Supports automatic mTHP order selection and per-workload THP policies.
- Only functional when THP is set to madise or always.

It requires CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION to enable. [1] This feature is unstable and may evolve in future kernel versions.

Quoted 6 lines
---
 include/linux/huge_mm.h    |  15 +++
 include/linux/khugepaged.h |  12 ++-
 mm/Kconfig                 |  12 +++
 mm/Makefile                |   1 +
 mm/bpf_thp.c               | 186 +++++++++++++++++++++++++++++++++++++

Please add new files to MAINTAINERS as you add them.

Quoted 11 lines
 mm/huge_memory.c           |  10 ++
 mm/khugepaged.c            |  26 +++++-
 mm/memory.c                |  18 +++-
 8 files changed, 273 insertions(+), 7 deletions(-)
 create mode 100644 mm/bpf_thp.c
include/linux/huge_mm.h +0 -0
index 1ac0d06fb3c1..f0c91d7bd267 100644

include/linux/huge_mm.h line 10

Quoted 5 lines
include/linux/huge_mm.h +2 -0
@@ -6,6 +6,8 @@
 
 #include <linux/fs.h> /* only for vma_is_dax() */
 #include <linux/kobject.h>
+#include <linux/pgtable.h>
+#include <linux/mm.h>

Hm this is a bit weird as mm.h includes huge_mm... I guess it will be handled by header defines but still.

Quoted 12 lines
include/linux/huge_mm.h +1 -0
 vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf);
 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
@@ -56,6 +58,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 static inline bool hugepage_global_always(void) · line 203

Quoted 6 lines
include/linux/huge_mm.h +3 -0
@@ -195,6 +198,18 @@ static inline bool hugepage_global_always(void)
 			(1<<TRANSPARENT_HUGEPAGE_FLAG);
 }
 
+#ifdef CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION
+int get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+			u64 vma_flags, enum tva_type tva_flags, int orders);

Not a massive fan of this naming to be honest. I think it should explicitly reference bpf, e.g. bpf_hook_thp_get_order() or something.

Right now this is super unclear as to what it's for.

Also wrt vma_flags - this type is wrong :) it's vm_flags_t and going to change to a bitmap of unlimiiteeed size soon. So probs best not to pass around as value type either.

But unclear us to purpose as mentioned elsewhere.

And also get_suggested_order() should be get_suggested_orderS() no? As you seem later in the code to be referencing a bitfield?

Also will mm ever != vma->vm_mm?

Are we hacking this for the sake of overloading what this does?

Also if we're returning a bitmask of orders which you seem to be (not sure I like that tbh - I feel like we shoudl simply provide one order but open for disucssion) - shouldn't it return an unsigned long?

Quoted 17 lines
include/linux/huge_mm.h +9 -0
+#else
+static inline int
+get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+		    u64 vma_flags, enum tva_type tva_flags, int orders)
+{
+	return orders;
+}
+#endif
+
 static inline int highest_order(unsigned long orders)
 {
 	return fls_long(orders) - 1;
include/linux/khugepaged.h +0 -0
index eb1946a70cff..d81c1228a21f 100644

include/linux/khugepaged.h line 8

Quoted 5 lines
include/linux/khugepaged.h +2 -0
@@ -4,6 +4,8 @@
 
 #include <linux/mm.h>
 
+#include <linux/huge_mm.h>
+

Hm this is iffy too, There's probably a reason we didn't include this before, the headers can be so so fragile. Let's be cautious...

Quoted 7 lines
include/linux/khugepaged.h +0 -0
 extern unsigned int khugepaged_max_ptes_none __read_mostly;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 extern struct attribute_group khugepaged_attr_group;
@@ -22,7 +24,15 @@ extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 
 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {

include/linux/khugepaged.h extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 35

Quoted 10 lines
include/linux/khugepaged.h +9 -1
-	if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm))
+	/*
+	 * THP allocation policy can be dynamically modified via BPF. Even if a
+	 * task was allowed to allocate THPs, BPF can decide whether its forked
+	 * child can allocate THPs.
+	 *
+	 * The MMF_VM_HUGEPAGE flag will be cleared by khugepaged.
+	 */
+	if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm) &&
+		get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER)))

Hmmm so there seems to be some kind of additional functionality you're providing here kinda quietly, which is to allow the exact same interface to determine whether we kick off khugepaged or not.

Don't love that, I think we should be hugely specific about that.

This bpf interface should literally be 'ok we're deciding what order we want'. It feels like a bit of a gross overloading?

Quoted 12 lines
include/linux/khugepaged.h +0 -0
 		__khugepaged_enter(mm);
 }
 
mm/Kconfig +1 -0
index 4108bcd96784..d10089e3f181 100644
@@ -924,6 +924,18 @@ config NO_PAGE_MAPCOUNT
 
 	  EXPERIMENTAL because the impact of some changes is still unclear.
 
+config EXPERIMENTAL_BPF_ORDER_SELECTION

mm/Kconfig config NO_PAGE_MAPCOUNT · line 937

Quoted 10 lines
mm/Kconfig +10 -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.

Thanks! This is important to document. Absolute nitty nit: can you capitalise 'WARNING'? Thanks!

mm/bpf_thp.c

Quoted 21 lines
mm/Kconfig +1 -0
+
 endif # TRANSPARENT_HUGEPAGE
 
 # simple helper to make the code a bit easier to read
mm/Makefile +1 -0
index ef54aa615d9d..cb55d1509be1 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_EXPERIMENTAL_BPF_ORDER_SELECTION) += bpf_thp.o
 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
 obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
mm/bpf_thp.c new file +0 -0
new file mode 100644
index 000000000000..fbff3b1bb988

As mentioned before, please update MAINTAINERS for new files. I went to great + painful lengths to get everything listed there so let's keep it that way please :P

Quoted 19 lines
mm/bpf_thp.c new file +18 -0
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+struct bpf_thp_ops {
+	/**
+	 * @get_suggested_order: Get the suggested THP orders for allocation
+	 * @mm: mm_struct associated with the THP allocation
+	 * @vma__nullable: vm_area_struct associated with the THP allocation (may be NULL)
+	 *                 When NULL, the decision should be based on @mm (i.e., when
+	 *                 triggered from an mm-scope hook rather than a VMA-specific
+	 *                 context).
+	 *                 Must belong to @mm (guaranteed by the caller).
+	 * @vma_flags: use these vm_flags instead of @vma->vm_flags (0 if @vma is NULL)
+	 * @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)

mm/bpf_thp.c line 28

Quoted 10 lines
mm/bpf_thp.c new file +10 -0
+	 * @orders: Bitmask of requested THP orders for this allocation
+	 *          - PMD-mapped allocation if PMD_ORDER is set
+	 *          - mTHP allocation otherwise
+	 *
+	 * Rerurn: Bitmask of suggested THP orders for allocation. The highest
+	 *         suggested order will not exceed the highest requested order
+	 *         in @orders.
+	 */
+	int (*get_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+				   u64 vma_flags, enum tva_type tva_flags, int orders) __rcu;

I feel like we should be declaring this function pointer type somewhere else as we're now duplicating this in two places.

mm/bpf_thp.c line 35

Quoted 7 lines
mm/bpf_thp.c new file +7 -0
+};
+
+static struct bpf_thp_ops bpf_thp;
+static DEFINE_SPINLOCK(thp_ops_lock);
+
+int get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+			u64 vma_flags, enum tva_type tva_flags, int orders)

surely tva_flag? As this is an enum value?

mm/bpf_thp.c line 38

Quoted 3 lines
mm/bpf_thp.c new file +3 -0
+{
+	int (*bpf_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+				   u64 vma_flags, enum tva_type tva_flags, int orders);

This type for vma flags is totally incorrect. vm_flags_t. And that's going to change soon to an opaque type.

Also right now it's actually an unsigned long.

I really really do not like that we're providing extra, unexplained VMA flags for some reason. I may be missing something :) so happy to hear why this is necessary.

However in future we really shouldn't be passing something like this.

Also - now a third duplication of the same function pointer :) can we do better than this? At least typedef it.

mm/bpf_thp.c line 44

Quoted 6 lines
mm/bpf_thp.c new file +6 -0
+	int suggested_orders = orders;
+
+	/* No BPF program is attached */
+	if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+		      &transparent_hugepage_flags))
+		return suggested_orders;

This is atomic ofc, but are we concerned about races, or I guess you expect only the first attached bpf program to work with it I suppose.

mm/bpf_thp.c line 46

Quoted 2 lines
mm/bpf_thp.c new file +2 -0
+
+	rcu_read_lock();

Is this sufficient? Anything stopping the mm or VMA going away here?

mm/bpf_thp.c line 51

Quoted 5 lines
mm/bpf_thp.c new file +5 -0
+	bpf_suggested_order = rcu_dereference(bpf_thp.get_suggested_order);
+	if (!bpf_suggested_order)
+		goto out;
+
+	suggested_orders = bpf_suggested_order(mm, vma__nullable, vma_flags, tva_flags, orders);

OK so now it's suggested order_S but we're invoking suggested order :) whaaatt? :)

mm/bpf_thp.c line 53

Quoted 2 lines
mm/bpf_thp.c new file +2 -0
+	if (highest_order(suggested_orders) > highest_order(orders))
+		suggested_orders = orders;

Hmmm so the semantics are - whichever is the highest order wins?

I thought the idea was we'd hand control over to bpf if provided in effect?

Definitely worth going over these semantics in the cover letter (and do forgive me if you have and I've missed! :)

Quoted 53 lines
mm/bpf_thp.c new file +53 -0
+
+out:
+	rcu_read_unlock();
+	return suggested_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_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.get_suggested_order));
+	rcu_assign_pointer(bpf_thp.get_suggested_order, ops->get_suggested_order);
+	spin_unlock(&thp_ops_lock);
+	return 0;
+}
+

mm/bpf_thp.c line 116

Quoted 10 lines
mm/bpf_thp.c new file +10 -0
+static void bpf_thp_unreg(void *kdata, struct bpf_link *link)
+{
+	spin_lock(&thp_ops_lock);
+	clear_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED, &transparent_hugepage_flags);
+	WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
+	rcu_replace_pointer(bpf_thp.get_suggested_order, NULL, lockdep_is_held(&thp_ops_lock));
+	spin_unlock(&thp_ops_lock);
+
+	synchronize_rcu();
+}

I am a total beginner with BPF implementations so don't feel like I can say much intelligent about the above. But presumably fairly standard fare BPF-wise?

Will perhaps try to dig deeper on another iteration :) as intersting to me.

Quoted 6 lines
mm/bpf_thp.c new file +6 -0
+
+static int bpf_thp_update(void *kdata, void *old_kdata, struct bpf_link *link)
+{
+	struct bpf_thp_ops *ops = kdata;
+	struct bpf_thp_ops *old = old_kdata;
+	int ret = 0;

mm/bpf_thp.c line 132

Quoted 10 lines
mm/bpf_thp.c new file +10 -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;
+	}

OK so we gate things on this flag and it's global, got it.

I see this is a hook, and I guess RCU-all-the-things is what BPF does which makes tonnes of sense.

Quoted 21 lines
mm/bpf_thp.c new file +21 -0
+	WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
+	rcu_replace_pointer(bpf_thp.get_suggested_order, ops->get_suggested_order,
+			    lockdep_is_held(&thp_ops_lock));
+
+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->get_suggested_order) {
+		pr_err("bpf_thp: required ops isn't implemented\n");
+		return -EINVAL;
+	}
+	return 0;
+}

mm/bpf_thp.c line 163

Quoted 10 lines
mm/bpf_thp.c new file +10 -0
+
+static int suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+			   u64 vma_flags, enum tva_type vm_flags, int orders)
+{
+	return orders;
+}
+
+static struct bpf_thp_ops __bpf_thp_ops = {
+	.get_suggested_order = suggested_order,
+};

Can you explain to me what this stub stuff is for? This is more 'BPF impl 101' stuff sorry :)

Quoted 29 lines
mm/bpf_thp.c new file +23 -0
+
+static struct bpf_struct_ops bpf_bpf_thp_ops = {
+	.verifier_ops = &thp_bpf_verifier_ops,
+	.init = bpf_thp_init,
+	.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 = 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);
mm/huge_memory.c +0 -0
index d89992b65acc..bd8f8f34ab3c 100644
@@ -1349,6 +1349,16 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
 		return ret;

mm/huge_memory.c vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1359

Quoted 10 lines
mm/huge_memory.c +8 -0
 	khugepaged_enter_vma(vma, vma->vm_flags);
 
+	/*
+	 * This check must occur after khugepaged_enter_vma() because:
+	 * 1. We may permit THP allocation via khugepaged
+	 * 2. While simultaneously disallowing THP allocation
+	 *    during page fault handling
+	 */
+	if (get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_PAGEFAULT, BIT(PMD_ORDER)) !=
+				BIT(PMD_ORDER))

Hmmm so you return a bitmask of orders, but then you only allow this fault if the only order provided is PMD order? That seems strange. Can you explain?

mm/huge_memory.c vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1360

Quoted 1 line
mm/huge_memory.c +1 -0
+		return VM_FAULT_FALLBACK;

It'd be good to have a helper function for this like:

	if (!bpf_hook_allow_pmd_order(vma, tva_flag))
		return VM_FAULT_FALLBACK;

And implemented like maybe:

static bool bpf_hook_allow_pmd_order(struct vm_area_struct *vma, enum tva_type tva_flag)

{
	int orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags, tva_flag,
			BIT(PMD_ORDER));

	return orders & BIT(PMD_ORDER);
}

It's good the tva flag gives context though.

Quoted 9 lines
mm/huge_memory.c +1 -0
+
 	if (!(vmf->flags & FAULT_FLAG_WRITE) &&
 			!mm_forbids_zeropage(vma->vm_mm) &&
 			transparent_hugepage_use_zero_page()) {
mm/khugepaged.c +0 -0
index d3d4f116e14b..935583626db6 100644

mm/khugepaged.c void khugepaged_enter_vma(struct vm_area_struct *vma, · line 479

Quoted 7 lines
mm/khugepaged.c +3 -1
@@ -474,7 +474,9 @@ void khugepaged_enter_vma(struct vm_area_struct *vma,
 {
 	if (!mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
 	    hugepage_pmd_enabled()) {
-		if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
+		if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER) &&
+		    get_suggested_order(vma->vm_mm, vma, vm_flags, TVA_KHUGEPAGED,
+					BIT(PMD_ORDER)))

I don't know why we aren't working the bpf hook into thp_vma_allowable_order()?

Also a helper would work here.

Quoted 28 lines
mm/khugepaged.c +2 -0
 			__khugepaged_enter(vma->vm_mm);
 	}
 }
@@ -934,6 +936,8 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
 		return SCAN_ADDRESS_RANGE;
 	if (!thp_vma_allowable_order(vma, vma->vm_flags, type, PMD_ORDER))
 		return SCAN_VMA_CHECK;
+	if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, type, BIT(PMD_ORDER)))
+		return SCAN_VMA_CHECK;
 
 
 
mm/khugepaged.c +5 -0
 	/*
 	 * Anon VMA expected, the address may be unmapped then
 	 * remapped to file after khugepaged reaquired the mmap_lock.
@@ -1465,6 +1469,11 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
 		/* khugepaged_mm_lock actually not necessary for the below */
 		mm_slot_free(mm_slot_cache, mm_slot);
 		mmdrop(mm);
+	} else if (!get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER))) {
+		hash_del(&slot->hash);
+		list_del(&slot->mm_node);
+		mm_flags_clear(MMF_VM_HUGEPAGE, mm);
+		mm_slot_free(mm_slot_cache, mm_slot);
 	}
 }
 

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1551

Quoted 5 lines
mm/khugepaged.c +2 -0
@@ -1538,6 +1547,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 	if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
 		return SCAN_VMA_CHECK;
 
+	if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_FORCED_COLLAPSE,
+				 BIT(PMD_ORDER)))

Again, can we please not duplicate thp_vma_allowable_order() logic?

The THP code is horrible enough, but now we have to remember to also do the bpf check?

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

Quoted 12 lines
mm/khugepaged.c +5 -0
+		return SCAN_VMA_CHECK;
 	/* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
 	if (userfaultfd_wp(vma))
 		return SCAN_PTE_UFFD_WP;
@@ -2416,6 +2428,10 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 	 * the next mm on the list.
 	 */
 	vma = NULL;
+
+	/* If this mm is not suitable for the scan list, we should remove it. */
+	if (!get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER)))
+		goto breakouterloop_mmap_lock;

OK again I'm really not loving this NULL, 0, -1 stuff. What is this supposed to mean? The idea here is we have a hook for 'trying to determine THP order' and now it's overloaded it seems in multiple ways?

I may be missing context here.

I'm also a bit perplexed by the comment as to what is intended here.

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

Quoted 11 lines
mm/khugepaged.c +3 -1
 	if (unlikely(!mmap_read_trylock(mm)))
 		goto breakouterloop_mmap_lock;
 
@@ -2432,7 +2448,9 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 			progress++;
 			break;
 		}
-		if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER)) {
+		if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER) ||
+		    !get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_KHUGEPAGED,
+					 BIT(PMD_ORDER))) {

Same various comments from above.

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2793

Quoted 11 lines
mm/khugepaged.c +4 -0
 skip:
 			progress++;
 			continue;
@@ -2769,6 +2787,10 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
 	if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
 		return -EINVAL;
 
+	if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_FORCED_COLLAPSE,
+				 BIT(PMD_ORDER)))
+		return -EINVAL;
+

Same various comments from above.

Quoted 24 lines
mm/khugepaged.c +0 -0
 	cc = kmalloc(sizeof(*cc), GFP_KERNEL);
 	if (!cc)
 		return -ENOMEM;
mm/memory.c +1 -1
index d9de6c056179..0178857aa058 100644
@@ -4486,6 +4486,7 @@ static inline unsigned long thp_swap_suitable_orders(pgoff_t swp_offset,
 static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
+	int order, suggested_orders;
 	unsigned long orders;
 	struct folio *folio;
 	unsigned long addr;
@@ -4493,7 +4494,6 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 	spinlock_t *ptl;
 	pte_t *pte;
 	gfp_t gfp;
-	int order;
 
 	/*
 	 * If uffd is active for the vma we need per-page fault fidelity to

mm/memory.c static struct folio *alloc_swap_folio(struct vm_fault *vmf) · line 4517

Quoted 8 lines
mm/memory.c +5 -0
@@ -4510,13 +4510,18 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 	if (!zswap_never_enabled())
 		goto fallback;
 
+	suggested_orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags,
+					       TVA_PAGEFAULT,
+					       BIT(PMD_ORDER) - 1);
+	if (!suggested_orders)
+		goto fallback;

Wait, but below we have a bunch of fallbacks, now BPF overrides everything?

I know I'm repaeting myself :P but can we just please put this into thp_vma_allowable_orders(), it's massively gross to just duplicate this check _everywhere_ with subtle differences.

Quoted 26 lines
mm/memory.c +2 -2
 	entry = pte_to_swp_entry(vmf->orig_pte);
 	/*
 	 * 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,
-					  BIT(PMD_ORDER) - 1);
+					  suggested_orders);
 	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
 	orders = thp_swap_suitable_orders(swp_offset(entry),
 					  vmf->address, orders);
@@ -5044,12 +5049,12 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	int order, suggested_orders;
 	unsigned long orders;
 	struct folio *folio;
 	unsigned long addr;
 	pte_t *pte;
 	gfp_t gfp;
-	int order;
 
 	/*
 	 * If uffd is active for the vma we need per-page fault fidelity to

mm/memory.c static struct folio *alloc_anon_folio(struct vm_fault *vmf) · line 5070

Quoted 8 lines
mm/memory.c +5 -0
@@ -5058,13 +5063,18 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 	if (unlikely(userfaultfd_armed(vma)))
 		goto fallback;
 
+	suggested_orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags,
+					       TVA_PAGEFAULT,
+					       BIT(PMD_ORDER) - 1);
+	if (!suggested_orders)
+		goto fallback;

Same comment as above.

Quoted 14 lines
mm/memory.c +1 -2
 	/*
 	 * Get a list of all the (large) orders below PMD_ORDER that are enabled
 	 * for this vma. Then filter out the orders that can't be allocated over
 	 * the faulting address and still be fully contained in the vma.
 	 */
 	orders = thp_vma_allowable_orders(vma, vma->vm_flags, TVA_PAGEFAULT,
-					  BIT(PMD_ORDER) - 1);
+					  suggested_orders);
 	orders = thp_vma_suitable_orders(vma, vmf->address, orders);
 
 	if (!orders)
--

2.47.3

#6 lore raw

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

Quoted 35 lines · Lorenzo Stoakes
Quoted 26 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook get_suggested_order() [0], 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, or other paths.
- System memory pressure
(May require new BPF helpers to accurately assess memory pressure.)

Key Details:
- Only one BPF program can be attached at a time, but it can be updated dynamically to adjust the policy.
- Supports automatic mTHP order selection and per-workload THP policies.
- Only functional when THP is set to madise or always.

It requires CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION to enable. [1] This feature is unstable and may evolve in future kernel versions.

Quoted 6 lines
---
 include/linux/huge_mm.h    |  15 +++
 include/linux/khugepaged.h |  12 ++-
 mm/Kconfig                 |  12 +++
 mm/Makefile                |   1 +
 mm/bpf_thp.c               | 186 +++++++++++++++++++++++++++++++++++++
Quoted 1 line

Please add new files to MAINTAINERS as you add them.

will do it.

Quoted 13 lines
Quoted 13 lines
 mm/huge_memory.c           |  10 ++
 mm/khugepaged.c            |  26 +++++-
 mm/memory.c                |  18 +++-
 8 files changed, 273 insertions(+), 7 deletions(-)
 create mode 100644 mm/bpf_thp.c
include/linux/huge_mm.h +0 -0
index 1ac0d06fb3c1..f0c91d7bd267 100644
@@ -6,6 +6,8 @@
 
Quoted 7 lines

include/linux/huge_mm.h line 10

Quoted 4 lines
include/linux/huge_mm.h +2 -0
 #include <linux/fs.h> /* only for vma_is_dax() */
 #include <linux/kobject.h>
+#include <linux/pgtable.h>
+#include <linux/mm.h>

Hm this is a bit weird as mm.h includes huge_mm... I guess it will be handled by header defines but still.

Some refactoring is needed for these two header files, but we can handle it separately later.

Quoted 16 lines
Quoted 16 lines
include/linux/huge_mm.h +1 -0
 vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf);
 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
@@ -56,6 +58,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;
@@ -195,6 +198,18 @@ static inline bool hugepage_global_always(void)
                      (1<<TRANSPARENT_HUGEPAGE_FLAG);
 }
 
Quoted 6 lines

include/linux/huge_mm.h static inline bool hugepage_global_always(void) · line 203

Quoted 3 lines
include/linux/huge_mm.h +3 -0
+#ifdef CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION
+int get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                     u64 vma_flags, enum tva_type tva_flags, int orders);

Not a massive fan of this naming to be honest. I think it should explicitly reference bpf, e.g. bpf_hook_thp_get_order() or something.

will change it to bpf_hook_thp_get_orders().

Quoted 6 lines

Right now this is super unclear as to what it's for.

Also wrt vma_flags - this type is wrong :) it's vm_flags_t and going to change to a bitmap of unlimiiteeed size soon. So probs best not to pass around as value type either.

As replied in another thread. I will change it.

Quoted 5 lines

But unclear us to purpose as mentioned elsewhere.

And also get_suggested_order() should be get_suggested_orderS() no? As you seem later in the code to be referencing a bitfield?

Right, it should be bpf_hook_thp_get_orderS().

Quoted 2 lines

Also will mm ever != vma->vm_mm?

No it can't. It can be guaranteed by the caller.

Quoted 2 lines

Are we hacking this for the sake of overloading what this does?

The @vma is actually unneeded. I will remove it.

Quoted 4 lines

Also if we're returning a bitmask of orders which you seem to be (not sure I like that tbh - I feel like we shoudl simply provide one order but open for disucssion) - shouldn't it return an unsigned long?

We are indifferent to whether a single order or a bitmask is returned, as we only use order-0 and order-9. We have no use cases for middle-order pages, though this feature might be useful for other architectures or for some special use cases.

Quoted 19 lines
Quoted 19 lines
include/linux/huge_mm.h +9 -0
+#else
+static inline int
+get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                 u64 vma_flags, enum tva_type tva_flags, int orders)
+{
+     return orders;
+}
+#endif
+
 static inline int highest_order(unsigned long orders)
 {
      return fls_long(orders) - 1;
include/linux/khugepaged.h +0 -0
index eb1946a70cff..d81c1228a21f 100644
@@ -4,6 +4,8 @@
 
Quoted 7 lines

include/linux/khugepaged.h line 8

Quoted 4 lines
include/linux/khugepaged.h +2 -0
 #include <linux/mm.h>
 
+#include <linux/huge_mm.h>
+

Hm this is iffy too, There's probably a reason we didn't include this before, the headers can be so so fragile. Let's be cautious...

I will check.

Quoted 23 lines
Quoted 8 lines
include/linux/khugepaged.h +0 -0
 extern unsigned int khugepaged_max_ptes_none __read_mostly;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 extern struct attribute_group khugepaged_attr_group;
@@ -22,7 +24,15 @@ extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 
 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {

include/linux/khugepaged.h extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 35

Quoted 10 lines
include/linux/khugepaged.h +9 -1
-     if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm))
+     /*
+      * THP allocation policy can be dynamically modified via BPF. Even if a
+      * task was allowed to allocate THPs, BPF can decide whether its forked
+      * child can allocate THPs.
+      *
+      * The MMF_VM_HUGEPAGE flag will be cleared by khugepaged.
+      */
+     if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm) &&
+             get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER)))

Hmmm so there seems to be some kind of additional functionality you're providing here kinda quietly, which is to allow the exact same interface to determine whether we kick off khugepaged or not.

Quoted 4 lines

Don't love that, I think we should be hugely specific about that.

This bpf interface should literally be 'ok we're deciding what order we want'. It feels like a bit of a gross overloading?

This makes sense. I have no objection to reverting to returning a single order.

Quoted 12 lines
Quoted 12 lines
include/linux/khugepaged.h +0 -0
              __khugepaged_enter(mm);
 }
 
mm/Kconfig +0 -0
index 4108bcd96784..d10089e3f181 100644
@@ -924,6 +924,18 @@ config NO_PAGE_MAPCOUNT
 
        EXPERIMENTAL because the impact of some changes is still unclear.
 
Quoted 14 lines

mm/Kconfig config NO_PAGE_MAPCOUNT · line 937

Quoted 11 lines
mm/Kconfig +11 -0
+config EXPERIMENTAL_BPF_ORDER_SELECTION
+     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.

Thanks! This is important to document. Absolute nitty nit: can you capitalise 'WARNING'? Thanks!

will do it.

Quoted 18 lines
Quoted 18 lines
mm/Kconfig +1 -0
+
 endif # TRANSPARENT_HUGEPAGE
 
 # simple helper to make the code a bit easier to read
mm/Makefile +1 -0
index ef54aa615d9d..cb55d1509be1 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_EXPERIMENTAL_BPF_ORDER_SELECTION) += bpf_thp.o
 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
 obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
mm/bpf_thp.c +0 -0
Quoted 8 lines

mm/bpf_thp.c

Quoted 4 lines

new file mode 100644
index 000000000000..fbff3b1bb988

mm/bpf_thp.c new file +0 -0

As mentioned before, please update MAINTAINERS for new files. I went to great + painful lengths to get everything listed there so let's keep it that way please :P

will do it.

Quoted 25 lines
Quoted 25 lines
mm/bpf_thp.c new file +23 -0
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+struct bpf_thp_ops {
+     /**
+      * @get_suggested_order: Get the suggested THP orders for allocation
+      * @mm: mm_struct associated with the THP allocation
+      * @vma__nullable: vm_area_struct associated with the THP allocation (may be NULL)
+      *                 When NULL, the decision should be based on @mm (i.e., when
+      *                 triggered from an mm-scope hook rather than a VMA-specific
+      *                 context).
+      *                 Must belong to @mm (guaranteed by the caller).
+      * @vma_flags: use these vm_flags instead of @vma->vm_flags (0 if @vma is NULL)
+      * @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)
+      * @orders: Bitmask of requested THP orders for this allocation
+      *          - PMD-mapped allocation if PMD_ORDER is set
+      *          - mTHP allocation otherwise
+      *
+      * Rerurn: Bitmask of suggested THP orders for allocation. The highest
Quoted 8 lines

mm/bpf_thp.c line 28

Quoted 5 lines
mm/bpf_thp.c new file +5 -0
+      *         suggested order will not exceed the highest requested order
+      *         in @orders.
+      */
+     int (*get_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                                u64 vma_flags, enum tva_type tva_flags, int orders) __rcu;

I feel like we should be declaring this function pointer type somewhere else as we're now duplicating this in two places.

agreed, I have already done it to fix the spare warning.

Quoted 10 lines

mm/bpf_thp.c line 35

Quoted 8 lines
mm/bpf_thp.c new file +7 -0
+};
+
+static struct bpf_thp_ops bpf_thp;
+static DEFINE_SPINLOCK(thp_ops_lock);
+
+int get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                     u64 vma_flags, enum tva_type tva_flags, int orders)

surely tva_flag? As this is an enum value?

will change it to tva_type instead.

Quoted 8 lines

mm/bpf_thp.c line 38

Quoted 4 lines
mm/bpf_thp.c new file +3 -0
+{
+     int (*bpf_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                                u64 vma_flags, enum tva_type tva_flags, int orders);

This type for vma flags is totally incorrect. vm_flags_t. And that's going to change soon to an opaque type.

Quoted 7 lines

Also right now it's actually an unsigned long.

I really really do not like that we're providing extra, unexplained VMA flags for some reason. I may be missing something :) so happy to hear why this is necessary.

However in future we really shouldn't be passing something like this.

will change it as replied in another thread.

Quoted 11 lines

Also - now a third duplication of the same function pointer :) can we do better than this? At least typedef it.

mm/bpf_thp.c line 44

Quoted 6 lines
mm/bpf_thp.c new file +6 -0
+     int suggested_orders = orders;
+
+     /* No BPF program is attached */
+     if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+                   &transparent_hugepage_flags))
+             return suggested_orders;
Quoted 2 lines

This is atomic ofc, but are we concerned about races, or I guess you expect only the first attached bpf program to work with it I suppose.

It is against the race to unreg or update.

Quoted 5 lines

mm/bpf_thp.c line 46

Quoted 3 lines
mm/bpf_thp.c new file +2 -0
+
+     rcu_read_lock();

Is this sufficient? Anything stopping the mm or VMA going away here?

This RCU lock is not for protecting the mm or VMA structures themselves, but for protecting the update of the function pointer. Arbitrary access to pointers within the mm_struct or vm_area_struct is prohibited, as they are guarded by the BPF verifier.

Quoted 9 lines

mm/bpf_thp.c line 51

Quoted 6 lines
mm/bpf_thp.c new file +5 -0
+     bpf_suggested_order = rcu_dereference(bpf_thp.get_suggested_order);
+     if (!bpf_suggested_order)
+             goto out;
+
+     suggested_orders = bpf_suggested_order(mm, vma__nullable, vma_flags, tva_flags, orders);

OK so now it's suggested order_S but we're invoking suggested order :) whaaatt? :)

will change it.

Quoted 5 lines

mm/bpf_thp.c line 53

Quoted 3 lines
mm/bpf_thp.c new file +2 -0
+     if (highest_order(suggested_orders) > highest_order(orders))
+             suggested_orders = orders;

Hmmm so the semantics are - whichever is the highest order wins?

The maximum requested order is determined by the callsite. For example:
- 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.

Quoted 5 lines

I thought the idea was we'd hand control over to bpf if provided in effect?

Definitely worth going over these semantics in the cover letter (and do forgive me if you have and I've missed! :)

It has already in the cover letter:

* Return: Bitmask of suggested THP orders for allocation. The highest
* suggested order will not exceed the highest requested order
* in @orders.

Quoted 59 lines
Quoted 59 lines
mm/bpf_thp.c new file +58 -0
+
+out:
+     rcu_read_unlock();
+     return suggested_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_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.get_suggested_order));
+     rcu_assign_pointer(bpf_thp.get_suggested_order, ops->get_suggested_order);
+     spin_unlock(&thp_ops_lock);
+     return 0;
+}
+
+static void bpf_thp_unreg(void *kdata, struct bpf_link *link)
+{
+     spin_lock(&thp_ops_lock);
+     clear_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED, &transparent_hugepage_flags);
+     WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
Quoted 8 lines

mm/bpf_thp.c line 116

Quoted 5 lines
mm/bpf_thp.c new file +5 -0
+     rcu_replace_pointer(bpf_thp.get_suggested_order, NULL, lockdep_is_held(&thp_ops_lock));
+     spin_unlock(&thp_ops_lock);
+
+     synchronize_rcu();
+}

I am a total beginner with BPF implementations so don't feel like I can say much intelligent about the above. But presumably fairly standard fare BPF-wise?

This implementation is necessary to support BPF program updates.

Quoted 51 lines

Will perhaps try to dig deeper on another iteration :) as intersting to me.

Quoted 6 lines
mm/bpf_thp.c new file +6 -0
+
+static int bpf_thp_update(void *kdata, void *old_kdata, struct bpf_link *link)
+{
+     struct bpf_thp_ops *ops = kdata;
+     struct bpf_thp_ops *old = old_kdata;
+     int ret = 0;

mm/bpf_thp.c line 132

Quoted 10 lines
mm/bpf_thp.c new file +10 -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;
+     }

OK so we gate things on this flag and it's global, got it.

I see this is a hook, and I guess RCU-all-the-things is what BPF does which makes tonnes of sense.

Quoted 26 lines
mm/bpf_thp.c new file +26 -0
+     WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
+     rcu_replace_pointer(bpf_thp.get_suggested_order, ops->get_suggested_order,
+                         lockdep_is_held(&thp_ops_lock));
+
+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->get_suggested_order) {
+             pr_err("bpf_thp: required ops isn't implemented\n");
+             return -EINVAL;
+     }
+     return 0;
+}
+
+static int suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                        u64 vma_flags, enum tva_type vm_flags, int orders)
+{
+     return orders;
Quoted 8 lines

mm/bpf_thp.c line 163

Quoted 5 lines
mm/bpf_thp.c new file +5 -0
+}
+
+static struct bpf_thp_ops __bpf_thp_ops = {
+     .get_suggested_order = suggested_order,
+};

Can you explain to me what this stub stuff is for? This is more 'BPF impl 101' stuff sorry :)

It is a CFI stub. cfi_stubs in BPF struct_ops are secure intermediary functions that prevent the kernel from making direct, unsafe jumps to BPF code. A new attached BPF program will run via this stub.

Quoted 32 lines
Quoted 32 lines
mm/bpf_thp.c new file +23 -0
+
+static struct bpf_struct_ops bpf_bpf_thp_ops = {
+     .verifier_ops = &thp_bpf_verifier_ops,
+     .init = bpf_thp_init,
+     .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 = 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);
mm/huge_memory.c +0 -0
index d89992b65acc..bd8f8f34ab3c 100644
@@ -1349,6 +1349,16 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
              return ret;
      khugepaged_enter_vma(vma, vma->vm_flags);
 
Quoted 11 lines

mm/huge_memory.c vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1359

Quoted 8 lines
mm/huge_memory.c +8 -0
+     /*
+      * This check must occur after khugepaged_enter_vma() because:
+      * 1. We may permit THP allocation via khugepaged
+      * 2. While simultaneously disallowing THP allocation
+      *    during page fault handling
+      */
+     if (get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_PAGEFAULT, BIT(PMD_ORDER)) !=
+                             BIT(PMD_ORDER))

Hmmm so you return a bitmask of orders, but then you only allow this fault if the only order provided is PMD order? That seems strange. Can you explain?

This is in the do_huge_pmd_anonymous_page() that can only accept a PMD order, otherwise it might result in unexpected behavior.

Quoted 11 lines

mm/huge_memory.c vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1360

Quoted 2 lines
mm/huge_memory.c +1 -0
+             return VM_FAULT_FALLBACK;

It'd be good to have a helper function for this like:

if (!bpf_hook_allow_pmd_order(vma, tva_flag))
return VM_FAULT_FALLBACK;

And implemented like maybe:

static bool bpf_hook_allow_pmd_order(struct vm_area_struct *vma, enum tva_type tva_flag)

Quoted 8 lines
{
        int orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags, tva_flag,
                        BIT(PMD_ORDER));

        return orders & BIT(PMD_ORDER);
}

It's good the tva flag gives context though.

Thanks for the suggestion.
will change it.

Quoted 7 lines
Quoted 6 lines
mm/huge_memory.c +1 -0
+
      if (!(vmf->flags & FAULT_FLAG_WRITE) &&
                      !mm_forbids_zeropage(vma->vm_mm) &&
                      transparent_hugepage_use_zero_page()) {
mm/khugepaged.c +0 -0
Quoted 13 lines

mm/khugepaged.c void khugepaged_enter_vma(struct vm_area_struct *vma, · line 479

Quoted 11 lines

index d3d4f116e14b..935583626db6 100644

mm/khugepaged.c +3 -1
@@ -474,7 +474,9 @@ void khugepaged_enter_vma(struct vm_area_struct *vma,
 {
      if (!mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
          hugepage_pmd_enabled()) {
-             if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
+             if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER) &&
+                 get_suggested_order(vma->vm_mm, vma, vm_flags, TVA_KHUGEPAGED,
+                                     BIT(PMD_ORDER)))

I don't know why we aren't working the bpf hook into thp_vma_allowable_order()?

Actually it can be added into thp_vma_allowable_order(). I will change it.

Quoted 34 lines

Also a helper would work here.

Quoted 31 lines
mm/khugepaged.c +2 -0
                      __khugepaged_enter(vma->vm_mm);
      }
 }
@@ -934,6 +936,8 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
              return SCAN_ADDRESS_RANGE;
      if (!thp_vma_allowable_order(vma, vma->vm_flags, type, PMD_ORDER))
              return SCAN_VMA_CHECK;
+     if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, type, BIT(PMD_ORDER)))
+             return SCAN_VMA_CHECK;
 
 
 

/*
* Anon VMA expected, the address may be unmapped then
* remapped to file after khugepaged reaquired the mmap_lock.

mm/khugepaged.c +5 -0
@@ -1465,6 +1469,11 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
              /* khugepaged_mm_lock actually not necessary for the below */
              mm_slot_free(mm_slot_cache, mm_slot);
              mmdrop(mm);
+     } else if (!get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER))) {
+             hash_del(&slot->hash);
+             list_del(&slot->mm_node);
+             mm_flags_clear(MMF_VM_HUGEPAGE, mm);
+             mm_slot_free(mm_slot_cache, mm_slot);
      }
 }
 
@@ -1538,6 +1547,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
      if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
              return SCAN_VMA_CHECK;
 
Quoted 7 lines

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1551

Quoted 2 lines
mm/khugepaged.c +2 -0
+     if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_FORCED_COLLAPSE,
+                              BIT(PMD_ORDER)))

Again, can we please not duplicate thp_vma_allowable_order() logic?

The THP code is horrible enough, but now we have to remember to also do the bpf check?

makes sense.

Quoted 18 lines

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

Quoted 13 lines
mm/khugepaged.c +5 -0
+             return SCAN_VMA_CHECK;
      /* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
      if (userfaultfd_wp(vma))
              return SCAN_PTE_UFFD_WP;
@@ -2416,6 +2428,10 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
       * the next mm on the list.
       */
      vma = NULL;
+
+     /* If this mm is not suitable for the scan list, we should remove it. */
+     if (!get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER)))
+             goto breakouterloop_mmap_lock;

OK again I'm really not loving this NULL, 0, -1 stuff. What is this supposed to mean? The idea here is we have a hook for 'trying to determine THP order' and now it's overloaded it seems in multiple ways?

Quoted 3 lines

I may be missing context here.

I'm also a bit perplexed by the comment as to what is intended here.

Using a BPF-based approach for THP adjustment allows us to dynamically enable or disable THP for running applications without causing any disruption. This capability is particularly valuable in production environments. The logic here is designed to achieve exactly that.

Quoted 4 lines
Quoted 4 lines
mm/khugepaged.c +0 -0
      if (unlikely(!mmap_read_trylock(mm)))
              goto breakouterloop_mmap_lock;
 
Quoted 10 lines

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

Quoted 8 lines
mm/khugepaged.c +3 -1
@@ -2432,7 +2448,9 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
                      progress++;
                      break;
              }
-             if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER)) {
+             if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER) ||
+                 !get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_KHUGEPAGED,
+                                      BIT(PMD_ORDER))) {

Same various comments from above.

will change it.

Quoted 8 lines
Quoted 8 lines
mm/khugepaged.c +0 -0
 skip:
                      progress++;
                      continue;
@@ -2769,6 +2787,10 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
      if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
              return -EINVAL;
 
Quoted 6 lines

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2793

Quoted 4 lines
mm/khugepaged.c +4 -0
+     if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_FORCED_COLLAPSE,
+                              BIT(PMD_ORDER)))
+             return -EINVAL;
+

Same various comments from above.

will change it.

Quoted 28 lines
Quoted 28 lines
mm/khugepaged.c +0 -0
      cc = kmalloc(sizeof(*cc), GFP_KERNEL);
      if (!cc)
              return -ENOMEM;
mm/memory.c +1 -1
index d9de6c056179..0178857aa058 100644
@@ -4486,6 +4486,7 @@ static inline unsigned long thp_swap_suitable_orders(pgoff_t swp_offset,
 static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 {
      struct vm_area_struct *vma = vmf->vma;
+     int order, suggested_orders;
      unsigned long orders;
      struct folio *folio;
      unsigned long addr;
@@ -4493,7 +4494,6 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
      spinlock_t *ptl;
      pte_t *pte;
      gfp_t gfp;
-     int order;
 
      /*
       * If uffd is active for the vma we need per-page fault fidelity to
@@ -4510,13 +4510,18 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
      if (!zswap_never_enabled())
              goto fallback;
 
Quoted 7 lines

mm/memory.c static struct folio *alloc_swap_folio(struct vm_fault *vmf) · line 4517

Quoted 5 lines
mm/memory.c +5 -0
+     suggested_orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags,
+                                            TVA_PAGEFAULT,
+                                            BIT(PMD_ORDER) - 1);
+     if (!suggested_orders)
+             goto fallback;

Wait, but below we have a bunch of fallbacks, now BPF overrides everything?

When allocating high-order pages is not feasible, such as during periods of high memory pressure, the system should immediately fall back to using 4 KB pages.

Quoted 4 lines

I know I'm repaeting myself :P but can we just please put this into thp_vma_allowable_orders(), it's massively gross to just duplicate this check _everywhere_ with subtle differences.

will change it.

Quoted 30 lines
Quoted 30 lines
mm/memory.c +2 -2
      entry = pte_to_swp_entry(vmf->orig_pte);
      /*
       * 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,
-                                       BIT(PMD_ORDER) - 1);
+                                       suggested_orders);
      orders = thp_vma_suitable_orders(vma, vmf->address, orders);
      orders = thp_swap_suitable_orders(swp_offset(entry),
                                        vmf->address, orders);
@@ -5044,12 +5049,12 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 {
      struct vm_area_struct *vma = vmf->vma;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+     int order, suggested_orders;
      unsigned long orders;
      struct folio *folio;
      unsigned long addr;
      pte_t *pte;
      gfp_t gfp;
-     int order;
 
      /*
       * If uffd is active for the vma we need per-page fault fidelity to
@@ -5058,13 +5063,18 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
      if (unlikely(userfaultfd_armed(vma)))
              goto fallback;
 
Quoted 7 lines

mm/memory.c static struct folio *alloc_swap_folio(struct vm_fault *vmf) · line 4517

Quoted 5 lines
mm/memory.c +5 -0
+     suggested_orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags,
+                                            TVA_PAGEFAULT,
+                                            BIT(PMD_ORDER) - 1);
+     if (!suggested_orders)
+             goto fallback;

Same comment as above.

will change it.

Thanks a lot for your comments.

--
Regards

Yafang

#7 lore raw

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

Quoted 81 lines · Yafang Shao
Quoted 35 lines · Lorenzo Stoakes
Quoted 26 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook get_suggested_order() [0], 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, or other paths.
- System memory pressure
(May require new BPF helpers to accurately assess memory pressure.)

Key Details:
- Only one BPF program can be attached at a time, but it can be updated dynamically to adjust the policy.
- Supports automatic mTHP order selection and per-workload THP policies.
- Only functional when THP is set to madise or always.

It requires CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION to enable. [1] This feature is unstable and may evolve in future kernel versions.

Quoted 6 lines
---
 include/linux/huge_mm.h    |  15 +++
 include/linux/khugepaged.h |  12 ++-
 mm/Kconfig                 |  12 +++
 mm/Makefile                |   1 +
 mm/bpf_thp.c               | 186 +++++++++++++++++++++++++++++++++++++
Quoted 1 line

Please add new files to MAINTAINERS as you add them.

will do it.

Quoted 13 lines
Quoted 13 lines
 mm/huge_memory.c           |  10 ++
 mm/khugepaged.c            |  26 +++++-
 mm/memory.c                |  18 +++-
 8 files changed, 273 insertions(+), 7 deletions(-)
 create mode 100644 mm/bpf_thp.c
include/linux/huge_mm.h +0 -0
index 1ac0d06fb3c1..f0c91d7bd267 100644
@@ -6,6 +6,8 @@
 
Quoted 7 lines

include/linux/huge_mm.h line 10

Quoted 4 lines
include/linux/huge_mm.h +2 -0
 #include <linux/fs.h> /* only for vma_is_dax() */
 #include <linux/kobject.h>
+#include <linux/pgtable.h>
+#include <linux/mm.h>

Hm this is a bit weird as mm.h includes huge_mm... I guess it will be handled by header defines but still.

Some refactoring is needed for these two header files, but we can handle it separately later.

Quoted 16 lines
Quoted 16 lines
include/linux/huge_mm.h +1 -0
 vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf);
 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
@@ -56,6 +58,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;
@@ -195,6 +198,18 @@ static inline bool hugepage_global_always(void)
                      (1<<TRANSPARENT_HUGEPAGE_FLAG);
 }
 
Quoted 8 lines
Quoted 6 lines

include/linux/huge_mm.h static inline bool hugepage_global_always(void) · line 203

Quoted 3 lines
include/linux/huge_mm.h +3 -0
+#ifdef CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION
+int get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                     u64 vma_flags, enum tva_type tva_flags, int orders);

Not a massive fan of this naming to be honest. I think it should explicitly reference bpf, e.g. bpf_hook_thp_get_order() or something.

will change it to bpf_hook_thp_get_orders().

Thanks!

Quoted 9 lines
Quoted 7 lines

Right now this is super unclear as to what it's for.

Also wrt vma_flags - this type is wrong :) it's vm_flags_t and going to change to a bitmap of unlimiiteeed size soon. So probs best not to pass around as value type either.

As replied in another thread. I will change it.

Thanks. Will check the other thread.

Quoted 8 lines
Quoted 6 lines

But unclear us to purpose as mentioned elsewhere.

And also get_suggested_order() should be get_suggested_orderS() no? As you seem later in the code to be referencing a bitfield?

Right, it should be bpf_hook_thp_get_orderS().

Thanks!

Quoted 5 lines
Quoted 3 lines

Also will mm ever != vma->vm_mm?

No it can't. It can be guaranteed by the caller.

In this case we don't need to pass mm separately then right?

Quoted 5 lines
Quoted 3 lines

Are we hacking this for the sake of overloading what this does?

The @vma is actually unneeded. I will remove it.

Ah OK.

I am still a little concerned about passing around a value reference to the VMA flags though, esp as this type can + will change in future (not sure what that means for BPF).

We may go to e.g. a 128 bit bitmap there etc.

Quoted 10 lines
Quoted 5 lines

Also if we're returning a bitmask of orders which you seem to be (not sure I like that tbh - I feel like we shoudl simply provide one order but open for disucssion) - shouldn't it return an unsigned long?

We are indifferent to whether a single order or a bitmask is returned, as we only use order-0 and order-9. We have no use cases for middle-order pages, though this feature might be useful for other architectures or for some special use cases.

Well surely we want to potentially specify a mTHP under certain circumstances no?

In any case I feel it's worth making any bitfield a system word size.

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
include/linux/huge_mm.h +9 -0
+#else
+static inline int
+get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                 u64 vma_flags, enum tva_type tva_flags, int orders)
+{
+     return orders;
+}
+#endif
+
 static inline int highest_order(unsigned long orders)
 {
      return fls_long(orders) - 1;
include/linux/khugepaged.h +0 -0
index eb1946a70cff..d81c1228a21f 100644
@@ -4,6 +4,8 @@
 
 #include <linux/mm.h>
 
Quoted 7 lines
Quoted 5 lines

include/linux/khugepaged.h line 8

Quoted 2 lines
include/linux/khugepaged.h +2 -0
+#include <linux/huge_mm.h>
+

Hm this is iffy too, There's probably a reason we didn't include this before, the headers can be so so fragile. Let's be cautious...

I will check.

Thanks!

Quoted 24 lines
Quoted 24 lines
Quoted 9 lines
include/linux/khugepaged.h +0 -0
 extern unsigned int khugepaged_max_ptes_none __read_mostly;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 extern struct attribute_group khugepaged_attr_group;
@@ -22,7 +24,15 @@ extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 
 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {

include/linux/khugepaged.h extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 35

Quoted 10 lines
include/linux/khugepaged.h +9 -1
-     if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm))
+     /*
+      * THP allocation policy can be dynamically modified via BPF. Even if a
+      * task was allowed to allocate THPs, BPF can decide whether its forked
+      * child can allocate THPs.
+      *
+      * The MMF_VM_HUGEPAGE flag will be cleared by khugepaged.
+      */
+     if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm) &&
+             get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER)))

Hmmm so there seems to be some kind of additional functionality you're providing here kinda quietly, which is to allow the exact same interface to determine whether we kick off khugepaged or not.

Quoted 6 lines
Quoted 4 lines

Don't love that, I think we should be hugely specific about that.

This bpf interface should literally be 'ok we're deciding what order we want'. It feels like a bit of a gross overloading?

This makes sense. I have no objection to reverting to returning a single order.

OK but key point here is - we're now determining if a forked child can _not_ allocate THPs using this function.

To me this should be a separate function rather than some _weird_ usage of this same function.

And generally at this point I think we should just drop this bit of code honestly.

Quoted 28 lines
Quoted 13 lines
Quoted 13 lines
include/linux/khugepaged.h +0 -0
              __khugepaged_enter(mm);
 }
 
mm/Kconfig +0 -0
index 4108bcd96784..d10089e3f181 100644
@@ -924,6 +924,18 @@ config NO_PAGE_MAPCOUNT
 
        EXPERIMENTAL because the impact of some changes is still unclear.
 
Quoted 14 lines

mm/Kconfig config NO_PAGE_MAPCOUNT · line 937

Quoted 11 lines
mm/Kconfig +11 -0
+config EXPERIMENTAL_BPF_ORDER_SELECTION
+     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.

Thanks! This is important to document. Absolute nitty nit: can you capitalise 'WARNING'? Thanks!

Quoted 1 line

will do it.

Thanks!

Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
mm/Kconfig +1 -0
+
 endif # TRANSPARENT_HUGEPAGE
 
 # simple helper to make the code a bit easier to read
mm/Makefile +1 -0
index ef54aa615d9d..cb55d1509be1 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_EXPERIMENTAL_BPF_ORDER_SELECTION) += bpf_thp.o
 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
 obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
mm/bpf_thp.c +0 -0
Quoted 8 lines

mm/bpf_thp.c

Quoted 4 lines

new file mode 100644
index 000000000000..fbff3b1bb988

mm/bpf_thp.c new file +0 -0

As mentioned before, please update MAINTAINERS for new files. I went to great + painful lengths to get everything listed there so let's keep it that way please :P

Quoted 1 line

will do it.

Thanks!

Quoted 35 lines
Quoted 26 lines
Quoted 26 lines
mm/bpf_thp.c new file +23 -0
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+struct bpf_thp_ops {
+     /**
+      * @get_suggested_order: Get the suggested THP orders for allocation
+      * @mm: mm_struct associated with the THP allocation
+      * @vma__nullable: vm_area_struct associated with the THP allocation (may be NULL)
+      *                 When NULL, the decision should be based on @mm (i.e., when
+      *                 triggered from an mm-scope hook rather than a VMA-specific
+      *                 context).
+      *                 Must belong to @mm (guaranteed by the caller).
+      * @vma_flags: use these vm_flags instead of @vma->vm_flags (0 if @vma is NULL)
+      * @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)
+      * @orders: Bitmask of requested THP orders for this allocation
+      *          - PMD-mapped allocation if PMD_ORDER is set
+      *          - mTHP allocation otherwise
+      *
+      * Rerurn: Bitmask of suggested THP orders for allocation. The highest
Quoted 8 lines

mm/bpf_thp.c line 28

Quoted 5 lines
mm/bpf_thp.c new file +5 -0
+      *         suggested order will not exceed the highest requested order
+      *         in @orders.
+      */
+     int (*get_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                                u64 vma_flags, enum tva_type tva_flags, int orders) __rcu;

I feel like we should be declaring this function pointer type somewhere else as we're now duplicating this in two places.

Quoted 1 line

agreed, I have already done it to fix the spare warning.

Thanks!

Quoted 12 lines
Quoted 11 lines

mm/bpf_thp.c line 35

Quoted 9 lines
mm/bpf_thp.c new file +7 -0
+};
+
+static struct bpf_thp_ops bpf_thp;
+static DEFINE_SPINLOCK(thp_ops_lock);
+
+int get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                     u64 vma_flags, enum tva_type tva_flags, int orders)

surely tva_flag? As this is an enum value?

Quoted 1 line

will change it to tva_type instead.

Thanks!

Quoted 11 lines
Quoted 11 lines

mm/bpf_thp.c line 38

Quoted 5 lines
mm/bpf_thp.c new file +3 -0
+{
+     int (*bpf_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                                u64 vma_flags, enum tva_type tva_flags, int orders);

This type for vma flags is totally incorrect. vm_flags_t. And that's going to change soon to an opaque type.

Also right now it's actually an unsigned long.

Quoted 7 lines
Quoted 5 lines

I really really do not like that we're providing extra, unexplained VMA flags for some reason. I may be missing something :) so happy to hear why this is necessary.

However in future we really shouldn't be passing something like this.

will change it as replied in another thread.

Thanks!

Quoted 12 lines
Quoted 12 lines

Also - now a third duplication of the same function pointer :) can we do better than this? At least typedef it.

Quoted 6 lines
mm/bpf_thp.c new file +6 -0
+     int suggested_orders = orders;
+
+     /* No BPF program is attached */
+     if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+                   &transparent_hugepage_flags))
+             return suggested_orders;
Quoted 4 lines
Quoted 2 lines

This is atomic ofc, but are we concerned about races, or I guess you expect only the first attached bpf program to work with it I suppose.

It is against the race to unreg or update.

OK cool, it does make sense overall.

Quoted 19 lines
Quoted 6 lines

mm/bpf_thp.c line 46

Quoted 4 lines
mm/bpf_thp.c new file +2 -0
+
+     rcu_read_lock();

Is this sufficient? Anything stopping the mm or VMA going away here?

This RCU lock is not for protecting the mm or VMA structures themselves, but for protecting the update of the function pointer. Arbitrary access to pointers within the mm_struct or vm_area_struct is prohibited, as they are guarded by the BPF verifier.

Quoted 7 lines
Quoted 6 lines
mm/bpf_thp.c new file +5 -0
+     bpf_suggested_order = rcu_dereference(bpf_thp.get_suggested_order);
+     if (!bpf_suggested_order)
+             goto out;
+
+     suggested_orders = bpf_suggested_order(mm, vma__nullable, vma_flags, tva_flags, orders);
Quoted 4 lines
Quoted 2 lines

OK so now it's suggested order_S but we're invoking suggested order :) whaaatt? :)

will change it.

Thanks!

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
mm/bpf_thp.c new file +2 -0
+     if (highest_order(suggested_orders) > highest_order(orders))
+             suggested_orders = orders;
Quoted 8 lines
Quoted 1 line

Hmmm so the semantics are - whichever is the highest order wins?

The maximum requested order is determined by the callsite. For example:
- 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.

OK, please document this in a comment here.

Quoted 4 lines
Quoted 4 lines

I thought the idea was we'd hand control over to bpf if provided in effect?

Quoted 8 lines
Quoted 2 lines

Definitely worth going over these semantics in the cover letter (and do forgive me if you have and I've missed! :)

It has already in the cover letter:

* Return: Bitmask of suggested THP orders for allocation. The highest
* suggested order will not exceed the highest requested order
* in @orders.

OK cool thanks, a comment here would be useful also.

Quoted 70 lines
Quoted 61 lines
Quoted 61 lines
mm/bpf_thp.c new file +58 -0
+
+out:
+     rcu_read_unlock();
+     return suggested_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_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.get_suggested_order));
+     rcu_assign_pointer(bpf_thp.get_suggested_order, ops->get_suggested_order);
+     spin_unlock(&thp_ops_lock);
+     return 0;
+}
+
+static void bpf_thp_unreg(void *kdata, struct bpf_link *link)
+{
+     spin_lock(&thp_ops_lock);
+     clear_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED, &transparent_hugepage_flags);
+     WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
Quoted 8 lines

mm/bpf_thp.c line 116

Quoted 5 lines
mm/bpf_thp.c new file +5 -0
+     rcu_replace_pointer(bpf_thp.get_suggested_order, NULL, lockdep_is_held(&thp_ops_lock));
+     spin_unlock(&thp_ops_lock);
+
+     synchronize_rcu();
+}

I am a total beginner with BPF implementations so don't feel like I can say much intelligent about the above. But presumably fairly standard fare BPF-wise?

Quoted 1 line

This implementation is necessary to support BPF program updates.

Ack.

Quoted 61 lines
Quoted 52 lines

Will perhaps try to dig deeper on another iteration :) as intersting to me.

Quoted 6 lines
mm/bpf_thp.c new file +6 -0
+
+static int bpf_thp_update(void *kdata, void *old_kdata, struct bpf_link *link)
+{
+     struct bpf_thp_ops *ops = kdata;
+     struct bpf_thp_ops *old = old_kdata;
+     int ret = 0;

mm/bpf_thp.c line 132

Quoted 10 lines
mm/bpf_thp.c new file +10 -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;
+     }

OK so we gate things on this flag and it's global, got it.

I see this is a hook, and I guess RCU-all-the-things is what BPF does which makes tonnes of sense.

Quoted 26 lines
mm/bpf_thp.c new file +26 -0
+     WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
+     rcu_replace_pointer(bpf_thp.get_suggested_order, ops->get_suggested_order,
+                         lockdep_is_held(&thp_ops_lock));
+
+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->get_suggested_order) {
+             pr_err("bpf_thp: required ops isn't implemented\n");
+             return -EINVAL;
+     }
+     return 0;
+}
+
+static int suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                        u64 vma_flags, enum tva_type vm_flags, int orders)
+{
+     return orders;
Quoted 8 lines

mm/bpf_thp.c line 163

Quoted 5 lines
mm/bpf_thp.c new file +5 -0
+}
+
+static struct bpf_thp_ops __bpf_thp_ops = {
+     .get_suggested_order = suggested_order,
+};

Can you explain to me what this stub stuff is for? This is more 'BPF impl 101' stuff sorry :)

Quoted 3 lines

It is a CFI stub. cfi_stubs in BPF struct_ops are secure intermediary functions that prevent the kernel from making direct, unsafe jumps to BPF code. A new attached BPF program will run via this stub.

Ack.

Quoted 45 lines
Quoted 33 lines
Quoted 33 lines
mm/bpf_thp.c new file +23 -0
+
+static struct bpf_struct_ops bpf_bpf_thp_ops = {
+     .verifier_ops = &thp_bpf_verifier_ops,
+     .init = bpf_thp_init,
+     .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 = 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);
mm/huge_memory.c +0 -0
index d89992b65acc..bd8f8f34ab3c 100644
@@ -1349,6 +1349,16 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
              return ret;
      khugepaged_enter_vma(vma, vma->vm_flags);
 
Quoted 11 lines

mm/huge_memory.c vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1359

Quoted 8 lines
mm/huge_memory.c +8 -0
+     /*
+      * This check must occur after khugepaged_enter_vma() because:
+      * 1. We may permit THP allocation via khugepaged
+      * 2. While simultaneously disallowing THP allocation
+      *    during page fault handling
+      */
+     if (get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_PAGEFAULT, BIT(PMD_ORDER)) !=
+                             BIT(PMD_ORDER))

Hmmm so you return a bitmask of orders, but then you only allow this fault if the only order provided is PMD order? That seems strange. Can you explain?

Quoted 2 lines

This is in the do_huge_pmd_anonymous_page() that can only accept a PMD order, otherwise it might result in unexpected behavior.

OK please document this in the comment.

Quoted 16 lines
Quoted 16 lines

mm/huge_memory.c vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1360

Quoted 3 lines
mm/huge_memory.c +1 -0
+             return VM_FAULT_FALLBACK;

It'd be good to have a helper function for this like:

if (!bpf_hook_allow_pmd_order(vma, tva_flag))
return VM_FAULT_FALLBACK;

And implemented like maybe:

static bool bpf_hook_allow_pmd_order(struct vm_area_struct *vma, enum tva_type tva_flag)

{
        int orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags, tva_flag,
                        BIT(PMD_ORDER));
Quoted 7 lines
Quoted 4 lines
        return orders & BIT(PMD_ORDER);
}

It's good the tva flag gives context though.

Thanks for the suggestion.
will change it.

Thanks!

Quoted 22 lines
Quoted 8 lines
Quoted 7 lines
mm/huge_memory.c +1 -0
+
      if (!(vmf->flags & FAULT_FLAG_WRITE) &&
                      !mm_forbids_zeropage(vma->vm_mm) &&
                      transparent_hugepage_use_zero_page()) {
mm/khugepaged.c +0 -0
Quoted 13 lines

mm/khugepaged.c void khugepaged_enter_vma(struct vm_area_struct *vma, · line 479

Quoted 11 lines

index d3d4f116e14b..935583626db6 100644

mm/khugepaged.c +3 -1
@@ -474,7 +474,9 @@ void khugepaged_enter_vma(struct vm_area_struct *vma,
 {
      if (!mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) &&
          hugepage_pmd_enabled()) {
-             if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER))
+             if (thp_vma_allowable_order(vma, vm_flags, TVA_KHUGEPAGED, PMD_ORDER) &&
+                 get_suggested_order(vma->vm_mm, vma, vm_flags, TVA_KHUGEPAGED,
+                                     BIT(PMD_ORDER)))

I don't know why we aren't working the bpf hook into thp_vma_allowable_order()?

Quoted 1 line

Actually it can be added into thp_vma_allowable_order(). I will change it.

Thanks!

Quoted 98 lines
Quoted 35 lines

Also a helper would work here.

Quoted 31 lines
mm/khugepaged.c +2 -0
                      __khugepaged_enter(vma->vm_mm);
      }
 }
@@ -934,6 +936,8 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
              return SCAN_ADDRESS_RANGE;
      if (!thp_vma_allowable_order(vma, vma->vm_flags, type, PMD_ORDER))
              return SCAN_VMA_CHECK;
+     if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, type, BIT(PMD_ORDER)))
+             return SCAN_VMA_CHECK;
 
 
 

/*
* Anon VMA expected, the address may be unmapped then
* remapped to file after khugepaged reaquired the mmap_lock.

mm/khugepaged.c +5 -0
@@ -1465,6 +1469,11 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
              /* khugepaged_mm_lock actually not necessary for the below */
              mm_slot_free(mm_slot_cache, mm_slot);
              mmdrop(mm);
+     } else if (!get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER))) {
+             hash_del(&slot->hash);
+             list_del(&slot->mm_node);
+             mm_flags_clear(MMF_VM_HUGEPAGE, mm);
+             mm_slot_free(mm_slot_cache, mm_slot);
      }
 }
 
@@ -1538,6 +1547,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
      if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
              return SCAN_VMA_CHECK;
 
Quoted 7 lines

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1551

Quoted 2 lines
mm/khugepaged.c +2 -0
+     if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_FORCED_COLLAPSE,
+                              BIT(PMD_ORDER)))

Again, can we please not duplicate thp_vma_allowable_order() logic?

The THP code is horrible enough, but now we have to remember to also do the bpf check?

makes sense.

Quoted 18 lines

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

Quoted 13 lines
mm/khugepaged.c +5 -0
+             return SCAN_VMA_CHECK;
      /* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
      if (userfaultfd_wp(vma))
              return SCAN_PTE_UFFD_WP;
@@ -2416,6 +2428,10 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
       * the next mm on the list.
       */
      vma = NULL;
+
+     /* If this mm is not suitable for the scan list, we should remove it. */
+     if (!get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER)))
+             goto breakouterloop_mmap_lock;

OK again I'm really not loving this NULL, 0, -1 stuff. What is this supposed to mean? The idea here is we have a hook for 'trying to determine THP order' and now it's overloaded it seems in multiple ways?

Quoted 3 lines

I may be missing context here.

I'm also a bit perplexed by the comment as to what is intended here.

Using a BPF-based approach for THP adjustment allows us to dynamically enable or disable THP for running applications without causing any disruption. This capability is particularly valuable in production environments. The logic here is designed to achieve exactly that.

Quoted 4 lines
Quoted 4 lines
mm/khugepaged.c +0 -0
      if (unlikely(!mmap_read_trylock(mm)))
              goto breakouterloop_mmap_lock;
 
Quoted 10 lines

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

Quoted 8 lines
mm/khugepaged.c +3 -1
@@ -2432,7 +2448,9 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
                      progress++;
                      break;
              }
-             if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER)) {
+             if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_KHUGEPAGED, PMD_ORDER) ||
+                 !get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_KHUGEPAGED,
+                                      BIT(PMD_ORDER))) {

Same various comments from above.

will change it.

Quoted 8 lines
Quoted 8 lines
mm/khugepaged.c +0 -0
 skip:
                      progress++;
                      continue;
@@ -2769,6 +2787,10 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
      if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
              return -EINVAL;
 
Quoted 43 lines
Quoted 6 lines

mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 2793

Quoted 4 lines
mm/khugepaged.c +4 -0
+     if (!get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_FORCED_COLLAPSE,
+                              BIT(PMD_ORDER)))
+             return -EINVAL;
+

Same various comments from above.

will change it.

Quoted 34 lines
Quoted 33 lines
mm/khugepaged.c +0 -0
      cc = kmalloc(sizeof(*cc), GFP_KERNEL);
      if (!cc)
              return -ENOMEM;
mm/memory.c +6 -1
index d9de6c056179..0178857aa058 100644
@@ -4486,6 +4486,7 @@ static inline unsigned long thp_swap_suitable_orders(pgoff_t swp_offset,
 static struct folio *alloc_swap_folio(struct vm_fault *vmf)
 {
      struct vm_area_struct *vma = vmf->vma;
+     int order, suggested_orders;
      unsigned long orders;
      struct folio *folio;
      unsigned long addr;
@@ -4493,7 +4494,6 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
      spinlock_t *ptl;
      pte_t *pte;
      gfp_t gfp;
-     int order;
 
      /*
       * If uffd is active for the vma we need per-page fault fidelity to
@@ -4510,13 +4510,18 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
      if (!zswap_never_enabled())
              goto fallback;
 
+     suggested_orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags,
+                                            TVA_PAGEFAULT,
+                                            BIT(PMD_ORDER) - 1);
+     if (!suggested_orders)
+             goto fallback;

(Thanks for all above! :)

Quoted 5 lines
Quoted 1 line

Wait, but below we have a bunch of fallbacks, now BPF overrides everything?

When allocating high-order pages is not feasible, such as during periods of high memory pressure, the system should immediately fall back to using 4 KB pages.

OK makes sense.

Quoted 7 lines
Quoted 5 lines

I know I'm repaeting myself :P but can we just please put this into thp_vma_allowable_orders(), it's massively gross to just duplicate this check _everywhere_ with subtle differences.

will change it.

Thanks

Quoted 39 lines
Quoted 31 lines
Quoted 31 lines
mm/memory.c +2 -2
      entry = pte_to_swp_entry(vmf->orig_pte);
      /*
       * 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,
-                                       BIT(PMD_ORDER) - 1);
+                                       suggested_orders);
      orders = thp_vma_suitable_orders(vma, vmf->address, orders);
      orders = thp_swap_suitable_orders(swp_offset(entry),
                                        vmf->address, orders);
@@ -5044,12 +5049,12 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 {
      struct vm_area_struct *vma = vmf->vma;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+     int order, suggested_orders;
      unsigned long orders;
      struct folio *folio;
      unsigned long addr;
      pte_t *pte;
      gfp_t gfp;
-     int order;
 
      /*
       * If uffd is active for the vma we need per-page fault fidelity to
@@ -5058,13 +5063,18 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
      if (unlikely(userfaultfd_armed(vma)))
              goto fallback;
 
Quoted 7 lines

mm/memory.c static struct folio *alloc_swap_folio(struct vm_fault *vmf) · line 4517

Quoted 5 lines
mm/memory.c +5 -0
+     suggested_orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags,
+                                            TVA_PAGEFAULT,
+                                            BIT(PMD_ORDER) - 1);
+     if (!suggested_orders)
+             goto fallback;

Same comment as above.

Quoted 1 line

will change it.

Thanks!

Quoted 3 lines

Thanks a lot for your comments.

No problem, thanks for the series!

I am generally excited about exploring this, so once we figure out details be good to see where this can go!

Quoted 6 lines

--
Regards

Yafang

Cheers, Lorenzo

#8 lore raw

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

Quoted 119 lines · Lorenzo Stoakes
Quoted 81 lines · Yafang Shao
Quoted 35 lines · Lorenzo Stoakes
Quoted 26 lines · Yafang Shao

This patch introduces a new BPF struct_ops called bpf_thp_ops for dynamic THP tuning. It includes a hook get_suggested_order() [0], 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, or other paths.
- System memory pressure
(May require new BPF helpers to accurately assess memory pressure.)

Key Details:
- Only one BPF program can be attached at a time, but it can be updated dynamically to adjust the policy.
- Supports automatic mTHP order selection and per-workload THP policies.
- Only functional when THP is set to madise or always.

It requires CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION to enable. [1] This feature is unstable and may evolve in future kernel versions.

Quoted 6 lines
---
 include/linux/huge_mm.h    |  15 +++
 include/linux/khugepaged.h |  12 ++-
 mm/Kconfig                 |  12 +++
 mm/Makefile                |   1 +
 mm/bpf_thp.c               | 186 +++++++++++++++++++++++++++++++++++++
Quoted 1 line

Please add new files to MAINTAINERS as you add them.

will do it.

Quoted 13 lines
Quoted 13 lines
 mm/huge_memory.c           |  10 ++
 mm/khugepaged.c            |  26 +++++-
 mm/memory.c                |  18 +++-
 8 files changed, 273 insertions(+), 7 deletions(-)
 create mode 100644 mm/bpf_thp.c
include/linux/huge_mm.h +0 -0
index 1ac0d06fb3c1..f0c91d7bd267 100644
@@ -6,6 +6,8 @@
 
Quoted 7 lines

include/linux/huge_mm.h line 10

Quoted 4 lines
include/linux/huge_mm.h +2 -0
 #include <linux/fs.h> /* only for vma_is_dax() */
 #include <linux/kobject.h>
+#include <linux/pgtable.h>
+#include <linux/mm.h>

Hm this is a bit weird as mm.h includes huge_mm... I guess it will be handled by header defines but still.

Some refactoring is needed for these two header files, but we can handle it separately later.

Quoted 16 lines
Quoted 16 lines
include/linux/huge_mm.h +1 -0
 vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf);
 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
@@ -56,6 +58,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;
@@ -195,6 +198,18 @@ static inline bool hugepage_global_always(void)
                      (1<<TRANSPARENT_HUGEPAGE_FLAG);
 }
 
Quoted 8 lines
Quoted 6 lines

include/linux/huge_mm.h static inline bool hugepage_global_always(void) · line 203

Quoted 3 lines
include/linux/huge_mm.h +3 -0
+#ifdef CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION
+int get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                     u64 vma_flags, enum tva_type tva_flags, int orders);

Not a massive fan of this naming to be honest. I think it should explicitly reference bpf, e.g. bpf_hook_thp_get_order() or something.

will change it to bpf_hook_thp_get_orders().

Thanks!

Quoted 9 lines
Quoted 7 lines

Right now this is super unclear as to what it's for.

Also wrt vma_flags - this type is wrong :) it's vm_flags_t and going to change to a bitmap of unlimiiteeed size soon. So probs best not to pass around as value type either.

As replied in another thread. I will change it.

Thanks. Will check the other thread.

Quoted 8 lines
Quoted 6 lines

But unclear us to purpose as mentioned elsewhere.

And also get_suggested_order() should be get_suggested_orderS() no? As you seem later in the code to be referencing a bitfield?

Right, it should be bpf_hook_thp_get_orderS().

Thanks!

Quoted 2 lines
Quoted 2 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Also will mm ever != vma->vm_mm?

No it can't. It can be guaranteed by the caller.

In this case we don't need to pass mm separately then right?

Right, we need to pass either @mm or @vma. However, there are cases where vma information is not available at certain call sites, such as in khugepaged. In those cases, we need to pass @mm instead.

Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

Are we hacking this for the sake of overloading what this does?

The @vma is actually unneeded. I will remove it.

Quoted 7 lines

Ah OK.

I am still a little concerned about passing around a value reference to the VMA flags though, esp as this type can + will change in future (not sure what that means for BPF).

We may go to e.g. a 128 bit bitmap there etc.

As mentioned in another thread, we only need to determine whether the flag is VM_HUGEPAGE or VM_NOHUGEPAGE, so it can be simplified.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Also if we're returning a bitmask of orders which you seem to be (not sure I like that tbh - I feel like we shoudl simply provide one order but open for disucssion) - shouldn't it return an unsigned long?

Quoted 7 lines
Quoted 4 lines

We are indifferent to whether a single order or a bitmask is returned, as we only use order-0 and order-9. We have no use cases for middle-order pages, though this feature might be useful for other architectures or for some special use cases.

Well surely we want to potentially specify a mTHP under certain circumstances no?

Perhaps there are use cases, but I haven’t found any use cases for this in our production environment. On the other hand, I can clearly see a risk that it could lead to more costly high-order allocations.

Quoted 64 lines

In any case I feel it's worth making any bitfield a system word size.

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
include/linux/huge_mm.h +9 -0
+#else
+static inline int
+get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                 u64 vma_flags, enum tva_type tva_flags, int orders)
+{
+     return orders;
+}
+#endif
+
 static inline int highest_order(unsigned long orders)
 {
      return fls_long(orders) - 1;
include/linux/khugepaged.h +0 -0
index eb1946a70cff..d81c1228a21f 100644
@@ -4,6 +4,8 @@
 
 #include <linux/mm.h>
 
Quoted 7 lines
Quoted 5 lines

include/linux/khugepaged.h line 8

Quoted 2 lines
include/linux/khugepaged.h +2 -0
+#include <linux/huge_mm.h>
+

Hm this is iffy too, There's probably a reason we didn't include this before, the headers can be so so fragile. Let's be cautious...

I will check.

Thanks!

Quoted 29 lines
Quoted 28 lines
Quoted 9 lines
include/linux/khugepaged.h +0 -0
 extern unsigned int khugepaged_max_ptes_none __read_mostly;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 extern struct attribute_group khugepaged_attr_group;
@@ -22,7 +24,15 @@ extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 
 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {

include/linux/khugepaged.h extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 35

Quoted 10 lines
include/linux/khugepaged.h +9 -1
-     if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm))
+     /*
+      * THP allocation policy can be dynamically modified via BPF. Even if a
+      * task was allowed to allocate THPs, BPF can decide whether its forked
+      * child can allocate THPs.
+      *
+      * The MMF_VM_HUGEPAGE flag will be cleared by khugepaged.
+      */
+     if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm) &&
+             get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER)))

Hmmm so there seems to be some kind of additional functionality you're providing here kinda quietly, which is to allow the exact same interface to determine whether we kick off khugepaged or not.

Don't love that, I think we should be hugely specific about that.

This bpf interface should literally be 'ok we're deciding what order we want'. It feels like a bit of a gross overloading?

Quoted 7 lines
Quoted 1 line

This makes sense. I have no objection to reverting to returning a single order.

OK but key point here is - we're now determining if a forked child can _not_ allocate THPs using this function.

To me this should be a separate function rather than some _weird_ usage of this same function.

Perhaps a separate function is better.

Quoted 3 lines

And generally at this point I think we should just drop this bit of code honestly.

MMF_VM_HUGEPAGE is set when the THP mode is "always" or "madvise". If it’s set, any forked child processes will inherit this flag. It is only cleared when the mm_struct is destroyed (please correct me if I’m wrong).

However, when you switch the THP mode to "never", tasks that still have MMF_VM_HUGEPAGE remain on the khugepaged scan list. This isn’t an issue under the current global mode because khugepaged doesn’t run when THP is set to "never".

The problem arises when we move from a global mode to a per-task mode. In that case, khugepaged may end up doing unnecessary work. For example, if the THP mode is "always", but some tasks are not allowed to allocate THP while still having MMF_VM_HUGEPAGE set, khugepaged will continue scanning them unnecessarily.

To avoid this, we should prevent setting this flag for child processes if they are not allowed to allocate THP in the first place. This way, khugepaged won’t waste cycles scanning them. While an alternative approach would be to set the flag at fork and later clear it for khugepaged, it’s clearly more efficient to avoid setting it from the start.

Quoted 193 lines
Quoted 29 lines
Quoted 14 lines
Quoted 14 lines
include/linux/khugepaged.h +0 -0
              __khugepaged_enter(mm);
 }
 
mm/Kconfig +0 -0
index 4108bcd96784..d10089e3f181 100644
@@ -924,6 +924,18 @@ config NO_PAGE_MAPCOUNT
 
        EXPERIMENTAL because the impact of some changes is still unclear.
 
Quoted 14 lines

mm/Kconfig config NO_PAGE_MAPCOUNT · line 937

Quoted 11 lines
mm/Kconfig +11 -0
+config EXPERIMENTAL_BPF_ORDER_SELECTION
+     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.

Thanks! This is important to document. Absolute nitty nit: can you capitalise 'WARNING'? Thanks!

Quoted 1 line

will do it.

Thanks!

Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
mm/Kconfig +1 -0
+
 endif # TRANSPARENT_HUGEPAGE
 
 # simple helper to make the code a bit easier to read
mm/Makefile +1 -0
index ef54aa615d9d..cb55d1509be1 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_EXPERIMENTAL_BPF_ORDER_SELECTION) += bpf_thp.o
 obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
 obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
 obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
mm/bpf_thp.c +0 -0
Quoted 8 lines

mm/bpf_thp.c

Quoted 4 lines

new file mode 100644
index 000000000000..fbff3b1bb988

mm/bpf_thp.c new file +0 -0

As mentioned before, please update MAINTAINERS for new files. I went to great + painful lengths to get everything listed there so let's keep it that way please :P

Quoted 1 line

will do it.

Thanks!

Quoted 35 lines
Quoted 26 lines
Quoted 26 lines
mm/bpf_thp.c new file +23 -0
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/huge_mm.h>
+#include <linux/khugepaged.h>
+
+struct bpf_thp_ops {
+     /**
+      * @get_suggested_order: Get the suggested THP orders for allocation
+      * @mm: mm_struct associated with the THP allocation
+      * @vma__nullable: vm_area_struct associated with the THP allocation (may be NULL)
+      *                 When NULL, the decision should be based on @mm (i.e., when
+      *                 triggered from an mm-scope hook rather than a VMA-specific
+      *                 context).
+      *                 Must belong to @mm (guaranteed by the caller).
+      * @vma_flags: use these vm_flags instead of @vma->vm_flags (0 if @vma is NULL)
+      * @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)
+      * @orders: Bitmask of requested THP orders for this allocation
+      *          - PMD-mapped allocation if PMD_ORDER is set
+      *          - mTHP allocation otherwise
+      *
+      * Rerurn: Bitmask of suggested THP orders for allocation. The highest
Quoted 8 lines

mm/bpf_thp.c line 28

Quoted 5 lines
mm/bpf_thp.c new file +5 -0
+      *         suggested order will not exceed the highest requested order
+      *         in @orders.
+      */
+     int (*get_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                                u64 vma_flags, enum tva_type tva_flags, int orders) __rcu;

I feel like we should be declaring this function pointer type somewhere else as we're now duplicating this in two places.

Quoted 1 line

agreed, I have already done it to fix the spare warning.

Thanks!

Quoted 12 lines
Quoted 11 lines

mm/bpf_thp.c line 35

Quoted 9 lines
mm/bpf_thp.c new file +7 -0
+};
+
+static struct bpf_thp_ops bpf_thp;
+static DEFINE_SPINLOCK(thp_ops_lock);
+
+int get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                     u64 vma_flags, enum tva_type tva_flags, int orders)

surely tva_flag? As this is an enum value?

Quoted 1 line

will change it to tva_type instead.

Thanks!

Quoted 11 lines
Quoted 11 lines

mm/bpf_thp.c line 38

Quoted 5 lines
mm/bpf_thp.c new file +3 -0
+{
+     int (*bpf_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                                u64 vma_flags, enum tva_type tva_flags, int orders);

This type for vma flags is totally incorrect. vm_flags_t. And that's going to change soon to an opaque type.

Also right now it's actually an unsigned long.

Quoted 7 lines
Quoted 5 lines

I really really do not like that we're providing extra, unexplained VMA flags for some reason. I may be missing something :) so happy to hear why this is necessary.

However in future we really shouldn't be passing something like this.

will change it as replied in another thread.

Thanks!

Quoted 12 lines
Quoted 12 lines

Also - now a third duplication of the same function pointer :) can we do better than this? At least typedef it.

Quoted 6 lines
mm/bpf_thp.c new file +6 -0
+     int suggested_orders = orders;
+
+     /* No BPF program is attached */
+     if (!test_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED,
+                   &transparent_hugepage_flags))
+             return suggested_orders;
Quoted 4 lines
Quoted 2 lines

This is atomic ofc, but are we concerned about races, or I guess you expect only the first attached bpf program to work with it I suppose.

It is against the race to unreg or update.

OK cool, it does make sense overall.

Quoted 19 lines
Quoted 6 lines

mm/bpf_thp.c line 46

Quoted 4 lines
mm/bpf_thp.c new file +2 -0
+
+     rcu_read_lock();

Is this sufficient? Anything stopping the mm or VMA going away here?

This RCU lock is not for protecting the mm or VMA structures themselves, but for protecting the update of the function pointer. Arbitrary access to pointers within the mm_struct or vm_area_struct is prohibited, as they are guarded by the BPF verifier.

Quoted 7 lines
Quoted 6 lines
mm/bpf_thp.c new file +5 -0
+     bpf_suggested_order = rcu_dereference(bpf_thp.get_suggested_order);
+     if (!bpf_suggested_order)
+             goto out;
+
+     suggested_orders = bpf_suggested_order(mm, vma__nullable, vma_flags, tva_flags, orders);
Quoted 4 lines
Quoted 2 lines

OK so now it's suggested order_S but we're invoking suggested order :) whaaatt? :)

will change it.

Thanks!

Quoted 7 lines
Quoted 6 lines

mm/bpf_thp.c line 53

Quoted 4 lines
mm/bpf_thp.c new file +2 -0
+     if (highest_order(suggested_orders) > highest_order(orders))
+             suggested_orders = orders;

Hmmm so the semantics are - whichever is the highest order wins?

Quoted 8 lines
Quoted 6 lines

The maximum requested order is determined by the callsite. For example:
- 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.

OK, please document this in a comment here.

will doc it.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

I thought the idea was we'd hand control over to bpf if provided in effect?

Definitely worth going over these semantics in the cover letter (and do forgive me if you have and I've missed! :)

Quoted 7 lines
Quoted 5 lines

It has already in the cover letter:

* Return: Bitmask of suggested THP orders for allocation. The highest
* suggested order will not exceed the highest requested order
* in @orders.

OK cool thanks, a comment here would be useful also.

will add it.

Quoted 187 lines
Quoted 71 lines
Quoted 62 lines
Quoted 62 lines
mm/bpf_thp.c new file +58 -0
+
+out:
+     rcu_read_unlock();
+     return suggested_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_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.get_suggested_order));
+     rcu_assign_pointer(bpf_thp.get_suggested_order, ops->get_suggested_order);
+     spin_unlock(&thp_ops_lock);
+     return 0;
+}
+
+static void bpf_thp_unreg(void *kdata, struct bpf_link *link)
+{
+     spin_lock(&thp_ops_lock);
+     clear_bit(TRANSPARENT_HUGEPAGE_BPF_ATTACHED, &transparent_hugepage_flags);
+     WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
Quoted 8 lines

mm/bpf_thp.c line 116

Quoted 5 lines
mm/bpf_thp.c new file +5 -0
+     rcu_replace_pointer(bpf_thp.get_suggested_order, NULL, lockdep_is_held(&thp_ops_lock));
+     spin_unlock(&thp_ops_lock);
+
+     synchronize_rcu();
+}

I am a total beginner with BPF implementations so don't feel like I can say much intelligent about the above. But presumably fairly standard fare BPF-wise?

Quoted 1 line

This implementation is necessary to support BPF program updates.

Ack.

Quoted 61 lines
Quoted 52 lines

Will perhaps try to dig deeper on another iteration :) as intersting to me.

Quoted 6 lines
mm/bpf_thp.c new file +6 -0
+
+static int bpf_thp_update(void *kdata, void *old_kdata, struct bpf_link *link)
+{
+     struct bpf_thp_ops *ops = kdata;
+     struct bpf_thp_ops *old = old_kdata;
+     int ret = 0;

mm/bpf_thp.c line 132

Quoted 10 lines
mm/bpf_thp.c new file +10 -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;
+     }

OK so we gate things on this flag and it's global, got it.

I see this is a hook, and I guess RCU-all-the-things is what BPF does which makes tonnes of sense.

Quoted 26 lines
mm/bpf_thp.c new file +26 -0
+     WARN_ON_ONCE(!rcu_access_pointer(bpf_thp.get_suggested_order));
+     rcu_replace_pointer(bpf_thp.get_suggested_order, ops->get_suggested_order,
+                         lockdep_is_held(&thp_ops_lock));
+
+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->get_suggested_order) {
+             pr_err("bpf_thp: required ops isn't implemented\n");
+             return -EINVAL;
+     }
+     return 0;
+}
+
+static int suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                        u64 vma_flags, enum tva_type vm_flags, int orders)
+{
+     return orders;
Quoted 8 lines

mm/bpf_thp.c line 163

Quoted 5 lines
mm/bpf_thp.c new file +5 -0
+}
+
+static struct bpf_thp_ops __bpf_thp_ops = {
+     .get_suggested_order = suggested_order,
+};

Can you explain to me what this stub stuff is for? This is more 'BPF impl 101' stuff sorry :)

Quoted 3 lines

It is a CFI stub. cfi_stubs in BPF struct_ops are secure intermediary functions that prevent the kernel from making direct, unsafe jumps to BPF code. A new attached BPF program will run via this stub.

Ack.

Quoted 45 lines
Quoted 44 lines
Quoted 31 lines
mm/bpf_thp.c new file +23 -0
+
+static struct bpf_struct_ops bpf_bpf_thp_ops = {
+     .verifier_ops = &thp_bpf_verifier_ops,
+     .init = bpf_thp_init,
+     .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 = 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);
mm/huge_memory.c +0 -0
index d89992b65acc..bd8f8f34ab3c 100644
@@ -1349,6 +1349,16 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
              return ret;

mm/huge_memory.c vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1359

Quoted 10 lines
mm/huge_memory.c +8 -0
      khugepaged_enter_vma(vma, vma->vm_flags);
 
+     /*
+      * This check must occur after khugepaged_enter_vma() because:
+      * 1. We may permit THP allocation via khugepaged
+      * 2. While simultaneously disallowing THP allocation
+      *    during page fault handling
+      */
+     if (get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_PAGEFAULT, BIT(PMD_ORDER)) !=
+                             BIT(PMD_ORDER))

Hmmm so you return a bitmask of orders, but then you only allow this fault if the only order provided is PMD order? That seems strange. Can you explain?

Quoted 4 lines
Quoted 2 lines

This is in the do_huge_pmd_anonymous_page() that can only accept a PMD order, otherwise it might result in unexpected behavior.

OK please document this in the comment.

will doc it.

--
Regards
Yafang

#9 lore raw

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

Quoted 6 lines · Yafang Shao
Quoted 4 lines · Lorenzo Stoakes
Quoted 2 lines · Yafang Shao
Quoted 1 line

Also will mm ever != vma->vm_mm?

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

No it can't. It can be guaranteed by the caller.

In this case we don't need to pass mm separately then right?

Right, we need to pass either @mm or @vma. However, there are cases where vma information is not available at certain call sites, such as in khugepaged. In those cases, we need to pass @mm instead.

Yeah... this is weird to me though, are you checking in _general_ what khugepaged should use, or otherwise surely it's per-VMA?

Otherwise this bpf hook seems ill-suited for that, and we should have a separate one for khugepaged surely?

I also hate that we're passing mm _just because of this one edge case_, otherwise always passing vma->vm_mm, it's a confusing interface.

Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

Are we hacking this for the sake of overloading what this does?

The @vma is actually unneeded. I will remove it.

Ah OK.

Quoted 8 lines
Quoted 5 lines

I am still a little concerned about passing around a value reference to the VMA flags though, esp as this type can + will change in future (not sure what that means for BPF).

We may go to e.g. a 128 bit bitmap there etc.

As mentioned in another thread, we only need to determine whether the flag is VM_HUGEPAGE or VM_NOHUGEPAGE, so it can be simplified.

OK cool thanks. Maybe missed.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 8 lines

Also if we're returning a bitmask of orders which you seem to be (not sure I like that tbh - I feel like we shoudl simply provide one order but open for disucssion) - shouldn't it return an unsigned long?

We are indifferent to whether a single order or a bitmask is returned, as we only use order-0 and order-9. We have no use cases for middle-order pages, though this feature might be useful for other architectures or for some special use cases.

Quoted 6 lines
Quoted 2 lines

Well surely we want to potentially specify a mTHP under certain circumstances no?

Perhaps there are use cases, but I haven’t found any use cases for this in our production environment. On the other hand, I can clearly see a risk that it could lead to more costly high-order allocations.

So why are we returning a bitmap then? Seems like we should just return a single order in this case... I think you say below that you are open to this?

Quoted 3 lines
Quoted 3 lines

In any case I feel it's worth making any bitfield a system word size.

Also :>)

If we do move to returning a single order, should be unsigned int.

Quoted 64 lines
Quoted 64 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
include/linux/huge_mm.h +9 -0
+#else
+static inline int
+get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                 u64 vma_flags, enum tva_type tva_flags, int orders)
+{
+     return orders;
+}
+#endif
+
 static inline int highest_order(unsigned long orders)
 {
      return fls_long(orders) - 1;
include/linux/khugepaged.h +0 -0
index eb1946a70cff..d81c1228a21f 100644
@@ -4,6 +4,8 @@
 
 #include <linux/mm.h>
 
Quoted 7 lines
Quoted 5 lines

include/linux/khugepaged.h line 8

Quoted 2 lines
include/linux/khugepaged.h +2 -0
+#include <linux/huge_mm.h>
+

Hm this is iffy too, There's probably a reason we didn't include this before, the headers can be so so fragile. Let's be cautious...

I will check.

Thanks!

Quoted 30 lines
Quoted 24 lines
Quoted 9 lines
include/linux/khugepaged.h +0 -0
 extern unsigned int khugepaged_max_ptes_none __read_mostly;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 extern struct attribute_group khugepaged_attr_group;
@@ -22,7 +24,15 @@ extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 
 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {

include/linux/khugepaged.h extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 35

Quoted 10 lines
include/linux/khugepaged.h +9 -1
-     if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm))
+     /*
+      * THP allocation policy can be dynamically modified via BPF. Even if a
+      * task was allowed to allocate THPs, BPF can decide whether its forked
+      * child can allocate THPs.
+      *
+      * The MMF_VM_HUGEPAGE flag will be cleared by khugepaged.
+      */
+     if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm) &&
+             get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER)))

Hmmm so there seems to be some kind of additional functionality you're providing here kinda quietly, which is to allow the exact same interface to determine whether we kick off khugepaged or not.

Quoted 4 lines

Don't love that, I think we should be hugely specific about that.

This bpf interface should literally be 'ok we're deciding what order we want'. It feels like a bit of a gross overloading?

This makes sense. I have no objection to reverting to returning a single order.

Quoted 7 lines
Quoted 5 lines

OK but key point here is - we're now determining if a forked child can _not_ allocate THPs using this function.

To me this should be a separate function rather than some _weird_ usage of this same function.

Perhaps a separate function is better.

Thanks!

Quoted 9 lines
Quoted 4 lines

And generally at this point I think we should just drop this bit of code honestly.

MMF_VM_HUGEPAGE is set when the THP mode is "always" or "madvise". If it’s set, any forked child processes will inherit this flag. It is only cleared when the mm_struct is destroyed (please correct me if I’m wrong).

__mmput()
-> khugepaged_exit()
-> (if MMF_VM_HUGEPAGE set) __khugepaged_exit()
-> Clear flag once mm fully done with (afaict), dropping associated mm refcount.

^--- this does seem to be accurate indeed.

Quoted 11 lines

However, when you switch the THP mode to "never", tasks that still have MMF_VM_HUGEPAGE remain on the khugepaged scan list. This isn’t an issue under the current global mode because khugepaged doesn’t run when THP is set to "never".

The problem arises when we move from a global mode to a per-task mode. In that case, khugepaged may end up doing unnecessary work. For example, if the THP mode is "always", but some tasks are not allowed to allocate THP while still having MMF_VM_HUGEPAGE set, khugepaged will continue scanning them unnecessarily.

But this can change right?

I really don't like the idea _at all_ of overriding this hook to do things other than what it says it does.

It's 'set which order to use' except when it's this case then it's 'will we do any work'.

This should be a separate callback or we should drop this and live with the possible additional work.

Quoted 7 lines

To avoid this, we should prevent setting this flag for child processes if they are not allowed to allocate THP in the first place. This way, khugepaged won’t waste cycles scanning them. While an alternative approach would be to set the flag at fork and later clear it for khugepaged, it’s clearly more efficient to avoid setting it from the start.

We also obviously should have a comment with all this context here.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 7 lines

include/linux/khugepaged.h extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 37

Quoted 5 lines
include/linux/khugepaged.h +2 -0
+     if (highest_order(suggested_orders) > highest_order(orders))
+             suggested_orders = orders;

Hmmm so the semantics are - whichever is the highest order wins?

The maximum requested order is determined by the callsite. For example:
- PMD-mapped THP uses PMD_ORDER
- mTHP uses (PMD_ORDER - 1)

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

We must respect this upper bound to avoid undefined behavior. So the highest suggested order can't exceed the highest requested order.

OK, please document this in a comment here.

will doc it.

Thanks!

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines

I thought the idea was we'd hand control over to bpf if provided in effect?

Definitely worth going over these semantics in the cover letter (and do forgive me if you have and I've missed! :)

It has already in the cover letter:

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

* Return: Bitmask of suggested THP orders for allocation. The highest
* suggested order will not exceed the highest requested order
* in @orders.

OK cool thanks, a comment here would be useful also.

will add it.

Thanks!

Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 43 lines
Quoted 30 lines
include/linux/khugepaged.h +23 -0
+
+static struct bpf_struct_ops bpf_bpf_thp_ops = {
+     .verifier_ops = &thp_bpf_verifier_ops,
+     .init = bpf_thp_init,
+     .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 = 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);
mm/huge_memory.c +0 -0
index d89992b65acc..bd8f8f34ab3c 100644
@@ -1349,6 +1349,16 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
              return ret;

mm/huge_memory.c vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1359

Quoted 10 lines
mm/huge_memory.c +8 -0
      khugepaged_enter_vma(vma, vma->vm_flags);
 
+     /*
+      * This check must occur after khugepaged_enter_vma() because:
+      * 1. We may permit THP allocation via khugepaged
+      * 2. While simultaneously disallowing THP allocation
+      *    during page fault handling
+      */
+     if (get_suggested_order(vma->vm_mm, vma, vma->vm_flags, TVA_PAGEFAULT, BIT(PMD_ORDER)) !=
+                             BIT(PMD_ORDER))

Hmmm so you return a bitmask of orders, but then you only allow this fault if the only order provided is PMD order? That seems strange. Can you explain?

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

This is in the do_huge_pmd_anonymous_page() that can only accept a PMD order, otherwise it might result in unexpected behavior.

OK please document this in the comment.

will doc it.

Thanks!

Quoted 5 lines

--
Regards
Yafang

Cheers, Lorenzo

#10 lore raw

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

Quoted 16 lines · Lorenzo Stoakes
Quoted 6 lines · Yafang Shao
Quoted 4 lines · Lorenzo Stoakes
Quoted 2 lines · Yafang Shao
Quoted 1 line

Also will mm ever != vma->vm_mm?

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

No it can't. It can be guaranteed by the caller.

In this case we don't need to pass mm separately then right?

Right, we need to pass either @mm or @vma. However, there are cases where vma information is not available at certain call sites, such as in khugepaged. In those cases, we need to pass @mm instead.

Quoted 8 lines

Yeah... this is weird to me though, are you checking in _general_ what khugepaged should use, or otherwise surely it's per-VMA?

Otherwise this bpf hook seems ill-suited for that, and we should have a separate one for khugepaged surely?

I also hate that we're passing mm _just because of this one edge case_, otherwise always passing vma->vm_mm, it's a confusing interface.

make sense.
I'll give some thought to how we can better handle this edge case.

Quoted 39 lines
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
Quoted 6 lines

Are we hacking this for the sake of overloading what this does?

The @vma is actually unneeded. I will remove it.

Ah OK.

Quoted 8 lines
Quoted 5 lines

I am still a little concerned about passing around a value reference to the VMA flags though, esp as this type can + will change in future (not sure what that means for BPF).

We may go to e.g. a 128 bit bitmap there etc.

As mentioned in another thread, we only need to determine whether the flag is VM_HUGEPAGE or VM_NOHUGEPAGE, so it can be simplified.

OK cool thanks. Maybe missed.

Quoted 17 lines
Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Quoted 8 lines
Quoted 3 lines

Also if we're returning a bitmask of orders which you seem to be (not sure I like that tbh - I feel like we shoudl simply provide one order but open for disucssion) - shouldn't it return an unsigned long?

We are indifferent to whether a single order or a bitmask is returned, as we only use order-0 and order-9. We have no use cases for middle-order pages, though this feature might be useful for other architectures or for some special use cases.

Well surely we want to potentially specify a mTHP under certain circumstances no?

Quoted 7 lines
Quoted 3 lines

Perhaps there are use cases, but I haven’t found any use cases for this in our production environment. On the other hand, I can clearly see a risk that it could lead to more costly high-order allocations.

So why are we returning a bitmap then? Seems like we should just return a single order in this case... I think you say below that you are open to this?

will return a single order in the next version.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

In any case I feel it's worth making any bitfield a system word size.

Also :>)

If we do move to returning a single order, should be unsigned int.

sure

Quoted 85 lines
Quoted 65 lines
Quoted 65 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
(quoted diff) +9 -0
+#else
+static inline int
+get_suggested_order(struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+                 u64 vma_flags, enum tva_type tva_flags, int orders)
+{
+     return orders;
+}
+#endif
+
 static inline int highest_order(unsigned long orders)
 {
      return fls_long(orders) - 1;
include/linux/khugepaged.h +0 -0
index eb1946a70cff..d81c1228a21f 100644
@@ -4,6 +4,8 @@
 
 #include <linux/mm.h>
 
Quoted 7 lines
Quoted 5 lines

include/linux/khugepaged.h line 8

Quoted 2 lines
include/linux/khugepaged.h +2 -0
+#include <linux/huge_mm.h>
+

Hm this is iffy too, There's probably a reason we didn't include this before, the headers can be so so fragile. Let's be cautious...

I will check.

Thanks!

Quoted 30 lines
Quoted 24 lines
Quoted 9 lines
include/linux/khugepaged.h +0 -0
 extern unsigned int khugepaged_max_ptes_none __read_mostly;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 extern struct attribute_group khugepaged_attr_group;
@@ -22,7 +24,15 @@ extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 
 static inline void khugepaged_fork(struct mm_struct *mm, struct mm_struct *oldmm)
 {

include/linux/khugepaged.h extern int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 35

Quoted 10 lines
include/linux/khugepaged.h +9 -1
-     if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm))
+     /*
+      * THP allocation policy can be dynamically modified via BPF. Even if a
+      * task was allowed to allocate THPs, BPF can decide whether its forked
+      * child can allocate THPs.
+      *
+      * The MMF_VM_HUGEPAGE flag will be cleared by khugepaged.
+      */
+     if (mm_flags_test(MMF_VM_HUGEPAGE, oldmm) &&
+             get_suggested_order(mm, NULL, 0, -1, BIT(PMD_ORDER)))

Hmmm so there seems to be some kind of additional functionality you're providing here kinda quietly, which is to allow the exact same interface to determine whether we kick off khugepaged or not.

Quoted 4 lines

Don't love that, I think we should be hugely specific about that.

This bpf interface should literally be 'ok we're deciding what order we want'. It feels like a bit of a gross overloading?

This makes sense. I have no objection to reverting to returning a single order.

Quoted 7 lines
Quoted 5 lines

OK but key point here is - we're now determining if a forked child can _not_ allocate THPs using this function.

To me this should be a separate function rather than some _weird_ usage of this same function.

Perhaps a separate function is better.

Thanks!

Quoted 9 lines
Quoted 4 lines

And generally at this point I think we should just drop this bit of code honestly.

MMF_VM_HUGEPAGE is set when the THP mode is "always" or "madvise". If it’s set, any forked child processes will inherit this flag. It is only cleared when the mm_struct is destroyed (please correct me if I’m wrong).

Quoted 6 lines

__mmput()
-> khugepaged_exit()
-> (if MMF_VM_HUGEPAGE set) __khugepaged_exit()
-> Clear flag once mm fully done with (afaict), dropping associated mm refcount.

^--- this does seem to be accurate indeed.

Thanks for the explanation.

Quoted 15 lines
Quoted 7 lines

However, when you switch the THP mode to "never", tasks that still have MMF_VM_HUGEPAGE remain on the khugepaged scan list. This isn’t an issue under the current global mode because khugepaged doesn’t run when THP is set to "never".

Quoted 5 lines

The problem arises when we move from a global mode to a per-task mode. In that case, khugepaged may end up doing unnecessary work. For example, if the THP mode is "always", but some tasks are not allowed to allocate THP while still having MMF_VM_HUGEPAGE set, khugepaged will continue scanning them unnecessarily.

But this can change right?

Quoted 8 lines

I really don't like the idea _at all_ of overriding this hook to do things other than what it says it does.

It's 'set which order to use' except when it's this case then it's 'will we do any work'.

This should be a separate callback or we should drop this and live with the possible additional work.

Perhaps we could reuse the MMF_DISABLE_THP flag by introducing a new BPF helper to set it when we want to disable THP for a specific task.

Separately from this patchset, I realized we can optimize khugepaged handling for the MMF_DISABLE_THP case with the following changes:

mm/khugepaged.c +5 -0
index 15203ea7d007..e9964edcee29 100644
@@ -402,6 +402,11 @@ void __init khugepaged_destroy(void)
        kmem_cache_destroy(mm_slot_cache);
 }
 
+static inline int hpage_collapse_test_disable(struct mm_struct *mm)
+{
+       return test_bit(MMF_DISABLE_THP, &mm->flags);
+}
+
 static inline int hpage_collapse_test_exit(struct mm_struct *mm)
 {
        return atomic_read(&mm->mm_users) == 0;
@@ -1448,6 +1453,11 @@ static void collect_mm_slot(struct

khugepaged_mm_slot *mm_slot)

                /* khugepaged_mm_lock actually not necessary for the below */
                mm_slot_free(mm_slot_cache, mm_slot);
                mmdrop(mm);
+       } else if (hpage_collapse_test_disable(mm)) {
+               hash_del(&slot->hash);
+               list_del(&slot->mm_node);
+               mm_flags_clear(MMF_VM_HUGEPAGE, mm);
+               mm_slot_free(mm_slot_cache, mm_slot);
        }

}

Specifically, if MMF_DISABLE_THP is set, we should remove it from mm_slot to prevent unnecessary khugepaged processing.

Quoted 10 lines
Quoted 8 lines

To avoid this, we should prevent setting this flag for child processes if they are not allowed to allocate THP in the first place. This way, khugepaged won’t waste cycles scanning them. While an alternative approach would be to set the flag at fork and later clear it for khugepaged, it’s clearly more efficient to avoid setting it from the start.

We also obviously should have a comment with all this context here.

Understood. I'll give some thought to a better way of handling this.

Signature · 2 lines
Regards
Yafang
#11 lore raw

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

Quoted 21 lines · Yafang Shao
Quoted 19 lines · Lorenzo Stoakes
Quoted 6 lines · Yafang Shao
Quoted 4 lines · Lorenzo Stoakes
Quoted 2 lines · Yafang Shao
Quoted 1 line

Also will mm ever != vma->vm_mm?

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

No it can't. It can be guaranteed by the caller.

In this case we don't need to pass mm separately then right?

Right, we need to pass either @mm or @vma. However, there are cases where vma information is not available at certain call sites, such as in khugepaged. In those cases, we need to pass @mm instead.

Yeah... this is weird to me though, are you checking in _general_ what khugepaged should use, or otherwise surely it's per-VMA?

Quoted 8 lines
Quoted 5 lines

Otherwise this bpf hook seems ill-suited for that, and we should have a separate one for khugepaged surely?

I also hate that we're passing mm _just because of this one edge case_, otherwise always passing vma->vm_mm, it's a confusing interface.

make sense.
I'll give some thought to how we can better handle this edge case.

Thanks!

Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

Also if we're returning a bitmask of orders which you seem to be (not sure I like that tbh - I feel like we shoudl simply provide one order but open for disucssion) - shouldn't it return an unsigned long?

Quoted 7 lines
Quoted 4 lines

We are indifferent to whether a single order or a bitmask is returned, as we only use order-0 and order-9. We have no use cases for middle-order pages, though this feature might be useful for other architectures or for some special use cases.

Well surely we want to potentially specify a mTHP under certain circumstances no?

Perhaps there are use cases, but I haven’t found any use cases for this in our production environment. On the other hand, I can clearly see a risk that it could lead to more costly high-order allocations.

Quoted 5 lines
Quoted 3 lines

So why are we returning a bitmap then? Seems like we should just return a single order in this case... I think you say below that you are open to this?

will return a single order in the next version.

Thanks

Quoted 11 lines
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines

In any case I feel it's worth making any bitfield a system word size.

Also :>)

If we do move to returning a single order, should be unsigned int.

sure

Thanks!

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

And generally at this point I think we should just drop this bit of code honestly.

MMF_VM_HUGEPAGE is set when the THP mode is "always" or "madvise". If it’s set, any forked child processes will inherit this flag. It is only cleared when the mm_struct is destroyed (please correct me if I’m wrong).

Quoted 8 lines
Quoted 6 lines

__mmput()
-> khugepaged_exit()
-> (if MMF_VM_HUGEPAGE set) __khugepaged_exit()
-> Clear flag once mm fully done with (afaict), dropping associated mm refcount.

^--- this does seem to be accurate indeed.

Thanks for the explanation.

No problem, this was more 'Lorenzo's thought process' :P

Quoted 19 lines
Quoted 19 lines
Quoted 8 lines

However, when you switch the THP mode to "never", tasks that still have MMF_VM_HUGEPAGE remain on the khugepaged scan list. This isn’t an issue under the current global mode because khugepaged doesn’t run when THP is set to "never".

Quoted 5 lines

The problem arises when we move from a global mode to a per-task mode. In that case, khugepaged may end up doing unnecessary work. For example, if the THP mode is "always", but some tasks are not allowed to allocate THP while still having MMF_VM_HUGEPAGE set, khugepaged will continue scanning them unnecessarily.

But this can change right?

I really don't like the idea _at all_ of overriding this hook to do things other than what it says it does.

Quoted 8 lines
Quoted 5 lines

It's 'set which order to use' except when it's this case then it's 'will we do any work'.

This should be a separate callback or we should drop this and live with the possible additional work.

Perhaps we could reuse the MMF_DISABLE_THP flag by introducing a new BPF helper to set it when we want to disable THP for a specific task.

Interesting, yeah perhaps that could work, as long as we're in a sensible context to be able to toggle this bit.

Quoted 33 lines

Separately from this patchset, I realized we can optimize khugepaged handling for the MMF_DISABLE_THP case with the following changes:

mm/khugepaged.c +10 -0
index 15203ea7d007..e9964edcee29 100644
@@ -402,6 +402,11 @@ void __init khugepaged_destroy(void)
        kmem_cache_destroy(mm_slot_cache);
 }
 
+static inline int hpage_collapse_test_disable(struct mm_struct *mm)
+{
+       return test_bit(MMF_DISABLE_THP, &mm->flags);
+}
+
 static inline int hpage_collapse_test_exit(struct mm_struct *mm)
 {
        return atomic_read(&mm->mm_users) == 0;
@@ -1448,6 +1453,11 @@ static void collect_mm_slot(struct
 khugepaged_mm_slot *mm_slot)
                /* khugepaged_mm_lock actually not necessary for the below */
                mm_slot_free(mm_slot_cache, mm_slot);
                mmdrop(mm);
+       } else if (hpage_collapse_test_disable(mm)) {
+               hash_del(&slot->hash);
+               list_del(&slot->mm_node);
+               mm_flags_clear(MMF_VM_HUGEPAGE, mm);
+               mm_slot_free(mm_slot_cache, mm_slot);
        }
 }
Quoted 2 lines

Specifically, if MMF_DISABLE_THP is set, we should remove it from mm_slot to prevent unnecessary khugepaged processing.

Ohhh interesting, perhaps send as separate patch?

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

To avoid this, we should prevent setting this flag for child processes if they are not allowed to allocate THP in the first place. This way, khugepaged won’t waste cycles scanning them. While an alternative approach would be to set the flag at fork and later clear it for khugepaged, it’s clearly more efficient to avoid setting it from the start.

Quoted 3 lines
Quoted 1 line

We also obviously should have a comment with all this context here.

Understood. I'll give some thought to a better way of handling this.

Thanks!

Quoted 4 lines

--
Regards
Yafang

Cheers, Lorenzo

#12 lore raw

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

Quoted 120 lines · Lorenzo Stoakes
Quoted 21 lines · Yafang Shao
Quoted 19 lines · Lorenzo Stoakes
Quoted 6 lines · Yafang Shao
Quoted 4 lines · Lorenzo Stoakes
Quoted 2 lines · Yafang Shao
Quoted 1 line

Also will mm ever != vma->vm_mm?

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

No it can't. It can be guaranteed by the caller.

In this case we don't need to pass mm separately then right?

Right, we need to pass either @mm or @vma. However, there are cases where vma information is not available at certain call sites, such as in khugepaged. In those cases, we need to pass @mm instead.

Yeah... this is weird to me though, are you checking in _general_ what khugepaged should use, or otherwise surely it's per-VMA?

Quoted 8 lines
Quoted 5 lines

Otherwise this bpf hook seems ill-suited for that, and we should have a separate one for khugepaged surely?

I also hate that we're passing mm _just because of this one edge case_, otherwise always passing vma->vm_mm, it's a confusing interface.

make sense.
I'll give some thought to how we can better handle this edge case.

Thanks!

Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

Also if we're returning a bitmask of orders which you seem to be (not sure I like that tbh - I feel like we shoudl simply provide one order but open for disucssion) - shouldn't it return an unsigned long?

Quoted 7 lines
Quoted 4 lines

We are indifferent to whether a single order or a bitmask is returned, as we only use order-0 and order-9. We have no use cases for middle-order pages, though this feature might be useful for other architectures or for some special use cases.

Well surely we want to potentially specify a mTHP under certain circumstances no?

Perhaps there are use cases, but I haven’t found any use cases for this in our production environment. On the other hand, I can clearly see a risk that it could lead to more costly high-order allocations.

Quoted 5 lines
Quoted 3 lines

So why are we returning a bitmap then? Seems like we should just return a single order in this case... I think you say below that you are open to this?

will return a single order in the next version.

Thanks

Quoted 11 lines
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines

In any case I feel it's worth making any bitfield a system word size.

Also :>)

If we do move to returning a single order, should be unsigned int.

sure

Thanks!

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

And generally at this point I think we should just drop this bit of code honestly.

MMF_VM_HUGEPAGE is set when the THP mode is "always" or "madvise". If it’s set, any forked child processes will inherit this flag. It is only cleared when the mm_struct is destroyed (please correct me if I’m wrong).

Quoted 8 lines
Quoted 6 lines

__mmput()
-> khugepaged_exit()
-> (if MMF_VM_HUGEPAGE set) __khugepaged_exit()
-> Clear flag once mm fully done with (afaict), dropping associated mm refcount.

^--- this does seem to be accurate indeed.

Thanks for the explanation.

No problem, this was more 'Lorenzo's thought process' :P

Quoted 22 lines
Quoted 22 lines
Quoted 8 lines

However, when you switch the THP mode to "never", tasks that still have MMF_VM_HUGEPAGE remain on the khugepaged scan list. This isn’t an issue under the current global mode because khugepaged doesn’t run when THP is set to "never".

Quoted 5 lines

The problem arises when we move from a global mode to a per-task mode. In that case, khugepaged may end up doing unnecessary work. For example, if the THP mode is "always", but some tasks are not allowed to allocate THP while still having MMF_VM_HUGEPAGE set, khugepaged will continue scanning them unnecessarily.

But this can change right?

I really don't like the idea _at all_ of overriding this hook to do things other than what it says it does.

It's 'set which order to use' except when it's this case then it's 'will we do any work'.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This should be a separate callback or we should drop this and live with the possible additional work.

Perhaps we could reuse the MMF_DISABLE_THP flag by introducing a new BPF helper to set it when we want to disable THP for a specific task.

Interesting, yeah perhaps that could work, as long as we're in a sensible context to be able to toggle this bit.

Right, we can't set the mm->flags arbitrarily. Perhaps we should add a generic BPF hook in dup_mmap().

mm/mmap.c +0 -0
index 7a057e0e8da9..1b60bdb08de1 100644
@@ -1843,6 +1843,8 @@ __latent_entropy int dup_mmap(struct mm_struct

*mm, struct mm_struct *oldmm)
loop_out:

        vma_iter_free(&vmi);
        if (!retval) {
+               /* Allow a BPF program to modify the new mm_struct in fork. */
+               bpf_hook_mm_fork(mm, oldmm);
                mt_set_in_rcu(vmi.mas.tree);
                ksm_fork(mm, oldmm);
                khugepaged_fork(mm, oldmm);

This provides a mechanism for BPF programs to configure the new mm_struct on demand, acting as a modern, flexible replacement for prctl() ;-)

Quoted 34 lines
Quoted 34 lines

Separately from this patchset, I realized we can optimize khugepaged handling for the MMF_DISABLE_THP case with the following changes:

mm/khugepaged.c +10 -0
index 15203ea7d007..e9964edcee29 100644
@@ -402,6 +402,11 @@ void __init khugepaged_destroy(void)
        kmem_cache_destroy(mm_slot_cache);
 }
 
+static inline int hpage_collapse_test_disable(struct mm_struct *mm)
+{
+       return test_bit(MMF_DISABLE_THP, &mm->flags);
+}
+
 static inline int hpage_collapse_test_exit(struct mm_struct *mm)
 {
        return atomic_read(&mm->mm_users) == 0;
@@ -1448,6 +1453,11 @@ static void collect_mm_slot(struct
 khugepaged_mm_slot *mm_slot)
                /* khugepaged_mm_lock actually not necessary for the below */
                mm_slot_free(mm_slot_cache, mm_slot);
                mmdrop(mm);
+       } else if (hpage_collapse_test_disable(mm)) {
+               hash_del(&slot->hash);
+               list_del(&slot->mm_node);
+               mm_flags_clear(MMF_VM_HUGEPAGE, mm);
+               mm_slot_free(mm_slot_cache, mm_slot);
        }
 }
Quoted 4 lines
Quoted 2 lines

Specifically, if MMF_DISABLE_THP is set, we should remove it from mm_slot to prevent unnecessary khugepaged processing.

Ohhh interesting, perhaps send as separate patch?

sure, I will send it separately.

Signature · 2 lines
Regards
Yafang
#13 lore raw

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

Quoted 25 lines · Yafang Shao
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 8 lines

However, when you switch the THP mode to "never", tasks that still have MMF_VM_HUGEPAGE remain on the khugepaged scan list. This isn’t an issue under the current global mode because khugepaged doesn’t run when THP is set to "never".

Quoted 5 lines

The problem arises when we move from a global mode to a per-task mode. In that case, khugepaged may end up doing unnecessary work. For example, if the THP mode is "always", but some tasks are not allowed to allocate THP while still having MMF_VM_HUGEPAGE set, khugepaged will continue scanning them unnecessarily.

But this can change right?

I really don't like the idea _at all_ of overriding this hook to do things other than what it says it does.

It's 'set which order to use' except when it's this case then it's 'will we do any work'.

This should be a separate callback or we should drop this and live with the possible additional work.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Perhaps we could reuse the MMF_DISABLE_THP flag by introducing a new BPF helper to set it when we want to disable THP for a specific task.

Interesting, yeah perhaps that could work, as long as we're in a sensible context to be able to toggle this bit.

Right, we can't set the mm->flags arbitrarily. Perhaps we should add a generic BPF hook in dup_mmap().

Yeah perhaps that could be a way forward :)

Quoted 15 lines
mm/mmap.c +2 -0
index 7a057e0e8da9..1b60bdb08de1 100644
@@ -1843,6 +1843,8 @@ __latent_entropy int dup_mmap(struct mm_struct
 *mm, struct mm_struct *oldmm)
 loop_out:
        vma_iter_free(&vmi);
        if (!retval) {
+               /* Allow a BPF program to modify the new mm_struct in fork. */
+               bpf_hook_mm_fork(mm, oldmm);
                mt_set_in_rcu(vmi.mas.tree);
                ksm_fork(mm, oldmm);
                khugepaged_fork(mm, oldmm);
Quoted 3 lines

This provides a mechanism for BPF programs to configure the new mm_struct on demand, acting as a modern, flexible replacement for prctl() ;-)

Hahaha that's obviously very appealing to me :)))

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

Separately from this patchset, I realized we can optimize khugepaged handling for the MMF_DISABLE_THP case with the following changes:

mm/khugepaged.c +10 -0
index 15203ea7d007..e9964edcee29 100644
@@ -402,6 +402,11 @@ void __init khugepaged_destroy(void)
        kmem_cache_destroy(mm_slot_cache);
 }
 
+static inline int hpage_collapse_test_disable(struct mm_struct *mm)
+{
+       return test_bit(MMF_DISABLE_THP, &mm->flags);
+}
+
 static inline int hpage_collapse_test_exit(struct mm_struct *mm)
 {
        return atomic_read(&mm->mm_users) == 0;
@@ -1448,6 +1453,11 @@ static void collect_mm_slot(struct
 khugepaged_mm_slot *mm_slot)
                /* khugepaged_mm_lock actually not necessary for the below */
                mm_slot_free(mm_slot_cache, mm_slot);
                mmdrop(mm);
+       } else if (hpage_collapse_test_disable(mm)) {
+               hash_del(&slot->hash);
+               list_del(&slot->mm_node);
+               mm_flags_clear(MMF_VM_HUGEPAGE, mm);
+               mm_slot_free(mm_slot_cache, mm_slot);
        }
 }
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Specifically, if MMF_DISABLE_THP is set, we should remove it from mm_slot to prevent unnecessary khugepaged processing.

Ohhh interesting, perhaps send as separate patch?

sure, I will send it separately.

Thanks!

Quoted 4 lines

--
Regards
Yafang

And overall - cheers for being an ABSOLUTE DELIGHT on review :) it's much appreciated. I shall buy you a beer (or whatever is your preferred beverage) at the next conference we are both at :)

Cheers, Lorenzo

#14 lore raw

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

Quoted 105 lines · Lorenzo Stoakes
Quoted 25 lines · Yafang Shao
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 8 lines

However, when you switch the THP mode to "never", tasks that still have MMF_VM_HUGEPAGE remain on the khugepaged scan list. This isn’t an issue under the current global mode because khugepaged doesn’t run when THP is set to "never".

Quoted 5 lines

The problem arises when we move from a global mode to a per-task mode. In that case, khugepaged may end up doing unnecessary work. For example, if the THP mode is "always", but some tasks are not allowed to allocate THP while still having MMF_VM_HUGEPAGE set, khugepaged will continue scanning them unnecessarily.

But this can change right?

I really don't like the idea _at all_ of overriding this hook to do things other than what it says it does.

It's 'set which order to use' except when it's this case then it's 'will we do any work'.

This should be a separate callback or we should drop this and live with the possible additional work.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Perhaps we could reuse the MMF_DISABLE_THP flag by introducing a new BPF helper to set it when we want to disable THP for a specific task.

Interesting, yeah perhaps that could work, as long as we're in a sensible context to be able to toggle this bit.

Right, we can't set the mm->flags arbitrarily. Perhaps we should add a generic BPF hook in dup_mmap().

Yeah perhaps that could be a way forward :)

Quoted 15 lines
mm/mmap.c +2 -0
index 7a057e0e8da9..1b60bdb08de1 100644
@@ -1843,6 +1843,8 @@ __latent_entropy int dup_mmap(struct mm_struct
 *mm, struct mm_struct *oldmm)
 loop_out:
        vma_iter_free(&vmi);
        if (!retval) {
+               /* Allow a BPF program to modify the new mm_struct in fork. */
+               bpf_hook_mm_fork(mm, oldmm);
                mt_set_in_rcu(vmi.mas.tree);
                ksm_fork(mm, oldmm);
                khugepaged_fork(mm, oldmm);
Quoted 3 lines

This provides a mechanism for BPF programs to configure the new mm_struct on demand, acting as a modern, flexible replacement for prctl() ;-)

Hahaha that's obviously very appealing to me :)))

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

Separately from this patchset, I realized we can optimize khugepaged handling for the MMF_DISABLE_THP case with the following changes:

mm/khugepaged.c +10 -0
index 15203ea7d007..e9964edcee29 100644
@@ -402,6 +402,11 @@ void __init khugepaged_destroy(void)
        kmem_cache_destroy(mm_slot_cache);
 }
 
+static inline int hpage_collapse_test_disable(struct mm_struct *mm)
+{
+       return test_bit(MMF_DISABLE_THP, &mm->flags);
+}
+
 static inline int hpage_collapse_test_exit(struct mm_struct *mm)
 {
        return atomic_read(&mm->mm_users) == 0;
@@ -1448,6 +1453,11 @@ static void collect_mm_slot(struct
 khugepaged_mm_slot *mm_slot)
                /* khugepaged_mm_lock actually not necessary for the below */
                mm_slot_free(mm_slot_cache, mm_slot);
                mmdrop(mm);
+       } else if (hpage_collapse_test_disable(mm)) {
+               hash_del(&slot->hash);
+               list_del(&slot->mm_node);
+               mm_flags_clear(MMF_VM_HUGEPAGE, mm);
+               mm_slot_free(mm_slot_cache, mm_slot);
        }
 }
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Specifically, if MMF_DISABLE_THP is set, we should remove it from mm_slot to prevent unnecessary khugepaged processing.

Ohhh interesting, perhaps send as separate patch?

sure, I will send it separately.

Thanks!

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

--
Regards
Yafang

And overall - cheers for being an ABSOLUTE DELIGHT on review :) it's much appreciated. I shall buy you a beer (or whatever is your preferred beverage) at the next conference we are both at :)

Honestly, that's exactly what I wanted to say to you too! I learned so much during your review process, and I owe you a beer (or your drink of choice) as well!

Signature · 2 lines
Regards
Yafang
#15 lore raw

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

In reply to Yafang Shao

On Tue, Aug 26, 2025 at 3:43 PM Yafang Shao <[email protected]> wrote:
[...]

mm/memory.c static struct folio *alloc_swap_folio(struct vm_fault *vmf) · line 4515

Quoted 7 lines
mm/memory.c +3 -0
@@ -4510,13 +4510,18 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
        if (!zswap_never_enabled())
                goto fallback;
 
+       suggested_orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags,
+                                              TVA_PAGEFAULT,
+                                              BIT(PMD_ORDER) - 1);

Can we separate this case from the normal anonymous page faults below? 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 28 lines
mm/memory.c +4 -2
+       if (!suggested_orders)
+               goto fallback;
        entry = pte_to_swp_entry(vmf->orig_pte);
        /*
         * 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,
-                                         BIT(PMD_ORDER) - 1);
+                                         suggested_orders);
        orders = thp_vma_suitable_orders(vma, vmf->address, orders);
        orders = thp_swap_suitable_orders(swp_offset(entry),
                                          vmf->address, orders);
@@ -5044,12 +5049,12 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
 {
        struct vm_area_struct *vma = vmf->vma;
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
+       int order, suggested_orders;
        unsigned long orders;
        struct folio *folio;
        unsigned long addr;
        pte_t *pte;
        gfp_t gfp;
-       int order;
 
        /*
         * If uffd is active for the vma we need per-page fault fidelity to

mm/memory.c static struct folio *alloc_anon_folio(struct vm_fault *vmf) · line 5070

Quoted 8 lines
mm/memory.c +5 -0
@@ -5058,13 +5063,18 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf)
        if (unlikely(userfaultfd_armed(vma)))
                goto fallback;
 
+       suggested_orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags,
+                                              TVA_PAGEFAULT,
+                                              BIT(PMD_ORDER) - 1);
+       if (!suggested_orders)
+               goto fallback;

Thanks
Barry

#16 lore raw

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

Quoted 8 lines · Barry Song

On Tue, Aug 26, 2025 at 3:43 PM Yafang Shao <[email protected]> wrote:
[...]

Quoted 4 lines
mm/memory.c +0 -0
@@ -4510,13 +4510,18 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf)
        if (!zswap_never_enabled())
                goto fallback;
 
Quoted 10 lines

mm/memory.c static struct folio *alloc_swap_folio(struct vm_fault *vmf) · line 4515

Quoted 3 lines
mm/memory.c +3 -0
+       suggested_orders = get_suggested_order(vma->vm_mm, vma, vma->vm_flags,
+                                              TVA_PAGEFAULT,
+                                              BIT(PMD_ORDER) - 1);

Can we separate this case from the normal anonymous page faults below? 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().

make sense to me.
Perhaps we can add a new tva_type such as TVA_SWAP for this case.

Signature · 2 lines
Regards
Yafang
#17 lore raw

PATCH v6 2/10 mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()

In reply to Yafang Shao

We will utilize this new kfunc bpf_mm_get_mem_cgroup() to retrieve the associated mem_cgroup from the given @mm. The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup() as a paired operation.

---
 mm/bpf_thp.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
mm/bpf_thp.c +50 -1
index fbff3b1bb988..b757e8f425fd 100644
@@ -175,10 +175,59 @@ static struct bpf_struct_ops bpf_bpf_thp_ops = {
 	.name = "bpf_thp_ops",
 };
 
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_mm_get_mem_cgroup - Get the memory cgroup associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup().
+ *
+ * Return: The associated mem_cgroup on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.
+ */
+__bpf_kfunc struct mem_cgroup *bpf_mm_get_mem_cgroup(struct mm_struct *mm)
+{
+	return get_mem_cgroup_from_mm(mm);
+}
+
+/**
+ * bpf_put_mem_cgroup - Release a memory cgroup obtained from bpf_mm_get_mem_cgroup()
+ * @memcg: The memory cgroup to release
+ */
+__bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
+{
+#ifdef CONFIG_MEMCG
+	if (!memcg)
+		return;
+	css_put(&memcg->css);
+#endif
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_thp_ids)
+BTF_ID_FLAGS(func, bpf_mm_get_mem_cgroup, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_RELEASE)
+BTF_KFUNCS_END(bpf_thp_ids)
+
+static const struct btf_kfunc_id_set bpf_thp_set = {
+	.owner = THIS_MODULE,
+	.set = &bpf_thp_ids,
+};
+
 static int __init bpf_thp_ops_init(void)
 {
-	int err = register_bpf_struct_ops(&bpf_bpf_thp_ops, bpf_thp_ops);
+	int err;
+
+	err = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_thp_set);
+	if (err) {
+		pr_err("bpf_thp: Failed to register kfunc sets (%d)\n", err);
+		return 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;
Signature · 1 line
2.47.3
#18 lore raw

Re: [PATCH v6 mm-new 02/10] mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()

+cc cgroup people, please do include them on this stuff.

BTW I see there is a BPF [STORAGE & CGROUPS] section in MAINTAINERS and kernel/bpf/cgroup.c etc. anything useful there for us?

Quoted 3 lines · Yafang Shao

We will utilize this new kfunc bpf_mm_get_mem_cgroup() to retrieve the associated mem_cgroup from the given @mm. The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup() as a paired operation.

What locking guarantees do we have that this is all fine?

Quoted 4 lines
---
 mm/bpf_thp.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-

Also not to be nitty (but I'm going to be anyway :P) but I'm not in love with the filename here.

So now we have

- khugepaged.c
- huge_memory.c
- bpf_thp.c

Let's maybe call it huge_memory_bpf.c for consistency? And obv as mentioned before, add it to the MAINTAINERS in the THP section plz.

Quoted 11 lines
 1 file changed, 50 insertions(+), 1 deletion(-)
mm/bpf_thp.c +1 -0
index fbff3b1bb988..b757e8f425fd 100644
@@ -175,10 +175,59 @@ static struct bpf_struct_ops bpf_bpf_thp_ops = {
 	.name = "bpf_thp_ops",
 };
 
+__bpf_kfunc_start_defs();

mm/bpf_thp.c static struct bpf_struct_ops bpf_bpf_thp_ops = { · line 188

Quoted 10 lines
mm/bpf_thp.c +10 -0
+
+/**
+ * bpf_mm_get_mem_cgroup - Get the memory cgroup associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup().
+ *
+ * Return: The associated mem_cgroup on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.

What kind of locking is assumed here?

Are we protected against mmdrop() clearing out the mm?

Quoted 6 lines
mm/bpf_thp.c +6 -0
+ */
+__bpf_kfunc struct mem_cgroup *bpf_mm_get_mem_cgroup(struct mm_struct *mm)
+{
+	return get_mem_cgroup_from_mm(mm);
+}
+

mm/bpf_thp.c static struct bpf_struct_ops bpf_bpf_thp_ops = { · line 204

Quoted 10 lines
mm/bpf_thp.c +10 -0
+/**
+ * bpf_put_mem_cgroup - Release a memory cgroup obtained from bpf_mm_get_mem_cgroup()
+ * @memcg: The memory cgroup to release
+ */
+__bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
+{
+#ifdef CONFIG_MEMCG
+	if (!memcg)
+		return;
+	css_put(&memcg->css);

Feels weird to have an ifdef here but not elsewhere, maybe the whole thing should be ifdef...?

Is there not a put equivalent for get_mem_cgroup_from_mm()? That is a bit weird.

Also do we now refrence the memcg global? That's pretty gross, could we not actually implement such a helper?

Is it valid to do this also? Maybe cgroup people can chime in.

Quoted 20 lines
mm/bpf_thp.c +17 -1
+#endif
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_thp_ids)
+BTF_ID_FLAGS(func, bpf_mm_get_mem_cgroup, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_RELEASE)
+BTF_KFUNCS_END(bpf_thp_ids)
+
+static const struct btf_kfunc_id_set bpf_thp_set = {
+	.owner = THIS_MODULE,
+	.set = &bpf_thp_ids,
+};
+
 static int __init bpf_thp_ops_init(void)
 {
-	int err = register_bpf_struct_ops(&bpf_bpf_thp_ops, bpf_thp_ops);
+	int err;
+

mm/bpf_thp.c static struct bpf_struct_ops bpf_bpf_thp_ops = { · line 233

Quoted 10 lines
mm/bpf_thp.c +6 -0
+	err = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_thp_set);
+	if (err) {
+		pr_err("bpf_thp: Failed to register kfunc sets (%d)\n", err);
+		return 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;

Am again assuming this is legit BPF-wise :) Not my area... yet :>)

Quoted 3 lines

--
2.47.3

#19 lore raw

Re: [PATCH v6 mm-new 02/10] mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()

Quoted 14 lines · Lorenzo Stoakes

mm/bpf_thp.c static struct bpf_struct_ops bpf_bpf_thp_ops = { · line 188

Quoted 11 lines
mm/bpf_thp.c +11 -0
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_mm_get_mem_cgroup - Get the memory cgroup associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup().
+ *
+ * Return: The associated mem_cgroup on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.

What kind of locking is assumed here?

Quoted 1 line

Are we protected against mmdrop() clearing out the mm?

No locking is needed. Just the valid mm object or NULL. Usually the underlying function (get_mem_cgroup_from_mm) is called in page fault context where the current is holding mm. Here the only requirement is that mm is valid either through explicit reference or the context.

Quoted 21 lines
Quoted 7 lines

+ */

mm/bpf_thp.c +5 -0
+__bpf_kfunc struct mem_cgroup *bpf_mm_get_mem_cgroup(struct mm_struct *mm)
+{
+	return get_mem_cgroup_from_mm(mm);
+}
+

mm/bpf_thp.c static struct bpf_struct_ops bpf_bpf_thp_ops = { · line 204

Quoted 10 lines
mm/bpf_thp.c +10 -0
+/**
+ * bpf_put_mem_cgroup - Release a memory cgroup obtained from bpf_mm_get_mem_cgroup()
+ * @memcg: The memory cgroup to release
+ */
+__bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
+{
+#ifdef CONFIG_MEMCG
+	if (!memcg)
+		return;
+	css_put(&memcg->css);

Feels weird to have an ifdef here but not elsewhere, maybe the whole thing should be ifdef...?

Quoted 6 lines

Is there not a put equivalent for get_mem_cgroup_from_mm()? That is a bit weird.

Also do we now refrence the memcg global? That's pretty gross, could we not actually implement such a helper?

Is it valid to do this also? Maybe cgroup people can chime in.

There is mem_cgroup_put() which should handle !CONFIG_MEMCG configs.

#20 lore raw

Re: [PATCH v6 mm-new 02/10] mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()

Quoted 15 lines · Shakeel Butt
Quoted 14 lines · Lorenzo Stoakes

mm/bpf_thp.c static struct bpf_struct_ops bpf_bpf_thp_ops = { · line 188

Quoted 11 lines
mm/bpf_thp.c +11 -0
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_mm_get_mem_cgroup - Get the memory cgroup associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup().
+ *
+ * Return: The associated mem_cgroup on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.

What kind of locking is assumed here?

Quoted 6 lines
Quoted 1 line

Are we protected against mmdrop() clearing out the mm?

No locking is needed. Just the valid mm object or NULL. Usually the underlying function (get_mem_cgroup_from_mm) is called in page fault context where the current is holding mm. Here the only requirement is that mm is valid either through explicit reference or the context.

I mean this may be down to me being not so familiar with BPF, but my concern is that we're handing _any_ mm here.

So presumably this could also be a remote mm?

If not then why are we accepting an mm parameter at all, when we could just grab current->mm?

If it's a remote mm, then we need to be absolutely sure that we won't UAF.

I also feel we should talk about this in the kdoc, unless BPF always somehow asserts these things to be the case + verifies them smoehow.

Quoted 22 lines
Quoted 22 lines
Quoted 8 lines

+ */

mm/bpf_thp.c +5 -0
+__bpf_kfunc struct mem_cgroup *bpf_mm_get_mem_cgroup(struct mm_struct *mm)
+{
+	return get_mem_cgroup_from_mm(mm);
+}
+

mm/bpf_thp.c static struct bpf_struct_ops bpf_bpf_thp_ops = { · line 204

Quoted 10 lines
mm/bpf_thp.c +10 -0
+/**
+ * bpf_put_mem_cgroup - Release a memory cgroup obtained from bpf_mm_get_mem_cgroup()
+ * @memcg: The memory cgroup to release
+ */
+__bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
+{
+#ifdef CONFIG_MEMCG
+	if (!memcg)
+		return;
+	css_put(&memcg->css);

Feels weird to have an ifdef here but not elsewhere, maybe the whole thing should be ifdef...?

Quoted 9 lines
Quoted 6 lines

Is there not a put equivalent for get_mem_cgroup_from_mm()? That is a bit weird.

Also do we now refrence the memcg global? That's pretty gross, could we not actually implement such a helper?

Is it valid to do this also? Maybe cgroup people can chime in.

There is mem_cgroup_put() which should handle !CONFIG_MEMCG configs.

OK Yafang - let's use this instead then?

#21 lore raw

Re: [PATCH v6 mm-new 02/10] mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()

Quoted 18 lines · Lorenzo Stoakes
Quoted 17 lines · Shakeel Butt
Quoted 15 lines · Lorenzo Stoakes

mm/bpf_thp.c static struct bpf_struct_ops bpf_bpf_thp_ops = { · line 188

Quoted 11 lines
mm/bpf_thp.c +11 -0
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_mm_get_mem_cgroup - Get the memory cgroup associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup().
+ *
+ * Return: The associated mem_cgroup on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.

What kind of locking is assumed here?

Are we protected against mmdrop() clearing out the mm?

Quoted 7 lines
Quoted 4 lines

No locking is needed. Just the valid mm object or NULL. Usually the underlying function (get_mem_cgroup_from_mm) is called in page fault context where the current is holding mm. Here the only requirement is that mm is valid either through explicit reference or the context.

I mean this may be down to me being not so familiar with BPF, but my concern is that we're handing _any_ mm here.

It's not really any mm but rather the mm whose validity is ensured by the caller. I don't know the BPF internals but if I understand Andrii's response on other email, the BPF verifier will make sure the BPF program is holding a valid mm on which it is calling this function. In non-BPF world, get_mem_cgroup_from_mm() assumes the caller is providing a valid mm.

Quoted 2 lines

So presumably this could also be a remote mm?

Which is fine as we already do this today i.e. page fault on accessing memory of a remote process.

Quoted 3 lines

If not then why are we accepting an mm parameter at all, when we could just grab current->mm?

Because current->mm might not be equal to the faulting mm as in the case of remote page fault.

Quoted 6 lines

If it's a remote mm, then we need to be absolutely sure that we won't UAF.

I also feel we should talk about this in the kdoc, unless BPF always somehow asserts these things to be the case + verifies them smoehow.

Yeah some text on how BPF verifier is making sure that the BPF program is handling a valid mm.

#22 lore raw

Re: [PATCH v6 mm-new 02/10] mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()

Quoted 27 lines · Shakeel Butt
Quoted 18 lines · Lorenzo Stoakes
Quoted 17 lines · Shakeel Butt
Quoted 15 lines · Lorenzo Stoakes

mm/bpf_thp.c static struct bpf_struct_ops bpf_bpf_thp_ops = { · line 188

Quoted 11 lines
mm/bpf_thp.c +11 -0
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_mm_get_mem_cgroup - Get the memory cgroup associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup().
+ *
+ * Return: The associated mem_cgroup on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.

What kind of locking is assumed here?

Are we protected against mmdrop() clearing out the mm?

Quoted 7 lines
Quoted 4 lines

No locking is needed. Just the valid mm object or NULL. Usually the underlying function (get_mem_cgroup_from_mm) is called in page fault context where the current is holding mm. Here the only requirement is that mm is valid either through explicit reference or the context.

I mean this may be down to me being not so familiar with BPF, but my concern is that we're handing _any_ mm here.

Quoted 6 lines

It's not really any mm but rather the mm whose validity is ensured by the caller. I don't know the BPF internals but if I understand Andrii's response on other email, the BPF verifier will make sure the BPF program is holding a valid mm on which it is calling this function. In non-BPF world, get_mem_cgroup_from_mm() assumes the caller is providing a valid mm.

OK cool. The verifier aspect of this is really nice... :)

Quoted 6 lines
Quoted 3 lines

So presumably this could also be a remote mm?

Which is fine as we already do this today i.e. page fault on accessing memory of a remote process.

OK.

Quoted 7 lines
Quoted 4 lines

If not then why are we accepting an mm parameter at all, when we could just grab current->mm?

Because current->mm might not be equal to the faulting mm as in the case of remote page fault.

Ack yeah as per above.

Quoted 10 lines
Quoted 7 lines

If it's a remote mm, then we need to be absolutely sure that we won't UAF.

I also feel we should talk about this in the kdoc, unless BPF always somehow asserts these things to be the case + verifies them smoehow.

Yeah some text on how BPF verifier is making sure that the BPF program is handling a valid mm.

This would be nice indeed!

#23 lore raw

Re: [PATCH v6 mm-new 02/10] mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()

In reply to Lorenzo Stoakes
Quoted 2 lines · Lorenzo Stoakes

+cc cgroup people, please do include them on this stuff.

sure.

Quoted 3 lines

BTW I see there is a BPF [STORAGE & CGROUPS] section in MAINTAINERS and kernel/bpf/cgroup.c etc. anything useful there for us?

BPF local storage can assist in implementing this feature. However, we still need to introduce a new helper, bpf_mm_get_mem_cgroup(), to retrieve the mem_cgroup from an mm_struct.

Quoted 7 lines · Yafang Shao
Quoted 3 lines

We will utilize this new kfunc bpf_mm_get_mem_cgroup() to retrieve the associated mem_cgroup from the given @mm. The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup() as a paired operation.

What locking guarantees do we have that this is all fine?

As explained by Shakeel, no locking is needed for this stuff.

Quoted 9 lines
Quoted 5 lines
---
 mm/bpf_thp.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-

Also not to be nitty (but I'm going to be anyway :P) but I'm not in love with the filename here.

Quoted 7 lines

So now we have

- khugepaged.c
- huge_memory.c
- bpf_thp.c

Let's maybe call it huge_memory_bpf.c for consistency?

makes sense.

Quoted 2 lines

And obv as mentioned
before, add it to the MAINTAINERS in the THP section plz.

will do it.

Signature · 2 lines
Regards
Yafang
#24 lore raw

Re: [PATCH v6 mm-new 02/10] mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()

Quoted 6 lines · Yafang Shao
Quoted 2 lines · Lorenzo Stoakes

+cc cgroup people, please do include them on this stuff.

sure.

Be good to cc on future respins for the whole series also! :) just so everybody is in the loop, thanks!

Quoted 10 lines
Quoted 4 lines

BTW I see there is a BPF [STORAGE & CGROUPS] section in MAINTAINERS and kernel/bpf/cgroup.c etc. anything useful there for us?

BPF local storage can assist in implementing this feature. However, we still need to introduce a new helper, bpf_mm_get_mem_cgroup(), to retrieve the mem_cgroup from an mm_struct.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines · Yafang Shao
Quoted 3 lines

We will utilize this new kfunc bpf_mm_get_mem_cgroup() to retrieve the associated mem_cgroup from the given @mm. The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup() as a paired operation.

What locking guarantees do we have that this is all fine?

As explained by Shakeel, no locking is needed for this stuff.

Thanks, I responded there.

Quoted 16 lines
Quoted 16 lines
Quoted 6 lines
---
 mm/bpf_thp.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-

Also not to be nitty (but I'm going to be anyway :P) but I'm not in love with the filename here.

So now we have

- khugepaged.c
- huge_memory.c
- bpf_thp.c

Quoted 8 lines
Quoted 1 line

Let's maybe call it huge_memory_bpf.c for consistency?

makes sense.

Quoted 2 lines

And obv as mentioned
before, add it to the MAINTAINERS in the THP section plz.

will do it.

Thank you on both! :)

Quoted 5 lines

--
Regards
Yafang

Cheers, Lorenzo

#25 lore raw

Re: [PATCH v6 mm-new 02/10] mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()

Quoted 11 lines · Lorenzo Stoakes
Quoted 6 lines · Yafang Shao
Quoted 2 lines · Lorenzo Stoakes

+cc cgroup people, please do include them on this stuff.

sure.

Be good to cc on future respins for the whole series also! :) just so everybody is in the loop, thanks!

Thanks for the reminder. I'll include them in the next version.

Signature · 2 lines
Regards
Yafang
#26 lore raw

Re: [PATCH v6 mm-new 02/10] mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()

In reply to Yafang Shao
Quoted 35 lines · Yafang Shao

We will utilize this new kfunc bpf_mm_get_mem_cgroup() to retrieve the associated mem_cgroup from the given @mm. The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup() as a paired operation.

---
 mm/bpf_thp.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
mm/bpf_thp.c +18 -0
index fbff3b1bb988..b757e8f425fd 100644
@@ -175,10 +175,59 @@ static struct bpf_struct_ops bpf_bpf_thp_ops = {
 	.name = "bpf_thp_ops",
 };
 
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_mm_get_mem_cgroup - Get the memory cgroup associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup().
+ *
+ * Return: The associated mem_cgroup on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.
+ */
+__bpf_kfunc struct mem_cgroup *bpf_mm_get_mem_cgroup(struct mm_struct *mm)
+{
+	return get_mem_cgroup_from_mm(mm);
+}
+
+/**

mm/bpf_thp.c static struct bpf_struct_ops bpf_bpf_thp_ops = { · line 205

Quoted 10 lines
mm/bpf_thp.c +10 -0
+ * bpf_put_mem_cgroup - Release a memory cgroup obtained from bpf_mm_get_mem_cgroup()
+ * @memcg: The memory cgroup to release
+ */
+__bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
+{
+#ifdef CONFIG_MEMCG
+	if (!memcg)
+		return;
+	css_put(&memcg->css);
+#endif

Just use mem_cgroup_put() here.

Quoted 32 lines
mm/bpf_thp.c +22 -1
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_thp_ids)
+BTF_ID_FLAGS(func, bpf_mm_get_mem_cgroup, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_RELEASE)
+BTF_KFUNCS_END(bpf_thp_ids)
+
+static const struct btf_kfunc_id_set bpf_thp_set = {
+	.owner = THIS_MODULE,
+	.set = &bpf_thp_ids,
+};
+
 static int __init bpf_thp_ops_init(void)
 {
-	int err = register_bpf_struct_ops(&bpf_bpf_thp_ops, bpf_thp_ops);
+	int err;
+
+	err = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_thp_set);
+	if (err) {
+		pr_err("bpf_thp: Failed to register kfunc sets (%d)\n", err);
+		return 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;
Signature · 1 line
2.47.3
#27 lore raw

Re: [PATCH v6 mm-new 02/10] mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()

Quoted 41 lines · Shakeel Butt
Quoted 39 lines · Yafang Shao

We will utilize this new kfunc bpf_mm_get_mem_cgroup() to retrieve the associated mem_cgroup from the given @mm. The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup() as a paired operation.

---
 mm/bpf_thp.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
mm/bpf_thp.c +22 -0
index fbff3b1bb988..b757e8f425fd 100644
@@ -175,10 +175,59 @@ static struct bpf_struct_ops bpf_bpf_thp_ops = {
      .name = "bpf_thp_ops",
 };
 
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_mm_get_mem_cgroup - Get the memory cgroup associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained mem_cgroup must be released by calling bpf_put_mem_cgroup().
+ *
+ * Return: The associated mem_cgroup on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.
+ */
+__bpf_kfunc struct mem_cgroup *bpf_mm_get_mem_cgroup(struct mm_struct *mm)
+{
+     return get_mem_cgroup_from_mm(mm);
+}
+
+/**
+ * bpf_put_mem_cgroup - Release a memory cgroup obtained from bpf_mm_get_mem_cgroup()
+ * @memcg: The memory cgroup to release
+ */
+__bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
Quoted 8 lines

mm/bpf_thp.c static struct bpf_struct_ops bpf_bpf_thp_ops = { · line 205

Quoted 6 lines
mm/bpf_thp.c +6 -0
+{
+#ifdef CONFIG_MEMCG
+     if (!memcg)
+             return;
+     css_put(&memcg->css);
+#endif

Just use mem_cgroup_put() here.

i will change it and thanks for the clarification.

Signature · 2 lines
Regards
Yafang
#28 lore raw

PATCH v6 3/10 mm: thp: add a new kfunc bpf_mm_get_task()

In reply to Yafang Shao

We will utilize this new kfunc bpf_mm_get_task() to retrieve the associated task_struct from the given @mm. The obtained task_struct must be released by calling bpf_task_release() as a paired operation.

---
 mm/bpf_thp.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
mm/bpf_thp.c +34 -0
index b757e8f425fd..46b3bc96359e 100644
@@ -205,11 +205,45 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
 #endif
 }
 
+/**
+ * bpf_mm_get_task - Get the task struct associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained task_struct must be released by calling bpf_task_release().
+ *
+ * Return: The associated task_struct on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.
+ */
+__bpf_kfunc struct task_struct *bpf_mm_get_task(struct mm_struct *mm)
+{
+#ifdef CONFIG_MEMCG
+	struct task_struct *task;
+
+	if (!mm)
+		return NULL;
+	rcu_read_lock();
+	task = rcu_dereference(mm->owner);
+	if (!task)
+		goto out;
+	if (!refcount_inc_not_zero(&task->rcu_users))
+		goto out;
+
+	rcu_read_unlock();
+	return task;
+
+out:
+	rcu_read_unlock();
+#endif
+	return NULL;
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(bpf_thp_ids)
 BTF_ID_FLAGS(func, bpf_mm_get_mem_cgroup, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_RELEASE)
+BTF_ID_FLAGS(func, bpf_mm_get_task, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL)
 BTF_KFUNCS_END(bpf_thp_ids)
 
 static const struct btf_kfunc_id_set bpf_thp_set = {
Signature · 1 line
2.47.3
#29 lore raw

Re: [PATCH v6 mm-new 03/10] mm: thp: add a new kfunc bpf_mm_get_task()

Quoted 3 lines · Yafang Shao

We will utilize this new kfunc bpf_mm_get_task() to retrieve the associated task_struct from the given @mm. The obtained task_struct must be released by calling bpf_task_release() as a paired operation.

You're basically describing the patch you're not saying why - yeah you're getting a task struct from an mm (only if CONFIG_MEMCG which you don't mention here), but not for what purpose you intend to use this?

Quoted 11 lines
---
 mm/bpf_thp.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
mm/bpf_thp.c +0 -0
index b757e8f425fd..46b3bc96359e 100644

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 212

Quoted 8 lines
mm/bpf_thp.c +5 -0
@@ -205,11 +205,45 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
 #endif
 }
 
+/**
+ * bpf_mm_get_task - Get the task struct associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained task_struct must be released by calling bpf_task_release().

Hmmm so now bpf programs can cause kernel bugs by keeping a reference around?

This feels extremely dodgy, I don't like this at all.

I thought the whole point of BPF was that this kind of thing couldn't possibly happen?

Or would this be a kernel bug?

If a bpf program can lead to a refcount not being put, this is not upstreamable surely?

Quoted 16 lines
mm/bpf_thp.c +15 -0
+ *
+ * Return: The associated task_struct on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.
+ */
+__bpf_kfunc struct task_struct *bpf_mm_get_task(struct mm_struct *mm)
+{
+#ifdef CONFIG_MEMCG
+	struct task_struct *task;
+
+	if (!mm)
+		return NULL;
+	rcu_read_lock();
+	task = rcu_dereference(mm->owner);
 
+	if (!task)

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)

Quoted 10 lines
mm/bpf_thp.c +10 -0
+		goto out;
+	if (!refcount_inc_not_zero(&task->rcu_users))
+		goto out;
+
+	rcu_read_unlock();
+	return task;
+
+out:
+	rcu_read_unlock();
+#endif

This #ifdeffery is horrid, can we please just have separate functions instead of inside the one? Thanks.

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 239

Quoted 1 line
mm/bpf_thp.c +1 -0
+	return NULL;

So we can't tell the difference between this failling due to CONFIG_MEMCG not being set (in which case it will _always_ fail) or we couldn't get a task or we couldn't get a refcount on the task.

Maybe this doesn't matter since perhaps we are only using this if CONFIG_MEMCG but in that case why even expose this if !CONFIG_MEMCG?

Quoted 14 lines
mm/bpf_thp.c +3 -0
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(bpf_thp_ids)
 BTF_ID_FLAGS(func, bpf_mm_get_mem_cgroup, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_RELEASE)
+BTF_ID_FLAGS(func, bpf_mm_get_task, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL)
 BTF_KFUNCS_END(bpf_thp_ids)
 

static const struct btf_kfunc_id_set bpf_thp_set = {
--
2.47.3

#30 lore raw

Re: [PATCH v6 mm-new 03/10] mm: thp: add a new kfunc bpf_mm_get_task()

Quoted 24 lines · Lorenzo Stoakes
Quoted 3 lines · Yafang Shao

We will utilize this new kfunc bpf_mm_get_task() to retrieve the associated task_struct from the given @mm. The obtained task_struct must be released by calling bpf_task_release() as a paired operation.

You're basically describing the patch you're not saying why - yeah you're getting a task struct from an mm (only if CONFIG_MEMCG which you don't mention here), but not for what purpose you intend to use this?

Quoted 14 lines
---
 mm/bpf_thp.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
mm/bpf_thp.c +0 -0
index b757e8f425fd..46b3bc96359e 100644
@@ -205,11 +205,45 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
 #endif
 }
 
Quoted 7 lines

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 212

Quoted 5 lines
mm/bpf_thp.c +5 -0
+/**
+ * bpf_mm_get_task - Get the task struct associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained task_struct must be released by calling bpf_task_release().

Hmmm so now bpf programs can cause kernel bugs by keeping a reference around?

BPF verifier will reject any program that cannot guarantee that bpf_task_release() will always be called. So there shouldn't be any problem here.

Quoted 11 lines

This feels extremely dodgy, I don't like this at all.

I thought the whole point of BPF was that this kind of thing couldn't possibly happen?

Or would this be a kernel bug?

If a bpf program can lead to a refcount not being put, this is not upstreamable surely?

Quoted 14 lines
Quoted 4 lines
mm/bpf_thp.c +4 -0
+ *
+ * Return: The associated task_struct on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 226

Quoted 10 lines
mm/bpf_thp.c +10 -0
+ */
+__bpf_kfunc struct task_struct *bpf_mm_get_task(struct mm_struct *mm)
+{
+#ifdef CONFIG_MEMCG
+     struct task_struct *task;
+
+     if (!mm)
+             return NULL;
+     rcu_read_lock();
+     task = rcu_dereference(mm->owner);

Question to Yafang, though. Instead of adding new kfunc just for this, have you tried marking mm->owner as BTF_TYPE_SAFE_TRUSTED_OR_NULL, which, if I understand correctly, would allow BPF program to just work with mm->owner (after checking for NULL) directly. And then you can just use existing bpf_task_acquire()

Quoted 5 lines

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 230

Quoted 5 lines
mm/bpf_thp.c +4 -0
+     if (!task)
+             goto out;
+     if (!refcount_inc_not_zero(&task->rcu_users))
+             goto out;

nit: just call bpf_task_acquire(), which will more obviously pair with suggested bpf_task_release()?

Quoted 34 lines

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 237

Quoted 7 lines
mm/bpf_thp.c +7 -0
+
+     rcu_read_unlock();
+     return task;
+
+out:
+     rcu_read_unlock();
+#endif

This #ifdeffery is horrid, can we please just have separate functions instead of inside the one? Thanks.

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 238

Quoted 1 line
mm/bpf_thp.c +1 -0
+     return NULL;

So we can't tell the difference between this failling due to CONFIG_MEMCG not being set (in which case it will _always_ fail) or we couldn't get a task or we couldn't get a refcount on the task.

Maybe this doesn't matter since perhaps we are only using this if CONFIG_MEMCG but in that case why even expose this if !CONFIG_MEMCG?

Quoted 14 lines
mm/bpf_thp.c +3 -1
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(bpf_thp_ids)
 BTF_ID_FLAGS(func, bpf_mm_get_mem_cgroup, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_RELEASE)
+BTF_ID_FLAGS(func, bpf_mm_get_task, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_RET_NULL)
 BTF_KFUNCS_END(bpf_thp_ids)
 
 static const struct btf_kfunc_id_set bpf_thp_set = {
--

2.47.3

#31 lore raw

Re: [PATCH v6 mm-new 03/10] mm: thp: add a new kfunc bpf_mm_get_task()

Quoted 35 lines · Andrii Nakryiko
Quoted 24 lines · Lorenzo Stoakes
Quoted 3 lines · Yafang Shao

We will utilize this new kfunc bpf_mm_get_task() to retrieve the associated task_struct from the given @mm. The obtained task_struct must be released by calling bpf_task_release() as a paired operation.

You're basically describing the patch you're not saying why - yeah you're getting a task struct from an mm (only if CONFIG_MEMCG which you don't mention here), but not for what purpose you intend to use this?

Quoted 14 lines
---
 mm/bpf_thp.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
mm/bpf_thp.c +0 -0
index b757e8f425fd..46b3bc96359e 100644
@@ -205,11 +205,45 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
 #endif
 }
 
Quoted 7 lines

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 212

Quoted 5 lines
mm/bpf_thp.c +5 -0
+/**
+ * bpf_mm_get_task - Get the task struct associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained task_struct must be released by calling bpf_task_release().

Hmmm so now bpf programs can cause kernel bugs by keeping a reference around?

Quoted 3 lines

BPF verifier will reject any program that cannot guarantee that bpf_task_release() will always be called. So there shouldn't be any problem here.

Thanks for the clarification.

Quoted 24 lines
Quoted 24 lines

This feels extremely dodgy, I don't like this at all.

I thought the whole point of BPF was that this kind of thing couldn't possibly happen?

Or would this be a kernel bug?

If a bpf program can lead to a refcount not being put, this is not upstreamable surely?

Quoted 12 lines
mm/bpf_thp.c +12 -0
+ *
+ * Return: The associated task_struct on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.
+ */
+__bpf_kfunc struct task_struct *bpf_mm_get_task(struct mm_struct *mm)
+{
+#ifdef CONFIG_MEMCG
+     struct task_struct *task;
+
+     if (!mm)
+             return NULL;
Quoted 8 lines
Quoted 2 lines

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 226

Quoted 2 lines
mm/bpf_thp.c +2 -0
+     rcu_read_lock();
+     task = rcu_dereference(mm->owner);

Question to Yafang, though. Instead of adding new kfunc just for this, have you tried marking mm->owner as BTF_TYPE_SAFE_TRUSTED_OR_NULL, which, if I understand correctly, would allow BPF program to just work with mm->owner (after checking for NULL) directly. And then you can just use existing bpf_task_acquire()

good suggestion.
will change it.

Quoted 9 lines
Quoted 6 lines

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 230

Quoted 6 lines
mm/bpf_thp.c +4 -0
+     if (!task)
+             goto out;
+     if (!refcount_inc_not_zero(&task->rcu_users))
+             goto out;

nit: just call bpf_task_acquire(), which will more obviously pair with suggested bpf_task_release()?

makes sense.

Signature · 2 lines
Regards
Yafang
#32 lore raw

Re: [PATCH v6 mm-new 03/10] mm: thp: add a new kfunc bpf_mm_get_task()

In reply to Andrii Nakryiko
Quoted 34 lines · Andrii Nakryiko
Quoted 24 lines · Lorenzo Stoakes
Quoted 3 lines · Yafang Shao

We will utilize this new kfunc bpf_mm_get_task() to retrieve the associated task_struct from the given @mm. The obtained task_struct must be released by calling bpf_task_release() as a paired operation.

You're basically describing the patch you're not saying why - yeah you're getting a task struct from an mm (only if CONFIG_MEMCG which you don't mention here), but not for what purpose you intend to use this?

Quoted 14 lines
---
 mm/bpf_thp.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
mm/bpf_thp.c +0 -0
index b757e8f425fd..46b3bc96359e 100644
@@ -205,11 +205,45 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
 #endif
 }
 
Quoted 7 lines

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 212

Quoted 5 lines
mm/bpf_thp.c +5 -0
+/**
+ * bpf_mm_get_task - Get the task struct associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained task_struct must be released by calling bpf_task_release().

Hmmm so now bpf programs can cause kernel bugs by keeping a reference around?

Quoted 3 lines

BPF verifier will reject any program that cannot guarantee that bpf_task_release() will always be called. So there shouldn't be any problem here.

Ah that's nice!

What specifically here is enforcing that? Apologies again - BPF is new to me.

#33 lore raw

Re: [PATCH v6 mm-new 03/10] mm: thp: add a new kfunc bpf_mm_get_task()

Quoted 36 lines · Lorenzo Stoakes
Quoted 34 lines · Andrii Nakryiko
Quoted 31 lines · Lorenzo Stoakes
Quoted 3 lines · Yafang Shao

We will utilize this new kfunc bpf_mm_get_task() to retrieve the associated task_struct from the given @mm. The obtained task_struct must be released by calling bpf_task_release() as a paired operation.

You're basically describing the patch you're not saying why - yeah you're getting a task struct from an mm (only if CONFIG_MEMCG which you don't mention here), but not for what purpose you intend to use this?

Quoted 11 lines
---
 mm/bpf_thp.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
mm/bpf_thp.c +0 -0
index b757e8f425fd..46b3bc96359e 100644

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 212

Quoted 8 lines
mm/bpf_thp.c +5 -0
@@ -205,11 +205,45 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
 #endif
 }
 
+/**
+ * bpf_mm_get_task - Get the task struct associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained task_struct must be released by calling bpf_task_release().

Hmmm so now bpf programs can cause kernel bugs by keeping a reference around?

Quoted 7 lines
Quoted 3 lines

BPF verifier will reject any program that cannot guarantee that bpf_task_release() will always be called. So there shouldn't be any problem here.

Ah that's nice!

What specifically here is enforcing that? Apologies again - BPF is new to me.

The KF_ACQUIRE and KF_RELEASE flags enforce resource management. If a BPF helper function (e.g., bpf_mm_get_task()) is marked with KF_ACQUIRE, the pointer it returns must be released by a corresponding helper marked with KF_RELEASE (e.g., bpf_task_release()). The BPF verifier will reject any program that fails to pair these calls correctly.

Signature · 2 lines
Regards
Yafang
#34 lore raw

Re: [PATCH v6 mm-new 03/10] mm: thp: add a new kfunc bpf_mm_get_task()

Quoted 44 lines · Yafang Shao
Quoted 42 lines · Lorenzo Stoakes
Quoted 34 lines · Andrii Nakryiko
Quoted 24 lines · Lorenzo Stoakes
Quoted 3 lines · Yafang Shao

We will utilize this new kfunc bpf_mm_get_task() to retrieve the associated task_struct from the given @mm. The obtained task_struct must be released by calling bpf_task_release() as a paired operation.

You're basically describing the patch you're not saying why - yeah you're getting a task struct from an mm (only if CONFIG_MEMCG which you don't mention here), but not for what purpose you intend to use this?

Quoted 14 lines
---
 mm/bpf_thp.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
mm/bpf_thp.c +0 -0
index b757e8f425fd..46b3bc96359e 100644
@@ -205,11 +205,45 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
 #endif
 }
 
Quoted 7 lines

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 212

Quoted 5 lines
mm/bpf_thp.c +5 -0
+/**
+ * bpf_mm_get_task - Get the task struct associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained task_struct must be released by calling bpf_task_release().

Hmmm so now bpf programs can cause kernel bugs by keeping a reference around?

Quoted 3 lines

BPF verifier will reject any program that cannot guarantee that bpf_task_release() will always be called. So there shouldn't be any problem here.

Ah that's nice!

Quoted 8 lines
Quoted 1 line

What specifically here is enforcing that? Apologies again - BPF is new to me.

The KF_ACQUIRE and KF_RELEASE flags enforce resource management. If a BPF helper function (e.g., bpf_mm_get_task()) is marked with KF_ACQUIRE, the pointer it returns must be released by a corresponding helper marked with KF_RELEASE (e.g., bpf_task_release()). The BPF verifier will reject any program that fails to pair these calls correctly.

OK that's really really nice actually! :)

Thanks!

Quoted 4 lines

--
Regards
Yafang

Cheers, Lorenzo

#35 lore raw

Re: [PATCH v6 mm-new 03/10] mm: thp: add a new kfunc bpf_mm_get_task()

In reply to Lorenzo Stoakes
Quoted 9 lines · Lorenzo Stoakes
Quoted 3 lines · Yafang Shao

We will utilize this new kfunc bpf_mm_get_task() to retrieve the associated task_struct from the given @mm. The obtained task_struct must be released by calling bpf_task_release() as a paired operation.

You're basically describing the patch you're not saying why - yeah you're getting a task struct from an mm (only if CONFIG_MEMCG which you don't mention here), but not for what purpose you intend to use this?

For example, we could retrieve task->comm or other attributes and make decisions based on that information. I’ll provide a clearer description in the next revision.

Quoted 25 lines
Quoted 12 lines
---
 mm/bpf_thp.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
mm/bpf_thp.c +0 -0
index b757e8f425fd..46b3bc96359e 100644

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 212

Quoted 8 lines
mm/bpf_thp.c +5 -0
@@ -205,11 +205,45 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
 #endif
 }
 
+/**
+ * bpf_mm_get_task - Get the task struct associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained task_struct must be released by calling bpf_task_release().

Hmmm so now bpf programs can cause kernel bugs by keeping a reference around?

This feels extremely dodgy, I don't like this at all.

Quoted 7 lines

I thought the whole point of BPF was that this kind of thing couldn't possibly happen?

Or would this be a kernel bug?

If a bpf program can lead to a refcount not being put, this is not upstreamable surely?

As explained by Andrii, the BPF verifier can protect it.

Quoted 31 lines
Quoted 17 lines
mm/bpf_thp.c +15 -0
+ *
+ * Return: The associated task_struct on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.
+ */
+__bpf_kfunc struct task_struct *bpf_mm_get_task(struct mm_struct *mm)
+{
+#ifdef CONFIG_MEMCG
+     struct task_struct *task;
+
+     if (!mm)
+             return NULL;
+     rcu_read_lock();
+     task = rcu_dereference(mm->owner);
 
+     if (!task)

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)

Quoted 10 lines
mm/bpf_thp.c +10 -0
+             goto out;
+     if (!refcount_inc_not_zero(&task->rcu_users))
+             goto out;
+
+     rcu_read_unlock();
+     return task;
+
+out:
+     rcu_read_unlock();
+#endif

This #ifdeffery is horrid, can we please just have separate functions instead of inside the one? Thanks.

Quoted 9 lines

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 239

Quoted 1 line
mm/bpf_thp.c +1 -0
+     return NULL;

So we can't tell the difference between this failling due to CONFIG_MEMCG not being set (in which case it will _always_ fail) or we couldn't get a task or we couldn't get a refcount on the task.

Maybe this doesn't matter since perhaps we are only using this if CONFIG_MEMCG but in that case why even expose this if !CONFIG_MEMCG?

As suggested by Andrii, I will remove this kfunc and mark mm->owner as BTF_TYPE_SAFE_TRUSTED_OR_NULL.

Thanks for your comments.

Signature · 2 lines
Regards
Yafang
#36 lore raw

Re: [PATCH v6 mm-new 03/10] mm: thp: add a new kfunc bpf_mm_get_task()

Quoted 8 lines · Yafang Shao
Quoted 6 lines · Lorenzo Stoakes
Quoted 3 lines · Yafang Shao

We will utilize this new kfunc bpf_mm_get_task() to retrieve the associated task_struct from the given @mm. The obtained task_struct must be released by calling bpf_task_release() as a paired operation.

Quoted 7 lines
Quoted 3 lines

You're basically describing the patch you're not saying why - yeah you're getting a task struct from an mm (only if CONFIG_MEMCG which you don't mention here), but not for what purpose you intend to use this?

For example, we could retrieve task->comm or other attributes and make decisions based on that information. I’ll provide a clearer description in the next revision.

Thanks!

Quoted 29 lines
Quoted 29 lines
Quoted 13 lines
---
 mm/bpf_thp.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
mm/bpf_thp.c +0 -0
index b757e8f425fd..46b3bc96359e 100644

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 212

Quoted 8 lines
mm/bpf_thp.c +5 -0
@@ -205,11 +205,45 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)
 #endif
 }
 
+/**
+ * bpf_mm_get_task - Get the task struct associated with a mm_struct.
+ * @mm: The mm_struct to query
+ *
+ * The obtained task_struct must be released by calling bpf_task_release().

Hmmm so now bpf programs can cause kernel bugs by keeping a reference around?

This feels extremely dodgy, I don't like this at all.

I thought the whole point of BPF was that this kind of thing couldn't possibly happen?

Quoted 6 lines
Quoted 4 lines

Or would this be a kernel bug?

If a bpf program can lead to a refcount not being put, this is not upstreamable surely?

As explained by Andrii, the BPF verifier can protect it.

Yeah that's nice!

Quoted 38 lines
Quoted 38 lines
Quoted 18 lines
mm/bpf_thp.c +15 -0
+ *
+ * Return: The associated task_struct on success, or NULL on failure. Note that
+ * this function depends on CONFIG_MEMCG being enabled - it will always return
+ * NULL if CONFIG_MEMCG is not configured.
+ */
+__bpf_kfunc struct task_struct *bpf_mm_get_task(struct mm_struct *mm)
+{
+#ifdef CONFIG_MEMCG
+     struct task_struct *task;
+
+     if (!mm)
+             return NULL;
+     rcu_read_lock();
+     task = rcu_dereference(mm->owner);
 
+     if (!task)

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg)

Quoted 10 lines
mm/bpf_thp.c +10 -0
+             goto out;
+     if (!refcount_inc_not_zero(&task->rcu_users))
+             goto out;
+
+     rcu_read_unlock();
+     return task;
+
+out:
+     rcu_read_unlock();
+#endif

This #ifdeffery is horrid, can we please just have separate functions instead of inside the one? Thanks.

mm/bpf_thp.c __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) · line 239

Quoted 1 line
mm/bpf_thp.c +1 -0
+     return NULL;

So we can't tell the difference between this failling due to CONFIG_MEMCG not being set (in which case it will _always_ fail) or we couldn't get a task or we couldn't get a refcount on the task.

Quoted 6 lines
Quoted 3 lines

Maybe this doesn't matter since perhaps we are only using this if CONFIG_MEMCG but in that case why even expose this if !CONFIG_MEMCG?

As suggested by Andrii, I will remove this kfunc and mark mm->owner as BTF_TYPE_SAFE_TRUSTED_OR_NULL.

OK thanks!

Quoted 2 lines

Thanks for your comments.

You're welcome :)

Quoted 4 lines

--
Regards
Yafang

Cheers, Lorenzo

#37 lore raw

PATCH v6 4/10 bpf: mark vma->vm_mm as trusted

In reply to Yafang Shao

Every VMA must have an associated mm_struct, and it is safe to access outside of RCU. Thus, we can mark it as trusted. With this change, BPF helpers can safely access vma->vm_mm to retrieve the associated task from the VMA.

---
 kernel/bpf/verifier.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/bpf/verifier.c +5 -0
index c4f69a9e9af6..984ffbca5cbe 100644
@@ -7154,6 +7154,10 @@ BTF_TYPE_SAFE_TRUSTED(struct file) {
 	struct inode *f_inode;
 };
 
+BTF_TYPE_SAFE_TRUSTED(struct vm_area_struct) {
+	struct mm_struct *vm_mm;
+};
+
 BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry) {
 	struct inode *d_inode;
 };
@@ -7193,6 +7197,7 @@ static bool type_is_trusted(struct bpf_verifier_env *env,
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct bpf_iter__task));
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct linux_binprm));
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct file));
+	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct vm_area_struct));
 
 	return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id, "__safe_trusted");
 }
Signature · 1 line
2.47.3
#38 lore raw

Re: [PATCH v6 mm-new 04/10] bpf: mark vma->vm_mm as trusted

Quoted 1 line · Yafang Shao

Every VMA must have an associated mm_struct, and it is safe to access

Err this isn't true? Pretty sure special VMAs don't have that set.

Quoted 3 lines

outside of RCU. Thus, we can mark it as trusted. With this change, BPF helpers can safely access vma->vm_mm to retrieve the associated task from the VMA.

On the basis of above don't think this is valid.

Quoted 32 lines
---
 kernel/bpf/verifier.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/bpf/verifier.c +5 -1
index c4f69a9e9af6..984ffbca5cbe 100644
@@ -7154,6 +7154,10 @@ BTF_TYPE_SAFE_TRUSTED(struct file) {
 	struct inode *f_inode;
 };
 
+BTF_TYPE_SAFE_TRUSTED(struct vm_area_struct) {
+	struct mm_struct *vm_mm;
+};
+
 BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry) {
 	struct inode *d_inode;
 };
@@ -7193,6 +7197,7 @@ static bool type_is_trusted(struct bpf_verifier_env *env,
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct bpf_iter__task));
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct linux_binprm));
 	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct file));
+	BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct vm_area_struct));
 
 	return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id, "__safe_trusted");
 }
--

2.47.3

#39 lore raw

Re: [PATCH v6 mm-new 04/10] bpf: mark vma->vm_mm as trusted

Quoted 5 lines · Lorenzo Stoakes
Quoted 1 line · Yafang Shao

Every VMA must have an associated mm_struct, and it is safe to access

Err this isn't true? Pretty sure special VMAs don't have that set.

I’m not aware of any VMA that doesn’t belong to an mm_struct. If there is such a case, it would be helpful if you could point it out. In any case, I’ll remove the VMA-related code in the next version since it’s unnecessary.

Quoted 7 lines
Quoted 4 lines

outside of RCU. Thus, we can mark it as trusted. With this change, BPF helpers can safely access vma->vm_mm to retrieve the associated task from the VMA.

On the basis of above don't think this is valid.

Signature · 2 lines
Regards
Yafang
#40 lore raw

Re: [PATCH v6 mm-new 04/10] bpf: mark vma->vm_mm as trusted

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

On Tue, Aug 26, 2025 at 03:19:42PM +0800, Yafang Shao wrote:

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Every VMA must have an associated mm_struct, and it is safe to access

Err this isn't true? Pretty sure special VMAs don't have that set.

I’m not aware of any VMA that doesn’t belong to an mm_struct. If there is such a case, it would be helpful if you could point it out. In any case, I’ll remove the VMA-related code in the next version since it’s unnecessary.

If you lok at get_vma_name() in fs/proc/task_mmu.c you'll see:

	if (!vma->vm_mm) {
		*name = "[vdso]";
		return;
	}

So a VDSO will have this condition.

I did a quick drgn()/printk() test and didn't see any, but maybe my system - but in any case this appears to be a valid situation that can arise, presumably because it's a VMA somehow shared with multiple mm's or something truly god awful like that :)

Cheers, Lorenzo

#41 lore raw

Re: [PATCH v6 mm-new 04/10] bpf: mark vma->vm_mm as trusted

Quoted 22 lines · Lorenzo Stoakes
Quoted 4 lines · Yafang Shao
Quoted 2 lines · Lorenzo Stoakes

On Tue, Aug 26, 2025 at 03:19:42PM +0800, Yafang Shao wrote:

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Every VMA must have an associated mm_struct, and it is safe to access

Err this isn't true? Pretty sure special VMAs don't have that set.

I’m not aware of any VMA that doesn’t belong to an mm_struct. If there is such a case, it would be helpful if you could point it out. In any case, I’ll remove the VMA-related code in the next version since it’s unnecessary.

If you lok at get_vma_name() in fs/proc/task_mmu.c you'll see:

        if (!vma->vm_mm) {
                *name = "[vdso]";
                return;
        }
Quoted 6 lines

So a VDSO will have this condition.

I did a quick drgn()/printk() test and didn't see any, but maybe my system - but in any case this appears to be a valid situation that can arise, presumably because it's a VMA somehow shared with multiple mm's or something truly god awful like that :)

Thanks for clarifying that.

Signature · 2 lines
Regards
Yafang
#42 lore raw

Re: [PATCH v6 mm-new 04/10] bpf: mark vma->vm_mm as trusted

Quoted 24 lines · Yafang Shao
Quoted 22 lines · Lorenzo Stoakes
Quoted 4 lines · Yafang Shao
Quoted 2 lines · Lorenzo Stoakes

On Tue, Aug 26, 2025 at 03:19:42PM +0800, Yafang Shao wrote:

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Every VMA must have an associated mm_struct, and it is safe to access

Err this isn't true? Pretty sure special VMAs don't have that set.

I’m not aware of any VMA that doesn’t belong to an mm_struct. If there is such a case, it would be helpful if you could point it out. In any case, I’ll remove the VMA-related code in the next version since it’s unnecessary.

If you lok at get_vma_name() in fs/proc/task_mmu.c you'll see:

        if (!vma->vm_mm) {
                *name = "[vdso]";
                return;
        }
Quoted 8 lines
Quoted 6 lines

So a VDSO will have this condition.

I did a quick drgn()/printk() test and didn't see any, but maybe my system - but in any case this appears to be a valid situation that can arise, presumably because it's a VMA somehow shared with multiple mm's or something truly god awful like that :)

Thanks for clarifying that.

No problem! These weird edge cases are... weird and hugely confusing. I should document some of this somewhere, as it's at the moment more 'oh yeah I remember...' then having to dig through to figure it out.

The "/dev/zero file-backed but actually anon if MAP_PRIVATE'd" is another fun unique case.

Quoted 4 lines

--
Regards
Yafang

#43 lore raw

Re: [PATCH v6 mm-new 04/10] bpf: mark vma->vm_mm as trusted

Quoted 33 lines · Lorenzo Stoakes
Quoted 31 lines · Yafang Shao
Quoted 28 lines · Lorenzo Stoakes
Quoted 4 lines · Yafang Shao
Quoted 2 lines · Lorenzo Stoakes

On Tue, Aug 26, 2025 at 03:19:42PM +0800, Yafang Shao wrote:

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Every VMA must have an associated mm_struct, and it is safe to access

Err this isn't true? Pretty sure special VMAs don't have that set.

I’m not aware of any VMA that doesn’t belong to an mm_struct. If there is such a case, it would be helpful if you could point it out. In any case, I’ll remove the VMA-related code in the next version since it’s unnecessary.

If you lok at get_vma_name() in fs/proc/task_mmu.c you'll see:

        if (!vma->vm_mm) {
                *name = "[vdso]";
                return;
        }

So a VDSO will have this condition.

I did a quick drgn()/printk() test and didn't see any, but maybe my system - but in any case this appears to be a valid situation that can arise, presumably because it's a VMA somehow shared with multiple mm's or something truly god awful like that :)

Quoted 8 lines
Quoted 1 line

Thanks for clarifying that.

No problem! These weird edge cases are... weird and hugely confusing. I should document some of this somewhere, as it's at the moment more 'oh yeah I remember...' then having to dig through to figure it out.

The "/dev/zero file-backed but actually anon if MAP_PRIVATE'd" is another fun unique case.

It would be immensely helpful if you could document these cases. We truly appreciate your contribution and the time you've invested in this.

Signature · 2 lines
Regards
Yafang
#44 lore raw

Re: [PATCH v6 mm-new 04/10] bpf: mark vma->vm_mm as trusted

Quoted 41 lines · Yafang Shao
Quoted 39 lines · Lorenzo Stoakes
Quoted 24 lines · Yafang Shao
Quoted 22 lines · Lorenzo Stoakes
Quoted 4 lines · Yafang Shao
Quoted 2 lines · Lorenzo Stoakes

On Tue, Aug 26, 2025 at 03:19:42PM +0800, Yafang Shao wrote:

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Every VMA must have an associated mm_struct, and it is safe to access

Err this isn't true? Pretty sure special VMAs don't have that set.

I’m not aware of any VMA that doesn’t belong to an mm_struct. If there is such a case, it would be helpful if you could point it out. In any case, I’ll remove the VMA-related code in the next version since it’s unnecessary.

If you lok at get_vma_name() in fs/proc/task_mmu.c you'll see:

        if (!vma->vm_mm) {
                *name = "[vdso]";
                return;
        }
Quoted 8 lines
Quoted 6 lines

So a VDSO will have this condition.

I did a quick drgn()/printk() test and didn't see any, but maybe my system - but in any case this appears to be a valid situation that can arise, presumably because it's a VMA somehow shared with multiple mm's or something truly god awful like that :)

Thanks for clarifying that.

No problem! These weird edge cases are... weird and hugely confusing. I should document some of this somewhere, as it's at the moment more 'oh yeah I remember...' then having to dig through to figure it out.

Quoted 6 lines
Quoted 2 lines

The "/dev/zero file-backed but actually anon if MAP_PRIVATE'd" is another fun unique case.

It would be immensely helpful if you could document these cases. We truly appreciate your contribution and the time you've invested in this.

Sure I will add to my TODOs :) I agree it's not great that we have these odd edge cases and do not document them clearly enough.

Will get to it :)

Quoted 4 lines

--
Regards
Yafang

Thanks, Lorenzo

#45 lore raw

PATCH v6 5/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. However, khugepaged might not run immediately during this test, making its count metrics unreliable.

---
 tools/testing/selftests/bpf/config            |   3 +
 .../selftests/bpf/prog_tests/thp_adjust.c     | 254 ++++++++++++++++++
 .../selftests/bpf/progs/test_thp_adjust.c     |  76 ++++++
 3 files changed, 333 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
tools/testing/selftests/bpf/config +3 -0
index 8916ab814a3e..27f0249c7600 100644
@@ -26,6 +26,7 @@ CONFIG_DMABUF_HEAPS=y
 CONFIG_DMABUF_HEAPS_SYSTEM=y
 CONFIG_DUMMY=y
 CONFIG_DYNAMIC_FTRACE=y
+CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION=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 +76 -0

76 lines not on this page · raw message

Signature · 1 line
2.47.3
#46 lore raw

PATCH v6 6/10 selftests/bpf: add test case for khugepaged fork

In reply to Yafang Shao

In this test case, the parent is allowed to alloc THP, but the child forked by it can't alloc THP.

---
 .../selftests/bpf/prog_tests/thp_adjust.c     | 59 +++++++++++++++++++
 .../selftests/bpf/progs/test_thp_adjust.c     | 39 ++++++++++++
 2 files changed, 98 insertions(+)
tools/testing/selftests/bpf/prog_tests/thp_adjust.c +59 -0
index a4a34ee28301..bf367c6e6f52 100644
@@ -1,5 +1,11 @@
 // SPDX-License-Identifier: GPL-2.0
 
+#define _GNU_SOURCE
+#include <sched.h>
+#include <sys/wait.h>
+#include <sys/syscall.h>
+#include <linux/sched.h>
+
 #include <math.h>
 #include <sys/mman.h>
 #include <test_progs.h>
@@ -170,6 +176,57 @@ static void subtest_thp_policy(void)
 	bpf_link__destroy(ops_link);
 }
 
+/*
+ * In this test case, we clone the child process directly into the root cgroup.
+ * Consequently, the child process is not permitted to alloc THP.
+ */
+static void subtest_thp_fork(void)
+{
+	struct clone_args args = {
+		.flags = CLONE_INTO_CGROUP,
+		.exit_signal = SIGCHLD,
+	};
+	struct bpf_link *ops_link;
+	int status, err;
+	pid_t pid;
+
+	skel->bss->ppid = getpid();
+	if (!ASSERT_GT(skel->bss->ppid, 0, "getpid"))
+		return;
+	args.cgroup = get_root_cgroup();
+	if (!ASSERT_GE(args.cgroup, 0, "get_root_cgrp_fd"))
+		return;
+
+	ops_link = bpf_map__attach_struct_ops(skel->maps.thp_fork_ops);
+	if (!ASSERT_OK_PTR(ops_link, "attach struct_ops"))
+		return;
+
+	pid = syscall(__NR_clone3, &args, sizeof(args));
+	if (!ASSERT_GE(pid, 0, "clone3"))
+		goto detach_ops;
+
+	if (pid == 0) {
+		/* child */
+		if (!ASSERT_NEQ(thp_alloc(), -1, "THP alloc"))
+			exit(EXIT_FAILURE);
+		thp_free();
+		exit(EXIT_SUCCESS);
+	}
+
+	err = waitpid(pid, &status, 0);
+	if (!ASSERT_EQ(err, pid, "waitpid"))
+		goto detach_ops;
+	ASSERT_EQ(skel->bss->fork_fail, 0, "fork_fail");
+	ASSERT_GT(skel->bss->fork_succeed, 0, "fork_succeed");
+
+	if (!ASSERT_NEQ(thp_alloc(), -1, "THP alloc"))
+		goto detach_ops;
+	thp_free();
+	ASSERT_GT(skel->bss->parent_succeed, 0, "parent_succeed");
+detach_ops:
+	bpf_link__destroy(ops_link);
+}
+
 static int thp_adjust_setup(void)
 {
 	int err, cgrp_fd, cgrp_id, pmd_order;
@@ -249,6 +306,8 @@ void test_thp_adjust(void)
 
 	if (test__start_subtest("alloc_in_khugepaged"))
 		subtest_thp_policy();
+	if (test__start_subtest("khugepaged_fork"))
+		subtest_thp_fork();
 
 	thp_adjust_destroy();
 }
tools/testing/selftests/bpf/progs/test_thp_adjust.c +39 -0
index 635915f31786..034086ce2f3d 100644
@@ -6,6 +6,7 @@
 
 char _license[] SEC("license") = "GPL";
 
+int ppid, fork_fail, fork_succeed, parent_succeed;
 int pf_alloc, pf_disallow, khugepaged_disallow;
 struct mm_struct *target_mm;
 int pmd_order, cgrp_id;
@@ -74,3 +75,41 @@ SEC(".struct_ops.link")
 struct bpf_thp_ops khugepaged_ops = {
 	.get_suggested_order = (void *)alloc_in_khugepaged,
 };
+
+
+SEC("struct_ops/get_suggested_order")
+int BPF_PROG(thp_fork_test, struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+	     u64 vma_flags, enum tva_type tva_flags, int orders)
+{
+	struct task_struct *p = bpf_get_current_task_btf();
+	struct mem_cgroup *memcg;
+	int suggested_orders = 0;
+
+	/* Only works when CONFIG_MEMCG is enabled. */
+	memcg = bpf_mm_get_mem_cgroup(mm);
+	if (!memcg)
+		return 0;
+
+	/* The tasks under this specific cgroup are allowed to alloc THP */
+	if (memcg->css.cgroup->kn->id == cgrp_id)
+		suggested_orders = orders;
+
+	if (p->parent->pid == ppid) {
+		/* The child is forked into root cgrp, so it can't alloc THP */
+		if (suggested_orders)
+			fork_fail++;
+		else
+			fork_succeed++;
+	} else if (p->pid == ppid) {
+		/* The parent can alloc THP */
+		if (suggested_orders)
+			parent_succeed++;
+	}
+	bpf_put_mem_cgroup(memcg);
+	return suggested_orders;
+}
+
+SEC(".struct_ops.link")
+struct bpf_thp_ops thp_fork_ops = {
+	.get_suggested_order = (void *)thp_fork_test,
+};
Signature · 1 line
2.47.3
#47 lore raw

PATCH v6 7/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 +++++++++++++++++++
 1 file changed, 23 insertions(+)
tools/testing/selftests/bpf/prog_tests/thp_adjust.c +23 -0
index bf367c6e6f52..6e65d7b0eb80 100644
@@ -227,6 +227,27 @@ static void subtest_thp_fork(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.thp_fork_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;
@@ -308,6 +329,8 @@ void test_thp_adjust(void)
 		subtest_thp_policy();
 	if (test__start_subtest("khugepaged_fork"))
 		subtest_thp_fork();
+	if (test__start_subtest("policy_update"))
+		subtest_thp_policy_update();
 
 	thp_adjust_destroy();
 }
Signature · 1 line
2.47.3
#48 lore raw

PATCH v6 8/10 selftests/bpf: add test cases for invalid thp_adjust usage

In reply to Yafang Shao

1. The trusted VMA pointer can be null and must be checked before dereferencing.
2. Resources acquired via bpf_mm_get_task() must be released with bpf_task_release().
3. Memory groups obtained through bpf_mm_get_mem_cgroup() must be released using bpf_put_mem_cgroup().

---
 .../selftests/bpf/prog_tests/thp_adjust.c     |  7 +++++
 .../bpf/progs/test_thp_adjust_trusted_vma.c   | 27 +++++++++++++++++++
 .../progs/test_thp_adjust_unreleased_memcg.c  | 24 +++++++++++++++++
 .../progs/test_thp_adjust_unreleased_task.c   | 25 +++++++++++++++++
 4 files changed, 83 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_vma.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_memcg.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_task.c
tools/testing/selftests/bpf/prog_tests/thp_adjust.c +7 -0
index 6e65d7b0eb80..846679acaff2 100644
@@ -11,6 +11,9 @@
 #include <test_progs.h>
 #include "cgroup_helpers.h"
 #include "test_thp_adjust.skel.h"
+#include "test_thp_adjust_trusted_vma.skel.h"
+#include "test_thp_adjust_unreleased_task.skel.h"
+#include "test_thp_adjust_unreleased_memcg.skel.h"
 
 #define LEN (16 * 1024 * 1024) /* 16MB */
 #define THP_ENABLED_FILE "/sys/kernel/mm/transparent_hugepage/enabled"
@@ -333,4 +336,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_unreleased_task);
+	RUN_TESTS(test_thp_adjust_unreleased_memcg);
 }
tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_vma.c new file +27 -0
new file mode 100644
index 000000000000..caa73bebefcf
@@ -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/get_suggested_order")
+__failure __msg("R1 invalid mem access 'trusted_ptr_or_null_'")
+int BPF_PROG(thp_trusted_vma, struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+	     u64 vma_flags, u64 tva_flags, int orders)
+{
+	struct mem_cgroup *memcg = bpf_mm_get_mem_cgroup(vma__nullable->vm_mm);
+
+	if (!memcg)
+		return 0;
+
+	bpf_put_mem_cgroup(memcg);
+	return 1;
+}
+SEC(".struct_ops.link")
+struct bpf_thp_ops thp_memcg_ops = {
+	.get_suggested_order = (void *)thp_trusted_vma,
+};
tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_memcg.c new file +24 -0
new file mode 100644
index 000000000000..467befebb35f
@@ -0,0 +1,24 @@
+// 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/get_suggested_order")
+__failure __msg("Unreleased reference")
+int BPF_PROG(thp_unreleased_memcg, struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+	     u64 vma_flags, u64 tva_flags, int orders)
+{
+	struct mem_cgroup *memcg = bpf_mm_get_mem_cgroup(mm);
+
+	/* The memcg should be released with bpf_put_mem_cgroup() */
+	return memcg ? 0 : 1;
+}
+SEC(".struct_ops.link")
+struct bpf_thp_ops thp_memcg_ops = {
+	.get_suggested_order = (void *)thp_unreleased_memcg,
+};
tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_task.c new file +25 -0
new file mode 100644
index 000000000000..50d756810412
@@ -0,0 +1,25 @@
+// 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/get_suggested_order")
+__failure __msg("Unreleased reference")
+int BPF_PROG(thp_unreleased_task, struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+	     u64 vma_flags, u64 tva_flags, int orders)
+{
+	struct task_struct *p = bpf_mm_get_task(mm);
+
+	/* The task should be released with bpf_task_release() */
+	return p ? 0 : 1;
+}
+
+SEC(".struct_ops.link")
+struct bpf_thp_ops thp_task_ops = {
+	.get_suggested_order = (void *)thp_unreleased_task,
+};
Signature · 1 line
2.47.3
#49 lore raw

PATCH v6 9/10 Documentation: add BPF-based THP adjustment documentation

In reply to Yafang Shao

Add the documentation.

---
 Documentation/admin-guide/mm/transhuge.rst | 47 ++++++++++++++++++++++
 1 file changed, 47 insertions(+)
Documentation/admin-guide/mm/transhuge.rst +47 -0
index a16a04841b96..1725b89426a9 100644
@@ -734,3 +734,50 @@ 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 (*get_suggested_order)(struct mm_struct *mm,
+                             struct vm_area_struct *vma__nullable,
+                             u64 vma_flags, enum tva_type tva_flags, int orders)
+
+Parameters::
+
+  @mm:  mm_struct associated with the THP allocation
+  @vma__nullable: vm_area_struct associated with the THP allocation (may be NULL)
+                  When NULL, the decision should be based on @mm (i.e., when
+                  triggered from an mm-scope hook rather than a VMA-specific
+                  context)
+                  Must belong to @mm (guaranteed by the caller).
+  @vma_flags: use these vm_flags instead of @vma->vm_flags (0 if @vma is NULL)
+  @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)
+  @orders: Bitmask of requested THP orders for this allocation
+           - PMD-mapped allocation if PMD_ORDER is set
+           - mTHP allocation otherwise
+
+Return value::
+
+  Bitmask of suggested THP orders for allocation. The highest suggested order
+  will not exceed the highest requested order in @orders.
+
+Implementation Notes
+--------------------
+
+This is currently an experimental feature.
+CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION 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
#50 lore raw

PATCH v6 10/10 MAINTAINERS: add entry for BPF-based THP adjustment

In reply to Yafang Shao

Add maintainership entry for the experimental BPF-driven THP adjustment feature. This experimental component may be removed in future releases. I will help with maintenance tasks for this feature during its development lifecycle.

---
 MAINTAINERS | 10 ++++++++++
 1 file changed, 10 insertions(+)
MAINTAINERS +10 -0
index 390829ae9803..71d0f7c58ce8 100644
@@ -16239,6 +16239,7 @@ F:	Documentation/admin-guide/mm/transhuge.rst
 F:	include/linux/huge_mm.h
 F:	include/linux/khugepaged.h
 F:	include/trace/events/huge_memory.h
+F:	mm/bpf_thp.c
 F:	mm/huge_memory.c
 F:	mm/khugepaged.c
 F:	mm/mm_slot.h
@@ -16246,6 +16247,15 @@ 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
 
+MEMORY MANAGEMENT - THP WITH BPF SUPPORT
+M:	Yafang Shao <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	mm/bpf_thp.c
+F:	tools/testing/selftests/bpf/prog_tests/thp_adjust.c
+F:	tools/testing/selftests/bpf/progs/test_thp_adjust*
+
 MEMORY MANAGEMENT - USERFAULTFD
 M:	Andrew Morton <[email protected]>
 R:	Peter Xu <[email protected]>
Signature · 1 line
2.47.3
#51 lore raw

Re: [PATCH v6 mm-new 10/10] MAINTAINERS: add entry for BPF-based THP adjustment

Quoted 25 lines · Yafang Shao

Add maintainership entry for the experimental BPF-driven THP adjustment feature. This experimental component may be removed in future releases. I will help with maintenance tasks for this feature during its development lifecycle.

---
 MAINTAINERS | 10 ++++++++++
 1 file changed, 10 insertions(+)
MAINTAINERS +1 -0
index 390829ae9803..71d0f7c58ce8 100644
@@ -16239,6 +16239,7 @@ F:	Documentation/admin-guide/mm/transhuge.rst
 F:	include/linux/huge_mm.h
 F:	include/linux/khugepaged.h
 F:	include/trace/events/huge_memory.h
+F:	mm/bpf_thp.c
 F:	mm/huge_memory.c
 F:	mm/khugepaged.c
 F:	mm/mm_slot.h
@@ -16246,6 +16247,15 @@ 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

MAINTAINERS F: tools/testing/selftests/mm/khugepaged.c · line 16258

Quoted 10 lines
MAINTAINERS +9 -0
 
+MEMORY MANAGEMENT - THP WITH BPF SUPPORT
+M:	Yafang Shao <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	mm/bpf_thp.c
+F:	tools/testing/selftests/bpf/prog_tests/thp_adjust.c
+F:	tools/testing/selftests/bpf/progs/test_thp_adjust*
+

Sorry but I don't agree with a separate section for this.

This should form part of the THP section only, I don't think it's warranted to do elsewise.

Quoted 6 lines
MAINTAINERS +0 -1
 MEMORY MANAGEMENT - USERFAULTFD
 M:	Andrew Morton <[email protected]>
 R:	Peter Xu <[email protected]>
--

2.47.3

#52 lore raw

Re: [PATCH v6 mm-new 10/10] MAINTAINERS: add entry for BPF-based THP adjustment

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

Add maintainership entry for the experimental BPF-driven THP adjustment feature. This experimental component may be removed in future releases. I will help with maintenance tasks for this feature during its development lifecycle.

---
 MAINTAINERS | 10 ++++++++++
 1 file changed, 10 insertions(+)
MAINTAINERS +1 -0
index 390829ae9803..71d0f7c58ce8 100644
@@ -16239,6 +16239,7 @@ F:    Documentation/admin-guide/mm/transhuge.rst
 F:   include/linux/huge_mm.h
 F:   include/linux/khugepaged.h
 F:   include/trace/events/huge_memory.h
+F:   mm/bpf_thp.c
 F:   mm/huge_memory.c
 F:   mm/khugepaged.c
 F:   mm/mm_slot.h
@@ -16246,6 +16247,15 @@ 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

MAINTAINERS F: tools/testing/selftests/mm/khugepaged.c · line 16258

Quoted 10 lines
MAINTAINERS +9 -0
 
+MEMORY MANAGEMENT - THP WITH BPF SUPPORT
+M:   Yafang Shao <[email protected]>
+L:   [email protected]
+L:   [email protected]
+S:   Maintained
+F:   mm/bpf_thp.c
+F:   tools/testing/selftests/bpf/prog_tests/thp_adjust.c
+F:   tools/testing/selftests/bpf/progs/test_thp_adjust*
+

Sorry but I don't agree with a separate section for this.

Quoted 2 lines

This should form part of the THP section only, I don't think it's warranted to do elsewise.

I initially added it as a separate entry to ensure that [email protected] would be CCed. However, I discovered that any file containing “bpf” will automatically CC that list. Therefore, it’s fine to include this under the THP entry instead.

Signature · 2 lines
Regards
Yafang
#53 lore raw
In reply to Yafang Shao
Quoted 18 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.

Quoted 6 lines

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 [0], this mechanism can also serve as a policy prototyping tool (test policies via BPF before upstreaming them).

There is a lot going on and most reviewers (including me) are fairly busy right now, so getting more detailed review could take a while.

This topic sounds like a good candidate for the bi-weekly MM alignment session.

Would you be interested in presenting the current bpf interface, how to use it, drawbacks, todos, ... in that forum?

David Rientjes, who organizes this meeting, is already on Cc.

Signature · 3 lines
Cheers

David / dhildenb
#54 lore raw
Quoted 29 lines · David Hildenbrand
Quoted 18 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.

Quoted 6 lines

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 [0], this mechanism can also serve as a policy prototyping tool (test policies via BPF before upstreaming them).

There is a lot going on and most reviewers (including me) are fairly busy right now, so getting more detailed review could take a while.

Quoted 7 lines

This topic sounds like a good candidate for the bi-weekly MM alignment session.

Would you be interested in presenting the current bpf interface, how to use it, drawbacks, todos, ... in that forum?

David Rientjes, who organizes this meeting, is already on Cc.

If we do this, would like an invite to it also!

Have been meaning to take a look into this in detail while in RFC but more so now obviously :) as discussed in THP cabal, I am broadly in favour of this as long we get the interface right.

Anyway let me have a look through...!

Cheers, Lorenzo

#55 lore raw
Quoted 39 lines · Lorenzo Stoakes
Quoted 29 lines · David Hildenbrand
Quoted 18 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.

Quoted 6 lines

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 [0], this mechanism can also serve as a policy prototyping tool (test policies via BPF before upstreaming them).

There is a lot going on and most reviewers (including me) are fairly busy right now, so getting more detailed review could take a while.

Quoted 7 lines

This topic sounds like a good candidate for the bi-weekly MM alignment session.

Would you be interested in presenting the current bpf interface, how to use it, drawbacks, todos, ... in that forum?

David Rientjes, who organizes this meeting, is already on Cc.

Quoted 7 lines

If we do this, would like an invite to it also!

Have been meaning to take a look into this in detail while in RFC but more so now obviously :) as discussed in THP cabal, I am broadly in favour of this as long we get the interface right.

Anyway let me have a look through...!

Thanks in advance.

Signature · 2 lines
Regards
Yafang
#56 lore raw
In reply to David Hildenbrand
Quoted 26 lines · David Hildenbrand
Quoted 18 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.

Quoted 6 lines

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 [0], this mechanism can also serve as a policy prototyping tool (test policies via BPF before upstreaming them).

Quoted 6 lines

There is a lot going on and most reviewers (including me) are fairly busy right now, so getting more detailed review could take a while.

This topic sounds like a good candidate for the bi-weekly MM alignment session.

Would you be interested in presenting the current bpf interface, how to use it,  drawbacks, todos, ... in that forum?

Could I get an invite please? Thanks!

Usama

Quoted 2 lines

David Rientjes, who organizes this meeting, is already on Cc.

#57 lore raw
Quoted 30 lines · Usama Arif
Quoted 26 lines · David Hildenbrand
Quoted 24 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 [0], this mechanism can also serve as a policy prototyping tool (test policies via BPF before upstreaming them).

Quoted 8 lines
Quoted 6 lines

There is a lot going on and most reviewers (including me) are fairly busy right now, so getting more detailed review could take a while.

This topic sounds like a good candidate for the bi-weekly MM alignment session.

Would you be interested in presenting the current bpf interface, how to use it, drawbacks, todos, ... in that forum?

Could I get an invite please? Thanks!

IIUC, a Google Meet link will be shared publicly on the upstream mailing list, allowing any interested developers to join. If the session is invite-only instead, I will make sure you receive an invitation, given the significant help you've already provided for this series.

Signature · 2 lines
Regards
Yafang
#58 lore raw
In reply to David Hildenbrand
Quoted 27 lines · David Hildenbrand
Quoted 18 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.

Quoted 6 lines

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 [0], this mechanism can also serve as a policy prototyping tool (test policies via BPF before upstreaming them).

Quoted 8 lines

There is a lot going on and most reviewers (including me) are fairly busy right now, so getting more detailed review could take a while.

This topic sounds like a good candidate for the bi-weekly MM alignment session.

Would you be interested in presenting the current bpf interface, how to use it, drawbacks, todos, ... in that forum?

Sure.

Quoted 2 lines

David Rientjes, who organizes this meeting, is already on Cc.

DavidR had previously reached out to me about this patchset.

Hello DavidR,

Would September 17 from 9:00–10:00 AM PDT (UTC-7) work for discussing this topic? If that time isn’t convenient, I’m happy to schedule a later session—this will also give me some time to prepare a brief slide.

On a related note, I’d like to take this opportunity to also share a short proposal on BPF-based NUMA balancing.

On our AMD EPYC servers, many services experience significant performance degradation due to cross-NUMA access. While NUMA balancing can help mitigate this, its current global enable/disable implementation often leads to overall system performance regression. We are exploring the use of BPF to selectively enable NUMA balancing only for NUMA-sensitive services, thereby minimizing unintended side effects. A similar approach has been proposed in [0] using prctl() or a cgroup interface. We believe this use case is particularly well-suited for a BPF-based solution, and I’ll briefly outline why in the slide. I’ve included the developers from [0] in CC for visibility, in case they are interested in joining the discussion.

Looking forward to your thoughts.

[0]. https://lore.kernel.org/lkml/[email protected]/

Signature · 2 lines
Regards
Yafang
#59 lore raw
In reply to Yafang Shao
Quoted 18 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.

Quoted 6 lines

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 [0], this mechanism can also serve as a policy prototyping tool (test policies via BPF before upstreaming them).

I think it's important to highlight here that we are exploring an _experimental_ implementation.

Quoted 5 lines

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

As suggested by David [0], we introduce a new BPF interface:

I do agree, to be clear, with this broad approach - that is, to provide the minimum information that a reasonable decision can be made upon and to keep things as simple as we can.

As per the THP cabal (I think? :) the general consensus was in line with this.

Quoted 8 lines

/**
* @get_suggested_order: Get the suggested THP orders for allocation
* @mm: mm_struct associated with the THP allocation
* @vma__nullable: vm_area_struct associated with the THP allocation (may be NULL)

 *                 When NULL, the decision should be based on @mm (i.e., when
 *                 triggered from an mm-scope hook rather than a VMA-specific
 *                 context).

I'm a little wary of handing a VMA to BPF, under what locking would it be provided?

Quoted 2 lines

* Must belong to @mm (guaranteed by the caller).
* @vma_flags: use these vm_flags instead of @vma->vm_flags (0 if @vma is NULL)

Hmm this one is also a bit odd - why would these flags differ? Note that I will be changing the VMA flags to a bitmap relatively soon which may be larger than the system word size.

So 'handing around all the flags' is something we probably want to avoid.

For the f_op->mmap_prepare stuff I provided an abstraction

Quoted 6 lines

* @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Rerurn: Bitmask of suggested THP orders for allocation. The highest

Obv. a cover letter thing but typo her :P rerurn -> return.

Quoted 2 lines

* suggested order will not exceed the highest requested order
* in @orders.

In what sense are they 'suggested'? Is this a product of sysfs settings or? I think this needs to be clearer.

Quoted 3 lines

*/
int (*get_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,

                            u64 vma_flags, enum tva_type tva_flags, int orders) __rcu;

Also here in what sense is this suggested? :)

Quoted 4 lines

This interface:
- Supports both use cases (per-workload tuning + policy prototyping).
- Can be extended with BPF helpers (e.g., for memory pressure awareness).

Hm how would extensions like this work?

Quoted 3 lines

This is an experimental feature. To use it, you must enable CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION.

Yes! Thanks. I am glad we are putting this behind a config flag.

Quoted 26 lines

Warning:
- The interface may change
- Behavior may differ in future kernel versions
- We might remove it in the future

Selftests
=========

BPF selftests
-------------

Patch #5: Implements a basic BPF THP policy that restricts THP allocation
via khugepaged to tasks within a specified memory cgroup.
Patch #6: Contains test cases validating the khugepaged fork behavior. Patch #7: Provides tests for dynamic BPF program updates and replacement. Patch #8: 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:
mm: add bitmap mm->flags field
mm/huge_memory: convert "tva_flags" to "enum tva_type"
mm: convert core mm to mm_flags_*() accessors

Quoted 3 lines

To enable BPF CI testing, these dependencies were manually applied to bpf-next [1]. All selftests in this series pass successfully. The observed CI failures are unrelated to these changes.

Cool, glad at least my mm changes were ok :)

Quoted 17 lines

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

As suggested by Usama [2], 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 and standard deviations.

The results across three configurations show negligible performance impact:
- Baseline (without this patch series)
- With patch series but no BPF program attached
- With patch series and BPF program attached

Quoted 5 lines

The result are as follows,

Number of runs: 10,000
Average throughput: 40-41 GB/sec
Standard deviation: 7-8 GB/sec

You're not giving data comparing the 3? Could you do so? Thanks.

Quoted 10 lines

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 [3]) while disabling it for others. This selective deployment has operated flawlessly, with no regression reports to date.

Quoted 4 lines

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

Obviously it's _really key_ to point out that this feature is intendend to be _absolutely_ ephemeral - we may or may not implement something like this
- it's really about both exploring how such an interface might look and also helping to determine how an 'automagic' future might look.

Quoted 24 lines

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

Based on our validation with production workloads, we observed mixed results with XFS large folios (also known as File 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 THP allocation policies.

Quoted 9 lines

Changes:
=======

RFC v5-> v6:
- Code improvement around the RCU usage (Usama)
- Add selftests for khugepaged fork (Usama)
- Add performance data for page fault (Usama)
- Remove the RFC tag

Sorry I haven't been involved in the RFC reviews, always intended to but workload etc.

Will be looking through this series as very interested in exploring this approach.

Cheers, Lorenzo

Quoted 65 lines

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: add support for BPF based THP order selection
mm: thp: add a new kfunc bpf_mm_get_mem_cgroup()
mm: thp: add a new kfunc bpf_mm_get_task()
bpf: mark vma->vm_mm as trusted
selftests/bpf: add a simple BPF based THP policy selftests/bpf: add test case for khugepaged fork selftests/bpf: add test case to update thp policy selftests/bpf: add test cases for invalid thp_adjust usage
Documentation: add BPF-based THP adjustment documentation
MAINTAINERS: add entry for BPF-based THP adjustment

 Documentation/admin-guide/mm/transhuge.rst    |  47 +++
 MAINTAINERS                                   |  10 +
 include/linux/huge_mm.h                       |  15 +
 include/linux/khugepaged.h                    |  12 +-
 kernel/bpf/verifier.c                         |   5 +
 mm/Kconfig                                    |  12 +
 mm/Makefile                                   |   1 +
 mm/bpf_thp.c                                  | 269 ++++++++++++++
 mm/huge_memory.c                              |  10 +
 mm/khugepaged.c                               |  26 +-
 mm/memory.c                                   |  18 +-
 tools/testing/selftests/bpf/config            |   3 +
 .../selftests/bpf/prog_tests/thp_adjust.c     | 343 ++++++++++++++++++
 .../selftests/bpf/progs/test_thp_adjust.c     | 115 ++++++
 .../bpf/progs/test_thp_adjust_trusted_vma.c   |  27 ++
 .../progs/test_thp_adjust_unreleased_memcg.c  |  24 ++
 .../progs/test_thp_adjust_unreleased_task.c   |  25 ++
 17 files changed, 955 insertions(+), 7 deletions(-)
 create mode 100644 mm/bpf_thp.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_trusted_vma.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_memcg.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_task.c

--
2.47.3

#60 lore raw
Quoted 20 lines · Lorenzo Stoakes
Quoted 18 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.

Quoted 7 lines
Quoted 6 lines

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 [0], this mechanism can also serve as a policy prototyping tool (test policies via BPF before upstreaming them).

Thank you for providing so many comments. I'll take some time to go through it carefully and will reply afterward.

Quoted 2 lines

I think it's important to highlight here that we are exploring an _experimental_ implementation.

I will add it.

Quoted 5 lines
Quoted 5 lines

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

Quoted 8 lines
Quoted 1 line

As suggested by David [0], we introduce a new BPF interface:

I do agree, to be clear, with this broad approach - that is, to provide the minimum information that a reasonable decision can be made upon and to keep things as simple as we can.

As per the THP cabal (I think? :) the general consensus was in line with this.

My testing in both test and production indicates the following parameters are essential:
- mm_struct (associated with the THP allocation)
- vma_flags (VM_HUGEPAGE, VM_NOHUGEPAGE, or N/A)
- tva_type
- The requested THP orders bitmask

I will retain these four and remove @vma__nullable.

Quoted 11 lines
Quoted 10 lines

/**
* @get_suggested_order: Get the suggested THP orders for allocation
* @mm: mm_struct associated with the THP allocation
* @vma__nullable: vm_area_struct associated with the THP allocation (may be NULL)

 *                 When NULL, the decision should be based on @mm (i.e., when
 *                 triggered from an mm-scope hook rather than a VMA-specific
 *                 context).
Quoted 2 lines

I'm a little wary of handing a VMA to BPF, under what locking would it be provided?

We cannot arbitrarily use members of the struct vm_area_struct because they are untrusted pointers. The only trusted pointer is vma->vm_mm, which can be accessed without holding any additional locks. For the VMA itself, the caller at the callsite has already taken the necessary locks, so we do not need to acquire them again.

My testing shows the @vma parameter is not needed. I will remove it in the next update.

Quoted 9 lines
Quoted 3 lines

* Must belong to @mm (guaranteed by the caller).
* @vma_flags: use these vm_flags instead of @vma->vm_flags (0 if @vma is NULL)

Hmm this one is also a bit odd - why would these flags differ? Note that I will be changing the VMA flags to a bitmap relatively soon which may be larger than the system word size.

So 'handing around all the flags' is something we probably want to avoid.

Good suggestion. Since we specifically need to identify VM_HUGEPAGE or VM_NOHUGEPAGE, I will add a new enum for clarity, bpf_thp_vma_type:

+enum bpf_thp_vma_type {

+       BPF_VM_NONE = 0,
+       BPF_VM_HUGEPAGE,        /* VM_HUGEPAGE */
+       BPF_VM_NOHUGEPAGE,      /* VM_NOHUGEPAGE */

+};

The enum can be extended in the future to support file-backed THP by adding new types.

Quoted 11 lines

For the f_op->mmap_prepare stuff I provided an abstraction

Quoted 6 lines

* @tva_flags: TVA flags for current @vma (-1 if @vma is NULL)
* @orders: Bitmask of requested THP orders for this allocation
* - PMD-mapped allocation if PMD_ORDER is set
* - mTHP allocation otherwise
*
* Rerurn: Bitmask of suggested THP orders for allocation. The highest

Obv. a cover letter thing but typo her :P rerurn -> return.

will change it.

Quoted 6 lines
Quoted 3 lines

* suggested order will not exceed the highest requested order
* in @orders.

In what sense are they 'suggested'? Is this a product of sysfs settings or? I think this needs to be clearer.

The order is suggested by a BPF program. I will clarify it in the next version.

Quoted 6 lines
Quoted 4 lines

*/
int (*get_suggested_order)(struct mm_struct *mm, struct vm_area_struct *vma__nullable,

                            u64 vma_flags, enum tva_type tva_flags, int orders) __rcu;

Also here in what sense is this suggested? :)

Agreed. I'll rename it to bpf_hook_thp_get_order() as suggested for clarity.

Quoted 7 lines
Quoted 5 lines

This interface:
- Supports both use cases (per-workload tuning + policy prototyping).
- Can be extended with BPF helpers (e.g., for memory pressure awareness).

Hm how would extensions like this work?

To optimize THP allocation, we should consult the PSI data beforehand. If memory pressure is already high—indicating difficulty in allocating high-order pages—the system should default to allocating 4K pages instead. This could be implemented by checking the PSI data of the relevant cgroup:

  struct cgroup *cgrp = task_dfl_cgroup(mm->owner);
  struct psi_group *psi = cgroup_psi(cgrp);  // or psi_system
  u64 psi_data = psi->total[PSI_AVGS][PSI_MEM];

The allocation strategy would then branch based on the value of psi_data. This may require new BPF helpers to access PSI data efficiently.

Quoted 56 lines
Quoted 4 lines

This is an experimental feature. To use it, you must enable CONFIG_EXPERIMENTAL_BPF_ORDER_SELECTION.

Yes! Thanks. I am glad we are putting this behind a config flag.

Quoted 26 lines

Warning:
- The interface may change
- Behavior may differ in future kernel versions
- We might remove it in the future

Selftests
=========

BPF selftests
-------------

Patch #5: Implements a basic BPF THP policy that restricts THP allocation
via khugepaged to tasks within a specified memory cgroup.
Patch #6: Contains test cases validating the khugepaged fork behavior. Patch #7: Provides tests for dynamic BPF program updates and replacement. Patch #8: 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:
mm: add bitmap mm->flags field
mm/huge_memory: convert "tva_flags" to "enum tva_type"
mm: convert core mm to mm_flags_*() accessors

Quoted 3 lines

To enable BPF CI testing, these dependencies were manually applied to bpf-next [1]. All selftests in this series pass successfully. The observed CI failures are unrelated to these changes.

Cool, glad at least my mm changes were ok :)

Quoted 17 lines

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

As suggested by Usama [2], 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 and standard deviations.

The results across three configurations show negligible performance impact:
- Baseline (without this patch series)
- With patch series but no BPF program attached
- With patch series and BPF program attached

Quoted 7 lines
Quoted 5 lines

The result are as follows,

Number of runs: 10,000
Average throughput: 40-41 GB/sec
Standard deviation: 7-8 GB/sec

You're not giving data comparing the 3? Could you do so? Thanks.

I tested all three cases. The results from the three test cases were similar, so I aggregated the data.

Quoted 16 lines
Quoted 11 lines

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 [3]) while disabling it for others. This selective deployment has operated flawlessly, with no regression reports to date.

Quoted 4 lines

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

Quoted 4 lines

Obviously it's _really key_ to point out that this feature is intendend to be _absolutely_ ephemeral - we may or may not implement something like this
- it's really about both exploring how such an interface might look and also helping to determine how an 'automagic' future might look.

Our users can benefit from this feature, which is why we have already deployed it on our production servers. We are now extending it to more workloads, such as RDMA applications, where THP provides significant performance gains. Given the complexity of our production environment, we have found that manual control is a necessary practice. I am presenting this case solely to demonstrate the feature's stability and that it does not introduce regressions. However, I understand this use case is not recommended by the maintainers and will clarify this in the next version.

Quoted 35 lines
Quoted 25 lines

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

Based on our validation with production workloads, we observed mixed results with XFS large folios (also known as File 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 THP allocation policies.

Quoted 9 lines

Changes:
=======

RFC v5-> v6:
- Code improvement around the RCU usage (Usama)
- Add selftests for khugepaged fork (Usama)
- Add performance data for page fault (Usama)
- Remove the RFC tag

Quoted 5 lines

Sorry I haven't been involved in the RFC reviews, always intended to but workload etc.

Will be looking through this series as very interested in exploring this approach.

Thanks a lot for your reviews.

Signature · 2 lines
Regards
Yafang