diff mbox series

[bpf-next,v2,05/10] libbpf: Add PT_REGS_SYSCALL macro

Message ID 20220204041955.1958263-6-iii@linux.ibm.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series libbpf: Fix accessing syscall arguments | expand

Checks

Context Check Description
bpf/vmtest-bpf-next success VM_Test
bpf/vmtest-bpf-next-PR success PR summary
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 7 maintainers not CCed: andrii@kernel.org kpsingh@kernel.org john.fastabend@gmail.com kafai@fb.com songliubraving@fb.com yhs@fb.com netdev@vger.kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 15 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Ilya Leoshkevich Feb. 4, 2022, 4:19 a.m. UTC
Some architectures pass a pointer to struct pt_regs to syscall
handlers, others unpack it into individual function parameters.
Introduce a macro to describe what a particular arch does, using
`passing pt_regs *` as a default.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
 tools/lib/bpf/bpf_tracing.h | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Andrii Nakryiko Feb. 4, 2022, 5:22 a.m. UTC | #1
On Thu, Feb 3, 2022 at 8:20 PM Ilya Leoshkevich <iii@linux.ibm.com> wrote:
>
> Some architectures pass a pointer to struct pt_regs to syscall
> handlers, others unpack it into individual function parameters.
> Introduce a macro to describe what a particular arch does, using
> `passing pt_regs *` as a default.
>
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
>  tools/lib/bpf/bpf_tracing.h | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
> index 30f0964f8c9e..400a4f002f77 100644
> --- a/tools/lib/bpf/bpf_tracing.h
> +++ b/tools/lib/bpf/bpf_tracing.h
> @@ -334,6 +334,15 @@ struct pt_regs;
>
>  #endif /* defined(bpf_target_defined) */
>
> +/*
> + * When invoked from a syscall handler kprobe, returns a pointer to a
> + * struct pt_regs containing syscall arguments and suitable for passing to
> + * PT_REGS_PARMn_SYSCALL() and PT_REGS_PARMn_CORE_SYSCALL().
> + */
> +#ifndef PT_REGS_SYSCALL
> +#define PT_REGS_SYSCALL(ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx))
> +#endif

maybe PT_REGS_SYSCALL_REGS? It returns regs, not the "syscall".
PT_REGS prefix is for consistency with all other pt_regs macros, but
"SYSCALL_REGS" is specifying what is actually returned by the macro

> +
>  #ifndef ___bpf_concat
>  #define ___bpf_concat(a, b) a ## b
>  #endif
> --
> 2.34.1
>
Andrii Nakryiko Feb. 4, 2022, 5:23 a.m. UTC | #2
On Thu, Feb 3, 2022 at 9:22 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, Feb 3, 2022 at 8:20 PM Ilya Leoshkevich <iii@linux.ibm.com> wrote:
> >
> > Some architectures pass a pointer to struct pt_regs to syscall
> > handlers, others unpack it into individual function parameters.
> > Introduce a macro to describe what a particular arch does, using
> > `passing pt_regs *` as a default.
> >
> > Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> > ---
> >  tools/lib/bpf/bpf_tracing.h | 9 +++++++++
> >  1 file changed, 9 insertions(+)
> >
> > diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
> > index 30f0964f8c9e..400a4f002f77 100644
> > --- a/tools/lib/bpf/bpf_tracing.h
> > +++ b/tools/lib/bpf/bpf_tracing.h
> > @@ -334,6 +334,15 @@ struct pt_regs;
> >
> >  #endif /* defined(bpf_target_defined) */
> >
> > +/*
> > + * When invoked from a syscall handler kprobe, returns a pointer to a
> > + * struct pt_regs containing syscall arguments and suitable for passing to
> > + * PT_REGS_PARMn_SYSCALL() and PT_REGS_PARMn_CORE_SYSCALL().
> > + */
> > +#ifndef PT_REGS_SYSCALL
> > +#define PT_REGS_SYSCALL(ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx))
> > +#endif
>
> maybe PT_REGS_SYSCALL_REGS? It returns regs, not the "syscall".
> PT_REGS prefix is for consistency with all other pt_regs macros, but
> "SYSCALL_REGS" is specifying what is actually returned by the macro
>

Oh, and instead of casting to `struct pt_regs *` directly, maybe use
__PT_REGS_CAST() instead? For some architectures it probably should
stay user_pt_regs (or whatever it is there).

