From: Yafang Shao <laoar.shao@gmail.com>
To: akpm@linux-foundation.org, david@redhat.com, ziy@nvidia.com,
baolin.wang@linux.alibaba.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,
ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
ameryhung@gmail.com, rientjes@google.com, corbet@lwn.net
Cc: bpf@vger.kernel.org, linux-mm@kvack.org,
linux-doc@vger.kernel.org, Yafang Shao <laoar.shao@gmail.com>
Subject: [PATCH v6 mm-new 08/10] selftests/bpf: add test cases for invalid thp_adjust usage
Date: Tue, 26 Aug 2025 15:19:46 +0800 [thread overview]
Message-ID: <20250826071948.2618-9-laoar.shao@gmail.com> (raw)
In-Reply-To: <20250826071948.2618-1-laoar.shao@gmail.com>
1. The trusted VMA pointer can be null and must be checked before
dereferencing.
2. Resources acquired via bpf_mm_get_task() must be released with
bpf_task_release().
3. Memory groups obtained through bpf_mm_get_mem_cgroup() must be released
using bpf_put_mem_cgroup().
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
.../selftests/bpf/prog_tests/thp_adjust.c | 7 +++++
.../bpf/progs/test_thp_adjust_trusted_vma.c | 27 +++++++++++++++++++
.../progs/test_thp_adjust_unreleased_memcg.c | 24 +++++++++++++++++
.../progs/test_thp_adjust_unreleased_task.c | 25 +++++++++++++++++
4 files changed, 83 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_vma.c
create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_memcg.c
create mode 100644 tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_task.c
diff --git a/tools/testing/selftests/bpf/prog_tests/thp_adjust.c b/tools/testing/selftests/bpf/prog_tests/thp_adjust.c
index 6e65d7b0eb80..846679acaff2 100644
--- a/tools/testing/selftests/bpf/prog_tests/thp_adjust.c
+++ b/tools/testing/selftests/bpf/prog_tests/thp_adjust.c
@@ -11,6 +11,9 @@
#include <test_progs.h>
#include "cgroup_helpers.h"
#include "test_thp_adjust.skel.h"
+#include "test_thp_adjust_trusted_vma.skel.h"
+#include "test_thp_adjust_unreleased_task.skel.h"
+#include "test_thp_adjust_unreleased_memcg.skel.h"
#define LEN (16 * 1024 * 1024) /* 16MB */
#define THP_ENABLED_FILE "/sys/kernel/mm/transparent_hugepage/enabled"
@@ -333,4 +336,8 @@ void test_thp_adjust(void)
subtest_thp_policy_update();
thp_adjust_destroy();
+
+ RUN_TESTS(test_thp_adjust_trusted_vma);
+ RUN_TESTS(test_thp_adjust_unreleased_task);
+ RUN_TESTS(test_thp_adjust_unreleased_memcg);
}
diff --git a/tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_vma.c b/tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_vma.c
new file mode 100644
index 000000000000..caa73bebefcf
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_thp_adjust_trusted_vma.c
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("struct_ops/get_suggested_order")
+__failure __msg("R1 invalid mem access 'trusted_ptr_or_null_'")
+int BPF_PROG(thp_trusted_vma, struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+ u64 vma_flags, u64 tva_flags, int orders)
+{
+ struct mem_cgroup *memcg = bpf_mm_get_mem_cgroup(vma__nullable->vm_mm);
+
+ if (!memcg)
+ return 0;
+
+ bpf_put_mem_cgroup(memcg);
+ return 1;
+}
+SEC(".struct_ops.link")
+struct bpf_thp_ops thp_memcg_ops = {
+ .get_suggested_order = (void *)thp_trusted_vma,
+};
diff --git a/tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_memcg.c b/tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_memcg.c
new file mode 100644
index 000000000000..467befebb35f
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_memcg.c
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("struct_ops/get_suggested_order")
+__failure __msg("Unreleased reference")
+int BPF_PROG(thp_unreleased_memcg, struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+ u64 vma_flags, u64 tva_flags, int orders)
+{
+ struct mem_cgroup *memcg = bpf_mm_get_mem_cgroup(mm);
+
+ /* The memcg should be released with bpf_put_mem_cgroup() */
+ return memcg ? 0 : 1;
+}
+SEC(".struct_ops.link")
+struct bpf_thp_ops thp_memcg_ops = {
+ .get_suggested_order = (void *)thp_unreleased_memcg,
+};
diff --git a/tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_task.c b/tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_task.c
new file mode 100644
index 000000000000..50d756810412
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_thp_adjust_unreleased_task.c
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("struct_ops/get_suggested_order")
+__failure __msg("Unreleased reference")
+int BPF_PROG(thp_unreleased_task, struct mm_struct *mm, struct vm_area_struct *vma__nullable,
+ u64 vma_flags, u64 tva_flags, int orders)
+{
+ struct task_struct *p = bpf_mm_get_task(mm);
+
+ /* The task should be released with bpf_task_release() */
+ return p ? 0 : 1;
+}
+
+SEC(".struct_ops.link")
+struct bpf_thp_ops thp_task_ops = {
+ .get_suggested_order = (void *)thp_unreleased_task,
+};
--
2.47.3
next prev parent reply other threads:[~2025-08-26 7:21 UTC|newest]
Thread overview: 61+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-26 7:19 [PATCH v6 mm-new 00/10] mm, bpf: BPF based THP order selection Yafang Shao
2025-08-26 7:19 ` [PATCH v6 mm-new 01/10] mm: thp: add support for " Yafang Shao
2025-08-27 2:57 ` kernel test robot
2025-08-27 11:39 ` Yafang Shao
2025-08-27 15:04 ` Lorenzo Stoakes
2025-08-27 15:03 ` Lorenzo Stoakes
2025-08-28 5:54 ` Yafang Shao
2025-08-28 10:50 ` Lorenzo Stoakes
2025-08-29 3:01 ` Yafang Shao
2025-08-29 10:42 ` Lorenzo Stoakes
2025-08-31 3:11 ` Yafang Shao
2025-09-01 11:39 ` Lorenzo Stoakes
2025-09-02 2:48 ` Yafang Shao
2025-09-02 7:50 ` Lorenzo Stoakes
2025-09-03 2:10 ` Yafang Shao
2025-08-29 4:56 ` Barry Song
2025-08-29 5:36 ` Yafang Shao
2025-08-26 7:19 ` [PATCH v6 mm-new 02/10] mm: thp: add a new kfunc bpf_mm_get_mem_cgroup() Yafang Shao
2025-08-27 15:34 ` Lorenzo Stoakes
2025-08-27 20:50 ` Shakeel Butt
2025-08-28 10:40 ` Lorenzo Stoakes
2025-08-28 16:00 ` Shakeel Butt
2025-08-29 10:45 ` Lorenzo Stoakes
2025-08-28 6:57 ` Yafang Shao
2025-08-28 10:42 ` Lorenzo Stoakes
2025-08-29 3:09 ` Yafang Shao
2025-08-27 20:45 ` Shakeel Butt
2025-08-28 6:58 ` Yafang Shao
2025-08-26 7:19 ` [PATCH v6 mm-new 03/10] mm: thp: add a new kfunc bpf_mm_get_task() Yafang Shao
2025-08-27 15:42 ` Lorenzo Stoakes
2025-08-27 21:50 ` Andrii Nakryiko
2025-08-28 6:50 ` Yafang Shao
2025-08-28 10:51 ` Lorenzo Stoakes
2025-08-29 3:15 ` Yafang Shao
2025-08-29 10:42 ` Lorenzo Stoakes
2025-08-28 6:47 ` Yafang Shao
2025-08-29 10:43 ` Lorenzo Stoakes
2025-08-26 7:19 ` [PATCH v6 mm-new 04/10] bpf: mark vma->vm_mm as trusted Yafang Shao
2025-08-27 15:45 ` Lorenzo Stoakes
2025-08-28 6:12 ` Yafang Shao
2025-08-28 11:11 ` Lorenzo Stoakes
2025-08-29 3:05 ` Yafang Shao
2025-08-29 10:49 ` Lorenzo Stoakes
2025-08-31 3:16 ` Yafang Shao
2025-09-01 10:36 ` Lorenzo Stoakes
2025-08-26 7:19 ` [PATCH v6 mm-new 05/10] selftests/bpf: add a simple BPF based THP policy Yafang Shao
2025-08-26 7:19 ` [PATCH v6 mm-new 06/10] selftests/bpf: add test case for khugepaged fork Yafang Shao
2025-08-26 7:19 ` [PATCH v6 mm-new 07/10] selftests/bpf: add test case to update thp policy Yafang Shao
2025-08-26 7:19 ` Yafang Shao [this message]
2025-08-26 7:19 ` [PATCH v6 mm-new 09/10] Documentation: add BPF-based THP adjustment documentation Yafang Shao
2025-08-26 7:19 ` [PATCH v6 mm-new 10/10] MAINTAINERS: add entry for BPF-based THP adjustment Yafang Shao
2025-08-27 15:47 ` Lorenzo Stoakes
2025-08-28 6:08 ` Yafang Shao
2025-08-26 7:42 ` [PATCH v6 mm-new 00/10] mm, bpf: BPF based THP order selection David Hildenbrand
2025-08-26 8:33 ` Lorenzo Stoakes
2025-08-26 12:06 ` Yafang Shao
2025-08-26 9:52 ` Usama Arif
2025-08-26 12:10 ` Yafang Shao
2025-08-26 12:03 ` Yafang Shao
2025-08-27 13:14 ` Lorenzo Stoakes
2025-08-28 2:58 ` 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=20250826071948.2618-9-laoar.shao@gmail.com \
--to=laoar.shao@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=baolin.wang@linux.alibaba.com \
--cc=bpf@vger.kernel.org \
--cc=corbet@lwn.net \
--cc=daniel@iogearbox.net \
--cc=david@redhat.com \
--cc=dev.jain@arm.com \
--cc=gutierrez.asier@huawei-partners.com \
--cc=hannes@cmpxchg.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=npache@redhat.com \
--cc=rientjes@google.com \
--cc=ryan.roberts@arm.com \
--cc=usamaarif642@gmail.com \
--cc=willy@infradead.org \
--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;
as well as URLs for NNTP newsgroup(s).