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