From: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>
To: linux-coco@lists.linux.dev, kvmarm@lists.linux.dev,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, iommu@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.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>,
Thomas Gleixner <tglx@kernel.org>, Will Deacon <will@kernel.org>,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-media@vger.kernel.org, linux-mm@kvack.org
Subject: [RFC PATCH v7 02/13] mm: Add an allocator for CoCo shared memory
Date: Mon, 21 Sep 2026 20:18:36 +0530 [thread overview]
Message-ID: <20260921144847.501151-3-aneesh.kumar@kernel.org> (raw)
In-Reply-To: <20260921144847.501151-1-aneesh.kumar@kernel.org>
Confidential-computing guests may require memory shared with the host to
be aligned and transitioned in units larger than PAGE_SIZE. Several DMA
users need struct page-backed allocations satisfying these requirements.
Provide a common allocator instead of requiring each user to open-code
this sequence.
Add alloc_cc_shared_pages() and its node-aware variant. The allocator
rounds the requested size to the architecture's shared granule,
allocates suitably aligned contiguous pages and transitions the
complete range to shared state. It also preserves the caller's GFP
policy.
A private-to-shared transition may alter memory contents. Mask
__GFP_ZERO from the underlying allocation and when requested, clear the
complete transitioned range after cc_make_shared() succeeds.
Return the page and transitioned size so free_cc_shared_pages() can
restore the complete range to private state before freeing it. If
private state cannot be established, retain the allocation instead of
returning a possibly shared page to the buddy allocator.
Also provide the shared-granule geometry and byte-oriented transition
helpers used by the allocator and by callers managing their own backing
memory.
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
include/linux/cc_shared.h | 39 +++++++++
mm/Makefile | 1 +
mm/cc_shared.c | 172 ++++++++++++++++++++++++++++++++++++++
3 files changed, 212 insertions(+)
create mode 100644 include/linux/cc_shared.h
create mode 100644 mm/cc_shared.c
diff --git a/include/linux/cc_shared.h b/include/linux/cc_shared.h
new file mode 100644
index 000000000000..5f8db7c468c5
--- /dev/null
+++ b/include/linux/cc_shared.h
@@ -0,0 +1,39 @@
+/* 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>
+
+struct page;
+
+struct cc_shared_pages {
+ struct page *page;
+ size_t shared_size;
+};
+
+struct cc_shared_layout {
+ size_t requested_size;
+ size_t shared_size;
+ size_t alignment;
+};
+
+/*
+ * 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_calc_layout(size_t requested, struct cc_shared_layout *layout);
+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);
+int alloc_cc_shared_pages_node(int nid, gfp_t gfp,
+ size_t requested, struct cc_shared_pages *mem);
+int alloc_cc_shared_pages(gfp_t gfp,
+ size_t requested, struct cc_shared_pages *mem);
+void free_cc_shared_pages(struct cc_shared_pages *mem);
+
+#endif /* _LINUX_CC_SHARED_H */
diff --git a/mm/Makefile b/mm/Makefile
index e7245cb88c66..6e6544428422 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -56,6 +56,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
compaction.o show_mem.o \
interval_tree.o list_lru.o workingset.o \
debug.o gup.o mmap_lock.o vma_init.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..3e33681218f1
--- /dev/null
+++ b/mm/cc_shared.c
@@ -0,0 +1,172 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2026 ARM Ltd.
+ */
+#include <linux/align.h>
+#include <linux/cc_shared.h>
+#include <linux/errno.h>
+#include <linux/export.h>
+#include <linux/gfp.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);
+
+int cc_shared_calc_layout(size_t requested, struct cc_shared_layout *layout)
+{
+ size_t granule, rounded;
+
+ if (!requested || !layout)
+ return -EINVAL;
+
+ granule = cc_shared_granule_size();
+ if (check_add_overflow(requested, granule - 1, &rounded))
+ return -EOVERFLOW;
+
+ rounded = ALIGN_DOWN(rounded, granule);
+ layout->requested_size = requested;
+ layout->shared_size = rounded;
+ layout->alignment = granule;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(cc_shared_calc_layout);
+
+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);
+}
+
+int alloc_cc_shared_pages_node(int nid, gfp_t gfp,
+ size_t requested, struct cc_shared_pages *mem)
+{
+ struct cc_shared_layout layout;
+ struct page *page;
+ unsigned int order;
+ bool zero = gfp & __GFP_ZERO;
+ int ret;
+
+ if (!mem)
+ return -EINVAL;
+
+ ret = cc_shared_calc_layout(requested, &layout);
+ if (ret)
+ return ret;
+
+ order = get_order(layout.shared_size);
+ if (order > MAX_PAGE_ORDER)
+ return -EINVAL;
+
+ /*
+ * State transitions require a linear-map address and may modify memory.
+ * Allocate from low memory and defer requested zeroing until afterwards.
+ */
+ gfp &= ~(__GFP_HIGHMEM | __GFP_ZERO);
+ if (nid == NUMA_NO_NODE)
+ page = alloc_pages(gfp, order);
+ else
+ page = alloc_pages_node(nid, gfp, order);
+ if (!page)
+ return -ENOMEM;
+
+ ret = cc_make_shared(page_address(page), layout.shared_size);
+ if (ret) {
+ if (!cc_make_private(page_address(page), layout.shared_size))
+ __free_pages(page, order);
+ else
+ pr_warn_ratelimited("leaking %zu bytes with uncertain shared state\n",
+ layout.shared_size);
+ return ret;
+ }
+
+ if (zero)
+ memset(page_address(page), 0, layout.shared_size);
+
+ mem->page = page;
+ mem->shared_size = layout.shared_size;
+ return 0;
+}
+EXPORT_SYMBOL_GPL(alloc_cc_shared_pages_node);
+
+int alloc_cc_shared_pages(gfp_t gfp,
+ size_t requested, struct cc_shared_pages *mem)
+{
+ return alloc_cc_shared_pages_node(NUMA_NO_NODE, gfp, requested, mem);
+}
+EXPORT_SYMBOL_GPL(alloc_cc_shared_pages);
+
+void free_cc_shared_pages(struct cc_shared_pages *mem)
+{
+ if (!mem || !mem->page)
+ return;
+
+ if (cc_make_private(page_address(mem->page), mem->shared_size)) {
+ pr_warn_ratelimited("leaking %zu bytes that cannot be made private\n",
+ mem->shared_size);
+ return;
+ }
+
+ __free_pages(mem->page, get_order(mem->shared_size));
+ mem->page = NULL;
+ mem->shared_size = 0;
+}
+EXPORT_SYMBOL_GPL(free_cc_shared_pages);
--
2.43.0
next prev parent reply other threads:[~2026-09-21 14:49 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-21 14:48 [RFC PATCH v7 00/13] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
2026-09-21 14:48 ` [RFC PATCH v7 01/13] arm64: realm: Add RHI helper to query IPA state change alignment Aneesh Kumar K.V (Arm)
2026-09-21 14:48 ` Aneesh Kumar K.V (Arm) [this message]
2026-09-22 16:25 ` [RFC PATCH v7 02/13] mm: Add an allocator for CoCo shared memory Catalin Marinas
2026-09-22 16:51 ` Jason Gunthorpe
2026-09-23 0:33 ` Suzuki K Poulose
2026-09-23 5:53 ` Aneesh Kumar K.V
2026-09-23 8:31 ` Aneesh Kumar K.V
2026-09-23 10:10 ` Catalin Marinas
2026-09-23 9:42 ` Catalin Marinas
2026-09-23 9:59 ` Aneesh Kumar K.V
2026-09-23 10:28 ` Aneesh Kumar K.V
2026-09-23 10:40 ` Catalin Marinas
2026-09-23 13:06 ` Jason Gunthorpe
2026-09-23 14:58 ` Aneesh Kumar K.V
2026-09-23 15:11 ` Suzuki K Poulose
2026-09-23 15:21 ` Jason Gunthorpe
2026-09-23 16:28 ` Kameron Carr
2026-09-23 17:23 ` Jason Gunthorpe
2026-09-23 18:36 ` Michael Kelley
2026-09-23 13:00 ` Jason Gunthorpe
2026-09-23 15:20 ` Mostafa Saleh
2026-09-21 14:48 ` [RFC PATCH v7 03/13] arm64: realm: Expose the CCA shared granule size through mem_encrypt ops Aneesh Kumar K.V (Arm)
2026-09-21 14:48 ` [RFC PATCH v7 04/13] irqchip/gic-v3-its: Resolve the default NUMA node explicitly Aneesh Kumar K.V (Arm)
2026-09-21 14:48 ` [RFC PATCH v7 05/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
2026-09-21 14:48 ` [RFC PATCH v7 06/13] dma-contiguous: Accept an explicit minimum alignment Aneesh Kumar K.V (Arm)
2026-09-23 10:35 ` Catalin Marinas
2026-09-23 11:49 ` Aneesh Kumar K.V
2026-09-23 13:49 ` Catalin Marinas
2026-09-21 14:48 ` [RFC PATCH v7 07/13] dma-pool: Allocate CoCo atomic pools using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
2026-09-21 14:48 ` [RFC PATCH v7 08/13] dma-direct: Align CoCo shared DMA allocations to the shared granule size Aneesh Kumar K.V (Arm)
2026-09-21 14:48 ` [RFC PATCH v7 09/13] swiotlb: Align shared IO TLB pools " Aneesh Kumar K.V (Arm)
2026-09-21 14:48 ` [RFC PATCH v7 10/13] swiotlb: Reject misaligned restricted DMA pools for CoCo guests Aneesh Kumar K.V (Arm)
2026-09-21 14:48 ` [RFC PATCH v7 11/13] dma-buf: system_heap: Limit scatterlist entries to the buffer size Aneesh Kumar K.V (Arm)
2026-09-21 14:48 ` [RFC PATCH v7 12/13] dma-buf: system_heap: Allocate shared buffers using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
2026-09-22 16:39 ` Catalin Marinas
2026-09-23 8:32 ` Aneesh Kumar K.V
2026-09-23 8:46 ` Christian König
2026-09-21 14:48 ` [RFC PATCH v7 13/13] swiotlb: Make rounded shared pool capacity allocatable Aneesh Kumar K.V (Arm)
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=20260921144847.501151-3-aneesh.kumar@kernel.org \
--to=aneesh.kumar@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=catalin.marinas@arm.com \
--cc=christian.koenig@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=joro@8bytes.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-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=m.szyprowski@samsung.com \
--cc=maz@kernel.org \
--cc=robin.murphy@arm.com \
--cc=steven.price@arm.com \
--cc=sumit.semwal@linaro.org \
--cc=suzuki.poulose@arm.com \
--cc=tglx@kernel.org \
--cc=will@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
all inboxes | Powered by JetHome®