From patchwork Mon Dec 18 02:40:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 13497368 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 961F1C35274 for ; Mon, 18 Dec 2023 18:46:21 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C7CC510E367; Mon, 18 Dec 2023 18:46:13 +0000 (UTC) Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by gabe.freedesktop.org (Postfix) with ESMTPS id A03FA10E1E5 for ; Mon, 18 Dec 2023 18:42:24 +0000 (UTC) Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-28b3b68be42so1145826a91.0 for ; Mon, 18 Dec 2023 10:42:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702924944; x=1703529744; darn=lists.freedesktop.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=G5EYHEqOKeBIt6a6HG2bP5krFWMMN/+OHgJTydxcJRs=; b=mOMFfbi8VA3JMZ0whZaSjqdMvXZr0S81kOhQmbF1zEI/QgOCfnX7ZyaFiVVEF4uY+H /p+v2m7l8MNviPrgflsGo2hLBB8WU65YanWD+kvFeKeUpebDT8HbeOLz8JIxe8+jR50J Fi4Rw8KsbBwmAMnzVvD4NCH1KS3DkT+Uugsj1RBm0zZ/dtKLNfXRBW5EsE9PiYp2dKgv CvPVfu+3xdt9rHw1p/FsFaLBJKW7CXfY3jyPjfM6MGDd3Hb9LNPk5IbJWuxQ4mFLQV51 jngH1xxlw5nsv9ePFVZhrxtjXguskdGeod9oc8aCg0xQX4XEepcf7kFQiX/n60kJxKtE w5yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702924944; x=1703529744; 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=G5EYHEqOKeBIt6a6HG2bP5krFWMMN/+OHgJTydxcJRs=; b=wJgSDkD+cZAbDU03JT/lMM6tVzZkK+muBv7CofbcLw+Wm6ul+2oLdDftdjU5qpjF4Z wpr36QnaT7E5Muqj5QiLacB3+Rx16AJq0jNQEXirLNK+cHIgkzCElIdm3reXn/nk1XCl eQequVswuLL+rPOyERv+eiykg+vjyghhzstnnYLbLlnr5qTiUKblQqBDgjgdPs5NJFJf 0oqVICIkt/djlTvQ99TxMgZMHv3GEPXBw2QoW5UzJ+fCQK2Xh9vCE4Ruoh7Lnf2smdR0 7LhJTjceU11M2s04bpmid6/4ImJpqS2FlwQNvvQ3RlJ/wa/bCV/23XpyJtFkqfrbE3pO 9BPg== X-Gm-Message-State: AOJu0YwYfMFgHSiVHtMa4wrwkP5E55AwE6HJ03QHSyds//fhIAtdkevy eZzYl8l46I+ubEco3e00AnCx+qvEB3DLbqSAOg== X-Google-Smtp-Source: AGHT+IGjcfEevp++Pd2H3pj+I2hiqHNx52lPAjqohjA2+exixRnO7jGoi61uq7cn0OD2rKiPydADTKL/EYRx+QoP2A== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:5cbf:3534:fb34:758e]) (user=almasrymina job=sendgmr) by 2002:a05:690c:338f:b0:5e3:b47a:2912 with SMTP id fl15-20020a05690c338f00b005e3b47a2912mr1365708ywb.6.1702867239583; Sun, 17 Dec 2023 18:40:39 -0800 (PST) Date: Sun, 17 Dec 2023 18:40:12 -0800 In-Reply-To: <20231218024024.3516870-1-almasrymina@google.com> Mime-Version: 1.0 References: <20231218024024.3516870-1-almasrymina@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231218024024.3516870-6-almasrymina@google.com> Subject: [RFC PATCH net-next v5 05/14] netdev: netdevice devmem allocator From: Mina Almasry To: Mathieu Desnoyers , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , 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 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kaiyuan Zhang , Pavel Begunkov , Alexei Starovoitov , "James E.J. Bottomley" , Eric Dumazet , Shuah Khan , Sumit Semwal , Mina Almasry , Willem de Bruijn , Jeroen de Borst , Daniel Borkmann , Jonathan Corbet , Helge Deller , Andrii Nakryiko , Jason Gunthorpe , Jakub Kicinski , Matt Turner , Paolo Abeni , Jesper Dangaard Brouer , Arnd Bergmann , Richard Henderson , Steven Rostedt , Shailend Chand , Ivan Kokshaysky , Harshitha Ramamurthy , Praveen Kaligineedi , Willem de Bruijn , Thomas Bogendoerfer , David Ahern , Ilias Apalodimas , David Wei , =?utf-8?q?Christian_K=C3=B6nig?= , Yunsheng Lin , Masami Hiramatsu , Shakeel Butt , "David S. Miller" Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 --- v1: - Rename devmem -> dmabuf (David). --- include/net/devmem.h | 12 ++++++++++++ include/net/netmem.h | 26 ++++++++++++++++++++++++++ net/core/dev.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/include/net/devmem.h b/include/net/devmem.h index 85ccbbe84c65..4207adadc2bb 100644 --- a/include/net/devmem.h +++ b/include/net/devmem.h @@ -67,6 +67,8 @@ struct dmabuf_genpool_chunk_owner { }; #ifdef CONFIG_DMA_SHARED_BUFFER +struct net_iov *netdev_alloc_dmabuf(struct netdev_dmabuf_binding *binding); +void netdev_free_dmabuf(struct net_iov *ppiov); void __netdev_dmabuf_binding_free(struct netdev_dmabuf_binding *binding); int netdev_bind_dmabuf(struct net_device *dev, unsigned int dmabuf_fd, struct netdev_dmabuf_binding **out); @@ -74,6 +76,16 @@ void netdev_unbind_dmabuf(struct netdev_dmabuf_binding *binding); int netdev_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx, struct netdev_dmabuf_binding *binding); #else +static inline struct net_iov * +netdev_alloc_dmabuf(struct netdev_dmabuf_binding *binding) +{ + return NULL; +} + +static inline void netdev_free_dmabuf(struct net_iov *ppiov) +{ +} + static inline void __netdev_dmabuf_binding_free(struct netdev_dmabuf_binding *binding) { diff --git a/include/net/netmem.h b/include/net/netmem.h index 45eb42d9990b..7fce2efc8707 100644 --- a/include/net/netmem.h +++ b/include/net/netmem.h @@ -14,8 +14,34 @@ 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; +} + +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 netdev_dmabuf_binding * +net_iov_binding(const struct net_iov *niov) +{ + return net_iov_owner(niov)->binding; +} + /* netmem */ struct netmem { diff --git a/net/core/dev.c b/net/core/dev.c index 2e474099267a..20ba528ef426 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -2119,6 +2119,44 @@ static int netdev_restart_rx_queue(struct net_device *dev, int rxq_idx) return err; } +struct net_iov *netdev_alloc_dmabuf(struct netdev_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->pp_magic = 0; + niov->pp = NULL; + niov->dma_addr = 0; + atomic_long_set(&niov->pp_ref_count, 0); + + netdev_dmabuf_binding_get(binding); + + return niov; +} + +void netdev_free_dmabuf(struct net_iov *niov) +{ + struct netdev_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); + + netdev_dmabuf_binding_put(binding); +} + /* Protected by rtnl_lock() */ static DEFINE_XARRAY_FLAGS(netdev_dmabuf_bindings, XA_FLAGS_ALLOC1);