diff mbox series

[PATCHv3,bpf,2/2] selftests/bpf: Add test for early update in prog_array_map_poke_run

Message ID 20231203204851.388654-3-jolsa@kernel.org (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: Fix map poke update | expand

Checks

Context Check Description
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-2 success Logs for Validate matrix.py
bpf/vmtest-bpf-VM_Test-3 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-4 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-7 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-8 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-VM_Test-9 success Logs for s390x-gcc / build / build for s390x with gcc
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf, async
netdev/ynl success SINGLE THREAD; Generated files up to date; no warnings/errors;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/cc_maintainers warning 7 maintainers not CCed: kpsingh@kernel.org shuah@kernel.org linux-kselftest@vger.kernel.org martin.lau@linux.dev mykolal@fb.com yonghong.song@linux.dev song@kernel.org
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-VM_Test-5 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-6 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-14 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-VM_Test-13 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-15 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-16 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-17 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-18 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-19 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-20 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-23 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-21 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-22 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-24 success Logs for x86_64-llvm-16 / build / build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-26 success Logs for x86_64-llvm-16 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-25 success Logs for x86_64-llvm-16 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-27 success Logs for x86_64-llvm-16 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-28 success Logs for x86_64-llvm-16 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-29 success Logs for x86_64-llvm-16 / veristat
bpf/vmtest-bpf-VM_Test-12 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-VM_Test-11 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-VM_Test-10 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc

Commit Message

Jiri Olsa Dec. 3, 2023, 8:48 p.m. UTC
Adding test that tries to trigger the BUG_ON during early map update
in prog_array_map_poke_run function.

The idea is to share prog array map between thread that constantly
updates it and another one loading a program that uses that prog
array.

Eventually we will hit a place where the program is ok to be updated
(poke->tailcall_target_stable check) but the address is still not
registered in kallsyms, so the bpf_arch_text_poke returns -EINVAL
and cause imbalance for the next tail call update check, which will
fail with -EBUSY in bpf_arch_text_poke as described in previous fix.

Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 .../selftests/bpf/prog_tests/tailcall_poke.c  | 74 +++++++++++++++++++
 .../selftests/bpf/progs/tailcall_poke.c       | 32 ++++++++
 2 files changed, 106 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
 create mode 100644 tools/testing/selftests/bpf/progs/tailcall_poke.c

Comments

Yonghong Song Dec. 5, 2023, 5:16 a.m. UTC | #1
On 12/3/23 3:48 PM, Jiri Olsa wrote:
> Adding test that tries to trigger the BUG_ON during early map update
> in prog_array_map_poke_run function.
>
> The idea is to share prog array map between thread that constantly
> updates it and another one loading a program that uses that prog
> array.
>
> Eventually we will hit a place where the program is ok to be updated
> (poke->tailcall_target_stable check) but the address is still not
> registered in kallsyms, so the bpf_arch_text_poke returns -EINVAL
> and cause imbalance for the next tail call update check, which will
> fail with -EBUSY in bpf_arch_text_poke as described in previous fix.
>
> Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
>   .../selftests/bpf/prog_tests/tailcall_poke.c  | 74 +++++++++++++++++++
>   .../selftests/bpf/progs/tailcall_poke.c       | 32 ++++++++
>   2 files changed, 106 insertions(+)
>   create mode 100644 tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
>   create mode 100644 tools/testing/selftests/bpf/progs/tailcall_poke.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c b/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
> new file mode 100644
> index 000000000000..f7e2c09fd772
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
> @@ -0,0 +1,74 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <unistd.h>
> +#include <test_progs.h>
> +#include "tailcall_poke.skel.h"
> +
> +#define JMP_TABLE "/sys/fs/bpf/jmp_table"
> +
> +static int thread_exit;
> +
> +static void *update(void *arg)
> +{
> +	__u32 zero = 0, prog1_fd, prog2_fd, map_fd;
> +	struct tailcall_poke *call = arg;
> +
> +	map_fd = bpf_map__fd(call->maps.jmp_table);
> +	prog1_fd = bpf_program__fd(call->progs.call1);
> +	prog2_fd = bpf_program__fd(call->progs.call2);
> +
> +	while (!thread_exit) {
> +		bpf_map_update_elem(map_fd, &zero, &prog1_fd, BPF_ANY);
> +		bpf_map_update_elem(map_fd, &zero, &prog2_fd, BPF_ANY);
> +	}
> +
> +	return NULL;
> +}
> +
> +void test_tailcall_poke(void)
> +{
> +	struct tailcall_poke *call, *test;
> +	int err, cnt = 10;
> +	pthread_t thread;
> +
> +	unlink(JMP_TABLE);
> +
> +	call = tailcall_poke__open_and_load();
> +	if (!ASSERT_OK_PTR(call, "tailcall_poke__open"))
> +		return;
> +
> +	err = bpf_map__pin(call->maps.jmp_table, JMP_TABLE);
> +	if (!ASSERT_OK(err, "bpf_map__pin"))
> +		goto out;

Just curious. What is the reason having bpf_map__pin() here
and below? I tried and it looks like removing bpf_map__pin()
and below bpf_map__set_pin_path() will make reproducing
the failure hard/impossible.

> +
> +	err = pthread_create(&thread, NULL, update, call);
> +	if (!ASSERT_OK(err, "new toggler"))
> +		goto out;
> +
> +	while (cnt--) {
> +		test = tailcall_poke__open();
> +		if (!ASSERT_OK_PTR(test, "tailcall_poke__open"))
> +			break;
> +
> +		err = bpf_map__set_pin_path(test->maps.jmp_table, JMP_TABLE);
> +		if (!ASSERT_OK(err, "bpf_map__pin")) {
> +			tailcall_poke__destroy(test);
> +			break;
> +		}
> +
> +		bpf_program__set_autoload(test->progs.test, true);
> +		bpf_program__set_autoload(test->progs.call1, false);
> +		bpf_program__set_autoload(test->progs.call2, false);
> +
> +		err = tailcall_poke__load(test);
> +		tailcall_poke__destroy(test);
> +		if (!ASSERT_OK(err, "tailcall_poke__load"))
> +			break;
> +	}
> +
> +	thread_exit = 1;
> +	ASSERT_OK(pthread_join(thread, NULL), "pthread_join");
> +
> +out:
> +	bpf_map__unpin(call->maps.jmp_table, JMP_TABLE);
> +	tailcall_poke__destroy(call);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/tailcall_poke.c b/tools/testing/selftests/bpf/progs/tailcall_poke.c
> new file mode 100644
> index 000000000000..c78b94b75e83
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/tailcall_poke.c
> @@ -0,0 +1,32 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <linux/bpf.h>
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +char _license[] SEC("license") = "GPL";
> +
> +struct {
> +	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
> +	__uint(max_entries, 1);
> +	__uint(key_size, sizeof(__u32));
> +	__uint(value_size, sizeof(__u32));
> +} jmp_table SEC(".maps");
> +
> +SEC("?fentry/bpf_fentry_test1")
> +int BPF_PROG(test, int a)
> +{
> +	bpf_tail_call_static(ctx, &jmp_table, 0);
> +	return 0;
> +}
> +
> +SEC("fentry/bpf_fentry_test1")
> +int BPF_PROG(call1, int a)
> +{
> +	return 0;
> +}
> +
> +SEC("fentry/bpf_fentry_test1")
> +int BPF_PROG(call2, int a)
> +{
> +	return 0;
> +}
Jiri Olsa Dec. 5, 2023, 8:43 a.m. UTC | #2
On Mon, Dec 04, 2023 at 09:16:52PM -0800, Yonghong Song wrote:
> 
> On 12/3/23 3:48 PM, Jiri Olsa wrote:
> > Adding test that tries to trigger the BUG_ON during early map update
> > in prog_array_map_poke_run function.
> > 
> > The idea is to share prog array map between thread that constantly
> > updates it and another one loading a program that uses that prog
> > array.
> > 
> > Eventually we will hit a place where the program is ok to be updated
> > (poke->tailcall_target_stable check) but the address is still not
> > registered in kallsyms, so the bpf_arch_text_poke returns -EINVAL
> > and cause imbalance for the next tail call update check, which will
> > fail with -EBUSY in bpf_arch_text_poke as described in previous fix.
> > 
> > Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > ---
> >   .../selftests/bpf/prog_tests/tailcall_poke.c  | 74 +++++++++++++++++++
> >   .../selftests/bpf/progs/tailcall_poke.c       | 32 ++++++++
> >   2 files changed, 106 insertions(+)
> >   create mode 100644 tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
> >   create mode 100644 tools/testing/selftests/bpf/progs/tailcall_poke.c
> > 
> > diff --git a/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c b/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
> > new file mode 100644
> > index 000000000000..f7e2c09fd772
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
> > @@ -0,0 +1,74 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +#include <unistd.h>
> > +#include <test_progs.h>
> > +#include "tailcall_poke.skel.h"
> > +
> > +#define JMP_TABLE "/sys/fs/bpf/jmp_table"
> > +
> > +static int thread_exit;
> > +
> > +static void *update(void *arg)
> > +{
> > +	__u32 zero = 0, prog1_fd, prog2_fd, map_fd;
> > +	struct tailcall_poke *call = arg;
> > +
> > +	map_fd = bpf_map__fd(call->maps.jmp_table);
> > +	prog1_fd = bpf_program__fd(call->progs.call1);
> > +	prog2_fd = bpf_program__fd(call->progs.call2);
> > +
> > +	while (!thread_exit) {
> > +		bpf_map_update_elem(map_fd, &zero, &prog1_fd, BPF_ANY);
> > +		bpf_map_update_elem(map_fd, &zero, &prog2_fd, BPF_ANY);
> > +	}
> > +
> > +	return NULL;
> > +}
> > +
> > +void test_tailcall_poke(void)
> > +{
> > +	struct tailcall_poke *call, *test;
> > +	int err, cnt = 10;
> > +	pthread_t thread;
> > +
> > +	unlink(JMP_TABLE);
> > +
> > +	call = tailcall_poke__open_and_load();
> > +	if (!ASSERT_OK_PTR(call, "tailcall_poke__open"))
> > +		return;
> > +
> > +	err = bpf_map__pin(call->maps.jmp_table, JMP_TABLE);
> > +	if (!ASSERT_OK(err, "bpf_map__pin"))
> > +		goto out;
> 
> Just curious. What is the reason having bpf_map__pin() here
> and below? I tried and it looks like removing bpf_map__pin()
> and below bpf_map__set_pin_path() will make reproducing
> the failure hard/impossible.

yes, it's there to share the jmp_table map between the two
skeleton instances, so the update thread changes the same
jmp_table map that's used in the skeleton we load in the
while loop below

I'll add some comments to the test

jirka

> 
> > +
> > +	err = pthread_create(&thread, NULL, update, call);
> > +	if (!ASSERT_OK(err, "new toggler"))
> > +		goto out;
> > +
> > +	while (cnt--) {
> > +		test = tailcall_poke__open();
> > +		if (!ASSERT_OK_PTR(test, "tailcall_poke__open"))
> > +			break;
> > +
> > +		err = bpf_map__set_pin_path(test->maps.jmp_table, JMP_TABLE);
> > +		if (!ASSERT_OK(err, "bpf_map__pin")) {
> > +			tailcall_poke__destroy(test);
> > +			break;
> > +		}
> > +
> > +		bpf_program__set_autoload(test->progs.test, true);
> > +		bpf_program__set_autoload(test->progs.call1, false);
> > +		bpf_program__set_autoload(test->progs.call2, false);
> > +
> > +		err = tailcall_poke__load(test);
> > +		tailcall_poke__destroy(test);
> > +		if (!ASSERT_OK(err, "tailcall_poke__load"))
> > +			break;
> > +	}
> > +
> > +	thread_exit = 1;
> > +	ASSERT_OK(pthread_join(thread, NULL), "pthread_join");
> > +
> > +out:
> > +	bpf_map__unpin(call->maps.jmp_table, JMP_TABLE);
> > +	tailcall_poke__destroy(call);
> > +}

SNIP
Yonghong Song Dec. 5, 2023, 4 p.m. UTC | #3
On 12/5/23 3:43 AM, Jiri Olsa wrote:
> On Mon, Dec 04, 2023 at 09:16:52PM -0800, Yonghong Song wrote:
>> On 12/3/23 3:48 PM, Jiri Olsa wrote:
>>> Adding test that tries to trigger the BUG_ON during early map update
>>> in prog_array_map_poke_run function.
>>>
>>> The idea is to share prog array map between thread that constantly
>>> updates it and another one loading a program that uses that prog
>>> array.
>>>
>>> Eventually we will hit a place where the program is ok to be updated
>>> (poke->tailcall_target_stable check) but the address is still not
>>> registered in kallsyms, so the bpf_arch_text_poke returns -EINVAL
>>> and cause imbalance for the next tail call update check, which will
>>> fail with -EBUSY in bpf_arch_text_poke as described in previous fix.
>>>
>>> Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
>>> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
>>> ---
>>>    .../selftests/bpf/prog_tests/tailcall_poke.c  | 74 +++++++++++++++++++
>>>    .../selftests/bpf/progs/tailcall_poke.c       | 32 ++++++++
>>>    2 files changed, 106 insertions(+)
>>>    create mode 100644 tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
>>>    create mode 100644 tools/testing/selftests/bpf/progs/tailcall_poke.c
>>>
>>> diff --git a/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c b/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
>>> new file mode 100644
>>> index 000000000000..f7e2c09fd772
>>> --- /dev/null
>>> +++ b/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
>>> @@ -0,0 +1,74 @@
>>> +// SPDX-License-Identifier: GPL-2.0
>>> +#include <unistd.h>
>>> +#include <test_progs.h>
>>> +#include "tailcall_poke.skel.h"
>>> +
>>> +#define JMP_TABLE "/sys/fs/bpf/jmp_table"
>>> +
>>> +static int thread_exit;
>>> +
>>> +static void *update(void *arg)
>>> +{
>>> +	__u32 zero = 0, prog1_fd, prog2_fd, map_fd;
>>> +	struct tailcall_poke *call = arg;
>>> +
>>> +	map_fd = bpf_map__fd(call->maps.jmp_table);
>>> +	prog1_fd = bpf_program__fd(call->progs.call1);
>>> +	prog2_fd = bpf_program__fd(call->progs.call2);
>>> +
>>> +	while (!thread_exit) {
>>> +		bpf_map_update_elem(map_fd, &zero, &prog1_fd, BPF_ANY);
>>> +		bpf_map_update_elem(map_fd, &zero, &prog2_fd, BPF_ANY);
>>> +	}
>>> +
>>> +	return NULL;
>>> +}
>>> +
>>> +void test_tailcall_poke(void)
>>> +{
>>> +	struct tailcall_poke *call, *test;
>>> +	int err, cnt = 10;
>>> +	pthread_t thread;
>>> +
>>> +	unlink(JMP_TABLE);
>>> +
>>> +	call = tailcall_poke__open_and_load();
>>> +	if (!ASSERT_OK_PTR(call, "tailcall_poke__open"))
>>> +		return;
>>> +
>>> +	err = bpf_map__pin(call->maps.jmp_table, JMP_TABLE);
>>> +	if (!ASSERT_OK(err, "bpf_map__pin"))
>>> +		goto out;
>> Just curious. What is the reason having bpf_map__pin() here
>> and below? I tried and it looks like removing bpf_map__pin()
>> and below bpf_map__set_pin_path() will make reproducing
>> the failure hard/impossible.
> yes, it's there to share the jmp_table map between the two
> skeleton instances, so the update thread changes the same
> jmp_table map that's used in the skeleton we load in the
> while loop below

This does make sense.

>
> I'll add some comments to the test

Thanks for explanation. Some comments are definitely helpful!

>
> jirka
>
>>> +
>>> +	err = pthread_create(&thread, NULL, update, call);
>>> +	if (!ASSERT_OK(err, "new toggler"))
>>> +		goto out;
>>> +
>>> +	while (cnt--) {
>>> +		test = tailcall_poke__open();
>>> +		if (!ASSERT_OK_PTR(test, "tailcall_poke__open"))
>>> +			break;
>>> +
>>> +		err = bpf_map__set_pin_path(test->maps.jmp_table, JMP_TABLE);
>>> +		if (!ASSERT_OK(err, "bpf_map__pin")) {
>>> +			tailcall_poke__destroy(test);
>>> +			break;
>>> +		}
>>> +
>>> +		bpf_program__set_autoload(test->progs.test, true);
>>> +		bpf_program__set_autoload(test->progs.call1, false);
>>> +		bpf_program__set_autoload(test->progs.call2, false);
>>> +
>>> +		err = tailcall_poke__load(test);
>>> +		tailcall_poke__destroy(test);
>>> +		if (!ASSERT_OK(err, "tailcall_poke__load"))
>>> +			break;
>>> +	}
>>> +
>>> +	thread_exit = 1;
>>> +	ASSERT_OK(pthread_join(thread, NULL), "pthread_join");
>>> +
>>> +out:
>>> +	bpf_map__unpin(call->maps.jmp_table, JMP_TABLE);
>>> +	tailcall_poke__destroy(call);
>>> +}
> SNIP
Jiri Olsa Dec. 5, 2023, 9:57 p.m. UTC | #4
On Tue, Dec 05, 2023 at 08:00:48AM -0800, Yonghong Song wrote:

SNIP

> > > > +void test_tailcall_poke(void)
> > > > +{
> > > > +	struct tailcall_poke *call, *test;
> > > > +	int err, cnt = 10;
> > > > +	pthread_t thread;
> > > > +
> > > > +	unlink(JMP_TABLE);
> > > > +
> > > > +	call = tailcall_poke__open_and_load();
> > > > +	if (!ASSERT_OK_PTR(call, "tailcall_poke__open"))
> > > > +		return;
> > > > +
> > > > +	err = bpf_map__pin(call->maps.jmp_table, JMP_TABLE);
> > > > +	if (!ASSERT_OK(err, "bpf_map__pin"))
> > > > +		goto out;
> > > Just curious. What is the reason having bpf_map__pin() here
> > > and below? I tried and it looks like removing bpf_map__pin()
> > > and below bpf_map__set_pin_path() will make reproducing
> > > the failure hard/impossible.
> > yes, it's there to share the jmp_table map between the two
> > skeleton instances, so the update thread changes the same
> > jmp_table map that's used in the skeleton we load in the
> > while loop below
> 
> This does make sense.
> 
> > 
> > I'll add some comments to the test
> 
> Thanks for explanation. Some comments are definitely helpful!

np, also looks like I should move this to prog_tests/tailcalls.c,
will send new version with that

thanks,
jirka


> 
> > 
> > jirka
> > 
> > > > +
> > > > +	err = pthread_create(&thread, NULL, update, call);
> > > > +	if (!ASSERT_OK(err, "new toggler"))
> > > > +		goto out;
> > > > +
> > > > +	while (cnt--) {
> > > > +		test = tailcall_poke__open();
> > > > +		if (!ASSERT_OK_PTR(test, "tailcall_poke__open"))
> > > > +			break;
> > > > +
> > > > +		err = bpf_map__set_pin_path(test->maps.jmp_table, JMP_TABLE);
> > > > +		if (!ASSERT_OK(err, "bpf_map__pin")) {
> > > > +			tailcall_poke__destroy(test);
> > > > +			break;
> > > > +		}
> > > > +
> > > > +		bpf_program__set_autoload(test->progs.test, true);
> > > > +		bpf_program__set_autoload(test->progs.call1, false);
> > > > +		bpf_program__set_autoload(test->progs.call2, false);
> > > > +
> > > > +		err = tailcall_poke__load(test);
> > > > +		tailcall_poke__destroy(test);
> > > > +		if (!ASSERT_OK(err, "tailcall_poke__load"))
> > > > +			break;
> > > > +	}
> > > > +
> > > > +	thread_exit = 1;
> > > > +	ASSERT_OK(pthread_join(thread, NULL), "pthread_join");
> > > > +
> > > > +out:
> > > > +	bpf_map__unpin(call->maps.jmp_table, JMP_TABLE);
> > > > +	tailcall_poke__destroy(call);
> > > > +}
> > SNIP
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c b/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
new file mode 100644
index 000000000000..f7e2c09fd772
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/tailcall_poke.c
@@ -0,0 +1,74 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <unistd.h>
+#include <test_progs.h>
+#include "tailcall_poke.skel.h"
+
+#define JMP_TABLE "/sys/fs/bpf/jmp_table"
+
+static int thread_exit;
+
+static void *update(void *arg)
+{
+	__u32 zero = 0, prog1_fd, prog2_fd, map_fd;
+	struct tailcall_poke *call = arg;
+
+	map_fd = bpf_map__fd(call->maps.jmp_table);
+	prog1_fd = bpf_program__fd(call->progs.call1);
+	prog2_fd = bpf_program__fd(call->progs.call2);
+
+	while (!thread_exit) {
+		bpf_map_update_elem(map_fd, &zero, &prog1_fd, BPF_ANY);
+		bpf_map_update_elem(map_fd, &zero, &prog2_fd, BPF_ANY);
+	}
+
+	return NULL;
+}
+
+void test_tailcall_poke(void)
+{
+	struct tailcall_poke *call, *test;
+	int err, cnt = 10;
+	pthread_t thread;
+
+	unlink(JMP_TABLE);
+
+	call = tailcall_poke__open_and_load();
+	if (!ASSERT_OK_PTR(call, "tailcall_poke__open"))
+		return;
+
+	err = bpf_map__pin(call->maps.jmp_table, JMP_TABLE);
+	if (!ASSERT_OK(err, "bpf_map__pin"))
+		goto out;
+
+	err = pthread_create(&thread, NULL, update, call);
+	if (!ASSERT_OK(err, "new toggler"))
+		goto out;
+
+	while (cnt--) {
+		test = tailcall_poke__open();
+		if (!ASSERT_OK_PTR(test, "tailcall_poke__open"))
+			break;
+
+		err = bpf_map__set_pin_path(test->maps.jmp_table, JMP_TABLE);
+		if (!ASSERT_OK(err, "bpf_map__pin")) {
+			tailcall_poke__destroy(test);
+			break;
+		}
+
+		bpf_program__set_autoload(test->progs.test, true);
+		bpf_program__set_autoload(test->progs.call1, false);
+		bpf_program__set_autoload(test->progs.call2, false);
+
+		err = tailcall_poke__load(test);
+		tailcall_poke__destroy(test);
+		if (!ASSERT_OK(err, "tailcall_poke__load"))
+			break;
+	}
+
+	thread_exit = 1;
+	ASSERT_OK(pthread_join(thread, NULL), "pthread_join");
+
+out:
+	bpf_map__unpin(call->maps.jmp_table, JMP_TABLE);
+	tailcall_poke__destroy(call);
+}
diff --git a/tools/testing/selftests/bpf/progs/tailcall_poke.c b/tools/testing/selftests/bpf/progs/tailcall_poke.c
new file mode 100644
index 000000000000..c78b94b75e83
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/tailcall_poke.c
@@ -0,0 +1,32 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+	__uint(max_entries, 1);
+	__uint(key_size, sizeof(__u32));
+	__uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+SEC("?fentry/bpf_fentry_test1")
+int BPF_PROG(test, int a)
+{
+	bpf_tail_call_static(ctx, &jmp_table, 0);
+	return 0;
+}
+
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(call1, int a)
+{
+	return 0;
+}
+
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(call2, int a)
+{
+	return 0;
+}