Skip to contentSkip to conversation

arm64: swiotlb: Reduce the default size if no ZONE_DMA bouncing needed

PATCH Applied Catalin Marinas 2 participants 4 messages Oct 6, 2023 – Oct 13, 2023 arm64

lore
Share & tools
lore

Catalin Marinas

author maintainer
#0 lore raw

PATCH 1/1 arm64: swiotlb: Reduce the default size if no ZONE_DMA bouncing needed

With CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC enabled, the arm64 kernel still allocates the default SWIOTLB buffer (64MB) even if ZONE_DMA is disabled or all the RAM fits into this zone. However, this potentially wastes a non-negligible amount of memory on platforms with little RAM.

Reduce the SWIOTLB size to 1MB per 1GB of RAM if only needed for kmalloc() buffer bouncing.

---
 arch/arm64/mm/init.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +9 -1
index 8a0f8604348b..54ee1a4868c2 100644
@@ -493,8 +493,16 @@ void __init mem_init(void)
 {
 	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
 
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC))
+	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
+		/*
+		 * If no bouncing needed for ZONE_DMA, reduce the swiotlb
+		 * buffer for kmalloc() bouncing to 1MB per 1GB of RAM.
+		 */
+		unsigned long size =
+			ALIGN(memblock_phys_mem_size(), SZ_1G) >> 10;
+		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
 		swiotlb = true;
+	}
 
 	swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
 

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#1 lore raw
Quoted 23 lines · Catalin Marinas

With CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC enabled, the arm64 kernel still allocates the default SWIOTLB buffer (64MB) even if ZONE_DMA is disabled or all the RAM fits into this zone. However, this potentially wastes a non-negligible amount of memory on platforms with little RAM.

Reduce the SWIOTLB size to 1MB per 1GB of RAM if only needed for kmalloc() buffer bouncing.

---
  arch/arm64/mm/init.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index 8a0f8604348b..54ee1a4868c2 100644
@@ -493,8 +493,16 @@ void __init mem_init(void)
  {

arch/arm64/mm/init.c void __init mem_init(void) · line 502

Quoted 10 lines
arch/arm64/mm/init.c +7 -1
  	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
 
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC))
+	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
+		/*
+		 * If no bouncing needed for ZONE_DMA, reduce the swiotlb
+		 * buffer for kmalloc() bouncing to 1MB per 1GB of RAM.
+		 */
+		unsigned long size =
+			ALIGN(memblock_phys_mem_size(), SZ_1G) >> 10;

Hmm, I wondered if DIV_ROUND_UP(memblock_phys_mem_size(), 1024) might be any easier, but by the time I've typed it out it's still just as long :)

Either way then,

Reviewed-by Robin Murphy <[email protected]>

arch/arm64/mm/init.c void __init mem_init(void) · line 508

Quoted 6 lines
arch/arm64/mm/init.c +2 -0
+		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
  		swiotlb = true;
+	}
 
  	swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
 

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author maintainer
#2 lore raw
Quoted 28 lines · Robin Murphy
Quoted 27 lines · Catalin Marinas

With CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC enabled, the arm64 kernel still allocates the default SWIOTLB buffer (64MB) even if ZONE_DMA is disabled or all the RAM fits into this zone. However, this potentially wastes a non-negligible amount of memory on platforms with little RAM.

Reduce the SWIOTLB size to 1MB per 1GB of RAM if only needed for kmalloc() buffer bouncing.

---
  arch/arm64/mm/init.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +2 -1
index 8a0f8604348b..54ee1a4868c2 100644
@@ -493,8 +493,16 @@ void __init mem_init(void)
  {
  	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC))
+	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
+		/*
Quoted 8 lines

arch/arm64/mm/init.c void __init mem_init(void) · line 502

Quoted 5 lines
arch/arm64/mm/init.c +5 -0
+		 * If no bouncing needed for ZONE_DMA, reduce the swiotlb
+		 * buffer for kmalloc() bouncing to 1MB per 1GB of RAM.
+		 */
+		unsigned long size =
+			ALIGN(memblock_phys_mem_size(), SZ_1G) >> 10;

Hmm, I wondered if DIV_ROUND_UP(memblock_phys_mem_size(), 1024) might be any easier, but by the time I've typed it out it's still just as long :)

It's not the same result as it's rounded up to the nearest KB rather than MB. But this may work as well. I just made up this number really.

Signature · 6 lines
Catalin

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
author maintainer Applied
#3 lore raw
In reply to Catalin Marinas
Quoted 9 lines · Catalin Marinas

With CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC enabled, the arm64 kernel still allocates the default SWIOTLB buffer (64MB) even if ZONE_DMA is disabled or all the RAM fits into this zone. However, this potentially wastes a non-negligible amount of memory on platforms with little RAM.

Reduce the SWIOTLB size to 1MB per 1GB of RAM if only needed for kmalloc() buffer bouncing.

[...]

Applied to arm64 (for-next/misc), thanks!

Also changed the division to DIV_ROUND_UP() as per Robin's suggestion.

[1/1] arm64: swiotlb: Reduce the default size if no ZONE_DMA bouncing needed

      https://git.kernel.org/arm64/c/65033574ade9
Signature · 7 lines
Catalin


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel