BPF List
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, "Alexei Starovoitov" <ast@kernel.org>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Jesper Dangaard Brouer" <brouer@redhat.com>
Subject: Re: [PATCH bpf-next v3 09/13] bpf: Wire up freeing of referenced kptr
Date: Fri, 25 Mar 2022 20:37:51 +0530	[thread overview]
Message-ID: <20220325150751.6uybnqtdpf3umcm7@apollo> (raw)
In-Reply-To: <CAADnVQJd8E6T1GRMVS+XZxKDdnMjo-WQ-CdM7+x18VPj9ufpFQ@mail.gmail.com>

On Wed, Mar 23, 2022 at 02:40:17AM IST, Alexei Starovoitov wrote:
> On Sun, Mar 20, 2022 at 8:55 AM Kumar Kartikeya Dwivedi
> <memxor@gmail.com> wrote:
> >
> > +               /* Find and stash the function pointer for the destruction function that
> > +                * needs to be eventually invoked from the map free path.
> > +                */
> > +               if (info_arr[i].flags & BPF_MAP_VALUE_OFF_F_REF) {
> > +                       const struct btf_type *dtor_func, *dtor_func_proto;
> > +                       const struct btf_param *args;
> > +                       const char *dtor_func_name;
> > +                       unsigned long addr;
> > +                       s32 dtor_btf_id;
> > +                       u32 nr_args;
> > +
> > +                       /* This call also serves as a whitelist of allowed objects that
> > +                        * can be used as a referenced pointer and be stored in a map at
> > +                        * the same time.
> > +                        */
> > +                       dtor_btf_id = btf_find_dtor_kfunc(off_btf, id);
> > +                       if (dtor_btf_id < 0) {
> > +                               ret = dtor_btf_id;
> > +                               btf_put(off_btf);
> > +                               goto end;
> > +                       }
> > +
> > +                       dtor_func = btf_type_by_id(off_btf, dtor_btf_id);
> > +                       if (!dtor_func || !btf_type_is_func(dtor_func)) {
> > +                               ret = -EINVAL;
> > +                               btf_put(off_btf);
> > +                               goto end;
> > +                       }
> > +
> > +                       dtor_func_proto = btf_type_by_id(off_btf, dtor_func->type);
> > +                       if (!dtor_func_proto || !btf_type_is_func_proto(dtor_func_proto)) {
> > +                               ret = -EINVAL;
> > +                               btf_put(off_btf);
> > +                               goto end;
> > +                       }
> > +
> > +                       /* Make sure the prototype of the destructor kfunc is 'void func(type *)' */
> > +                       t = btf_type_by_id(off_btf, dtor_func_proto->type);
> > +                       if (!t || !btf_type_is_void(t)) {
> > +                               ret = -EINVAL;
> > +                               btf_put(off_btf);
> > +                               goto end;
> > +                       }
> > +
> > +                       nr_args = btf_type_vlen(dtor_func_proto);
> > +                       args = btf_params(dtor_func_proto);
> > +
> > +                       t = NULL;
> > +                       if (nr_args)
> > +                               t = btf_type_by_id(off_btf, args[0].type);
> > +                       /* Allow any pointer type, as width on targets Linux supports
> > +                        * will be same for all pointer types (i.e. sizeof(void *))
> > +                        */
> > +                       if (nr_args != 1 || !t || !btf_type_is_ptr(t)) {
> > +                               ret = -EINVAL;
> > +                               btf_put(off_btf);
> > +                               goto end;
> > +                       }
> > +
> > +                       if (btf_is_module(btf)) {
> > +                               mod = btf_try_get_module(off_btf);
> > +                               if (!mod) {
> > +                                       ret = -ENXIO;
> > +                                       btf_put(off_btf);
> > +                                       goto end;
> > +                               }
> > +                       }
> > +
> > +                       dtor_func_name = __btf_name_by_offset(off_btf, dtor_func->name_off);
> > +                       addr = kallsyms_lookup_name(dtor_func_name);
> > +                       if (!addr) {
> > +                               ret = -EINVAL;
> > +                               module_put(mod);
> > +                               btf_put(off_btf);
> > +                               goto end;
> > +                       }
> > +                       tab->off[i].dtor = (void *)addr;
>
> Most of the above should probably be in register_btf_id_dtor_kfuncs().
> It's best to fail early.
> Here we'll just remember dtor function pointer to speed up release.

Ok, will move all of these checks to register_btf_id_dtor_kfuncs.

--
Kartikeya

  reply	other threads:[~2022-03-25 15:09 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-20 15:54 [PATCH bpf-next v3 00/13] Introduce typed pointer support in BPF maps Kumar Kartikeya Dwivedi
2022-03-20 15:54 ` [PATCH bpf-next v3 01/13] bpf: Make btf_find_field more generic Kumar Kartikeya Dwivedi
2022-03-20 15:54 ` [PATCH bpf-next v3 02/13] bpf: Move check_ptr_off_reg before check_map_access Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 03/13] bpf: Allow storing unreferenced kptr in map Kumar Kartikeya Dwivedi
2022-03-21 23:39   ` Joanne Koong
2022-03-22  7:04     ` Kumar Kartikeya Dwivedi
2022-03-22 20:22       ` Andrii Nakryiko
2022-03-25 14:51         ` Kumar Kartikeya Dwivedi
2022-03-22  5:45   ` Andrii Nakryiko
2022-03-22  7:16     ` Kumar Kartikeya Dwivedi
2022-03-22  7:43       ` Kumar Kartikeya Dwivedi
2022-03-22 18:52       ` Andrii Nakryiko
2022-03-25 14:42         ` Kumar Kartikeya Dwivedi
2022-03-25 22:59           ` Andrii Nakryiko
2022-03-22 18:06   ` Martin KaFai Lau
2022-03-25 14:45     ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 04/13] bpf: Indicate argument that will be released in bpf_func_proto Kumar Kartikeya Dwivedi
2022-03-22  1:47   ` Joanne Koong
2022-03-22  7:34     ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 05/13] bpf: Allow storing referenced kptr in map Kumar Kartikeya Dwivedi
2022-03-22 20:59   ` Martin KaFai Lau
2022-03-25 14:57     ` Kumar Kartikeya Dwivedi
2022-03-25 23:39       ` Martin KaFai Lau
2022-03-26  1:01         ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 06/13] bpf: Prevent escaping of kptr loaded from maps Kumar Kartikeya Dwivedi
2022-03-22  5:58   ` Andrii Nakryiko
2022-03-22  7:18     ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 07/13] bpf: Adapt copy_map_value for multiple offset case Kumar Kartikeya Dwivedi
2022-03-22 20:38   ` Andrii Nakryiko
2022-03-25 15:06     ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 08/13] bpf: Populate pairs of btf_id and destructor kfunc in btf Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 09/13] bpf: Wire up freeing of referenced kptr Kumar Kartikeya Dwivedi
2022-03-22 20:51   ` Andrii Nakryiko
2022-03-25 14:50     ` Kumar Kartikeya Dwivedi
2022-03-22 21:10   ` Alexei Starovoitov
2022-03-25 15:07     ` Kumar Kartikeya Dwivedi [this message]
2022-03-20 15:55 ` [PATCH bpf-next v3 10/13] bpf: Teach verifier about kptr_get kfunc helpers Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 11/13] libbpf: Add kptr type tag macros to bpf_helpers.h Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 12/13] selftests/bpf: Add C tests for kptr Kumar Kartikeya Dwivedi
2022-03-22 21:00   ` Andrii Nakryiko
2022-03-25 14:52     ` Kumar Kartikeya Dwivedi
2022-03-24  9:10   ` Jiri Olsa
2022-03-25 14:52     ` Kumar Kartikeya Dwivedi
2022-03-20 15:55 ` [PATCH bpf-next v3 13/13] selftests/bpf: Add verifier " Kumar Kartikeya Dwivedi

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=20220325150751.6uybnqtdpf3umcm7@apollo \
    --to=memxor@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=toke@redhat.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