Skip to content

Commit

Permalink
selftests/bpf: use raw_tp program for atomic test
Browse files Browse the repository at this point in the history
Now atomic tests will attach fentry program and run it through
bpf_prog_test_run_opts(), but attaching fentry program depends on bpf
trampoline which is only available under x86-64. Considering many archs
have atomic support, using raw_tp program instead.

Signed-off-by: Hou Tao <houtao1@huawei.com>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
  • Loading branch information
Hou Tao authored and Nobody committed Feb 17, 2022
1 parent 3a6721d commit ea4dbf4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 83 deletions.
91 changes: 22 additions & 69 deletions tools/testing/selftests/bpf/prog_tests/atomics.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@
static void test_add(struct atomics_lskel *skel)
{
int err, prog_fd;
int link_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);

link_fd = atomics_lskel__add__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(add)"))
return;

/* No need to attach it, just run it directly */
prog_fd = skel->progs.add.prog_fd;
err = bpf_prog_test_run_opts(prog_fd, &topts);
if (!ASSERT_OK(err, "test_run_opts err"))
goto cleanup;
return;
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
goto cleanup;
return;

ASSERT_EQ(skel->data->add64_value, 3, "add64_value");
ASSERT_EQ(skel->bss->add64_result, 1, "add64_result");
Expand All @@ -31,27 +27,20 @@ static void test_add(struct atomics_lskel *skel)
ASSERT_EQ(skel->bss->add_stack_result, 1, "add_stack_result");

ASSERT_EQ(skel->data->add_noreturn_value, 3, "add_noreturn_value");

cleanup:
close(link_fd);
}

static void test_sub(struct atomics_lskel *skel)
{
int err, prog_fd;
int link_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);

link_fd = atomics_lskel__sub__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(sub)"))
return;

/* No need to attach it, just run it directly */
prog_fd = skel->progs.sub.prog_fd;
err = bpf_prog_test_run_opts(prog_fd, &topts);
if (!ASSERT_OK(err, "test_run_opts err"))
goto cleanup;
return;
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
goto cleanup;
return;

ASSERT_EQ(skel->data->sub64_value, -1, "sub64_value");
ASSERT_EQ(skel->bss->sub64_result, 1, "sub64_result");
Expand All @@ -63,27 +52,20 @@ static void test_sub(struct atomics_lskel *skel)
ASSERT_EQ(skel->bss->sub_stack_result, 1, "sub_stack_result");

ASSERT_EQ(skel->data->sub_noreturn_value, -1, "sub_noreturn_value");

cleanup:
close(link_fd);
}

static void test_and(struct atomics_lskel *skel)
{
int err, prog_fd;
int link_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);

link_fd = atomics_lskel__and__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(and)"))
return;

/* No need to attach it, just run it directly */
prog_fd = skel->progs.and.prog_fd;
err = bpf_prog_test_run_opts(prog_fd, &topts);
if (!ASSERT_OK(err, "test_run_opts err"))
goto cleanup;
return;
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
goto cleanup;
return;

ASSERT_EQ(skel->data->and64_value, 0x010ull << 32, "and64_value");
ASSERT_EQ(skel->bss->and64_result, 0x110ull << 32, "and64_result");
Expand All @@ -92,26 +74,20 @@ static void test_and(struct atomics_lskel *skel)
ASSERT_EQ(skel->bss->and32_result, 0x110, "and32_result");

ASSERT_EQ(skel->data->and_noreturn_value, 0x010ull << 32, "and_noreturn_value");
cleanup:
close(link_fd);
}

static void test_or(struct atomics_lskel *skel)
{
int err, prog_fd;
int link_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);

link_fd = atomics_lskel__or__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(or)"))
return;

/* No need to attach it, just run it directly */
prog_fd = skel->progs.or.prog_fd;
err = bpf_prog_test_run_opts(prog_fd, &topts);
if (!ASSERT_OK(err, "test_run_opts err"))
goto cleanup;
return;
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
goto cleanup;
return;

ASSERT_EQ(skel->data->or64_value, 0x111ull << 32, "or64_value");
ASSERT_EQ(skel->bss->or64_result, 0x110ull << 32, "or64_result");
Expand All @@ -120,26 +96,20 @@ static void test_or(struct atomics_lskel *skel)
ASSERT_EQ(skel->bss->or32_result, 0x110, "or32_result");

ASSERT_EQ(skel->data->or_noreturn_value, 0x111ull << 32, "or_noreturn_value");
cleanup:
close(link_fd);
}

static void test_xor(struct atomics_lskel *skel)
{
int err, prog_fd;
int link_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);

link_fd = atomics_lskel__xor__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(xor)"))
return;

/* No need to attach it, just run it directly */
prog_fd = skel->progs.xor.prog_fd;
err = bpf_prog_test_run_opts(prog_fd, &topts);
if (!ASSERT_OK(err, "test_run_opts err"))
goto cleanup;
return;
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
goto cleanup;
return;

ASSERT_EQ(skel->data->xor64_value, 0x101ull << 32, "xor64_value");
ASSERT_EQ(skel->bss->xor64_result, 0x110ull << 32, "xor64_result");
Expand All @@ -148,26 +118,20 @@ static void test_xor(struct atomics_lskel *skel)
ASSERT_EQ(skel->bss->xor32_result, 0x110, "xor32_result");

ASSERT_EQ(skel->data->xor_noreturn_value, 0x101ull << 32, "xor_nxoreturn_value");
cleanup:
close(link_fd);
}

static void test_cmpxchg(struct atomics_lskel *skel)
{
int err, prog_fd;
int link_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);

