Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf/selftests: Test for bpf_per_cpu_ptr() and bpf_this_cpu_ptr()

Test bpf_per_cpu_ptr() and bpf_this_cpu_ptr(). Test two paths in the
kernel. If the base pointer points to a struct, the returned reg is
of type PTR_TO_BTF_ID. Direct pointer dereference can be applied on
the returned variable. If the base pointer isn't a struct, the
returned reg is of type PTR_TO_MEM, which also supports direct pointer
dereference.

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-7-haoluo@google.com

authored by

Hao Luo and committed by
Alexei Starovoitov
00dc73e4 63d9b80d

+50
+18
tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
··· 11 11 void test_ksyms_btf(void) 12 12 { 13 13 __u64 runqueues_addr, bpf_prog_active_addr; 14 + __u32 this_rq_cpu; 15 + int this_bpf_prog_active; 14 16 struct test_ksyms_btf *skel = NULL; 15 17 struct test_ksyms_btf__data *data; 16 18 struct btf *btf; ··· 65 63 "got %llu, exp %llu\n", 66 64 (unsigned long long)data->out__bpf_prog_active_addr, 67 65 (unsigned long long)bpf_prog_active_addr); 66 + 67 + CHECK(data->out__rq_cpu == -1, "rq_cpu", 68 + "got %u, exp != -1\n", data->out__rq_cpu); 69 + CHECK(data->out__bpf_prog_active < 0, "bpf_prog_active", 70 + "got %d, exp >= 0\n", data->out__bpf_prog_active); 71 + CHECK(data->out__cpu_0_rq_cpu != 0, "cpu_rq(0)->cpu", 72 + "got %u, exp 0\n", data->out__cpu_0_rq_cpu); 73 + 74 + this_rq_cpu = data->out__this_rq_cpu; 75 + CHECK(this_rq_cpu != data->out__rq_cpu, "this_rq_cpu", 76 + "got %u, exp %u\n", this_rq_cpu, data->out__rq_cpu); 77 + 78 + this_bpf_prog_active = data->out__this_bpf_prog_active; 79 + CHECK(this_bpf_prog_active != data->out__bpf_prog_active, "this_bpf_prog_active", 80 + "got %d, exp %d\n", this_bpf_prog_active, 81 + data->out__bpf_prog_active); 68 82 69 83 cleanup: 70 84 btf__free(btf);
+32
tools/testing/selftests/bpf/progs/test_ksyms_btf.c
··· 8 8 __u64 out__runqueues_addr = -1; 9 9 __u64 out__bpf_prog_active_addr = -1; 10 10 11 + __u32 out__rq_cpu = -1; /* percpu struct fields */ 12 + int out__bpf_prog_active = -1; /* percpu int */ 13 + 14 + __u32 out__this_rq_cpu = -1; 15 + int out__this_bpf_prog_active = -1; 16 + 17 + __u32 out__cpu_0_rq_cpu = -1; /* cpu_rq(0)->cpu */ 18 + 11 19 extern const struct rq runqueues __ksym; /* struct type global var. */ 12 20 extern const int bpf_prog_active __ksym; /* int type global var. */ 13 21 14 22 SEC("raw_tp/sys_enter") 15 23 int handler(const void *ctx) 16 24 { 25 + struct rq *rq; 26 + int *active; 27 + __u32 cpu; 28 + 17 29 out__runqueues_addr = (__u64)&runqueues; 18 30 out__bpf_prog_active_addr = (__u64)&bpf_prog_active; 31 + 32 + cpu = bpf_get_smp_processor_id(); 33 + 34 + /* test bpf_per_cpu_ptr() */ 35 + rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, cpu); 36 + if (rq) 37 + out__rq_cpu = rq->cpu; 38 + active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu); 39 + if (active) 40 + out__bpf_prog_active = *active; 41 + 42 + rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, 0); 43 + if (rq) /* should always be valid, but we can't spare the check. */ 44 + out__cpu_0_rq_cpu = rq->cpu; 45 + 46 + /* test bpf_this_cpu_ptr */ 47 + rq = (struct rq *)bpf_this_cpu_ptr(&runqueues); 48 + out__this_rq_cpu = rq->cpu; 49 + active = (int *)bpf_this_cpu_ptr(&bpf_prog_active); 50 + out__this_bpf_prog_active = *active; 19 51 20 52 return 0; 21 53 }