tools/bpf: selftests: Add iter progs for bpf_map/task/task_file
authorYonghong Song <yhs@fb.com>
Sat, 9 May 2020 17:59:22 +0000 (10:59 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 10 May 2020 00:05:27 +0000 (17:05 -0700)
commitacf61631746c01850a9df0cd5617c5c29214776c
tree6be8f88c4a6646cff6c3bcdeedd0b9f467ceef89
parent7c128a6bbd4f5b6780a90f3ce9aff192b7dd9d6a
tools/bpf: selftests: Add iter progs for bpf_map/task/task_file

The implementation is arbitrary, just to show how the bpf programs
can be written for bpf_map/task/task_file. They can be costomized
for specific needs.

For example, for bpf_map, the iterator prints out:
  $ cat /sys/fs/bpf/my_bpf_map
      id   refcnt  usercnt  locked_vm
       3        2        0         20
       6        2        0         20
       9        2        0         20
      12        2        0         20
      13        2        0         20
      16        2        0         20
      19        2        0         20
      %%% END %%%

For task, the iterator prints out:
  $ cat /sys/fs/bpf/my_task
    tgid      gid
       1        1
       2        2
    ....
    1944     1944
    1948     1948
    1949     1949
    1953     1953
    === END ===

For task/file, the iterator prints out:
  $ cat /sys/fs/bpf/my_task_file
    tgid      gid       fd      file
       1        1        0 ffffffff95c97600
       1        1        1 ffffffff95c97600
       1        1        2 ffffffff95c97600
    ....
    1895     1895      255 ffffffff95c8fe00
    1932     1932        0 ffffffff95c8fe00
    1932     1932        1 ffffffff95c8fe00
    1932     1932        2 ffffffff95c8fe00
    1932     1932        3 ffffffff95c185c0

This is able to print out all open files (fd and file->f_op), so user can compare
f_op against a particular kernel file operations to find what it is.
For example, from /proc/kallsyms, we can find
  ffffffff95c185c0 r eventfd_fops
so we will know tgid 1932 fd 3 is an eventfd file descriptor.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200509175922.2477576-1-yhs@fb.com
tools/testing/selftests/bpf/progs/bpf_iter_bpf_map.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/bpf_iter_task.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/bpf_iter_task_file.c [new file with mode: 0644]