bpf: implement syscall command BPF_MAP_GET_NEXT_KEY for stacktrace map
Subsystems (6)
v2 cover bpf: implement syscall command BPF_MAP_GET_NEXT_KEY for stacktrace map
The patch set implements bpf syscall command BPF_MAP_GET_NEXT_KEY for stacktrace map. Patch #1 is the core implementation and Patch #2 implements a bpf test at tools/testing/selftests/bpf directory. Please see individual patch comments for details.
Changelog:
v1 -> v2:
- For invalid key (key pointer is non-NULL), sets next_key to be the first valid key.
Yonghong Song (2):
bpf: implement syscall command BPF_MAP_GET_NEXT_KEY for stacktrace map tools/bpf: add a bpf selftest for stacktrace
kernel/bpf/stackmap.c | 28 ++++- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/test_progs.c | 127 ++++++++++++++++++++++ tools/testing/selftests/bpf/test_stacktrace_map.c | 62 +++++++++++ 4 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_stacktrace_map.c
Signature · 1 line
2.9.5
PATCH v2 1/2 bpf: implement syscall command BPF_MAP_GET_NEXT_KEY for stacktrace map
Currently, bpf syscall command BPF_MAP_GET_NEXT_KEY is not supported for stacktrace map. However, there are use cases where user space wants to enumerate all stacktrace map entries where BPF_MAP_GET_NEXT_KEY command will be really helpful. In addition, if user space wants to delete all map entries in order to save memory and does not want to close the map file descriptor, BPF_MAP_GET_NEXT_KEY may help improve performance if map entries are sparsely populated.
The implementation has similar behavior for BPF_MAP_GET_NEXT_KEY implementation in hashtab. If user provides a NULL key pointer or an invalid key, the first key is returned. Otherwise, the first valid key after the input parameter "key" is returned, or -ENOENT if no valid key can be found.
--- kernel/bpf/stackmap.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-)
kernel/bpf/stackmap.c +26 -2
@@ -226,9 +226,33 @@ int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)return 0 ; } -static int stack_map_get_next_key (struct bpf_map *map,void *key,void *next_key) +static int stack_map_get_next_key (struct bpf_map *map,void *key, +void *next_key) { -return -EINVAL ; +struct bpf_stack_map *smap =container_of (map, + struct bpf_stack_map, map); +u32 id; + +WARN_ON_ONCE (!rcu_read_lock_held ()); + +if (!key) { + id =0 ; + }else { + id = *(u32 *)key; +if (id >= smap->n_buckets || !smap->buckets[id]) + id =0 ; +else + id++; + } + +while (id < smap->n_buckets && !smap->buckets[id]) + id++; + +if (id >= smap->n_buckets) +return -ENOENT ; + + *(u32 *)next_key = id; +return 0 ; } static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
Signature · 1 line
2.9.5
PATCH v2 2/2 tools/bpf: add a bpf selftest for stacktrace
In reply to Yonghong SongAdded a bpf selftest in test_progs at tools directory for stacktrace. The test will populate a hashtable map and a stacktrace map at the same time with the same key, stackid.
The user space will compare both maps, using BPF_MAP_LOOKUP_ELEM command and BPF_MAP_GET_NEXT_KEY command, to ensure that both have the same set of keys.
--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/test_progs.c | 127 ++++++++++++++++++++++ tools/testing/selftests/bpf/test_stacktrace_map.c | 62 +++++++++++ 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/test_stacktrace_map.c
tools/testing/selftests/bpf/Makefile +1 -1
@@ -19,7 +19,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \ test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o sockmap_parse_prog.o \ sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \ - test_l4lb_noinline.o test_xdp_noinline.o + test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o TEST_PROGS := test_kmod.sh test_xdp_redirect.sh test_xdp_meta.sh \ test_offload.py
tools/testing/selftests/bpf/test_progs.c +127 -0
139 lines not on this page · raw message
tools/testing/selftests/bpf/test_stacktrace_map.c new file +62 -0
62 lines not on this page · raw message
Signature · 1 line
2.9.5
Quoted 8 lines · Yonghong Song
The patch set implements bpf syscall command
BPF_MAP_GET_NEXT_KEYfor stacktrace map. Patch #1 is the core implementation and Patch #2 implements a bpf test attools/testing/selftests/bpfdirectory. Please see individual patch comments for details.Changelog:
v1 -> v2:
- For invalid key (key pointer is non-NULL), setsnext_keyto be the first valid key.
Applied to bpf-next, thanks Yonghong!