WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / prog_tests / ksyms_btf.c
blobb58b775d19f3f9f217c27671ec2c06af9212d323
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Google */
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"
10 static int duration;
12 static void test_basic(void)
14 __u64 runqueues_addr, bpf_prog_active_addr;
15 __u32 this_rq_cpu;
16 int this_bpf_prog_active;
17 struct test_ksyms_btf *skel = NULL;
18 struct test_ksyms_btf__data *data;
19 int err;
21 err = kallsyms_find("runqueues", &runqueues_addr);
22 if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno))
23 return;
24 if (CHECK(err == -ENOENT, "ksym_find", "symbol 'runqueues' not found\n"))
25 return;
27 err = kallsyms_find("bpf_prog_active", &bpf_prog_active_addr);
28 if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno))
29 return;
30 if (CHECK(err == -ENOENT, "ksym_find", "symbol 'bpf_prog_active' not found\n"))
31 return;
33 skel = test_ksyms_btf__open_and_load();
34 if (CHECK(!skel, "skel_open", "failed to open and load skeleton\n"))
35 goto cleanup;
37 err = test_ksyms_btf__attach(skel);
38 if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
39 goto cleanup;
41 /* trigger tracepoint */
42 usleep(1);
44 data = skel->data;
45 CHECK(data->out__runqueues_addr != runqueues_addr, "runqueues_addr",
46 "got %llu, exp %llu\n",
47 (unsigned long long)data->out__runqueues_addr,
48 (unsigned long long)runqueues_addr);
49 CHECK(data->out__bpf_prog_active_addr != bpf_prog_active_addr, "bpf_prog_active_addr",
50 "got %llu, exp %llu\n",
51 (unsigned long long)data->out__bpf_prog_active_addr,
52 (unsigned long long)bpf_prog_active_addr);
54 CHECK(data->out__rq_cpu == -1, "rq_cpu",
55 "got %u, exp != -1\n", data->out__rq_cpu);
56 CHECK(data->out__bpf_prog_active < 0, "bpf_prog_active",
57 "got %d, exp >= 0\n", data->out__bpf_prog_active);
58 CHECK(data->out__cpu_0_rq_cpu != 0, "cpu_rq(0)->cpu",
59 "got %u, exp 0\n", data->out__cpu_0_rq_cpu);
61 this_rq_cpu = data->out__this_rq_cpu;
62 CHECK(this_rq_cpu != data->out__rq_cpu, "this_rq_cpu",
63 "got %u, exp %u\n", this_rq_cpu, data->out__rq_cpu);
65 this_bpf_prog_active = data->out__this_bpf_prog_active;
66 CHECK(this_bpf_prog_active != data->out__bpf_prog_active, "this_bpf_prog_active",
67 "got %d, exp %d\n", this_bpf_prog_active,
68 data->out__bpf_prog_active);
70 cleanup:
71 test_ksyms_btf__destroy(skel);
74 static void test_null_check(void)
76 struct test_ksyms_btf_null_check *skel;
78 skel = test_ksyms_btf_null_check__open_and_load();
79 CHECK(skel, "skel_open", "unexpected load of a prog missing null check\n");
81 test_ksyms_btf_null_check__destroy(skel);
84 void test_ksyms_btf(void)
86 int percpu_datasec;
87 struct btf *btf;
89 btf = libbpf_find_kernel_btf();
90 if (CHECK(IS_ERR(btf), "btf_exists", "failed to load kernel BTF: %ld\n",
91 PTR_ERR(btf)))
92 return;
94 percpu_datasec = btf__find_by_name_kind(btf, ".data..percpu",
95 BTF_KIND_DATASEC);
96 btf__free(btf);
97 if (percpu_datasec < 0) {
98 printf("%s:SKIP:no PERCPU DATASEC in kernel btf\n",
99 __func__);
100 test__skip();
101 return;
104 if (test__start_subtest("basic"))
105 test_basic();
107 if (test__start_subtest("null_check"))
108 test_null_check();