From: Eduard Zingerman <eddyz87@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
daniel@iogearbox.net, kernel-team@fb.com, yhs@fb.com
Subject: Re: [PATCH bpf-next 2/4] selftests/bpf: convenience macro for use with 'asm volatile' blocks
Date: Thu, 22 Dec 2022 02:12:05 +0200 [thread overview]
Message-ID: <0d59b7bd38e7ba3c7f7664441577d4b7cef3fcf4.camel@gmail.com> (raw)
In-Reply-To: <CAEf4BzbbyYJHCF_YVPJdYQF7Mh-RwPkdpNCJPHvxb3MXKH2S=Q@mail.gmail.com>
On Tue, 2022-12-20 at 13:05 -0800, Andrii Nakryiko wrote:
> On Fri, Dec 16, 2022 at 6:17 PM Eduard Zingerman <eddyz87@gmail.com> wrote:
> >
> > A set of macros useful for writing naked BPF functions using inline
> > assembly. E.g. as follows:
> >
> > struct map_struct {
> > ...
> > } map SEC(".maps");
> >
> > SEC(...)
> > __naked int foo_test(void)
> > {
> > asm volatile(
> > "r0 = 0;"
> > "*(u64*)(r10 - 8) = r0;"
> > "r1 = %[map] ll;"
> > "r2 = r10;"
> > "r2 += -8;"
> > "call %[bpf_map_lookup_elem];"
> > "r0 = 0;"
> > "exit;"
> > :
> > : __imm(bpf_map_lookup_elem),
> > __imm_addr(map)
> > : __clobber_all);
> > }
> >
> > Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
> > ---
> > tools/testing/selftests/bpf/progs/bpf_misc.h | 6 ++++++
> > 1 file changed, 6 insertions(+)
> >
> > diff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h
> > index a42363a3fef1..bbf56ad95636 100644
> > --- a/tools/testing/selftests/bpf/progs/bpf_misc.h
> > +++ b/tools/testing/selftests/bpf/progs/bpf_misc.h
> > @@ -8,6 +8,12 @@
> > #define __log_level(lvl) __attribute__((btf_decl_tag("comment:test_log_level="#lvl)))
> > #define __test_state_freq __attribute__((btf_decl_tag("comment:test_state_freq")))
> >
> > +/* Convenience macro for use with 'asm volatile' blocks */
> > +#define __naked __attribute__((naked))
> > +#define __clobber_all "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "memory"
>
> I found that this one doesn't work well when passing some inputs as
> registers (e.g., for address of a variable on stack). Compiler
> complains that it couldn't find any free registers to use. So I ended
> up using
>
> #define __asm_common_clobbers "r0", "r1", "r2", "r3", "r4", "r5", "memory"
>
> and adding "r6", "r7", etc manually, depending on the test.
>
> So maybe let's add it upfront as `__clobber_common` as well?
Will do.
>
> But changes look good to me:
>
> Acked-by: Andrii Nakryiko <andrii@kernel.org>
>
> > +#define __imm(name) [name]"i"(name)
> > +#define __imm_addr(name) [name]"i"(&name)
> > +
> > #if defined(__TARGET_ARCH_x86)
> > #define SYSCALL_WRAPPER 1
> > #define SYS_PREFIX "__x64_"
> > --
> > 2.38.2
> >
next prev parent reply other threads:[~2022-12-22 0:12 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-17 2:17 [PATCH bpf-next 0/4] reduce BPF_ID_MAP_SIZE to fit only valid programs Eduard Zingerman
2022-12-17 2:17 ` [PATCH bpf-next 1/4] selftests/bpf: support for BPF_F_TEST_STATE_FREQ in test_loader Eduard Zingerman
2022-12-17 18:44 ` Yonghong Song
2022-12-20 21:03 ` Andrii Nakryiko
2022-12-22 0:11 ` Eduard Zingerman
2022-12-22 19:07 ` Andrii Nakryiko
2022-12-17 2:17 ` [PATCH bpf-next 2/4] selftests/bpf: convenience macro for use with 'asm volatile' blocks Eduard Zingerman
2022-12-17 18:58 ` Yonghong Song
2022-12-20 21:05 ` Andrii Nakryiko
2022-12-22 0:12 ` Eduard Zingerman [this message]
2022-12-17 2:17 ` [PATCH bpf-next 3/4] bpf: reduce BPF_ID_MAP_SIZE to fit only valid programs Eduard Zingerman
2022-12-17 18:59 ` Yonghong Song
2022-12-20 21:06 ` Andrii Nakryiko
2022-12-17 2:17 ` [PATCH bpf-next 4/4] selftests/bpf: check if verifier.c:check_ids() handles 64+5 ids Eduard Zingerman
2022-12-17 19:17 ` Yonghong Song
2022-12-20 21:18 ` Andrii Nakryiko
2022-12-22 0:33 ` Eduard Zingerman
2022-12-20 21:21 ` [PATCH bpf-next 0/4] reduce BPF_ID_MAP_SIZE to fit only valid programs Andrii Nakryiko
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=0d59b7bd38e7ba3c7f7664441577d4b7cef3fcf4.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@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