Re: [PATCH 3/5] dma-direct: refine dma_direct_alloc zone selection

From: Benjamin Herrenschmidt
Date: Wed Sep 26 2018 - 21:45:43 EST


On Thu, 2018-09-20 at 20:52 +0200, Christoph Hellwig wrote:
> +static gfp_t __dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
> + u64 *phys_mask)
> +{
> + if (force_dma_unencrypted())
> + *phys_mask = __dma_to_phys(dev, dma_mask);
> + else
> + *phys_mask = dma_to_phys(dev, dma_mask);
> +
> + /* GFP_DMA32 and GFP_DMA are no ops without the corresponding zones: */
> + if (*phys_mask <= DMA_BIT_MASK(ARCH_ZONE_DMA_BITS))
> + return GFP_DMA;
> + if (*phys_mask <= DMA_BIT_MASK(32))
> + return GFP_DMA32;
> + return 0;
> +}

I'm not sure this is entirely right.

Let's say the mask is 30 bits. You will return GFP_DMA32, which will
fail if you allocate something above 1G (which is legit for
ZONE_DMA32).

I think the logic should be:

if (mask < ZONE_DMA)
fail;
else if (mask < ZONE_DMA32)
use ZONE_DMA or fail if doesn't exist
else if (mask < top_of_ram)
use ZONE_DMA32 or fail if doesn't exist
else
use ZONE_NORMAL

Additionally, we want to fold-in the top-of-ram test such that we don't
fail the second case if the mask is 31-bits (smaller than ZONE_DMA32)
but top of ram is also small enough.

So the top of ram test should take precendence.

Cheers,
Ben.