coco: guest: Add a shared-granule allocator for host-shared memory
Subsystems (20)
v10 cover coco: guest: Add a shared-granule allocator for host-shared memory
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.
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(). 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.
The series also makes zeroing part of private-to-shared transitions, with each architecture clearing memory at the appropriate point. Callers must therefore initialize shared buffers after the transition; existing contents are not preserved.
The shared-granule size defaults to PAGE_SIZE and can be overridden by an architecture. The arm64 CCA changes to discover the host's IPA state change alignment are not included in this series.
GIC ITS command queues and tables and the dma-buf system_cc_shared heap use the common allocator. DMA direct, atomic DMA pools and SWIOTLB retain their allocation paths and use the common layout or transition helpers. Restricted DMA pools are checked for shared-granule alignment.
Hyper-V users of set_memory_encrypted() and set_memory_decrypted() are not changed to use cc_make_shared() 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.
Notes:
* Codex assisted with implementation, review and series organization.
Changes from v9:
https://lore.kernel.org/all/[email protected]
* Leak allocations after failed sharing in the common allocator and SWIOTLB.
* Move x86 zeroing from the AMD, TDX and Hyper-V vTOM callbacks to set_memory_decrypted(), after a successful transition
* Add set_memory_decrypted_nozero() for VGA mappings, which must change memory attributes without clearing device memory.
* Zero memory even when encryption is inactive in the generic, arm64 and pSeries implementations of set_memory_decrypted().
* Move pSeries zeroing after sharing. Document why pKVM and s390 must clear private contents before sharing
* Drop the GIC ITS VPE L1 preallocation patch from this series. This issue predates the series and will be addressed separately.
* Add mm/cc_shared.c and include/linux/cc_shared.h to the TSM MAINTAINERS entry.
Changes from v8:
https://lore.kernel.org/all/[email protected]
* Rebase onto the 7.4-rc1
* Move TDX and AMD (including SEV-SNP) zeroing to their finish callbacks so private-to-shared transitions clear the final shared mapping.
* Clear __GFP_MOVABLE for shared allocations, which cannot be migrated.
* Resolve NUMA_NO_NODE in the common ITS allocation helper to preserve placement for all ITS callers.
* Stop SWIOTLB allocation retries when the reduced pool would fall below the shared granule size, avoiding an allocation retry loop.
Changes from v7:
https://lore.kernel.org/all/[email protected]
* 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/[email protected]
* 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/[email protected]
* Rebased to latest kernel
* Drop patch arm64: realm: Move Realm memory encryption ops to RSI code
Changes from v4:
https://lore.kernel.org/all/[email protected]
* 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/[email protected]
* Fix build error reported by kernel test robot <[email protected]>
Changes from v2:
https://lore.kernel.org/all/[email protected]
* 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.
Aneesh Kumar K.V (Arm) (13):
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
MAINTAINERS | 2 + arch/arm/mm/dma-mapping.c | 5 +- arch/arm64/mm/mem_encrypt.c | 8 +- arch/powerpc/platforms/pseries/svm.c | 13 +- arch/s390/mm/init.c | 7 + arch/x86/hyperv/hv_init.c | 6 +- arch/x86/include/asm/set_memory.h | 1 + arch/x86/include/asm/vga.h | 4 +- arch/x86/kernel/kvmclock.c | 6 +- arch/x86/mm/mem_encrypt_amd.c | 1 + arch/x86/mm/pat/set_memory.c | 19 +- drivers/dma-buf/heaps/system_heap.c | 128 +++++----- drivers/firmware/arm_rmm/rsi.c | 9 + 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 | 42 ++-- drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 4 + include/linux/cc_shared.h | 39 +++ include/linux/dma-map-ops.h | 9 +- include/linux/set_memory.h | 8 +- kernel/dma/contiguous.c | 41 ++- kernel/dma/direct.c | 73 ++++-- kernel/dma/ops_helpers.c | 2 +- kernel/dma/pool.c | 27 +- kernel/dma/swiotlb.c | 94 ++++--- kernel/kexec_file.c | 3 +- mm/Makefile | 1 + mm/cc_shared.c | 238 ++++++++++++++++++ 30 files changed, 635 insertions(+), 211 deletions(-) create mode 100644 include/linux/cc_shared.h create mode 100644 mm/cc_shared.c
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
prerequisite-patch-id: 121492d71cc22b2adbfdedd668868c5a449ad764
prerequisite-patch-id: ed31fd728f0adba3d60dcd6f3bae8da1a2193bcc
Signature · 1 line
2.43.0
PATCH v10 1/13 mm: Add an allocator for CoCo shared memory
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.
--- MAINTAINERS | 2 + include/linux/cc_shared.h | 39 +++++++ mm/Makefile | 1 + mm/cc_shared.c | 238 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 280 insertions(+) create mode 100644 include/linux/cc_shared.h create mode 100644 mm/cc_shared.c
MAINTAINERS +2 -0
@@ -28553,6 +28553,8 @@ F: drivers/pci/tsm/ F: drivers/virt/coco/guest/ F: drivers/virt/coco/tsm-core.c F: include/linux/*tsm*.h +F: include/linux/cc_shared.h +F: mm/cc_shared.c F: samples/tsm-mr/ TRUSTED SERVICES TEE DRIVER
include/linux/cc_shared.h new file +39 -0
@@ -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 */
mm/Makefile +1 -0
@@ -57,6 +57,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \ interval_tree.o list_lru.o workingset.o \ debug.o gup.o mmap_lock.o vma_init.o char-mem.o \ $(mmu-y) +obj-y += cc_shared.o # Give 'page_alloc' its own module-parameter namespace page-alloc-y := page_alloc.o
mm/cc_shared.c new file +238 -0
238 lines not on this page · raw message
Signature · 1 line
2.43.0
Catalin Marinas
maintainerRe: [PATCH v10 01/13] mm: Add an allocator for CoCo shared memory
MAINTAINERS F: drivers/pci/tsm/ · line 28557
Quoted 10 lines · Aneesh Kumar K.V (Arm)
MAINTAINERS +2
-0@@ -28553,6 +28553,8 @@ F: drivers/pci/tsm/ F: drivers/virt/coco/guest/ F: drivers/virt/coco/tsm-core.c F: include/linux/*tsm*.h +F: include/linux/cc_shared.h +F: mm/cc_shared.c
Would it make sense to put these under a separate CoCo memory sharing and encryption entry? It should also cover include/linux/mem_encrypt.h and include/linux/cc_platform.h. Both are shared interfaces with no specific MAINTAINERS entry.
I don’t mind who maintains the entry but I’d list both linux-coco and linux-mm. We can leave include/linux/set_memory.h out for now since most of that header deals with page permissions and cacheability.
Signature · 1 line
Catalin
PATCH v10 2/13 mm: Zero memory during shared memory transitions
In reply to Aneesh Kumar K.V (Arm)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. TDX clears memory after the private-to-shared conversion succeeds in the finish callback. AMD clears memory in its finish callback after the page-table update, TLB flush, and hypervisor notification, including for SEV-SNP. This zeros the final shared mapping without adding a flag to set_memory_decrypted() or threading it through 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.
--- arch/arm64/mm/mem_encrypt.c | 8 +++- arch/powerpc/platforms/pseries/svm.c | 13 ++++-- arch/s390/mm/init.c | 7 ++++ arch/x86/hyperv/hv_init.c | 6 ++- arch/x86/include/asm/set_memory.h | 1 + arch/x86/include/asm/vga.h | 4 +- arch/x86/kernel/kvmclock.c | 6 +-- arch/x86/mm/mem_encrypt_amd.c | 1 + arch/x86/mm/pat/set_memory.c | 19 ++++++++- drivers/firmware/arm_rmm/rsi.c | 9 ++++ 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 | 4 ++ include/linux/set_memory.h | 8 +++- kernel/dma/direct.c | 22 ++++++---- mm/cc_shared.c | 7 +--- 17 files changed, 115 insertions(+), 54 deletions(-)
arch/arm64/mm/mem_encrypt.c +7 -1
@@ -17,6 +17,7 @@#include <linux/compiler.h> #include <linux/err.h> #include <linux/mm.h> +#include <linux/string.h> #include <asm/mem_encrypt.h>
@@ -42,8 +43,13 @@ EXPORT_SYMBOL_GPL(set_memory_encrypted);int set_memory_decrypted (unsigned long addr,int numpages) { -if (likely (!crypt_ops) ||WARN_ON (!PAGE_ALIGNED (addr))) +if (likely (!crypt_ops)) { +memset ((void *)addr,0 , (size_t )numpages <<PAGE_SHIFT );return 0 ; + } + +if (WARN_ON (!PAGE_ALIGNED (addr))) +return -EINVAL ;return crypt_ops->decrypt (addr, numpages); }
arch/powerpc/platforms/pseries/svm.c +9 -4
@@ -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>
@@ -53,13 +54,17 @@ int set_memory_encrypted(unsigned long addr, int numpages)int set_memory_decrypted (unsigned long addr,int numpages) { -if (!cc_platform_has (CC_ATTR_MEM_ENCRYPT )) -return 0 ; -if (!PAGE_ALIGNED (addr))return -EINVAL ; -uv_share_page (PHYS_PFN (__pa (addr)), numpages); +if (cc_platform_has (CC_ATTR_MEM_ENCRYPT )) +uv_share_page (PHYS_PFN (__pa (addr)), numpages); +/* +* Firmware may scrub pages with nonzero values during +* private-to-shared conversion. Clear the final shared mapping +* after the transition so that callers receive zeroed memory. +*/ +memset ((void *)addr,0 , (size_t )numpages <<PAGE_SHIFT );return 0 ; }
arch/s390/mm/init.c +7 -0
@@ -129,6 +129,13 @@ int set_memory_encrypted(unsigned long vaddr, int numpages)int set_memory_decrypted (unsigned long vaddr,int numpages) {int i; + +/* +* uv_set_shared() exposes existing contents to the host; see the data +* leakage warning in share() in asm/uv.h. Clear private data first. +*/ +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));
arch/x86/hyperv/hv_init.c +4 -2
@@ -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); } }
arch/x86/include/asm/set_memory.h +1 -0
@@ -52,6 +52,7 @@ int set_memory_4k(unsigned long addr, int numpages);bool set_memory_enc_stop_conversion (void );int set_memory_encrypted (unsigned long addr,int numpages);int set_memory_decrypted (unsigned long addr,int numpages); +int set_memory_decrypted_nozero (unsigned long addr,int numpages);int set_memory_np_noalias (unsigned long addr,int numpages);int set_memory_nonglobal (unsigned long addr,int numpages);
arch/x86/include/asm/vga.h +2 -2
@@ -14,7 +14,7 @@* On the PC, we can just recalculate addresses and then * access the videoram directly without any black magic. * To support memory encryption however, we need to access -* the videoram as decrypted memory. +* the videoram as decrypted memory without clearing its contents. */ #define VGA_MAP_MEM (x, s) \
@@ -22,7 +22,7 @@unsigned long start = (unsigned long )phys_to_virt (x); \ \if (IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT )) \ -set_memory_decrypted (start, (s) >>PAGE_SHIFT ); \ +set_memory_decrypted_nozero (start, (s) >>PAGE_SHIFT ); \ \ start; \ })
arch/x86/kernel/kvmclock.c +3 -3
@@ -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 )
arch/x86/mm/mem_encrypt_amd.c +1 -0
@@ -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>
arch/x86/mm/pat/set_memory.c +18 -1
@@ -2545,10 +2545,27 @@ EXPORT_SYMBOL_GPL(set_memory_encrypted);int set_memory_decrypted (unsigned long addr,int numpages) { -return __set_memory_enc_dec (addr, numpages, false); +int ret =__set_memory_enc_dec (addr, numpages, false); + +/* +* Firmware may scrub pages with nonzero values during private-to-shared +* conversion. Clear the final shared mapping after the transition so +* that callers receive zeroed memory. +*/ +if (!ret) +memset ((void *)addr,0 , (size_t )numpages <<PAGE_SHIFT ); + +return ret; }EXPORT_SYMBOL_GPL (set_memory_decrypted); +/* Device memory needs a decrypted mapping without writes to its contents. */ +int set_memory_decrypted_nozero (unsigned long addr,int numpages) +{ +return __set_memory_enc_dec (addr, numpages, false); +} +EXPORT_SYMBOL_GPL (set_memory_decrypted_nozero); +int set_pages_uc (struct page *page,int numpages) {unsigned long addr = (unsigned long )page_address (page);
drivers/firmware/arm_rmm/rsi.c +9 -0
@@ -6,6 +6,7 @@#include <linux/jump_label.h> #include <linux/memblock.h> #include <linux/psci.h> +#include <linux/string.h> #include <linux/swiotlb.h> #include <linux/arm-rsi-cmds.h> #include <linux/kobject.h>
@@ -146,6 +147,14 @@ static int realm_set_memory_decrypted(unsigned long addr, int numpages) {int ret =__set_memory_enc_dec (addr, numpages, false); +/* +* The RMM may scrub pages with nonzero values before returning them to +* the host. Zeroing before conversion would not guarantee zeroed shared +* memory, so clear the final shared mapping after the transition. +*/ +if (!ret) +memset ((void *)addr,0 , (size_t )numpages <<PAGE_SHIFT ); +WARN (ret,"Failed to decrypt memory, %d pages will be leaked" , numpages);
drivers/hv/connection.c +20 -21
55 lines not on this page · raw message
drivers/hv/hv.c +6 -5
@@ -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:
drivers/hv/hv_common.c +0 -2
@@ -534,8 +534,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 ); }/*
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +4 -0
@@ -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,9 @@ static int pkvm_set_memory_encrypted(unsigned long addr, int numpages)static int pkvm_set_memory_decrypted (unsigned long addr,int numpages) { +/* Sharing exposes the existing contents to the host; clear them first. */ +memset ((void *)addr,0 , (size_t )numpages <<PAGE_SHIFT ); +return __set_memory_range (ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID , addr, numpages); }
include/linux/set_memory.h +7 -1
@@ -5,6 +5,10 @@#ifndef _LINUX_SET_MEMORY_H_#define _LINUX_SET_MEMORY_H_ +#include <linux/string.h> + +#include <asm/page.h> +/** * DOC: Kernel page table permissions *
@@ -21,7 +25,8 @@* * When an architecture does not implement these APIs they succeed without * doing anything, so a return value of 0 does not mean that the permissions -* were actually changed. +* were actually changed. set_memory_decrypted() also clears the range, +* including when no encryption change is needed. It is for normal RAM only. * * Callers that depend on the permissions being applied must ensure that the * architecture supports the required operation for the target addresses. The
@@ -186,6 +191,7 @@ static inline int set_memory_encrypted(unsigned long addr, int numpages)static inline int set_memory_decrypted (unsigned long addr,int numpages) { +memset ((void *)addr,0 , (size_t )numpages <<PAGE_SHIFT );return 0 ; }#endif /* CONFIG_ARCH_HAS_MEM_ENCRYPT */
kernel/dma/direct.c +15 -7
@@ -229,7 +229,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
@@ -237,7 +238,6 @@ void *dma_direct_alloc(struct device *dev, size_t size,* allocation. */ allow_highmem = false; - mark_mem_decrypt = true; } size =PAGE_ALIGN (size);
@@ -341,7 +341,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;
@@ -451,10 +453,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);
@@ -465,6 +470,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; }
@@ -473,11 +479,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;
mm/cc_shared.c +2 -5
@@ -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);
@@ -116,7 +115,8 @@ static int __alloc_cc_shared_pages_node(int nid, gfp_t gfp,return -EINVAL ;/* * 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. * Shared allocations have no migration support, so they must not * be allocated with __GFP_MOVABLE. */
@@ -143,9 +143,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 ;
Signature · 1 line
2.43.0
Fuad Tabba
reviewerRe: [PATCH v10 02/13] mm: Zero memory during shared memory transitions
Hi Aneesh,
On Mon, 16 Nov 2026 at 09:12, Aneesh Kumar K.V (Arm) <[email protected]> wrote:
[...]
Quoted 7 lines
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-transitionmemset()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.
pKVM shares memory in place, and a guest has to be able to share memory it has already set up, as Vincent pointed out [1]. That was one of the primary reasons for in-place conversion in guest_memfd.
Cheers,
/fuad
Re: [PATCH v10 02/13] mm: Zero memory during shared memory transitions
Quoted 13 lines · Fuad Tabba
Hi Aneesh,
On Mon, 16 Nov 2026 at 09:12, Aneesh Kumar K.V (Arm) <[email protected]> wrote:
[...]Quoted 7 lines
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-transitionmemset()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.
Quoted 8 lines
pKVM shares memory in place, and a guest has to be able to share memory it has already set up, as Vincent pointed out [1]. That was one of the primary reasons for in-place conversion in
guest_memfd.Cheers,
/fuad
Thanks for catching this. We discussed it before, but I reached the opposite conclusion:
https://lore.kernel.org/all/[email protected]
My mistake. How do you suggest we handle this? We want to avoid zeroing pages multiple times with different CoCo configurations. For example, to allocate a zeroed page, we currently do:
alloc_pages(__GFP_ZERO)set_memory_decrypted()memset(0, PAGE_SIZE)
Should we do something like
https://lore.kernel.org/all/[email protected]/
-anees
Re: [PATCH v10 02/13] mm: Zero memory during shared memory transitions
Quoted 30 lines · Aneesh Kumar K.V
Quoted 13 lines · Fuad Tabba
Hi Aneesh,
On Mon, 16 Nov 2026 at 09:12, Aneesh Kumar K.V (Arm) <[email protected]> wrote:
[...]Quoted 7 lines
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-transitionmemset()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.Quoted 8 lines
pKVM shares memory in place, and a guest has to be able to share memory it has already set up, as Vincent pointed out [1]. That was one of the primary reasons for in-place conversion in
guest_memfd.Cheers,
/fuadThanks for catching this. We discussed it before, but I reached the opposite conclusion:
Quoted 7 lines
My mistake. How do you suggest we handle this? We want to avoid zeroing pages multiple times with different CoCo configurations. For example, to allocate a zeroed page, we currently do:
alloc_pages(__GFP_ZERO)set_memory_decrypted()memset(0, PAGE_SIZE)
Just like you are wrapping set_memory_decrypted() in an allocator API the user that wants to have the no-change sharing needs its own special API too.
Then you can bury a special implementation for pkvm there.
Where is this special user in the kernel anyhow?
Jason
Fuad Tabba
reviewerRe: [PATCH v10 02/13] mm: Zero memory during shared memory transitions
Hi Jason, Aneesh,
On Mon, 16 Nov 2026 14:02:59 +0000, Jason Gunthorpe <[email protected]> wrote:
[...]
Quoted 3 lines
Just like you are wrapping
set_memory_decrypted()in an allocator API the user that wants to have the no-change sharing needs its own special API too.
That works. A separate API can cover no-change sharing when a user needs it, so I'm fine with set_memory_decrypted() zeroing.
Aneesh, the pKVM hunk does need cache maintenance after the memset(), which I have in the Cc: stable patch I posted earlier today [1].
Cheers,
/fuad
[1] https://lore.kernel.org/all/[email protected]/
Quoted 50 lines · Jason Gunthorpe
Quoted 30 lines · Aneesh Kumar K.V
Quoted 13 lines · Fuad Tabba
Hi Aneesh,
On Mon, 16 Nov 2026 at 09:12, Aneesh Kumar K.V (Arm) <[email protected]> wrote:
[...]Quoted 7 lines
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-transitionmemset()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.Quoted 8 lines
pKVM shares memory in place, and a guest has to be able to share memory it has already set up, as Vincent pointed out [1]. That was one of the primary reasons for in-place conversion in
guest_memfd.Cheers,
/fuadThanks for catching this. We discussed it before, but I reached the opposite conclusion:
Quoted 7 lines
My mistake. How do you suggest we handle this? We want to avoid zeroing pages multiple times with different CoCo configurations. For example, to allocate a zeroed page, we currently do:
alloc_pages(__GFP_ZERO)set_memory_decrypted()memset(0, PAGE_SIZE)Just like you are wrapping
set_memory_decrypted()in an allocator API the user that wants to have the no-change sharing needs its own special API too.Then you can bury a special implementation for pkvm there.
Where is this special user in the kernel anyhow?
Jason
Re: [PATCH v10 02/13] mm: Zero memory during shared memory transitions
Quoted 8 lines · Fuad Tabba
Hi Jason, Aneesh,
On Mon, 16 Nov 2026 14:02:59 +0000, Jason Gunthorpe <[email protected]> wrote:
[...]Quoted 3 lines
Just like you are wrapping
set_memory_decrypted()in an allocator API the user that wants to have the no-change sharing needs its own special API too.
Quoted 6 lines
That works. A separate API can cover no-change sharing when a user needs it, so I'm fine with
set_memory_decrypted()zeroing.Aneesh, the pKVM hunk does need cache maintenance after the
memset(), which I have in the Cc: stable patch I posted earlier today [1].
I will add that, along with the relevant comments.
Thanks,
-aneesh
Re: [PATCH v10 02/13] mm: Zero memory during shared memory transitions
In reply to Aneesh Kumar K.V (Arm)Quoted 12 lines · Aneesh Kumar K.V (Arm)
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. TDX clears memory after the private-to-shared conversion succeeds in the finish callback. AMD clears memory in its finish callback after the page-table update, TLB flush, and hypervisor notification, including for SEV-SNP. This zeros the final shared mapping without adding a flag toset_memory_decrypted()or threading it through architecture-specific callbacks.
Quoted 8 lines
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-transitionmemset()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.
Could the x86 and s390 maintainers please review this change?
Quoted 29 lines
Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Mark Rutland <[email protected]>Cc Madhavan Srinivasan <[email protected]>Cc Michael Ellerman <[email protected]>Cc Nicholas Piggin <[email protected]>Cc Christophe Leroy (CS GROUP) <[email protected]>Cc Ritesh Harjani (IBM) <[email protected]>Cc Shrikanth Hegde <[email protected]>Cc Alexander Gordeev <[email protected]>Cc Gerald Schaefer <[email protected]>Cc Heiko Carstens <[email protected]>Cc Vasily Gorbik <[email protected]>Cc Christian Borntraeger <[email protected]>Cc Sven Schnelle <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc Borislav Petkov <[email protected]>Cc Dave Hansen <[email protected]>Cc H. Peter Anvin <[email protected]>Cc Kiryl Shutsemau <[email protected]>Cc Rick Edgecombe <[email protected]>Cc K. Y. Srinivasan <[email protected]>Cc Haiyang Zhang <[email protected]>Cc Wei Liu <[email protected]>Cc Dexuan Cui <[email protected]>Cc Long Li <[email protected]>
Quoted 8 lines
Cc Paolo Bonzini <[email protected]>Cc Vitaly Kuznetsov <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Marek Szyprowski <[email protected]>Cc Robin Murphy <[email protected]>Cc Andrew Morton <[email protected]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>
[ ... 506 lines skipped ... ]
Re: [PATCH v10 02/13] mm: Zero memory during shared memory transitions
In reply to Aneesh Kumar K.V (Arm)arch/x86/kernel/kvmclock.c static void __init kvmclock_init_mem(void) · line 252
Quoted 4 lines · Aneesh Kumar K.V (Arm)
arch/x86/kernel/kvmclock.c +1
-1if (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);
NAK on this specific patch.
Good idea, but this needs to be broken out to do fewer things at once. The vga changes, for instance, are not explained anywhere.
Re: [PATCH v10 02/13] mm: Zero memory during shared memory transitions
Quoted 10 lines · Dave Hansen
arch/x86/kernel/kvmclock.c static void __init kvmclock_init_mem(void) · line 252
Quoted 4 lines · Aneesh Kumar K.V (Arm)
arch/x86/kernel/kvmclock.c +1
-1if (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);NAK on this specific patch.
Good idea, but this needs to be broken out to do fewer things at once.
Are you suggesting that this be split into per-architecture patches?
Quoted 1 line
The vga changes, for instance, are not explained anywhere.
modified arch/x86/include/asm/vga.h
@@ -14,7 +14,7 @@
* On the PC, we can just recalculate addresses and then
* access the videoram directly without any black magic.
* To support memory encryption however, we need to access
- * the videoram as decrypted memory.
+ * the videoram as decrypted memory without clearing its contents.
*/
#define VGA_MAP_MEM(x, s) \
@@ -22,7 +22,7 @@
unsigned long start = (unsigned long)phys_to_virt(x); \
\
if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) \
- set_memory_decrypted(start, (s) >> PAGE_SHIFT); \
+ set_memory_decrypted_nozero(start, (s) >> PAGE_SHIFT); \
\
start; \
})
The VGA change is an API substitution intended to preserve the existing
behavior: set_memory_decrypted_nozero() avoids the newly added memset(). I have not audited the VGA-specific memory encryption handling in detail.
I had intended the updated comment to explain this, but I can also split the patch and add that explanation.
-aneesh
Re: [PATCH v10 02/13] mm: Zero memory during shared memory transitions
Quoted 11 lines · Aneesh Kumar K.V
Quoted 9 lines · Dave Hansen
arch/x86/kernel/kvmclock.c static void __init kvmclock_init_mem(void) · line 252
Quoted 4 lines · Aneesh Kumar K.V (Arm)
arch/x86/kernel/kvmclock.c +1
-1if (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);NAK on this specific patch.
Good idea, but this needs to be broken out to do fewer things at once.
Are you suggesting that this be split into per-architecture patches?
Not necessarily. Just smaller, more reviewable pieces.
Re: [PATCH v10 02/13] mm: Zero memory during shared memory transitions
In reply to Aneesh Kumar K.V (Arm)Am 16.11.26 um 10:11 schrieb Aneesh Kumar K.V (Arm):
[..]
Quoted 8 lines
arch/s390/mm/init.c +0
-0@@ -129,6 +129,13 @@ int set_memory_encrypted(unsigned long vaddr, int numpages)int set_memory_decrypted (unsigned long vaddr,int numpages) {int i;
arch/s390/mm/init.c int set_memory_encrypted(unsigned long vaddr, int numpages) · line 141
Quoted 10 lines
arch/s390/mm/init.c +7
-0+ +/* +* uv_set_shared() exposes existing contents to the host; see the data +* leakage warning in share() in asm/uv.h. Clear private data first. +*/ +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));
s390 parts look sane.
This is the right order to memset before share if we want to guarantee that the memory does not expose things on s390.
The only side effect could be double zeroing., e.g. the default swiotlb pool gets an extra memset (memblock already zeroed it). Should not matter I guess.
All users are basically dma and swiotlb users and then device drivers like virtio use dma_alloc_coherent and do not call set_memory_decrypted directly.
PATCH v10 3/13 irqchip/gic-v3-its: Resolve the default NUMA node explicitly
In reply to Aneesh Kumar K.V (Arm)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_node().
The CoCo shared memory allocator interprets NUMA_NO_NODE as a request to apply the current task's memory policy. Resolving it to numa_mem_id() in the common ITS allocation helper preserves the existing nearest-memory-node placement for all callers
This patch does not change the current allocation behavior.
--- drivers/irqchip/irq-gic-v3-its.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/irqchip/irq-gic-v3-its.c +4 -0
@@ -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>
@@ -218,6 +219,9 @@ static struct page *its_alloc_pages_node(int node, gfp_t gfp,struct page *page;int ret =0 ; +if (node ==NUMA_NO_NODE ) + node =numa_mem_id (); + page =alloc_pages_node (node, gfp | gfp_flags_quirk, order);if (!page)
Signature · 1 line
2.43.0
PATCH v10 4/13 irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
In reply to Aneesh Kumar K.V (Arm)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.
--- drivers/irqchip/irq-gic-v3-its.c | 40 ++++++++++++-------------------- 1 file changed, 15 insertions(+), 25 deletions(-)
drivers/irqchip/irq-gic-v3-its.c +15 -25
@@ -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>
@@ -216,28 +215,16 @@ 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 ; +struct cc_shared_pages mem;if (node ==NUMA_NO_NODE ) node =numa_mem_id (); - page =alloc_pages_node (node, gfp | gfp_flags_quirk, order); - -if (!page) -return NULL ; - - 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)
@@ -247,13 +234,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;
@@ -278,7 +267,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;
Signature · 1 line
2.43.0
Catalin Marinas
maintainerRe: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
Hi Aneesh,
I'm replying on this patch rather than on the one introducing the API. I think it's more obvious here why I think we can make things simpler.
Quoted 19 lines · Aneesh Kumar K.V (Arm)
static struct page *its_alloc_pages_node(int node,
gfp_tgfp,drivers/irqchip/irq-gic-v3-its.c +1
-11unsigned int order) { -struct page *page; -int ret =0 ; +struct cc_shared_pages mem;if (node ==NUMA_NO_NODE ) node =numa_mem_id (); - page =alloc_pages_node (node, gfp | gfp_flags_quirk, order); - -if (!page) -return NULL ; - - ret =set_memory_decrypted ((unsigned long )page_address (page), -1 << order); -/* -* If set_memory_decrypted() fails then we don't know what state the
drivers/irqchip/irq-gic-v3-its.c static gfp_t gfp_flags_quirk; · line 227
Quoted 10 lines
drivers/irqchip/irq-gic-v3-its.c +3
-5-* 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;
This part is mostly fine but I do wonder why not make this alloc function the same as buddy alloc_pages_node() - return a struct page pointer.
Quoted 8 lines
static void
its_free_pages(void *addr, unsigned int order){drivers/irqchip/irq-gic-v3-its.c +1
-5-/* -* 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;
drivers/irqchip/irq-gic-v3-its.c static struct page *its_alloc_pages(gfp_t gfp, unsigned int order) · line 246
Quoted 10 lines
drivers/irqchip/irq-gic-v3-its.c +7
-1+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); }
Here it gets weird and I don't see why we can't have just a buddy-like interface. The kernel-doc for alloc says that shared_size must be retained for the corresponding free but without reading the source code, you can't tell that cc_shared_calc_layout() gives you the same shared_size populated by alloc. We have a similar pattern in dma-buf but using the compound page order.
What's wrong with a simpler API:
struct page *alloc_cc_shared_pages_node(int nid, gfp_t gfp, unsigned int order); struct page *alloc_cc_shared_pages(gfp_t gfp, unsigned int order); void free_cc_shared_pages(struct page *page, unsigned int order);
Both alloc and free take the original request order and perform the round up internally without exposing the details to the caller. Alloc shares the whole resulting buddy block and free makes the same block private before releasing it. We can document that free takes the original requested order and releases the whole allocation. Partial frees aren't supported.
We can get rid of layout and mem structures (or at least not as part of the API). Of course, this assumes that the shared granule size remains constant for a guest's lifetime but I don't think that's a problem.
drivers/irqchip/irq-gic-v3-its.c static void *itt_alloc_pool(int node, int size) · line 271
Quoted 7 lines
drivers/irqchip/irq-gic-v3-its.c +2
-1@@ -278,7 +267,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);
Here we can introduce:
unsigned int cc_shared_page_order(unsigned int order);
and just use (PAGE_SIZE << cc_shared_page_order(0)) for gen_pool_add().
Was there a caller you had in mind that needs the common allocator to share less than the whole buddy block?
Signature · 1 line
Catalin
Re: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
Quoted 10 lines · Catalin Marinas
Quoted 10 lines
drivers/irqchip/irq-gic-v3-its.c +2
-6-/* -* 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 ;
Quoted 6 lines
drivers/irqchip/irq-gic-v3-its.c static gfp_t gfp_flags_quirk; · line 227
Quoted 2 lines
drivers/irqchip/irq-gic-v3-its.c +1
-1-return page; +return mem.page;This part is mostly fine but I do wonder why not make this alloc function the same as buddy
alloc_pages_node()- return a struct page pointer.
I'd argue we shouldn't even be returning struct page here, do many places even use the struct page? What for?
Exposing struct page to places that just want a kernel addr means they have to call page_address and we are back to the highmem weirdness.
Quoted 17 lines
Quoted 17 lines
static void
its_free_pages(void *addr, unsigned int order){drivers/irqchip/irq-gic-v3-its.c +8
-6-/* -* 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);
Quoted 8 lines
Quoted 1 line
}
Here it gets weird and I don't see why we can't have just a buddy-like interface. The kernel-doc for alloc says that
shared_sizemust be retained for the corresponding free but without reading the source code, you can't tell thatcc_shared_calc_layout()gives you the sameshared_sizepopulated by alloc. We have a similar pattern in dma-buf but using the compound page order.
This makes sense, we should not be passing size to a free function, make a compount page/high order folio instead if the API is going to be struct page based.
Quoted 4 lines
We can get rid of layout and mem structures (or at least not as part of the API). Of course, this assumes that the shared granule size remains constant for a guest's lifetime but I don't think that's a problem.
It should be encoded in the struct page meta data, as the order.
Quoted 2 lines
Was there a caller you had in mind that needs the common allocator to share less than the whole buddy block?
If we want this it definitely needs to be a void * based API not struct page one though..
Jason
Catalin Marinas
maintainerRe: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
Quoted 18 lines · Jason Gunthorpe
Quoted 10 lines · Catalin Marinas
Quoted 10 lines
drivers/irqchip/irq-gic-v3-its.c +2
-6-/* -* 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 ;Quoted 6 lines
drivers/irqchip/irq-gic-v3-its.c static gfp_t gfp_flags_quirk; · line 227
Quoted 2 lines
drivers/irqchip/irq-gic-v3-its.c +1
-1-return page; +return mem.page;This part is mostly fine but I do wonder why not make this alloc function the same as buddy
alloc_pages_node()- return a struct page pointer.
Quoted 2 lines
I'd argue we shouldn't even be returning struct page here, do many places even use the struct page? What for?
I just suggested it for similarity with alloc_pages_node(). It can be made better indeed.
Quoted 2 lines
Exposing struct page to places that just want a kernel addr means they have to call
page_addressand we are back to the highmem weirdness.
dma-buf system heap needs struct page for scatterlist, so we need the virt_to_page() conversion if we return a VA.
Quoted 30 lines
Quoted 17 lines
Quoted 17 lines
static void
its_free_pages(void *addr, unsigned int order){drivers/irqchip/irq-gic-v3-its.c +8
-6-/* -* 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);Quoted 8 lines
Quoted 1 line
}
Here it gets weird and I don't see why we can't have just a buddy-like interface. The kernel-doc for alloc says that
shared_sizemust be retained for the corresponding free but without reading the source code, you can't tell thatcc_shared_calc_layout()gives you the sameshared_sizepopulated by alloc. We have a similar pattern in dma-buf but using the compound page order.This makes sense, we should not be passing size to a free function, make a compount page/high order folio instead if the API is going to be struct page based.
Quoted 6 lines
Quoted 4 lines
We can get rid of layout and mem structures (or at least not as part of the API). Of course, this assumes that the shared granule size remains constant for a guest's lifetime but I don't think that's a problem.
It should be encoded in the struct page meta data, as the order.
I agree.
Quoted 5 lines
Quoted 2 lines
Was there a caller you had in mind that needs the common allocator to share less than the whole buddy block?
If we want this it definitely needs to be a void * based API not struct page one though..
For whole-block sharing, either a VA or a folio works for me. If the allocator takes an order, the requested size and the sharing granule are both powers of two. Rounding up to the granule already gives a valid buddy block size, so there’s no need for a separate shared_size.
Signature · 1 line
Catalin
Re: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
Quoted 5 lines · Catalin Marinas
Quoted 2 lines
Exposing struct page to places that just want a kernel addr means they have to call
page_addressand we are back to the highmem weirdness.dma-buf system heap needs struct page for scatterlist, so we need the
virt_to_page()conversion if we return a VA.
Yeah, but if that is a one off I'd give it is own API..
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
Was there a caller you had in mind that needs the common allocator to share less than the whole buddy block?
If we want this it definitely needs to be a void * based API not struct page one though..
For whole-block sharing, either a VA or a folio works for me. If the allocator takes an order, the requested size and the sharing granule are both powers of two. Rounding up to the granule already gives a valid buddy block size, so there’s no need for a separate
shared_size.
But you still will need the allocator to use the struct page metadata for its own tracking information, so it shouldn't be exposed to the caller..
Jason
Re: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
In reply to Catalin MarinasQuoted 94 lines · Catalin Marinas
Hi Aneesh,
I'm replying on this patch rather than on the one introducing the API. I think it's more obvious here why I think we can make things simpler.
Quoted 19 lines · Aneesh Kumar K.V (Arm)
static struct page *its_alloc_pages_node(int node,
gfp_tgfp,drivers/irqchip/irq-gic-v3-its.c +1
-11unsigned int order) { -struct page *page; -int ret =0 ; +struct cc_shared_pages mem;if (node ==NUMA_NO_NODE ) node =numa_mem_id (); - page =alloc_pages_node (node, gfp | gfp_flags_quirk, order); - -if (!page) -return NULL ; - - ret =set_memory_decrypted ((unsigned long )page_address (page), -1 << order); -/* -* If set_memory_decrypted() fails then we don't know what state the drivers/irqchip/irq-gic-v3-its.c static gfp_t gfp_flags_quirk; · line 227
Quoted 10 lines
drivers/irqchip/irq-gic-v3-its.c +3
-5-* 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;This part is mostly fine but I do wonder why not make this alloc function the same as buddy
alloc_pages_node()- return a struct page pointer.Quoted 8 lines
static void
its_free_pages(void *addr, unsigned int order){drivers/irqchip/irq-gic-v3-its.c +1
-5-/* -* 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;drivers/irqchip/irq-gic-v3-its.c static struct page *its_alloc_pages(gfp_t gfp, unsigned int order) · line 246
Quoted 10 lines
drivers/irqchip/irq-gic-v3-its.c +7
-1+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); }Here it gets weird and I don't see why we can't have just a buddy-like interface. The kernel-doc for alloc says that
shared_sizemust be retained for the corresponding free but without reading the source code, you can't tell thatcc_shared_calc_layout()gives you the sameshared_sizepopulated by alloc. We have a similar pattern in dma-buf but using the compound page order.What's wrong with a simpler API:
struct page *alloc_cc_shared_pages_node(int nid,
gfp_tgfp, unsigned int order); struct page *alloc_cc_shared_pages(gfp_t gfp, unsigned int order); voidfree_cc_shared_pages(struct page *page, unsigned int order);Both alloc and free take the original request order and perform the round up internally without exposing the details to the caller. Alloc shares the whole resulting buddy block and free makes the same block private before releasing it. We can document that free takes the original requested order and releases the whole allocation. Partial frees aren't supported.
We can get rid of layout and mem structures (or at least not as part of the API). Of course, this assumes that the shared granule size remains constant for a guest's lifetime but I don't think that's a problem.
drivers/irqchip/irq-gic-v3-its.c static void *itt_alloc_pool(int node, int size) · line 271
Quoted 7 lines
drivers/irqchip/irq-gic-v3-its.c +2
-1@@ -278,7 +267,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);Here we can introduce:
Quoted 7 lines
unsigned int
cc_shared_page_order(unsigned int order);and just use (
PAGE_SIZE<<cc_shared_page_order(0)) forgen_pool_add().Was there a caller you had in mind that needs the common allocator to share less than the whole buddy block?
No, I don't have a caller that needs to share less than the whole buddy block.
Taking Jason's feedback into account, we could have the allocator request compound pages and recover the actual allocation order with compound_order(). That would avoid passing an order to free:
void *alloc_cc_shared_node(int nid, gfp_t gfp, unsigned int order); void *alloc_cc_shared(gfp_t gfp, unsigned int order); void free_cc_shared(void *addr);
Alloc would round up internally and share the entire buddy block; free would recover its order, make the whole block private, and release it.
As you noted, dma-buf would need virt_to_page() to obtain the pages for its scatterlist.
-aneesh
Re: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
Quoted 7 lines · Aneesh Kumar K.V
Taking Jason's feedback into account, we could have the allocator request compound pages and recover the actual allocation order with
compound_order(). That would avoid passing an order to free:void *alloc_cc_shared_node(int nid,
gfp_tgfp, unsigned int order); void *alloc_cc_shared(gfp_t gfp, unsigned int order); voidfree_cc_shared(void *addr);
It probably shouldn't be order here, that is hard to use. Just size_t len
And round up internally
If we ever want to do some kind of slab sharing scheme then the API is already set up for it.
Quoted 2 lines
As you noted, dma-buf would need
virt_to_page()to obtain the pages for its scatterlist.
I think that is fine, other places working with kalloc/vmap already do that.
Jason
Catalin Marinas
maintainerRe: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
Quoted 6 lines · Jason Gunthorpe
Quoted 4 lines · Aneesh Kumar K.V
Taking Jason's feedback into account, we could have the allocator request compound pages and recover the actual allocation order with
compound_order(). That would avoid passing an order to free:
Quoted 8 lines
Quoted 3 lines
void *alloc_cc_shared_node(int nid,
gfp_tgfp, unsigned int order); void *alloc_cc_shared(gfp_t gfp, unsigned int order); voidfree_cc_shared(void *addr);It probably shouldn't be order here, that is hard to use. Just
size_tlenAnd round up internally
Minor point in favour of keeping the '_pages' naming and order is that it makes it more obvious what alignment is returned for code that needs it like the ITS. Otherwise we can just document it, I don't have a strong opinion either way (only that it's simpler than what was proposed earlier).
Signature · 1 line
Catalin
Re: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
Quoted 14 lines · Catalin Marinas
Quoted 13 lines · Jason Gunthorpe
Quoted 7 lines · Aneesh Kumar K.V
Taking Jason's feedback into account, we could have the allocator request compound pages and recover the actual allocation order with
compound_order(). That would avoid passing an order to free:void *alloc_cc_shared_node(int nid,
gfp_tgfp, unsigned int order); void *alloc_cc_shared(gfp_t gfp, unsigned int order); voidfree_cc_shared(void *addr);It probably shouldn't be order here, that is hard to use. Just
size_tlen
Quoted 7 lines
Quoted 1 line
And round up internally
Minor point in favour of keeping the '
_pages' naming and order is that it makes it more obvious what alignment is returned for code that needs it like the ITS. Otherwise we can just document it, I don't have a strong opinion either way (only that it's simpler than what was proposed earlier).
All our allocators return things aligned to their size, does ITS need higher alignment than that?
Jason
Marc Zyngier
maintainerRe: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
Quoted 18 lines · Jason Gunthorpe
Quoted 16 lines · Catalin Marinas
Quoted 14 lines · Jason Gunthorpe
Quoted 7 lines · Aneesh Kumar K.V
Taking Jason's feedback into account, we could have the allocator request compound pages and recover the actual allocation order with
compound_order(). That would avoid passing an order to free:void *alloc_cc_shared_node(int nid,
gfp_tgfp, unsigned int order); void *alloc_cc_shared(gfp_t gfp, unsigned int order); voidfree_cc_shared(void *addr);It probably shouldn't be order here, that is hard to use. Just
size_tlenAnd round up internally
Quoted 8 lines
Quoted 5 lines
Minor point in favour of keeping the '
_pages' naming and order is that it makes it more obvious what alignment is returned for code that needs it like the ITS. Otherwise we can just document it, I don't have a strong opinion either way (only that it's simpler than what was proposed earlier).All our allocators return things aligned to their size, does ITS need higher alignment than that?
Yes. The ITT is always 256 byte aligned, irrespective of the size (5.3.10 MAPD), and the command queue always aligned to 64kB, irrespective of the size (5.2.8 The ITS command interface).
This mean that you can end up with a 16 byte ITT aligned to 256 bytes, and a 4kB command queue aligned to 64kB.
We can of course avoid all of that by wasting even more memory, which I am definitely happy to.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
In reply to Catalin MarinasQuoted 14 lines · Catalin Marinas
Quoted 13 lines · Jason Gunthorpe
Quoted 7 lines · Aneesh Kumar K.V
Taking Jason's feedback into account, we could have the allocator request compound pages and recover the actual allocation order with
compound_order(). That would avoid passing an order to free:void *alloc_cc_shared_node(int nid,
gfp_tgfp, unsigned int order); void *alloc_cc_shared(gfp_t gfp, unsigned int order); voidfree_cc_shared(void *addr);It probably shouldn't be order here, that is hard to use. Just
size_tlen
Quoted 8 lines
Quoted 1 line
And round up internally
Minor point in favour of keeping the '
_pages' naming and order is that it makes it more obvious what alignment is returned for code that needs it like the ITS. Otherwise we can just document it, I don't have a strong opinion either way (only that it's simpler than what was proposed earlier).
Thanks. I'll keep the byte-based interface for now and document the returned alignment:
void *alloc_cc_shared_node(int nid, gfp_t gfp, size_t len); void *alloc_cc_shared(gfp_t gfp, size_t len);
void free_cc_shared(void *addr);
This leaves room for a kmalloc-style shared buffer cache in the future, where allocations could come from already-shared backing memory. Callers would request a byte count without needing to know the underlying allocation order.
For now, the implementation will round up the size internally, allocate compound pages, and share the entire allocation. Will add this to the kernel doc?
* Allocate a physically contiguous block of at least @len bytes, rounded
* up to the sharing granule and then to a power-of-two number of pages.
* The physical base address is aligned to the full allocation size.
* The entire allocation is shared and zeroed.
-aneesh
Re: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
Quoted 37 lines · Aneesh Kumar K.V
Quoted 14 lines · Catalin Marinas
Quoted 13 lines · Jason Gunthorpe
Quoted 7 lines · Aneesh Kumar K.V
Taking Jason's feedback into account, we could have the allocator request compound pages and recover the actual allocation order with
compound_order(). That would avoid passing an order to free:void *alloc_cc_shared_node(int nid,
gfp_tgfp, unsigned int order); void *alloc_cc_shared(gfp_t gfp, unsigned int order); voidfree_cc_shared(void *addr);It probably shouldn't be order here, that is hard to use. Just
size_tlenQuoted 8 lines
Quoted 1 line
And round up internally
Minor point in favour of keeping the '
_pages' naming and order is that it makes it more obvious what alignment is returned for code that needs it like the ITS. Otherwise we can just document it, I don't have a strong opinion either way (only that it's simpler than what was proposed earlier).Thanks. I'll keep the byte-based interface for now and document the returned alignment:
void *alloc_cc_shared_node(int nid,
gfp_tgfp,size_tlen); void *alloc_cc_shared(gfp_t gfp,size_tlen);
voidfree_cc_shared(void *addr);This leaves room for a kmalloc-style shared buffer cache in the future, where allocations could come from already-shared backing memory. Callers would request a byte count without needing to know the underlying allocation order.
Quoted 8 lines
For now, the implementation will round up the size internally, allocate compound pages, and share the entire allocation. Will add this to the kernel doc?
* Allocate a physically contiguous block of at least @len bytes, rounded
* up to the sharing granule and then to a power-of-two number of pages.
* The physical base address is aligned to the full allocation size.
* The entire allocation is shared and zeroed.
It is "power-of-two sharing granule" right?
And when doing the ITS conversions you'll need to min() to the required alignments that Mark explained.
Jason
Re: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
Quoted 47 lines · Jason Gunthorpe
Quoted 37 lines · Aneesh Kumar K.V
Quoted 14 lines · Catalin Marinas
Quoted 13 lines · Jason Gunthorpe
Quoted 7 lines · Aneesh Kumar K.V
Taking Jason's feedback into account, we could have the allocator request compound pages and recover the actual allocation order with
compound_order(). That would avoid passing an order to free:void *alloc_cc_shared_node(int nid,
gfp_tgfp, unsigned int order); void *alloc_cc_shared(gfp_t gfp, unsigned int order); voidfree_cc_shared(void *addr);It probably shouldn't be order here, that is hard to use. Just
size_tlenQuoted 8 lines
Quoted 1 line
And round up internally
Minor point in favour of keeping the '
_pages' naming and order is that it makes it more obvious what alignment is returned for code that needs it like the ITS. Otherwise we can just document it, I don't have a strong opinion either way (only that it's simpler than what was proposed earlier).Thanks. I'll keep the byte-based interface for now and document the returned alignment:
void *alloc_cc_shared_node(int nid,
gfp_tgfp,size_tlen); void *alloc_cc_shared(gfp_t gfp,size_tlen);
voidfree_cc_shared(void *addr);This leaves room for a kmalloc-style shared buffer cache in the future, where allocations could come from already-shared backing memory. Callers would request a byte count without needing to know the underlying allocation order.
Quoted 8 lines
For now, the implementation will round up the size internally, allocate compound pages, and share the entire allocation. Will add this to the kernel doc?
* Allocate a physically contiguous block of at least @len bytes, rounded
* up to the sharing granule and then to a power-of-two number of pages.
* The physical base address is aligned to the full allocation size.
* The entire allocation is shared and zeroed.
Quoted 5 lines
It is "power-of-two sharing granule" right?
And when doing the ITS conversions you'll need to
min()to the required alignments that Mark explained.
Yes, the allocation size is a power-of-two multiple of the sharing granule, since the sharing granule is itself a power-of-two multiple of PAGE_SIZE. However, is that the clearest way to document it? The code does:
cc_shared_round_size(len, &size); order = get_order(size); page = alloc_pages(gfp, order);
Would it be clearer to describe the returned alignment in terms of PAGE_SIZE and the resulting order?
The ITS code already sizes its requests assuming that the allocator returns memory aligned to the allocation size. Since the new allocator preserves that guarantee, I thought those requests would continue to meet the ITS alignment requirements.
-aneesh
Re: [PATCH v10 04/13] irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory allocator
Quoted 11 lines · Aneesh Kumar K.V
Yes, the allocation size is a power-of-two multiple of the sharing granule, since the sharing granule is itself a power-of-two multiple of
PAGE_SIZE. However, is that the clearest way to document it? The code does:cc_shared_round_size(len, &size); order = get_order(size); page = alloc_pages(gfp, order);Would it be clearer to describe the returned alignment in terms of
PAGE_SIZEand the resulting order?
The API is "The alocation is aligned to the request size".
You can mention it is over allocated to fit the platform's shared/private granule so is very ineffecitient for small allocations.
Quoted 4 lines
The ITS code already sizes its requests assuming that the allocator returns memory aligned to the allocation size. Since the new allocator preserves that guarantee, I thought those requests would continue to meet the ITS alignment requirements.
Sounds great
Jason
PATCH v10 5/13 dma-contiguous: Derive shared alignment from DMA attributes
In reply to Aneesh Kumar K.V (Arm)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.
--- 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(-)
arch/arm/mm/dma-mapping.c +3 -2
@@ -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;
drivers/iommu/dma-iommu.c +1 -1
@@ -1646,7 +1646,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) { cpu_addr =alloc_pages_exact_nid (node, alloc_size, gfp);if (!cpu_addr)
include/linux/dma-map-ops.h +5 -4
@@ -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 ; }
kernel/dma/contiguous.c +30 -11
101 lines not on this page · raw message
kernel/dma/direct.c +1 -1
@@ -125,7 +125,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)))
kernel/dma/ops_helpers.c +1 -1
@@ -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) {void *va =alloc_pages_exact_nid (dev_to_node (dev), size, gfp);
kernel/dma/pool.c +1 -1
@@ -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 );
kernel/kexec_file.c +2 -1
@@ -702,7 +702,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 ;
Signature · 1 line
2.43.0
PATCH v10 6/13 dma-pool: Allocate CoCo atomic pools using CoCo shared memory allocator
In reply to Aneesh Kumar K.V (Arm)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.
--- kernel/dma/pool.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-)
kernel/dma/pool.c +21 -6
@@ -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,19 @@ 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) { +int err; + + err =cc_shared_calc_layout (pool_size, &layout); +if (err) +return err; + 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 +115,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 +142,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 +159,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:
Signature · 1 line
2.43.0
PATCH v10 7/13 dma-direct: Align CoCo shared DMA allocations to the shared granule size
In reply to Aneesh Kumar K.V (Arm)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.
--- kernel/dma/direct.c | 51 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 12 deletions(-)
kernel/dma/direct.c +39 -12
@@ -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;
@@ -125,7 +125,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)))
@@ -202,7 +202,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 ;
@@ -223,6 +223,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;
@@ -302,8 +303,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 ;
@@ -322,7 +330,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; }
@@ -381,6 +389,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);/*
@@ -424,6 +433,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 {
@@ -435,10 +450,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)
@@ -451,6 +464,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;
@@ -474,7 +488,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 ;
@@ -500,6 +520,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
@@ -516,6 +537,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 ;
Signature · 1 line
2.43.0
PATCH v10 8/13 swiotlb: Align shared IO TLB pools to the shared granule size
In reply to Aneesh Kumar K.V (Arm)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.
--- kernel/dma/swiotlb.c | 53 +++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 23 deletions(-)
kernel/dma/swiotlb.c +30 -23
@@ -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>
@@ -367,13 +368,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);
@@ -434,8 +435,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
@@ -443,9 +444,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",
@@ -454,7 +455,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 ; }
@@ -576,7 +577,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 ) {
@@ -585,6 +586,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; }
@@ -598,8 +601,13 @@ int swiotlb_init_late(size_t size, gfp_t gfp_mask,free_pages ((unsigned long )vstart, order); nslabs =ALIGN (nslabs >>1 ,IO_TLB_SEGSIZE ); -if (nslabs <IO_TLB_MIN_SLABS ) +if (nslabs <=IO_TLB_MIN_SLABS )return rc; + +/* Avoid infinite loop */ +if (WARN_ON (nslabs < (cc_shared_granule_size () >>IO_TLB_SHIFT ))) +return rc; + retried = true; goto retry; }
@@ -624,8 +632,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;
@@ -665,11 +672,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; }
@@ -709,12 +716,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 ;
@@ -726,15 +736,10 @@ 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))) - goto error; +if (cc_shared &&cc_make_shared (vaddr, bytes)) +/* leak pages on error */ +return NULL ;return page; - -error: -/* Intentional leak if pages cannot be encrypted again. */ -if (cc_shared && !set_memory_encrypted ((unsigned long )vaddr,PFN_UP (bytes))) -__free_pages (page, order); -return NULL ; }/**
@@ -805,9 +810,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)); }
Signature · 1 line
2.43.0
PATCH v10 9/13 swiotlb: Reject misaligned restricted DMA pools for CoCo guests
In reply to Aneesh Kumar K.V (Arm)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().
--- kernel/dma/swiotlb.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
kernel/dma/swiotlb.c +10 -3
@@ -39,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>
@@ -2007,6 +2006,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
@@ -2040,8 +2047,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);
Signature · 1 line
2.43.0
PATCH v10 10/13 dma-buf: system_heap: Limit scatterlist entries to the buffer size
In reply to Aneesh Kumar K.V (Arm)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.
--- drivers/dma-buf/heaps/system_heap.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/dma-buf/heaps/system_heap.c +6 -1
@@ -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); }
Signature · 1 line
2.43.0
PATCH v10 11/13 dma-buf: system_heap: Allocate shared buffers using CoCo shared memory allocator
In reply to Aneesh Kumar K.V (Arm)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.
--- drivers/dma-buf/heaps/system_heap.c | 121 ++++++++++++---------------- 1 file changed, 53 insertions(+), 68 deletions(-)
drivers/dma-buf/heaps/system_heap.c +53 -68
@@ -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);
Signature · 1 line
2.43.0
PATCH v10 12/13 swiotlb: Make rounded shared pool capacity allocatable
In reply to Aneesh Kumar K.V (Arm)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.
--- kernel/dma/swiotlb.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-)
kernel/dma/swiotlb.c +24 -6
@@ -328,6 +328,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;
@@ -431,11 +439,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
@@ -453,12 +462,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; }
@@ -471,6 +481,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;
@@ -495,13 +506,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",
@@ -867,6 +879,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);
Signature · 1 line
2.43.0
PATCH v10 13/13 mm: Assert CoCo shared allocations may sleep
In reply to Aneesh Kumar K.V (Arm)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.
--- mm/cc_shared.c | 3 +++ 1 file changed, 3 insertions(+)
mm/cc_shared.c +3 -0
@@ -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;
Signature · 1 line
2.43.0
Suzuki K Poulose
reviewerCc Michael Roth, Alexey, Pankaj Gupta
Quoted 10 lines · Aneesh Kumar K.V (Arm)
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.
Quoted 10 lines
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_memfdor 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.
IIUC, AMD SEV-SNP also has a similar requirement for other reasons, to align the "shared" allocations within a guest (2MB ?, as mentioned by David Kaplan at the LPC).
Michael, Alexey,
Please could you share your thoughts on this series ?
Suzuki
Quoted 24 lines · Suzuki K Poulose
Cc Michael Roth, Alexey, Pankaj Gupta
Quoted 10 lines · Aneesh Kumar K.V (Arm)
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.
Quoted 10 lines
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_memfdor 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.
Quoted 7 lines
IIUC, AMD SEV-SNP also has a similar requirement for other reasons, to align the "shared" allocations within a guest (2MB ?, as mentioned by David Kaplan at the LPC).
Michael, Alexey,
Please could you share your thoughts on this series ?
Yes, I think we concluded at LPC that AMD should use this series with a 2MB setting rather than try to do their HW assisted smash/unsmash
Jason