From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 5A76A1B941 for ; Tue, 5 Mar 2024 02:02:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709604135; cv=none; b=W1AIjyN5kwt0IgeVnGdP/RhyrdW5HTiznLd6GT04Lvp1P6Gjc3ZFd2agJ4C+j6clHp+ojcHZYK4ThPmRa1VF+QCLQq1CQT5q0N0ZU3U9vDVhFCVPF20Ihs/WySDwh4nKpFVDD5zqatkCo70mLHbnGMIkE+NaNXJ9w9U3WXw+1iQ= 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=p2FS9YnY; arc=none smtp.client-ip=209.85.219.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="p2FS9YnY" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc3645a6790so357113276.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=1709604131; x=1710208931; 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=p2FS9YnYQT3dALGaGB3DMbn5Ul/fFVboaF/CR7TAbJ7DQCmHweHw0rCc5yEN9XglGv 56U93gs1b+lpp4Ii4bNJV0Q+0FRU7R4rfSD8EAzvfu373//pKBj95BOr+9Kknjztky8R FTF/gztKr+rwyZqrSeQdjSqwI+L4dlPQKEwVTpk8z6GFxPA1S7z3XBzBdkmF2BSBIE/I QMxNOyk9ckGGRyrpMi6KjfOfl0EueNZH4BNDjes14jTH0sJvq30w7N9/J/csS8YHkHV7 VX7CKwRWiO3Qxq00i4F46u41vI7N5x1VVKeXWEvKk2EoAoLQZyuademRDx8ipfNrBRYG qiaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709604131; x=1710208931; 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=C3p2bhGVP9op1hv16M8i7hZsBMqpszo3fVbZwVBcX2xAECAt2co8+ErZy7hPr1t4Rj h+GbLmIr241symO56toKIcSJsuyBC+nuzXhqbKUir+Zqc458e/UIVKS/U627158HpUbs XwNLyV6uUfC2iCSC6ZNaRhrRxaLR+cZb0Lq2EeuuL3riB0m6oMS/D45yhUIbFn3un5YH DL/XcmqcNPJy2T7JGYAugRR+h0Yrx3GOwEBn29RaFBHWIpGNZLWBVD9h8sRmG2yuS9xc cNJhaIM9g1OYHV5oUQz8vljKxSyPkD380998s51SUn50UzlkJzw/WD1JaOdRcMr2PdGf xDqw== X-Gm-Message-State: AOJu0YxVErrCIBo5e6szC6Ujga/L3LXR3plzJ2DFR9DMNgPqeJK/quCR mULLcFLCwONO+ynwF6nplpfobVIxIIwVy2DXEBQMD2nFhf/DqKJEFLvp833WHocNVkshpxSvWi9 U5NlEsi+lL9YhNoJrn60NvIHaaRS1rN+EF+ydAkj6qXSoHJ2fT2PV1bZCWIeeviPpKDn4N9UwpL CKkdTHSckJYcTh4x/GAAGtFopl7f/ungp1a9IUfYVSuVvGgkwgDiKJmEHR6P0= X-Google-Smtp-Source: AGHT+IFp6UEtUOWoZdRym40bjXyf80/9kbOdm1B3G9vAhJbMjUPJhOdTewfBOuFMN7wsO34DBHuDxFG4/drkODwMOA== 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: netdev@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