Skip to contentSkip to conversation

arm64/coco: Convert pKVM to a CC platform

PATCH Applied Mostafa Saleh 5 participants 19 messages Jun 3 – Jul 22 arm64

lore
Share & tools
lore
#0 lore raw

cover arm64/coco: Convert pKVM to a CC platform

The goal of series is to convert pKVM as a CC platform similar to ARM CCA, that makes CC guests consistently discovered which avoid breaking it by the ongoing DMA rework [1]

The first patch is a minor clean up I noticed while reading the code.

And the second patch is a bug fix found by Sashiko.

The third patch is the main one.

[1]https://lore.kernel.org/all/[email protected]/

Mostafa Saleh (3):
arm64/mm: Simplify SWIOTLB setup in arch_mm_preinit() drivers/virt: pkvm: Fix end calculation in mmio_guard_ioremap_hook() arm64/coco: Add pKVM as a CC platform

 arch/arm64/include/asm/hypervisor.h           | 13 ++++++++++
 arch/arm64/include/asm/mem_encrypt.h          |  3 ++-
 arch/arm64/kernel/rsi.c                       | 12 ---------
 arch/arm64/mm/init.c                          | 26 +++++++++++++------
 drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c |  5 +++-
 5 files changed, 37 insertions(+), 22 deletions(-)
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
#1 lore raw

PATCH 1/3 arm64/mm: Simplify SWIOTLB setup in arch_mm_preinit()

At the moment, arch_mm_preinit() checks if the system has limited addressing or is running under CCA to enable SWIOTLB, only after to be forced to true anyway if it was false due to CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC being unconditionally true for arm64.

Simplify this logic, by making it clear that SWIOTLB is always used but its size depends on the address layout of the system.

---
 arch/arm64/mm/init.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
arch/arm64/mm/init.c +4 -7
index 97987f850a33..fcc9c05a8fe6 100644
@@ -336,25 +336,22 @@ void __init arch_setup_zero_pages(void)
 void __init arch_mm_preinit(void)
 {
 	unsigned int flags = SWIOTLB_VERBOSE;
-	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
+	bool limited_addressing = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
 
 	if (is_realm_world()) {
-		swiotlb = true;
 		flags |= SWIOTLB_FORCE;
-	}
-
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
+	} else if (!limited_addressing) {
 		/*
 		 * If no bouncing needed for ZONE_DMA, reduce the swiotlb
 		 * buffer for kmalloc() bouncing to 1MB per 1GB of RAM.
 		 */
 		unsigned long size =
 			DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
+
 		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
-		swiotlb = true;
 	}
 
-	swiotlb_init(swiotlb, flags);
+	swiotlb_init(true, flags);
 
 	/*
 	 * Check boundaries twice: Some fundamental inconsistencies can be
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
#2 lore raw

Re: [PATCH 1/3] arm64/mm: Simplify SWIOTLB setup in arch_mm_preinit()

Hi,

Quoted 22 lines · Mostafa Saleh

At the moment, arch_mm_preinit() checks if the system has limited addressing or is running under CCA to enable SWIOTLB, only after to be forced to true anyway if it was false due to CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC being unconditionally true for arm64.

Simplify this logic, by making it clear that SWIOTLB is always used but its size depends on the address layout of the system.

---
 arch/arm64/mm/init.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
arch/arm64/mm/init.c +0 -0
index 97987f850a33..fcc9c05a8fe6 100644
@@ -336,25 +336,22 @@ void __init arch_setup_zero_pages(void)
 void __init arch_mm_preinit(void)
 {
 	unsigned int flags = SWIOTLB_VERBOSE;

arch/arm64/mm/init.c void __init arch_setup_zero_pages(void) · line 343

Quoted 10 lines
arch/arm64/mm/init.c +2 -5
-	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
+	bool limited_addressing = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
 
 	if (is_realm_world()) {
-		swiotlb = true;
 		flags |= SWIOTLB_FORCE;
-	}
-
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
+	} else if (!limited_addressing) {

Can realm_world() coexist with limited_addressing? Then the size adjustment is still required.

I'd also make this more explicit:

	if (max_pfn > PFN_DOWN(arm64_dma_phys_limit))
Quoted 19 lines
arch/arm64/mm/init.c +2 -2
 		/*
 		 * If no bouncing needed for ZONE_DMA, reduce the swiotlb
 		 * buffer for kmalloc() bouncing to 1MB per 1GB of RAM.
 		 */
 		unsigned long size =
 			DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
