Skip to contentSkip to conversation

drivers/virt: pkvm: Zero and clean memory before sharing it with the host

PATCH In review Fuad Tabba 3 participants 8 messages Nov 16

Subsystems (3)
lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

PATCH 1/1 drivers/virt: pkvm: Zero and clean memory before sharing it with the host

From: "Aneesh Kumar K.V (Arm)" <[email protected]>

On pKVM, set_memory_decrypted() shares pages with the host in place and without cache maintenance. The atomic pool, dma-direct and dynamic SWIOTLB pools zero their memory only after sharing it, so unless the page allocator has zeroed it, the host can read whatever the guest last left in it. Zeroing first isn't enough on its own, as the zeroes may not have been written back to memory, and a host that maps the pages Non-cacheable then reads the old contents.

Zero the range and clean it to the PoC before sharing it, so that no caller can hand the host stale data.

Fixes ebc59b120c588 ("drivers/virt: pkvm: Hook up mem_encrypt API using pKVM hypercalls")
Fixes e62decaf98e7c ("arm64/coco: Add pKVM as a CC platform")
[fuad: take the pKVM hunk of v10 patch 2 as a standalone fix, add the clean to the PoC]

---

Notes:
This is the pKVM hunk of patch 2 of Aneesh's v10 [1] plus a clean to the PoC, taken on its own because it fixes a bug that has exposed guest memory to the host since v6.12.

Based on Linux 7.4-rc3 (aebb727447640).

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

 drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +11 -0
index 26fe9c3f22e36..81d1cd24a2eff 100644
@@ -9,10 +9,12 @@
 
 #include <linux/arm-smccc.h>
 #include <linux/array_size.h>
+#include <linux/cacheflush.h>
 #include <linux/io.h>
 #include <linux/mem_encrypt.h>
 #include <linux/mm.h>
 #include <linux/pgtable.h>
+#include <linux/string.h>
 
 #include <asm/hypervisor.h>
 
@@ -61,6 +63,15 @@ static int pkvm_set_memory_encrypted(unsigned long addr, int numpages)
 
 static int pkvm_set_memory_decrypted(unsigned long addr, int numpages)
 {
+	size_t size = numpages * PAGE_SIZE;
+
+	/*
+	 * The host can read the pages as soon as they are shared, possibly
+	 * through a Non-cacheable mapping. No caller relies on their contents.
+	 */
+	memset((void *)addr, 0, size);
+	dcache_clean_poc(addr, addr + size);
+
 	return __set_memory_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID,
 				  addr, numpages);
 }

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.47.3
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Fuad Tabba

From: "Aneesh Kumar K.V (Arm)" <[email protected]>

On pKVM, set_memory_decrypted() shares pages with the host in place and without cache maintenance. The atomic pool, dma-direct and dynamic SWIOTLB pools zero their memory only after sharing it, so unless the page allocator has zeroed it, the host can read whatever the guest last left in it. Zeroing first isn't enough on its own, as the zeroes may not have been written back to memory, and a host that maps the pages Non-cacheable then reads the old contents.

Zero the range and clean it to the PoC before sharing it, so that no caller can hand the host stale data.

Quoted 8 lines
Fixes ebc59b120c588 ("drivers/virt: pkvm: Hook up mem_encrypt API using pKVM hypercalls")
Fixes e62decaf98e7c ("arm64/coco: Add pKVM as a CC platform")
[fuad: take the pKVM hunk of v10 patch 2 as a standalone fix, add the clean to the PoC]
Quoted 49 lines

---

Notes:
This is the pKVM hunk of patch 2 of Aneesh's v10 [1] plus a clean to the PoC, taken on its own because it fixes a bug that has exposed guest memory to the host since v6.12.

Based on Linux 7.4-rc3 (aebb727447640).

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

  drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +11 -0
index 26fe9c3f22e36..81d1cd24a2eff 100644
@@ -9,10 +9,12 @@
 
  #include <linux/arm-smccc.h>
  #include <linux/array_size.h>
+#include <linux/cacheflush.h>
  #include <linux/io.h>
  #include <linux/mem_encrypt.h>
  #include <linux/mm.h>
  #include <linux/pgtable.h>
+#include <linux/string.h>
 
  #include <asm/hypervisor.h>
 
@@ -61,6 +63,15 @@ static int pkvm_set_memory_encrypted(unsigned long addr, int numpages)
 
  static int pkvm_set_memory_decrypted(unsigned long addr, int numpages)
  {
+	size_t size = numpages * PAGE_SIZE;
+
+	/*
+	 * The host can read the pages as soon as they are shared, possibly
+	 * through a Non-cacheable mapping. No caller relies on their contents.
+	 */
+	memset((void *)addr, 0, size);
+	dcache_clean_poc(addr, addr + size);
+
  	return __set_memory_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID,
  				  addr, numpages);
  }

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

