alpha: enable DMA CMA support
Subsystems (2)
Magnus Lindholm
maintainerv4 cover alpha: enable DMA CMA support
This series enables CMA for Alpha coherent DMA allocations and parses early parameters before reserving the CMA area.
Changes in v4:
- Rebase onto Marek's stable dma_alloc_coherent_node_api tag.
- Pass dev_to_node(dev) as the fourth argument to dma_alloc_contiguous(), preserving the previous device-node selection.
Patch 1 is unchanged. Matt Turner's Reviewed-by tags are retained on both patches.
Built with GCC 14.2.1 and boot-tested on a three-CPU AlphaServer ES40 (DP264 SMP), with CMA, compaction and jump labels enabled. Booting with cma=64M reserved 64 MiB as requested.
A private coherent-DMA probe passed 39 allocation/free cases across all three CPUs. Requests included 1 byte, 8 KiB - 1, 8 KiB, 8 KiB + 1, 12 KiB, 16 KiB - 1, 16 KiB, 16 KiB + 1, 20 KiB, 24 KiB and 32 MiB. Checks covered zeroing, data integrity, page-rounded CMA usage and restoration of the CMA bitmap after free. GFP_ATOMIC requests of 12 KiB and 20 KiB correctly bypassed CMA on every CPU. No new kernel errors appeared during the tests; loading the private probe produced only the expected out-of-tree-module taint. No hardware DMA transfers were submitted by the probe.
These two commits were tested in the complete Alpha queue at b4a6d2833fb56d3cf04c4d243f00b7a3f92bd43a. The exported patches also apply directly to the API tag below. Both pass checkpatch. Other reservation-size boots and no-IOMMU testing remain outstanding.
Base repository:
https://git.kernel.org/pub/scm/linux/kernel/git/mszyprowski/linux.git
Tag: dma_alloc_coherent_node_api
Previous version:
https://lore.kernel.org/r/[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 | 20 +++++++++++++------ arch/alpha/kernel/setup.c | 20 +++++++++++++++++++ 4 files changed, 36 insertions(+), 7 deletions(-)
base-commit: 31f7e12d621bc5b1dcda0b5e6855e39adc8aaebd
Signature · 1 line
2.43.0
Magnus Lindholm
maintainerPATCH v4 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
@@ -26,4 +26,5 @@#include <linux/errno.h> #include <linux/init.h> +#include <linux/jump_label.h> #include <linux/string.h> #include <linux/ioport.h>
@@ -580,4 +581,8 @@ setup_arch(char **cmdline_p) 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
Magnus Lindholm
maintainerPATCH v4 2/2 alpha: enable contiguous memory allocation for coherent DMA
In reply to Magnus LindholmSelect 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 | 20 +++++++++++++------ arch/alpha/kernel/setup.c | 15 ++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-)
Documentation/features/io/dma-contiguous/arch-support.txt +1 -1
@@ -7,5 +7,5 @@ | arch |status| ----------------------- - | alpha: | TODO | + | alpha: | ok | | arc: | TODO | | arm: | ok |
arch/alpha/Kconfig +1 -0
@@ -32,4 +32,5 @@ config ALPHA select HAVE_ASM_MODVERSIONS select HAVE_DEBUG_KMEMLEAK + select HAVE_DMA_CONTIGUOUS select HAVE_GUP_FAST select HAVE_KERNEL_BZIP2
arch/alpha/kernel/pci_iommu.c +14 -6
@@ -409,13 +409,19 @@ 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,dev_to_node (dev)); +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
@@ -427,5 +433,5 @@ 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 ;
@@ -453,6 +459,8 @@ static void alpha_pci_free_coherent(struct device *dev, size_t size, {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",
arch/alpha/kernel/setup.c +15 -0
@@ -32,4 +32,5 @@#include <linux/platform_device.h> #include <linux/memblock.h> +#include <linux/dma-map-ops.h> #include <linux/pci.h> #include <linux/seq_file.h>
@@ -585,4 +586,18 @@ setup_arch(char **cmdline_p)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