aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnd Bergmann <arnd@arndb.de>2017-09-05 04:10:09 -0400
committerChristoph Hellwig <hch@lst.de>2017-09-05 07:23:11 -0400
commitd35b0996fef3bfe76665e87bbff7d95c6807350a (patch)
tree1938ce180b8428314beb93fbcdfc2a7f29f5f796
parentedeb8e4ccb79eaed4a5126945cce00489f09b849 (diff)
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>
-rw-r--r--drivers/base/dma-coherent.c38
1 files changed, 25 insertions, 13 deletions
diff --git a/drivers/base/dma-coherent.c b/drivers/base/dma-coherent.c
index f82a504583d4..a39b2166b145 100644
--- a/drivers/base/dma-coherent.c
+++ b/drivers/base/dma-coherent.c
@@ -37,7 +37,7 @@ static inline dma_addr_t dma_get_device_base(struct device *dev,
37 return mem->device_base; 37 return mem->device_base;
38} 38}
39 39
40static bool dma_init_coherent_memory( 40static int dma_init_coherent_memory(
41 phys_addr_t phys_addr, dma_addr_t device_addr, size_t size, int flags, 41 phys_addr_t phys_addr, dma_addr_t device_addr, size_t size, int flags,
42 struct dma_coherent_mem **mem) 42 struct dma_coherent_mem **mem)
43{ 43{
@@ -45,20 +45,28 @@ static bool dma_init_coherent_memory(
45 void __iomem *mem_base = NULL; 45 void __iomem *mem_base = NULL;
46 int pages = size >> PAGE_SHIFT; 46 int pages = size >> PAGE_SHIFT;
47 int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); 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 goto out; 52 goto out;
53 }
51 54
52 mem_base = memremap(phys_addr, size, MEMREMAP_WC); 55 mem_base = memremap(phys_addr, size, MEMREMAP_WC);
53 if (!mem_base) 56 if (!mem_base) {
57 ret = -EINVAL;
54 goto out; 58 goto out;
55 59 }
56 dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL); 60 dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
57 if (!dma_mem) 61 if (!dma_mem) {
62 ret = -ENOMEM;
58 goto out; 63 goto out;
64 }
59 dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL); 65 dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
60 if (!dma_mem->bitmap) 66 if (!dma_mem->bitmap) {
67 ret = -ENOMEM;
61 goto out; 68 goto out;
69 }
62 70
63 dma_mem->virt_base = mem_base; 71 dma_mem->virt_base = mem_base;
64 dma_mem->device_base = device_addr; 72 dma_mem->device_base = device_addr;
@@ -68,13 +76,13 @@ static bool dma_init_coherent_memory(
68 spin_lock_init(&dma_mem->spinlock); 76 spin_lock_init(&dma_mem->spinlock);
69 77
70 *mem = dma_mem; 78 *mem = dma_mem;
71 return true; 79 return 0;
72 80
73out: 81out:
74 kfree(dma_mem); 82 kfree(dma_mem);
75 if (mem_base) 83 if (mem_base)
76 memunmap(mem_base); 84 memunmap(mem_base);
77 return false; 85 return ret;
78} 86}
79 87
80static void dma_release_coherent_memory(struct dma_coherent_mem *mem) 88static void dma_release_coherent_memory(struct dma_coherent_mem *mem)
@@ -338,14 +346,18 @@ static struct reserved_mem *dma_reserved_default_memory __initdata;
338static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev) 346static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
339{ 347{
340 struct dma_coherent_mem *mem = rmem->priv; 348 struct dma_coherent_mem *mem = rmem->priv;
349 int ret;
350
351 if (!mem)
352 return -ENODEV;
341 353
342 if (!mem && 354 ret = dma_init_coherent_memory(rmem->base, rmem->base, rmem->size,
343 !dma_init_coherent_memory(rmem->base, rmem->base, rmem->size, 355 DMA_MEMORY_EXCLUSIVE, &mem);
344 DMA_MEMORY_EXCLUSIVE, 356
345 &mem)) { 357 if (ret) {
346 pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %ld MiB\n", 358 pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %ld MiB\n",
347 &rmem->base, (unsigned long)rmem->size / SZ_1M); 359 &rmem->base, (unsigned long)rmem->size / SZ_1M);
348 return -ENODEV; 360 return ret;
349 } 361 }
350 mem->use_dev_dma_pfn_offset = true; 362 mem->use_dev_dma_pfn_offset = true;
351 rmem->priv = mem; 363 rmem->priv = mem;