Skip to contentSkip to conversation

dma-mapping: use bit masking to check VM_DMA_COHERENT

PATCH In review Yosry Ahmed 2 participants 3 messages Aug 10, 2024 – Aug 12, 2024 DMA mapping helpers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma-mapping: use bit masking to check VM_DMA_COHERENT

In dma_common_find_pages(), area->flags are compared directly with VM_DMA_COHERENT. This works because VM_DMA_COHERENT is the only set flag.

During development of a new feature (ASI [1]), a new VM flag is introduced, and that flag can be injected into VM_DMA_COHERENT mappings (among others). The presence of that flag caused dma_common_find_pages() to return NULL for VM_DMA_COHERENT addresses, leading to a lot of problems ending in crashing during boot. It took a bit of time to figure this problem out.

It was a mistake to inject a VM flag to begin with, but it took a significant amount of debugging to figure out the problem. Most users of area->flags use bitmasking rather than equivalency to check for flags. Update dma_common_find_pages() and dma_common_free_remap() to do the same, which would have avoided the boot crashing. Instead, add a warning in dma_common_find_pages() if any extra VM flags are set to catch such problems more easily during development.

No functional change intended.

[1]https://lore.kernel.org/lkml/[email protected]/

---
 kernel/dma/remap.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/dma/remap.c +4 -2
index 27596f3b4aef3..9e2afad1c6152 100644
@@ -10,8 +10,10 @@ struct page **dma_common_find_pages(void *cpu_addr)
 {
 	struct vm_struct *area = find_vm_area(cpu_addr);
 
-	if (!area || area->flags != VM_DMA_COHERENT)
+	if (!area || !(area->flags & VM_DMA_COHERENT))
 		return NULL;
+	WARN(area->flags != VM_DMA_COHERENT,
+	     "unexpected flags in area: %p\n", cpu_addr);
 	return area->pages;
 }
 
@@ -61,7 +63,7 @@ void dma_common_free_remap(void *cpu_addr, size_t size)
 {
 	struct vm_struct *area = find_vm_area(cpu_addr);
 
-	if (!area || area->flags != VM_DMA_COHERENT) {
+	if (!area || !(area->flags & VM_DMA_COHERENT)) {
 		WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
 		return;
 	}
Signature · 1 line
2.46.0.76.ge559c4bf1a-goog
#1 lore raw

Thanks Yosry, this looks good to me. I don't see a real urgency here, so I plan to add it to the dma-mapping tree for 6.12.

#2 lore raw
Quoted 4 lines · Christoph Hellwig

Thanks Yosry, this looks good to me. I don't see a real urgency here, so I plan to add it to the dma-mapping tree for 6.12.

Yeah no urgency here, thanks for the swift response, Cristoph!