-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Ksyms_btf to test typed ksyms
Selftests for typed ksyms. Tests two types of ksyms: one is a struct, the other is a plain int. This tests two paths in the kernel. Struct ksyms will be converted into PTR_TO_BTF_ID by the verifier while int typed ksyms will be converted into PTR_TO_MEM. Signed-off-by: Hao Luo <haoluo@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200929235049.2533242-4-haoluo@google.com
- Loading branch information
Hao Luo
authored and
Alexei Starovoitov
committed
Oct 2, 2020
1 parent
d370bbe
commit 2c2f6ab
Showing
5 changed files
with
137 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2020 Google */ | ||
|
||
#include <test_progs.h> | ||
#include <bpf/libbpf.h> | ||
#include <bpf/btf.h> | ||
#include "test_ksyms_btf.skel.h" | ||
|
||
static int duration; | ||
|
||
void test_ksyms_btf(void) | ||
{ | ||
__u64 runqueues_addr, bpf_prog_active_addr; | ||
struct test_ksyms_btf *skel = NULL; | ||
struct test_ksyms_btf__data *data; | ||
struct btf *btf; | ||
int percpu_datasec; | ||
int err; | ||
|
||
err = kallsyms_find("runqueues", &runqueues_addr); | ||
if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno)) | ||
return; | ||
if (CHECK(err == -ENOENT, "ksym_find", "symbol 'runqueues' not found\n")) | ||
return; | ||
|
||
err = kallsyms_find("bpf_prog_active", &bpf_prog_active_addr); | ||
if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno)) | ||
return; | ||
if (CHECK(err == -ENOENT, "ksym_find", "symbol 'bpf_prog_active' not found\n")) | ||
return; | ||
|
||
btf = libbpf_find_kernel_btf(); | ||
if (CHECK(IS_ERR(btf), "btf_exists", "failed to load kernel BTF: %ld\n", | ||
PTR_ERR(btf))) | ||
return; | ||
|
||
percpu_datasec = btf__find_by_name_kind(btf, ".data..percpu", | ||
BTF_KIND_DATASEC); | ||
if (percpu_datasec < 0) { | ||
printf("%s:SKIP:no PERCPU DATASEC in kernel btf\n", | ||
__func__); | ||
test__skip(); | ||
goto cleanup; | ||
} | ||
|
||
skel = test_ksyms_btf__open_and_load(); | ||
if (CHECK(!skel, "skel_open", "failed to open and load skeleton\n")) | ||
goto cleanup; | ||
|
||
err = test_ksyms_btf__attach(skel); | ||
if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) | ||
goto cleanup; | ||
|
||
/* trigger tracepoint */ | ||
usleep(1); | ||
|
||
data = skel->data; | ||
CHECK(data->out__runqueues_addr != runqueues_addr, "runqueues_addr", | ||
"got %llu, exp %llu\n", | ||
(unsigned long long)data->out__runqueues_addr, | ||
(unsigned long long)runqueues_addr); | ||
CHECK(data->out__bpf_prog_active_addr != bpf_prog_active_addr, "bpf_prog_active_addr", | ||
"got %llu, exp %llu\n", | ||
(unsigned long long)data->out__bpf_prog_active_addr, | ||
(unsigned long long)bpf_prog_active_addr); | ||
|
||
cleanup: | ||
btf__free(btf); | ||
test_ksyms_btf__destroy(skel); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2020 Google */ | ||
|
||
#include "vmlinux.h" | ||
|
||
#include <bpf/bpf_helpers.h> | ||
|
||
__u64 out__runqueues_addr = -1; | ||
__u64 out__bpf_prog_active_addr = -1; | ||
|
||
extern const struct rq runqueues __ksym; /* struct type global var. */ | ||
extern const int bpf_prog_active __ksym; /* int type global var. */ | ||
|
||
SEC("raw_tp/sys_enter") | ||
int handler(const void *ctx) | ||
{ | ||
out__runqueues_addr = (__u64)&runqueues; | ||
out__bpf_prog_active_addr = (__u64)&bpf_prog_active; | ||
|
||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters