]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
selftests/bpf: Add stacktrace map lookup_and_delete_elem test case
authorTao Chen <chen.dylane@linux.dev>
Thu, 25 Sep 2025 17:50:30 +0000 (01:50 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Thu, 25 Sep 2025 23:17:30 +0000 (16:17 -0700)
Add tests for stacktrace map lookup and delete:
1. use bpf_map_lookup_and_delete_elem to lookup and delete the target
   stack_id,
2. lookup the deleted stack_id again to double check.

Signed-off-by: Tao Chen <chen.dylane@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250925175030.1615837-3-chen.dylane@linux.dev
tools/testing/selftests/bpf/prog_tests/stacktrace_map.c
tools/testing/selftests/bpf/progs/stacktrace_map.c

index 2e3da2030a2df1e4589f80b9a809c48268119e28..c23b97414813abe6425d003996d5c1328328b2c0 100644 (file)
@@ -7,7 +7,8 @@ void test_stacktrace_map(void)
        struct stacktrace_map *skel;
        int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
        int err, stack_trace_len;
-       __u32 key, val, duration = 0;
+       __u32 key, val, stack_id, duration = 0;
+       __u64 stack[PERF_MAX_STACK_DEPTH];
 
        skel = stacktrace_map__open_and_load();
        if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
@@ -48,6 +49,14 @@ void test_stacktrace_map(void)
                  "err %d errno %d\n", err, errno))
                goto out;
 
+       stack_id = skel->bss->stack_id;
+       err = bpf_map_lookup_and_delete_elem(stackmap_fd, &stack_id,  stack);
+       if (!ASSERT_OK(err, "lookup and delete target stack_id"))
+               goto out;
+
+       err = bpf_map_lookup_elem(stackmap_fd, &stack_id, stack);
+       if (!ASSERT_EQ(err, -ENOENT, "lookup deleted stack_id"))
+               goto out;
 out:
        stacktrace_map__destroy(skel);
 }
index 47568007b6683b740c57484bf4fe9d720f36acce..0c77df05be7fce9a4c6bd7900e31de4bdd19780f 100644 (file)
@@ -50,6 +50,7 @@ struct sched_switch_args {
        int next_prio;
 };
 
+__u32 stack_id;
 SEC("tracepoint/sched/sched_switch")
 int oncpu(struct sched_switch_args *ctx)
 {
@@ -64,6 +65,7 @@ int oncpu(struct sched_switch_args *ctx)
        /* The size of stackmap and stackid_hmap should be the same */
        key = bpf_get_stackid(ctx, &stackmap, 0);
        if ((int)key >= 0) {
+               stack_id = key;
                bpf_map_update_elem(&stackid_hmap, &key, &val, 0);
                stack_p = bpf_map_lookup_elem(&stack_amap, &key);
                if (stack_p)