From: Yafang Shao <laoar.shao@gmail.com>
To: akpm@linux-foundation.org, ast@kernel.org, daniel@iogearbox.net,
andrii@kernel.org
Cc: bpf@vger.kernel.org, linux-mm@kvack.org,
Yafang Shao <laoar.shao@gmail.com>
Subject: [RFC PATCH 3/4] mm: add BPF hook for THP adjustment
Date: Tue, 29 Apr 2025 10:41:38 +0800 [thread overview]
Message-ID: <20250429024139.34365-4-laoar.shao@gmail.com> (raw)
In-Reply-To: <20250429024139.34365-1-laoar.shao@gmail.com>
We will use the @vma parameter in BPF programs to determine whether THP can
be used. The typical workflow is as follows:
1. Retrieve the mm_struct from the given @vma.
2. Obtain the task_struct associated with the mm_struct
It depends on CONFIG_MEMCG.
3. Adjust THP behavior dynamically based on task attributes
E.g., based on the task’s cgroup
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
mm/Makefile | 3 +++
mm/bpf.c | 36 ++++++++++++++++++++++++++++++++++++
mm/bpf.h | 21 +++++++++++++++++++++
mm/internal.h | 3 +++
4 files changed, 63 insertions(+)
create mode 100644 mm/bpf.c
create mode 100644 mm/bpf.h
diff --git a/mm/Makefile b/mm/Makefile
index e7f6bbf8ae5f..97055da04746 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -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.o
+endif
obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o
obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o
diff --git a/mm/bpf.c b/mm/bpf.c
new file mode 100644
index 000000000000..72eebcdbad56
--- /dev/null
+++ b/mm/bpf.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Author: Yafang Shao <laoar.shao@gmail.com>
+ */
+
+#include <linux/bpf.h>
+#include <linux/mm_types.h>
+
+__bpf_hook_start();
+
+/* Checks if this @vma can use THP. */
+__weak noinline int
+mm_bpf_thp_vma_allowable(struct vm_area_struct *vma)
+{
+ /* At present, fmod_ret exclusively uses 0 to signify that the return
+ * value remains unchanged.
+ */
+ return 0;
+}
+
+__bpf_hook_end();
+
+BTF_SET8_START(mm_bpf_fmod_ret_ids)
+BTF_ID_FLAGS(func, mm_bpf_thp_vma_allowable)
+BTF_SET8_END(mm_bpf_fmod_ret_ids)
+
+static const struct btf_kfunc_id_set mm_bpf_fmodret_set = {
+ .owner = THIS_MODULE,
+ .set = &mm_bpf_fmod_ret_ids,
+};
+
+static int __init bpf_mm_kfunc_init(void)
+{
+ return register_btf_fmodret_id_set(&mm_bpf_fmodret_set);
+}
+late_initcall(bpf_mm_kfunc_init);
diff --git a/mm/bpf.h b/mm/bpf.h
new file mode 100644
index 000000000000..e03a38084b08
--- /dev/null
+++ b/mm/bpf.h
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __MM_BPF_H
+#define __MM_BPF_H
+
+#define MM_BPF_ALLOWABLE (1)
+#define MM_BPF_NOT_ALLOWABLE (-1)
+
+#define MM_BPF_ALLOWABLE_HOOK(func, args...) { \
+ int ret = func(args); \
+ \
+ if (ret == MM_BPF_ALLOWABLE) \
+ return 1; \
+ if (ret == MM_BPF_NOT_ALLOWABLE) \
+ return 0; \
+}
+
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+int mm_bpf_thp_vma_allowable(struct vm_area_struct *vma);
+#endif
+
+#endif
diff --git a/mm/internal.h b/mm/internal.h
index aa698a11dd68..c8bf405fa581 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -21,6 +21,7 @@
/* Internal core VMA manipulation functions. */
#include "vma.h"
+#include "bpf.h"
struct folio_batch;
@@ -1632,6 +1633,7 @@ static inline bool reclaim_pt_is_enabled(unsigned long start, unsigned long end,
*/
static inline bool hugepage_global_enabled(struct vm_area_struct *vma)
{
+ MM_BPF_ALLOWABLE_HOOK(mm_bpf_thp_vma_allowable, vma);
return transparent_hugepage_flags &
((1<<TRANSPARENT_HUGEPAGE_FLAG) |
(1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG));
@@ -1639,6 +1641,7 @@ static inline bool hugepage_global_enabled(struct vm_area_struct *vma)
static inline bool hugepage_global_always(struct vm_area_struct *vma)
{
+ MM_BPF_ALLOWABLE_HOOK(mm_bpf_thp_vma_allowable, vma);
return transparent_hugepage_flags &
(1<<TRANSPARENT_HUGEPAGE_FLAG);
}
--
2.43.5
next prev parent reply other threads:[~2025-04-29 2:42 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-29 2:41 [RFC PATCH 0/4] mm, bpf: BPF based " Yafang Shao
2025-04-29 2:41 ` [RFC PATCH 1/4] mm: move hugepage_global_{enabled,always}() to internal.h Yafang Shao
2025-04-29 15:13 ` Zi Yan
2025-04-30 2:40 ` Yafang Shao
2025-04-30 12:11 ` Zi Yan
2025-04-30 14:43 ` Yafang Shao
2025-04-29 2:41 ` [RFC PATCH 2/4] mm: pass VMA parameter to hugepage_global_{enabled,always}() Yafang Shao
2025-04-29 15:31 ` Zi Yan
2025-04-30 2:46 ` Yafang Shao
2025-04-29 2:41 ` Yafang Shao [this message]
2025-04-29 15:19 ` [RFC PATCH 3/4] mm: add BPF hook for THP adjustment Alexei Starovoitov
2025-04-30 2:48 ` Yafang Shao
2025-04-29 2:41 ` [RFC PATCH 4/4] selftests/bpf: Add selftest " Yafang Shao
2025-04-29 3:11 ` [RFC PATCH 0/4] mm, bpf: BPF based " Matthew Wilcox
2025-04-29 4:53 ` Yafang Shao
2025-04-29 15:09 ` Zi Yan
2025-04-30 2:33 ` Yafang Shao
2025-04-30 13:19 ` Zi Yan
2025-04-30 14:38 ` Yafang Shao
2025-04-30 15:00 ` Zi Yan
2025-04-30 15:16 ` Yafang Shao
2025-04-30 15:21 ` Liam R. Howlett
2025-04-30 15:37 ` Yafang Shao
2025-04-30 15:53 ` Liam R. Howlett
2025-04-30 16:06 ` Yafang Shao
2025-04-30 17:45 ` Johannes Weiner
2025-04-30 17:53 ` Zi Yan
2025-05-01 19:36 ` Gutierrez Asier
2025-05-02 5:48 ` Yafang Shao
2025-05-02 12:00 ` Zi Yan
2025-05-02 12:18 ` Yafang Shao
2025-05-02 13:04 ` David Hildenbrand
2025-05-02 13:06 ` Matthew Wilcox
2025-05-02 13:34 ` Zi Yan
2025-05-05 2:35 ` Yafang Shao
2025-05-05 9:11 ` Gutierrez Asier
2025-05-05 9:38 ` Yafang Shao
2025-04-30 17:59 ` Johannes Weiner
2025-05-01 0:40 ` Yafang Shao
2025-04-30 14:40 ` Liam R. Howlett
2025-04-30 14:49 ` Yafang Shao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250429024139.34365-4-laoar.shao@gmail.com \
--to=laoar.shao@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=linux-mm@kvack.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox