Skip to content

Commit c77740d

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 proper handling of cpu_plus field. Signed-off-by: Song Liu <[email protected]>
1 parent 260897f commit c77740d

File tree

2 files changed

+99
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)