BPF List
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@kernel.org>
Subject: [PATCH RFC bpf-next v1 2/2] [EXAMPLE] selftests/bpf: Add timer deadlock example
Date: Sun,  6 Nov 2022 07:21:52 +0530	[thread overview]
Message-ID: <20221106015152.2556188-3-memxor@gmail.com> (raw)
In-Reply-To: <20221106015152.2556188-1-memxor@gmail.com>

This is just an example to showcase that the deadlock can occur in
practice. Run this on an unfixed kernel by uncommenting the skipping
part in timer_deadlock.c

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 .../selftests/bpf/prog_tests/timer_deadlock.c | 29 +++++++++++
 .../selftests/bpf/progs/timer_deadlock.c      | 50 +++++++++++++++++++
 2 files changed, 79 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/timer_deadlock.c
 create mode 100644 tools/testing/selftests/bpf/progs/timer_deadlock.c

diff --git a/tools/testing/selftests/bpf/prog_tests/timer_deadlock.c b/tools/testing/selftests/bpf/prog_tests/timer_deadlock.c
new file mode 100644
index 000000000000..83657577d137
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/timer_deadlock.c
@@ -0,0 +1,29 @@
+#include <test_progs.h>
+#include <network_helpers.h>
+
+#include "timer_deadlock.skel.h"
+
+void test_timer_deadlock(void)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts,
+		.data_in = &pkt_v4,
+		.data_size_in = sizeof(pkt_v4),
+		.repeat = 1,
+	);
+	struct timer_deadlock *skel;
+
+	/* Remove to observe deadlock */
+	test__skip();
+	return;
+
+	skel = timer_deadlock__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "timer_deadlock__open_and_load"))
+		return;
+	if (!ASSERT_OK(timer_deadlock__attach(skel), "timer_deadlock__attach"))
+		goto end;
+	ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(skel->progs.tc_prog), &topts), "test_run");
+	ASSERT_EQ(topts.retval, 0, "test_run retval");
+end:
+	timer_deadlock__destroy(skel);
+}
+
diff --git a/tools/testing/selftests/bpf/progs/timer_deadlock.c b/tools/testing/selftests/bpf/progs/timer_deadlock.c
new file mode 100644
index 000000000000..ac05c7320144
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/timer_deadlock.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_helpers.h>
+
+int tid = 0;
+
+struct map_value {
+	struct bpf_timer timer;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__type(key, int);
+	__type(value, struct map_value);
+	__uint(max_entries, 1);
+} array_map SEC(".maps");
+
+static int cb(struct bpf_map *map, int *key, struct map_value *val)
+{
+	return 0;
+}
+
+SEC("tc")
+int tc_prog(void *ctx)
+{
+	struct task_struct *current = bpf_get_current_task_btf();
+	struct map_value *v, val = {};
+
+	v = bpf_map_lookup_elem(&array_map, &(int){0});
+	if (!v)
+		return 0;
+	bpf_timer_init(&v->timer, &array_map, 0);
+	bpf_timer_set_callback(&v->timer, &cb);
+
+	tid = current->pid;
+	return bpf_map_update_elem(&array_map, &(int){0}, &val, 0);
+}
+
+SEC("fentry/bpf_prog_put")
+int fentry_prog(void *ctx)
+{
+	struct map_value val = {};
+
+	if (tid == bpf_get_current_task_btf()->pid)
+		bpf_map_update_elem(&array_map, &(int){0}, &val, 0);
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
-- 
2.38.1


      parent reply	other threads:[~2022-11-06  1:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-06  1:51 [PATCH RFC bpf-next v1 0/2] Fix deadlock with bpf_timer in fentry/fexit progs Kumar Kartikeya Dwivedi
2022-11-06  1:51 ` [PATCH RFC bpf-next v1 1/2] bpf: Fix deadlock for bpf_timer's spinlock Kumar Kartikeya Dwivedi
2022-11-06 21:20   ` Alexei Starovoitov
2022-11-06 21:44     ` Kumar Kartikeya Dwivedi
2022-11-07  0:31       ` Alexei Starovoitov
2022-11-07  1:48         ` Kumar Kartikeya Dwivedi
2022-11-07  2:34           ` Alexei Starovoitov
2022-11-07  3:45             ` Alexei Starovoitov
2022-11-07 23:13               ` Kumar Kartikeya Dwivedi
2022-11-06  1:51 ` Kumar Kartikeya Dwivedi [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=20221106015152.2556188-3-memxor@gmail.com \
    --to=memxor@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=martin.lau@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox