[PATCH bpf-next v1 3/3] selftests/bpf: Test loads through __iomem pointers

From: Kumar Kartikeya Dwivedi

Date: Fri Nov 27 2026 - 21:53:42 EST


Give bpf_testmod a function with an __iomem struct pointer argument, and a
struct with two __iomem members, one pointing to a struct and one to an
int. Check that a program cannot load through any of the three, and that
it can still read the value of the pointers.

The subtests are skipped when the BTF of bpf_testmod has no "iomem" type
tag.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@xxxxxxxxx>
---
.../selftests/bpf/prog_tests/btf_tag.c | 55 +++++++++++++++++++
.../selftests/bpf/progs/btf_type_tag_iomem.c | 49 +++++++++++++++++
.../selftests/bpf/test_kmods/bpf_testmod.c | 15 +++++
3 files changed, 119 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/btf_type_tag_iomem.c

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_tag.c b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
index 071430cd54de..164359841381 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_tag.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
@@ -11,6 +11,7 @@ struct btf_type_tag_test {
#include "btf_type_tag.skel.h"
#include "btf_type_tag_user.skel.h"
#include "btf_type_tag_percpu.skel.h"
+#include "btf_type_tag_iomem.skel.h"

static void test_btf_decl_tag(void)
{
@@ -224,6 +225,51 @@ static void test_btf_type_tag_vmlinux_percpu(bool load_test)
btf__free(vmlinux_btf);
}

+/* Load only @name. A load through an __iomem pointer has to fail. */
+static void test_btf_type_tag_mod_iomem(const char *name, bool fails)
+{
+ struct btf *vmlinux_btf = NULL, *module_btf = NULL;
+ struct btf_type_tag_iomem *skel = NULL;
+ struct bpf_program *prog;
+ char log[4096] = {};
+ int err;
+
+ if (load_btfs(&vmlinux_btf, &module_btf, /*needs_vmlinux_tag=*/false))
+ return;
+
+ /* skip the test if the module does not have __iomem tags */
+ if (btf__find_by_name_kind(module_btf, "iomem", BTF_KIND_TYPE_TAG) <= 0) {
+ printf("%s:SKIP: no __iomem btf_type_tag in bpf_testmod", __func__);
+ test__skip();
+ goto cleanup;
+ }
+
+ skel = btf_type_tag_iomem__open();
+ if (!ASSERT_OK_PTR(skel, "btf_type_tag_iomem"))
+ goto cleanup;
+
+ bpf_object__for_each_program(prog, skel->obj)
+ bpf_program__set_autoload(prog, !strcmp(bpf_program__name(prog), name));
+
+ prog = bpf_object__find_program_by_name(skel->obj, name);
+ if (!ASSERT_OK_PTR(prog, "find_program"))
+ goto cleanup;
+ bpf_program__set_log_buf(prog, log, sizeof(log));
+
+ err = btf_type_tag_iomem__load(skel);
+ if (fails) {
+ ASSERT_ERR(err, name);
+ ASSERT_HAS_SUBSTR(log, "invalid mem access 'scalar'", "log");
+ } else {
+ ASSERT_OK(err, name);
+ }
+
+cleanup:
+ btf_type_tag_iomem__destroy(skel);
+ btf__free(module_btf);
+ btf__free(vmlinux_btf);
+}
+
void test_btf_tag(void)
{
if (test__start_subtest("btf_decl_tag"))
@@ -246,4 +292,13 @@ void test_btf_tag(void)
test_btf_type_tag_vmlinux_percpu(true);
if (test__start_subtest("btf_type_tag_percpu_vmlinux_helper"))
test_btf_type_tag_vmlinux_percpu(false);
+
+ if (test__start_subtest("btf_type_tag_iomem_arg"))
+ test_btf_type_tag_mod_iomem("test_iomem_arg", true);
+ if (test__start_subtest("btf_type_tag_iomem_struct"))
+ test_btf_type_tag_mod_iomem("test_iomem_struct", true);
+ if (test__start_subtest("btf_type_tag_iomem_int"))
+ test_btf_type_tag_mod_iomem("test_iomem_int", true);
+ if (test__start_subtest("btf_type_tag_iomem_value"))
+ test_btf_type_tag_mod_iomem("test_iomem_value", false);
}
diff --git a/tools/testing/selftests/bpf/progs/btf_type_tag_iomem.c b/tools/testing/selftests/bpf/progs/btf_type_tag_iomem.c
new file mode 100644
index 000000000000..29f1faa318e8
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf_type_tag_iomem.c
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+struct bpf_testmod_btf_type_tag_1 {
+ int a;
+};
+
+struct bpf_testmod_btf_type_tag_4 {
+ struct bpf_testmod_btf_type_tag_1 *p;
+ int *q;
+};
+
+long g;
+
+/* The argument is an __iomem pointer to a struct. */
+SEC("fentry/bpf_testmod_test_btf_type_tag_iomem_1")
+int BPF_PROG(test_iomem_arg, struct bpf_testmod_btf_type_tag_1 *arg)
+{
+ g = arg->a;
+ return 0;
+}
+
+/* The member is an __iomem pointer to a struct. */
+SEC("fentry/bpf_testmod_test_btf_type_tag_iomem_2")
+int BPF_PROG(test_iomem_struct, struct bpf_testmod_btf_type_tag_4 *arg)
+{
+ g = arg->p->a;
+ return 0;
+}
+
+/* The member is an __iomem pointer to an int. */
+SEC("fentry/bpf_testmod_test_btf_type_tag_iomem_2")
+int BPF_PROG(test_iomem_int, struct bpf_testmod_btf_type_tag_4 *arg)
+{
+ g = *arg->q;
+ return 0;
+}
+
+/* The value of the pointers can still be read. */
+SEC("fentry/bpf_testmod_test_btf_type_tag_iomem_2")
+int BPF_PROG(test_iomem_value, struct bpf_testmod_btf_type_tag_4 *arg)
+{
+ g = (long)arg->p + (long)arg->q;
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 1fbc0061cc24..debcc58418e7 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -650,6 +650,11 @@ struct bpf_testmod_btf_type_tag_3 {
struct bpf_testmod_btf_type_tag_1 __percpu *p;
};

+struct bpf_testmod_btf_type_tag_4 {
+ struct bpf_testmod_btf_type_tag_1 __iomem *p;
+ int __iomem *q;
+};
+
noinline int
bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user *arg) {
BTF_TYPE_EMIT(func_proto_typedef);
@@ -673,6 +678,16 @@ bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
return arg->p->a;
}

+noinline int
+bpf_testmod_test_btf_type_tag_iomem_1(struct bpf_testmod_btf_type_tag_1 __iomem *arg) {
+ return !!arg;
+}
+
+noinline int
+bpf_testmod_test_btf_type_tag_iomem_2(struct bpf_testmod_btf_type_tag_4 *arg) {
+ return arg->p && arg->q;
+}
+
noinline int bpf_testmod_loop_test(int n)
{
/* Make sum volatile, so smart compilers, such as clang, will not
--
2.53.1-Meta