-
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.
Merge branch 'Introduce access remote cpu elem support in BPF percpu …
…map' Feng zhou says: ==================== From: Feng Zhou <zhoufeng.zf@bytedance.com> Trace some functions, such as enqueue_task_fair, need to access the corresponding cpu, not the current cpu, and bpf_map_lookup_elem percpu map cannot do it. So add bpf_map_lookup_percpu_elem to accomplish it for percpu_array_map, percpu_hash_map, lru_percpu_hash_map. v1->v2: Addressed comments from Alexei Starovoitov. - add a selftest for bpf_map_lookup_percpu_elem. ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
- Loading branch information
Showing
11 changed files
with
203 additions
and
2 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
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
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
46 changes: 46 additions & 0 deletions
46
tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c
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,46 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
// Copyright (c) 2022 Bytedance | ||
|
||
#include <test_progs.h> | ||
|
||
#include "test_map_lookup_percpu_elem.skel.h" | ||
|
||
#define TEST_VALUE 1 | ||
|
||
void test_map_lookup_percpu_elem(void) | ||
{ | ||
struct test_map_lookup_percpu_elem *skel; | ||
int key = 0, ret; | ||
int nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); | ||
int *buf; | ||
|
||
buf = (int *)malloc(nr_cpus*sizeof(int)); | ||
if (!ASSERT_OK_PTR(buf, "malloc")) | ||
return; | ||
memset(buf, 0, nr_cpus*sizeof(int)); | ||
buf[0] = TEST_VALUE; | ||
|
||
skel = test_map_lookup_percpu_elem__open_and_load(); | ||
if (!ASSERT_OK_PTR(skel, "test_map_lookup_percpu_elem__open_and_load")) | ||
return; | ||
ret = test_map_lookup_percpu_elem__attach(skel); | ||
ASSERT_OK(ret, "test_map_lookup_percpu_elem__attach"); | ||
|
||
ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_array_map), &key, buf, 0); | ||
ASSERT_OK(ret, "percpu_array_map update"); | ||
|
||
ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_hash_map), &key, buf, 0); | ||
ASSERT_OK(ret, "percpu_hash_map update"); | ||
|
||
ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_lru_hash_map), &key, buf, 0); | ||
ASSERT_OK(ret, "percpu_lru_hash_map update"); | ||
|
||
syscall(__NR_getuid); | ||
|
||
ret = skel->bss->percpu_array_elem_val == TEST_VALUE && | ||
skel->bss->percpu_hash_elem_val == TEST_VALUE && | ||
skel->bss->percpu_lru_hash_elem_val == TEST_VALUE; | ||
ASSERT_OK(!ret, "bpf_map_lookup_percpu_elem success"); | ||
|
||
test_map_lookup_percpu_elem__destroy(skel); | ||
} |
54 changes: 54 additions & 0 deletions
54
tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c
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,54 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
// Copyright (c) 2022 Bytedance | ||
|
||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
|
||
int percpu_array_elem_val = 0; | ||
int percpu_hash_elem_val = 0; | ||
int percpu_lru_hash_elem_val = 0; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); | ||
__uint(max_entries, 1); | ||
__type(key, __u32); | ||
__type(value, __u32); | ||
} percpu_array_map SEC(".maps"); | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_PERCPU_HASH); | ||
__uint(max_entries, 1); | ||
__type(key, __u32); | ||
__type(value, __u32); | ||
} percpu_hash_map SEC(".maps"); | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH); | ||
__uint(max_entries, 1); | ||
__type(key, __u32); | ||
__type(value, __u32); | ||
} percpu_lru_hash_map SEC(".maps"); | ||
|
||
SEC("tp/syscalls/sys_enter_getuid") | ||
int sysenter_getuid(const void *ctx) | ||
{ | ||
__u32 key = 0; | ||
__u32 cpu = 0; | ||
__u32 *value; | ||
|
||
value = bpf_map_lookup_percpu_elem(&percpu_array_map, &key, cpu); | ||
if (value) | ||
percpu_array_elem_val = *value; | ||
|
||
value = bpf_map_lookup_percpu_elem(&percpu_hash_map, &key, cpu); | ||
if (value) | ||
percpu_hash_elem_val = *value; | ||
|
||
value = bpf_map_lookup_percpu_elem(&percpu_lru_hash_map, &key, cpu); | ||
if (value) | ||
percpu_lru_hash_elem_val = *value; | ||
|
||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |