From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Quentin Monnet <quentin@isovalent.com>
Cc: Andrii Nakryiko <andriin@fb.com>, bpf <bpf@vger.kernel.org>,
Networking <netdev@vger.kernel.org>,
Alexei Starovoitov <ast@fb.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH bpf-next 07/10] bpftool: expose attach_type-to-string array to non-cgroup code
Date: Fri, 24 Apr 2020 09:27:22 -0700 [thread overview]
Message-ID: <CAEf4BzY9tjQm1f8eTyjYjthTF9n6tZ59r1mpUsYWL4+bFuch2Q@mail.gmail.com> (raw)
In-Reply-To: <34110254-6384-153f-af39-d5f9f3a50acb@isovalent.com>
On Fri, Apr 24, 2020 at 3:32 AM Quentin Monnet <quentin@isovalent.com> wrote:
>
> 2020-04-23 22:35 UTC-0700 ~ Andrii Nakryiko <andriin@fb.com>
> > Move attach_type_strings into main.h for access in non-cgroup code.
> > bpf_attach_type is used for non-cgroup attach types quite widely now. So also
> > complete missing string translations for non-cgroup attach types.
> >
> > Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> > ---
> > tools/bpf/bpftool/cgroup.c | 28 +++-------------------------
> > tools/bpf/bpftool/main.h | 32 ++++++++++++++++++++++++++++++++
> > 2 files changed, 35 insertions(+), 25 deletions(-)
> >
> > diff --git a/tools/bpf/bpftool/cgroup.c b/tools/bpf/bpftool/cgroup.c
> > index 62c6a1d7cd18..d1fd9c9f2690 100644
> > --- a/tools/bpf/bpftool/cgroup.c
> > +++ b/tools/bpf/bpftool/cgroup.c
> > @@ -31,35 +31,13 @@
> >
> > static unsigned int query_flags;
> >
> > -static const char * const attach_type_strings[] = {
> > - [BPF_CGROUP_INET_INGRESS] = "ingress",
> > - [BPF_CGROUP_INET_EGRESS] = "egress",
> > - [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create",
> > - [BPF_CGROUP_SOCK_OPS] = "sock_ops",
> > - [BPF_CGROUP_DEVICE] = "device",
> > - [BPF_CGROUP_INET4_BIND] = "bind4",
> > - [BPF_CGROUP_INET6_BIND] = "bind6",
> > - [BPF_CGROUP_INET4_CONNECT] = "connect4",
> > - [BPF_CGROUP_INET6_CONNECT] = "connect6",
> > - [BPF_CGROUP_INET4_POST_BIND] = "post_bind4",
> > - [BPF_CGROUP_INET6_POST_BIND] = "post_bind6",
> > - [BPF_CGROUP_UDP4_SENDMSG] = "sendmsg4",
> > - [BPF_CGROUP_UDP6_SENDMSG] = "sendmsg6",
> > - [BPF_CGROUP_SYSCTL] = "sysctl",
> > - [BPF_CGROUP_UDP4_RECVMSG] = "recvmsg4",
> > - [BPF_CGROUP_UDP6_RECVMSG] = "recvmsg6",
> > - [BPF_CGROUP_GETSOCKOPT] = "getsockopt",
> > - [BPF_CGROUP_SETSOCKOPT] = "setsockopt",
> > - [__MAX_BPF_ATTACH_TYPE] = NULL,
>
> So you removed the "[__MAX_BPF_ATTACH_TYPE] = NULL" from the new array,
> if I understand correctly this is because all attach type enum members
> are now in the new attach_type_name[] so we're safe by looping until we
> reach __MAX_BPF_ATTACH_TYPE. Sounds good in theory but...
>
Well, NULL is default value, so having [__MAX_BPF_ATTACH_TYPE] = NULL
just increases ARRAY_SIZE(attach_type_names) by one. Which is
generally not needed, because we do proper < ARRAY_SIZE() checks
everywhere... except for one place. show_bpf_prog in cgroup.c looks up
name directly and can pass NULL into jsonw_string_field which will
crash.
I can fix that by setting [__MAX_BPF_ATTACH_TYPE] to "unknown" or
adding extra check in show_bpf_prog() code? Any preferences?
> > -};
> > -
> > static enum bpf_attach_type parse_attach_type(const char *str)
> > {
> > enum bpf_attach_type type;
> >
> > for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
> > - if (attach_type_strings[type] &&
> > - is_prefix(str, attach_type_strings[type]))
> > + if (attach_type_name[type] &&
> > + is_prefix(str, attach_type_name[type]))
> > return type;
> > }
>
> ... I'm concerned the "attach_type_name[type]" here could segfault if we
> add a new attach type to the kernel, but don't report it immediately to
> bpftool's array.
I don't think so. Here we'll iterate over all possible bpf_attach_type
(as far as our copy of UAPI header is concerned, of course). If some
of the values don't have entries in attach_type_name array, we'll get
back NULL (same as with explicit [__MAX_BPF_ATTACH_TYPE] = NULL, btw),
which will get handled properly in the loop. And caller will get back
__MAX_BPF_ATTACH_TYPE as bpf_attach_type value. So unless I'm still
missing something, it seems to be working exactly the same as before?
>
> Is there any drawback with keeping the "[__MAX_BPF_ATTACH_TYPE] = NULL"?
> Or change here to loop on ARRAY_SIZE(), as you do in your own patch for
> link?
ARRAY_SIZE() == __MAX_BPF_ATTACH_TYPE, isn't it? Previously ARRAY_SIZE
was (__MAX_BPF_ATTACH_TYPE + 1), but I don't think it's necessary?
The only difference is show_bpf_prog() which now is going to do out of
array reads, while previously it would get NULL. But both cases are
bad and needs fixing.
next prev parent reply other threads:[~2020-04-24 16:27 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-24 5:34 [PATCH bpf-next 00/10] bpf_link observability APIs Andrii Nakryiko
2020-04-24 5:34 ` [PATCH bpf-next 01/10] bpf: refactor bpf_link update handling Andrii Nakryiko
2020-04-24 5:34 ` [PATCH bpf-next 02/10] bpf: allocate ID for bpf_link Andrii Nakryiko
2020-04-24 5:34 ` [PATCH bpf-next 03/10] bpf: support GET_FD_BY_ID and GET_NEXT_ID " Andrii Nakryiko
2020-04-24 5:34 ` [PATCH bpf-next 04/10] bpf: add support for BPF_OBJ_GET_INFO_BY_FD " Andrii Nakryiko
2020-04-24 5:35 ` [PATCH bpf-next 05/10] libbpf: add low-level APIs for new bpf_link commands Andrii Nakryiko
2020-04-24 5:35 ` [PATCH bpf-next 06/10] selftests/bpf: test bpf_link's get_next_id, get_fd_by_id, and get_obj_info Andrii Nakryiko
2020-04-24 5:35 ` [PATCH bpf-next 07/10] bpftool: expose attach_type-to-string array to non-cgroup code Andrii Nakryiko
2020-04-24 10:32 ` Quentin Monnet
2020-04-24 16:27 ` Andrii Nakryiko [this message]
2020-04-24 17:08 ` Quentin Monnet
2020-04-25 0:12 ` Andrii Nakryiko
2020-04-25 10:19 ` Quentin Monnet
2020-04-24 5:35 ` [PATCH bpf-next 08/10] bpftool: add bpf_link show and pin support Andrii Nakryiko
2020-04-24 10:32 ` Quentin Monnet
2020-04-24 16:30 ` Andrii Nakryiko
2020-04-24 5:35 ` [PATCH bpf-next 09/10] bpftool: add bpftool-link manpage Andrii Nakryiko
2020-04-24 10:33 ` Quentin Monnet
2020-04-24 16:32 ` Andrii Nakryiko
2020-04-24 5:35 ` [PATCH bpf-next 10/10] bpftool: add link bash completions Andrii Nakryiko
2020-04-24 10:33 ` Quentin Monnet
2020-04-24 11:40 ` [PATCH bpf-next 00/10] bpf_link observability APIs Toke Høiland-Jørgensen
2020-04-24 15:54 ` Andrii Nakryiko
2020-04-24 16:39 ` Toke Høiland-Jørgensen
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=CAEf4BzY9tjQm1f8eTyjYjthTF9n6tZ59r1mpUsYWL4+bFuch2Q@mail.gmail.com \
--to=andrii.nakryiko@gmail.com \
--cc=andriin@fb.com \
--cc=ast@fb.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=netdev@vger.kernel.org \
--cc=quentin@isovalent.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;
as well as URLs for NNTP newsgroup(s).