[PATCH 4/4][Unstable][SRU Disco] dma-contiguous: fix !CONFIG_DMA_CMA version of dma_{alloc, free}_contiguous()

dann frazier dann.frazier at canonical.com
Tue Jun 4 21:00:10 UTC 2019

From: Nicolin Chen <nicoleotsuka at gmail.com>

BugLink: https://bugs.launchpad.net/bugs/1823753

Commit fdaeec198ada ("dma-contiguous: add dma_{alloc,free}_contiguous()
helpers") adds a pair of new helper functions so as to abstract code in
the dma-direct (and other places in the future), however it breaks QEMU
boot feature using x86_64 defconfig.

That's because x86_64 defconfig has CONFIG_DMA_CMA=n so those two newly
introduced helper functions are empty in their !CONFIG_DMA_CMA version,
while previously the platform independent dma-direct code had fallback
alloc_pages_node() and __free_pages().

So this patch fixes it by adding alloc_pages_node() and __free_pages()
in the !CONFIG_DMA_CMA version of the two helper functions.

Tested with below QEMU command:
  qemu-system-x86_64 -m 512m \
      -drive file=images/x86_64/rootfs.ext4,format=raw,if=ide \
      -append 'console=ttyS0 root=/dev/sda' -nographic \
      -kernel arch/x86_64/boot/bzImage

with the rootfs from the below link:

Fixes: fdaeec198ada ("dma-contiguous: add dma_{alloc,free}_contiguous() helpers")
Reported-by: Nathan Chancellor <natechancellor at gmail.com>
Signed-off-by: Nicolin Chen <nicoleotsuka at gmail.com>
Signed-off-by: Christoph Hellwig <hch at lst.de>
(cherry picked from commit dd3dcede9fa0a0b661ac1f24843f4a1b1317fdb6 linux-next)
Signed-off-by: dann frazier <dann.frazier at canonical.com>
 include/linux/dma-contiguous.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/include/linux/dma-contiguous.h b/include/linux/dma-contiguous.h
index 15f77c7d84e28..1499e2477180a 100644
--- a/include/linux/dma-contiguous.h
+++ b/include/linux/dma-contiguous.h
@@ -54,6 +54,7 @@
 #ifdef __KERNEL__
 #include <linux/device.h>
+#include <linux/mm.h>
 struct cma;
 struct page;
@@ -159,15 +160,20 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
 	return false;
+/* 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)
-	return NULL;
+	int node = dev ? dev_to_node(dev) : NUMA_NO_NODE;
+	size_t align = get_order(PAGE_ALIGN(size));
+	return alloc_pages_node(node, gfp, align);
 static inline void dma_free_contiguous(struct device *dev, struct page *page,
 		size_t size)
+	__free_pages(page, get_order(size));

More information about the kernel-team mailing list