dma-coherent: fix dma_declare_coherent_memory() logic error

A recent change interprets the return code of dma_init_coherent_memory
as an error value, but it is instead a boolean, where 'true' indicates
success. This leads causes the caller to always do the wrong thing,
and also triggers a compile-time warning about it:

drivers/base/dma-coherent.c: In function 'dma_declare_coherent_memory':
drivers/base/dma-coherent.c:99:15: error: 'mem' may be used uninitialized in this function [-Werror=maybe-uninitialized]

I ended up changing the code a little more, to give use the usual
error handling, as this seemed the best way to fix up the warning
and make the code look reasonable at the same time.

Fixes: 2436bdcda53f ("dma-coherent: remove the DMA_MEMORY_MAP and DMA_MEMORY_IO flags")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Christoph Hellwig <hch@lst.de>

authored by

Arnd Bergmann and committed by
Christoph Hellwig
d35b0996 edeb8e4c

+25 -13
+25 -13
drivers/base/dma-coherent.c
··· 37 return mem->device_base; 38 } 39 40 - static bool dma_init_coherent_memory( 41 phys_addr_t phys_addr, dma_addr_t device_addr, size_t size, int flags, 42 struct dma_coherent_mem **mem) 43 { ··· 45 void __iomem *mem_base = NULL; 46 int pages = size >> PAGE_SHIFT; 47 int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); 48 49 - if (!size) 50 goto out; 51 52 mem_base = memremap(phys_addr, size, MEMREMAP_WC); 53 - if (!mem_base) 54 goto out; 55 - 56 dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL); 57 - if (!dma_mem) 58 goto out; 59 dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL); 60 - if (!dma_mem->bitmap) 61 goto out; 62 63 dma_mem->virt_base = mem_base; 64 dma_mem->device_base = device_addr; ··· 76 spin_lock_init(&dma_mem->spinlock); 77 78 *mem = dma_mem; 79 - return true; 80 81 out: 82 kfree(dma_mem); 83 if (mem_base) 84 memunmap(mem_base); 85 - return false; 86 } 87 88 static void dma_release_coherent_memory(struct dma_coherent_mem *mem) ··· 346 static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev) 347 { 348 struct dma_coherent_mem *mem = rmem->priv; 349 350 - if (!mem && 351 - !dma_init_coherent_memory(rmem->base, rmem->base, rmem->size, 352 - DMA_MEMORY_EXCLUSIVE, 353 - &mem)) { 354 pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %ld MiB\n", 355 &rmem->base, (unsigned long)rmem->size / SZ_1M); 356 - return -ENODEV; 357 } 358 mem->use_dev_dma_pfn_offset = true; 359 rmem->priv = mem;
··· 37 return mem->device_base; 38 } 39 40 + static int dma_init_coherent_memory( 41 phys_addr_t phys_addr, dma_addr_t device_addr, size_t size, int flags, 42 struct dma_coherent_mem **mem) 43 { ··· 45 void __iomem *mem_base = NULL; 46 int pages = size >> PAGE_SHIFT; 47 int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); 48 + int ret; 49 50 + if (!size) { 51 + ret = -EINVAL; 52 goto out; 53 + } 54 55 mem_base = memremap(phys_addr, size, MEMREMAP_WC); 56 + if (!mem_base) { 57 + ret = -EINVAL; 58 goto out; 59 + } 60 dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL); 61 + if (!dma_mem) { 62 + ret = -ENOMEM; 63 goto out; 64 + } 65 dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL); 66 + if (!dma_mem->bitmap) { 67 + ret = -ENOMEM; 68 goto out; 69 + } 70 71 dma_mem->virt_base = mem_base; 72 dma_mem->device_base = device_addr; ··· 68 spin_lock_init(&dma_mem->spinlock); 69 70 *mem = dma_mem; 71 + return 0; 72 73 out: 74 kfree(dma_mem); 75 if (mem_base) 76 memunmap(mem_base); 77 + return ret; 78 } 79 80 static void dma_release_coherent_memory(struct dma_coherent_mem *mem) ··· 338 static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev) 339 { 340 struct dma_coherent_mem *mem = rmem->priv; 341 + int ret; 342 343 + if (!mem) 344 + return -ENODEV; 345 + 346 + ret = dma_init_coherent_memory(rmem->base, rmem->base, rmem->size, 347 + DMA_MEMORY_EXCLUSIVE, &mem); 348 + 349 + if (ret) { 350 pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %ld MiB\n", 351 &rmem->base, (unsigned long)rmem->size / SZ_1M); 352 + return ret; 353 } 354 mem->use_dev_dma_pfn_offset = true; 355 rmem->priv = mem;