From: Yonghong Song <yhs@fb.com>
To: <bpf@vger.kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
Cong Wang <xiyou.wangcong@gmail.com>,
Daniel Borkmann <daniel@iogearbox.net>, <kernel-team@fb.com>,
Andrii Nakryiko <andrii@kernel.org>
Subject: [PATCH bpf-next v4 07/12] bpf: add arraymap support for bpf_for_each_map_elem() helper
Date: Thu, 25 Feb 2021 21:13:13 -0800 [thread overview]
Message-ID: <20210226051313.3429218-1-yhs@fb.com> (raw)
In-Reply-To: <20210226051305.3428235-1-yhs@fb.com>
This patch added support for arraymap and percpu arraymap.
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Yonghong Song <yhs@fb.com>
---
kernel/bpf/arraymap.c | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 1f8453343bf2..463d25e1e67e 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -625,6 +625,42 @@ static const struct bpf_iter_seq_info iter_seq_info = {
.seq_priv_size = sizeof(struct bpf_iter_seq_array_map_info),
};
+static int bpf_for_each_array_elem(struct bpf_map *map, void *callback_fn,
+ void *callback_ctx, u64 flags)
+{
+ u32 i, key, num_elems = 0;
+ struct bpf_array *array;
+ bool is_percpu;
+ u64 ret = 0;
+ void *val;
+
+ if (flags != 0)
+ return -EINVAL;
+
+ is_percpu = map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
+ array = container_of(map, struct bpf_array, map);
+ if (is_percpu)
+ migrate_disable();
+ for (i = 0; i < map->max_entries; i++) {
+ if (is_percpu)
+ val = this_cpu_ptr(array->pptrs[i]);
+ else
+ val = array->value + array->elem_size * i;
+ num_elems++;
+ key = i;
+ ret = BPF_CAST_CALL(callback_fn)((u64)(long)map,
+ (u64)(long)&key, (u64)(long)val,
+ (u64)(long)callback_ctx, 0);
+ /* return value: 0 - continue, 1 - stop and return */
+ if (ret)
+ break;
+ }
+
+ if (is_percpu)
+ migrate_enable();
+ return num_elems;
+}
+
static int array_map_btf_id;
const struct bpf_map_ops array_map_ops = {
.map_meta_equal = array_map_meta_equal,
@@ -643,6 +679,8 @@ const struct bpf_map_ops array_map_ops = {
.map_check_btf = array_map_check_btf,
.map_lookup_batch = generic_map_lookup_batch,
.map_update_batch = generic_map_update_batch,
+ .map_set_for_each_callback_args = map_set_for_each_callback_args,
+ .map_for_each_callback = bpf_for_each_array_elem,
.map_btf_name = "bpf_array",
.map_btf_id = &array_map_btf_id,
.iter_seq_info = &iter_seq_info,
@@ -660,6 +698,8 @@ const struct bpf_map_ops percpu_array_map_ops = {
.map_delete_elem = array_map_delete_elem,
.map_seq_show_elem = percpu_array_map_seq_show_elem,
.map_check_btf = array_map_check_btf,
+ .map_set_for_each_callback_args = map_set_for_each_callback_args,
+ .map_for_each_callback = bpf_for_each_array_elem,
.map_btf_name = "bpf_array",
.map_btf_id = &percpu_array_map_btf_id,
.iter_seq_info = &iter_seq_info,
--
2.24.1
next prev parent reply other threads:[~2021-02-26 5:14 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-26 5:13 [PATCH bpf-next v4 00/12] bpf: add bpf_for_each_map_elem() helper Yonghong Song
2021-02-26 5:13 ` [PATCH bpf-next v4 01/12] bpf: factor out visit_func_call_insn() in check_cfg() Yonghong Song
2021-02-26 19:12 ` Andrii Nakryiko
2021-02-26 5:13 ` [PATCH bpf-next v4 02/12] bpf: factor out verbose_invalid_scalar() Yonghong Song
2021-02-26 5:13 ` [PATCH bpf-next v4 03/12] bpf: refactor check_func_call() to allow callback function Yonghong Song
2021-02-26 19:13 ` Andrii Nakryiko
2021-02-26 5:13 ` [PATCH bpf-next v4 04/12] bpf: change return value of verifier function add_subprog() Yonghong Song
2021-02-26 19:14 ` Andrii Nakryiko
2021-02-26 5:13 ` [PATCH bpf-next v4 05/12] bpf: add bpf_for_each_map_elem() helper Yonghong Song
2021-02-26 19:22 ` Andrii Nakryiko
2021-02-26 19:27 ` Alexei Starovoitov
2021-02-26 5:13 ` [PATCH bpf-next v4 06/12] bpf: add hashtab support for " Yonghong Song
2021-02-26 5:13 ` Yonghong Song [this message]
2021-02-26 5:13 ` [PATCH bpf-next v4 08/12] libbpf: move function is_ldimm64() earlier in libbpf.c Yonghong Song
2021-02-26 5:13 ` [PATCH bpf-next v4 09/12] libbpf: support subprog address relocation Yonghong Song
2021-02-26 5:13 ` [PATCH bpf-next v4 10/12] bpftool: print subprog address properly Yonghong Song
2021-02-26 5:13 ` [PATCH bpf-next v4 11/12] selftests/bpf: add hashmap test for bpf_for_each_map_elem() helper Yonghong Song
2021-02-26 19:17 ` Andrii Nakryiko
2021-02-26 5:13 ` [PATCH bpf-next v4 12/12] selftests/bpf: add arraymap " Yonghong Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210226051313.3429218-1-yhs@fb.com \
--to=yhs@fb.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=xiyou.wangcong@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox