Skip to content

Commit ce4eb7e

Browse files
liu-song-6kernel-patches-bot
authored andcommitted
selftests/bpf: add raw_tp_test_run
This test runs test_run for raw_tracepoint program. The test covers ctx input, retval output, and running on correct cpu. Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Song Liu <[email protected]>
1 parent ac4c53c commit ce4eb7e

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Copyright (c) 2019 Facebook */
3+
#include <test_progs.h>
4+
#include <linux/bpf.h>
5+
#include "bpf/libbpf_internal.h"
6+
#include "test_raw_tp_test_run.skel.h"
7+
8+
static int duration;
9+
10+
void test_raw_tp_test_run(void)
11+
{
12+
struct bpf_prog_test_run_attr test_attr = {};
13+
int comm_fd = -1, err, nr_online, i, prog_fd;
14+
__u64 args[2] = {0x1234ULL, 0x5678ULL};
15+
int expected_retval = 0x1234 + 0x5678;
16+
struct test_raw_tp_test_run *skel;
17+
char buf[] = "new_name";
18+
bool *online = NULL;
19+
DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
20+
.ctx_in = args,
21+
.ctx_size_in = sizeof(args),
22+
.flags = BPF_F_TEST_RUN_ON_CPU,
23+
);
24+
25+
err = parse_cpu_mask_file("/sys/devices/system/cpu/online", &online,
26+
&nr_online);
27+
if (CHECK(err, "parse_cpu_mask_file", "err %d\n", err))
28+
return;
29+
30+
skel = test_raw_tp_test_run__open_and_load();
31+
if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
32+
goto cleanup;
33+
34+
err = test_raw_tp_test_run__attach(skel);
35+
if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
36+
goto cleanup;
37+
38+
comm_fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
39+
if (CHECK(comm_fd < 0, "open /proc/self/comm", "err %d\n", errno))
40+
goto cleanup;
41+
42+
err = write(comm_fd, buf, sizeof(buf));
43+
CHECK(err < 0, "task rename", "err %d", errno);
44+
45+
CHECK(skel->bss->count == 0, "check_count", "didn't increase\n");
46+
CHECK(skel->data->on_cpu != 0xffffffff, "check_on_cpu", "got wrong value\n");
47+
48+
prog_fd = bpf_program__fd(skel->progs.rename);
49+
test_attr.prog_fd = prog_fd;
50+
test_attr.ctx_in = args;
51+
test_attr.ctx_size_in = sizeof(__u64);
52+
53+
err = bpf_prog_test_run_xattr(&test_attr);
54+
CHECK(err == 0, "test_run", "should fail for too small ctx\n");
55+
56+
test_attr.ctx_size_in = sizeof(args);
57+
err = bpf_prog_test_run_xattr(&test_attr);
58+
CHECK(err < 0, "test_run", "err %d\n", errno);
59+
CHECK(test_attr.retval != expected_retval, "check_retval",
60+
"expect 0x%x, got 0x%x\n", expected_retval, test_attr.retval);
61+
62+
for (i = 0; i < nr_online; i++) {
63+
if (!online[i])
64+
continue;
65+
66+
opts.cpu = i;
67+
opts.retval = 0;
68+
err = bpf_prog_test_run_opts(prog_fd, &opts);
69+
CHECK(err < 0, "test_run_opts", "err %d\n", errno);
70+
CHECK(skel->data->on_cpu != i, "check_on_cpu",
71+
"expect %d got %d\n", i, skel->data->on_cpu);
72+
CHECK(opts.retval != expected_retval,
73+
"check_retval", "expect 0x%x, got 0x%x\n",
74+
expected_retval, opts.retval);
75+
}
76+
77+
/* invalid cpu ID should fail with ENXIO */
78+
opts.cpu = 0xffffffff;
79+
err = bpf_prog_test_run_opts(prog_fd, &opts);
80+
CHECK(err != -1 || errno != ENXIO,
81+
"test_run_opts_fail",
82+
"should failed with ENXIO\n");
83+
84+
/* non-zero cpu w/o BPF_F_TEST_RUN_ON_CPU should fail with EINVAL */
85+
opts.cpu = 1;
86+
opts.flags = 0;
87+
err = bpf_prog_test_run_opts(prog_fd, &opts);
88+
CHECK(err != -1 || errno != EINVAL,
89+
"test_run_opts_fail",
90+
"should failed with EINVAL\n");
91+
92+
cleanup:
93+
close(comm_fd);
94+
test_raw_tp_test_run__destroy(skel);
95+
free(online);
96+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2020 Facebook */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
8+
__u32 count = 0;
9+
__u32 on_cpu = 0xffffffff;
10+
11+
SEC("raw_tp/task_rename")
12+
int BPF_PROG(rename, struct task_struct *task, char *comm)
13+
{
14+
15+
count++;
16+
if ((__u64) task == 0x1234ULL && (__u64) comm == 0x5678ULL) {
17+
on_cpu = bpf_get_smp_processor_id();
18+
return (int)task + (int)comm;
19+
}
20+
21+
return 0;
22+
}
23+
24+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)