]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
bpf: generalize and export map_get_next_key for arrays
authorAnton Protopopov <a.s.protopopov@gmail.com>
Sun, 19 Oct 2025 20:21:31 +0000 (20:21 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 21 Oct 2025 18:17:25 +0000 (11:17 -0700)
The kernel/bpf/array.c file defines the array_map_get_next_key()
function which finds the next key for array maps. It actually doesn't
use any map fields besides the generic max_entries field. Generalize
it, and export as bpf_array_get_next_key() such that it can be
re-used by other array-like maps.

Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20251019202145.3944697-4-a.s.protopopov@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/bpf.h
kernel/bpf/arraymap.c

index 3bda915cd7a8e8fc994815d59e0c6211f6b51f4f..e53cda0aabb6845215c9c5d4fefbee307b91975b 100644 (file)
@@ -2107,6 +2107,12 @@ struct bpf_array {
        };
 };
 
+/*
+ * The bpf_array_get_next_key() function may be used for all array-like
+ * maps, i.e., maps with u32 keys with range [0 ,..., max_entries)
+ */
+int bpf_array_get_next_key(struct bpf_map *map, void *key, void *next_key);
+
 #define BPF_COMPLEXITY_LIMIT_INSNS      1000000 /* yes. 1M insns */
 #define MAX_TAIL_CALL_CNT 33
 
index 0ba790c2d2e5fdda967fcc94eb6ecf41250da76f..1eeb31c5b317d671d9a156ae06fcbf5263089d16 100644 (file)
@@ -335,18 +335,17 @@ int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
 }
 
 /* Called from syscall */
-static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
+int bpf_array_get_next_key(struct bpf_map *map, void *key, void *next_key)
 {
-       struct bpf_array *array = container_of(map, struct bpf_array, map);
        u32 index = key ? *(u32 *)key : U32_MAX;
        u32 *next = (u32 *)next_key;
 
-       if (index >= array->map.max_entries) {
+       if (index >= map->max_entries) {
                *next = 0;
                return 0;
        }
 
-       if (index == array->map.max_entries - 1)
+       if (index == map->max_entries - 1)
                return -ENOENT;
 
        *next = index + 1;
@@ -789,7 +788,7 @@ const struct bpf_map_ops array_map_ops = {
        .map_alloc_check = array_map_alloc_check,
        .map_alloc = array_map_alloc,
        .map_free = array_map_free,
-       .map_get_next_key = array_map_get_next_key,
+       .map_get_next_key = bpf_array_get_next_key,
        .map_release_uref = array_map_free_internal_structs,
        .map_lookup_elem = array_map_lookup_elem,
        .map_update_elem = array_map_update_elem,
@@ -815,7 +814,7 @@ const struct bpf_map_ops percpu_array_map_ops = {
        .map_alloc_check = array_map_alloc_check,
        .map_alloc = array_map_alloc,
        .map_free = array_map_free,
-       .map_get_next_key = array_map_get_next_key,
+       .map_get_next_key = bpf_array_get_next_key,
        .map_lookup_elem = percpu_array_map_lookup_elem,
        .map_gen_lookup = percpu_array_map_gen_lookup,
        .map_update_elem = array_map_update_elem,
@@ -1204,7 +1203,7 @@ const struct bpf_map_ops prog_array_map_ops = {
        .map_poke_track = prog_array_map_poke_track,
        .map_poke_untrack = prog_array_map_poke_untrack,
        .map_poke_run = prog_array_map_poke_run,
-       .map_get_next_key = array_map_get_next_key,
+       .map_get_next_key = bpf_array_get_next_key,
        .map_lookup_elem = fd_array_map_lookup_elem,
        .map_delete_elem = fd_array_map_delete_elem,
        .map_fd_get_ptr = prog_fd_array_get_ptr,
@@ -1308,7 +1307,7 @@ const struct bpf_map_ops perf_event_array_map_ops = {
        .map_alloc_check = fd_array_map_alloc_check,
        .map_alloc = array_map_alloc,
        .map_free = perf_event_fd_array_map_free,
-       .map_get_next_key = array_map_get_next_key,
+       .map_get_next_key = bpf_array_get_next_key,
        .map_lookup_elem = fd_array_map_lookup_elem,
        .map_delete_elem = fd_array_map_delete_elem,
        .map_fd_get_ptr = perf_event_fd_array_get_ptr,
@@ -1344,7 +1343,7 @@ const struct bpf_map_ops cgroup_array_map_ops = {
        .map_alloc_check = fd_array_map_alloc_check,
        .map_alloc = array_map_alloc,
        .map_free = cgroup_fd_array_free,
-       .map_get_next_key = array_map_get_next_key,
+       .map_get_next_key = bpf_array_get_next_key,
        .map_lookup_elem = fd_array_map_lookup_elem,
        .map_delete_elem = fd_array_map_delete_elem,
        .map_fd_get_ptr = cgroup_fd_array_get_ptr,
@@ -1429,7 +1428,7 @@ const struct bpf_map_ops array_of_maps_map_ops = {
        .map_alloc_check = fd_array_map_alloc_check,
        .map_alloc = array_of_map_alloc,
        .map_free = array_of_map_free,
-       .map_get_next_key = array_map_get_next_key,
+       .map_get_next_key = bpf_array_get_next_key,
        .map_lookup_elem = array_of_map_lookup_elem,
        .map_delete_elem = fd_array_map_delete_elem,
        .map_fd_get_ptr = bpf_map_fd_get_ptr,