Re: [PATCH 0/2] ARM: preserve DMA_FROM_DEVICE buffer contents
From: "Arnd Bergmann" <arnd@arndb.de>
Date: 2026-09-10 13:16:50
Also in:
lkml, stable
On Thu, Sep 10, 2026, at 12:55, Will Deacon wrote:
On Thu, Sep 10, 2026 at 11:14:28AM +0200, Arnd Bergmann wrote:quoted
I see this as a tradeoff that can go either way: - the current 32-bit Arm approach (also arc, hexagon, microblaze, mips, nios2, openrisc, powerpc32, sh) is obviously faster as it avoids the writeback, but it relies on device drivers to ensure no stale data can leak back into userspace. - Will's patch changed arm64 (later copied into riscv) to avoid that risk by adding the overhead out of caution, and avoid having to audit and fix all drivers.How would you envisage fixing a driver for this? There were two issues I tried to address by moving from invalidate to clean on arm64: 1. If the DMA transfer didn't write every cacheline in the buffer, then we could expose stale data in the gaps. 2. If the buffer has a pre-existing userspace mapping, then we expose stale data during the window between the DMA map() call and the DMA itself. Fixing (1) in the driver would presumably require it to walk through the buffer after the transfer and zero all the gaps, with an appreciation for the cache writeback granule (!= cacheline size) and then (somehow) clean those parts back to the PoC. Is that something any drivers attempt today?
I'm not aware of any driver doing this, but also haven't tried looking for them. I think the usual assumption is that a driver asking for a variable-length reply should ensure that it doesn't access of the data that was not returned, and that the driver understands which parts were received. One thing that the arm32 implementation (but not any others as far as IIRC) does is to do a writeback+invalidate for any partial cache lines passed into dma_sync_*(), but this of course does not handle short transfers. We had at some point discussed using KASAN to debug these better: mark any memory that is passed to a device as unaccessible through the DMA mapping API (rounded up to full cache lines), and then mark the data as accessible again during the sync to the CPU (not rounding up). As long as the driver only passes the actually received size into dma_sync_single_for_cpu(), any later access would trigger a KASAN assertion.
Fixing (2) in the driver would presumably require ruling out the possibility of a user alias, which sounds hard and possibly ABI breaking for some drivers (depending on how they manage their buffers).
There are not that many subsystems that do streaming DMA into user-mapped buffers, so I also can't think of any good example here where things would actually go wrong in practice. Have you been able to find an example that runs into this scenario? Block drivers always transfer entire pages, and I don't think you can access a page until a transfer from userspace has completed. GPU and media drivers might be affected, but it looks like those usually use coherent mappings.
quoted
- actually measure the performance overhead: you already did the work to test this on three separate arm implementations but did not share performance numbers. Can you quantify how much this costs us on the hardware you used?It's worth noting that many Arm CPUs upgrade invalidate to clean+invalidate (either due to the micro-architecture, errata or because of virtualisation).
Right.
Arnd