dma-coherent: Warn if OF reserved memory is beyond current coherent DMA mask
authorChen-Yu Tsai <wenst@chromium.org>
Mon, 21 Apr 2025 08:39:29 +0000 (16:39 +0800)
committerMarek Szyprowski <m.szyprowski@samsung.com>
Tue, 22 Apr 2025 15:44:09 +0000 (17:44 +0200)
When a reserved memory region described in the device tree is attached
to a device, it is expected that the device's limitations are correctly
included in that description.

However, if the device driver failed to implement DMA address masking
or addressing beyond the default 32 bits (on arm64), then bad things
could happen because the DMA address was truncated, such as playing
back audio with no actual audio coming out, or DMA overwriting random
blocks of kernel memory.

Check against the coherent DMA mask when the memory regions are attached
to the device. Give a warning when the memory region can not be covered
by the mask.

A warning instead of a hard error was chosen, because it is possible
that existing drivers could be working fine even if they forgot to
extend the coherent DMA mask.

Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Link: https://lore.kernel.org/r/20250421083930.374173-1-wenst@chromium.org
kernel/dma/coherent.c

index 3b2bdca9f1d4b0274bf4874892b94730cd05c5df..77c8d9487a9ab16840218d63dfb79ea694f6e647 100644 (file)
@@ -336,16 +336,22 @@ static phys_addr_t dma_reserved_default_memory_size __initdata;
 
 static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
 {
-       if (!rmem->priv) {
-               struct dma_coherent_mem *mem;
+       struct dma_coherent_mem *mem = rmem->priv;
 
+       if (!mem) {
                mem = dma_init_coherent_memory(rmem->base, rmem->base,
                                               rmem->size, true);
                if (IS_ERR(mem))
                        return PTR_ERR(mem);
                rmem->priv = mem;
        }
-       dma_assign_coherent_memory(dev, rmem->priv);
+
+       /* Warn if the device potentially can't use the reserved memory */
+       if (mem->device_base + rmem->size - 1 >
+           min_not_zero(dev->coherent_dma_mask, dev->bus_dma_limit))
+               dev_warn(dev, "reserved memory is beyond device's set DMA address range\n");
+
+       dma_assign_coherent_memory(dev, mem);
        return 0;
 }