From: Catalin Marinas <catalin.marinas@arm.com>
To: Kameron Carr <kameroncarr@linux.microsoft.com>
Cc: akpm@linux-foundation.org, urezki@gmail.com, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, rppt@kernel.org,
mhklinux@outlook.com, linux-coco@lists.linux.dev,
Suzuki K Poulose <Suzuki.Poulose@arm.com>
Subject: Re: [RFC PATCH] mm/vmalloc: add vmalloc_decrypted() and vzalloc_decrypted()
Date: Mon, 8 Jun 2026 16:37:02 +0100 [thread overview]
Message-ID: <aibhnnDFQTHWMEFe@arm.com> (raw)
In-Reply-To: <20260521205834.1012925-1-kameroncarr@linux.microsoft.com>
+ linux-coco, Suzuki (for the arm64 behaviour)
On Thu, May 21, 2026 at 01:58:34PM -0700, Kameron Carr wrote:
> 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.
>
> Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
> Link: https://lore.kernel.org/linux-arm-kernel/ZmNJdSxSz-sYpVgI@arm.com/
> Signed-off-by: Kameron Carr <kameroncarr@linux.microsoft.com>
> ---
> 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/20260521205834.1012925-1-kameroncarr@linux.microsoft.com
[...]
> +/*
> + * 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
> @@ -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.
> +/**
> + * 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);
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.
--
Catalin
parent reply other threads:[~2026-06-08 15:37 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <20260521205834.1012925-1-kameroncarr@linux.microsoft.com>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aibhnnDFQTHWMEFe@arm.com \
--to=catalin.marinas@arm.com \
--cc=Suzuki.Poulose@arm.com \
--cc=akpm@linux-foundation.org \
--cc=kameroncarr@linux.microsoft.com \
--cc=linux-coco@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhklinux@outlook.com \
--cc=rppt@kernel.org \
--cc=urezki@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox