From: Yafang Shao <laoar.shao@gmail.com>
To: akpm@linux-foundation.org, ast@kernel.org, daniel@iogearbox.net,
andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com,
song@kernel.org, yonghong.song@linux.dev,
john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me,
haoluo@google.com, jolsa@kernel.org, david@redhat.com,
ziy@nvidia.com, lorenzo.stoakes@oracle.com,
Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com,
dev.jain@arm.com, hannes@cmpxchg.org, usamaarif642@gmail.com,
gutierrez.asier@huawei-partners.com, willy@infradead.org,
ameryhung@gmail.com, rientjes@google.com, corbet@lwn.net,
21cnbao@gmail.com, shakeel.butt@linux.dev, tj@kernel.org,
lance.yang@linux.dev, rdunlap@infradead.org
Cc: bpf@vger.kernel.org, linux-mm@kvack.org,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
Yafang Shao <laoar.shao@gmail.com>
Subject: [PATCH v11 mm-new 09/10] selftests/bpf: add test case to update THP policy
Date: Mon, 20 Oct 2025 11:16:54 +0800 [thread overview]
Message-ID: <20251020031655.1093-5-laoar.shao@gmail.com> (raw)
In-Reply-To: <20251020031655.1093-1-laoar.shao@gmail.com>
This test case exercises the BPF THP update mechanism by modifying an
existing policy. The behavior confirms that:
- EBUSY error occurs when attempting to install a BPF program on a process
that already has an active BPF program
- Updates to currently running programs are successfully processed
- Local prog can't be updated by a global prog
- Global prog can't be updated by a local prog
- Global prog can be attached even if there's a local prog
- Local prog can't be attached if there's a global prog
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
.../selftests/bpf/prog_tests/thp_adjust.c | 79 +++++++++++++++++++
.../selftests/bpf/progs/test_thp_adjust.c | 29 +++++++
2 files changed, 108 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/thp_adjust.c b/tools/testing/selftests/bpf/prog_tests/thp_adjust.c
index 2b23e2d08092..0d570cee9006 100644
--- a/tools/testing/selftests/bpf/prog_tests/thp_adjust.c
+++ b/tools/testing/selftests/bpf/prog_tests/thp_adjust.c
@@ -194,6 +194,79 @@ static void subtest_thp_eligible(void)
bpf_link__destroy(ops_link);
}
+static void subtest_thp_policy_update(void)
+{
+ struct bpf_link *old_link, *new_link;
+ int elighble, err, pid;
+ char *ptr;
+
+ pid = getpid();
+ ptr = thp_alloc();
+
+ old_link = bpf_map__attach_struct_ops(skel->maps.thp_eligible_ops);
+ if (!ASSERT_OK_PTR(old_link, "attach_old_link"))
+ goto free;
+
+ elighble = get_thp_eligible(pid, (unsigned long)ptr);
+ ASSERT_EQ(elighble, 0, "THPeligible");
+
+ /* Attach multi BPF-THP to a single process is rejected. */
+ new_link = bpf_map__attach_struct_ops(skel->maps.thp_eligible_ops2);
+ if (!ASSERT_NULL(new_link, "attach_new_link"))
+ goto destory_old;
+ ASSERT_EQ(errno, EBUSY, "attach_new_link");
+
+ elighble = get_thp_eligible(pid, (unsigned long)ptr);
+ ASSERT_EQ(elighble, 0, "THPeligible");
+
+ err = bpf_link__update_map(old_link, skel->maps.thp_eligible_ops2);
+ ASSERT_EQ(err, 0, "update_old_link");
+
+ elighble = get_thp_eligible(pid, (unsigned long)ptr);
+ ASSERT_EQ(elighble, 1, "THPeligible");
+
+ /* Per process prog can't be update by a global prog */
+ err = bpf_link__update_map(old_link, skel->maps.swap_ops);
+ ASSERT_EQ(err, -EINVAL, "update_old_link");
+
+destory_old:
+ bpf_link__destroy(old_link);
+free:
+ thp_free(ptr);
+}
+
+static void subtest_thp_global_policy(void)
+{
+ struct bpf_link *local_link, *global_link;
+ int err;
+
+ local_link = bpf_map__attach_struct_ops(skel->maps.thp_eligible_ops);
+ if (!ASSERT_OK_PTR(local_link, "attach_local_link"))
+ return;
+
+ /* global prog can be attached even if there is a local prog */
+ global_link = bpf_map__attach_struct_ops(skel->maps.swap_ops);
+ if (!ASSERT_OK_PTR(global_link, "attach_global_link")) {
+ bpf_link__destroy(local_link);
+ return;
+ }
+
+ bpf_link__destroy(local_link);
+
+ /* local prog can't be attaached if there is a global prog */
+ local_link = bpf_map__attach_struct_ops(skel->maps.thp_eligible_ops);
+ if (!ASSERT_NULL(local_link, "attach_new_link"))
+ goto destory_global;
+ ASSERT_EQ(errno, EBUSY, "attach_new_link");
+
+ /* global prog can't be updated by a local prog */
+ err = bpf_link__update_map(global_link, skel->maps.thp_eligible_ops);
+ ASSERT_EQ(err, -EINVAL, "update_old_link");
+
+destory_global:
+ bpf_link__destroy(global_link);
+}
+
static int thp_adjust_setup(void)
{
int err = -1, pmd_order;
@@ -214,6 +287,8 @@ static int thp_adjust_setup(void)
skel->bss->pmd_order = pmd_order;
skel->struct_ops.thp_eligible_ops->pid = getpid();
+ skel->struct_ops.thp_eligible_ops2->pid = getpid();
+ /* swap_ops is a global prog since its pid is not set. */
err = test_thp_adjust__load(skel);
if (!ASSERT_OK(err, "load"))
@@ -240,6 +315,10 @@ void test_thp_adjust(void)
if (test__start_subtest("thp_eligible"))
subtest_thp_eligible();
+ if (test__start_subtest("policy_update"))
+ subtest_thp_policy_update();
+ if (test__start_subtest("global_policy"))
+ subtest_thp_global_policy();
thp_adjust_destroy();
}
diff --git a/tools/testing/selftests/bpf/progs/test_thp_adjust.c b/tools/testing/selftests/bpf/progs/test_thp_adjust.c
index b180a7f9b923..44648326819a 100644
--- a/tools/testing/selftests/bpf/progs/test_thp_adjust.c
+++ b/tools/testing/selftests/bpf/progs/test_thp_adjust.c
@@ -22,3 +22,32 @@ SEC(".struct_ops.link")
struct bpf_thp_ops thp_eligible_ops = {
.thp_get_order = (void *)thp_not_eligible,
};
+
+SEC("struct_ops/thp_get_order")
+int BPF_PROG(thp_eligible, struct vm_area_struct *vma, enum tva_type type,
+ unsigned long orders)
+{
+ /* THPeligible in /proc/pid/smaps is 1 */
+ if (type == TVA_SMAPS)
+ return pmd_order;
+ return pmd_order;
+}
+
+SEC(".struct_ops.link")
+struct bpf_thp_ops thp_eligible_ops2 = {
+ .thp_get_order = (void *)thp_eligible,
+};
+
+SEC("struct_ops/thp_get_order")
+int BPF_PROG(alloc_not_in_swap, struct vm_area_struct *vma, enum tva_type type,
+ unsigned long orders)
+{
+ if (type == TVA_SWAP_PAGEFAULT)
+ return 0;
+ return -1;
+}
+
+SEC(".struct_ops.link")
+struct bpf_thp_ops swap_ops = {
+ .thp_get_order = (void *)alloc_not_in_swap,
+};
--
2.47.3
prev parent reply other threads:[~2025-10-20 3:17 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-20 3:16 [PATCH v11 mm-new 05/10] mm: thp: enable THP allocation exclusively through khugepaged Yafang Shao
2025-10-20 3:16 ` [PATCH v11 mm-new 06/10] mm: bpf-thp: add support for global mode Yafang Shao
2025-10-20 3:16 ` [PATCH v11 mm-new 07/10] Documentation: add BPF THP Yafang Shao
2025-10-20 3:16 ` [PATCH v11 mm-new 08/10] selftests/bpf: add a simple BPF based THP policy Yafang Shao
2025-10-20 3:16 ` Yafang Shao [this message]
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=20251020031655.1093-5-laoar.shao@gmail.com \
--to=laoar.shao@gmail.com \
--cc=21cnbao@gmail.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=ameryhung@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=corbet@lwn.net \
--cc=daniel@iogearbox.net \
--cc=david@redhat.com \
--cc=dev.jain@arm.com \
--cc=eddyz87@gmail.com \
--cc=gutierrez.asier@huawei-partners.com \
--cc=hannes@cmpxchg.org \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=lance.yang@linux.dev \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=martin.lau@linux.dev \
--cc=npache@redhat.com \
--cc=rdunlap@infradead.org \
--cc=rientjes@google.com \
--cc=ryan.roberts@arm.com \
--cc=sdf@fomichev.me \
--cc=shakeel.butt@linux.dev \
--cc=song@kernel.org \
--cc=tj@kernel.org \
--cc=usamaarif642@gmail.com \
--cc=willy@infradead.org \
--cc=yonghong.song@linux.dev \
--cc=ziy@nvidia.com \
/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