From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Alan Maguire <alan.maguire@oracle.com>
Cc: ast@kernel.org, daniel@iogearbox.net, andriin@fb.com, yhs@fb.com,
linux@rasmusvillemoes.dk, andriy.shevchenko@linux.intel.com,
pmladek@suse.com, kafai@fb.com, songliubraving@fb.com,
john.fastabend@gmail.com, kpsingh@chromium.org, shuah@kernel.org,
rdna@fb.com, scott.branden@broadcom.com, quentin@isovalent.com,
cneirabustos@gmail.com, jakub@cloudflare.com, mingo@redhat.com,
rostedt@goodmis.org, bpf@vger.kernel.org, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
acme@kernel.org
Subject: Re: [PATCH v6 bpf-next 3/6] bpf: add bpf_snprintf_btf helper
Date: Thu, 24 Sep 2020 17:42:29 -0700 [thread overview]
Message-ID: <20200925004229.memxz26rt4jkzd4m@ast-mbp.dhcp.thefacebook.com> (raw)
In-Reply-To: <1600883188-4831-4-git-send-email-alan.maguire@oracle.com>
On Wed, Sep 23, 2020 at 06:46:25PM +0100, Alan Maguire wrote:
> +
> +static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
> + u64 flags, const struct btf **btf,
> + s32 *btf_id)
> +{
> + u8 btf_kind = BTF_KIND_TYPEDEF;
> + char type_name[KSYM_NAME_LEN];
> + const struct btf_type *t;
> + const char *btf_type;
> + int ret;
> +
> + if (unlikely(flags & ~(BTF_F_ALL)))
> + return -EINVAL;
> +
> + if (btf_ptr_size != sizeof(struct btf_ptr))
> + return -EINVAL;
> +
> + *btf = bpf_get_btf_vmlinux();
> +
> + if (IS_ERR_OR_NULL(*btf))
> + return PTR_ERR(*btf);
> +
> + if (ptr->type != NULL) {
> + ret = copy_from_kernel_nofault(type_name, ptr->type,
> + sizeof(type_name));
nofault copy from bpf program global data... hmm...
I guess that works, but...
> + if (ret)
> + return ret;
> +
> + btf_type = type_name;
> +
> + if (strncmp(btf_type, "struct ", strlen("struct ")) == 0) {
> + btf_kind = BTF_KIND_STRUCT;
> + btf_type += strlen("struct ");
> + } else if (strncmp(btf_type, "union ", strlen("union ")) == 0) {
> + btf_kind = BTF_KIND_UNION;
> + btf_type += strlen("union ");
> + } else if (strncmp(btf_type, "enum ", strlen("enum ")) == 0) {
> + btf_kind = BTF_KIND_ENUM;
> + btf_type += strlen("enum ");
> + }
> +
> + if (strlen(btf_type) == 0)
> + return -EINVAL;
> +
> + /* Assume type specified is a typedef as there's not much
> + * benefit in specifying int types other than wasting time
> + * on BTF lookups; we optimize for the most useful path.
> + *
> + * Fall back to BTF_KIND_INT if this fails.
> + */
> + *btf_id = btf_find_by_name_kind(*btf, btf_type, btf_kind);
> + if (*btf_id < 0)
> + *btf_id = btf_find_by_name_kind(*btf, btf_type,
> + BTF_KIND_INT);
with all that fragility...
> + } else if (ptr->type_id > 0)
> + *btf_id = ptr->type_id;
since __builtin_btf_type_id() landed in llvm in February and it works
may be support type_id only?
Manually specifying type name as a string is error prone.
Plus that copy_from_kernel... which is doing copy from bpf prog.
I slept on it, but still feels unclean.
May be do type_id only for now and if we really really need string types
we can add it later after initial patches land?
next prev parent reply other threads:[~2020-09-25 0:42 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-23 17:46 [PATCH v6 bpf-next 0/6] bpf: add helpers to support BTF-based kernel data display Alan Maguire
2020-09-23 17:46 ` [PATCH v6 bpf-next 1/6] bpf: provide function to get vmlinux BTF information Alan Maguire
2020-09-23 17:46 ` [PATCH v6 bpf-next 2/6] bpf: move to generic BTF show support, apply it to seq files/strings Alan Maguire
2020-09-25 0:33 ` Alexei Starovoitov
2020-09-23 17:46 ` [PATCH v6 bpf-next 3/6] bpf: add bpf_snprintf_btf helper Alan Maguire
2020-09-25 0:42 ` Alexei Starovoitov [this message]
2020-09-23 17:46 ` [PATCH v6 bpf-next 4/6] selftests/bpf: add bpf_snprintf_btf helper tests Alan Maguire
2020-09-25 0:50 ` Alexei Starovoitov
2020-09-25 17:36 ` Andrii Nakryiko
2020-09-23 17:46 ` [PATCH v6 bpf-next 5/6] bpf: add bpf_seq_printf_btf helper Alan Maguire
2020-09-23 17:46 ` [PATCH v6 bpf-next 6/6] selftests/bpf: add test for " Alan Maguire
2020-09-25 1:26 ` Alexei Starovoitov
2020-09-28 14:12 ` Alan Maguire
2020-09-28 17:51 ` Andrii Nakryiko
2020-09-29 1:54 ` Alexei Starovoitov
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=20200925004229.memxz26rt4jkzd4m@ast-mbp.dhcp.thefacebook.com \
--to=alexei.starovoitov@gmail.com \
--cc=acme@kernel.org \
--cc=alan.maguire@oracle.com \
--cc=andriin@fb.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=cneirabustos@gmail.com \
--cc=daniel@iogearbox.net \
--cc=jakub@cloudflare.com \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mingo@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=pmladek@suse.com \
--cc=quentin@isovalent.com \
--cc=rdna@fb.com \
--cc=rostedt@goodmis.org \
--cc=scott.branden@broadcom.com \
--cc=shuah@kernel.org \
--cc=songliubraving@fb.com \
--cc=yhs@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 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).