Commit 1713e33b authored by Alexei Starovoitov's avatar Alexei Starovoitov
Browse files

Merge branch 'libbpf: deprecate legacy BPF map definitions'



Andrii Nakryiko says:

====================

Officially deprecate legacy BPF map definitions in libbpf. They've been slated
for deprecation for a while in favor of more powerful BTF-defined map
definitions and this patch set adds warnings and a way to enforce this in
libbpf through LIBBPF_STRICT_MAP_DEFINITIONS strict mode flag.

Selftests are fixed up and updated, BPF documentation is updated, bpftool's
strict mode usage is adjusted to avoid breaking users unnecessarily.

v1->v2:
  - replace missed bpf_map_def case in Documentation/bpf/btf.rst (Alexei).
====================

Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 1058b6a7 96c85308
Loading
Loading
Loading
Loading
+14 −18
Original line number Diff line number Diff line
@@ -565,18 +565,15 @@ A map can be created with ``btf_fd`` and specified key/value type id.::
In libbpf, the map can be defined with extra annotation like below:
::

    struct bpf_map_def SEC("maps") btf_map = {
        .type = BPF_MAP_TYPE_ARRAY,
        .key_size = sizeof(int),
        .value_size = sizeof(struct ipv_counts),
        .max_entries = 4,
    };
    BPF_ANNOTATE_KV_PAIR(btf_map, int, struct ipv_counts);
    struct {
        __uint(type, BPF_MAP_TYPE_ARRAY);
        __type(key, int);
        __type(value, struct ipv_counts);
        __uint(max_entries, 4);
    } btf_map SEC(".maps");

Here, the parameters for macro BPF_ANNOTATE_KV_PAIR are map name, key and
value types for the map. During ELF parsing, libbpf is able to extract
key/value type_id's and assign them to BPF_MAP_CREATE attributes
automatically.
During ELF parsing, libbpf is able to extract key/value type_id's and assign
them to BPF_MAP_CREATE attributes automatically.

.. _BPF_Prog_Load:

@@ -824,13 +821,12 @@ structure has bitfields. For example, for the following map,::
           ___A b1:4;
           enum A b2:4;
      };
      struct bpf_map_def SEC("maps") tmpmap = {
           .type = BPF_MAP_TYPE_ARRAY,
           .key_size = sizeof(__u32),
           .value_size = sizeof(struct tmp_t),
           .max_entries = 1,
      };
      BPF_ANNOTATE_KV_PAIR(tmpmap, int, struct tmp_t);
      struct {
           __uint(type, BPF_MAP_TYPE_ARRAY);
           __type(key, int);
           __type(value, struct tmp_t);
           __uint(max_entries, 1);
      } tmpmap SEC(".maps");

bpftool is able to pretty print like below:
::
+8 −1
Original line number Diff line number Diff line
@@ -478,7 +478,14 @@ int main(int argc, char **argv)
	}

	if (!legacy_libbpf) {
		ret = libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
		enum libbpf_strict_mode mode;

		/* Allow legacy map definitions for skeleton generation.
		 * It will still be rejected if users use LIBBPF_STRICT_ALL
		 * mode for loading generated skeleton.
		 */
		mode = (__LIBBPF_STRICT_LAST - 1) & ~LIBBPF_STRICT_MAP_DEFINITIONS;
		ret = libbpf_set_strict_mode(mode);
		if (ret)
			p_err("failed to enable libbpf strict mode: %d", ret);
	}
+1 −1
Original line number Diff line number Diff line
@@ -133,7 +133,7 @@ struct bpf_map_def {
	unsigned int value_size;
	unsigned int max_entries;
	unsigned int map_flags;
};
} __attribute__((deprecated("use BTF-defined maps in .maps section")));

enum libbpf_pin_type {
	LIBBPF_PIN_NONE,
+8 −0
Original line number Diff line number Diff line
@@ -1937,6 +1937,11 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
	if (obj->efile.maps_shndx < 0)
		return 0;

	if (libbpf_mode & LIBBPF_STRICT_MAP_DEFINITIONS) {
		pr_warn("legacy map definitions in SEC(\"maps\") are not supported\n");
		return -EOPNOTSUPP;
	}

	if (!symbols)
		return -EINVAL;

@@ -1999,6 +2004,8 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
			return -LIBBPF_ERRNO__FORMAT;
		}

		pr_warn("map '%s' (legacy): legacy map definitions are deprecated, use BTF-defined maps instead\n", map_name);

		if (ELF64_ST_BIND(sym->st_info) == STB_LOCAL) {
			pr_warn("map '%s' (legacy): static maps are not supported\n", map_name);
			return -ENOTSUP;
@@ -4190,6 +4197,7 @@ static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
		return 0;

	if (!bpf_map__is_internal(map)) {
		pr_warn("Use of BPF_ANNOTATE_KV_PAIR is deprecated, use BTF-defined maps in .maps section instead\n");
		ret = btf__get_map_kv_tids(obj->btf, map->name, def->key_size,
					   def->value_size, &key_type_id,
					   &value_type_id);
+5 −0
Original line number Diff line number Diff line
@@ -73,6 +73,11 @@ enum libbpf_strict_mode {
	 * operation.
	 */
	LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK = 0x10,
	/*
	 * Error out on any SEC("maps") map definition, which are deprecated
	 * in favor of BTF-defined map definitions in SEC(".maps").
	 */
	LIBBPF_STRICT_MAP_DEFINITIONS = 0x20,

	__LIBBPF_STRICT_LAST,
};
Loading