Skip to contentSkip to conversation

alpha: enable DMA CMA support

PATCH Superseded Magnus Lindholm 4 participants 10 messages Nov 15 – Nov 27

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Magnus Lindholm

author maintainer
#0 lore raw

v3 cover alpha: enable DMA CMA support

Changes since v2, following Matt Turner's review:

- Initialize jump labels before parsing early parameters, so handlers can safely change static keys. Carry Matt's Reviewed-by on patch 1.
- Page-align sizes in both coherent allocation and free. The CMA helper assumes a page-aligned size; truncating an unaligned request can cause an undersized allocation, an overwrite, and release of unowned pages.
- Change the failure message to "allocation failed".

V2 was boot-tested on a three-CPU AlphaServer ES40 with cma=64M, including 32 MiB, 24 KiB and 8 KiB allocation tests and GFP_ATOMIC bypass checks. All of those sizes were page multiples, so they did not exercise the alignment bug. Those results are not validation of v3; build and runtime validation of this revision remain pending.

Planned regression cases include 12 KiB and 20 KiB requests, checking zeroing, mapped size, and allocation/release accounting against the page-rounded size on each CPU. Repeat the existing static-key tests on a combined kernel with jump-label v3.

Reservation policy is unchanged: prefer top-down placement below the direct-map and usable RAM limits, with generic cma= range overrides. Other reservation-size boots and no-IOMMU testing remain outstanding.

Magnus Lindholm (2):
alpha: parse early parameters before reserving contiguous memory
alpha: enable contiguous memory allocation for coherent DMA

 .../io/dma-contiguous/arch-support.txt        |  2 +-
 arch/alpha/Kconfig                            |  1 +
 arch/alpha/kernel/pci_iommu.c                 | 19 ++++++++++++------
 arch/alpha/kernel/setup.c                     | 20 +++++++++++++++++++
 4 files changed, 35 insertions(+), 7 deletions(-)

base-commit: 0b4cadbe7c39d830dec35c4a2655483de0dae568

Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v3 1/2 alpha: parse early parameters before reserving contiguous memory

Run parse_early_param() in setup_arch(), after machine initialization and before further memory reservations. This lets the subsequent CMA reservation honor cma= before start_kernel() reaches its normal call.

Initialize jump labels first so early handlers can safely change static keys. The later jump_label_init() call in start_kernel() is harmless.

The parser uses boot_command_line and guards against repeated calls. This moves all early parameter handlers, not just the CMA handler.

---
 arch/alpha/kernel/setup.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/alpha/kernel/setup.c +5 -0
index 9fda01b73b18..f21a7c8f5213 100644
@@ -25,6 +25,7 @@
 #include <linux/cpu.h>
 #include <linux/errno.h>
 #include <linux/init.h>
+#include <linux/jump_label.h>
 #include <linux/string.h>
 #include <linux/ioport.h>
 #include <linux/panic_notifier.h>
@@ -579,6 +580,10 @@ setup_arch(char **cmdline_p)
 	if (alpha_mv.init_arch)
 		alpha_mv.init_arch();
 
+	/* Early options may change static keys and configure DMA CMA. */
+	jump_label_init();
+	parse_early_param();
+
 	/* Reserve standard resources.  */
 	reserve_std_resources();
 
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH v3 2/2 alpha: enable contiguous memory allocation for coherent DMA

In reply to Magnus Lindholm

Select HAVE_DMA_CONTIGUOUS and reserve a default CMA area after the machine has initialized its DMA windows. Prefer top-down placement below the direct-map and usable RAM limits. Skip the default reservation when no direct-map window exists. Explicit cma= ranges retain their generic override semantics; top-down placement is a preference, not a hard lower bound excluding the first 16 MiB.

Use dma_alloc_contiguous(), which rejects non-blocking callers, before falling back to alloc_pages_exact(). Pair both paths with dma_free_contiguous(), whose buddy fallback uses free_pages_exact(). Skip CMA on the GFP_DMA retry after a mapping failure so it cannot return the same unsuitable CMA pages. Retain zeroing for CMA memory.

Round the size up to a page boundary in both allocation and free. CMA allocates size >> PAGE_SHIFT pages, so an unaligned size could otherwise allocate too few pages and overrun the allocation during zeroing. Keep the mapping and unmapping lengths consistent with the aligned size.

With CONFIG_DMA_CMA=y, the default CMA_SIZE_MBYTES setting reserves 16 MiB without a cma= argument. cma=0 disables the default area.

