mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Allocating SEV C-bit-cleared pages (without relying on swiotlb)
@ 2025-03-27 14:12 Teddy Astie
  2025-03-28 16:30 ` Michael Kelley
  0 siblings, 1 reply; 3+ messages in thread
From: Teddy Astie @ 2025-03-27 14:12 UTC (permalink / raw)
  To: linux-kernel, linux-mm; +Cc: Xen-devel

Hello Linux mailing list !

For porting Linux code to make it work on Xen with AMD-SEV, I need to 
change the allocation of some pages to use "shared pages" (C-bit 
cleared) instead of private pages (C-bit set, which is the default kind) 
as these pages needs to be shared with the hypervisor/Dom0.

Is there a facility to allocate pages with C-bit cleared (and if not 
running under SEV, just allocate a plain page) ? Current Linux code for 
SEV seems to only rely on swiotlb as access to shared page is mostly 
made through DMA-kind devices (e.g virtio or emulated device), but I 
don't think it is the best approach.

Regards
Teddy


Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

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

* RE: Allocating SEV C-bit-cleared pages (without relying on swiotlb)
  2025-03-27 14:12 Allocating SEV C-bit-cleared pages (without relying on swiotlb) Teddy Astie
@ 2025-03-28 16:30 ` Michael Kelley
  2025-03-28 17:46   ` Teddy Astie
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Kelley @ 2025-03-28 16:30 UTC (permalink / raw)
  To: Teddy Astie, linux-kernel, linux-mm; +Cc: Xen-devel

From: Teddy Astie <teddy.astie@vates.tech> Sent: Thursday, March 27, 2025 7:12 AM
> To: linux-kernel@vger.kernel.org; linux-mm@vger.kernel.org
> Cc: Xen-devel <xen-devel@lists.xenproject.org>
> Subject: Allocating SEV C-bit-cleared pages (without relying on swiotlb)
> 
> Hello Linux mailing list !
> 
> For porting Linux code to make it work on Xen with AMD-SEV, I need to
> change the allocation of some pages to use "shared pages" (C-bit
> cleared) instead of private pages (C-bit set, which is the default kind)
> as these pages needs to be shared with the hypervisor/Dom0.
> 
> Is there a facility to allocate pages with C-bit cleared (and if not
> running under SEV, just allocate a plain page) ? Current Linux code for
> SEV seems to only rely on swiotlb as access to shared page is mostly
> made through DMA-kind devices (e.g virtio or emulated device), but I
> don't think it is the best approach.
> 

For allocating memory that can be shared with the hypervisor,
allocate memory as usual (with alloc_pages(), for example), then
call set_memory_decrypted() on that memory. This approach
works in general for Confidential Computing (CoCo) VMs,
regardless of whether the underlying hardware is AMD SEV-SNP,
Intel TDX, or ARM64 CCA.  If you are running in a non-CoCo
VM, set_memory_decrypted() is a no-op, so you can call it
without having to check whether you are in a CoCo VM.

When freeing the memory, do the reverse. Call
set_memory_encrypted() first, then free the memory as
usual. Note that if set_memory_encrypted() fails for any
reason, just leak the memory instead of freeing it because
the encrypted state is unknown after such a failure.

If you search for set_memory_decrypted() in kernel code,
you'll find several examples.  See drivers/hv/hv_connection.c
as one place where code for running on Hyper-V follows
this paradigm. There are several other examples as well.

Michael

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

* Re: Allocating SEV C-bit-cleared pages (without relying on swiotlb)
  2025-03-28 16:30 ` Michael Kelley
@ 2025-03-28 17:46   ` Teddy Astie
  0 siblings, 0 replies; 3+ messages in thread
From: Teddy Astie @ 2025-03-28 17:46 UTC (permalink / raw)
  To: Michael Kelley, linux-kernel, linux-mm; +Cc: Xen-devel

Le 28/03/2025 à 17:31, Michael Kelley a écrit :
> From: Teddy Astie <teddy.astie@vates.tech> Sent: Thursday, March 27, 2025 7:12 AM
>> To: linux-kernel@vger.kernel.org; linux-mm@vger.kernel.org
>> Cc: Xen-devel <xen-devel@lists.xenproject.org>
>> Subject: Allocating SEV C-bit-cleared pages (without relying on swiotlb)
>>
>> Hello Linux mailing list !
>>
>> For porting Linux code to make it work on Xen with AMD-SEV, I need to
>> change the allocation of some pages to use "shared pages" (C-bit
>> cleared) instead of private pages (C-bit set, which is the default kind)
>> as these pages needs to be shared with the hypervisor/Dom0.
>>
>> Is there a facility to allocate pages with C-bit cleared (and if not
>> running under SEV, just allocate a plain page) ? Current Linux code for
>> SEV seems to only rely on swiotlb as access to shared page is mostly
>> made through DMA-kind devices (e.g virtio or emulated device), but I
>> don't think it is the best approach.
>>
>
> For allocating memory that can be shared with the hypervisor,
> allocate memory as usual (with alloc_pages(), for example), then
> call set_memory_decrypted() on that memory. This approach
> works in general for Confidential Computing (CoCo) VMs,
> regardless of whether the underlying hardware is AMD SEV-SNP,
> Intel TDX, or ARM64 CCA.  If you are running in a non-CoCo
> VM, set_memory_decrypted() is a no-op, so you can call it
> without having to check whether you are in a CoCo VM.
>
> When freeing the memory, do the reverse. Call
> set_memory_encrypted() first, then free the memory as
> usual. Note that if set_memory_encrypted() fails for any
> reason, just leak the memory instead of freeing it because
> the encrypted state is unknown after such a failure.
>
> If you search for set_memory_decrypted() in kernel code,
> you'll find several examples.  See drivers/hv/hv_connection.c
> as one place where code for running on Hyper-V follows
> this paradigm. There are several other examples as well.
>
> Michael

set_memory_decrypted does the job.

Thanks
Teddy


Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech


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

end of thread, other threads:[~2025-03-28 17:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-27 14:12 Allocating SEV C-bit-cleared pages (without relying on swiotlb) Teddy Astie
2025-03-28 16:30 ` Michael Kelley
2025-03-28 17:46   ` Teddy Astie

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®