selftests/bpf: Fix test_ksyms on non-SMP kernels
[linux-2.6-microblaze.git] / tools / testing / selftests / bpf / prog_tests / ksyms.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3
4 #include <test_progs.h>
5 #include "test_ksyms.skel.h"
6 #include <sys/stat.h>
7
8 static int duration;
9
10 static __u64 kallsyms_find(const char *sym)
11 {
12         char type, name[500];
13         __u64 addr, res = 0;
14         FILE *f;
15
16         f = fopen("/proc/kallsyms", "r");
17         if (CHECK(!f, "kallsyms_fopen", "failed to open: %d\n", errno))
18                 return 0;
19
20         while (fscanf(f, "%llx %c %499s%*[^\n]\n", &addr, &type, name) > 0) {
21                 if (strcmp(name, sym) == 0) {
22                         res = addr;
23                         goto out;
24                 }
25         }
26
27         CHECK(false, "not_found", "symbol %s not found\n", sym);
28 out:
29         fclose(f);
30         return res;
31 }
32
33 void test_ksyms(void)
34 {
35         __u64 per_cpu_start_addr = kallsyms_find("__per_cpu_start");
36         __u64 link_fops_addr = kallsyms_find("bpf_link_fops");
37         const char *btf_path = "/sys/kernel/btf/vmlinux";
38         struct test_ksyms *skel;
39         struct test_ksyms__data *data;
40         struct stat st;
41         __u64 btf_size;
42         int err;
43
44         if (CHECK(stat(btf_path, &st), "stat_btf", "err %d\n", errno))
45                 return;
46         btf_size = st.st_size;
47
48         skel = test_ksyms__open_and_load();
49         if (CHECK(!skel, "skel_open", "failed to open and load skeleton\n"))
50                 return;
51
52         err = test_ksyms__attach(skel);
53         if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
54                 goto cleanup;
55
56         /* trigger tracepoint */
57         usleep(1);
58
59         data = skel->data;
60         CHECK(data->out__bpf_link_fops != link_fops_addr, "bpf_link_fops",
61               "got 0x%llx, exp 0x%llx\n",
62               data->out__bpf_link_fops, link_fops_addr);
63         CHECK(data->out__bpf_link_fops1 != 0, "bpf_link_fops1",
64               "got %llu, exp %llu\n", data->out__bpf_link_fops1, (__u64)0);
65         CHECK(data->out__btf_size != btf_size, "btf_size",
66               "got %llu, exp %llu\n", data->out__btf_size, btf_size);
67         CHECK(data->out__per_cpu_start != per_cpu_start_addr, "__per_cpu_start",
68               "got %llu, exp %llu\n", data->out__per_cpu_start,
69               per_cpu_start_addr);
70
71 cleanup:
72         test_ksyms__destroy(skel);
73 }