Skip to content

Commit e2842be

Browse files
tohojoborkmann
authored andcommitted
libbpf: Add setter for initial value for internal maps
For internal maps (most notably the maps backing global variables), libbpf uses an internal mmaped area to store the data after opening the object. This data is subsequently copied into the kernel map when the object is loaded. This adds a function to set a new value for that data, which can be used to before it is loaded into the kernel. This is especially relevant for RODATA maps, since those are frozen on load. Signed-off-by: Toke Høiland-Jørgensen <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5a95cbb commit e2842be

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6758,6 +6758,17 @@ void *bpf_map__priv(const struct bpf_map *map)
67586758
return map ? map->priv : ERR_PTR(-EINVAL);
67596759
}
67606760

6761+
int bpf_map__set_initial_value(struct bpf_map *map,
6762+
const void *data, size_t size)
6763+
{
6764+
if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG ||
6765+
size != map->def.value_size || map->fd >= 0)
6766+
return -EINVAL;
6767+
6768+
memcpy(map->mmaped, data, size);
6769+
return 0;
6770+
}
6771+
67616772
bool bpf_map__is_offload_neutral(const struct bpf_map *map)
67626773
{
67636774
return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;

tools/lib/bpf/libbpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
407407
LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
408408
bpf_map_clear_priv_t clear_priv);
409409
LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
410+
LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
411+
const void *data, size_t size);
410412
LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
411413
LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
412414
LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ LIBBPF_0.0.8 {
243243
bpf_link__pin;
244244
bpf_link__pin_path;
245245
bpf_link__unpin;
246+
bpf_map__set_initial_value;
246247
bpf_program__set_attach_target;
247248
bpf_set_link_xdp_fd_opts;
248249
} LIBBPF_0.0.7;

0 commit comments

Comments
 (0)