From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796205333; b=mY2Be8ASxuC2AFtYB23vSlwf6EHKKGRB/3wm5stbiJikXlz6VGBzfGgl+K4u6uGWCIik FNgQAWJfbJr17kN7l/RBgD3VqHtv8A2nsTJOt7Pdf1mR1TZC1V7JY250WwumlfUr4Kqiw X8wY535HcOqNgxp8NRytC3gPiUAsyXOj09hVJHSfBUPDB7FZNzUKLmKWQW8cMS3DP38hB rbTUO4voPykVopr9bXWA3uMkjSCZb+FpPw9Jv8T9cTimhajKkEjvos6Tx6wbH623uj1OV maNCuv26+qpB/XUNiUu2BaB0e+mzWKj53Ozo9/DcMd92Gh/fKYv+YQDRBBO0vTgVh4A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796205333; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=HNsfQRPLBpsbmSDKdconH0e+sZ6/ZnB+LhgCT2AJfzE=; b=W7BfPEZZEwomNtno1/FHEeSpcP1fFz1llvi1fbrTm3JdT9WsDZp9+n58cbKv93zqXHeH yxfCNIoAWhlGwLnaZSrPiFsjjJKekExmA02mwKlqi8vFu/vsvdpIpLlaRhR1pfiCyMpqs Dg/l3jySh9G4t9Fm4YKXtal/mYygvmfT/bKgG4PixdpdQfGZRlu7ii0pgzbKUDbeloCEp KGuGALdPnYxbS6/oBCNl+6PWWVTfl8XiDO071GX7awk4I07pDosggw565Vq6eZsrIg8Sj FNrmNXPuRme3+qmNBWyyypnn9Xvk/Aqk1BDMIu4mkgeSN275kHtcpCwa8Et/pgu3IFQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="a/b59yy5"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D547606A322; Wed, 2 Dec 2026 09:55:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=a/b59yy5 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6FB811F00930; Wed, 2 Dec 2026 09:55:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796205332; bh=HNsfQRPLBpsbmSDKdconH0e+sZ6/ZnB+LhgCT2AJfzE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=a/b59yy5JTnXfnCYx52Es1mFJt2FaHhO3yaIS0XVLg8IFpU9BGlVGzt5SXwKVAbXI is/M8XImzRNCC2r2xZBLja7gcxvE9nv9+Zu22gVm0Jx1p9j2aqvj6ws2s+nVLSfy9F RwN9b1iIcyO/4HUu94PohAN1pnDqLGQRXSOYig/K5EWDGjRzIT+/Xo3pLoDgu/knc7 dlJOGUSZy/f7aozFLzK54Kt++GZ7/DlvVaTm4qolSIYFstLDooebfLQsaIHiyMYVa/ y85buDwF+XLPMeOMyXb/TKrlzguwt68xjXbnpUZ19H0oKHOZaC6hRMO2QCxiJlVBfp JxlCNrq6u0VEA== From: "Aneesh Kumar K.V (Arm)" To: linux-coco@lists.linux.dev Cc: "Aneesh Kumar K.V (Arm)" , Andrew Morton , Catalin Marinas , =?UTF-8?q?Christian=20K=C3=B6nig?= , Jason Gunthorpe , Joerg Roedel , Marc Zyngier , Marek Szyprowski , Robin Murphy , Steven Price , Sumit Semwal , Suzuki K Poulose , Will Deacon , "T.J. Mercier" , Madhavan Srinivasan , Heiko Carstens , Christian Borntraeger , Sven Schnelle , Dave Hansen , Kiryl Shutsemau , Rick Edgecombe , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Haiyang Zhang , Wei Liu , Dexuan Cui , Andy Lutomirski , Peter Zijlstra , Alexander Gordeev , Gerald Schaefer , Vasily Gorbik , x86@kernel.org, dri-devel@lists.freedesktop.org, iommu@lists.linux.dev, linaro-mm-sig@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-media@vger.kernel.org, linux-mm@kvack.org, kvmarm@lists.linux.dev, patches@lists.linux.dev Subject: [PATCH v11 02/23] mm: Add an allocator for CoCo shared memory Date: Wed, 2 Dec 2026 15:24:25 +0530 Message-ID: <20261202095446.2029567-3-aneesh.kumar@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261202095446.2029567-1-aneesh.kumar@kernel.org> References: <20261202095446.2029567-1-aneesh.kumar@kernel.org> X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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