Skip to contentSkip to conversation

bpf: print a little more info about maps via cat /sys/fs/bpf/pinned_name

PATCH In review Maciej Żenczykowski 2 participants 6 messages May 21, 2022 – May 25, 2022

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

PATCH 1/1 bpf: print a little more info about maps via cat /sys/fs/bpf/pinned_name

From: Maciej Żenczykowski <[email protected]>

While this information can be fetched via bpftool,
the cli tool itself isn't always available on more limited systems.

From the information printed particularly the 'id' is useful since when combined with /proc/pid/fd/X and /proc/pid/fdinfo/X it allows tracking down which bpf maps a process has open (which can be useful for tracking down fd leaks).

---
 kernel/bpf/inode.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/bpf/inode.c +3 -0
index 4f841e16779e..784266e258fe 100644
@@ -257,6 +257,9 @@ static int map_seq_show(struct seq_file *m, void *v)
 	if (unlikely(v == SEQ_START_TOKEN)) {
 		seq_puts(m, "# WARNING!! The output is for debug purpose only\n");
 		seq_puts(m, "# WARNING!! The output format will change\n");
+		seq_printf(m, "# type: %d, key_size: %d, value_size: %d, max_entries: %d, id: %d\n",
+			   map->map_type, map->key_size, map->value_size, map->max_entries,
+			   map->id);
 	} else {
 		map->ops->map_seq_show_elem(map, key, m);
 	}
Signature · 1 line
2.36.1.124.g0e6072fb45-goog
#1 lore raw
Quoted 21 lines · Maciej Żenczykowski

From: Maciej Żenczykowski <[email protected]>

While this information can be fetched via bpftool,
the cli tool itself isn't always available on more limited systems.

From the information printed particularly the 'id' is useful since when combined with /proc/pid/fd/X and /proc/pid/fdinfo/X it allows tracking down which bpf maps a process has open (which can be useful for tracking down fd leaks).

---
 kernel/bpf/inode.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/bpf/inode.c +0 -0
index 4f841e16779e..784266e258fe 100644

kernel/bpf/inode.c static int map_seq_show(struct seq_file *m, void *v) · line 262

