dma-direct: simplify the use atomic pool logic in dma_direct_alloc
authorChristoph Hellwig <hch@lst.de>
Fri, 6 Oct 2023 13:13:34 +0000 (15:13 +0200)
committerChristoph Hellwig <hch@lst.de>
Sun, 22 Oct 2023 14:38:54 +0000 (16:38 +0200)
The logic in dma_direct_alloc when to use the atomic pool vs remapping
grew a bit unreadable.  Consolidate it into a single check, and clean
up the set_uncached vs remap logic a bit as well.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
Reviewed-by: Greg Ungerer <gerg@linux-m68k.org>
Tested-by: Greg Ungerer <gerg@linux-m68k.org>
kernel/dma/direct.c

index c078090cd38ecc45306d32cc6c92a5957d8a421c..9657ef7c055eaa333efc7063d0fae57dda71927a 100644 (file)
@@ -234,27 +234,22 @@ void *dma_direct_alloc(struct device *dev, size_t size,
                                        dma_handle);
 
                /*
-                * Otherwise remap if the architecture is asking for it.  But
-                * given that remapping memory is a blocking operation we'll
-                * instead have to dip into the atomic pools.
+                * Otherwise we require the architecture to either be able to
+                * mark arbitrary parts of the kernel direct mapping uncached,
+                * or remapped it uncached.
                 */
+               set_uncached = IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED);
                remap = IS_ENABLED(CONFIG_DMA_DIRECT_REMAP);
-               if (remap) {
-                       if (dma_direct_use_pool(dev, gfp))
-                               return dma_direct_alloc_from_pool(dev, size,
-                                               dma_handle, gfp);
-               } else {
-                       if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED))
-                               return NULL;
-                       set_uncached = true;
-               }
+               if (!set_uncached && !remap)
+                       return NULL;
        }
 
        /*
-        * Decrypting memory may block, so allocate the memory from the atomic
-        * pools if we can't block.
+        * Remapping or decrypting memory may block, allocate the memory from
+        * the atomic pools instead if we aren't allowed block.
         */
-       if (force_dma_unencrypted(dev) && dma_direct_use_pool(dev, gfp))
+       if ((remap || force_dma_unencrypted(dev)) &&
+           dma_direct_use_pool(dev, gfp))
                return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp);
 
        /* we always manually zero the memory once we are done */