Skip to contentSkip to conversation

alpha: enable DMA CMA support

PATCH Superseded Magnus Lindholm 2 participants 5 messages Nov 14

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Magnus Lindholm

author maintainer
#0 lore raw

v2 cover alpha: enable DMA CMA support

This revision addresses Matt Turner's review of the original CMA patch.

Split early parameter parsing into a prerequisite patch. Use the generic DMA contiguous allocation/free helpers, skip CMA on the GFP_DMA retry, and use exact-size buddy allocations to match current helper semantics. Remove the redundant highmem check and preserve existing zeroing.

Reserve top-down below the direct-map window and usable RAM limits. This avoids preferentially taking low memory with bottom-up allocation; it is not a strict exclusion of the first 16 MiB. Explicit cma= address ranges retain the generic override semantics. If there is no direct-map window, skip the default reservation.

The configured default is 16 MiB; cma=0 disables the default reservation.

These two patches apply independently on for-next at 0b4cadbe7c39. The ES40 test kernel additionally contains jump-label v3. The combined kernel, boot image and modules cross-build with W=1. Both CMA patches pass checkpatch with no warnings or errors.

CMA testing on a three-CPU AlphaServer ES40 with cma=64M:

- Reserved 64 MiB at 0x7c000000, above the first 16 MiB and below the 2 GiB direct-map limit.
- Ran four allocation cases on each CPU (12 cases total).
- 32 MiB and 24 KiB (three-page) GFP_KERNEL allocations used CMA.
- 8 KiB (one-page) GFP_KERNEL and 24 KiB GFP_ATOMIC allocations bypassed CMA.
- All buffers passed zeroing and CPU write/read checks.
- CMA allocation/release counters balanced, and available_pages returned to its pre-test value after each allocation was freed.

The initial harness compared CmaFree immediately after freeing a small allocation. That was not a reliable per-allocation check; verification using the CMA sysfs counters confirmed that all test pages were released.

No new kernel warnings or faults appeared during testing. These tests exercise coherent allocation and freeing, not device-initiated DMA. GFP_ATOMIC was tested from process context. Boots with cma=0, cma=4M and the default size, fragmentation stress, and no-IOMMU platform validation remain outstanding.

Link to v1:
https://lore.kernel.org/linux-alpha/[email protected]/

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                  | 13 +++++++++----
 arch/alpha/kernel/setup.c                      | 18 ++++++++++++++++++
 4 files changed, 29 insertions(+), 5 deletions(-)

base-commit: 0b4cadbe7c39d830dec35c4a2655483de0dae568

Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v2 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.

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 | 3 +++
 1 file changed, 3 insertions(+)
arch/alpha/kernel/setup.c +3 -0
index 9fda01b73b18..c7b7ebcc47b6 100644
@@ -579,6 +579,9 @@ setup_arch(char **cmdline_p)
 	if (alpha_mv.init_arch)
 		alpha_mv.init_arch();
 
+	/* Parse early options before reserving memory for DMA CMA. */
+	parse_early_param();
+
 	/* Reserve standard resources.  */
 	reserve_std_resources();
 
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH v2 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.

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

---
 .../features/io/dma-contiguous/arch-support.txt   |  2 +-
 arch/alpha/Kconfig                                |  1 +
 arch/alpha/kernel/pci_iommu.c                     | 13 +++++++++----
 arch/alpha/kernel/setup.c                         | 15 +++++++++++++++
 4 files changed, 26 insertions(+), 5 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..0337bcef26cb 100644
@@ -408,12 +408,17 @@ 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;
 
 	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",
@@ -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
@@ -453,7 +458,7 @@ static void alpha_pci_free_coherent(struct device *dev, size_t size,
 {
 	struct pci_dev *pdev = alpha_gendev_to_pci(dev);
 	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 c7b7ebcc47b6..0cd94c9023c1 100644
@@ -30,6 +30,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>
@@ -582,6 +583,20 @@ setup_arch(char **cmdline_p)
 	/* Parse early options before reserving memory for DMA CMA. */
 	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 v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] alpha: parse early parameters before reserving contiguous memory

In reply to Magnus Lindholm

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

Quoted 11 lines · Magnus Lindholm
arch/alpha/kernel/setup.c +3 -0
index 9fda01b73b18..c7b7ebcc47b6 100644
@@ -579,6 +579,9 @@ setup_arch(char **cmdline_p)
      if (alpha_mv.init_arch)
              alpha_mv.init_arch();
 
+     /* Parse early options before reserving memory for DMA CMA. */
+     parse_early_param();
+

Once your jump label series lands, this will run before start_kernel() calls jump_label_init(), and any early_param handler that flips a static key will trip the "used before call to jump_label_init" warning. threadirqs and randomize_kstack_offset= both do that. We don't select IRQ_FORCED_THREADING or HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET today, so nothing breaks yet, but arm64 and riscv both call jump_label_init() right before parse_early_param() in setup_arch() for this reason. It returns early if called again, so it's safe to add here. Whichever of the two series goes in second should probably do that.

maintainer
#4 lore raw

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

In reply to Magnus Lindholm

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

Quoted 13 lines · Magnus Lindholm
arch/alpha/kernel/pci_iommu.c +3 -2
@@ -408,12 +408,17 @@ 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;
 
      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);

size here comes straight from the driver's dma_alloc_coherent() call and isn't necessarily a multiple of PAGE_SIZE. dma_alloc_contiguous() assumes it is: cma_alloc_aligned() passes size >> PAGE_SHIFT to cma_alloc(), but dma_free_contiguous() releases PAGE_ALIGN(size) >> PAGE_SHIFT pages.

So a 12K allocation gets a single 8K page from CMA, the memset() below runs 4K off the end of it, and the free later releases two pages, one of which we never owned. dma_direct_alloc() does size = PAGE_ALIGN(size) before it gets this far. I think we need the same at the top of both alpha_pci_alloc_coherent() and alpha_pci_free_coherent().

The 8K, 24K and 32M cases in your test are all page multiples, so they wouldn't have caught this. Something like 12K or 20K would.

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

Quoted 7 lines
arch/alpha/kernel/pci_iommu.c +4 -0
+     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",

Minor, but this message isn't accurate anymore. Maybe just say "allocation failed".

Rest of it looks good to me. Skipping CMA on the GFP_DMA retry is the right call.