-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add a test for btf_type_tag "percpu"
Add test for percpu btf_type_tag. Similar to the "user" tag, we test the following cases: 1. __percpu struct field. 2. __percpu as function parameter. 3. per_cpu_ptr() accepts dynamically allocated __percpu memory. Because the test for "user" and the test for "percpu" are very similar, a little bit of refactoring has been done in btf_tag.c. Basically, both tests share the same function for loading vmlinux and module btf. Example output from log: > ./test_progs -v -t btf_tag libbpf: prog 'test_percpu1': BPF program load failed: Permission denied libbpf: prog 'test_percpu1': -- BEGIN PROG LOAD LOG -- ... ; g = arg->a; 1: (61) r1 = *(u32 *)(r1 +0) R1 is ptr_bpf_testmod_btf_type_tag_1 access percpu memory: off=0 ... test_btf_type_tag_mod_percpu:PASS:btf_type_tag_percpu 0 nsec #26/6 btf_tag/btf_type_tag_percpu_mod1:OK libbpf: prog 'test_percpu2': BPF program load failed: Permission denied libbpf: prog 'test_percpu2': -- BEGIN PROG LOAD LOG -- ... ; g = arg->p->a; 2: (61) r1 = *(u32 *)(r1 +0) R1 is ptr_bpf_testmod_btf_type_tag_1 access percpu memory: off=0 ... test_btf_type_tag_mod_percpu:PASS:btf_type_tag_percpu 0 nsec #26/7 btf_tag/btf_type_tag_percpu_mod2:OK libbpf: prog 'test_percpu_load': BPF program load failed: Permission denied libbpf: prog 'test_percpu_load': -- BEGIN PROG LOAD LOG -- ... ; g = (__u64)cgrp->rstat_cpu->updated_children; 2: (79) r1 = *(u64 *)(r1 +48) R1 is ptr_cgroup_rstat_cpu access percpu memory: off=48 ... test_btf_type_tag_vmlinux_percpu:PASS:btf_type_tag_percpu_load 0 nsec #26/8 btf_tag/btf_type_tag_percpu_vmlinux_load:OK load_btfs:PASS:could not load vmlinux BTF 0 nsec test_btf_type_tag_vmlinux_percpu:PASS:btf_type_tag_percpu 0 nsec test_btf_type_tag_vmlinux_percpu:PASS:btf_type_tag_percpu_helper 0 nsec #26/9 btf_tag/btf_type_tag_percpu_vmlinux_helper:OK Signed-off-by: Hao Luo <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
- Loading branch information
1 parent
5844101
commit 50c6b8a
Showing
3 changed files
with
215 additions
and
29 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
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,66 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2022 Google */ | ||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
struct bpf_testmod_btf_type_tag_1 { | ||
int a; | ||
}; | ||
|
||
struct bpf_testmod_btf_type_tag_2 { | ||
struct bpf_testmod_btf_type_tag_1 *p; | ||
}; | ||
|
||
__u64 g; | ||
|
||
SEC("fentry/bpf_testmod_test_btf_type_tag_percpu_1") | ||
int BPF_PROG(test_percpu1, struct bpf_testmod_btf_type_tag_1 *arg) | ||
{ | ||
g = arg->a; | ||
return 0; | ||
} | ||
|
||
SEC("fentry/bpf_testmod_test_btf_type_tag_percpu_2") | ||
int BPF_PROG(test_percpu2, struct bpf_testmod_btf_type_tag_2 *arg) | ||
{ | ||
g = arg->p->a; | ||
return 0; | ||
} | ||
|
||
/* trace_cgroup_mkdir(struct cgroup *cgrp, const char *path) | ||
* | ||
* struct cgroup_rstat_cpu { | ||
* ... | ||
* struct cgroup *updated_children; | ||
* ... | ||
* }; | ||
* | ||
* struct cgroup { | ||
* ... | ||
* struct cgroup_rstat_cpu __percpu *rstat_cpu; | ||
* ... | ||
* }; | ||
*/ | ||
SEC("tp_btf/cgroup_mkdir") | ||
int BPF_PROG(test_percpu_load, struct cgroup *cgrp, const char *path) | ||
{ | ||
g = (__u64)cgrp->rstat_cpu->updated_children; | ||
return 0; | ||
} | ||
|
||
SEC("tp_btf/cgroup_mkdir") | ||
int BPF_PROG(test_percpu_helper, struct cgroup *cgrp, const char *path) | ||
{ | ||
struct cgroup_rstat_cpu *rstat; | ||
__u32 cpu; | ||
|
||
cpu = bpf_get_smp_processor_id(); | ||
rstat = (struct cgroup_rstat_cpu *)bpf_per_cpu_ptr(cgrp->rstat_cpu, cpu); | ||
if (rstat) { | ||
/* READ_ONCE */ | ||
*(volatile int *)rstat; | ||
} | ||
|
||
return 0; | ||
} |