Quoted 6 lines
kernel/bpf/inode.c +3 -0
@@ -257,6 +257,9 @@ static int map_seq_show(struct seq_file *m, void *v)
        if (unlikely(v == SEQ_START_TOKEN)) {
                seq_puts(m, "# WARNING!! The output is for debug purpose only\n");
                seq_puts(m, "# WARNING!! The output format will change\n");
+               seq_printf(m, "# type: %d, key_size: %d, value_size: %d, max_entries: %d, id: %d\n",
+                          map->map_type, map->key_size, map->value_size, map->max_entries,
+                          map->id);

Maybe use cat /sys/fs/bpf/maps.debug instead?
It prints map id.

#2 lore raw
Quoted 19 lines · Alexei Starovoitov
Quoted 16 lines · Maciej Żenczykowski

From: Maciej Żenczykowski <[email protected]>

While this information can be fetched via bpftool,
the cli tool itself isn't always available on more limited systems.

From the information printed particularly the 'id' is useful since when combined with /proc/pid/fd/X and /proc/pid/fdinfo/X it allows tracking down which bpf maps a process has open (which can be useful for tracking down fd leaks).

---
 kernel/bpf/inode.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 14 lines

kernel/bpf/inode.c static int map_seq_show(struct seq_file *m, void *v) · line 262

Quoted 11 lines
kernel/bpf/inode.c +3 -0
index 4f841e16779e..784266e258fe 100644
@@ -257,6 +257,9 @@ static int map_seq_show(struct seq_file *m, void *v)
        if (unlikely(v == SEQ_START_TOKEN)) {
                seq_puts(m, "# WARNING!! The output is for debug purpose only\n");
                seq_puts(m, "# WARNING!! The output format will change\n");
+               seq_printf(m, "# type: %d, key_size: %d, value_size: %d, max_entries: %d, id: %d\n",
+                          map->map_type, map->key_size, map->value_size, map->max_entries,
+                          map->id);

Maybe use cat /sys/fs/bpf/maps.debug instead?
It prints map id.

Is this something that was very recently added?
I'm not seeing it on my 5.16 machine with bpffs mounted at /sys/fs/bpf.

#3 lore raw
Quoted 37 lines · Maciej Żenczykowski
Quoted 19 lines · Alexei Starovoitov
Quoted 16 lines · Maciej Żenczykowski

From: Maciej Żenczykowski <[email protected]>

While this information can be fetched via bpftool,
the cli tool itself isn't always available on more limited systems.

From the information printed particularly the 'id' is useful since when combined with /proc/pid/fd/X and /proc/pid/fdinfo/X it allows tracking down which bpf maps a process has open (which can be useful for tracking down fd leaks).

---
 kernel/bpf/inode.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 14 lines

kernel/bpf/inode.c static int map_seq_show(struct seq_file *m, void *v) · line 262

Quoted 11 lines
kernel/bpf/inode.c +3 -0
index 4f841e16779e..784266e258fe 100644
@@ -257,6 +257,9 @@ static int map_seq_show(struct seq_file *m, void *v)
        if (unlikely(v == SEQ_START_TOKEN)) {
                seq_puts(m, "# WARNING!! The output is for debug purpose only\n");
                seq_puts(m, "# WARNING!! The output format will change\n");
+               seq_printf(m, "# type: %d, key_size: %d, value_size: %d, max_entries: %d, id: %d\n",
+                          map->map_type, map->key_size, map->value_size, map->max_entries,
+                          map->id);

Maybe use cat /sys/fs/bpf/maps.debug instead?
It prints map id.

Quoted 2 lines

Is this something that was very recently added?
I'm not seeing it on my 5.16 machine with bpffs mounted at /sys/fs/bpf.

It was there since 2020.
Make sure you have CONFIG_BPF_PRELOAD.

#4 lore raw
Quoted 40 lines · Alexei Starovoitov
Quoted 37 lines · Maciej Żenczykowski
Quoted 33 lines · Alexei Starovoitov
Quoted 21 lines · Maciej Żenczykowski

From: Maciej Żenczykowski <[email protected]>

While this information can be fetched via bpftool,
the cli tool itself isn't always available on more limited systems.

From the information printed particularly the 'id' is useful since when combined with /proc/pid/fd/X and /proc/pid/fdinfo/X it allows tracking down which bpf maps a process has open (which can be useful for tracking down fd leaks).

---
 kernel/bpf/inode.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/bpf/inode.c +0 -0
index 4f841e16779e..784266e258fe 100644

kernel/bpf/inode.c static int map_seq_show(struct seq_file *m, void *v) · line 262

Quoted 6 lines
kernel/bpf/inode.c +3 -0
@@ -257,6 +257,9 @@ static int map_seq_show(struct seq_file *m, void *v)
        if (unlikely(v == SEQ_START_TOKEN)) {
                seq_puts(m, "# WARNING!! The output is for debug purpose only\n");
                seq_puts(m, "# WARNING!! The output format will change\n");
+               seq_printf(m, "# type: %d, key_size: %d, value_size: %d, max_entries: %d, id: %d\n",
+                          map->map_type, map->key_size, map->value_size, map->max_entries,
+                          map->id);

Maybe use cat /sys/fs/bpf/maps.debug instead?
It prints map id.

Quoted 5 lines
Quoted 2 lines

Is this something that was very recently added?
I'm not seeing it on my 5.16 machine with bpffs mounted at /sys/fs/bpf.

It was there since 2020.
Make sure you have CONFIG_BPF_PRELOAD.

Hmm. This seems very annoying to use in practice.

* it seems to default to disabled, and as such is disabled on:
- my Debian laptop and desktop (well google's corp Linux distro, but it should be close enough to Debian here)
- my latest Fedora 36 Desktop and VMs
- our production (server) kernels
- all current Android Common Kernel / Generic Kernel Image / Pixel kernel trees
- Android UML test image kernel build scripts
* enabling it on our server kernels results in a compilation failure (probably some missing backports, they're backported up the wazoo)
* enabling it on ACK 5.10 tree results in a very different build failure
* enabling it on ACK 5.15 successfully builds, but doesn't seem to work:

# uname -r
5.15.41-04342-g39bba8f6b9fe
# zcat /proc/config.gz  | egrep BPF_PRELOAD
CONFIG_BPF_PRELOAD=y
CONFIG_BPF_PRELOAD_UMD=y
# cat /proc/mounts | egrep sys
none /sys sysfs rw,relatime 0 0
none /sys/fs/bpf bpf rw,relatime 0 0
# cd /sys/fs/bpf
root@uml-x86-64:/sys/fs/bpf# ls -al
total 0
drwxrwxrwt 2 root root 0 May 25 15:13 .
drwxr-xr-x 6 root root 0 May 25 15:13 ..

Perhaps a limitation of UML, or something funky in the build process, or the files don't show up until maps/progs are pinned? Hard to say - this is after running our bpf (python) test suite (though it might not use pinned maps/programs)...

* finally.. I think it increases kernel size by a lot since I see:
120K ./kernel/bpf/preload/bpf_preload_kern.o
and in a different tree:
232K ./kernel/bpf/preload/bpf_preload.ko

I did get it to work by building a net next tree with the option manually enabled and installing it on some dev servers.
Those by default have some pinned progs/maps, and I do see the debug files.

When it works it's really nice... but I don't think I'm willing to pay the complexity/memory costs.

#5 lore raw
Quoted 2 lines · Maciej Żenczykowski

When it works it's really nice...

Load your own bpf map iterator program and pin anywhere in bpffs under name of choice and your own access permissions. See selftests/bpf/progs/bpf_iter_bpf_map.c