> > +
> >  #ifndef ___bpf_concat
> >  #define ___bpf_concat(a, b) a ## b
> >  #endif
> > --
> > 2.34.1
> >
Ilya Leoshkevich Feb. 4, 2022, 12:29 p.m. UTC | #3
On Thu, 2022-02-03 at 21:23 -0800, Andrii Nakryiko wrote:
> On Thu, Feb 3, 2022 at 9:22 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> > 
> > On Thu, Feb 3, 2022 at 8:20 PM Ilya Leoshkevich <iii@linux.ibm.com>
> > wrote:
> > > 
> > > Some architectures pass a pointer to struct pt_regs to syscall
> > > handlers, others unpack it into individual function parameters.
> > > Introduce a macro to describe what a particular arch does, using
> > > `passing pt_regs *` as a default.
> > > 
> > > Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> > > ---
> > >  tools/lib/bpf/bpf_tracing.h | 9 +++++++++
> > >  1 file changed, 9 insertions(+)
> > > 
> > > diff --git a/tools/lib/bpf/bpf_tracing.h
> > > b/tools/lib/bpf/bpf_tracing.h
> > > index 30f0964f8c9e..400a4f002f77 100644
> > > --- a/tools/lib/bpf/bpf_tracing.h
> > > +++ b/tools/lib/bpf/bpf_tracing.h
> > > @@ -334,6 +334,15 @@ struct pt_regs;
> > > 
> > >  #endif /* defined(bpf_target_defined) */
> > > 
> > > +/*
> > > + * When invoked from a syscall handler kprobe, returns a pointer
> > > to a
> > > + * struct pt_regs containing syscall arguments and suitable for
> > > passing to
> > > + * PT_REGS_PARMn_SYSCALL() and PT_REGS_PARMn_CORE_SYSCALL().
> > > + */
> > > +#ifndef PT_REGS_SYSCALL
> > > +#define PT_REGS_SYSCALL(ctx) ((struct pt_regs
> > > *)PT_REGS_PARM1(ctx))
> > > +#endif
> > 
> > maybe PT_REGS_SYSCALL_REGS? It returns regs, not the "syscall".
> > PT_REGS prefix is for consistency with all other pt_regs macros,
> > but
> > "SYSCALL_REGS" is specifying what is actually returned by the macro
> > 
> 
> Oh, and instead of casting to `struct pt_regs *` directly, maybe use
> __PT_REGS_CAST() instead? For some architectures it probably should
> stay user_pt_regs (or whatever it is there).
> 
> > > +
> > >  #ifndef ___bpf_concat
> > >  #define ___bpf_concat(a, b) a ## b
> > >  #endif
> > > --
> > > 2.34.1
> > > 

I think it's better to keep this as struct pt_regs *, so that in
bpf progs we can do

	struct pt_regs *real_regs = PT_REGS_SYSCALL(ctx);

without having to worry about which arch we are on, or using the
opaque void *.
Andrii Nakryiko Feb. 4, 2022, 6:09 p.m. UTC | #4
On Fri, Feb 4, 2022 at 4:30 AM Ilya Leoshkevich <iii@linux.ibm.com> wrote:
>
> On Thu, 2022-02-03 at 21:23 -0800, Andrii Nakryiko wrote:
> > On Thu, Feb 3, 2022 at 9:22 PM Andrii Nakryiko
> > <andrii.nakryiko@gmail.com> wrote:
> > >
> > > On Thu, Feb 3, 2022 at 8:20 PM Ilya Leoshkevich <iii@linux.ibm.com>
> > > wrote:
> > > >
> > > > Some architectures pass a pointer to struct pt_regs to syscall
> > > > handlers, others unpack it into individual function parameters.
> > > > Introduce a macro to describe what a particular arch does, using
> > > > `passing pt_regs *` as a default.
> > > >
> > > > Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> > > > ---
> > > >  tools/lib/bpf/bpf_tracing.h | 9 +++++++++
> > > >  1 file changed, 9 insertions(+)
> > > >
> > > > diff --git a/tools/lib/bpf/bpf_tracing.h
> > > > b/tools/lib/bpf/bpf_tracing.h
> > > > index 30f0964f8c9e..400a4f002f77 100644
> > > > --- a/tools/lib/bpf/bpf_tracing.h
> > > > +++ b/tools/lib/bpf/bpf_tracing.h
> > > > @@ -334,6 +334,15 @@ struct pt_regs;
> > > >
> > > >  #endif /* defined(bpf_target_defined) */
> > > >
> > > > +/*
> > > > + * When invoked from a syscall handler kprobe, returns a pointer
> > > > to a
> > > > + * struct pt_regs containing syscall arguments and suitable for
> > > > passing to
> > > > + * PT_REGS_PARMn_SYSCALL() and PT_REGS_PARMn_CORE_SYSCALL().
> > > > + */
> > > > +#ifndef PT_REGS_SYSCALL
> > > > +#define PT_REGS_SYSCALL(ctx) ((struct pt_regs
> > > > *)PT_REGS_PARM1(ctx))
> > > > +#endif
> > >
> > > maybe PT_REGS_SYSCALL_REGS? It returns regs, not the "syscall".
> > > PT_REGS prefix is for consistency with all other pt_regs macros,
> > > but
> > > "SYSCALL_REGS" is specifying what is actually returned by the macro
> > >
> >
> > Oh, and instead of casting to `struct pt_regs *` directly, maybe use
> > __PT_REGS_CAST() instead? For some architectures it probably should
> > stay user_pt_regs (or whatever it is there).
> >
> > > > +
> > > >  #ifndef ___bpf_concat
> > > >  #define ___bpf_concat(a, b) a ## b
> > > >  #endif
> > > > --
> > > > 2.34.1
> > > >
>
> I think it's better to keep this as struct pt_regs *, so that in
> bpf progs we can do
>
>         struct pt_regs *real_regs = PT_REGS_SYSCALL(ctx);
>
> without having to worry about which arch we are on, or using the
> opaque void *.

Makes sense, sounds good to me.
diff mbox series

Patch

diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
index 30f0964f8c9e..400a4f002f77 100644
--- a/tools/lib/bpf/bpf_tracing.h
+++ b/tools/lib/bpf/bpf_tracing.h
@@ -334,6 +334,15 @@  struct pt_regs;
 
 #endif /* defined(bpf_target_defined) */
 
+/*
+ * When invoked from a syscall handler kprobe, returns a pointer to a
+ * struct pt_regs containing syscall arguments and suitable for passing to
+ * PT_REGS_PARMn_SYSCALL() and PT_REGS_PARMn_CORE_SYSCALL().
+ */
+#ifndef PT_REGS_SYSCALL
+#define PT_REGS_SYSCALL(ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx))
+#endif
+
 #ifndef ___bpf_concat
 #define ___bpf_concat(a, b) a ## b
 #endif