Confidential-computing guests may require shared memory to be aligned and transitioned in units larger than PAGE_SIZE. Provide a common allocator instead of requiring callers to open-code allocation and state changes. Add alloc_cc_shared() and alloc_cc_shared_node(), taking a byte count and returning a linear-map address. Round internally to the sharing granule and a power-of-two number of pages, and share the entire allocation. Its physical base is aligned to the allocation size. Always request compound metadata so cc_shared_alloc_size() and free_cc_shared() can recover the actual size without a caller-supplied descriptor or order. Exclude highmem and movable allocations even without memory encryption: the returned address must be in the linear map and there is no migration support. Preserve task NUMA policy for NUMA_NO_NODE. Defer requested zeroing until after the transition, which may modify memory contents. Retain allocations if sharing fails or private state cannot be restored. Cc: Andrew Morton Signed-off-by: Aneesh Kumar K.V (Arm) --- MAINTAINERS | 2 + include/linux/cc_shared.h | 25 ++++ mm/Makefile | 1 + mm/cc_shared.c | 240 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 268 insertions(+) create mode 100644 include/linux/cc_shared.h create mode 100644 mm/cc_shared.c diff --git a/MAINTAINERS b/MAINTAINERS index 4cadc468d28d..6e723c6eb31b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -6763,7 +6763,9 @@ L: linux-coco@lists.linux.dev L: linux-mm@kvack.org S: Maintained F: include/linux/cc_platform.h +F: include/linux/cc_shared.h F: include/linux/mem_encrypt.h +F: mm/cc_shared.c CONFIDENTIAL COMPUTING THREAT MODEL FOR X86 VIRTUALIZATION (SNP/TDX) M: Elena Reshetova diff --git a/include/linux/cc_shared.h b/include/linux/cc_shared.h new file mode 100644 index 000000000000..23ba457b5f57 --- /dev/null +++ b/include/linux/cc_shared.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_CC_SHARED_H +#define _LINUX_CC_SHARED_H + +#include +#include + +/* + * Architectures may override this to return the granule used for transitions + * between private and shared memory. The value must be a power of two and no + * smaller than PAGE_SIZE. + */ +size_t arch_cc_shared_granule_size(void); + +size_t cc_shared_granule_size(void); +int cc_shared_round_size(size_t requested, size_t *size); +bool cc_shared_range_valid(phys_addr_t base, size_t size); +int cc_make_shared(void *addr, size_t size); +int cc_make_private(void *addr, size_t size); +void *alloc_cc_shared_node(int nid, gfp_t gfp, size_t len); +void *alloc_cc_shared(gfp_t gfp, size_t len); +size_t cc_shared_alloc_size(void *addr); +void free_cc_shared(void *addr); + +#endif /* _LINUX_CC_SHARED_H */ diff --git a/mm/Makefile b/mm/Makefile index 2a3ec53d62ee..a712656449d1 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -57,6 +57,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \ interval_tree.o list_lru.o workingset.o \ debug.o gup.o mmap_lock.o vma_init.o char-mem.o \ $(mmu-y) +obj-y += cc_shared.o # Give 'page_alloc' its own module-parameter namespace page-alloc-y := page_alloc.o diff --git a/mm/cc_shared.c b/mm/cc_shared.c new file mode 100644 index 000000000000..e60e2269adb6 --- /dev/null +++ b/mm/cc_shared.c @@ -0,0 +1,240 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2026 ARM Ltd. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +size_t __weak arch_cc_shared_granule_size(void) +{ + return PAGE_SIZE; +} + +size_t cc_shared_granule_size(void) +{ + size_t granule = arch_cc_shared_granule_size(); + + if (WARN_ON_ONCE(granule < PAGE_SIZE || !is_power_of_2(granule))) + return PAGE_SIZE; + + return granule; +} +EXPORT_SYMBOL_GPL(cc_shared_granule_size); + +/** + * cc_shared_round_size - round a byte count to the sharing granule + * @requested: nonzero number of bytes + * @size: rounded byte count, unchanged on failure + * + * Return: 0 on success, -EINVAL for a zero request, or -EOVERFLOW. + */ +int cc_shared_round_size(size_t requested, size_t *size) +{ + size_t granule, rounded; + + if (!requested) + return -EINVAL; + + granule = cc_shared_granule_size(); + if (check_add_overflow(requested, granule - 1, &rounded)) + return -EOVERFLOW; + + *size = ALIGN_DOWN(rounded, granule); + return 0; +} +EXPORT_SYMBOL_GPL(cc_shared_round_size); + +bool cc_shared_range_valid(phys_addr_t base, size_t size) +{ + size_t granule = cc_shared_granule_size(); + + if (!size) + return false; + + return IS_ALIGNED(base, granule) && IS_ALIGNED(size, granule); +} +EXPORT_SYMBOL_GPL(cc_shared_range_valid); + +static int cc_validate_transition(void *addr, size_t size) +{ + phys_addr_t phys; + + if (!addr || !size || !PAGE_ALIGNED(addr) || + !virt_addr_valid(addr)) + return -EINVAL; + + phys = page_to_phys(virt_to_page(addr)); + if (!cc_shared_range_valid(phys, size)) + return -EINVAL; + + return 0; +} + +int cc_make_shared(void *addr, size_t size) +{ + int ret = cc_validate_transition(addr, size); + + if (ret) + return ret; + + return set_memory_decrypted((unsigned long)addr, size >> PAGE_SHIFT); +} + +int cc_make_private(void *addr, size_t size) +{ + int ret = cc_validate_transition(addr, size); + + if (ret) + return ret; + + return set_memory_encrypted((unsigned long)addr, size >> PAGE_SHIFT); +} + +/** + * alloc_cc_shared_node - allocate a whole block of shared memory + * @nid: NUMA node from which to allocate, or %NUMA_NO_NODE for task policy + * @gfp: allocation flags + * @len: requested number of bytes; must be nonzero + * + * Allocate a physically contiguous block of at least @len bytes. + * The physical base address is aligned to the full allocation size. + * The entire allocation is shared and zeroed. + * + * The allocation is rounded up to satisfy the platform's sharing granule, + * which can make small allocations inefficient. The current implementation + * uses compound pages for higher-order allocations to record their size. + * + * Allocations always have a linear-map address and cannot be migrated. + * __GFP_HIGHMEM and __GFP_MOVABLE are ignored. Callers must not split the + * allocation or free individual pages. Release the entire allocation with + * free_cc_shared(). + * + * Must be called from sleepable context. + * Return: The allocation's linear-map address, or NULL on failure. + */ +void *alloc_cc_shared_node(int nid, gfp_t gfp, size_t len) +{ + bool encrypted = cc_platform_has(CC_ATTR_MEM_ENCRYPT); + struct page *page; + unsigned int order; + void *addr; + size_t size; + + might_sleep(); + + if (cc_shared_round_size(len, &size)) + return NULL; + + order = get_order(size); + if (order > MAX_PAGE_ORDER) + return NULL; + + /* + * Returning a linear-map address requires low memory even when memory + * encryption is inactive. Callers may retain virtual or physical + * addresses, and this allocator provides no migration support, so the + * allocation must not be marked movable in either case. + */ + gfp &= ~(__GFP_HIGHMEM | __GFP_MOVABLE); + gfp |= __GFP_COMP; + gfp &= ~__GFP_ZERO; + + if (nid == NUMA_NO_NODE) + page = alloc_pages(gfp, order); + else + page = alloc_pages_node(nid, gfp, order); + if (!page) + return NULL; + + addr = page_address(page); + size = PAGE_SIZE << order; + if (encrypted && cc_make_shared(addr, size)) { + /* + * A failed transition may leave the range partially shared. + * Making already-private pages private again is not safe on + * every architecture. Retain the entire allocation rather than + * return pages with uncertain state to the page allocator. + */ + pr_warn_ratelimited("leaking %zu bytes with uncertain shared state\n", + size); + return NULL; + } + + memset(addr, 0, size); + return addr; +} +EXPORT_SYMBOL_GPL(alloc_cc_shared_node); + +/** + * alloc_cc_shared - allocate a whole block of shared memory + * @gfp: allocation flags + * @len: requested number of bytes; must be nonzero + * + * Equivalent to alloc_cc_shared_node() with %NUMA_NO_NODE. + * + * Return: The allocation's linear-map address, or NULL on failure. + */ +void *alloc_cc_shared(gfp_t gfp, size_t len) +{ + return alloc_cc_shared_node(NUMA_NO_NODE, gfp, len); +} +EXPORT_SYMBOL_GPL(alloc_cc_shared); + +/** + * cc_shared_alloc_size - get the size of a shared allocation + * @addr: original address returned by alloc_cc_shared_node() or + * alloc_cc_shared(); must be non-NULL and not yet freed + * + * Return: The full allocation size recorded in compound-page metadata, + * including any rounding performed by the allocator. + */ +size_t cc_shared_alloc_size(void *addr) +{ + return page_size(virt_to_page(addr)); +} +EXPORT_SYMBOL_GPL(cc_shared_alloc_size); + +/** + * free_cc_shared - make an entire shared allocation private and free it + * @addr: original address returned by alloc_cc_shared_node() or + * alloc_cc_shared(), or NULL + * + * Use the allocation size from compound-page metadata. Partial frees are + * not supported. Retain the allocation if making it private fails. + * + * Must be called from sleepable context. + */ +void free_cc_shared(void *addr) +{ + struct page *page; + size_t size; + + might_sleep(); + + if (!addr) + return; + + page = virt_to_page(addr); + size = cc_shared_alloc_size(addr); + if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) { + if (cc_make_private(addr, size)) { + pr_warn_ratelimited("leaking %zu bytes that cannot be made private\n", + size); + return; + } + } + + __free_pages(page, compound_order(page)); +} +EXPORT_SYMBOL_GPL(free_cc_shared); -- 2.43.0