From nobody Sun Feb 8 18:49:25 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4864F345CA6 for ; Thu, 29 Jan 2026 01:16:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769649372; cv=none; b=objcbFUrDD8X3ROCzw/uxFKcoomwPbHeXN2FITtN+OI1X9jS9RaxI/mwRcacEZ1Vy3NaYj6rESJmGHfpo5+p4vYPy+imLx2Li8PEK1Rgjt/wlo+OnOtqlc4ACSCRHrshdTC7cbz5C63hH2stf5T/EpK4lEVb9byg4ACltLY1meg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769649372; c=relaxed/simple; bh=xyU67iIG8B0z1lftPU8r+DkLP9n8GV25IDyjReoJtDA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hZnVv4ntneadZBfYyRhKlnsriitADqYIn+PYGSGIjn2xC9tLLt+sE2SG+2YV2y8tL99kKJD2dZFYxFLBJThcIcHBTdOTdsPkmFEUc8llQk9YIWUOczDjPiW5STjacG9Rc/RqnofmK7H51UKmvH9PnFETrhb3zTQsHqYc88x40us= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=FxffVin7; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FxffVin7" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b62da7602a0so305549a12.2 for ; Wed, 28 Jan 2026 17:16:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769649369; x=1770254169; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=83UiBV3UrNTMNXOexHb3DCeTDDgbi4QS7S/0ehY+L8M=; b=FxffVin7zPw86OwPlBQrcaRWJCcsyS0f6OOjPg6ViCZJtzbA8WgAMjAckH98lMCivH Mf8ljmvmuFADbVRsgZ5kItBQZakcqJh8enANA7rGCqvXt/TwSWLvqwNY3pjPdPGSyOW7 srq4QBFXSvXxM6pSGJVke7WiKAJ/OP7na5hNFQiIDZ+KKcHeqC/vvFtB44djsR1i+XgG VvkSfik4OPiXiFNxsmTKPCguhSOxzENx/j7l/V0VYLl8EgdX734hilkKfIQ7WwL9a0CP JsfaIKE0MD+cHqQKEJGNq5HnSN2RB2c6cBiYjFbReNuYaV4U4tLsq/ELNbb3+zcr9mmL j/Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769649369; x=1770254169; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=83UiBV3UrNTMNXOexHb3DCeTDDgbi4QS7S/0ehY+L8M=; b=Y0GISsRXIvctciiDTu3+SSy8KDf0ggiDnbKt6RAb8ziEZgUZuONpaUPZlBvWRHX8UU vbI1i/n0H4lsfcyxNEFQV3V+JFcjxp/2tpO2OWm3Q/2QXkekvIBE6vH3wn8UrUXPdHWL vzUSYPYvHu8YT6nsTTLj064kELfJv6nYwJOjxGbMRsML1nObOt+yOieQWqlW+Abyrapr 6NJIkCbwrNEcU6gP2WsyjrN+j6KLuwlSUibrq/kWCJwSVN0gNtW46Wgqk0OSAlGuUsVR 4iuQyQgqAG4C4q1RDkNwpEdUrnj1KIa5oKPzWMyJiE0jiOLxG7OOQkVj8XttC2JciKKG hE3w== X-Gm-Message-State: AOJu0Yy/aqEv0BF4b+y6zl3GRnRzszR6qBJL171h4wjWTFTdP+RXvwvp FwnTsRF65KyAMpN/kKPz6EBXVyrul0ngtt+LCYM4MrJCByUvFIMmC6xJyFJu7rbY6tVjqKPZRC2 qpmpUxQ== X-Received: from pjbsy16.prod.google.com ([2002:a17:90b:2d10:b0:34c:2124:a2b0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:820e:b0:38d:fa67:e87f with SMTP id adf61e73a8af0-38ec627b9f1mr7469147637.12.1769649368609; Wed, 28 Jan 2026 17:16:08 -0800 (PST) Reply-To: Sean Christopherson Date: Wed, 28 Jan 2026 17:14:53 -0800 In-Reply-To: <20260129011517.3545883-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129011517.3545883-1-seanjc@google.com> X-Mailer: git-send-email 2.53.0.rc1.217.geba53bf80e-goog Message-ID: <20260129011517.3545883-22-seanjc@google.com> Subject: [RFC PATCH v5 21/45] x86/tdx: Add APIs to support get/put of DPAMT entries from KVM, under spinlock From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Kiryl Shutsemau , Sean Christopherson , Paolo Bonzini Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org, Kai Huang , Rick Edgecombe , Yan Zhao , Vishal Annapurve , Ackerley Tng , Sagi Shahar , Binbin Wu , Xiaoyao Li , Isaku Yamahata Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Rick Edgecombe Implement a PAMT "caching" scheme, similar to KVM's pre-allocated cache of MMU assets, along with APIs to allow KVM to pre-allocate PAMT pages before acquiring its mmu_lock spinlock, but wait until S-EPT entries are created to actually update the Dynamic PAMT. Signed-off-by: Rick Edgecombe Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- arch/x86/include/asm/tdx.h | 17 ++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 65 +++++++++++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index fa29be18498c..c39e2920d0c3 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -136,6 +136,23 @@ static inline bool tdx_supports_dynamic_pamt(const str= uct tdx_sys_info *sysinfo) return false; /* To be enabled when kernel is ready */ } =20 +/* Simple structure for pre-allocating Dynamic PAMT pages outside of locks= . */ +struct tdx_pamt_cache { + struct list_head page_list; + int cnt; +}; + +static inline void tdx_init_pamt_cache(struct tdx_pamt_cache *cache) +{ + INIT_LIST_HEAD(&cache->page_list); + cache->cnt =3D 0; +} + +void tdx_free_pamt_cache(struct tdx_pamt_cache *cache); +int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npage= s); +int tdx_pamt_get(struct page *page, struct tdx_pamt_cache *cache); +void tdx_pamt_put(struct page *page); + void tdx_quirk_reset_page(struct page *page); =20 int tdx_guest_keyid_alloc(void); diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index d333d2790913..53b29c827520 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -2064,13 +2064,34 @@ u64 tdh_phymem_page_wbinvd_hkid(u64 hkid, struct pa= ge *page) } EXPORT_SYMBOL_FOR_KVM(tdh_phymem_page_wbinvd_hkid); =20 -static int alloc_pamt_array(u64 *pa_array) +static struct page *tdx_alloc_page_pamt_cache(struct tdx_pamt_cache *cache) +{ + struct page *page; + + page =3D list_first_entry_or_null(&cache->page_list, struct page, lru); + if (page) { + list_del(&page->lru); + cache->cnt--; + } + + return page; +} + +static struct page *alloc_dpamt_page(struct tdx_pamt_cache *cache) +{ + if (cache) + return tdx_alloc_page_pamt_cache(cache); + + return alloc_page(GFP_KERNEL_ACCOUNT); +} + +static int alloc_pamt_array(u64 *pa_array, struct tdx_pamt_cache *cache) { struct page *page; int i; =20 for (i =3D 0; i < tdx_dpamt_entry_pages(); i++) { - page =3D alloc_page(GFP_KERNEL_ACCOUNT); + page =3D alloc_dpamt_page(cache); if (!page) goto err; pa_array[i] =3D page_to_phys(page); @@ -2151,7 +2172,7 @@ static u64 tdh_phymem_pamt_remove(struct page *page, = u64 *pamt_pa_array) static DEFINE_SPINLOCK(pamt_lock); =20 /* Bump PAMT refcount for the given page and allocate PAMT memory if neede= d */ -static int tdx_pamt_get(struct page *page) +int tdx_pamt_get(struct page *page, struct tdx_pamt_cache *cache) { u64 pamt_pa_array[MAX_NR_DPAMT_ARGS]; atomic_t *pamt_refcount; @@ -2170,7 +2191,7 @@ static int tdx_pamt_get(struct page *page) if (atomic_inc_not_zero(pamt_refcount)) return 0; =20 - ret =3D alloc_pamt_array(pamt_pa_array); + ret =3D alloc_pamt_array(pamt_pa_array, cache); if (ret) goto out_free; =20 @@ -2222,12 +2243,13 @@ static int tdx_pamt_get(struct page *page) free_pamt_array(pamt_pa_array); return ret; } +EXPORT_SYMBOL_FOR_KVM(tdx_pamt_get); =20 /* * Drop PAMT refcount for the given page and free PAMT memory if it is no * longer needed. */ -static void tdx_pamt_put(struct page *page) +void tdx_pamt_put(struct page *page) { u64 pamt_pa_array[MAX_NR_DPAMT_ARGS]; atomic_t *pamt_refcount; @@ -2281,6 +2303,37 @@ static void tdx_pamt_put(struct page *page) */ free_pamt_array(pamt_pa_array); } +EXPORT_SYMBOL_FOR_KVM(tdx_pamt_put); + +void tdx_free_pamt_cache(struct tdx_pamt_cache *cache) +{ + struct page *page; + + while ((page =3D tdx_alloc_page_pamt_cache(cache))) + __free_page(page); +} +EXPORT_SYMBOL_FOR_KVM(tdx_free_pamt_cache); + +int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npage= s) +{ + if (WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo))) + return 0; + + npages *=3D tdx_dpamt_entry_pages(); + + while (cache->cnt < npages) { + struct page *page =3D alloc_page(GFP_KERNEL_ACCOUNT); + + if (!page) + return -ENOMEM; + + list_add(&page->lru, &cache->page_list); + cache->cnt++; + } + + return 0; +} +EXPORT_SYMBOL_FOR_KVM(tdx_topup_pamt_cache); =20 /* * Return a page that can be gifted to the TDX-Module for use as a "contro= l" @@ -2296,7 +2349,7 @@ struct page *__tdx_alloc_control_page(gfp_t gfp) if (!page) return NULL; =20 - if (tdx_pamt_get(page)) { + if (tdx_pamt_get(page, NULL)) { __free_page(page); return NULL; } --=20 2.53.0.rc1.217.geba53bf80e-goog