* [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory
@ 2026-09-24 10:05 Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 01/14] mm: Add an allocator for CoCo shared memory Aneesh Kumar K.V (Arm)
` (13 more replies)
0 siblings, 14 replies; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
This series tightens the alignment requirements for buffers that are shared
between confidential-computing guests and the host, and adds a common
allocator for host-shared memory.
When a guest runs with private memory, buffers shared with the hypervisor
are not only accessed by the guest. They are also accessed by the host
kernel, and the host may manage the corresponding shared/private state at a
granularity larger than the guest page size.
This matters for CCA systems where the Realm stage-2 mappings managed by
the RMM can still operate at 4K granularity, while the non-secure host may
manage the IPA state change at a larger page size, for example 64K. In that
case, allowing a guest to convert and share only a 4K subrange of a
host-managed granule is unsafe.
Architectures such as Arm can detect incorrect accesses to Realm physical
address space PFNs through GPC faults. However, relying on that as the only
line of defence is fragile and can still lead to kernel crashes. The risk
is especially visible for shared buffers that are later mmapped into
userspace, such as guest_memfd or dma-buf backed allocations. Once
userspace can access the mapping, the kernel cannot guarantee that
applications will only touch the intended 4K region rather than the whole
host page mapped into their address space. Those userspace addresses may
also be passed back into the kernel and accessed through the linear map,
resulting in a GPC fault.
To avoid this, host-shared buffers must satisfy two constraints:
- the address must be aligned to the CoCo shared-granule size
- the size must be a multiple of that granule size
The series adds a common CoCo shared-memory layer for enforcing these
constraints. It provides shared-granule geometry and range-validation
helpers, byte-oriented private/shared transition helpers, and
alloc_cc_shared_pages() with a node-aware variant. The allocator rounds a
request to the architecture shared granule, allocates suitably aligned
contiguous pages, transitions the complete allocation to shared state, and
returns the transitioned size alongside the page.
The corresponding free helper restores the complete allocation to private
state before returning it to the buddy allocator. If private state cannot
be restored safely, the allocation is deliberately leaked rather than
returning potentially shared memory for unrelated use. Since a
private-to-shared transition may modify memory contents, __GFP_ZERO is
applied after the transition.
The generic shared-granule size defaults to PAGE_SIZE. For arm64 CCA, the
series queries the host IPA state change alignment through the Realm Host
Interface, caches it during Realm initialization, and exposes it through
the arm64 memory-encryption operations.
The common allocator is used for host-shared allocations whose backing is
owned by an individual caller:
- GIC ITS command queues and tables
- dma-direct allocations backed by CMA or the page allocator
- backing allocations for the CoCo atomic DMA pools
- dma-buf system_cc_shared heap allocations
Hyper-V users of set_memory_encrypted() and set_memory_decrypted() are not
changed by this series. Those paths are not currently used by the arm64 CCA
code path, and therefore are not part of the arm64 CCA IPA state change
alignment problem addressed here.
NOTE: I have not added explicit MAINTAINERS entries for mm/cc_shared.c and
include/linux/cc_shared.h, as I am unsure whether we need a separate section
for common CoCo-related files. I will add the entries based on feedback.
Changes from v7:
https://lore.kernel.org/all/20260921144847.501151-1-aneesh.kumar@kernel.org
* Add the following new patches:
* "irqchip/gic-v3-its: Preallocate VPE L1 tables"
* "mm: Assert CoCo shared allocations may sleep"
* "mm: Zero memory during shared memory transitions"
* Drop the arm64 RHI and shared granule size patches so that the series can
be rebased on top of upstream to enable Shashiko review.
Changes from v6:
https://lore.kernel.org/all/20260904103452.1197239-1-aneesh.kumar@kernel.org
* Add a common allocator and geometry/transition helpers for CoCo host-shared
memory.
* Convert GIC ITS, dma-direct, atomic DMA pools, and the dma-buf
system_cc_shared heap to the common allocator.
* Limit dma-buf scatterlist entries to the requested buffer size so rounded
backing is not exposed to importers.
Changes from v5:
https://lore.kernel.org/all/20260706060432.1375570-1-aneesh.kumar@kernel.org
* Rebased to latest kernel
* Drop patch arm64: realm: Move Realm memory encryption ops to RSI code
Changes from v4:
https://lore.kernel.org/all/20260427063108.909019-1-aneesh.kumar@kernel.org
* Rename the helpers to use CoCo terminology
(mem_cc_shared_granule_size() / mem_cc_align_to_shared_granule() instead of
mem_decrypt_granule_size() / mem_decrypt_align()).
* Use __DMA_ATTR_ALLOC_CC_SHARED to pass CoCo shared allocation requirements
down to CMA-based allocation helpers.
* Add validation for restricted DMA pools to reject pools that are not aligned
to the shared granule size.
* Add dma-buf system heap handling for cc-shared buffers.
* Split the previous combined DMA/SWIOTLB/ITS change into smaller subsystem
patches covering ITS, DMA direct, SWIOTLB, restricted DMA pools, dma-buf
system heap, and arm64 Realm support.
* Rework arm64 Realm support by moving Realm memory encryption ops into RSI
code and exposing the CCA shared granule size through arm64_mem_crypt_ops.
Changes from v3:
https://lore.kernel.org/all/20260309102625.2315725-1-aneesh.kumar@kernel.org
* Fix build error reported by kernel test robot <lkp@intel.com>
Changes from v2:
https://lore.kernel.org/all/20251221160920.297689-1-aneesh.kumar@kernel.org
* Rebase to latest kernel
* Consider swiotlb always decrypted and don't align when allocating from swiotlb.
Changes from v1:
* Rename the helper to mem_encrypt_align
* Improve the commit message
* Handle DMA allocations from contiguous memory
* Handle DMA allocations from the pool
* swiotlb is still considered unencrypted. Support for an encrypted swiotlb pool
is left as TODO and is independent of this series.
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: "Christian König" <christian.koenig@amd.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Joerg Roedel (AMD) <joro@8bytes.org>
Cc: Marc Zyngier <maz@kernel.org>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Cc: Steven Price <steven.price@arm.com>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Suzuki K Poulose <suzuki.poulose@arm.com>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Will Deacon <will@kernel.org>
Cc: Russell King <linux@armlinux.org.uk>
Cc: Benjamin Gaignard <benjamin.gaignard@collabora.com>
Cc: Brian Starkey <Brian.Starkey@arm.com>
Cc: John Stultz <jstultz@google.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Radu Rendec <radu@rendec.net>
Cc: "T.J. Mercier" <tjmercier@google.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Christophe Leroy (CS GROUP) <chleroy@kernel.org>
Cc: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
Cc: Shrikanth Hegde <sshegde@linux.ibm.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Gerald Schaefer <gerald.schaefer@linux.ibm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: x86@kernel.org
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Kiryl Shutsemau <kas@kernel.org>
Cc: Rick Edgecombe <rick.p.edgecombe@intel.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Cc: Wei Liu <wei.liu@kernel.org>
Cc: Dexuan Cui <decui@microsoft.com>
Cc: Long Li <longli@microsoft.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: dri-devel@lists.freedesktop.org
Cc: iommu@lists.linux.dev
Cc: linaro-mm-sig@lists.linaro.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-media@vger.kernel.org
Cc: linux-mm@kvack.org
Aneesh Kumar K.V (Arm) (14):
mm: Add an allocator for CoCo shared memory
mm: Zero memory during shared memory transitions
irqchip/gic-v3-its: Resolve the default NUMA node explicitly
irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory
allocator
dma-contiguous: Derive shared alignment from DMA attributes
dma-pool: Allocate CoCo atomic pools using CoCo shared memory
allocator
dma-direct: Align CoCo shared DMA allocations to the shared granule
size
swiotlb: Align shared IO TLB pools to the shared granule size
swiotlb: Reject misaligned restricted DMA pools for CoCo guests
dma-buf: system_heap: Limit scatterlist entries to the buffer size
dma-buf: system_heap: Allocate shared buffers using CoCo shared memory
allocator
swiotlb: Make rounded shared pool capacity allocatable
mm: Assert CoCo shared allocations may sleep
irqchip/gic-v3-its: Preallocate VPE L1 tables
arch/arm/mm/dma-mapping.c | 5 +-
arch/arm64/mm/pageattr.c | 3 +
arch/powerpc/platforms/pseries/svm.c | 2 +
arch/s390/mm/init.c | 3 +
arch/x86/coco/tdx/tdx.c | 3 +
arch/x86/hyperv/hv_init.c | 6 +-
arch/x86/hyperv/ivm.c | 4 +
arch/x86/kernel/kvmclock.c | 6 +-
arch/x86/mm/mem_encrypt_amd.c | 4 +
drivers/dma-buf/heaps/system_heap.c | 128 +++++-----
drivers/hv/connection.c | 41 ++--
drivers/hv/hv.c | 11 +-
drivers/hv/hv_common.c | 2 -
drivers/iommu/dma-iommu.c | 2 +-
drivers/irqchip/irq-gic-v3-its.c | 153 +++++++++---
drivers/irqchip/irq-gic-v3.c | 4 +-
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 3 +
include/linux/cc_shared.h | 39 +++
include/linux/dma-map-ops.h | 9 +-
include/linux/irqchip/arm-gic-v3.h | 3 +-
kernel/dma/contiguous.c | 41 +++-
kernel/dma/direct.c | 73 ++++--
kernel/dma/ops_helpers.c | 2 +-
kernel/dma/pool.c | 25 +-
kernel/dma/swiotlb.c | 80 ++++--
kernel/kexec_file.c | 3 +-
mm/Makefile | 1 +
mm/cc_shared.c | 232 ++++++++++++++++++
28 files changed, 682 insertions(+), 206 deletions(-)
create mode 100644 include/linux/cc_shared.h
create mode 100644 mm/cc_shared.c
base-commit: 704340f1cd0dcef829eb62f5b48ae95a2ce17bdf
--
2.43.0
^ permalink raw reply [flat|nested] 24+ messages in thread
* [RFC PATCH v8 01/14] mm: Add an allocator for CoCo shared memory
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:19 ` sashiko-bot
2026-09-24 10:05 ` [RFC PATCH v8 02/14] mm: Zero memory during shared memory transitions Aneesh Kumar K.V (Arm)
` (12 subsequent siblings)
13 siblings, 1 reply; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
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.
Cc: Andrew Morton <akpm@linux-foundation.org>
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 | 232 ++++++++++++++++++++++++++++++++++++++
3 files changed, 272 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..85e16f4504b8
--- /dev/null
+++ b/mm/cc_shared.c
@@ -0,0 +1,232 @@
+// 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/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);
+}
+
+static 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;
+
+ 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;
+}
+
+/**
+ * alloc_cc_shared_pages_node - allocate memory that can be shared
+ * @nid: NUMA node from which to allocate, or %NUMA_NO_NODE
+ * @gfp: allocation flags
+ * @requested: number of bytes requested; must be nonzero
+ * @mem: storage for the allocated page and the size of the shared range
+ *
+ * Allocate at least @requested bytes and make the allocation shared when
+ * memory encryption is active. A memory-state transition requires a valid
+ * linear-map address, so such allocations never come from high memory
+ *
+ * The shared range may be rounded up to the architecture's transition
+ * granule. On success, @mem->shared_size records the actual size that was
+ * made shared and must be retained unchanged for free_cc_shared_pages().
+ * @mem is not modified on failure.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int alloc_cc_shared_pages_node(int nid, gfp_t gfp,
+ size_t requested,
+ struct cc_shared_pages *mem)
+{
+ struct page *page;
+ unsigned int order;
+
+ if (!mem || !requested)
+ return -EINVAL;
+
+ if (cc_platform_has(CC_ATTR_MEM_ENCRYPT))
+ return __alloc_cc_shared_pages_node(nid, gfp, requested, mem);
+
+ order = get_order(requested);
+ if (order > MAX_PAGE_ORDER)
+ return -EINVAL;
+
+ if (nid == NUMA_NO_NODE)
+ page = alloc_pages(gfp, order);
+ else
+ page = alloc_pages_node(nid, gfp, order);
+ if (!page)
+ return -ENOMEM;
+
+ mem->page = page;
+ mem->shared_size = requested;
+ return 0;
+}
+EXPORT_SYMBOL_GPL(alloc_cc_shared_pages_node);
+
+/**
+ * alloc_cc_shared_pages - allocate memory that can be shared
+ * @gfp: allocation flags
+ * @requested: number of bytes requested; must be nonzero
+ * @mem: storage for the allocated page and the size of the shared range
+ *
+ * Equivalent to alloc_cc_shared_pages_node() with %NUMA_NO_NODE.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+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_platform_has(CC_ATTR_MEM_ENCRYPT))
+ goto free_pages;
+
+ 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:
+ __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
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 02/14] mm: Zero memory during shared memory transitions
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 01/14] mm: Add an allocator for CoCo shared memory Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:22 ` sashiko-bot
2026-09-25 12:08 ` Kiryl Shutsemau
2026-09-24 10:05 ` [RFC PATCH v8 03/14] irqchip/gic-v3-its: Resolve the default NUMA node explicitly Aneesh Kumar K.V (Arm)
` (11 subsequent siblings)
13 siblings, 2 replies; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
Architectures need to zero memory at different points in a private-to-shared
transition. For example, pKVM must clear memory before sharing it, while Arm
CCA can only clear it after the RSI transition has completed.
Make zeroing an implicit property of set_memory_decrypted(). Each architecture
or platform performs the clear at the safe point in its transition. On x86,
perform it in the prepare callback while the private mapping remains
accessible. This avoids adding a flag to set_memory_decrypted() and threading
it through the architecture-specific callbacks.
Keep allocations on platforms without memory encryption on the ordinary page
allocator path so the original GFP constraints, including __GFP_ZERO, remain
intact. Remove post-transition memset() calls that are now redundant and
explicitly clear allocations on paths where no transition occurs.
This intentionally makes every private-to-shared transition destructive;
callers can no longer use set_memory_decrypted() to preserve existing contents.
Assisted-by: Codex:gpt-5
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Christophe Leroy (CS GROUP) <chleroy@kernel.org>
Cc: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
Cc: Shrikanth Hegde <sshegde@linux.ibm.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Gerald Schaefer <gerald.schaefer@linux.ibm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: x86@kernel.org
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Kiryl Shutsemau <kas@kernel.org>
Cc: Rick Edgecombe <rick.p.edgecombe@intel.com>
Cc: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Cc: Wei Liu <wei.liu@kernel.org>
Cc: Dexuan Cui <decui@microsoft.com>
Cc: Long Li <longli@microsoft.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
arch/arm64/mm/pageattr.c | 3 ++
arch/powerpc/platforms/pseries/svm.c | 2 +
arch/s390/mm/init.c | 3 ++
arch/x86/coco/tdx/tdx.c | 3 ++
arch/x86/hyperv/hv_init.c | 6 ++-
arch/x86/hyperv/ivm.c | 4 ++
arch/x86/kernel/kvmclock.c | 6 +--
arch/x86/mm/mem_encrypt_amd.c | 4 ++
drivers/hv/connection.c | 41 +++++++++----------
drivers/hv/hv.c | 11 ++---
drivers/hv/hv_common.c | 2 -
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 3 ++
kernel/dma/direct.c | 22 ++++++----
mm/cc_shared.c | 7 +---
14 files changed, 72 insertions(+), 45 deletions(-)
diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c
index bbe98ac9ad8c..ae1f5de66fad 100644
--- a/arch/arm64/mm/pageattr.c
+++ b/arch/arm64/mm/pageattr.c
@@ -339,6 +339,9 @@ static int realm_set_memory_decrypted(unsigned long addr, int numpages)
{
int ret = __set_memory_enc_dec(addr, numpages, false);
+ if (!ret)
+ memset((void *)addr, 0, (size_t)numpages << PAGE_SHIFT);
+
WARN(ret, "Failed to decrypt memory, %d pages will be leaked",
numpages);
diff --git a/arch/powerpc/platforms/pseries/svm.c b/arch/powerpc/platforms/pseries/svm.c
index 7a403dbd35ee..a344e094fe1c 100644
--- a/arch/powerpc/platforms/pseries/svm.c
+++ b/arch/powerpc/platforms/pseries/svm.c
@@ -9,6 +9,7 @@
#include <linux/mm.h>
#include <linux/memblock.h>
#include <linux/mem_encrypt.h>
+#include <linux/string.h>
#include <linux/cc_platform.h>
#include <asm/machdep.h>
#include <asm/svm.h>
@@ -59,6 +60,7 @@ int set_memory_decrypted(unsigned long addr, int numpages)
if (!PAGE_ALIGNED(addr))
return -EINVAL;
+ memset((void *)addr, 0, (size_t)numpages << PAGE_SHIFT);
uv_share_page(PHYS_PFN(__pa(addr)), numpages);
return 0;
diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c
index be7e009e7b59..f2e28a2710e0 100644
--- a/arch/s390/mm/init.c
+++ b/arch/s390/mm/init.c
@@ -129,6 +129,9 @@ int set_memory_encrypted(unsigned long vaddr, int numpages)
int set_memory_decrypted(unsigned long vaddr, int numpages)
{
int i;
+
+ memset((void *)vaddr, 0, (size_t)numpages << PAGE_SHIFT);
+
/* make specified pages shared (swiotlb, dma_alloca) */
for (i = 0; i < numpages; ++i) {
uv_set_shared(virt_to_phys((void *)vaddr));
diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c
index f904a636d449..1f1f39082391 100644
--- a/arch/x86/coco/tdx/tdx.c
+++ b/arch/x86/coco/tdx/tdx.c
@@ -978,6 +978,9 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc)
static int tdx_enc_status_change_prepare(unsigned long vaddr, int numpages,
bool enc)
{
+ if (!enc)
+ memset((void *)vaddr, 0, (size_t)numpages << PAGE_SHIFT);
+
/*
* Only handle shared->private conversion here.
* See the comment in tdx_early_init().
diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c
index 0b4a1c0b0b16..a42468e81c34 100644
--- a/arch/x86/hyperv/hv_init.c
+++ b/arch/x86/hyperv/hv_init.c
@@ -156,8 +156,10 @@ static int hv_cpu_init(unsigned int cpu)
* page in non-root partition here.
*/
if (*hvp && !ms_hyperv.paravisor_present && hv_isolation_type_snp()) {
- WARN_ON_ONCE(set_memory_decrypted((unsigned long)(*hvp), 1));
- memset(*hvp, 0, PAGE_SIZE);
+ int ret;
+
+ ret = set_memory_decrypted((unsigned long)*hvp, 1);
+ WARN_ON_ONCE(ret);
}
}
diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c
index 2ce4dfe53472..e9c7799a0094 100644
--- a/arch/x86/hyperv/ivm.c
+++ b/arch/x86/hyperv/ivm.c
@@ -755,6 +755,10 @@ static int hv_mark_gpa_visibility(u16 count, const u64 pfn[],
*/
static int hv_vtom_clear_present(unsigned long kbuffer, int pagecount, bool enc)
{
+ if (!enc)
+ memset((void *)kbuffer, 0,
+ (size_t)pagecount << PAGE_SHIFT);
+
return set_memory_np(kbuffer, pagecount);
}
diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c
index cb3d0ca1fa22..3e9b7ad3dea2 100644
--- a/arch/x86/kernel/kvmclock.c
+++ b/arch/x86/kernel/kvmclock.c
@@ -248,7 +248,7 @@ static void __init kvmclock_init_mem(void)
* be mapped decrypted.
*/
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
- r = set_memory_decrypted((unsigned long) hvclock_mem,
+ r = set_memory_decrypted((unsigned long)hvclock_mem,
1UL << order);
if (r) {
__free_pages(p, order);
@@ -256,9 +256,9 @@ static void __init kvmclock_init_mem(void)
pr_warn("kvmclock: set_memory_decrypted() failed. Disabling\n");
return;
}
+ } else {
+ memset(hvclock_mem, 0, PAGE_SIZE << order);
}
-
- memset(hvclock_mem, 0, PAGE_SIZE << order);
}
static int __init kvm_setup_vsyscall_timeinfo(void)
diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c
index 2f8c32173972..47cae102acd2 100644
--- a/arch/x86/mm/mem_encrypt_amd.c
+++ b/arch/x86/mm/mem_encrypt_amd.c
@@ -13,6 +13,7 @@
#include <linux/dma-direct.h>
#include <linux/swiotlb.h>
#include <linux/mem_encrypt.h>
+#include <linux/string.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/bitops.h>
@@ -285,6 +286,9 @@ static void enc_dec_hypercall(unsigned long vaddr, unsigned long size, bool enc)
static int amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool enc)
{
+ if (!enc)
+ memset((void *)vaddr, 0, (size_t)npages << PAGE_SHIFT);
+
/*
* To maintain the security guarantees of SEV-SNP guests, make sure
* to invalidate the memory before encryption attribute is cleared.
diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c
index 1ab3581b096a..3be779093f3c 100644
--- a/drivers/hv/connection.c
+++ b/drivers/hv/connection.c
@@ -13,6 +13,7 @@
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/delay.h>
+#include <linux/cc_platform.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/slab.h>
@@ -263,29 +264,27 @@ int vmbus_connect(void)
goto cleanup;
}
- ret = set_memory_decrypted((unsigned long)
- vmbus_connection.monitor_pages[0], 1);
- ret |= set_memory_decrypted((unsigned long)
- vmbus_connection.monitor_pages[1], 1);
- if (ret) {
- /*
- * If set_memory_decrypted() fails, the encryption state
- * of the memory is unknown. So leak the memory instead
- * of risking returning decrypted memory to the free list.
- * For simplicity, always handle both pages the same.
- */
- vmbus_connection.monitor_pages[0] = NULL;
- vmbus_connection.monitor_pages[1] = NULL;
- goto cleanup;
+ if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
+ ret = set_memory_decrypted((unsigned long)vmbus_connection.monitor_pages[0],
+ 1);
+ ret |= set_memory_decrypted((unsigned long)vmbus_connection.monitor_pages[1],
+ 1);
+ if (ret) {
+ /*
+ * If set_memory_decrypted() fails, the encryption state
+ * of the memory is unknown. So leak the memory instead
+ * of risking returning decrypted memory to the free list.
+ * For simplicity, always handle both pages the same.
+ */
+ vmbus_connection.monitor_pages[0] = NULL;
+ vmbus_connection.monitor_pages[1] = NULL;
+ goto cleanup;
+ }
+ } else {
+ memset(vmbus_connection.monitor_pages[0], 0, HV_HYP_PAGE_SIZE);
+ memset(vmbus_connection.monitor_pages[1], 0, HV_HYP_PAGE_SIZE);
}
- /*
- * Set_memory_decrypted() will change the memory contents if
- * decryption occurs, so zero monitor pages here.
- */
- memset(vmbus_connection.monitor_pages[0], 0x00, HV_HYP_PAGE_SIZE);
- memset(vmbus_connection.monitor_pages[1], 0x00, HV_HYP_PAGE_SIZE);
-
msginfo = kzalloc(sizeof(*msginfo) +
sizeof(struct vmbus_channel_initiate_contact),
GFP_KERNEL);
diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c
index fe50090dcc01..6e836a02910a 100644
--- a/drivers/hv/hv.c
+++ b/drivers/hv/hv.c
@@ -123,12 +123,13 @@ static int hv_alloc_page(void **page, bool decrypt, const char *note)
if (!*page)
return -ENOMEM;
- if (decrypt)
+ if (decrypt) {
ret = set_memory_decrypted((unsigned long)*page, 1);
- if (ret)
- goto failed;
-
- memset(*page, 0, PAGE_SIZE);
+ if (ret)
+ goto failed;
+ } else {
+ memset(*page, 0, PAGE_SIZE);
+ }
return 0;
failed:
diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c
index 31256cb22b39..af6b1531d600 100644
--- a/drivers/hv/hv_common.c
+++ b/drivers/hv/hv_common.c
@@ -505,8 +505,6 @@ int hv_common_cpu_init(unsigned int cpu)
/* It may be unsafe to free 'mem' */
return ret;
}
-
- memset(mem, 0x00, pgcount * HV_HYP_PAGE_SIZE);
}
/*
diff --git a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
index 26fe9c3f22e3..7d922e0a28f9 100644
--- a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
+++ b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
@@ -13,6 +13,7 @@
#include <linux/mem_encrypt.h>
#include <linux/mm.h>
#include <linux/pgtable.h>
+#include <linux/string.h>
#include <asm/hypervisor.h>
@@ -61,6 +62,8 @@ static int pkvm_set_memory_encrypted(unsigned long addr, int numpages)
static int pkvm_set_memory_decrypted(unsigned long addr, int numpages)
{
+ memset((void *)addr, 0, (size_t)numpages << PAGE_SHIFT);
+
return __set_memory_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID,
addr, numpages);
}
diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index da665ca22d5c..356d4e09e1c8 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -211,7 +211,8 @@ void *dma_direct_alloc(struct device *dev, size_t size,
if (force_dma_unencrypted(dev))
attrs |= __DMA_ATTR_ALLOC_CC_SHARED;
- if (attrs & __DMA_ATTR_ALLOC_CC_SHARED) {
+ mark_mem_decrypt = attrs & __DMA_ATTR_ALLOC_CC_SHARED;
+ if (mark_mem_decrypt) {
/*
* Unencrypted/shared DMA requires a linear-mapped buffer
* address to look up the PFN and set architecture-required PFN
@@ -219,7 +220,6 @@ void *dma_direct_alloc(struct device *dev, size_t size,
* allocation.
*/
allow_highmem = false;
- mark_mem_decrypt = true;
}
size = PAGE_ALIGN(size);
@@ -324,7 +324,9 @@ void *dma_direct_alloc(struct device *dev, size_t size,
cpu_addr = page_address(page);
}
- memset(cpu_addr, 0, size);
+ /* Zero after remapping because the page may be in HighMem. */
+ if (!mark_mem_decrypt)
+ memset(cpu_addr, 0, size);
if (set_uncached) {
void *uncached_cpu_addr;
@@ -435,10 +437,13 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
unsigned long attrs = 0;
struct page *page;
void *cpu_addr;
+ bool mark_mem_decrypt;
if (force_dma_unencrypted(dev))
attrs |= __DMA_ATTR_ALLOC_CC_SHARED;
+ mark_mem_decrypt = attrs & __DMA_ATTR_ALLOC_CC_SHARED;
+
if ((attrs & __DMA_ATTR_ALLOC_CC_SHARED) && dma_direct_use_pool(dev, gfp))
return dma_direct_alloc_from_pool(dev, size, dma_handle,
&cpu_addr, gfp, attrs);
@@ -449,6 +454,7 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
return NULL;
cpu_addr = page_address(page);
+ mark_mem_decrypt = false;
goto setup_page;
}
@@ -457,11 +463,13 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
return NULL;
cpu_addr = page_address(page);
- if ((attrs & __DMA_ATTR_ALLOC_CC_SHARED) &&
- dma_set_decrypted(dev, cpu_addr, size))
- goto out_leak_pages;
setup_page:
- memset(cpu_addr, 0, size);
+ if (mark_mem_decrypt) {
+ if (dma_set_decrypted(dev, cpu_addr, size))
+ goto out_leak_pages;
+ } else {
+ memset(cpu_addr, 0, size);
+ }
*dma_handle = phys_to_dma_direct(dev, page_to_phys(page),
attrs & __DMA_ATTR_ALLOC_CC_SHARED);
return page;
diff --git a/mm/cc_shared.c b/mm/cc_shared.c
index 85e16f4504b8..9eb79832a702 100644
--- a/mm/cc_shared.c
+++ b/mm/cc_shared.c
@@ -104,7 +104,6 @@ static int __alloc_cc_shared_pages_node(int nid, gfp_t gfp,
struct cc_shared_layout layout;
struct page *page;
unsigned int order;
- bool zero = gfp & __GFP_ZERO;
int ret;
ret = cc_shared_calc_layout(requested, &layout);
@@ -117,7 +116,8 @@ static int __alloc_cc_shared_pages_node(int nid, gfp_t gfp,
/*
* State transitions require a linear-map address and may modify memory.
- * Allocate from low memory and defer requested zeroing until afterwards.
+ * Allocate from low memory and let the architecture place zeroing at the
+ * appropriate point in the transition.
*/
gfp &= ~(__GFP_HIGHMEM | __GFP_ZERO);
if (nid == NUMA_NO_NODE)
@@ -137,9 +137,6 @@ static int __alloc_cc_shared_pages_node(int nid, gfp_t gfp,
return ret;
}
- if (zero)
- memset(page_address(page), 0, layout.shared_size);
-
mem->page = page;
mem->shared_size = layout.shared_size;
return 0;
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 03/14] irqchip/gic-v3-its: Resolve the default NUMA node explicitly
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 01/14] mm: Add an allocator for CoCo shared memory Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 02/14] mm: Zero memory during shared memory transitions Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:19 ` sashiko-bot
2026-09-24 10:05 ` [RFC PATCH v8 04/14] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
` (10 subsequent siblings)
13 siblings, 1 reply; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
alloc_pages_node() resolves NUMA_NO_NODE to numa_mem_id() internally. In
preparation for switching ITS allocations to the CoCo shared memory
allocator, resolve the default node explicitly in its_alloc_pages().
The CoCo shared memory allocator interprets NUMA_NO_NODE as a request to
apply the current task's memory policy. Passing numa_mem_id() instead
preserves the existing nearest-memory-node placement across the
allocator switch. This patch does not change the current allocation
behavior.
Cc: Marc Zyngier <maz@kernel.org>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Radu Rendec <radu@rendec.net>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
drivers/irqchip/irq-gic-v3-its.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
index e9807af23537..ac5507acb1a0 100644
--- a/drivers/irqchip/irq-gic-v3-its.c
+++ b/drivers/irqchip/irq-gic-v3-its.c
@@ -32,6 +32,7 @@
#include <linux/set_memory.h>
#include <linux/slab.h>
#include <linux/syscore_ops.h>
+#include <linux/topology.h>
#include <linux/irqchip.h>
#include <linux/irqchip/arm-gic-v3.h>
@@ -236,7 +237,7 @@ static struct page *its_alloc_pages_node(int node, gfp_t gfp,
static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)
{
- return its_alloc_pages_node(NUMA_NO_NODE, gfp, order);
+ return its_alloc_pages_node(numa_mem_id(), gfp, order);
}
static void its_free_pages(void *addr, unsigned int order)
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 04/14] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
` (2 preceding siblings ...)
2026-09-24 10:05 ` [RFC PATCH v8 03/14] irqchip/gic-v3-its: Resolve the default NUMA node explicitly Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:19 ` sashiko-bot
2026-09-24 10:05 ` [RFC PATCH v8 05/14] dma-contiguous: Derive shared alignment from DMA attributes Aneesh Kumar K.V (Arm)
` (9 subsequent siblings)
13 siblings, 1 reply; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
ITS command queues and tables are shared with the host in a
confidential-computing guest. The ITS allocator currently assumes that
the guest PAGE_SIZE is sufficient for both allocation alignment and
private/shared state changes.
Route ITS page allocations through alloc_cc_shared_pages_node(). Keep
the tracked table size based on the ITS-requested order. On release,
reconstruct the common allocation metadata from the original ITS order
and the architecture granule size so the same transitioned range and
buddy order are used.
Cc: Marc Zyngier <maz@kernel.org>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Radu Rendec <radu@rendec.net>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
drivers/irqchip/irq-gic-v3-its.c | 40 ++++++++++++--------------------
1 file changed, 15 insertions(+), 25 deletions(-)
diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
index ac5507acb1a0..4814161df02e 100644
--- a/drivers/irqchip/irq-gic-v3-its.c
+++ b/drivers/irqchip/irq-gic-v3-its.c
@@ -8,6 +8,7 @@
#include <linux/acpi_iort.h>
#include <linux/bitfield.h>
#include <linux/bitmap.h>
+#include <linux/cc_shared.h>
#include <linux/cpu.h>
#include <linux/crash_dump.h>
#include <linux/delay.h>
@@ -19,7 +20,6 @@
#include <linux/irqdomain.h>
#include <linux/list.h>
#include <linux/log2.h>
-#include <linux/mem_encrypt.h>
#include <linux/memblock.h>
#include <linux/mm.h>
#include <linux/msi.h>
@@ -29,7 +29,6 @@
#include <linux/of_pci.h>
#include <linux/of_platform.h>
#include <linux/percpu.h>
-#include <linux/set_memory.h>
#include <linux/slab.h>
#include <linux/syscore_ops.h>
#include <linux/topology.h>
@@ -214,25 +213,13 @@ static gfp_t gfp_flags_quirk;
static struct page *its_alloc_pages_node(int node, gfp_t gfp,
unsigned int order)
{
- struct page *page;
- int ret = 0;
-
- page = alloc_pages_node(node, gfp | gfp_flags_quirk, order);
-
- if (!page)
- return NULL;
+ struct cc_shared_pages mem;
- ret = set_memory_decrypted((unsigned long)page_address(page),
- 1 << order);
- /*
- * If set_memory_decrypted() fails then we don't know what state the
- * page is in, so we can't free it. Instead we leak it.
- * set_memory_decrypted() will already have WARNed.
- */
- if (ret)
+ if (alloc_cc_shared_pages_node(node, gfp | gfp_flags_quirk,
+ PAGE_SIZE << order, &mem))
return NULL;
- return page;
+ return mem.page;
}
static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)
@@ -242,13 +229,15 @@ static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)
static void its_free_pages(void *addr, unsigned int order)
{
- /*
- * If the memory cannot be encrypted again then we must leak the pages.
- * set_memory_encrypted() will already have WARNed.
- */
- if (set_memory_encrypted((unsigned long)addr, 1 << order))
+ struct cc_shared_layout layout;
+ struct cc_shared_pages mem;
+
+ if (WARN_ON(cc_shared_calc_layout(PAGE_SIZE << order, &layout)))
return;
- free_pages((unsigned long)addr, order);
+
+ mem.page = virt_to_page(addr);
+ mem.shared_size = layout.shared_size;
+ free_cc_shared_pages(&mem);
}
static struct gen_pool *itt_pool;
@@ -273,7 +262,8 @@ static void *itt_alloc_pool(int node, int size)
if (!page)
break;
- gen_pool_add(itt_pool, (unsigned long)page_address(page), PAGE_SIZE, node);
+ gen_pool_add(itt_pool, (unsigned long)page_address(page),
+ cc_shared_granule_size(), node);
} while (!addr);
return (void *)addr;
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 05/14] dma-contiguous: Derive shared alignment from DMA attributes
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
` (3 preceding siblings ...)
2026-09-24 10:05 ` [RFC PATCH v8 04/14] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 06/14] dma-pool: Allocate CoCo atomic pools using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
` (8 subsequent siblings)
13 siblings, 0 replies; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
Confidential-computing shared DMA allocations require CMA backing memory
to be aligned to the architecture shared-memory granule. Passing explicit
alignment orders through the DMA allocation stack exposes this derived
constraint to callers and requires separate preferred and mandatory values.
The DMA core already records whether an allocation needs shared backing in
__DMA_ATTR_ALLOC_CC_SHARED. Pass allocation attributes to the DMA-contiguous
entry points and derive the mandatory alignment there before calling CMA.
Preserve the existing alignment clamping for ordinary allocations, while
rejecting shared allocations whose granule exceeds CONFIG_CMA_ALIGNMENT so
callers can fall back to the page allocator.
Cc: Russell King <linux@armlinux.org.uk>
Cc: Robin Murphy <robin.murphy@arm.com>
Cc: "Joerg Roedel (AMD)" <joro@8bytes.org>
Cc: Will Deacon <will@kernel.org>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Pratyush Yadav <pratyush@kernel.org>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
arch/arm/mm/dma-mapping.c | 5 +++--
drivers/iommu/dma-iommu.c | 2 +-
include/linux/dma-map-ops.h | 9 ++++----
kernel/dma/contiguous.c | 41 +++++++++++++++++++++++++++----------
kernel/dma/direct.c | 2 +-
kernel/dma/ops_helpers.c | 2 +-
kernel/dma/pool.c | 2 +-
kernel/kexec_file.c | 3 ++-
8 files changed, 44 insertions(+), 22 deletions(-)
diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c
index 7761099dde9e..9714fcd51941 100644
--- a/arch/arm/mm/dma-mapping.c
+++ b/arch/arm/mm/dma-mapping.c
@@ -398,7 +398,8 @@ static void *__alloc_from_contiguous(struct device *dev, size_t size,
struct page *page;
void *ptr = NULL;
- page = dma_alloc_from_contiguous(dev, count, order, gfp & __GFP_NOWARN);
+ page = dma_alloc_from_contiguous(dev, count, order, 0,
+ gfp & __GFP_NOWARN);
if (!page)
return NULL;
@@ -866,7 +867,7 @@ static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
unsigned long order = get_order(size);
struct page *page;
- page = dma_alloc_from_contiguous(dev, count, order,
+ page = dma_alloc_from_contiguous(dev, count, order, 0,
gfp & __GFP_NOWARN);
if (!page)
goto error;
diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index 58c624513cd4..59baf2687612 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -1627,7 +1627,7 @@ static void *iommu_dma_alloc_pages(struct device *dev, size_t size,
struct page *page = NULL;
void *cpu_addr;
- page = dma_alloc_contiguous(dev, alloc_size, gfp);
+ page = dma_alloc_contiguous(dev, alloc_size, gfp, 0);
if (!page)
page = alloc_pages_node(node, gfp, get_order(alloc_size));
if (!page)
diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h
index 8fae2b7deb20..f3490dd3f8ac 100644
--- a/include/linux/dma-map-ops.h
+++ b/include/linux/dma-map-ops.h
@@ -99,10 +99,11 @@ int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
phys_addr_t limit, struct cma **res_cma, bool fixed);
struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
- unsigned int order, bool no_warn);
+ unsigned int order, unsigned long attrs, bool no_warn);
bool dma_release_from_contiguous(struct device *dev, struct page *pages,
int count);
-struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp);
+struct page *dma_alloc_contiguous(struct device *dev, size_t size,
+ gfp_t gfp, unsigned long attrs);
void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size);
@@ -125,7 +126,7 @@ static inline int dma_contiguous_reserve_area(phys_addr_t size,
return -ENOSYS;
}
static inline struct page *dma_alloc_from_contiguous(struct device *dev,
- size_t count, unsigned int order, bool no_warn)
+ size_t count, unsigned int order, unsigned long attrs, bool no_warn)
{
return NULL;
}
@@ -136,7 +137,7 @@ static inline bool dma_release_from_contiguous(struct device *dev,
}
/* Use fallback alloc() and free() when CONFIG_DMA_CMA=n */
static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size,
- gfp_t gfp)
+ gfp_t gfp, unsigned long attrs)
{
return NULL;
}
diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c
index 66093460584e..1a29565c9185 100644
--- a/kernel/dma/contiguous.c
+++ b/kernel/dma/contiguous.c
@@ -39,6 +39,7 @@
#include <asm/page.h>
+#include <linux/cc_shared.h>
#include <linux/memblock.h>
#include <linux/err.h>
#include <linux/sizes.h>
@@ -357,19 +358,29 @@ int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
* dma_alloc_from_contiguous() - allocate pages from contiguous area
* @dev: Pointer to device for which the allocation is performed.
* @count: Requested number of pages.
- * @align: Requested alignment of pages (in PAGE_SIZE order).
+ * @align: Preferred alignment of pages (in PAGE_SIZE order).
+ * @attrs: DMA allocation attributes.
* @no_warn: Avoid printing message about failed allocation.
*
* This function allocates memory buffer for specified device. It uses
* device specific contiguous memory area if available or the default
* global one. Requires architecture specific dev_get_cma_area() helper
* function.
+ *
+ * The preferred alignment is capped at CONFIG_CMA_ALIGNMENT. The internal
+ * shared-allocation attribute requires at least the architecture shared
+ * granule alignment and fails if that exceeds the CMA alignment limit.
*/
struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
- unsigned int align, bool no_warn)
+ unsigned int align, unsigned long attrs, bool no_warn)
{
- if (align > CONFIG_CMA_ALIGNMENT)
- align = CONFIG_CMA_ALIGNMENT;
+ unsigned int required_align = 0;
+
+ if (attrs & __DMA_ATTR_ALLOC_CC_SHARED)
+ required_align = get_order(cc_shared_granule_size());
+ if (required_align > CONFIG_CMA_ALIGNMENT)
+ return NULL;
+ align = min(max(align, required_align), CONFIG_CMA_ALIGNMENT);
return cma_alloc(dev_get_cma_area(dev), count, align, no_warn);
}
@@ -390,10 +401,9 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
return cma_release(dev_get_cma_area(dev), pages, count);
}
-static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp)
+static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp,
+ unsigned int align)
{
- unsigned int align = min(get_order(size), CONFIG_CMA_ALIGNMENT);
-
return cma_alloc(cma, size >> PAGE_SHIFT, align, gfp & __GFP_NOWARN);
}
@@ -402,6 +412,7 @@ static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp)
* @dev: Pointer to device for which the allocation is performed.
* @size: Requested allocation size.
* @gfp: Allocation flags.
+ * @attrs: DMA allocation attributes.
*
* tries to use device specific contiguous memory area if available, or it
* tries to use per-numa cma, if the allocation fails, it will fallback to
@@ -412,8 +423,11 @@ static struct page *cma_alloc_aligned(struct cma *cma, size_t size, gfp_t gfp)
* there is no need to waste CMA pages for that kind; it also helps reduce
* fragmentations.
*/
-struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
+struct page *dma_alloc_contiguous(struct device *dev, size_t size,
+ gfp_t gfp, unsigned long attrs)
{
+ unsigned int required_align = 0;
+ unsigned int align = get_order(size);
#ifdef CONFIG_DMA_NUMA_CMA
int nid = dev_to_node(dev);
#endif
@@ -421,8 +435,13 @@ struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
/* CMA can be used only in the context which permits sleeping */
if (!gfpflags_allow_blocking(gfp))
return NULL;
+ if (attrs & __DMA_ATTR_ALLOC_CC_SHARED)
+ required_align = get_order(cc_shared_granule_size());
+ if (required_align > CONFIG_CMA_ALIGNMENT)
+ return NULL;
+ align = min(max(align, required_align), CONFIG_CMA_ALIGNMENT);
if (dev->cma_area)
- return cma_alloc_aligned(dev->cma_area, size, gfp);
+ return cma_alloc_aligned(dev->cma_area, size, gfp, align);
if (size <= PAGE_SIZE)
return NULL;
@@ -431,7 +450,7 @@ struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
struct cma *cma = dma_contiguous_numa_area[nid];
struct page *page;
if (cma) {
- page = cma_alloc_aligned(cma, size, gfp);
+ page = cma_alloc_aligned(cma, size, gfp, align);
if (page)
return page;
}
@@ -440,7 +459,7 @@ struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp)
if (!dma_contiguous_default_area)
return NULL;
- return cma_alloc_aligned(dma_contiguous_default_area, size, gfp);
+ return cma_alloc_aligned(dma_contiguous_default_area, size, gfp, align);
}
/**
diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index 356d4e09e1c8..cb14419f8a09 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -124,7 +124,7 @@ static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
WARN_ON_ONCE(!PAGE_ALIGNED(size));
gfp |= dma_direct_optimal_gfp_mask(dev, &phys_limit);
- page = dma_alloc_contiguous(dev, size, gfp);
+ page = dma_alloc_contiguous(dev, size, gfp, 0);
if (page) {
if (dma_coherent_ok(dev, page_to_phys(page), size) &&
(allow_highmem || !PageHighMem(page)))
diff --git a/kernel/dma/ops_helpers.c b/kernel/dma/ops_helpers.c
index 6b5f9208d31c..8320cc0fada5 100644
--- a/kernel/dma/ops_helpers.c
+++ b/kernel/dma/ops_helpers.c
@@ -66,7 +66,7 @@ struct page *dma_common_alloc_pages(struct device *dev, size_t size,
struct page *page;
phys_addr_t phys;
- page = dma_alloc_contiguous(dev, size, gfp);
+ page = dma_alloc_contiguous(dev, size, gfp, 0);
if (!page)
page = alloc_pages_node(dev_to_node(dev), gfp, get_order(size));
if (!page)
diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c
index 00f422a1e896..70b7f64b17ab 100644
--- a/kernel/dma/pool.c
+++ b/kernel/dma/pool.c
@@ -99,7 +99,7 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
pool_size = 1 << (PAGE_SHIFT + order);
if (cma_in_zone(gfp))
page = dma_alloc_from_contiguous(NULL, 1 << order,
- order, false);
+ order, 0, false);
if (!page)
page = alloc_pages(gfp | __GFP_NOWARN, order);
} while (!page && order-- > 0);
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 59fb9d71e9d8..2a337ce7264e 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -682,7 +682,8 @@ static int kexec_alloc_contig(struct kexec_buf *kbuf)
if (kbuf->image->type == KEXEC_TYPE_CRASH)
return -EPERM;
- p = dma_alloc_from_contiguous(NULL, nr_pages, get_order(kbuf->buf_align), true);
+ p = dma_alloc_from_contiguous(NULL, nr_pages,
+ get_order(kbuf->buf_align), 0, true);
if (!p)
return -ENOMEM;
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 06/14] dma-pool: Allocate CoCo atomic pools using CoCo shared memory allocator
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
` (4 preceding siblings ...)
2026-09-24 10:05 ` [RFC PATCH v8 05/14] dma-contiguous: Derive shared alignment from DMA attributes Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:19 ` sashiko-bot
2026-09-24 10:05 ` [RFC PATCH v8 07/14] dma-direct: Align CoCo shared DMA allocations to the shared granule size Aneesh Kumar K.V (Arm)
` (7 subsequent siblings)
13 siblings, 1 reply; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
Atomic DMA allocations cannot perform a potentially sleeping
private/shared state transition at allocation time. The atomic DMA pools
avoid this by transitioning their backing allocations in a sleepable
context and suballocating memory that is already shared.
Architectures may require these transitions to use a shared granule size
larger than PAGE_SIZE. The existing fallback loop can reduce the backing
allocation below the order required by that size, producing a range that
cannot be safely transitioned.
For pools marked cc_shared, round the requested pool size up to a multiple
of the shared granule size and prevent allocation fallback below the order
required by that size. Pass __DMA_ATTR_ALLOC_CC_SHARED to
dma_alloc_from_contiguous() so it enforces the mandatory alignment. If CMA
cannot satisfy it, fall back to the buddy allocator. Non-shared pools pass
zero attributes and retain the existing CMA alignment policy.
Use the cc_make_shared() and cc_make_private() helpers to transition the
backing allocation and validate its address and size.
Individual atomic allocations may remain smaller than the shared granule
size because the backing allocation remains owned by the pool and in the
shared state.
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Assisted-by: Codex:gpt-5
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
kernel/dma/pool.c | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c
index 70b7f64b17ab..f059312c15aa 100644
--- a/kernel/dma/pool.c
+++ b/kernel/dma/pool.c
@@ -4,12 +4,12 @@
* Copyright (C) 2020 Google LLC
*/
#include <linux/cma.h>
+#include <linux/cc_shared.h>
#include <linux/debugfs.h>
#include <linux/dma-map-ops.h>
#include <linux/dma-direct.h>
#include <linux/init.h>
#include <linux/genalloc.h>
-#include <linux/set_memory.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/cc_platform.h>
@@ -85,6 +85,9 @@ static bool cma_in_zone(gfp_t gfp)
static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
gfp_t gfp)
{
+ struct cc_shared_layout layout;
+ unsigned long attrs = 0;
+ unsigned int min_order = 0;
unsigned int order;
struct page *page = NULL;
bool leak_pages = false;
@@ -92,6 +95,17 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
int ret = -ENOMEM;
pgprot_t prot __maybe_unused;
+ if (dma_pool->cc_shared) {
+ ret = cc_shared_calc_layout(pool_size, &layout);
+ if (ret)
+ goto out;
+ pool_size = layout.shared_size;
+ min_order = get_order(layout.alignment);
+ if (min_order > MAX_PAGE_ORDER)
+ return -E2BIG;
+ attrs = __DMA_ATTR_ALLOC_CC_SHARED;
+ }
+
/* Cannot allocate larger than MAX_PAGE_ORDER */
order = min(get_order(pool_size), MAX_PAGE_ORDER);
@@ -99,10 +113,10 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
pool_size = 1 << (PAGE_SHIFT + order);
if (cma_in_zone(gfp))
page = dma_alloc_from_contiguous(NULL, 1 << order,
- order, 0, false);
+ order, attrs, false);
if (!page)
page = alloc_pages(gfp | __GFP_NOWARN, order);
- } while (!page && order-- > 0);
+ } while (!page && order-- > min_order);
if (!page)
goto out;
@@ -126,8 +140,7 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
* shrink so no re-encryption occurs in dma_direct_free().
*/
if (dma_pool->cc_shared) {
- ret = set_memory_decrypted((unsigned long)page_to_virt(page),
- 1 << order);
+ ret = cc_make_shared(page_to_virt(page), pool_size);
if (ret) {
leak_pages = true;
goto remove_mapping;
@@ -144,7 +157,7 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
encrypt_mapping:
if (dma_pool->cc_shared &&
- set_memory_encrypted((unsigned long)page_to_virt(page), 1 << order))
+ cc_make_private(page_to_virt(page), pool_size))
leak_pages = true;
remove_mapping:
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 07/14] dma-direct: Align CoCo shared DMA allocations to the shared granule size
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
` (5 preceding siblings ...)
2026-09-24 10:05 ` [RFC PATCH v8 06/14] dma-pool: Allocate CoCo atomic pools using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 08/14] swiotlb: Align shared IO TLB pools " Aneesh Kumar K.V (Arm)
` (6 subsequent siblings)
13 siblings, 0 replies; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
Use the common CoCo shared-memory geometry helpers for allocations backed
directly by CMA or the page allocator. Round the backing allocation to a
whole shared granule and pass the resolved shared-allocation attribute
through the DMA contiguous allocator so it can enforce the mandatory
alignment. Transition the complete range through the common shared/private
helpers.
Recompute the layout before freeing ordinary direct allocations so the
transition back to private memory and dma_free_contiguous() cover exactly
the range acquired by the allocation path. If restoring private state
fails, retain the existing fail-safe behavior and leak the pages rather
than returning potentially shared memory to the allocator.
This also applies the same rules to dma_direct_alloc_pages(), covering
callers which require a struct page result rather than a CPU virtual
address.
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Assisted-by: Codex:gpt-5
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
kernel/dma/direct.c | 51 ++++++++++++++++++++++++++++++++++-----------
1 file changed, 39 insertions(+), 12 deletions(-)
diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index cb14419f8a09..09a8a842fc6f 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -11,10 +11,10 @@
#include <linux/scatterlist.h>
#include <linux/pfn.h>
#include <linux/vmalloc.h>
-#include <linux/set_memory.h>
#include <linux/slab.h>
#include <linux/pci-p2pdma.h>
#include <linux/cc_platform.h>
+#include <linux/cc_shared.h>
#include "direct.h"
@@ -85,7 +85,7 @@ static int dma_set_decrypted(struct device *dev, void *vaddr, size_t size)
{
int ret;
- ret = set_memory_decrypted((unsigned long)vaddr, PFN_UP(size));
+ ret = cc_make_shared(vaddr, size);
if (ret)
pr_warn_ratelimited("leaking DMA memory that can't be decrypted\n");
return ret;
@@ -95,7 +95,7 @@ static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size)
{
int ret;
- ret = set_memory_encrypted((unsigned long)vaddr, PFN_UP(size));
+ ret = cc_make_private(vaddr, size);
if (ret)
pr_warn_ratelimited("leaking DMA memory that can't be re-encrypted\n");
return ret;
@@ -115,7 +115,7 @@ static struct page *dma_direct_alloc_swiotlb(struct device *dev, size_t size,
}
static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
- gfp_t gfp, bool allow_highmem)
+ gfp_t gfp, bool allow_highmem, unsigned long attrs)
{
int node = dev_to_node(dev);
struct page *page;
@@ -124,7 +124,7 @@ static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
WARN_ON_ONCE(!PAGE_ALIGNED(size));
gfp |= dma_direct_optimal_gfp_mask(dev, &phys_limit);
- page = dma_alloc_contiguous(dev, size, gfp, 0);
+ page = dma_alloc_contiguous(dev, size, gfp, attrs);
if (page) {
if (dma_coherent_ok(dev, page_to_phys(page), size) &&
(allow_highmem || !PageHighMem(page)))
@@ -184,7 +184,7 @@ static void *dma_direct_alloc_no_mapping(struct device *dev, size_t size,
{
struct page *page;
- page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true);
+ page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true, 0);
if (!page)
return NULL;
@@ -205,6 +205,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,
bool remap = false, set_uncached = false;
bool mark_mem_decrypt = false;
bool allow_highmem = true;
+ struct cc_shared_layout layout;
struct page *page;
void *cpu_addr;
@@ -285,8 +286,15 @@ void *dma_direct_alloc(struct device *dev, size_t size,
return NULL;
}
+ if (mark_mem_decrypt) {
+ if (cc_shared_calc_layout(size, &layout))
+ return NULL;
+ size = layout.shared_size;
+ }
+
/* we always manually zero the memory once we are done */
- page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, allow_highmem);
+ page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO,
+ allow_highmem, attrs);
if (!page)
return NULL;
@@ -305,7 +313,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,
void *lm_addr;
lm_addr = page_address(page);
- if (set_memory_decrypted((unsigned long)lm_addr, PFN_UP(size)))
+ if (dma_set_decrypted(dev, lm_addr, size))
goto out_leak_pages;
}
@@ -364,6 +372,7 @@ void dma_direct_free(struct device *dev, size_t size,
phys_addr_t phys;
bool mark_mem_encrypted = false;
struct io_tlb_pool *swiotlb_pool;
+ struct cc_shared_layout layout;
unsigned int page_order = get_order(size);
/*
@@ -408,6 +417,12 @@ void dma_direct_free(struct device *dev, size_t size,
/* Swiotlb doesn't need a page attribute update on free */
mark_mem_encrypted = false;
+ if (mark_mem_encrypted) {
+ if (WARN_ON_ONCE(cc_shared_calc_layout(size, &layout)))
+ return;
+ size = layout.shared_size;
+ }
+
if (is_vmalloc_addr(cpu_addr)) {
vunmap(cpu_addr);
} else {
@@ -419,10 +434,8 @@ void dma_direct_free(struct device *dev, size_t size,
void *lm_addr;
lm_addr = phys_to_virt(phys);
- if (set_memory_encrypted((unsigned long)lm_addr, PFN_UP(size))) {
- pr_warn_ratelimited("leaking DMA memory that can't be re-encrypted\n");
+ if (dma_set_encrypted(dev, lm_addr, size))
return;
- }
}
if (swiotlb_pool)
@@ -435,6 +448,7 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
{
unsigned long attrs = 0;
+ struct cc_shared_layout layout;
struct page *page;
void *cpu_addr;
bool mark_mem_decrypt;
@@ -458,7 +472,13 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
goto setup_page;
}
- page = __dma_direct_alloc_pages(dev, size, gfp, false);
+ if (mark_mem_decrypt) {
+ if (cc_shared_calc_layout(size, &layout))
+ return NULL;
+ size = layout.shared_size;
+ }
+
+ page = __dma_direct_alloc_pages(dev, size, gfp, false, attrs);
if (!page)
return NULL;
@@ -484,6 +504,7 @@ void dma_direct_free_pages(struct device *dev, size_t size,
phys_addr_t phys;
void *vaddr = page_address(page);
struct io_tlb_pool *swiotlb_pool;
+ struct cc_shared_layout layout;
/*
* if the device had requested for an unencrypted buffer,
* convert it to encrypted on free
@@ -500,6 +521,12 @@ void dma_direct_free_pages(struct device *dev, size_t size,
if (swiotlb_pool)
mark_mem_encrypted = false;
+ if (mark_mem_encrypted) {
+ if (WARN_ON_ONCE(cc_shared_calc_layout(size, &layout)))
+ return;
+ size = layout.shared_size;
+ }
+
if (mark_mem_encrypted && dma_set_encrypted(dev, vaddr, size))
return;
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 08/14] swiotlb: Align shared IO TLB pools to the shared granule size
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
` (6 preceding siblings ...)
2026-09-24 10:05 ` [RFC PATCH v8 07/14] dma-direct: Align CoCo shared DMA allocations to the shared granule size Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:22 ` sashiko-bot
2026-09-24 10:05 ` [RFC PATCH v8 09/14] swiotlb: Reject misaligned restricted DMA pools for CoCo guests Aneesh Kumar K.V (Arm)
` (5 subsequent siblings)
13 siblings, 1 reply; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
Align and size the early memblock pool to the common CoCo shared
granule. Use the same rounded extent when the pool is transitioned and
when it is released. This deliberately leaves the slot count unchanged:
any rounded tail belongs to the pool allocation but is not advertised as
allocatable SWIOTLB space.
Replace the default and dynamic pool set_memory calls with the helpers
so alignment validation and architecture dispatch are kept in one place.
As before, pages are intentionally leaked if their private state cannot
be restored safely.
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
kernel/dma/swiotlb.c | 40 +++++++++++++++++++++++-----------------
1 file changed, 23 insertions(+), 17 deletions(-)
diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
index ded7016a46a7..394db93b7eb9 100644
--- a/kernel/dma/swiotlb.c
+++ b/kernel/dma/swiotlb.c
@@ -22,6 +22,7 @@
#include <linux/cache.h>
#include <linux/cc_platform.h>
+#include <linux/cc_shared.h>
#include <linux/ctype.h>
#include <linux/debugfs.h>
#include <linux/dma-direct.h>
@@ -38,7 +39,6 @@
#include <linux/pfn.h>
#include <linux/rculist.h>
#include <linux/scatterlist.h>
-#include <linux/set_memory.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/swiotlb.h>
@@ -369,13 +369,13 @@ void __init swiotlb_update_mem_attributes(void)
if (!mem->nslabs || mem->late_alloc)
return;
- bytes = PAGE_ALIGN(mem->nslabs << IO_TLB_SHIFT);
+
+ bytes = ALIGN(mem->nslabs << IO_TLB_SHIFT, cc_shared_granule_size());
if (io_tlb_default_mem.cc_shared) {
int ret;
- ret = set_memory_decrypted((unsigned long)mem->vaddr,
- bytes >> PAGE_SHIFT);
+ ret = cc_make_shared(mem->vaddr, bytes);
if (ret) {
pr_warn("Failed to decrypt default memory pool, disabling it\n");
swiotlb_mark_pool_used(mem);
@@ -436,8 +436,8 @@ static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
unsigned int flags,
int (*remap)(void *tlb, unsigned long nslabs))
{
- size_t bytes = PAGE_ALIGN(nslabs << IO_TLB_SHIFT);
void *tlb;
+ size_t bytes = ALIGN(nslabs << IO_TLB_SHIFT, cc_shared_granule_size());
/*
* By default allocate the bounce buffer memory from low memory, but
@@ -445,9 +445,9 @@ static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
* memory encryption.
*/
if (flags & SWIOTLB_ANY)
- tlb = memblock_alloc(bytes, PAGE_SIZE);
+ tlb = memblock_alloc(bytes, cc_shared_granule_size());
else
- tlb = memblock_alloc_low(bytes, PAGE_SIZE);
+ tlb = memblock_alloc_low(bytes, cc_shared_granule_size());
if (!tlb) {
pr_warn("%s: Failed to allocate %zu bytes tlb structure\n",
@@ -456,7 +456,7 @@ static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
}
if (remap && remap(tlb, nslabs) < 0) {
- memblock_free(tlb, PAGE_ALIGN(bytes));
+ memblock_free(tlb, bytes);
pr_warn("%s: Failed to remap %zu bytes\n", __func__, bytes);
return NULL;
}
@@ -578,7 +578,7 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,
swiotlb_adjust_nareas(num_possible_cpus());
retry:
- order = get_order(nslabs << IO_TLB_SHIFT);
+ order = get_order(ALIGN(nslabs << IO_TLB_SHIFT, cc_shared_granule_size()));
nslabs = SLABS_PER_PAGE << order;
while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
@@ -587,6 +587,8 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,
if (vstart)
break;
order--;
+ if (order < get_order(cc_shared_granule_size()))
+ break;
nslabs = SLABS_PER_PAGE << order;
retried = true;
}
@@ -626,8 +628,7 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,
goto error_slots;
if (io_tlb_default_mem.cc_shared) {
- rc = set_memory_decrypted((unsigned long)vstart,
- (nslabs << IO_TLB_SHIFT) >> PAGE_SHIFT);
+ rc = cc_make_shared(vstart, nslabs << IO_TLB_SHIFT);
if (rc) {
leak_pages = true;
goto error_decrypt;
@@ -667,11 +668,11 @@ void __init swiotlb_exit(void)
pr_info("tearing down default memory pool\n");
tbl_vaddr = (unsigned long)phys_to_virt(mem->start);
- tbl_size = PAGE_ALIGN(mem->end - mem->start);
+ tbl_size = ALIGN(mem->end - mem->start, cc_shared_granule_size());
slots_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), mem->nslabs));
if (io_tlb_default_mem.cc_shared) {
- if (set_memory_encrypted(tbl_vaddr, tbl_size >> PAGE_SHIFT))
+ if (cc_make_private((void *)tbl_vaddr, tbl_size))
leak_pages = true;
}
@@ -711,12 +712,15 @@ void __init swiotlb_exit(void)
static struct page *alloc_dma_pages(gfp_t gfp, size_t bytes,
u64 phys_limit, unsigned long attrs)
{
- unsigned int order = get_order(bytes);
bool cc_shared = attrs & __DMA_ATTR_ALLOC_CC_SHARED;
+ unsigned int order;
struct page *page;
phys_addr_t paddr;
void *vaddr;
+ if (cc_shared)
+ bytes = ALIGN(bytes, cc_shared_granule_size());
+ order = get_order(bytes);
page = alloc_pages(gfp, order);
if (!page)
return NULL;
@@ -728,13 +732,13 @@ static struct page *alloc_dma_pages(gfp_t gfp, size_t bytes,
}
vaddr = phys_to_virt(paddr);
- if (cc_shared && set_memory_decrypted((unsigned long)vaddr, PFN_UP(bytes)))
+ if (cc_shared && cc_make_shared(vaddr, bytes))
goto error;
return page;
error:
/* Intentional leak if pages cannot be encrypted again. */
- if (cc_shared && !set_memory_encrypted((unsigned long)vaddr, PFN_UP(bytes)))
+ if (cc_shared && !cc_make_private(vaddr, bytes))
__free_pages(page, order);
return NULL;
}
@@ -807,9 +811,11 @@ static void swiotlb_free_tlb(void *vaddr, size_t bytes, bool cc_shared)
dma_free_from_pool(NULL, vaddr, bytes))
return;
+ if (cc_shared)
+ bytes = ALIGN(bytes, cc_shared_granule_size());
/* Intentional leak if pages cannot be encrypted again. */
if (!cc_shared ||
- !set_memory_encrypted((unsigned long)vaddr, PFN_UP(bytes)))
+ !cc_make_private(vaddr, bytes))
__free_pages(virt_to_page(vaddr), get_order(bytes));
}
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 09/14] swiotlb: Reject misaligned restricted DMA pools for CoCo guests
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
` (7 preceding siblings ...)
2026-09-24 10:05 ` [RFC PATCH v8 08/14] swiotlb: Align shared IO TLB pools " Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 10/14] dma-buf: system_heap: Limit scatterlist entries to the buffer size Aneesh Kumar K.V (Arm)
` (4 subsequent siblings)
13 siblings, 0 replies; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
A restricted DMA pool is a firmware-described ownership boundary. Unlike
a pool allocated by SWIOTLB itself, its base cannot be moved and its
size cannot be rounded without claiming memory outside the reserved
region.
Confidential-computing guests may require private/shared state changes
to cover units larger than PAGE_SIZE. Passing a misaligned restricted
region to set_memory_decrypted() can therefore fail.
Validate both the physical base and the complete reserved size with the
common CoCo shared-range helper before allocating SWIOTLB metadata or
performing a state transition. Perform the transition through
cc_make_shared().
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
kernel/dma/swiotlb.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
index 394db93b7eb9..5f65b11260e0 100644
--- a/kernel/dma/swiotlb.c
+++ b/kernel/dma/swiotlb.c
@@ -2008,6 +2008,14 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
return -EINVAL;
}
+ if (cc_platform_has(CC_ATTR_MEM_ENCRYPT) &&
+ !cc_shared_range_valid(rmem->base, rmem->size)) {
+ dev_err(dev,
+ "Restricted DMA pool must be aligned to %#zx bytes for memory encryption\n",
+ cc_shared_granule_size());
+ return -EINVAL;
+ }
+
/*
* Since multiple devices can share the same pool, the private data,
* io_tlb_mem struct, will be initialized by the first device attached
@@ -2041,8 +2049,8 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
int ret;
mem->cc_shared = true;
- ret = set_memory_decrypted((unsigned long)phys_to_virt(rmem->base),
- rmem->size >> PAGE_SHIFT);
+ ret = cc_make_shared(phys_to_virt(rmem->base),
+ rmem->size);
if (ret) {
dev_err(dev, "Failed to decrypt restricted DMA pool\n");
kfree(pool->areas);
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 10/14] dma-buf: system_heap: Limit scatterlist entries to the buffer size
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
` (8 preceding siblings ...)
2026-09-24 10:05 ` [RFC PATCH v8 09/14] swiotlb: Reject misaligned restricted DMA pools for CoCo guests Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 11/14] dma-buf: system_heap: Allocate shared buffers using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
` (3 subsequent siblings)
13 siblings, 0 replies; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
The system heap currently allocates each backing page no larger than the
remaining dma-buf length. It can therefore use the complete
compound-page size for every scatterlist entry while keeping the total
length equal to the buffer size.
Shared backing allocations may need to be rounded up to an architecture
shared granule size. A backing allocation can then be larger than the
remaining buffer length. Describing the complete allocation in the
scatterlist would incorrectly expose the rounded tail to scatterlist
consumers as part of the dma-buf.
Track the remaining buffer length while constructing the scatterlist and
limit each entry to the smaller of the compound-page size and the
remaining length. The complete backing allocation remains owned by the
heap and is still released normally.
This does not change behavior with the current allocation policy, but
prepares the heap for shared-granule-sized backing allocations.
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Benjamin Gaignard <benjamin.gaignard@collabora.com>
Cc: Brian Starkey <Brian.Starkey@arm.com>
Cc: John Stultz <jstultz@google.com>
Cc: "T.J. Mercier" <tjmercier@google.com>
Cc: "Christian König" <christian.koenig@amd.com>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
drivers/dma-buf/heaps/system_heap.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c
index c8959eadc71d..b5b8cdf65f23 100644
--- a/drivers/dma-buf/heaps/system_heap.c
+++ b/drivers/dma-buf/heaps/system_heap.c
@@ -406,6 +406,7 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
struct system_heap_buffer *buffer;
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
unsigned long size_remaining = len;
+ unsigned long sg_remaining = len;
unsigned int max_order = orders[0];
struct system_heap_priv *priv = dma_heap_get_drvdata(heap);
bool cc_shared = priv->cc_shared;
@@ -454,7 +455,11 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
sg = table->sgl;
list_for_each_entry_safe(page, tmp_page, &pages, lru) {
- sg_set_page(sg, page, page_size(page), 0);
+ unsigned long sg_len;
+
+ sg_len = min_t(unsigned long, page_size(page), sg_remaining);
+ sg_set_page(sg, page, sg_len, 0);
+ sg_remaining -= sg_len;
sg = sg_next(sg);
list_del(&page->lru);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 11/14] dma-buf: system_heap: Allocate shared buffers using CoCo shared memory allocator
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
` (9 preceding siblings ...)
2026-09-24 10:05 ` [RFC PATCH v8 10/14] dma-buf: system_heap: Limit scatterlist entries to the buffer size Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 12/14] swiotlb: Make rounded shared pool capacity allocatable Aneesh Kumar K.V (Arm)
` (2 subsequent siblings)
13 siblings, 0 replies; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
The system_cc_shared heap currently allocates its backing pages using the
normal system-heap order policy and changes each resulting compound page to
shared state. That is unsafe when an architecture requires state changes in
units larger than PAGE_SIZE: an order-0 tail is neither sufficiently aligned
nor large enough to transition independently.
Use the common CoCo shared-page allocator for every backing allocation of the
shared heap. Preserve the existing preferred-order search and its GFP policy:
each candidate order is passed to the common allocator as a byte request, and
that allocator rounds it up when the architecture shared granule is larger.
Add __GFP_COMP for shared allocations because an order-0 candidate can
be rounded into a high-order allocation. The system heap uses
compound_order() and page_size() for accounting and release, so the
returned allocation must retain compound-page semantics.
Calculate a rounded internal backing length but retain the original
length in dma_buf::size. The preceding scatterlist-length change ensures
that the rounded tail is not included in DMA mappings or other
operations. This permits a 4 KiB request on a 64 KiB shared-granule
system without exposing the extra 60 KiB to an importer.
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Benjamin Gaignard <benjamin.gaignard@collabora.com>
Cc: Brian Starkey <Brian.Starkey@arm.com>
Cc: John Stultz <jstultz@google.com>
Cc: "T.J. Mercier" <tjmercier@google.com>
Cc: "Christian König" <christian.koenig@amd.com>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
drivers/dma-buf/heaps/system_heap.c | 121 ++++++++++++----------------
1 file changed, 53 insertions(+), 68 deletions(-)
diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c
index b5b8cdf65f23..5970b76416ae 100644
--- a/drivers/dma-buf/heaps/system_heap.c
+++ b/drivers/dma-buf/heaps/system_heap.c
@@ -11,14 +11,13 @@
*/
#include <linux/cc_platform.h>
+#include <linux/cc_shared.h>
#include <linux/dma-buf.h>
#include <linux/dma-mapping.h>
#include <linux/dma-heap.h>
#include <linux/err.h>
#include <linux/highmem.h>
-#include <linux/mem_encrypt.h>
#include <linux/mm.h>
-#include <linux/set_memory.h>
#include <linux/module.h>
#include <linux/pgtable.h>
#include <linux/scatterlist.h>
@@ -65,34 +64,6 @@ static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP};
static const unsigned int orders[] = {8, 4, 0};
#define NUM_ORDERS ARRAY_SIZE(orders)
-static int system_heap_set_page_decrypted(struct page *page)
-{
- unsigned long addr = (unsigned long)page_address(page);
- unsigned int nr_pages = 1 << compound_order(page);
- int ret;
-
- ret = set_memory_decrypted(addr, nr_pages);
- if (ret)
- pr_warn_ratelimited("dma-buf system heap: failed to decrypt page at %p\n",
- page_address(page));
-
- return ret;
-}
-
-static int system_heap_set_page_encrypted(struct page *page)
-{
- unsigned long addr = (unsigned long)page_address(page);
- unsigned int nr_pages = 1 << compound_order(page);
- int ret;
-
- ret = set_memory_encrypted(addr, nr_pages);
- if (ret)
- pr_warn_ratelimited("dma-buf system heap: failed to re-encrypt page at %p, leaking memory\n",
- page_address(page));
-
- return ret;
-}
-
static int dup_sg_table(struct sg_table *from, struct sg_table *to)
{
struct scatterlist *sg, *new_sg;
@@ -337,6 +308,20 @@ static void system_heap_vunmap(struct dma_buf *dmabuf, struct iosys_map *map)
iosys_map_clear(map);
}
+static void system_heap_free_page(struct page *page, bool cc_shared)
+{
+ struct cc_shared_pages mem;
+
+ if (!cc_shared) {
+ __free_pages(page, compound_order(page));
+ return;
+ }
+
+ mem.page = page;
+ mem.shared_size = page_size(page);
+ free_cc_shared_pages(&mem);
+}
+
static void system_heap_dma_buf_release(struct dma_buf *dmabuf)
{
struct system_heap_buffer *buffer = dmabuf->priv;
@@ -345,19 +330,8 @@ static void system_heap_dma_buf_release(struct dma_buf *dmabuf)
int i;
table = &buffer->sg_table;
- for_each_sgtable_sg(table, sg, i) {
- struct page *page = sg_page(sg);
-
- /*
- * Intentionally leak pages that cannot be re-encrypted
- * to prevent shared memory from being reused.
- */
- if (cc_shared_buffer(buffer) &&
- system_heap_set_page_encrypted(page))
- continue;
-
- __free_pages(page, compound_order(page));
- }
+ for_each_sgtable_sg(table, sg, i)
+ system_heap_free_page(sg_page(sg), cc_shared_buffer(buffer));
sg_free_table(table);
kfree(buffer);
}
@@ -375,22 +349,39 @@ static const struct dma_buf_ops system_heap_buf_ops = {
.release = system_heap_dma_buf_release,
};
+static struct page *system_heap_alloc_order(unsigned int order,
+ gfp_t flags, bool cc_shared)
+{
+ struct cc_shared_pages mem;
+
+ if (!cc_shared)
+ return alloc_pages(flags, order);
+
+ /* The shared granule can raise the actual allocation order. */
+ flags |= __GFP_COMP;
+ if (alloc_cc_shared_pages(flags, PAGE_SIZE << order, &mem))
+ return NULL;
+
+ return mem.page;
+}
+
static struct page *alloc_largest_available(unsigned long size,
- unsigned int max_order)
+ unsigned int max_order, bool cc_shared)
{
struct page *page;
- int i;
gfp_t flags;
+ int i;
for (i = 0; i < NUM_ORDERS; i++) {
if (size < (PAGE_SIZE << orders[i]))
continue;
if (max_order < orders[i])
continue;
+
flags = order_flags[i];
if (mem_accounting)
flags |= __GFP_ACCOUNT;
- page = alloc_pages(flags, orders[i]);
+ page = system_heap_alloc_order(orders[i], flags, cc_shared);
if (!page)
continue;
return page;
@@ -405,6 +396,7 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
{
struct system_heap_buffer *buffer;
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
+ struct cc_shared_layout layout;
unsigned long size_remaining = len;
unsigned long sg_remaining = len;
unsigned int max_order = orders[0];
@@ -417,6 +409,16 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
struct page *page, *tmp_page;
int i, ret = -ENOMEM;
+ if (cc_shared) {
+ int err;
+
+ err = cc_shared_calc_layout(len, &layout);
+ if (err)
+ return ERR_PTR(err);
+
+ size_remaining = layout.shared_size;
+ }
+
buffer = kzalloc_obj(*buffer);
if (!buffer)
return ERR_PTR(-ENOMEM);
@@ -439,7 +441,8 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
goto free_buffer;
}
- page = alloc_largest_available(size_remaining, max_order);
+ page = alloc_largest_available(size_remaining, max_order,
+ cc_shared);
if (!page)
goto free_buffer;
@@ -464,14 +467,6 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
list_del(&page->lru);
}
- if (cc_shared_buffer(buffer)) {
- for_each_sgtable_sg(table, sg, i) {
- ret = system_heap_set_page_decrypted(sg_page(sg));
- if (ret)
- goto free_pages;
- }
- }
-
/* create the dmabuf */
exp_info.exp_name = dma_heap_get_name(heap);
exp_info.ops = &system_heap_buf_ops;
@@ -486,22 +481,12 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
return dmabuf;
free_pages:
- for_each_sgtable_sg(table, sg, i) {
- struct page *p = sg_page(sg);
-
- /*
- * Intentionally leak pages that cannot be re-encrypted
- * to prevent shared memory from being reused.
- */
- if (cc_shared_buffer(buffer) &&
- system_heap_set_page_encrypted(p))
- continue;
- __free_pages(p, compound_order(p));
- }
+ for_each_sgtable_sg(table, sg, i)
+ system_heap_free_page(sg_page(sg), cc_shared);
sg_free_table(table);
free_buffer:
list_for_each_entry_safe(page, tmp_page, &pages, lru)
- __free_pages(page, compound_order(page));
+ system_heap_free_page(page, cc_shared);
kfree(buffer);
return ERR_PTR(ret);
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 12/14] swiotlb: Make rounded shared pool capacity allocatable
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
` (10 preceding siblings ...)
2026-09-24 10:05 ` [RFC PATCH v8 11/14] dma-buf: system_heap: Allocate shared buffers using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 13/14] mm: Assert CoCo shared allocations may sleep Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 14/14] irqchip/gic-v3-its: Preallocate VPE L1 tables Aneesh Kumar K.V (Arm)
13 siblings, 0 replies; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
CoCo shared memory may need to be allocated and transitioned in units larger
than the requested object. Before this change, users handled the resulting
capacity as follows:
User Rounded capacity reused
dma-buf system heap no
DMA-direct no
regular GIC tables no
small GIC ITTs yes, through a gen_pool
early SWIOTLB pool no
late SWIOTLB pool yes
persistent dynamic SWIOTLB no
transient dynamic SWIOTLB no, one mapping only
atomic DMA pools yes, through a gen_pool
restricted SWIOTLB pool no additional padding
Improve the early and persistent dynamic SWIOTLB pools. They already own
and transition backing rounded to the shared granule size, and SWIOTLB
is itself a suballocator. Advertise the rounded extent as slots, size
the slot metadata to match. This makes the extra capacity available
without reserving more backing memory.
Keep transient dynamic pools unchanged. A transient pool belongs to one
DMA mapping and is destroyed when that mapping is unmapped, so its spare
backing cannot satisfy a later request without changing the lifetime
model.
Do not attempt the same optimization for dma-buf, DMA-direct or regular
GIC objects. Those allocations have independent caller-visible sizes and
lifetimes. Reusing their padding requires a shared-granule suballocator
with reference counting, per-object mappings and accounting.
Note:
For the current 64 KiB CCA shared granule size, SWIOTLB pool sizes are
already multiples of the 256 KiB IO_TLB segment size. Consequently, the
rounding does not change any runtime values on current CCA systems. It
instead makes the code express the intended invariant that pool metadata
describes the complete shared-granule-aligned backing allocation.
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
kernel/dma/swiotlb.c | 30 ++++++++++++++++++++++++------
1 file changed, 24 insertions(+), 6 deletions(-)
diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
index 5f65b11260e0..8d71f2e0fd85 100644
--- a/kernel/dma/swiotlb.c
+++ b/kernel/dma/swiotlb.c
@@ -330,6 +330,14 @@ static inline unsigned long nr_slots(u64 val)
return DIV_ROUND_UP(val, IO_TLB_SIZE);
}
+static unsigned long swiotlb_align_nslabs(unsigned long nslabs)
+{
+ unsigned long granule_nslabs;
+
+ granule_nslabs = cc_shared_granule_size() >> IO_TLB_SHIFT;
+ return ALIGN(nslabs, granule_nslabs);
+}
+
static void swiotlb_mark_pool_used(struct io_tlb_pool *pool)
{
unsigned long i;
@@ -433,11 +441,12 @@ static void add_mem_pool(struct io_tlb_mem *mem, struct io_tlb_pool *pool)
}
static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
- unsigned int flags,
+ unsigned long *alloc_nslabs, unsigned int flags,
int (*remap)(void *tlb, unsigned long nslabs))
{
+ unsigned long aligned_nslabs = swiotlb_align_nslabs(nslabs);
+ size_t bytes = aligned_nslabs << IO_TLB_SHIFT;
void *tlb;
- size_t bytes = ALIGN(nslabs << IO_TLB_SHIFT, cc_shared_granule_size());
/*
* By default allocate the bounce buffer memory from low memory, but
@@ -455,12 +464,13 @@ static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
return NULL;
}
- if (remap && remap(tlb, nslabs) < 0) {
+ if (remap && remap(tlb, aligned_nslabs) < 0) {
memblock_free(tlb, bytes);
pr_warn("%s: Failed to remap %zu bytes\n", __func__, bytes);
return NULL;
}
+ *alloc_nslabs = aligned_nslabs;
return tlb;
}
@@ -473,6 +483,7 @@ void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags,
{
struct io_tlb_pool *mem = &io_tlb_default_mem.defpool;
unsigned long nslabs;
+ unsigned long alloc_nslabs;
unsigned int nareas;
size_t alloc_size;
void *tlb;
@@ -497,13 +508,14 @@ void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags,
swiotlb_adjust_nareas(num_possible_cpus());
nslabs = default_nslabs;
- nareas = limit_nareas(default_nareas, nslabs);
- while ((tlb = swiotlb_memblock_alloc(nslabs, flags, remap)) == NULL) {
+ while ((tlb = swiotlb_memblock_alloc(nslabs, &alloc_nslabs, flags,
+ remap)) == NULL) {
if (nslabs <= IO_TLB_MIN_SLABS)
return;
nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE);
- nareas = limit_nareas(nareas, nslabs);
}
+ nslabs = alloc_nslabs;
+ nareas = limit_nareas(default_nareas, nslabs);
if (default_nslabs != nslabs) {
pr_info("SWIOTLB bounce buffer size adjusted %lu -> %lu slabs",
@@ -869,6 +881,12 @@ static struct io_tlb_pool *swiotlb_alloc_pool(struct device *dev,
tlb_size = nslabs << IO_TLB_SHIFT;
}
+ /* Transient pools are tied to one mapping and cannot reuse padding. */
+ if (mem->cc_shared && !dev) {
+ nslabs = swiotlb_align_nslabs(nslabs);
+ tlb_size = nslabs << IO_TLB_SHIFT;
+ }
+
slot_order = get_order(array_size(sizeof(*pool->slots), nslabs));
pool->slots = (struct io_tlb_slot *)
__get_free_pages(gfp, slot_order);
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 13/14] mm: Assert CoCo shared allocations may sleep
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
` (11 preceding siblings ...)
2026-09-24 10:05 ` [RFC PATCH v8 12/14] swiotlb: Make rounded shared pool capacity allocatable Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 14/14] irqchip/gic-v3-its: Preallocate VPE L1 tables Aneesh Kumar K.V (Arm)
13 siblings, 0 replies; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
On platforms with memory encryption, alloc_cc_shared_pages_node()
transitions the allocated memory to the shared state.
set_memory_decrypted() may sleep, so the transition must run from a
sleepable context.
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
mm/cc_shared.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/cc_shared.c b/mm/cc_shared.c
index 9eb79832a702..6f68a3f44ef8 100644
--- a/mm/cc_shared.c
+++ b/mm/cc_shared.c
@@ -8,6 +8,7 @@
#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>
@@ -106,6 +107,8 @@ static int __alloc_cc_shared_pages_node(int nid, gfp_t gfp,
unsigned int order;
int ret;
+ might_sleep();
+
ret = cc_shared_calc_layout(requested, &layout);
if (ret)
return ret;
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [RFC PATCH v8 14/14] irqchip/gic-v3-its: Preallocate VPE L1 tables
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
` (12 preceding siblings ...)
2026-09-24 10:05 ` [RFC PATCH v8 13/14] mm: Assert CoCo shared allocations may sleep Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:05 ` Aneesh Kumar K.V (Arm)
13 siblings, 0 replies; 24+ messages in thread
From: Aneesh Kumar K.V (Arm) @ 2026-09-24 10:05 UTC (permalink / raw)
To: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu
Cc: Aneesh Kumar K.V (Arm), Andrew Morton, Baoquan He, Mike Rapoport,
Pasha Tatashin, Pratyush Yadav, Catalin Marinas,
Christian König, Jason Gunthorpe, Joerg Roedel, Marc Zyngier,
Marek Szyprowski, Robin Murphy, Steven Price, Sumit Semwal,
Suzuki K Poulose, Thomas Gleixner, Will Deacon, Russell King,
Benjamin Gaignard, Brian Starkey, John Stultz, Mark Rutland,
Radu Rendec, T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Kiryl Shutsemau, Rick Edgecombe,
K . Y . Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li,
Paolo Bonzini, Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra,
dri-devel, linaro-mm-sig, linux-media, linux-mm
VPE L1 tables are allocated from the CPU-starting callback, where sleeping is
not allowed. Remove the page allocation from this atomic path.
Use a blocking allocation for the boot CPU and record the resulting table
order. Allocate a page of that order from the sleepable CPU hotplug prepare
stage for each secondary CPU. Consume the preallocated page only when the CPU
cannot inherit an existing redistributor or ITS table.
Reclaim unused preallocations after the CPU reaches the online state, and from
the prepare-state teardown when CPU bring-up aborts.
Assisted-by: Codex:gpt-5
Cc: Marc Zyngier <maz@kernel.org>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Radu Rendec <radu@rendec.net>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
drivers/irqchip/irq-gic-v3-its.c | 110 +++++++++++++++++++++++++++--
drivers/irqchip/irq-gic-v3.c | 4 +-
include/linux/irqchip/arm-gic-v3.h | 3 +-
3 files changed, 108 insertions(+), 9 deletions(-)
diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
index 4814161df02e..a8f37fb97845 100644
--- a/drivers/irqchip/irq-gic-v3-its.c
+++ b/drivers/irqchip/irq-gic-v3-its.c
@@ -195,6 +195,7 @@ static DEFINE_PER_CPU(struct cpu_lpi_count, cpu_lpi_count);
static LIST_HEAD(its_nodes);
static DEFINE_RAW_SPINLOCK(its_lock);
static struct rdists *gic_rdists;
+static unsigned int vpe_l1_prealloc_order = UINT_MAX;
static struct irq_domain *its_parent;
static unsigned long its_list_map;
@@ -2884,12 +2885,35 @@ static bool allocate_vpe_l2_table(int cpu, u32 id)
return true;
}
-static int allocate_vpe_l1_table(void)
+static unsigned int vpe_l1_table_order(u64 val)
+{
+ unsigned int psz;
+ u64 npg;
+
+ switch (FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val)) {
+ default:
+ case GIC_PAGE_SIZE_4K:
+ psz = SZ_4K;
+ break;
+ case GIC_PAGE_SIZE_16K:
+ psz = SZ_16K;
+ break;
+ case GIC_PAGE_SIZE_64K:
+ psz = SZ_64K;
+ break;
+ }
+
+ npg = FIELD_GET(GICR_VPROPBASER_4_1_SIZE, val) + 1;
+ return get_order(npg * psz);
+}
+
+static int allocate_vpe_l1_table(bool use_prealloc)
{
void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
u64 val, gpsz, npg, pa;
unsigned int psz = SZ_64K;
unsigned int np, epp, esz;
+ unsigned int order;
struct page *page;
if (!gic_rdists->has_rvpeid)
@@ -2982,7 +3006,16 @@ static int allocate_vpe_l1_table(void)
pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n",
np, npg, psz, epp, esz);
- page = its_alloc_pages(GFP_ATOMIC | __GFP_ZERO, get_order(np * PAGE_SIZE));
+ order = get_order(np * PAGE_SIZE);
+ if (use_prealloc) {
+ if (WARN_ON_ONCE(order != vpe_l1_prealloc_order))
+ return -EINVAL;
+
+ page = gic_data_rdist()->vpe_l1_prealloc;
+ gic_data_rdist()->vpe_l1_prealloc = NULL;
+ } else {
+ page = its_alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
+ }
if (!page)
return -ENOMEM;
@@ -2999,6 +3032,9 @@ static int allocate_vpe_l1_table(void)
val |= GICR_VPROPBASER_4_1_VALID;
out:
+ if (!use_prealloc)
+ vpe_l1_prealloc_order = vpe_l1_table_order(val);
+
gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
cpumask_set_cpu(smp_processor_id(), gic_data_rdist()->vpe_table_mask);
@@ -3138,7 +3174,7 @@ static u64 its_clear_vpend_valid(void __iomem *vlpi_base, u64 clr, u64 set)
return val;
}
-static void its_cpu_init_lpis(void)
+static void its_cpu_init_lpis(bool use_prealloc)
{
void __iomem *rbase = gic_data_rdist_rd_base();
struct page *pend_page;
@@ -3251,7 +3287,7 @@ static void its_cpu_init_lpis(void)
val = its_clear_vpend_valid(vlpi_base, 0, 0);
}
- if (allocate_vpe_l1_table()) {
+ if (allocate_vpe_l1_table(use_prealloc)) {
/*
* If the allocation has failed, we're in massive trouble.
* Disable direct injection, and pray that no VM was
@@ -5422,7 +5458,7 @@ static int redist_disable_lpis(void)
return 0;
}
-int its_cpu_init(void)
+int its_cpu_init(bool use_prealloc)
{
if (!list_empty(&its_nodes)) {
int ret;
@@ -5431,13 +5467,66 @@ int its_cpu_init(void)
if (ret)
return ret;
- its_cpu_init_lpis();
+ its_cpu_init_lpis(use_prealloc);
its_cpu_init_collections();
}
return 0;
}
+static int its_vpe_l1_prepare(unsigned int cpu)
+{
+ struct page **prealloc;
+
+ if (!gic_rdists->has_rvpeid ||
+ vpe_l1_prealloc_order == UINT_MAX ||
+ (gic_data_rdist_cpu(cpu)->flags & RD_LOCAL_LPI_ENABLED))
+ return 0;
+
+ prealloc = &gic_data_rdist_cpu(cpu)->vpe_l1_prealloc;
+ if (*prealloc)
+ return 0;
+
+ *prealloc = its_alloc_pages_node(cpu_to_node(cpu),
+ GFP_KERNEL | __GFP_ZERO,
+ vpe_l1_prealloc_order);
+ return *prealloc ? 0 : -ENOMEM;
+}
+
+static int its_vpe_l1_cleanup(unsigned int cpu)
+{
+ struct page *page;
+
+ page = xchg(&gic_data_rdist_cpu(cpu)->vpe_l1_prealloc, NULL);
+ if (page)
+ its_free_pages(page_address(page), vpe_l1_prealloc_order);
+
+ return 0;
+}
+
+static int __init its_vpe_l1_cpuhp_init(void)
+{
+ int prepare_state, state;
+
+ state = cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN,
+ "irqchip/arm/gicv3-vpe:prepare",
+ its_vpe_l1_prepare,
+ its_vpe_l1_cleanup);
+ if (state < 0)
+ return state;
+ prepare_state = state;
+
+ state = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
+ "irqchip/arm/gicv3-vpe:online",
+ its_vpe_l1_cleanup, NULL);
+ if (state < 0) {
+ cpuhp_remove_state_nocalls(prepare_state);
+ return state;
+ }
+
+ return 0;
+}
+
static void rdist_memreserve_cpuhp_cleanup_workfn(struct work_struct *work)
{
cpuhp_remove_state_nocalls(gic_rdists->cpuhp_memreserve_state);
@@ -5862,6 +5951,15 @@ int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
if (WARN_ON(!has_v4_1 && rdists->has_rvpeid))
rdists->has_rvpeid = false;
+ if (rdists->has_rvpeid) {
+ err = its_vpe_l1_cpuhp_init();
+ if (err) {
+ rdists->has_rvpeid = false;
+ rdists->has_vlpis = false;
+ pr_err("ITS: Failed to prepare VPE tables, disabling GICv4 support\n");
+ }
+ }
+
if (has_v4 & rdists->has_vlpis) {
const struct irq_domain_ops *sgi_ops;
diff --git a/drivers/irqchip/irq-gic-v3.c b/drivers/irqchip/irq-gic-v3.c
index 6e1fa5b247fc..e2e00372dcfb 100644
--- a/drivers/irqchip/irq-gic-v3.c
+++ b/drivers/irqchip/irq-gic-v3.c
@@ -1320,7 +1320,7 @@ static int gic_starting_cpu(unsigned int cpu)
gic_cpu_init();
if (gic_dist_supports_lpis())
- its_cpu_init();
+ its_cpu_init(true);
return 0;
}
@@ -2053,7 +2053,7 @@ static int __init gic_init_bases(phys_addr_t dist_phys_base,
if (gic_dist_supports_lpis()) {
its_init(handle, &gic_data.rdists, gic_data.domain, dist_prio_irq);
- its_cpu_init();
+ its_cpu_init(false);
its_lpi_memreserve_init();
} else {
if (IS_ENABLED(CONFIG_ARM_GIC_V2M))
diff --git a/include/linux/irqchip/arm-gic-v3.h b/include/linux/irqchip/arm-gic-v3.h
index ea5fd2374ebe..a5565a745100 100644
--- a/include/linux/irqchip/arm-gic-v3.h
+++ b/include/linux/irqchip/arm-gic-v3.h
@@ -621,6 +621,7 @@ struct rdists {
u64 flags;
cpumask_t *vpe_table_mask;
void *vpe_l1_base;
+ struct page *vpe_l1_prealloc;
} __percpu *rdist;
phys_addr_t prop_table_pa;
void *prop_table_va;
@@ -637,7 +638,7 @@ struct rdists {
struct irq_domain;
struct fwnode_handle;
int __init its_lpi_memreserve_init(void);
-int its_cpu_init(void);
+int its_cpu_init(bool use_prealloc);
int its_init(struct fwnode_handle *handle, struct rdists *rdists,
struct irq_domain *domain, u8 irq_prio);
int mbi_init(struct fwnode_handle *fwnode, struct irq_domain *parent);
--
2.43.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* Re: [RFC PATCH v8 01/14] mm: Add an allocator for CoCo shared memory
2026-09-24 10:05 ` [RFC PATCH v8 01/14] mm: Add an allocator for CoCo shared memory Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:19 ` sashiko-bot
2026-09-24 14:22 ` Aneesh Kumar K.V
0 siblings, 1 reply; 24+ messages in thread
From: sashiko-bot @ 2026-09-24 10:19 UTC (permalink / raw)
To: Aneesh Kumar K.V (Arm)
Cc: dri-devel, media-ci, Marc Zyngier, kvmarm, Oliver Upton
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] mm: alloc_cc_shared_pages_node fails to strip __GFP_MOVABLE
- [Medium] mm: alloc_cc_shared_pages_node fails to strip __GFP_HIGHMEM in fallback path
- [Medium] mm: cc_make_shared and cc_make_private are not exported for modular callers
--
commit 3978c8ce72bbc2aa23e57a460b585808665262a4
Author: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
mm: Add an allocator for CoCo shared memory
This commit introduces a common allocator for confidential-computing guests
to allocate shared memory. It provides helpers to allocate properly aligned
contiguous pages and transition them to shared state. It also adds helpers
for transitioning memory states when callers manage their own backing memory.
> diff --git a/mm/cc_shared.c b/mm/cc_shared.c
> new file mode 100644
> index 0000000000000..85e16f4504b8f
> --- /dev/null
> +++ b/mm/cc_shared.c
[ ... ]
> +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);
> +}
[Severity: Medium]
Will this function be unavailable to dynamically loadable modules since it
is missing an EXPORT_SYMBOL_GPL() macro?
The commit message states it should be usable by callers managing their own
backing memory, but virtio or guest drivers won't be able to link to it
without an export.
> +
> +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);
> +}
[Severity: Medium]
Does this function also need an EXPORT_SYMBOL_GPL() macro so that modules
can successfully link and call it?
[ ... ]
> +static int __alloc_cc_shared_pages_node(int nid, gfp_t gfp,
> + size_t requested,
> + struct cc_shared_pages *mem)
> +{
[ ... ]
> + /*
> + * 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);
[Severity: High]
Does this code inadvertently leave __GFP_MOVABLE intact if passed by the
caller?
Pages are returned as shared memory, but they lack migration hooks
(movable_operations) to handle encryption state transitions during page
migration.
If a caller passes __GFP_MOVABLE, the page might be placed in ZONE_MOVABLE.
Since the kernel migration code does not know how to handle encryption state
transitions, the page cannot be safely migrated. Could this permanently pin
the movable zone, thereby breaking memory hot-unplug and compaction?
> + if (nid == NUMA_NO_NODE)
> + page = alloc_pages(gfp, order);
[ ... ]
> +int alloc_cc_shared_pages_node(int nid, gfp_t gfp,
> + size_t requested,
> + struct cc_shared_pages *mem)
> +{
[ ... ]
> + if (cc_platform_has(CC_ATTR_MEM_ENCRYPT))
> + return __alloc_cc_shared_pages_node(nid, gfp, requested, mem);
> +
> + order = get_order(requested);
> + if (order > MAX_PAGE_ORDER)
> + return -EINVAL;
> +
> + if (nid == NUMA_NO_NODE)
> + page = alloc_pages(gfp, order);
> + else
> + page = alloc_pages_node(nid, gfp, order);
[Severity: Medium]
Can this fallback path inadvertently allocate a highmem page?
The function's kernel-doc explicitly guarantees: "A memory-state transition
requires a valid linear-map address, so such allocations never come from
high memory".
In the non-encrypted fallback path, the caller's unmodified gfp flags
(potentially containing __GFP_HIGHMEM) are passed directly to alloc_pages()
or alloc_pages_node() without any masking.
If a caller passes a flag mask containing __GFP_HIGHMEM (e.g., GFP_HIGHUSER)
on a system without memory encryption, the fallback path will allocate a
highmem page. Any subsequent attempt by the caller to access the memory via
page_address() could return NULL and cause a crash, breaking the API
contract. Should the gfp flags be sanitized in this path as well?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260924100529.1398790-1-aneesh.kumar@kernel.org?part=1
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [RFC PATCH v8 04/14] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
2026-09-24 10:05 ` [RFC PATCH v8 04/14] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:19 ` sashiko-bot
0 siblings, 0 replies; 24+ messages in thread
From: sashiko-bot @ 2026-09-24 10:19 UTC (permalink / raw)
To: Aneesh Kumar K.V (Arm)
Cc: Oliver Upton, Marc Zyngier, kvmarm, dri-devel, media-ci
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] irqchip/gic-v3-its: buddy allocator corruption due to discarded shared_size
--
commit d897529703a71c914b0334abcb563ffb37979469
Author: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
This commit updates the ITS allocator to route page allocations through
alloc_cc_shared_pages_node(). It reconstructs common allocation metadata
from the original ITS order and the architecture granule size on release.
> diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
> index ac5507acb1a0c..4814161df02ed 100644
> --- a/drivers/irqchip/irq-gic-v3-its.c
> +++ b/drivers/irqchip/irq-gic-v3-its.c
[ ... ]
> @@ -214,25 +213,13 @@ static gfp_t gfp_flags_quirk;
> static struct page *its_alloc_pages_node(int node, gfp_t gfp,
> unsigned int order)
> {
> - struct page *page;
> - int ret = 0;
> -
> - page = alloc_pages_node(node, gfp | gfp_flags_quirk, order);
> -
> - if (!page)
> - return NULL;
> + struct cc_shared_pages mem;
>
> - ret = set_memory_decrypted((unsigned long)page_address(page),
> - 1 << order);
> - /*
> - * If set_memory_decrypted() fails then we don't know what state the
> - * page is in, so we can't free it. Instead we leak it.
> - * set_memory_decrypted() will already have WARNed.
> - */
> - if (ret)
> + if (alloc_cc_shared_pages_node(node, gfp | gfp_flags_quirk,
> + PAGE_SIZE << order, &mem))
> return NULL;
>
> - return page;
> + return mem.page;
> }
[Severity: High]
Is it safe to discard mem.shared_size here? The API contract seems to require
callers to retain the true allocated size so it can be passed back when
freeing the pages.
> static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)
> @@ -242,13 +229,15 @@ static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)
>
> static void its_free_pages(void *addr, unsigned int order)
> {
> - /*
> - * If the memory cannot be encrypted again then we must leak the pages.
> - * set_memory_encrypted() will already have WARNed.
> - */
> - if (set_memory_encrypted((unsigned long)addr, 1 << order))
> + struct cc_shared_layout layout;
> + struct cc_shared_pages mem;
> +
> + if (WARN_ON(cc_shared_calc_layout(PAGE_SIZE << order, &layout)))
> return;
> - free_pages((unsigned long)addr, order);
> +
> + mem.page = virt_to_page(addr);
> + mem.shared_size = layout.shared_size;
> + free_cc_shared_pages(&mem);
> }
[Severity: High]
Does recalculating the size here cause a mismatch on non-CC platforms?
When memory encryption is disabled, alloc_cc_shared_pages_node() does not
round up the allocation to the granule size; it simply allocates the
requested order.
However, cc_shared_calc_layout() unconditionally rounds up to the
architectural granule size. If the granule size exceeds the requested
allocation size, free_cc_shared_pages() will call __free_pages() with a
higher order than was actually allocated. Can this corrupt the buddy
allocator by freeing unallocated pages?
> static struct gen_pool *itt_pool;
> @@ -273,7 +262,8 @@ static void *itt_alloc_pool(int node, int size)
> if (!page)
> break;
>
> - gen_pool_add(itt_pool, (unsigned long)page_address(page), PAGE_SIZE, node);
> + gen_pool_add(itt_pool, (unsigned long)page_address(page),
> + cc_shared_granule_size(), node);
> } while (!addr);
>
> return (void *)addr;
[Severity: High]
Will adding a hardcoded granule size to the generic pool advertise unallocated
out-of-bounds memory to consumers?
On non-CC platforms, the underlying allocation size is just a single page,
but this adds the full cc_shared_granule_size() bytes to the pool. Could
this lead to pool consumers accessing memory beyond what was actually
allocated?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260924100529.1398790-1-aneesh.kumar@kernel.org?part=4
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [RFC PATCH v8 03/14] irqchip/gic-v3-its: Resolve the default NUMA node explicitly
2026-09-24 10:05 ` [RFC PATCH v8 03/14] irqchip/gic-v3-its: Resolve the default NUMA node explicitly Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:19 ` sashiko-bot
0 siblings, 0 replies; 24+ messages in thread
From: sashiko-bot @ 2026-09-24 10:19 UTC (permalink / raw)
To: Aneesh Kumar K.V (Arm)
Cc: media-ci, kvmarm, Oliver Upton, Marc Zyngier, dri-devel
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] irqchip/gic-v3-its: incomplete NUMA_NO_NODE resolution exposes allocations to task mempolicy
--
commit 6d762ab83972132164ae57c1422f1916decb2fe3
Author: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
irqchip/gic-v3-its: Resolve the default NUMA node explicitly
alloc_pages_node() resolves NUMA_NO_NODE to numa_mem_id() internally. In
preparation for switching ITS allocations to the CoCo shared memory
allocator, resolve the default node explicitly in its_alloc_pages().
The CoCo shared memory allocator interprets NUMA_NO_NODE as a request to
apply the current task's memory policy. Passing numa_mem_id() instead
preserves the existing nearest-memory-node placement across the
allocator switch. This patch does not change the current allocation
behavior.
> diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
> --- a/drivers/irqchip/irq-gic-v3-its.c
> +++ b/drivers/irqchip/irq-gic-v3-its.c
> @@ -236,7 +237,7 @@ static struct page *its_alloc_pages_node(int node, gfp_t gfp,
>
> static struct page *its_alloc_pages(gfp_t gfp, unsigned int order)
> {
> - return its_alloc_pages_node(NUMA_NO_NODE, gfp, order);
> + return its_alloc_pages_node(numa_mem_id(), gfp, order);
> }
[Severity: Medium]
Will this leave other callers of its_alloc_pages_node() exposed to the
task memory policy change?
When a device probe or ITT allocation occurs with its->numa_node ==
NUMA_NO_NODE (which is common when firmware lacks NUMA info), callers pass
NUMA_NO_NODE directly to its_alloc_pages_node():
drivers/irqchip/irq-gic-v3-its.c:its_alloc_pages_node() {
...
page = alloc_pages_node(node, gfp | gfp_flags_quirk, order);
...
}
If node is NUMA_NO_NODE, it passes it directly to the underlying allocator.
When the underlying allocator interprets NUMA_NO_NODE as a request to apply
task mempolicy, these direct callers will be subjected to the current task's
memory policy instead of bypassing it and using the local node.
If a user binds the driver via sysfs under a restrictive task mempolicy,
the allocations could land on suboptimal distant nodes or fail entirely.
Should the NUMA_NO_NODE resolution logic be moved inside its_alloc_pages_node()
to ensure all callers are protected?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260924100529.1398790-1-aneesh.kumar@kernel.org?part=3
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [RFC PATCH v8 06/14] dma-pool: Allocate CoCo atomic pools using CoCo shared memory allocator
2026-09-24 10:05 ` [RFC PATCH v8 06/14] dma-pool: Allocate CoCo atomic pools using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:19 ` sashiko-bot
0 siblings, 0 replies; 24+ messages in thread
From: sashiko-bot @ 2026-09-24 10:19 UTC (permalink / raw)
To: Aneesh Kumar K.V (Arm)
Cc: Marc Zyngier, dri-devel, media-ci, kvmarm, Oliver Upton
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] dma-pool: atomic_pool_expand() falsely returns success on failure
--
commit 7de8bc3a8ea9e2bbef83e09d72fa88999ce898b9
Author: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
dma-pool: Allocate CoCo atomic pools using CoCo shared memory allocator
This commit modifies atomic DMA pools to use the CoCo shared memory allocator.
It prevents allocation fallback below the required shared granule size, and
uses the cc_make_shared() helper to transition the backing allocations in a
sleepable context.
> diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c
> index 70b7f64b17ab5..f059312c15aa5 100644
> --- a/kernel/dma/pool.c
> +++ b/kernel/dma/pool.c
[ ... ]
> @@ -85,6 +85,9 @@ static bool cma_in_zone(gfp_t gfp)
> static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
> gfp_t gfp)
> {
> + struct cc_shared_layout layout;
> + unsigned long attrs = 0;
> + unsigned int min_order = 0;
> unsigned int order;
> struct page *page = NULL;
> bool leak_pages = false;
> @@ -92,6 +95,17 @@ static int atomic_pool_expand(struct dma_gen_pool *dma_pool, size_t pool_size,
> int ret = -ENOMEM;
> pgprot_t prot __maybe_unused;
>
> + if (dma_pool->cc_shared) {
> + ret = cc_shared_calc_layout(pool_size, &layout);
> + if (ret)
> + goto out;
[Severity: High]
If cc_shared_calc_layout() succeeds, it returns 0 and overwrites the initial
ret = -ENOMEM. Could this cause the function to falsely return 0 (success) on
subsequent failures?
For instance, if page allocation fails below, the function jumps to out and
returns the current value of ret:
if (!page)
goto out;
Similarly, if remapping fails later in the function:
addr = dma_common_contiguous_remap(...)
if (!addr)
goto free_page;
In both cases, ret will be 0 instead of -ENOMEM. Would it be better to reset
ret back to -ENOMEM when these failures occur, or use a separate variable
for the cc_shared_calc_layout() return value?
> + pool_size = layout.shared_size;
> + min_order = get_order(layout.alignment);
> + if (min_order > MAX_PAGE_ORDER)
> + return -E2BIG;
> + attrs = __DMA_ATTR_ALLOC_CC_SHARED;
> + }
> +
> /* Cannot allocate larger than MAX_PAGE_ORDER */
> order = min(get_order(pool_size), MAX_PAGE_ORDER);
>
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260924100529.1398790-1-aneesh.kumar@kernel.org?part=6
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [RFC PATCH v8 02/14] mm: Zero memory during shared memory transitions
2026-09-24 10:05 ` [RFC PATCH v8 02/14] mm: Zero memory during shared memory transitions Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:22 ` sashiko-bot
2026-09-25 12:08 ` Kiryl Shutsemau
1 sibling, 0 replies; 24+ messages in thread
From: sashiko-bot @ 2026-09-24 10:22 UTC (permalink / raw)
To: Aneesh Kumar K.V (Arm)
Cc: Oliver Upton, media-ci, dri-devel, kvmarm, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] mm: ciphertext garbage in shared mappings due to zeroing in prepare callback
- [High] hv: uninitialized kernel memory leaked on non-confidential VMs
--
commit 0b9cafc26c64a65c124d5f6d15281e2bb9c41b6e
Author: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
mm: Zero memory during shared memory transitions
This commit makes zeroing an implicit property of set_memory_decrypted().
Each architecture clears memory at a safe point in its transition, and
redundant post-transition memset() calls are removed from standard allocators.
> diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c
> --- a/arch/x86/coco/tdx/tdx.c
> +++ b/arch/x86/coco/tdx/tdx.c
> @@ -978,6 +978,9 @@ static int tdx_enc_status_change_prepare(unsigned long vaddr, int numpages,
> bool enc)
> {
> + if (!enc)
> + memset((void *)vaddr, 0, (size_t)numpages << PAGE_SHIFT);
> +
[Severity: Critical]
Does this write encrypted zeroes into RAM? Because this memset() in
tdx_enc_status_change_prepare() happens before the PTE encryption bit is
cleared, writing zeroes through the encrypted mapping results in ciphertext
being stored. When the mapping later transitions to shared, reading this
memory will yield that raw ciphertext.
[ ... ]
> diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c
> --- a/arch/x86/mm/mem_encrypt_amd.c
> +++ b/arch/x86/mm/mem_encrypt_amd.c
> @@ -285,6 +286,9 @@ static void enc_dec_hypercall(unsigned long vaddr, unsigned long size, bool enc)
>
> static int amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool enc)
> {
> + if (!enc)
> + memset((void *)vaddr, 0, (size_t)npages << PAGE_SHIFT);
> +
[Severity: Critical]
Could amd_enc_status_change_prepare() suffer from the same ciphertext issue?
Writing zeroes to the private encrypted mapping before the transition leaves
ciphertext in RAM, which will appear as garbage once the page is mapped as
decrypted and shared.
[ ... ]
> diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c
> --- a/drivers/hv/hv.c
> +++ b/drivers/hv/hv.c
> @@ -123,12 +123,13 @@ static int hv_alloc_page(void **page, bool decrypt, const char *note)
> if (!*page)
> return -ENOMEM;
>
> - if (decrypt)
> + if (decrypt) {
> ret = set_memory_decrypted((unsigned long)*page, 1);
> - if (ret)
> - goto failed;
> -
> - memset(*page, 0, PAGE_SIZE);
> + if (ret)
> + goto failed;
> + } else {
> + memset(*page, 0, PAGE_SIZE);
> + }
> return 0;
[Severity: High]
Does hv_alloc_page() bypass memset() on non-confidential VMs? On standard
VMs, decrypt can evaluate to true. set_memory_decrypted() is a no-op that
returns 0 there, and since __get_free_page() doesn't use __GFP_ZERO, this
path could return completely uninitialized kernel memory to the hypervisor.
[ ... ]
> diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c
> --- a/drivers/hv/hv_common.c
> +++ b/drivers/hv/hv_common.c
> @@ -505,8 +505,6 @@ int hv_common_cpu_init(unsigned int cpu)
> /* It may be unsafe to free 'mem' */
> return ret;
> }
> -
> - memset(mem, 0x00, pgcount * HV_HYP_PAGE_SIZE);
> }
[Severity: High]
Could hv_common_cpu_init() leak uninitialized memory here? The
kmalloc_array() allocation above doesn't use __GFP_ZERO, so removing this
memset() leaves the hypercall input/output page uninitialized on standard
VMs.
[ ... ]
> diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
> --- a/kernel/dma/direct.c
> +++ b/kernel/dma/direct.c
> @@ -324,7 +324,9 @@ void *dma_direct_alloc(struct device *dev, size_t size,
> cpu_addr = page_address(page);
> }
>
> - memset(cpu_addr, 0, size);
> + /* Zero after remapping because the page may be in HighMem. */
> + if (!mark_mem_decrypt)
> + memset(cpu_addr, 0, size);
>
> if (set_uncached) {
[Severity: Critical]
Is it safe to bypass this memset() for shared CC memory in
dma_direct_alloc()? Since the memset() inside the x86 prepare callbacks
leaves ciphertext in memory, skipping the post-transition zeroing here means
we will return garbage-filled memory to callers expecting clean DMA buffers.
[ ... ]
> @@ -457,11 +463,13 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
> return NULL;
>
> cpu_addr = page_address(page);
> - if ((attrs & __DMA_ATTR_ALLOC_CC_SHARED) &&
> - dma_set_decrypted(dev, cpu_addr, size))
> - goto out_leak_pages;
> setup_page:
> - memset(cpu_addr, 0, size);
> + if (mark_mem_decrypt) {
> + if (dma_set_decrypted(dev, cpu_addr, size))
> + goto out_leak_pages;
> + } else {
> + memset(cpu_addr, 0, size);
> + }
> *dma_handle = phys_to_dma_direct(dev, page_to_phys(page),
> attrs & __DMA_ATTR_ALLOC_CC_SHARED);
[Severity: Critical]
Does skipping the zeroing here in dma_direct_alloc_pages() for
mark_mem_decrypt also expose callers to raw ciphertext in the returned
buffer?
[ ... ]
> diff --git a/mm/cc_shared.c b/mm/cc_shared.c
> --- a/mm/cc_shared.c
> +++ b/mm/cc_shared.c
> @@ -137,9 +137,6 @@ static int __alloc_cc_shared_pages_node(int nid, gfp_t gfp,
> return ret;
> }
>
> - if (zero)
> - memset(page_address(page), 0, layout.shared_size);
> -
> mem->page = page;
> mem->shared_size = layout.shared_size;
[Severity: Critical]
Can removing this memset() cause __alloc_cc_shared_pages_node() to return
memory containing ciphertext? Callers expecting zeroed memory will receive
garbage instead.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260924100529.1398790-1-aneesh.kumar@kernel.org?part=2
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [RFC PATCH v8 08/14] swiotlb: Align shared IO TLB pools to the shared granule size
2026-09-24 10:05 ` [RFC PATCH v8 08/14] swiotlb: Align shared IO TLB pools " Aneesh Kumar K.V (Arm)
@ 2026-09-24 10:22 ` sashiko-bot
0 siblings, 0 replies; 24+ messages in thread
From: sashiko-bot @ 2026-09-24 10:22 UTC (permalink / raw)
To: Aneesh Kumar K.V (Arm)
Cc: media-ci, kvmarm, dri-devel, Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mm: infinite loop in swiotlb_init_late() when remap fails
--
commit 9e7f1b72520615870ee21508117e4c5c7e0eb684
Author: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
swiotlb: Align shared IO TLB pools to the shared granule size
This commit aligns and sizes the early memblock pool to the common CoCo
shared granule, leaving the slot count unchanged. It replaces the default
and dynamic pool set_memory calls with helpers to keep alignment validation
and architecture dispatch centralized. Pages are intentionally leaked if
their private state cannot be restored safely.
> diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
> index ded7016a46a71..394db93b7eb96 100644
> --- a/kernel/dma/swiotlb.c
> +++ b/kernel/dma/swiotlb.c
[ ... ]
> @@ -578,7 +578,7 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,
> swiotlb_adjust_nareas(num_possible_cpus());
>
> retry:
> - order = get_order(nslabs << IO_TLB_SHIFT);
> + order = get_order(ALIGN(nslabs << IO_TLB_SHIFT, cc_shared_granule_size()));
> nslabs = SLABS_PER_PAGE << order;
[Severity: Medium]
If remap() fails later in this function, nslabs is halved and the code jumps
back to the retry label to try a smaller allocation:
kernel/dma/swiotlb.c:swiotlb_init_late() {
...
if (remap)
rc = remap(vstart, nslabs);
if (rc) {
free_pages((unsigned long)vstart, order);
nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE);
if (nslabs < IO_TLB_MIN_SLABS)
return rc;
retried = true;
goto retry;
}
...
}
Does the new ALIGN macro at the retry label risk inflating the halved size
back to its original value if cc_shared_granule_size() is >= 2MB?
If so, this would restore nslabs to its original failing value, overriding
the shrinking mechanism and creating an infinite loop.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260924100529.1398790-1-aneesh.kumar@kernel.org?part=8
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [RFC PATCH v8 01/14] mm: Add an allocator for CoCo shared memory
2026-09-24 10:19 ` sashiko-bot
@ 2026-09-24 14:22 ` Aneesh Kumar K.V
2026-09-24 18:26 ` Jason Gunthorpe
0 siblings, 1 reply; 24+ messages in thread
From: Aneesh Kumar K.V @ 2026-09-24 14:22 UTC (permalink / raw)
To: sashiko-reviews; +Cc: dri-devel, media-ci, Marc Zyngier, kvmarm, Oliver Upton
sashiko-bot@kernel.org writes:
> Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
> - [High] mm: alloc_cc_shared_pages_node fails to strip __GFP_MOVABLE
> - [Medium] mm: alloc_cc_shared_pages_node fails to strip __GFP_HIGHMEM in fallback path
> - [Medium] mm: cc_make_shared and cc_make_private are not exported for modular callers
> --
>
> commit 3978c8ce72bbc2aa23e57a460b585808665262a4
> Author: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
> mm: Add an allocator for CoCo shared memory
>
> This commit introduces a common allocator for confidential-computing guests
> to allocate shared memory. It provides helpers to allocate properly aligned
> contiguous pages and transition them to shared state. It also adds helpers
> for transitioning memory states when callers manage their own backing memory.
>
>> diff --git a/mm/cc_shared.c b/mm/cc_shared.c
>> new file mode 100644
>> index 0000000000000..85e16f4504b8f
>> --- /dev/null
>> +++ b/mm/cc_shared.c
> [ ... ]
>> +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);
>> +}
>
> [Severity: Medium]
> Will this function be unavailable to dynamically loadable modules since it
> is missing an EXPORT_SYMBOL_GPL() macro?
>
> The commit message states it should be usable by callers managing their own
> backing memory, but virtio or guest drivers won't be able to link to it
> without an export.
>
>> +
>> +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);
>> +}
>
> [Severity: Medium]
> Does this function also need an EXPORT_SYMBOL_GPL() macro so that modules
> can successfully link and call it?
>
I guess we don't want the cc_make_shared() and cc_make_private() APIs to be
called by modules. Modules that need CoCo shared memory should use
alloc_cc_shared_pages().
>
> [ ... ]
>> +static int __alloc_cc_shared_pages_node(int nid, gfp_t gfp,
>> + size_t requested,
>> + struct cc_shared_pages *mem)
>> +{
> [ ... ]
>> + /*
>> + * 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);
>
> [Severity: High]
> Does this code inadvertently leave __GFP_MOVABLE intact if passed by the
> caller?
>
> Pages are returned as shared memory, but they lack migration hooks
> (movable_operations) to handle encryption state transitions during page
> migration.
>
> If a caller passes __GFP_MOVABLE, the page might be placed in ZONE_MOVABLE.
> Since the kernel migration code does not know how to handle encryption state
> transitions, the page cannot be safely migrated. Could this permanently pin
> the movable zone, thereby breaking memory hot-unplug and compaction?
>
I don't think we want CoCo shared memory allocations to be movable, but
I'm also not sure whether we should filter out all of these GFP flags.
>
>> + if (nid == NUMA_NO_NODE)
>> + page = alloc_pages(gfp, order);
> [ ... ]
>> +int alloc_cc_shared_pages_node(int nid, gfp_t gfp,
>> + size_t requested,
>> + struct cc_shared_pages *mem)
>> +{
> [ ... ]
>> + if (cc_platform_has(CC_ATTR_MEM_ENCRYPT))
>> + return __alloc_cc_shared_pages_node(nid, gfp, requested, mem);
>> +
>> + order = get_order(requested);
>> + if (order > MAX_PAGE_ORDER)
>> + return -EINVAL;
>> +
>> + if (nid == NUMA_NO_NODE)
>> + page = alloc_pages(gfp, order);
>> + else
>> + page = alloc_pages_node(nid, gfp, order);
>
> [Severity: Medium]
> Can this fallback path inadvertently allocate a highmem page?
>
> The function's kernel-doc explicitly guarantees: "A memory-state transition
> requires a valid linear-map address, so such allocations never come from
> high memory".
>
Only when a private-to-shared transition is required.
>
> In the non-encrypted fallback path, the caller's unmodified gfp flags
> (potentially containing __GFP_HIGHMEM) are passed directly to alloc_pages()
> or alloc_pages_node() without any masking.
>
> If a caller passes a flag mask containing __GFP_HIGHMEM (e.g., GFP_HIGHUSER)
> on a system without memory encryption, the fallback path will allocate a
> highmem page. Any subsequent attempt by the caller to access the memory via
> page_address() could return NULL and cause a crash, breaking the API
> contract. Should the gfp flags be sanitized in this path as well?
>
From the allocation API's perspective, both GFP flags are supported. The
only restriction is that memory cannot come from highmem when the
allocation requires a private-to-shared transition.
-aneesh
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [RFC PATCH v8 01/14] mm: Add an allocator for CoCo shared memory
2026-09-24 14:22 ` Aneesh Kumar K.V
@ 2026-09-24 18:26 ` Jason Gunthorpe
0 siblings, 0 replies; 24+ messages in thread
From: Jason Gunthorpe @ 2026-09-24 18:26 UTC (permalink / raw)
To: Aneesh Kumar K.V
Cc: sashiko-reviews, dri-devel, media-ci, Marc Zyngier, kvmarm,
Oliver Upton
On Thu, Sep 24, 2026 at 07:52:39PM +0530, Aneesh Kumar K.V wrote:
> >> +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);
> >> +}
> >
> > [Severity: Medium]
> > Will this function be unavailable to dynamically loadable modules since it
> > is missing an EXPORT_SYMBOL_GPL() macro?
> >
> > The commit message states it should be usable by callers managing their own
> > backing memory, but virtio or guest drivers won't be able to link to it
> > without an export.
> >> +
> >> +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);
> >> +}
> >
> > [Severity: Medium]
> > Does this function also need an EXPORT_SYMBOL_GPL() macro so that modules
> > can successfully link and call it?
>
> I guess we don't want the cc_make_shared() and cc_make_private() APIs to be
> called by modules. Modules that need CoCo shared memory should use
> alloc_cc_shared_pages().
Indeed, but we may also be over indexing on the cc name given AMD also
uses this for their troubled host memory encryption address limit problem :\
The note about virtio seems a bit strange, these are new functions why
does it think virtio is calling them?
> >> + /*
> >> + * 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);
> >
> > [Severity: High]
> > Does this code inadvertently leave __GFP_MOVABLE intact if passed by the
> > caller?
> >
> > Pages are returned as shared memory, but they lack migration hooks
> > (movable_operations) to handle encryption state transitions during page
> > migration.
> >
> > If a caller passes __GFP_MOVABLE, the page might be placed in ZONE_MOVABLE.
> > Since the kernel migration code does not know how to handle encryption state
> > transitions, the page cannot be safely migrated. Could this permanently pin
> > the movable zone, thereby breaking memory hot-unplug and compaction?
>
> I don't think we want CoCo shared memory allocations to be movable, but
> I'm also not sure whether we should filter out all of these GFP flags.
It sounds like we should at least *reject* movable. It is right that
seems incompatible with this, and we have no use case..
> >> + if (cc_platform_has(CC_ATTR_MEM_ENCRYPT))
> >> + return __alloc_cc_shared_pages_node(nid, gfp, requested, mem);
> >> +
> >> + order = get_order(requested);
> >> + if (order > MAX_PAGE_ORDER)
> >> + return -EINVAL;
> >> +
> >> + if (nid == NUMA_NO_NODE)
> >> + page = alloc_pages(gfp, order);
> >> + else
> >> + page = alloc_pages_node(nid, gfp, order);
> >
> > [Severity: Medium]
> > Can this fallback path inadvertently allocate a highmem page?
> >
> > The function's kernel-doc explicitly guarantees: "A memory-state transition
> > requires a valid linear-map address, so such allocations never come from
> > high memory".
>
> Only when a private-to-shared transition is required.
IMHO disable the whole thing at kconfig if highmem is enabled. Make
the calls always fail.
Jason
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [RFC PATCH v8 02/14] mm: Zero memory during shared memory transitions
2026-09-24 10:05 ` [RFC PATCH v8 02/14] mm: Zero memory during shared memory transitions Aneesh Kumar K.V (Arm)
2026-09-24 10:22 ` sashiko-bot
@ 2026-09-25 12:08 ` Kiryl Shutsemau
1 sibling, 0 replies; 24+ messages in thread
From: Kiryl Shutsemau @ 2026-09-25 12:08 UTC (permalink / raw)
To: Aneesh Kumar K.V (Arm)
Cc: linux-coco, kvmarm, linux-arm-kernel, linux-kernel, iommu,
Andrew Morton, Baoquan He, Mike Rapoport, Pasha Tatashin,
Pratyush Yadav, Catalin Marinas, Christian König,
Jason Gunthorpe, Joerg Roedel, Marc Zyngier, Marek Szyprowski,
Robin Murphy, Steven Price, Sumit Semwal, Suzuki K Poulose,
Thomas Gleixner, Will Deacon, Russell King, Benjamin Gaignard,
Brian Starkey, John Stultz, Mark Rutland, Radu Rendec,
T.J. Mercier, Madhavan Srinivasan, Michael Ellerman,
Nicholas Piggin, Christophe Leroy, Ritesh Harjani,
Shrikanth Hegde, Alexander Gordeev, Gerald Schaefer,
Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
Sven Schnelle, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H . Peter Anvin, Rick Edgecombe, K . Y . Srinivasan,
Haiyang Zhang, Wei Liu, Dexuan Cui, Long Li, Paolo Bonzini,
Vitaly Kuznetsov, Andy Lutomirski, Peter Zijlstra, dri-devel,
linaro-mm-sig, linux-media, linux-mm
On Thu, Sep 24, 2026 at 03:35:17PM +0530, Aneesh Kumar K.V (Arm) wrote:
> diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c
> index f904a636d449..1f1f39082391 100644
> --- a/arch/x86/coco/tdx/tdx.c
> +++ b/arch/x86/coco/tdx/tdx.c
> @@ -978,6 +978,9 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc)
> static int tdx_enc_status_change_prepare(unsigned long vaddr, int numpages,
> bool enc)
> {
> + if (!enc)
> + memset((void *)vaddr, 0, (size_t)numpages << PAGE_SHIFT);
> +
> /*
> * Only handle shared->private conversion here.
> * See the comment in tdx_early_init().
This doesn't make sense to me.
On TDX the memset zeroes a page that is about to be dropped.
MapGPA(shared) makes the VMM back the GPA with different memory, and
what the guest reads afterwards through the shared mapping is whatever
the host has there.
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply [flat|nested] 24+ messages in thread
end of thread, other threads:[~2026-09-25 12:09 UTC | newest]
Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-24 10:05 [RFC PATCH v8 00/14] coco: guest: Add a shared-granule allocator for host-shared memory Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 01/14] mm: Add an allocator for CoCo shared memory Aneesh Kumar K.V (Arm)
2026-09-24 10:19 ` sashiko-bot
2026-09-24 14:22 ` Aneesh Kumar K.V
2026-09-24 18:26 ` Jason Gunthorpe
2026-09-24 10:05 ` [RFC PATCH v8 02/14] mm: Zero memory during shared memory transitions Aneesh Kumar K.V (Arm)
2026-09-24 10:22 ` sashiko-bot
2026-09-25 12:08 ` Kiryl Shutsemau
2026-09-24 10:05 ` [RFC PATCH v8 03/14] irqchip/gic-v3-its: Resolve the default NUMA node explicitly Aneesh Kumar K.V (Arm)
2026-09-24 10:19 ` sashiko-bot
2026-09-24 10:05 ` [RFC PATCH v8 04/14] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
2026-09-24 10:19 ` sashiko-bot
2026-09-24 10:05 ` [RFC PATCH v8 05/14] dma-contiguous: Derive shared alignment from DMA attributes Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 06/14] dma-pool: Allocate CoCo atomic pools using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
2026-09-24 10:19 ` sashiko-bot
2026-09-24 10:05 ` [RFC PATCH v8 07/14] dma-direct: Align CoCo shared DMA allocations to the shared granule size Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 08/14] swiotlb: Align shared IO TLB pools " Aneesh Kumar K.V (Arm)
2026-09-24 10:22 ` sashiko-bot
2026-09-24 10:05 ` [RFC PATCH v8 09/14] swiotlb: Reject misaligned restricted DMA pools for CoCo guests Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 10/14] dma-buf: system_heap: Limit scatterlist entries to the buffer size Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 11/14] dma-buf: system_heap: Allocate shared buffers using CoCo shared memory allocator Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 12/14] swiotlb: Make rounded shared pool capacity allocatable Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 13/14] mm: Assert CoCo shared allocations may sleep Aneesh Kumar K.V (Arm)
2026-09-24 10:05 ` [RFC PATCH v8 14/14] irqchip/gic-v3-its: Preallocate VPE L1 tables Aneesh Kumar K.V (Arm)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox