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: 180106 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp992964dyi; Sun, 17 Dec 2023 18:43:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IFzvnUfYDflITYgGi6iqY7d/TZJ8vhqyFg72du5o7BVaTILxZb9/SxYjx2TiwV+l/4l23aA X-Received: by 2002:ac8:5f89:0:b0:425:4043:7631 with SMTP id j9-20020ac85f89000000b0042540437631mr21133881qta.89.1702867409695; Sun, 17 Dec 2023 18:43:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702867409; cv=none; d=google.com; s=arc-20160816; b=s9mnZXR1XwtExu8Ur+X4mIKWJrJzE26DoOs2nyFNgrJS8nOx1mQC/OjBuhfbwkeM/i 5wk6wN3jeEmoWyPw5Kg8Qxkgq6pwrDnqee4c61+eA5MMMWVH36P/eKxqDo+sZe1eK4Jl KOqeCwDE8rkeNpsxh893+o0li61SFouF0nAPlE5JwytLvinWp7o1PkLC1X2b3XE9S369 DxdwtMFNYOsTqB3/DPdETWOM7AL4GbZotWw/K7K7gsW8Vwzqn/bVqMW2v5kw2HkC+yFb sqGlCZfio6TWsaGCTBvARGqdAvE8Cti7V/Cg5BGP38b7aMhEl4Bv37nNlAoTV98/X8bc q3tQ== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2929-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id bb21-20020a05622a1b1500b004239b93e4f0si22987725qtb.784.2023.12.17.18.43.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 18:43:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2929-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2929-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 6D61E1C20FE5 for ; Mon, 18 Dec 2023 02:43:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 989CA10A07; Mon, 18 Dec 2023 02:40:48 +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_K=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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785585897223630378 X-GMAIL-MSGID: 1785585897223630378 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);