#2 lore raw
In reply to Fuad Tabba
Quoted 53 lines · Fuad Tabba

From: "Aneesh Kumar K.V (Arm)" <[email protected]>

On pKVM, set_memory_decrypted() shares pages with the host in place and without cache maintenance. The atomic pool, dma-direct and dynamic SWIOTLB pools zero their memory only after sharing it, so unless the page allocator has zeroed it, the host can read whatever the guest last left in it. Zeroing first isn't enough on its own, as the zeroes may not have been written back to memory, and a host that maps the pages Non-cacheable then reads the old contents.

Zero the range and clean it to the PoC before sharing it, so that no caller can hand the host stale data.

Fixes ebc59b120c588 ("drivers/virt: pkvm: Hook up mem_encrypt API using pKVM hypercalls")
Fixes e62decaf98e7c ("arm64/coco: Add pKVM as a CC platform")
[fuad: take the pKVM hunk of v10 patch 2 as a standalone fix, add the clean to the PoC]

---

Notes:
This is the pKVM hunk of patch 2 of Aneesh's v10 [1] plus a clean to the PoC, taken on its own because it fixes a bug that has exposed guest memory to the host since v6.12.

Based on Linux 7.4-rc3 (aebb727447640).

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

 drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +2 -0
index 26fe9c3f22e36..81d1cd24a2eff 100644
@@ -9,10 +9,12 @@
 
 #include <linux/arm-smccc.h>
 #include <linux/array_size.h>
+#include <linux/cacheflush.h>
 #include <linux/io.h>
 #include <linux/mem_encrypt.h>
 #include <linux/mm.h>
 #include <linux/pgtable.h>
+#include <linux/string.h>
 
 #include <asm/hypervisor.h>
 

drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c static int pkvm_set_memory_encrypted(unsigned long addr, int numpages) · line 72

Quoted 10 lines
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +7 -0
@@ -61,6 +63,15 @@ static int pkvm_set_memory_encrypted(unsigned long addr, int numpages)
 
 static int pkvm_set_memory_decrypted(unsigned long addr, int numpages)
 {
+	size_t size = numpages * PAGE_SIZE;
+
+	/*
+	 * The host can read the pages as soon as they are shared, possibly
+	 * through a Non-cacheable mapping. No caller relies on their contents.
+	 */
+	memset((void *)addr, 0, size);

Are we sure we want that? The guest may want to setup memory and then share it. I am not sure about modifying this API.

drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c static int pkvm_set_memory_encrypted(unsigned long addr, int numpages) · line 73

Quoted 1 line
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +1 -0
+	dcache_clean_poc(addr, addr + size);

I believe the hypervisor is already doing the cache maintainance when the page is mapped back into the host. (see stage2_map_walker_try_leaf())

Signature · 1 line
Vincent
Quoted 9 lines
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +1 -0
+
 	return __set_memory_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID,
 				  addr, numpages);
 }

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.47.3
author reviewer
#3 lore raw

Hi Vincent,

On Mon, 16 Nov 2026 at 11:28, Vincent Donnefort <[email protected]> wrote:
[...]

Quoted 8 lines

drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c static int pkvm_set_memory_encrypted(unsigned long addr, int numpages) · line 72

Quoted 5 lines
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +5 -0
+     /*
+      * The host can read the pages as soon as they are shared, possibly
+      * through a Non-cacheable mapping. No caller relies on their contents.
+      */
+     memset((void *)addr, 0, size);

Are we sure we want that? The guest may want to setup memory and then share it. I am not sure about modifying this API.

Right! I wanted to fix the bug for the ITS property table, which is shared before it's initialized, and totally forgot that this is the path all sharing goes through... I'll drop this, and put that fix back in the ITS series.

Quoted 4 lines
Quoted 1 line

+ dcache_clean_poc(addr, addr + size);

I believe the hypervisor is already doing the cache maintainance when the page is mapped back into the host. (see stage2_map_walker_try_leaf())

I don't think it does on this path. host_mmu.mm_ops doesn't have a dcache_clean_inval_poc() callback, so stage2_map_walker_try_leaf() doesn't do the CMO for a host mapping. On the guest's side, __pkvm_guest_share_host() only changes the software bits of the existing mapping, and the walker returns before the CMO in that case.

Cheers,
/fuad

#4 lore raw
Quoted 23 lines · Fuad Tabba

Hi Vincent,

On Mon, 16 Nov 2026 at 11:28, Vincent Donnefort <[email protected]> wrote:
[...]

Quoted 8 lines

drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c static int pkvm_set_memory_encrypted(unsigned long addr, int numpages) · line 72

Quoted 5 lines
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +5 -0
+     /*
+      * The host can read the pages as soon as they are shared, possibly
+      * through a Non-cacheable mapping. No caller relies on their contents.
+      */
+     memset((void *)addr, 0, size);

Are we sure we want that? The guest may want to setup memory and then share it. I am not sure about modifying this API.

Right! I wanted to fix the bug for the ITS property table, which is shared before it's initialized, and totally forgot that this is the path all sharing goes through... I'll drop this, and put that fix back in the ITS series.

Quoted 4 lines
Quoted 1 line

+ dcache_clean_poc(addr, addr + size);

I believe the hypervisor is already doing the cache maintainance when the page is mapped back into the host. (see stage2_map_walker_try_leaf())

Quoted 8 lines

I don't think it does on this path. host_mmu.mm_ops doesn't have a dcache_clean_inval_poc() callback, so stage2_map_walker_try_leaf() doesn't do the CMO for a host mapping. On the guest's side, __pkvm_guest_share_host() only changes the software bits of the existing mapping, and the walker returns before the CMO in that case.

Cheers,
/fuad

Ha yes of course, that's the host s2 we are mapping into. And on the guest side, we are only changing the software bits, so no CMO. Actually I should have read the comment in stage2_map_walker_try_leaf() more carefully.

That said, I wonder if that shouldn't be handled by the hypervisor, just like hyp_poison_page() do the cache clean.

That would be slightly slower than letting the guest (who has a mapping already) do it, but pKVM is enforcing isolation between host and guest, and it feels like it should do it by default no matter the memory transition.

Signature · 1 line
Vincent
author reviewer
#5 lore raw

On Mon, 16 Nov 2026 at 14:04, Vincent Donnefort <[email protected]> wrote:
[...]

Quoted 10 lines

Ha yes of course, that's the host s2 we are mapping into. And on the guest side, we are only changing the software bits, so no CMO. Actually I should have read the comment in stage2_map_walker_try_leaf() more carefully.

That said, I wonder if that shouldn't be handled by the hypervisor, just like hyp_poison_page() do the cache clean.

That would be slightly slower than letting the guest (who has a mapping already) do it, but pKVM is enforcing isolation between host and guest, and it feels like it should do it by default no matter the memory transition.

After the discussion on the related thread [1], if we have the zeroing here, we need the clean here as well. So I think that this patch is the right fix now.

Cheers,
/fuad

[1] https://lore.kernel.org/all/CA+EHjTywWTaKLtM6OPF_0ETv29-m8JsHdvF3BeW9Xa=h713eEg@mail.gmail.com/

Quoted 3 lines

--
Vincent

#6 lore raw
Quoted 17 lines · Fuad Tabba

On Mon, 16 Nov 2026 at 14:04, Vincent Donnefort <[email protected]> wrote:
[...]

Quoted 10 lines

Ha yes of course, that's the host s2 we are mapping into. And on the guest side, we are only changing the software bits, so no CMO. Actually I should have read the comment in stage2_map_walker_try_leaf() more carefully.

That said, I wonder if that shouldn't be handled by the hypervisor, just like hyp_poison_page() do the cache clean.

That would be slightly slower than letting the guest (who has a mapping already) do it, but pKVM is enforcing isolation between host and guest, and it feels like it should do it by default no matter the memory transition.

After the discussion on the related thread [1], if we have the zeroing here, we need the clean here as well. So I think that this patch is the right fix now.

Quoted 10 lines

Cheers,
/fuad

[1] https://lore.kernel.org/all/CA+EHjTywWTaKLtM6OPF_0ETv29-m8JsHdvF3BeW9Xa=h713eEg@mail.gmail.com/

Quoted 3 lines

--
Vincent

While I don't disagree with having the memset in CMOs in the guest, I still believe the hypervisor should do it as well.

Linux is not the only virtualised guest and they all should be able to trust the hypervisor to do the right thing for them, which it doesn't.

Signature · 1 line
Vincent
author reviewer
#7 lore raw

On Mon, 16 Nov 2026 at 16:42, Vincent Donnefort <[email protected]> wrote:
[...]

Quoted 5 lines

While I don't disagree with having the memset in CMOs in the guest, I still believe the hypervisor should do it as well.

Linux is not the only virtualised guest and they all should be able to trust the hypervisor to do the right thing for them, which it doesn't.

I agree, and I think that that's the consensus [1].

This fix is for the guest part.

Cheers,
/fuad

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