From: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>
To: linux-coco@lists.linux.dev
Cc: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Catalin Marinas" <catalin.marinas@arm.com>,
"Christian König" <christian.koenig@amd.com>,
"Jason Gunthorpe" <jgg@ziepe.ca>,
"Joerg Roedel" <joro@8bytes.org>, "Marc Zyngier" <maz@kernel.org>,
"Marek Szyprowski" <m.szyprowski@samsung.com>,
"Robin Murphy" <robin.murphy@arm.com>,
"Steven Price" <steven.price@arm.com>,
"Sumit Semwal" <sumit.semwal@linaro.org>,
"Suzuki K Poulose" <suzuki.poulose@arm.com>,
"Will Deacon" <will@kernel.org>,
"T.J. Mercier" <tjmercier@google.com>,
"Madhavan Srinivasan" <maddy@linux.ibm.com>,
"Heiko Carstens" <hca@linux.ibm.com>,
"Christian Borntraeger" <borntraeger@linux.ibm.com>,
"Sven Schnelle" <svens@linux.ibm.com>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
"Kiryl Shutsemau" <kas@kernel.org>,
"Rick Edgecombe" <rick.p.edgecombe@intel.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Thomas Gleixner" <tglx@kernel.org>,
"Ingo Molnar" <mingo@redhat.com>,
"Borislav Petkov" <bp@alien8.de>,
"Haiyang Zhang" <haiyangz@microsoft.com>,
"Wei Liu" <wei.liu@kernel.org>,
"Dexuan Cui" <decui@microsoft.com>,
"Andy Lutomirski" <luto@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Alexander Gordeev" <agordeev@linux.ibm.com>,
"Gerald Schaefer" <gerald.schaefer@linux.ibm.com>,
"Vasily Gorbik" <gor@linux.ibm.com>,
x86@kernel.org, dri-devel@lists.freedesktop.org,
iommu@lists.linux.dev, linaro-mm-sig@lists.linaro.org,
linux-arm-kernel@lists.infradead.org,
linux-media@vger.kernel.org, linux-mm@kvack.org,
kvmarm@lists.linux.dev, patches@lists.linux.dev
Subject: [PATCH v11 02/23] mm: Add an allocator for CoCo shared memory
Date: Wed, 2 Dec 2026 15:24:25 +0530 [thread overview]
Message-ID: <20261202095446.2029567-3-aneesh.kumar@kernel.org> (raw)
In-Reply-To: <20261202095446.2029567-1-aneesh.kumar@kernel.org>
Confidential-computing guests may require shared memory to be aligned
and transitioned in units larger than PAGE_SIZE. Provide a common
allocator instead of requiring callers to open-code allocation and state
changes.
Add alloc_cc_shared() and alloc_cc_shared_node(), taking a byte count
and returning a linear-map address. Round internally to the sharing
granule and a power-of-two number of pages, and share the entire
allocation. Its physical base is aligned to the allocation size. Always
request compound metadata so cc_shared_alloc_size() and free_cc_shared()
can recover the actual size without a caller-supplied descriptor or
order.
Exclude highmem and movable allocations even without memory encryption:
the returned address must be in the linear map and there is no migration
support. Preserve task NUMA policy for NUMA_NO_NODE. Defer requested
zeroing until after the transition, which may modify memory contents.
Retain allocations if sharing fails or private state cannot be restored.
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
MAINTAINERS | 2 +
include/linux/cc_shared.h | 25 ++++
mm/Makefile | 1 +
mm/cc_shared.c | 240 ++++++++++++++++++++++++++++++++++++++
4 files changed, 268 insertions(+)
create mode 100644 include/linux/cc_shared.h
create mode 100644 mm/cc_shared.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 4cadc468d28d..6e723c6eb31b 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -6763,7 +6763,9 @@ L: linux-coco@lists.linux.dev
L: linux-mm@kvack.org
S: Maintained
F: include/linux/cc_platform.h
+F: include/linux/cc_shared.h
F: include/linux/mem_encrypt.h
+F: mm/cc_shared.c
CONFIDENTIAL COMPUTING THREAT MODEL FOR X86 VIRTUALIZATION (SNP/TDX)
M: Elena Reshetova <elena.reshetova@intel.com>
diff --git a/include/linux/cc_shared.h b/include/linux/cc_shared.h
new file mode 100644
index 000000000000..23ba457b5f57
--- /dev/null
+++ b/include/linux/cc_shared.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_CC_SHARED_H
+#define _LINUX_CC_SHARED_H
+
+#include <linux/gfp_types.h>
+#include <linux/types.h>
+
+/*
+ * Architectures may override this to return the granule used for transitions
+ * between private and shared memory. The value must be a power of two and no
+ * smaller than PAGE_SIZE.
+ */
+size_t arch_cc_shared_granule_size(void);
+
+size_t cc_shared_granule_size(void);
+int cc_shared_round_size(size_t requested, size_t *size);
+bool cc_shared_range_valid(phys_addr_t base, size_t size);
+int cc_make_shared(void *addr, size_t size);
+int cc_make_private(void *addr, size_t size);
+void *alloc_cc_shared_node(int nid, gfp_t gfp, size_t len);
+void *alloc_cc_shared(gfp_t gfp, size_t len);
+size_t cc_shared_alloc_size(void *addr);
+void free_cc_shared(void *addr);
+
+#endif /* _LINUX_CC_SHARED_H */
diff --git a/mm/Makefile b/mm/Makefile
index 2a3ec53d62ee..a712656449d1 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -57,6 +57,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
interval_tree.o list_lru.o workingset.o \
debug.o gup.o mmap_lock.o vma_init.o char-mem.o \
$(mmu-y)
+obj-y += cc_shared.o
# Give 'page_alloc' its own module-parameter namespace
page-alloc-y := page_alloc.o
diff --git a/mm/cc_shared.c b/mm/cc_shared.c
new file mode 100644
index 000000000000..e60e2269adb6
--- /dev/null
+++ b/mm/cc_shared.c
@@ -0,0 +1,240 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2026 ARM Ltd.
+ */
+#include <linux/align.h>
+#include <linux/cc_platform.h>
+#include <linux/cc_shared.h>
+#include <linux/errno.h>
+#include <linux/export.h>
+#include <linux/gfp.h>
+#include <linux/kernel.h>
+#include <linux/log2.h>
+#include <linux/mm.h>
+#include <linux/mem_encrypt.h>
+#include <linux/numa.h>
+#include <linux/overflow.h>
+#include <linux/set_memory.h>
+
+size_t __weak arch_cc_shared_granule_size(void)
+{
+ return PAGE_SIZE;
+}
+
+size_t cc_shared_granule_size(void)
+{
+ size_t granule = arch_cc_shared_granule_size();
+
+ if (WARN_ON_ONCE(granule < PAGE_SIZE || !is_power_of_2(granule)))
+ return PAGE_SIZE;
+
+ return granule;
+}
+EXPORT_SYMBOL_GPL(cc_shared_granule_size);
+
+/**
+ * cc_shared_round_size - round a byte count to the sharing granule
+ * @requested: nonzero number of bytes
+ * @size: rounded byte count, unchanged on failure
+ *
+ * Return: 0 on success, -EINVAL for a zero request, or -EOVERFLOW.
+ */
+int cc_shared_round_size(size_t requested, size_t *size)
+{
+ size_t granule, rounded;
+
+ if (!requested)
+ return -EINVAL;
+
+ granule = cc_shared_granule_size();
+ if (check_add_overflow(requested, granule - 1, &rounded))
+ return -EOVERFLOW;
+
+ *size = ALIGN_DOWN(rounded, granule);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(cc_shared_round_size);
+
+bool cc_shared_range_valid(phys_addr_t base, size_t size)
+{
+ size_t granule = cc_shared_granule_size();
+
+ if (!size)
+ return false;
+
+ return IS_ALIGNED(base, granule) && IS_ALIGNED(size, granule);
+}
+EXPORT_SYMBOL_GPL(cc_shared_range_valid);
+
+static int cc_validate_transition(void *addr, size_t size)
+{
+ phys_addr_t phys;
+
+ if (!addr || !size || !PAGE_ALIGNED(addr) ||
+ !virt_addr_valid(addr))
+ return -EINVAL;
+
+ phys = page_to_phys(virt_to_page(addr));
+ if (!cc_shared_range_valid(phys, size))
+ return -EINVAL;
+
+ return 0;
+}
+
+int cc_make_shared(void *addr, size_t size)
+{
+ int ret = cc_validate_transition(addr, size);
+
+ if (ret)
+ return ret;
+
+ return set_memory_decrypted((unsigned long)addr, size >> PAGE_SHIFT);
+}
+
+int cc_make_private(void *addr, size_t size)
+{
+ int ret = cc_validate_transition(addr, size);
+
+ if (ret)
+ return ret;
+
+ return set_memory_encrypted((unsigned long)addr, size >> PAGE_SHIFT);
+}
+
+/**
+ * alloc_cc_shared_node - allocate a whole block of shared memory
+ * @nid: NUMA node from which to allocate, or %NUMA_NO_NODE for task policy
+ * @gfp: allocation flags
+ * @len: requested number of bytes; must be nonzero
+ *
+ * Allocate a physically contiguous block of at least @len bytes.
+ * The physical base address is aligned to the full allocation size.
+ * The entire allocation is shared and zeroed.
+ *
+ * The allocation is rounded up to satisfy the platform's sharing granule,
+ * which can make small allocations inefficient. The current implementation
+ * uses compound pages for higher-order allocations to record their size.
+ *
+ * Allocations always have a linear-map address and cannot be migrated.
+ * __GFP_HIGHMEM and __GFP_MOVABLE are ignored. Callers must not split the
+ * allocation or free individual pages. Release the entire allocation with
+ * free_cc_shared().
+ *
+ * Must be called from sleepable context.
+ * Return: The allocation's linear-map address, or NULL on failure.
+ */
+void *alloc_cc_shared_node(int nid, gfp_t gfp, size_t len)
+{
+ bool encrypted = cc_platform_has(CC_ATTR_MEM_ENCRYPT);
+ struct page *page;
+ unsigned int order;
+ void *addr;
+ size_t size;
+
+ might_sleep();
+
+ if (cc_shared_round_size(len, &size))
+ return NULL;
+
+ order = get_order(size);
+ if (order > MAX_PAGE_ORDER)
+ return NULL;
+
+ /*
+ * Returning a linear-map address requires low memory even when memory
+ * encryption is inactive. Callers may retain virtual or physical
+ * addresses, and this allocator provides no migration support, so the
+ * allocation must not be marked movable in either case.
+ */
+ gfp &= ~(__GFP_HIGHMEM | __GFP_MOVABLE);
+ gfp |= __GFP_COMP;
+ gfp &= ~__GFP_ZERO;
+
+ if (nid == NUMA_NO_NODE)
+ page = alloc_pages(gfp, order);
+ else
+ page = alloc_pages_node(nid, gfp, order);
+ if (!page)
+ return NULL;
+
+ addr = page_address(page);
+ size = PAGE_SIZE << order;
+ if (encrypted && cc_make_shared(addr, size)) {
+ /*
+ * A failed transition may leave the range partially shared.
+ * Making already-private pages private again is not safe on
+ * every architecture. Retain the entire allocation rather than
+ * return pages with uncertain state to the page allocator.
+ */
+ pr_warn_ratelimited("leaking %zu bytes with uncertain shared state\n",
+ size);
+ return NULL;
+ }
+
+ memset(addr, 0, size);
+ return addr;
+}
+EXPORT_SYMBOL_GPL(alloc_cc_shared_node);
+
+/**
+ * alloc_cc_shared - allocate a whole block of shared memory
+ * @gfp: allocation flags
+ * @len: requested number of bytes; must be nonzero
+ *
+ * Equivalent to alloc_cc_shared_node() with %NUMA_NO_NODE.
+ *
+ * Return: The allocation's linear-map address, or NULL on failure.
+ */
+void *alloc_cc_shared(gfp_t gfp, size_t len)
+{
+ return alloc_cc_shared_node(NUMA_NO_NODE, gfp, len);
+}
+EXPORT_SYMBOL_GPL(alloc_cc_shared);
+
+/**
+ * cc_shared_alloc_size - get the size of a shared allocation
+ * @addr: original address returned by alloc_cc_shared_node() or
+ * alloc_cc_shared(); must be non-NULL and not yet freed
+ *
+ * Return: The full allocation size recorded in compound-page metadata,
+ * including any rounding performed by the allocator.
+ */
+size_t cc_shared_alloc_size(void *addr)
+{
+ return page_size(virt_to_page(addr));
+}
+EXPORT_SYMBOL_GPL(cc_shared_alloc_size);
+
+/**
+ * free_cc_shared - make an entire shared allocation private and free it
+ * @addr: original address returned by alloc_cc_shared_node() or
+ * alloc_cc_shared(), or NULL
+ *
+ * Use the allocation size from compound-page metadata. Partial frees are
+ * not supported. Retain the allocation if making it private fails.
+ *
+ * Must be called from sleepable context.
+ */
+void free_cc_shared(void *addr)
+{
+ struct page *page;
+ size_t size;
+
+ might_sleep();
+
+ if (!addr)
+ return;
+
+ page = virt_to_page(addr);
+ size = cc_shared_alloc_size(addr);
+ if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
+ if (cc_make_private(addr, size)) {
+ pr_warn_ratelimited("leaking %zu bytes that cannot be made private\n",
+ size);
+ return;
+ }
+ }
+
+ __free_pages(page, compound_order(page));
+}
+EXPORT_SYMBOL_GPL(free_cc_shared);
--
2.43.0
prev parent reply other threads:[~2026-12-02 9:55 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 9:54 [PATCH v11 00/23] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
2026-12-02 9:54 ` [PATCH v11 01/23] MAINTAINERS: Add CoCo memory sharing and encryption entry Aneesh Kumar K.V (Arm)
2026-12-02 9:54 ` Aneesh Kumar K.V (Arm) [this message]
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=20261202095446.2029567-3-aneesh.kumar@kernel.org \
--to=aneesh.kumar@kernel.org \
--cc=agordeev@linux.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=borntraeger@linux.ibm.com \
--cc=bp@alien8.de \
--cc=catalin.marinas@arm.com \
--cc=christian.koenig@amd.com \
--cc=dave.hansen@linux.intel.com \
--cc=decui@microsoft.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gerald.schaefer@linux.ibm.com \
--cc=gor@linux.ibm.com \
--cc=haiyangz@microsoft.com \
--cc=hca@linux.ibm.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=joro@8bytes.org \
--cc=kas@kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-coco@lists.linux.dev \
--cc=linux-media@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=luto@kernel.org \
--cc=m.szyprowski@samsung.com \
--cc=maddy@linux.ibm.com \
--cc=maz@kernel.org \
--cc=mingo@redhat.com \
--cc=patches@lists.linux.dev \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=rick.p.edgecombe@intel.com \
--cc=robin.murphy@arm.com \
--cc=steven.price@arm.com \
--cc=sumit.semwal@linaro.org \
--cc=suzuki.poulose@arm.com \
--cc=svens@linux.ibm.com \
--cc=tglx@kernel.org \
--cc=tjmercier@google.com \
--cc=wei.liu@kernel.org \
--cc=will@kernel.org \
--cc=x86@kernel.org \
/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