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 >= 0 || 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 >= 0 || 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 }
97