Received: by 2002:a05:7412:b112:b0:f9:3106:f1c0 with SMTP id az18csp17608rdb; Sun, 17 Dec 2023 18:43:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IGf6oQq7HTBj38VFPXjjBTchG7kouN796HpxCgWgKq5Nmw53vyd/Cxu5PlcrXNrMLX9Jqix X-Received: by 2002:a50:bb4c:0:b0:550:f408:6db8 with SMTP id y70-20020a50bb4c000000b00550f4086db8mr8174550ede.23.1702867387036; Sun, 17 Dec 2023 18:43:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702867387; cv=none; d=google.com; s=arc-20160816; b=QbWlDGK3hvVXUqRPwoIEJ0+BZFxGcGIZLClq4bOJQI69dv5QdsXlGGtnITceF4ip7P n/ib9Ze+q035fKlFwvnGnAizXv7FtdoK23PRGET+1G1hH8QAzPESJqZbDISy2i5yWCy7 A19wTx+Y5AGfQqPeCOxdnEEM4igJzzjrgQo4RjH3vbyG0+4NUXNV1IiOCRGM8vEUYZTB zR1JWs1hKy0VFMsnHDcjBiqR6hPVffnYUvnwyUtbrY2JdJG+M44rS1DE2L1Lu9mDlhfr LcFrfDnhcy4cEVx5eklJnp9kjS7bBrbGrQykTxKoHM/ObxLsLtsgjtp72qYsjZZfuOav R9wg== ARC-Message-Signature: i=1; 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=G5EYHEqOKeBIt6a6HG2bP5krFWMMN/+OHgJTydxcJRs=; fh=12Y/+q4Q330f164yAx6QvqXGU1NVew2yR8fswrq0I10=; b=EVZ60B4uxXGuASn40SsYQRMgoli6piILvgBY/MbK/8NXTbIsWhf057NfmCTXB+UY7N ha5no5WhvHSst1J7ahhAJESnqCfFdc9LZc52hKOhNQAOmA16Blf4DH8SfeBlMBmoBHok G6KuubYCF1rLNQP0JaWV6y0gPchAyTXfD+EsYJeE8Pcgbt7HA5xTdxz/3n22PfZW9c5J shjDq4kcjWPffPU3krQgpR0NNY7YxnhfNNJvZGzCe0XX0qteONI1sJUgG554ZRMg4U/d dgt/X4YZqyJDTKJyG9fCNY1hYPHlU13t0YSHuN6SLwtEHvk2TaMLXLpSCNP6Z/lUE3z7 Fn8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lIpbQZcf; spf=pass (google.com: domain of linux-kernel+bounces-2929-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2929-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 l17-20020a056402255100b0054c4c21cf98si10159895edb.275.2023.12.17.18.43.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 18:43:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2929-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=lIpbQZcf; spf=pass (google.com: domain of linux-kernel+bounces-2929-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2929-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 9D8CE1F2153D for ; Mon, 18 Dec 2023 02:43:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 59C2710794; Mon, 18 Dec 2023 02:40:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lIpbQZcf" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 8CE2BDF40 for ; Mon, 18 Dec 2023 02:40:40 +0000 (UTC) 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-f201.google.com with SMTP id 00721157ae682-5e2aa53a692so24022237b3.2 for ; Sun, 17 Dec 2023 18:40:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702867239; x=1703472039; 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=G5EYHEqOKeBIt6a6HG2bP5krFWMMN/+OHgJTydxcJRs=; b=lIpbQZcfuqP3JtQ401jtG4I3k87ZCKgaW15mU6wFw+45qGNLTPweH0v5X2a9yQgEEm RPSuUpkmAPz/6yf0DEuEUHCaWWBWk5y/8bLF7hvDswQPQBxo3mjuAiEt6B5cddvflN10 JODtQuJwoqODrN9hwR8vEIiL9uGgxPG8mZhEJcGxAzxs3oU/uaOFmax/Kt7uT7bEj2nh 8Aq0MqPrZTNtpCnFVhDtIMY/qIodsj6Ze5TjqyOCky7AkBDZUAGmujLSFLF5WBzJkKxD StooB142ipw2GENTpmuY9NCcHCFQqb5j2kyZCTCcijDYDMs8ZfaM8XuofZsgpWnmfo3+ Dcgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702867239; x=1703472039; 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=XhPZV4YTtQQmpzXLIhZAOY7jy/l9RRYH20WOLqsAy/sWA0F5RAf+j3Ux6/JyB/DItQ 9Xj3dPO5c4DKU+KWwPx3HhLRf8WpPcuumPOjUSdjGezfB4q5SRgPxgIMgRyCdGESxiMc 5yjVu14CRpOCBv2+t8zfRcYG/nimQES5YydbrQrN4tuFowRD95CsGB18mp868FwRHlNp S+kSqLcnKFSfEzbp9LRUsHIQEEsRyHefY+FCJDN8d1PrCaq/AzIGqN2TlrAuQnmt7R/C Bq/10Hf6DVuYVsuaQzEwPTAk4eZolYKSl3FyTBX69YascjqQs9/9AUFWQbd+qmek/ai/ /k4Q== X-Gm-Message-State: AOJu0YwnP6hjTCGBvkeTkzd3z/jd64T9v8cgjoKpB5zJtsVrViZQqxwD z/XWIiRhToWHcMlgIWBdM2y8flh0J39XHGk2lg== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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 , Jesper Dangaard Brouer , Ilias Apalodimas , Steven Rostedt , Masami Hiramatsu , Arnd Bergmann , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , 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 --- 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); -- 2.43.0.472.g3155946c3a-goog