Re: [PATCH bpf] bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES

From: Toke Høiland-Jørgensen
Date: Thu Feb 09 2023 - 15:06:07 EST


Alexander Lobakin <alexandr.lobakin@xxxxxxxxx> writes:

> &xdp_buff and &xdp_frame are bound in a way that
>
> xdp_buff->data_hard_start == xdp_frame
>
> It's always the case and e.g. xdp_convert_buff_to_frame() relies on
> this.
> IOW, the following:
>
> for (u32 i = 0; i < 0xdead; i++) {
> xdpf = xdp_convert_buff_to_frame(&xdp);
> xdp_convert_frame_to_buff(xdpf, &xdp);
> }
>
> shouldn't ever modify @xdpf's contents or the pointer itself.
> However, "live packet" code wrongly treats &xdp_frame as part of its
> context placed *before* the data_hard_start. With such flow,
> data_hard_start is sizeof(*xdpf) off to the right and no longer points
> to the XDP frame.

Oh, nice find!

> Instead of replacing `sizeof(ctx)` with `offsetof(ctx, xdpf)` in several
> places and praying that there are no more miscalcs left somewhere in the
> code, unionize ::frm with ::data in a flex array, so that both starts
> pointing to the actual data_hard_start and the XDP frame actually starts
> being a part of it, i.e. a part of the headroom, not the context.
> A nice side effect is that the maximum frame size for this mode gets
> increased by 40 bytes, as xdp_buff::frame_sz includes everything from
> data_hard_start (-> includes xdpf already) to the end of XDP/skb shared
> info.

I like the union approach, however...

> (was found while testing XDP traffic generator on ice, which calls
> xdp_convert_frame_to_buff() for each XDP frame)
>
> Fixes: b530e9e1063e ("bpf: Add "live packet" mode for XDP in BPF_PROG_RUN")
> Signed-off-by: Alexander Lobakin <alexandr.lobakin@xxxxxxxxx>
> ---
> net/bpf/test_run.c | 13 ++++++++-----
> 1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
> index 2723623429ac..c3cce7a8d47d 100644
> --- a/net/bpf/test_run.c
> +++ b/net/bpf/test_run.c
> @@ -97,8 +97,11 @@ static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,
> struct xdp_page_head {
> struct xdp_buff orig_ctx;
> struct xdp_buff ctx;
> - struct xdp_frame frm;
> - u8 data[];
> + union {
> + /* ::data_hard_start starts here */
> + DECLARE_FLEX_ARRAY(struct xdp_frame, frm);
> + DECLARE_FLEX_ARRAY(u8, data);
> + };

...why does the xdp_frame need to be a flex array? Shouldn't this just be:

+ union {
+ /* ::data_hard_start starts here */
+ struct xdp_frame frm;
+ DECLARE_FLEX_ARRAY(u8, data);
+ };

which would also get rid of the other three hunks of the patch?

-Toke