generic: 6.6: backport arm64 swiotlb default size reduction
[openwrt/staging/stintel.git] / target / linux / generic / backport-6.6 / 300-v6.7-arm64-swiotlb-Reduce-the-default-size-if-no-ZONE_DMA.patch
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
5 bouncing needed
6
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.
11
12 Reduce the SWIOTLB size to 1MB per 1GB of RAM if only needed for
13 kmalloc() buffer bouncing.
14
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>
20 ---
21 arch/arm64/mm/init.c | 11 ++++++++++-
22 1 file changed, 10 insertions(+), 1 deletion(-)
23
24 --- a/arch/arm64/mm/init.c
25 +++ b/arch/arm64/mm/init.c
26 @@ -16,6 +16,7 @@
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>
33 #include <linux/of.h>
34 @@ -493,8 +494,16 @@ void __init mem_init(void)
35 {
36 bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
37
38 - if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC))
39 + if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
40 + /*
41 + * If no bouncing needed for ZONE_DMA, reduce the swiotlb
42 + * buffer for kmalloc() bouncing to 1MB per 1GB of RAM.
43 + */
44 + unsigned long size =
45 + DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
46 + swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
47 swiotlb = true;
48 + }
49
50 swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
51