mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] dma-coherent: Warn if OF reserved memory is beyond current coherent DMA mask
@ 2025-04-21  8:39 ` Chen-Yu Tsai
  2025-04-22 17:43   ` Marek Szyprowski
  0 siblings, 1 reply; 2+ messages in thread
From: Chen-Yu Tsai @ 2025-04-21  8:39 UTC (permalink / raw)
  To: Marek Szyprowski, Robin Murphy; +Cc: Chen-Yu Tsai, iommu, linux-kernel

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>
---
I talked about this on IRC on 2025-04-15 [1].

It seems that printing a warning was the most acceptable thing to do.

[1] https://libera.irclog.whitequark.org/armlinux/2025-04-15

 kernel/dma/coherent.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/kernel/dma/coherent.c b/kernel/dma/coherent.c
index 3b2bdca9f1d4..77c8d9487a9a 100644
--- a/kernel/dma/coherent.c
+++ b/kernel/dma/coherent.c
@@ -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;
 }
 
-- 
2.49.0.805.g082f7c87e0-goog


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] dma-coherent: Warn if OF reserved memory is beyond current coherent DMA mask
  2025-04-21  8:39 ` [PATCH] dma-coherent: Warn if OF reserved memory is beyond current coherent DMA mask Chen-Yu Tsai
@ 2025-04-22 17:43   ` Marek Szyprowski
  0 siblings, 0 replies; 2+ messages in thread
From: Marek Szyprowski @ 2025-04-22 17:43 UTC (permalink / raw)
  To: Chen-Yu Tsai, Robin Murphy; +Cc: iommu, linux-kernel

On 21.04.2025 10:39, Chen-Yu Tsai wrote:
> 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>

Thanks, applied to dma-mapping-fixes branch.

Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-04-22 17:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20250421083941eucas1p28b1de88f77e9de88f52b2c72918c9561@eucas1p2.samsung.com>
2025-04-21  8:39 ` [PATCH] dma-coherent: Warn if OF reserved memory is beyond current coherent DMA mask Chen-Yu Tsai
2025-04-22 17:43   ` Marek Szyprowski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®