300-v6.7-arm64-swiotlb-Reduce-the-default-size-if-no-ZONE_DMA.patch 1.7 KB

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