From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Yonghong Song <yhs@fb.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
"davem@davemloft.net" <davem@davemloft.net>,
"daniel@iogearbox.net" <daniel@iogearbox.net>,
"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
"bpf@vger.kernel.org" <bpf@vger.kernel.org>,
Kernel Team <Kernel-team@fb.com>
Subject: Re: [PATCH bpf-next 1/6] libbpf: Sanitize BTF_KIND_FUNC linkage
Date: Wed, 8 Jan 2020 12:12:57 -0800 [thread overview]
Message-ID: <20200108201256.2wtawgl3e4d4dkka@ast-mbp> (raw)
In-Reply-To: <d2fab68b-cd03-7a15-e353-c614f6079b89@fb.com>
On Wed, Jan 08, 2020 at 06:57:18PM +0000, Yonghong Song wrote:
>
>
> On 1/7/20 11:25 PM, Alexei Starovoitov wrote:
> > In case kernel doesn't support static/global/extern liknage of BTF_KIND_FUNC
> > sanitize BTF produced by llvm.
> >
> > Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> > ---
> > tools/include/uapi/linux/btf.h | 6 ++++++
> > tools/lib/bpf/libbpf.c | 35 +++++++++++++++++++++++++++++++++-
> > 2 files changed, 40 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/include/uapi/linux/btf.h b/tools/include/uapi/linux/btf.h
> > index 1a2898c482ee..5a667107ad2c 100644
> > --- a/tools/include/uapi/linux/btf.h
> > +++ b/tools/include/uapi/linux/btf.h
> > @@ -146,6 +146,12 @@ enum {
> > BTF_VAR_GLOBAL_EXTERN = 2,
> > };
> >
> > +enum btf_func_linkage {
> > + BTF_FUNC_STATIC = 0,
> > + BTF_FUNC_GLOBAL = 1,
> > + BTF_FUNC_EXTERN = 2,
> > +};
> > +
> > /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe
> > * additional information related to the variable such as its linkage.
> > */
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index 7513165b104f..f72b3ed6c34b 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -166,6 +166,8 @@ struct bpf_capabilities {
> > __u32 btf_datasec:1;
> > /* BPF_F_MMAPABLE is supported for arrays */
> > __u32 array_mmap:1;
> > + /* static/global/extern is supported for BTF_KIND_FUNC */
> > + __u32 btf_func_linkage:1;
> > };
> >
> > enum reloc_type {
> > @@ -1817,13 +1819,14 @@ static bool section_have_execinstr(struct bpf_object *obj, int idx)
> >
> > static void bpf_object__sanitize_btf(struct bpf_object *obj)
> > {
> > + bool has_func_linkage = obj->caps.btf_func_linkage;
> > bool has_datasec = obj->caps.btf_datasec;
> > bool has_func = obj->caps.btf_func;
> > struct btf *btf = obj->btf;
> > struct btf_type *t;
> > int i, j, vlen;
> >
> > - if (!obj->btf || (has_func && has_datasec))
> > + if (!obj->btf || (has_func && has_datasec && has_func_linkage))
> > return;
> >
> > for (i = 1; i <= btf__get_nr_types(btf); i++) {
> > @@ -1871,6 +1874,9 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj)
> > } else if (!has_func && btf_is_func(t)) {
> > /* replace FUNC with TYPEDEF */
> > t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
> > + } else if (!has_func_linkage && btf_is_func(t)) {
> > + /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
> > + t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);
>
> The comment says we only sanitize BTF_FUNC_GLOBAL here.
> Actually, it also sanitize BTF_FUNC_EXTERN.
>
> Currently, in kernel/bpf/btf.c, we have
> static int btf_check_all_types(struct btf_verifier_env *env)
> {
> ...
> if (btf_type_is_func(t)) {
> err = btf_func_check(env, t);
> if (err)
> return err;
> }
> ...
> }
>
> btf_func_check() will ensure func btf_type->type is a func_proto
> and all arguments of func_proto has a name except void which is
> considered as varg.
>
> For extern function, the argument name is lost in llvm/clang.
>
> -bash-4.4$ cat test.c
>
> extern int foo(int a);
> int test() { return foo(5); }
> -bash-4.4$
> -bash-4.4$ clang -target bpf -O2 -g -S -emit-llvm test.c
>
> !2 = !{}
> !4 = !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !5,
> flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2)
> !5 = !DISubroutineType(types: !6)
> !6 = !{!7, !7}
> !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
>
> To avoid kernel complaints, we need to sanitize in a different way.
> For example extern BTF_KIND_FUNC could be rewritten to a
> BTF_KIND_PTR to void.
Good point. I'll reword the comment and rename the test to btf_func_global,
so it probes kernel for KIND_GLOBAL only and santizes only that bit.
KIND_EXTERN sanitization is to be done later. Separate libbpf and kernel patches.
next prev parent reply other threads:[~2020-01-08 20:13 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-01-08 7:25 [PATCH bpf-next 0/6] bpf: Introduce global functions Alexei Starovoitov
2020-01-08 7:25 ` [PATCH bpf-next 1/6] libbpf: Sanitize BTF_KIND_FUNC linkage Alexei Starovoitov
2020-01-08 17:35 ` Song Liu
2020-01-08 18:57 ` Yonghong Song
2020-01-08 20:12 ` Alexei Starovoitov [this message]
2020-01-08 7:25 ` [PATCH bpf-next 2/6] libbpf: Collect static vs global info about functions Alexei Starovoitov
2020-01-08 10:25 ` Toke Høiland-Jørgensen
2020-01-08 16:25 ` Yonghong Song
2020-01-09 8:50 ` Toke Høiland-Jørgensen
2020-01-08 17:57 ` Song Liu
2020-01-08 20:10 ` Alexei Starovoitov
2020-01-08 7:25 ` [PATCH bpf-next 3/6] bpf: Introduce function-by-function verification Alexei Starovoitov
2020-01-08 10:28 ` Toke Høiland-Jørgensen
2020-01-08 20:06 ` Alexei Starovoitov
2020-01-09 8:57 ` Toke Høiland-Jørgensen
2020-01-09 23:03 ` Alexei Starovoitov
2020-01-10 10:08 ` Toke Høiland-Jørgensen
2020-01-08 19:10 ` Song Liu
2020-01-08 20:20 ` Alexei Starovoitov
2020-01-08 21:24 ` Song Liu
2020-01-08 23:05 ` Alexei Starovoitov
2020-01-14 23:39 ` Stanislav Fomichev
2020-01-14 23:56 ` Andrii Nakryiko
2020-01-15 0:44 ` Stanislav Fomichev
2020-01-08 7:25 ` [PATCH bpf-next 4/6] selftests/bpf: Add fexit-to-skb test for global funcs Alexei Starovoitov
2020-01-08 19:15 ` Song Liu
2020-01-08 7:25 ` [PATCH bpf-next 5/6] selftests/bpf: Add a test for a large global function Alexei Starovoitov
2020-01-08 19:16 ` Song Liu
2020-01-08 19:17 ` Song Liu
2020-01-08 7:25 ` [PATCH bpf-next 6/6] selftests/bpf: Modify a test to check global functions Alexei Starovoitov
2020-01-08 19:18 ` Song Liu
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=20200108201256.2wtawgl3e4d4dkka@ast-mbp \
--to=alexei.starovoitov@gmail.com \
--cc=Kernel-team@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=netdev@vger.kernel.org \
--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 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.