Re: [PATCH bpf-next 2/7] bpf: JIT helpers for fmod_ret progs

From: KP Singh
Date: Tue Mar 03 2020 - 17:28:21 EST


On 03-Mär 14:26, Andrii Nakryiko wrote:
> On Tue, Mar 3, 2020 at 6:13 AM KP Singh <kpsingh@xxxxxxxxxxxx> wrote:
> >
> > From: KP Singh <kpsingh@xxxxxxxxxx>
> >
> > * Split the invoke_bpf program to prepare for special handling of
> > fmod_ret programs introduced in a subsequent patch.
> > * Move the definition of emit_cond_near_jump and emit_nops as they are
> > needed for fmod_ret.
> > * Refactor branch target alignment into its own function
> > align16_branch_target.
> >
> > Signed-off-by: KP Singh <kpsingh@xxxxxxxxxx>
> > ---
> > arch/x86/net/bpf_jit_comp.c | 158 ++++++++++++++++++++----------------
> > 1 file changed, 90 insertions(+), 68 deletions(-)
> >
> > diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> > index 15c7d28bc05c..475e354c2e88 100644
> > --- a/arch/x86/net/bpf_jit_comp.c
> > +++ b/arch/x86/net/bpf_jit_comp.c
> > @@ -1361,35 +1361,100 @@ static void restore_regs(const struct btf_func_model *m, u8 **prog, int nr_args,
> > -(stack_size - i * 8));
> > }
> >
>
> [...]
>
> > +
> > +/* From Intel 64 and IA-32 Architectures Optimization
> > + * Reference Manual, 3.4.1.4 Code Alignment, Assembly/Compiler
> > + * Coding Rule 11: All branch targets should be 16-byte
> > + * aligned.
> > + */
> > +static void align16_branch_target(u8 **pprog)
> > +{
> > + u8 *target, *prog = *pprog;
> > +
> > + target = PTR_ALIGN(prog, 16);
> > + if (target != prog)
> > + emit_nops(&prog, target - prog);
> > + if (target != prog)
> > + pr_err("calcultion error\n");
>
> this wasn't in the original code, do you feel like it's more important
> to check this and print error?
>
> also typo: calculation error, but then it's a bit brief and
> uninformative message. So I don't know, maybe just drop it?

Ah, good catch! this is deinitely not intended to be here.
It's a debug artifact and needs to dropped indeed.

- KP

>
> > +}
> > +
> > +static int emit_cond_near_jump(u8 **pprog, void *func, void *ip, u8 jmp_cond)
> > +{
> > + u8 *prog = *pprog;
> > + int cnt = 0;
> > + s64 offset;
> > +
> > + offset = func - (ip + 2 + 4);
> > + if (!is_simm32(offset)) {
> > + pr_err("Target %p is out of range\n", func);
> > + return -EINVAL;
> > + }
> > + EMIT2_off32(0x0F, jmp_cond + 0x10, offset);
> > + *pprog = prog;
> > + return 0;
> > +}
> > +
>
> [...]