link_fd = atomics_lskel__cmpxchg__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(cmpxchg)"))
return;

/* No need to attach it, just run it directly */
prog_fd = skel->progs.cmpxchg.prog_fd;
err = bpf_prog_test_run_opts(prog_fd, &topts);
if (!ASSERT_OK(err, "test_run_opts err"))
goto cleanup;
return;
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
goto cleanup;
return;

ASSERT_EQ(skel->data->cmpxchg64_value, 2, "cmpxchg64_value");
ASSERT_EQ(skel->bss->cmpxchg64_result_fail, 1, "cmpxchg_result_fail");
Expand All @@ -176,45 +140,34 @@ static void test_cmpxchg(struct atomics_lskel *skel)
ASSERT_EQ(skel->data->cmpxchg32_value, 2, "lcmpxchg32_value");
ASSERT_EQ(skel->bss->cmpxchg32_result_fail, 1, "cmpxchg_result_fail");
ASSERT_EQ(skel->bss->cmpxchg32_result_succeed, 1, "cmpxchg_result_succeed");

cleanup:
close(link_fd);
}

static void test_xchg(struct atomics_lskel *skel)
{
int err, prog_fd;
int link_fd;
LIBBPF_OPTS(bpf_test_run_opts, topts);

link_fd = atomics_lskel__xchg__attach(skel);
if (!ASSERT_GT(link_fd, 0, "attach(xchg)"))
return;

/* No need to attach it, just run it directly */
prog_fd = skel->progs.xchg.prog_fd;
err = bpf_prog_test_run_opts(prog_fd, &topts);
if (!ASSERT_OK(err, "test_run_opts err"))
goto cleanup;
return;
if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
goto cleanup;
return;

ASSERT_EQ(skel->data->xchg64_value, 2, "xchg64_value");
ASSERT_EQ(skel->bss->xchg64_result, 1, "xchg64_result");

ASSERT_EQ(skel->data->xchg32_value, 2, "xchg32_value");
ASSERT_EQ(skel->bss->xchg32_result, 1, "xchg32_result");

cleanup:
close(link_fd);
}

void test_atomics(void)
{
struct atomics_lskel *skel;
__u32 duration = 0;

skel = atomics_lskel__open_and_load();
if (CHECK(!skel, "skel_load", "atomics skeleton failed\n"))
if (!ASSERT_OK_PTR(skel, "atomics skeleton load"))
return;

if (skel->data->skip_tests) {
Expand Down
28 changes: 14 additions & 14 deletions tools/testing/selftests/bpf/progs/atomics.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ __u64 add_stack_value_copy = 0;
__u64 add_stack_result = 0;
__u64 add_noreturn_value = 1;

SEC("fentry/bpf_fentry_test1")
int BPF_PROG(add, int a)
SEC("raw_tp/sys_enter")
int add(const void *ctx)
{
if (pid != (bpf_get_current_pid_tgid() >> 32))
return 0;
Expand All @@ -46,8 +46,8 @@ __s64 sub_stack_value_copy = 0;
__s64 sub_stack_result = 0;
__s64 sub_noreturn_value = 1;

SEC("fentry/bpf_fentry_test1")
int BPF_PROG(sub, int a)
SEC("raw_tp/sys_enter")
int sub(const void *ctx)
{
if (pid != (bpf_get_current_pid_tgid() >> 32))
return 0;
Expand All @@ -70,8 +70,8 @@ __u32 and32_value = 0x110;
__u32 and32_result = 0;
__u64 and_noreturn_value = (0x110ull << 32);

SEC("fentry/bpf_fentry_test1")
int BPF_PROG(and, int a)
SEC("raw_tp/sys_enter")
int and(const void *ctx)
{
if (pid != (bpf_get_current_pid_tgid() >> 32))
return 0;
Expand All @@ -91,8 +91,8 @@ __u32 or32_value = 0x110;
__u32 or32_result = 0;
__u64 or_noreturn_value = (0x110ull << 32);

SEC("fentry/bpf_fentry_test1")
int BPF_PROG(or, int a)
SEC("raw_tp/sys_enter")
int or(const void *ctx)
{
if (pid != (bpf_get_current_pid_tgid() >> 32))
return 0;
Expand All @@ -111,8 +111,8 @@ __u32 xor32_value = 0x110;
__u32 xor32_result = 0;
__u64 xor_noreturn_value = (0x110ull << 32);

SEC("fentry/bpf_fentry_test1")
int BPF_PROG(xor, int a)
SEC("raw_tp/sys_enter")
int xor(const void *ctx)
{
if (pid != (bpf_get_current_pid_tgid() >> 32))
return 0;
Expand All @@ -132,8 +132,8 @@ __u32 cmpxchg32_value = 1;
__u32 cmpxchg32_result_fail = 0;
__u32 cmpxchg32_result_succeed = 0;

SEC("fentry/bpf_fentry_test1")
int BPF_PROG(cmpxchg, int a)
SEC("raw_tp/sys_enter")
int cmpxchg(const void *ctx)
{
if (pid != (bpf_get_current_pid_tgid() >> 32))
return 0;
Expand All @@ -153,8 +153,8 @@ __u64 xchg64_result = 0;
__u32 xchg32_value = 1;
__u32 xchg32_result = 0;

SEC("fentry/bpf_fentry_test1")
int BPF_PROG(xchg, int a)
SEC("raw_tp/sys_enter")
int xchg(const void *ctx)
{
if (pid != (bpf_get_current_pid_tgid() >> 32))
return 0;
Expand Down

0 comments on commit ea4dbf4

Please sign in to comment.