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

From: Alexei Starovoitov
Date: Tue Mar 03 2020 - 18:56:11 EST


On Tue, Mar 03, 2020 at 11:28:12PM +0100, KP Singh wrote:
> > > +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.

That spurious pr_err() caught my attention as well.
After further analysis there is a bug here.
The function is missing last line:
*pprog = prog;
Without it the nop insertion is actually not happenning.
Nops are being written, but next insns will overwrite them.
When I noticed it by code review I applied the patches to my tree
and run the tests and, as expected, all tests passed.
The existing test_xdp_veth.sh emits the most amount of unaligned
branches. Since then I've been thinking whether we could add a test
to catch things like this and couldn't come up with a way to test it
without burning a lot of code on it. So let's fix it and move on.
Could you rename this helper? May be emit_align() and pass 16 into it?
The code is not branch target specific. It's aligning the start
of the next instruction.
Also could you add a comment to:
align16_branch_target(&prog);
for (i = 0; i < fmod_ret->nr_progs; i++)
emit_cond_near_jump(&branches[i], prog, branches[i],
X86_JNE);
kfree(branches);
to say that the loop is updating prior location to jump to aligned
branch target ?