Re: [PATCH v5 bpf-next 2/5] libbpf: Add "map_extra" as a per-map-type extra flag

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On 10/22/21 3:02 PM, Joanne Koong wrote:
This patch adds the libbpf infrastructure for supporting a
per-map-type "map_extra" field, whose definition will be
idiosyncratic depending on map type.

For example, for the bloom filter map, the lower 4 bits of
map_extra is used to denote the number of hash functions.

Please note that until libbpf 1.0 is here, the
"bpf_create_map_params" struct is used as a temporary
means for propagating the map_extra field to the kernel.

Signed-off-by: Joanne Koong <joannekoong@xxxxxx>
---
  include/uapi/linux/bpf.h         |  1 +
  tools/include/uapi/linux/bpf.h   |  1 +
  tools/lib/bpf/bpf.c              | 27 ++++++++++++++++++++-
  tools/lib/bpf/bpf_gen_internal.h |  2 +-
  tools/lib/bpf/gen_loader.c       |  3 ++-
  tools/lib/bpf/libbpf.c           | 41 ++++++++++++++++++++++++++++----
  tools/lib/bpf/libbpf.h           |  3 +++
  tools/lib/bpf/libbpf.map         |  2 ++
  tools/lib/bpf/libbpf_internal.h  | 25 ++++++++++++++++++-
  9 files changed, 96 insertions(+), 9 deletions(-)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 66827b93f548..bb64d407b8bd 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -5646,6 +5646,7 @@ struct bpf_map_info {
  	__u32 btf_id;
  	__u32 btf_key_type_id;
  	__u32 btf_value_type_id;
+	__u64 map_extra;
  } __attribute__((aligned(8)));
struct bpf_btf_info {
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 66827b93f548..bb64d407b8bd 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -5646,6 +5646,7 @@ struct bpf_map_info {
  	__u32 btf_id;
  	__u32 btf_key_type_id;
  	__u32 btf_value_type_id;
+	__u64 map_extra;
  } __attribute__((aligned(8)));


this is kernel UAPI changes. They should go into the first patch or separate patch. And you probably need to extend bpf_map_get_info_by_fd() such that it actually populates map_extra field.


struct bpf_btf_info {


[...]


diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index db6e48014839..751cfb9778dc 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -400,6 +400,7 @@ struct bpf_map {
  	char *pin_path;
  	bool pinned;
  	bool reused;
+	__u64 map_extra;
  };
enum extern_type {
@@ -2313,6 +2314,17 @@ int parse_btf_map_def(const char *map_name, struct btf *btf,
  			}
  			map_def->pinning = val;
  			map_def->parts |= MAP_DEF_PINNING;
+		} else if (strcmp(name, "map_extra") == 0) {
+			/*
+			 * TODO: When the BTF array supports __u64s, read into
+			 * map_def->map_extra directly.
+			 */


Please drop the TODO comment. There are no plans to extend BTF arrays to support __u64 sizes.


+			__u32 map_extra;
+
+			if (!get_map_field_int(map_name, btf, m, &map_extra))
+				return -EINVAL;
+			map_def->map_extra = map_extra;
+			map_def->parts |= MAP_DEF_MAP_EXTRA;
  		} else {
  			if (strict) {
  				pr_warn("map '%s': unknown field '%s'.\n", map_name, name);
@@ -2337,6 +2349,7 @@ static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def
  	map->def.value_size = def->value_size;
  	map->def.max_entries = def->max_entries;
  	map->def.map_flags = def->map_flags;
+	map->map_extra = def->map_extra;
map->numa_node = def->numa_node;
  	map->btf_key_type_id = def->key_type_id;
@@ -2360,7 +2373,9 @@ static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def
  	if (def->parts & MAP_DEF_MAX_ENTRIES)
  		pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries);
  	if (def->parts & MAP_DEF_MAP_FLAGS)
-		pr_debug("map '%s': found map_flags = %u.\n", map->name, def->map_flags);
+		pr_debug("map '%s': found map_flags = 0x%X.\n", map->name, def->map_flags);
+	if (def->parts & MAP_DEF_MAP_EXTRA)
+		pr_debug("map '%s': found map_extra = 0x%llX.\n", map->name, def->map_extra);


%llx will cause compilation warnings on some architectures if used with __u64. Please cast to (long long) to avoid that.


  	if (def->parts & MAP_DEF_PINNING)
  		pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning);
  	if (def->parts & MAP_DEF_NUMA_NODE)
@@ -4199,6 +4214,7 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd)
  	map->btf_key_type_id = info.btf_key_type_id;
  	map->btf_value_type_id = info.btf_value_type_id;
  	map->reused = true;
+	map->map_extra = info.map_extra;
return 0;


[...]




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux