Skip to content

Commit

Permalink
bpf: fix bpffs non-array map seq_show issue
Browse files Browse the repository at this point in the history
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: a26ca7c ("bpf: btf: Add pretty print support to the basic arraymap")
Acked-by: Alexei Starovoitov <[email protected]>
Signed-off-by: Yonghong Song <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
  • Loading branch information
yonghong-song authored and borkmann committed Aug 10, 2018
1 parent 60afdf0 commit dc1508a
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions kernel/bpf/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,21 @@ 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;
}

done:
++(*pos);
return key;
}
Expand Down

0 comments on commit dc1508a

Please sign in to comment.