12c2f6abeSHao Luo // SPDX-License-Identifier: GPL-2.0
22c2f6abeSHao Luo /* Copyright (c) 2020 Google */
32c2f6abeSHao Luo
42c2f6abeSHao Luo #include <test_progs.h>
52c2f6abeSHao Luo #include <bpf/libbpf.h>
62c2f6abeSHao Luo #include <bpf/btf.h>
72c2f6abeSHao Luo #include "test_ksyms_btf.skel.h"
88568c3ceSMartin KaFai Lau #include "test_ksyms_btf_null_check.skel.h"
92211c825SHao Luo #include "test_ksyms_weak.skel.h"
10087cba79SKumar Kartikeya Dwivedi #include "test_ksyms_weak.lskel.h"
119497c458SHao Luo #include "test_ksyms_btf_write_check.skel.h"
122c2f6abeSHao Luo
132c2f6abeSHao Luo static int duration;
142c2f6abeSHao Luo
test_basic(void)158568c3ceSMartin KaFai Lau static void test_basic(void)
162c2f6abeSHao Luo {
172c2f6abeSHao Luo __u64 runqueues_addr, bpf_prog_active_addr;
1800dc73e4SHao Luo __u32 this_rq_cpu;
1900dc73e4SHao Luo int this_bpf_prog_active;
202c2f6abeSHao Luo struct test_ksyms_btf *skel = NULL;
212c2f6abeSHao Luo struct test_ksyms_btf__data *data;
222c2f6abeSHao Luo int err;
232c2f6abeSHao Luo
242c2f6abeSHao Luo err = kallsyms_find("runqueues", &runqueues_addr);
252c2f6abeSHao Luo if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno))
262c2f6abeSHao Luo return;
272c2f6abeSHao Luo if (CHECK(err == -ENOENT, "ksym_find", "symbol 'runqueues' not found\n"))
282c2f6abeSHao Luo return;
292c2f6abeSHao Luo
302c2f6abeSHao Luo err = kallsyms_find("bpf_prog_active", &bpf_prog_active_addr);
312c2f6abeSHao Luo if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno))
322c2f6abeSHao Luo return;
332c2f6abeSHao Luo if (CHECK(err == -ENOENT, "ksym_find", "symbol 'bpf_prog_active' not found\n"))
342c2f6abeSHao Luo return;
352c2f6abeSHao Luo
362c2f6abeSHao Luo skel = test_ksyms_btf__open_and_load();
372c2f6abeSHao Luo if (CHECK(!skel, "skel_open", "failed to open and load skeleton\n"))
382c2f6abeSHao Luo goto cleanup;
392c2f6abeSHao Luo
402c2f6abeSHao Luo err = test_ksyms_btf__attach(skel);
412c2f6abeSHao Luo if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
422c2f6abeSHao Luo goto cleanup;
432c2f6abeSHao Luo
442c2f6abeSHao Luo /* trigger tracepoint */
452c2f6abeSHao Luo usleep(1);
462c2f6abeSHao Luo
472c2f6abeSHao Luo data = skel->data;
482c2f6abeSHao Luo CHECK(data->out__runqueues_addr != runqueues_addr, "runqueues_addr",
492c2f6abeSHao Luo "got %llu, exp %llu\n",
502c2f6abeSHao Luo (unsigned long long)data->out__runqueues_addr,
512c2f6abeSHao Luo (unsigned long long)runqueues_addr);
522c2f6abeSHao Luo CHECK(data->out__bpf_prog_active_addr != bpf_prog_active_addr, "bpf_prog_active_addr",
532c2f6abeSHao Luo "got %llu, exp %llu\n",
542c2f6abeSHao Luo (unsigned long long)data->out__bpf_prog_active_addr,
552c2f6abeSHao Luo (unsigned long long)bpf_prog_active_addr);
562c2f6abeSHao Luo
5700dc73e4SHao Luo CHECK(data->out__rq_cpu == -1, "rq_cpu",
5800dc73e4SHao Luo "got %u, exp != -1\n", data->out__rq_cpu);
5900dc73e4SHao Luo CHECK(data->out__bpf_prog_active < 0, "bpf_prog_active",
6000dc73e4SHao Luo "got %d, exp >= 0\n", data->out__bpf_prog_active);
6100dc73e4SHao Luo CHECK(data->out__cpu_0_rq_cpu != 0, "cpu_rq(0)->cpu",
6200dc73e4SHao Luo "got %u, exp 0\n", data->out__cpu_0_rq_cpu);
6300dc73e4SHao Luo
6400dc73e4SHao Luo this_rq_cpu = data->out__this_rq_cpu;
6500dc73e4SHao Luo CHECK(this_rq_cpu != data->out__rq_cpu, "this_rq_cpu",
6600dc73e4SHao Luo "got %u, exp %u\n", this_rq_cpu, data->out__rq_cpu);
6700dc73e4SHao Luo
6800dc73e4SHao Luo this_bpf_prog_active = data->out__this_bpf_prog_active;
6900dc73e4SHao Luo CHECK(this_bpf_prog_active != data->out__bpf_prog_active, "this_bpf_prog_active",
7000dc73e4SHao Luo "got %d, exp %d\n", this_bpf_prog_active,
7100dc73e4SHao Luo data->out__bpf_prog_active);
7200dc73e4SHao Luo
732c2f6abeSHao Luo cleanup:
742c2f6abeSHao Luo test_ksyms_btf__destroy(skel);
752c2f6abeSHao Luo }
768568c3ceSMartin KaFai Lau
test_null_check(void)778568c3ceSMartin KaFai Lau static void test_null_check(void)
788568c3ceSMartin KaFai Lau {
798568c3ceSMartin KaFai Lau struct test_ksyms_btf_null_check *skel;
808568c3ceSMartin KaFai Lau
818568c3ceSMartin KaFai Lau skel = test_ksyms_btf_null_check__open_and_load();
828568c3ceSMartin KaFai Lau CHECK(skel, "skel_open", "unexpected load of a prog missing null check\n");
838568c3ceSMartin KaFai Lau
848568c3ceSMartin KaFai Lau test_ksyms_btf_null_check__destroy(skel);
858568c3ceSMartin KaFai Lau }
868568c3ceSMartin KaFai Lau
test_weak_syms(void)872211c825SHao Luo static void test_weak_syms(void)
882211c825SHao Luo {
892211c825SHao Luo struct test_ksyms_weak *skel;
902211c825SHao Luo struct test_ksyms_weak__data *data;
912211c825SHao Luo int err;
922211c825SHao Luo
932211c825SHao Luo skel = test_ksyms_weak__open_and_load();
94087cba79SKumar Kartikeya Dwivedi if (!ASSERT_OK_PTR(skel, "test_ksyms_weak__open_and_load"))
952211c825SHao Luo return;
962211c825SHao Luo
972211c825SHao Luo err = test_ksyms_weak__attach(skel);
98087cba79SKumar Kartikeya Dwivedi if (!ASSERT_OK(err, "test_ksyms_weak__attach"))
992211c825SHao Luo goto cleanup;
1002211c825SHao Luo
1012211c825SHao Luo /* trigger tracepoint */
1022211c825SHao Luo usleep(1);
1032211c825SHao Luo
1042211c825SHao Luo data = skel->data;
1052211c825SHao Luo ASSERT_EQ(data->out__existing_typed, 0, "existing typed ksym");
1062211c825SHao Luo ASSERT_NEQ(data->out__existing_typeless, -1, "existing typeless ksym");
1072211c825SHao Luo ASSERT_EQ(data->out__non_existent_typeless, 0, "nonexistent typeless ksym");
1082211c825SHao Luo ASSERT_EQ(data->out__non_existent_typed, 0, "nonexistent typed ksym");
1092211c825SHao Luo
1102211c825SHao Luo cleanup:
1112211c825SHao Luo test_ksyms_weak__destroy(skel);
1122211c825SHao Luo }
1132211c825SHao Luo
test_weak_syms_lskel(void)114087cba79SKumar Kartikeya Dwivedi static void test_weak_syms_lskel(void)
115087cba79SKumar Kartikeya Dwivedi {
116087cba79SKumar Kartikeya Dwivedi struct test_ksyms_weak_lskel *skel;
117087cba79SKumar Kartikeya Dwivedi struct test_ksyms_weak_lskel__data *data;
118087cba79SKumar Kartikeya Dwivedi int err;
119087cba79SKumar Kartikeya Dwivedi
120087cba79SKumar Kartikeya Dwivedi skel = test_ksyms_weak_lskel__open_and_load();
121087cba79SKumar Kartikeya Dwivedi if (!ASSERT_OK_PTR(skel, "test_ksyms_weak_lskel__open_and_load"))
122087cba79SKumar Kartikeya Dwivedi return;
123087cba79SKumar Kartikeya Dwivedi
124087cba79SKumar Kartikeya Dwivedi err = test_ksyms_weak_lskel__attach(skel);
125087cba79SKumar Kartikeya Dwivedi if (!ASSERT_OK(err, "test_ksyms_weak_lskel__attach"))
126087cba79SKumar Kartikeya Dwivedi goto cleanup;
127087cba79SKumar Kartikeya Dwivedi
128087cba79SKumar Kartikeya Dwivedi /* trigger tracepoint */
129087cba79SKumar Kartikeya Dwivedi usleep(1);
130087cba79SKumar Kartikeya Dwivedi
131087cba79SKumar Kartikeya Dwivedi data = skel->data;
132087cba79SKumar Kartikeya Dwivedi ASSERT_EQ(data->out__existing_typed, 0, "existing typed ksym");
133087cba79SKumar Kartikeya Dwivedi ASSERT_NEQ(data->out__existing_typeless, -1, "existing typeless ksym");
134087cba79SKumar Kartikeya Dwivedi ASSERT_EQ(data->out__non_existent_typeless, 0, "nonexistent typeless ksym");
135087cba79SKumar Kartikeya Dwivedi ASSERT_EQ(data->out__non_existent_typed, 0, "nonexistent typed ksym");
136087cba79SKumar Kartikeya Dwivedi
137087cba79SKumar Kartikeya Dwivedi cleanup:
138087cba79SKumar Kartikeya Dwivedi test_ksyms_weak_lskel__destroy(skel);
139087cba79SKumar Kartikeya Dwivedi }
140087cba79SKumar Kartikeya Dwivedi
test_write_check(bool test_handler1)141*7cb29b1cSKumar Kartikeya Dwivedi static void test_write_check(bool test_handler1)
1429497c458SHao Luo {
1439497c458SHao Luo struct test_ksyms_btf_write_check *skel;
1449497c458SHao Luo
145*7cb29b1cSKumar Kartikeya Dwivedi skel = test_ksyms_btf_write_check__open();
146*7cb29b1cSKumar Kartikeya Dwivedi if (!ASSERT_OK_PTR(skel, "test_ksyms_btf_write_check__open"))
147*7cb29b1cSKumar Kartikeya Dwivedi return;
148*7cb29b1cSKumar Kartikeya Dwivedi bpf_program__set_autoload(test_handler1 ? skel->progs.handler2 : skel->progs.handler1, false);
149*7cb29b1cSKumar Kartikeya Dwivedi ASSERT_ERR(test_ksyms_btf_write_check__load(skel),
150*7cb29b1cSKumar Kartikeya Dwivedi "unexpected load of a prog writing to ksym memory\n");
1519497c458SHao Luo
1529497c458SHao Luo test_ksyms_btf_write_check__destroy(skel);
1539497c458SHao Luo }
1549497c458SHao Luo
test_ksyms_btf(void)1558568c3ceSMartin KaFai Lau void test_ksyms_btf(void)
1568568c3ceSMartin KaFai Lau {
1578568c3ceSMartin KaFai Lau int percpu_datasec;
1588568c3ceSMartin KaFai Lau struct btf *btf;
1598568c3ceSMartin KaFai Lau
1608568c3ceSMartin KaFai Lau btf = libbpf_find_kernel_btf();
161bad2e478SAndrii Nakryiko if (!ASSERT_OK_PTR(btf, "btf_exists"))
1628568c3ceSMartin KaFai Lau return;
1638568c3ceSMartin KaFai Lau
1648568c3ceSMartin KaFai Lau percpu_datasec = btf__find_by_name_kind(btf, ".data..percpu",
1658568c3ceSMartin KaFai Lau BTF_KIND_DATASEC);
1668568c3ceSMartin KaFai Lau btf__free(btf);
1678568c3ceSMartin KaFai Lau if (percpu_datasec < 0) {
1688568c3ceSMartin KaFai Lau printf("%s:SKIP:no PERCPU DATASEC in kernel btf\n",
1698568c3ceSMartin KaFai Lau __func__);
1708568c3ceSMartin KaFai Lau test__skip();
1718568c3ceSMartin KaFai Lau return;
1728568c3ceSMartin KaFai Lau }
1738568c3ceSMartin KaFai Lau
1748568c3ceSMartin KaFai Lau if (test__start_subtest("basic"))
1758568c3ceSMartin KaFai Lau test_basic();
1768568c3ceSMartin KaFai Lau
1778568c3ceSMartin KaFai Lau if (test__start_subtest("null_check"))
1788568c3ceSMartin KaFai Lau test_null_check();
1792211c825SHao Luo
1802211c825SHao Luo if (test__start_subtest("weak_ksyms"))
1812211c825SHao Luo test_weak_syms();
182087cba79SKumar Kartikeya Dwivedi
183087cba79SKumar Kartikeya Dwivedi if (test__start_subtest("weak_ksyms_lskel"))
184087cba79SKumar Kartikeya Dwivedi test_weak_syms_lskel();
1859497c458SHao Luo
186*7cb29b1cSKumar Kartikeya Dwivedi if (test__start_subtest("write_check1"))
187*7cb29b1cSKumar Kartikeya Dwivedi test_write_check(true);
188*7cb29b1cSKumar Kartikeya Dwivedi
189*7cb29b1cSKumar Kartikeya Dwivedi if (test__start_subtest("write_check2"))
190*7cb29b1cSKumar Kartikeya Dwivedi test_write_check(false);
1918568c3ceSMartin KaFai Lau }
192