Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp125388lqs; Mon, 4 Mar 2024 18:05:12 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXj08gvUHTcqZ4OaAzEGVyt42wHL9zZlrN/e0ZTOrB/jdAX2GG7VCvlEQPNyWfvuWc5H+9xA+8txAdvpq9G7vShLh/IEmt5qe1ARjGbrg== X-Google-Smtp-Source: AGHT+IHFntnU7gc3vbnELLOBPfO+w6gfs1S+ZnjC5h/gJZlmt+FhTO57KT/5/c3OEx5OlNlHo+s0 X-Received: by 2002:a05:6402:5c2:b0:567:6a66:d103 with SMTP id n2-20020a05640205c200b005676a66d103mr2099907edx.0.1709604311869; Mon, 04 Mar 2024 18:05:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709604311; cv=pass; d=google.com; s=arc-20160816; b=MHuiWiZjYQDXD79AVznzdqnnLiTV0948xPMTXO6P4zcWrODDnsuFyNDn7+xnbZSP3t eWn77WE4iQ6i8A6pveIK60glqP7s0W3v6nCP9h6PORHYM8oph/vaePmfjk1jkbfnKo5S 2yoIH641VjE9SrMEm2s6l3aAk6qGQvgPK1mbmjNnEixo8vyDDcbR7PE3yQlmH3+Um/D8 mtu4wm7zPZfF58Sj/XCFuoyJn/M2Yp+ncDguzQo7c0HYzG3cxTWeqDiXQKyZ6UNfJrpD MuHbNipSz7wgtsshkBktCJW2HkWaLFjZOjU3bMxaYkHLZNv5x2sK7XE6UeEK1+mKYSwx P/+w== 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=GYNPKIZQ281lVVO6ngxcCFyACEH85a0Tth4BHfTjTEU=; fh=zM6aK+o5LDwTrtG8J30rKJmuI6wh83wzq8hiK/E6eww=; b=MEWVlIWQ7Z/8aSZhJrGTXINMYRja9IDQLo5UEBWrakc6KbEmp1cJB+mbONBi5hCtLz ntXGnzula5RiawkRsBXSESCOeABfS6nT1jg3Q6d2WLJs4fnu5aV3zZun6NzVR6F/60LG Fy5HT7vUHqa4fpyR6hCaFo2F+xMqJDm64mIjYUYEYumhrg+tt9PKmSToUsYzsLB2KizK 8oxWkjBr5NMP9mv31X5LI+XXTh7MUmM/JYlXadwIK7WppyhWbeJP1zKhl55na4b843jC oJ8QsleWCCRZhjIVHbdx4l3GikWb2JBqWNMrfVgjBgXFTuHR7JqyWKLUHPAKJq2LeL0t aj7Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=AfKALwNM; 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-91527-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91527-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. [147.75.80.249]) by mx.google.com with ESMTPS id t23-20020a50d717000000b005669d6c06e1si4470250edi.508.2024.03.04.18.05.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 18:05:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-91527-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=AfKALwNM; 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-91527-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91527-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 6A5AF1F25090 for ; Tue, 5 Mar 2024 02:05:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B21937E769; Tue, 5 Mar 2024 02:02:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AfKALwNM" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 E966129437 for ; Tue, 5 Mar 2024 02:02:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709604135; cv=none; b=rcCo2JApje1zbYX35iFPhWUAGscUCaxTwf2FBxqX0VCmdxHOf3XPsv5U9e8eccK31A5eNTUKgZYOi5DDgvz6aWTR+GnN+dKZFYtoWrx62Yr78szfgwUwwvpTzZkxcQRUeas2bXrBdeJVy1P/lsxYS1rSBgsr22JvdTlFHXNx2Wk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709604135; c=relaxed/simple; bh=cvQhiIpC3y+RS9Gm99XbZoZlqQ7K9itfikVyQItkfww=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eNrxaHtQV1x2qpAzKd6qdK9R5KpNSxBiDCVttqgODqweflQc6Isfs9OkIfFwVk+1e11PchFZ2BYVvXafFoDcFwNFa4T/WoLPEuuYC/oVAnjJyT0OxmLaFPUEXriO7BMcnshv2YUmMK0Jri78lHLchHeAqiQcsHhOljU5YtsyLME= 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=AfKALwNM; arc=none smtp.client-ip=209.85.219.201 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-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc3645a6790so357105276.0 for ; Mon, 04 Mar 2024 18:02:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709604130; x=1710208930; 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=GYNPKIZQ281lVVO6ngxcCFyACEH85a0Tth4BHfTjTEU=; b=AfKALwNMjAAVlDN+SvPY8KglsT5I76vqxr0bHIbXyU+wTdjgM+Z7sMNwzvS5zYxwcd 39LBe/XaH+3Xdd2o9ST323JA8QBylAl+49dz1wrf7ruYNgVsTzXpAA3M44Q/0kkSeFQZ XFf6wQvF2ppRphiGYJ71tlEBOo8i7RET1aLRI85pRsmOFnCnwO+dokpJmzrGwbInsp54 +7gI9GnpZcT80vrKeam638irWwUbQRiOB+soGiYmGkQZNDx1Is/sW9ELvcbX88KMWnCl DQcaqbizaJJguAtAyU3L/8dglxpJd+vwBRTzWQSzOy4aaPoy7foT5HaXVUp+d8oRkIiy dSHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709604130; x=1710208930; 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=GYNPKIZQ281lVVO6ngxcCFyACEH85a0Tth4BHfTjTEU=; b=B3B6P8SIJeMeAAOE2Qa6S6V2JsiildJieqMkCbJdzlwVOnEn3VjDH4mpQgfS23YAzw weYcajZAFXswh+MHwwLCwVT6Zs1bgxWB3o32piL8DjVo9SOrKUtYZ2Oj/Nc+mzb9yN/7 B+srWO8vCf/QR9rhRqzQCqu/BdUnigQrvSmPWM4st7bRMH8F5QDd634V/b/CnHpO0Aas HPflGFYnio+r2MR5Si+UMbXtGkZtGVzGb+7/cweoXLAApkJD3cDsJUIKUR1e87jk5nO3 4cRaaWXwayv9i91U7OlfNoX3dJybaMyrp90a9nyLibEse6mpo6qij4JBn2sYwP3Qt6qv JlrA== X-Forwarded-Encrypted: i=1; AJvYcCWmXANywf9niFaUfU2jMC6PsrPMbDLXpX00aOGa8hJJE/34fGveoBawuEnPQBo6s9xEtOWTyLlXLeoZTGabPPZLxbdDgzXeOaDVDRrW X-Gm-Message-State: AOJu0YzGwmlg5Azzpv5mpt+5KvAB7Uo2+WulaxswbhKmQAdXefb73whS CaoaJveSFITRIiATfVurqOsuHWvboIoxPwsjqPOluKGclo14T2mcdkEljtBgSYOj9yrAL0LSRiz KDUZCiW2dU5PsJlSy4m4yQg== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:b614:914c:63cd:3830]) (user=almasrymina job=sendgmr) by 2002:a05:6902:2492:b0:dcb:b9d7:2760 with SMTP id ds18-20020a056902249200b00dcbb9d72760mr2965477ybb.13.1709604130576; Mon, 04 Mar 2024 18:02:10 -0800 (PST) Date: Mon, 4 Mar 2024 18:01:41 -0800 In-Reply-To: <20240305020153.2787423-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: <20240305020153.2787423-1-almasrymina@google.com> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog Message-ID: <20240305020153.2787423-7-almasrymina@google.com> Subject: [RFC PATCH net-next v6 06/15] 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 , David Ahern , Willem de Bruijn , Shuah Khan , Sumit Semwal , "=?UTF-8?q?Christian=20K=C3=B6nig?=" , Pavel Begunkov , David Wei , Jason Gunthorpe , Yunsheng Lin , Shailend Chand , Harshitha Ramamurthy , Shakeel Butt , Jeroen de Borst , Praveen Kaligineedi , Willem de Bruijn , Kaiyuan Zhang 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 --- 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 | 12 ++++++++++++ include/net/netmem.h | 40 ++++++++++++++++++++++++++++++++++++++++ net/core/devmem.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 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 72e932a1a948..ca17ea1d33f8 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 netdev_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 779ad990971e..57d3a1f223ef 100644 --- a/net/core/devmem.c +++ b/net/core/devmem.c @@ -93,6 +93,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); -- 2.44.0.rc1.240.g4c46232300-goog