mm/vmalloc: add vmalloc_decrypted() and vzalloc_decrypted()
Subsystems (2)
RFC PATCH 1/1 mm/vmalloc: add vmalloc_decrypted() and vzalloc_decrypted()
In confidential computing environments (arm64 CCA, x86 SEV/TDX), guest memory is encrypted by default and must be explicitly transitioned to a decrypted/shared state for host-visible access. Calling set_memory_decrypted() on a vmalloc address is not supported, and not recommended as it would be inefficient to decrypt the pages after they have been mapped.
Add vmalloc_decrypted() and vzalloc_decrypted() which decrypt pages on the linear map before creating the vmalloc mapping via vmap(), so physical pages are never mapped with conflicting encryption attributes across aliases. A new VM_DECRYPTED flag marks these allocations so that vfree() automatically re-encrypts pages before returning them to the page allocator.
--- include/linux/vmalloc.h | 7 ++ mm/vmalloc.c | 163 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+)
include/linux/vmalloc.h +7 -0
@@ -38,6 +38,7 @@ struct iov_iter; /* in uio.h */#define VM_DEFER_KMEMLEAK 0#endif #define VM_SPARSE 0x00001000/* sparse vm_area. not all pages are present. */ +#define VM_DECRYPTED 0x00002000/* pages decrypted for host-shared access, re-encrypt on vfree */ /* bits [20..32] reserved for arch specific ioremap internals */
@@ -153,6 +154,12 @@ extern void *vmalloc_noprof(unsigned long size) __alloc_size(1);extern void *vzalloc_noprof (unsigned long size)__alloc_size(1) ;#define vzalloc (...) alloc_hooks(vzalloc_noprof(__VA_ARGS__)) +extern void *vmalloc_decrypted_noprof (unsigned long size)__alloc_size(1) ; +#define vmalloc_decrypted (...) alloc_hooks(vmalloc_decrypted_noprof(__VA_ARGS__)) + +extern void *vzalloc_decrypted_noprof (unsigned long size)__alloc_size(1) ; +#define vzalloc_decrypted (...) alloc_hooks(vzalloc_decrypted_noprof(__VA_ARGS__)) +extern void *vmalloc_user_noprof (unsigned long size)__alloc_size(1) ;#define vmalloc_user (...) alloc_hooks(vmalloc_user_noprof(__VA_ARGS__))
mm/vmalloc.c +163 -0
193 lines not on this page · raw message
base-commit: e9add7501ad3297dad9b90ce201266830a68ab47
Signature · 1 line
2.45.4
Quoted 2 lines · Kameron Carr
+/*
+ * Transition a single contiguous block of @nr pages at index @idx in
There's no parameter called @nr_pages; you probably meant @nr.
mm/vmalloc.c void vfree_atomic(const void *addr) · line 3426
Quoted 6 lines
+ * @area->pages to encrypted or decrypted state. On failure, the block's
+ * page-pointer slots are cleared so the standard free path will not return
+ * the pages to the allocator (they are leaked).
+ */mm/vmalloc.c +2
-0+static int __vm_pages_enc_dec (struct vm_struct *area,unsigned int idx, +unsigned int nr,bool encrypt)
This 'bool encrypt' parameter is an antipattern. Just split this into two functions.
mm/vmalloc.c void vfree_atomic(const void *addr) · line 3436
Quoted 10 lines
mm/vmalloc.c +10
-0+{ +unsigned long addr = + (unsigned long )kasan_reset_tag (page_address (area->pages[idx])); +int err = encrypt ?set_memory_encrypted (addr, nr) : +set_memory_decrypted (addr, nr); + +if (err) +memset (&area->pages[idx],0 , nr *sizeof (*area->pages)); +return err; +}
Does it really make sense to pass in 'area' and 'idx' rather than passing in &area->pages[idx]?
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Kameron,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on e9add7501ad3297dad9b90ce201266830a68ab47]
url: https://github.com/intel-lab-lkp/linux/commits/Kameron-Carr/mm-vmalloc-add-vmalloc_decrypted-and-vzalloc_decrypted/20260522-050151 base: e9add7501ad3297dad9b90ce201266830a68ab47 patch link: https://lore.kernel.org/r/20260521205834.1012925-1-kameroncarr%40linux.microsoft.com patch subject: [RFC PATCH] mm/vmalloc: add vmalloc_decrypted() and vzalloc_decrypted() config: s390-allyesconfig (https://download.01.org/0day-ci/archive/20260524/[email protected]/config) compiler: s390-linux-gcc (GCC) 15.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260524/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
mm/vmalloc.c: In function '__vm_pages_enc_dec':
>> mm/vmalloc.c:3430:29: error: implicit declaration of function 'set_memory_encrypted' [-Wimplicit-function-declaration]
3430 | int err = encrypt ? set_memory_encrypted(addr, nr) :
| ^~~~~~~~~~~~~~~~~~~~
>> mm/vmalloc.c:3431:29: error: implicit declaration of function 'set_memory_decrypted' [-Wimplicit-function-declaration]
3431 | set_memory_decrypted(addr, nr);
| ^~~~~~~~~~~~~~~~~~~~vim +/set_memory_encrypted +3430 mm/vmalloc.c
3418
3419 /*
3420 * Transition a single contiguous block of @nr pages at index @idx in
3421 * @area->pages to encrypted or decrypted state. On failure, the block's
3422 * page-pointer slots are cleared so the standard free path will not return
3423 * the pages to the allocator (they are leaked).
3424 */
3425 static int __vm_pages_enc_dec(struct vm_struct *area, unsigned int idx,
3426 unsigned int nr, bool encrypt)
3427 {
3428 unsigned long addr =
3429 (unsigned long)kasan_reset_tag(page_address(area->pages[idx]));
> 3430 int err = encrypt ? set_memory_encrypted(addr, nr) : > 3431 set_memory_decrypted(addr, nr);
3432
3433 if (err)
3434 memset(&area->pages[idx], 0, nr * sizeof(*area->pages));
3435 return err;
3436 }
3437
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
+ linux-coco, Suzuki (for the arm64 behaviour)
Quoted 14 lines · Kameron Carr
In confidential computing environments (arm64 CCA, x86 SEV/TDX), guest memory is encrypted by default and must be explicitly transitioned to a decrypted/shared state for host-visible access. Calling
set_memory_decrypted()on a vmalloc address is not supported, and not recommended as it would be inefficient to decrypt the pages after they have been mapped.Add
vmalloc_decrypted()andvzalloc_decrypted()which decrypt pages on the linear map before creating the vmalloc mapping viavmap(), so physical pages are never mapped with conflicting encryption attributes across aliases. A newVM_DECRYPTEDflag marks these allocations so thatvfree()automatically re-encrypts pages before returning them to the page allocator.
Quoted 7 lines
Suggested-by Catalin Marinas <[email protected]>Signed-off-by Kameron Carr <[email protected]>--- include/linux/vmalloc.h | 7 ++ mm/vmalloc.c | 163 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+)
There are a few Sashiko comments worth reviewing:
https://sashiko.dev/#/patchset/[email protected]
[...]
Quoted 64 lines
mm/vmalloc.c +60
-0+/* +* Re-encrypt the linear-map alias of all pages backing a VM_DECRYPTED area. +* Best-effort: on per-block failure the loop continues so as many pages as +* possible are returned to the encrypted state. Pages that fail to +* transition are left out of area->pages and leaked. +*/ +static int vm_pages_encrypt (struct vm_struct *area) +{ +unsigned int nr =1U <<vm_area_page_order (area); +unsigned int i; +unsigned int leaked; +int ret =0 ; + +for (i =0 ; i < area->nr_pages; i += nr) { +int err =__vm_pages_enc_dec (area, i, nr, true); + +if (err && !ret) + ret = err; + } + + leaked =vm_compact_leaked_pages (area); +if (leaked) +pr_warn ("vmalloc: re-encryption failed, leaked %u pages\n" , + leaked); +return ret; +} + +/* +* Decrypt the linear-map alias of all pages backing a VM_DECRYPTED area. +* On failure, the already-decrypted prefix is rolled back to encrypted. +* Pages that fail either the initial decrypt or the rollback re-encrypt are +* left out of area->pages and leaked. +*/ +static int vm_pages_decrypt (struct vm_struct *area) +{ +unsigned int nr =1U <<vm_area_page_order (area); +unsigned int i; +unsigned int leaked; +int ret =0 ; + +for (i =0 ; i < area->nr_pages; i += nr) { + ret =__vm_pages_enc_dec (area, i, nr, false); +if (ret) + goto rollback; + } +return 0 ; + +rollback: +while (i) { + i -= nr; +__vm_pages_enc_dec (area, i, nr, true); + } + + leaked =vm_compact_leaked_pages (area); +if (leaked) +pr_warn ("vmalloc: decryption failed, leaked %u pages\n" , + leaked); +return ret; +} +/** * vfree - Release memory allocated by vmalloc() * @addr: Memory base address
mm/vmalloc.c void vfree(const void *addr) · line 3558
Quoted 5 lines
mm/vmalloc.c +2
-0@@ -3457,6 +3554,9 @@ void vfree(const void *addr)return ; } +if (unlikely (vm->flags &VM_DECRYPTED )) +vm_pages_encrypt (vm);
I think we still have the vmalloc aliases at this point as we lazily reclaim them. We should call vm_unmap_aliases() before vm_pages_encrypt(). It matches the x86 __set_memory_enc_pgtable() as well with the explicit call to vm_unmap_aliases().
The vrealloc() path may have some issues as well but I haven't looked in detail. Not sure it actually re-allocs decrypted pages. The simplest is to reject vrealloc() for such vms until we have a use-case.
Quoted 9 lines
mm/vmalloc.c +9
-0+/** +* vzalloc_decrypted - allocate zeroed virtually contiguous decrypted memory +* @size: allocation size +* +* Like vmalloc_decrypted(), but the memory is set to zero. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vzalloc_decrypted_noprof (unsigned long size)
mm/vmalloc.c void *vzalloc_noprof(unsigned long size) · line 4360
Quoted 10 lines
mm/vmalloc.c +10
-0+{ +void *addr; + + addr =__vmalloc_node_range_noprof (size,1 ,VMALLOC_START ,VMALLOC_END , +GFP_KERNEL , +pgprot_decrypted (PAGE_KERNEL ), +VM_DECRYPTED ,NUMA_NO_NODE , +__builtin_return_address (0 )); +if (addr) +memset (addr,0 , size);
Talking to Suzuki, the small window between set_memory_decrypted() and memset() potentially exposing stale data is safe, at least for Arm CCA as the memory would be scrubbed (there are other places in the kernel where we do something similar). I assume that's also the case for other architectures, although not sure what pKVM does.
Signature · 1 line
Catalin
Quoted 17 lines · Catalin Marinas
Quoted 17 lines
mm/vmalloc.c +17
-0+/** +* vzalloc_decrypted - allocate zeroed virtually contiguous decrypted memory +* @size: allocation size +* +* Like vmalloc_decrypted(), but the memory is set to zero. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vzalloc_decrypted_noprof (unsigned long size) +{ +void *addr; + + addr =__vmalloc_node_range_noprof (size,1 ,VMALLOC_START ,VMALLOC_END , +GFP_KERNEL , +pgprot_decrypted (PAGE_KERNEL ), +VM_DECRYPTED ,NUMA_NO_NODE , +__builtin_return_address (0 ));
Quoted 8 lines
mm/vmalloc.c void *vzalloc_noprof(unsigned long size) · line 4360
Quoted 2 lines
mm/vmalloc.c +2
-0+if (addr) +memset (addr,0 , size);Talking to Suzuki, the small window between
set_memory_decrypted()andmemset()potentially exposing stale data is safe, at least for Arm CCA as the memory would be scrubbed (there are other places in the kernel where we do something similar). I assume that's also the case for other architectures, although not sure what pKVM does.
It seems like a poor practice though, this should probably be re-organized to use __GFP_ZERO so things are ordered sensibly.
But what is the purpose of this? I guess some hyperv thing - but shouldn't we have a more structured way to "DMA map" things for the hypervisor instead of stuff like this? Why can't you use dma_alloc_coherent() which actually gives you an address that is sensible to pass to the hypervisor?
Jason
Quoted 27 lines · Jason Gunthorpe
Quoted 17 lines · Catalin Marinas
Quoted 17 lines
mm/vmalloc.c +17
-0+/** +* vzalloc_decrypted - allocate zeroed virtually contiguous decrypted memory +* @size: allocation size +* +* Like vmalloc_decrypted(), but the memory is set to zero. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vzalloc_decrypted_noprof (unsigned long size) +{ +void *addr; + + addr =__vmalloc_node_range_noprof (size,1 ,VMALLOC_START ,VMALLOC_END , +GFP_KERNEL , +pgprot_decrypted (PAGE_KERNEL ), +VM_DECRYPTED ,NUMA_NO_NODE , +__builtin_return_address (0 ));Quoted 8 lines
mm/vmalloc.c void *vzalloc_noprof(unsigned long size) · line 4360
Quoted 2 lines
mm/vmalloc.c +2
-0+if (addr) +memset (addr,0 , size);Talking to Suzuki, the small window between
set_memory_decrypted()andmemset()potentially exposing stale data is safe, at least for Arm CCA as the memory would be scrubbed (there are other places in the kernel where we do something similar). I assume that's also the case for other architectures, although not sure what pKVM does.
Quoted 2 lines
It seems like a poor practice though, this should probably be re-organized to use
__GFP_ZEROso things are ordered sensibly.
__GFP_ZERO doesn't work if the intermediate set_memory_decrypted() mangles the data (e.g. changes encryption keys) and it no longer reads as zeros.
Quoted 5 lines
But what is the purpose of this? I guess some hyperv thing - but shouldn't we have a more structured way to "DMA map" things for the hypervisor instead of stuff like this? Why can't you use
dma_alloc_coherent()which actually gives you an address that is sensible to pass to the hypervisor?
IIRC netvsc_init_buf() uses vzalloc() to allocate some memory and that buffer ends up in set_memory_decrypted() via vmbus_establish_gpadl(). arm64 does not support changing the decrypted/shared attributed of vmalloc mappings and I don't think we should add it. Better to just allocate it properly upfront.
We might be able to use the DMA API but we won't get something like vmalloc() - physically non-contiguous. I think dma_alloc_noncontiguous() just falls back to dma_direct_alloc_pages() in the absence of an iommu.
Signature · 1 line
Catalin
Quoted 28 lines · Catalin Marinas
Quoted 27 lines · Jason Gunthorpe
Quoted 25 lines · Catalin Marinas
Quoted 9 lines
mm/vmalloc.c +9
-0+/** +* vzalloc_decrypted - allocate zeroed virtually contiguous decrypted memory +* @size: allocation size +* +* Like vmalloc_decrypted(), but the memory is set to zero. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vzalloc_decrypted_noprof (unsigned long size)mm/vmalloc.c void *vzalloc_noprof(unsigned long size) · line 4360
Quoted 10 lines
mm/vmalloc.c +10
-0+{ +void *addr; + + addr =__vmalloc_node_range_noprof (size,1 ,VMALLOC_START ,VMALLOC_END , +GFP_KERNEL , +pgprot_decrypted (PAGE_KERNEL ), +VM_DECRYPTED ,NUMA_NO_NODE , +__builtin_return_address (0 )); +if (addr) +memset (addr,0 , size);Talking to Suzuki, the small window between
set_memory_decrypted()andmemset()potentially exposing stale data is safe, at least for Arm CCA as the memory would be scrubbed (there are other places in the kernel where we do something similar). I assume that's also the case for other architectures, although not sure what pKVM does.
Quoted 6 lines
Quoted 2 lines
It seems like a poor practice though, this should probably be re-organized to use
__GFP_ZEROso things are ordered sensibly.
__GFP_ZEROdoesn't work if the intermediateset_memory_decrypted()mangles the data (e.g. changes encryption keys) and it no longer reads as zeros.
I thought arches are either preserving the memory content or zeroing it, you are saying some arch leaves it as garbage? I'd argue that's an arch bug and they should clear it in their path.
Otherwise this sharp edge is not documented and we have many other places getting it wrong, eg system_heap_allocate() doesn't re-zero the memory after decrypting it.
Quoted 11 lines
Quoted 5 lines
But what is the purpose of this? I guess some hyperv thing - but shouldn't we have a more structured way to "DMA map" things for the hypervisor instead of stuff like this? Why can't you use
dma_alloc_coherent()which actually gives you an address that is sensible to pass to the hypervisor?IIRC
netvsc_init_buf()usesvzalloc()to allocate some memory and that buffer ends up inset_memory_decrypted()viavmbus_establish_gpadl(). arm64 does not support changing the decrypted/shared attributed of vmalloc mappings and I don't think we should add it. Better to just allocate it properly upfront.
Sure
Quoted 2 lines
We might be able to use the DMA API but we won't get something like
vmalloc()- physically non-contiguous.
The entry point is dma_alloc_noncontiguous() and you get a scatterlist back.
Quoted 2 lines
I think
dma_alloc_noncontiguous()just falls back todma_direct_alloc_pages()in the absence of an iommu.
In all cases you get a scatterlist with a CPU list and a DMA list. iommu gives a smaller DMA list.
If you want a vmap then you can feed that CPU page list from the sgl into vmap().
A dma_alloc_noncontiguous_vmap() helper would not be hard to make, and IMHO, would make alot more sense for hyperv to treat the memory access from the hypervisor as "DMA" instead of trying to re-invent the DMA API.. :\
HCH was already saying we should not be allowing drivers to use set_memory_decrypted() at all, and hyperv is the biggest non-core user right now...
Jason
From: Jason Gunthorpe <[email protected]> Sent: Friday, June 12, 2026 11:18 AM
Quoted 33 lines · Catalin Marinas
Quoted 31 lines · Jason Gunthorpe
Quoted 29 lines · Catalin Marinas
Quoted 17 lines
Quoted 17 lines
mm/vmalloc.c +17
-0+/** +* vzalloc_decrypted - allocate zeroed virtually contiguous decrypted memory +* @size: allocation size +* +* Like vmalloc_decrypted(), but the memory is set to zero. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vzalloc_decrypted_noprof (unsigned long size) +{ +void *addr; + + addr =__vmalloc_node_range_noprof (size,1 ,VMALLOC_START ,VMALLOC_END , +GFP_KERNEL , +pgprot_decrypted (PAGE_KERNEL ), +VM_DECRYPTED ,NUMA_NO_NODE , +__builtin_return_address (0 ));Quoted 8 lines
mm/vmalloc.c void *vzalloc_noprof(unsigned long size) · line 4360
Quoted 2 lines
mm/vmalloc.c +2
-0+if (addr) +memset (addr,0 , size);Talking to Suzuki, the small window between
set_memory_decrypted()andmemset()potentially exposing stale data is safe, at least for Arm CCA as the memory would be scrubbed (there are other places in the kernel where we do something similar). I assume that's also the case for other architectures, although not sure what pKVM does.It seems like a poor practice though, this should probably be re-organized to use
__GFP_ZEROso things are ordered sensibly.
Quoted 7 lines
Quoted 3 lines
__GFP_ZEROdoesn't work if the intermediateset_memory_decrypted()mangles the data (e.g. changes encryption keys) and it no longer reads as zeros.I thought arches are either preserving the memory content or zeroing it, you are saying some arch leaves it as garbage? I'd argue that's an arch bug and they should clear it in their path.
AMD SEV-SNP leaves the memory contents as garbage after an encryption or decryption state change. On the flip side, my understanding has been that TDX zeroes the memory (or at least has an option to do so) after such a state change, though a couple of AI chats say TDX also leaves garbage. To be sure, I'd have to run an experiment to check in a TDX guest on Hyper-V.
Quoted 4 lines
Otherwise this sharp edge is not documented and we have many other places getting it wrong, eg
system_heap_allocate()doesn't re-zero the memory after decrypting it.
In the Hyper-V code that uses set_memory_decrypted()/encrypted(), there's always an explicit call to set the memory to zero afterwards.
Michael
Quoted 40 lines
Quoted 7 lines
Quoted 6 lines
But what is the purpose of this? I guess some hyperv thing - but shouldn't we have a more structured way to "DMA map" things for the hypervisor instead of stuff like this? Why can't you use
dma_alloc_coherent()which actually gives you an address that is sensible to pass to the hypervisor?Quoted 5 lines
IIRC
netvsc_init_buf()usesvzalloc()to allocate some memory and that buffer ends up inset_memory_decrypted()viavmbus_establish_gpadl(). arm64 does not support changing the decrypted/shared attributed of vmalloc mappings and I don't think we should add it. Better to just allocate it properly upfront.Sure
Quoted 2 lines
We might be able to use the DMA API but we won't get something like
vmalloc()- physically non-contiguous.The entry point is
dma_alloc_noncontiguous()and you get a scatterlist back.Quoted 2 lines
I think
dma_alloc_noncontiguous()just falls back todma_direct_alloc_pages()in the absence of an iommu.In all cases you get a scatterlist with a CPU list and a DMA list. iommu gives a smaller DMA list.
If you want a vmap then you can feed that CPU page list from the sgl into
vmap().A
dma_alloc_noncontiguous_vmap()helper would not be hard to make, and IMHO, would make alot more sense for hyperv to treat the memory access from the hypervisor as "DMA" instead of trying to re-invent the DMA API.. :\HCH was already saying we should not be allowing drivers to use
set_memory_decrypted()at all, and hyperv is the biggest non-core user right now...Jason
Quoted 10 lines · Michael Kelley
Quoted 3 lines
I thought arches are either preserving the memory content or zeroing it, you are saying some arch leaves it as garbage? I'd argue that's an arch bug and they should clear it in their path.
AMD SEV-SNP leaves the memory contents as garbage after an encryption or decryption state change. On the flip side, my understanding has been that TDX zeroes the memory (or at least has an option to do so) after such a state change, though a couple of AI chats say TDX also leaves garbage. To be sure, I'd have to run an experiment to check in a TDX guest on Hyper-V.
So there are many bugs then if the pre-zero is lost and you have to zero it again. Even swiotlb doesn't reliably zero it's pools in the right order under these rules, though alloc coherent does get it right at least.
IMHO this is too sketchy to be usable and optimizing for AMD is not the right call, IMHO.
Quoted 6 lines
Quoted 3 lines
Otherwise this sharp edge is not documented and we have many other places getting it wrong, eg
system_heap_allocate()doesn't re-zero the memory after decrypting it.In the Hyper-V code that uses set_memory_decrypted()/encrypted(), there's always an explicit call to set the memory to zero afterwards.
Good for it, maybe next time improve the APIs :(
Even more compelling that hyper-v should be using the dma api..
Jason
Quoted 22 lines · Jason Gunthorpe
Quoted 21 lines · Catalin Marinas
Quoted 20 lines · Jason Gunthorpe
Quoted 19 lines · Catalin Marinas
Quoted 19 lines
mm/vmalloc.c +19
-0+/** +* vzalloc_decrypted - allocate zeroed virtually contiguous decrypted memory +* @size: allocation size +* +* Like vmalloc_decrypted(), but the memory is set to zero. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +void *vzalloc_decrypted_noprof (unsigned long size) +{ +void *addr; + + addr =__vmalloc_node_range_noprof (size,1 ,VMALLOC_START ,VMALLOC_END , +GFP_KERNEL , +pgprot_decrypted (PAGE_KERNEL ), +VM_DECRYPTED ,NUMA_NO_NODE , +__builtin_return_address (0 )); +if (addr) +memset (addr,0 , size);
[...]
Quoted 12 lines
Quoted 11 lines
Quoted 5 lines
But what is the purpose of this? I guess some hyperv thing - but shouldn't we have a more structured way to "DMA map" things for the hypervisor instead of stuff like this? Why can't you use
dma_alloc_coherent()which actually gives you an address that is sensible to pass to the hypervisor?IIRC
netvsc_init_buf()usesvzalloc()to allocate some memory and that buffer ends up inset_memory_decrypted()viavmbus_establish_gpadl(). arm64 does not support changing the decrypted/shared attributed of vmalloc mappings and I don't think we should add it. Better to just allocate it properly upfront.
Quoted 7 lines
Sure
Quoted 2 lines
We might be able to use the DMA API but we won't get something like
vmalloc()- physically non-contiguous.The entry point is
dma_alloc_noncontiguous()and you get a scatterlist back.
Yes but not scattered pages unless there's an iommu behind. Anyway, that's an implementation detail, something like dma_alloc_noncontiguous_vmap() could allocate scattered pages as a fallback.
Quoted 9 lines
Quoted 2 lines
I think
dma_alloc_noncontiguous()just falls back todma_direct_alloc_pages()in the absence of an iommu.In all cases you get a scatterlist with a CPU list and a DMA list. iommu gives a smaller DMA list.
If you want a vmap then you can feed that CPU page list from the sgl into
vmap().
Quoted 8 lines
A
dma_alloc_noncontiguous_vmap()helper would not be hard to make, and IMHO, would make alot more sense for hyperv to treat the memory access from the hypervisor as "DMA" instead of trying to re-invent the DMA API.. :\HCH was already saying we should not be allowing drivers to use
set_memory_decrypted()at all, and hyperv is the biggest non-core user right now...
That's a good aim longer term. I'm not familiar with hyper-v but I think it needs a mix of private or shared allocations depending on whether a paravisor is present. That's handled by the vmbus code and the information is encoded in the vmbus_channel objects.
Currently, something like netvsc_init_buf() just does a vzalloc() and passes it down to vmbus_establish_gpadl() which knows how to interpret the channel encryption status. I assume with the vzalloc_decrypted() API, that info needs to be interpreted at the netvsc_init_buf() level to know which allocation to call.
If we move towards a dma_alloc_noncontiguous_vmap() API we need vmbus to encode the encryption requirement in the hv_device::device somehow so that force_dma_unencrypted() knows what do return. We have the DMA_ATTR_CC_SHARED but that's not interpreted on the DMA alloc path, so there's a bit more work needed on the DMA API I think (not sure whether Aneesh's series covers any of this).
Signature · 1 line
Catalin
Quoted 7 lines · Catalin Marinas
Quoted 2 lines
The entry point is
dma_alloc_noncontiguous()and you get a scatterlist back.Yes but not scattered pages unless there's an iommu behind. Anyway, that's an implementation detail, something like
dma_alloc_noncontiguous_vmap()could allocate scattered pages as a fallback.
Oh I never noticed it deliberately returns only a single dma entry. I think that could be optionally weakened without alot of trouble
There is also dma_vmap_noncontiguous() already, so I think the main framework is there, though it seems like it needs a a bit mmore features.
Quoted 5 lines
Currently, something like
netvsc_init_buf()just does avzalloc()and passes it down tovmbus_establish_gpadl()which knows how to interpret the channel encryption status. I assume with thevzalloc_decrypted()API, that info needs to be interpreted at thenetvsc_init_buf()level to know which allocation to call.
But it doesn't end at alloc does it? hyperv will still have to reach into the vmap and convert it into an appropriate IPA to pass to the hypervisor. That really needs to use the arch helpers the DMA API has and those should not be called by any sort of driver environment like hyperv.
Quoted 3 lines
If we move towards a
dma_alloc_noncontiguous_vmap()API we need vmbus to encode the encryption requirement in thehv_device::devicesomehow so thatforce_dma_unencrypted()knows what do return.
Yes, they would have to act like PCI and mark in-TEE and out of-TEE struct devices properly so the DMA API knows what to do instead of open coding a copy of all this logic in hyperv.
Quoted 2 lines
We have the
DMA_ATTR_CC_SHAREDbut that's not interpreted on the DMA alloc path,
It is to describe memory that was deliberately allocated as decrypted, not to control allocation choices.
Quoted 2 lines
so there's a bit more work needed on the DMA API I think (not sure whether Aneesh's series covers any of this).
I don't think it does directly. It largely sets the stage to properly allow a struct device to opt out of force_dma_unencrypted() so we get support a T=1 PCI device.
Jason