From patchwork Mon Dec 18 02:40:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 180110 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp993408dyi; Sun, 17 Dec 2023 18:45:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IHo2Uz+KSFjeO2F+uXVJ8OniWq9thOlUE1hSt8IAo3kAyzxPxvX44EfmHnMBr55vNEucL3m X-Received: by 2002:a05:6e02:1887:b0:35f:af09:23d1 with SMTP id o7-20020a056e02188700b0035faf0923d1mr2406508ilu.28.1702867524417; Sun, 17 Dec 2023 18:45:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702867524; cv=none; d=google.com; s=arc-20160816; b=j1RF6uKodBq0Q1mIzo7LkeekBAlu26parBJnv18YZ6wMYZRvSs7XTyIARX+3nzcO4Q 38XCfNqrZkDFwo0xLfeTSYlGu6is4J8mCm3Nm71CL4XJMbWvoNqN9NI0uoscEsm6nOn1 VBX2cQZ34puJ+5urs8OIH+sQxJvGtAD1/Cz6zGXzy3Gw5SU+Z7sqW2ZM4YGDL1PVWmaD dXzFZhBp2rOHvQP7Lo6a1rds/0N7yl8Cz8gWzMj5Xzr/Xz+CtXeY39+6O2jTUErEHBEG 2p0QeZ2JkvCo49zVy6p1J8FtgfV9h7vy+2NCLNSCRxbWXzzOqd0T7ZcEs+ClZu8swQSv H3BQ== 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=0NYFi1E6S6T+SKvDHn8CGr+ocURXqvLv0Q7fBvZhwls=; fh=12Y/+q4Q330f164yAx6QvqXGU1NVew2yR8fswrq0I10=; b=sPXp/2zgD27BRVBpeI0GrsrnqIFpHs2n/jhGNJVf3TzgkF2SZ7XLx5p0gsNMEgDM6c RE3gb4LXN6UVi8wLHzhRp8U9LO5HzMc0/VWqnjoI3y2ERDCb7LEOA97GyWm1hqB9fWJT tnPlyL2EwA2oKED5SQazKF18c19rKCBtIcZF8XUlxeAEBUPBA60F7U/os48fAHXTFBLk jRLJ0rYFxAJTK7BkF2QN4UUjkG2nPfYmnzlNyQlZaI+5tWBfkh1Cwjtsx1PXPqQscuiH GLl8sBQob125i94vRpwXNeCVuJApSB6Btn+v4AbUQJpK5VGp0KWaPKc+ZGRItsupIS0S UIcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Z+qsBgna; spf=pass (google.com: domain of linux-kernel+bounces-2932-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2932-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id p15-20020a170902b08f00b001cfd0495288si5984344plr.298.2023.12.17.18.45.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 18:45:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2932-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Z+qsBgna; spf=pass (google.com: domain of linux-kernel+bounces-2932-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2932-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 2B5A8281DB5 for ; Mon, 18 Dec 2023 02:45:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EBA28F507; Mon, 18 Dec 2023 02:40:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z+qsBgna" 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 1A3C01094F for ; Mon, 18 Dec 2023 02:40:46 +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-5d10f5bf5d9so18246957b3.3 for ; Sun, 17 Dec 2023 18:40:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702867246; x=1703472046; 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=0NYFi1E6S6T+SKvDHn8CGr+ocURXqvLv0Q7fBvZhwls=; b=Z+qsBgnasuuxs3iuFAJbsGsQM374fDMR0brqLTKWKFjaapuFgPqvOJ0VkuOnjkiNfX 6LpdsXSZBslq2OPcwfyAML+csPOy6XU0ZnrmrJrwPO5Jvt0jLzBd+f1Cu6Ba0/fgjgaw 6YO+kc/UtGdAhdDmlYS/ga7tGrCzi88z+PGLaUcOkbadMzBGrfJe2bUb8Wsa8vUb+9zl 6S8MTKmFSMxOAarfMqgq7oXguACYEy2coXygqCLjE8sKppdvygbjdygh4H/dehOT3UMB TUA4SRoD7uE3e7iAIXj/byARQa+xlQNUe/m7cqlme/tKauksrlu79KBLP4QTxe31JsmO oIkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702867246; x=1703472046; 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=0NYFi1E6S6T+SKvDHn8CGr+ocURXqvLv0Q7fBvZhwls=; b=fNbL5tjugSIRt/uL/ptNCCZ++5CSDmEl+XVOfjNElYkv4WWtG/0+9L0/FFz9+ZHiGL LN7WO9XvRBG/c7nshZw+VNeYbO1HocabeOwdJirhmwqG4Bn+JRQSzK0mES1n+lWMvQ+6 rKiZUmQw9Kr24wIl9GRyqo76GTwYIEFF8qbuBaF1C7z3a2xrwajaId1bkUhwB4Bcmti+ HzfZC/IiaH1P/iskIbmHZUNmbM10a+H3PpXZP3NxBEEnvBJEjcd1hVHKn+fmCT+Gg9Co +Df77F7LMj/foRu+sVbxFj0XTgQ8ZtlG+eLmzgqZaCLyHLeZZpLJTfYVw83ES3hjLjbG hAjw== X-Gm-Message-State: AOJu0YzNiZdr7tiATs1EkJagfiZ3il86QoIoFwd0TE4TCwr5Vt2EpUnp BtsAJIOt0PfdHauQKzCQqumY5/oyj+aWLqS+tQ== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:5cbf:3534:fb34:758e]) (user=almasrymina job=sendgmr) by 2002:a05:690c:1e:b0:5d7:545e:3bd0 with SMTP id bc30-20020a05690c001e00b005d7545e3bd0mr1609684ywb.4.1702867246059; Sun, 17 Dec 2023 18:40:46 -0800 (PST) Date: Sun, 17 Dec 2023 18:40:15 -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-9-almasrymina@google.com> Subject: [RFC PATCH net-next v5 08/14] memory-provider: dmabuf devmem memory provider 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: 1785586017411128861 X-GMAIL-MSGID: 1785586017411128861 Implement a memory provider that allocates dmabuf devmem in the form of net_iov. The provider receives a reference to the struct netdev_dmabuf_binding via the pool->mp_priv pointer. The driver needs to set this pointer for the provider in the net_iov. The provider obtains a reference on the netdev_dmabuf_binding which guarantees the binding and the underlying mapping remains alive until the provider is destroyed. Usage of PP_FLAG_DMA_MAP is required for this memory provide such that the page_pool can provide the driver with the dma-addrs of the devmem. Support for PP_FLAG_DMA_SYNC_DEV is omitted for simplicity & p.order != 0. Signed-off-by: Willem de Bruijn Signed-off-by: Kaiyuan Zhang Signed-off-by: Mina Almasry --- v2: - Disable devmem for p.order != 0 v1: - static_branch check in page_is_page_pool_iov() (Willem & Paolo). - PP_DEVMEM -> PP_IOV (David). - Require PP_FLAG_DMA_MAP (Jakub). memory provider --- include/net/netmem.h | 14 ++++++ include/net/page_pool/types.h | 2 + net/core/page_pool.c | 93 +++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) diff --git a/include/net/netmem.h b/include/net/netmem.h index 7557aecc0f78..ab3824b7b789 100644 --- a/include/net/netmem.h +++ b/include/net/netmem.h @@ -97,6 +97,20 @@ static inline bool netmem_is_net_iov(const struct netmem *netmem) #endif } +static inline struct net_iov *netmem_to_net_iov(struct netmem *netmem) +{ + if (netmem_is_net_iov(netmem)) + return (struct net_iov *)((unsigned long)netmem & ~NET_IOV); + + DEBUG_NET_WARN_ON_ONCE(true); + return NULL; +} + +static inline struct netmem *net_iov_to_netmem(struct net_iov *niov) +{ + return (struct netmem *)((unsigned long)niov | NET_IOV); +} + static inline struct page *netmem_to_page(struct netmem *netmem) { if (WARN_ON_ONCE(netmem_is_net_iov(netmem))) diff --git a/include/net/page_pool/types.h b/include/net/page_pool/types.h index 433ae9ae658b..3ddef7d7ba74 100644 --- a/include/net/page_pool/types.h +++ b/include/net/page_pool/types.h @@ -134,6 +134,8 @@ struct memory_provider_ops { bool (*release_page)(struct page_pool *pool, struct netmem *netmem); }; +extern const struct memory_provider_ops dmabuf_devmem_ops; + struct page_pool { struct page_pool_params_fast p; diff --git a/net/core/page_pool.c b/net/core/page_pool.c index 173158a3dd61..231840112956 100644 --- a/net/core/page_pool.c +++ b/net/core/page_pool.c @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -20,12 +21,15 @@ #include #include #include +#include +#include #include #include "page_pool_priv.h" DEFINE_STATIC_KEY_FALSE(page_pool_mem_providers); +EXPORT_SYMBOL(page_pool_mem_providers); #define DEFER_TIME (msecs_to_jiffies(1000)) #define DEFER_WARN_INTERVAL (60 * HZ) @@ -175,6 +179,7 @@ static void page_pool_producer_unlock(struct page_pool *pool, static int page_pool_init(struct page_pool *pool, const struct page_pool_params *params) { + struct netdev_dmabuf_binding *binding = NULL; unsigned int ring_qsize = 1024; /* Default */ int err; @@ -237,6 +242,14 @@ static int page_pool_init(struct page_pool *pool, /* Driver calling page_pool_create() also call page_pool_destroy() */ refcount_set(&pool->user_cnt, 1); + if (pool->p.queue) + binding = READ_ONCE(pool->p.queue->binding); + + if (binding) { + pool->mp_ops = &dmabuf_devmem_ops; + pool->mp_priv = binding; + } + if (pool->mp_ops) { err = pool->mp_ops->init(pool); if (err) { @@ -1055,3 +1068,83 @@ void page_pool_update_nid(struct page_pool *pool, int new_nid) } } EXPORT_SYMBOL(page_pool_update_nid); + +/*** "Dmabuf devmem memory provider" ***/ + +static int mp_dmabuf_devmem_init(struct page_pool *pool) +{ + struct netdev_dmabuf_binding *binding = pool->mp_priv; + + if (!binding) + return -EINVAL; + + if (!(pool->p.flags & PP_FLAG_DMA_MAP)) + return -EOPNOTSUPP; + + if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV) + return -EOPNOTSUPP; + + if (pool->p.order != 0) + return -E2BIG; + + netdev_dmabuf_binding_get(binding); + return 0; +} + +static struct netmem *mp_dmabuf_devmem_alloc_pages(struct page_pool *pool, + gfp_t gfp) +{ + struct netdev_dmabuf_binding *binding = pool->mp_priv; + struct netmem *netmem; + struct net_iov *niov; + dma_addr_t dma_addr; + + niov = netdev_alloc_dmabuf(binding); + if (!niov) + return NULL; + + dma_addr = net_iov_dma_addr(niov); + + netmem = net_iov_to_netmem(niov); + + page_pool_set_pp_info(pool, netmem); + + if (page_pool_set_dma_addr_netmem(netmem, dma_addr)) + goto err_free; + + pool->pages_state_hold_cnt++; + trace_page_pool_state_hold(pool, netmem, pool->pages_state_hold_cnt); + return netmem; + +err_free: + netdev_free_dmabuf(niov); + return NULL; +} + +static void mp_dmabuf_devmem_destroy(struct page_pool *pool) +{ + struct netdev_dmabuf_binding *binding = pool->mp_priv; + + netdev_dmabuf_binding_put(binding); +} + +static bool mp_dmabuf_devmem_release_page(struct page_pool *pool, + struct netmem *netmem) +{ + WARN_ON_ONCE(!netmem_is_net_iov(netmem)); + + page_pool_clear_pp_info(netmem); + + netdev_free_dmabuf(netmem_to_net_iov(netmem)); + + /* We don't want the page pool put_page()ing our net_iovs. */ + return false; +} + +const struct memory_provider_ops dmabuf_devmem_ops = { + .init = mp_dmabuf_devmem_init, + .destroy = mp_dmabuf_devmem_destroy, + .alloc_pages = mp_dmabuf_devmem_alloc_pages, + .release_page = mp_dmabuf_devmem_release_page, +}; +EXPORT_SYMBOL(dmabuf_devmem_ops);