+
 		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
-		swiotlb = true;
 	}
 
-	swiotlb_init(swiotlb, flags);
+	swiotlb_init(true, flags);
 
 	/*
 	 * Check boundaries twice: Some fundamental inconsistencies can be
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
Signature · 2 lines
Sincerely yours,
Mike.
#3 lore raw

Re: [PATCH 1/3] arm64/mm: Simplify SWIOTLB setup in arch_mm_preinit()

Hi Mike,

Quoted 28 lines · Mike Rapoport

Hi,

Quoted 25 lines · Mostafa Saleh

At the moment, arch_mm_preinit() checks if the system has limited addressing or is running under CCA to enable SWIOTLB, only after to be forced to true anyway if it was false due to CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC being unconditionally true for arm64.

Simplify this logic, by making it clear that SWIOTLB is always used but its size depends on the address layout of the system.

---
 arch/arm64/mm/init.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
arch/arm64/mm/init.c +1 -1
index 97987f850a33..fcc9c05a8fe6 100644
@@ -336,25 +336,22 @@ void __init arch_setup_zero_pages(void)
 void __init arch_mm_preinit(void)
 {
 	unsigned int flags = SWIOTLB_VERBOSE;
-	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
+	bool limited_addressing = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
 
Quoted 10 lines

arch/arm64/mm/init.c void __init arch_setup_zero_pages(void) · line 343

Quoted 7 lines
arch/arm64/mm/init.c +1 -4
 	if (is_realm_world()) {
-		swiotlb = true;
 		flags |= SWIOTLB_FORCE;
-	}
-
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
+	} else if (!limited_addressing) {

Can realm_world() coexist with limited_addressing? Then the size adjustment is still required.

My guess would be no, my understanding is that CCA guests have niche hardware.

However, the check for limited_addressing is negated, we adjust the SWIOTLB to be smaller if no bouncing is needed.
So, if this is a realm world, address space layout is irrelevant as we keep the original size which is larger.

Otherwise, this alters the current behaviour.

Quoted 4 lines

I'd also make this more explicit:

	if (max_pfn > PFN_DOWN(arm64_dma_phys_limit))

Sure, I can drop the bool.

Thanks,
Mostafa

Quoted 24 lines
Quoted 20 lines
arch/arm64/mm/init.c +2 -2
 		/*
 		 * If no bouncing needed for ZONE_DMA, reduce the swiotlb
 		 * buffer for kmalloc() bouncing to 1MB per 1GB of RAM.
 		 */
 		unsigned long size =
 			DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
+
 		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
-		swiotlb = true;
 	}
 
-	swiotlb_init(swiotlb, flags);
+	swiotlb_init(true, flags);
 
 	/*
 	 * Check boundaries twice: Some fundamental inconsistencies can be
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
Signature · 2 lines
Sincerely yours,
Mike.
#4 lore raw

Re: [PATCH 1/3] arm64/mm: Simplify SWIOTLB setup in arch_mm_preinit()

Quoted 42 lines · Mostafa Saleh

Hi Mike,

Quoted 28 lines · Mike Rapoport

Hi,

Quoted 25 lines · Mostafa Saleh

At the moment, arch_mm_preinit() checks if the system has limited addressing or is running under CCA to enable SWIOTLB, only after to be forced to true anyway if it was false due to CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC being unconditionally true for arm64.

Simplify this logic, by making it clear that SWIOTLB is always used but its size depends on the address layout of the system.

---
 arch/arm64/mm/init.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
arch/arm64/mm/init.c +1 -1
index 97987f850a33..fcc9c05a8fe6 100644
@@ -336,25 +336,22 @@ void __init arch_setup_zero_pages(void)
 void __init arch_mm_preinit(void)
 {
 	unsigned int flags = SWIOTLB_VERBOSE;
-	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
+	bool limited_addressing = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
 
Quoted 10 lines

arch/arm64/mm/init.c void __init arch_setup_zero_pages(void) · line 343

Quoted 7 lines
arch/arm64/mm/init.c +1 -4
 	if (is_realm_world()) {
-		swiotlb = true;
 		flags |= SWIOTLB_FORCE;
-	}
-
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
+	} else if (!limited_addressing) {

Can realm_world() coexist with limited_addressing? Then the size adjustment is still required.

Quoted 8 lines

My guess would be no, my understanding is that CCA guests have niche hardware.

However, the check for limited_addressing is negated, we adjust the SWIOTLB to be smaller if no bouncing is needed.
So, if this is a realm world, address space layout is irrelevant as we keep the original size which is larger.

That’s correct. Realm guests need swiotlb to always be enabled, and we should not reduce the size of the pool.

Quoted 10 lines

Otherwise, this alters the current behaviour.

Quoted 4 lines

I'd also make this more explicit:

	if (max_pfn > PFN_DOWN(arm64_dma_phys_limit))

Sure, I can drop the bool.

-aneesh

Reviewed-by on v1 1/3
#5 lore raw

Re: [PATCH 1/3] arm64/mm: Simplify SWIOTLB setup in arch_mm_preinit()

In reply to Mostafa Saleh
Quoted 9 lines · Mostafa Saleh

At the moment, arch_mm_preinit() checks if the system has limited addressing or is running under CCA to enable SWIOTLB, only after to be forced to true anyway if it was false due to CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC being unconditionally true for arm64.

Simplify this logic, by making it clear that SWIOTLB is always used but its size depends on the address layout of the system.

Quoted 41 lines
---
 arch/arm64/mm/init.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
arch/arm64/mm/init.c +4 -7
index 97987f850a33..fcc9c05a8fe6 100644
@@ -336,25 +336,22 @@ void __init arch_setup_zero_pages(void)
 void __init arch_mm_preinit(void)
 {
 	unsigned int flags = SWIOTLB_VERBOSE;
-	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
+	bool limited_addressing = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
 
 	if (is_realm_world()) {
-		swiotlb = true;
 		flags |= SWIOTLB_FORCE;
-	}
-
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
+	} else if (!limited_addressing) {
 		/*
 		 * If no bouncing needed for ZONE_DMA, reduce the swiotlb
 		 * buffer for kmalloc() bouncing to 1MB per 1GB of RAM.
 		 */
 		unsigned long size =
 			DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
