From patchwork Thu Feb 15 13:26:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 13558381 X-Patchwork-Delegate: kuba@kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9404212FF88 for ; Thu, 15 Feb 2024 13:26:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708003604; cv=none; b=i1kJMBjxxHiR7UbT88f1oemAiw9xwpobbqHHRkf9wUKDTcKXH9hgWSmO3Q6ETLKLGzb0XRjqMGVdv9X2e1YKf8XHjA3b2tpW4atV9YVo+mhZQfzFoQTvd7aR74dIvmqMHE9XfXeRd+NZNnf+atu5fKCQEQ+185alhyFZkh6iUhE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708003604; c=relaxed/simple; bh=z+o5iOnXAyRBzVGt5Tc6uCVeHlkYoqCfK8Ac6jiEb6M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=avG32fwHpTIhksHSPsffE8IyxF+iBlCZTFM9yYYkJmp3H/NSlegVoIRI/EZoAsVU4CSwRJj3tRTKsRJCUCRRyOL/+jfu0dUXEs/vZiiGD3VwVgorq7LLvQFSTwAwNRIB8x9G4nR9wN3sO2AgNMM19Q46V4UQFZTkCNpxmAd+y78= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=aJ5ovC8y; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="aJ5ovC8y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708003601; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Tv8iEvuALyULMFKYPzlVkSrmB4LE1yRmY8GywglSPNM=; b=aJ5ovC8yhtUd4LaTAWMXJwBIRoM0UGP7N4V2LiyM1ugPcrIGCrB8YRQMDRJO1ocqA5+FU2 +wuzwZJ/wxNXHk2w6Q2YBmdIjmHE8c8k5WiNYzsc1678ltoFjnpEOaxEQHfZOPxnPsYiuA GT4WErhu0yPHU06J8I0lr9KX389QBsI= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-570-gKNbBUZ6ND2KH7AIDBZVDw-1; Thu, 15 Feb 2024 08:26:40 -0500 X-MC-Unique: gKNbBUZ6ND2KH7AIDBZVDw-1 Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-559391de41bso639172a12.3 for ; Thu, 15 Feb 2024 05:26:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708003599; x=1708608399; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Tv8iEvuALyULMFKYPzlVkSrmB4LE1yRmY8GywglSPNM=; b=WSBNQtTe3YQhRShS2oUAi6Jvvhu6U2vZShjSZw4Io89qM3rb6VkSj4igI3nRqMp/Zl jyzdnvw2WEy95l8EZBX5pCaPoAoIXtZyg7rAQF01WueEMJhRec2XhUKxWT1yzODASqT5 cf3S5VWv2gB8enQyxb+uZ83GV5cQ9dIOrt5IE2/1CUTe2lpC7neLIoFI+qo3R8hior1J rLmBl2Y+Hk4wzyrcwuoBBlRFQEAqvtzPjPOVVzZCa5VdTQaV0zSScolPyIOBKeer5qx8 dmXsSLx+vmtjQzOKUqA7KZR9hnYvQWJx3ji5UDd6VbxB+ju69+ZGY8xiyNn76L9/OjAb hYFQ== X-Forwarded-Encrypted: i=1; AJvYcCX1raaBXvoDpaHE15LsBzhsNS8XAi6DxUIT1GcPGuA0NGGl2MVc52wFIDgF/jpnhfjhjbQJX4M6BXKrBPo6QDynxwVj X-Gm-Message-State: AOJu0YyTuYUB4zTCVHpBZyKDJWt2TEUmGbQITwRsE8nhkgL5TNN1zViX dfJjokqG/wjtN1kzfcD2i0lF6cJRidGFI7Va4CaKWnJlRjSaXQ+X20CK2DvoGYYxmsoi9ab0mSZ +BDblAQf5uExjmlTxkTHI57QMcvF5gVUFt5ODdPUfMl2Yw3+eVA== X-Received: by 2002:a05:6402:1a42:b0:562:71:8105 with SMTP id bf2-20020a0564021a4200b0056200718105mr1175805edb.11.1708003599257; Thu, 15 Feb 2024 05:26:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IHPf/ND7jD6kLW9EkMyz5hznD/WCdWi9FJ5Pk1ElHBJ0YDMzOQcRFnXqZQMypPf5OuMvSf+UA== X-Received: by 2002:a05:6402:1a42:b0:562:71:8105 with SMTP id bf2-20020a0564021a4200b0056200718105mr1175796edb.11.1708003599040; Thu, 15 Feb 2024 05:26:39 -0800 (PST) Received: from alrua-x1.borgediget.toke.dk ([45.145.92.2]) by smtp.gmail.com with ESMTPSA id de8-20020a056402308800b00562149c7bf4sm534659edb.48.2024.02.15.05.26.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 05:26:38 -0800 (PST) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 025BC10F59B9; Thu, 15 Feb 2024 14:26:37 +0100 (CET) From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend Cc: Alexander Lobakin , =?utf-8?q?Toke_H=C3=B8?= =?utf-8?q?iland-J=C3=B8rgensen?= , netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH net-next 1/3] net: Register system page pool as an XDP memory model Date: Thu, 15 Feb 2024 14:26:30 +0100 Message-ID: <20240215132634.474055-2-toke@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240215132634.474055-1-toke@redhat.com> References: <20240215132634.474055-1-toke@redhat.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org To make the system page pool usable as a source for allocating XDP frames, we need to register it with xdp_reg_mem_model(), so that page return works correctly. This is done in preparation for using the system page pool for the XDP live frame mode in BPF_TEST_RUN; for the same reason, make the per-cpu variable non-static so we can access it from the test_run code as well. Signed-off-by: Toke Høiland-Jørgensen --- include/linux/netdevice.h | 1 + net/core/dev.c | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index c541550b0e6e..e1dfdf0c4075 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -3345,6 +3345,7 @@ static inline void input_queue_tail_incr_save(struct softnet_data *sd, } DECLARE_PER_CPU_ALIGNED(struct softnet_data, softnet_data); +DECLARE_PER_CPU_ALIGNED(struct page_pool *, system_page_pool); static inline int dev_recursion_level(void) { diff --git a/net/core/dev.c b/net/core/dev.c index d8dd293a7a27..cdb916a647e7 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -428,7 +428,7 @@ EXPORT_PER_CPU_SYMBOL(softnet_data); * PP consumers must pay attention to run APIs in the appropriate context * (e.g. NAPI context). */ -static DEFINE_PER_CPU_ALIGNED(struct page_pool *, system_page_pool); +DEFINE_PER_CPU_ALIGNED(struct page_pool *, system_page_pool); #ifdef CONFIG_LOCKDEP /* @@ -11739,12 +11739,20 @@ static int net_page_pool_create(int cpuid) .pool_size = SYSTEM_PERCPU_PAGE_POOL_SIZE, .nid = NUMA_NO_NODE, }; + struct xdp_mem_info info; struct page_pool *pp_ptr; + int err; pp_ptr = page_pool_create_percpu(&page_pool_params, cpuid); if (IS_ERR(pp_ptr)) return -ENOMEM; + err = xdp_reg_mem_model(&info, MEM_TYPE_PAGE_POOL, pp_ptr); + if (err) { + page_pool_destroy(pp_ptr); + return err; + } + per_cpu(system_page_pool, cpuid) = pp_ptr; #endif return 0; @@ -11834,12 +11842,15 @@ static int __init net_dev_init(void) out: if (rc < 0) { for_each_possible_cpu(i) { + struct xdp_mem_info mem = { .type = MEM_TYPE_PAGE_POOL }; struct page_pool *pp_ptr; pp_ptr = per_cpu(system_page_pool, i); if (!pp_ptr) continue; + mem.id = pp_ptr->xdp_mem_id; + xdp_unreg_mem_model(&mem); page_pool_destroy(pp_ptr); per_cpu(system_page_pool, i) = NULL; } From patchwork Thu Feb 15 13:26:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 13558383 X-Patchwork-Delegate: kuba@kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C9BF12BE8D for ; Thu, 15 Feb 2024 13:26:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708003605; cv=none; b=F28IAvRoyATLJOWi+F/BHrAj5B+N25QM4TB92OFOMVbL90UF1meYP66auiZEOXjoTX+XB7h8XSjoJ29hSFRcduWXCWOce2Th+6zuYYAMyGnk11nYeNLrrNaK6U+Hk7FbFgN93oFLJitJAaTDECNfU6OCrjvkSCBasnUDIqpeDvU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708003605; c=relaxed/simple; bh=7WA3XHLMFKhdbspeydhiJXmgmQ3pp9zijHtxzQQc1iw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IiHZqOe931nP/JW0J0knsYik6lq0jqLHTJqkJDdMC3SLRXJOIayuIyZVDU2wqWFZAOEqTOFECWHr/nMlIN3et6bvj479hrASie5ZeiE6Bb8DAYCLcTZR+ZVgJAHA9FA7nrLL4dS8gvvdHDeb08rdov5Z/Q8rSOUBspOLqpwYFf8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=bSnYTc6u; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="bSnYTc6u" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708003602; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bIIW9lTPNrP13OJ/aTfa3Gowpk1DCuaoFBbweL99mOE=; b=bSnYTc6uSKjj23eATepT+GLO+YDmeycQD7RVw0K6TKN0Qmp+AMmvvBpVuDU+gQx3OgRmoc TDa1V6+6PaF0mhFwkorzjow28MEt6ybz6Rph2jkDQ7g8PNGqdGi866Y39wLoPkVySnTVNR 51rSWu4qFDmK5ZEzgdfOXYhTQ7kPygE= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-151-mB4Zg97LPryd3UJ5hHb2PQ-1; Thu, 15 Feb 2024 08:26:40 -0500 X-MC-Unique: mB4Zg97LPryd3UJ5hHb2PQ-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a3d7cd58ae2so48876566b.1 for ; Thu, 15 Feb 2024 05:26:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708003600; x=1708608400; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bIIW9lTPNrP13OJ/aTfa3Gowpk1DCuaoFBbweL99mOE=; b=bE0+chh49eJkluQeXL7LVgVM7HMiSVP1vWxDy+6wsYw/hMaXdzD5Vi6+LJnRxePtuG jWExnozhvvzteOi6GhcHp1SFWQDFGogTG2D0NfikOnO7MaYiPHL2MOMrmjM59i2JcRSB PQQbRWr/vEd7KLSAJHzDfSIig2GBLFMUPanu2/b4+1HTy0UlJri3vVPe3ES8RqI0aTNf soCkWqzo8fCTCjNes7C0hXN3c7TDUjWdXto/Dcfg7/kJeu5l+0cIFOiyX9XL3Deiaugh ncnhDQ12Fayee6fOzof/f9ncodJELEWgqrhcNOwuVI4WoaP319ulfYABYvZzT1aH1esL J0fw== X-Forwarded-Encrypted: i=1; AJvYcCUrigIrF9VoGIQrQsxsFlJMIzV1Ofsud61j3KztL8Hk+eu+LiKexZwePbMH58Yge5EbAr5Ymm3gl5BzZssi5thrl0db X-Gm-Message-State: AOJu0YzX77BOEkszEXlPc+nCroDCwaF1cY6qkpmKr+VTNhU+T5SawcwX pU7QWmjZiFl15HYDI8hrVFdw0U+ZPCFYbhFoXSjcR8cMFegn5N2OUXmk184aRZdoTK9ghBHZKE3 KYn3BHlNebVCLnV3A9+TT9BnPNSfjI971m22OsidLK+qPMKqFvQ== X-Received: by 2002:a17:906:ca56:b0:a38:9590:cde8 with SMTP id jx22-20020a170906ca5600b00a389590cde8mr1516024ejb.73.1708003599772; Thu, 15 Feb 2024 05:26:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IEKYPsuoPIqzesQq2jztNW0ZYlrQYfLyusrO/TcBvKVuXNEBD1TliDBZZ+iqDd8LoU92urZ4Q== X-Received: by 2002:a17:906:ca56:b0:a38:9590:cde8 with SMTP id jx22-20020a170906ca5600b00a389590cde8mr1516006ejb.73.1708003599414; Thu, 15 Feb 2024 05:26:39 -0800 (PST) Received: from alrua-x1.borgediget.toke.dk ([45.145.92.2]) by smtp.gmail.com with ESMTPSA id hd15-20020a170907968f00b00a3d62948fadsm548652ejc.173.2024.02.15.05.26.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 05:26:39 -0800 (PST) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 5BC3A10F59BB; Thu, 15 Feb 2024 14:26:38 +0100 (CET) From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: Alexander Lobakin , =?utf-8?q?Toke_H=C3=B8?= =?utf-8?q?iland-J=C3=B8rgensen?= , Eric Dumazet , Paolo Abeni , bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH net-next 2/3] bpf: test_run: Use system page pool for XDP live frame mode Date: Thu, 15 Feb 2024 14:26:31 +0100 Message-ID: <20240215132634.474055-3-toke@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240215132634.474055-1-toke@redhat.com> References: <20240215132634.474055-1-toke@redhat.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org The BPF_TEST_RUN code in XDP live frame mode creates a new page pool each time it is called and uses that to allocate the frames used for the XDP run. This works well if the syscall is used with a high repetitions number, as it allows for efficient page recycling. However, if used with a small number of repetitions, the overhead of creating and tearing down the page pool is significant, and can even lead to system stalls if the syscall is called in a tight loop. Now that we have a persistent system page pool instance, it becomes pretty straight forward to change the test_run code to use it. The only wrinkle is that we can no longer rely on a custom page init callback from page_pool itself; instead, we change the test_run code to write a random cookie value to the beginning of the page as an indicator that the page has been initialised and can be re-used without copying the initial data again. Signed-off-by: Toke Høiland-Jørgensen Acked-by: Daniel Borkmann --- net/bpf/test_run.c | 134 ++++++++++++++++++++++----------------------- 1 file changed, 66 insertions(+), 68 deletions(-) diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index dfd919374017..c742869a0612 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -94,10 +94,14 @@ static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations, } /* We put this struct at the head of each page with a context and frame - * initialised when the page is allocated, so we don't have to do this on each - * repetition of the test run. + * initialised the first time a given page is used, saving the memcpy() of the + * data on subsequent repetition of the test run. The cookie value is used to + * mark the page data the first time we initialise it so we can skip it the next + * time we see that page. */ + struct xdp_page_head { + u64 cookie; struct xdp_buff orig_ctx; struct xdp_buff ctx; union { @@ -111,10 +115,9 @@ struct xdp_test_data { struct xdp_buff *orig_ctx; struct xdp_rxq_info rxq; struct net_device *dev; - struct page_pool *pp; struct xdp_frame **frames; struct sk_buff **skbs; - struct xdp_mem_info mem; + u64 cookie; u32 batch_size; u32 frame_cnt; }; @@ -126,48 +129,9 @@ struct xdp_test_data { #define TEST_XDP_FRAME_SIZE (PAGE_SIZE - sizeof(struct xdp_page_head)) #define TEST_XDP_MAX_BATCH 256 -static void xdp_test_run_init_page(struct page *page, void *arg) -{ - struct xdp_page_head *head = phys_to_virt(page_to_phys(page)); - struct xdp_buff *new_ctx, *orig_ctx; - u32 headroom = XDP_PACKET_HEADROOM; - struct xdp_test_data *xdp = arg; - size_t frm_len, meta_len; - struct xdp_frame *frm; - void *data; - - orig_ctx = xdp->orig_ctx; - frm_len = orig_ctx->data_end - orig_ctx->data_meta; - meta_len = orig_ctx->data - orig_ctx->data_meta; - headroom -= meta_len; - - new_ctx = &head->ctx; - frm = head->frame; - data = head->data; - memcpy(data + headroom, orig_ctx->data_meta, frm_len); - - xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq); - xdp_prepare_buff(new_ctx, data, headroom, frm_len, true); - new_ctx->data = new_ctx->data_meta + meta_len; - - xdp_update_frame_from_buff(new_ctx, frm); - frm->mem = new_ctx->rxq->mem; - - memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx)); -} - static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_ctx) { - struct page_pool *pp; int err = -ENOMEM; - struct page_pool_params pp_params = { - .order = 0, - .flags = 0, - .pool_size = xdp->batch_size, - .nid = NUMA_NO_NODE, - .init_callback = xdp_test_run_init_page, - .init_arg = xdp, - }; xdp->frames = kvmalloc_array(xdp->batch_size, sizeof(void *), GFP_KERNEL); if (!xdp->frames) @@ -177,34 +141,21 @@ static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_c if (!xdp->skbs) goto err_skbs; - pp = page_pool_create(&pp_params); - if (IS_ERR(pp)) { - err = PTR_ERR(pp); - goto err_pp; - } - - /* will copy 'mem.id' into pp->xdp_mem_id */ - err = xdp_reg_mem_model(&xdp->mem, MEM_TYPE_PAGE_POOL, pp); - if (err) - goto err_mmodel; - - xdp->pp = pp; - /* We create a 'fake' RXQ referencing the original dev, but with an * xdp_mem_info pointing to our page_pool */ xdp_rxq_info_reg(&xdp->rxq, orig_ctx->rxq->dev, 0, 0); - xdp->rxq.mem.type = MEM_TYPE_PAGE_POOL; - xdp->rxq.mem.id = pp->xdp_mem_id; + xdp->rxq.mem.type = MEM_TYPE_PAGE_POOL; /* mem id is set per-frame below */ xdp->dev = orig_ctx->rxq->dev; xdp->orig_ctx = orig_ctx; + /* We need a random cookie for each run as pages can stick around + * between runs in the system page pool + */ + get_random_bytes(&xdp->cookie, sizeof(xdp->cookie)); + return 0; -err_mmodel: - page_pool_destroy(pp); -err_pp: - kvfree(xdp->skbs); err_skbs: kvfree(xdp->frames); return err; @@ -212,8 +163,6 @@ static int xdp_test_run_setup(struct xdp_test_data *xdp, struct xdp_buff *orig_c static void xdp_test_run_teardown(struct xdp_test_data *xdp) { - xdp_unreg_mem_model(&xdp->mem); - page_pool_destroy(xdp->pp); kfree(xdp->frames); kfree(xdp->skbs); } @@ -235,8 +184,12 @@ static bool ctx_was_changed(struct xdp_page_head *head) head->orig_ctx.data_end != head->ctx.data_end; } -static void reset_ctx(struct xdp_page_head *head) +static void reset_ctx(struct xdp_page_head *head, struct xdp_test_data *xdp) { + /* mem id can change if we migrate CPUs between batches */ + if (head->frame->mem.id != xdp->rxq.mem.id) + head->frame->mem.id = xdp->rxq.mem.id; + if (likely(!frame_was_changed(head) && !ctx_was_changed(head))) return; @@ -246,6 +199,48 @@ static void reset_ctx(struct xdp_page_head *head) xdp_update_frame_from_buff(&head->ctx, head->frame); } +static struct xdp_page_head * +xdp_test_run_init_page(struct page *page, struct xdp_test_data *xdp) +{ + struct xdp_page_head *head = phys_to_virt(page_to_phys(page)); + struct xdp_buff *new_ctx, *orig_ctx; + u32 headroom = XDP_PACKET_HEADROOM; + size_t frm_len, meta_len; + struct xdp_frame *frm; + void *data; + + /* Optimise for the recycle case, which is the normal case when doing + * high-repetition REDIRECTS to drivers that return frames. + */ + if (likely(head->cookie == xdp->cookie)) { + reset_ctx(head, xdp); + return head; + } + + head->cookie = xdp->cookie; + + orig_ctx = xdp->orig_ctx; + frm_len = orig_ctx->data_end - orig_ctx->data_meta; + meta_len = orig_ctx->data - orig_ctx->data_meta; + headroom -= meta_len; + + new_ctx = &head->ctx; + frm = head->frame; + data = head->data; + memcpy(data + headroom, orig_ctx->data_meta, frm_len); + + xdp_init_buff(new_ctx, TEST_XDP_FRAME_SIZE, &xdp->rxq); + xdp_prepare_buff(new_ctx, data, headroom, frm_len, true); + new_ctx->data = new_ctx->data_meta + meta_len; + + xdp_update_frame_from_buff(new_ctx, frm); + frm->mem = new_ctx->rxq->mem; + + memcpy(&head->orig_ctx, new_ctx, sizeof(head->orig_ctx)); + + return head; +} + static int xdp_recv_frames(struct xdp_frame **frames, int nframes, struct sk_buff **skbs, struct net_device *dev) @@ -287,6 +282,7 @@ static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog, struct xdp_page_head *head; struct xdp_frame *frm; bool redirect = false; + struct page_pool *pp; struct xdp_buff *ctx; struct page *page; @@ -295,15 +291,17 @@ static int xdp_test_run_batch(struct xdp_test_data *xdp, struct bpf_prog *prog, local_bh_disable(); xdp_set_return_frame_no_direct(); + pp = this_cpu_read(system_page_pool); + xdp->rxq.mem.id = pp->xdp_mem_id; + for (i = 0; i < batch_sz; i++) { - page = page_pool_dev_alloc_pages(xdp->pp); + page = page_pool_dev_alloc_pages(pp); if (!page) { err = -ENOMEM; goto out; } - head = phys_to_virt(page_to_phys(page)); - reset_ctx(head); + head = xdp_test_run_init_page(page, xdp); ctx = &head->ctx; frm = head->frame; xdp->frame_cnt++; From patchwork Thu Feb 15 13:26:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 13558384 X-Patchwork-Delegate: kuba@kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6AE67130E4F for ; Thu, 15 Feb 2024 13:26:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708003609; cv=none; b=LxdK1sLomv7/Jay2GTKzM2FbTo6RckC3uwa8SjhNv+yyT9m6nPnsUsYwC2MbUXt4rD34jFNmLHITJrl9MbuSJ30fhGf5IME9Y8HvAQnKUjOE9gK5GIyYOsXgM6Ms48OxwQaBSDfNp+gJ+YtHLVM4oM+CuFvxmybCwXCnE/BKVMM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708003609; c=relaxed/simple; bh=QDdsa22JRBCbPcK2RkRXKRkISSPXJi+h0sYd5xj90HI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Uyyp1io2YcnAfuoYcYclE0cpx35Y6hrOBYjrfNrcMXPwmerXb+4z18ye6oj7HZX3nNd9NShfHTsMUQraUgASVF742u2T5Fh/7Z+hhDdvDNNMGTHmTssSafzk9Ytx7veIQYZNPYr0xsrWrHKWCGiibgX6TrK86xwE9CjyYaO70zQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Zf+WyE67; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Zf+WyE67" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708003606; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xlLpS5Tb/Pfs7J4bWfeg5Rka0NiuA94LlXE1OEm0A9I=; b=Zf+WyE67yfa/wW4r1qpq6r3qQ8UAyQ78cdsBoccED6REAue5Nbh4/9G3xI/qRjR5XhoB15 A/RnGVHAzraRQo/eTK9GPqYrVLHesuBw8CQDEsMBr0/tjjUTaSV8OicuFrP8Y7G/RKOBU5 PwQTw1L7MP1OEYu39ZeIUrRt4hh3QZI= Received: from mail-lf1-f69.google.com (mail-lf1-f69.google.com [209.85.167.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-561-r3nmhU8eOcqP6I1iHibWYw-1; Thu, 15 Feb 2024 08:26:44 -0500 X-MC-Unique: r3nmhU8eOcqP6I1iHibWYw-1 Received: by mail-lf1-f69.google.com with SMTP id 2adb3069b0e04-5117210e716so708380e87.2 for ; Thu, 15 Feb 2024 05:26:44 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708003603; x=1708608403; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xlLpS5Tb/Pfs7J4bWfeg5Rka0NiuA94LlXE1OEm0A9I=; b=IAR1Ze8TDW2c2Bld6fSLn9wLhPcvft0gHNtuuLo6XPn5mf6XqwTHIpOkZdIGQuf47H RRjDawKjSpQ4Tpts3vpxl5ovi5badHI32R+prwRCklNPcxkbj/6SDsJ83emUcGXd2xGJ bCgvKJU03FIKnaKLB90JKTYyB8YxyeLKqOGGz+ca9ZT+jsCKUBVERG0jWRDok831oElk 8v+ALwOOn9H5KNoqK6XuIY1Y+1dDJmuYTPVh0bKx4MuBApp15ctMYo5th1iesMsKd/GJ lKrj9jCWjKq4RhPjaKQUkz9XvmUfJUO/eoiJoly7H2J/igcdU4RY9cuxpZsGr5b8UGxN EMlg== X-Forwarded-Encrypted: i=1; AJvYcCW08PxNIVKE07RtsWlGXEyo06LMcHZGuyzAaChi5qy0jmWG/HRbccvhGIQSQ6JbGOos0VPVTc3/iftHhIkYZDdRk4L7 X-Gm-Message-State: AOJu0YzWkH8H8vzqXHkyEBwhODuIyIuNlRT64/KvGMOUcoBFCO+V2OMk t+WqsGNjMvUXkCKqX8xh+oZm0pnW8isLH81J5OIpVHMeCoZHxVooTpZT8kuy3irc/n6FQwAqi98 7oeRSXT5rAtZR5oU6tksstS/ScoID7ap2w6XySSZiHinTg5IyOw== X-Received: by 2002:a05:6512:3da8:b0:512:888a:e6d2 with SMTP id k40-20020a0565123da800b00512888ae6d2mr1396914lfv.62.1708003603074; Thu, 15 Feb 2024 05:26:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IEdud5K3pG6VPBPAz6cjIUmZme+rRP4ncVLG9wjgux0pk9gbYuydcHQNbSP50Ifr+PB1VIhEw== X-Received: by 2002:a05:6512:3da8:b0:512:888a:e6d2 with SMTP id k40-20020a0565123da800b00512888ae6d2mr1396889lfv.62.1708003602686; Thu, 15 Feb 2024 05:26:42 -0800 (PST) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id um10-20020a170906cf8a00b00a3d599f47c2sm555581ejb.18.2024.02.15.05.26.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 05:26:39 -0800 (PST) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id A329910F59BD; Thu, 15 Feb 2024 14:26:38 +0100 (CET) From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer Cc: Alexander Lobakin , =?utf-8?q?Toke_H=C3=B8?= =?utf-8?q?iland-J=C3=B8rgensen?= , Eric Dumazet , Paolo Abeni , bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH net-next 3/3] bpf: test_run: Fix cacheline alignment of live XDP frame data structures Date: Thu, 15 Feb 2024 14:26:32 +0100 Message-ID: <20240215132634.474055-4-toke@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240215132634.474055-1-toke@redhat.com> References: <20240215132634.474055-1-toke@redhat.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org The live XDP frame code in BPF_PROG_RUN suffered from suboptimal cache line placement due to the forced cache line alignment of struct xdp_rxq_info. Rearrange things so we don't waste a whole cache line on padding, and also add explicit alignment to the data_hard_start field in the start-of-page data structure we use for the data pages. Signed-off-by: Toke Høiland-Jørgensen Acked-by: Daniel Borkmann --- net/bpf/test_run.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index c742869a0612..d153a3b3528f 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -108,12 +108,12 @@ struct xdp_page_head { /* ::data_hard_start starts here */ DECLARE_FLEX_ARRAY(struct xdp_frame, frame); DECLARE_FLEX_ARRAY(u8, data); - }; + } ____cacheline_aligned; }; struct xdp_test_data { - struct xdp_buff *orig_ctx; struct xdp_rxq_info rxq; + struct xdp_buff *orig_ctx; struct net_device *dev; struct xdp_frame **frames; struct sk_buff **skbs;