mm, bpf: BPF based THP adjustment
Subsystems (5)
v3 cover mm, bpf: BPF based THP adjustment
Background
----------
We have consistently configured THP to "never" on our production servers due to past incidents caused by its behavior:
- Increased memory consumption
THP significantly raises overall memory usage.
- Latency spikes
Random latency spikes occur due to more frequent memory compaction activity triggered by THP.
- Lack of Fine-Grained Control
THP tuning knobs are globally configured, making them unsuitable for containerized environments. When different workloads run on the same host, enabling THP globally (without per-workload control) can cause unpredictable behavior.
Due to these issues, system administrators remain hesitant to switch to "madvise" or "always" modes—unless finer-grained control over THP behavior is implemented.
New Motivation
--------------
We have now identified that certain AI workloads achieve substantial performance gains with THP enabled. However, we’ve also verified that some workloads see little to no benefit—or are even negatively impacted—by THP.
In our Kubernetes environment, we deploy mixed workloads on a single server to maximize resource utilization. Our goal is to selectively enable THP for services that benefit from it while keeping it disabled for others. This approach allows us to incrementally enable THP for additional services and assess how to make it more viable in production.
Proposed Solution
-----------------
To enable fine-grained control over THP behavior, we propose dynamically adjusting THP policies using BPF. This approach allows per-workload THP tuning, providing greater flexibility and precision.
The BPF-based THP adjustment mechanism introduces two new APIs for granular policy control:
- THP allocator
int (*allocator)(unsigned long vm_flags, unsigned long tva_flags);
The BPF program returns either THP_ALLOC_CURRENT or THP_ALLOC_KHUGEPAGED, indicating whether THP allocation should be performed synchronously (current task) or asynchronously (khugepaged).
The decision is based on the current task context, VMA flags, and TVA flags.
- THP reclaimer
int (*reclaimer)(bool vma_madvised);
The BPF program returns either RECLAIMER_CURRENT or RECLAIMER_KSWAPD, determining whether memory reclamation is handled by the current task or kswapd.
We may explore implementing fine-grained tuning for khugepaged in future iterations.
Alternative Proposals
---------------------
- Gutierrez’s cgroup-based approach [1]
- Proposed adding a new cgroup file to control THP policy.
- However, as Johannes noted, cgroups are designed for hierarchical resource allocation, not arbitrary policy settings [2].
- Usama’s per-task THP proposal based on prctl() [3]:
- Enabling THP per task via prctl().
- This provides an alternative approach for per-workload THP tuning, though it lacks dynamic policy adjustment capabilities and thus offers limited flexibility.
This is currently a PoC implementation with limited test. Feedback of any kind is welcome.
RFC v2->v3:
Thanks to the valuable input from David and Lorenzo:
- Finer-graind tuning based on madvise or always mode
- Use BPF to write more advanced policies / allocation logic
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 (5):
mm, thp: use __thp_vma_allowable_orders() in khugepaged_enter_vma() mm, thp: add bpf thp hook to determine thp allocator
mm, thp: add bpf thp hook to determine thp reclaimer
mm: thp: add bpf thp struct ops
selftests/bpf: Add selftest for THP adjustment
include/linux/huge_mm.h | 8 + mm/Makefile | 3 + mm/bpf_thp.c | 184 ++++++++++++++++++ mm/huge_memory.c | 5 + mm/khugepaged.c | 6 +- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/thp_adjust.c | 158 +++++++++++++++ .../selftests/bpf/progs/test_thp_adjust.c | 38 ++++ 8 files changed, 401 insertions(+), 2 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
Signature · 1 line
2.43.5
RFC PATCH v3 1/5 mm, thp: use __thp_vma_allowable_orders() in khugepaged_enter_vma()
The order has already been validated in hugepage_pmd_enabled(), so there's no need to recheck it in thp_vma_allowable_orders().
--- mm/khugepaged.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/khugepaged.c +2 -2
@@ -474,8 +474,8 @@ void khugepaged_enter_vma(struct vm_area_struct *vma, {if (!test_bit (MMF_VM_HUGEPAGE , &vma->vm_mm->flags) &&hugepage_pmd_enabled ()) { -if (thp_vma_allowable_order (vma, vm_flags,TVA_ENFORCE_SYSFS , -PMD_ORDER )) +if (__thp_vma_allowable_orders (vma, vm_flags,TVA_ENFORCE_SYSFS , +BIT (PMD_ORDER )))__khugepaged_enter (vma->vm_mm); } }
Signature · 1 line
2.43.5
Re: [RFC PATCH v3 1/5] mm, thp: use __thp_vma_allowable_orders() in khugepaged_enter_vma()
Quoted 3 lines · Yafang Shao
The order has already been validated in
hugepage_pmd_enabled(), so there's no need to recheck it inthp_vma_allowable_orders().
The checks are not equivalent.
hugepage_pmd_enabled just checks if the sysfs entries allow hugification. thp_vma_allowable_orders modifies the orders that can be used based on vm_flags, which is not done in hugepage_pmd_enabled.
Quoted 20 lines
Signed-off-by Yafang Shao <[email protected]>--- mm/khugepaged.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mm/khugepaged.c +2
-2@@ -474,8 +474,8 @@ void khugepaged_enter_vma(struct vm_area_struct *vma, {if (!test_bit (MMF_VM_HUGEPAGE , &vma->vm_mm->flags) &&hugepage_pmd_enabled ()) { -if (thp_vma_allowable_order (vma, vm_flags,TVA_ENFORCE_SYSFS , -PMD_ORDER )) +if (__thp_vma_allowable_orders (vma, vm_flags,TVA_ENFORCE_SYSFS , +BIT (PMD_ORDER )))__khugepaged_enter (vma->vm_mm); } }
Re: [RFC PATCH v3 1/5] mm, thp: use __thp_vma_allowable_orders() in khugepaged_enter_vma()
Quoted 9 lines · Usama Arif
Quoted 3 lines · Yafang Shao
The order has already been validated in
hugepage_pmd_enabled(), so there's no need to recheck it inthp_vma_allowable_orders().
Quoted 5 lines
The checks are not equivalent.
hugepage_pmd_enabledjust checks if the sysfs entries allow hugification.thp_vma_allowable_ordersmodifies the orders that can be used based onvm_flags, which is not done inhugepage_pmd_enabled.
Thank you for catching that. I overlooked the vm_flags check.
Signature · 2 lines
Regards Yafang
RFC PATCH v3 2/5 mm, thp: add bpf thp hook to determine thp allocator
In reply to Yafang ShaoA new hook bpf_thp_allocator() is added to determine if the THP is allocated by khugepaged or by the current task.
--- include/linux/huge_mm.h | 10 ++++++++++ mm/khugepaged.c | 2 ++ 2 files changed, 12 insertions(+)
include/linux/huge_mm.h +10 -0
@@ -190,6 +190,14 @@ static inline bool hugepage_global_always(void) (1 <<TRANSPARENT_HUGEPAGE_FLAG ); } +#define THP_ALLOC_KHUGEPAGED (1 << 1) +#define THP_ALLOC_CURRENT (1 << 2) +static inline int bpf_thp_allocator (unsigned long vm_flags, +unsigned long tva_flags) +{ +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; +} +static inline int highest_order (unsigned long orders) {return fls_long (orders) -1 ;
@@ -290,6 +298,8 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,if ((tva_flags &TVA_ENFORCE_SYSFS ) &&vma_is_anonymous (vma)) {unsigned long mask =READ_ONCE (huge_anon_orders_always); +if (!(bpf_thp_allocator (vm_flags, tva_flags) &THP_ALLOC_CURRENT )) +return 0 ;if (vm_flags &VM_HUGEPAGE ) mask |=READ_ONCE (huge_anon_orders_madvise); if (hugepage_global_always() ||
mm/khugepaged.c +2 -0
@@ -472,6 +472,8 @@ void __khugepaged_enter(struct mm_struct *mm)void khugepaged_enter_vma (struct vm_area_struct *vma,unsigned long vm_flags) { +if (!(bpf_thp_allocator (vm_flags,0 ) &THP_ALLOC_KHUGEPAGED )) +return ;if (!test_bit (MMF_VM_HUGEPAGE , &vma->vm_mm->flags) &&hugepage_pmd_enabled ()) { if (__thp_vma_allowable_orders(vma, vm_flags, TVA_ENFORCE_SYSFS,
Signature · 1 line
2.43.5
Re: [RFC PATCH v3 2/5] mm, thp: add bpf thp hook to determine thp allocator
Quoted 2 lines · Yafang Shao
A new hook
bpf_thp_allocator()is added to determine if the THP is allocated by khugepaged or by the current task.
I would add in the summary why we need this. I am assuming I will find out when reviewing the next few patches, but would be good to know here.
Quoted 12 lines
Signed-off-by Yafang Shao <[email protected]>--- include/linux/huge_mm.h | 10 ++++++++++ mm/khugepaged.c | 2 ++ 2 files changed, 12 insertions(+)include/linux/huge_mm.h +0
-0
include/linux/huge_mm.h static inline bool hugepage_global_always(void) · line 198
Quoted 9 lines
include/linux/huge_mm.h +6
-0@@ -190,6 +190,14 @@ static inline bool hugepage_global_always(void) (1 <<TRANSPARENT_HUGEPAGE_FLAG ); } +#define THP_ALLOC_KHUGEPAGED (1 << 1) +#define THP_ALLOC_CURRENT (1 << 2) +static inline int bpf_thp_allocator (unsigned long vm_flags, +unsigned long tva_flags) +{ +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ;
You dont use either vm_flags or tva_flags in this function? I am guessing you wanted to check if these bits are set here?
But you dont seem to be setting these flags anywhere? I am guessing its in a future patch. If it is, I would move the setting of these bits here as its confusing to only see the check without knowing where its
I feel this patch is broken and needs to be rewritten.
include/linux/huge_mm.h unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, · line 302
Quoted 11 lines
include/linux/huge_mm.h +4
-0+} +static inline int highest_order (unsigned long orders) {return fls_long (orders) -1 ;@@ -290,6 +298,8 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,if ((tva_flags &TVA_ENFORCE_SYSFS ) &&vma_is_anonymous (vma)) {unsigned long mask =READ_ONCE (huge_anon_orders_always); +if (!(bpf_thp_allocator (vm_flags, tva_flags) &THP_ALLOC_CURRENT )) +return 0 ;
I am assuming that this is the point to check for allocation, but thp_vma_allowable_orders is not just used for allocation, its used for in other places as well, like hugepage_vma_revalidate and swap.
Quoted 16 lines
include/linux/huge_mm.h +0
-0if (vm_flags &VM_HUGEPAGE ) mask |=READ_ONCE (huge_anon_orders_madvise); if (hugepage_global_always() ||mm/khugepaged.c +2
-0@@ -472,6 +472,8 @@ void __khugepaged_enter(struct mm_struct *mm)void khugepaged_enter_vma (struct vm_area_struct *vma,unsigned long vm_flags) { +if (!(bpf_thp_allocator (vm_flags,0 ) &THP_ALLOC_KHUGEPAGED )) +return ;if (!test_bit (MMF_VM_HUGEPAGE , &vma->vm_mm->flags) &&hugepage_pmd_enabled ()) { if (__thp_vma_allowable_orders(vma, vm_flags, TVA_ENFORCE_SYSFS,
Re: [RFC PATCH v3 2/5] mm, thp: add bpf thp hook to determine thp allocator
Quoted 36 lines · Usama Arif
Quoted 2 lines · Yafang Shao
A new hook
bpf_thp_allocator()is added to determine if the THP is allocated by khugepaged or by the current task.I would add in the summary why we need this. I am assuming I will find out when reviewing the next few patches, but would be good to know here.
Quoted 12 lines
Signed-off-by Yafang Shao <[email protected]>--- include/linux/huge_mm.h | 10 ++++++++++ mm/khugepaged.c | 2 ++ 2 files changed, 12 insertions(+)include/linux/huge_mm.h +0
-0include/linux/huge_mm.h static inline bool hugepage_global_always(void) · line 198
Quoted 9 lines
include/linux/huge_mm.h +6
-0@@ -190,6 +190,14 @@ static inline bool hugepage_global_always(void) (1 <<TRANSPARENT_HUGEPAGE_FLAG ); } +#define THP_ALLOC_KHUGEPAGED (1 << 1) +#define THP_ALLOC_CURRENT (1 << 2) +static inline int bpf_thp_allocator (unsigned long vm_flags, +unsigned long tva_flags) +{ +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ;You dont use either
vm_flagsortva_flagsin this function? I am guessing you wanted to check if these bits are set here?
Quoted 5 lines
But you dont seem to be setting these flags anywhere? I am guessing its in a future patch. If it is, I would move the setting of these bits here as its confusing to only see the check without knowing where its
I feel this patch is broken and needs to be rewritten.
The bpf_thp_allocator() function serves as a placeholder that will be implemented by a BPF program. The BPF implementation will use the @vm_flags and @tva_flags parameters to determine whether a task qualifies for THP allocation.
I see how this could be confusing.
Quoted 9 lines
Quoted 9 lines
include/linux/huge_mm.h +2
-0+} +static inline int highest_order (unsigned long orders) {return fls_long (orders) -1 ;@@ -290,6 +298,8 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,if ((tva_flags &TVA_ENFORCE_SYSFS ) &&vma_is_anonymous (vma)) {unsigned long mask =READ_ONCE (huge_anon_orders_always);
Quoted 6 lines
include/linux/huge_mm.h unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, · line 302
Quoted 2 lines
include/linux/huge_mm.h +2
-0+if (!(bpf_thp_allocator (vm_flags, tva_flags) &THP_ALLOC_CURRENT )) +return 0 ;I am assuming that this is the point to check for allocation, but
thp_vma_allowable_ordersis not just used for allocation, its used for in other places as well, likehugepage_vma_revalidateand swap.
Agreed, some adjustments are necessary.
Signature · 2 lines
Regards Yafang
RFC PATCH v3 3/5 mm, thp: add bpf thp hook to determine thp reclaimer
In reply to Yafang ShaoA new hook, bpf_thp_gfp_mask(), is introduced to determine whether memory reclamation is being performed by the current task or by kswapd.
--- include/linux/huge_mm.h | 5 +++++ mm/huge_memory.c | 5 +++++ 2 files changed, 10 insertions(+)
include/linux/huge_mm.h +5 -0
@@ -198,6 +198,11 @@ static inline int bpf_thp_allocator(unsigned long vm_flags,return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; } +static inline gfp_t bpf_thp_gfp_mask (bool vma_madvised) +{ +return 0 ; +} +static inline int highest_order (unsigned long orders) {return fls_long (orders) -1 ;
mm/huge_memory.c +5 -0
@@ -1280,6 +1280,11 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf)gfp_t vma_thp_gfp_mask (struct vm_area_struct *vma) {const bool vma_madvised = vma && (vma->vm_flags &VM_HUGEPAGE ); +gfp_t gfp_mask; + + gfp_mask =bpf_thp_gfp_mask (vma_madvised); +if (gfp_mask) +return gfp_mask;/* Always do synchronous compaction */ if (test_bit (TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG , &transparent_hugepage_flags))
Signature · 1 line
2.43.5
Re: [RFC PATCH v3 3/5] mm, thp: add bpf thp hook to determine thp reclaimer
Quoted 30 lines · Yafang Shao
A new hook,
bpf_thp_gfp_mask(), is introduced to determine whether memory reclamation is being performed by the current task or by kswapd.Signed-off-by Yafang Shao <[email protected]>--- include/linux/huge_mm.h | 5 +++++ mm/huge_memory.c | 5 +++++ 2 files changed, 10 insertions(+)include/linux/huge_mm.h +5
-0@@ -198,6 +198,11 @@ static inline int bpf_thp_allocator(unsigned long vm_flags,return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; } +static inline gfp_t bpf_thp_gfp_mask (bool vma_madvised) +{ +return 0 ; +} +static inline int highest_order (unsigned long orders) {return fls_long (orders) -1 ;mm/huge_memory.c +0
-0
mm/huge_memory.c static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1285
Quoted 6 lines
mm/huge_memory.c +3
-0@@ -1280,6 +1280,11 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf)gfp_t vma_thp_gfp_mask (struct vm_area_struct *vma) {const bool vma_madvised = vma && (vma->vm_flags &VM_HUGEPAGE ); +gfp_t gfp_mask; + + gfp_mask =bpf_thp_gfp_mask (vma_madvised);
I am guessing bpf_thp_gfp_mask returns 0, as its something yet to be implemented, but I really dont understand what this patch is supposed to do.
Quoted 5 lines
mm/huge_memory.c +2
-0+if (gfp_mask) +return gfp_mask;/* Always do synchronous compaction */ if (test_bit (TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG , &transparent_hugepage_flags))
Re: [RFC PATCH v3 3/5] mm, thp: add bpf thp hook to determine thp reclaimer
Quoted 36 lines · Usama Arif
Quoted 32 lines · Yafang Shao
A new hook,
bpf_thp_gfp_mask(), is introduced to determine whether memory reclamation is being performed by the current task or by kswapd.Signed-off-by Yafang Shao <[email protected]>--- include/linux/huge_mm.h | 5 +++++ mm/huge_memory.c | 5 +++++ 2 files changed, 10 insertions(+)include/linux/huge_mm.h +5
-0@@ -198,6 +198,11 @@ static inline int bpf_thp_allocator(unsigned long vm_flags,return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; } +static inline gfp_t bpf_thp_gfp_mask (bool vma_madvised) +{ +return 0 ; +} +static inline int highest_order (unsigned long orders) {return fls_long (orders) -1 ;mm/huge_memory.c +0
-0@@ -1280,6 +1280,11 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf)gfp_t vma_thp_gfp_mask (struct vm_area_struct *vma) {
Quoted 8 lines
mm/huge_memory.c static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1285
Quoted 4 lines
mm/huge_memory.c +3
-0const bool vma_madvised = vma && (vma->vm_flags &VM_HUGEPAGE ); +gfp_t gfp_mask; + + gfp_mask =bpf_thp_gfp_mask (vma_madvised);I am guessing
bpf_thp_gfp_maskreturns 0, as its something yet to be implemented, but I really dont understand what this patch is supposed to do.
This change only introduces a placeholder for the BPF program. It might be cleaner to squash it into patch #4.
Signature · 2 lines
Regards Yafang
RFC PATCH v3 4/5 mm: thp: add bpf thp struct ops
In reply to Yafang ShaoA new bpf_thp struct ops is introduced to provide finer-grained control over THP allocation policy. The struct ops includes two APIs for determining the THP allocator and reclaimer behavior:
- THP allocator
int (*allocator)(unsigned long vm_flags, unsigned long tva_flags);
The BPF program returns either THP_ALLOC_CURRENT or THP_ALLOC_KHUGEPAGED, indicating whether THP allocation should be performed synchronously (current task) or asynchronously (khugepaged).
The decision is based on the current task context, VMA flags, and TVA flags.
- THP reclaimer
int (*reclaimer)(bool vma_madvised);
The BPF program returns either RECLAIMER_CURRENT or RECLAIMER_KSWAPD, determining whether memory reclamation is handled by the current task or kswapd.
The decision depends on the current task and VMA flags.
--- include/linux/huge_mm.h | 13 +-- mm/Makefile | 3 + mm/bpf_thp.c | 184 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 10 deletions(-) create mode 100644 mm/bpf_thp.c
include/linux/huge_mm.h +3 -10
@@ -54,6 +54,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 ;
@@ -192,16 +193,8 @@ static inline bool hugepage_global_always(void)#define THP_ALLOC_KHUGEPAGED (1 << 1)#define THP_ALLOC_CURRENT (1 << 2) -static inline int bpf_thp_allocator (unsigned long vm_flags, -unsigned long tva_flags) -{ -return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; -} - -static inline gfp_t bpf_thp_gfp_mask (bool vma_madvised) -{ -return 0 ; -} +int bpf_thp_allocator (unsigned long vm_flags,unsigned long tva_flags); +gfp_t bpf_thp_gfp_mask (bool vma_madvised);static inline int highest_order (unsigned long orders) {
mm/Makefile +3 -0
@@ -99,6 +99,9 @@ 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 +ifdef CONFIG_BPF_SYSCALL +obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += bpf_thp.o +endif 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 +184 -0
184 lines not on this page · raw message
Signature · 1 line
2.43.5
kernel test robot
build botRe: [RFC PATCH v3 4/5] mm: thp: add bpf thp struct ops
kernel test robot noticed the following build errors:
Hi Yafang,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/mm-thp-use-__thp_vma_allowable_orders-in-khugepaged_enter_vma/20250608-153842 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20250608073516.22415-5-laoar.shao%40gmail.com patch subject: [RFC PATCH v3 4/5] mm: thp: add bpf thp struct ops config: x86_64-buildonly-randconfig-004-20250608 (https://download.01.org/0day-ci/archive/20250608/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250608/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
ld: mm/memory.o: in function `thp_vma_allowable_orders.constprop.0':
>> memory.c:(.text+0x186b): undefined reference to `bpf_thp_allocator' ld: mm/huge_memory.o: in function `vma_thp_gfp_mask': >> huge_memory.c:(.text+0x46fd): undefined reference to `bpf_thp_gfp_mask' ld: mm/khugepaged.o: in function `thp_vma_allowable_orders.constprop.0': >> khugepaged.c:(.text+0x2a98): undefined reference to `bpf_thp_allocator' ld: mm/khugepaged.o: in function `khugepaged_enter_vma': khugepaged.c:(.text+0x5228): undefined reference to `bpf_thp_allocator' ld: fs/proc/task_mmu.o: in function `show_smap': >> task_mmu.c:(.text+0x2ce8): undefined reference to `bpf_thp_allocator'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [RFC PATCH v3 4/5] mm: thp: add bpf thp struct ops
In reply to Yafang ShaoQuoted 49 lines · Yafang Shao
A new
bpf_thpstruct ops is introduced to provide finer-grained control over THP allocation policy. The struct ops includes two APIs for determining the THP allocator and reclaimer behavior:- THP allocator
int (*allocator)(unsigned long vm_flags, unsigned long tva_flags);The BPF program returns either
THP_ALLOC_CURRENTorTHP_ALLOC_KHUGEPAGED, indicating whether THP allocation should be performed synchronously (current task) or asynchronously (khugepaged).The decision is based on the current task context, VMA flags, and TVA flags.
- THP reclaimer
int (*reclaimer)(bool vma_madvised);The BPF program returns either
RECLAIMER_CURRENTorRECLAIMER_KSWAPD, determining whether memory reclamation is handled by the current task or kswapd.The decision depends on the current task and VMA flags.
Signed-off-by Yafang Shao <[email protected]>--- include/linux/huge_mm.h | 13 +-- mm/Makefile | 3 + mm/bpf_thp.c | 184 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 10 deletions(-) create mode 100644 mm/bpf_thp.cinclude/linux/huge_mm.h +1
-0@@ -54,6 +54,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 ;@@ -192,16 +193,8 @@ static inline bool hugepage_global_always(void)#define THP_ALLOC_KHUGEPAGED (1 << 1)#define THP_ALLOC_CURRENT (1 << 2)
include/linux/huge_mm.h static inline bool hugepage_global_always(void) · line 204
Quoted 10 lines
include/linux/huge_mm.h +0
-10-static inline int bpf_thp_allocator (unsigned long vm_flags, -unsigned long tva_flags) -{ -return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; -} - -static inline gfp_t bpf_thp_gfp_mask (bool vma_madvised) -{ -return 0 ; -}
It makes it quite confusing for review to add code in earlier patches and remove it here.
I dont think you should have had the first 3 patches? and the code is mostly in this patch?
Quoted 72 lines
include/linux/huge_mm.h +2
-0+int bpf_thp_allocator (unsigned long vm_flags,unsigned long tva_flags); +gfp_t bpf_thp_gfp_mask (bool vma_madvised);static inline int highest_order (unsigned long orders) {mm/Makefile +3
-0@@ -99,6 +99,9 @@ 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 +ifdef CONFIG_BPF_SYSCALL +obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += bpf_thp.o +endif obj-$(CONFIG_PAGE_COUNTER) += page_counter.o obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.omm/bpf_thp.c new file +47
-0@@ -0,0 +1,184 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <linux/btf.h> +#include <linux/huge_mm.h> +#include <linux/khugepaged.h> + +#define RECLAIMER_CURRENT (1 << 1) +#define RECLAIMER_KSWAPD (1 << 2) +#define RECLAIMER_BOTH (RECLAIMER_CURRENT | RECLAIMER_KSWAPD) + +struct bpf_thp_ops { +/** +* @allocator: Specifies whether the THP allocation is performed +* by the current task or by khugepaged. +* @vm_flags: Flags for the VMA in the current allocation context +* @tva_flags: Flags for the TVA in the current allocation context +* +* Rerurn: +* - THP_ALLOC_CURRENT: THP was allocated synchronously by the calling +* task's context. +* - THP_ALLOC_KHUGEPAGED: THP was allocated asynchronously by the +* khugepaged kernel thread. +* - 0: THP allocation is disallowed in the current context. +*/ +int (*allocator)(unsigned long vm_flags,unsigned long tva_flags); +/** +* @reclaimer: Specifies the entity performing page reclaim: +* - current task context +* - kswapd +* - none (no reclaim) +* @vma_madvised: MADV flags for this VMA (e.g., MADV_HUGEPAGE, MADV_NOHUGEPAGE) +* +* Return: +* - RECLAIMER_CURRENT: Direct reclaim by the current task if THP +* allocation fails. +* - RECLAIMER_KSWAPD: Wake kswapd to reclaim memory if THP allocation fails. +* - RECLAIMER_ALL: Both current and kswapd will perform the reclaim +* - 0: No reclaim will be attempted. +*/ +int (*reclaimer)(bool vma_madvised); +}; + +static struct bpf_thp_ops bpf_thp; + +int bpf_thp_allocator (unsigned long vm_flags,unsigned long tva_flags) +{
mm/bpf_thp.c line 57
Quoted 10 lines
mm/bpf_thp.c new file +10
-0+int allocator; + +/* No BPF program is attached */ +if (!(transparent_hugepage_flags & (1 <<TRANSPARENT_HUGEPAGE_BPF_ATTACHED ))) +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; + +if (current_is_khugepaged ()) +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; +if (!bpf_thp.allocator) +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ;
Probably make it
if (current_is_khugepaged() || !bpf_thp.allocator)
return THP_ALLOC_KHUGEPAGED | THP_ALLOC_CURRENT;
Quoted 20 lines
mm/bpf_thp.c new file +20
-0+ + allocator = bpf_thp.allocator (vm_flags, tva_flags); +if (!allocator) +return 0 ; +/* invalid return value */ +if (allocator & ~(THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT )) +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; +return allocator; +} + +gfp_t bpf_thp_gfp_mask (bool vma_madvised) +{ +int reclaimer; + +if (!(transparent_hugepage_flags & (1 <<TRANSPARENT_HUGEPAGE_BPF_ATTACHED ))) +return 0 ; + +if (!bpf_thp.reclaimer) +return 0 ; +
mm/bpf_thp.c line 87
Quoted 10 lines
mm/bpf_thp.c new file +10
-0+ reclaimer = bpf_thp.reclaimer (vma_madvised); +switch (reclaimer) { +case RECLAIMER_CURRENT : +return GFP_TRANSHUGE | __GFP_NORETRY; +case RECLAIMER_KSWAPD : +return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM; +case RECLAIMER_BOTH : +return GFP_TRANSHUGE | __GFP_KSWAPD_RECLAIM | __GFP_NORETRY; +default : +return 0 ;
maybe you let the userspace decide GFP flags instead of having RECLAIMER_xyz?
Quoted 97 lines
mm/bpf_thp.c new file +97
-0+ } +} + +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_reg (void *kdata,struct bpf_link *link) +{ +struct bpf_thp_ops *ops = kdata; + +/* TODO: add support for multiple attaches */ +if (test_and_set_bit (TRANSPARENT_HUGEPAGE_BPF_ATTACHED , + &transparent_hugepage_flags)) +return -EOPNOTSUPP ; + bpf_thp.allocator = ops->allocator; + bpf_thp.reclaimer = ops->reclaimer; +return 0 ; +} + +static void bpf_thp_unreg (void *kdata,struct bpf_link *link) +{ +clear_bit (TRANSPARENT_HUGEPAGE_BPF_ATTACHED , &transparent_hugepage_flags); + bpf_thp.allocator =NULL ; + bpf_thp.reclaimer =NULL ; +} + +static int bpf_thp_check_member (const struct btf_type *t, +const struct btf_member *member, +const struct bpf_prog *prog) +{ +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_init (struct btf *btf) +{ +return 0 ; +} + +static int allocator (unsigned long vm_flags,unsigned long tva_flags) +{ +return 0 ; +} + +static int reclaimer (bool vma_madvised) +{ +return 0 ; +} + +static struct bpf_thp_ops __bpf_thp_ops = { + .allocator = allocator, + .reclaimer = reclaimer, +}; + +static struct bpf_struct_ops bpf_bpf_thp_ops = { + .verifier_ops = &thp_bpf_verifier_ops, + .init = bpf_thp_init, + .check_member = bpf_thp_check_member, + .init_member = bpf_thp_init_member, + .reg = bpf_thp_reg, + .unreg = bpf_thp_unreg, + .name ="bpf_thp_ops" , + .cfi_stubs = &__bpf_thp_ops, + .owner =THIS_MODULE , +}; + +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);
Re: [RFC PATCH v3 4/5] mm: thp: add bpf thp struct ops
In reply to Yafang ShaoQuoted 135 lines · Yafang Shao
A new
bpf_thpstruct ops is introduced to provide finer-grained control over THP allocation policy. The struct ops includes two APIs for determining the THP allocator and reclaimer behavior:- THP allocator
int (*allocator)(unsigned long vm_flags, unsigned long tva_flags);The BPF program returns either
THP_ALLOC_CURRENTorTHP_ALLOC_KHUGEPAGED, indicating whether THP allocation should be performed synchronously (current task) or asynchronously (khugepaged).The decision is based on the current task context, VMA flags, and TVA flags.
- THP reclaimer
int (*reclaimer)(bool vma_madvised);The BPF program returns either
RECLAIMER_CURRENTorRECLAIMER_KSWAPD, determining whether memory reclamation is handled by the current task or kswapd.The decision depends on the current task and VMA flags.
Signed-off-by Yafang Shao <[email protected]>--- include/linux/huge_mm.h | 13 +-- mm/Makefile | 3 + mm/bpf_thp.c | 184 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 10 deletions(-)create mode 100644
mm/bpf_thp.cinclude/linux/huge_mm.h +3
-10@@ -54,6 +54,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 ;@@ -192,16 +193,8 @@ static inline bool hugepage_global_always(void)#define THP_ALLOC_KHUGEPAGED (1 << 1)#define THP_ALLOC_CURRENT (1 << 2) -static inline int bpf_thp_allocator (unsigned long vm_flags, -unsigned long tva_flags) -{ -return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; -} - -static inline gfp_t bpf_thp_gfp_mask (bool vma_madvised) -{ -return 0 ; -} +int bpf_thp_allocator (unsigned long vm_flags,unsigned long tva_flags); +gfp_t bpf_thp_gfp_mask (bool vma_madvised);static inline int highest_order (unsigned long orders) {mm/Makefile +3
-0@@ -99,6 +99,9 @@ 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 +ifdef CONFIG_BPF_SYSCALL +obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += bpf_thp.o +endif obj-$(CONFIG_PAGE_COUNTER) += page_counter.o obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.omm/bpf_thp.c new file +51
-0@@ -0,0 +1,184 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <linux/btf.h> +#include <linux/huge_mm.h> +#include <linux/khugepaged.h> + +#define RECLAIMER_CURRENT (1 << 1) +#define RECLAIMER_KSWAPD (1 << 2) +#define RECLAIMER_BOTH (RECLAIMER_CURRENT | RECLAIMER_KSWAPD) + +struct bpf_thp_ops { +/** +* @allocator: Specifies whether the THP allocation is performed +* by the current task or by khugepaged. +* @vm_flags: Flags for the VMA in the current allocation context +* @tva_flags: Flags for the TVA in the current allocation context +* +* Rerurn: +* - THP_ALLOC_CURRENT: THP was allocated synchronously by the calling +* task's context. +* - THP_ALLOC_KHUGEPAGED: THP was allocated asynchronously by the +* khugepaged kernel thread. +* - 0: THP allocation is disallowed in the current context. +*/ +int (*allocator)(unsigned long vm_flags,unsigned long tva_flags); +/** +* @reclaimer: Specifies the entity performing page reclaim: +* - current task context +* - kswapd +* - none (no reclaim) +* @vma_madvised: MADV flags for this VMA (e.g., MADV_HUGEPAGE, MADV_NOHUGEPAGE) +* +* Return: +* - RECLAIMER_CURRENT: Direct reclaim by the current task if THP +* allocation fails. +* - RECLAIMER_KSWAPD: Wake kswapd to reclaim memory if THP allocation fails. +* - RECLAIMER_ALL: Both current and kswapd will perform the reclaim +* - 0: No reclaim will be attempted. +*/ +int (*reclaimer)(bool vma_madvised); +}; + +static struct bpf_thp_ops bpf_thp; + +int bpf_thp_allocator (unsigned long vm_flags,unsigned long tva_flags) +{ +int allocator; + +/* No BPF program is attached */ +if (!(transparent_hugepage_flags & (1 <<TRANSPARENT_HUGEPAGE_BPF_ATTACHED )))
mm/bpf_thp.c line 61
Quoted 10 lines
mm/bpf_thp.c new file +10
-0+return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; + +if (current_is_khugepaged ()) +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; +if (!bpf_thp.allocator) +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; + + allocator = bpf_thp.allocator (vm_flags, tva_flags); +if (!allocator) +return 0 ;
The check seems redundant. Is it?
Quoted 46 lines
mm/bpf_thp.c new file +46
-0+/* invalid return value */ +if (allocator & ~(THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT )) +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; +return allocator; +} + +gfp_t bpf_thp_gfp_mask (bool vma_madvised) +{ +int reclaimer; + +if (!(transparent_hugepage_flags & (1 <<TRANSPARENT_HUGEPAGE_BPF_ATTACHED ))) +return 0 ; + +if (!bpf_thp.reclaimer) +return 0 ; + + reclaimer = bpf_thp.reclaimer (vma_madvised); +switch (reclaimer) { +case RECLAIMER_CURRENT : +return GFP_TRANSHUGE | __GFP_NORETRY; +case RECLAIMER_KSWAPD : +return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM; +case RECLAIMER_BOTH : +return GFP_TRANSHUGE | __GFP_KSWAPD_RECLAIM | __GFP_NORETRY; +default : +return 0 ; + } +} + +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,
mm/bpf_thp.c line 117
Quoted 10 lines
mm/bpf_thp.c new file +10
-0+}; + +static int bpf_thp_reg (void *kdata,struct bpf_link *link) +{ +struct bpf_thp_ops *ops = kdata; + +/* TODO: add support for multiple attaches */ +if (test_and_set_bit (TRANSPARENT_HUGEPAGE_BPF_ATTACHED , + &transparent_hugepage_flags)) +return -EOPNOTSUPP ;
I think returning -EBUSY if the struct_ops is already attached is a better choice
Quoted 19 lines
mm/bpf_thp.c new file +19
-0+ bpf_thp.allocator = ops->allocator; + bpf_thp.reclaimer = ops->reclaimer; +return 0 ; +} + +static void bpf_thp_unreg (void *kdata,struct bpf_link *link) +{ +clear_bit (TRANSPARENT_HUGEPAGE_BPF_ATTACHED , &transparent_hugepage_flags); + bpf_thp.allocator =NULL ; + bpf_thp.reclaimer =NULL ; +} + +static int bpf_thp_check_member (const struct btf_type *t, +const struct btf_member *member, +const struct bpf_prog *prog) +{ +return 0 ; +} +
[...]
Quoted 21 lines
mm/bpf_thp.c new file +21
-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_init (struct btf *btf) +{ +return 0 ; +} + +static int allocator (unsigned long vm_flags,unsigned long tva_flags) +{ +return 0 ; +} + +static int reclaimer (bool vma_madvised) +{ +return 0 ; +}
mm/bpf_thp.c line 167
Quoted 10 lines
mm/bpf_thp.c new file +10
-0+ +static struct bpf_thp_ops __bpf_thp_ops = { + .allocator = allocator, + .reclaimer = reclaimer, +}; + +static struct bpf_struct_ops bpf_bpf_thp_ops = { + .verifier_ops = &thp_bpf_verifier_ops, + .init = bpf_thp_init, + .check_member = bpf_thp_check_member,
nit. check_member doesn't need to be defined if it does not do anything.
Quoted 17 lines
mm/bpf_thp.c new file +17
-0+ .init_member = bpf_thp_init_member, + .reg = bpf_thp_reg, + .unreg = bpf_thp_unreg, + .name = "bpf_thp_ops", + .cfi_stubs = &__bpf_thp_ops, + .owner = THIS_MODULE, +}; + +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);
Re: [RFC PATCH v3 4/5] mm: thp: add bpf thp struct ops
Quoted 143 lines · Amery Hung
Quoted 139 lines · Yafang Shao
A new
bpf_thpstruct ops is introduced to provide finer-grained control over THP allocation policy. The struct ops includes two APIs for determining the THP allocator and reclaimer behavior:- THP allocator
int (*allocator)(unsigned long vm_flags, unsigned long tva_flags);The BPF program returns either
THP_ALLOC_CURRENTorTHP_ALLOC_KHUGEPAGED, indicating whether THP allocation should be performed synchronously (current task) or asynchronously (khugepaged).The decision is based on the current task context, VMA flags, and TVA flags.
- THP reclaimer
int (*reclaimer)(bool vma_madvised);The BPF program returns either
RECLAIMER_CURRENTorRECLAIMER_KSWAPD, determining whether memory reclamation is handled by the current task or kswapd.The decision depends on the current task and VMA flags.
Signed-off-by Yafang Shao <[email protected]>--- include/linux/huge_mm.h | 13 +-- mm/Makefile | 3 + mm/bpf_thp.c | 184 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+), 10 deletions(-)create mode 100644
mm/bpf_thp.cinclude/linux/huge_mm.h +3
-10@@ -54,6 +54,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 ;@@ -192,16 +193,8 @@ static inline bool hugepage_global_always(void)#define THP_ALLOC_KHUGEPAGED (1 << 1)#define THP_ALLOC_CURRENT (1 << 2) -static inline int bpf_thp_allocator (unsigned long vm_flags, -unsigned long tva_flags) -{ -return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; -} - -static inline gfp_t bpf_thp_gfp_mask (bool vma_madvised) -{ -return 0 ; -} +int bpf_thp_allocator (unsigned long vm_flags,unsigned long tva_flags); +gfp_t bpf_thp_gfp_mask (bool vma_madvised);static inline int highest_order (unsigned long orders) {mm/Makefile +3
-0@@ -99,6 +99,9 @@ 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 +ifdef CONFIG_BPF_SYSCALL +obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += bpf_thp.o +endif obj-$(CONFIG_PAGE_COUNTER) += page_counter.o obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.omm/bpf_thp.c new file +55
-0@@ -0,0 +1,184 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <linux/btf.h> +#include <linux/huge_mm.h> +#include <linux/khugepaged.h> + +#define RECLAIMER_CURRENT (1 << 1) +#define RECLAIMER_KSWAPD (1 << 2) +#define RECLAIMER_BOTH (RECLAIMER_CURRENT | RECLAIMER_KSWAPD) + +struct bpf_thp_ops { +/** +* @allocator: Specifies whether the THP allocation is performed +* by the current task or by khugepaged. +* @vm_flags: Flags for the VMA in the current allocation context +* @tva_flags: Flags for the TVA in the current allocation context +* +* Rerurn: +* - THP_ALLOC_CURRENT: THP was allocated synchronously by the calling +* task's context. +* - THP_ALLOC_KHUGEPAGED: THP was allocated asynchronously by the +* khugepaged kernel thread. +* - 0: THP allocation is disallowed in the current context. +*/ +int (*allocator)(unsigned long vm_flags,unsigned long tva_flags); +/** +* @reclaimer: Specifies the entity performing page reclaim: +* - current task context +* - kswapd +* - none (no reclaim) +* @vma_madvised: MADV flags for this VMA (e.g., MADV_HUGEPAGE, MADV_NOHUGEPAGE) +* +* Return: +* - RECLAIMER_CURRENT: Direct reclaim by the current task if THP +* allocation fails. +* - RECLAIMER_KSWAPD: Wake kswapd to reclaim memory if THP allocation fails. +* - RECLAIMER_ALL: Both current and kswapd will perform the reclaim +* - 0: No reclaim will be attempted. +*/ +int (*reclaimer)(bool vma_madvised); +}; + +static struct bpf_thp_ops bpf_thp; + +int bpf_thp_allocator (unsigned long vm_flags,unsigned long tva_flags) +{ +int allocator; + +/* No BPF program is attached */ +if (!(transparent_hugepage_flags & (1 <<TRANSPARENT_HUGEPAGE_BPF_ATTACHED ))) +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; + +if (current_is_khugepaged ()) +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ;
Quoted 8 lines
mm/bpf_thp.c line 61
Quoted 6 lines
mm/bpf_thp.c new file +6
-0+if (!bpf_thp.allocator) +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; + + allocator = bpf_thp.allocator (vm_flags, tva_flags); +if (!allocator) +return 0 ;The check seems redundant. Is it?
Right, thanks for pointing it out.
Quoted 52 lines
Quoted 52 lines
mm/bpf_thp.c new file +51
-0+/* invalid return value */ +if (allocator & ~(THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT )) +return THP_ALLOC_KHUGEPAGED |THP_ALLOC_CURRENT ; +return allocator; +} + +gfp_t bpf_thp_gfp_mask (bool vma_madvised) +{ +int reclaimer; + +if (!(transparent_hugepage_flags & (1 <<TRANSPARENT_HUGEPAGE_BPF_ATTACHED ))) +return 0 ; + +if (!bpf_thp.reclaimer) +return 0 ; + + reclaimer = bpf_thp.reclaimer (vma_madvised); +switch (reclaimer) { +case RECLAIMER_CURRENT : +return GFP_TRANSHUGE | __GFP_NORETRY; +case RECLAIMER_KSWAPD : +return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM; +case RECLAIMER_BOTH : +return GFP_TRANSHUGE | __GFP_KSWAPD_RECLAIM | __GFP_NORETRY; +default : +return 0 ; + } +} + +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_reg (void *kdata,struct bpf_link *link) +{ +struct bpf_thp_ops *ops = kdata;
Quoted 8 lines
mm/bpf_thp.c line 117
Quoted 5 lines
mm/bpf_thp.c new file +5
-0+ +/* TODO: add support for multiple attaches */ +if (test_and_set_bit (TRANSPARENT_HUGEPAGE_BPF_ATTACHED , + &transparent_hugepage_flags)) +return -EOPNOTSUPP ;I think returning
-EBUSYif thestruct_opsis already attached is a better choice
Makes sense. Thanks for the suggestion.
Quoted 48 lines
Quoted 20 lines
mm/bpf_thp.c new file +19
-0+ bpf_thp.allocator = ops->allocator; + bpf_thp.reclaimer = ops->reclaimer; +return 0 ; +} + +static void bpf_thp_unreg (void *kdata,struct bpf_link *link) +{ +clear_bit (TRANSPARENT_HUGEPAGE_BPF_ATTACHED , &transparent_hugepage_flags); + bpf_thp.allocator =NULL ; + bpf_thp.reclaimer =NULL ; +} + +static int bpf_thp_check_member (const struct btf_type *t, +const struct btf_member *member, +const struct bpf_prog *prog) +{ +return 0 ; +} +[...]
Quoted 25 lines
mm/bpf_thp.c new file +25
-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_init (struct btf *btf) +{ +return 0 ; +} + +static int allocator (unsigned long vm_flags,unsigned long tva_flags) +{ +return 0 ; +} + +static int reclaimer (bool vma_madvised) +{ +return 0 ; +} + +static struct bpf_thp_ops __bpf_thp_ops = { + .allocator = allocator, + .reclaimer = reclaimer,
Quoted 8 lines
mm/bpf_thp.c line 167
Quoted 6 lines
mm/bpf_thp.c new file +6
-0+}; + +static struct bpf_struct_ops bpf_bpf_thp_ops = { + .verifier_ops = &thp_bpf_verifier_ops, + .init = bpf_thp_init, + .check_member = bpf_thp_check_member,nit.
check_memberdoesn't need to be defined if it does not do anything.
I will remove it.
Signature · 2 lines
Regards Yafang
RFC PATCH v3 5/5 selftests/bpf: Add selftest for THP adjustment
In reply to Yafang ShaoThis test case uses a BPF program to enforce the following THP allocation policy:
- Current task will wakeup khugepaged to allocate THP
The result is as follows,
$ ./test_progs --name="thp_adjust" #437 thp_adjust:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
CONFIG_TRANSPARENT_HUGEPAGE=y is required for this test.
--- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/thp_adjust.c | 158 ++++++++++++++++++ .../selftests/bpf/progs/test_thp_adjust.c | 38 +++++ 3 files changed, 197 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 +1 -0
@@ -118,3 +118,4 @@ CONFIG_XDP_SOCKETS=y CONFIG_XFRM_INTERFACE=y CONFIG_TCP_CONG_DCTCP=y CONFIG_TCP_CONG_BBR=y +CONFIG_TRANSPARENT_HUGEPAGE=y
tools/testing/selftests/bpf/prog_tests/thp_adjust.c new file +158 -0
158 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_thp_adjust.c new file +38 -0
@@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +#define THP_ALLOC_KHUGEPAGED (1<<1) + +int target_pid; +int khugepaged_enter; + +SEC("fentry/__khugepaged_enter") +int BPF_PROG (thp_run ,struct mm_struct *mm) +{ +struct task_struct *current =bpf_get_current_task_btf (); + +if (current->mm == mm && current->pid == target_pid) + khugepaged_enter++; +return 0 ; +} + +SEC("struct_ops/allocator") +int BPF_PROG (bpf_thp_allocator ) +{ +struct task_struct *current =bpf_get_current_task_btf (); + +/* Allocate THP for this task in khugepaged. */ +if (current->pid == target_pid) +return THP_ALLOC_KHUGEPAGED ; +return 0 ; +} + +SEC(".struct_ops.link") +struct bpf_thp_ops thp = { + .allocator = (void *)bpf_thp_allocator, +};
Signature · 1 line
2.43.5
On 08.06.25 09:35, Yafang Shao wrote:
Sorry for not replying earlier, I was caught up with all other stuff.
I still consider this a very interesting approach, although I think we should think more about what a reasonable policy would look like medoium-term (in particular, multiple THP sizes, not always falling back to small pages if it means splitting excessively in the buddy etc.)
Quoted 48 lines
Background
----------We have consistently configured THP to "never" on our production servers due to past incidents caused by its behavior:
- Increased memory consumption
THP significantly raises overall memory usage.- Latency spikes
Random latency spikes occur due to more frequent memory compaction activity triggered by THP.- Lack of Fine-Grained Control
THP tuning knobs are globally configured, making them unsuitable for containerized environments. When different workloads run on the same host, enabling THP globally (without per-workload control) can cause unpredictable behavior.Due to these issues, system administrators remain hesitant to switch to "madvise" or "always" modes—unless finer-grained control over THP behavior is implemented.
New Motivation
--------------We have now identified that certain AI workloads achieve substantial performance gains with THP enabled. However, we’ve also verified that some workloads see little to no benefit—or are even negatively impacted—by THP.
In our Kubernetes environment, we deploy mixed workloads on a single server to maximize resource utilization. Our goal is to selectively enable THP for services that benefit from it while keeping it disabled for others. This approach allows us to incrementally enable THP for additional services and assess how to make it more viable in production.
Proposed Solution
-----------------To enable fine-grained control over THP behavior, we propose dynamically adjusting THP policies using BPF. This approach allows per-workload THP tuning, providing greater flexibility and precision.
The BPF-based THP adjustment mechanism introduces two new APIs for granular policy control:
- THP allocator
Quoted 8 lines
int (*allocator)(unsigned long vm_flags, unsigned long tva_flags);The BPF program returns either
THP_ALLOC_CURRENTorTHP_ALLOC_KHUGEPAGED, indicating whether THP allocation should be performed synchronously (current task) or asynchronously (khugepaged).The decision is based on the current task context, VMA flags, and TVA flags.
I think we should go one step further and actually get advises about the orders (THP sizes) to use. It might be helpful if the program would have access to system stats, to make an educated decision.
Given page fault information and system information, the program could then decide which orders to try to allocate.
That means, one would query during page faults and during khugepaged, which order one should try -- compared to our current approach of "start with the largest order that is enabled and fits".
Quoted 8 lines
- THP reclaimer
int (*reclaimer)(bool vma_madvised);The BPF program returns either
RECLAIMER_CURRENTorRECLAIMER_KSWAPD, determining whether memory reclamation is handled by the current task or kswapd.
Not sure about that, will have to look into the details.
But what could be interesting is deciding how to deal with underutilized THPs: for now we will try replacing zero-filled pages by the shared zeropage during a split. *maybe* some workloads could benefit from ... not doing that, and instead optimize the split.
Will maybe be a bit more trick, though.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 9 lines · David Hildenbrand
On 08.06.25 09:35, Yafang Shao wrote:
Sorry for not replying earlier, I was caught up with all other stuff.
I still consider this a very interesting approach, although I think we should think more about what a reasonable policy would look like medoium-term (in particular, multiple THP sizes, not always falling back to small pages if it means splitting excessively in the buddy etc.)
I find it difficult to understand why we introduced the mTHP sysfs knobs instead of implementing automatic THP size switching within the kernel. I'm skeptical about its practical utility in real-world workloads.
In contrast, XFS large folio (AKA. File THP) can automatically select orders between 0 and 9. Based on our verification, this feature has proven genuinely useful for certain specific workloads—though it's not yet perfect.
Quoted 58 lines
Quoted 49 lines
Background
----------We have consistently configured THP to "never" on our production servers due to past incidents caused by its behavior:
- Increased memory consumption
THP significantly raises overall memory usage.- Latency spikes
Random latency spikes occur due to more frequent memory compaction activity triggered by THP.- Lack of Fine-Grained Control
THP tuning knobs are globally configured, making them unsuitable for containerized environments. When different workloads run on the same host, enabling THP globally (without per-workload control) can cause unpredictable behavior.Due to these issues, system administrators remain hesitant to switch to "madvise" or "always" modes—unless finer-grained control over THP behavior is implemented.
New Motivation
--------------We have now identified that certain AI workloads achieve substantial performance gains with THP enabled. However, we’ve also verified that some workloads see little to no benefit—or are even negatively impacted—by THP.
In our Kubernetes environment, we deploy mixed workloads on a single server to maximize resource utilization. Our goal is to selectively enable THP for services that benefit from it while keeping it disabled for others. This approach allows us to incrementally enable THP for additional services and assess how to make it more viable in production.
Proposed Solution
-----------------To enable fine-grained control over THP behavior, we propose dynamically adjusting THP policies using BPF. This approach allows per-workload THP tuning, providing greater flexibility and precision.
The BPF-based THP adjustment mechanism introduces two new APIs for granular policy control:
- THP allocator
Quoted 8 lines
int (*allocator)(unsigned long vm_flags, unsigned long tva_flags);The BPF program returns either
THP_ALLOC_CURRENTorTHP_ALLOC_KHUGEPAGED, indicating whether THP allocation should be performed synchronously (current task) or asynchronously (khugepaged).The decision is based on the current task context, VMA flags, and TVA flags.
Quoted 6 lines
I think we should go one step further and actually get advises about the orders (THP sizes) to use. It might be helpful if the program would have access to system stats, to make an educated decision.
Given page fault information and system information, the program could then decide which orders to try to allocate.
Yes, that aligns with my thoughts as well. For instance, we could automate the decision-making process based on factors like PSI, memory fragmentation, and other metrics. However, this logic could be implemented within BPF programs—all we’d need is to extend the feature by introducing a few kfuncs (also known as BPF helpers).
Quoted 8 lines
That means, one would query during page faults and during khugepaged, which order one should try -- compared to our current approach of "start with the largest order that is enabled and fits".
Quoted 3 lines
- THP reclaimer
Quoted 7 lines
Quoted 5 lines
int (*reclaimer)(bool vma_madvised);The BPF program returns either
RECLAIMER_CURRENTorRECLAIMER_KSWAPD, determining whether memory reclamation is handled by the current task or kswapd.Not sure about that, will have to look into the details.
Some workloads allocate all their memory during initialization and do not require THP at runtime. For such cases, aggressively attempting THP allocation is beneficial. However, other workloads may dynamically allocate THP during execution—if these are latency-sensitive, we must avoid introducing long allocation delays.
Given these differing requirements, the global /sys/kernel/mm/transparent_hugepage/defrag setting is insufficient. Instead, we should implement per-workload defrag policies to better optimize performance based on individual application behavior.
Quoted 5 lines
But what could be interesting is deciding how to deal with underutilized
THPs: for now we will try replacing zero-filled pages by the shared zeropage during a split. *maybe* some workloads could benefit from ... not doing that, and instead optimize the split.
I believe a per-workload THP shrinker (e.g., /sys/kernel/mm/transparent_hugepage/shrink_underused) would also be valuable.
Thank you for the suggestion.
Signature · 2 lines
Regards Yafang
Quoted 16 lines · Yafang Shao
Quoted 9 lines · David Hildenbrand
On 08.06.25 09:35, Yafang Shao wrote:
Sorry for not replying earlier, I was caught up with all other stuff.
I still consider this a very interesting approach, although I think we should think more about what a reasonable policy would look like medoium-term (in particular, multiple THP sizes, not always falling back to small pages if it means splitting excessively in the buddy etc.)
I find it difficult to understand why we introduced the mTHP sysfs knobs instead of implementing automatic THP size switching within the kernel. I'm skeptical about its practical utility in real-world workloads.
Quoted 4 lines
In contrast, XFS large folio (AKA. File THP) can automatically select orders between 0 and 9. Based on our verification, this feature has proven genuinely useful for certain specific workloads—though it's not yet perfect.
I suggest you do some digging about the history of these toggles and the plans for the future (automatic), there has been plenty of talk about all that.
[...]
Quoted 16 lines
Quoted 16 lines
Quoted 11 lines
- THP allocator
int (*allocator)(unsigned long
vm_flags, unsigned longtva_flags);The BPF program returns either
THP_ALLOC_CURRENTorTHP_ALLOC_KHUGEPAGED, indicating whether THP allocation should be performed synchronously (current task) or asynchronously (khugepaged).The decision is based on the current task context, VMA flags, and TVA flags.
I think we should go one step further and actually get advises about the orders (THP sizes) to use. It might be helpful if the program would have access to system stats, to make an educated decision.
Quoted 8 lines
Quoted 2 lines
Given page fault information and system information, the program could then decide which orders to try to allocate.
Yes, that aligns with my thoughts as well. For instance, we could automate the decision-making process based on factors like PSI, memory fragmentation, and other metrics. However, this logic could be implemented within BPF programs—all we’d need is to extend the feature by introducing a few kfuncs (also known as BPF helpers).
We discussed this yesterday at a THP upstream meeting, and what we should look into is:
(1) Having a callback like
unsigned int (*get_suggested_order)(.., bool in_pagefault);
Where we can provide some information about the fault (vma size/flags/anon_name), and whether we are in the page fault (or in khugepaged).
Maybe we want a bitmap of orders to try (fallback), not sure yet.
(2) Having some way to tag these callbacks as "this is absolutely unstable for now and can be changed as we please.".
One idea will be to use this mechanism as a way to easily prototype policies, and once we know that a policy works, start moving it into the core.
In general, the core, without a BPF program, should be able to continue providing a sane default behavior.
Quoted 23 lines
Quoted 9 lines
That means, one would query during page faults and during khugepaged, which order one should try -- compared to our current approach of "start with the largest order that is enabled and fits".
Quoted 3 lines
- THP reclaimer
Quoted 7 lines
Quoted 5 lines
int (*reclaimer)(bool
vma_madvised);The BPF program returns either
RECLAIMER_CURRENTorRECLAIMER_KSWAPD, determining whether memory reclamation is handled by the current task or kswapd.Not sure about that, will have to look into the details.
Some workloads allocate all their memory during initialization and do not require THP at runtime. For such cases, aggressively attempting THP allocation is beneficial. However, other workloads may dynamically allocate THP during execution—if these are latency-sensitive, we must avoid introducing long allocation delays.
Quoted 4 lines
Given these differing requirements, the global
/sys/kernel/mm/transparent_hugepage/defragsetting is insufficient. Instead, we should implement per-workload defrag policies to better optimize performance based on individual application behavior.
We'll be very careful about the callbacks we will offer. Maybe the get_suggested_order() callback could itself make a decision and not suggest a high order if allocation would require comapction.
Initially, we should keep it simple and see what other callbacks to add / how to extend get_suggested_order(), to cover these cases.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 32 lines · David Hildenbrand
Quoted 17 lines · Yafang Shao
Quoted 16 lines · David Hildenbrand
Quoted 11 lines
- THP allocator
int (*allocator)(unsigned long
vm_flags, unsigned longtva_flags);The BPF program returns either
THP_ALLOC_CURRENTorTHP_ALLOC_KHUGEPAGED, indicating whether THP allocation should be performed synchronously (current task) or asynchronously (khugepaged).The decision is based on the current task context, VMA flags, and TVA flags.
I think we should go one step further and actually get advises about the orders (THP sizes) to use. It might be helpful if the program would have access to system stats, to make an educated decision.
Quoted 8 lines
Quoted 2 lines
Given page fault information and system information, the program could then decide which orders to try to allocate.
Yes, that aligns with my thoughts as well. For instance, we could automate the decision-making process based on factors like PSI, memory fragmentation, and other metrics. However, this logic could be implemented within BPF programs—all we’d need is to extend the feature by introducing a few kfuncs (also known as BPF helpers).
We discussed this yesterday at a THP upstream meeting, and what we should look into is:
(1) Having a callback like
Quoted 7 lines
unsigned int (*get_suggested_order)(.., bool
in_pagefault);Where we can provide some information about the fault (vma size/flags/anon_name), and whether we are in the page fault (or in khugepaged).
Maybe we want a bitmap of orders to try (fallback), not sure yet.
Ah I mentioned fallback below then noticed you mentioned here :)
Quoted 10 lines
(2) Having some way to tag these callbacks as "this is absolutely unstable for now and can be changed as we please.".
One idea will be to use this mechanism as a way to easily prototype policies, and once we know that a policy works, start moving it into the core.
In general, the core, without a BPF program, should be able to continue providing a sane default behavior.
I have warmed to this approach overall and I think one thing that was very clearly positive about this that came out of the call was the idea that we can rapidly prototype different ideas.
I think a key to all this is ensuring that we:
- Mark this interface very clearly unstable to begin with.
- Keep the interface as simple as possible.
I think perhaps the more challenging thing here will be providing the right amount of information to the caller to make decisions.
Also precisely how we use this too - obviously we need to be _trying_ to allocate at the requested order but should that fail allocate less but precisely how we do the fallback is something to think about.
I think generally this is the current best way forward before an automagic world... which is a very long-term project.
Quoted 29 lines
Quoted 24 lines
Quoted 10 lines
That means, one would query during page faults and during khugepaged, which order one should try -- compared to our current approach of "start with the largest order that is enabled and fits".
Quoted 3 lines
- THP reclaimer
Quoted 7 lines
Quoted 5 lines
int (*reclaimer)(bool
vma_madvised);The BPF program returns either
RECLAIMER_CURRENTorRECLAIMER_KSWAPD, determining whether memory reclamation is handled by the current task or kswapd.Not sure about that, will have to look into the details.
Some workloads allocate all their memory during initialization and do not require THP at runtime. For such cases, aggressively attempting THP allocation is beneficial. However, other workloads may dynamically allocate THP during execution—if these are latency-sensitive, we must avoid introducing long allocation delays.
Quoted 4 lines
Given these differing requirements, the global
/sys/kernel/mm/transparent_hugepage/defragsetting is insufficient. Instead, we should implement per-workload defrag policies to better optimize performance based on individual application behavior.
Quoted 6 lines
We'll be very careful about the callbacks we will offer. Maybe the
get_suggested_order()callback could itself make a decision and not suggest a high order if allocation would require comapction.Initially, we should keep it simple and see what other callbacks to add / how to extend
get_suggested_order(), to cover these cases.
Yes, caution vital here.
Quoted 6 lines
--
Cheers,David / dhildenb
Quoted 54 lines · David Hildenbrand
Quoted 16 lines · Yafang Shao
Quoted 9 lines · David Hildenbrand
On 08.06.25 09:35, Yafang Shao wrote:
Sorry for not replying earlier, I was caught up with all other stuff.
I still consider this a very interesting approach, although I think we should think more about what a reasonable policy would look like medoium-term (in particular, multiple THP sizes, not always falling back to small pages if it means splitting excessively in the buddy etc.)
I find it difficult to understand why we introduced the mTHP sysfs knobs instead of implementing automatic THP size switching within the kernel. I'm skeptical about its practical utility in real-world workloads.
Quoted 4 lines
In contrast, XFS large folio (AKA. File THP) can automatically select orders between 0 and 9. Based on our verification, this feature has proven genuinely useful for certain specific workloads—though it's not yet perfect.
I suggest you do some digging about the history of these toggles and the plans for the future (automatic), there has been plenty of talk about all that.
[...]
Quoted 16 lines
Quoted 16 lines
Quoted 11 lines
- THP allocator
int (*allocator)(unsigned long
vm_flags, unsigned longtva_flags);The BPF program returns either
THP_ALLOC_CURRENTorTHP_ALLOC_KHUGEPAGED, indicating whether THP allocation should be performed synchronously (current task) or asynchronously (khugepaged).The decision is based on the current task context, VMA flags, and TVA flags.
I think we should go one step further and actually get advises about the orders (THP sizes) to use. It might be helpful if the program would have access to system stats, to make an educated decision.
Quoted 8 lines
Quoted 2 lines
Given page fault information and system information, the program could then decide which orders to try to allocate.
Yes, that aligns with my thoughts as well. For instance, we could automate the decision-making process based on factors like PSI, memory fragmentation, and other metrics. However, this logic could be implemented within BPF programs—all we’d need is to extend the feature by introducing a few kfuncs (also known as BPF helpers).
Quoted 6 lines
We discussed this yesterday at a THP upstream meeting, and what we should look into is:
(1) Having a callback like
unsigned int (*get_suggested_order)(.., bool
in_pagefault);
This interface meets our needs precisely, enabling allocation orders of either 0 or 9 as required by our workloads.
Quoted 9 lines
Where we can provide some information about the fault (vma size/flags/anon_name), and whether we are in the page fault (or in khugepaged).
Maybe we want a bitmap of orders to try (fallback), not sure yet.
(2) Having some way to tag these callbacks as "this is absolutely unstable for now and can be changed as we please.".
BPF has already helped us complete this, so we don’t need to implement this restriction.
Note that all BPF kfuncs (including struct_ops) are currently unstable and may change in the future.
Alexei, could you confirm this understanding?
Quoted 7 lines
One idea will be to use this mechanism as a way to easily prototype policies, and once we know that a policy works, start moving it into the core.
In general, the core, without a BPF program, should be able to continue providing a sane default behavior.
makes sense.
Quoted 29 lines
Quoted 24 lines
Quoted 10 lines
That means, one would query during page faults and during khugepaged, which order one should try -- compared to our current approach of "start with the largest order that is enabled and fits".
Quoted 3 lines
- THP reclaimer
Quoted 7 lines
Quoted 5 lines
int (*reclaimer)(bool
vma_madvised);The BPF program returns either
RECLAIMER_CURRENTorRECLAIMER_KSWAPD, determining whether memory reclamation is handled by the current task or kswapd.Not sure about that, will have to look into the details.
Some workloads allocate all their memory during initialization and do not require THP at runtime. For such cases, aggressively attempting THP allocation is beneficial. However, other workloads may dynamically allocate THP during execution—if these are latency-sensitive, we must avoid introducing long allocation delays.
Quoted 4 lines
Given these differing requirements, the global
/sys/kernel/mm/transparent_hugepage/defragsetting is insufficient. Instead, we should implement per-workload defrag policies to better optimize performance based on individual application behavior.
Quoted 6 lines
We'll be very careful about the callbacks we will offer. Maybe the
get_suggested_order()callback could itself make a decision and not suggest a high order if allocation would require comapction.Initially, we should keep it simple and see what other callbacks to add / how to extend
get_suggested_order(), to cover these cases.
Yes, we can proceed by adding a simple get_suggested_order() and address any remaining details in follow-up work.
--
Regards
Yafang
Quoted 18 lines
Quoted 7 lines
We discussed this yesterday at a THP upstream meeting, and what we should look into is:
(1) Having a callback like
unsigned int (*get_suggested_order)(.., bool
in_pagefault);This interface meets our needs precisely, enabling allocation orders of either 0 or 9 as required by our workloads.
Quoted 7 lines
Where we can provide some information about the fault (vma size/flags/anon_name), and whether we are in the page fault (or in khugepaged).
Maybe we want a bitmap of orders to try (fallback), not sure yet.
Quoted 8 lines
Quoted 2 lines
(2) Having some way to tag these callbacks as "this is absolutely unstable for now and can be changed as we please.".
BPF has already helped us complete this, so we don’t need to implement this restriction.
Note that all BPF kfuncs (includingstruct_ops) are currently unstable and may change in the future.Quoted 1 line
Alexei, could you confirm this understanding?
Every MM person I talked to about this was like "as soon as it's actively used out there (e.g., a distro supports it), there is no way you can easily change these callbacks ever again - it will just silently become stable."
That is actually the biggest concern from the MM side: being stuck with an interface that was promised to be "unstable" but suddenly it's not-so-unstable anymore, and we have to support something that is very likely to be changed in the future.
Which guarantees do we have in the regard?
How can we make it clear to anybody using this specific interface that "if you depend on this being stable, you should learn how to read and you are to blame, not the MM people" ?
Signature · 3 lines
Cheers, David / dhildenb
Quoted 38 lines · David Hildenbrand
Quoted 19 lines
Quoted 8 lines
We discussed this yesterday at a THP upstream meeting, and what we should look into is:
(1) Having a callback like
unsigned int (*get_suggested_order)(.., bool
in_pagefault);This interface meets our needs precisely, enabling allocation orders of either 0 or 9 as required by our workloads.
Quoted 7 lines
Where we can provide some information about the fault (vma size/flags/anon_name), and whether we are in the page fault (or in khugepaged).
Maybe we want a bitmap of orders to try (fallback), not sure yet.
Quoted 8 lines
Quoted 2 lines
(2) Having some way to tag these callbacks as "this is absolutely unstable for now and can be changed as we please.".
BPF has already helped us complete this, so we don’t need to implement this restriction.
Note that all BPF kfuncs (includingstruct_ops) are currently unstable and may change in the future.Quoted 1 line
Alexei, could you confirm this understanding?
Every MM person I talked to about this was like "as soon as it's actively used out there (e.g., a distro supports it), there is no way you can easily change these callbacks ever again - it will just silently become stable."
That is actually the biggest concern from the MM side: being stuck with an interface that was promised to be "unstable" but suddenly it's not-so-unstable anymore, and we have to support something that is very likely to be changed in the future.
Quoted 5 lines
Which guarantees do we have in the regard?
How can we make it clear to anybody using this specific interface that "if you depend on this being stable, you should learn how to read and you are to blame, not the MM people" ?
As explained in the kernel document [0]:
kfuncs provide a kernel <-> kernel API, and thus are not bound by any of the strict stability restrictions associated with kernel <-> user UAPIs. This means they can be thought of as similar to EXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they’re defined in when it’s deemed necessary.
[0] https://docs.kernel.org/bpf/kfuncs.html#bpf-kfunc-lifecycle-expectations
That said, users of BPF kfuncs should treat them as inherently unstable and take responsibility for verifying their compatibility when switching kernel versions. However, this does not imply that BPF kfuncs can be modified arbitrarily.
For widely adopted kfuncs that deliver substantial value, changes should be made cautiously—preferably through backward-compatible extensions to ensure continued functionality across new kernel versions. Removal should only be considered in exceptional cases, such as:
- Severe, unfixable issues within the kernel
- Maintenance burdens that block new features or critical improvements.
Signature · 2 lines
Regards Yafang
Quoted 61 lines · Yafang Shao
Quoted 38 lines · David Hildenbrand
Quoted 19 lines
Quoted 8 lines
We discussed this yesterday at a THP upstream meeting, and what we should look into is:
(1) Having a callback like
unsigned int (*get_suggested_order)(.., bool
in_pagefault);This interface meets our needs precisely, enabling allocation orders of either 0 or 9 as required by our workloads.
Quoted 7 lines
Where we can provide some information about the fault (vma size/flags/anon_name), and whether we are in the page fault (or in khugepaged).
Maybe we want a bitmap of orders to try (fallback), not sure yet.
Quoted 8 lines
Quoted 2 lines
(2) Having some way to tag these callbacks as "this is absolutely unstable for now and can be changed as we please.".
BPF has already helped us complete this, so we don’t need to implement this restriction.
Note that all BPF kfuncs (includingstruct_ops) are currently unstable and may change in the future.Quoted 1 line
Alexei, could you confirm this understanding?
Every MM person I talked to about this was like "as soon as it's actively used out there (e.g., a distro supports it), there is no way you can easily change these callbacks ever again - it will just silently become stable."
That is actually the biggest concern from the MM side: being stuck with an interface that was promised to be "unstable" but suddenly it's not-so-unstable anymore, and we have to support something that is very likely to be changed in the future.
Quoted 5 lines
Which guarantees do we have in the regard?
How can we make it clear to anybody using this specific interface that "if you depend on this being stable, you should learn how to read and you are to blame, not the MM people" ?
As explained in the kernel document [0]:
kfuncs provide a kernel <-> kernel API, and thus are not bound by any of the strict stability restrictions associated with kernel <-> user UAPIs. This means they can be thought of as similar to
EXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they’re defined in when it’s deemed necessary.[0] https://docs.kernel.org/bpf/kfuncs.html#bpf-kfunc-lifecycle-expectations
That said, users of BPF kfuncs should treat them as inherently unstable and take responsibility for verifying their compatibility when switching kernel versions. However, this does not imply that BPF kfuncs can be modified arbitrarily.
Quoted 7 lines
For widely adopted kfuncs that deliver substantial value, changes should be made cautiously—preferably through backward-compatible extensions to ensure continued functionality across new kernel versions. Removal should only be considered in exceptional cases, such as:
- Severe, unfixable issues within the kernel
- Maintenance burdens that block new features or critical improvements.
And that is exactly what we are worried about.
You don't know beforehand whether something will be "widely adopted".
Even if there is the "A kfunc will never have any hard stability guarantees." in there.
The concerning bit is:
"kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer. "
Just no. Not going to happen for the kfuncs we know upfront (like here) will stand in our way in the future at some point and *will* be changed one way or another.
So for these kfuncs I want a clear way of expressing "whatever the kfuncs doc says, this here is completely unstable even if widely used"
Signature · 3 lines
Cheers, David / dhildenb
Quoted 7 lines · David Hildenbrand
Quoted 6 lines · Yafang Shao
Quoted 5 lines · David Hildenbrand
Quoted 5 lines
Quoted 5 lines
We discussed this yesterday at a THP upstream meeting, and what we should look into is:
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
(1) Having a callback like
unsigned int (*get_suggested_order)(.., bool
in_pagefault);This interface meets our needs precisely, enabling allocation orders of either 0 or 9 as required by our workloads.
That's great to hear, and to be clear my views align with David on this - I feel like having a _carefully chosen_ BPF interface could be valuable here, especially in the short to medium term where it will allow us to more rapidly iterate on an automated [m]THP mechanism.
I think one key question here is - do we want to retain a _permanent_ BPF hook here?
In any cae, for the first experiments with this we absolutely _must_ be able to express that this is going away, NO, not based on whether it's widely used, it IS going away.
Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 8 lines
Quoted 8 lines
Where we can provide some information about the fault (vma size/flags/anon_name), and whether we are in the page fault (or in khugepaged).
Maybe we want a bitmap of orders to try (fallback), not sure yet.
Quoted 8 lines
Quoted 2 lines
(2) Having some way to tag these callbacks as "this is absolutely unstable for now and can be changed as we please.".
BPF has already helped us complete this, so we don’t need to implement this restriction.
Note that all BPF kfuncs (includingstruct_ops) are currently unstable and may change in the future.Quoted 1 line
Alexei, could you confirm this understanding?
Every MM person I talked to about this was like "as soon as it's actively used out there (e.g., a distro supports it), there is no way you can easily change these callbacks ever again - it will just silently become stable."
That is actually the biggest concern from the MM side: being stuck with an interface that was promised to be "unstable" but suddenly it's not-so-unstable anymore, and we have to support something that is very likely to be changed in the future.
Which guarantees do we have in the regard?
How can we make it clear to anybody using this specific interface that "if you depend on this being stable, you should learn how to read and you are to blame, not the MM people" ?
Quoted 8 lines
Quoted 8 lines
As explained in the kernel document [0]:
kfuncs provide a kernel <-> kernel API, and thus are not bound by any of the strict stability restrictions associated with kernel <-> user UAPIs. This means they can be thought of as similar to
EXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they’re defined in when it’s deemed necessary.
I find this documentation super contradictory. I'm sorry but you can't have:
"...can therefore be modified or removed by a maintainer of the subsystem
they’re defined in when it’s deemed necessary."
And:
"kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer."
At the same time. Let alone:
"A kfunc will never have any hard stability guarantees. BPF APIs cannot and will not ever hard-block a change in the kernel purely for stability reasons"
Make your mind up!!
I mean the EXPORT_SYMBOL_GPL() example isn't accurate AT ALL - we can _absolutely_ change or remove those _at will_ as we don't care about external modules.
Really this seems to be saying, in not so many words, that this is basically a kAPI and you can't change it.
So this strictly violates what we need here.
Quoted 23 lines
Quoted 8 lines
[0] https://docs.kernel.org/bpf/kfuncs.html#bpf-kfunc-lifecycle-expectations
That said, users of BPF kfuncs should treat them as inherently unstable and take responsibility for verifying their compatibility when switching kernel versions. However, this does not imply that BPF kfuncs can be modified arbitrarily.
Quoted 7 lines
For widely adopted kfuncs that deliver substantial value, changes should be made cautiously—preferably through backward-compatible extensions to ensure continued functionality across new kernel versions. Removal should only be considered in exceptional cases, such as:
- Severe, unfixable issues within the kernel
- Maintenance burdens that block new features or critical improvements.And that is exactly what we are worried about.
You don't know beforehand whether something will be "widely adopted".
Even if there is the "A kfunc will never have any hard stability guarantees." in there.
Quoted 8 lines
The concerning bit is:
"kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer. "
Just no. Not going to happen for the kfuncs we know upfront (like here) will stand in our way in the future at some point and *will* be changed one way or another.
Yes, and the EXPORT*() example is plain wrong in that document.
Quoted 4 lines
So for these kfuncs I want a clear way of expressing "whatever the kfuncs doc says, this here is completely unstable even if widely used"
I wonder if we can use a CONFIG_xxx and put this behind that, which specifically says 'WE WILL REMOVE THIS' CONFIG_EXPERIMENTAL_DO_NOT_USE_THP_THINGY :P
Quoted 6 lines
--
Cheers,David / dhildenb
Cheers, Lorenzo
Quoted 21 lines · Lorenzo Stoakes
Quoted 7 lines · David Hildenbrand
Quoted 6 lines · Yafang Shao
Quoted 5 lines · David Hildenbrand
Quoted 5 lines
Quoted 5 lines
We discussed this yesterday at a THP upstream meeting, and what we should look into is:
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
(1) Having a callback like
unsigned int (*get_suggested_order)(.., bool
in_pagefault);This interface meets our needs precisely, enabling allocation orders of either 0 or 9 as required by our workloads.
That's great to hear, and to be clear my views align with David on this - I feel like having a
_carefullychosen_BPF interface could be valuable here, especially in the short to medium term where it will allow us to more rapidly iterate on an automated [m]THP mechanism.
Quoted 6 lines
I think one key question here is - do we want to retain a _permanent_ BPF hook here?
In any cae, for the first experiments with this we absolutely _must_ be able to express that this is going away, NO, not based on whether it's widely used, it IS going away.
If this BPF kfunc provides clear user value with minimal maintenance overhead, what would be the rationale for removing it? That said, if you and David both agree it should be deprecated, I won't object - though I'd suggest following the standard deprecation process.
Quoted 63 lines
Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
Quoted 9 lines
Quoted 9 lines
Where we can provide some information about the fault (vma size/flags/anon_name), and whether we are in the page fault (or in khugepaged).
Maybe we want a bitmap of orders to try (fallback), not sure yet.
Quoted 8 lines
Quoted 2 lines
(2) Having some way to tag these callbacks as "this is absolutely unstable for now and can be changed as we please.".
BPF has already helped us complete this, so we don’t need to implement this restriction.
Note that all BPF kfuncs (includingstruct_ops) are currently unstable and may change in the future.Quoted 1 line
Alexei, could you confirm this understanding?
Every MM person I talked to about this was like "as soon as it's actively used out there (e.g., a distro supports it), there is no way you can easily change these callbacks ever again - it will just silently become stable."
That is actually the biggest concern from the MM side: being stuck with an interface that was promised to be "unstable" but suddenly it's not-so-unstable anymore, and we have to support something that is very likely to be changed in the future.
Which guarantees do we have in the regard?
How can we make it clear to anybody using this specific interface that "if you depend on this being stable, you should learn how to read and you are to blame, not the MM people" ?
Quoted 8 lines
Quoted 8 lines
As explained in the kernel document [0]:
kfuncs provide a kernel <-> kernel API, and thus are not bound by any of the strict stability restrictions associated with kernel <-> user UAPIs. This means they can be thought of as similar to
EXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they’re defined in when it’s deemed necessary.I find this documentation super contradictory. I'm sorry but you can't have:
"...can therefore be modified or removed by a maintainer of the subsystem
they’re defined in when it’s deemed necessary."And:
"kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer."
At the same time. Let alone:
"A kfunc will never have any hard stability guarantees. BPF APIs cannot and will not ever hard-block a change in the kernel purely for stability reasons"
Make your mind up!!
Quoted 8 lines
I mean the
EXPORT_SYMBOL_GPL()example isn't accurate AT ALL - we can _absolutely_ change or remove those_atwill_as we don't care about external modules.Really this seems to be saying, in not so many words, that this is basically a kAPI and you can't change it.
So this strictly violates what we need here.
The maintainers have the authority to make the final determination ;-)
Quoted 38 lines
Quoted 25 lines
Quoted 10 lines
[0] https://docs.kernel.org/bpf/kfuncs.html#bpf-kfunc-lifecycle-expectations
That said, users of BPF kfuncs should treat them as inherently unstable and take responsibility for verifying their compatibility when switching kernel versions. However, this does not imply that BPF kfuncs can be modified arbitrarily.
Quoted 7 lines
For widely adopted kfuncs that deliver substantial value, changes should be made cautiously—preferably through backward-compatible extensions to ensure continued functionality across new kernel versions. Removal should only be considered in exceptional cases, such as:
- Severe, unfixable issues within the kernel
- Maintenance burdens that block new features or critical improvements.And that is exactly what we are worried about.
You don't know beforehand whether something will be "widely adopted".
Even if there is the "A kfunc will never have any hard stability guarantees." in there.
Quoted 8 lines
The concerning bit is:
"kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer. "
Just no. Not going to happen for the kfuncs we know upfront (like here) will stand in our way in the future at some point and *will* be changed one way or another.
Yes, and the EXPORT*() example is plain wrong in that document.
Quoted 2 lines
Quoted 6 lines
Quoted 2 lines
So for these kfuncs I want a clear way of expressing "whatever the kfuncs doc says, this here is completely unstable even if widely used"
I wonder if we can use a
CONFIG_xxxand put this behind that, which specifically says 'WE WILL REMOVE THIS'CONFIG_EXPERIMENTAL_DO_NOT_USE_THP_THINGY:P
That's a reasonable suggestion. We could implement this function under CONFIG_EXPERIMENTAL to mark it as experimental infrastructure.
Signature · 2 lines
Regards Yafang
Quoted 26 lines · Yafang Shao
Quoted 24 lines · Lorenzo Stoakes
Quoted 7 lines · David Hildenbrand
Quoted 6 lines · Yafang Shao
Quoted 5 lines · David Hildenbrand
Quoted 5 lines
Quoted 5 lines
We discussed this yesterday at a THP upstream meeting, and what we should look into is:
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
(1) Having a callback like
unsigned int (*get_suggested_order)(.., bool
in_pagefault);This interface meets our needs precisely, enabling allocation orders of either 0 or 9 as required by our workloads.
That's great to hear, and to be clear my views align with David on this - I feel like having a
_carefullychosen_BPF interface could be valuable here, especially in the short to medium term where it will allow us to more rapidly iterate on an automated [m]THP mechanism.I think one key question here is - do we want to retain a _permanent_ BPF hook here?
Quoted 8 lines
Quoted 3 lines
In any cae, for the first experiments with this we absolutely _must_ be able to express that this is going away, NO, not based on whether it's widely used, it IS going away.
If this BPF kfunc provides clear user value with minimal maintenance overhead, what would be the rationale for removing it? That said, if you and David both agree it should be deprecated, I won't object - though I'd suggest following the standard deprecation process.
You see herein lies the problem... :) from my point of view we want to have the ability to choose, fundamentally.
We may find out the proposed interface is unworkable, or sets assumptions we don't want to make.
So I think hiding ehhind a CONFIG_ flag is the best idea here to really enforce that and make it clear.
Personally I have a sense that we _will_ introduce something permanent. We just need to have the 'space' to positively decide to do that once the experimentation is complete.
Quoted 24 lines
Quoted 24 lines
I find this documentation super contradictory. I'm sorry but you can't have:
"...can therefore be modified or removed by a maintainer of the subsystem
they’re defined in when it’s deemed necessary."And:
"kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer."
At the same time. Let alone:
"A kfunc will never have any hard stability guarantees. BPF APIs cannot and will not ever hard-block a change in the kernel purely for stability reasons"
Make your mind up!!
I mean the
EXPORT_SYMBOL_GPL()example isn't accurate AT ALL - we can _absolutely_ change or remove those_atwill_as we don't care about external modules.
Quoted 6 lines
Quoted 4 lines
Really this seems to be saying, in not so many words, that this is basically a kAPI and you can't change it.
So this strictly violates what we need here.
The maintainers have the authority to make the final determination ;-)
Well the kernel doesn't entirely work this way... pressure can come which impacts what others may do.
If you have people saying 'hey we rely on this and removing it will break our cloud deployment' and 'hey I checked the docs and it says you guys have to take this into account', I am not so sure Andrew or Linus will accept the patch.
Quoted 6 lines
Quoted 3 lines
I wonder if we can use a
CONFIG_xxxand put this behind that, which specifically says 'WE WILL REMOVE THIS'CONFIG_EXPERIMENTAL_DO_NOT_USE_THP_THINGY:PThat's a reasonable suggestion. We could implement this function under
CONFIG_EXPERIMENTALto mark it as experimental infrastructure.
Thanks! Yes, I was looking for this flag :P didn't know if we still had that or not actually...
But, yeah, putting it behind that explicitly also makes it very clearly.
CONFIG_EXPERIMENTAL_BPF_FAULT_ORDER relies on CONFIG_EXPERIMENTAL makes it you know... pretty clear ;)
Quoted 4 lines
--
Regards
Yafang
Cheers, Lorenzo
Quoted 43 lines · Lorenzo Stoakes
Quoted 26 lines · Yafang Shao
Quoted 24 lines · Lorenzo Stoakes
Quoted 7 lines · David Hildenbrand
Quoted 6 lines · Yafang Shao
Quoted 5 lines · David Hildenbrand
Quoted 5 lines
Quoted 5 lines
We discussed this yesterday at a THP upstream meeting, and what we should look into is:
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
(1) Having a callback like
unsigned int (*get_suggested_order)(.., bool
in_pagefault);This interface meets our needs precisely, enabling allocation orders of either 0 or 9 as required by our workloads.
That's great to hear, and to be clear my views align with David on this - I feel like having a
_carefullychosen_BPF interface could be valuable here, especially in the short to medium term where it will allow us to more rapidly iterate on an automated [m]THP mechanism.I think one key question here is - do we want to retain a _permanent_ BPF hook here?
Quoted 8 lines
Quoted 3 lines
In any cae, for the first experiments with this we absolutely _must_ be able to express that this is going away, NO, not based on whether it's widely used, it IS going away.
If this BPF kfunc provides clear user value with minimal maintenance overhead, what would be the rationale for removing it? That said, if you and David both agree it should be deprecated, I won't object - though I'd suggest following the standard deprecation process.
You see herein lies the problem... :) from my point of view we want to have the ability to choose, fundamentally.
We may find out the proposed interface is unworkable, or sets assumptions we don't want to make.
Quoted 6 lines
So I think hiding ehhind a
CONFIG_flag is the best idea here to really enforce that and make it clear.Personally I have a sense that we _will_ introduce something permanent. We just need to have the 'space' to positively decide to do that once the experimentation is complete.
Thanks for your explanation.
Quoted 32 lines
Quoted 25 lines
Quoted 25 lines
I find this documentation super contradictory. I'm sorry but you can't have:
"...can therefore be modified or removed by a maintainer of the subsystem
they’re defined in when it’s deemed necessary."And:
"kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer."
At the same time. Let alone:
"A kfunc will never have any hard stability guarantees. BPF APIs cannot and will not ever hard-block a change in the kernel purely for stability reasons"
Make your mind up!!
I mean the
EXPORT_SYMBOL_GPL()example isn't accurate AT ALL - we can _absolutely_ change or remove those_atwill_as we don't care about external modules.Quoted 6 lines
Quoted 4 lines
Really this seems to be saying, in not so many words, that this is basically a kAPI and you can't change it.
So this strictly violates what we need here.
The maintainers have the authority to make the final determination ;-)
Quoted 7 lines
Well the kernel doesn't entirely work this way... pressure can come which impacts what others may do.
If you have people saying 'hey we rely on this and removing it will break our cloud deployment' and 'hey I checked the docs and it says you guys have to take this into account', I am not so sure Andrew or Linus will accept the patch.
understood.
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
I wonder if we can use a
CONFIG_xxxand put this behind that, which specifically says 'WE WILL REMOVE THIS'CONFIG_EXPERIMENTAL_DO_NOT_USE_THP_THINGY:PThat's a reasonable suggestion. We could implement this function under
CONFIG_EXPERIMENTALto mark it as experimental infrastructure.
Quoted 7 lines
Thanks! Yes, I was looking for this flag :P didn't know if we still had that or not actually...
But, yeah, putting it behind that explicitly also makes it very clearly.
CONFIG_EXPERIMENTAL_BPF_FAULT_ORDERrelies onCONFIG_EXPERIMENTALmakes it you know... pretty clear ;)
Agreed. Let's move forward with the CONFIG_EXPERIMENTAL_BPF_FAULT_ORDER option.
Signature · 2 lines
Regards Yafang
Quoted 84 lines · David Hildenbrand
Quoted 61 lines · Yafang Shao
Quoted 38 lines · David Hildenbrand
Quoted 19 lines
Quoted 8 lines
We discussed this yesterday at a THP upstream meeting, and what we should look into is:
(1) Having a callback like
unsigned int (*get_suggested_order)(.., bool
in_pagefault);This interface meets our needs precisely, enabling allocation orders of either 0 or 9 as required by our workloads.
Quoted 7 lines
Where we can provide some information about the fault (vma size/flags/anon_name), and whether we are in the page fault (or in khugepaged).
Maybe we want a bitmap of orders to try (fallback), not sure yet.
Quoted 8 lines
Quoted 2 lines
(2) Having some way to tag these callbacks as "this is absolutely unstable for now and can be changed as we please.".
BPF has already helped us complete this, so we don’t need to implement this restriction.
Note that all BPF kfuncs (includingstruct_ops) are currently unstable and may change in the future.Quoted 1 line
Alexei, could you confirm this understanding?
Every MM person I talked to about this was like "as soon as it's actively used out there (e.g., a distro supports it), there is no way you can easily change these callbacks ever again - it will just silently become stable."
That is actually the biggest concern from the MM side: being stuck with an interface that was promised to be "unstable" but suddenly it's not-so-unstable anymore, and we have to support something that is very likely to be changed in the future.
Quoted 5 lines
Which guarantees do we have in the regard?
How can we make it clear to anybody using this specific interface that "if you depend on this being stable, you should learn how to read and you are to blame, not the MM people" ?
As explained in the kernel document [0]:
kfuncs provide a kernel <-> kernel API, and thus are not bound by any of the strict stability restrictions associated with kernel <-> user UAPIs. This means they can be thought of as similar to
EXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they’re defined in when it’s deemed necessary.[0] https://docs.kernel.org/bpf/kfuncs.html#bpf-kfunc-lifecycle-expectations
That said, users of BPF kfuncs should treat them as inherently unstable and take responsibility for verifying their compatibility when switching kernel versions. However, this does not imply that BPF kfuncs can be modified arbitrarily.
Quoted 7 lines
For widely adopted kfuncs that deliver substantial value, changes should be made cautiously—preferably through backward-compatible extensions to ensure continued functionality across new kernel versions. Removal should only be considered in exceptional cases, such as:
- Severe, unfixable issues within the kernel
- Maintenance burdens that block new features or critical improvements.And that is exactly what we are worried about.
You don't know beforehand whether something will be "widely adopted".
Even if there is the "A kfunc will never have any hard stability guarantees." in there.
The concerning bit is:
"kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer. "
Quoted 7 lines
Just no. Not going to happen for the kfuncs we know upfront (like here) will stand in our way in the future at some point and *will* be changed one way or another.
So for these kfuncs I want a clear way of expressing "whatever the kfuncs doc says, this here is completely unstable even if widely used"
This statement does not conflict with the BPF kfuncs documentation, as it explicitly states:
"This means they can be thought of as similar to EXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they're defined in when deemed necessary."
There is no question that subsystem maintainers have the authority to remove kfuncs. However, the reason I raised the issue of removing widely used kfuncs is to highlight the recommended practice:
- First mark the kfunc as KF_DEPRECATED.
- Remove it in the next development cycle.
While this is not a strict requirement—maintainers can remove kfuncs immediately without deprecation—following this guideline helps avoid unnecessary disruptions for users.
Signature · 2 lines
Regards Yafang
Quoted 8 lines · Yafang Shao
Quoted 2 lines
So for these kfuncs I want a clear way of expressing "whatever the kfuncs doc says, this here is completely unstable even if widely used"
This statement does not conflict with the BPF kfuncs documentation, as it explicitly states:
"This means they can be thought of as similar toEXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they're defined in when deemed necessary."
Except that's not how EXPORT_SYMBOL_GPL() works at all, we can remove at will and are only required to update in-kernel users.
So that comparison is simply bogus.
Quoted 3 lines
There is no question that subsystem maintainers have the authority to remove kfuncs. However, the reason I raised the issue of removing
Except the documentation that seems to very strongly suggest otherwise?
Quoted 3 lines
widely used kfuncs is to highlight the recommended practice:
- First mark the kfunc asKF_DEPRECATED.
- Remove it in the next development cycle.
This seems reasonable, but I'm not in the slightest confident in us just relying on this.
Quoted 4 lines
While this is not a strict requirement—maintainers can remove kfuncs immediately without deprecation—following this guideline helps avoid unnecessary disruptions for users.
The documentation doesn't state this, you are surely just inferring it?
Quoted 4 lines
--
Regards
Yafang
Overall I think we need a different mechanism in addition to this, such as a very clearly described CONFIG_ option that makes it ABUNDANTLY clear that the config and thus the related BPF hook may be removed at any time.
Ideally with 'experimental' or such in the name, or perhaps even tainting the kernel.
We definitely need something better than what this documentation is saying, sorry. I am not in any way confident in relying no what this document states.
Cheers, Lorenzo
Quoted 30 lines · Lorenzo Stoakes
Quoted 8 lines · Yafang Shao
Quoted 2 lines
So for these kfuncs I want a clear way of expressing "whatever the kfuncs doc says, this here is completely unstable even if widely used"
This statement does not conflict with the BPF kfuncs documentation, as it explicitly states:
"This means they can be thought of as similar toEXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they're defined in when deemed necessary."Except that's not how
EXPORT_SYMBOL_GPL()works at all, we can remove at will and are only required to update in-kernel users.So that comparison is simply bogus.
Quoted 3 lines
There is no question that subsystem maintainers have the authority to remove kfuncs. However, the reason I raised the issue of removing
Except the documentation that seems to very strongly suggest otherwise?
Quoted 3 lines
widely used kfuncs is to highlight the recommended practice:
- First mark the kfunc asKF_DEPRECATED.
- Remove it in the next development cycle.This seems reasonable, but I'm not in the slightest confident in us just relying on this.
Quoted 1 line
Quoted 5 lines
Quoted 3 lines
While this is not a strict requirement—maintainers can remove kfuncs immediately without deprecation—following this guideline helps avoid unnecessary disruptions for users.
The documentation doesn't state this, you are surely just inferring it?
As noted in the other thread, the maintainers ultimately have final say on this matter. I'm simply sharing my perspective here.
Quoted 6 lines
Quoted 5 lines
--
Regards
Yafang
Quoted 6 lines
Overall I think we need a different mechanism in addition to this, such as a very clearly described
CONFIG_option that makes it ABUNDANTLY clear that the config and thus the related BPF hook may be removed at any time.Ideally with 'experimental' or such in the name, or perhaps even tainting the kernel.
Agreed.
Quoted 4 lines
We definitely need something better than what this documentation is saying, sorry. I am not in any way confident in relying no what this document states.
The documentation has always been difficult to understand ;-)
Signature · 2 lines
Regards Yafang
Quoted 10 lines · Yafang Shao
On Tue, Jul 22, 2025 at 7:55 PM Lorenzo Stoakes
Quoted 6 lines
Quoted 4 lines
While this is not a strict requirement—maintainers can remove kfuncs immediately without deprecation—following this guideline helps avoid unnecessary disruptions for users.
The documentation doesn't state this, you are surely just inferring it?
As noted in the other thread, the maintainers ultimately have final say on this matter. I'm simply sharing my perspective here.
Sure, sorry, I was probably being a bit too succinct in my reply here :P see other thread, I think the situation won't necessarily be quite as clear in practice.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
--
Regards
Yafang
Quoted 8 lines
Quoted 6 lines
Overall I think we need a different mechanism in addition to this, such as a very clearly described
CONFIG_option that makes it ABUNDANTLY clear that the config and thus the related BPF hook may be removed at any time.Ideally with 'experimental' or such in the name, or perhaps even tainting the kernel.
Agreed.
Thanks, discussed in other thread also.
Quoted 7 lines
Quoted 5 lines
We definitely need something better than what this documentation is saying, sorry. I am not in any way confident in relying no what this document states.
The documentation has always been difficult to understand ;-)
:) I think it is being rather too cautious or really circumspect in its wording, and could do with being a lot more direct, in my view.
But I suppose since organising kernel developers into agreeing on something is like herding cats, it might be difficult to get agreement to the point where the documentation could really be that.
Quoted 4 lines
--
Regards
Yafang
Cheers, Lorenzo
Quoted 19 lines · Yafang Shao
Background
----------We have consistently configured THP to "never" on our production servers due to past incidents caused by its behavior:
- Increased memory consumption
THP significantly raises overall memory usage.- Latency spikes
Random latency spikes occur due to more frequent memory compaction activity triggered by THP.- Lack of Fine-Grained Control
THP tuning knobs are globally configured, making them unsuitable for containerized environments. When different workloads run on the same host, enabling THP globally (without per-workload control) can cause unpredictable behavior.
Quoted 4 lines
Due to these issues, system administrators remain hesitant to switch to "madvise" or "always" modes—unless finer-grained control over THP behavior is implemented.
Would this solution work and be carried over in fork+exec? As that is something which is very common. How would that look like?
Quoted 23 lines · Usama Arif
Quoted 19 lines · Yafang Shao
Background
----------We have consistently configured THP to "never" on our production servers due to past incidents caused by its behavior:
- Increased memory consumption
THP significantly raises overall memory usage.- Latency spikes
Random latency spikes occur due to more frequent memory compaction activity triggered by THP.- Lack of Fine-Grained Control
THP tuning knobs are globally configured, making them unsuitable for containerized environments. When different workloads run on the same host, enabling THP globally (without per-workload control) can cause unpredictable behavior.
Quoted 7 lines
Quoted 4 lines
Due to these issues, system administrators remain hesitant to switch to "madvise" or "always" modes—unless finer-grained control over THP behavior is implemented.
Would this solution work and be carried over in fork+exec? As that is something which is very common. How would that look like?
The current implementation doesn't handle the fork+exec case properly. I've developed an updated version that addresses this issue, though I haven't submitted it for review yet.
In the new version:khugepaged_fork() will verify process eligibility before entering __khugepaged_enter()
If a parent process loses THP allocation privileges:
1. its child processes will skip __khugepaged_enter()
2. it will be removed from the khugepaged mm_slot.
3. the MMF_VM_HUGEPAGE will be cleared
Signature · 2 lines
Regards Yafang