1 From 65033574ade97afccba074d837fd269903a83a9a Mon Sep 17 00:00:00 2001
2 From: Catalin Marinas <catalin.marinas@arm.com>
3 Date: Thu, 5 Oct 2023 16:40:30 +0100
4 Subject: [PATCH] arm64: swiotlb: Reduce the default size if no ZONE_DMA
7 With CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC enabled, the arm64 kernel still
8 allocates the default SWIOTLB buffer (64MB) even if ZONE_DMA is disabled
9 or all the RAM fits into this zone. However, this potentially wastes a
10 non-negligible amount of memory on platforms with little RAM.
12 Reduce the SWIOTLB size to 1MB per 1GB of RAM if only needed for
13 kmalloc() buffer bouncing.
15 Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
16 Suggested-by: Ross Burton <ross.burton@arm.com>
17 Cc: Ross Burton <ross.burton@arm.com>
18 Cc: Will Deacon <will@kernel.org>
19 Reviewed-by: Robin Murphy <robin.murphy@arm.com>
21 arch/arm64/mm/init.c | 11 ++++++++++-
22 1 file changed, 10 insertions(+), 1 deletion(-)
24 --- a/arch/arm64/mm/init.c
25 +++ b/arch/arm64/mm/init.c
27 #include <linux/nodemask.h>
28 #include <linux/initrd.h>
29 #include <linux/gfp.h>
30 +#include <linux/math.h>
31 #include <linux/memblock.h>
32 #include <linux/sort.h>
34 @@ -493,8 +494,16 @@ void __init mem_init(void)
36 bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
38 - if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC))
39 + if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
41 + * If no bouncing needed for ZONE_DMA, reduce the swiotlb
42 + * buffer for kmalloc() bouncing to 1MB per 1GB of RAM.
44 + unsigned long size =
45 + DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
46 + swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
50 swiotlb_init(swiotlb, SWIOTLB_VERBOSE);