• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Google */
3 
4 #include <test_progs.h>
5 #include <bpf/libbpf.h>
6 #include <bpf/btf.h>
7 #include "test_ksyms_btf.skel.h"
8 #include "test_ksyms_btf_null_check.skel.h"
9 #include "test_ksyms_btf_write_check.skel.h"
10 
11 static int duration;
12 
test_basic(void)13 static void test_basic(void)
14 {
15 	__u64 runqueues_addr, bpf_prog_active_addr;
16 	__u32 this_rq_cpu;
17 	int this_bpf_prog_active;
18 	struct test_ksyms_btf *skel = NULL;
19 	struct test_ksyms_btf__data *data;
20 	int err;
21 
22 	err = kallsyms_find("runqueues", &runqueues_addr);
23 	if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno))
24 		return;
25 	if (CHECK(err == -ENOENT, "ksym_find", "symbol 'runqueues' not found\n"))
26 		return;
27 
28 	err = kallsyms_find("bpf_prog_active", &bpf_prog_active_addr);
29 	if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno))
30 		return;
31 	if (CHECK(err == -ENOENT, "ksym_find", "symbol 'bpf_prog_active' not found\n"))
32 		return;
33 
34 	skel = test_ksyms_btf__open_and_load();
35 	if (CHECK(!skel, "skel_open", "failed to open and load skeleton\n"))
36 		goto cleanup;
37 
38 	err = test_ksyms_btf__attach(skel);
39 	if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
40 		goto cleanup;
41 
42 	/* trigger tracepoint */
43 	usleep(1);
44 
45 	data = skel->data;
46 	CHECK(data->out__runqueues_addr != runqueues_addr, "runqueues_addr",
47 	      "got %llu, exp %llu\n",
48 	      (unsigned long long)data->out__runqueues_addr,
49 	      (unsigned long long)runqueues_addr);
50 	CHECK(data->out__bpf_prog_active_addr != bpf_prog_active_addr, "bpf_prog_active_addr",
51 	      "got %llu, exp %llu\n",
52 	      (unsigned long long)data->out__bpf_prog_active_addr,
53 	      (unsigned long long)bpf_prog_active_addr);
54 
55 	CHECK(data->out__rq_cpu == -1, "rq_cpu",
56 	      "got %u, exp != -1\n", data->out__rq_cpu);
57 	CHECK(data->out__bpf_prog_active < 0, "bpf_prog_active",
58 	      "got %d, exp >= 0\n", data->out__bpf_prog_active);
59 	CHECK(data->out__cpu_0_rq_cpu != 0, "cpu_rq(0)->cpu",
60 	      "got %u, exp 0\n", data->out__cpu_0_rq_cpu);
61 
62 	this_rq_cpu = data->out__this_rq_cpu;
63 	CHECK(this_rq_cpu != data->out__rq_cpu, "this_rq_cpu",
64 	      "got %u, exp %u\n", this_rq_cpu, data->out__rq_cpu);
65 
66 	this_bpf_prog_active = data->out__this_bpf_prog_active;
67 	CHECK(this_bpf_prog_active != data->out__bpf_prog_active, "this_bpf_prog_active",
68 	      "got %d, exp %d\n", this_bpf_prog_active,
69 	      data->out__bpf_prog_active);
70 
71 cleanup:
72 	test_ksyms_btf__destroy(skel);
73 }
74 
test_null_check(void)75 static void test_null_check(void)
76 {
77 	struct test_ksyms_btf_null_check *skel;
78 
79 	skel = test_ksyms_btf_null_check__open_and_load();
80 	CHECK(skel, "skel_open", "unexpected load of a prog missing null check\n");
81 
82 	test_ksyms_btf_null_check__destroy(skel);
83 }
84 
test_write_check(void)85 static void test_write_check(void)
86 {
87 	struct test_ksyms_btf_write_check *skel;
88 
89 	skel = test_ksyms_btf_write_check__open_and_load();
90 	CHECK(skel, "skel_open", "unexpected load of a prog writing to ksym memory\n");
91 
92 	test_ksyms_btf_write_check__destroy(skel);
93 }
94 
test_ksyms_btf(void)95 void test_ksyms_btf(void)
96 {
97 	int percpu_datasec;
98 	struct btf *btf;
99 
100 	btf = libbpf_find_kernel_btf();
101 	if (CHECK(IS_ERR(btf), "btf_exists", "failed to load kernel BTF: %ld\n",
102 		  PTR_ERR(btf)))
103 		return;
104 
105 	percpu_datasec = btf__find_by_name_kind(btf, ".data..percpu",
106 						BTF_KIND_DATASEC);
107 	btf__free(btf);
108 	if (percpu_datasec < 0) {
109 		printf("%s:SKIP:no PERCPU DATASEC in kernel btf\n",
110 		       __func__);
111 		test__skip();
112 		return;
113 	}
114 
115 	if (test__start_subtest("basic"))
116 		test_basic();
117 
118 	if (test__start_subtest("null_check"))
119 		test_null_check();
120 
121 	if (test__start_subtest("write_check"))
122 		test_write_check();
123 }
124