🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #12264

[PATCH 04/11] dma-contiguous: pass the NUMA node to dma_alloc_contiguous()

#12264
From: Jakub Kicinski
Date: Fri, 06 Nov 2026 18:44
169 lines
15171 bytes
dma_alloc_contiguous() picks the per-numa CMA area based on the node of
the device. Let the callers pass the node in, so that dma-direct and
iommu-dma honor the node requested via dma_alloc_attrs_node() for CMA
allocations, too. dma_common_alloc_pages() keeps using the device's node.

Unlike the page allocator, CMA does not fall back to the areas of other
nodes, only to the global CMA area. The allocation may fail even though
the CMA area of the device's node still has free space. Document this.

This completes the wiring of the node, so also document which
allocations don't take it into account.

Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Reviewed-by: Björn Töpel <bjorn@kernel.org>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
v1:
 - move after the dma-direct and iommu-dma patches (Robin)
 - docs: document that CMA does not fall back to other nodes' areas (Björn)
 - docs: document which allocations ignore the node
rfc: https://lore.kernel.org/20261027022528.633359-2-kuba@kernel.org
---
 Documentation/core-api/dma-api.rst |  5 ++++-
 include/linux/dma-map-ops.h        |  5 +++--
 drivers/iommu/dma-iommu.c          |  2 +-
 kernel/dma/contiguous.c            | 12 +++++-------
 kernel/dma/direct.c                |  2 +-
 kernel/dma/mapping.c               |  7 +++++++
 kernel/dma/ops_helpers.c           |  5 +++--
 7 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/Documentation/core-api/dma-api.rst b/Documentation/core-api/dma-api.rst
index f44cc770a472..e6fe24e936ee 100644
--- a/Documentation/core-api/dma-api.rst
+++ b/Documentation/core-api/dma-api.rst
@@ -69,7 +69,10 @@ frequently by both the device and a CPU on a different node, for example
 descriptor rings serviced by that CPU.
 
 Like with kmalloc_node() the node is only a preference, the page allocator
-may fall back to other nodes if the given node is out of memory.
+may fall back to other nodes if the given node is out of memory. Note that
+contiguous memory allocator (CMA) does not: only the CMA area of the given
+node and the global CMA area are tried. The allocation may fail even though
+the CMA area of another node, e.g. that of the device, still has free space.
 
 ::
 
diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h
index afaf2c12189b..6014ec433b03 100644
--- a/include/linux/dma-map-ops.h
+++ b/include/linux/dma-map-ops.h
@@ -102,7 +102,8 @@ struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
 				       unsigned int order, bool no_warn);
 bool dma_release_from_contiguous(struct device *dev, struct page *pages,
 				 int count);
-struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp);
+struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp,
+				  int nid);
 void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
 
 void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size);
@@ -136,7 +137,7 @@ static inline bool dma_release_from_contiguous(struct device *dev,
 }
 /* Use fallback alloc() and free() when CONFIG_DMA_CMA=n */
 static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size,
-		gfp_t gfp)
+		gfp_t gfp, int nid)
 {
 	return NULL;
 }
diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index 3e3a1a987a23..b66e83cfbc5f 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -1648,7 +1648,7 @@ static void *iommu_dma_alloc_pages(struct device *dev, size_t size,
 	struct page *page = NULL;
 	void *cpu_addr;
 
-	page = dma_alloc_contiguous(dev, alloc_size, gfp);
+	page = dma_alloc_contiguous(dev, alloc_size, gfp, node);
 	if (!page) {
 		cpu_addr = alloc_pages_exact_nid(node, alloc_size, gfp);
 		if (!cpu_addr)
diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c
index 90937dc7c627..e30ec6a08af0 100644
--- a/kernel/dma/contiguous.c
+++ b/kernel/dma/contiguous.c
@@ -402,22 +402,20 @@ static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp)
  * @dev:   Pointer to device for which the allocation is performed.
  * @size:  Requested allocation size.
  * @gfp:   Allocation flags.
+ * @nid:   NUMA node to allocate the memory on.
  *
  * tries to use device specific contiguous memory area if available, or it
- * tries to use per-numa cma, if the allocation fails, it will fallback to
- * try default global one.
+ * tries to use per-numa cma of @nid, if the allocation fails, it will fallback
+ * to try default global one.
  *
  * Note that it bypass one-page size of allocations from the per-numa and
  * global area as the addresses within one page are always contiguous, so
  * there is no need to waste CMA pages for that kind; it also helps reduce
  * fragmentations.
  */
-struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
+struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp,
+				  int nid)
 {
-#ifdef CONFIG_DMA_NUMA_CMA
-	int nid = dev_to_node(dev);
-#endif
-
 	/* CMA can be used only in the context which permits sleeping */
 	if (!gfpflags_allow_blocking(gfp))
 		return NULL;
diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index a9d24d8bc77c..37c49e84dea6 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -124,7 +124,7 @@ static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
 	WARN_ON_ONCE(!PAGE_ALIGNED(size));
 
 	gfp |= dma_direct_optimal_gfp_mask(dev, &phys_limit);
-	page = dma_alloc_contiguous(dev, size, gfp);
+	page = dma_alloc_contiguous(dev, size, gfp, node);
 	if (page) {
 		if (dma_coherent_ok(dev, page_to_phys(page), size) &&
 		    (allow_highmem || !PageHighMem(page)))
diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c
index f98d4dec8649..b3d0811b1d82 100644
--- a/kernel/dma/mapping.c
+++ b/kernel/dma/mapping.c
@@ -642,6 +642,13 @@ EXPORT_SYMBOL_GPL(dma_get_required_mask);
  * Same as dma_alloc_attrs() but places the memory on @node rather than on the
  * node of @dev. Like with kmalloc_node() the node is only a preference,
  * the page allocator may fall back to other nodes if @node is out of memory.
+ * The contiguous memory allocator (CMA) does not: only the CMA area of @node
+ * and the global CMA area are tried. The allocation may fail even though the
+ * CMA area of another node, e.g. that of @dev, still has free space.
+ *
+ * Allocations served from per-device or global coherent pools, per-device CMA
+ * areas, restricted or atomic pools, arch_dma_alloc() or dma_map_ops
+ * implementations do not take @node into account.
  *
  * Return: the CPU address of the allocation, or NULL on failure.
  */
diff --git a/kernel/dma/ops_helpers.c b/kernel/dma/ops_helpers.c
index 5cdf3f60cf47..2bff02c53fb6 100644
--- a/kernel/dma/ops_helpers.c
+++ b/kernel/dma/ops_helpers.c
@@ -63,12 +63,13 @@ struct page *dma_common_alloc_pages(struct device *dev, size_t size,
 		dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
 {
 	const struct dma_map_ops *ops = get_dma_ops(dev);
+	int nid = dev_to_node(dev);
 	struct page *page;
 	phys_addr_t phys;
 
-	page = dma_alloc_contiguous(dev, size, gfp);
+	page = dma_alloc_contiguous(dev, size, gfp, nid);
 	if (!page) {
-		void *va = alloc_pages_exact_nid(dev_to_node(dev), size, gfp);
+		void *va = alloc_pages_exact_nid(nid, size, gfp);
 
 		if (!va)
 			return NULL;
-- 
2.56.0
Technical Details
Message-ID: <20261106184433.761422-5-kuba@kernel.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-358111-linux-kernel-netdev=dewy.ru
References: <20261106184433.761422-1-kuba@kernel.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-358111-linux-kernel-netdev=dewy.ru
From: kuba@kernel.org (Jakub Kicinski)
Newsgroups: linux.kernel.netdev
Subject: [PATCH 04/11] dma-contiguous: pass the NUMA node to dma_alloc_contiguous()
Date: Fri,  6 Nov 2026 10:44:26 -0800
Organization: Usenet Network
Message-ID: <20261106184433.761422-5-kuba@kernel.org>
References: <20261106184433.761422-1-kuba@kernel.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3947479"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:mYOsL6O3f9Gpbtg8qnBgA74WHtw= sha256:LAfDZxJXVWdv5crZHzUOE/hZfXBxHHYjlhGpHZVkXvc=
	sha1:5ootW0F1uf13ey+HbehvchZnlk4= sha256:q5hFYvhep/6T0LxwlUHXMnXXnurggv+0YMeRzgodP9k=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 124DA1132D4; Fri,  6 Nov 2026 21:45:46 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id z1aFhjuJnCcn;
	Fri,  6 Nov 2026 21:45:41 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-358111-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id D969011326C; Fri,  6 Nov 2026 21:45:32 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id 805982F2EA; Fri,  6 Nov 2026 18:44:54 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1793990692;
	b=gWxbMtRvGppMKbqg7uqok9pa2/EYzSVMcwWXD0iN7qEeJYnLp//RRQDs+SP+4iwomjm6
	 N/s2lPEz1GkMHic9dxqS4SFTM8zZdEdyPq9x91Y6849IZ4hUvKQ6zMzQ/SsTfUqtT2Jzp
	 ukDe03U0vERHs+q07hxnbEywPMqImn8KHZBRiYQ3k/WoDhGgR5C8yzYgk+4g0ThmXnH78
	 yS7tlNHR/wUdgJwRq+W+EuuuWBz8OfW6YmpbqFiVKJkJ1THuTWNMjkwchHxPIIP54CLAd
	 TLPLhnjLOFjtuuIJqZYfDI//lYg7SHCY98dAOyWqSMEU7qnARqnVhl0iZQO5cbvMOJg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793990692;
	h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:In-Reply-To:References:X-Mailing-List:
	 List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe:
	 List-Owner:Precedence:MIME-Version:Content-Type:
	 Content-Transfer-Encoding;
	bh=Nc2OvHqLLh27h+gzgJd0GnRqElP9jnY8ftxbnsln3X0=;
	b=vKAOzXD7FgRAQkjl2faO+g5SO8dNBxdZYK3pu8usC8MbiVXE2eq5PiTCqXamh6fh0DyW
	 qHLezBs6n5N7aFcW7IElcFd6ATK4S3KvO5Ac5D5J2eNApbh389J2Y2lPxpPC6tx5b8Z4B
	 WrkAZa3v6JuMJu4bKIbOR6iFtktdFDBMCDk97b6Fqt3w1hveR3v2s7qj7yQAvd3cswClR
	 QGz1idsbW3wdsC+l+LPJyqQi2Z+eQAbGVIJO1IJV4IZEo0yt47z1PMZIwJ4MzpWtMUXgt
	 OrnnSnnsQNWz67eaoZX1zZgEZtbKzvNfrLz85Yc8Kso2s6FG04tr6rIaHCkKZomReBQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Cqzgpb5u;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id D68AA606A33D;
	Fri,  6 Nov 2026 18:44:52 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Cqzgpb5u
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1793990692;
	b=Ujg7XTTH4JFFS5szy78sF9Eeu+jGT+gFI5kc3Fv/nT1f+Dgv9+TfAtJFuT6mnIXjzt65
	 YW3txeVsvXJUnQI0RrTyI9QmoBlvDrtIYo3keZ+wV0wpynFmgbWXgH0kn+/EtqAfDfz5W
	 I4bJ6aUCvLZWDFxqkGUnhSSbwxA3hTxXmsOROhRZC0WwiplhbGDaKs1LJGRWLWQsGfLys
	 BlbZbMWOnawb6uRJX00tEuOzY3Yz80HGT/+xJMqQO0zPm11WmIizIBsbXJQRQadvoOKeC
	 MhDcFO159pJyUVrBFvtzLh2WiAZywvznxjgfd35torac7CHqWj1bpL+4b3xPLogW5pA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793990692;
	h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer:
	 In-Reply-To:References:MIME-Version:Content-Type:
	 Content-Transfer-Encoding;
	bh=Nc2OvHqLLh27h+gzgJd0GnRqElP9jnY8ftxbnsln3X0=;
	b=SX8PCq3QToELzfTE2OJkLc6yrKomU+AZ5KhxN+UqsrJC4YdLJAkb3adv0Xgslzoba0Gu
	 EV+vrzNyZWywfeRL4Hw49C1RbbmUOaKa+pS57JhDKfx8UnL8wyw36HkNcV8NunMfwFmH+
	 KkeOWzzWV46K9P2DAxgIsjOo25vN5qCecqQHy4fr2fBqpb3rbyJelEKXcQsNzWvh4+xtH
	 ujIz+vKvsyVNDB4pdQ/if5TyhKdjIJm6RIh+YMnkLU0uE6bfxWlmxzy1ThCSdssuU56ns
	 IV8L3cecEqR1+yajxnZuS7PYBmV2q5g+0iapusocW+LGGsoYdJNRXNaMgnJACNI3d0Q==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Cqzgpb5u;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6DCF606A335;
	Fri,  6 Nov 2026 18:44:51 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 077941F008A7;
	Fri,  6 Nov 2026 18:44:49 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1793990691;
	bh=Nc2OvHqLLh27h+gzgJd0GnRqElP9jnY8ftxbnsln3X0=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References;
	b=Cqzgpb5uc19NT60V3JKr9KHXLIJiIFpaO/YPFxtm44nIDb1FxC0yhxv/Y+QZ7ei5M
	 JSmX9pX/eBWWDZWCWydSxop/6t7UvqjY2DHZ8ECD6MiMsEqNaoQgFm3qbTUMU9rz7w
	 SWA69aa3pUyCf3N2OG1ImpeFiyHTN0yq2K2Vr9Ep+tOsDdMGzeKwQ6S2JaXYwKNuIz
	 q8xNV2MO4Oo/plY5bv4CvffO2mfpjfXnmt9EX7sKnNhXe2ki5rG8PeDaGY3B2zR98w
	 KKPPtYJJwl23uDLhFrhAJpkKT79wKM8nsOHabDcsvR0mFQ5lRBgrwRyhho5i7xQRW+
	 5+LgC7GHujdMA==
X-Cc: netdev@vger.kernel.org,
	iommu@lists.linux.dev,
	joro@8bytes.org,
	will@kernel.org,
	Jakub Kicinski <kuba@kernel.org>,
	Björn Töpel <bjorn@kernel.org>
X-Mailer: git-send-email 2.56.0
X-In-Reply-To: <20261106184433.761422-1-kuba@kernel.org>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list