Skip to contentSkip to conversation

bpf: fix bpffs non-array map seq_show issue

PATCH In review Yonghong Song 3 participants 10 messages Aug 9, 2018

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: fix bpffs non-array map seq_show issue

In function map_seq_next() of kernel/bpf/inode.c, the first key will be the "0" regardless of the map type. This works for array. But for hash type, if it happens key "0" is in the map, the bpffs map show will miss some items if the key "0" is not the first element of the first bucket.

This patch fixed the issue by guaranteeing to get the first element, if the seq_show is just started, by passing NULL pointer key to map_get_next_key() callback. This way, no missing elements will occur for
bpffs hash table show even if key "0" is in the map.

Fixes a26ca7c982cb5 ("bpf: btf: Add pretty print support to the basic arraymap")
---
 kernel/bpf/inode.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/bpf/inode.c +5 -2
index 76efe9a183f5..794d06186e93 100644
@@ -196,14 +196,17 @@ static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
 {
 	struct bpf_map *map = seq_file_to_map(m);
 	void *key = map_iter(m)->key;
+	void *prev_key;
 
 	if (map_iter(m)->done)
 		return NULL;
 
 	if (unlikely(v == SEQ_START_TOKEN))
-		goto done;
+		prev_key = NULL;
+	else
+		prev_key = key;
 
-	if (map->ops->map_get_next_key(map, key, key)) {
+	if (map->ops->map_get_next_key(map, prev_key, key)) {
 		map_iter(m)->done = true;
 		return NULL;
 	}
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Yonghong Song

In function map_seq_next() of kernel/bpf/inode.c, the first key will be the "0" regardless of the map type. This works for array. But for hash type, if it happens key "0" is in the map, the bpffs map show will miss some items if the key "0" is not the first element of the first bucket.

Quoted 8 lines

This patch fixed the issue by guaranteeing to get the first element, if the seq_show is just started, by passing NULL pointer key to map_get_next_key() callback. This way, no missing elements will occur for
bpffs hash table show even if key "0" is in the map.

Fixes a26ca7c982cb5 ("bpf: btf: Add pretty print support to the basic arraymap")
#2 lore raw
Quoted 8 lines · Alexei Starovoitov
Quoted 7 lines · Yonghong Song

In function map_seq_next() of kernel/bpf/inode.c, the first key will be the "0" regardless of the map type. This works for array. But for hash type, if it happens key "0" is in the map, the bpffs map show will miss some items if the key "0" is not the first element of the first bucket.

Quoted 5 lines
Quoted 5 lines

This patch fixed the issue by guaranteeing to get the first element, if the seq_show is just started, by passing NULL pointer key to map_get_next_key() callback. This way, no missing elements will occur for
bpffs hash table show even if key "0" is in the map.

Currently, map_seq_show_elem callback is only implemented for arraymap. So the problem actually is not exposed.

The issue is discovered when I tried to implement map_seq_show_elem for hash maps, and I will have followup patches for it.

So this patch probably should apply to bpf-next or I can include this patch in my later patch set which implements map_seq_show_elem for hash map which can demonstrate the problem.

Please let me know.

Quoted 6 lines
Quoted 3 lines
Fixes a26ca7c982cb5 ("bpf: btf: Add pretty print support to the basic arraymap")
maintainer
#3 lore raw
Quoted 22 lines · Yonghong Song
Quoted 8 lines · Alexei Starovoitov
Quoted 7 lines · Yonghong Song

In function map_seq_next() of kernel/bpf/inode.c, the first key will be the "0" regardless of the map type. This works for array. But for hash type, if it happens key "0" is in the map, the bpffs map show will miss some items if the key "0" is not the first element of the first bucket.

Quoted 5 lines
Quoted 5 lines

This patch fixed the issue by guaranteeing to get the first element, if the seq_show is just started, by passing NULL pointer key to map_get_next_key() callback. This way, no missing elements will occur for
bpffs hash table show even if key "0" is in the map.

Currently, map_seq_show_elem callback is only implemented for arraymap. So the problem actually is not exposed.

The issue is discovered when I tried to implement map_seq_show_elem for hash maps, and I will have followup patches for it.

Quoted 11 lines

So this patch probably should apply to bpf-next or I can include this patch in my later patch set which implements map_seq_show_elem for hash map which can demonstrate the problem.

Please let me know.

Quoted 4 lines
Quoted 2 lines
Fixes a26ca7c982cb5 ("bpf: btf: Add pretty print support to the basic arraymap")

Given this doesn't affect any current code, I think bpf-next would be fine.

Anyway, this cannot be used as-is, results in following compile warning ...

# make -j4 kernel/bpf/
  DESCEND  objtool
  CALL    scripts/checksyscalls.sh
  CC      kernel/bpf/verifier.o
  CC      kernel/bpf/inode.o
kernel/bpf/inode.c: In function ‘map_seq_next’:
kernel/bpf/inode.c:214:1: warning: label ‘done’ defined but not used [-Wunused-label]
 done:
 ^~~~
  AR      kernel/bpf/built-in.a
#4 lore raw
Quoted 33 lines · Daniel Borkmann
Quoted 32 lines · Yonghong Song
Quoted 8 lines · Alexei Starovoitov
Quoted 7 lines · Yonghong Song

In function map_seq_next() of kernel/bpf/inode.c, the first key will be the "0" regardless of the map type. This works for array. But for hash type, if it happens key "0" is in the map, the bpffs map show will miss some items if the key "0" is not the first element of the first bucket.

Quoted 5 lines
Quoted 5 lines

This patch fixed the issue by guaranteeing to get the first element, if the seq_show is just started, by passing NULL pointer key to map_get_next_key() callback. This way, no missing elements will occur for
bpffs hash table show even if key "0" is in the map.

Currently, map_seq_show_elem callback is only implemented for arraymap. So the problem actually is not exposed.

The issue is discovered when I tried to implement map_seq_show_elem for hash maps, and I will have followup patches for it.

So this patch probably should apply to bpf-next or I can include this patch in my later patch set which implements map_seq_show_elem for hash map which can demonstrate the problem.

Please let me know.

Quoted 3 lines
Quoted 2 lines
Fixes a26ca7c982cb5 ("bpf: btf: Add pretty print support to the basic arraymap")
Quoted 7 lines
Quoted 1 line
Quoted 1 line

Given this doesn't affect any current code, I think bpf-next would be fine.

Anyway, this cannot be used as-is, results in following compile warning ...

Thanks and will fix the problem and resubmit the patch set to bpf-next.

Quoted 12 lines
# make -j4 kernel/bpf/
   DESCEND  objtool
   CALL    scripts/checksyscalls.sh
   CC      kernel/bpf/verifier.o
   CC      kernel/bpf/inode.o
kernel/bpf/inode.c: In function ‘map_seq_next’:
kernel/bpf/inode.c:214:1: warning: label ‘done’ defined but not used [-Wunused-label]
  done:
  ^~~~
   AR      kernel/bpf/built-in.a
maintainer
#5 lore raw
Quoted 17 lines · Yonghong Song
Quoted 16 lines · Daniel Borkmann
Quoted 15 lines · Yonghong Song
Quoted 13 lines · Alexei Starovoitov
Quoted 12 lines · Yonghong Song

In function map_seq_next() of kernel/bpf/inode.c, the first key will be the "0" regardless of the map type. This works for array. But for hash type, if it happens key "0" is in the map, the bpffs map show will miss some items if the key "0" is not the first element of the first bucket.

This patch fixed the issue by guaranteeing to get the first element, if the seq_show is just started, by passing NULL pointer key to map_get_next_key() callback. This way, no missing elements will occur for
bpffs hash table show even if key "0" is in the map.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Currently, map_seq_show_elem callback is only implemented for arraymap. So the problem actually is not exposed.

The issue is discovered when I tried to implement map_seq_show_elem for hash maps, and I will have followup patches for it.

Btw, on that note, I would also prefer if we could decouple BTF from the map_seq_show_elem() as there is really no reason to have it on a per-map. I had a patch below which would enable it for all map types generically, and bpftool works out of the box for it. Also, array doesn't really have to be 'int' type enforced as long as it's some data structure with 4 bytes, it's all fine, so this can be made fully generic (we only eventually care about the match in size).

Quoted 1 line

From 0a8be27cbc2ac0c6fc2632865b5afe37222a1fc7 Mon Sep 17 00:00:00 2001

Message-Id: <0a8be27cbc2ac0c6fc2632865b5afe37222a1fc7.1533830053.git.daniel@iogearbox.net>
From: Daniel Borkmann <[email protected]>
Date: Thu, 9 Aug 2018 16:50:21 +0200
Subject: [PATCH bpf-next] bpf, btf: enable for all maps

# bpftool m dump id 19
[{
        "key": {
            "": {
                "vip": 0,
                "vipv6": []
            },
            "port": 0,
            "family": 0,
            "proto": 0
        },
        "value": {
            "flags": 0,
            "vip_num": 0
        }
    }
]
---
 include/linux/bpf.h   |  4 +---
 kernel/bpf/arraymap.c | 27 ---------------------------
 kernel/bpf/inode.c    |  3 ++-
 kernel/bpf/syscall.c  | 24 ++++++++++++++++++++----
 4 files changed, 23 insertions(+), 35 deletions(-)
include/linux/bpf.h +1 -3
index cd8790d..91aa4be 100644
@@ -48,8 +48,6 @@ struct bpf_map_ops {
 	u32 (*map_fd_sys_lookup_elem)(void *ptr);
 	void (*map_seq_show_elem)(struct bpf_map *map, void *key,
 				  struct seq_file *m);
-	int (*map_check_btf)(const struct bpf_map *map, const struct btf *btf,
-			     u32 key_type_id, u32 value_type_id);
 };
 
 struct bpf_map {
@@ -118,7 +116,7 @@ static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
 
 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
 {
-	return map->ops->map_seq_show_elem && map->ops->map_check_btf;
+	return map->ops->map_seq_show_elem;
 }
 
 extern const struct bpf_map_ops bpf_map_offload_ops;
kernel/bpf/arraymap.c +0 -27
index 2aa55d030..67f0bdf 100644
@@ -358,32 +358,6 @@ static void array_map_seq_show_elem(struct bpf_map *map, void *key,
 	rcu_read_unlock();
 }
 
-static int array_map_check_btf(const struct bpf_map *map, const struct btf *btf,
-			       u32 btf_key_id, u32 btf_value_id)
-{
-	const struct btf_type *key_type, *value_type;
-	u32 key_size, value_size;
-	u32 int_data;
-
-	key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
-	if (!key_type || BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
-		return -EINVAL;
-
-	int_data = *(u32 *)(key_type + 1);
-	/* bpf array can only take a u32 key.  This check makes
-	 * sure that the btf matches the attr used during map_create.
-	 */
-	if (BTF_INT_BITS(int_data) != 32 || key_size != 4 ||
-	    BTF_INT_OFFSET(int_data))
-		return -EINVAL;
-
-	value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
-	if (!value_type || value_size != map->value_size)
-		return -EINVAL;
-
-	return 0;
-}
-
 const struct bpf_map_ops array_map_ops = {
 	.map_alloc_check = array_map_alloc_check,
 	.map_alloc = array_map_alloc,
@@ -394,7 +368,6 @@ const struct bpf_map_ops array_map_ops = {
 	.map_delete_elem = array_map_delete_elem,
 	.map_gen_lookup = array_map_gen_lookup,
 	.map_seq_show_elem = array_map_seq_show_elem,
-	.map_check_btf = array_map_check_btf,
 };
 
 const struct bpf_map_ops percpu_array_map_ops = {
kernel/bpf/inode.c +2 -1
index 76efe9a..400f27d 100644
@@ -332,7 +332,8 @@ static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
 	struct bpf_map *map = arg;
 
 	return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops,
-			     map->btf ? &bpffs_map_fops : &bpffs_obj_fops);
+			     bpf_map_support_seq_show(map) ?
+			     &bpffs_map_fops : &bpffs_obj_fops);
 }
 
 static struct dentry *
kernel/bpf/syscall.c +20 -4
index 5af4e9e..0b6f6e8 100644
@@ -455,6 +455,23 @@ static int bpf_obj_name_cpy(char *dst, const char *src)
 	return 0;
 }
 
+static int map_check_btf(const struct bpf_map *map, const struct btf *btf,
+			 u32 btf_key_id, u32 btf_value_id)
+{
+	const struct btf_type *key_type, *value_type;
+	u32 key_size, value_size;
+
+	key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
+	if (!key_type || key_size != map->key_size)
+		return -EINVAL;
+
+	value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
+	if (!value_type || value_size != map->value_size)
+		return -EINVAL;
+
+	return 0;
+}
+
 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
 /* called via syscall */
 static int map_create(union bpf_attr *attr)
@@ -489,8 +506,7 @@ static int map_create(union bpf_attr *attr)
 	atomic_set(&map->refcnt, 1);
 	atomic_set(&map->usercnt, 1);
 
-	if (bpf_map_support_seq_show(map) &&
-	    (attr->btf_key_type_id || attr->btf_value_type_id)) {
+	if (attr->btf_key_type_id || attr->btf_value_type_id) {
 		struct btf *btf;
 
 		if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
@@ -504,8 +520,8 @@ static int map_create(union bpf_attr *attr)
 			goto free_map_nouncharge;
 		}
 
-		err = map->ops->map_check_btf(map, btf, attr->btf_key_type_id,
-					      attr->btf_value_type_id);
+		err = map_check_btf(map, btf, attr->btf_key_type_id,
+				    attr->btf_value_type_id);
 		if (err) {
 			btf_put(btf);
 			goto free_map_nouncharge;
Signature · 1 line
2.9.5
#6 lore raw
Quoted 25 lines · Daniel Borkmann
Quoted 17 lines · Yonghong Song
Quoted 16 lines · Daniel Borkmann
Quoted 15 lines · Yonghong Song
Quoted 13 lines · Alexei Starovoitov
Quoted 12 lines · Yonghong Song

In function map_seq_next() of kernel/bpf/inode.c, the first key will be the "0" regardless of the map type. This works for array. But for hash type, if it happens key "0" is in the map, the bpffs map show will miss some items if the key "0" is not the first element of the first bucket.

This patch fixed the issue by guaranteeing to get the first element, if the seq_show is just started, by passing NULL pointer key to map_get_next_key() callback. This way, no missing elements will occur for
bpffs hash table show even if key "0" is in the map.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Currently, map_seq_show_elem callback is only implemented for arraymap. So the problem actually is not exposed.

The issue is discovered when I tried to implement map_seq_show_elem for hash maps, and I will have followup patches for it.

Quoted 8 lines

Btw, on that note, I would also prefer if we could decouple BTF from the map_seq_show_elem() as there is really no reason to have it on a per-map. I had a patch below which would enable it for all map types generically, and bpftool works out of the box for it. Also, array doesn't really have to be 'int' type enforced as long as it's some data structure with 4 bytes, it's all fine, so this can be made fully generic (we only eventually care about the match in size).

I agree with a generic map_check_btf as mostly we only care about size and this change should enable btftool btf based pretty print for hash/lru_hash tables.

Quoted 105 lines

From 0a8be27cbc2ac0c6fc2632865b5afe37222a1fc7 Mon Sep 17 00:00:00 2001
Message-Id: <0a8be27cbc2ac0c6fc2632865b5afe37222a1fc7.1533830053.git.daniel@iogearbox.net>
From: Daniel Borkmann <[email protected]>
Date: Thu, 9 Aug 2018 16:50:21 +0200
Subject: [PATCH bpf-next] bpf, btf: enable for all maps

# bpftool m dump id 19
[{
         "key": {
             "": {
                 "vip": 0,
                 "vipv6": []
             },
             "port": 0,
             "family": 0,
             "proto": 0
         },
         "value": {
             "flags": 0,
             "vip_num": 0
         }
     }
]
---
  include/linux/bpf.h   |  4 +---
  kernel/bpf/arraymap.c | 27 ---------------------------
  kernel/bpf/inode.c    |  3 ++-
  kernel/bpf/syscall.c  | 24 ++++++++++++++++++++----
  4 files changed, 23 insertions(+), 35 deletions(-)
include/linux/bpf.h +1 -3
index cd8790d..91aa4be 100644
@@ -48,8 +48,6 @@ struct bpf_map_ops {
  	u32 (*map_fd_sys_lookup_elem)(void *ptr);
  	void (*map_seq_show_elem)(struct bpf_map *map, void *key,
  				  struct seq_file *m);
-	int (*map_check_btf)(const struct bpf_map *map, const struct btf *btf,
-			     u32 key_type_id, u32 value_type_id);
  };
 
  struct bpf_map {
@@ -118,7 +116,7 @@ static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
 
  static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
  {
-	return map->ops->map_seq_show_elem && map->ops->map_check_btf;
+	return map->ops->map_seq_show_elem;
  }
 
  extern const struct bpf_map_ops bpf_map_offload_ops;
kernel/bpf/arraymap.c +0 -27
index 2aa55d030..67f0bdf 100644
@@ -358,32 +358,6 @@ static void array_map_seq_show_elem(struct bpf_map *map, void *key,
  	rcu_read_unlock();
  }
 
-static int array_map_check_btf(const struct bpf_map *map, const struct btf *btf,
-			       u32 btf_key_id, u32 btf_value_id)
-{
-	const struct btf_type *key_type, *value_type;
-	u32 key_size, value_size;
-	u32 int_data;
-
-	key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
-	if (!key_type || BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
-		return -EINVAL;
-
-	int_data = *(u32 *)(key_type + 1);
-	/* bpf array can only take a u32 key.  This check makes
-	 * sure that the btf matches the attr used during map_create.
-	 */
-	if (BTF_INT_BITS(int_data) != 32 || key_size != 4 ||
-	    BTF_INT_OFFSET(int_data))
-		return -EINVAL;
-
-	value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
-	if (!value_type || value_size != map->value_size)
-		return -EINVAL;
-
-	return 0;
-}
-
  const struct bpf_map_ops array_map_ops = {
  	.map_alloc_check = array_map_alloc_check,
  	.map_alloc = array_map_alloc,
@@ -394,7 +368,6 @@ const struct bpf_map_ops array_map_ops = {
  	.map_delete_elem = array_map_delete_elem,
  	.map_gen_lookup = array_map_gen_lookup,
  	.map_seq_show_elem = array_map_seq_show_elem,
-	.map_check_btf = array_map_check_btf,
  };
 
  const struct bpf_map_ops percpu_array_map_ops = {
kernel/bpf/inode.c +0 -0
index 76efe9a..400f27d 100644

kernel/bpf/inode.c static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg) · line 336

Quoted 6 lines
kernel/bpf/inode.c +2 -1
@@ -332,7 +332,8 @@ static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
  	struct bpf_map *map = arg;
 
  	return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops,
-			     map->btf ? &bpffs_map_fops : &bpffs_obj_fops);
+			     bpf_map_support_seq_show(map) ?
+			     &bpffs_map_fops : &bpffs_obj_fops);

There are an issue here, the condition bpf_map_support_seq_show(map) may not be enough since the map specific implementation assumes availability of btf and proper map key/value btf_type_id's. We can either use

    map->btf && bpf_map_support_seq_show(map)

condition here, or check map->btf in each individual implementation of map_support_seq_show().

Quoted 53 lines
kernel/bpf/inode.c +0 -0
  }
 
  static struct dentry *
kernel/bpf/syscall.c +20 -4
index 5af4e9e..0b6f6e8 100644
@@ -455,6 +455,23 @@ static int bpf_obj_name_cpy(char *dst, const char *src)
  	return 0;
  }
 
+static int map_check_btf(const struct bpf_map *map, const struct btf *btf,
+			 u32 btf_key_id, u32 btf_value_id)
+{
+	const struct btf_type *key_type, *value_type;
+	u32 key_size, value_size;
+
+	key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
+	if (!key_type || key_size != map->key_size)
+		return -EINVAL;
+
+	value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
+	if (!value_type || value_size != map->value_size)
+		return -EINVAL;
+
+	return 0;
+}
+
  #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
  /* called via syscall */
  static int map_create(union bpf_attr *attr)
@@ -489,8 +506,7 @@ static int map_create(union bpf_attr *attr)
  	atomic_set(&map->refcnt, 1);
  	atomic_set(&map->usercnt, 1);
 
-	if (bpf_map_support_seq_show(map) &&
-	    (attr->btf_key_type_id || attr->btf_value_type_id)) {
+	if (attr->btf_key_type_id || attr->btf_value_type_id) {
  		struct btf *btf;
 
  		if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
@@ -504,8 +520,8 @@ static int map_create(union bpf_attr *attr)
  			goto free_map_nouncharge;
  		}
 
-		err = map->ops->map_check_btf(map, btf, attr->btf_key_type_id,
-					      attr->btf_value_type_id);
+		err = map_check_btf(map, btf, attr->btf_key_type_id,
+				    attr->btf_value_type_id);
  		if (err) {
  			btf_put(btf);
  			goto free_map_nouncharge;
maintainer
#7 lore raw
Quoted 35 lines · Yonghong Song
Quoted 25 lines · Daniel Borkmann
Quoted 17 lines · Yonghong Song
Quoted 16 lines · Daniel Borkmann
Quoted 15 lines · Yonghong Song
Quoted 13 lines · Alexei Starovoitov
Quoted 12 lines · Yonghong Song

In function map_seq_next() of kernel/bpf/inode.c, the first key will be the "0" regardless of the map type. This works for array. But for hash type, if it happens key "0" is in the map, the bpffs map show will miss some items if the key "0" is not the first element of the first bucket.

This patch fixed the issue by guaranteeing to get the first element, if the seq_show is just started, by passing NULL pointer key to map_get_next_key() callback. This way, no missing elements will occur for
bpffs hash table show even if key "0" is in the map.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Currently, map_seq_show_elem callback is only implemented for arraymap. So the problem actually is not exposed.

The issue is discovered when I tried to implement map_seq_show_elem for hash maps, and I will have followup patches for it.

Quoted 8 lines

Btw, on that note, I would also prefer if we could decouple BTF from the map_seq_show_elem() as there is really no reason to have it on a per-map. I had a patch below which would enable it for all map types generically, and bpftool works out of the box for it. Also, array doesn't really have to be 'int' type enforced as long as it's some data structure with 4 bytes, it's all fine, so this can be made fully generic (we only eventually care about the match in size).

Quoted 2 lines

I agree with a generic map_check_btf as mostly we only care about size and this change should enable btftool btf based pretty print for hash/lru_hash tables.

Yep, agree, the below output from bpftool is from test_xdp_noinline.o where both work with it.

Quoted 106 lines
Quoted 106 lines

 From 0a8be27cbc2ac0c6fc2632865b5afe37222a1fc7 Mon Sep 17 00:00:00 2001
Message-Id: <0a8be27cbc2ac0c6fc2632865b5afe37222a1fc7.1533830053.git.daniel@iogearbox.net>
From: Daniel Borkmann <[email protected]>
Date: Thu, 9 Aug 2018 16:50:21 +0200
Subject: [PATCH bpf-next] bpf, btf: enable for all maps

# bpftool m dump id 19
[{
         "key": {
             "": {
                 "vip": 0,
                 "vipv6": []
             },
             "port": 0,
             "family": 0,
             "proto": 0
         },
         "value": {
             "flags": 0,
             "vip_num": 0
         }
     }
]

---
  include/linux/bpf.h   |  4 +--- kernel/bpf/arraymap.c | 27 --------------------------- kernel/bpf/inode.c    |  3 ++- kernel/bpf/syscall.c  | 24 ++++++++++++++++++++----

  4 files changed, 23 insertions(+), 35 deletions(-)
include/linux/bpf.h +1 -3
index cd8790d..91aa4be 100644
@@ -48,8 +48,6 @@ struct bpf_map_ops {
       u32 (*map_fd_sys_lookup_elem)(void *ptr);
       void (*map_seq_show_elem)(struct bpf_map *map, void *key,
                     struct seq_file *m);
-    int (*map_check_btf)(const struct bpf_map *map, const struct btf *btf,
-                 u32 key_type_id, u32 value_type_id);
   };
 
   struct bpf_map {
@@ -118,7 +116,7 @@ static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
 
   static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
   {
-    return map->ops->map_seq_show_elem && map->ops->map_check_btf;
+    return map->ops->map_seq_show_elem;
   }
 
   extern const struct bpf_map_ops bpf_map_offload_ops;
kernel/bpf/arraymap.c +0 -27
index 2aa55d030..67f0bdf 100644
@@ -358,32 +358,6 @@ static void array_map_seq_show_elem(struct bpf_map *map, void *key,
       rcu_read_unlock();
   }
 
-static int array_map_check_btf(const struct bpf_map *map, const struct btf *btf,
-                   u32 btf_key_id, u32 btf_value_id)
-{
-    const struct btf_type *key_type, *value_type;
-    u32 key_size, value_size;
-    u32 int_data;
-
-    key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
-    if (!key_type || BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
-        return -EINVAL;
-
-    int_data = *(u32 *)(key_type + 1);
-    /* bpf array can only take a u32 key.  This check makes
-     * sure that the btf matches the attr used during map_create.
-     */
-    if (BTF_INT_BITS(int_data) != 32 || key_size != 4 ||
-        BTF_INT_OFFSET(int_data))
-        return -EINVAL;
-
-    value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
-    if (!value_type || value_size != map->value_size)
-        return -EINVAL;
-
-    return 0;
-}
-
   const struct bpf_map_ops array_map_ops = {
       .map_alloc_check = array_map_alloc_check,
       .map_alloc = array_map_alloc,
@@ -394,7 +368,6 @@ const struct bpf_map_ops array_map_ops = {
       .map_delete_elem = array_map_delete_elem,
       .map_gen_lookup = array_map_gen_lookup,
       .map_seq_show_elem = array_map_seq_show_elem,
-    .map_check_btf = array_map_check_btf,
   };
 
   const struct bpf_map_ops percpu_array_map_ops = {
kernel/bpf/inode.c +0 -0
index 76efe9a..400f27d 100644
@@ -332,7 +332,8 @@ static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
       struct bpf_map *map = arg;
 
Quoted 10 lines

kernel/bpf/inode.c static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg) · line 336

Quoted 4 lines
kernel/bpf/inode.c +2 -1
       return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops,
-                 map->btf ? &bpffs_map_fops : &bpffs_obj_fops);
+                 bpf_map_support_seq_show(map) ?
+                 &bpffs_map_fops : &bpffs_obj_fops);

There are an issue here, the condition bpf_map_support_seq_show(map) may not be enough since the map specific implementation assumes availability of btf and proper map key/value btf_type_id's. We can either use
    map->btf && bpf_map_support_seq_show(map)
condition here, or check map->btf in each individual implementation
of map_support_seq_show().

Good, point, agree. Will fix and cook proper patch later today. Thanks Yonghong!

Quoted 53 lines
Quoted 53 lines
kernel/bpf/inode.c +0 -0
   }
 

  static struct dentry *

kernel/bpf/syscall.c +20 -4
index 5af4e9e..0b6f6e8 100644
@@ -455,6 +455,23 @@ static int bpf_obj_name_cpy(char *dst, const char *src)
       return 0;
   }
 
+static int map_check_btf(const struct bpf_map *map, const struct btf *btf,
+             u32 btf_key_id, u32 btf_value_id)
+{
+    const struct btf_type *key_type, *value_type;
+    u32 key_size, value_size;
+
+    key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
+    if (!key_type || key_size != map->key_size)
+        return -EINVAL;
+
+    value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
+    if (!value_type || value_size != map->value_size)
+        return -EINVAL;
+
+    return 0;
+}
+
   #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
   /* called via syscall */
   static int map_create(union bpf_attr *attr)
@@ -489,8 +506,7 @@ static int map_create(union bpf_attr *attr)
       atomic_set(&map->refcnt, 1);
       atomic_set(&map->usercnt, 1);
 
-    if (bpf_map_support_seq_show(map) &&
-        (attr->btf_key_type_id || attr->btf_value_type_id)) {
+    if (attr->btf_key_type_id || attr->btf_value_type_id) {
           struct btf *btf;
 
           if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
@@ -504,8 +520,8 @@ static int map_create(union bpf_attr *attr)
               goto free_map_nouncharge;
           }
 
-        err = map->ops->map_check_btf(map, btf, attr->btf_key_type_id,
-                          attr->btf_value_type_id);
+        err = map_check_btf(map, btf, attr->btf_key_type_id,
+                    attr->btf_value_type_id);
           if (err) {
               btf_put(btf);
               goto free_map_nouncharge;
#8 lore raw
Quoted 159 lines · Daniel Borkmann
Quoted 35 lines · Yonghong Song
Quoted 25 lines · Daniel Borkmann
Quoted 17 lines · Yonghong Song
Quoted 16 lines · Daniel Borkmann
Quoted 15 lines · Yonghong Song
Quoted 13 lines · Alexei Starovoitov
Quoted 12 lines · Yonghong Song

In function map_seq_next() of kernel/bpf/inode.c, the first key will be the "0" regardless of the map type. This works for array. But for hash type, if it happens key "0" is in the map, the bpffs map show will miss some items if the key "0" is not the first element of the first bucket.

This patch fixed the issue by guaranteeing to get the first element, if the seq_show is just started, by passing NULL pointer key to map_get_next_key() callback. This way, no missing elements will occur for
bpffs hash table show even if key "0" is in the map.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Currently, map_seq_show_elem callback is only implemented for arraymap. So the problem actually is not exposed.

The issue is discovered when I tried to implement map_seq_show_elem for hash maps, and I will have followup patches for it.

Quoted 8 lines

Btw, on that note, I would also prefer if we could decouple BTF from the map_seq_show_elem() as there is really no reason to have it on a per-map. I had a patch below which would enable it for all map types generically, and bpftool works out of the box for it. Also, array doesn't really have to be 'int' type enforced as long as it's some data structure with 4 bytes, it's all fine, so this can be made fully generic (we only eventually care about the match in size).

Quoted 2 lines

I agree with a generic map_check_btf as mostly we only care about size and this change should enable btftool btf based pretty print for hash/lru_hash tables.

Yep, agree, the below output from bpftool is from test_xdp_noinline.o where both work with it.

Quoted 106 lines
Quoted 106 lines

 From 0a8be27cbc2ac0c6fc2632865b5afe37222a1fc7 Mon Sep 17 00:00:00 2001
Message-Id: <0a8be27cbc2ac0c6fc2632865b5afe37222a1fc7.1533830053.git.daniel@iogearbox.net>
From: Daniel Borkmann <[email protected]>
Date: Thu, 9 Aug 2018 16:50:21 +0200
Subject: [PATCH bpf-next] bpf, btf: enable for all maps

# bpftool m dump id 19
[{
          "key": {
              "": {
                  "vip": 0,
                  "vipv6": []
              },
              "port": 0,
              "family": 0,
              "proto": 0
          },
          "value": {
              "flags": 0,
              "vip_num": 0
          }
      }
]
---
   include/linux/bpf.h   |  4 +---
   kernel/bpf/arraymap.c | 27 ---------------------------
   kernel/bpf/inode.c    |  3 ++-
   kernel/bpf/syscall.c  | 24 ++++++++++++++++++++----
   4 files changed, 23 insertions(+), 35 deletions(-)
include/linux/bpf.h +1 -3
index cd8790d..91aa4be 100644
@@ -48,8 +48,6 @@ struct bpf_map_ops {
       u32 (*map_fd_sys_lookup_elem)(void *ptr);
       void (*map_seq_show_elem)(struct bpf_map *map, void *key,
                     struct seq_file *m);
-    int (*map_check_btf)(const struct bpf_map *map, const struct btf *btf,
-                 u32 key_type_id, u32 value_type_id);
   };
 
   struct bpf_map {
@@ -118,7 +116,7 @@ static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
 
   static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
   {
-    return map->ops->map_seq_show_elem && map->ops->map_check_btf;
+    return map->ops->map_seq_show_elem;
   }
 
   extern const struct bpf_map_ops bpf_map_offload_ops;
kernel/bpf/arraymap.c +0 -27
index 2aa55d030..67f0bdf 100644
@@ -358,32 +358,6 @@ static void array_map_seq_show_elem(struct bpf_map *map, void *key,
       rcu_read_unlock();
   }
 
-static int array_map_check_btf(const struct bpf_map *map, const struct btf *btf,
-                   u32 btf_key_id, u32 btf_value_id)
-{
-    const struct btf_type *key_type, *value_type;
-    u32 key_size, value_size;
-    u32 int_data;
-
-    key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
-    if (!key_type || BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
-        return -EINVAL;
-
-    int_data = *(u32 *)(key_type + 1);
-    /* bpf array can only take a u32 key.  This check makes
-     * sure that the btf matches the attr used during map_create.
-     */
-    if (BTF_INT_BITS(int_data) != 32 || key_size != 4 ||
-        BTF_INT_OFFSET(int_data))
-        return -EINVAL;
-
-    value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
-    if (!value_type || value_size != map->value_size)
-        return -EINVAL;
-
-    return 0;
-}
-
   const struct bpf_map_ops array_map_ops = {
       .map_alloc_check = array_map_alloc_check,
       .map_alloc = array_map_alloc,
@@ -394,7 +368,6 @@ const struct bpf_map_ops array_map_ops = {
       .map_delete_elem = array_map_delete_elem,
       .map_gen_lookup = array_map_gen_lookup,
       .map_seq_show_elem = array_map_seq_show_elem,
-    .map_check_btf = array_map_check_btf,
   };
 
   const struct bpf_map_ops percpu_array_map_ops = {
kernel/bpf/inode.c +0 -0
index 76efe9a..400f27d 100644
@@ -332,7 +332,8 @@ static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
       struct bpf_map *map = arg;
 
Quoted 10 lines

kernel/bpf/inode.c static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg) · line 336

Quoted 4 lines
kernel/bpf/inode.c +2 -1
       return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops,
-                 map->btf ? &bpffs_map_fops : &bpffs_obj_fops);
+                 bpf_map_support_seq_show(map) ?
+                 &bpffs_map_fops : &bpffs_obj_fops);

There are an issue here, the condition bpf_map_support_seq_show(map) may not be enough since the map specific implementation assumes availability of btf and proper map key/value btf_type_id's. We can either use
    map->btf && bpf_map_support_seq_show(map)
condition here, or check map->btf in each individual implementation
of map_support_seq_show().

Quoted 1 line

Good, point, agree. Will fix and cook proper patch later today.

Sounds good. i will wait until this gets merged and then resubmit.

Quoted 56 lines

Thanks Yonghong!

Quoted 53 lines
Quoted 53 lines
kernel/bpf/inode.c +0 -0
   }
 
   static struct dentry *
kernel/bpf/syscall.c +20 -4
index 5af4e9e..0b6f6e8 100644
@@ -455,6 +455,23 @@ static int bpf_obj_name_cpy(char *dst, const char *src)
       return 0;
   }
 
+static int map_check_btf(const struct bpf_map *map, const struct btf *btf,
+             u32 btf_key_id, u32 btf_value_id)
+{
+    const struct btf_type *key_type, *value_type;
+    u32 key_size, value_size;
+
+    key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
+    if (!key_type || key_size != map->key_size)
+        return -EINVAL;
+
+    value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
+    if (!value_type || value_size != map->value_size)
+        return -EINVAL;
+
+    return 0;
+}
+
   #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
   /* called via syscall */
   static int map_create(union bpf_attr *attr)
@@ -489,8 +506,7 @@ static int map_create(union bpf_attr *attr)
       atomic_set(&map->refcnt, 1);
       atomic_set(&map->usercnt, 1);
 
-    if (bpf_map_support_seq_show(map) &&
-        (attr->btf_key_type_id || attr->btf_value_type_id)) {
+    if (attr->btf_key_type_id || attr->btf_value_type_id) {
           struct btf *btf;
 
           if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
@@ -504,8 +520,8 @@ static int map_create(union bpf_attr *attr)
               goto free_map_nouncharge;
           }
 
-        err = map->ops->map_check_btf(map, btf, attr->btf_key_type_id,
-                          attr->btf_value_type_id);
+        err = map_check_btf(map, btf, attr->btf_key_type_id,
+                    attr->btf_value_type_id);
           if (err) {
               btf_put(btf);
               goto free_map_nouncharge;
maintainer
#9 lore raw
Quoted 160 lines · Yonghong Song
Quoted 159 lines · Daniel Borkmann
Quoted 35 lines · Yonghong Song
Quoted 25 lines · Daniel Borkmann
Quoted 17 lines · Yonghong Song
Quoted 16 lines · Daniel Borkmann
Quoted 15 lines · Yonghong Song
Quoted 13 lines · Alexei Starovoitov
Quoted 12 lines · Yonghong Song

In function map_seq_next() of kernel/bpf/inode.c, the first key will be the "0" regardless of the map type. This works for array. But for hash type, if it happens key "0" is in the map, the bpffs map show will miss some items if the key "0" is not the first element of the first bucket.

This patch fixed the issue by guaranteeing to get the first element, if the seq_show is just started, by passing NULL pointer key to map_get_next_key() callback. This way, no missing elements will occur for
bpffs hash table show even if key "0" is in the map.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Currently, map_seq_show_elem callback is only implemented for arraymap. So the problem actually is not exposed.

The issue is discovered when I tried to implement map_seq_show_elem for hash maps, and I will have followup patches for it.

Quoted 8 lines

Btw, on that note, I would also prefer if we could decouple BTF from the map_seq_show_elem() as there is really no reason to have it on a per-map. I had a patch below which would enable it for all map types generically, and bpftool works out of the box for it. Also, array doesn't really have to be 'int' type enforced as long as it's some data structure with 4 bytes, it's all fine, so this can be made fully generic (we only eventually care about the match in size).

Quoted 2 lines

I agree with a generic map_check_btf as mostly we only care about size and this change should enable btftool btf based pretty print for hash/lru_hash tables.

Yep, agree, the below output from bpftool is from test_xdp_noinline.o where both work with it.

Quoted 116 lines
Quoted 104 lines

  From 0a8be27cbc2ac0c6fc2632865b5afe37222a1fc7 Mon Sep 17 00:00:00 2001
Message-Id: <0a8be27cbc2ac0c6fc2632865b5afe37222a1fc7.1533830053.git.daniel@iogearbox.net>
From: Daniel Borkmann <[email protected]>
Date: Thu, 9 Aug 2018 16:50:21 +0200
Subject: [PATCH bpf-next] bpf, btf: enable for all maps

# bpftool m dump id 19
[{
          "key": {
              "": {
                  "vip": 0,
                  "vipv6": []
              },
              "port": 0,
              "family": 0,
              "proto": 0
          },
          "value": {
              "flags": 0,
              "vip_num": 0
          }
      }
]

---
   include/linux/bpf.h   |  4 +--- kernel/bpf/arraymap.c | 27 --------------------------- kernel/bpf/inode.c    |  3 ++- kernel/bpf/syscall.c  | 24 ++++++++++++++++++++----

   4 files changed, 23 insertions(+), 35 deletions(-)
include/linux/bpf.h +1 -3
index cd8790d..91aa4be 100644
@@ -48,8 +48,6 @@ struct bpf_map_ops {
        u32 (*map_fd_sys_lookup_elem)(void *ptr);
        void (*map_seq_show_elem)(struct bpf_map *map, void *key,
                      struct seq_file *m);
-    int (*map_check_btf)(const struct bpf_map *map, const struct btf *btf,
-                 u32 key_type_id, u32 value_type_id);
    };
 
    struct bpf_map {
@@ -118,7 +116,7 @@ static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
 
    static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
    {
-    return map->ops->map_seq_show_elem && map->ops->map_check_btf;
+    return map->ops->map_seq_show_elem;
    }
 
    extern const struct bpf_map_ops bpf_map_offload_ops;
kernel/bpf/arraymap.c +0 -27
index 2aa55d030..67f0bdf 100644
@@ -358,32 +358,6 @@ static void array_map_seq_show_elem(struct bpf_map *map, void *key,
        rcu_read_unlock();
    }
 
-static int array_map_check_btf(const struct bpf_map *map, const struct btf *btf,
-                   u32 btf_key_id, u32 btf_value_id)
-{
-    const struct btf_type *key_type, *value_type;
-    u32 key_size, value_size;
-    u32 int_data;
-
-    key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
-    if (!key_type || BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
-        return -EINVAL;
-
-    int_data = *(u32 *)(key_type + 1);
-    /* bpf array can only take a u32 key.  This check makes
-     * sure that the btf matches the attr used during map_create.
-     */
-    if (BTF_INT_BITS(int_data) != 32 || key_size != 4 ||
-        BTF_INT_OFFSET(int_data))
-        return -EINVAL;
-
-    value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
-    if (!value_type || value_size != map->value_size)
-        return -EINVAL;
-
-    return 0;
-}
-
    const struct bpf_map_ops array_map_ops = {
        .map_alloc_check = array_map_alloc_check,
        .map_alloc = array_map_alloc,
@@ -394,7 +368,6 @@ const struct bpf_map_ops array_map_ops = {
        .map_delete_elem = array_map_delete_elem,
        .map_gen_lookup = array_map_gen_lookup,
        .map_seq_show_elem = array_map_seq_show_elem,
-    .map_check_btf = array_map_check_btf,
    };
 
    const struct bpf_map_ops percpu_array_map_ops = {
kernel/bpf/inode.c +0 -0
index 76efe9a..400f27d 100644

kernel/bpf/inode.c static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg) · line 336

Quoted 6 lines
kernel/bpf/inode.c +2 -1
@@ -332,7 +332,8 @@ static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
        struct bpf_map *map = arg;
 
        return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops,
-                 map->btf ? &bpffs_map_fops : &bpffs_obj_fops);
+                 bpf_map_support_seq_show(map) ?
+                 &bpffs_map_fops : &bpffs_obj_fops);

There are an issue here, the condition bpf_map_support_seq_show(map) may not be enough since the map specific implementation assumes availability of btf and proper map key/value btf_type_id's. We can either use
     map->btf && bpf_map_support_seq_show(map)
condition here, or check map->btf in each individual implementation
of map_support_seq_show().

Quoted 3 lines
Quoted 1 line

Good, point, agree. Will fix and cook proper patch later today.

Sounds good. i will wait until this gets merged and then resubmit.

Ok, just flushed it out. Thanks!