[PATCH v2 08/14] arm64: Enforce bounce buffers for realm DMA

Steven Price posted 14 patches 1 year, 10 months ago
There is a newer version of this series
[PATCH v2 08/14] arm64: Enforce bounce buffers for realm DMA
Posted by Steven Price 1 year, 10 months ago
Within a realm guest it's not possible for a device emulated by the VMM
to access arbitrary guest memory. So force the use of bounce buffers to
ensure that the memory the emulated devices are accessing is in memory
which is explicitly shared with the host.

Co-developed-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Steven Price <steven.price@arm.com>
---
 arch/arm64/kernel/rsi.c |  2 ++
 arch/arm64/mm/init.c    | 11 +++++++++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/rsi.c b/arch/arm64/kernel/rsi.c
index 159bc428c77b..5c8ed3aaa35f 100644
--- a/arch/arm64/kernel/rsi.c
+++ b/arch/arm64/kernel/rsi.c
@@ -5,6 +5,8 @@
 
 #include <linux/jump_label.h>
 #include <linux/memblock.h>
+#include <linux/swiotlb.h>
+
 #include <asm/rsi.h>
 
 struct realm_config __attribute((aligned(PAGE_SIZE))) config;
diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
index 786fd6ce5f17..01a2e3ce6921 100644
--- a/arch/arm64/mm/init.c
+++ b/arch/arm64/mm/init.c
@@ -370,7 +370,9 @@ void __init bootmem_init(void)
  */
 void __init mem_init(void)
 {
-	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
+	bool swiotlb = (max_pfn > PFN_DOWN(arm64_dma_phys_limit));
+
+	swiotlb |= is_realm_world();
 
 	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
 		/*
@@ -383,7 +385,12 @@ void __init mem_init(void)
 		swiotlb = true;
 	}
 
-	swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
+	if (is_realm_world()) {
+		swiotlb_init(swiotlb, SWIOTLB_VERBOSE | SWIOTLB_FORCE);
+		swiotlb_update_mem_attributes();
+	} else {
+		swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
+	}
 
 	/* this will put all unused low memory onto the freelists */
 	memblock_free_all();
-- 
2.34.1
Re: [PATCH v2 08/14] arm64: Enforce bounce buffers for realm DMA
Posted by Catalin Marinas 1 year, 9 months ago
On Fri, Apr 12, 2024 at 09:42:07AM +0100, Steven Price wrote:
> diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
> index 786fd6ce5f17..01a2e3ce6921 100644
> --- a/arch/arm64/mm/init.c
> +++ b/arch/arm64/mm/init.c
> @@ -370,7 +370,9 @@ void __init bootmem_init(void)
>   */
>  void __init mem_init(void)
>  {
> -	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
> +	bool swiotlb = (max_pfn > PFN_DOWN(arm64_dma_phys_limit));

This series tends to add unnecessary brackets.

> +
> +	swiotlb |= is_realm_world();

I first thought we wouldn't need this, just passing 'true' further down
but I guess you want to avoid reducing the bounce buffer size when it's
only needed for kmalloc() bouncing.

>  
>  	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
>  		/*
> @@ -383,7 +385,12 @@ void __init mem_init(void)
>  		swiotlb = true;
>  	}
>  
> -	swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
> +	if (is_realm_world()) {
> +		swiotlb_init(swiotlb, SWIOTLB_VERBOSE | SWIOTLB_FORCE);
> +		swiotlb_update_mem_attributes();
> +	} else {
> +		swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
> +	}

Just do this higher up and avoid calling is_realm_world() twice:

	unsigned int flags = SWIOTLB_VERBOSE;

	...

	if (is_realm_world()) {
		swiotlb = true;
		flags |= SWIOTLB_FORCE;
	}

	...

	swiotlb_init(swiotlb, flags);

-- 
Catalin