Skip to contentSkip to conversation

ARM: dma-mapping: split cache maintenance at highmem boundary

PATCH In review Karl Mehltretter 3 participants 4 messages Sep 9 – Sep 10 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: dma-mapping: split cache maintenance at highmem boundary

dma_cache_maint_page() processes highmem pages one at a time, but passes the complete remaining range to a cache operation when the current page is lowmem. If a physically contiguous range starts in lowmem and crosses into highmem, the operation continues beyond high_memory through virtual addresses which do not map the highmem pages.

On a Raspberry Pi 400 running an ARM32 LPAE kernel, an 8 KiB scatterlist entry crossing this boundary caused a deterministic data abort at f0000000 in v7_dma_clean_range().

Limit the crossing lowmem iteration to the end of its page. Subsequent iterations advance page by page until the existing highmem path takes over. Keep the bulk operation for ranges contained in lowmem.

Fixes 43377453af83 ("[ARM] introduce dma_cache_maint_page()")
Assisted-by LLM

---
Testing:

- Raspberry Pi 400, ARM32 LPAE, 8 KiB scatterlist entry crossing the 0x30000000 lowmem/highmem boundary: all four unpatched boots panicked at f0000000 in v7_dma_clean_range(). All four patched boots completed. Three 100-iteration patched runs checked 409,600 bytes in each DMA direction with zero mismatches.
- W=1 builds of arch/arm/mm/dma-mapping.o passed with CONFIG_HIGHMEM=y and CONFIG_HIGHMEM=n. They emitted only five pre-existing kernel-doc warnings.

The hardware kernels used base 986c24e0fe44. dma-mapping.c is unchanged between that commit and this patch's base, 893e11787f78.

 arch/arm/mm/dma-mapping.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm/mm/dma-mapping.c +3 -0
index 7761099dde9e..887209a09226 100644
@@ -663,6 +663,9 @@ static void dma_cache_maint_page(phys_addr_t phys, size_t size,
 		} else {
 			phys += offset;
 			vaddr = phys_to_virt(phys);
+			if (IS_ENABLED(CONFIG_HIGHMEM) &&
+			    len > (unsigned long)high_memory - (unsigned long)vaddr)
+				len = PAGE_SIZE - offset;
 			op(vaddr, len, dir);
 		}
 		offset = 0;
Signature · 1 line
2.53.0
#1 lore raw
Quoted 10 lines · Karl Mehltretter

dma_cache_maint_page() processes highmem pages one at a time, but passes the complete remaining range to a cache operation when the current page is lowmem. If a physically contiguous range starts in lowmem and crosses into highmem, the operation continues beyond high_memory through virtual addresses which do not map the highmem pages.

On a Raspberry Pi 400 running an ARM32 LPAE kernel, an 8 KiB scatterlist entry crossing this boundary caused a deterministic data abort at f0000000 in v7_dma_clean_range().

Quoted 8 lines

Limit the crossing lowmem iteration to the end of its page. Subsequent iterations advance page by page until the existing highmem path takes over. Keep the bulk operation for ranges contained in lowmem.

Fixes 43377453af83 ("[ARM] introduce dma_cache_maint_page()")
Assisted-by LLM

Hi Karl,

I can see how the fix addresses the problem, but I don't yet see how you can arrive in this situation. What type of memory allocation can produce a physically contiguous page range from multiple zones?

Arnd

#2 lore raw
Quoted 5 lines · Arnd Bergmann

I can see how the fix addresses the problem, but I don't yet see how you can arrive in this situation. What type of memory allocation can produce a physically contiguous page range from multiple zones?

Hi Arnd,

You're right that a buddy allocation cannot span zones. I found this issue during code review while investigating another ARM DMA issue. I plan to post a patch for that soon.

My Pi test deliberately reserved the last lowmem and first highmem pages in DT. It confirms the crash with a crossing SG entry, but I have not found an ordinary workload that creates one. I should have made that clear in the changelog.

In a follow-up QEMU test, sg_alloc_table_from_pages() merged the reserved pair into one 8192-byte entry. It checks that the pages are physically adjacent but does not check page_zone().

I also searched a bit for a real-world example. Rockchip's vendor 4.4 boot-logo code looks like a possible case: it builds an SG table from firmware-reserved pages and calls dma_map_sg() when no display IOMMU is used. Product logs show the boot-logo feature in use, but I found no record of its reserved memory crossing the lowmem/highmem boundary. Such a crossing seems unlikely.

I still think the change is worthwhile. For v2, I will update the changelog to make the test setup and its limits clear. I may also drop the
Cc: stable tag.

Thanks,
Karl

#3 lore raw
Quoted 22 lines · Karl Mehltretter
Quoted 5 lines · Arnd Bergmann

I can see how the fix addresses the problem, but I don't yet see how you can arrive in this situation. What type of memory allocation can produce a physically contiguous page range from multiple zones?

Hi Arnd,

You're right that a buddy allocation cannot span zones. I found this issue during code review while investigating another ARM DMA issue. I plan to post a patch for that soon.

My Pi test deliberately reserved the last lowmem and first highmem pages in DT. It confirms the crash with a crossing SG entry, but I have not found an ordinary workload that creates one. I should have made that clear in the changelog.

In a follow-up QEMU test, sg_alloc_table_from_pages() merged the reserved pair into one 8192-byte entry. It checks that the pages are physically adjacent but does not check page_zone().

Quoted 6 lines

I also searched a bit for a real-world example. Rockchip's vendor 4.4 boot-logo code looks like a possible case: it builds an SG table from firmware-reserved pages and calls dma_map_sg() when no display IOMMU is used. Product logs show the boot-logo feature in use, but I found no record of its reserved memory crossing the lowmem/highmem boundary. Such a crossing seems unlikely.

That is completely bogus to begin with though. Calling dma_map_* on reserved non-kernel memory is liable to blow up in various ways anyway (e.g. in sparsemem page_to_pfn/pfn_to_page) - what's the justification for being more lenient towards one particular corner of invalid usage?

Thanks,
Robin.

Quoted 6 lines

I still think the change is worthwhile. For v2, I will update the changelog to make the test setup and its limits clear. I may also drop the
Cc: stable tag.

Thanks,
Karl