From: "Toke Høiland-Jørgensen" <toke@redhat.com>
To: Joanne Koong <joannekoong@fb.com>, bpf@vger.kernel.org
Cc: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net,
kafai@fb.com, Kernel-team@fb.com,
Joanne Koong <joannekoong@fb.com>
Subject: Re: [PATCH bpf-next 1/3] bpf: Add bpf_for_each helper
Date: Thu, 18 Nov 2021 12:11:36 +0100 [thread overview]
Message-ID: <87wnl5en13.fsf@toke.dk> (raw)
In-Reply-To: <20211118010404.2415864-2-joannekoong@fb.com>
Joanne Koong <joannekoong@fb.com> writes:
> This patch adds the kernel-side and API changes for a new helper
> function, bpf_for_each:
>
> long bpf_for_each(u32 nr_interations, void *callback_fn,
> void *callback_ctx, u64 flags);
>
> bpf_for_each invokes the "callback_fn" nr_iterations number of times
> or until the callback_fn returns 1.
>
> A few things to please note:
> ~ The "u64 flags" parameter is currently unused but is included in
> case a future use case for it arises.
> ~ In the kernel-side implementation of bpf_for_each (kernel/bpf/bpf_iter.c),
> bpf_callback_t is used as the callback function cast.
> ~ A program can have nested bpf_for_each calls but the program must
> still adhere to the verifier constraint of its stack depth (the stack depth
> cannot exceed MAX_BPF_STACK))
> ~ The next patch will include the tests and benchmark
>
> Signed-off-by: Joanne Koong <joannekoong@fb.com>
Great to see this! One small nit, below, but otherwise:
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
> ---
> include/linux/bpf.h | 1 +
> include/uapi/linux/bpf.h | 23 +++++++++++++++++++++++
> kernel/bpf/bpf_iter.c | 32 ++++++++++++++++++++++++++++++++
> kernel/bpf/helpers.c | 2 ++
> kernel/bpf/verifier.c | 28 ++++++++++++++++++++++++++++
> tools/include/uapi/linux/bpf.h | 23 +++++++++++++++++++++++
> 6 files changed, 109 insertions(+)
>
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 6deebf8bf78f..d9b69a896c91 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -2107,6 +2107,7 @@ extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
> extern const struct bpf_func_proto bpf_task_storage_get_proto;
> extern const struct bpf_func_proto bpf_task_storage_delete_proto;
> extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
> +extern const struct bpf_func_proto bpf_for_each_proto;
> extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
> extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
> extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index bd0c9f0487f6..ea5098920ed2 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -4750,6 +4750,28 @@ union bpf_attr {
> * The number of traversed map elements for success, **-EINVAL** for
> * invalid **flags**.
> *
> + * long bpf_for_each(u32 nr_iterations, void *callback_fn, void *callback_ctx, u64 flags)
> + * Description
> + * For **nr_iterations**, call **callback_fn** function with
> + * **callback_ctx** as the context parameter.
> + * The **callback_fn** should be a static function and
> + * the **callback_ctx** should be a pointer to the stack.
> + * The **flags** is used to control certain aspects of the helper.
> + * Currently, the **flags** must be 0.
> + *
> + * long (\*callback_fn)(u32 index, void \*ctx);
> + *
> + * where **index** is the current index in the iteration. The index
> + * is zero-indexed.
> + *
> + * If **callback_fn** returns 0, the helper will continue to the next
> + * iteration. If return value is 1, the helper will skip the rest of
> + * the iterations and return. Other return values are not used now.
The code will actually return for any non-zero value, though? So
shouldn't the documentation reflect this? Or, alternatively, should the
verifier enforce that the function can only return 0 or 1?
next prev parent reply other threads:[~2021-11-18 11:13 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-18 1:04 [PATCH bpf-next 0/3] Add bpf_for_each helper Joanne Koong
2021-11-18 1:04 ` [PATCH bpf-next 1/3] bpf: " Joanne Koong
2021-11-18 11:11 ` Toke Høiland-Jørgensen [this message]
2021-11-18 18:03 ` Yonghong Song
2021-11-19 12:17 ` Toke Høiland-Jørgensen
2021-11-18 17:59 ` Yonghong Song
2021-11-19 2:40 ` Joanne Koong
2021-11-18 20:14 ` Andrii Nakryiko
[not found] ` <9cf25708-c878-65db-0dfd-a76e83fe9e39@fb.com>
2021-11-19 21:50 ` Joanne Koong
2021-11-18 1:04 ` [PATCH bpf-next 2/3] selftests/bpf: Add tests for bpf_for_each Joanne Koong
2021-11-18 20:23 ` Andrii Nakryiko
2021-11-18 1:04 ` [PATCH bpf-next 3/3] selftest/bpf/benchs: add bpf_for_each benchmark Joanne Koong
2021-11-18 11:18 ` Toke Høiland-Jørgensen
2021-11-18 19:55 ` Andrii Nakryiko
2021-11-19 13:04 ` Toke Høiland-Jørgensen
2021-11-19 22:50 ` Andrii Nakryiko
2021-11-22 14:27 ` Toke Høiland-Jørgensen
2021-11-18 20:00 ` Andrii Nakryiko
2021-11-18 11:14 ` [PATCH bpf-next 0/3] Add bpf_for_each helper Toke Høiland-Jørgensen
2021-11-19 2:35 ` Joanne Koong
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=87wnl5en13.fsf@toke.dk \
--to=toke@redhat.com \
--cc=Kernel-team@fb.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=joannekoong@fb.com \
--cc=kafai@fb.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.