Bug in add_dma_entry()'s debugging code

From: Alan Stern
Date: Mon Nov 27 2023 - 11:02:26 EST


Among other things, add_dma_entry() in kernel/dma/debug.c prints an
error message when it sees two overlapping FROM_DEVICE DMA mappings.
The actual overlap detection is performed by a separate routine,
active_cacheline_insert(). But the criterion this routine uses is
wrong.

All it looks for is mappings that start on the same cache line. However
on architectures that have cache-coherent DMA (such as x86), touching
the same cache line does not mean that two DMA mappings will interfere
with each other. To truly overlap, they would have to touch the same
_bytes_.

The routine does not check for this, and consequently we get error
messages about overlapping mappings when in fact there is no overlap.
This bug has been reported in

https://bugzilla.kernel.org/show_bug.cgi?id=215740

How should this be fixed? Since the check done in add_dma_entry() is
completely invalid for x86 and similar architectures, should it simply
be removed for them? Or should the check be enhanced to look for
byte-granularity overlap?

Alan Stern

PS: As a separate issue, active_cacheline_insert() fails to detect
overlap in situations where a mapping occupies more than one cache line.
For example, if mapping A uses lines N and N+1 and mapping B uses line
N+1, no overlap will be detected because the radix-tree keys for A and B
will be different (N vs. N+1).