Commit 2824ecb7 authored by Daniel Borkmann's avatar Daniel Borkmann Committed by Alexei Starovoitov

bpf: allow for key-less BTF in array map

Given we'll be reusing BPF array maps for global data/bss/rodata
sections, we need a way to associate BTF DataSec type as its map
value type. In usual cases we have this ugly BPF_ANNOTATE_KV_PAIR()
macro hack e.g. via 38d5d3b3 ("bpf: Introduce BPF_ANNOTATE_KV_PAIR")
to get initial map to type association going. While more use cases
for it are discouraged, this also won't work for global data since
the use of array map is a BPF loader detail and therefore unknown
at compilation time. For array maps with just a single entry we make
an exception in terms of BTF in that key type is declared optional
if value type is of DataSec type. The latter LLVM is guaranteed to
emit and it also aligns with how we regard global data maps as just
a plain buffer area reusing existing map facilities for allowing
things like introspection with existing tools.
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 1dc92851
...@@ -51,6 +51,7 @@ bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s, ...@@ -51,6 +51,7 @@ bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,
const struct btf_member *m, const struct btf_member *m,
u32 expected_offset, u32 expected_size); u32 expected_offset, u32 expected_size);
int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t); int btf_find_spin_lock(const struct btf *btf, const struct btf_type *t);
bool btf_type_is_void(const struct btf_type *t);
#ifdef CONFIG_BPF_SYSCALL #ifdef CONFIG_BPF_SYSCALL
const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id); const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
......
...@@ -391,6 +391,7 @@ static void array_map_seq_show_elem(struct bpf_map *map, void *key, ...@@ -391,6 +391,7 @@ static void array_map_seq_show_elem(struct bpf_map *map, void *key,
return; return;
} }
if (map->btf_key_type_id)
seq_printf(m, "%u: ", *(u32 *)key); seq_printf(m, "%u: ", *(u32 *)key);
btf_type_seq_show(map->btf, map->btf_value_type_id, value, m); btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
seq_puts(m, "\n"); seq_puts(m, "\n");
...@@ -428,6 +429,18 @@ static int array_map_check_btf(const struct bpf_map *map, ...@@ -428,6 +429,18 @@ static int array_map_check_btf(const struct bpf_map *map,
{ {
u32 int_data; u32 int_data;
/* One exception for keyless BTF: .bss/.data/.rodata map */
if (btf_type_is_void(key_type)) {
if (map->map_type != BPF_MAP_TYPE_ARRAY ||
map->max_entries != 1)
return -EINVAL;
if (BTF_INFO_KIND(value_type->info) != BTF_KIND_DATASEC)
return -EINVAL;
return 0;
}
if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT) if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
return -EINVAL; return -EINVAL;
......
...@@ -326,7 +326,7 @@ static bool btf_type_is_modifier(const struct btf_type *t) ...@@ -326,7 +326,7 @@ static bool btf_type_is_modifier(const struct btf_type *t)
return false; return false;
} }
static bool btf_type_is_void(const struct btf_type *t) bool btf_type_is_void(const struct btf_type *t)
{ {
return t == &btf_void; return t == &btf_void;
} }
......
...@@ -504,9 +504,16 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf, ...@@ -504,9 +504,16 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf,
u32 key_size, value_size; u32 key_size, value_size;
int ret = 0; int ret = 0;
/* Some maps allow key to be unspecified. */
if (btf_key_id) {
key_type = btf_type_id_size(btf, &btf_key_id, &key_size); key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
if (!key_type || key_size != map->key_size) if (!key_type || key_size != map->key_size)
return -EINVAL; return -EINVAL;
} else {
key_type = btf_type_by_id(btf, 0);
if (!map->ops->map_check_btf)
return -EINVAL;
}
value_type = btf_type_id_size(btf, &btf_value_id, &value_size); value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
if (!value_type || value_size != map->value_size) if (!value_type || value_size != map->value_size)
...@@ -573,7 +580,7 @@ static int map_create(union bpf_attr *attr) ...@@ -573,7 +580,7 @@ static int map_create(union bpf_attr *attr)
if (attr->btf_key_type_id || attr->btf_value_type_id) { if (attr->btf_key_type_id || attr->btf_value_type_id) {
struct btf *btf; struct btf *btf;
if (!attr->btf_key_type_id || !attr->btf_value_type_id) { if (!attr->btf_value_type_id) {
err = -EINVAL; err = -EINVAL;
goto free_map_nouncharge; goto free_map_nouncharge;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment