From: Menglong Dong <menglong8.dong@gmail.com>
To: martin.lau@linux.dev, leon.hwang@linux.dev
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
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, shuah@kernel.org,
jiang.biao@linux.dev, linux-kernel@vger.kernel.org,
bpf@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: [PATCH bpf 2/2] selftests/bpf: test map deadlock caused by NMI
Date: Thu, 30 Oct 2025 11:00:10 +0800 [thread overview]
Message-ID: <20251030030010.95352-3-dongml2@chinatelecom.cn> (raw)
In-Reply-To: <20251030030010.95352-1-dongml2@chinatelecom.cn>
In this testing, map updating and deleting both happen in NMI context and
user context, which is used to detect the possible deadlock.
For now, LRU is added in the testing, and more map type can be added in
the feature.
Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
.../selftests/bpf/prog_tests/map_deadlock.c | 136 ++++++++++++++++++
.../selftests/bpf/progs/map_deadlock.c | 52 +++++++
2 files changed, 188 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/map_deadlock.c
create mode 100644 tools/testing/selftests/bpf/progs/map_deadlock.c
diff --git a/tools/testing/selftests/bpf/prog_tests/map_deadlock.c b/tools/testing/selftests/bpf/prog_tests/map_deadlock.c
new file mode 100644
index 000000000000..17fcf1f5efa6
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/map_deadlock.c
@@ -0,0 +1,136 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <bpf/libbpf.h>
+#include <linux/perf_event.h>
+#include <sys/syscall.h>
+#include <sys/ioctl.h>
+#include <pthread.h>
+#include "map_deadlock.skel.h"
+
+
+static int perf_open_all_cpus(struct perf_event_attr *attr, int fds[], int max_cpus)
+{
+ int n = 0;
+
+ for (int cpu = 0; cpu < max_cpus; cpu++) {
+ int fd = syscall(__NR_perf_event_open, attr, -1 /* pid: all */, cpu,
+ -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
+ if (fd < 0)
+ continue;
+ fds[cpu] = fd;
+ n++;
+ }
+ return n;
+}
+
+struct thread_arg {
+ int map_fd;
+ bool *stop;
+};
+
+static void *user_update_thread(void *argp)
+{
+ struct thread_arg *arg = argp;
+ u32 key = 0;
+ u64 val = 1;
+
+ while (!*arg->stop) {
+ key++;
+ val++;
+ bpf_map_update_elem(arg->map_fd, &key, &val, BPF_ANY);
+ if ((key & 0x7) == 0)
+ bpf_map_delete_elem(arg->map_fd, &key);
+ }
+ return NULL;
+}
+
+static void test_map(const char *map_name, int map_index)
+{
+ struct perf_event_attr attr = {
+ .type = PERF_TYPE_HARDWARE,
+ .size = sizeof(struct perf_event_attr),
+ .config = PERF_COUNT_HW_CPU_CYCLES,
+ .sample_period = 1000000,
+ .freq = 0,
+ .disabled = 0,
+ .wakeup_events = 1,
+ };
+ int map_fd, nfd = 0, max_cpus, err;
+ struct bpf_link **links = NULL;
+ struct map_deadlock *skel;
+ struct bpf_program *prog;
+ struct thread_arg targ;
+ bool stop = false;
+ int *fds = NULL;
+ pthread_t thr;
+
+ skel = map_deadlock__open();
+ if (!ASSERT_OK_PTR(skel, "map_deadlock__open"))
+ return;
+ skel->rodata->map_index = map_index;
+ err = map_deadlock__load(skel);
+ if (!ASSERT_OK(err, "map_deadlock__load"))
+ goto out;
+
+ prog = skel->progs.on_perf;
+ map_fd = bpf_object__find_map_fd_by_name(skel->obj, map_name);
+ if (!ASSERT_GE(map_fd, 0, map_name))
+ goto out;
+
+ max_cpus = libbpf_num_possible_cpus();
+ if (!ASSERT_GT(max_cpus, 0, "num cpus"))
+ goto out;
+
+ links = calloc(max_cpus, sizeof(*links));
+ ASSERT_OK_PTR(links, "alloc links");
+ fds = calloc(max_cpus, sizeof(*fds));
+ ASSERT_OK_PTR(fds, "alloc fds");
+ for (int i = 0; i < max_cpus; i++)
+ fds[i] = -1;
+
+ nfd = perf_open_all_cpus(&attr, fds, max_cpus);
+ if (!ASSERT_GT(nfd, 0, "perf fds"))
+ goto out;
+
+ for (int cpu = 0; cpu < max_cpus; cpu++) {
+ if (fds[cpu] < 0)
+ continue;
+ links[cpu] = bpf_program__attach_perf_event(prog, fds[cpu]);
+ if (!ASSERT_OK_PTR(links[cpu], "attach perf"))
+ goto out;
+ }
+
+ targ.map_fd = map_fd;
+ targ.stop = &stop;
+ err = pthread_create(&thr, NULL, user_update_thread, &targ);
+ if (!ASSERT_OK(err, "create thr"))
+ goto out;
+
+ /* 1 second should be enough to trigger the deadlock */
+ sleep(1);
+ stop = true;
+ (void)pthread_join(thr, NULL);
+ /* TODO: read dmesg to check the deadlock? */
+out:
+ if (links) {
+ for (int cpu = 0; cpu < max_cpus; cpu++) {
+ if (links[cpu])
+ bpf_link__destroy(links[cpu]);
+ }
+ }
+ if (fds) {
+ for (int cpu = 0; cpu < max_cpus; cpu++) {
+ if (fds[cpu] >= 0)
+ close(fds[cpu]);
+ }
+ }
+ free(links);
+ free(fds);
+ map_deadlock__destroy(skel);
+}
+
+void test_map_deadlock(void)
+{
+ if (test__start_subtest("lru"))
+ test_map("lru_map", 0);
+}
diff --git a/tools/testing/selftests/bpf/progs/map_deadlock.c b/tools/testing/selftests/bpf/progs/map_deadlock.c
new file mode 100644
index 000000000000..6966224955fc
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/map_deadlock.c
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char LICENSE[] SEC("license") = "GPL";
+
+struct lru_map {
+ __uint(type, BPF_MAP_TYPE_LRU_HASH);
+ __uint(max_entries, 1024);
+ __type(key, u32);
+ __type(value, u64);
+} lru_map SEC(".maps");
+
+struct map_list {
+ __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
+ __uint(max_entries, 1);
+ __uint(key_size, sizeof(int));
+ __uint(value_size, sizeof(int));
+ __array(values, struct lru_map);
+} map_list SEC(".maps") = {
+ .values = { [0] = &lru_map },
+};
+
+const volatile int map_index;
+
+static __always_inline void do_update_delete(void *map)
+{
+ u64 ts = bpf_ktime_get_ns();
+ u32 key = (u32)(ts >> 12);
+ u64 val = ts;
+
+ if ((ts & 1) == 0)
+ bpf_map_update_elem(map, &key, &val, BPF_ANY);
+ else
+ bpf_map_delete_elem(map, &key);
+}
+
+SEC("perf_event")
+int on_perf(struct bpf_perf_event_data *ctx)
+{
+ int key = map_index;
+ void *target_map;
+
+ target_map = bpf_map_lookup_elem(&map_list, &key);
+ if (!target_map)
+ return 0;
+
+ for (int i = 0; i < 4; i++)
+ do_update_delete(target_map);
+ return 0;
+}
--
2.51.2
prev parent reply other threads:[~2025-10-30 3:00 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-30 3:00 [PATCH bpf 0/2] use rqspinlock for bpf lru map Menglong Dong
2025-10-30 3:00 ` [PATCH bpf 1/2] bpf: use rqspinlock for " Menglong Dong
2025-10-30 3:38 ` bot+bpf-ci
2025-10-30 22:38 ` Alexei Starovoitov
2025-10-31 2:12 ` Menglong Dong
2025-10-30 3:00 ` Menglong Dong [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=20251030030010.95352-3-dongml2@chinatelecom.cn \
--to=menglong8.dong@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=jiang.biao@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=leon.hwang@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/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
all inboxes | Powered by JetHome®