Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2415390lqz; Tue, 2 Apr 2024 17:43:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW30I8+IKREOV8IsK2z1SDPNVueyY6uhact3JxgKW2L6Tt4y68UjV35VpvJeTodpSYe19lT4eLtOJMBsa7veQmQbSsRPplBDWs7cFv0zw== X-Google-Smtp-Source: AGHT+IF9d4r8apUzZI1rqkqONmv1cVFE8hQlTxYKRMJWQSskTr267BgjNFbOByZUQ9MkkvlPICjk X-Received: by 2002:a17:907:86ab:b0:a4e:5bad:b6f3 with SMTP id qa43-20020a17090786ab00b00a4e5badb6f3mr7047683ejc.10.1712104998983; Tue, 02 Apr 2024 17:43:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712104998; cv=pass; d=google.com; s=arc-20160816; b=drKeCbd4Exq0FqQII7aXGcO4qL5qRgyL7KgvGBK3N6j6cBaGInoGqyWx4zUm6Ru8Dg SnBQ+QrG/eRb+qum5MbDH+2be3CJeHXk6/Gt5OZblBz9GNNhAqi5VQv4gxa7I4mLfXjk LQ1qbX6SwtiVQT2p+TpfKoXaP2J0ptcFVX5+eQjJ9CAMNfOCZZ2FAh1M1j892n0ax+3k eNy/4JR2hm3vnMtIrFaEcze03NnMkFT0titP4FfKV+nAZkepJ39abRPvHAKXD5sKucG7 +c8SyOijSh3vWqMjgQB47/ppe1C+zfRH76GTucY/eIow5OX6dHroyUwEyuKLE23V6wbg XCyQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=7nhXcrf/psLdQ173XjHGFDimEkHTNNBO9mtpBEGwTzo=; fh=FboUZw2T19Emki/6yFoQgcl2nYieMusNOhZp/2oSVUk=; b=V8z5IPcwLV1Uz3+o3ZQCrmAkYeYZDgH7UoLFqL2o5H9Ybs/E9HF3pAp8vcRn58YV9C x/r6DvG+3CKW4+jDsg+IL9Iez1E08bmzQmzNQhqkslDEhlpjN4dXGep32tycirpY67Hb NoUguF385gMyBAw1TNe4n0Yk6IBVLrO4eq6W+YZ0QyCB8XV42C6fQFezvy0U0Y2MCrPG W/OTB/zyw+XkwsCc1NepUyE167qOuMazN6r0z5AU8Aiv0xFmRvn7fVcIE5Vz0lsDRr61 3dQU1eddsqkiOvBfjPZBMhq0SolPHK9bdwZ4rC7eROAD20T/2KFiEofsyXPdiqkSdD3Z xeiw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=i94AQCit; arc=pass (i=1 spf=pass spfdomain=flex--almasrymina.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-128861-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128861-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id z22-20020a1709060f1600b00a4956d4d603si6036015eji.639.2024.04.02.17.43.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Apr 2024 17:43:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-128861-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=i94AQCit; arc=pass (i=1 spf=pass spfdomain=flex--almasrymina.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-128861-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128861-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 7A54B1F265D3 for ; Wed, 3 Apr 2024 00:23:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 043B833CD1; Wed, 3 Apr 2024 00:21:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="i94AQCit" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88F871946C for ; Wed, 3 Apr 2024 00:21:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712103675; cv=none; b=mYrieXBT6uMrMgurVSmgm7a1a44xG4i+kPLMYNSGN65vouwrpdKYiVSYzUTg/DNnTfwbGcjR7uOhh3qmZiyntt5/+JeRBdrff/ro6NXuqH/uQUh26LvMn+kx0x3/TyDO3K8u98IX81GLlB4D0rcRBXfjXmdx0SOMt2O9Wvxnw9w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712103675; c=relaxed/simple; bh=xFTAZD9KiJJOW2yUnRdKg1aeM3LKxDpVKUim5i6Calc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dCxhPzI19p8D1ZAVWH1HCxYKGXhLG+0+/dcM9VUtnqXNPh5Scji4NwAWoGqtv4vsYcDJKs+dOe2SqScRZb+paCjcFxxUw2SJoFI38wiQmrf8TT/jPlLoDY//ZvlNAALFPS9NOj34h9HTfjWq0i7tpX9DhwIPJB7dXqYgBpX6YMc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=i94AQCit; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60cbba6f571so102060607b3.1 for ; Tue, 02 Apr 2024 17:21:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712103670; x=1712708470; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7nhXcrf/psLdQ173XjHGFDimEkHTNNBO9mtpBEGwTzo=; b=i94AQCitHHhsrKsNMbd3mEi83gWSL9tCjjNF5TXsdchfsReeRLM+N10/teqkFLyALL Y13DyOiTo6SJVf7NcAqSKrg4EWmxgGaDymiF8WaeTh84Dci5dXvnGBts6QDOqkucPIXa fzf2WcjZIfW7ef82Oyrix5gUEuZRwdScTSCVryV8d0YjjfDPTWvrnlWjeApukwk1GzFW 0XJz4AZ1BzihVdBTdqZYP3edsce8H6MRjVYOcdjwhcm0IC+v+vi4A0MhYEDLOjnyCKNN nw8q1bSNHUqu96O5gGne6I0meWBVnXwxzYydJh8W+vO7ARsRBFQEYl5g70W7Veu6KZHL H7rQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712103670; x=1712708470; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7nhXcrf/psLdQ173XjHGFDimEkHTNNBO9mtpBEGwTzo=; b=Fq6EKd27hqFPFGlsUtsHx0E5/HB5f/ZRdzPzCi9X4z69O5/s46qh0wEL01OxSxImfG krx74BjhN5zvnx23fhVmnLyVjJ66tObAIGHAV6lwVBptCEGxFvXFypKsmcscn1BRLa6R E//Ne2ky8QOgHKGK5k5jTuvu9IRlPQDapOFMujf7ehIsfIJ42m3b+fWCq/ul//NH+GJv Sb4Q3FwcsZmSR7im0HNNklv8j7HoRyCyC3nrdFMHTylit7nPViWMrb4rsXUPnXZCULFA m/bYjabm3+mWpVEMQkowAStmUoEHCmH2YcV8SgFPFU+E8+NXdHba7Ip0wzWoi+RD1kTK nISQ== X-Forwarded-Encrypted: i=1; AJvYcCUC42qXbbpQNMoQzMEZAJ2J9v3c8JV6lgniLcDCurEnPjegosRYKrO/1YXLcRJNIN0j3ArHmOauRNbkSerT7SF+ZhzJv0YG+C+czDYc X-Gm-Message-State: AOJu0YwYhSXqX0KnoMMP+feKJQ3efo2easlZnnW66A2T8zHODvHcHSDX mpo7s1eBhtBuJBEBGm+dcEbR22pPh8aVe30ALiZh51vwO8Czw1TpzryqvIH6ePCRfwTWTmye8LR nvxQAYYTkCNwCUk9pBJH2xw== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:1726:7620:90a1:78b9]) (user=almasrymina job=sendgmr) by 2002:a81:52c7:0:b0:615:102e:7d3b with SMTP id g190-20020a8152c7000000b00615102e7d3bmr1133480ywb.1.1712103670342; Tue, 02 Apr 2024 17:21:10 -0700 (PDT) Date: Tue, 2 Apr 2024 17:20:42 -0700 In-Reply-To: <20240403002053.2376017-1-almasrymina@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240403002053.2376017-1-almasrymina@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240403002053.2376017-6-almasrymina@google.com> Subject: [RFC PATCH net-next v8 05/14] netdev: netdevice devmem allocator From: Mina Almasry To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-alpha@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, sparclinux@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-arch@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Cc: Mina Almasry , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Jonathan Corbet , Richard Henderson , Ivan Kokshaysky , Matt Turner , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Andreas Larsson , Jesper Dangaard Brouer , Ilias Apalodimas , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Arnd Bergmann , 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 , Steffen Klassert , Herbert Xu , David Ahern , Willem de Bruijn , Shuah Khan , Sumit Semwal , "=?UTF-8?q?Christian=20K=C3=B6nig?=" , Amritha Nambiar , Maciej Fijalkowski , Alexander Mikhalitsyn , Kaiyuan Zhang , Christian Brauner , Simon Horman , David Howells , Florian Westphal , Yunsheng Lin , Kuniyuki Iwashima , Jens Axboe , Arseniy Krasnov , Aleksander Lobakin , Michael Lass , Jiri Pirko , Sebastian Andrzej Siewior , Lorenzo Bianconi , Richard Gobert , Sridhar Samudrala , Xuan Zhuo , Johannes Berg , Abel Wu , Breno Leitao , Pavel Begunkov , David Wei , Jason Gunthorpe , Shailend Chand , Harshitha Ramamurthy , Shakeel Butt , Jeroen de Borst , Praveen Kaligineedi , Willem de Bruijn Content-Type: text/plain; charset="UTF-8" Implement netdev devmem allocator. The allocator takes a given struct netdev_dmabuf_binding as input and allocates net_iov from that binding. The allocation simply delegates to the binding's genpool for the allocation logic and wraps the returned memory region in a net_iov struct. Signed-off-by: Willem de Bruijn Signed-off-by: Kaiyuan Zhang Signed-off-by: Mina Almasry --- v8: - Rename netdev_dmabuf_binding -> net_devmem_dmabuf_binding to avoid patch-by-patch build error. - Move niov->pp_magic/pp/pp_ref_counter usage to later patch to avoid patch-by-patch build error. v7: - netdev_ -> net_devmem_* naming (Yunsheng). v6: - Add comment on net_iov_dma_addr to explain why we don't use niov->dma_addr (Pavel) - Refactor new functions into net/core/devmem.c (Pavel) v1: - Rename devmem -> dmabuf (David). --- include/net/devmem.h | 13 +++++++++++++ include/net/netmem.h | 40 ++++++++++++++++++++++++++++++++++++++++ net/core/devmem.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/include/net/devmem.h b/include/net/devmem.h index fa03bdabdffd..cd3186f5d1fb 100644 --- a/include/net/devmem.h +++ b/include/net/devmem.h @@ -68,7 +68,20 @@ int net_devmem_bind_dmabuf(struct net_device *dev, unsigned int dmabuf_fd, void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding); int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx, struct net_devmem_dmabuf_binding *binding); +struct net_iov * +net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding); +void net_devmem_free_dmabuf(struct net_iov *ppiov); #else +static inline struct net_iov * +net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding) +{ + return NULL; +} + +static inline void net_devmem_free_dmabuf(struct net_iov *ppiov) +{ +} + static inline void __net_devmem_dmabuf_binding_free(struct net_devmem_dmabuf_binding *binding) { diff --git a/include/net/netmem.h b/include/net/netmem.h index 72e932a1a948..33014370a885 100644 --- a/include/net/netmem.h +++ b/include/net/netmem.h @@ -14,8 +14,48 @@ struct net_iov { struct dmabuf_genpool_chunk_owner *owner; + unsigned long dma_addr; }; +static inline struct dmabuf_genpool_chunk_owner * +net_iov_owner(const struct net_iov *niov) +{ + return niov->owner; +} + +static inline unsigned int net_iov_idx(const struct net_iov *niov) +{ + return niov - net_iov_owner(niov)->niovs; +} + +/* This returns the absolute dma_addr_t calculated from + * net_iov_owner(niov)->owner->base_dma_addr, not the page_pool-owned + * niov->dma_addr. + * + * The absolute dma_addr_t is a dma_addr_t that is always uncompressed. + * + * The page_pool-owner niov->dma_addr is the absolute dma_addr compressed into + * an unsigned long. Special handling is done when the unsigned long is 32-bit + * but the dma_addr_t is 64-bit. + * + * In general code looking for the dma_addr_t should use net_iov_dma_addr(), + * while page_pool code looking for the unsigned long dma_addr which mirrors + * the field in struct page should use niov->dma_addr. + */ +static inline dma_addr_t net_iov_dma_addr(const struct net_iov *niov) +{ + struct dmabuf_genpool_chunk_owner *owner = net_iov_owner(niov); + + return owner->base_dma_addr + + ((dma_addr_t)net_iov_idx(niov) << PAGE_SHIFT); +} + +static inline struct net_devmem_dmabuf_binding * +net_iov_binding(const struct net_iov *niov) +{ + return net_iov_owner(niov)->binding; +} + /* netmem */ /** diff --git a/net/core/devmem.c b/net/core/devmem.c index 02a6abaecf40..268fc8455a6d 100644 --- a/net/core/devmem.c +++ b/net/core/devmem.c @@ -103,6 +103,42 @@ static int net_devmem_restart_rx_queue(struct net_device *dev, int rxq_idx) return err; } +struct net_iov * +net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding) +{ + struct dmabuf_genpool_chunk_owner *owner; + unsigned long dma_addr; + struct net_iov *niov; + ssize_t offset; + ssize_t index; + + dma_addr = gen_pool_alloc_owner(binding->chunk_pool, PAGE_SIZE, + (void **)&owner); + if (!dma_addr) + return NULL; + + offset = dma_addr - owner->base_dma_addr; + index = offset / PAGE_SIZE; + niov = &owner->niovs[index]; + + niov->dma_addr = 0; + + net_devmem_dmabuf_binding_get(binding); + + return niov; +} + +void net_devmem_free_dmabuf(struct net_iov *niov) +{ + struct net_devmem_dmabuf_binding *binding = net_iov_binding(niov); + unsigned long dma_addr = net_iov_dma_addr(niov); + + if (gen_pool_has_addr(binding->chunk_pool, dma_addr, PAGE_SIZE)) + gen_pool_free(binding->chunk_pool, dma_addr, PAGE_SIZE); + + net_devmem_dmabuf_binding_put(binding); +} + /* Protected by rtnl_lock() */ static DEFINE_XARRAY_FLAGS(net_devmem_dmabuf_bindings, XA_FLAGS_ALLOC1); -- 2.44.0.478.gd926399ef9-goog