---
 .../io/dma-contiguous/arch-support.txt        |  2 +-
 arch/alpha/Kconfig                            |  1 +
 arch/alpha/kernel/pci_iommu.c                 | 19 +++++++++++++------
 arch/alpha/kernel/setup.c                     | 15 +++++++++++++++
 4 files changed, 30 insertions(+), 7 deletions(-)
Documentation/features/io/dma-contiguous/arch-support.txt +1 -1
index 3c6ce35d704f..6cd205a991f6 100644
@@ -6,7 +6,7 @@
     -----------------------
     |         arch |status|
     -----------------------
-    |       alpha: | TODO |
+    |       alpha: |  ok  |
     |         arc: | TODO |
     |         arm: |  ok  |
     |       arm64: |  ok  |
arch/alpha/Kconfig +1 -0
index 6ccb2d230326..cddc1f3af209 100644
@@ -30,6 +30,7 @@ config ALPHA
 	select HAVE_ARCH_COMPILER_H
 	select HAVE_ASM_MODVERSIONS
 	select HAVE_DEBUG_KMEMLEAK
+	select HAVE_DMA_CONTIGUOUS
 	select HAVE_GUP_FAST
 	select HAVE_KERNEL_BZIP2
 	select HAVE_KERNEL_GZIP
arch/alpha/kernel/pci_iommu.c +13 -6
index 955b6ca61627..19aa5c05de2b 100644
@@ -408,15 +408,20 @@ static void *alpha_pci_alloc_coherent(struct device *dev, size_t size,
 {
 	struct pci_dev *pdev = alpha_gendev_to_pci(dev);
 	void *cpu_addr;
-	long order = get_order(size);
+	struct page *page;
 
+	size = PAGE_ALIGN(size);
 	gfp &= ~GFP_DMA;
 
 try_again:
-	cpu_addr = (void *)__get_free_pages(gfp | __GFP_ZERO, order);
+	/* A low-memory retry must not return the same unsuitable CMA pages. */
+	page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);
+	if (page)
+		cpu_addr = page_address(page);
+	else
+		cpu_addr = alloc_pages_exact(size, gfp | __GFP_ZERO);
 	if (! cpu_addr) {
-		printk(KERN_INFO "pci_alloc_consistent: "
-		       "get_free_pages failed from %ps\n",
+		pr_info("pci_alloc_consistent: allocation failed from %ps\n",
 			__builtin_return_address(0));
 		/* ??? Really atomic allocation?  Otherwise we could play
 		   with vmalloc and sg if we can't find contiguous memory.  */
@@ -426,7 +431,7 @@ static void *alpha_pci_alloc_coherent(struct device *dev, size_t size,
 
 	*dma_addrp = pci_map_single_1(pdev, virt_to_phys(cpu_addr), size, 0);
 	if (*dma_addrp == DMA_MAPPING_ERROR) {
-		free_pages((unsigned long)cpu_addr, order);
+		dma_free_contiguous(dev, virt_to_page(cpu_addr), size);
 		if (alpha_mv.mv_pci_tbi || (gfp & GFP_DMA))
 			return NULL;
 		/* The address doesn't fit required mask and we
@@ -452,8 +457,10 @@ static void alpha_pci_free_coherent(struct device *dev, size_t size,
 				    unsigned long attrs)
 {
 	struct pci_dev *pdev = alpha_gendev_to_pci(dev);
+
+	size = PAGE_ALIGN(size);
 	dma_unmap_single(&pdev->dev, dma_addr, size, DMA_BIDIRECTIONAL);
-	free_pages((unsigned long)cpu_addr, get_order(size));
+	dma_free_contiguous(dev, virt_to_page(cpu_addr), size);
 
 	DBGA2("pci_free_consistent: [%llx,%zx] from %ps\n",
 	      dma_addr, size, __builtin_return_address(0));
arch/alpha/kernel/setup.c +15 -0
index f21a7c8f5213..f40a87b30a7c 100644
@@ -31,6 +31,7 @@
 #include <linux/panic_notifier.h>
 #include <linux/platform_device.h>
 #include <linux/memblock.h>
+#include <linux/dma-map-ops.h>
 #include <linux/pci.h>
 #include <linux/seq_file.h>
 #include <linux/root_dev.h>
@@ -584,6 +585,20 @@ setup_arch(char **cmdline_p)
 	jump_label_init();
 	parse_early_param();
 
+	/*
+	 * Prefer higher memory within the direct-map window, rather than
+	 * consuming scarce low memory with a bottom-up CMA reservation.
+	 * Explicit cma= ranges retain the generic command-line semantics.
+	 */
+	if (__direct_map_size) {
+		bool bottom_up = memblock_bottom_up();
+
+		memblock_set_bottom_up(false);
+		dma_contiguous_reserve(min_t(phys_addr_t, __direct_map_size,
+					     PFN_PHYS(max_low_pfn)));
+		memblock_set_bottom_up(bottom_up);
+	}
+
 	/* Reserve standard resources.  */
 	reserve_std_resources();
 
Signature · 1 line
2.43.0
maintainer Reviewed-by on v3 2/2
#3 lore raw

Re: [PATCH v3 2/2] alpha: enable contiguous memory allocation for coherent DMA

Quoted 3 lines · Magnus Lindholm

Use dma_alloc_contiguous(), which rejects non-blocking callers, before falling back to alloc_pages_exact(). Pair both paths with dma_free_contiguous(), whose buddy fallback uses free_pages_exact().

Right, and the !CONFIG_DMA_CMA stub does the same since 641a9b9cc697 ("dma-mapping: use exact allocation for DMA pages"), so the pairing holds either way. Just something to keep in mind if this ever gets backported past that commit.

Quoted 2 lines
+     size = PAGE_ALIGN(size);
      gfp &= ~GFP_DMA;

Thanks, this and the matching change in alpha_pci_free_coherent() look right. The CMA alloc and release counts now agree, and the memset() covers the whole aligned size.

arch/alpha/kernel/setup.c setup_arch(char **cmdline_p) · line 600

Quoted 3 lines
arch/alpha/kernel/setup.c +2 -0
+     if (__direct_map_size) {
...
+     }

Looks good. Every remaining core sets a nonzero __direct_map_size, so the check is purely defensive today, which is fine.

I build-tested the series on top of 0b4cadbe7c39 with ALPHA_GENERIC=y, SMP=y, CMA=y, DMA_CMA=y and W=1, and saw no new warnings in pci_iommu.c or setup.c. I haven't booted it.

#4 lore raw

Re: [PATCH v3 2/2] alpha: enable contiguous memory allocation for coherent DMA

In reply to Magnus Lindholm

Hello,

Quoted 84 lines · Magnus Lindholm

Select HAVE_DMA_CONTIGUOUS and reserve a default CMA area after the machine has initialized its DMA windows. Prefer top-down placement below the direct-map and usable RAM limits. Skip the default reservation when no direct-map window exists. Explicit cma= ranges retain their generic override semantics; top-down placement is a preference, not a hard lower bound excluding the first 16 MiB.

Use dma_alloc_contiguous(), which rejects non-blocking callers, before falling back to alloc_pages_exact(). Pair both paths with dma_free_contiguous(), whose buddy fallback uses free_pages_exact(). Skip CMA on the GFP_DMA retry after a mapping failure so it cannot return the same unsuitable CMA pages. Retain zeroing for CMA memory.

Round the size up to a page boundary in both allocation and free. CMA allocates size >> PAGE_SHIFT pages, so an unaligned size could otherwise allocate too few pages and overrun the allocation during zeroing. Keep the mapping and unmapping lengths consistent with the aligned size.

With CONFIG_DMA_CMA=y, the default CMA_SIZE_MBYTES setting reserves 16 MiB without a cma= argument. cma=0 disables the default area.

---
 .../io/dma-contiguous/arch-support.txt        |  2 +-
 arch/alpha/Kconfig                            |  1 +
 arch/alpha/kernel/pci_iommu.c                 | 19 +++++++++++++------
 arch/alpha/kernel/setup.c                     | 15 +++++++++++++++
 4 files changed, 30 insertions(+), 7 deletions(-)
Documentation/features/io/dma-contiguous/arch-support.txt +1 -1
index 3c6ce35d704f..6cd205a991f6 100644
@@ -6,7 +6,7 @@
     -----------------------
     |         arch |status|
     -----------------------
-    |       alpha: | TODO |
+    |       alpha: |  ok  |
     |         arc: | TODO |
     |         arm: |  ok  |
     |       arm64: |  ok  |
arch/alpha/Kconfig +1 -0
index 6ccb2d230326..cddc1f3af209 100644
@@ -30,6 +30,7 @@ config ALPHA
 	select HAVE_ARCH_COMPILER_H
 	select HAVE_ASM_MODVERSIONS
 	select HAVE_DEBUG_KMEMLEAK
+	select HAVE_DMA_CONTIGUOUS
 	select HAVE_GUP_FAST
 	select HAVE_KERNEL_BZIP2
 	select HAVE_KERNEL_GZIP
arch/alpha/kernel/pci_iommu.c +9 -4
index 955b6ca61627..19aa5c05de2b 100644
@@ -408,15 +408,20 @@ static void *alpha_pci_alloc_coherent(struct device *dev, size_t size,
 {
 	struct pci_dev *pdev = alpha_gendev_to_pci(dev);
 	void *cpu_addr;
-	long order = get_order(size);
+	struct page *page;
 
+	size = PAGE_ALIGN(size);
 	gfp &= ~GFP_DMA;
 
 try_again:
-	cpu_addr = (void *)__get_free_pages(gfp | __GFP_ZERO, order);
+	/* A low-memory retry must not return the same unsuitable CMA pages. */
+	page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);
+	if (page)
+		cpu_addr = page_address(page);
+	else
+		cpu_addr = alloc_pages_exact(size, gfp | __GFP_ZERO);
 	if (! cpu_addr) {
-		printk(KERN_INFO "pci_alloc_consistent: "
-		       "get_free_pages failed from %ps\n",
+		pr_info("pci_alloc_consistent: allocation failed from %ps\n",
 			__builtin_return_address(0));
 		/* ??? Really atomic allocation?  Otherwise we could play
 		   with vmalloc and sg if we can't find contiguous memory.  */

arch/alpha/kernel/pci_iommu.c static void *alpha_pci_alloc_coherent(struct device *dev, size_t size, · line 434

Quoted 5 lines
arch/alpha/kernel/pci_iommu.c +1 -1
@@ -426,7 +431,7 @@ static void *alpha_pci_alloc_coherent(struct device *dev, size_t size,
 
 	*dma_addrp = pci_map_single_1(pdev, virt_to_phys(cpu_addr), size, 0);
 	if (*dma_addrp == DMA_MAPPING_ERROR) {
-		free_pages((unsigned long)cpu_addr, order);
+		dma_free_contiguous(dev, virt_to_page(cpu_addr), size);

On next-20261123 I get a build error on an ARCH=alpha allmodconfig build

arch/alpha/kernel/pci_iommu.c: In function ‘alpha_pci_alloc_coherent’:
arch/alpha/kernel/pci_iommu.c:418:41: error: too few arguments to function ‘dma_alloc_contiguous’; expected 4, have 3

  418 |         page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);
      |                                         ^~~~~~~~~~~~~~~~~~~~
In file included from arch/alpha/kernel/pci_iommu.c:14:
include/linux/dma-map-ops.h:105:14: note: declared here
  105 | struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp,
      |              ^~~~~~~~~~~~~~~~~~~~

which seems to be a conflict between

	next-20261123~159^2 = f03df6019b56 ("alpha: enable contiguous memory allocation for coherent DMA")

from
https://git.kernel.org/pub/scm/linux/kernel/git/lindholm/alpha.git for-next

and

	next-20261123~104^2~23^2~7 = 31f7e12d621b ("dma-contiguous: pass the NUMA node to dma_alloc_contiguous()")

from
https://git.kernel.org/pub/scm/linux/kernel/git/kuba/linux.git dma_alloc_coherent_node

I guess the right thing to do is to pull

https://git.kernel.org/pub/scm/linux/kernel/git/kuba/linux.git dma_alloc_coherent_node

into the alpha tree and fix the offending function call in the merge commit.

Best regards
Uwe

author maintainer
#5 lore raw

Re: [PATCH v3 2/2] alpha: enable contiguous memory allocation for coherent DMA

Hi Uwe,

Quoted 119 lines · Uwe Kleine-König

Hello,

Quoted 84 lines · Magnus Lindholm

Select HAVE_DMA_CONTIGUOUS and reserve a default CMA area after the machine has initialized its DMA windows. Prefer top-down placement below the direct-map and usable RAM limits. Skip the default reservation when no direct-map window exists. Explicit cma= ranges retain their generic override semantics; top-down placement is a preference, not a hard lower bound excluding the first 16 MiB.

Use dma_alloc_contiguous(), which rejects non-blocking callers, before falling back to alloc_pages_exact(). Pair both paths with dma_free_contiguous(), whose buddy fallback uses free_pages_exact(). Skip CMA on the GFP_DMA retry after a mapping failure so it cannot return the same unsuitable CMA pages. Retain zeroing for CMA memory.

Round the size up to a page boundary in both allocation and free. CMA allocates size >> PAGE_SHIFT pages, so an unaligned size could otherwise allocate too few pages and overrun the allocation during zeroing. Keep the mapping and unmapping lengths consistent with the aligned size.

With CONFIG_DMA_CMA=y, the default CMA_SIZE_MBYTES setting reserves 16 MiB without a cma= argument. cma=0 disables the default area.

---
 .../io/dma-contiguous/arch-support.txt        |  2 +-
 arch/alpha/Kconfig                            |  1 +
 arch/alpha/kernel/pci_iommu.c                 | 19 +++++++++++++------
 arch/alpha/kernel/setup.c                     | 15 +++++++++++++++
 4 files changed, 30 insertions(+), 7 deletions(-)
Documentation/features/io/dma-contiguous/arch-support.txt +1 -1
index 3c6ce35d704f..6cd205a991f6 100644
@@ -6,7 +6,7 @@
     -----------------------
     |         arch |status|
     -----------------------
-    |       alpha: | TODO |
+    |       alpha: |  ok  |
     |         arc: | TODO |
     |         arm: |  ok  |
     |       arm64: |  ok  |
arch/alpha/Kconfig +1 -0
index 6ccb2d230326..cddc1f3af209 100644
@@ -30,6 +30,7 @@ config ALPHA
      select HAVE_ARCH_COMPILER_H
      select HAVE_ASM_MODVERSIONS
      select HAVE_DEBUG_KMEMLEAK
+     select HAVE_DMA_CONTIGUOUS
      select HAVE_GUP_FAST
      select HAVE_KERNEL_BZIP2
      select HAVE_KERNEL_GZIP
arch/alpha/kernel/pci_iommu.c +9 -4
index 955b6ca61627..19aa5c05de2b 100644
@@ -408,15 +408,20 @@ static void *alpha_pci_alloc_coherent(struct device *dev, size_t size,
 {
      struct pci_dev *pdev = alpha_gendev_to_pci(dev);
      void *cpu_addr;
-     long order = get_order(size);
+     struct page *page;
 
+     size = PAGE_ALIGN(size);
      gfp &= ~GFP_DMA;
 
 try_again:
-     cpu_addr = (void *)__get_free_pages(gfp | __GFP_ZERO, order);
+     /* A low-memory retry must not return the same unsuitable CMA pages. */
+     page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);
+     if (page)
+             cpu_addr = page_address(page);
+     else
+             cpu_addr = alloc_pages_exact(size, gfp | __GFP_ZERO);
      if (! cpu_addr) {
-             printk(KERN_INFO "pci_alloc_consistent: "
-                    "get_free_pages failed from %ps\n",
+             pr_info("pci_alloc_consistent: allocation failed from %ps\n",
                      __builtin_return_address(0));
              /* ??? Really atomic allocation?  Otherwise we could play
                 with vmalloc and sg if we can't find contiguous memory.  */

arch/alpha/kernel/pci_iommu.c static void *alpha_pci_alloc_coherent(struct device *dev, size_t size, · line 434

Quoted 5 lines
arch/alpha/kernel/pci_iommu.c +1 -1
@@ -426,7 +431,7 @@ static void *alpha_pci_alloc_coherent(struct device *dev, size_t size,
 
      *dma_addrp = pci_map_single_1(pdev, virt_to_phys(cpu_addr), size, 0);
      if (*dma_addrp == DMA_MAPPING_ERROR) {
-             free_pages((unsigned long)cpu_addr, order);
+             dma_free_contiguous(dev, virt_to_page(cpu_addr), size);

On next-20261123 I get a build error on an ARCH=alpha allmodconfig build

arch/alpha/kernel/pci_iommu.c: In function ‘alpha_pci_alloc_coherent’:
arch/alpha/kernel/pci_iommu.c:418:41: error: too few arguments to function ‘dma_alloc_contiguous’; expected 4, have 3

  418 |         page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);
      |                                         ^~~~~~~~~~~~~~~~~~~~
In file included from arch/alpha/kernel/pci_iommu.c:14:
include/linux/dma-map-ops.h:105:14: note: declared here
  105 | struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp,
      |              ^~~~~~~~~~~~~~~~~~~~

which seems to be a conflict between

        next-20261123~159^2 = f03df6019b56 ("alpha: enable contiguous memory allocation for coherent DMA")

from
https://git.kernel.org/pub/scm/linux/kernel/git/lindholm/alpha.git for-next

and

        next-20261123~104^2~23^2~7 = 31f7e12d621b ("dma-contiguous: pass the NUMA node to dma_alloc_contiguous()")

from
https://git.kernel.org/pub/scm/linux/kernel/git/kuba/linux.git dma_alloc_coherent_node

Quoted 6 lines

I guess the right thing to do is to pull

https://git.kernel.org/pub/scm/linux/kernel/git/kuba/linux.git dma_alloc_coherent_node

into the alpha tree and fix the offending function call in the merge commit.

Thanks for catching this and identifying the interacting commits.

I reproduced the error on next-20261123. In
arch/alpha/kernel/pci_iommu.c, I changed:

page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);

to:

page = (gfp & GFP_DMA) ? NULL :
dma_alloc_contiguous(dev, size, gfp, dev_to_node(dev));

This fixes the allmodconfig pci_iommu.o build. A full DP264 UP build with CMA enabled also passed, as did an object build with CMA disabled. The full allmodconfig build hit an unrelated cgroup sample error involving PATH_MAX and LINE_MAX.

I'd prefer to keep the Alpha tree independent of the DMA series. Could we coordinate with Jakub and Marek to carry the adjustment in linux-next and ensure it reaches Linus when both changes are present? I'm happy to help with further testing.

If that proves awkward, I can remove the CMA patch from for-next and defer it to a separate Alpha pull request once the DMA changes have landed, updating the caller then.

Thanks,
Magnus

Quoted 3 lines

Best regards
Uwe

#6 lore raw

Re: [PATCH v3 2/2] alpha: enable contiguous memory allocation for coherent DMA

Hi Magnus,

Quoted 140 lines · Magnus Lindholm
Quoted 115 lines · Uwe Kleine-König
Quoted 84 lines · Magnus Lindholm

Select HAVE_DMA_CONTIGUOUS and reserve a default CMA area after the machine has initialized its DMA windows. Prefer top-down placement below the direct-map and usable RAM limits. Skip the default reservation when no direct-map window exists. Explicit cma= ranges retain their generic override semantics; top-down placement is a preference, not a hard lower bound excluding the first 16 MiB.

Use dma_alloc_contiguous(), which rejects non-blocking callers, before falling back to alloc_pages_exact(). Pair both paths with dma_free_contiguous(), whose buddy fallback uses free_pages_exact(). Skip CMA on the GFP_DMA retry after a mapping failure so it cannot return the same unsuitable CMA pages. Retain zeroing for CMA memory.

Round the size up to a page boundary in both allocation and free. CMA allocates size >> PAGE_SHIFT pages, so an unaligned size could otherwise allocate too few pages and overrun the allocation during zeroing. Keep the mapping and unmapping lengths consistent with the aligned size.

With CONFIG_DMA_CMA=y, the default CMA_SIZE_MBYTES setting reserves 16 MiB without a cma= argument. cma=0 disables the default area.

---
 .../io/dma-contiguous/arch-support.txt        |  2 +-
 arch/alpha/Kconfig                            |  1 +
 arch/alpha/kernel/pci_iommu.c                 | 19 +++++++++++++------
 arch/alpha/kernel/setup.c                     | 15 +++++++++++++++
 4 files changed, 30 insertions(+), 7 deletions(-)
Documentation/features/io/dma-contiguous/arch-support.txt +1 -1
index 3c6ce35d704f..6cd205a991f6 100644
@@ -6,7 +6,7 @@
     -----------------------
     |         arch |status|
     -----------------------
-    |       alpha: | TODO |
+    |       alpha: |  ok  |
     |         arc: | TODO |
     |         arm: |  ok  |
     |       arm64: |  ok  |
arch/alpha/Kconfig +1 -0
index 6ccb2d230326..cddc1f3af209 100644
@@ -30,6 +30,7 @@ config ALPHA
      select HAVE_ARCH_COMPILER_H
      select HAVE_ASM_MODVERSIONS
      select HAVE_DEBUG_KMEMLEAK
+     select HAVE_DMA_CONTIGUOUS
      select HAVE_GUP_FAST
      select HAVE_KERNEL_BZIP2
      select HAVE_KERNEL_GZIP
arch/alpha/kernel/pci_iommu.c +9 -4
index 955b6ca61627..19aa5c05de2b 100644
@@ -408,15 +408,20 @@ static void *alpha_pci_alloc_coherent(struct device *dev, size_t size,
 {
      struct pci_dev *pdev = alpha_gendev_to_pci(dev);
      void *cpu_addr;
-     long order = get_order(size);
+     struct page *page;
 
+     size = PAGE_ALIGN(size);
      gfp &= ~GFP_DMA;
 
 try_again:
-     cpu_addr = (void *)__get_free_pages(gfp | __GFP_ZERO, order);
+     /* A low-memory retry must not return the same unsuitable CMA pages. */
+     page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);
+     if (page)
+             cpu_addr = page_address(page);
+     else
+             cpu_addr = alloc_pages_exact(size, gfp | __GFP_ZERO);
      if (! cpu_addr) {
-             printk(KERN_INFO "pci_alloc_consistent: "
-                    "get_free_pages failed from %ps\n",
+             pr_info("pci_alloc_consistent: allocation failed from %ps\n",
                      __builtin_return_address(0));
              /* ??? Really atomic allocation?  Otherwise we could play
                 with vmalloc and sg if we can't find contiguous memory.  */

arch/alpha/kernel/pci_iommu.c static void *alpha_pci_alloc_coherent(struct device *dev, size_t size, · line 434

Quoted 5 lines
arch/alpha/kernel/pci_iommu.c +1 -1
@@ -426,7 +431,7 @@ static void *alpha_pci_alloc_coherent(struct device *dev, size_t size,
 
      *dma_addrp = pci_map_single_1(pdev, virt_to_phys(cpu_addr), size, 0);
      if (*dma_addrp == DMA_MAPPING_ERROR) {
-             free_pages((unsigned long)cpu_addr, order);
+             dma_free_contiguous(dev, virt_to_page(cpu_addr), size);

On next-20261123 I get a build error on an ARCH=alpha allmodconfig build

arch/alpha/kernel/pci_iommu.c: In function ‘alpha_pci_alloc_coherent’:
arch/alpha/kernel/pci_iommu.c:418:41: error: too few arguments to function ‘dma_alloc_contiguous’; expected 4, have 3

  418 |         page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);
      |                                         ^~~~~~~~~~~~~~~~~~~~
In file included from arch/alpha/kernel/pci_iommu.c:14:
include/linux/dma-map-ops.h:105:14: note: declared here
  105 | struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp,
      |              ^~~~~~~~~~~~~~~~~~~~

which seems to be a conflict between

        next-20261123~159^2 = f03df6019b56 ("alpha: enable contiguous memory allocation for coherent DMA")

from
https://git.kernel.org/pub/scm/linux/kernel/git/lindholm/alpha.git for-next

and

        next-20261123~104^2~23^2~7 = 31f7e12d621b ("dma-contiguous: pass the NUMA node to dma_alloc_contiguous()")

from
https://git.kernel.org/pub/scm/linux/kernel/git/kuba/linux.git dma_alloc_coherent_node

Quoted 6 lines

I guess the right thing to do is to pull

https://git.kernel.org/pub/scm/linux/kernel/git/kuba/linux.git dma_alloc_coherent_node

into the alpha tree and fix the offending function call in the merge commit.

Thanks for catching this and identifying the interacting commits.

I reproduced the error on next-20261123. In
arch/alpha/kernel/pci_iommu.c, I changed:

page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);

to:

page = (gfp & GFP_DMA) ? NULL :
dma_alloc_contiguous(dev, size, gfp, dev_to_node(dev));

This fixes the allmodconfig pci_iommu.o build. A full DP264 UP build with CMA enabled also passed, as did an object build with CMA disabled. The full allmodconfig build hit an unrelated cgroup sample error involving PATH_MAX and LINE_MAX.

Quoted 8 lines

I'd prefer to keep the Alpha tree independent of the DMA series. Could we coordinate with Jakub and Marek to carry the adjustment in linux-next and ensure it reaches Linus when both changes are present? I'm happy to help with further testing.

If that proves awkward, I can remove the CMA patch from for-next and defer it to a separate Alpha pull request once the DMA changes have landed, updating the caller then.

I think that rebasing the alpha changes on top of Jakub's stable tag:

git://git.kernel.org/pub/scm/linux/kernel/git/kuba/linux dma_alloc_coherent_node

would be the easiest way of handling this conflict as this way we will avoid any build breaks and keep the changes bisectable.

However if You still want to keep Your CMA changes based on v7.4-rc1 in alpha tree, then please provide a stable tag or branch with them. I will merge it to dma-mapping-for-next and apply the needed fixup.

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
#7 lore raw

Re: [PATCH v3 2/2] alpha: enable contiguous memory allocation for coherent DMA

Hello,

Quoted 21 lines · Marek Szyprowski

Hi Magnus,

Quoted 18 lines · Magnus Lindholm

Thanks for catching this and identifying the interacting commits.

I reproduced the error on next-20261123. In
arch/alpha/kernel/pci_iommu.c, I changed:

page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);

to:

page = (gfp & GFP_DMA) ? NULL :
dma_alloc_contiguous(dev, size, gfp, dev_to_node(dev));

This fixes the allmodconfig pci_iommu.o build. A full DP264 UP build with CMA enabled also passed, as did an object build with CMA disabled. The full allmodconfig build hit an unrelated cgroup sample error involving PATH_MAX and LINE_MAX.

I'd prefer to keep the Alpha tree independent of the DMA series.

That's up to you, but in my eyes merging in the immutable branch provided by Jakub into your tree (or rebasing on top of it as suggested by Marek) is really the easiest option. That way you don't have to wait for the change to hit Linus' tree and there is no further need for coordination or others paying attention to not create non-bisectable commit graphs.

However looking at the immutable branch I see it contains both the dma API change and then separately various adaptions to ethernet drivers. IMHO the immutable branch should only contain the API changes (that is the commits up to 31f7e12d621b) to reduce the amount of possible conflict.

Best regards
Uwe

author maintainer
#8 lore raw

Re: [PATCH v3 2/2] alpha: enable contiguous memory allocation for coherent DMA

Hi Uwe, Marek, Jakub,

Quoted 33 lines · Uwe Kleine-König

Hello,

Quoted 21 lines · Marek Szyprowski

Hi Magnus,

Quoted 18 lines · Magnus Lindholm

Thanks for catching this and identifying the interacting commits.

I reproduced the error on next-20261123. In
arch/alpha/kernel/pci_iommu.c, I changed:

page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);

to:

page = (gfp & GFP_DMA) ? NULL :
dma_alloc_contiguous(dev, size, gfp, dev_to_node(dev));

This fixes the allmodconfig pci_iommu.o build. A full DP264 UP build with CMA enabled also passed, as did an object build with CMA disabled. The full allmodconfig build hit an unrelated cgroup sample error involving PATH_MAX and LINE_MAX.

I'd prefer to keep the Alpha tree independent of the DMA series.

That's up to you, but in my eyes merging in the immutable branch provided by Jakub into your tree (or rebasing on top of it as suggested by Marek) is really the easiest option. That way you don't have to wait for the change to hit Linus' tree and there is no further need for coordination or others paying attention to not create non-bisectable commit graphs.

Quoted 5 lines

However looking at the immutable branch I see it contains both the dma API change and then separately various adaptions to ethernet drivers. IMHO the immutable branch should only contain the API changes (that is the commits up to 31f7e12d621b) to reduce the amount of possible conflict.

Thanks, that makes sense. I guess best is for me to rebase the Alpha changes onto the shared DMA API changes and update the caller in my CMA commit.

As you point out, using only the four API commits through 31f7e12d621b would avoid bringing the Ethernet driver conversions into the Alpha tree.

Jakub, Marek, would that commit be a suitable immutable base for this? A separate stable tag at that point would be helpful.

I'll then rebase, retest, and notify Mark before updating for-next.

Thanks,
Magnus

Quoted 3 lines

Best regards
Uwe

#9 lore raw

Re: [PATCH v3 2/2] alpha: enable contiguous memory allocation for coherent DMA

Quoted 34 lines · Magnus Lindholm
Quoted 27 lines · Uwe Kleine-König
Quoted 13 lines · Marek Szyprowski
Quoted 12 lines · Magnus Lindholm

Thanks for catching this and identifying the interacting commits.

I reproduced the error on next-20261123. In
arch/alpha/kernel/pci_iommu.c, I changed:

page = (gfp & GFP_DMA) ? NULL : dma_alloc_contiguous(dev, size, gfp);

to:

page = (gfp & GFP_DMA) ? NULL :
dma_alloc_contiguous(dev, size, gfp, dev_to_node(dev));

Quoted 6 lines
Quoted 6 lines

This fixes the allmodconfig pci_iommu.o build. A full DP264 UP build with CMA enabled also passed, as did an object build with CMA disabled. The full allmodconfig build hit an unrelated cgroup sample error involving PATH_MAX and LINE_MAX.

I'd prefer to keep the Alpha tree independent of the DMA series.

That's up to you, but in my eyes merging in the immutable branch provided by Jakub into your tree (or rebasing on top of it as suggested by Marek) is really the easiest option. That way you don't have to wait for the change to hit Linus' tree and there is no further need for coordination or others paying attention to not create non-bisectable commit graphs.

Quoted 5 lines

However looking at the immutable branch I see it contains both the dma API change and then separately various adaptions to ethernet drivers. IMHO the immutable branch should only contain the API changes (that is the commits up to 31f7e12d621b) to reduce the amount of possible conflict.

Quoted 7 lines

Thanks, that makes sense. I guess best is for me to rebase the Alpha changes onto the shared DMA API changes and update the caller in my CMA commit.

As you point out, using only the four API commits through 31f7e12d621b would avoid bringing the Ethernet driver conversions into the Alpha tree.

Jakub, Marek, would that commit be a suitable immutable base for this?

Yes, that would also work.

Quoted 3 lines

A separate stable tag at that point would be helpful.

I'll then rebase, retest, and notify Mark before updating for-next.

Here is a stable tag for You, I'm sorry for the delay:

https://git.kernel.org/pub/scm/linux/kernel/git/mszyprowski/linux.git dma_alloc_coherent_node_api

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland