mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drivers/virt: pkvm: Zero and clean memory before sharing it with the host
@ 2026-11-16 11:05 Fuad Tabba
  2026-11-16 11:13 ` Suzuki K Poulose
  2026-11-16 11:28 ` Vincent Donnefort
  0 siblings, 2 replies; 3+ messages in thread
From: Fuad Tabba @ 2026-11-16 11:05 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Aneesh Kumar K.V (Arm)
  Cc: Mark Rutland, Marc Zyngier, Mostafa Saleh, Vincent Donnefort,
	Suzuki K Poulose, linux-arm-kernel, kvmarm, linux-coco,
	linux-kernel, Fuad Tabba, stable

From: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>

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")
Link: https://lore.kernel.org/r/20261116091137.94252-3-aneesh.kumar@kernel.org
Cc: stable@vger.kernel.org
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
[fuad: take the pKVM hunk of v10 patch 2 as a standalone fix, add the clean to the PoC]
Co-developed-by: Fuad Tabba <fuad.tabba@linux.dev>
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---

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/20261116091137.94252-3-aneesh.kumar@kernel.org

 drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
index 26fe9c3f22e36..81d1cd24a2eff 100644
--- a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
+++ b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
@@ -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.47.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drivers/virt: pkvm: Zero and clean memory before sharing it with the host
  2026-11-16 11:05 [PATCH] drivers/virt: pkvm: Zero and clean memory before sharing it with the host Fuad Tabba
@ 2026-11-16 11:13 ` Suzuki K Poulose
  2026-11-16 11:28 ` Vincent Donnefort
  1 sibling, 0 replies; 3+ messages in thread
From: Suzuki K Poulose @ 2026-11-16 11:13 UTC (permalink / raw)
  To: Fuad Tabba, Catalin Marinas, Will Deacon, Aneesh Kumar K.V (Arm)
  Cc: Mark Rutland, Marc Zyngier, Mostafa Saleh, Vincent Donnefort,
	linux-arm-kernel, kvmarm, linux-coco, linux-kernel, Fuad Tabba,
	stable

On 16/11/2026 11:05, Fuad Tabba wrote:
> From: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>
> 
> 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")
> Link: https://lore.kernel.org/r/20261116091137.94252-3-aneesh.kumar@kernel.org
> Cc: stable@vger.kernel.org
> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
> [fuad: take the pKVM hunk of v10 patch 2 as a standalone fix, add the clean to the PoC]
> Co-developed-by: Fuad Tabba <fuad.tabba@linux.dev>
> Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>

Reviewed-by: Suzuki K Poulose <suzuki.poulose@arm.com>

> ---
> 
> 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/20261116091137.94252-3-aneesh.kumar@kernel.org
> 
>   drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 11 +++++++++++
>   1 file changed, 11 insertions(+)
> 
> diff --git a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
> index 26fe9c3f22e36..81d1cd24a2eff 100644
> --- a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
> +++ b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
> @@ -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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drivers/virt: pkvm: Zero and clean memory before sharing it with the host
  2026-11-16 11:05 [PATCH] drivers/virt: pkvm: Zero and clean memory before sharing it with the host Fuad Tabba
  2026-11-16 11:13 ` Suzuki K Poulose
@ 2026-11-16 11:28 ` Vincent Donnefort
  1 sibling, 0 replies; 3+ messages in thread
From: Vincent Donnefort @ 2026-11-16 11:28 UTC (permalink / raw)
  To: Fuad Tabba
  Cc: Catalin Marinas, Will Deacon, Aneesh Kumar K.V (Arm),
	Mark Rutland, Marc Zyngier, Mostafa Saleh, Suzuki K Poulose,
	linux-arm-kernel, kvmarm, linux-coco, linux-kernel, Fuad Tabba,
	stable

On Mon, Nov 16, 2026 at 11:05:49AM +0000, Fuad Tabba wrote:
> From: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>
> 
> 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")
> Link: https://lore.kernel.org/r/20261116091137.94252-3-aneesh.kumar@kernel.org
> Cc: stable@vger.kernel.org
> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
> [fuad: take the pKVM hunk of v10 patch 2 as a standalone fix, add the clean to the PoC]
> Co-developed-by: Fuad Tabba <fuad.tabba@linux.dev>
> Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
> ---
> 
> 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/20261116091137.94252-3-aneesh.kumar@kernel.org
> 
>  drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
> index 26fe9c3f22e36..81d1cd24a2eff 100644
> --- a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
> +++ b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
> @@ -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);

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. 

> +	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())

-- 
Vincent

> +
>  	return __set_memory_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID,
>  				  addr, numpages);
>  }
> 
> base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
> -- 
> 2.47.3
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-16 11:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 11:05 [PATCH] drivers/virt: pkvm: Zero and clean memory before sharing it with the host Fuad Tabba
2026-11-16 11:13 ` Suzuki K Poulose
2026-11-16 11:28 ` Vincent Donnefort

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®