Skip to content

Commit d35b099

Browse files
arndbChristoph Hellwig
authored andcommitted
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: 2436bdc ("dma-coherent: remove the DMA_MEMORY_MAP and DMA_MEMORY_IO flags") Signed-off-by: Arnd Bergmann <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]>
1 parent edeb8e4 commit d35b099

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

drivers/base/dma-coherent.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,36 @@ static inline dma_addr_t dma_get_device_base(struct device *dev,
3737
return mem->device_base;
3838
}
3939

40-
static bool dma_init_coherent_memory(
40+
static int dma_init_coherent_memory(
4141
phys_addr_t phys_addr, dma_addr_t device_addr, size_t size, int flags,
4242
struct dma_coherent_mem **mem)
4343
{
4444
struct dma_coherent_mem *dma_mem = NULL;
4545
void __iomem *mem_base = NULL;
4646
int pages = size >> PAGE_SHIFT;
4747
int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
48+
int ret;
4849

49-
if (!size)
50+
if (!size) {
51+
ret = -EINVAL;
5052
goto out;
53+
}
5154

5255
mem_base = memremap(phys_addr, size, MEMREMAP_WC);
53-
if (!mem_base)
56+
if (!mem_base) {
57+
ret = -EINVAL;
5458
goto out;
55-
59+
}
5660
dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
57-
if (!dma_mem)
61+
if (!dma_mem) {
62+
ret = -ENOMEM;
5863
goto out;
64+
}
5965
dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
60-
if (!dma_mem->bitmap)
66+
if (!dma_mem->bitmap) {
67+
ret = -ENOMEM;
6168
goto out;
69+
}
6270

6371
dma_mem->virt_base = mem_base;
6472
dma_mem->device_base = device_addr;
@@ -68,13 +76,13 @@ static bool dma_init_coherent_memory(
6876
spin_lock_init(&dma_mem->spinlock);
6977

7078
*mem = dma_mem;
71-
return true;
79+
return 0;
7280

7381
out:
7482
kfree(dma_mem);
7583
if (mem_base)
7684
memunmap(mem_base);
77-
return false;
85+
return ret;
7886
}
7987

8088
static void dma_release_coherent_memory(struct dma_coherent_mem *mem)
@@ -338,14 +346,18 @@ static struct reserved_mem *dma_reserved_default_memory __initdata;
338346
static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
339347
{
340348
struct dma_coherent_mem *mem = rmem->priv;
349+
int ret;
350+
351+
if (!mem)
352+
return -ENODEV;
341353

342-
if (!mem &&
343-
!dma_init_coherent_memory(rmem->base, rmem->base, rmem->size,
344-
DMA_MEMORY_EXCLUSIVE,
345-
&mem)) {
354+
ret = dma_init_coherent_memory(rmem->base, rmem->base, rmem->size,
355+
DMA_MEMORY_EXCLUSIVE, &mem);
356+
357+
if (ret) {
346358
pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %ld MiB\n",
347359
&rmem->base, (unsigned long)rmem->size / SZ_1M);
348-
return -ENODEV;
360+
return ret;
349361
}
350362
mem->use_dev_dma_pfn_offset = true;
351363
rmem->priv = mem;

0 commit comments

Comments
 (0)