bpf: bpf link iterator
Subsystems (5)
cover bpf: bpf link iterator
Bpf links seem to be one of the important structures for which no iterator is provided. Such iterator could be useful in those cases when generic 'task/file' is not suitable or better performance is needed.
The implementation is mostly copied from prog iterator, and I would like to get any high-level feedback about what needs to be different or have to be taken into account. As a side note, I would also appreciate if someone could point me out to some guide about writing selftests for bpf subsystem -- for some unclear reason I couldn't compile the test from this changeset, and was testing it only manually with a custom test program.
Dmitrii Dolgov (2):
bpf: Add bpf_link iterator
selftests/bpf: Add bpf_link test
include/linux/bpf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/link_iter.c | 107 ++++++++++++++++++ kernel/bpf/syscall.c | 19 ++++ .../selftests/bpf/prog_tests/bpf_iter.c | 15 +++ .../selftests/bpf/progs/bpf_iter_bpf_link.c | 18 +++ 6 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/link_iter.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c
Signature · 1 line
2.32.0
RFC PATCH 1/2 bpf: Add bpf_link iterator
Implement bpf_link iterator to traverse links via bpf_seq_file operations. The changeset is mostly shamelessly copied from commit a228a64fc1e4 ("bpf: Add bpf_prog iterator")
--- include/linux/bpf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/link_iter.c | 107 +++++++++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 19 ++++++++ 4 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/link_iter.c
include/linux/bpf.h +1 -0
@@ -1489,6 +1489,7 @@ void bpf_link_put(struct bpf_link *link);int bpf_link_new_fd (struct bpf_link *link);struct file *bpf_link_new_file (struct bpf_link *link,int *reserved_fd);struct bpf_link *bpf_link_get_from_fd (u32 ufd); +struct bpf_link *bpf_link_get_curr_or_next (u32 *id);int bpf_obj_pin_user (u32 ufd,const char __user *pathname);int bpf_obj_get_user (const char __user *pathname,int flags);
kernel/bpf/Makefile +1 -1
@@ -6,7 +6,7 @@ cflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse endif CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy) -obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o
kernel/bpf/link_iter.c new file +107 -0
107 lines not on this page · raw message
kernel/bpf/syscall.c +19 -0
@@ -4454,6 +4454,25 @@ struct bpf_link *bpf_link_by_id(u32 id)return link; } +struct bpf_link *bpf_link_get_curr_or_next (u32 *id) +{ +struct bpf_link *link; + +spin_lock_bh (&link_idr_lock); +again: + link =idr_get_next (&link_idr, id); +if (link) { + link =bpf_link_inc_not_zero (link); +if (IS_ERR (link)) { + (*id)++; + goto again; + } + } +spin_unlock_bh (&link_idr_lock); + +return link; +} +#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_idstatic int bpf_link_get_fd_by_id (const union bpf_attr *attr)
Signature · 1 line
2.32.0
Re: [RFC PATCH bpf-next 1/2] bpf: Add bpf_link iterator
Quoted 45 lines · Dmitrii Dolgov
Implement
bpf_linkiterator to traverse links viabpf_seq_fileoperations. The changeset is mostly shamelessly copied from commita228a64fc1e4("bpf: Addbpf_progiterator")Signed-off-by Dmitrii Dolgov <[email protected]>--- include/linux/bpf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/link_iter.c | 107 +++++++++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 19 ++++++++ 4 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/link_iter.cinclude/linux/bpf.h +1
-0@@ -1489,6 +1489,7 @@ void bpf_link_put(struct bpf_link *link);int bpf_link_new_fd (struct bpf_link *link);struct file *bpf_link_new_file (struct bpf_link *link,int *reserved_fd);struct bpf_link *bpf_link_get_from_fd (u32 ufd); +struct bpf_link *bpf_link_get_curr_or_next (u32 *id);int bpf_obj_pin_user (u32 ufd,const char __user *pathname);int bpf_obj_get_user (const char __user *pathname,int flags);kernel/bpf/Makefile +1
-1@@ -6,7 +6,7 @@ cflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse endif CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy) -obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.okernel/bpf/link_iter.c new file +0
-0
kernel/bpf/link_iter.c line 2
Quoted 2 lines
kernel/bpf/link_iter.c new file +2
-0@@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2020 Facebook */
this probably needs a bit of update?
Quoted 10 lines
kernel/bpf/link_iter.c new file +10
-0+#include <linux/bpf.h> +#include <linux/fs.h> +#include <linux/filter.h> +#include <linux/kernel.h> +#include <linux/btf_ids.h> + +struct bpf_iter_seq_link_info { +u32 link_id; +}; +
[...]
Re: [RFC PATCH bpf-next 1/2] bpf: Add bpf_link iterator
In reply to Dmitrii DolgovQuoted 3 lines · Dmitrii Dolgov
Implement
bpf_linkiterator to traverse links viabpf_seq_fileoperations. The changeset is mostly shamelessly copied from commita228a64fc1e4("bpf: Addbpf_progiterator")
LGTM except one copyright issue mentioned by Andrii early.
Quoted 41 lines
Signed-off-by Dmitrii Dolgov <[email protected]>--- include/linux/bpf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/link_iter.c | 107 +++++++++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 19 ++++++++ 4 files changed, 128 insertions(+), 1 deletion(-)create mode 100644
kernel/bpf/link_iter.cinclude/linux/bpf.h +1
-0@@ -1489,6 +1489,7 @@ void bpf_link_put(struct bpf_link *link);int bpf_link_new_fd (struct bpf_link *link);struct file *bpf_link_new_file (struct bpf_link *link,int *reserved_fd);struct bpf_link *bpf_link_get_from_fd (u32 ufd); +struct bpf_link *bpf_link_get_curr_or_next (u32 *id);int bpf_obj_pin_user (u32 ufd,const char __user *pathname);int bpf_obj_get_user (const char __user *pathname,int flags);kernel/bpf/Makefile +1
-1@@ -6,7 +6,7 @@ cflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse endif CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy) -obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.okernel/bpf/link_iter.c new file +0
-0
kernel/bpf/link_iter.c line 2
Quoted 2 lines
kernel/bpf/link_iter.c new file +2
-0@@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2020 Facebook */
Change to your own copyright.
Quoted 6 lines
kernel/bpf/link_iter.c new file +6
-0+#include <linux/bpf.h> +#include <linux/fs.h> +#include <linux/filter.h> +#include <linux/kernel.h> +#include <linux/btf_ids.h> +
[...]
A message that is not in the archive (sent off-list or not yet received): CA+q6zcXkSBrmnUt3jS+zggqJjUFJQ2J_qUmA4HXtcFmYzYppMg@mail.gmail.com
Re: [RFC PATCH bpf-next 1/2] bpf: Add bpf_link iterator
Quoted 13 lines · Dmitry Dolgov
Quoted 10 lines · On Thu, Apr 28, 2022, 6:14 PM Yonghong Song <[email protected] <mailto:[email protected]>>…
kernel/bpf/link_iter.c line 2
Quoted 8 lines
kernel/bpf/link_iter.c new file +2
-0@@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2020 Facebook */ Change to your own copyright.
Quoted 1 line
Thanks for reviewing. I have to admit I'm not sure how should it look like, is there anything like a guideline/best practices about what to put into the copyright line?
You can do
/* Copyright (c) 2022 <Your Company Name> */
or
/* Copyright (c) 2022 <Your Name> */
RFC PATCH 2/2 selftests/bpf: Add bpf link iter test
In reply to Dmitrii DolgovAdd a simple test for bpf link iterator
--- .../selftests/bpf/prog_tests/bpf_iter.c | 15 +++++++++++++++ .../selftests/bpf/progs/bpf_iter_bpf_link.c | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +15 -0
@@ -26,6 +26,7 @@#include "bpf_iter_bpf_sk_storage_map.skel.h" #include "bpf_iter_test_kern5.skel.h" #include "bpf_iter_test_kern6.skel.h" +#include "bpf_iter_bpf_link.skel.h" static int duration;
@@ -1172,6 +1173,20 @@ static void test_buf_neg_offset(void)bpf_iter_test_kern6__destroy (skel); } +static void test_link_iter (void ) +{ +struct bpf_iter_bpf_link *skel; + + skel =bpf_iter_bpf_link__open_and_load (); +if (CHECK (skel,"bpf_iter_bpf_link__open_and_load" , +"skeleton open_and_load unexpected success\n" )) +return ; + +do_dummy_read (skel->progs.dump_bpf_link); + +bpf_iter_bpf_link__destroy (skel); +} +#define CMP_BUFFER_SIZE 1024static char task_vma_output[CMP_BUFFER_SIZE ];static char proc_maps_output[CMP_BUFFER_SIZE ];
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c new file +18 -0
@@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Facebook */ +#include "bpf_iter.h" +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +SEC("iter/bpf_link") +int dump_bpf_link (struct bpf_iter__bpf_link *ctx) +{ +struct seq_file *seq = ctx->meta->seq; +struct bpf_link *link = ctx->link; +int link_id; + + link_id = link->id; +bpf_seq_write (seq, &link_id,sizeof (link_id)); +return 0 ; +}
Signature · 1 line
2.32.0
Re: [RFC PATCH bpf-next 2/2] selftests/bpf: Add bpf link iter test
Quoted 25 lines · Dmitrii Dolgov
Add a simple test for bpf link iterator
Signed-off-by Dmitrii Dolgov <[email protected]>--- .../selftests/bpf/prog_tests/bpf_iter.c | 15 +++++++++++++++ .../selftests/bpf/progs/bpf_iter_bpf_link.c | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.ctools/testing/selftests/bpf/prog_tests/bpf_iter.c +1
-0@@ -26,6 +26,7 @@#include "bpf_iter_bpf_sk_storage_map.skel.h" #include "bpf_iter_test_kern5.skel.h" #include "bpf_iter_test_kern6.skel.h" +#include "bpf_iter_bpf_link.skel.h" static int duration;@@ -1172,6 +1173,20 @@ static void test_buf_neg_offset(void)bpf_iter_test_kern6__destroy (skel); }
tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_buf_neg_offset(void) · line 1184
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +9
-0+static void test_link_iter (void ) +{ +struct bpf_iter_bpf_link *skel; + + skel =bpf_iter_bpf_link__open_and_load (); +if (CHECK (skel,"bpf_iter_bpf_link__open_and_load" , +"skeleton open_and_load unexpected success\n" )) +return ; +
please don't use CHECK() for new tests, you need ASSERT_OK_PTR() here
Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +5
-0+do_dummy_read (skel->progs.dump_bpf_link); + +bpf_iter_bpf_link__destroy (skel); +} +#define CMP_BUFFER_SIZE 1024static char task_vma_output[CMP_BUFFER_SIZE ];static char proc_maps_output[CMP_BUFFER_SIZE ];tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c new file +0
-0
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c line 9
Quoted 9 lines
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c new file +9
-0@@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Facebook */ +#include "bpf_iter.h" +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +SEC("iter/bpf_link") +int dump_bpf_link (struct bpf_iter__bpf_link *ctx)
see progs/bpf_iter.h, let's add bpf_iter__bpf_link there as well
Quoted 12 lines
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c new file +9
-1+{ +struct seq_file *seq = ctx->meta->seq; +struct bpf_link *link = ctx->link; +int link_id; + + link_id = link->id; +bpf_seq_write (seq, &link_id,sizeof (link_id)); +return 0 ; +} --2.32.0
Re: [RFC PATCH bpf-next 2/2] selftests/bpf: Add bpf link iter test
In reply to Dmitrii DolgovQuoted 22 lines · Dmitrii Dolgov
Add a simple test for bpf link iterator
Signed-off-by Dmitrii Dolgov <[email protected]>--- .../selftests/bpf/prog_tests/bpf_iter.c | 15 +++++++++++++++ .../selftests/bpf/progs/bpf_iter_bpf_link.c | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+)create mode 100644
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.ctools/testing/selftests/bpf/prog_tests/bpf_iter.c +1
-0@@ -26,6 +26,7 @@#include "bpf_iter_bpf_sk_storage_map.skel.h" #include "bpf_iter_test_kern5.skel.h" #include "bpf_iter_test_kern6.skel.h" +#include "bpf_iter_bpf_link.skel.h" static int duration;
tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_buf_neg_offset(void) · line 1176
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +1
-0@@ -1172,6 +1173,20 @@ static void test_buf_neg_offset(void)bpf_iter_test_kern6__destroy (skel); } +static void test_link_iter (void )
This function is used. Please add a proper subtest for this in function test_bpf_iter().
tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_buf_neg_offset(void) · line 1183
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +7
-0+{ +struct bpf_iter_bpf_link *skel; + + skel =bpf_iter_bpf_link__open_and_load (); +if (CHECK (skel,"bpf_iter_bpf_link__open_and_load" , +"skeleton open_and_load unexpected success\n" )) +return ;
This is not correct. You should have CHECK(!skel, ...) to return
only if skel is NULL. The error message "skeleton open_and_load unexpected success\n" is not correct either. Probably a copy-paste error.
Also, since you are working on this file, probably convert all CHECK's in this file to ASSERT_*() macros as patch #2. Then this patch itself can be patch #3 using ASSERT_*() as well.
Quoted 15 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +6
-0+ +do_dummy_read (skel->progs.dump_bpf_link); + +bpf_iter_bpf_link__destroy (skel); +} +#define CMP_BUFFER_SIZE 1024static char task_vma_output[CMP_BUFFER_SIZE ];static char proc_maps_output[CMP_BUFFER_SIZE ];tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c new file +0
-0
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c line 2
Quoted 2 lines
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c new file +2
-0@@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Facebook */
copyright issue.
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c line 9
Quoted 7 lines
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c new file +7
-0+#include "bpf_iter.h" +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +SEC("iter/bpf_link") +int dump_bpf_link (struct bpf_iter__bpf_link *ctx)
Please put bpf_iter__bpf_link definition in bpf_iter.h so the test can work with an old version of vmlinux.h.
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c line 13
Quoted 4 lines
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c new file +4
-0+{ +struct seq_file *seq = ctx->meta->seq; +struct bpf_link *link = ctx->link; +int link_id;
The 'link' pointer could be NULL as in previous patch we have:
+ .ctx_arg_info = {
+ { offsetof(struct bpf_iter__bpf_link, link),
+ PTR_TO_BTF_ID_OR_NULL },
+ },
So you need to add a check below.
if (!link) return 0;
Quoted 5 lines
tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c new file +5
-0+ + link_id = link->id; +bpf_seq_write (seq, &link_id,sizeof (link_id)); +return 0 ; +}
Quoted 5 lines · Dmitrii Dolgov
Bpf links seem to be one of the important structures for which no iterator is provided. Such iterator could be useful in those cases when generic 'task/file' is not suitable or better performance is needed.
Overall looks good, I'll defer to Yonghong to validate kernel-side iterator logic. Yonghong, can you please take a look? Thanks!
Quoted 8 lines
The implementation is mostly copied from prog iterator, and I would like to get any high-level feedback about what needs to be different or have to be taken into account. As a side note, I would also appreciate if someone could point me out to some guide about writing selftests for bpf subsystem -- for some unclear reason I couldn't compile the test from this changeset, and was testing it only manually with a custom test program.
What was the error? Generally, you need very recent Clang (probably built from sources), latest pahole built from sources, and you should compile kernel before building selftests/bpf.
Quoted 17 lines
Dmitrii Dolgov (2):
bpf: Addbpf_linkiterator
selftests/bpf: Addbpf_linktestinclude/linux/bpf.h | 1 + kernel/bpf/Makefile | 2 +- kernel/bpf/link_iter.c | 107 ++++++++++++++++++ kernel/bpf/syscall.c | 19 ++++ .../selftests/bpf/prog_tests/bpf_iter.c | 15 +++ .../selftests/bpf/progs/bpf_iter_bpf_link.c | 18 +++ 6 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/link_iter.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_bpf_link.c--
2.32.0