From patchwork Thu Feb 9 17:28:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 13134891 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E508C636D7 for ; Thu, 9 Feb 2023 17:30:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229729AbjBIRaC (ORCPT ); Thu, 9 Feb 2023 12:30:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229510AbjBIRaB (ORCPT ); Thu, 9 Feb 2023 12:30:01 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F8A45EA32; Thu, 9 Feb 2023 09:29:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675963792; x=1707499792; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=QpC9th3x5/NYugjD4PK4bxeyl0R1lZv2d7zZ1oILV1o=; b=Q32eRKdARy6uKXeWX1Rxzlme/l7e6qRnj4R4sN/A81ueBFFQMfA6n+YU Dm5CfVOJDMa/lJKDFBN/QdRtKu9ItCPkPVp5vfUGEjaeot3DM/gxSlh3N Mz1Qk2MM0ZU/34q4g3FV9dHQXRMB86aaHksqZRwj/7Fxovk6QUI1FZ/Zc 7nY3aF7iiOiU4svUvcNGBxAB/ztQWHJ/4oqJgIc0XnSVwY+Tr0Oz2md60 CsLg/eSjwjJpD2Cj+ZwwTJ57o6fbK89rGR9lYe4GxSEhKKtxLqQuGo1qc +QHzK5KPqQ2dcvM54ISWGUVPxk3m7eMdh1aowQfzkqcK4TkPRHRimQi7n g==; X-IronPort-AV: E=McAfee;i="6500,9779,10616"; a="332316606" X-IronPort-AV: E=Sophos;i="5.97,284,1669104000"; d="scan'208";a="332316606" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Feb 2023 09:29:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10616"; a="700146775" X-IronPort-AV: E=Sophos;i="5.97,284,1669104000"; d="scan'208";a="700146775" Received: from irvmail002.ir.intel.com ([10.43.11.120]) by orsmga001.jf.intel.com with ESMTP; 09 Feb 2023 09:29:47 -0800 Received: from newjersey.igk.intel.com (newjersey.igk.intel.com [10.102.20.203]) by irvmail002.ir.intel.com (Postfix) with ESMTP id E908D38187; Thu, 9 Feb 2023 17:29:45 +0000 (GMT) From: Alexander Lobakin To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Alexander Lobakin , =?utf-8?q?Toke_H=C3=B8il?= =?utf-8?q?and-J=C3=B8rgensen?= , Martin KaFai Lau , Song Liu , Jesper Dangaard Brouer , Jakub Kicinski , bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf] bpf, test_run: fix &xdp_frame misplacement for LIVE_FRAMES Date: Thu, 9 Feb 2023 18:28:27 +0100 Message-Id: <20230209172827.874728-1-alexandr.lobakin@intel.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net &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. 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. (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 --- 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); + }; }; struct xdp_test_data { @@ -132,7 +135,7 @@ static void xdp_test_run_init_page(struct page *page, void *arg) headroom -= meta_len; new_ctx = &head->ctx; - frm = &head->frm; + frm = head->frm; data = &head->data; memcpy(data + headroom, orig_ctx->data_meta, frm_len); @@ -223,7 +226,7 @@ static void reset_ctx(struct xdp_page_head *head) head->ctx.data = head->orig_ctx.data; head->ctx.data_meta = head->orig_ctx.data_meta; head->ctx.data_end = head->orig_ctx.data_end; - xdp_update_frame_from_buff(&head->ctx, &head->frm); + xdp_update_frame_from_buff(&head->ctx, head->frm); } static int xdp_recv_frames(struct xdp_frame **frames, int nframes, @@ -285,7 +288,7 @@ static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog, head = phys_to_virt(page_to_phys(page)); reset_ctx(head); ctx = &head->ctx; - frm = &head->frm; + frm = head->frm; xdp->frame_cnt++; act = bpf_prog_run_xdp(prog, ctx);