All of lore.kernel.org
 help / color / mirror / Atom feed
From: Amery Hung <ameryhung@gmail.com>
To: bpf@vger.kernel.org
Cc: alexei.starovoitov@gmail.com, andrii@kernel.org,
	daniel@iogearbox.net, tj@kernel.org, martin.lau@kernel.org,
	ameryhung@gmail.com, kernel-team@meta.com
Subject: [PATCH bpf-next v1 4/4] selftests/bpf: Test accessing struct_ops this pointer in timer callback
Date: Mon,  9 Jun 2025 16:27:46 -0700	[thread overview]
Message-ID: <20250609232746.1030044-4-ameryhung@gmail.com> (raw)
In-Reply-To: <20250609232746.1030044-1-ameryhung@gmail.com>

Check accessing aux->this_st_ops in timer callback. Make sure a kfunc in
a timer callback can get a correct this pointer when a struct_ops map is
attached, and NULL after the map is detached.

Signed-off-by: Amery Hung <ameryhung@gmail.com>
---
 .../bpf/prog_tests/test_struct_ops_this_ptr.c | 51 ++++++++++++++++
 .../bpf/progs/struct_ops_this_ptr_in_timer.c  | 60 +++++++++++++++++++
 .../selftests/bpf/test_kmods/bpf_testmod.c    |  1 +
 3 files changed, 112 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_this_ptr_in_timer.c

diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_this_ptr.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_this_ptr.c
index 6ef238a2050a..933f9310f462 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_this_ptr.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_this_ptr.c
@@ -3,8 +3,59 @@
 #include <test_progs.h>
 
 #include "struct_ops_this_ptr.skel.h"
+#include "struct_ops_this_ptr_in_timer.skel.h"
+
+static void test_struct_ops_this_ptr_in_timer_common(int timer_nsec, int expected_data)
+{
+	struct struct_ops_this_ptr_in_timer *skel;
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	struct bpf_link *link;
+	int err, prog_fd;
+
+	skel = struct_ops_this_ptr_in_timer__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+		return;
+
+	skel->bss->timer_nsec = timer_nsec;
+
+	link = bpf_map__attach_struct_ops(skel->maps.testmod_this_ptr);
+	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
+		goto out;
+
+	prog_fd = bpf_program__fd(skel->progs.syscall_this_ptr_in_timer);
+	err = bpf_prog_test_run_opts(prog_fd, &topts);
+	ASSERT_OK(err, "bpf_prog_test_run_opts");
+
+	bpf_link__destroy(link);
+
+	/* Check st_ops3_data after timer_cb runs */
+	while (!READ_ONCE(skel->bss->st_ops3_data))
+		sched_yield();
+	ASSERT_EQ(skel->bss->st_ops3_data, expected_data, "st_ops->data");
+out:
+	struct_ops_this_ptr_in_timer__destroy(skel);
+}
+
+static void test_struct_ops_this_ptr_in_timer(void)
+{
+	/* Run timer callback immediately */
+	test_struct_ops_this_ptr_in_timer_common(0, 1234);
+}
+
+static void test_struct_ops_this_ptr_in_timer_after_detach(void)
+{
+	/*
+	 * Run timer callback 0.1s after test run. By then the struct_ops map
+	 * should have been detached.
+	 */
+	test_struct_ops_this_ptr_in_timer_common(100000000, -1);
+}
 
 void serial_test_struct_ops_this_ptr(void)
 {
 	RUN_TESTS(struct_ops_this_ptr);
+	if (test__start_subtest("struct_ops_this_ptr_in_timer"))
+		test_struct_ops_this_ptr_in_timer();
+	if (test__start_subtest("struct_ops_this_ptr_in_timer_after_detach"))
+		test_struct_ops_this_ptr_in_timer_after_detach();
 }
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_this_ptr_in_timer.c b/tools/testing/selftests/bpf/progs/struct_ops_this_ptr_in_timer.c
new file mode 100644
index 000000000000..e5f76945e967
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/struct_ops_this_ptr_in_timer.c
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include "../test_kmods/bpf_testmod.h"
+#include "../test_kmods/bpf_testmod_kfunc.h"
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+struct elem {
+	struct bpf_timer timer;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, int);
+	__type(value, struct elem);
+} array_map SEC(".maps");
+
+int st_ops3_data;
+int timer_nsec;
+
+__noinline static int timer_cb(void *map, int *key, struct bpf_timer *timer)
+{
+	st_ops3_data = bpf_kfunc_st_ops_test_this_ptr_impl(NULL);
+	return 0;
+}
+
+SEC("struct_ops")
+int BPF_PROG(test1)
+{
+	struct bpf_timer *timer;
+	int key = 0;
+
+	timer = bpf_map_lookup_elem(&array_map, &key);
+	if (!timer)
+		return 0;
+
+	bpf_timer_init(timer, &array_map, 1);
+	bpf_timer_set_callback(timer, timer_cb);
+	bpf_timer_start(timer, timer_nsec, 0);
+	return 1;
+}
+
+SEC("syscall")
+__success __retval(1)
+int syscall_this_ptr_in_timer(void *ctx)
+{
+	return bpf_testmod_ops3_call_test_1();
+}
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops3 testmod_this_ptr = {
+	.test_1 = (void *)test1,
+	.data = 1234,
+};
+
+
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index f692ee43d25c..65953cd63b7c 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -1157,6 +1157,7 @@ static const struct bpf_verifier_ops bpf_testmod_verifier_ops = {
 };
 
 static const struct bpf_verifier_ops bpf_testmod_verifier_ops3 = {
+	.get_func_proto	 = bpf_base_func_proto,
 	.is_valid_access = bpf_testmod_ops_is_valid_access,
 };
 
-- 
2.47.1


  parent reply	other threads:[~2025-06-09 23:27 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-09 23:27 [PATCH bpf-next v1 1/4] bpf: Save struct_ops instance pointer in bpf_prog_aux Amery Hung
2025-06-09 23:27 ` [PATCH bpf-next v1 2/4] bpf: Allow verifier to fixup kernel module kfuncs Amery Hung
2025-06-09 23:27 ` [PATCH bpf-next v1 3/4] selftests/bpf: Test accessing struct_ops this pointer Amery Hung
2025-06-09 23:27 ` Amery Hung [this message]
     [not found]   ` <8c4943cb40967d152abe032b4208a7cdd89b539da26783afaa61e37eb6663cbf@mail.kernel.org>
2025-06-10 16:45     ` [PATCH bpf-next v1 4/4] selftests/bpf: Test accessing struct_ops this pointer in timer callback Amery Hung
2025-06-10 22:15 ` [PATCH bpf-next v1 1/4] bpf: Save struct_ops instance pointer in bpf_prog_aux Tejun Heo
2025-06-11  7:16 ` kernel test robot
2025-06-12 23:08 ` Andrii Nakryiko
2025-06-18 22:18   ` Amery Hung
2025-06-24 15:38     ` Andrii Nakryiko
2025-06-24 15:59       ` Alexei Starovoitov
2025-06-24 19:41         ` Andrii Nakryiko

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=20250609232746.1030044-4-ameryhung@gmail.com \
    --to=ameryhung@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@meta.com \
    --cc=martin.lau@kernel.org \
    --cc=tj@kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.