Skip to content

Commit cf0f715

Browse files
laoarKernel Patches Daemon
authored andcommitted
Documentation: add BPF THP
Add the documentation. Signed-off-by: Yafang Shao <[email protected]>
1 parent e0c95cc commit cf0f715

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

Documentation/admin-guide/mm/transhuge.rst

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,3 +738,116 @@ support enabled just fine as always. No difference can be noted in
738738
hugetlbfs other than there will be less overall fragmentation. All
739739
usual features belonging to hugetlbfs are preserved and
740740
unaffected. libhugetlbfs will also work fine as usual.
741+
742+
BPF THP
743+
=======
744+
745+
:Author: Yafang Shao <[email protected]>
746+
:Date: October 2025
747+
748+
Overview
749+
--------
750+
751+
When the system is configured with "always" or "madvise" THP mode, a BPF program
752+
can be used to adjust THP allocation policies dynamically. This enables
753+
fine-grained control over THP decisions based on various factors including
754+
workload identity, allocation context, and system memory pressure.
755+
756+
Program Interface
757+
-----------------
758+
759+
This feature implements a struct_ops BPF program with the following interface::
760+
761+
struct bpf_thp_ops {
762+
pid_t pid;
763+
thp_order_fn_t *thp_get_order;
764+
};
765+
766+
Callback Functions
767+
------------------
768+
769+
thp_get_order()
770+
~~~~~~~~~~~~~~~
771+
772+
.. code-block:: c
773+
774+
int thp_get_order(struct vm_area_struct *vma,
775+
enum tva_type type,
776+
unsigned long orders);
777+
778+
Parameters
779+
^^^^^^^^^^
780+
781+
``vma``
782+
``vm_area_struct`` associated with the THP allocation.
783+
784+
``type``
785+
TVA type for the current ``vma``.
786+
787+
``orders``
788+
Bitmask of available THP orders for this allocation.
789+
790+
Return value
791+
^^^^^^^^^^^^
792+
793+
- The suggested THP order for allocation from the BPF program
794+
- Must be a valid, available order from the provided ``orders`` bitmask
795+
796+
Operation Modes
797+
---------------
798+
799+
Per Process Mode
800+
~~~~~~~~~~~~~~~~
801+
802+
When registering a BPF-THP with a specific PID, the program is installed in the
803+
target task's ``mm_struct``::
804+
805+
struct mm_struct {
806+
struct bpf_thp_ops __rcu *bpf_thp;
807+
};
808+
809+
Inheritance Behavior
810+
^^^^^^^^^^^^^^^^^^^^
811+
812+
- Existing child processes are unaffected
813+
- Newly forked children inherit the BPF-THP from their parent
814+
- The BPF-THP persists across execve() calls
815+
816+
Management Rules
817+
^^^^^^^^^^^^^^^^
818+
819+
- When a BPF-THP instance is unregistered, all managed tasks' ``bpf_thp``
820+
pointers are reset to ``NULL``
821+
- When a BPF-THP instance is updated, all managed tasks' ``bpf_thp`` pointers
822+
are automatically updated to the new version
823+
- Each process can be managed by only one BPF-THP instance at a time
824+
825+
Global Mode
826+
~~~~~~~~~~~
827+
828+
If no PID is specified during registration, the BPF-THP operates in global mode.
829+
In this mode, all tasks in the system are managed by the global instance.
830+
831+
Global Mode Precedence
832+
^^^^^^^^^^^^^^^^^^^^^^
833+
834+
- The global instance takes precedence over all per-process instances
835+
- All existing per-process instances are disabled when a global instance is
836+
registered
837+
- New per-process registrations are blocked while a global instance is active
838+
- Existing per-process instances remain registered (no forced unregistration)
839+
840+
Instance Management
841+
^^^^^^^^^^^^^^^^^^^
842+
843+
- Updates are type-isolated: global instances can only be updated by new global
844+
instances, and per-process instances by new per-process instances
845+
- Only one global BPF-THP can be registered at a time
846+
- Global instances can be updated dynamically without requiring task restarts
847+
848+
Implementation Notes
849+
--------------------
850+
851+
- This is currently an experimental feature
852+
- ``CONFIG_BPF_THP`` must be enabled to use this functionality
853+
- The feature depends on proper THP configuration ("always" or "madvise" mode)

0 commit comments

Comments
 (0)