[PATCH 2/2] intel-iommu: don't unnecessarily call dma_alloc_from_contiguous()

From: Akinobu Mita
Date: Sun Sep 28 2014 - 11:52:51 EST

If CONFIG_DMA_CMA is enabled, intel_alloc_coherent() tries to allocate
memory region by dma_alloc_from_contiguous() before trying to use

This wastes CMA region by small DMA-coherent buffers which can be
allocated by alloc_pages(). And it also causes performance degradation,
as this is trying to drive _all_ dma mapping allocations through a
_very_ small window, reported by Peter Hurley.

This fixes it by trying to allocate by alloc_pages() first in
intel_alloc_coherent() as dma_alloc_from_contiguous should be called
only for huge allocation.

Signed-off-by: Akinobu Mita <akinobu.mita@xxxxxxxxx>
Reported-by: Peter Hurley <peter@xxxxxxxxxxxxxxxxxx>
Cc: Peter Hurley <peter@xxxxxxxxxxxxxxxxxx>
Cc: Marek Szyprowski <m.szyprowski@xxxxxxxxxxx>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@xxxxxxxxxx>
Cc: David Woodhouse <dwmw2@xxxxxxxxxxxxx>
Cc: Don Dutile <ddutile@xxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
Cc: Andi Kleen <andi@xxxxxxxxxxxxxx>
Cc: Yinghai Lu <yinghai@xxxxxxxxxx>
Cc: x86@xxxxxxxxxx
Cc: iommu@xxxxxxxxxxxxxxxxxxxxxxxxxx
drivers/iommu/intel-iommu.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c
index 3441615..f9794fa 100644
--- a/drivers/iommu/intel-iommu.c
+++ b/drivers/iommu/intel-iommu.c
@@ -3250,7 +3250,8 @@ static void *intel_alloc_coherent(struct device *dev, size_t size,
flags |= GFP_DMA32;

- if (flags & __GFP_WAIT) {
+ page = alloc_pages(flags | __GFP_NOWARN, order);
+ if (!page && (flags & __GFP_WAIT)) {
unsigned int count = size >> PAGE_SHIFT;

page = dma_alloc_from_contiguous(dev, count, order);
@@ -3261,10 +3262,10 @@ static void *intel_alloc_coherent(struct device *dev, size_t size,

- if (!page)
- page = alloc_pages(flags, order);
- if (!page)
+ if (!page) {
+ warn_alloc_failed(flags, order, NULL);
return NULL;
+ }
memset(page_address(page), 0, size);

*dma_handle = __intel_map_single(dev, page_to_phys(page), size,

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/