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

Configure Feed

Select the types of activity you want to include in your feed.

bpf: generalize and export map_get_next_key for arrays

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>

authored by

Anton Protopopov and committed by
Alexei Starovoitov
44481e49 f7d72d0b

+15 -10
+6
include/linux/bpf.h
··· 2107 2107 }; 2108 2108 }; 2109 2109 2110 + /* 2111 + * The bpf_array_get_next_key() function may be used for all array-like 2112 + * maps, i.e., maps with u32 keys with range [0 ,..., max_entries) 2113 + */ 2114 + int bpf_array_get_next_key(struct bpf_map *map, void *key, void *next_key); 2115 + 2110 2116 #define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */ 2111 2117 #define MAX_TAIL_CALL_CNT 33 2112 2118
+9 -10
kernel/bpf/arraymap.c
··· 335 335 } 336 336 337 337 /* Called from syscall */ 338 - static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key) 338 + int bpf_array_get_next_key(struct bpf_map *map, void *key, void *next_key) 339 339 { 340 - struct bpf_array *array = container_of(map, struct bpf_array, map); 341 340 u32 index = key ? *(u32 *)key : U32_MAX; 342 341 u32 *next = (u32 *)next_key; 343 342 344 - if (index >= array->map.max_entries) { 343 + if (index >= map->max_entries) { 345 344 *next = 0; 346 345 return 0; 347 346 } 348 347 349 - if (index == array->map.max_entries - 1) 348 + if (index == map->max_entries - 1) 350 349 return -ENOENT; 351 350 352 351 *next = index + 1; ··· 788 789 .map_alloc_check = array_map_alloc_check, 789 790 .map_alloc = array_map_alloc, 790 791 .map_free = array_map_free, 791 - .map_get_next_key = array_map_get_next_key, 792 + .map_get_next_key = bpf_array_get_next_key, 792 793 .map_release_uref = array_map_free_internal_structs, 793 794 .map_lookup_elem = array_map_lookup_elem, 794 795 .map_update_elem = array_map_update_elem, ··· 814 815 .map_alloc_check = array_map_alloc_check, 815 816 .map_alloc = array_map_alloc, 816 817 .map_free = array_map_free, 817 - .map_get_next_key = array_map_get_next_key, 818 + .map_get_next_key = bpf_array_get_next_key, 818 819 .map_lookup_elem = percpu_array_map_lookup_elem, 819 820 .map_gen_lookup = percpu_array_map_gen_lookup, 820 821 .map_update_elem = array_map_update_elem, ··· 1203 1204 .map_poke_track = prog_array_map_poke_track, 1204 1205 .map_poke_untrack = prog_array_map_poke_untrack, 1205 1206 .map_poke_run = prog_array_map_poke_run, 1206 - .map_get_next_key = array_map_get_next_key, 1207 + .map_get_next_key = bpf_array_get_next_key, 1207 1208 .map_lookup_elem = fd_array_map_lookup_elem, 1208 1209 .map_delete_elem = fd_array_map_delete_elem, 1209 1210 .map_fd_get_ptr = prog_fd_array_get_ptr, ··· 1307 1308 .map_alloc_check = fd_array_map_alloc_check, 1308 1309 .map_alloc = array_map_alloc, 1309 1310 .map_free = perf_event_fd_array_map_free, 1310 - .map_get_next_key = array_map_get_next_key, 1311 + .map_get_next_key = bpf_array_get_next_key, 1311 1312 .map_lookup_elem = fd_array_map_lookup_elem, 1312 1313 .map_delete_elem = fd_array_map_delete_elem, 1313 1314 .map_fd_get_ptr = perf_event_fd_array_get_ptr, ··· 1343 1344 .map_alloc_check = fd_array_map_alloc_check, 1344 1345 .map_alloc = array_map_alloc, 1345 1346 .map_free = cgroup_fd_array_free, 1346 - .map_get_next_key = array_map_get_next_key, 1347 + .map_get_next_key = bpf_array_get_next_key, 1347 1348 .map_lookup_elem = fd_array_map_lookup_elem, 1348 1349 .map_delete_elem = fd_array_map_delete_elem, 1349 1350 .map_fd_get_ptr = cgroup_fd_array_get_ptr, ··· 1428 1429 .map_alloc_check = fd_array_map_alloc_check, 1429 1430 .map_alloc = array_of_map_alloc, 1430 1431 .map_free = array_of_map_free, 1431 - .map_get_next_key = array_map_get_next_key, 1432 + .map_get_next_key = bpf_array_get_next_key, 1432 1433 .map_lookup_elem = array_of_map_lookup_elem, 1433 1434 .map_delete_elem = fd_array_map_delete_elem, 1434 1435 .map_fd_get_ptr = bpf_map_fd_get_ptr,