+
 		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
-		swiotlb = true;
 	}
 
-	swiotlb_init(swiotlb, flags);
+	swiotlb_init(true, flags);
 
 	/*
 	 * Check boundaries twice: Some fundamental inconsistencies can be
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
maintainer Reviewed-by on v1 1/3
#6 lore raw

Re: [PATCH 1/3] arm64/mm: Simplify SWIOTLB setup in arch_mm_preinit()

In reply to Mostafa Saleh
Quoted 22 lines · Mostafa Saleh

At the moment, arch_mm_preinit() checks if the system has limited addressing or is running under CCA to enable SWIOTLB, only after to be forced to true anyway if it was false due to CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC being unconditionally true for arm64.

Simplify this logic, by making it clear that SWIOTLB is always used but its size depends on the address layout of the system.

---
 arch/arm64/mm/init.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
arch/arm64/mm/init.c +0 -0
index 97987f850a33..fcc9c05a8fe6 100644
@@ -336,25 +336,22 @@ void __init arch_setup_zero_pages(void)
 void __init arch_mm_preinit(void)
 {
 	unsigned int flags = SWIOTLB_VERBOSE;

arch/arm64/mm/init.c void __init arch_setup_zero_pages(void) · line 343

Quoted 10 lines
arch/arm64/mm/init.c +2 -5
-	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
+	bool limited_addressing = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
 
 	if (is_realm_world()) {
-		swiotlb = true;
 		flags |= SWIOTLB_FORCE;
-	}
-
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
+	} else if (!limited_addressing) {

I'm fine with implementing Mike's comment on doing the check inline.

I recall some people still comment out the dma kmalloc bounce option:

https://github.com/raspberrypi/linux/commit/a07564753639fe5ffb3f59d7364f14e6e68e3e9e

(there was no way I could convince them it's worse memory wastage without the option)

So happy to make it slightly harder for them ;).

#7 lore raw

Re: [PATCH 1/3] arm64/mm: Simplify SWIOTLB setup in arch_mm_preinit()

Quoted 38 lines · Catalin Marinas
Quoted 22 lines · Mostafa Saleh

At the moment, arch_mm_preinit() checks if the system has limited addressing or is running under CCA to enable SWIOTLB, only after to be forced to true anyway if it was false due to CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC being unconditionally true for arm64.

Simplify this logic, by making it clear that SWIOTLB is always used but its size depends on the address layout of the system.

---
 arch/arm64/mm/init.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
arch/arm64/mm/init.c +0 -0
index 97987f850a33..fcc9c05a8fe6 100644
@@ -336,25 +336,22 @@ void __init arch_setup_zero_pages(void)
 void __init arch_mm_preinit(void)
 {
 	unsigned int flags = SWIOTLB_VERBOSE;

arch/arm64/mm/init.c void __init arch_setup_zero_pages(void) · line 343

Quoted 10 lines
arch/arm64/mm/init.c +2 -5
-	bool swiotlb = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
+	bool limited_addressing = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
 
 	if (is_realm_world()) {
-		swiotlb = true;
 		flags |= SWIOTLB_FORCE;
-	}
-
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb) {
+	} else if (!limited_addressing) {

I'm fine with implementing Mike's comment on doing the check inline.

I recall some people still comment out the dma kmalloc bounce option:

Quoted 8 lines

https://github.com/raspberrypi/linux/commit/a07564753639fe5ffb3f59d7364f14e6e68e3e9e

(there was no way I could convince them it's worse memory wastage without the option)

So happy to make it slightly harder for them ;).

Thanks! Would you prefer if I send a new version removing limited_addressing?

Thanks,
Mostafa

#8 lore raw

PATCH 2/3 drivers/virt: pkvm: Fix end calculation in mmio_guard_ioremap_hook()

In reply to Mostafa Saleh

Sashiko (locally) reports a logical issues in mmio_guard_ioremap_hook()

mmio_guard_ioremap_hook() attempts to handle unaligned addresses and sizes. However, aligning the start address before adding the size, might shift the end to the page before.

Fixes 0f1269495800 ("drivers/virt: pkvm: Intercept ioremap using pKVM MMIO_GUARD hypercall")
---
 drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +1 -1
index 4230b817a80b..d66291def0f4 100644
@@ -82,8 +82,8 @@ static int mmio_guard_ioremap_hook(phys_addr_t phys, size_t size,
 	if (protval != PROT_DEVICE_nGnRE && protval != PROT_DEVICE_nGnRnE)
 		return 0;
 
+	end = PAGE_ALIGN(phys + size);
 	phys = PAGE_ALIGN_DOWN(phys);
-	end = phys + PAGE_ALIGN(size);
 
 	while (phys < end) {
 		const int func_id = ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_FUNC_ID;
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
maintainer Reviewed-by on v1 2/3
#9 lore raw

Re: [PATCH 2/3] drivers/virt: pkvm: Fix end calculation in mmio_guard_ioremap_hook()

Quoted 8 lines · Mostafa Saleh

Sashiko (locally) reports a logical issues in mmio_guard_ioremap_hook()

mmio_guard_ioremap_hook() attempts to handle unaligned addresses and sizes. However, aligning the start address before adding the size, might shift the end to the page before.

Fixes 0f1269495800 ("drivers/virt: pkvm: Intercept ioremap using pKVM MMIO_GUARD hypercall")
#10 lore raw

PATCH 3/3 arm64/coco: Add pKVM as a CC platform

In reply to Mostafa Saleh

pKVM does support memory encryption, expose that to the rest of the kernel through cc_platform_has()

At the moment, all devices inside the guest are emulated which requires its memory to be shared back to the host (decrypted), so set force_dma_unencrypted() to always return true.

Although, typically pKVM guests rely on restricted-dma-pools to bounce traffic, with this change, it is possible to solely rely on the default SWIOTLB for that (assuming the appropriate size is set from the command line)

---
This change is critical for the ongoing refactoring of the DMA-API[1] that will break protected guests under pKVM with this patch. That is due to this rework will make the state of the SWIOTLB and restricted dma pools depends on the value returned by cc_platform_has()

[1] https://lore.kernel.org/all/[email protected]/

---
 arch/arm64/include/asm/hypervisor.h           | 13 +++++++++++++
 arch/arm64/include/asm/mem_encrypt.h          |  3 ++-
 arch/arm64/kernel/rsi.c                       | 12 ------------
 arch/arm64/mm/init.c                          | 15 ++++++++++++++-
 drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c |  3 +++
 5 files changed, 32 insertions(+), 14 deletions(-)
arch/arm64/include/asm/hypervisor.h +13 -0
index a12fd897c877..8889a0ba1ec5 100644
@@ -3,6 +3,9 @@
 #define _ASM_ARM64_HYPERVISOR_H
 
 #include <asm/xen/hypervisor.h>
+#include <linux/jump_label.h>
+
+DECLARE_STATIC_KEY_FALSE(pkvm_guest);
 
 void kvm_init_hyp_services(void);
 bool kvm_arm_hyp_service_available(u32 func_id);
@@ -10,8 +13,18 @@ void kvm_arm_target_impl_cpu_init(void);
 
 #ifdef CONFIG_ARM_PKVM_GUEST
 void pkvm_init_hyp_services(void);
+
+static inline bool is_protected_kvm_guest(void)
+{
+	return static_branch_unlikely(&pkvm_guest);
+}
 #else
 static inline void pkvm_init_hyp_services(void) { };
+
+static inline bool is_protected_kvm_guest(void)
+{
+	return false;
+}
 #endif
 
 static inline void kvm_arch_init_hyp_services(void)
arch/arm64/include/asm/mem_encrypt.h +2 -1
index 314b2b52025f..636f45b4d8af 100644
@@ -2,6 +2,7 @@
 #ifndef __ASM_MEM_ENCRYPT_H
 #define __ASM_MEM_ENCRYPT_H
 
+#include <asm/hypervisor.h>
 #include <asm/rsi.h>
 
 struct device;
@@ -20,7 +21,7 @@ int realm_register_memory_enc_ops(void);
 
 static inline bool force_dma_unencrypted(struct device *dev)
 {
-	return is_realm_world();
+	return is_realm_world() || is_protected_kvm_guest();
 }
 
 /*
arch/arm64/kernel/rsi.c +0 -12
index 92160f2e57ff..25ca75ce1a4d 100644
@@ -7,7 +7,6 @@
 #include <linux/memblock.h>
 #include <linux/psci.h>
 #include <linux/swiotlb.h>
-#include <linux/cc_platform.h>
 #include <linux/platform_device.h>
 
 #include <asm/io.h>
@@ -23,17 +22,6 @@ EXPORT_SYMBOL(prot_ns_shared);
 DEFINE_STATIC_KEY_FALSE_RO(rsi_present);
 EXPORT_SYMBOL(rsi_present);
 
-bool cc_platform_has(enum cc_attr attr)
-{
-	switch (attr) {
-	case CC_ATTR_MEM_ENCRYPT:
-		return is_realm_world();
-	default:
-		return false;
-	}
-}
-EXPORT_SYMBOL_GPL(cc_platform_has);
-
 static bool rsi_version_matches(void)
 {
 	unsigned long ver_lower, ver_higher;
arch/arm64/mm/init.c +14 -1
index fcc9c05a8fe6..edcc305ec883 100644
@@ -11,6 +11,7 @@
 #include <linux/errno.h>
 #include <linux/swap.h>
 #include <linux/init.h>
+#include <linux/cc_platform.h>
 #include <linux/cache.h>
 #include <linux/mman.h>
 #include <linux/nodemask.h>
@@ -36,6 +37,7 @@
 
 #include <asm/boot.h>
 #include <asm/fixmap.h>
+#include <asm/hypervisor.h>
 #include <asm/kasan.h>
 #include <asm/kernel-pgtable.h>
 #include <asm/kvm_host.h>
@@ -338,7 +340,7 @@ void __init arch_mm_preinit(void)
 	unsigned int flags = SWIOTLB_VERBOSE;
 	bool limited_addressing = max_pfn > PFN_DOWN(arm64_dma_phys_limit);
 
-	if (is_realm_world()) {
+	if (is_realm_world() || is_protected_kvm_guest()) {
 		flags |= SWIOTLB_FORCE;
 	} else if (!limited_addressing) {
 		/*
@@ -413,6 +415,17 @@ void dump_mem_limit(void)
 	}
 }
 
+bool cc_platform_has(enum cc_attr attr)
+{
+	switch (attr) {
+	case CC_ATTR_MEM_ENCRYPT:
+		return is_realm_world() || is_protected_kvm_guest();
+	default:
+		return false;
+	}
+}
+EXPORT_SYMBOL_GPL(cc_platform_has);
+
 #ifdef CONFIG_EXECMEM
 static u64 module_direct_base __ro_after_init = 0;
 static u64 module_plt_base __ro_after_init = 0;
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +3 -0
index d66291def0f4..26fe9c3f22e3 100644
@@ -17,6 +17,7 @@
 #include <asm/hypervisor.h>
 
 static size_t pkvm_granule;
+DEFINE_STATIC_KEY_FALSE_RO(pkvm_guest);
 
 static int arm_smccc_do_one_page(u32 func_id, phys_addr_t phys)
 {
@@ -120,4 +121,6 @@ void pkvm_init_hyp_services(void)
 
 	if (kvm_arm_hyp_service_available(ARM_SMCCC_KVM_FUNC_MMIO_GUARD))
 		arm64_ioremap_prot_hook_register(&mmio_guard_ioremap_hook);
+
+	static_branch_enable(&pkvm_guest);
 }
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
#11 lore raw

Re: [PATCH 3/3] arm64/coco: Add pKVM as a CC platform

Quoted 62 lines · Mostafa Saleh

pKVM does support memory encryption, expose that to the rest of the kernel through cc_platform_has()

At the moment, all devices inside the guest are emulated which requires its memory to be shared back to the host (decrypted), so set force_dma_unencrypted() to always return true.

Although, typically pKVM guests rely on restricted-dma-pools to bounce traffic, with this change, it is possible to solely rely on the default SWIOTLB for that (assuming the appropriate size is set from the command line)

---
This change is critical for the ongoing refactoring of the DMA-API[1] that will break protected guests under pKVM with this patch. That is due to this rework will make the state of the SWIOTLB and restricted dma pools depends on the value returned by cc_platform_has()

[1] https://lore.kernel.org/all/[email protected]/

---
 arch/arm64/include/asm/hypervisor.h           | 13 +++++++++++++
 arch/arm64/include/asm/mem_encrypt.h          |  3 ++-
 arch/arm64/kernel/rsi.c                       | 12 ------------
 arch/arm64/mm/init.c                          | 15 ++++++++++++++-
 drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c |  3 +++
 5 files changed, 32 insertions(+), 14 deletions(-)
arch/arm64/include/asm/hypervisor.h +13 -0
index a12fd897c877..8889a0ba1ec5 100644
@@ -3,6 +3,9 @@
 #define _ASM_ARM64_HYPERVISOR_H
 
 #include <asm/xen/hypervisor.h>
+#include <linux/jump_label.h>
+
+DECLARE_STATIC_KEY_FALSE(pkvm_guest);
 
 void kvm_init_hyp_services(void);
 bool kvm_arm_hyp_service_available(u32 func_id);
@@ -10,8 +13,18 @@ void kvm_arm_target_impl_cpu_init(void);
 
 #ifdef CONFIG_ARM_PKVM_GUEST
 void pkvm_init_hyp_services(void);
+
+static inline bool is_protected_kvm_guest(void)
+{
+	return static_branch_unlikely(&pkvm_guest);
+}
 #else
 static inline void pkvm_init_hyp_services(void) { };
+
+static inline bool is_protected_kvm_guest(void)
+{
+	return false;
+}
 #endif
 
 static inline void kvm_arch_init_hyp_services(void)

...

drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c line 21

Quoted 10 lines
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +1 -0
index d66291def0f4..26fe9c3f22e3 100644
@@ -17,6 +17,7 @@
 #include <asm/hypervisor.h>
 
 static size_t pkvm_granule;
+DEFINE_STATIC_KEY_FALSE_RO(pkvm_guest);
 

Do we need EXPORT_SYMBOL on this?

Quoted 12 lines
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +2 -0
 static int arm_smccc_do_one_page(u32 func_id, phys_addr_t phys)
 {
@@ -120,4 +121,6 @@ void pkvm_init_hyp_services(void)
 
 	if (kvm_arm_hyp_service_available(ARM_SMCCC_KVM_FUNC_MMIO_GUARD))
 		arm64_ioremap_prot_hook_register(&mmio_guard_ioremap_hook);
+
+	static_branch_enable(&pkvm_guest);
 }
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog

-aneesh

#12 lore raw

Re: [PATCH 3/3] arm64/coco: Add pKVM as a CC platform

Quoted 31 lines · Aneesh Kumar K.V
Quoted 29 lines · Mostafa Saleh

pKVM does support memory encryption, expose that to the rest of the kernel through cc_platform_has()

At the moment, all devices inside the guest are emulated which requires its memory to be shared back to the host (decrypted), so set force_dma_unencrypted() to always return true.

Although, typically pKVM guests rely on restricted-dma-pools to bounce traffic, with this change, it is possible to solely rely on the default SWIOTLB for that (assuming the appropriate size is set from the command line)

---
This change is critical for the ongoing refactoring of the DMA-API[1] that will break protected guests under pKVM with this patch. That is due to this rework will make the state of the SWIOTLB and restricted dma pools depends on the value returned by cc_platform_has()

[1] https://lore.kernel.org/all/[email protected]/

---
 arch/arm64/include/asm/hypervisor.h           | 13 +++++++++++++
 arch/arm64/include/asm/mem_encrypt.h          |  3 ++-
 arch/arm64/kernel/rsi.c                       | 12 ------------
 arch/arm64/mm/init.c                          | 15 ++++++++++++++-
 drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c |  3 +++
 5 files changed, 32 insertions(+), 14 deletions(-)

index d66291def0f4..26fe9c3f22e3 100644

[...]

Quoted 10 lines

drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c line 21

Quoted 8 lines
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +1 -0
@@ -17,6 +17,7 @@
 #include <asm/hypervisor.h>
 
 static size_t pkvm_granule;
+DEFINE_STATIC_KEY_FALSE_RO(pkvm_guest);
 

Do we need EXPORT_SYMBOL on this?

I was not sure about that, all users of this are in tree, I saw RME code have the EXPORT but did not know why?

Thanks,
Mostafa

Quoted 3 lines

-aneesh

#13 lore raw

Re: [PATCH 3/3] arm64/coco: Add pKVM as a CC platform

Quoted 38 lines · Mostafa Saleh
Quoted 31 lines · Aneesh Kumar K.V
Quoted 29 lines · Mostafa Saleh

pKVM does support memory encryption, expose that to the rest of the kernel through cc_platform_has()

At the moment, all devices inside the guest are emulated which requires its memory to be shared back to the host (decrypted), so set force_dma_unencrypted() to always return true.

Although, typically pKVM guests rely on restricted-dma-pools to bounce traffic, with this change, it is possible to solely rely on the default SWIOTLB for that (assuming the appropriate size is set from the command line)

---
This change is critical for the ongoing refactoring of the DMA-API[1] that will break protected guests under pKVM with this patch. That is due to this rework will make the state of the SWIOTLB and restricted dma pools depends on the value returned by cc_platform_has()

[1] https://lore.kernel.org/all/[email protected]/

---
 arch/arm64/include/asm/hypervisor.h           | 13 +++++++++++++
 arch/arm64/include/asm/mem_encrypt.h          |  3 ++-
 arch/arm64/kernel/rsi.c                       | 12 ------------
 arch/arm64/mm/init.c                          | 15 ++++++++++++++-
 drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c |  3 +++
 5 files changed, 32 insertions(+), 14 deletions(-)

index d66291def0f4..26fe9c3f22e3 100644

[...]

Quoted 5 lines
Quoted 5 lines
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +0 -0
@@ -17,6 +17,7 @@
 #include <asm/hypervisor.h>
 
Quoted 9 lines
Quoted 5 lines

drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c line 21

Quoted 3 lines
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +1 -0
 static size_t pkvm_granule;
+DEFINE_STATIC_KEY_FALSE_RO(pkvm_guest);
 

Do we need EXPORT_SYMBOL on this?

I was not sure about that, all users of this are in tree, I saw RME code have the EXPORT but did not know why?

arm-cca-guest is one example. I was assuming is_protected_kvm_guest() would be a helper that could get pulled into various code paths via force_dma_unencrypted().

If we have not found any build failures for now, we can probably avoid that change for now.

-aneesh

maintainer
#14 lore raw

Re: [PATCH 3/3] arm64/coco: Add pKVM as a CC platform

Quoted 46 lines · Aneesh Kumar K.V
Quoted 44 lines · Mostafa Saleh
Quoted 31 lines · Aneesh Kumar K.V
Quoted 29 lines · Mostafa Saleh

pKVM does support memory encryption, expose that to the rest of the kernel through cc_platform_has()

At the moment, all devices inside the guest are emulated which requires its memory to be shared back to the host (decrypted), so set force_dma_unencrypted() to always return true.

Although, typically pKVM guests rely on restricted-dma-pools to bounce traffic, with this change, it is possible to solely rely on the default SWIOTLB for that (assuming the appropriate size is set from the command line)

---
This change is critical for the ongoing refactoring of the DMA-API[1] that will break protected guests under pKVM with this patch. That is due to this rework will make the state of the SWIOTLB and restricted dma pools depends on the value returned by cc_platform_has()

[1] https://lore.kernel.org/all/[email protected]/

---
 arch/arm64/include/asm/hypervisor.h           | 13 +++++++++++++
 arch/arm64/include/asm/mem_encrypt.h          |  3 ++-
 arch/arm64/kernel/rsi.c                       | 12 ------------
 arch/arm64/mm/init.c                          | 15 ++++++++++++++-
 drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c |  3 +++
 5 files changed, 32 insertions(+), 14 deletions(-)

index d66291def0f4..26fe9c3f22e3 100644

[...]

Quoted 10 lines

drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c line 21

Quoted 8 lines
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +1 -0
@@ -17,6 +17,7 @@
 #include <asm/hypervisor.h>
 
 static size_t pkvm_granule;
+DEFINE_STATIC_KEY_FALSE_RO(pkvm_guest);
 

Do we need EXPORT_SYMBOL on this?

Quoted 7 lines
Quoted 3 lines

I was not sure about that, all users of this are in tree, I saw RME code have the EXPORT but did not know why?

arm-cca-guest is one example. I was assuming is_protected_kvm_guest() would be a helper that could get pulled into various code paths via force_dma_unencrypted().

For now, the only callers of force_dma_unencrypted() I can see are in kernel/dma/*, so we can skip the exporting.

Signature · 1 line
Catalin
maintainer Reviewed-by on v1 3/3
#15 lore raw

Re: [PATCH 3/3] arm64/coco: Add pKVM as a CC platform

In reply to Mostafa Saleh
Quoted 7 lines · Mostafa Saleh

pKVM does support memory encryption, expose that to the rest of the kernel through cc_platform_has()

At the moment, all devices inside the guest are emulated which requires its memory to be shared back to the host (decrypted), so set force_dma_unencrypted() to always return true.

Quoted 6 lines

Although, typically pKVM guests rely on restricted-dma-pools to bounce traffic, with this change, it is possible to solely rely on the default SWIOTLB for that (assuming the appropriate size is set from the command line)

Good to see reamls and pkvm guests aligned, easier to reason about the code paths. Thanks!

Tested-by on the v1 series
#16 lore raw
In reply to Mostafa Saleh
Quoted 17 lines · Mostafa Saleh

The goal of series is to convert pKVM as a CC platform similar to ARM CCA, that makes CC guests consistently discovered which avoid breaking it by the ongoing DMA rework [1]

The first patch is a minor clean up I noticed while reading the code.

And the second patch is a bug fix found by Sashiko.

The third patch is the main one.

[1]https://lore.kernel.org/all/[email protected]/

Mostafa Saleh (3):
arm64/mm: Simplify SWIOTLB setup in arch_mm_preinit() drivers/virt: pkvm: Fix end calculation in mmio_guard_ioremap_hook() arm64/coco: Add pKVM as a CC platform

Quoted 9 lines
 arch/arm64/include/asm/hypervisor.h           | 13 ++++++++++
 arch/arm64/include/asm/mem_encrypt.h          |  3 ++-
 arch/arm64/kernel/rsi.c                       | 12 ---------
 arch/arm64/mm/init.c                          | 26 +++++++++++++------
 drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c |  5 +++-
 5 files changed, 37 insertions(+), 22 deletions(-)
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog

A rebased dma-rework tree on top of the pKVM changes can be found at:
https://gitlab.arm.com/linux-arm/linux-cca/-/tree/scratch/pkvm/testing?ref_type=heads

I was able to successfully boot a protected VM using the latest version of kvmtool.

-aneesh

maintainer
#17 lore raw
In reply to Mostafa Saleh
Quoted 9 lines · Mostafa Saleh

The goal of series is to convert pKVM as a CC platform similar to ARM CCA, that makes CC guests consistently discovered which avoid breaking it by the ongoing DMA rework [1]

The first patch is a minor clean up I noticed while reading the code.

And the second patch is a bug fix found by Sashiko.

[...]

Applied second patch to arm64 (for-next/fixes), thanks!

[2/3] drivers/virt: pkvm: Fix end calculation in mmio_guard_ioremap_hook()

      https://git.kernel.org/arm64/c/62c740f823a8

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev
maintainer Applied
#18 lore raw
In reply to Mostafa Saleh
Quoted 9 lines · Mostafa Saleh

The goal of series is to convert pKVM as a CC platform similar to ARM CCA, that makes CC guests consistently discovered which avoid breaking it by the ongoing DMA rework [1]

The first patch is a minor clean up I noticed while reading the code.

And the second patch is a bug fix found by Sashiko.

[...]

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

[1/3] arm64/mm: Simplify SWIOTLB setup in arch_mm_preinit()

      https://git.kernel.org/arm64/c/442d366cc1aa
[3/3] arm64/coco: Add pKVM as a CC platform
      https://git.kernel.org/arm64/c/e62decaf98e7

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev