From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439017; cv=none; d=zohomail.com; s=zohoarc; b=ffiLUdjgRGXshlVA0l2CwD7E6f3nLqiUEpfcCsg++aLwvw1KWiivzSnWCirhGn54ccYiWwn/XjRH6cEys/swWgwCjk1WXGop/bQX0G/Opdmy0KaF8KZM4FpOPkyAdu41lPlOh7D06QbFu+WQMGYdswgxQJ64xnMmaE6FGNQkbCk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439017; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ScatGeN79yCIkgIHLED9u5BvoQ34d9Lt73N7pOTAq9E=; b=ZSZBpau8RUqcTTlG26bF/LBGDIhpinpWeOODmK94Yr6iarAsyo7n54on8IDG5cKyfS/4gPHFo6UZMua+fAeyXqBiOWXvQ8/POV57N5OJFAUhLnHFdCWFYP1GL+INYaXA/c6L7FAQfKIg1pxujwiaEIQ7TJdKOD7clrOSEO6mJ6g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 17924390171451019.995377174832; Mon, 19 Oct 2026 12:43:37 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454762.1668274 (Exim 4.92) (envelope-from ) id 1xItGP-0005au-OH; Mon, 19 Oct 2026 19:43:17 +0000 Received: by outflank-mailman (output) from mailman id 1454762.1668274; Mon, 19 Oct 2026 19:43:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGP-0005an-Lj; Mon, 19 Oct 2026 19:43:17 +0000 Received: by outflank-mailman (input) for mailman id 1454762; Mon, 19 Oct 2026 19:43:16 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGO-0005OJ-9U for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:43:16 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItGO-000000022ji-1FsU; Mon, 19 Oct 2026 19:43:16 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGO-00000001FjN-06xx; Mon, 19 Oct 2026 19:43:16 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=ScatGeN79yCIkgIHLED9u5BvoQ34d9Lt73N7pOTAq9E=; b=o09OGX7ye6Ka7mG88fKyYBbouv OjSiR6qqlfZzWCTtuH8ustkHEh5nj3x5xVNi6+1Fdus/Fbs+Y47IEDbwxXo0UaGcVmaRX0b02aPCk VxAZO/OR711Si63DnpRDrZjOx3wNNCzKxeJuPqY1mF/HItHREh0iIk+OvdGlQ6iOGXLo=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 01/30] x86/PoD: map one page at a time in p2m_pod_zero_check() Date: Mon, 19 Oct 2026 20:42:32 +0100 Message-ID: <20261019-asi-hvm-v4-1-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439018168158500 Content-Type: text/plain; charset="utf-8" p2m_pod_zero_check() maps every page of its batch that passes its checks, up to POD_SWEEP_STRIDE (16) of them, and keeps them mapped across the p2m lookups and updates which follow, each of which maps page-table pages of its own. Its callers hold mappings too: the p2m lookup which found the entry to populate still has its page-table page mapped, and a PV mmu_update() the L1 page it is writing. map_domain_page() cannot always provide that many at once. It uses the mapcache on behalf of PV vCPUs only: in debug builds for every page, in release builds for pages beyond the reach of the directmap in PV guests' page-tables (5TiB, 3.5TiB with CONFIG_BIGMEM). The mapcache has MAPCACHE_VCPU_ENTRIES (16) slots per vCPU of the domain, shared by its vCPUs, and a mapping which finds no slot free or reclaimable hits the BUG_ON() in map_domain_page(): the host crashes. The sweep runs in the context of whichever vCPU populates a PoD entry while the domain's PoD cache is empty. The guest's own vCPUs, being HVM, map through the directmap; a PV vCPU gets there when its domain maps or copies the guest's memory: a device model in dom0 or in a stub domain, a backend doing grant copies. A PV domain with a single vCPU, as a device-model stub domain or a dom0 booted with dom0_max_vcpus=3D1 is, has 16 slots, and a sweep over 16 candidate pages needs more. The sweep has mapped its whole batch at once since 9ad9a1609fbe ("PoD memory 5/9: emergency scan"), when x86-64 reached all memory through the directmap. The per-vCPU budget came with the mapcache's return to x86-64 in 4b28bf6ae90b ("x86: re-introduce map_domain_page() et al"). Map one page at a time instead, as p2m_pod_zero_check_superpage() already does: map a page for the quick check and unmap it again, and map it anew for the full check after the p2m update and the TLB flush. The checks and their order are unchanged. Each page is mapped twice, which costs little next to scanning it, and the error paths no longer have mappings to undo. Fixes: 4b28bf6ae90b ("x86: re-introduce map_domain_page() et al") Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap Acked-by: Roger Pau Monn=C3=A9 --- Changes in v4: - Track the pages to check in a bitmask walked with for_each_set_bit(), not an array of bools (Andrew). - Drop the new comment above the first loop; edit the old one's "and map the pages" instead (Andrew). NB this issue was raised in review and verified only by code inspection; it hasn't been demonstrated. --- xen/arch/x86/mm/p2m-pod.c | 72 +++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/xen/arch/x86/mm/p2m-pod.c b/xen/arch/x86/mm/p2m-pod.c index 4602c32cff..ed298fa7d2 100644 --- a/xen/arch/x86/mm/p2m-pod.c +++ b/xen/arch/x86/mm/p2m-pod.c @@ -901,9 +901,10 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_t= *gfns, unsigned int count { mfn_t mfns[POD_SWEEP_STRIDE]; p2m_type_t types[POD_SWEEP_STRIDE]; - unsigned long *map[POD_SWEEP_STRIDE]; + unsigned int check =3D 0; + const unsigned long *map; struct domain *d =3D p2m->domain; - unsigned int i, j, max_ref =3D 1; + unsigned int j, max_ref =3D 1; =20 BUG_ON(count > POD_SWEEP_STRIDE); =20 @@ -911,8 +912,11 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_t= *gfns, unsigned int count if ( paging_mode_shadow(d) ) max_ref++; =20 - /* First, get the gfn list, translate to mfns, and map the pages. */ - for ( i =3D 0; i < count; i++ ) + /* + * First, get the gfn list, translate to mfns, and check for being a + * plausible candidate. + */ + for ( unsigned int i =3D 0; i < count; i++ ) { p2m_access_t a; =20 @@ -921,9 +925,8 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_t = *gfns, unsigned int count =20 /* * If this is ram, and not a pagetable or a special page, and - * probably not mapped elsewhere, map it; otherwise, skip. + * probably not mapped elsewhere, check it; otherwise, skip. */ - map[i] =3D NULL; if ( p2m_is_ram(types[i]) ) { const struct page_info *pg =3D mfn_to_page(mfns[i]); @@ -932,7 +935,7 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_t = *gfns, unsigned int count (pg->count_info & PGC_allocated) && !(pg->count_info & PGC_shadowed_pt) && ((pg->count_info & PGC_count_mask) <=3D max_ref) ) - map[i] =3D map_domain_page(mfns[i]); + check |=3D 1U << i; } } =20 @@ -940,20 +943,23 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_= t *gfns, unsigned int count * Then, go through and check for zeroed pages, removing write permiss= ion * for those with zeroes. */ - for ( i =3D 0; i < count; i++ ) + for_each_set_bit ( i, check ) { - if ( !map[i] ) - continue; - /* Quick zero-check */ + map =3D map_domain_page(mfns[i]); for ( j =3D 0; j < 16; j++ ) - if ( *(map[i] + j) !=3D 0 ) - goto skip; + if ( map[j] !=3D 0 ) + break; + unmap_domain_page(map); =20 /* Try to remove the page, restoring old mapping if it fails. */ - if ( p2m_set_entry(p2m, gfns[i], INVALID_MFN, PAGE_ORDER_4K, + if ( j < 16 || + p2m_set_entry(p2m, gfns[i], INVALID_MFN, PAGE_ORDER_4K, p2m_populate_on_demand, p2m->default_access) ) - goto skip; + { + check &=3D ~(1U << i); + continue; + } =20 /* * See if the page was successfully unmapped. (Allow one refcount @@ -961,6 +967,8 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_t = *gfns, unsigned int count */ if ( (mfn_to_page(mfns[i])->count_info & PGC_count_mask) > 1 ) { + check &=3D ~(1U << i); + /* * If the previous p2m_set_entry call succeeded, this one shou= ldn't * be able to fail. If it does, crashing the domain should be= safe. @@ -970,38 +978,27 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_= t *gfns, unsigned int count { ASSERT_UNREACHABLE(); domain_crash(d); - goto out_unmap; + return; } - - skip: - unmap_domain_page(map[i]); - map[i] =3D NULL; - - continue; } } =20 p2m_tlb_flush_sync(p2m); =20 /* Now check each page for real */ - for ( i =3D 0; i < count; i++ ) + for_each_set_bit ( i, check ) { - if ( !map[i] ) - continue; - - for ( j =3D 0; j < (PAGE_SIZE / sizeof(*map[i])); j++ ) - if ( *(map[i] + j) !=3D 0 ) + map =3D map_domain_page(mfns[i]); + for ( j =3D 0; j < (PAGE_SIZE / sizeof(*map)); j++ ) + if ( map[j] !=3D 0 ) break; - - unmap_domain_page(map[i]); - - map[i] =3D NULL; + unmap_domain_page(map); =20 /* * See comment in p2m_pod_zero_check_superpage() re gnttab * check timing. */ - if ( j < (PAGE_SIZE / sizeof(*map[i])) ) + if ( j < (PAGE_SIZE / sizeof(*map)) ) { /* * If the previous p2m_set_entry call succeeded, this one shou= ldn't @@ -1012,14 +1009,7 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn= _t *gfns, unsigned int count { ASSERT_UNREACHABLE(); domain_crash(d); - out_unmap: - /* - * Something went wrong, probably crashing the domain. Un= map - * everything and return. - */ - for ( i =3D 0; i < count; i++ ) - if ( map[i] ) - unmap_domain_page(map[i]); + return; } } else --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439025; cv=none; d=zohomail.com; s=zohoarc; b=WrBH7muSv53vFzx9FY/9QAHN1QT0R4OtiYh9sbeotxPw+DDONZykKYmhr4bKU32rs+xcP+0X/t9ENXRQM7dkeuQqQ0fRyB9N3+CXDQO8vVk3JAreKqgIJXxj3iXXqINpZyUFhy5WSfK7r/D26Uvv75rHfaKIkTp9u+3MMkrypJk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439025; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=jkPIl+dPP6zf0AQ7LteorT5djg7uxPkCmb8aNhe84Jw=; b=RaP1CCAwtfjocbsgzr+k0XQ+TRlVyWCbh/7MdMlv+JQMBnF+oIWn3dEYOo41MrBePa1kdPvNrGlU8dZep2+VAdj98vyXv3pxiguQ9Kgs1sSnMr+dwiMbnE6UDlcbEQZHFGF6Oz043QyzdJzoPi63+5nLBpXMGHkpTWPet0QsCSk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439024921421.5148626861038; Mon, 19 Oct 2026 12:43:44 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454771.1668338 (Exim 4.92) (envelope-from ) id 1xItGd-0007O4-8l; Mon, 19 Oct 2026 19:43:31 +0000 Received: by outflank-mailman (output) from mailman id 1454771.1668338; Mon, 19 Oct 2026 19:43:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGd-0007N6-0Q; Mon, 19 Oct 2026 19:43:31 +0000 Received: by outflank-mailman (input) for mailman id 1454771; Mon, 19 Oct 2026 19:43:29 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGb-00077Q-Er for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:43:29 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItGP-000000022jq-0zl8; Mon, 19 Oct 2026 19:43:17 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGO-00000001FjN-3zHt; Mon, 19 Oct 2026 19:43:16 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=jkPIl+dPP6zf0AQ7LteorT5djg7uxPkCmb8aNhe84Jw=; b=iM/SmPKHE2sQAwkaLYZYQY8srW Uql64ogIv6tHXYC80tTTUHJOBSFxpWCMW7dAmPFWqyOVS2WZrhZp9PGZMM0CUAs1imQ6VpxIyFmM6 z19rNIidUMjzJ3bIYsxC/GfScRFiiB5EHAR2cTDmfZASr1jEtw2JjJH5bbPzkyAAYXfo=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 02/30] x86/mm: refuse to promote an L4 while the domain has no vCPU Date: Mon, 19 Oct 2026 20:42:33 +0100 Message-ID: <20261019-asi-hvm-v4-2-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439026285158500 Content-Type: text/plain; charset="utf-8" init_xen_l4_slots() puts the domain's per-domain L3 in slot 260 of the L4s it fills. Since fcf91ce9de66 ("x86/pv: don't populate the GDT/LDT L3 slot at domain creation") a PV domain gets that L3 with its first vCPU (pv_create_gdt_ldt_l1tab()). Only mapcache_domain_init() makes it earlier, with the domain, and release builds skip that unless memory hotplug is enabled or the host's memory reaches past the part of the directmap PV guests can see. So in release builds on most hosts, an L4 promoted before XEN_DOMCTL_max_vcpus has created the domain's vCPUs gets a slot 260 entry made from a NULL page (0x803e900000000063), pointing far beyond the host's memory. Debug builds happen to have the L3 that early. Refuse such an L4 in every build, so that there is one rule: promote_l4_table() returns -EINVAL while the domain has no vCPU 0, before taking any reference. libxl is unaffected: it creates the vCPUs before it builds or restores a domain. There's no way to trigger the issue from the current toolstack; this is just belt-and-braces. Fixes: fcf91ce9de66 ("x86/pv: don't populate the GDT/LDT L3 slot at domain = creation") Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - New in this version, split out of "x86/hvm: introduce per-vCPU L3 page-table": promote_l4_table() fails while the domain has no vCPU --- xen/arch/x86/mm.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index e42cd33c0d..3f6f9f3bd4 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -1798,11 +1798,21 @@ static int promote_l4_table(struct page_info *page) { struct domain *d =3D page_get_owner(page); mfn_t l4mfn =3D page_to_mfn(page); - l4_pgentry_t *pl4e =3D map_domain_page(l4mfn); + l4_pgentry_t *pl4e; unsigned int i; int rc =3D 0; unsigned int partial_flags =3D page->partial_flags; =20 + /* + * init_xen_l4_slots() puts the domain's per-domain L3 in slot 260, an= d a + * PV domain may get that only with its first vCPU. The domain has no + * vCPUs before XEN_DOMCTL_max_vcpus. + */ + if ( !domain_vcpu(d, 0) ) + return -EINVAL; + + pl4e =3D map_domain_page(l4mfn); + for ( i =3D page->nr_validated_ptes; i < L4_PAGETABLE_ENTRIES; i++, partial_flags =3D 0 ) { --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439016; cv=none; d=zohomail.com; s=zohoarc; b=ntAB7e0TgdTApHhTc2gXAtSgSvXPtDUV11MgL6ZXoUx9FgrsMERJ26LNknMHQpj2Msye6ms4M2PalrjJ6O4rtNPUAVyiVsrVEBXi/mTHONXKRbrYDGanoa40K8cREXqSZz+o1beT5OMOoeWaHhmB5s1qLTIt22rJ+7xdeAD1DCQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439016; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=gVZuKX9SYj+BXemwpZoxl0keumPErYRZAVZ10RQNwMQ=; b=HRjgJcMvRmJQOe8AWqMhC+6DXhmnDJrbtwhIbYSVj+jFr4tV3Aqs1fdQDteRO+QXAndsprUAShJ3RAi0nz8R4BDk6CLnq5Eo4oFMwAXXiR5KHJBzTBjcfU/dJxsgO5suslkp6Bi/GxQRvJnN9mqGeFk8E3mcnQC4YizNjTMfKxc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439016766472.82195734582547; Mon, 19 Oct 2026 12:43:36 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454763.1668282 (Exim 4.92) (envelope-from ) id 1xItGR-0005oG-Vg; Mon, 19 Oct 2026 19:43:19 +0000 Received: by outflank-mailman (output) from mailman id 1454763.1668282; Mon, 19 Oct 2026 19:43:19 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGR-0005o9-SZ; Mon, 19 Oct 2026 19:43:19 +0000 Received: by outflank-mailman (input) for mailman id 1454763; Mon, 19 Oct 2026 19:43:18 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGQ-0005l1-Fu for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:43:18 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItGQ-000000022js-0xCB; Mon, 19 Oct 2026 19:43:18 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGP-00000001FjN-44FX; Mon, 19 Oct 2026 19:43:17 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=gVZuKX9SYj+BXemwpZoxl0keumPErYRZAVZ10RQNwMQ=; b=4ypTgErHnSe8mBjfFUTfHIqJzn yeBSQhXXpoISNR4eZo5O2S6gT26l9DBcKRYRigqNwuBekiz36jrdZwxkShvoBwcU1G3ZEdcvv3LCQ dt1jBQ2zrsODVGcUWADvQgDPxGR/rbX5ddUeajgTyvFpthdoczv0DyjaxxrNtogg8owU=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 03/30] x86/mm: purge unneeded destroy_perdomain_mapping() Date: Mon, 19 Oct 2026 20:42:34 +0100 Message-ID: <20261019-asi-hvm-v4-3-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439018129158500 From: Roger Pau Monn=C3=A9 We want to change per-domain mappings to be per-vCPU mappings. In preparation for that, we want to arrange that destroy_perdomain_mapping() work either with a single perdomain area, or with a per-vCPU perdomain area. There are two calls made from domain-scoped contexts; both calls turn out to be unnecessary: - destroy_perdomain_mapping() is not logically the undo of create_perdomain_mapping(), as the name and its use in hvm_domain_initialise() suggest. create_ allocates a per-domain L3, but destroy_ tears down mappings without freeing it; and since the call here passes nr =3D=3D 0, it tears down nothing at all. The per-domain L3 page is actually freed by free_perdomain_mappings(), which hvm_domain_initialise()'s caller, arch_domain_create(), already invokes on its failure path. - The call in pv_domain_destroy() is redundant at both of its call sites. arch_domain_destroy() unconditionally calls free_perdomain_mappings(), which tears down the same entries and additionally frees the page-table structures; and the other caller, the error path of pv_domain_initialise(), returns into arch_domain_create()'s failure path, which invokes free_perdomain_mappings() as well. One vCPU-scoped caller, pv_destroy_gdt_ldt_l1tab(), is covered the same way: a vCPU is only ever destroyed together with its domain, or on the failure path of creating it, and both routes end in free_perdomain_mappings(); the GDT/LDT slots hold no area-owned pages, so there is nothing else for the call to do. While we're getting rid of unneccessary callers, drop this one too. Note that free_compat_arg_xlat() keeps its explicit teardown: switch_compat() calls it on the failure path. While in practice most toolstacks will immediately destroy the domain, in theory a toolstack could let it live on as a 64-bit guest. Release the resources immediately rather than waiting for domain destruction. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5 Signed-off-by: George Dunlap Reviewed-by: Jan Beulich --- Changes in v4: - Remove an unnecessary comment (Jan). Changes in v3: - Note that pv_domain_destroy()'s other call site, the error path of pv_domain_initialise(), is covered the same way (Jan). - Also drop pv_destroy_gdt_ldt_l1tab(), the per-vCPU teardown call (Jan), and comment on why free_compat_arg_xlat() keeps its own. The Reviewed-by tags given for the previous form are dropped. Changes in v2: - Added to the series Changes since the previously posted version: - Reworked the commit message to make it more clear how it fits in with the larger series. No functional change. --- xen/arch/x86/hvm/hvm.c | 1 - xen/arch/x86/pv/domain.c | 10 ---------- 2 files changed, 11 deletions(-) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index ef9c7cd057..6aefe91e02 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -730,7 +730,6 @@ int hvm_domain_initialise(struct domain *d, XFREE(d->arch.hvm.irq); fail0: hvm_destroy_cacheattr_region_list(d); - destroy_perdomain_mapping(d, PERDOMAIN_VIRT_START, 0); fail: hvm_domain_relinquish_resources(d); XFREE(d->arch.hvm.io_handler); diff --git a/xen/arch/x86/pv/domain.c b/xen/arch/x86/pv/domain.c index 0c42ae58aa..28ea0742ab 100644 --- a/xen/arch/x86/pv/domain.c +++ b/xen/arch/x86/pv/domain.c @@ -319,12 +319,6 @@ static int pv_create_gdt_ldt_l1tab(struct vcpu *v) NULL); } =20 -static void pv_destroy_gdt_ldt_l1tab(struct vcpu *v) -{ - destroy_perdomain_mapping(v->domain, GDT_VIRT_START(v), - 1U << GDT_LDT_VCPU_SHIFT); -} - void pv_vcpu_destroy(struct vcpu *v) { if ( is_pv_32bit_vcpu(v) ) @@ -333,7 +327,6 @@ void pv_vcpu_destroy(struct vcpu *v) release_compat_l4(v); } =20 - pv_destroy_gdt_ldt_l1tab(v); XFREE(v->arch.pv.trap_ctxt); } =20 @@ -381,9 +374,6 @@ void pv_domain_destroy(struct domain *d) { pv_l1tf_domain_destroy(d); =20 - destroy_perdomain_mapping(d, GDT_LDT_VIRT_START, - GDT_LDT_MBYTES << (20 - PAGE_SHIFT)); - XFREE(d->arch.pv.cpuidmasks); =20 FREE_XENHEAP_PAGE(d->arch.pv.gdt_ldt_l1tab); --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439021; cv=none; d=zohomail.com; s=zohoarc; b=RV9fof8Kg3iqalgY9y21rFkwpRAdUahOxsrVZ58dThRCml+a9RWcm71vVzJ5HCN0WkIzz6ETO7eoSja+AxGdczeUcRQP3Pc3z43wrUYmX2Ri/0sqRj2As04T9cDse/b9MZfjsa1NvrFkTlTzuZPZvoQkoPk6bLNiwmenr0Vr0kM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439021; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=8+JAad6tubYajhSY57aMWHsaXENtbq4fiWg5qfzI9E4=; b=aCspFwYds6XWHMsYffl+uaa/nyJEB8a47upxIBvB7hcAITnp+wi9CqZ3KqrTd8gnWNc0dl/uVud/IPgsfbc+fvanKHs/5WgSsk0e1OrJzeNVzae7HOF7Rfh7jaqVmLMA4i9bhYGVSI7ZresHchxF6O8gDk5dP75r/EsN4He/Of8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439021273838.7581736529016; Mon, 19 Oct 2026 12:43:41 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454764.1668292 (Exim 4.92) (envelope-from ) id 1xItGT-00061V-7E; Mon, 19 Oct 2026 19:43:21 +0000 Received: by outflank-mailman (output) from mailman id 1454764.1668292; Mon, 19 Oct 2026 19:43:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGT-00061L-3K; Mon, 19 Oct 2026 19:43:21 +0000 Received: by outflank-mailman (input) for mailman id 1454764; Mon, 19 Oct 2026 19:43:19 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGR-0005o0-QB for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:43:19 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItGR-000000022k0-1GbZ; Mon, 19 Oct 2026 19:43:19 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGQ-00000001FjN-4BPF; Mon, 19 Oct 2026 19:43:19 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=8+JAad6tubYajhSY57aMWHsaXENtbq4fiWg5qfzI9E4=; b=iBtYW3U07XnDEU+FpkZ9lFj8sO SUE+I3OGT26j8xHUkuLNvYpPYRDaBz3+FlqzmEmIgXvkGq4khwO4sdoTXqloJM1T0/9TTJw2VORJ9 059A/Ktsj8ue7Nlq34kGLozlKegxNA/CkRr5FvHhA/+L4RNKVEM51VT3l1Cj/hVyAm/s=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , Teddy Astie Subject: [PATCH v4 04/30] x86/mm: prepare destroy_perdomain_mapping() for per-vCPU perdomain areas Date: Mon, 19 Oct 2026 20:42:35 +0100 Message-ID: <20261019-asi-hvm-v4-4-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439022128158500 From: Roger Pau Monn=C3=A9 We want to change per-domain mappings to be per-vCPU mappings. In preparation for that, we want to arrange that destroy_perdomain_mapping() work either with a single perdomain area, or with a per-vCPU perdomain area. The one remaining caller, free_compat_arg_xlat(), is already in a vCPU context, so we just need to change the parameter from a domain pointer to a vCPU pointer. No functional change. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap Reviewed-by: Roger Pau Monn=C3=A9 --- Changes in v4: - Say "No functional change." (Roger). Changes in v3: - Rebased onto staging without the GDT/LDT conversions (for the HVM-only series): free_compat_arg_xlat() is the only caller left. - Drop the linear-page-table fast path altogether (Jan): its only user would have been guest-driven LDT teardown, and without it the function does not depend on the linear mapping being available. Changes in v2: - Added to the series Changes since the previously posted version: - The fast path is gone (v3), and with it the previously posted version's sync_local_execstate() call. - Reword and retitle for clarity (was: "x86/mm: switch destroy_perdomain_mapping() parameter from domain to vCPU"). --- xen/arch/x86/include/asm/mm.h | 2 +- xen/arch/x86/mm.c | 3 ++- xen/arch/x86/x86_64/mm.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index aa515efbee..20a1d27cd0 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -609,7 +609,7 @@ int compat_arch_memory_op(unsigned long cmd, XEN_GUEST_= HANDLE_PARAM(void) arg); int create_perdomain_mapping(struct domain *d, unsigned long va, unsigned int nr, l1_pgentry_t **pl1tab, struct page_info **ppg); -void destroy_perdomain_mapping(struct domain *d, unsigned long va, +void destroy_perdomain_mapping(const struct vcpu *v, unsigned long va, unsigned int nr); void free_perdomain_mappings(struct domain *d); =20 diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 3f6f9f3bd4..70b4cc87ae 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6361,10 +6361,11 @@ int create_perdomain_mapping(struct domain *d, unsi= gned long va, return rc; } =20 -void destroy_perdomain_mapping(struct domain *d, unsigned long va, +void destroy_perdomain_mapping(const struct vcpu *v, unsigned long va, unsigned int nr) { const l3_pgentry_t *l3tab, *pl3e; + const struct domain *d =3D v->domain; =20 ASSERT(va >=3D PERDOMAIN_VIRT_START && va < PERDOMAIN_VIRT_SLOT(PERDOMAIN_SLOTS)); diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c index 8eadab7933..f467ed426b 100644 --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -739,7 +739,7 @@ int setup_compat_arg_xlat(struct vcpu *v) =20 void free_compat_arg_xlat(struct vcpu *v) { - destroy_perdomain_mapping(v->domain, ARG_XLAT_START(v), + destroy_perdomain_mapping(v, ARG_XLAT_START(v), PFN_UP(COMPAT_ARG_XLAT_SIZE)); } =20 --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439018; cv=none; d=zohomail.com; s=zohoarc; b=LNLDVgu6zfFmUCZ8qkfql5NeGLKMy7OnxMQ7A3xlFhOLO04O7d7E7IwIQFd64xTiVO/8E1xTA3NCm+w1koLVUlCVEh9FqEgCz2tkz4ezAAF56XDUSRZo5GssDYdiajE4eIimG3wp6bYDwP/fZNxNNmqBujPwKe2w4iZUmCouJoM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439018; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=2WAx232e1o23xbVvwluUdMmJZVaoZJGamK8uLinVDPU=; b=H2eWkHaBSw5jB7huhSJPxn6dJ7S8dcPSZ3aIa1BaGrFC1cA8gHEelu20pWeIfrN9sm6qOZ8cAiHtX2KWW92U0VAc3/olCiJgIAHVTpQc/XI0Td9ogZWnXimzXocJja6XsgAwlzq8Oml8z3DbS1MzIUjbfm9uaO3kMv255tbAbdI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439018162450.3932466196745; Mon, 19 Oct 2026 12:43:38 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454765.1668298 (Exim 4.92) (envelope-from ) id 1xItGT-00064h-Jk; Mon, 19 Oct 2026 19:43:21 +0000 Received: by outflank-mailman (output) from mailman id 1454765.1668298; Mon, 19 Oct 2026 19:43:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGT-000641-BK; Mon, 19 Oct 2026 19:43:21 +0000 Received: by outflank-mailman (input) for mailman id 1454765; Mon, 19 Oct 2026 19:43:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGS-0005ur-CS for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:43:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItGS-000000022k8-1ErZ; Mon, 19 Oct 2026 19:43:20 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGS-00000001FjN-09tM; Mon, 19 Oct 2026 19:43:20 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=2WAx232e1o23xbVvwluUdMmJZVaoZJGamK8uLinVDPU=; b=NKMK8/Ki0ezCIEAlL7cZwz7gyp L1u3bm3PXbOvQ0ed6wuwklEkss95YOhTORtDceTntsIwdGFHOWnNrzsjVee65HkFrWdU1zKXbZiii T3q0KLx2NkVAVHH23HsPaP8wXRx7H+TW3czd75oZqrCbwv2g4IODQ/E+diO1FL4xnh2E=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , Teddy Astie Subject: [PATCH v4 05/30] x86/domain_page: drop redundant create_perdomain_mapping() call Date: Mon, 19 Oct 2026 20:42:36 +0100 Message-ID: <20261019-asi-hvm-v4-5-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439020149158500 From: Roger Pau Monn=C3=A9 We want to change per-domain mappings to be per-vCPU mappings. In preparation for that, we want to arrange that create_perdomain_mapping() work either with a single perdomain area, or with a per-vCPU perdomain area. One of the current calls in a domain context turns out to be unnecessary: mapcache_domain_init() pre-plumbs L1 tables over the whole inuse/garbage bitmap range (sized for the full MAPCACHE_ENTRIES capacity), without populating any data pages. The plumbing is redundant: mapcache_vcpu_init() installs the bitmap pages the domain will actually use with populating calls, which allocate any missing page-table structure on demand -- and since d->max_vcpus is fixed before any vCPU is created, the range those calls cover never grows. The pre-plumbed tail beyond it backs virtual addresses that are never populated at all. In debug builds, and on hosts with memory hotplug or with more memory than the PV directmap covers, the call was also what first allocated a PV domain's per-domain L3. Release builds on other hosts already allocated it only with the first vCPU. Drop the call; now we always allocate only with the first vCPU. With the only fallible operation gone, mapcache_domain_init() becomes void, and arch_domain_create()'s error handling for it goes away. The per-domain-mapping work in mapcache_vcpu_init() stays where it is rather than moving into mapcache_domain_init() along with this change, ready for a later patch which introduces per-vCPU mapcaches. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap Reviewed-by: Roger Pau Monn=C3=A9 --- Changes in v3: - Rebased onto staging without the GDT/LDT conversions (for the HVM-only series); no functional difference. - Say why the per-vCPU part stays in mapcache_vcpu_init() (Jan). - Say that the call also first allocated a PV domain's per-domain L3 in some builds, which all builds now do with the first vCPU. Changes in v2: - Added to the series (split out of the following patch). Changes since the previously posted version: - Split out of "x86/mm: switch {create,destroy}_perdomain_mapping() domain parameter to vCPU", where the removal was folded into the parameter switch without its own rationale. --- xen/arch/x86/domain.c | 3 +-- xen/arch/x86/domain_page.c | 8 ++------ xen/arch/x86/include/asm/domain.h | 2 +- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 368e4deff3..8e17fbe6d9 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -908,8 +908,7 @@ int arch_domain_create(struct domain *d, } else if ( is_pv_domain(d) ) { - if ( (rc =3D mapcache_domain_init(d)) !=3D 0 ) - goto fail; + mapcache_domain_init(d); =20 if ( (rc =3D pv_domain_initialise(d)) !=3D 0 ) goto fail; diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 5744754f11..f02be1675b 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -223,7 +223,7 @@ void unmap_domain_page(const void *ptr) local_irq_restore(flags); } =20 -int mapcache_domain_init(struct domain *d) +void mapcache_domain_init(struct domain *d) { struct mapcache_domain *dcache =3D &d->arch.pv.mapcache; unsigned int bitmap_pages; @@ -232,7 +232,7 @@ int mapcache_domain_init(struct domain *d) =20 #ifdef NDEBUG if ( !mem_hotplug && max_page <=3D PFN_DOWN(__pa(HYPERVISOR_VIRT_END -= 1)) ) - return 0; + return; #endif =20 BUILD_BUG_ON(MAPCACHE_VIRT_END + PAGE_SIZE * (3 + @@ -244,10 +244,6 @@ int mapcache_domain_init(struct domain *d) (bitmap_pages + 1) * PAGE_SIZE / sizeof(long); =20 spin_lock_init(&dcache->lock); - - return create_perdomain_mapping(d, (unsigned long)dcache->inuse, - 2 * bitmap_pages + 1, - NIL(l1_pgentry_t *), NULL); } =20 int mapcache_vcpu_init(struct vcpu *v) diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 2d0a915410..212daae1f5 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -89,7 +89,7 @@ struct mapcache_domain { unsigned long *garbage; }; =20 -int mapcache_domain_init(struct domain *d); +void mapcache_domain_init(struct domain *d); int mapcache_vcpu_init(struct vcpu *v); =20 /* x86/64: toggle guest between kernel and user modes. */ --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439024; cv=none; d=zohomail.com; s=zohoarc; b=S6h63WHNylTpURDwTEV5Jx0Z89TpBaJz8X1UsGL7+V4nbM9R0CGjFZm9BhBpxH2va/vcWJQDjOEM63HbdgtiTPFtV+R7sLMX1vY24saXsHlM85rZCU1UzRloSd4ACwvRURcSJOCp4U95Pj0d0qUKPo22+HM9/HfdGRxEUL3d328= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439024; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=fz3Khl+YQ3OVqb+Nyg7wjka6rX6rxBEMbOvfyB10Kfg=; b=T2sOg/m2XAi0IQ6IuSikh329JLTymVu1jh32glT17+Y5qdCk/DnKUsRt66MI9YNehXh7MadXmiam+e+Hs/UeGM1rcCCtDHeTjJxM625w7+1HyePFRjAgTvivKCq0sNkYVO4OZ2d+ARtYicWIHHNPI3GMwxrLxj43quketct/u2U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179243902413262.80787869414928; Mon, 19 Oct 2026 12:43:44 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454766.1668311 (Exim 4.92) (envelope-from ) id 1xItGW-0006VP-0B; Mon, 19 Oct 2026 19:43:24 +0000 Received: by outflank-mailman (output) from mailman id 1454766.1668311; Mon, 19 Oct 2026 19:43:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGV-0006VA-Ra; Mon, 19 Oct 2026 19:43:23 +0000 Received: by outflank-mailman (input) for mailman id 1454766; Mon, 19 Oct 2026 19:43:22 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGU-0006Ij-44 for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:43:22 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItGT-000000022kG-1M3w; Mon, 19 Oct 2026 19:43:21 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGT-00000001FjN-0Hsu; Mon, 19 Oct 2026 19:43:21 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=fz3Khl+YQ3OVqb+Nyg7wjka6rX6rxBEMbOvfyB10Kfg=; b=XJczJ526bG6HuY812+loa90tB5 1+6oWDUmNLoiFcFoAOCKbpEJHt9ToG1Zsw2QL3K41BIXSklS0npnpiYLzQYsjKA6wLK5Zs8xIvjJz lSiqjdV8qsRqziu5pRJozUoYUTL/QUS4MdWQBpUvjSBW7byQDAbRQL/PtBqti0zZJx7o=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 06/30] x86/mm: prepare create_perdomain_mapping() for per-vCPU perdomain areas Date: Mon, 19 Oct 2026 20:42:37 +0100 Message-ID: <20261019-asi-hvm-v4-6-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439026352158500 From: Roger Pau Monn=C3=A9 We want to change per-domain mappings to be per-vCPU mappings. In preparation for that, we want to arrange that create_perdomain_mapping() work either with a single perdomain area, or with a per-vCPU perdomain area. Most of the remaining callers are already in a vCPU context. This is no accident: the perdomain area has always been laid out in per-vCPU slices -- each vCPU has its own GDT/LDT window, its own COMPAT_ARG_XLAT pages, its own window of mapcache entries -- and each vCPU's slice is set up as that vCPU is created. For these callers, we just need to change the parameter from a domain pointer to a vCPU pointer. Once the perdomain area itself becomes per-vCPU, the same calls will populate the owning vCPU's own area rather than slices of a shared one. One exception is the call in hvm_domain_initialise(). An HVM vCPU's monitor table is created during vCPU initialisation, and init_xen_l4_slots() stamps the perdomain slot into it at that point -- far earlier than for PV, where the Xen slots are written only once guest page tables are built. hvm_domain_initialise() therefore had an explicit create_perdomain_mapping() call just to make the perdomain root exist ahead of that. Move it to arch_vcpu_create(), covering HVM and PV alike. With a single shared area, the call allocates at most once per domain; but once each vCPU has its own perdomain area, this is the call that will allocate every vCPU's root before any page tables referencing it are built. For PV guests nothing observable changes: the root was already being created during vCPU creation as a side effect (by mapcache_vcpu_init(), or failing that pv_create_gdt_ldt_l1tab()); it now merely becomes explicit. Note that we cannot yet do a parallel movement of free_perdomain_mappings(): the per-domain page-table hierarchy is still a single domain-wide structure shared by all vCPUs, so tearing it down from a per-vCPU path would pull the mappings out from under sibling vCPUs (e.g. on a partially failed, retryable XEN_DOMCTL_max_vcpus), and vCPU-create error paths can rely on domain destruction to free a partially set up hierarchy. Once a domain's area is per-vCPU, a later change gives its vCPUs a teardown of their own; the domain-wide teardown stays for domains with a shared area, PV ones included. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap Reviewed-by: Jan Beulich --- Changes in v4: - Message: drop a sentence repeating its paragraph, and say that the domain-wide teardown stays for domains with a shared area (Roger). - End the comment in arch_vcpu_create() at the colon (Roger). Changes in v3: - Rebased onto staging without the GDT/LDT conversions (for the HVM-only series): the capture parameters stay, as the PV GDT/LDT stash still uses them. Changes in v2: - Added to the series Changes since the previously posted version: - Keep free_perdomain_mappings() (and hence perdomain teardown) domain-scoped. - Keep the idle domain without a perdomain area. - Retitle (was: "x86/mm: switch {create,destroy}_perdomain_mapping() domain parameter to vCPU"); destroy_perdomain_mapping() was switched in a separate patch. - Split the removal of mapcache_domain_init()'s redundant create_perdomain_mapping() call into its own (preceding) patch. --- xen/arch/x86/domain.c | 8 ++++++++ xen/arch/x86/domain_page.c | 6 +++--- xen/arch/x86/hvm/hvm.c | 5 ----- xen/arch/x86/include/asm/mm.h | 2 +- xen/arch/x86/mm.c | 3 ++- xen/arch/x86/pv/domain.c | 2 +- xen/arch/x86/x86_64/mm.c | 2 +- 7 files changed, 16 insertions(+), 12 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 8e17fbe6d9..94a1ea904a 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -517,6 +517,14 @@ int arch_vcpu_create(struct vcpu *v) =20 if ( !is_idle_domain(d) ) { + /* + * Make sure the per-domain L3 exists ahead of any consumer (e.g. + * init_xen_l4_slots() for the HVM monitor tables). + */ + rc =3D create_perdomain_mapping(v, PERDOMAIN_VIRT_START, 0, NULL, = NULL); + if ( rc ) + return rc; + paging_vcpu_init(v); =20 if ( (rc =3D vcpu_init_fpu(v)) !=3D 0 ) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index f02be1675b..f3e65e2d44 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -260,15 +260,15 @@ int mapcache_vcpu_init(struct vcpu *v) if ( ents > dcache->entries ) { /* Populate page tables. */ - int rc =3D create_perdomain_mapping(d, MAPCACHE_VIRT_START, ents, + int rc =3D create_perdomain_mapping(v, MAPCACHE_VIRT_START, ents, NIL(l1_pgentry_t *), NULL); =20 /* Populate bit maps. */ if ( !rc ) - rc =3D create_perdomain_mapping(d, (unsigned long)dcache->inus= e, + rc =3D create_perdomain_mapping(v, (unsigned long)dcache->inus= e, nr, NULL, NIL(struct page_info *= )); if ( !rc ) - rc =3D create_perdomain_mapping(d, (unsigned long)dcache->garb= age, + rc =3D create_perdomain_mapping(v, (unsigned long)dcache->garb= age, nr, NULL, NIL(struct page_info *= )); =20 if ( rc ) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 6aefe91e02..71da15c0a4 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -620,10 +620,6 @@ int hvm_domain_initialise(struct domain *d, INIT_LIST_HEAD(&d->arch.hvm.mmcfg_regions); INIT_LIST_HEAD(&d->arch.hvm.msix_tables); =20 - rc =3D create_perdomain_mapping(d, PERDOMAIN_VIRT_START, 0, NULL, NULL= ); - if ( rc ) - goto fail; - hvm_init_cacheattr_region_list(d); =20 rc =3D paging_enable(d, PG_refcounts|PG_translate|PG_external); @@ -730,7 +726,6 @@ int hvm_domain_initialise(struct domain *d, XFREE(d->arch.hvm.irq); fail0: hvm_destroy_cacheattr_region_list(d); - fail: hvm_domain_relinquish_resources(d); XFREE(d->arch.hvm.io_handler); XFREE(d->arch.hvm.pl_time); diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index 20a1d27cd0..c9085214fd 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -606,7 +606,7 @@ int compat_arch_memory_op(unsigned long cmd, XEN_GUEST_= HANDLE_PARAM(void) arg); #define NIL(type) ((type *)-sizeof(type)) #define IS_NIL(ptr) (!((uintptr_t)(ptr) + sizeof(*(ptr)))) =20 -int create_perdomain_mapping(struct domain *d, unsigned long va, +int create_perdomain_mapping(struct vcpu *v, unsigned long va, unsigned int nr, l1_pgentry_t **pl1tab, struct page_info **ppg); void destroy_perdomain_mapping(const struct vcpu *v, unsigned long va, diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 70b4cc87ae..c6bd53354b 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6238,10 +6238,11 @@ static bool perdomain_l1e_needs_freeing(l1_pgentry_= t l1e) (_PAGE_PRESENT | _PAGE_AVAIL0); } =20 -int create_perdomain_mapping(struct domain *d, unsigned long va, +int create_perdomain_mapping(struct vcpu *v, unsigned long va, unsigned int nr, l1_pgentry_t **pl1tab, struct page_info **ppg) { + struct domain *d =3D v->domain; struct page_info *pg; l3_pgentry_t *l3tab; l2_pgentry_t *l2tab; diff --git a/xen/arch/x86/pv/domain.c b/xen/arch/x86/pv/domain.c index 28ea0742ab..f3c711b85b 100644 --- a/xen/arch/x86/pv/domain.c +++ b/xen/arch/x86/pv/domain.c @@ -313,7 +313,7 @@ int switch_compat(struct domain *d) =20 static int pv_create_gdt_ldt_l1tab(struct vcpu *v) { - return create_perdomain_mapping(v->domain, GDT_VIRT_START(v), + return create_perdomain_mapping(v, GDT_VIRT_START(v), 1U << GDT_LDT_VCPU_SHIFT, v->domain->arch.pv.gdt_ldt_l1tab, NULL); diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c index f467ed426b..7b04537066 100644 --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -732,7 +732,7 @@ void __init zap_low_mappings(void) =20 int setup_compat_arg_xlat(struct vcpu *v) { - return create_perdomain_mapping(v->domain, ARG_XLAT_START(v), + return create_perdomain_mapping(v, ARG_XLAT_START(v), PFN_UP(COMPAT_ARG_XLAT_SIZE), NULL, NIL(struct page_info *)); } --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439017; cv=none; d=zohomail.com; s=zohoarc; b=LMZClXPSuy0sBg8u/x17cD6AcvBoYyJlDkvrX1h9xx93jBeP9U5OYbRXYhGkkuoyL4wYSqdUb1p9rylPl0eucs8qxLR3X/vgT46Tbud/7U7zcu04e8itLyIXQe3tUbngk66VjvxuSDUh215T66JCd0tRnT+YMzPP2rNs0sbfK68= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439017; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=6OJOmz2h1qdSQxscI6ufRihl1BCNT0xYCyFQN2O5ZQY=; b=C5CnKi3CZS6Oxi4iBQYVHpqZI7r7TWg4wSuLJ59eifHplZMixb5rTh2OiE0VdvRLuBftHIrUgk45vq4BBd5729sUhMIh4djf+QC3TgcB889nDxnwsYuaVJpL4W72F7FSg7ggV8/v3a8BtBONO9KtMS9OS/IDnsEaXdRK8Gwvtms= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439017574626.4275271201774; Mon, 19 Oct 2026 12:43:37 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454767.1668313 (Exim 4.92) (envelope-from ) id 1xItGW-0006YD-8Q; Mon, 19 Oct 2026 19:43:24 +0000 Received: by outflank-mailman (output) from mailman id 1454767.1668313; Mon, 19 Oct 2026 19:43:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGW-0006Xu-3Q; Mon, 19 Oct 2026 19:43:24 +0000 Received: by outflank-mailman (input) for mailman id 1454767; Mon, 19 Oct 2026 19:43:22 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGU-0006TJ-R3 for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:43:22 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItGU-000000022kO-3GLw; Mon, 19 Oct 2026 19:43:22 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGU-00000001FjN-2BAz; Mon, 19 Oct 2026 19:43:22 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=6OJOmz2h1qdSQxscI6ufRihl1BCNT0xYCyFQN2O5ZQY=; b=rj1if8TX1/ezr4RUzRtSvgBz1t /Ds7StBnJxj3AVCWe3isOklU/ssaKqOydWNtFvQxrKr+8Ami+saV1BfHOJ1tKm85/hfgnvizr5NF2 hutHiLBi7hJ6baF8OE8QIJjrdGfeZ+d9wno0s5nvTXqpRVSJ8DYYk0eDxI1Rr7xjvktk=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini , Teddy Astie Subject: [PATCH v4 07/30] x86/spec-ctrl: introduce Address Space Isolation command line option Date: Mon, 19 Oct 2026 20:42:38 +0100 Message-ID: <20261019-asi-hvm-v4-7-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439018206158500 From: Roger Pau Monn=C3=A9 Introduce the `asi=3D` command line option, and the opt_vcpu_pt_hvm and opt_pvh_vcpu_pt_hwdom knobs plus the per-domain d->arch.asi.vcpu_pt setting they control. The option is introduced ahead of the functionality it enables, so that the newly added code can be keyed on it from the start; all knobs currently default to off, and enabling any of them adds a boot-time warning that the feature is not functional and is meant for development only. The mechanisms this option controls apply to HVM domains: an HVM vCPU already runs on its own monitor table, so a per-vCPU per-domain area needs no work at context switch. d->arch.asi.vcpu_pt is therefore only ever set for HVM domains, a PVH hardware domain included; PV domains, a PV hardware domain included, are unaffected. Besides the plain boolean, `pv=3D`, `hvm=3D` and `dom0=3D` select the featu= res for PV domains or HVM domains other than the hardware domain, or for the hardware domain, PV or PVH. None of the features applies to PV domains, so `pv=3D` has no effect, and `dom0=3D` acts on a PVH hardware domain only. The boot log gains "ASI features for ..." lines for Dom0 and HVM domains, so hardware-domain-only configurations remain visible. Further per-mechanism tokens arrive with their mechanisms in later patches. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - Docs: "domains" throughout, not "guests" (Roger). - Group the per-domain settings as one-bit fields of d->arch.asi (Roger). - Rename opt_vcpu_pt_hwdom to opt_pvh_vcpu_pt_hwdom: it acts on a PVH hardware domain only (Jan). - parse_asi(): break in the success branch and fall through to default:, rather than a default: label after an else (MISRA C:2012 Rule 16.2; Jan). - Message: enabling a knob adds a boot-time warning; it does not taint (Roger). - Add the pv=3D and dom0=3D sub-options here, beside hvm=3D (Roger). Changes in v3: - HVM only: drop the PV knob and its interaction with XPTI. vCPU-PT applies to HVM domains and to a PVH hardware domain; the Dom0 boot log line says None for a PV one. Changes in v2: - Added to the series Changes since the previously posted version: - Include the hardware domain in the development warning and the boot log summary. - Make the opt_vcpu_pt_* knobs plain booleans preinitialised to false, dropping the late -1 resolution. - Documentation: mention possible protection against unmitigated attacks, and state that hvm=3D does not affect the hardware domain. - Rewrite the commit message. --- docs/misc/xen-command-line.pandoc | 29 ++++++++++ xen/arch/x86/include/asm/domain.h | 6 ++ xen/arch/x86/include/asm/spec_ctrl.h | 2 + xen/arch/x86/spec_ctrl.c | 82 ++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+) diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line= .pandoc index a6f08463d1..839d06da26 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -202,6 +202,35 @@ to appropriate auditing by Xen. Argo is disabled by d= efault. This option is disabled by default, to protect domains from a DoS by a buggy or malicious other domain spamming the ring. =20 +### asi (x86) +> `=3D List of [ , {pv,hvm,dom0}=3D, +> vcpu-pt=3D | vcpu-pt=3Dhvm=3D ]` + +> Default: `false` + +Offers control over whether the hypervisor will engage in Address Space +Isolation, by not having potentially sensitive information permanently map= ped +in the VMM page-tables. Using this option might avoid the need to apply +mitigations for certain speculative related attacks, at the cost of mapping +sensitive information on-demand. It might also offer some protection agai= nst +unmitigated speculation-related attacks. + +The mechanisms currently implemented apply to HVM domains, including a PVH +hardware domain. PV domains, including a PV hardware domain, are unaffect= ed. + +* `pv=3D`, `hvm=3D` and `dom0=3D` enable the features for PV domains or HV= M domains + other than the hardware domain, or for the hardware domain, PV or PVH. = The + plain boolean does all three: `asi=3D1,dom0=3D0` leaves the hardware dom= ain + out. The forms naming no domain type (an un-suffixed `vcpu-pt=3D`) + cover the hardware domain too. + +**WARNING: manual de-selection of enabled options will invalidate any +protection offered by the feature. The fine grained options provided below +are meant to be used for debugging purposes only.** + +* `vcpu-pt` gives each vCPU its own per-domain area: the per-domain slot of + each vCPU's monitor table maps a region private to that vCPU. + ### asid (x86) > `=3D ` =20 diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 212daae1f5..12f79feb42 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -474,6 +474,12 @@ struct arch_domain /* Don't unconditionally inject #GP for unhandled MSRs. */ bool msr_relaxed; =20 + /* Fine grained ASI related options. */ + struct { + /* Give each vCPU its own per-domain area (HVM only). */ + bool vcpu_pt:1; + } asi; + /* Emulated devices enabled bitmap. */ uint32_t emulation_flags; } __cacheline_aligned; diff --git a/xen/arch/x86/include/asm/spec_ctrl.h b/xen/arch/x86/include/as= m/spec_ctrl.h index 8f82533c41..d84f985e82 100644 --- a/xen/arch/x86/include/asm/spec_ctrl.h +++ b/xen/arch/x86/include/asm/spec_ctrl.h @@ -87,6 +87,8 @@ extern uint8_t default_scf; =20 extern int8_t opt_xpti_hwdom, opt_xpti_domu; =20 +extern bool opt_pvh_vcpu_pt_hwdom, opt_vcpu_pt_hvm; + extern bool cpu_has_bug_l1tf; extern int8_t opt_pv_l1tf_hwdom, opt_pv_l1tf_domu; extern bool opt_bp_spec_reduce; diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index bc8538a56f..66c878212a 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -86,6 +86,10 @@ bool __ro_after_init opt_bp_spec_reduce =3D true; =20 static bool __initdata opt_ibpb_alt; =20 +/* Give each vCPU of an HVM domain its own per-domain area (vCPU-PT). */ +bool __ro_after_init opt_vcpu_pt_hvm; +bool __ro_after_init opt_pvh_vcpu_pt_hwdom; + static int __init cf_check parse_spec_ctrl(const char *s) { const char *ss; @@ -487,6 +491,69 @@ static int __init cf_check parse_pv_l1tf(const char *s) } custom_param("pv-l1tf", parse_pv_l1tf); =20 +static int __init cf_check parse_asi(const char *s) +{ + const char *ss; + int val, rc =3D 0; + + /* Interpret 'asi' alone in its positive boolean form. */ + if ( *s =3D=3D '\0' ) + opt_pvh_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D true; + + do { + ss =3D strchr(s, ','); + if ( !ss ) + ss =3D strchr(s, '\0'); + + val =3D parse_bool(s, ss); + switch ( val ) + { + case 0: + case 1: + opt_pvh_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D val; + break; + + default: + if ( (val =3D parse_boolean("pv", s, ss)) >=3D 0 ) + /* PV domains are unaffected. */; + else if ( (val =3D parse_boolean("hvm", s, ss)) >=3D 0 ) + opt_vcpu_pt_hvm =3D val; + else if ( (val =3D parse_boolean("dom0", s, ss)) >=3D 0 ) + opt_pvh_vcpu_pt_hwdom =3D val; + else if ( (val =3D parse_boolean("vcpu-pt", s, ss)) !=3D -1 ) + { + switch ( val ) + { + case 1: + case 0: + opt_pvh_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D val; + break; + + case -2: + s +=3D strlen("vcpu-pt=3D"); + if ( (val =3D parse_boolean("hvm", s, ss)) >=3D 0 ) + { + opt_vcpu_pt_hvm =3D val; + break; + } + fallthrough; + default: + rc =3D -EINVAL; + break; + } + } + else if ( *s ) + rc =3D -EINVAL; + break; + } + + s =3D ss + 1; + } while ( *ss ); + + return rc; +} +custom_param("asi", parse_asi); + static void __init print_details(enum ind_thunk thunk) { unsigned int _7d0 =3D 0, _7d2 =3D 0, e8b =3D 0, e21a =3D 0, e21c =3D 0= , max =3D 0, tmp; @@ -680,6 +747,13 @@ static void __init print_details(enum ind_thunk thunk) opt_pv_l1tf_hwdom ? "enabled" : "disabled", opt_pv_l1tf_domu ? "enabled" : "disabled"); #endif + + printk(" ASI features for Dom0:%s\n", + opt_dom0_pvh && opt_pvh_vcpu_pt_hwdom ? " vCPU-PT" : " None"); +#ifdef CONFIG_HVM + printk(" ASI features for HVM VMs:%s\n", + opt_vcpu_pt_hvm ? " vCPU-PT" : " None"); +#endif } =20 static bool __init check_smt_enabled(void) @@ -1866,6 +1940,9 @@ void spec_ctrl_init_domain(struct domain *d) if ( pv ) d->arch.pv.xpti =3D is_hardware_domain(d) ? opt_xpti_hwdom : opt_xpti_domu; + + d->arch.asi.vcpu_pt =3D !pv && (is_hardware_domain(d) ? opt_pvh_vcpu_p= t_hwdom + : opt_vcpu_pt_hvm); } =20 void __init init_speculation_mitigations(void) @@ -2158,6 +2235,11 @@ void __init init_speculation_mitigations(void) hw_smt_enabled && default_xen_spec_ctrl ) setup_force_cpu_cap(X86_FEATURE_SC_MSR_IDLE); =20 + if ( opt_pvh_vcpu_pt_hwdom || opt_vcpu_pt_hvm ) + warning_add( + "Address Space Isolation is not functional, this option is\n" + "intended to be used only for development purposes.\n"); + xpti_init_default(); =20 l1tf_calculations(); --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439029; cv=none; d=zohomail.com; s=zohoarc; b=MENCloPbJgXJxfLBgUFGGzome6mTqzUb1amG7ZLTJNez6eiFg4E5AtaKUYfbE4xz22EHUF1dGgrxGQSfFiNZpr8EQUMI7U5puOBPAUAbYZNkw4iSH68INEYqyj9BxevkfmM8NXUluctP5CTOkdTs9AS8fp8sUQRNpThzCTYlv1c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439029; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=gqNXY0Fy96nvsg760epSXdia2CmgV3Q0H0of+zL6Uo8=; b=VsxmYNqFX/e/m6ZR8wPkZyPm+KGvQ7KgajLoZb+95WG9Vvg40qyJCH+/UIWV5ztwHNBuOv3xgOwqDTpUGlIKphjPnuHlg/pNy7CLcrojSQ/6mQO9Wr2OTjfMfGCfc5LU/7yEAqTr/8NykX23gaq3rKjn6SJadU1r0Eo1Zrs43VQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439029253396.4622860205292; Mon, 19 Oct 2026 12:43:49 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454770.1668329 (Exim 4.92) (envelope-from ) id 1xItGb-00077m-N3; Mon, 19 Oct 2026 19:43:29 +0000 Received: by outflank-mailman (output) from mailman id 1454770.1668329; Mon, 19 Oct 2026 19:43:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGb-00077P-Fd; Mon, 19 Oct 2026 19:43:29 +0000 Received: by outflank-mailman (input) for mailman id 1454770; Mon, 19 Oct 2026 19:43:29 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGa-00075p-Uj for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:43:28 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItGV-000000022kX-3WZT; Mon, 19 Oct 2026 19:43:23 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGV-00000001FjN-2Sl0; Mon, 19 Oct 2026 19:43:23 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=gqNXY0Fy96nvsg760epSXdia2CmgV3Q0H0of+zL6Uo8=; b=63LyVTQDDFyWxylg9QAQV+60RR TRY0KrUZ9LOs/OHxzML/8JqcEplHlg58VVMmb3npyb5+EEkvMQfHoj/tL3fPaCP5Qxua+Nz4rTzaB Hded2vk533sm+6IYu68eAeMFdcyruOXYscOFW97uCChL2xcm87zyS2OJ5O/N2sMdqgtE=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Tim Deegan Subject: [PATCH v4 08/30] x86/mm: have init_xen_l4_slots() take a vCPU in place of the domain Date: Mon, 19 Oct 2026 20:42:39 +0100 Message-ID: <20261019-asi-hvm-v4-8-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439030354158500 Content-Type: text/plain; charset="utf-8" A later change gives the vCPUs of some domains per-domain areas of their own, and slot 260 of a table private to one vCPU must then hold that vCPU's L3. init_xen_l4_slots(), which fills slot 260, needs the vCPU for that. Have it take a vCPU in place of the domain. A table private to one vCPU passes that vCPU; tables the domain's vCPUs share -- promoted PV guest L4s, PV shadow L4s, and the PV dom0 L4 under construction -- pass any of them. promote_l4_table() passes vCPU 0, whose existence it already checks, and its call is reflowed. No functional change: slot 260 still gets the domain's per-domain L3. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - New in this version, split out of "x86/hvm: introduce per-vCPU L3 page-table": init_xen_l4_slots() takes a vCPU in place of the domain (Roger, Jan). - Reflow promote_l4_table()'s call to init_xen_l4_slots() (Jan). --- xen/arch/x86/include/asm/mm.h | 2 +- xen/arch/x86/mm.c | 16 +++++++++++----- xen/arch/x86/mm/hap/hap.c | 2 +- xen/arch/x86/mm/shadow/hvm.c | 2 +- xen/arch/x86/mm/shadow/multi.c | 2 +- xen/arch/x86/pv/dom0_build.c | 2 +- xen/arch/x86/pv/domain.c | 2 +- 7 files changed, 17 insertions(+), 11 deletions(-) diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index c9085214fd..de45a111f3 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -370,7 +370,7 @@ int devalidate_page(struct page_info *page, unsigned lo= ng type, =20 void init_xen_pae_l2_slots(l2_pgentry_t *l2t, const struct domain *d); void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, - const struct domain *d, mfn_t sl4mfn, bool ro_mpt); + const struct vcpu *v, mfn_t sl4mfn, bool ro_mpt); bool fill_ro_mpt(mfn_t mfn); void zap_ro_mpt(mfn_t mfn); =20 diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index c6bd53354b..ac074cc62d 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -1687,14 +1687,18 @@ static int promote_l3_table(struct page_info *page) * This function must write all ROOT_PAGETABLE_PV_XEN_SLOTS, to clobber any * values a guest may have left there from promote_l4_table(). * - * l4t, l4mfn, and d are mandatory, but l4mfn doesn't need to be the mfn u= nder + * l4t, l4mfn, and v are mandatory, but l4mfn doesn't need to be the mfn u= nder * *l4t. All other parameters are optional and will either fill or zero t= he * appropriate slots. Pagetables not shared with guests will gain the * extended directmap. + * + * v is the vCPU the table is private to or, for a table the domain's vCPUs + * share, any of them. */ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, - const struct domain *d, mfn_t sl4mfn, bool ro_mpt) + const struct vcpu *v, mfn_t sl4mfn, bool ro_mpt) { + const struct domain *d =3D v->domain; /* * PV vcpus need a shortened directmap. HVM and Idle vcpus get the fu= ll * directmap. @@ -1797,6 +1801,8 @@ void zap_ro_mpt(mfn_t mfn) static int promote_l4_table(struct page_info *page) { struct domain *d =3D page_get_owner(page); + /* Any vCPU will do: a PV domain's vCPUs all use the domain-wide L3. */ + const struct vcpu *v =3D domain_vcpu(d, 0); mfn_t l4mfn =3D page_to_mfn(page); l4_pgentry_t *pl4e; unsigned int i; @@ -1808,7 +1814,7 @@ static int promote_l4_table(struct page_info *page) * PV domain may get that only with its first vCPU. The domain has no * vCPUs before XEN_DOMCTL_max_vcpus. */ - if ( !domain_vcpu(d, 0) ) + if ( !v ) return -EINVAL; =20 pl4e =3D map_domain_page(l4mfn); @@ -1887,8 +1893,8 @@ static int promote_l4_table(struct page_info *page) =20 if ( !rc ) { - init_xen_l4_slots(pl4e, l4mfn, - d, INVALID_MFN, VM_ASSIST(d, m2p_strict)); + init_xen_l4_slots(pl4e, l4mfn, v, INVALID_MFN, + VM_ASSIST(d, m2p_strict)); atomic_inc(&d->arch.pv.nr_l4_pages); } unmap_domain_page(pl4e); diff --git a/xen/arch/x86/mm/hap/hap.c b/xen/arch/x86/mm/hap/hap.c index 0ede4181a0..aba4f77df9 100644 --- a/xen/arch/x86/mm/hap/hap.c +++ b/xen/arch/x86/mm/hap/hap.c @@ -407,7 +407,7 @@ static mfn_t hap_make_monitor_table(struct vcpu *v) m4mfn =3D page_to_mfn(pg); l4e =3D map_domain_page(m4mfn); =20 - init_xen_l4_slots(l4e, m4mfn, d, INVALID_MFN, false); + init_xen_l4_slots(l4e, m4mfn, v, INVALID_MFN, false); unmap_domain_page(l4e); =20 return m4mfn; diff --git a/xen/arch/x86/mm/shadow/hvm.c b/xen/arch/x86/mm/shadow/hvm.c index e6fb97c4b6..0b23326214 100644 --- a/xen/arch/x86/mm/shadow/hvm.c +++ b/xen/arch/x86/mm/shadow/hvm.c @@ -760,7 +760,7 @@ mfn_t sh_make_monitor_table(const struct vcpu *v, unsig= ned int shadow_levels) * shadow-linear mapping will either be inserted below when creating * lower level monitor tables, or later in sh_update_cr3(). */ - init_xen_l4_slots(l4e, m4mfn, d, INVALID_MFN, false); + init_xen_l4_slots(l4e, m4mfn, v, INVALID_MFN, false); =20 if ( shadow_levels < 4 ) { diff --git a/xen/arch/x86/mm/shadow/multi.c b/xen/arch/x86/mm/shadow/multi.c index 7f72b4b7b2..ebbf4a12ce 100644 --- a/xen/arch/x86/mm/shadow/multi.c +++ b/xen/arch/x86/mm/shadow/multi.c @@ -974,7 +974,7 @@ sh_make_shadow(struct vcpu *v, mfn_t gmfn, u32 shadow_t= ype) =20 BUILD_BUG_ON(sizeof(l4_pgentry_t) !=3D sizeof(shadow_l4e_t)); =20 - init_xen_l4_slots(l4t, gmfn, d, smfn, (!is_pv_32bit_domain(d) = && + init_xen_l4_slots(l4t, gmfn, v, smfn, (!is_pv_32bit_domain(d) = && VM_ASSIST(d, m2p_strict= ))); unmap_domain_page(l4t); } diff --git a/xen/arch/x86/pv/dom0_build.c b/xen/arch/x86/pv/dom0_build.c index 24f2fa0b7f..dcb1d407ec 100644 --- a/xen/arch/x86/pv/dom0_build.c +++ b/xen/arch/x86/pv/dom0_build.c @@ -725,7 +725,7 @@ static int __init dom0_construct(const struct boot_doma= in *bd) l4start =3D l4tab =3D __va(mpt_alloc); mpt_alloc +=3D PAGE_SIZE; clear_page(l4tab); init_xen_l4_slots(l4tab, _mfn(virt_to_mfn(l4start)), - d, INVALID_MFN, true); + v, INVALID_MFN, true); v->arch.guest_table =3D pagetable_from_paddr(__pa(l4start)); } else diff --git a/xen/arch/x86/pv/domain.c b/xen/arch/x86/pv/domain.c index f3c711b85b..f139d8f83c 100644 --- a/xen/arch/x86/pv/domain.c +++ b/xen/arch/x86/pv/domain.c @@ -127,7 +127,7 @@ static int setup_compat_l4(struct vcpu *v) mfn =3D page_to_mfn(pg); l4tab =3D map_domain_page(mfn); clear_page(l4tab); - init_xen_l4_slots(l4tab, mfn, v->domain, INVALID_MFN, false); + init_xen_l4_slots(l4tab, mfn, v, INVALID_MFN, false); unmap_domain_page(l4tab); =20 /* This page needs to look like a pagetable so that it can be shadowed= */ --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439035; cv=none; d=zohomail.com; s=zohoarc; b=W5HJGhF8BBvT05ZRr/U1ZaGdAlH6miCE2+Ou1kEH+W3Lz4gu3jlstQxeZ6zSbx327atzDnATQQ46Acze5Wj/vmmfGE12MahqVxAmQYds8T+1hX9wZLcZ5hMydSVGlE6n8FvgMTNChiTyxgA4+r+cr0e7zkgGH9pAtfPkEhoaK78= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439035; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=djncFJQMGREcvSArGBCTiVkXx4p/IkOBHDslyykTajc=; b=j+a75PNmCx/X4oyF9y6DW9nOpfuXREJOAM8i9OzW2ZQ2LyRrIzm8sy0N8hdn91Y3oUk0u2NZXESNiDObUH56bVZ5O6BgZ0Lu3O8/BJINTjycPLXCyOtP6R0fcCOJn4gTNxxvwQ+rOkmknl9+2EvSVrpWRor+ymUPZ8I4sA8D8so= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439035256151.80212414705068; Mon, 19 Oct 2026 12:43:55 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454772.1668342 (Exim 4.92) (envelope-from ) id 1xItGd-0007S5-NY; Mon, 19 Oct 2026 19:43:31 +0000 Received: by outflank-mailman (output) from mailman id 1454772.1668342; Mon, 19 Oct 2026 19:43:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGd-0007Qb-EG; Mon, 19 Oct 2026 19:43:31 +0000 Received: by outflank-mailman (input) for mailman id 1454772; Mon, 19 Oct 2026 19:43:30 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItGc-0007E8-2H for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:43:30 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItGW-000000022kb-3mSS; Mon, 19 Oct 2026 19:43:24 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGW-00000001FjN-2i3h; Mon, 19 Oct 2026 19:43:24 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=djncFJQMGREcvSArGBCTiVkXx4p/IkOBHDslyykTajc=; b=ZCqMGSirfqIGpLddDJiiELm2vo Re+kcmOo6FEaIXtthP/ZGljeW3KoXyVm7bDeKmTFnloD5crNsb1kFzjU1Qs1wfvREuoMOArFLLZSP slWugFnUJTi5I4jvo1oYTU/qy8MVrgf+WnEUIGG50mGJN4AGYNPUu96UR2AYN6x6nmT0=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 09/30] x86/hvm: introduce per-vCPU L3 page-table Date: Mon, 19 Oct 2026 20:42:40 +0100 Message-ID: <20261019-asi-hvm-v4-9-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439036456158500 From: Roger Pau Monn=C3=A9 The per-domain area is currently a single domain-wide structure: one L3, referenced from every root page-table associated with the domain, so every mapping in it is visible to every vCPU of the domain. Its contents are already laid out in per-vCPU slices (each vCPU's COMPAT_ARG_XLAT pages, and for PV each vCPU's GDT/LDT window and mapcache entries), but the visibility is domain-wide. Meanwhile, much of the per-vCPU state Xen maintains -- the VMCB, the VMX MSR load/save areas, FPU/XSAVE state -- lives in always-mapped memory. Allow the "per-domain" area to be per-vCPU instead ("VCPU-PT") for HVM domains. HVM monitor tables are already per-vCPU, so this needs nothing at context switch: every monitor table carries its vCPU's own L3 from creation, and the per-vCPU mappings are isolated from other running vCPUs at once. We will later build on this, adding a per-vCPU mapcache and per-vCPU mapped areas, into which we can move the vCPU state mentioned above. Add pervcpu_l3_pg to the arch_vcpu struct, to correspond to the perdomain_l3_pg in the domain struct. (We retain both so that we can switch between per-domain and per-vCPU on a domain-by-domain basis; PV domains always use the domain-wide one.) In {create,destroy}_perdomain_mapping(), if d->arch.asi.vcpu_pt, use pervcpu_l3_pg as the per-domain L3 (allocating it for a vCPU if it's NULL, just as we allocate for a domain in !vcpu_pt mode); otherwise, use perdomain_l3_pg. Introduce a helper, perdomain_l3(), to consistently choose the correct one. Introduce free_pervcpu_mappings() to free this tree, called from arch_vcpu_destroy() on normal teardown. Since the vcpu structure holds the only reference to pervcpu_l3_pg, arch_vcpu_create() must also call it on its error paths: nothing else records the allocation once the vcpu struct is torn down. The domain-wide free_perdomain_mappings() keeps covering non-vCPU-PT domains. init_xen_l4_slots() is given the vCPU a table is private to or, for a table the domain's vCPUs share, any of them. Have it install that vCPU's perdomain_l3() in slot 260: the tables a domain's vCPUs share are PV ones, and a PV domain's vCPUs all use the domain-wide L3. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-fable-5-1, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - Rebase over earlier changes - d->arch.vcpu_pt is now d->arch.asi.vcpu_pt. - init_xen_l4_slots() taking a vCPU in place of the domain, and promote_l4_table() failing while the domain has none, are now patches of their own (Roger, Jan). - Drop the fail_early label: every cleanup step after fail: is safe before its setup has run (Roger). - free_pervcpu_mappings(): clear the pointer, then free, as XFREE() does (Roger). - Likewise in free_perdomain_mappings(). Changes in v3: - HVM only: PV domains keep the domain-wide area, so the PV parts go (the single-vCPU limit, the shadow refusals, the XPTI slot install). init_xen_l4_slots() takes the vCPU as an extra, optional argument rather than in place of the domain, so PV guest L4s, which no vCPU owns, are promoted exactly as before. - Retitle (was: "x86/mm: introduce per-vCPU L3 page-table"). - free_compat_arg_xlat()'s comment names free_pervcpu_mappings() as the teardown for a vCPU-PT domain. Changes in v2: - Added to the series Changes since the previously posted version: - Expand the commit message with the motivation and the design rationale. - Keep domain-wide freeing intact and introduce a vCPU-scoped free_pervcpu_mappings() instead of re-scoping free_perdomain_mappings(); fix the arch_vcpu_create() error-path leaks of a partially built per-vCPU hierarchy. - Add a perdomain_l3() helper for the root selection, rather than open-coding the vcpu_pt choice (and testing both root pointers) at each site. --- xen/arch/x86/domain.c | 9 +++-- xen/arch/x86/include/asm/domain.h | 4 ++ xen/arch/x86/include/asm/mm.h | 2 + xen/arch/x86/mm.c | 66 +++++++++++++++++++++++-------- 4 files changed, 61 insertions(+), 20 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 94a1ea904a..7a4da203d4 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -513,7 +513,7 @@ int arch_vcpu_create(struct vcpu *v) =20 rc =3D mapcache_vcpu_init(v); if ( rc ) - return rc; + goto fail; =20 if ( !is_idle_domain(d) ) { @@ -523,12 +523,12 @@ int arch_vcpu_create(struct vcpu *v) */ rc =3D create_perdomain_mapping(v, PERDOMAIN_VIRT_START, 0, NULL, = NULL); if ( rc ) - return rc; + goto fail; =20 paging_vcpu_init(v); =20 if ( (rc =3D vcpu_init_fpu(v)) !=3D 0 ) - return rc; + goto fail; =20 vmce_init_vcpu(v); =20 @@ -576,6 +576,7 @@ int arch_vcpu_create(struct vcpu *v) vcpu_destroy_fpu(v); xfree(v->arch.msrs); v->arch.msrs =3D NULL; + free_pervcpu_mappings(v); =20 return rc; } @@ -596,6 +597,8 @@ void arch_vcpu_destroy(struct vcpu *v) pv_vcpu_destroy(v); else ASSERT_UNREACHABLE(); + + free_pervcpu_mappings(v); } =20 int arch_sanitise_domain_config(const struct xen_domctl_createdomain *conf= ig) diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 12f79feb42..4140a92f15 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -332,6 +332,7 @@ struct monitor_write_data { =20 struct arch_domain { + /* Domain-wide L3 for the per-domain slot; NULL in a vCPU-PT domain. */ struct page_info *perdomain_l3_pg; =20 /* I/O-port admin-specified access capabilities. */ @@ -688,6 +689,9 @@ struct arch_vcpu =20 struct vcpu_msrs *msrs; =20 + /* Per-vCPU L3 for the per-domain slot; NULL in a non-vCPU-PT domain. = */ + struct page_info *pervcpu_l3_pg; + struct { bool next_interrupt_enabled; } monitor; diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index de45a111f3..12d1c06c12 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -369,6 +369,7 @@ int devalidate_page(struct page_info *page, unsigned lo= ng type, int preemptible); =20 void init_xen_pae_l2_slots(l2_pgentry_t *l2t, const struct domain *d); +struct page_info *perdomain_l3(const struct vcpu *v); void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, const struct vcpu *v, mfn_t sl4mfn, bool ro_mpt); bool fill_ro_mpt(mfn_t mfn); @@ -612,6 +613,7 @@ int create_perdomain_mapping(struct vcpu *v, unsigned l= ong va, void destroy_perdomain_mapping(const struct vcpu *v, unsigned long va, unsigned int nr); void free_perdomain_mappings(struct domain *d); +void free_pervcpu_mappings(struct vcpu *v); =20 void __iomem *ioremap_wc(paddr_t pa, size_t len); =20 diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index ac074cc62d..362a88a300 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -1681,6 +1681,18 @@ static int promote_l3_table(struct page_info *page) } #endif /* CONFIG_PV */ =20 +/* + * The root of the per-domain area in use by @v: the vCPU's own L3 for a + * vCPU-PT domain, the domain-wide one otherwise. + */ +struct page_info *perdomain_l3(const struct vcpu *v) +{ + const struct domain *d =3D v->domain; + + return d->arch.asi.vcpu_pt ? v->arch.pervcpu_l3_pg + : d->arch.perdomain_l3_pg; +} + /* * Fill an L4 with Xen entries. * @@ -1693,7 +1705,8 @@ static int promote_l3_table(struct page_info *page) * extended directmap. * * v is the vCPU the table is private to or, for a table the domain's vCPUs - * share, any of them. + * share, any of them: slot 260 gets v's per-domain area (perdomain_l3()), + * which in a domain whose vCPUs share tables (PV) is the domain-wide one. */ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, const struct vcpu *v, mfn_t sl4mfn, bool ro_mpt) @@ -1726,7 +1739,7 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, =20 /* Slot 260: Per-domain mappings. */ l4t[l4_table_offset(PERDOMAIN_VIRT_START)] =3D - l4e_from_page(d->arch.perdomain_l3_pg, __PAGE_HYPERVISOR_RW); + l4e_from_page(perdomain_l3(v), __PAGE_HYPERVISOR_RW); =20 /* Slot 4: Per-domain mappings mirror. */ BUILD_BUG_ON(IS_ENABLED(CONFIG_PV32) && @@ -6249,7 +6262,7 @@ int create_perdomain_mapping(struct vcpu *v, unsigned= long va, struct page_info **ppg) { struct domain *d =3D v->domain; - struct page_info *pg; + struct page_info *pg, *l3_pg =3D perdomain_l3(v); l3_pgentry_t *l3tab; l2_pgentry_t *l2tab; l1_pgentry_t *l1tab; @@ -6258,14 +6271,17 @@ int create_perdomain_mapping(struct vcpu *v, unsign= ed long va, ASSERT(va >=3D PERDOMAIN_VIRT_START && va < PERDOMAIN_VIRT_SLOT(PERDOMAIN_SLOTS)); =20 - if ( !d->arch.perdomain_l3_pg ) + if ( !l3_pg ) { pg =3D alloc_domheap_page(d, MEMF_no_owner); if ( !pg ) return -ENOMEM; l3tab =3D __map_domain_page(pg); clear_page(l3tab); - d->arch.perdomain_l3_pg =3D pg; + if ( d->arch.asi.vcpu_pt ) + v->arch.pervcpu_l3_pg =3D pg; + else + d->arch.perdomain_l3_pg =3D pg; if ( !nr ) { unmap_domain_page(l3tab); @@ -6275,7 +6291,7 @@ int create_perdomain_mapping(struct vcpu *v, unsigned= long va, else if ( !nr ) return 0; else - l3tab =3D __map_domain_page(d->arch.perdomain_l3_pg); + l3tab =3D __map_domain_page(l3_pg); =20 ASSERT(!l3_table_offset(va ^ (va + nr * PAGE_SIZE - 1))); =20 @@ -6372,16 +6388,16 @@ void destroy_perdomain_mapping(const struct vcpu *v= , unsigned long va, unsigned int nr) { const l3_pgentry_t *l3tab, *pl3e; - const struct domain *d =3D v->domain; + struct page_info *l3_pg =3D perdomain_l3(v); =20 ASSERT(va >=3D PERDOMAIN_VIRT_START && va < PERDOMAIN_VIRT_SLOT(PERDOMAIN_SLOTS)); ASSERT(!nr || !l3_table_offset(va ^ (va + nr * PAGE_SIZE - 1))); =20 - if ( !d->arch.perdomain_l3_pg ) + if ( !l3_pg ) return; =20 - l3tab =3D __map_domain_page(d->arch.perdomain_l3_pg); + l3tab =3D __map_domain_page(l3_pg); pl3e =3D l3tab + l3_table_offset(va); =20 if ( l3e_get_flags(*pl3e) & _PAGE_PRESENT ) @@ -6420,16 +6436,11 @@ void destroy_perdomain_mapping(const struct vcpu *v= , unsigned long va, unmap_domain_page(l3tab); } =20 -void free_perdomain_mappings(struct domain *d) +static void free_perdomain_l3(struct page_info *l3pg) { - l3_pgentry_t *l3tab; + l3_pgentry_t *l3tab =3D __map_domain_page(l3pg); unsigned int i; =20 - if ( !d->arch.perdomain_l3_pg ) - return; - - l3tab =3D __map_domain_page(d->arch.perdomain_l3_pg); - for ( i =3D 0; i < PERDOMAIN_SLOTS; ++i) if ( l3e_get_flags(l3tab[i]) & _PAGE_PRESENT ) { @@ -6465,8 +6476,29 @@ void free_perdomain_mappings(struct domain *d) } =20 unmap_domain_page(l3tab); - free_domheap_page(d->arch.perdomain_l3_pg); + free_domheap_page(l3pg); +} + +void free_perdomain_mappings(struct domain *d) +{ + struct page_info *l3pg =3D d->arch.perdomain_l3_pg; + + if ( !l3pg ) + return; + d->arch.perdomain_l3_pg =3D NULL; + free_perdomain_l3(l3pg); +} + +void free_pervcpu_mappings(struct vcpu *v) +{ + struct page_info *l3pg =3D v->arch.pervcpu_l3_pg; + + if ( !l3pg ) + return; + + v->arch.pervcpu_l3_pg =3D NULL; + free_perdomain_l3(l3pg); } =20 static void write_sss_token(unsigned long *ptr) --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439755; cv=none; d=zohomail.com; s=zohoarc; b=AhC3pvikKDMhDTjJJRN+mqUQpvxwTKypL3K692lBJXjh381XvCWiVn58Wb9x9L6/pCuXAnqH6VlnJYrd8PzuXzp0MZ+3Y8cuh2ZRF2dcjTsh7s046/yjnucGsxCvjeWAaC6hhze9WlwxGLsTv4Fieqh0NAygI2WMP3Lxtv5l668= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439755; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=eMzms4kJ3QFpMQV91hrT3uRFSRB8DVNpEMsCuiyn6Aw=; b=nWiKiOpbpq8dFXaQF0zUuqym7RLV1C2CZPpihs33OKNmovWEar7MHz4JwivdjOpdhgiYrC5m4xanwgaNj7Wd89zV4bB9HHc1nZvRoK2rJ46JF494HrXIVOTvLpMaA32zo+eQuUkx43sKv3r/lHqLYLj7fo5I709BKm2RdCMVKm4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179243975547367.25906613629445; Mon, 19 Oct 2026 12:55:55 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454850.1668509 (Exim 4.92) (envelope-from ) id 1xItSJ-0007cy-0i; Mon, 19 Oct 2026 19:55:35 +0000 Received: by outflank-mailman (output) from mailman id 1454850.1668509; Mon, 19 Oct 2026 19:55:34 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItSH-0007W8-5d; Mon, 19 Oct 2026 19:55:33 +0000 Received: by outflank-mailman (input) for mailman id 1454850; Mon, 19 Oct 2026 19:55:21 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS4-0004uj-Sc for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022zs-3hwi; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGX-00000001FjN-2QLd; Mon, 19 Oct 2026 19:43:25 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=eMzms4kJ3QFpMQV91hrT3uRFSRB8DVNpEMsCuiyn6Aw=; b=ZmsEhrIAG33dUiD+l1lP8dUZ1k 3oQuYQq/BVvuSK2DZ6UX6oJ7RMQ0bqZo5aPg3SSnZrUuEyiZiIJ1iLCk2wiEBw/tZUvngtN4j517h dSVvxwiClkKUTO7hl+/JYXm5pONe6ykfViJIk5CLveNIY+S/zQZ2p3sI3u1M6UJm9+ms=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 10/30] x86/domain_page: drop redundant is_pv_vcpu() checks Date: Mon, 19 Oct 2026 20:42:41 +0100 Message-ID: <20261019-asi-hvm-v4-10-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439757583158500 Content-Type: text/plain; charset="utf-8" The is_pv_vcpu() checks after mapcache_current_vcpu() never fail: they have been redundant since 622c9a5ba95d ("x86/mm: accurately track which vCPU page-tables are loaded") made mapcache_current_vcpu() do the check itself. mapcache_current_vcpu() is now guaranteed to return a PV vCPU with a caching structure (which is what is needed for the subsequent dcache->inuse check): either a scheduled PV vCPU, or this CPU's idle vCPU (possibly after finishing a lazy context switch). Drop the checks; their speculation barriers (evaluate_nospec()) go too. mapcache_current_vcpu() returns NULL if this CPU's pgtable_vcpu is not yet set (early boot) or is not a PV vCPU. Otherwise it reads pgtable_vcpu again and returns that. In between, pgtable_vcpu changes only when a lazy context switch from a PV vCPU is completed, by mapcache_current_vcpu() itself (sync_local_execstate()) or first by an interrupt such as a FLUSH_VCPU_STATE IPI; completing it loads the idle vCPU's page-tables and sets pgtable_vcpu to the idle vCPU. No other writer of pgtable_vcpu runs in an interrupt that returns to the code it interrupted. During an EFI runtime call pgtable_vcpu is the idle vCPU: efi_rs_enter() completes any lazy switch, then loads EFI's page-tables, which carry the directmap. So the vCPU returned is either the PV vCPU checked or this CPU's idle vCPU. The idle vCPU passes is_pv_vcpu(), as the idle domain is created without XEN_DOMCTL_CDF_hvm, but its mapcache is never set up: domain_create() returns for the idle domain before calling arch_domain_create(), which is what calls mapcache_domain_init(). With its dcache->inuse NULL, map_domain_page() uses the directmap, and unmap_domain_page()'s ASSERT() on dcache->inuse still catches a mapcache address unmapped with no mapcache in use. In builds without CONFIG_PV, is_pv_vcpu() is always false and mapcache_current_vcpu() always returns NULL. No functional change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - New in this version, split out of "x86/domain_page: prepare the mapcache for per-vCPU accounting", with a justification of its own. NB I've included Claude's complete analysis in the commit message. I've reworded the commit message so that the middle two paragraphs can be dropped, or reworded more briefly on commit. --- xen/arch/x86/domain_page.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index f3e65e2d44..ed68483695 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -74,7 +74,7 @@ void *map_domain_page(mfn_t mfn) #endif =20 v =3D mapcache_current_vcpu(); - if ( !v || !is_pv_vcpu(v) ) + if ( !v ) return mfn_to_virt(mfn_x(mfn)); =20 dcache =3D &v->domain->arch.pv.mapcache; @@ -179,7 +179,7 @@ void unmap_domain_page(const void *ptr) ASSERT(va >=3D MAPCACHE_VIRT_START && va < MAPCACHE_VIRT_END); =20 v =3D mapcache_current_vcpu(); - ASSERT(v && is_pv_vcpu(v)); + ASSERT(v); =20 dcache =3D &v->domain->arch.pv.mapcache; ASSERT(dcache->inuse); --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439734; cv=none; d=zohomail.com; s=zohoarc; b=nS0TxUooqQQZ49PwyMEF1iPFjKR9elvr1g++zwojckeDKbokINd/MqODcDPn78m9Ize9Chezqdi+pUz+5B+T/fKN3CFtDDxoEdW/32XSaDLp7GHzt6oUniuaIHblye2PILiP8BcTISJGeAjq2npM7Pi9bgYMO1mGf4+Q7HTGAj8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439734; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=nYii0FVbr7qWQUooLUKA16mNwNVCccFAiU2dNNQmLGs=; b=XpUniMVJdBKGK8CFtmSspYbOwWAUDcAcu9nn9ACjfkw/s2sPyCc8+D1KWHjwGyjPgx69Hd0efZVsi/Lz8JJTbVUFek6qe9wuySSS6pz2K8+Tk3QE4Cfv1ehUX/se2yDMuXUKuirj7IPW2Hpg7zXxP6Ziz60f8NrB0Qj0Qt5424E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439734839653.2339633142412; Mon, 19 Oct 2026 12:55:34 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454835.1668373 (Exim 4.92) (envelope-from ) id 1xItS1-0003pl-Fs; Mon, 19 Oct 2026 19:55:17 +0000 Received: by outflank-mailman (output) from mailman id 1454835.1668373; Mon, 19 Oct 2026 19:55:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS1-0003nu-BK; Mon, 19 Oct 2026 19:55:17 +0000 Received: by outflank-mailman (input) for mailman id 1454835; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003hI-HX for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yg-2eEW; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGY-00000001FjN-2hyN; Mon, 19 Oct 2026 19:43:26 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=nYii0FVbr7qWQUooLUKA16mNwNVCccFAiU2dNNQmLGs=; b=0hhp481ohQwC1X8i/yMgQcwHaf hUW3sXG1nbP67YqB6hbAYUV710w6zdV6gDpwkMqjPNVIK5IGt79ADe15n7qmbK2lSncvtyxvvB9iI 7LY969cZe+R6r9yTy1DpRAHkpuBTrKSsP29n7ZyzJsF2slLUa0Nfe0haS4cLFpPstzQA=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 11/30] x86/domain_page: prepare the mapcache for per-vCPU accounting Date: Mon, 19 Oct 2026 20:42:42 +0100 Message-ID: <20261019-asi-hvm-v4-11-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439735343158500 From: Roger Pau Monn=C3=A9 Currently, only PV domains have a mapcache, which is domain-wide one. In preparation for enabling per-vCPU mapcaches for HVM domains, split the accounting out into a struct mapcache, embedded in struct mapcache_domain. Introduce has_mapcache(), for whether a vCPU has a mapcache, and vcpu_mapcache(), which returns the accounting for a vCPU's mapcache and, in *dcache, the domain-wide mapcache whose lock and TLB epoch the caller has to take into account. For now has_mapcache() is is_pv_vcpu(), and vcpu_mapcache() always returns the domain's accounting. Move map_domain_page()'s handling of the domain-wide lock and TLB epoch into three helpers: mapcache_enter() takes the lock and catches up with a wrap another vCPU caused, mapcache_new_epoch() starts a new epoch after a wrap, and mapcache_exit() drops the lock. A per-vCPU mapcache can then skip them without map_domain_page() being re-indented. struct mapcache_domain keeps its size (40 bytes, x86_64 debug build), and so do the structures containing it. No functional change. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - Reword has_mapcache()'s comment (Roger). - Move the comment on the domain-wide lock and TLB epoch into the function body (Roger). - Rename mapcache_lock() / mapcache_unlock() to mapcache_enter() / mapcache_exit() (Roger). - mapcache_new_epoch(): assert that the domain-wide lock is held (Roger). - mapcache_vcpu_init(): drop the comment on the early return for a disabled domain-wide mapcache (Jan). - mapcache_vcpu_init(): initialise ents and nr at their declarations, as before (Jan). - Dropping the redundant is_pv_vcpu() checks in map_domain_page() and unmap_domain_page() is now a patch of its own (Jan). Changes in v3: - New in this version, split out of "x86/hvm: introduce per-vCPU mapcache for vCPU-PT domains" for review. --- xen/arch/x86/domain_page.c | 137 ++++++++++++++++++++---------- xen/arch/x86/include/asm/domain.h | 19 +++-- 2 files changed, 102 insertions(+), 54 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index ed68483695..e4ea7a254f 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -18,17 +18,19 @@ #include #include =20 +/* Whether the vCPU supports mapcache usage, might not be in use. */ +static bool has_mapcache(const struct vcpu *v) +{ + return is_pv_vcpu(v); +} + static inline struct vcpu *mapcache_current_vcpu(void) { const struct vcpu *v =3D this_cpu(pgtable_vcpu); const struct vcpu *curr =3D current; =20 - /* - * During early boot pgtable_vcpu is not set, callers must handle NULL. - * Non-PV domains don't have a mapcache, the directmap covers all phys= ical - * address space. - */ - if ( !v || !is_pv_vcpu(v) ) + /* During early boot pgtable_vcpu is not set, callers must handle NULL= . */ + if ( !v || !has_mapcache(v) ) return NULL; =20 /* @@ -54,6 +56,52 @@ static inline struct vcpu *mapcache_current_vcpu(void) return ACCESS_ONCE(this_cpu(pgtable_vcpu)); } =20 +/* + * The accounting for v's mapcache: the domain's, with *dcache set so the + * caller takes the domain-wide lock and TLB-epoch machinery into account. + */ +static struct mapcache *vcpu_mapcache(struct vcpu *v, + struct mapcache_domain **dcache) +{ + struct domain *d =3D v->domain; + + *dcache =3D &d->arch.pv.mapcache; + return &(*dcache)->cache; +} + +static void mapcache_enter(struct mapcache_domain *dcache, + struct mapcache_vcpu *vcache) +{ + /* The domain's vCPUs share the mapcache's slots. */ + spin_lock(&dcache->lock); + + /* Has some other CPU caused a wrap? We must flush if so. */ + if ( unlikely(dcache->epoch !=3D vcache->shadow_epoch) ) + { + vcache->shadow_epoch =3D dcache->epoch; + if ( NEED_FLUSH(this_cpu(tlbflush_time), dcache->tlbflush_timestam= p) ) + { + perfc_incr(domain_page_tlb_flush); + flush_tlb_local(); + } + } +} + +/* A wrap has just flushed this CPU's TLB: start a new epoch. */ +static void mapcache_new_epoch(struct mapcache_domain *dcache, + struct mapcache_vcpu *vcache) +{ + ASSERT(spin_is_locked(&dcache->lock)); + + vcache->shadow_epoch =3D ++dcache->epoch; + dcache->tlbflush_timestamp =3D tlbflush_current_time(); +} + +static void mapcache_exit(struct mapcache_domain *dcache) +{ + spin_unlock(&dcache->lock); +} + #define mapcache_l2_entry(e) ((e) >> PAGETABLE_ORDER) #define MAPCACHE_L2_ENTRIES (mapcache_l2_entry(MAPCACHE_ENTRIES - 1) + 1) #define MAPCACHE_L1ENT(idx) \ @@ -66,6 +114,7 @@ void *map_domain_page(mfn_t mfn) struct vcpu *v; struct mapcache_domain *dcache; struct mapcache_vcpu *vcache; + struct mapcache *cache; struct vcpu_maphash_entry *hashent; =20 #ifdef NDEBUG @@ -77,9 +126,9 @@ void *map_domain_page(mfn_t mfn) if ( !v ) return mfn_to_virt(mfn_x(mfn)); =20 - dcache =3D &v->domain->arch.pv.mapcache; + cache =3D vcpu_mapcache(v, &dcache); vcache =3D &v->arch.pv.mapcache; - if ( !dcache->inuse ) + if ( !cache->inuse ) return mfn_to_virt(mfn_x(mfn)); =20 perfc_incr(map_domain_page_count); @@ -90,41 +139,30 @@ void *map_domain_page(mfn_t mfn) if ( hashent->mfn =3D=3D mfn_x(mfn) ) { idx =3D hashent->idx; - ASSERT(idx < dcache->entries); + ASSERT(idx < cache->entries); hashent->refcnt++; ASSERT(hashent->refcnt); ASSERT(mfn_eq(l1e_get_mfn(MAPCACHE_L1ENT(idx)), mfn)); goto out; } =20 - spin_lock(&dcache->lock); + mapcache_enter(dcache, vcache); =20 - /* Has some other CPU caused a wrap? We must flush if so. */ - if ( unlikely(dcache->epoch !=3D vcache->shadow_epoch) ) - { - vcache->shadow_epoch =3D dcache->epoch; - if ( NEED_FLUSH(this_cpu(tlbflush_time), dcache->tlbflush_timestam= p) ) - { - perfc_incr(domain_page_tlb_flush); - flush_tlb_local(); - } - } - - idx =3D find_next_zero_bit(dcache->inuse, dcache->entries, dcache->cur= sor); - if ( unlikely(idx >=3D dcache->entries) ) + idx =3D find_next_zero_bit(cache->inuse, cache->entries, cache->cursor= ); + if ( unlikely(idx >=3D cache->entries) ) { unsigned long accum =3D 0, prev =3D 0; =20 /* /First/, clean the garbage map and update the inuse list. */ - for ( i =3D 0; i < BITS_TO_LONGS(dcache->entries); i++ ) + for ( i =3D 0; i < BITS_TO_LONGS(cache->entries); i++ ) { accum |=3D prev; - dcache->inuse[i] &=3D ~xchg(&dcache->garbage[i], 0); - prev =3D ~dcache->inuse[i]; + cache->inuse[i] &=3D ~xchg(&cache->garbage[i], 0); + prev =3D ~cache->inuse[i]; } =20 - if ( accum | (prev & BITMAP_LAST_WORD_MASK(dcache->entries)) ) - idx =3D find_first_zero_bit(dcache->inuse, dcache->entries); + if ( accum | (prev & BITMAP_LAST_WORD_MASK(cache->entries)) ) + idx =3D find_first_zero_bit(cache->inuse, cache->entries); else { /* Replace a hash entry instead. */ @@ -144,19 +182,18 @@ void *map_domain_page(mfn_t mfn) i =3D 0; } while ( i !=3D MAPHASH_HASHFN(mfn_x(mfn)) ); } - BUG_ON(idx >=3D dcache->entries); + BUG_ON(idx >=3D cache->entries); =20 /* /Second/, flush TLBs. */ perfc_incr(domain_page_tlb_flush); flush_tlb_local(); - vcache->shadow_epoch =3D ++dcache->epoch; - dcache->tlbflush_timestamp =3D tlbflush_current_time(); + mapcache_new_epoch(dcache, vcache); } =20 - set_bit(idx, dcache->inuse); - dcache->cursor =3D idx + 1; + set_bit(idx, cache->inuse); + cache->cursor =3D idx + 1; =20 - spin_unlock(&dcache->lock); + mapcache_exit(dcache); =20 l1e_write(&MAPCACHE_L1ENT(idx), l1e_from_mfn(mfn, __PAGE_HYPERVISOR_RW= )); =20 @@ -170,6 +207,7 @@ void unmap_domain_page(const void *ptr) unsigned int idx; struct vcpu *v; struct mapcache_domain *dcache; + struct mapcache *cache; unsigned long va =3D (unsigned long)ptr, mfn, flags; struct vcpu_maphash_entry *hashent; =20 @@ -181,8 +219,8 @@ void unmap_domain_page(const void *ptr) v =3D mapcache_current_vcpu(); ASSERT(v); =20 - dcache =3D &v->domain->arch.pv.mapcache; - ASSERT(dcache->inuse); + cache =3D vcpu_mapcache(v, &dcache); + ASSERT(cache->inuse); =20 idx =3D PFN_DOWN(va - MAPCACHE_VIRT_START); mfn =3D l1e_get_pfn(MAPCACHE_L1ENT(idx)); @@ -205,7 +243,7 @@ void unmap_domain_page(const void *ptr) hashent->mfn); l1e_write(&MAPCACHE_L1ENT(hashent->idx), l1e_empty()); /* /Second/, mark as garbage. */ - set_bit(hashent->idx, dcache->garbage); + set_bit(hashent->idx, cache->garbage); } =20 /* Add newly-freed mapping to the maphash. */ @@ -217,7 +255,7 @@ void unmap_domain_page(const void *ptr) /* /First/, zap the PTE. */ l1e_write(&MAPCACHE_L1ENT(idx), l1e_empty()); /* /Second/, mark as garbage. */ - set_bit(idx, dcache->garbage); + set_bit(idx, cache->garbage); } =20 local_irq_restore(flags); @@ -239,9 +277,9 @@ void mapcache_domain_init(struct domain *d) 2 * PFN_UP(BITS_TO_LONGS(MAPCACHE_ENTRIES) * sizeof(long)= )) > MAPCACHE_VIRT_START + (PERDOMAIN_SLOT_MBYTES << 20)); bitmap_pages =3D PFN_UP(BITS_TO_LONGS(MAPCACHE_ENTRIES) * sizeof(long)= ); - dcache->inuse =3D (void *)MAPCACHE_VIRT_END + PAGE_SIZE; - dcache->garbage =3D dcache->inuse + - (bitmap_pages + 1) * PAGE_SIZE / sizeof(long); + dcache->cache.inuse =3D (void *)MAPCACHE_VIRT_END + PAGE_SIZE; + dcache->cache.garbage =3D dcache->cache.inuse + + (bitmap_pages + 1) * PAGE_SIZE / sizeof(long); =20 spin_lock_init(&dcache->lock); } @@ -249,15 +287,20 @@ void mapcache_domain_init(struct domain *d) int mapcache_vcpu_init(struct vcpu *v) { struct domain *d =3D v->domain; - struct mapcache_domain *dcache =3D &d->arch.pv.mapcache; + struct mapcache_domain *dcache; + struct mapcache *cache; unsigned long i; unsigned int ents =3D d->max_vcpus * MAPCACHE_VCPU_ENTRIES; unsigned int nr =3D PFN_UP(BITS_TO_LONGS(ents) * sizeof(long)); =20 - if ( !is_pv_vcpu(v) || !dcache->inuse ) + if ( !has_mapcache(v) ) + return 0; + + cache =3D vcpu_mapcache(v, &dcache); + if ( !cache->inuse ) return 0; =20 - if ( ents > dcache->entries ) + if ( ents > cache->entries ) { /* Populate page tables. */ int rc =3D create_perdomain_mapping(v, MAPCACHE_VIRT_START, ents, @@ -265,16 +308,16 @@ int mapcache_vcpu_init(struct vcpu *v) =20 /* Populate bit maps. */ if ( !rc ) - rc =3D create_perdomain_mapping(v, (unsigned long)dcache->inus= e, + rc =3D create_perdomain_mapping(v, (unsigned long)cache->inuse, nr, NULL, NIL(struct page_info *= )); if ( !rc ) - rc =3D create_perdomain_mapping(v, (unsigned long)dcache->garb= age, + rc =3D create_perdomain_mapping(v, (unsigned long)cache->garba= ge, nr, NULL, NIL(struct page_info *= )); =20 if ( rc ) return rc; =20 - dcache->entries =3D ents; + cache->entries =3D ents; } =20 /* Mark all maphash entries as not in use. */ diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 4140a92f15..542c6b5eca 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -57,6 +57,16 @@ struct trap_bounce { unsigned long eip; }; =20 +struct mapcache { + /* The number of array entries, and a cursor into the array. */ + unsigned int entries; + unsigned int cursor; + + /* Which mappings are in use, and which are garbage to reap next epoch= ? */ + unsigned long *inuse; + unsigned long *garbage; +}; + #define MAPHASH_ENTRIES 8 #define MAPHASH_HASHFN(pfn) ((pfn) & (MAPHASH_ENTRIES-1)) #define MAPHASHENT_NOTINUSE ((u32)~0U) @@ -73,10 +83,6 @@ struct mapcache_vcpu { }; =20 struct mapcache_domain { - /* The number of array entries, and a cursor into the array. */ - unsigned int entries; - unsigned int cursor; - /* Protects map_domain_page(). */ spinlock_t lock; =20 @@ -84,9 +90,8 @@ struct mapcache_domain { unsigned int epoch; u32 tlbflush_timestamp; =20 - /* Which mappings are in use, and which are garbage to reap next epoch= ? */ - unsigned long *inuse; - unsigned long *garbage; + /* Accounting of the domain-wide mapcache. */ + struct mapcache cache; }; =20 void mapcache_domain_init(struct domain *d); --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439740; cv=none; d=zohomail.com; s=zohoarc; b=VIny7HUIFVX7VruLy3kvRQ8Ges/MALX/Uphxg80dzl+5pxGgTTZVVUfgdqg4Uie77uJnAwksmcZGQSQvGqthie0q1GwYMZx013bL9shwvqglUIm+HPfO8mfA+grNshsZ5ljO9kYEp7GI3UfXMfKYoNV6fi5iTStmiuafqP8CrZg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439740; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=aVPwz/HIeDRqTabRAgDisgdvx3aA/KVHe9Dvzv5GxMA=; b=WV0/UkEyrRlvl4FuZMoazBccGMzllq1DhUDagebLfXaNNdTQHFWWSGCnf5nnJvEt3/qjOPZhCio1y27m8xMCnwACQq2SGWXW79EezG9T2dTK2/XjTyb1MT5ZnlKeo6zrHj5EKJHA7yYM3NUG0COajxNPUpWRLVtVqm0LbzIqzkg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439740205676.3573230473281; Mon, 19 Oct 2026 12:55:40 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454842.1668439 (Exim 4.92) (envelope-from ) id 1xItS6-0005Dn-Gz; Mon, 19 Oct 2026 19:55:22 +0000 Received: by outflank-mailman (output) from mailman id 1454842.1668439; Mon, 19 Oct 2026 19:55:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS5-0005Ae-VT; Mon, 19 Oct 2026 19:55:21 +0000 Received: by outflank-mailman (input) for mailman id 1454842; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003hk-M8 for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022zU-3EEm; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGZ-00000001FjN-2zkB; Mon, 19 Oct 2026 19:43:27 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=aVPwz/HIeDRqTabRAgDisgdvx3aA/KVHe9Dvzv5GxMA=; b=E00Ua+4/MtlsFXfQ/Vz5+nagTR QLCIosl6WA6fF3CH0m6SkDRMg/mleKgVtsF3QKq6+IQCYAEZ+sXA6dAQER/9pE3HKR8dZGSvZHhvh fwqJ2CTwS5nstW+MDRShQIATtmnjqmUithxK1ANvKG57OosELK4Sc1uJuqc21owerVB4=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , Teddy Astie Subject: [PATCH v4 12/30] x86/domain_page: move struct mapcache_vcpu to struct arch_vcpu Date: Mon, 19 Oct 2026 20:42:43 +0100 Message-ID: <20261019-asi-hvm-v4-12-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439741328158500 From: Roger Pau Monn=C3=A9 struct mapcache_vcpu, the per-vCPU part of the mapcache (the maphash and the vCPU's shadow of the domain's TLB epoch), lives in struct pv_vcpu, as only PV vCPUs have a mapcache. In preparation for implementing mapcaches for HVM domains, move it to struct arch_vcpu: v->arch.pv.mapcache becomes v->arch.mapcache. struct pv_vcpu shrinks from 512 to 376 bytes, and struct vcpu grows from 2880 to 3008 bytes (x86_64, debug build), within its page: from here on HVM vCPUs carry the 136-byte struct mapcache_vcpu too, ahead of its use by those of vCPU-PT domains. No functional change. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap Reviewed-by: Roger Pau Monn=C3=A9 --- Changes in v3: - New in this version, split out of "x86/hvm: introduce per-vCPU mapcache for vCPU-PT domains" for review. --- xen/arch/x86/domain_page.c | 6 +++--- xen/arch/x86/include/asm/domain.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index e4ea7a254f..205d7b832a 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -127,7 +127,7 @@ void *map_domain_page(mfn_t mfn) return mfn_to_virt(mfn_x(mfn)); =20 cache =3D vcpu_mapcache(v, &dcache); - vcache =3D &v->arch.pv.mapcache; + vcache =3D &v->arch.mapcache; if ( !cache->inuse ) return mfn_to_virt(mfn_x(mfn)); =20 @@ -224,7 +224,7 @@ void unmap_domain_page(const void *ptr) =20 idx =3D PFN_DOWN(va - MAPCACHE_VIRT_START); mfn =3D l1e_get_pfn(MAPCACHE_L1ENT(idx)); - hashent =3D &v->arch.pv.mapcache.hash[MAPHASH_HASHFN(mfn)]; + hashent =3D &v->arch.mapcache.hash[MAPHASH_HASHFN(mfn)]; =20 local_irq_save(flags); =20 @@ -324,7 +324,7 @@ int mapcache_vcpu_init(struct vcpu *v) BUILD_BUG_ON(MAPHASHENT_NOTINUSE < MAPCACHE_ENTRIES); for ( i =3D 0; i < MAPHASH_ENTRIES; i++ ) { - struct vcpu_maphash_entry *hashent =3D &v->arch.pv.mapcache.hash[i= ]; + struct vcpu_maphash_entry *hashent =3D &v->arch.mapcache.hash[i]; =20 hashent->mfn =3D ~0UL; /* never valid to map */ hashent->idx =3D MAPHASHENT_NOTINUSE; diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 542c6b5eca..45d308407e 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -545,9 +545,6 @@ struct arch_domain =20 struct pv_vcpu { - /* map_domain_page() mapping cache. */ - struct mapcache_vcpu mapcache; - unsigned int vgc_flags; =20 struct trap_info *trap_ctxt; @@ -643,6 +640,9 @@ struct arch_vcpu #define async_exception_state(t) async_exception_state[(t)-1] uint8_t async_exception_mask; =20 + /* map_domain_page() mapping cache. */ + struct mapcache_vcpu mapcache; + /* Virtual Machine Extensions */ union { struct pv_vcpu pv; --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439743; cv=none; d=zohomail.com; s=zohoarc; b=XY065AXZ3oGM3zW/l/WXE6vH3P+cWlGsQSFqAjMx9xVPgXcC86gm1R482mID0mdqEevjvwMd18z5ASjbitVTqDD5I1VBekgnOQOA9XPPNUHEvMLYS+2KDFuhfOv/ZWBUha/4P2zyPPI42PNdIcZA9/HPdleSNxpdpIjSb5M+eQg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439743; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=zn3ZcHMMfyhR8QfM5FyYpbEc+e7vY+aEPBv+DrQrlkY=; b=A2jbyouQpLiagC1VoMaATsce1pT5Yehfbjsf+PNRxEXWaD15+Mu9ND5bPexlIRq6EcLRnvkJl8JH+Ot1ZaYYaAAESsMJHIweu1B292lsNA81obBCExcT/pbwT8ikGuRQnV/I8Kbn7Eqw5DdS0h11B+hMcsU+o5pwN3QAHsh8MM4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439743722899.4787737585682; Mon, 19 Oct 2026 12:55:43 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454832.1668363 (Exim 4.92) (envelope-from ) id 1xItS1-0003id-0Y; Mon, 19 Oct 2026 19:55:17 +0000 Received: by outflank-mailman (output) from mailman id 1454832.1668363; Mon, 19 Oct 2026 19:55:16 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS0-0003iV-TN; Mon, 19 Oct 2026 19:55:16 +0000 Received: by outflank-mailman (input) for mailman id 1454832; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003gx-Dv for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yS-21Dl; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGa-00000001FjN-2f7h; Mon, 19 Oct 2026 19:43:28 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=zn3ZcHMMfyhR8QfM5FyYpbEc+e7vY+aEPBv+DrQrlkY=; b=2sgViO0fKh8kn3LpCSBTEwx3AA gGriFuMiDcxcPGIN/f1nBNOGEW3nC5kPFCO8oTfUgQpHMdnhG9CVfKy0j9F0Ixx4ZL8pBDB42n2JZ WjruUnICtjothAFkN1q3G3+mQWxr2UxiS9EABKYJshar6Yfa1IU0ht/52zhOzpcbe2ME=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , Teddy Astie Subject: [PATCH v4 13/30] x86/domain_page: don't unmap a mapcache entry that is not present Date: Mon, 19 Oct 2026 20:42:44 +0100 Message-ID: <20261019-asi-hvm-v4-13-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439745340158500 Mappings must be torn down by the vCPU that created them, and only once. Have unmap_domain_page() check that the entry it is handed is present and bail out otherwise (ASSERT_UNREACHABLE()), rather than read an MFN out of an empty PTE and act on it, as a second unmap of an entry the first one zapped would. (A second unmap of an entry the maphash kept is caught, as before, only by the refcount ASSERT() in debug builds.) Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap Reviewed-by: Roger Pau Monn=C3=A9 --- Changes in v4: - Reword the comment on the new check (Roger). Changes in v3: - New in this version, split out of "x86/hvm: introduce per-vCPU mapcache for vCPU-PT domains" for review. --- xen/arch/x86/domain_page.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 205d7b832a..47ccefa4e9 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -223,6 +223,15 @@ void unmap_domain_page(const void *ptr) ASSERT(cache->inuse); =20 idx =3D PFN_DOWN(va - MAPCACHE_VIRT_START); + /* + * Sanity check: an entry that isn't present means a double or stray + * unmap. Play it safe: don't act on the MFN in an empty PTE. + */ + if ( !(l1e_get_flags(MAPCACHE_L1ENT(idx)) & _PAGE_PRESENT) ) + { + ASSERT_UNREACHABLE(); + return; + } mfn =3D l1e_get_pfn(MAPCACHE_L1ENT(idx)); hashent =3D &v->arch.mapcache.hash[MAPHASH_HASHFN(mfn)]; =20 --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439728; cv=none; d=zohomail.com; s=zohoarc; b=E+O19iYW+8OddREn+IonfcOU8zD+uu4YRrlkd3Qj7bsgAtuZKZ5x83UD8enimSxAED+98MyOdctMdRCuGOtw1ivqG7hTxS5LY6fVa3CWbpC1LGzm2bqzYNQQCH1GPhLapm9cIzMizk/eqsPVTVU1NP3+8EYTD68P4dUmVDy0RmE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439728; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=YJ9wJhjNHJbAx4M8lq+zFhKHnsHGMBHNIuKf/lCuqCk=; b=nJwMYShUf0ahQE3BLT3AlFdaTuKkoxWB/DXXoepZEkqmaUdj6rKxyZ+6aVvJ9kx46cg18ftHhPzpQCZAFjoz4pN+/ZCVbAB22t0YGs6KQYSl5lSdwIl0qFNmGv6xEdRPYfmCTXBsxFOZ5UlOJkgQjz1OhnnI1zkPKhasSAGdrXo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439728173880.1367267798374; Mon, 19 Oct 2026 12:55:28 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454833.1668368 (Exim 4.92) (envelope-from ) id 1xItS1-0003kX-8K; Mon, 19 Oct 2026 19:55:17 +0000 Received: by outflank-mailman (output) from mailman id 1454833.1668368; Mon, 19 Oct 2026 19:55:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS1-0003jh-3l; Mon, 19 Oct 2026 19:55:17 +0000 Received: by outflank-mailman (input) for mailman id 1454833; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003h2-G4 for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022ye-2VeB; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGb-00000001FjN-30XF; Mon, 19 Oct 2026 19:43:29 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=YJ9wJhjNHJbAx4M8lq+zFhKHnsHGMBHNIuKf/lCuqCk=; b=BgWdGlmdhVDUHGYylgeoPeXSIs Nx77inNA4dmlcPbdTS7wsw5kodnAtP1BuGoXVHF7zfgBa1ZifTis9zCQ6f8ur+//juvwNquuooEiE wnDd3BMZproA3oEpYLj4eKSVcvydfT/OEGo7oeAOkXvpHjC18CmFeno7ZaPejszCLfrc=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 14/30] x86/hvm: introduce per-vCPU mapcache for vCPU-PT domains Date: Mon, 19 Oct 2026 20:42:45 +0100 Message-ID: <20261019-asi-hvm-v4-14-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439729155158500 From: Roger Pau Monn=C3=A9 Add a struct mapcache to struct mapcache_vcpu for the per-vCPU accounting, with MAPCACHE_PERVCPU_ENTRIES (64) slots. Its inuse and garbage bitmaps are then an unsigned long each: declare them inline in struct mapcache_vcpu, rather than mapping them in the per-domain area as the domain-wide bitmaps are. A per-vCPU mapcache is only ever used by its own vCPU, and so by one pCPU at a time. We can therefore skip both the lock and the TLB-epoch tracking the domain-wide mapcache needs; a wrap still flushes the local TLB. struct mapcache_vcpu grows from 136 to 176 bytes, and struct vcpu from 3008 to 3072 bytes (x86_64, debug build), within its page. Unlike a domain-wide mapcache, a per-vCPU one cannot, by design, borrow slots from other vCPUs: MAPCACHE_PERVCPU_ENTRIES (64) is the most single-page mappings such a vCPU can hold at once, interrupt handlers' included, and one more hits the BUG_ON() in map_domain_page(). The PoD zero-page sweep held up to 16 on top of its callers' until "x86/PoD: map one page at a time in p2m_pod_zero_check()". A domain-wide mapcache keeps MAPCACHE_VCPU_ENTRIES (16) slots per vCPU; a per-vCPU one gets 64, for a wider margin. The extra slots take no memory: the area is a whole L1 page whatever the number of slots, and each bitmap is still one unsigned long. Wraps, each of which flushes the local TLB, also come less often. mapcache_current_vcpu()'s lazy-context-switch handling (sync_local_execstate() plus a pgtable_vcpu re-read) now applies to any vCPU with a mapcache: when the idle vCPU runs lazily on an HVM vCPU's monitor table, the full switch happens before the idle context uses a mapcache. In release builds map_domain_page() still tries the directmap first, for any vCPU; debug builds exercise the new mapcache. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - d->arch.vcpu_pt is now d->arch.asi.vcpu_pt. - mapcache_lock() / mapcache_unlock() are now mapcache_enter() / mapcache_exit(). has_mapcache()'s comment no longer changes, and the early return in mapcache_enter() has a one-line comment instead of a sentence added to the block (Roger). - mapcache_vcpu_init(): ents and nr become assignments here, ents now depending on the kind of mapcache (Jan). - A per-vCPU mapcache gets 64 slots (MAPCACHE_PERVCPU_ENTRIES) in place of MAPCACHE_VCPU_ENTRIES (16), which still sizes the domain-wide mapcache; its bitmaps, now full, are described as an unsigned long each (Roger). - Move the comment on why a per-vCPU mapcache needs only a local flush from map_domain_page()'s TLB flush to mapcache_new_epoch()'s early return, as one line (Roger). Changes in v3: - HVM only, and per-vCPU from the start: the HVM enablement of "x86: Initialize the mapcache for PV, HVM, and idle domains" (from the directmap removal series) is folded in, without the idle domain and without a domain-wide HVM mapcache. - Retitle (was: "x86/mm: introduce a per-vCPU mapcache when using ASI"). - Say which second unmaps the not-present check in unmap_domain_page() catches: those of an entry the first unmap zapped. - State the budget of single-page mappings a vCPU can hold at once. - The mapcache accounting refactor, the move of struct mapcache_vcpu to struct arch_vcpu and the not-present check in unmap_domain_page() are split out, for review, into the three patches before this one. --- xen/arch/x86/domain_page.c | 61 +++++++++++++++++++++++-------- xen/arch/x86/include/asm/config.h | 2 + xen/arch/x86/include/asm/domain.h | 6 +++ 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 47ccefa4e9..770fee80a4 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -21,7 +21,7 @@ /* Whether the vCPU supports mapcache usage, might not be in use. */ static bool has_mapcache(const struct vcpu *v) { - return is_pv_vcpu(v); + return is_pv_vcpu(v) || v->domain->arch.asi.vcpu_pt; } =20 static inline struct vcpu *mapcache_current_vcpu(void) @@ -34,10 +34,10 @@ static inline struct vcpu *mapcache_current_vcpu(void) return NULL; =20 /* - * If we are in a lazy context-switch state from a PV vCPU do a full s= witch - * to the idle vCPU now, otherwise an incoming FLUSH_VCPU_STATE IPI wo= uld - * change the page tables under our feet an invalidate any in-use mapc= ache - * entries. + * If we are in a lazy context-switch state from a vCPU with a mapcach= e do + * a full switch to the idle vCPU now, otherwise an incoming + * FLUSH_VCPU_STATE IPI would change the page tables under our feet and + * invalidate any in-use mapcache entries. */ if ( unlikely(this_cpu(curr_vcpu) !=3D curr) ) { @@ -57,14 +57,21 @@ static inline struct vcpu *mapcache_current_vcpu(void) } =20 /* - * The accounting for v's mapcache: the domain's, with *dcache set so the - * caller takes the domain-wide lock and TLB-epoch machinery into account. + * The accounting for v's mapcache: its own for a vCPU-PT domain, the doma= in's + * otherwise, in which case *dcache is set so the caller takes the domain-= wide + * lock and TLB-epoch machinery into account. */ static struct mapcache *vcpu_mapcache(struct vcpu *v, struct mapcache_domain **dcache) { struct domain *d =3D v->domain; =20 + if ( d->arch.asi.vcpu_pt ) + { + *dcache =3D NULL; + return &v->arch.mapcache.cache; + } + *dcache =3D &d->arch.pv.mapcache; return &(*dcache)->cache; } @@ -72,6 +79,10 @@ static struct mapcache *vcpu_mapcache(struct vcpu *v, static void mapcache_enter(struct mapcache_domain *dcache, struct mapcache_vcpu *vcache) { + /* Only its own vCPU uses a per-vCPU mapcache: no lock or epoch needed= . */ + if ( !dcache ) + return; + /* The domain's vCPUs share the mapcache's slots. */ spin_lock(&dcache->lock); =20 @@ -91,6 +102,10 @@ static void mapcache_enter(struct mapcache_domain *dcac= he, static void mapcache_new_epoch(struct mapcache_domain *dcache, struct mapcache_vcpu *vcache) { + /* Per-vCPU: no epoch, as page-table loads flush its non-global entrie= s. */ + if ( !dcache ) + return; + ASSERT(spin_is_locked(&dcache->lock)); =20 vcache->shadow_epoch =3D ++dcache->epoch; @@ -99,7 +114,8 @@ static void mapcache_new_epoch(struct mapcache_domain *d= cache, =20 static void mapcache_exit(struct mapcache_domain *dcache) { - spin_unlock(&dcache->lock); + if ( dcache ) + spin_unlock(&dcache->lock); } =20 #define mapcache_l2_entry(e) ((e) >> PAGETABLE_ORDER) @@ -299,15 +315,25 @@ int mapcache_vcpu_init(struct vcpu *v) struct mapcache_domain *dcache; struct mapcache *cache; unsigned long i; - unsigned int ents =3D d->max_vcpus * MAPCACHE_VCPU_ENTRIES; - unsigned int nr =3D PFN_UP(BITS_TO_LONGS(ents) * sizeof(long)); + unsigned int ents, nr; =20 if ( !has_mapcache(v) ) return 0; =20 cache =3D vcpu_mapcache(v, &dcache); - if ( !cache->inuse ) + if ( !dcache ) + { + /* Per-vCPU bitmaps are an unsigned long each, in the vCPU itself.= */ + cache->inuse =3D v->arch.mapcache.inuse; + cache->garbage =3D v->arch.mapcache.garbage; + ents =3D MAPCACHE_PERVCPU_ENTRIES; + } + else if ( !cache->inuse ) return 0; + else + ents =3D d->max_vcpus * MAPCACHE_VCPU_ENTRIES; + + nr =3D PFN_UP(BITS_TO_LONGS(ents) * sizeof(long)); =20 if ( ents > cache->entries ) { @@ -315,13 +341,16 @@ int mapcache_vcpu_init(struct vcpu *v) int rc =3D create_perdomain_mapping(v, MAPCACHE_VIRT_START, ents, NIL(l1_pgentry_t *), NULL); =20 - /* Populate bit maps. */ - if ( !rc ) + /* Populate the domain-wide bit maps (kept in the per-domain area)= . */ + if ( !rc && dcache ) + { rc =3D create_perdomain_mapping(v, (unsigned long)cache->inuse, nr, NULL, NIL(struct page_info *= )); - if ( !rc ) - rc =3D create_perdomain_mapping(v, (unsigned long)cache->garba= ge, - nr, NULL, NIL(struct page_info *= )); + if ( !rc ) + rc =3D create_perdomain_mapping(v, (unsigned long)cache->g= arbage, + nr, NULL, + NIL(struct page_info *)); + } =20 if ( rc ) return rc; diff --git a/xen/arch/x86/include/asm/config.h b/xen/arch/x86/include/asm/c= onfig.h index aebd3e8f45..ee84c57432 100644 --- a/xen/arch/x86/include/asm/config.h +++ b/xen/arch/x86/include/asm/config.h @@ -279,6 +279,8 @@ extern unsigned long xen_phys_start; =20 /* map_domain_page() map cache. The second per-domain-mapping sub-area. */ #define MAPCACHE_VCPU_ENTRIES (CONFIG_PAGING_LEVELS * CONFIG_PAGING_LEV= ELS) +/* Single-page slots of a per-vCPU mapcache (vCPU-PT domains). */ +#define MAPCACHE_PERVCPU_ENTRIES 64 #define MAPCACHE_ENTRIES (MAX_VIRT_CPUS * MAPCACHE_VCPU_ENTRIES) #define MAPCACHE_VIRT_START PERDOMAIN_VIRT_SLOT(1) #define MAPCACHE_VIRT_END (MAPCACHE_VIRT_START + \ diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 45d308407e..7076e38848 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -80,6 +80,12 @@ struct mapcache_vcpu { uint32_t idx; uint32_t refcnt; } hash[MAPHASH_ENTRIES]; + + /* Accounting of a per-vCPU mapcache (vCPU-PT domains). */ + struct mapcache cache; + /* Backing for cache.{inuse,garbage}. */ + DECLARE_BITMAP(inuse, MAPCACHE_PERVCPU_ENTRIES); + DECLARE_BITMAP(garbage, MAPCACHE_PERVCPU_ENTRIES); }; =20 struct mapcache_domain { --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439761; cv=none; d=zohomail.com; s=zohoarc; b=cf63XDp1M63Qqd2l0wrb2ZJ9onoIPRbhOp7YtvntIg+DLNzRkkVpIoATDXLRrFVB2Mbt2nl0QxcxmfcBU5PTdrfWk05XwecacXXVVuc/MxPMNGvc2e/zH3hBTg8vsOSNVceMkKvTN+UbIMehhyklGk22OwH4CqKEPc8Qj543N00= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439761; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=+Qh68FKnxSRb1Fv+xxuele9p2cel+WGPjQ8nK81SNtY=; b=fD1VY6ntBir5HJ2ouvLL1vU/DsWuRN3qHzM3dNaQWZ+JdQTLA1A29zWYXnw8lfO2seWtJnc260n8EGcJ7UNBxlHfszbfbKg9OLagiGxaDsCFbP4Ly5R/vWxnIEcOxkTJY+vUalr16Nyewty8tZZEOZovf8+nEetrbcS0jIPg18Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439761308969.9548251877745; Mon, 19 Oct 2026 12:56:01 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454849.1668492 (Exim 4.92) (envelope-from ) id 1xItSF-00078H-EW; Mon, 19 Oct 2026 19:55:31 +0000 Received: by outflank-mailman (output) from mailman id 1454849.1668492; Mon, 19 Oct 2026 19:55:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItSE-000744-Hq; Mon, 19 Oct 2026 19:55:30 +0000 Received: by outflank-mailman (input) for mailman id 1454849; Mon, 19 Oct 2026 19:55:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS4-0004sm-L7 for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yK-1MGT; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGc-00000001FjN-2gvR; Mon, 19 Oct 2026 19:43:30 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=+Qh68FKnxSRb1Fv+xxuele9p2cel+WGPjQ8nK81SNtY=; b=w+SqXr/yxkcAUG5++mX4Ydr8jg XfJoRzQzSuZQLv8hOTZ0fucmhRmUy7meK3AMu1y9Ypj9ZgOEesCIdii+yJ03hBqnP563jdYwf5MNx nAY2F/MwB4ZDQl5Wb3o5mT6yva1vRwoHBz7qQ1+WkG+Z4A4aJgEFpUKEs+aQsiQtRdd0=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 15/30] x86/mm: let Xen's page-table walker operate on a given root Date: Mon, 19 Oct 2026 20:42:46 +0100 Message-ID: <20261019-asi-hvm-v4-15-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439761739158500 Content-Type: text/plain; charset="utf-8" map_pages_to_xen() and modify_xen_mappings() maintain Xen's own mappings, walking from idle_pg_table, whose Xen slots every other root page-table copies. A later patch introduces a second, sparse directmap hierarchy: page tables for the directmap virtual address range that some guest contexts use in place of idle_pg_table's. It needs the same maintenance at the same virtual addresses. Give virt_to_xen_l{3,2,1}e() the L4 to walk from, and move the bodies of map_pages_to_xen() and modify_xen_mappings() into map_pages_in() and modify_mappings_in(), which take the root as well. The public functions pass idle_pg_table. The EFI runtime-services page-tables mirror idle_pg_table's L4 entries only, so a newly allocated L3 is propagated to them only when the root is idle_pg_table. TLB flushing is unaffected: flushes are by virtual address and go to every online CPU, which covers both hierarchies. No functional change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - Message: name the second hierarchy as sparse; flushes reach every CPU (Roger). Changes in v3: - New in this version. --- xen/arch/x86/mm.c | 55 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 362a88a300..514e121c8b 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5229,13 +5229,14 @@ static DEFINE_SPINLOCK(map_pgdir_lock); =20 /* * For virt_to_xen_lXe() functions, they take a linear address and return a - * pointer to Xen's LX entry. Caller needs to unmap the pointer. + * pointer to Xen's LX entry in the page-table hierarchy rooted at @root + * (idle_pg_table for Xen's own mappings). Caller needs to unmap the poin= ter. */ -static l3_pgentry_t *virt_to_xen_l3e(unsigned long v) +static l3_pgentry_t *virt_to_xen_l3e(l4_pgentry_t *root, unsigned long v) { l4_pgentry_t *pl4e; =20 - pl4e =3D &idle_pg_table[l4_table_offset(v)]; + pl4e =3D &root[l4_table_offset(v)]; if ( !(l4e_get_flags(*pl4e) & _PAGE_PRESENT) ) { bool locking =3D system_state > SYS_STATE_boot; @@ -5251,7 +5252,8 @@ static l3_pgentry_t *virt_to_xen_l3e(unsigned long v) l4_pgentry_t l4e =3D l4e_from_mfn(l3mfn, __PAGE_HYPERVISOR); =20 l4e_write(pl4e, l4e); - efi_update_l4_pgtable(l4_table_offset(v), l4e); + if ( root =3D=3D idle_pg_table ) + efi_update_l4_pgtable(l4_table_offset(v), l4e); l3mfn =3D INVALID_MFN; } if ( locking ) @@ -5262,11 +5264,11 @@ static l3_pgentry_t *virt_to_xen_l3e(unsigned long = v) return map_l3t_from_l4e(*pl4e) + l3_table_offset(v); } =20 -static l2_pgentry_t *virt_to_xen_l2e(unsigned long v) +static l2_pgentry_t *virt_to_xen_l2e(l4_pgentry_t *root, unsigned long v) { l3_pgentry_t *pl3e, l3e; =20 - pl3e =3D virt_to_xen_l3e(v); + pl3e =3D virt_to_xen_l3e(root, v); if ( !pl3e ) return NULL; =20 @@ -5300,11 +5302,11 @@ static l2_pgentry_t *virt_to_xen_l2e(unsigned long = v) return map_l2t_from_l3e(l3e) + l2_table_offset(v); } =20 -static l1_pgentry_t *virt_to_xen_l1e(unsigned long v) +static l1_pgentry_t *virt_to_xen_l1e(l4_pgentry_t *root, unsigned long v) { l2_pgentry_t *pl2e, l2e; =20 - pl2e =3D virt_to_xen_l2e(v); + pl2e =3D virt_to_xen_l2e(root, v); if ( !pl2e ) return NULL; =20 @@ -5380,7 +5382,7 @@ mfn_t xen_map_to_mfn(unsigned long va) bool locking =3D system_state > SYS_STATE_boot; unsigned int l2_offset =3D l2_table_offset(va); unsigned int l1_offset =3D l1_table_offset(va); - const l3_pgentry_t *pl3e =3D virt_to_xen_l3e(va); + const l3_pgentry_t *pl3e =3D virt_to_xen_l3e(idle_pg_table, va); const l2_pgentry_t *pl2e =3D NULL; const l1_pgentry_t *pl1e =3D NULL; struct page_info *l3page; @@ -5420,7 +5422,12 @@ mfn_t xen_map_to_mfn(unsigned long va) return ret; } =20 -int map_pages_to_xen( +/* + * map_pages_to_xen() in the hierarchy rooted at @root: idle_pg_table's, or + * another hierarchy sharing its virtual addresses. + */ +static int map_pages_in( + l4_pgentry_t *root, unsigned long virt, mfn_t mfn, unsigned long nr_mfns, @@ -5471,7 +5478,7 @@ int map_pages_to_xen( UNMAP_DOMAIN_PAGE(pl3e); UNMAP_DOMAIN_PAGE(pl2e); =20 - pl3e =3D virt_to_xen_l3e(virt); + pl3e =3D virt_to_xen_l3e(root, virt); if ( !pl3e ) goto out; =20 @@ -5595,7 +5602,7 @@ int map_pages_to_xen( free_xen_pagetable(l2mfn); } =20 - pl2e =3D virt_to_xen_l2e(virt); + pl2e =3D virt_to_xen_l2e(root, virt); if ( !pl2e ) goto out; =20 @@ -5640,7 +5647,7 @@ int map_pages_to_xen( /* Normal page mapping. */ if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ) { - pl1e =3D virt_to_xen_l1e(virt); + pl1e =3D virt_to_xen_l1e(root, virt); if ( pl1e =3D=3D NULL ) goto out; } @@ -5825,6 +5832,15 @@ int map_pages_to_xen( return rc; } =20 +int map_pages_to_xen( + unsigned long virt, + mfn_t mfn, + unsigned long nr_mfns, + pte_attr_t flags) +{ + return map_pages_in(idle_pg_table, virt, mfn, nr_mfns, flags); +} + int __init populate_pt_range(unsigned long virt, unsigned long nr_mfns) { return map_pages_to_xen(virt, INVALID_MFN, nr_mfns, MAP_SMALL_PAGES); @@ -5841,8 +5857,12 @@ int __init populate_pt_range(unsigned long virt, uns= igned long nr_mfns) * flags are ignored. * * It is an error to call with present flags over an unpopulated range. + * + * modify_mappings_in() does so in the hierarchy rooted at @root, as + * map_pages_in() does. */ -int modify_xen_mappings(unsigned long s, unsigned long e, pte_attr_t nf) +static int modify_mappings_in(l4_pgentry_t *root, unsigned long s, + unsigned long e, pte_attr_t nf) { bool locking =3D system_state > SYS_STATE_boot; l3_pgentry_t *pl3e =3D NULL; @@ -5869,7 +5889,7 @@ int modify_xen_mappings(unsigned long s, unsigned lon= g e, pte_attr_t nf) UNMAP_DOMAIN_PAGE(pl2e); UNMAP_DOMAIN_PAGE(pl3e); =20 - pl3e =3D virt_to_xen_l3e(v); + pl3e =3D virt_to_xen_l3e(root, v); if ( !pl3e ) goto out; =20 @@ -6120,6 +6140,11 @@ int modify_xen_mappings(unsigned long s, unsigned lo= ng e, pte_attr_t nf) =20 #undef flush_area =20 +int modify_xen_mappings(unsigned long s, unsigned long e, pte_attr_t nf) +{ + return modify_mappings_in(idle_pg_table, s, e, nf); +} + int destroy_xen_mappings(unsigned long s, unsigned long e) { return modify_xen_mappings(s, e, _PAGE_NONE); --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439750; cv=none; d=zohomail.com; s=zohoarc; b=Hq1lBt4+DSS0WMgEU5oTPXhUM/9q5L3P4bD/DUbEQbn/aktTZ3erjYvgrGFXGOi5mWrjaECZtL7kOLtrVLzVw9fUe70vtiTpmHMRh9c0N9fSUMQ5aj5DM3mJV3fFKLJWsIqv8EmwProc0SZvK8CD+sxqJd9VEewTjL8IUE9WLUU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439750; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=m8BHF4nspVGbZ4EkQQctpimDT5jF79rMCrrWtMtITns=; b=KTUQMvFIsaSVbtfvD+fgVKZDhG+7Kuw/FBYGDOL5l+teJIDX1tyWmU3tZS8FRJJstPQkzD3ZONSDlEB9nKgepXuoFCjKfra2ovaAlnUHZ37qSlcpOQYvCT1Y0mQOleDzqFnj6VMFh8RYvjto+vGKu8M6RHwnFzWnXQJlu9+ZXt4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439750191334.3636644492883; Mon, 19 Oct 2026 12:55:50 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454848.1668500 (Exim 4.92) (envelope-from ) id 1xItSG-0007J8-L8; Mon, 19 Oct 2026 19:55:32 +0000 Received: by outflank-mailman (output) from mailman id 1454848.1668500; Mon, 19 Oct 2026 19:55:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItSF-0007G6-He; Mon, 19 Oct 2026 19:55:31 +0000 Received: by outflank-mailman (input) for mailman id 1454848; Mon, 19 Oct 2026 19:55:20 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS4-0004s9-JR for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yO-1eKI; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGe-00000001FjN-1GFv; Mon, 19 Oct 2026 19:43:32 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=m8BHF4nspVGbZ4EkQQctpimDT5jF79rMCrrWtMtITns=; b=Xc6R/ZERY4rFQ4GjcU7LBh9FlR +Y7hOJQcomHnyKD5kWvIuHlaSriVFKdXL9UE/qzohbBU6VlkkoY+KzQmUgGopzrZD0BBuuW5TDaaW uzaZsvS5GO3gFJ5uZw5eHiCkLKUWpDfLbjicJtySUwfTePQSQG1AKK09wFXfYvW4AySE=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Anthony PERARD , Michal Orzel , Julien Grall , Stefano Stabellini Subject: [PATCH v4 16/30] x86/mm: build and maintain a sparse view of the directmap Date: Mon, 19 Oct 2026 20:42:47 +0100 Message-ID: <20261019-asi-hvm-v4-16-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439751588158500 Content-Type: text/plain; charset="utf-8" The directmap maps all of RAM, and every root page-table carries it, so while any guest context runs, all guest memory is reachable from Xen's address space. For ASI, guest contexts are to run on a view of the directmap which maps only what Xen must reach through it at all times, leaving guest memory to transient mappings (map_domain_page()). Rather than remove the directmap for everyone, keep the full directmap as it is, for the idle domain and for guest contexts not using ASI, and build a second, sparse view: page-tables for the same virtual addresses (the part of the directmap PV guests can see, where everything Xen reaches through the directmap lives), mapping a subset of what the full directmap maps, each entry a copy of the full directmap's. Boot and the idle domain keep the full directmap, so nothing in them needs converting. The sparse view maps the xenheap, and memory never handed to the heap allocator (Xen's image, boot-time allocations, firmware tables). It is built once the boot allocator has handed its memory to the heap, from the full directmap minus the ranges the heap was given, plus the xenheap allocations made meanwhile. The page-tables then come from the heap, and no other CPU is up. The sparse view has an L3 for each of its L4 slots from the start, so its L4 entries never change and root page-tables can take copies of them. It is then kept in step: - xenheap allocations are added, and taken out again before the pages are freed. If that fails the pages stay mapped, and are leaked with a warning rather than reused; - memory handed to the heap later (boot modules, .init) is taken out, and is not handed over if that fails; - a PV dom0's initrd, assigned to dom0 in place, is taken out; - page-tables from the boot allocator, which the sparse view maps with every other boot-time allocation, are taken out when idle_pg_table's hierarchy frees them, and are not freed if that fails, so that the sparse view never keeps a page the heap may hand out; - changes to the full directmap through map_pages_to_xen() and modify_xen_mappings() are mirrored. After boot, map_pages_to_xen() maps into the directmap only memory Xen keeps (stack guard and shadow stack pages, firmware tables); memory hot-add maps the new range into the full directmap only, so that the sparse view never needs page-table pages for it. modify_xen_mappings_lite() needs no change: it only changes the mappings of Xen's image, in L4 slot 261, which every root page-table shares with idle_pg_table, outside the range the sparse view replaces. Common code gains four hooks for this, arch_heap_pages_init(), arch_xenheap_needs_scrub() and arch_xenheap_pages_{alloc,free}(), no-ops unless CONFIG_SPARSE_DIRECTMAP is selected. A failed arch_xenheap_pages_alloc() may have mapped part of the pages: alloc_xenheap_pages() then takes them out through arch_xenheap_pages_free(), as a free would, before freeing them, and leaks them if that fails too. xen_mapping_lookup(), built only with CONFIG_SPARSE_DIRECTMAP, reads an entry of either hierarchy under the L3 table lock. alloc_xenheap_pages() accepts pages still awaiting scrubbing (MEMF_no_scrub). That is harmless while the directmap maps all memory anyway, but the sparse view would make such a page's contents, a dying domain's data for instance, reachable from every context on it until overwritten. With a sparse view configured, arch_xenheap_needs_scrub() makes xenheap allocations scrub those pages first, as domain heap allocations do; clean pages need nothing extra. The allocator may still have to scrub a page before returning it; this will be mapped with map_domain_page(). This is fine for HVM domains on the sparse view, as the mapcache is per-vCPU, so no other vCPU can read unscrubbed data. PV domains' mapcache is still per-domain, so for PV domains on the sparse view, this opens up a window where other vCPUs could potentially speculatively access the unscrubbed data. This would be fixed by having per-vCPU mapcaches for PV domains, or synchronously scrubbing pages on free. Neither are in the scope of the current work. arch_mfns_in_directmap() says whether an MFN range is reachable through the directmap; it now answers for every context, so false once a sparse view is configured. Its one caller, init_node_heap(), then keeps a NUMA node's heap metadata in the xenheap instead of carving it out of the node's memory through the directmap: carved out of a range handed to the heap, it would be left out of the sparse view, which every heap allocation needs it in. The sparse view is built only when some domain type is to use it, which nothing can request yet: this patch changes nothing on its own. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - Message: "domain type", not "guest type". - Wrap a line that reached 80 columns. - Memory hot-add maps the new range into the full directmap only. - Define opt_sparse_dmap_hwdom here, beside the PV and HVM knobs (Roger). - add_to_sparse_view(): assert that the lookup finds the MFN expected (Roger). - cmp_mfn_range(): explicit return values, as in cmp_node() (Roger). - Comments and variable name changes in sparse_dmap_init()'s loop over the heap's ranges (Roger). - Message: drop "(NUMA node heap metadata)" (Roger). - Message: drop the reference to the directmap removal series (Roger). - Message: modify_xen_mappings_lite() needs no change (Roger). - Message: note the limit of scrubbing at allocation for PV domains (Roger). - Message: only boot and the idle domain keep the full directmap. - Set the NX bit in this patch, rather than adding it in a later patch. Changes in v3: - New in this version. --- xen/arch/x86/Kconfig | 1 + xen/arch/x86/Makefile | 1 + xen/arch/x86/include/asm/mm.h | 16 +- xen/arch/x86/include/asm/sparse-directmap.h | 67 +++ xen/arch/x86/mm.c | 125 +++++- xen/arch/x86/pv/dom0_build.c | 4 + xen/arch/x86/setup.c | 4 + xen/arch/x86/sparse-directmap.c | 431 ++++++++++++++++++++ xen/arch/x86/x86_64/mm.c | 10 +- xen/common/Kconfig | 18 + xen/common/page_alloc.c | 41 +- xen/include/xen/mm.h | 35 ++ 12 files changed, 737 insertions(+), 16 deletions(-) create mode 100644 xen/arch/x86/include/asm/sparse-directmap.h create mode 100644 xen/arch/x86/sparse-directmap.c diff --git a/xen/arch/x86/Kconfig b/xen/arch/x86/Kconfig index e5535ac484..cd72102ccc 100644 --- a/xen/arch/x86/Kconfig +++ b/xen/arch/x86/Kconfig @@ -31,6 +31,7 @@ config X86 select HAS_SCHED_GRANULARITY select HAS_SHARED_INFO imply HAS_SOFT_RESET + select HAS_SPARSE_DIRECTMAP select HAS_UBSAN select HAS_VMAP select HAS_VPCI if HVM diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile index 2be90ea098..86adf042e9 100644 --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -63,6 +63,7 @@ obj-y +=3D shutdown.o obj-y +=3D smp.o obj-y +=3D smpboot.o obj-y +=3D spec_ctrl.o +obj-$(CONFIG_SPARSE_DIRECTMAP) +=3D sparse-directmap.o obj-y +=3D srat.o obj-y +=3D string.o obj-$(CONFIG_SYSCTL) +=3D sysctl.o diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index 12d1c06c12..7612cf66ef 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -7,6 +7,7 @@ #include #include #include +#include #include =20 /* @@ -597,6 +598,15 @@ mfn_t alloc_xen_pagetable(void); void free_xen_pagetable(mfn_t mfn); void *alloc_mapped_pagetable(mfn_t *pmfn); =20 +int map_pages_in(l4_pgentry_t *root, unsigned long virt, mfn_t mfn, + unsigned long nr_mfns, pte_attr_t flags); +int modify_mappings_in(l4_pgentry_t *root, unsigned long s, unsigned long = e, + pte_attr_t nf); +#ifdef CONFIG_SPARSE_DIRECTMAP +mfn_t xen_mapping_lookup(l4_pgentry_t *root, unsigned long va, + pte_attr_t *flags, unsigned long *nr); +#endif + int __sync_local_execstate(void); =20 /* Arch-specific portion of memory_op hypercall. */ @@ -657,12 +667,16 @@ void write_32bit_pse_identmap(uint32_t *l2); =20 /* * x86 maps part of physical memory via the directmap region. - * Return whether the range of MFN falls in the directmap region. + * Return whether the range of MFN falls in the directmap region in every + * context: never, with a sparse directmap view configured. */ static inline bool arch_mfns_in_directmap(unsigned long mfn, unsigned long= nr) { unsigned long eva =3D min(DIRECTMAP_VIRT_END, HYPERVISOR_VIRT_END); =20 + if ( sparse_dmap_configured() ) + return false; + return (mfn + nr) <=3D (virt_to_mfn(eva - 1) + 1); } =20 diff --git a/xen/arch/x86/include/asm/sparse-directmap.h b/xen/arch/x86/inc= lude/asm/sparse-directmap.h new file mode 100644 index 0000000000..ea14d44334 --- /dev/null +++ b/xen/arch/x86/include/asm/sparse-directmap.h @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef X86_SPARSE_DIRECTMAP_H +#define X86_SPARSE_DIRECTMAP_H + +#include +#include +#include + +#ifdef CONFIG_SPARSE_DIRECTMAP + +/* + * The part of the directmap the sparse view covers: the part PV guests can + * see, which is where everything Xen reaches through the directmap lives. + * SPARSE_DMAP_END_MFN is the first MFN past it. + */ +#define SPARSE_DMAP_START DIRECTMAP_VIRT_START +#define SPARSE_DMAP_END HYPERVISOR_VIRT_END +#define SPARSE_DMAP_END_MFN (virt_to_mfn(SPARSE_DMAP_END - 1) + 1) + +/* + * Which domains use the sparse view: PV domains and HVM domains other than + * the hardware domain, and the hardware domain, PV or PVH. + */ +extern bool opt_sparse_dmap_pv, opt_sparse_dmap_hvm, opt_sparse_dmap_hwdom; + +/* Root of the sparse view, once built (NULL before, or if not configured)= . */ +extern l4_pgentry_t *sparse_dmap_root; + +static inline bool sparse_dmap_configured(void) +{ + return opt_sparse_dmap_pv || opt_sparse_dmap_hvm || opt_sparse_dmap_hw= dom; +} + +void sparse_dmap_init(void); +int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, unsigned long nr, + pte_attr_t flags); +int sparse_dmap_mirror_modify(unsigned long s, unsigned long e, + pte_attr_t nf); +int sparse_dmap_remove(mfn_t mfn, unsigned long nr); +int sparse_dmap_drop_pagetable(mfn_t mfn); + +#else /* !CONFIG_SPARSE_DIRECTMAP */ + +static inline bool sparse_dmap_configured(void) { return false; } +static inline void sparse_dmap_init(void) {} +static inline int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, + unsigned long nr, pte_attr_t flag= s) +{ + return 0; +} +static inline int sparse_dmap_mirror_modify(unsigned long s, unsigned long= e, + pte_attr_t nf) +{ + return 0; +} +static inline int sparse_dmap_remove(mfn_t mfn, unsigned long nr) +{ + return 0; +} +static inline int sparse_dmap_drop_pagetable(mfn_t mfn) +{ + return 0; +} + +#endif /* CONFIG_SPARSE_DIRECTMAP */ + +#endif /* X86_SPARSE_DIRECTMAP_H */ diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 514e121c8b..7e3009251c 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -127,6 +127,7 @@ #include #include #include +#include #include #include #include @@ -5422,11 +5423,101 @@ mfn_t xen_map_to_mfn(unsigned long va) return ret; } =20 +#ifdef CONFIG_SPARSE_DIRECTMAP +/* + * Look @va up in the hierarchy rooted at @root, without allocating anythi= ng. + * Returns the MFN mapped at @va, or INVALID_MFN, with the mapping's flags= in + * L1 form in *flags, and in *nr the number of pages from @va to the end of + * the entry that maps @va (or that finds it unmapped). + */ +mfn_t xen_mapping_lookup(l4_pgentry_t *root, unsigned long va, + pte_attr_t *flags, unsigned long *nr) +{ + bool locking =3D system_state > SYS_STATE_boot; + l4_pgentry_t l4e =3D root[l4_table_offset(va)]; + const l3_pgentry_t *pl3e =3D NULL; + const l2_pgentry_t *pl2e =3D NULL; + const l1_pgentry_t *pl1e =3D NULL; + struct page_info *l3page; + unsigned int shift =3D L4_PAGETABLE_SHIFT; + mfn_t ret =3D INVALID_MFN; + + L3T_INIT(l3page); + *flags =3D 0; + + if ( !(l4e_get_flags(l4e) & _PAGE_PRESENT) ) + goto out; + + pl3e =3D map_l3t_from_l4e(l4e) + l3_table_offset(va); + l3page =3D l4e_get_page(l4e); + L3T_LOCK(l3page); + + shift =3D L3_PAGETABLE_SHIFT; + if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ) + goto out; + if ( l3e_get_flags(*pl3e) & _PAGE_PSE ) + { + /* The superpage PAT bit sits among the address bits. */ + *flags =3D lNf_to_l1f(l3e_get_flags(*pl3e)) | + (l3e_get_intpte(*pl3e) & _PAGE_PSE_PAT ? _PAGE_PAT : 0); + ret =3D _mfn((l3e_get_pfn(*pl3e) & + ~((1UL << (2 * PAGETABLE_ORDER)) - 1)) + + (PFN_DOWN(va) & ((1UL << (2 * PAGETABLE_ORDER)) - 1))); + goto out; + } + + pl2e =3D map_l2t_from_l3e(*pl3e) + l2_table_offset(va); + shift =3D L2_PAGETABLE_SHIFT; + if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ) + goto out; + if ( l2e_get_flags(*pl2e) & _PAGE_PSE ) + { + *flags =3D lNf_to_l1f(l2e_get_flags(*pl2e)) | + (l2e_get_intpte(*pl2e) & _PAGE_PSE_PAT ? _PAGE_PAT : 0); + ret =3D _mfn((l2e_get_pfn(*pl2e) & ~((1UL << PAGETABLE_ORDER) - 1)= ) + + l1_table_offset(va)); + goto out; + } + + pl1e =3D map_l1t_from_l2e(*pl2e) + l1_table_offset(va); + shift =3D PAGE_SHIFT; + if ( l1e_get_flags(*pl1e) & _PAGE_PRESENT ) + { + *flags =3D l1e_get_flags(*pl1e); + ret =3D l1e_get_mfn(*pl1e); + } + + out: + L3T_UNLOCK(l3page); + unmap_domain_page(pl1e); + unmap_domain_page(pl2e); + unmap_domain_page(pl3e); + *nr =3D ((((va >> shift) + 1) << shift) - va) >> PAGE_SHIFT; + return ret; +} +#endif /* CONFIG_SPARSE_DIRECTMAP */ + +/* + * Free a page-table page that the hierarchy rooted at @root has stopped + * using. One of idle_pg_table's may be in the sparse directmap view: it = is + * taken out first, under the sparse view's L3 lock inside an L3 lock of + * idle_pg_table's hierarchy (the only order in which the two nest), and + * kept if that fails. The sparse view's own page-tables are not in it, a= nd + * are freed directly: the caller may hold the sparse view's L3 lock. + */ +static void free_pagetable_in(const l4_pgentry_t *root, mfn_t mfn) +{ + if ( root =3D=3D idle_pg_table && sparse_dmap_drop_pagetable(mfn) ) + return; + + free_xen_pagetable(mfn); +} + /* * map_pages_to_xen() in the hierarchy rooted at @root: idle_pg_table's, or * another hierarchy sharing its virtual addresses. */ -static int map_pages_in( +int map_pages_in( l4_pgentry_t *root, unsigned long virt, mfn_t mfn, @@ -5531,10 +5622,10 @@ static int map_pages_in( ol2e =3D l2t[i]; if ( (l2e_get_flags(ol2e) & _PAGE_PRESENT) && !(l2e_get_flags(ol2e) & _PAGE_PSE) ) - free_xen_pagetable(l2e_get_mfn(ol2e)); + free_pagetable_in(root, l2e_get_mfn(ol2e)); } unmap_domain_page(l2t); - free_xen_pagetable(l3e_get_mfn(ol3e)); + free_pagetable_in(root, l3e_get_mfn(ol3e)); } } =20 @@ -5632,7 +5723,7 @@ static int map_pages_in( flush_flags(l1e_get_flags(l1t[i])); flush_area(virt, flush_flags); unmap_domain_page(l1t); - free_xen_pagetable(l2e_get_mfn(ol2e)); + free_pagetable_in(root, l2e_get_mfn(ol2e)); } } =20 @@ -5764,7 +5855,7 @@ static int map_pages_in( flush_area(virt - PAGE_SIZE, FLUSH_TLB_GLOBAL | FLUSH_ORDER(PAGETABLE_ORDER)); - free_xen_pagetable(l2e_get_mfn(ol2e)); + free_pagetable_in(root, l2e_get_mfn(ol2e)); } else if ( locking ) spin_unlock(&map_pgdir_lock); @@ -5811,7 +5902,7 @@ static int map_pages_in( flush_area(virt - PAGE_SIZE, FLUSH_TLB_GLOBAL | FLUSH_ORDER(2*PAGETABLE_ORDER)); - free_xen_pagetable(l3e_get_mfn(ol3e)); + free_pagetable_in(root, l3e_get_mfn(ol3e)); } else if ( locking ) spin_unlock(&map_pgdir_lock); @@ -5838,7 +5929,12 @@ int map_pages_to_xen( unsigned long nr_mfns, pte_attr_t flags) { - return map_pages_in(idle_pg_table, virt, mfn, nr_mfns, flags); + int rc =3D map_pages_in(idle_pg_table, virt, mfn, nr_mfns, flags); + + if ( !rc ) + rc =3D sparse_dmap_mirror_map(virt, mfn, nr_mfns, flags); + + return rc; } =20 int __init populate_pt_range(unsigned long virt, unsigned long nr_mfns) @@ -5861,8 +5957,8 @@ int __init populate_pt_range(unsigned long virt, unsi= gned long nr_mfns) * modify_mappings_in() does so in the hierarchy rooted at @root, as * map_pages_in() does. */ -static int modify_mappings_in(l4_pgentry_t *root, unsigned long s, - unsigned long e, pte_attr_t nf) +int modify_mappings_in(l4_pgentry_t *root, unsigned long s, + unsigned long e, pte_attr_t nf) { bool locking =3D system_state > SYS_STATE_boot; l3_pgentry_t *pl3e =3D NULL; @@ -6072,7 +6168,7 @@ static int modify_mappings_in(l4_pgentry_t *root, uns= igned long s, if ( locking ) spin_unlock(&map_pgdir_lock); flush_area(NULL, FLUSH_TLB_GLOBAL); /* flush before free */ - free_xen_pagetable(l1mfn); + free_pagetable_in(root, l1mfn); } else if ( locking ) spin_unlock(&map_pgdir_lock); @@ -6116,7 +6212,7 @@ static int modify_mappings_in(l4_pgentry_t *root, uns= igned long s, if ( locking ) spin_unlock(&map_pgdir_lock); flush_area(NULL, FLUSH_TLB_GLOBAL); /* flush before free */ - free_xen_pagetable(l2mfn); + free_pagetable_in(root, l2mfn); } else if ( locking ) spin_unlock(&map_pgdir_lock); @@ -6142,7 +6238,12 @@ static int modify_mappings_in(l4_pgentry_t *root, un= signed long s, =20 int modify_xen_mappings(unsigned long s, unsigned long e, pte_attr_t nf) { - return modify_mappings_in(idle_pg_table, s, e, nf); + int rc =3D modify_mappings_in(idle_pg_table, s, e, nf); + + if ( !rc ) + rc =3D sparse_dmap_mirror_modify(s, e, nf); + + return rc; } =20 int destroy_xen_mappings(unsigned long s, unsigned long e) diff --git a/xen/arch/x86/pv/dom0_build.c b/xen/arch/x86/pv/dom0_build.c index dcb1d407ec..f18959f3b4 100644 --- a/xen/arch/x86/pv/dom0_build.c +++ b/xen/arch/x86/pv/dom0_build.c @@ -22,6 +22,7 @@ #include #include #include +#include =20 /* Allow ring-3 access in long mode as guest cannot use ring 1 ... */ #define BASE_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER) @@ -650,6 +651,9 @@ static int __init dom0_construct(const struct boot_doma= in *bd) { if ( assign_pages(mfn_to_page(_mfn(initrd_mfn)), count, d, 0) ) BUG(); + /* The initrd is dom0's now: out of the sparse directmap. */ + if ( sparse_dmap_remove(_mfn(initrd_mfn), PFN_UP(initrd_len)) ) + panic("Cannot unmap dom0's initrd\n"); /* * We have mapped the initrd directly into dom0, and assigned = the * pages. Tell the boot_module handling that we've freed it, s= o the diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c index fef3c2dd44..f95aa36e05 100644 --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -54,6 +54,7 @@ #include #include #include +#include #include #include =20 @@ -1944,6 +1945,9 @@ void asmlinkage __init noreturn __start_xen(void) =20 system_state =3D SYS_STATE_boot; =20 + /* The heap now has all memory: page-tables come from it from here on.= */ + sparse_dmap_init(); + bsp_stack =3D cpu_alloc_stack(0); /* Needs to know IDT vs FRED */ if ( !bsp_stack ) panic("No memory for BSP stack\n"); diff --git a/xen/arch/x86/sparse-directmap.c b/xen/arch/x86/sparse-directma= p.c new file mode 100644 index 0000000000..d35a8d6979 --- /dev/null +++ b/xen/arch/x86/sparse-directmap.c @@ -0,0 +1,431 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * A sparse view of the directmap. + * + * Xen maps all of RAM at DIRECTMAP_VIRT_START, and every root page-table + * carries those mappings. The sparse view is a second set of page-tables + * for the same virtual addresses (the part of the directmap PV guests can + * see), mapping only what Xen needs to reach through the directmap at all + * times: + * + * - Memory never handed to the heap: Xen's image, boot-time allocations, + * firmware tables. Not a PV dom0's initrd, used in place, nor the frame + * table and M2P pages carved from hot-added memory. + * - Memory handed to the heap, then allocated to the xenheap. + * + * The rest of the heap's memory, where guest memory comes from, is not + * mapped. Every mapping in the sparse view is either identical to the + * full directmap's at the same address, or empty. + * + * Guest contexts selected on the command line run on root page-tables + * carrying the sparse view's L3s in the directmap slots. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +bool __ro_after_init opt_sparse_dmap_pv; +bool __ro_after_init opt_sparse_dmap_hvm; +bool __ro_after_init opt_sparse_dmap_hwdom; + +/* Root of the sparse view: only ever walked and copied from, never loaded= . */ +static l4_pgentry_t __aligned(PAGE_SIZE) sparse_l4[L4_PAGETABLE_ENTRIES]; +l4_pgentry_t *__ro_after_init sparse_dmap_root; + +/* + * Before the sparse view is built: the MFN ranges handed to the heap, whi= ch + * it leaves out, and the xenheap allocations made since, which it puts ba= ck. + */ +struct mfn_range { + unsigned long s, e; +}; +static struct mfn_range early_heap[256]; +static unsigned int nr_early_heap; +static struct mfn_range early_xenheap[2 * MAX_NUMNODES]; +static unsigned int nr_early_xenheap; +static bool early_overflow; + +/* + * Clip [*mfn, *mfn + *nr) to what the sparse view covers; false if nothin= g is + * left. + */ +static bool clip_to_sparse_view(unsigned long *mfn, unsigned long *nr) +{ + unsigned long end =3D SPARSE_DMAP_END_MFN; + + if ( *mfn >=3D end ) + return false; + if ( *nr > end - *mfn ) + *nr =3D end - *mfn; + + return *nr; +} + +static void early_range_add(struct mfn_range *r, unsigned int *nr, + unsigned int max, unsigned long s, unsigned lo= ng e) +{ + if ( *nr && r[*nr - 1].e =3D=3D s ) + r[*nr - 1].e =3D e; + else if ( *nr < max ) + r[(*nr)++] =3D (struct mfn_range){ s, e }; + else + early_overflow =3D true; +} + +static void early_range_del(struct mfn_range *r, unsigned int *nr, + unsigned long s, unsigned long e) +{ + unsigned int i; + + for ( i =3D 0; i < *nr; i++ ) + if ( r[i].s =3D=3D s && r[i].e =3D=3D e ) + { + r[i] =3D r[--*nr]; + return; + } +} + +/* + * Copy the full directmap's mappings of MFNs [mfn, mfn + nr) into the spa= rse + * view. + */ +static int add_to_sparse_view(l4_pgentry_t *root, unsigned long mfn, + unsigned long nr) +{ + unsigned long start =3D (unsigned long)mfn_to_virt(mfn); + unsigned long va =3D start, end =3D start + (nr << PAGE_SHIFT); + + while ( va < end ) + { + pte_attr_t flags; + unsigned long n; + mfn_t va_mfn =3D xen_mapping_lookup(idle_pg_table, va, &flags, &n); + int rc; + + ASSERT(mfn_eq(va_mfn, INVALID_MFN) || + mfn_x(va_mfn) =3D=3D mfn + ((va - start) >> PAGE_SHIFT)); + n =3D min(n, (end - va) >> PAGE_SHIFT); + if ( !mfn_eq(va_mfn, INVALID_MFN) && + (rc =3D map_pages_in(root, va, va_mfn, n, flags)) ) + return rc; + va +=3D n << PAGE_SHIFT; + } + + return 0; +} + +static int remove_from_sparse_view(unsigned long mfn, unsigned long nr) +{ + unsigned long va =3D (unsigned long)mfn_to_virt(mfn); + + return modify_mappings_in(sparse_dmap_root, va, va + (nr << PAGE_SHIFT= ), + _PAGE_NONE); +} + +/* Memory is about to be handed to the heap allocator. */ +bool arch_heap_pages_init(mfn_t mfn, unsigned long nr) +{ + unsigned long s =3D mfn_x(mfn); + + if ( !sparse_dmap_configured() || !clip_to_sparse_view(&s, &nr) ) + return true; + + if ( !sparse_dmap_root ) + { + early_range_add(early_heap, &nr_early_heap, ARRAY_SIZE(early_heap), + s, s + nr); + return true; + } + + if ( remove_from_sparse_view(s, nr) ) + { + printk(XENLOG_ERR + "sparse directmap: cannot unmap MFNs %#lx-%#lx, not freeing= \n", + s, s + nr - 1); + return false; + } + + return true; +} + +/* + * Xenheap pages enter the sparse view, which guest contexts run on: those + * still holding a previous owner's data must be scrubbed first. + */ +bool arch_xenheap_needs_scrub(void) +{ + return sparse_dmap_configured(); +} + +/* Pages have been allocated from the heap as xenheap pages. */ +int arch_xenheap_pages_alloc(mfn_t mfn, unsigned int order) +{ + unsigned long s =3D mfn_x(mfn), nr =3D 1UL << order; + + if ( !sparse_dmap_configured() ) + return 0; + + /* Xenheap pages must be reachable through the sparse view. */ + BUG_ON(s + nr > SPARSE_DMAP_END_MFN); + + if ( !sparse_dmap_root ) + { + early_range_add(early_xenheap, &nr_early_xenheap, + ARRAY_SIZE(early_xenheap), s, s + nr); + return 0; + } + + return add_to_sparse_view(sparse_dmap_root, s, nr); +} + +/* + * Xenheap pages are about to be freed. A failure means the pages may sti= ll + * be reachable through the sparse view, and must not be reused. + */ +int arch_xenheap_pages_free(mfn_t mfn, unsigned int order) +{ + unsigned long s =3D mfn_x(mfn), nr =3D 1UL << order; + + if ( !sparse_dmap_configured() ) + return 0; + + BUG_ON(s + nr > SPARSE_DMAP_END_MFN); + + if ( !sparse_dmap_root ) + { + early_range_del(early_xenheap, &nr_early_xenheap, s, s + nr); + return 0; + } + + return remove_from_sparse_view(s, nr); +} + +/* Memory leaves Xen's hands without passing through the heap. */ +int sparse_dmap_remove(mfn_t mfn, unsigned long nr) +{ + return arch_heap_pages_init(mfn, nr) ? 0 : -ENOMEM; +} + +/* + * A page-table page of idle_pg_table's hierarchy is about to be freed. O= ne + * the boot allocator provided is in the sparse view, as every boot-time + * allocation is: take it out before the heap may hand it on. One allocat= ed + * since came from the heap and is not in the sparse view, which a lookup + * finds without changing, or flushing, anything. Non-zero: the page may + * still be mapped, and must not be freed. + */ +int sparse_dmap_drop_pagetable(mfn_t mfn) +{ + unsigned long s =3D mfn_x(mfn), nr =3D 1, n; + pte_attr_t flags; + int rc; + + if ( !sparse_dmap_root || !clip_to_sparse_view(&s, &nr) || + mfn_eq(xen_mapping_lookup(sparse_dmap_root, + (unsigned long)mfn_to_virt(s), &flags, = &n), + INVALID_MFN) ) + return 0; + + rc =3D remove_from_sparse_view(s, 1); + if ( rc ) + printk(XENLOG_WARNING + "Leaking page-table page %"PRI_mfn": still in the sparse di= rectmap\n", + mfn_x(mfn)); + + return rc; +} + +int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, unsigned long nr, + pte_attr_t flags) +{ + if ( !sparse_dmap_root || virt < SPARSE_DMAP_START || + virt >=3D SPARSE_DMAP_END ) + return 0; + + nr =3D min(nr, (SPARSE_DMAP_END - virt) >> PAGE_SHIFT); + + return map_pages_in(sparse_dmap_root, virt, mfn, nr, flags); +} + +int sparse_dmap_mirror_modify(unsigned long s, unsigned long e, + pte_attr_t nf) +{ + unsigned long va; + + if ( !sparse_dmap_root || e <=3D SPARSE_DMAP_START || + s >=3D SPARSE_DMAP_END ) + return 0; + + s =3D max(s, SPARSE_DMAP_START); + e =3D min(e, SPARSE_DMAP_END); + + if ( !(nf & _PAGE_PRESENT) ) + return modify_mappings_in(sparse_dmap_root, s, e, nf); + + /* + * A permission change applies where the sparse view maps anything: it + * never creates mappings, and the sparse view need not map all of [s,= e). + */ + for ( va =3D s; va < e; ) + { + pte_attr_t flags; + unsigned long n; + mfn_t mfn =3D xen_mapping_lookup(sparse_dmap_root, va, &flags, &n); + int rc; + + n =3D min(n, (e - va) >> PAGE_SHIFT); + if ( !mfn_eq(mfn, INVALID_MFN) && + (rc =3D modify_mappings_in(sparse_dmap_root, va, + va + (n << PAGE_SHIFT), nf)) ) + return rc; + va +=3D n << PAGE_SHIFT; + } + + return 0; +} + +static int __init cf_check cmp_mfn_range(const void *a, const void *b) +{ + const struct mfn_range *l =3D a, *r =3D b; + + if ( l->s > r->s ) + return 1; + if ( l->s < r->s ) + return -1; + + return 0; +} + +static void __init cf_check swap_mfn_range(void *a, void *b) +{ + SWAP(*(struct mfn_range *)a, *(struct mfn_range *)b); +} + +/* + * Build the sparse view: the full directmap, minus the ranges handed to t= he + * heap so far, plus the xenheap allocations made from them. Called once = the + * heap has taken over from the boot allocator, so that the page-tables co= me + * from the heap, and before any other CPU is up. + */ +void __init sparse_dmap_init(void) +{ + unsigned long va, mapped =3D 0; + unsigned int i, heap_idx; + + if ( !sparse_dmap_configured() ) + return; + + if ( early_overflow ) + { + printk(XENLOG_WARNING + "Sparse directmap: too many early heap ranges, disabled\n"); + opt_sparse_dmap_pv =3D opt_sparse_dmap_hvm =3D opt_sparse_dmap_hwd= om =3D + false; + return; + } + + /* An L3 for every slot, so that the L4 entries never change. */ + for ( va =3D SPARSE_DMAP_START; va < SPARSE_DMAP_END; + va +=3D 1UL << L4_PAGETABLE_SHIFT ) + { + mfn_t l3mfn; + void *l3t =3D alloc_mapped_pagetable(&l3mfn); + + if ( !l3t ) + panic("Sparse directmap: out of memory\n"); + unmap_domain_page(l3t); + sparse_l4[l4_table_offset(va)] =3D + l4e_from_mfn(l3mfn, __PAGE_HYPERVISOR); + } + + sort(early_heap, nr_early_heap, sizeof(*early_heap), cmp_mfn_range, + swap_mfn_range); + + /* + * The full directmap, leaf by leaf, minus the ranges added to the + * heap. Leaves come in address order, which is MFN order in the + * directmap, and the heap ranges are sorted and disjoint, so + * heap_idx only moves forward. + */ + for ( va =3D SPARSE_DMAP_START, heap_idx =3D 0; va < SPARSE_DMAP_END; ) + { + pte_attr_t flags; + unsigned long n, s, e; + + /* Get information about the leaf PTE at va. */ + mfn_t mfn =3D xen_mapping_lookup(idle_pg_table, va, &flags, &n); + + /* Clip the va range we're considering to SPARSE_DMAP_END. */ + n =3D min(n, (SPARSE_DMAP_END - va) >> PAGE_SHIFT); + if ( mfn_eq(mfn, INVALID_MFN) ) + { + va +=3D n << PAGE_SHIFT; + continue; + } + + /* + * Walk through the range [mfn, mfn + n) looking for ranges not + * handed to the heap. + */ + for ( s =3D mfn_x(mfn), e =3D s + n; s < e; ) + { + unsigned long upto =3D e; + + /* Step past heap ranges that end at or before s. */ + while ( heap_idx < nr_early_heap && early_heap[heap_idx].e <= =3D s ) + heap_idx++; + + /* s lies in a heap range: skip to its end. */ + if ( heap_idx < nr_early_heap && early_heap[heap_idx].s <=3D s= ) + { + s =3D min(e, early_heap[heap_idx].e); + continue; + } + + /* Map up to the next heap range, or to the end of the leaf. */ + if ( heap_idx < nr_early_heap ) + upto =3D min(e, early_heap[heap_idx].s); + + if ( map_pages_in(sparse_l4, va + ((s - mfn_x(mfn)) << PAGE_SH= IFT), + _mfn(s), upto - s, flags) ) + panic("Sparse directmap: out of memory\n"); + mapped +=3D upto - s; + s =3D upto; + } + + va +=3D n << PAGE_SHIFT; + } + + /* + * Now add back in ranges from the heap which were allocated to + * the xenheap. + */ + for ( i =3D 0; i < nr_early_xenheap; i++ ) + { + unsigned long nr =3D early_xenheap[i].e - early_xenheap[i].s; + + if ( add_to_sparse_view(sparse_l4, early_xenheap[i].s, nr) ) + panic("Sparse directmap: out of memory\n"); + mapped +=3D nr; + } + + /* + * Nothing in the directmap is executed: its slots are NX, as the full + * directmap's are. + */ + if ( cpu_has_nx ) + for ( i =3D l4_table_offset(SPARSE_DMAP_START); + i < l4_table_offset(SPARSE_DMAP_END); i++ ) + l4e_add_flags(sparse_l4[i], _PAGE_NX_BIT); + + sparse_dmap_root =3D sparse_l4; + + printk(XENLOG_INFO "Sparse directmap: %lu pages mapped\n", mapped); +} diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c index 7b04537066..c5d9dd64a3 100644 --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -1231,8 +1231,14 @@ int memory_add(unsigned long spfn, unsigned long epf= n, unsigned int pxm) i =3D virt_to_mfn(HYPERVISOR_VIRT_END - 1) + 1; if ( spfn < i ) { - ret =3D map_pages_to_xen((unsigned long)mfn_to_virt(spfn), _mfn(sp= fn), - min(epfn, i) - spfn, PAGE_HYPERVISOR); + /* + * Into the full directmap only, not a sparse directmap view: the + * frame table and M2P carved from the start of the range are + * reached through their own mappings, and the rest goes to the + * heap, which the sparse view leaves out. + */ + ret =3D map_pages_in(idle_pg_table, (unsigned long)mfn_to_virt(spf= n), + _mfn(spfn), min(epfn, i) - spfn, PAGE_HYPERVISO= R); if ( ret ) goto destroy_directmap; } diff --git a/xen/common/Kconfig b/xen/common/Kconfig index 5b289e444f..8f2f605e45 100644 --- a/xen/common/Kconfig +++ b/xen/common/Kconfig @@ -167,6 +167,9 @@ config HAS_STATIC_MEMORY config HAS_SOFT_RESET bool =20 +config HAS_SPARSE_DIRECTMAP + bool + config HAS_STACK_PROTECTOR bool =20 @@ -299,6 +302,21 @@ config SPECULATIVE_HARDEN_LOCK This option is disabled by default at run time, and needs to be enabled on the command line. =20 + +config SPARSE_DIRECTMAP + bool "Sparse directmap view" + depends on HAS_SPARSE_DIRECTMAP + help + Build support for a sparse view of the directmap, alongside the + full one: it maps only the memory Xen needs to reach at all times + (the Xen heap, Xen's own image, firmware tables), while guest + memory is reached through transient mappings. Guest contexts + selected with the `asi=3D` command line option run on the sparse + directmap view, narrowing the memory reachable from Xen's address + space while they run, and hence the memory disclosure surface of + speculative side channels. + + If unsure, say N. endmenu =20 menu "Other hardening" diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index fe150eccd7..69e8e982b6 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -1971,6 +1971,9 @@ static void init_heap_pages( unsigned long i; bool need_scrub =3D scrub_debug; =20 + if ( !arch_heap_pages_init(page_to_mfn(pg), nr_pages) ) + return; + /* * Keep MFN 0 away from the buddy allocator to avoid crossing zone * boundary when merging two buddies. @@ -2540,10 +2543,33 @@ void *alloc_xenheap_pages(unsigned int order, unsig= ned int memflags) if ( !(memflags >> _MEMF_bits) ) memflags |=3D MEMF_bits(xenheap_bits); =20 - pg =3D alloc_domheap_pages(NULL, order, memflags | MEMF_no_scrub); + /* + * Pages still awaiting scrubbing may do as xenheap pages: the directm= ap + * maps them anyway. Not so if an arch's sparse directmap view makes = them + * reachable where the rest of the heap's memory is not. + */ + if ( !arch_xenheap_needs_scrub() ) + memflags |=3D MEMF_no_scrub; + + pg =3D alloc_domheap_pages(NULL, order, memflags); if ( unlikely(pg =3D=3D NULL) ) return NULL; =20 + if ( arch_xenheap_pages_alloc(page_to_mfn(pg), order) ) + { + /* + * The hook may have mapped part of the pages before failing: take + * them out again as a free would, and leak them if that fails too. + */ + if ( arch_xenheap_pages_free(page_to_mfn(pg), order) ) + printk(XENLOG_WARNING + "Leaking xenheap pages %"PRI_mfn"+%u: still mapped\n", + mfn_x(page_to_mfn(pg)), 1U << order); + else + free_domheap_pages(pg, order); + return NULL; + } + for ( i =3D 0; i < (1u << order); i++ ) pg[i].count_info |=3D PGC_xen_heap; =20 @@ -2562,6 +2588,19 @@ void free_xenheap_pages(void *v, unsigned int order) =20 pg =3D virt_to_page(v); =20 + /* + * If the pages cannot be taken out of an arch's sparse directmap view, + * they may remain reachable through it: leak them rather than hand th= em + * out. + */ + if ( arch_xenheap_pages_free(page_to_mfn(pg), order) ) + { + printk(XENLOG_WARNING + "Leaking xenheap pages %"PRI_mfn"+%u: still mapped\n", + mfn_x(page_to_mfn(pg)), 1U << order); + return; + } + for ( i =3D 0; i < (1u << order); i++ ) pg[i].count_info &=3D ~PGC_xen_heap; =20 diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index 153c57f5dd..2bcc45cf58 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -94,6 +94,41 @@ bool scrub_free_pages(void); #define alloc_xenheap_page() (alloc_xenheap_pages(0,0)) #define free_xenheap_page(v) (free_xenheap_pages(v,0)) =20 +/* + * Hooks for an architecture keeping a second, sparse view of its directma= p, + * which maps the xenheap but not the rest of the heap's memory. + */ +#ifdef CONFIG_SPARSE_DIRECTMAP +/* Memory is about to be handed to the heap. False: don't, keep it out. */ +bool arch_heap_pages_init(mfn_t mfn, unsigned long nr); +/* Xenheap pages are to be allocated. True: scrub those needing it first.= */ +bool arch_xenheap_needs_scrub(void); +/* + * Xenheap pages were allocated. Non-zero: failed, possibly part-way; the + * pages go through arch_xenheap_pages_free() before being freed again. + */ +int arch_xenheap_pages_alloc(mfn_t mfn, unsigned int order); +/* Xenheap pages are to be freed. Non-zero: failed, don't reuse them. */ +int arch_xenheap_pages_free(mfn_t mfn, unsigned int order); +#else +static inline bool arch_heap_pages_init(mfn_t mfn, unsigned long nr) +{ + return true; +} +static inline bool arch_xenheap_needs_scrub(void) +{ + return false; +} +static inline int arch_xenheap_pages_alloc(mfn_t mfn, unsigned int order) +{ + return 0; +} +static inline int arch_xenheap_pages_free(mfn_t mfn, unsigned int order) +{ + return 0; +} +#endif + /* Free an allocation, and zero the pointer to it. */ #define FREE_XENHEAP_PAGES(p, o) do { \ void *_ptr_ =3D (p); \ --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439746; cv=none; d=zohomail.com; s=zohoarc; b=oI/eWxwDgfZF21ZbyhCSNMxmHkzXOkCFbTBHRB08SRx05h3mqX6azDJTwczHkAzgsZoZQu3ZYV/Aqd/w3FGAmOhgLBk8LF7my94tnryo8ox34gxcmq40L6odpxi+zO86fKVgTDI9ridDV0X5DwbATtORwCfniAb5An281m/U7x0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439746; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=IljL9ERbJZTdTGatiECVH6x/M/gfKWv60kBNbg4WwnQ=; b=Cx/+MIH6Vx6qw/wivTiwPffwfhZ/QvGsCUJGuUYfqtfQEdqoDKkPEjNBapDuytofJIfyiYCuZbKozkzeA/HJeLJfLEHHCB97cSZiVwTU5XWy58m69bmcQ+jQJMPbqLardZ0kOMsz9ViXc8s/i90iR6ko95y8dvSJIw6Y9DkAPic= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439746377116.03384115364202; Mon, 19 Oct 2026 12:55:46 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454846.1668466 (Exim 4.92) (envelope-from ) id 1xItS9-0005zL-FZ; Mon, 19 Oct 2026 19:55:25 +0000 Received: by outflank-mailman (output) from mailman id 1454846.1668466; Mon, 19 Oct 2026 19:55:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS8-0005rb-Mo; Mon, 19 Oct 2026 19:55:24 +0000 Received: by outflank-mailman (input) for mailman id 1454846; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003i0-QS for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-0000000230A-3wyE; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGf-00000001FjN-12Qo; Mon, 19 Oct 2026 19:43:33 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=IljL9ERbJZTdTGatiECVH6x/M/gfKWv60kBNbg4WwnQ=; b=iqGzzSm0J/I43i2gk1lTExYrvR SnW6w+qJiJ1EroduJaeymaSVWRtf3EydBaZp1LR8/avjxkq8/tnVXswKyqbCLVsoWZM390KclAoiZ GkliKUC5x3LUxUU5FieyQJe41nSAmwrMYYMmfXYm8wAz0C6V1+IOyame+senp930EiDo=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 17/30] x86/domain_page: key the directmap fast paths on the loaded view Date: Mon, 19 Oct 2026 20:42:48 +0100 Message-ID: <20261019-asi-hvm-v4-17-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439747458158500 Content-Type: text/plain; charset="utf-8" map_domain_page() returns directmap addresses whenever it can: for any MFN the directmap covers in release builds, and for vCPUs without a mapcache. Once guest contexts may run on the sparse view of the directmap, modify this to take the fast path returning the directmap address only when the full directmap is loaded. The release-build shortcut ahead of the vCPU lookup looks at the vCPU whose page-tables are loaded, without completing a lazy context switch: if that vCPU is not on the sparse view, its page-tables map the page through the directmap, and completing the switch would only move the CPU to the idle vCPU's page-tables, which map it too. A vCPU on the sparse view always has a mapcache: HVM ones through per-vCPU page-tables, PV ones through the domain-wide mapcache, which mapcache_domain_init() now sets up for a domain using the sparse view in release builds too, rather than only when the directmap does not cover all memory. map_domain_page_global() mappings must be valid in every context: use vmap() for all of them once a sparse view is configured, as arch_mfns_in_directmap() then says. Add d->arch.asi.sparse_dmap for this; nothing sets it yet. No functional change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - d->arch.sparse_dmap is now a one-bit field of d->arch.asi, beside vcpu_pt. - Wrap a line that reached 80 columns. - Drop sparse_dmap_active: d->arch.asi.sparse_dmap alone says whether a domain's root page-tables carry the sparse view (Roger). Builds without CONFIG_SPARSE_DIRECTMAP compile the test out, as before. - Drop the second directmap check after the vCPU lookup: the !cache->inuse case covers it. Changes in v3: - New in this version. --- xen/arch/x86/domain_page.c | 34 ++++++++++++++++++++++++++++--- xen/arch/x86/include/asm/domain.h | 3 +++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 770fee80a4..6b662706fb 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -56,6 +56,13 @@ static inline struct vcpu *mapcache_current_vcpu(void) return ACCESS_ONCE(this_cpu(pgtable_vcpu)); } =20 +/* Whether @v's page-tables carry the sparse view of the directmap. */ +static bool on_sparse_dmap(const struct vcpu *v) +{ + return IS_ENABLED(CONFIG_SPARSE_DIRECTMAP) && + v->domain->arch.asi.sparse_dmap; +} + /* * The accounting for v's mapcache: its own for a vCPU-PT domain, the doma= in's * otherwise, in which case *dcache is set so the caller takes the domain-= wide @@ -135,9 +142,19 @@ void *map_domain_page(mfn_t mfn) =20 #ifdef NDEBUG if ( mfn_x(mfn) <=3D PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) ) - return mfn_to_virt(mfn_x(mfn)); + { + const struct vcpu *pt_vcpu =3D this_cpu(pgtable_vcpu); + + /* + * The page-tables loaded map @mfn through the directmap, unless t= hey + * carry the sparse view. + */ + if ( !pt_vcpu || !on_sparse_dmap(pt_vcpu) ) + return mfn_to_virt(mfn_x(mfn)); + } #endif =20 + /* No vCPU (early boot), or no mapcache: the full directmap is loaded.= */ v =3D mapcache_current_vcpu(); if ( !v ) return mfn_to_virt(mfn_x(mfn)); @@ -145,7 +162,10 @@ void *map_domain_page(mfn_t mfn) cache =3D vcpu_mapcache(v, &dcache); vcache =3D &v->arch.mapcache; if ( !cache->inuse ) + { + ASSERT(!on_sparse_dmap(v)); return mfn_to_virt(mfn_x(mfn)); + } =20 perfc_incr(map_domain_page_count); =20 @@ -294,7 +314,11 @@ void mapcache_domain_init(struct domain *d) ASSERT(is_pv_domain(d)); =20 #ifdef NDEBUG - if ( !mem_hotplug && max_page <=3D PFN_DOWN(__pa(HYPERVISOR_VIRT_END -= 1)) ) + /* + * Not needed when the directmap loaded for the domain covers everythi= ng. + */ + if ( !d->arch.asi.sparse_dmap && !mem_hotplug && + max_page <=3D PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) ) return; #endif =20 @@ -378,8 +402,12 @@ void *map_domain_page_global(mfn_t mfn) system_state < SYS_STATE_active) || local_irq_is_enabled())); =20 + /* + * Global mappings must hold in every context: a sparse view of the + * directmap rules the directmap out. + */ #ifdef NDEBUG - if ( mfn_x(mfn) <=3D PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) ) + if ( arch_mfns_in_directmap(mfn_x(mfn), 1) ) return mfn_to_virt(mfn_x(mfn)); #endif =20 diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 7076e38848..f84ac15759 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -490,6 +490,9 @@ struct arch_domain struct { /* Give each vCPU its own per-domain area (HVM only). */ bool vcpu_pt:1; + + /* The domain's root page-tables carry the sparse directmap view. = */ + bool sparse_dmap:1; } asi; =20 /* Emulated devices enabled bitmap. */ --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439738; cv=none; d=zohomail.com; s=zohoarc; b=iUAOfm/DVrDPjJbxmzgMz1FYaxmkzSfYMjVPBDBPyk3nNKw+QT3oKJRDMgyM2DMgd0jtQSPL7Q9nX2um8mJdMH803CIzilm+vx90BT/QPpQmM8d2lUl2JteaDitBcJWrshRE+zI0N6HQ0SrkM/WRoRPm0x2BHB30J1YQ5rN6BgY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439738; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=dbeZKV9O+bmNdrh0qt89ublfkcV74oImeh0hl17sAps=; b=Ag88fIUGCmQZJDjAYyfeHMjw/T8XU/ujyST9amR+3lAnFEYxN7O2qJ3YAzUHFE16yvv9wH9Mtvq34Bcj4gL4jgTu9CSaSO2MGqUpalHM8mPzlDc6bVt6LobODyiY83PB7H04n/oyC7/fQ+h0hmTYH+dDWQUT1G7I2HLibVl2DTQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439738043877.0294164260808; Mon, 19 Oct 2026 12:55:38 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454845.1668459 (Exim 4.92) (envelope-from ) id 1xItS8-0005l2-B7; Mon, 19 Oct 2026 19:55:24 +0000 Received: by outflank-mailman (output) from mailman id 1454845.1668459; Mon, 19 Oct 2026 19:55:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS7-0005he-Sl; Mon, 19 Oct 2026 19:55:23 +0000 Received: by outflank-mailman (input) for mailman id 1454845; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003hw-P4 for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022zl-3YoA; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGg-00000001FjN-0lcO; Mon, 19 Oct 2026 19:43:34 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=dbeZKV9O+bmNdrh0qt89ublfkcV74oImeh0hl17sAps=; b=jgoFtm5p3xGtx39+eoGKrwQ/W9 5PsVIafcft7cfDUDsGeu2STkBtHg3nkAD2IxUNQz98NiQnpb50qQp+2QRWtnFcfa4VmlufVd+8Z/1 EtD349aaIv1/hXmZxn2mQ6HaIbLCAFlA/C1wqDs3zBhx8EQeQfkwPzDRfAz7dH3iH3e4=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 18/30] x86/domain_page: clear a mapcache slot at its last unmap Date: Mon, 19 Oct 2026 20:42:49 +0100 Message-ID: <20261019-asi-hvm-v4-18-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439739379158500 Content-Type: text/plain; charset="utf-8" unmap_domain_page() keeps a recently unmapped slot in the vCPU's maphash (MAPHASH_ENTRIES, 8 per vCPU) for a quick remap of the same MFN, and leaves its PTE in place until the entry is displaced by a later unmap or reclaimed by a full cache. Until then the page stays mapped, although nothing refers to the mapping and the mapcache holds no reference to the page. Where the mapcache clears a slot's PTE, it defers the flush: the slot's translation can stay in the TLB of the CPU which used it until that CPU next flushes. While the full directmap is loaded, that reaches nothing it does not map anyway. On the sparse view of the directmap, Xen reaches domain-heap memory only through mappings made for the purpose. A mapping or a translation left behind keeps the page reachable after its last unmap: while Xen serves another of the guest's contexts on the same vCPU, and, once the page is freed and handed to another domain, from a context which has nothing to do with it. So clear a slot's PTE when its last reference goes, maphash slots included: the hash entry keeps the slot, not the mapping, and a later hit maps the page again in the same slot. The slot has mapped nothing else in between, so that needs no flush. Reusing the slot for another page still goes through the garbage map, or through the replacement of a hash entry when the cache is full, both of which flush first. In a per-vCPU mapcache, as vCPU-PT domains have, also invalidate the slot's translation on the local CPU, right after clearing the PTE. The slot has a second address, in the mirror of the per-domain area that every domain but a 64-bit PV one gets for the compat argument translation area, and its translation is invalidated too: two INVLPGs. That leaves no translation on any CPU, as the vCPU's page-tables are loaded on one CPU at a time, and a CPU loading other page-tables (switch_cr3_cr4()) flushes non-global translations, the mapcache's included. A PV domain's domain-wide mapcache has its PTEs cleared but no INVLPG, as CPUs running the domain's other vCPUs may hold the slot's translation too, which a local INVLPG would not reach. For it, as before, builds with PV support flush a page freed by its owner from TLBs before the page is handed out again. The cost is one PTE write per unmap into the maphash and per remap from it, and in a per-vCPU mapcache two INVLPGs per slot's last unmap. A second unmap of an entry in the maphash now finds its PTE not present and is caught by the check unmap_domain_page() has for entries the first unmap zapped, rather than by the reference count's ASSERT() in debug builds only. The change does not depend on asi=3D or on a sparse view of the directmap being configured: PV vCPUs use the domain-wide mapcache on the default command line too. For them it adds the PTE writes above, and leaves up to MAPHASH_ENTRIES fewer pages per vCPU mapped in the domain-wide area. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - Merge v3's 15/18 ("x86/mm: flush owned pages before reuse with the sparse directmap view") and 16/18 ("x86/domain_page: keep the maphash's slots, not their mappings"). Rather than flush pages freed by their owner before reuse wherever a sparse view is configured, invalidate a per-vCPU mapcache slot's translations on the local CPU right after its PTE is cleared (Roger). - Message: don't restate when PV vCPUs use the mapcache (Roger). Changes in v3: - New in this version. For reviewers: with a slot cleared and invalidated at its last unmap, a per-vCPU mapcache's maphash saves only the search for a free slot, and a second slot for a page mapped twice at once. One page in two slots is fine, and already happens when it is not in the maphash. So per-vCPU mapcaches could drop the maphash, and with it their garbage map and wrap-time flush. In v5, or as a follow-up? --- xen/arch/x86/domain_page.c | 53 +++++++++++++++++++++++++------ xen/arch/x86/include/asm/domain.h | 5 ++- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 6b662706fb..4198ee7c18 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -130,6 +130,26 @@ static void mapcache_exit(struct mapcache_domain *dcac= he) #define MAPCACHE_L1ENT(idx) \ __linear_l1_table[l1_linear_offset(MAPCACHE_VIRT_START + pfn_to_paddr(= idx))] =20 +/* + * Clear a slot's PTE. For a per-vCPU mapcache (HVM domains only), then a= lso + * drop the slot's translations from this CPU's TLB, at its address and at= its + * alias in the per-domain mirror, which every HVM domain has. No other C= PU + * can hold them: the vCPU's page-tables are loaded on one CPU at a time, = and + * loading other page-tables flushes non-global translations. + */ +static void mapcache_zap(const struct mapcache_domain *dcache, + unsigned int idx) +{ + unsigned long va =3D MAPCACHE_VIRT_START + pfn_to_paddr(idx); + + l1e_write(&MAPCACHE_L1ENT(idx), l1e_empty()); + if ( dcache ) + return; + + flush_tlb_one_local(va); + flush_tlb_one_local(va - PERDOMAIN_VIRT_START + PERDOMAIN_ALT_VIRT_STA= RT); +} + void *map_domain_page(mfn_t mfn) { unsigned long flags; @@ -176,6 +196,14 @@ void *map_domain_page(mfn_t mfn) { idx =3D hashent->idx; ASSERT(idx < cache->entries); + /* + * An entry the maphash kept holds on to its slot, not to its mapp= ing + * (see unmap_domain_page()): map the page again. No flush is nee= ded, + * as the slot has mapped nothing else since. + */ + if ( !hashent->refcnt ) + l1e_write(&MAPCACHE_L1ENT(idx), + l1e_from_mfn(mfn, __PAGE_HYPERVISOR_RW)); hashent->refcnt++; ASSERT(hashent->refcnt); ASSERT(mfn_eq(l1e_get_mfn(MAPCACHE_L1ENT(idx)), mfn)); @@ -208,8 +236,8 @@ void *map_domain_page(mfn_t mfn) if ( hashent->idx !=3D MAPHASHENT_NOTINUSE && !hashent->re= fcnt ) { idx =3D hashent->idx; - ASSERT(l1e_get_pfn(MAPCACHE_L1ENT(idx)) =3D=3D hashent= ->mfn); - l1e_write(&MAPCACHE_L1ENT(idx), l1e_empty()); + ASSERT(!(l1e_get_flags(MAPCACHE_L1ENT(idx)) & + _PAGE_PRESENT)); hashent->idx =3D MAPHASHENT_NOTINUSE; hashent->mfn =3D ~0UL; break; @@ -273,32 +301,39 @@ void unmap_domain_page(const void *ptr) =20 local_irq_save(flags); =20 + /* + * The maphash keeps a freed slot, not its mapping: the page may change + * hands meanwhile. A domain-wide mapcache's translation left in a TLB + * is covered by the flush the allocator makes before reusing an owned + * page. + */ if ( hashent->idx =3D=3D idx ) { ASSERT(hashent->mfn =3D=3D mfn); ASSERT(hashent->refcnt); hashent->refcnt--; + if ( !hashent->refcnt ) + mapcache_zap(dcache, idx); } else if ( !hashent->refcnt ) { + /* The entry replaced has no mapping left: mark its slot as garbag= e. */ if ( hashent->idx !=3D MAPHASHENT_NOTINUSE ) { - /* /First/, zap the PTE. */ - ASSERT(l1e_get_pfn(MAPCACHE_L1ENT(hashent->idx)) =3D=3D - hashent->mfn); - l1e_write(&MAPCACHE_L1ENT(hashent->idx), l1e_empty()); - /* /Second/, mark as garbage. */ + ASSERT(!(l1e_get_flags(MAPCACHE_L1ENT(hashent->idx)) & + _PAGE_PRESENT)); set_bit(hashent->idx, cache->garbage); } =20 - /* Add newly-freed mapping to the maphash. */ + /* Add the newly-freed slot to the maphash, without its mapping. */ + mapcache_zap(dcache, idx); hashent->mfn =3D mfn; hashent->idx =3D idx; } else { /* /First/, zap the PTE. */ - l1e_write(&MAPCACHE_L1ENT(idx), l1e_empty()); + mapcache_zap(dcache, idx); /* /Second/, mark as garbage. */ set_bit(idx, cache->garbage); } diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index f84ac15759..0e405157b4 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -74,7 +74,10 @@ struct mapcache_vcpu { /* Shadow of mapcache_domain.epoch. */ unsigned int shadow_epoch; =20 - /* Lock-free per-VCPU hash of recently-used mappings. */ + /* + * Lock-free per-VCPU hash of recently-used mappings. An entry without + * references keeps its slot but not its mapping (see unmap_domain_pag= e()). + */ struct vcpu_maphash_entry { unsigned long mfn; uint32_t idx; --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439733; cv=none; d=zohomail.com; s=zohoarc; b=KRnPQCrbvJS7ZOh/TaTma/nIxa0Qpz+o18D9X6lwnvhfi31T2g6/2fPGNbmvJqB3rUfdBUdd+50bFQYfPHZFi7T2HyqJpwJidQavzeCS/yXobIpvOquJSbZd/OyWiiGXzk4DD0GxALaT0GaPGy/GGXEtllLbOPiYnP6z0Egy3jI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439733; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=LBUz4QCpPQtpmzUBSklZ+EZWcdBElnumvszUFYusF9k=; b=RNsvNrgPt2OphxdjxYX4Zl6x91EkfYt4iE1o1fFQpXRAxJR6CQZf2cbMsspyVP3nwaGdthEIT5p3GfW5BWa7BmJ6lKpwkaoDLgksXJQLvk7rFFSGKPWwL1IZEdnwoFiFVQVli9w6zrjPb7Az37pVzMqFBZRVJBSCfS8Q/s3S62s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439733327761.4133545038387; Mon, 19 Oct 2026 12:55:33 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454836.1668385 (Exim 4.92) (envelope-from ) id 1xItS2-00043n-2k; Mon, 19 Oct 2026 19:55:18 +0000 Received: by outflank-mailman (output) from mailman id 1454836.1668385; Mon, 19 Oct 2026 19:55:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS1-000411-TQ; Mon, 19 Oct 2026 19:55:17 +0000 Received: by outflank-mailman (input) for mailman id 1454836; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003h9-Gp for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yU-29Zu; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGh-00000001FjN-0TMy; Mon, 19 Oct 2026 19:43:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=LBUz4QCpPQtpmzUBSklZ+EZWcdBElnumvszUFYusF9k=; b=1rtHKqxdhSBzVicd7dD6yPpiul ELjFZ39fJpxFwN61r3SRdlfEgYrIREVUShAshyHO3gBkIVHIYENnN4NzC2H3IBkOnOfAaw0ULMa03 aXhWzl0RZk0U546ZweI0/n0yUYZWXgPE+E5xujFIOtGxhaTvP0FgWiVKYwIvfmgH/nhQ=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 19/30] x86/mm: run guest contexts on the sparse directmap view Date: Mon, 19 Oct 2026 20:42:50 +0100 Message-ID: <20261019-asi-hvm-v4-19-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439735373158500 Content-Type: text/plain; charset="utf-8" In init_xen_l4_slots(), if d->arch.asi.sparse_dmap is set, point the directmap slots at the sparse view, copying from idle_pg_table only the Xen slots below them. A root page-table not shared with a PV guest gets no extended directmap, which the sparse view does not cover. d->arch.asi.sparse_dmap is decided at domain creation: PV domains all have a mapcache, HVM domains need per-vCPU page-tables for one. No option asks for the sparse view yet. The sparse view is built before any domain is created, so the root page-tables of every domain using it carry it from their creation, dom0's included. Dom0's builders reach dom0's memory only through guest virtual addresses and map_domain_page(). In PV shim mode, replace_va_mapping() reads the guest's L4 through the directmap; map it with map_domain_page() instead, as the lower levels already are. Shadow L4s, dom0=3Dshadow's included, get the sparse view from init_xen_l4_slots(). The two directmaps differ where the sparse view leaves memory out, so no translation of the full directmap may remain usable on page-tables carrying the sparse view. Directmap mappings stay global nevertheless: every CR3 write which can move a CPU onto the sparse view goes through switch_cr3_cr4(), which flushes the old page-tables' translations, global entries included, before it returns. The CR3 writes which keep entries -- a PV vCPU's switch between its kernel and user page-tables, XPTI's entry and exit -- stay within one vCPU's page-tables, which carry the same directmap, or go to XPTI's per-CPU root, which maps only Xen's own per-CPU pages there. Removals from the sparse view are flushed on every CPU, global entries included. PV guests running with global pages, a PV dom0 not using the sparse view among them, so keep their directmap TLB entries across switches between guest kernel and user mode. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - d->arch.vcpu_pt and d->arch.sparse_dmap are now d->arch.asi.vcpu_pt and d->arch.asi.sparse_dmap. - Drop sparse_dmap_active and sparse_dmap_activate(): root page-tables carry the sparse view from their creation, dom0's included (Roger). - The sparse view's slots are now marked NX in "x86/mm: build and maintain a sparse view of the directmap". - PV shim: replace_va_mapping() maps the guest's L4 with map_domain_page(). - Give sparse_dmap_wanted() its hardware-domain case (Roger). - init_xen_l4_slots() takes the directmap slots from the sparse view, rather than copying the full directmap's and overwriting them; sparse_dmap_install() goes (Roger). - Message: drop the sentence on NX (Roger). Changes in v3: - New in this version. --- xen/arch/x86/include/asm/sparse-directmap.h | 9 +++++++ xen/arch/x86/mm.c | 28 +++++++++++++++++++-- xen/arch/x86/pv/shim.c | 4 ++- xen/arch/x86/spec_ctrl.c | 7 ++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/xen/arch/x86/include/asm/sparse-directmap.h b/xen/arch/x86/inc= lude/asm/sparse-directmap.h index ea14d44334..2f8b94f4c1 100644 --- a/xen/arch/x86/include/asm/sparse-directmap.h +++ b/xen/arch/x86/include/asm/sparse-directmap.h @@ -31,6 +31,14 @@ static inline bool sparse_dmap_configured(void) return opt_sparse_dmap_pv || opt_sparse_dmap_hvm || opt_sparse_dmap_hw= dom; } =20 +/* Whether a new domain of the given type is to use the sparse view. */ +static inline bool sparse_dmap_wanted(bool pv, bool hwdom) +{ + return sparse_dmap_root && + (hwdom ? opt_sparse_dmap_hwdom + : pv ? opt_sparse_dmap_pv : opt_sparse_dmap_hvm); +} + void sparse_dmap_init(void); int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, unsigned long nr, pte_attr_t flags); @@ -42,6 +50,7 @@ int sparse_dmap_drop_pagetable(mfn_t mfn); #else /* !CONFIG_SPARSE_DIRECTMAP */ =20 static inline bool sparse_dmap_configured(void) { return false; } +static inline bool sparse_dmap_wanted(bool pv, bool hwdom) { return false;= } static inline void sparse_dmap_init(void) {} static inline int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, unsigned long nr, pte_attr_t flag= s) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 7e3009251c..7b5a355e6e 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -1703,7 +1703,8 @@ struct page_info *perdomain_l3(const struct vcpu *v) * l4t, l4mfn, and v are mandatory, but l4mfn doesn't need to be the mfn u= nder * *l4t. All other parameters are optional and will either fill or zero t= he * appropriate slots. Pagetables not shared with guests will gain the - * extended directmap. + * extended directmap, unless the domain runs on the sparse view of the + * directmap, which replaces the directmap slots. * * v is the vCPU the table is private to or, for a table the domain's vCPUs * share, any of them: slot 260 gets v's per-domain area (perdomain_l3()), @@ -1715,7 +1716,7 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, const struct domain *d =3D v->domain; /* * PV vcpus need a shortened directmap. HVM and Idle vcpus get the fu= ll - * directmap. + * directmap, unless the domain runs on the sparse view (below). */ bool short_directmap =3D !paging_mode_external(d); =20 @@ -1750,6 +1751,29 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mf= n, l4t[l4_table_offset(PERDOMAIN_VIRT_START)]; =20 /* Slot 261-: text/data/bss, RW M2P, vmap, frametable, directmap. */ +#ifdef CONFIG_SPARSE_DIRECTMAP + if ( d->arch.asi.sparse_dmap ) + { + unsigned int i =3D l4_table_offset(SPARSE_DMAP_START); + + memcpy(&l4t[l4_table_offset(XEN_VIRT_START)], + &idle_pg_table[l4_table_offset(XEN_VIRT_START)], + (i - l4_table_offset(XEN_VIRT_START)) * sizeof(*l4t)); + + /* + * Every slot the sparse view covers gets its L3, whatever the full + * directmap has there, so that memory added later is covered too.= A + * PV root has guest slots above; any other root loses the rest of= the + * directmap, which the sparse view does not cover. + */ + for ( ; i < l4_table_offset(SPARSE_DMAP_END); i++ ) + l4t[i] =3D sparse_dmap_root[i]; + if ( !short_directmap ) + for ( ; i < l4_table_offset(DIRECTMAP_VIRT_END); i++ ) + l4t[i] =3D l4e_empty(); + } + else +#endif #ifndef NDEBUG if ( short_directmap && unlikely(root_pgt_pv_xen_slots < ROOT_PAGETABLE_PV_XEN_SLOTS) ) diff --git a/xen/arch/x86/pv/shim.c b/xen/arch/x86/pv/shim.c index 41000b0523..2a47dbc56c 100644 --- a/xen/arch/x86/pv/shim.c +++ b/xen/arch/x86/pv/shim.c @@ -148,12 +148,14 @@ const struct platform_bad_page *__init pv_shim_reserv= ed_pages(unsigned int *size static void __init replace_va_mapping(struct domain *d, l4_pgentry_t *l4st= art, unsigned long va, mfn_t mfn) { - l4_pgentry_t l4e =3D l4start[l4_table_offset(va)]; + const l4_pgentry_t *l4t =3D map_domain_page(virt_to_mfn(l4start)); + l4_pgentry_t l4e =3D l4t[l4_table_offset(va)]; l3_pgentry_t l3e =3D l3e_from_l4e(l4e, l3_table_offset(va)); l2_pgentry_t l2e =3D l2e_from_l3e(l3e, l2_table_offset(va)); l1_pgentry_t *pl1e =3D map_l1t_from_l2e(l2e) + l1_table_offset(va); struct page_info *page =3D mfn_to_page(l1e_get_mfn(*pl1e)); =20 + unmap_domain_page(l4t); put_page_and_type(page); =20 *pl1e =3D l1e_from_mfn(mfn, (!is_pv_32bit_domain(d) ? L1_PROT diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index 66c878212a..32e6ba28a3 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -1943,6 +1943,13 @@ void spec_ctrl_init_domain(struct domain *d) =20 d->arch.asi.vcpu_pt =3D !pv && (is_hardware_domain(d) ? opt_pvh_vcpu_p= t_hwdom : opt_vcpu_pt_hvm); + + /* + * The sparse directmap view needs a mapcache: PV domains all have one, + * HVM domains through per-vCPU page-tables. + */ + d->arch.asi.sparse_dmap =3D (pv || d->arch.asi.vcpu_pt) && + sparse_dmap_wanted(pv, is_hardware_domain(d)= ); } =20 void __init init_speculation_mitigations(void) --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439746; cv=none; d=zohomail.com; s=zohoarc; b=QNorzyH/WpN75jOlmDlbQKQ1NJ0TkcUQztId7G/uwwOC75HfzT7TxZZUZTNAvUhphFmwLRRMDAEJlhNPBr1vK81ifL9qCOGiSBAaCvzCQpAl5XcGRC18j4wzSCR9HNfmTPxE71ivUfEpXTnpbxy1YuO4EdmYIUXf9Q/Y+xjnvrA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439746; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=4vwFFDZCpFYjWxxI53FAUMca7zB7w00pZUIvfcxHC8k=; b=nUdBcCOpFeXe8WxaLA+R+L0L9eC1YthNm2DMtTZnu+yAnNDeWxpfXRvO74OYRaDfhSc86fNt8Ijz5s00zH60/mXQ4+RAHuuhN9Rbp1yoxRho9L/NNzLHBcHTEyTLZtz2x4vaGGStzb/zT0yw5IOSG2ckHdgIELzf+syyH/ret9M= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439745908843.2483663673421; Mon, 19 Oct 2026 12:55:45 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454838.1668400 (Exim 4.92) (envelope-from ) id 1xItS3-0004GB-1D; Mon, 19 Oct 2026 19:55:19 +0000 Received: by outflank-mailman (output) from mailman id 1454838.1668400; Mon, 19 Oct 2026 19:55:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS2-0004Dq-IV; Mon, 19 Oct 2026 19:55:18 +0000 Received: by outflank-mailman (input) for mailman id 1454838; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003hB-H3 for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yG-0zXK; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGi-00000001FjN-1w6N; Mon, 19 Oct 2026 19:43:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=4vwFFDZCpFYjWxxI53FAUMca7zB7w00pZUIvfcxHC8k=; b=YsEWkED9HVO4ibSOhCJ6K4n+To i1vnUzp3MH3wFb7JEnxx0p7SXoQFiRKzzW4S9xq02XaOGfnQ6Lo3yrxARE3H4A6MnEEfJ4qg+6h/t Ui6e2Gh6Rr+Jj1mDA9LPKywG7TYHBIIIXn0pF7zuXKno3BnQSq1wKtbatf6rplINqyxw=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini , Teddy Astie Subject: [PATCH v4 20/30] x86/spec-ctrl: wire the sparse directmap view into the asi= option Date: Mon, 19 Oct 2026 20:42:51 +0100 Message-ID: <20261019-asi-hvm-v4-20-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439747503158500 Content-Type: text/plain; charset="utf-8" Let asi=3D choose which domains run on the sparse view of the directmap, when the hypervisor is built with CONFIG_SPARSE_DIRECTMAP: * `asi` and `asi=3D` enable or disable the sparse view, along with vCPU-PT, for every domain including the hardware domain, PV or PVH. HVM domains, a PVH hardware domain included, use the sparse view together with vCPU-PT, which gives them the mapcache it needs; PV domains all have one. A PV domain on the sparse view keeps its domain-wide mapcache and per-domain mappings: the sparse view separates it from other domains, not its vCPUs from each other. * `asi=3Dhvm=3D`, `asi=3Dpv=3D` and `asi=3Ddom0=3D` do t= he same for HVM or PV domains other than the hardware domain, or for the hardware domain, whatever its type: `asi=3D1,dom0=3D0` leaves the hardware domain out of ASI altogether. * A new `asi=3Ddirectmap=3D` selects the sparse view for all domains, as `asi=3Dvcpu-pt=3D` selects vCPU-PT; `vcpu-pt=3D` alone leaves t= he sparse view off. On a hypervisor built without CONFIG_SPARSE_DIRECTMAP the forms enabling all features leave the sparse view off, and `directmap=3D` is rejected as invalid, as compiled-out `spec-ctrl=3D` sub-options are. A domain type the build lacks is ignored, so that the sparse view is not built for nothing. * For now, a PV domain uses one of XPTI and the sparse view, not both; a later patch lets them combine. Enabled explicitly, for dom0, domUs or both, XPTI takes precedence: the PV domains it covers keep the full directmap, and the boot log says so. Left to its default, XPTI yields: it is disabled for the PV domains using the sparse view, with a boot-log warning where it would otherwise be enabled, as the sparse view does not replace it yet (it still maps the xenheap and Xen's stacks). Each domain's case is settled at its creation, and again if it becomes 32-bit, which XPTI does not cover: switch_compat() then puts a domain that XPTI kept off the sparse view on it, with the domain-wide mapcache, which release builds may not have set up for it. The boot log gains a line with the directmap mode, "sparse" once a sparse view is configured and "full" otherwise, and the "ASI features for ..." lines, one for PV domUs now among them, say which domains use the sparse view. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - Docs, comments and message: "domains" throughout, not "guests" (Roger). - Follow the rename of opt_vcpu_pt_hwdom to opt_pvh_vcpu_pt_hwdom and the move of the per-domain settings into d->arch.asi. - pv=3D and dom0=3D move to the patch introducing asi=3D, beside hvm=3D; opt_sparse_dmap_hwdom and sparse_dmap_wanted()'s hardware-domain case to the patches that add the other knobs and sparse_dmap_wanted() (Roger). - directmap=3D on a build without CONFIG_SPARSE_DIRECTMAP is rejected as invalid, as compiled-out spec-ctrl=3D sub-options are, rather than ignored with a warning (Roger). - A PV domain that XPTI kept off the sparse view goes on it when it becomes 32-bit, which ends XPTI; it used to end up with neither. Changes in v3: - New in this version. --- docs/misc/xen-command-line.pandoc | 30 +++++- xen/arch/x86/include/asm/sparse-directmap.h | 7 ++ xen/arch/x86/pv/domain.c | 16 ++++ xen/arch/x86/spec_ctrl.c | 101 ++++++++++++++++++-- 4 files changed, 139 insertions(+), 15 deletions(-) diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line= .pandoc index 839d06da26..092c858f34 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -203,7 +203,7 @@ to appropriate auditing by Xen. Argo is disabled by de= fault. buggy or malicious other domain spamming the ring. =20 ### asi (x86) -> `=3D List of [ , {pv,hvm,dom0}=3D, +> `=3D List of [ , {pv,hvm,dom0}=3D, directmap=3D, > vcpu-pt=3D | vcpu-pt=3Dhvm=3D ]` =20 > Default: `false` @@ -215,14 +215,16 @@ mitigations for certain speculative related attacks, = at the cost of mapping sensitive information on-demand. It might also offer some protection agai= nst unmitigated speculation-related attacks. =20 -The mechanisms currently implemented apply to HVM domains, including a PVH -hardware domain. PV domains, including a PV hardware domain, are unaffect= ed. +The mechanisms currently implemented are `vcpu-pt`, for HVM domains includ= ing +a PVH hardware domain, and `directmap`, for all domains including the hard= ware +domain, PV or PVH. The plain boolean enables or disables both, wherever t= hey +apply. =20 * `pv=3D`, `hvm=3D` and `dom0=3D` enable the features for PV domains or HV= M domains other than the hardware domain, or for the hardware domain, PV or PVH. = The plain boolean does all three: `asi=3D1,dom0=3D0` leaves the hardware dom= ain - out. The forms naming no domain type (an un-suffixed `vcpu-pt=3D`) - cover the hardware domain too. + out. The forms naming no domain type (an un-suffixed `vcpu-pt=3D`= , and + `directmap=3D`) cover the hardware domain too. =20 **WARNING: manual de-selection of enabled options will invalidate any protection offered by the feature. The fine grained options provided below @@ -231,6 +233,18 @@ are meant to be used for debugging purposes only.** * `vcpu-pt` gives each vCPU its own per-domain area: the per-domain slot of each vCPU's monitor table maps a region private to that vCPU. =20 +* `directmap` (only available when Xen is built with + `CONFIG_SPARSE_DIRECTMAP`) runs guest contexts on a sparse view of the + directmap, which maps the memory Xen needs to reach at all times (the Xen + heap, Xen's own image, firmware tables), while guest memory is mapped + transiently. This selection is system-wide; `pv=3D`, `hvm=3D` and `dom0= =3D` + make it for one domain type, along with the other features. HVM domains= use + the sparse view only together with `vcpu-pt`, and PV domains only without + XPTI, which yields to the sparse view unless enabled explicitly (see + `xpti`). The whole-feature forms (`asi`, `asi=3D`, and `pv=3D`, `= hvm=3D` + and `dom0=3D`) set this too, on builds that support it; on other builds = they + leave it off. + ### asid (x86) > `=3D ` =20 @@ -3132,6 +3146,12 @@ Meltdown for all domains. With `dom0` and `domu` it is possible to control page table isolation for dom0 or guest domains only. =20 +With `asi`, XPTI and the sparse directmap view (`asi=3Ddirectmap`) exclude= each +other for a PV domain. Enabled explicitly, as a whole or with `dom0` or +`domu`, XPTI keeps the PV domains it covers off the sparse view, and the b= oot +log says so. Left to its default, XPTI is disabled for the PV domains usi= ng +the sparse view, with a boot-log warning where it would otherwise be enabl= ed. + ### xsave (x86) > `=3D ` =20 diff --git a/xen/arch/x86/include/asm/sparse-directmap.h b/xen/arch/x86/inc= lude/asm/sparse-directmap.h index 2f8b94f4c1..db4df82215 100644 --- a/xen/arch/x86/include/asm/sparse-directmap.h +++ b/xen/arch/x86/include/asm/sparse-directmap.h @@ -31,6 +31,10 @@ static inline bool sparse_dmap_configured(void) return opt_sparse_dmap_pv || opt_sparse_dmap_hvm || opt_sparse_dmap_hw= dom; } =20 +static inline bool sparse_dmap_pv(void) { return opt_sparse_dmap_pv; } +static inline bool sparse_dmap_hvm(void) { return opt_sparse_dmap_hvm; } +static inline bool sparse_dmap_hwdom(void) { return opt_sparse_dmap_hwdom;= } + /* Whether a new domain of the given type is to use the sparse view. */ static inline bool sparse_dmap_wanted(bool pv, bool hwdom) { @@ -51,6 +55,9 @@ int sparse_dmap_drop_pagetable(mfn_t mfn); =20 static inline bool sparse_dmap_configured(void) { return false; } static inline bool sparse_dmap_wanted(bool pv, bool hwdom) { return false;= } +static inline bool sparse_dmap_pv(void) { return false; } +static inline bool sparse_dmap_hvm(void) { return false; } +static inline bool sparse_dmap_hwdom(void) { return false; } static inline void sparse_dmap_init(void) {} static inline int sparse_dmap_mirror_map(unsigned long virt, mfn_t mfn, unsigned long nr, pte_attr_t flag= s) diff --git a/xen/arch/x86/pv/domain.c b/xen/arch/x86/pv/domain.c index f139d8f83c..579d6133f5 100644 --- a/xen/arch/x86/pv/domain.c +++ b/xen/arch/x86/pv/domain.c @@ -248,6 +248,20 @@ int switch_compat(struct domain *d) d->arch.has_32bit_shinfo =3D 1; d->arch.pv.is_32bit =3D true; =20 + /* + * XPTI, cleared below, is for 64-bit domains. A domain it kept off t= he + * sparse directmap view goes on it now if wanted, and the domain-wide + * mapcache the view needs is set up for the domain and its vCPUs. + */ + if ( d->arch.pv.xpti && sparse_dmap_wanted(true, is_hardware_domain(d)= ) ) + { + d->arch.asi.sparse_dmap =3D true; + mapcache_domain_init(d); + for_each_vcpu ( d, v ) + if ( (rc =3D mapcache_vcpu_init(v)) ) + goto undo_and_fail; + } + /* * For 32bit PV guests the shared_info machine address must fit in a 3= 2-bit * field within the guest's start_info structure. We might need to fr= ee @@ -301,6 +315,8 @@ int switch_compat(struct domain *d) =20 undo_and_fail: d->arch.pv.is_32bit =3D d->arch.has_32bit_shinfo =3D false; + if ( d->arch.pv.xpti ) + d->arch.asi.sparse_dmap =3D false; for_each_vcpu( d, v ) { free_compat_arg_xlat(v); diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index 32e6ba28a3..25726978b9 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -387,6 +387,19 @@ int8_t __ro_after_init opt_xpti_domu =3D -1; =20 static __init void xpti_init_default(void) { + /* + * XPTI and the sparse directmap view exclude each other for a PV doma= in + * (see spec_ctrl_init_domain()). Enabled explicitly, XPTI takes + * precedence: the PV domains it covers stay off the sparse view. Lef= t to + * its default, it yields: it is off where the sparse view is asked fo= r. + */ + if ( !opt_dom0_pvh && opt_xpti_hwdom =3D=3D 1 && sparse_dmap_hwdom() ) + printk(XENLOG_ERR + "XPTI enabled, disabling Dom0 sparse-directmap\n"); + if ( opt_xpti_domu =3D=3D 1 && sparse_dmap_pv() ) + printk(XENLOG_ERR + "XPTI enabled, disabling PV DomU sparse-directmap\n"); + if ( (boot_cpu_data.vendor & (X86_VENDOR_AMD | X86_VENDOR_HYGON)) || cpu_has_rdcl_no ) { @@ -397,10 +410,21 @@ static __init void xpti_init_default(void) } else { + bool yield_hwdom =3D opt_xpti_hwdom < 0 && !opt_dom0_pvh && + sparse_dmap_hwdom(); + bool yield_domu =3D opt_xpti_domu < 0 && sparse_dmap_pv(); + + /* The default would be on here: say where it yields. */ + if ( yield_hwdom || yield_domu ) + printk(XENLOG_WARNING + "XPTI defaults off for PV %s on the sparse directmap\n", + yield_hwdom ? (yield_domu ? "Dom0 and DomU" : "Dom0") + : "DomU"); + if ( opt_xpti_hwdom < 0 ) - opt_xpti_hwdom =3D 1; + opt_xpti_hwdom =3D !sparse_dmap_hwdom(); if ( opt_xpti_domu < 0 ) - opt_xpti_domu =3D 1; + opt_xpti_domu =3D !sparse_dmap_pv(); } } =20 @@ -491,6 +515,23 @@ static int __init cf_check parse_pv_l1tf(const char *s) } custom_param("pv-l1tf", parse_pv_l1tf); =20 +/* + * Set the sparse directmap view's knob for each domain type given. A type + * the build lacks stays off, so that no view is built for it, and without + * CONFIG_SPARSE_DIRECTMAP all stay off. + */ +static void __init set_sparse_dmap(bool pv, bool hvm, bool hwdom, bool val) +{ +#ifdef CONFIG_SPARSE_DIRECTMAP + if ( pv && IS_ENABLED(CONFIG_PV) ) + opt_sparse_dmap_pv =3D val; + if ( hvm && IS_ENABLED(CONFIG_HVM) ) + opt_sparse_dmap_hvm =3D val; + if ( hwdom ) + opt_sparse_dmap_hwdom =3D val; +#endif +} + static int __init cf_check parse_asi(const char *s) { const char *ss; @@ -498,7 +539,10 @@ static int __init cf_check parse_asi(const char *s) =20 /* Interpret 'asi' alone in its positive boolean form. */ if ( *s =3D=3D '\0' ) + { opt_pvh_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D true; + set_sparse_dmap(true, true, true, true); + } =20 do { ss =3D strchr(s, ','); @@ -511,15 +555,33 @@ static int __init cf_check parse_asi(const char *s) case 0: case 1: opt_pvh_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D val; + set_sparse_dmap(true, true, true, val); break; =20 default: if ( (val =3D parse_boolean("pv", s, ss)) >=3D 0 ) - /* PV domains are unaffected. */; + set_sparse_dmap(true, false, false, val); else if ( (val =3D parse_boolean("hvm", s, ss)) >=3D 0 ) + { opt_vcpu_pt_hvm =3D val; + set_sparse_dmap(false, true, false, val); + } else if ( (val =3D parse_boolean("dom0", s, ss)) >=3D 0 ) + { opt_pvh_vcpu_pt_hwdom =3D val; + set_sparse_dmap(false, false, true, val); + } + else if ( (val =3D parse_boolean("directmap", s, ss)) >=3D 0 ) + { + /* System-wide: every domain type, the hardware domain too= . */ + if ( IS_ENABLED(CONFIG_SPARSE_DIRECTMAP) ) + set_sparse_dmap(true, true, true, val); + else + { + no_config_param("SPARSE_DIRECTMAP", "asi", s, ss); + rc =3D -EINVAL; + } + } else if ( (val =3D parse_boolean("vcpu-pt", s, ss)) !=3D -1 ) { switch ( val ) @@ -748,12 +810,30 @@ static void __init print_details(enum ind_thunk thunk) opt_pv_l1tf_domu ? "enabled" : "disabled"); #endif =20 - printk(" ASI features for Dom0:%s\n", - opt_dom0_pvh && opt_pvh_vcpu_pt_hwdom ? " vCPU-PT" : " None"); + { + bool hwdom_pt =3D opt_dom0_pvh && opt_pvh_vcpu_pt_hwdom; + bool hwdom_sd =3D sparse_dmap_hwdom() && + (opt_dom0_pvh ? hwdom_pt : !opt_xpti_hwdom); + + printk(" ASI directmap mode: %s\n", + sparse_dmap_configured() ? "sparse" : "full"); + printk(" ASI features for Dom0:%s%s%s\n", + hwdom_pt || hwdom_sd ? "" : " None", + hwdom_pt ? " vCPU-PT" : "", + hwdom_sd ? " sparse-directmap" : ""); #ifdef CONFIG_HVM - printk(" ASI features for HVM VMs:%s\n", - opt_vcpu_pt_hvm ? " vCPU-PT" : " None"); + printk(" ASI features for HVM VMs:%s%s%s\n", + opt_vcpu_pt_hvm ? "" : " None", + opt_vcpu_pt_hvm ? " vCPU-PT" : "", + opt_vcpu_pt_hvm && sparse_dmap_hvm() + ? " sparse-directmap" : ""); #endif +#ifdef CONFIG_PV + printk(" ASI features for PV VMs:%s\n", + sparse_dmap_pv() && !opt_xpti_domu + ? " sparse-directmap" : " None"); +#endif + } } =20 static bool __init check_smt_enabled(void) @@ -1946,9 +2026,10 @@ void spec_ctrl_init_domain(struct domain *d) =20 /* * The sparse directmap view needs a mapcache: PV domains all have one, - * HVM domains through per-vCPU page-tables. + * HVM domains through per-vCPU page-tables. A PV domain using XPTI + * stays on the full directmap. */ - d->arch.asi.sparse_dmap =3D (pv || d->arch.asi.vcpu_pt) && + d->arch.asi.sparse_dmap =3D (pv ? !d->arch.pv.xpti : d->arch.asi.vcpu_= pt) && sparse_dmap_wanted(pv, is_hardware_domain(d)= ); } =20 @@ -2242,7 +2323,7 @@ void __init init_speculation_mitigations(void) hw_smt_enabled && default_xen_spec_ctrl ) setup_force_cpu_cap(X86_FEATURE_SC_MSR_IDLE); =20 - if ( opt_pvh_vcpu_pt_hwdom || opt_vcpu_pt_hvm ) + if ( opt_pvh_vcpu_pt_hwdom || opt_vcpu_pt_hvm || sparse_dmap_configure= d() ) warning_add( "Address Space Isolation is not functional, this option is\n" "intended to be used only for development purposes.\n"); --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439749; cv=none; d=zohomail.com; s=zohoarc; b=RFXDOkZfdL/CarNL5JB2o26uzYp639pQUHtg2TV9sLoKFBDxaFNL59Pm2exJutwLZI9+zzjKpMKmaqUlXrWxCHPuJZDtEAHcTjuAsSTxw55ojhF1oTqw12SS3IkOMXLEDus6h5cFUcfLaVR6RYJG+LGIXKHXb6zQAeY81Dar6FE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439749; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=LN8Bez7AsiyQjAHOZqZjhab+Xv6VL8QpdV20hUs5obk=; b=Vsj/askETm/0kF/kcnPH7jKUO/CeG4QrxZJz/28hF9OuDY7IDmGruVNo4lPquuiatMwJs4OMJVUP41VlID0O5g4+w097ujnfBbiheHADy9J+UExgAUtVPGWxrG5DKWXhLQ9LDe3dKeCFbkctGu0N5uqyAs0ZlwDv2UcZJWO7sfo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439749832316.50951593552304; Mon, 19 Oct 2026 12:55:49 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454844.1668447 (Exim 4.92) (envelope-from ) id 1xItS7-0005Tg-8Y; Mon, 19 Oct 2026 19:55:23 +0000 Received: by outflank-mailman (output) from mailman id 1454844.1668447; Mon, 19 Oct 2026 19:55:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS6-0005Q6-Rx; Mon, 19 Oct 2026 19:55:22 +0000 Received: by outflank-mailman (input) for mailman id 1454844; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003hu-Oa for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yI-1FGJ; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGj-00000001FjN-315T; Mon, 19 Oct 2026 19:43:37 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=LN8Bez7AsiyQjAHOZqZjhab+Xv6VL8QpdV20hUs5obk=; b=XPbhxbKiTjI9rUYIxjlVR6X38Q N7dJH7OMsei63XdjTT2UHl/zCZ6QC7VeeM9YoTWORP9VVidYAySyX7FH0R/3Chm5P6//eVhQbcLoP hOg0xa01PX9wX4lZpq2BQXTW51n5jmYtYbw2NXvSZ68P16j9uNdDo9MTrm4LAfa1CVW0=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v4 21/30] xen/xmalloc: return an emptied region after dropping the pool lock Date: Mon, 19 Oct 2026 20:42:52 +0100 Message-ID: <20261019-asi-hvm-v4-21-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439751470158500 Content-Type: text/plain; charset="utf-8" When a free leaves a whole region of an xmalloc pool empty, xmem_pool_free() hands the region back through pool->put_mem() with pool->lock still held. For the xenheap pool that is free_xenheap_pages(), which takes the heap lock inside the pool lock and runs arch_xenheap_pages_free(). On x86 with a sparse directmap view configured, the latter takes the page out of the view, at the cost of one to three rounds of TLB-flush IPIs to every CPU, while every other CPU's sub-page xmalloc() and xfree() waits on the pool lock. By then the region is on no free list and no longer counted in the pool, so nothing else can reach it. Drop the lock first and return the region afterwards, as xmem_pool_alloc() already does when it calls pool->get_mem(), and say at both callbacks that they are called without the pool lock, so that they keep not relying on it. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- xen/common/xmalloc_tlsf.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/xen/common/xmalloc_tlsf.c b/xen/common/xmalloc_tlsf.c index b43219b4af..caf33f1a04 100644 --- a/xen/common/xmalloc_tlsf.c +++ b/xen/common/xmalloc_tlsf.c @@ -106,7 +106,10 @@ struct xmem_pool { unsigned long used_size; unsigned long num_regions; =20 - /* User provided functions for expanding/shrinking pool */ + /* + * User provided functions for expanding/shrinking pool. They are + * called without the pool lock held, and must not rely on it. + */ xmem_pool_get_memory *get_mem; xmem_pool_put_memory *put_mem; =20 @@ -488,17 +491,18 @@ void xmem_pool_free(void *ptr, struct xmem_pool *pool) =20 if ( (b->prev_hdr =3D=3D NULL) && ((tmp_b->size & BLOCK_SIZE_MASK) =3D= =3D 0) ) { - pool->put_mem(b); pool->num_regions--; pool->used_size -=3D BHDR_OVERHEAD; /* sentinel block header */ - goto out; + spin_unlock(&pool->lock); + pool->put_mem(b); + return; } =20 INSERT_BLOCK(b, pool, fl, sl); =20 tmp_b->size |=3D PREV_FREE; tmp_b->prev_hdr =3D b; - out: + spin_unlock(&pool->lock); } =20 --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439755; cv=none; d=zohomail.com; s=zohoarc; b=GmVpX+ZcXnpVTlIyatj7Ohbyipt8fHXvDDlfH1SZ4vzmSZTS1iFHmRQCPfeMYQPNEgf125I0BT3qV2YEBopjjh50Z7qxJTSvN1GyXN3OXWuqVq7VbWkqwAuk0EeJViGx4Ffxbe/MvK3CjMYFsP3z/phaA4Jq2aihaFk0/l2eJ6I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439755; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=LeUN4GWtDg25LdWPguyYFlprUdbIgPn6gxNUTDepJeY=; b=OqnlR0YQ4hb+TUy4mxha9fcadFjH5PsGPXowp8TyDr0NE6GupVqP5Daa0elbabxSEczdFAjqxVV8SDu5+2iKk8KwqBg9/9i1B4Jt7v3+T2gfo2G5lZdGRtEqnt0y/eydTffibttQSLNOgUj80aLXJsL5xmBQ9O3pxpxri2K7+4Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439755760571.6366963215523; Mon, 19 Oct 2026 12:55:55 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454851.1668515 (Exim 4.92) (envelope-from ) id 1xItSK-0007vI-1c; Mon, 19 Oct 2026 19:55:36 +0000 Received: by outflank-mailman (output) from mailman id 1454851.1668515; Mon, 19 Oct 2026 19:55:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItSI-0007r6-QH; Mon, 19 Oct 2026 19:55:34 +0000 Received: by outflank-mailman (input) for mailman id 1454851; Mon, 19 Oct 2026 19:55:21 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS4-0004vB-TC for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:20 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022zw-3qRA; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGl-00000001FjN-0CHb; Mon, 19 Oct 2026 19:43:39 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=LeUN4GWtDg25LdWPguyYFlprUdbIgPn6gxNUTDepJeY=; b=hnt901M2zDsh7qOiplQLH+lttG ZIewuytRXco8S5EaFg5m7vVL+ERIbqIQSDb3K63SXuDxtpiEmCFAz23l/r2RFs6p/67N5CvAIvHo7 /Itu3oUx8U3F/4f22ni/Gw1n6A+4ghpeZQ+1wZUDayOWVvVEmq4NhBwhKWygtZuuGDcY=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Anthony PERARD , Michal Orzel , Julien Grall , Stefano Stabellini Subject: [PATCH v4 22/30] x86/mm: split a sparse directmap superpage with a page being freed Date: Mon, 19 Oct 2026 20:42:53 +0100 Message-ID: <20261019-asi-hvm-v4-22-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439757663158500 Content-Type: text/plain; charset="utf-8" A xenheap free takes its pages out of the sparse directmap view. Where a superpage covers them, the superpage has to be split, and modify_mappings_in() allocates the page-table page for that. The sparse view has such superpages wherever a xenheap block of 2MiB or more was mapped whole, and wherever map_pages_in() merged the entries of a 2MiB region into one, as it does in the full directmap, once all 512 pages were mapped. The allocation can fail once the hardware domain exists, and the free then keeps its pages out of the heap for good, with a warning. Use one of the pages being freed instead. The sparse view's free hook makes the first of them what alloc_xen_pagetable() would return, and passes it to modify_mappings_in(), which uses it, should a split need a page, before allocating one. The new L1 maps the rest of the superpage, and the page's own entry is cleared along with the others being freed. The hook tells free_xenheap_pages() whether it kept the page, and free_xenheap_pages() then frees the rest of the block as blocks of orders 0 to order - 1. The page goes back to the heap once the L1 has emptied, as any page-table page of the sparse view does. One page is enough if the sparse view has no 1GiB pages: a block smaller than 2MiB lies within one 2MiB page and needs one L1, and a larger block covers whole 2MiB pages and needs none, where a block within a 1GiB page could need an L2 as well. So map_pages_in() makes 1GiB pages, by mapping or by merging, only in idle_pg_table's hierarchy; the sparse view keeps its 2MiB pages, mapped and merged. A xenheap free then allocates nothing in the sparse view. The costs: a 2MiB region split this way can merge again only once it has emptied, as its L1 is one of its own pages; where the sparse view maps a whole 1GiB region, which takes memory never handed to the heap and xenheap memory filling it, contexts on it use 2MiB pages instead of one 1GiB page; and arch_xenheap_pages_free() gains a parameter. The other removals from the sparse view (memory handed to the heap, idle_pg_table's boot-time page-tables, and the undo of a failed xenheap allocation) still allocate when they split a superpage. The sparse view is correct without this, as a free that cannot split its superpage fails safe. It is kept apart from "x86/mm: build and maintain a sparse view of the directmap" so that it can be weighed on its own. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- xen/arch/x86/include/asm/mm.h | 2 +- xen/arch/x86/mm.c | 34 ++++++++++++++++++++++------ xen/arch/x86/sparse-directmap.c | 40 ++++++++++++++++++++++++++------- xen/common/page_alloc.c | 14 ++++++++---- xen/include/xen/mm.h | 11 ++++++--- 5 files changed, 78 insertions(+), 23 deletions(-) diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index 7612cf66ef..af43ae01b0 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -601,7 +601,7 @@ void *alloc_mapped_pagetable(mfn_t *pmfn); int map_pages_in(l4_pgentry_t *root, unsigned long virt, mfn_t mfn, unsigned long nr_mfns, pte_attr_t flags); int modify_mappings_in(l4_pgentry_t *root, unsigned long s, unsigned long = e, - pte_attr_t nf); + pte_attr_t nf, mfn_t *spare); #ifdef CONFIG_SPARSE_DIRECTMAP mfn_t xen_mapping_lookup(l4_pgentry_t *root, unsigned long va, pte_attr_t *flags, unsigned long *nr); diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 7b5a355e6e..449ae561dc 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5540,6 +5540,10 @@ static void free_pagetable_in(const l4_pgentry_t *ro= ot, mfn_t mfn) /* * map_pages_to_xen() in the hierarchy rooted at @root: idle_pg_table's, or * another hierarchy sharing its virtual addresses. + * + * Only idle_pg_table's hierarchy gets 1GiB pages: a xenheap free splits a + * superpage of the sparse directmap view with one of the pages it frees, + * which is enough for a 2MiB page only. */ int map_pages_in( l4_pgentry_t *root, @@ -5549,6 +5553,7 @@ int map_pages_in( pte_attr_t flags) { bool locking =3D system_state > SYS_STATE_boot; + bool page1gb =3D cpu_has_page1gb && root =3D=3D idle_pg_table; l3_pgentry_t *pl3e =3D NULL, ol3e; l2_pgentry_t *pl2e =3D NULL, ol2e; l1_pgentry_t *pl1e, ol1e; @@ -5601,7 +5606,7 @@ int map_pages_in( L3T_LOCK(current_l3page); ol3e =3D *pl3e; =20 - if ( cpu_has_page1gb && + if ( page1gb && IS_L3E_ALIGNED(virt, flags & _PAGE_PRESENT ? mfn : _mfn(0)) && nr_mfns >=3D (1UL << (L3_PAGETABLE_SHIFT - PAGE_SHIFT)) && !(flags & (_PAGE_PAT | MAP_SMALL_PAGES)) ) @@ -5887,7 +5892,7 @@ int map_pages_in( } =20 check_l3: - if ( cpu_has_page1gb && + if ( page1gb && (flags =3D=3D PAGE_HYPERVISOR) && ((nr_mfns =3D=3D 0) || IS_L3E_ALIGNED(virt, mfn)) ) { @@ -5966,6 +5971,18 @@ int __init populate_pt_range(unsigned long virt, uns= igned long nr_mfns) return map_pages_to_xen(virt, INVALID_MFN, nr_mfns, MAP_SMALL_PAGES); } =20 +static mfn_t take_or_alloc_pagetable(mfn_t *spare) +{ + mfn_t mfn =3D spare ? *spare : INVALID_MFN; + + if ( mfn_eq(mfn, INVALID_MFN) ) + return alloc_xen_pagetable(); + + *spare =3D INVALID_MFN; + + return mfn; +} + /* * Alter the permissions of a range of Xen virtual address space. * @@ -5979,10 +5996,13 @@ int __init populate_pt_range(unsigned long virt, un= signed long nr_mfns) * It is an error to call with present flags over an unpopulated range. * * modify_mappings_in() does so in the hierarchy rooted at @root, as - * map_pages_in() does. + * map_pages_in() does. @spare, if not NULL, may hold a page the caller + * gives up for a page-table page, which shattering a superpage then uses + * before allocating one. *@spare reads INVALID_MFN once it has been used, + * and the page is then no longer the caller's. */ int modify_mappings_in(l4_pgentry_t *root, unsigned long s, - unsigned long e, pte_attr_t nf) + unsigned long e, pte_attr_t nf, mfn_t *spare) { bool locking =3D system_state > SYS_STATE_boot; l3_pgentry_t *pl3e =3D NULL; @@ -6046,7 +6066,7 @@ int modify_mappings_in(l4_pgentry_t *root, unsigned l= ong s, } =20 /* PAGE1GB: shatter the superpage and fall through. */ - l2mfn =3D alloc_xen_pagetable(); + l2mfn =3D take_or_alloc_pagetable(spare); if ( mfn_eq(l2mfn, INVALID_MFN) ) goto out; =20 @@ -6104,7 +6124,7 @@ int modify_mappings_in(l4_pgentry_t *root, unsigned l= ong s, { l1_pgentry_t *l1t; /* PSE: shatter the superpage and try again. */ - mfn_t l1mfn =3D alloc_xen_pagetable(); + mfn_t l1mfn =3D take_or_alloc_pagetable(spare); =20 if ( mfn_eq(l1mfn, INVALID_MFN) ) goto out; @@ -6262,7 +6282,7 @@ int modify_mappings_in(l4_pgentry_t *root, unsigned l= ong s, =20 int modify_xen_mappings(unsigned long s, unsigned long e, pte_attr_t nf) { - int rc =3D modify_mappings_in(idle_pg_table, s, e, nf); + int rc =3D modify_mappings_in(idle_pg_table, s, e, nf, NULL); =20 if ( !rc ) rc =3D sparse_dmap_mirror_modify(s, e, nf); diff --git a/xen/arch/x86/sparse-directmap.c b/xen/arch/x86/sparse-directma= p.c index d35a8d6979..adec474f72 100644 --- a/xen/arch/x86/sparse-directmap.c +++ b/xen/arch/x86/sparse-directmap.c @@ -121,12 +121,13 @@ static int add_to_sparse_view(l4_pgentry_t *root, uns= igned long mfn, return 0; } =20 -static int remove_from_sparse_view(unsigned long mfn, unsigned long nr) +static int remove_from_sparse_view(unsigned long mfn, unsigned long nr, + mfn_t *spare) { unsigned long va =3D (unsigned long)mfn_to_virt(mfn); =20 return modify_mappings_in(sparse_dmap_root, va, va + (nr << PAGE_SHIFT= ), - _PAGE_NONE); + _PAGE_NONE, spare); } =20 /* Memory is about to be handed to the heap allocator. */ @@ -144,7 +145,7 @@ bool arch_heap_pages_init(mfn_t mfn, unsigned long nr) return true; } =20 - if ( remove_from_sparse_view(s, nr) ) + if ( remove_from_sparse_view(s, nr, NULL) ) { printk(XENLOG_ERR "sparse directmap: cannot unmap MFNs %#lx-%#lx, not freeing= \n", @@ -188,10 +189,18 @@ int arch_xenheap_pages_alloc(mfn_t mfn, unsigned int = order) /* * Xenheap pages are about to be freed. A failure means the pages may sti= ll * be reachable through the sparse view, and must not be reused. + * + * If the caller allows, the first page serves as the page-table page that + * splitting a superpage of the sparse view needs: the new L1 maps the rest + * of the superpage, and the page leaves the sparse view with the others. + * One page is enough: the sparse view has no 1GiB pages, a block below 2M= iB + * lies within one 2MiB page, and a larger one covers whole ones. */ -int arch_xenheap_pages_free(mfn_t mfn, unsigned int order) +int arch_xenheap_pages_free(mfn_t mfn, unsigned int order, bool *first_kep= t) { unsigned long s =3D mfn_x(mfn), nr =3D 1UL << order; + mfn_t spare =3D INVALID_MFN; + int rc; =20 if ( !sparse_dmap_configured() ) return 0; @@ -204,7 +213,22 @@ int arch_xenheap_pages_free(mfn_t mfn, unsigned int or= der) return 0; } =20 - return remove_from_sparse_view(s, nr); + if ( first_kept ) + { + struct page_info *pg =3D mfn_to_page(mfn); + + /* Made what alloc_xen_pagetable() would return, in case it is use= d. */ + pg->count_info &=3D ~PGC_xen_heap; + page_set_owner(pg, NULL); + spare =3D mfn; + } + + rc =3D remove_from_sparse_view(s, nr, &spare); + + if ( first_kept && mfn_eq(spare, INVALID_MFN) ) + *first_kept =3D true; + + return rc; } =20 /* Memory leaves Xen's hands without passing through the heap. */ @@ -233,7 +257,7 @@ int sparse_dmap_drop_pagetable(mfn_t mfn) INVALID_MFN) ) return 0; =20 - rc =3D remove_from_sparse_view(s, 1); + rc =3D remove_from_sparse_view(s, 1, NULL); if ( rc ) printk(XENLOG_WARNING "Leaking page-table page %"PRI_mfn": still in the sparse di= rectmap\n", @@ -267,7 +291,7 @@ int sparse_dmap_mirror_modify(unsigned long s, unsigned= long e, e =3D min(e, SPARSE_DMAP_END); =20 if ( !(nf & _PAGE_PRESENT) ) - return modify_mappings_in(sparse_dmap_root, s, e, nf); + return modify_mappings_in(sparse_dmap_root, s, e, nf, NULL); =20 /* * A permission change applies where the sparse view maps anything: it @@ -283,7 +307,7 @@ int sparse_dmap_mirror_modify(unsigned long s, unsigned= long e, n =3D min(n, (e - va) >> PAGE_SHIFT); if ( !mfn_eq(mfn, INVALID_MFN) && (rc =3D modify_mappings_in(sparse_dmap_root, va, - va + (n << PAGE_SHIFT), nf)) ) + va + (n << PAGE_SHIFT), nf, NULL)) ) return rc; va +=3D n << PAGE_SHIFT; } diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 69e8e982b6..055e358f3a 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -2561,7 +2561,7 @@ void *alloc_xenheap_pages(unsigned int order, unsigne= d int memflags) * The hook may have mapped part of the pages before failing: take * them out again as a free would, and leak them if that fails too. */ - if ( arch_xenheap_pages_free(page_to_mfn(pg), order) ) + if ( arch_xenheap_pages_free(page_to_mfn(pg), order, NULL) ) printk(XENLOG_WARNING "Leaking xenheap pages %"PRI_mfn"+%u: still mapped\n", mfn_x(page_to_mfn(pg)), 1U << order); @@ -2580,6 +2580,7 @@ void free_xenheap_pages(void *v, unsigned int order) { struct page_info *pg; unsigned int i; + bool first_kept =3D false; =20 ASSERT_ALLOC_CONTEXT(); =20 @@ -2593,7 +2594,7 @@ void free_xenheap_pages(void *v, unsigned int order) * they may remain reachable through it: leak them rather than hand th= em * out. */ - if ( arch_xenheap_pages_free(page_to_mfn(pg), order) ) + if ( arch_xenheap_pages_free(page_to_mfn(pg), order, &first_kept) ) { printk(XENLOG_WARNING "Leaking xenheap pages %"PRI_mfn"+%u: still mapped\n", @@ -2601,10 +2602,15 @@ void free_xenheap_pages(void *v, unsigned int order) return; } =20 - for ( i =3D 0; i < (1u << order); i++ ) + for ( i =3D first_kept ? 1 : 0; i < (1u << order); i++ ) pg[i].count_info &=3D ~PGC_xen_heap; =20 - free_heap_pages(pg, order, true); + if ( !first_kept ) + free_heap_pages(pg, order, true); + else + /* The rest of the block, as the blocks of orders 0 to order - 1. = */ + for ( i =3D 0; i < order; i++ ) + free_heap_pages(pg + (1u << i), i, true); } =20 #endif /* CONFIG_SEPARATE_XENHEAP */ diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index 2bcc45cf58..debd511caf 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -108,8 +108,12 @@ bool arch_xenheap_needs_scrub(void); * pages go through arch_xenheap_pages_free() before being freed again. */ int arch_xenheap_pages_alloc(mfn_t mfn, unsigned int order); -/* Xenheap pages are to be freed. Non-zero: failed, don't reuse them. */ -int arch_xenheap_pages_free(mfn_t mfn, unsigned int order); +/* + * Xenheap pages are to be freed. Non-zero: failed, don't reuse them. Wi= th + * @first_kept not NULL, the arch may keep the first page for itself, and + * then sets *@first_kept to true: that page is not the caller's to free. + */ +int arch_xenheap_pages_free(mfn_t mfn, unsigned int order, bool *first_kep= t); #else static inline bool arch_heap_pages_init(mfn_t mfn, unsigned long nr) { @@ -123,7 +127,8 @@ static inline int arch_xenheap_pages_alloc(mfn_t mfn, u= nsigned int order) { return 0; } -static inline int arch_xenheap_pages_free(mfn_t mfn, unsigned int order) +static inline int arch_xenheap_pages_free(mfn_t mfn, unsigned int order, + bool *first_kept) { return 0; } --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439748; cv=none; d=zohomail.com; s=zohoarc; b=P25yrB19J4e/Jm/AmxjMi/qW5h2xJyoxEfF7io4yvrbNwUHBX+4EMkFlZD6foZ1Qa/epZMg5DyccDfEjxw93Z58vo0hb6EUuKiD1y/9BbLsyUzD9V2Zt9e+uj0SFKT14mNdBAGIWz9tykKpMXtFliq1YfT+6moH8NK1u16g5V3o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439748; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Yt1PBD1wcL13XT6rHzyFnBSPgO0t4Ozjz4QBOUPAPsc=; b=JhroXjkFMho98jx9iBFab3w6NDK0LjhZ5JcF8DKG3iZ+TkFuuoet2Ditd2sgcDdwQ7MDH6g0Ge9JtntMVohvZVRd3yjmL7I4N7dlAbKauqkcCuNL3Y7kjJk3YPXRHbXsdbD40WhrK5tnvGVHV2UMbe3QSn9fXNNO6m18nWC0L4A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439748173822.5805663413648; Mon, 19 Oct 2026 12:55:48 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454843.1668433 (Exim 4.92) (envelope-from ) id 1xItS5-00051C-Ol; Mon, 19 Oct 2026 19:55:21 +0000 Received: by outflank-mailman (output) from mailman id 1454843.1668433; Mon, 19 Oct 2026 19:55:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS5-0004yb-5t; Mon, 19 Oct 2026 19:55:21 +0000 Received: by outflank-mailman (input) for mailman id 1454843; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003hv-Oi for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yk-2mTs; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGm-00000001FjN-0Rk8; Mon, 19 Oct 2026 19:43:40 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=Yt1PBD1wcL13XT6rHzyFnBSPgO0t4Ozjz4QBOUPAPsc=; b=SNuacrP2SMgDjyEETYlQpWsLCD BOsRFiAORmC5ukmHrvWd8JJM5ctP765o0TTDMX0m6qOSpODM6jo1TioApfNMQnE0v093NZ3UzE0zE qW+x5a4LVZ0e/yPeb1hfLK7Mk9XZKbmF8DiPk8e4BoT1ZoInhOSS6MkNQ4a/fptMcG2U=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 23/30] x86/mm: introduce populate_perdomain_mapping() Date: Mon, 19 Oct 2026 20:42:54 +0100 Message-ID: <20261019-asi-hvm-v4-23-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439749481158500 From: Roger Pau Monn=C3=A9 The per-domain area has central machinery for building its page-tables and for managing the backing pages it owns itself: create_perdomain_mapping() / destroy_perdomain_mapping(), used by the mapcache and the compat argument-translation area. What the interface lacks is a way to install a caller's own pages at a chosen address, which a per-vCPU state window, introduced by a later patch, needs: its users allocate their pages themselves and map them there. Introduce populate_perdomain_mapping(v, va, mfn, nr, flags) for this. It writes the given MFNs, with the given page-table flags, into v's view of the per-domain area, walking the per-domain page-table structures with transient mappings, so v need not be current. Callers don't need to know where the page-tables live, how the area is structured, or whether it is per-domain or per-vCPU. We require the range to already have been populated down to the L1 tables by create_perdomain_mapping(). TLB flushing is left to the caller. A present entry not owned by the area (!_PAGE_AVAIL0) is replaced. A present entry owned by the area (_PAGE_AVAIL0, installed by create_perdomain_mapping() itself) is never expected: such a page is referenced only by the mapping, and no caller replaces area-owned backing. Should one ever do so, the page is leaked with a message rather than freed. The function is not for contexts with interrupts off: its users map their pages when allocating them. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claud= e Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - Re-introduced into the series. - HVM only: the users are the state window's, all of which map their pages at allocation time, so drop the linear-mapping fast path and the IRQs-off walk that context-switch callers needed, along with the map_domain_page_irqoff() variants. --- xen/arch/x86/include/asm/mm.h | 3 ++ xen/arch/x86/mm.c | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index af43ae01b0..6d50e17259 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -620,6 +620,9 @@ int compat_arch_memory_op(unsigned long cmd, XEN_GUEST_= HANDLE_PARAM(void) arg); int create_perdomain_mapping(struct vcpu *v, unsigned long va, unsigned int nr, l1_pgentry_t **pl1tab, struct page_info **ppg); +void populate_perdomain_mapping(const struct vcpu *v, unsigned long va, + const mfn_t *mfn, unsigned int nr, + unsigned int flags); void destroy_perdomain_mapping(const struct vcpu *v, unsigned long va, unsigned int nr); void free_perdomain_mappings(struct domain *d); diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 449ae561dc..bf029df566 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6554,6 +6554,68 @@ int create_perdomain_mapping(struct vcpu *v, unsigne= d long va, return rc; } =20 +/* + * Map @nr pages, @mfn[0..nr-1], at consecutive pages from @va in v's view= of + * the per-domain area, with page-table @flags. The range must lie within= a + * single per-domain slot, and must already have been plumbed down to the = L1 + * tables by create_perdomain_mapping(). Present entries are replaced, and + * no TLB flushing is done. + * + * The walk maps the per-domain page-table pages transiently, so v need not + * be current. It is not for contexts with interrupts disabled. + */ +void populate_perdomain_mapping(const struct vcpu *v, unsigned long va, + const mfn_t *mfn, unsigned int nr, + unsigned int flags) +{ + struct page_info *l3_pg =3D perdomain_l3(v); + const l3_pgentry_t *l3tab; + const l2_pgentry_t *l2tab; + l1_pgentry_t *l1tab =3D NULL; + unsigned int i; + + ASSERT(va >=3D PERDOMAIN_VIRT_START && + va < PERDOMAIN_VIRT_SLOT(PERDOMAIN_SLOTS)); + ASSERT(!nr || !l3_table_offset(va ^ (va + nr * PAGE_SIZE - 1))); + ASSERT(local_irq_is_enabled()); + + if ( !nr ) + return; + + BUG_ON(!l3_pg); + l3tab =3D __map_domain_page(l3_pg); + BUG_ON(!(l3e_get_flags(l3tab[l3_table_offset(va)]) & _PAGE_PRESENT)); + l2tab =3D map_l2t_from_l3e(l3tab[l3_table_offset(va)]); + unmap_domain_page(l3tab); + + for ( i =3D 0; i < nr; i++, va +=3D PAGE_SIZE ) + { + l1_pgentry_t *pl1e; + + if ( !l1tab || !l1_table_offset(va) ) + { + const l2_pgentry_t *pl2e =3D &l2tab[l2_table_offset(va)]; + + unmap_domain_page(l1tab); + BUG_ON(!(l2e_get_flags(*pl2e) & _PAGE_PRESENT)); + l1tab =3D map_l1t_from_l2e(*pl2e); + } + + pl1e =3D &l1tab[l1_table_offset(va)]; + if ( perdomain_l1e_needs_freeing(*pl1e) ) + { + printk(XENLOG_ERR + "%pv: leaking area-owned page %"PRI_mfn" at %#lx\n", + v, mfn_x(l1e_get_mfn(*pl1e)), va); + ASSERT_UNREACHABLE(); + } + l1e_write(pl1e, l1e_from_mfn(mfn[i], flags)); + } + + unmap_domain_page(l1tab); + unmap_domain_page(l2tab); +} + void destroy_perdomain_mapping(const struct vcpu *v, unsigned long va, unsigned int nr) { --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439739; cv=none; d=zohomail.com; s=zohoarc; b=gbIwyxBp4fUpij2o4+dHbTS8yqpz7LCoZ4JnbVVzx2kKHiu7vlluqejTbUs88y+Do4OR55aVDzu3i0W74dbR0FWOpTSuAW8nOtLIgCGwxsiO7SNLG8wo085VNgr9jIEbWelrjsj8ZHfKz6CX7K7zPB+VdPkDCF87w43VZ7ySD14= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439739; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=KIMuN+gRzQ2bf6n6qCzbyKF+T+td17QWHkd6BfMjllw=; b=Z3Sxp/hGkRH5KkHLPkY3gCNFn6T609DVt6lRNrM600xA8mcTtfoYjbXWcHItcdx4KvdU05voAcMudhrrRE0PhjSwSKHHc9CbLbX7Ltx86Fhl2oOIqFkXfKUc8MkDZDSlpV+QpAS7x+XF7wYCjOr1W9Aj0ctHxyx4yirtx38j4vU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179243973986036.4499893962053; Mon, 19 Oct 2026 12:55:39 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454839.1668416 (Exim 4.92) (envelope-from ) id 1xItS4-0004g1-6g; Mon, 19 Oct 2026 19:55:20 +0000 Received: by outflank-mailman (output) from mailman id 1454839.1668416; Mon, 19 Oct 2026 19:55:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS3-0004cT-Rm; Mon, 19 Oct 2026 19:55:19 +0000 Received: by outflank-mailman (input) for mailman id 1454839; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003ha-KT for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022zJ-34G7; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGn-00000001FjN-09tA; Mon, 19 Oct 2026 19:43:41 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=KIMuN+gRzQ2bf6n6qCzbyKF+T+td17QWHkd6BfMjllw=; b=0/spY6nDQijmbovDb1WpQiYZ8m d2R+DPchyj5phcApmFz5PjEZcDTGwmmv06NMT9R8BzCYeCd0kegJnMACDtxqjeXVgml0TgedsDwPM Fv9U769D1bjWa45owU+3eQ2aYcGBiH+y1DB27EN10mhIH7y+eKV1lxqt4aINsXiijiq4=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 24/30] x86/mm: introduce a per-vCPU state window in the perdomain area Date: Mon, 19 Oct 2026 20:42:55 +0100 Message-ID: <20261019-asi-hvm-v4-24-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439741397158500 Content-Type: text/plain; charset="utf-8" Under Address Space Isolation, guest-owned register state that Xen keeps in always-mapped memory moves to the domain heap, mapped only in the contexts that use it. The long-lived per-vCPU structures involved (the SVM VMCB, the VMX guest MSR load/save list, the guest XSAVE area, and later the aggregated small per-vCPU allocations) all want the same shape: one or more domain-heap pages, mapped once at fixed per-vCPU virtual addresses for the life of the vCPU. Add a fourth per-domain-mapping sub-area providing that destination: a small per-vCPU stride of page slots, with a fixed index assigned to each relocated structure and the page tables created at vCPU creation time. Consumers allocate their own pages and install them with populate_perdomain_mapping(); the backing pages remain caller-owned, so their lifetime stays with the consumer, while the page tables are torn down with the rest of the perdomain area. Before freeing their pages, consumers take them out of the window with unmap_vcpu_state(), on failure paths as at teardown: vCPU creation can fail once a consumer has mapped its pages, and the domain lives on. It flushes the domain's dirty CPUs, which include every CPU whose page-tables carry the window, in the vCPU's own area or in the shared one; so far that mask is empty on every call, as the vCPU has not run or the domain is paused. A consumer reaching a page from another context, through a transient map_domain_page() mapping, drops the mapping with unmap_vcpu_state_page(), which also flushes this CPU's translations of it, there and at its alias in the per-domain mirror: a domain-wide mapcache defers that flush, and the translations would otherwise outlive the mapping. For a domain running without per-vCPU page tables the window has the same domain-wide visibility as the other perdomain sub-areas: the virtual addresses are keyed on vcpu_id, so the same VA arithmetic serves both modes. With vcpu-pt enabled, a vCPU's window is simply absent from its siblings' address spaces. This is infrastructure for moving the SVM VMCB, the VMX MSR load/save lists and the guest XSAVE areas off always-mapped memory. Nothing occupies the area yet; the only behavioural change is the page-table allocation itself and the failure path that comes with it: vCPU creation can now fail with -ENOMEM. Both apply to every guest vCPU, with or without asi=3D, PV ones included. A domain with per-vCPU page tables gets an L2 and an L1 per vCPU; any other domain, every PV domain among them, one L2 plus an L1 per 64 vCPUs: 2 pages for up to 64 vCPUs. A PV domain's window stays empty until the XSAVE areas move there. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - New in this version. --- xen/arch/x86/domain.c | 12 +++++++++ xen/arch/x86/include/asm/config.h | 21 ++++++++++++++- xen/arch/x86/include/asm/mm.h | 2 ++ xen/arch/x86/mm.c | 44 +++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 7a4da203d4..04868a781e 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -525,6 +525,18 @@ int arch_vcpu_create(struct vcpu *v) if ( rc ) goto fail; =20 + BUILD_BUG_ON((MAX_VIRT_CPUS << VCPU_STATE_VA_SHIFT) > + (PERDOMAIN_SLOT_MBYTES << 20)); + BUILD_BUG_ON(VCPU_STATE_IDX_VMCB >=3D VCPU_STATE_NR_PAGES); + BUILD_BUG_ON(VCPU_STATE_IDX_N2VMCB >=3D VCPU_STATE_NR_PAGES); + BUILD_BUG_ON(VCPU_STATE_IDX_VMX_MSR >=3D VCPU_STATE_NR_PAGES); + /* Page tables only; torn down with the rest of the perdomain area= . */ + rc =3D create_perdomain_mapping(v, VCPU_STATE_START(v), + VCPU_STATE_NR_PAGES, + NIL(l1_pgentry_t *), NULL); + if ( rc !=3D 0 ) + goto fail; + paging_vcpu_init(v); =20 if ( (rc =3D vcpu_init_fpu(v)) !=3D 0 ) diff --git a/xen/arch/x86/include/asm/config.h b/xen/arch/x86/include/asm/c= onfig.h index ee84c57432..8a265e25c8 100644 --- a/xen/arch/x86/include/asm/config.h +++ b/xen/arch/x86/include/asm/config.h @@ -158,7 +158,7 @@ /* Slot 260: per-domain mappings (including map cache). */ #define PERDOMAIN_VIRT_START (PML4_ADDR(260)) #define PERDOMAIN_SLOT_MBYTES (PML4_ENTRY_BYTES >> (20 + PAGETABLE_ORDER= )) -#define PERDOMAIN_SLOTS 3 +#define PERDOMAIN_SLOTS 4 #define PERDOMAIN_VIRT_SLOT(s) (PERDOMAIN_VIRT_START + (s) * \ (PERDOMAIN_SLOT_MBYTES << 20)) /* Slot 4: mirror of per-domain mappings (for compat xlat area accesses). = */ @@ -293,6 +293,25 @@ extern unsigned long xen_phys_start; #define ARG_XLAT_START(v) \ (ARG_XLAT_VIRT_START + ((v)->vcpu_id << ARG_XLAT_VA_SHIFT)) =20 +/* + * Per-vCPU state window. The fourth per-domain-mapping sub-area: a small + * per-vCPU stride of page slots holding long-lived per-vCPU structures ke= pt + * out of always-mapped memory, each mapped once for the life of the vCPU = at + * a fixed index within the stride. + */ +#define VCPU_STATE_SLOT 3 +#define VCPU_STATE_VIRT_START PERDOMAIN_VIRT_SLOT(VCPU_STATE_SLOT) +#define VCPU_STATE_ORDER 3 +#define VCPU_STATE_NR_PAGES (1U << VCPU_STATE_ORDER) +#define VCPU_STATE_VA_SHIFT (VCPU_STATE_ORDER + PAGE_SHIFT) +#define VCPU_STATE_START(v) \ + (VCPU_STATE_VIRT_START + ((v)->vcpu_id << VCPU_STATE_VA_SHIFT)) +#define VCPU_STATE_ADDR(v, idx) \ + (VCPU_STATE_START(v) + (idx) * PAGE_SIZE) +#define VCPU_STATE_IDX_VMCB 0 +#define VCPU_STATE_IDX_N2VMCB 1 +#define VCPU_STATE_IDX_VMX_MSR 2 + #define ELFSIZE 64 =20 #define ARCH_CRASH_SAVE_VMCOREINFO diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index 6d50e17259..6eaea3ba01 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -625,6 +625,8 @@ void populate_perdomain_mapping(const struct vcpu *v, u= nsigned long va, unsigned int flags); void destroy_perdomain_mapping(const struct vcpu *v, unsigned long va, unsigned int nr); +void unmap_vcpu_state(const struct vcpu *v, unsigned int idx, unsigned int= nr); +void unmap_vcpu_state_page(const void *va); void free_perdomain_mappings(struct domain *d); void free_pervcpu_mappings(struct vcpu *v); =20 diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index bf029df566..9c9f3e728c 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6668,6 +6668,50 @@ void destroy_perdomain_mapping(const struct vcpu *v,= unsigned long va, unmap_domain_page(l3tab); } =20 +/* + * Take @nr slots of @v's state window, from @idx, out of the per-domain + * area, before the caller frees the pages behind them: the pages are the + * caller's own, and must not stay mapped once freed. The window's + * translations are usable only on CPUs whose page-tables carry it, all in + * the domain's dirty mask: with per-vCPU page-tables, the CPU running v or + * holding it lazily; otherwise, those of any of the domain's vCPUs. + */ +void unmap_vcpu_state(const struct vcpu *v, unsigned int idx, unsigned int= nr) +{ + const struct domain *d =3D v->domain; + + /* Beyond the stride is the next vCPU's window, in a shared area. */ + BUG_ON(idx >=3D VCPU_STATE_NR_PAGES || nr > VCPU_STATE_NR_PAGES - idx); + + destroy_perdomain_mapping(v, VCPU_STATE_ADDR(v, idx), nr); + + flush_tlb_mask(d->dirty_cpumask); +} + +/* + * Drop a transient mapping of a page behind a vCPU's state window, and th= is + * CPU's translations of it: at its address, and at its alias in the + * per-domain mirror, which every domain but a 64-bit PV one has (flushing + * that address there is harmless). A per-vCPU mapcache flushes both + * already, with the slot's last mapping; a domain-wide one defers the + * flush, and CPUs running other vCPUs of its domain may have cached the + * slot too. A directmap address, where the caller has no mapcache, needs + * nothing. + */ +void unmap_vcpu_state_page(const void *va) +{ + unsigned long addr =3D (unsigned long)va; + + unmap_domain_page(va); + + if ( addr < DIRECTMAP_VIRT_START ) + { + flush_tlb_one_local(addr); + flush_tlb_one_local(addr - PERDOMAIN_VIRT_START + + PERDOMAIN_ALT_VIRT_START); + } +} + static void free_perdomain_l3(struct page_info *l3pg) { l3_pgentry_t *l3tab =3D __map_domain_page(l3pg); --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439739; cv=none; d=zohomail.com; s=zohoarc; b=DVFjoNhsrABS9z0cTPLCpk3DfvWUbpV1KwrKvELxvK0IuIhjq1cLhrflTiHt3409sS6j1XWnk8vrHazKVxk75BGwVmtEHF5wYrc+GUAKeco3r8jkeumIBiMcLCospGdY/okL8+tSaAUOYj9wmVtIDMyhEmVyiCOxph2bNLlWNek= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439739; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Mu6k2BaQFLQ9iQqejDHFz+H+XwqcF1BQPCpuR3LOvj4=; b=IFOTyUo6JsS0QGCsCYUOB+bqk62QIf1GKEx0XrjrdOJEeNkZJhS6pxhHBAXTyHkGc7xB0JgtPc1834ACHwrINDb8zMFcknv8Orsioi6VgvSPCcn9DOag6QdhYhHzLEZB8ByOuZqi+L4w5vXLDfn6QcSsGBkSGG/cs3CriZ1diA4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439739299164.87281332503483; Mon, 19 Oct 2026 12:55:39 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454834.1668379 (Exim 4.92) (envelope-from ) id 1xItS1-0003up-NW; Mon, 19 Oct 2026 19:55:17 +0000 Received: by outflank-mailman (output) from mailman id 1454834.1668379; Mon, 19 Oct 2026 19:55:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS1-0003uH-Jc; Mon, 19 Oct 2026 19:55:17 +0000 Received: by outflank-mailman (input) for mailman id 1454834; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003h4-GU for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yY-2Nbo; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGo-00000001FjN-0HBF; Mon, 19 Oct 2026 19:43:42 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=Mu6k2BaQFLQ9iQqejDHFz+H+XwqcF1BQPCpuR3LOvj4=; b=5N/xWK8vMsxKqNS9s6OwGEgY51 MC+3dNN4OKtNXIg8kOVNPuXk36+PnCEYA1CYZibi8pVs/YneAZZoNLCMxZX5RQnU6TE/PNKfwHsBb iJwqjOUfuId4Y2CCvd5Mj6fW2wYbXHGuZ/RmN/WS5rN2sY4jQkBcIM8mojak2DtPXJPg=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jason Andryuk , Teddy Astie Subject: [PATCH v4 25/30] x86/svm: introduce svm_vmcb_map() and svm_vmcb_unmap() Date: Mon, 19 Oct 2026 20:42:56 +0100 Message-ID: <20261019-asi-hvm-v4-25-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439741423158500 Content-Type: text/plain; charset="utf-8" Some accessors of a vCPU's VMCB can run with that vCPU paused, from another vCPU's context: context save/load, the segment and CPL getters, PAT/TSC/intercept updates reachable from toolstack operations, vm_event, the 'v' debug key, and construct_vmcb() itself, which runs before the vCPU ever entered guest context. svm_nested_features_on_efer_update() is among them: it is reachable remotely via hvm_load_cpu_ctxt() -> svm_update_guest_efer(). Route them through new svm_vmcb_map/unmap helpers; construct_vmcb() takes the VMCB from its caller. svm_set_tsc_offset() needs both VMCBs rather than just the active one, so it maps them individually via svm_vmcb_map_one(). A vCPU stays in nested guest mode across a deschedule or a pause, so this path can run remotely with the L2 VMCB active; guest mode does not imply v =3D=3D current. Hot vmexit-path accesses all have v =3D=3D current and keep dereferencing svm->vmcb directly. No functional change: the helpers return the VMCB's existing address. They prepare for moving VMCBs off always-mapped memory, where a VMCB's mapping is valid only in its own vCPU's context. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - New in this version. --- xen/arch/x86/hvm/svm/nestedsvm.c | 4 +- xen/arch/x86/hvm/svm/svm.c | 133 +++++++++++++++++++++++-------- xen/arch/x86/hvm/svm/vmcb.c | 29 ++++++- xen/arch/x86/hvm/svm/vmcb.h | 9 +++ 4 files changed, 136 insertions(+), 39 deletions(-) diff --git a/xen/arch/x86/hvm/svm/nestedsvm.c b/xen/arch/x86/hvm/svm/nested= svm.c index 2b998e26bc..dc853e5f1a 100644 --- a/xen/arch/x86/hvm/svm/nestedsvm.c +++ b/xen/arch/x86/hvm/svm/nestedsvm.c @@ -1554,7 +1554,7 @@ void svm_vmexit_do_clgi(struct cpu_user_regs *regs, s= truct vcpu *v) */ void svm_nested_features_on_efer_update(struct vcpu *v) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); struct nestedsvm *svm =3D &vcpu_nestedsvm(v); u32 general2_intercepts; vintr_t vintr; @@ -1612,6 +1612,8 @@ void svm_nested_features_on_efer_update(struct vcpu *= v) vmcb_set_general2_intercepts(vmcb, general2_intercepts); } } + + svm_vmcb_unmap(v, vmcb); } =20 void __init start_nested_svm(struct hvm_function_table *hvm_function_table) diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 71e48351f2..241843d8c0 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -107,7 +107,7 @@ static void cf_check svm_cpu_down(void) static void cf_check svm_update_guest_cr( struct vcpu *v, unsigned int cr, unsigned int flags) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); uint64_t value; =20 switch ( cr ) @@ -173,11 +173,13 @@ static void cf_check svm_update_guest_cr( default: BUG(); } + + svm_vmcb_unmap(v, vmcb); } =20 static void cf_check svm_update_guest_efer(struct vcpu *v) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); unsigned long guest_efer =3D v->arch.hvm.guest_efer, xen_efer =3D read_efer(); =20 @@ -204,6 +206,7 @@ static void cf_check svm_update_guest_efer(struct vcpu = *v) guest_efer |=3D EFER_SVME; =20 vmcb_set_efer(vmcb, guest_efer); + svm_vmcb_unmap(v, vmcb); =20 ASSERT(nestedhvm_enabled(v->domain) || !nsvm_efer_svm_enabled(v)); @@ -352,7 +355,7 @@ static void svm_restore_dr(struct vcpu *v) =20 static int svm_vmcb_save(struct vcpu *v, struct hvm_hw_cpu *c) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); =20 if ( vmcb->event_inj.v && hvm_event_needs_reinjection(vmcb->event_inj.type, @@ -362,13 +365,15 @@ static int svm_vmcb_save(struct vcpu *v, struct hvm_h= w_cpu *c) c->error_code =3D vmcb->event_inj.ec; } =20 + svm_vmcb_unmap(v, vmcb); + return 1; } =20 static int svm_vmcb_restore(struct vcpu *v, struct hvm_hw_cpu *c) { struct page_info *page =3D NULL; - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb; struct p2m_domain *p2m =3D p2m_get_hostp2m(v->domain); =20 if ( c->pending_valid ) @@ -417,6 +422,8 @@ static int svm_vmcb_restore(struct vcpu *v, struct hvm_= hw_cpu *c) svm_update_guest_cr(v, 0, 0); svm_update_guest_cr(v, 4, 0); =20 + vmcb =3D svm_vmcb_map(v); + if ( !paging_mode_shadow(v->domain) ) { vmcb_set_np(vmcb, true); @@ -436,6 +443,8 @@ static int svm_vmcb_restore(struct vcpu *v, struct hvm_= hw_cpu *c) vmcb->event_inj.raw =3D 0; =20 vmcb->cleanbits.raw =3D 0; + svm_vmcb_unmap(v, vmcb); + paging_update_paging_modes(v); =20 return 0; @@ -443,7 +452,7 @@ static int svm_vmcb_restore(struct vcpu *v, struct hvm_= hw_cpu *c) =20 static void svm_save_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + const struct vmcb_struct *vmcb =3D svm_vmcb_map(v); =20 data->sysenter_cs =3D vmcb->sysenter_cs; data->sysenter_esp =3D vmcb->sysenter_esp; @@ -453,11 +462,13 @@ static void svm_save_cpu_state(struct vcpu *v, struct= hvm_hw_cpu *data) data->msr_star =3D vmcb->star; data->msr_cstar =3D vmcb->cstar; data->msr_syscall_mask =3D vmcb->sfmask; + + svm_vmcb_unmap(v, vmcb); } =20 static void svm_load_cpu_state(struct vcpu *v, struct hvm_hw_cpu *data) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); =20 vmcb->lstar =3D data->msr_lstar; vmcb->star =3D data->msr_star; @@ -467,6 +478,9 @@ static void svm_load_cpu_state(struct vcpu *v, struct h= vm_hw_cpu *data) vmcb->sysenter_cs =3D data->sysenter_cs; vmcb->sysenter_esp =3D data->sysenter_esp; vmcb->sysenter_eip =3D data->sysenter_eip; + + svm_vmcb_unmap(v, vmcb); + v->arch.hvm.guest_efer =3D data->msr_efer; svm_update_guest_efer(v); } @@ -492,7 +506,7 @@ static int cf_check svm_load_vmcb_ctxt(struct vcpu *v, = struct hvm_hw_cpu *ctxt) =20 static unsigned cf_check int svm_get_interrupt_shadow(struct vcpu *v) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); unsigned int intr_shadow =3D 0; =20 if ( vmcb->int_stat.intr_shadow ) @@ -503,13 +517,15 @@ static unsigned cf_check int svm_get_interrupt_shadow= (struct vcpu *v) : (vmcb_get_general1_intercepts(vmcb) & GENERAL1_INTERCEPT_IRET) ) intr_shadow |=3D HVM_INTR_SHADOW_NMI; =20 + svm_vmcb_unmap(v, vmcb); + return intr_shadow; } =20 static void cf_check svm_set_interrupt_shadow( struct vcpu *v, unsigned int intr_shadow) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); bool block_nmi =3D intr_shadow & HVM_INTR_SHADOW_NMI; =20 vmcb->int_stat.intr_shadow =3D @@ -527,10 +543,13 @@ static void cf_check svm_set_interrupt_shadow( =20 vmcb_set_general1_intercepts(vmcb, gen1); } + + svm_vmcb_unmap(v, vmcb); } =20 static int cf_check svm_guest_x86_mode(struct vcpu *v) { + /* Current only (see guest_cpu_user_regs() below): no mapping needed. = */ struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; =20 if ( unlikely(!(v->arch.hvm.guest_cr[0] & X86_CR0_PE)) ) @@ -544,8 +563,7 @@ static int cf_check svm_guest_x86_mode(struct vcpu *v) =20 static void cf_check svm_cpuid_policy_changed(struct vcpu *v) { - struct svm_vcpu *svm =3D &v->arch.hvm.svm; - struct vmcb_struct *vmcb =3D svm->vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); const struct cpu_policy *cp =3D v->domain->arch.cpu_policy; u32 bitmap =3D vmcb_get_exception_intercepts(vmcb); =20 @@ -555,6 +573,7 @@ static void cf_check svm_cpuid_policy_changed(struct vc= pu *v) bitmap &=3D ~(1U << X86_EXC_UD); =20 vmcb_set_exception_intercepts(vmcb, bitmap); + svm_vmcb_unmap(v, vmcb); =20 /* Give access to MSR_SPEC_CTRL if the guest has been told about it. */ svm_intercept_msr(v, MSR_SPEC_CTRL, @@ -598,13 +617,18 @@ void svm_sync_vmcb(struct vcpu *v, enum vmcb_sync_sta= te new_state) =20 static unsigned int cf_check svm_get_cpl(struct vcpu *v) { - return vmcb_get_cpl(v->arch.hvm.svm.vmcb); + const struct vmcb_struct *vmcb =3D svm_vmcb_map(v); + unsigned int cpl =3D vmcb_get_cpl(vmcb); + + svm_vmcb_unmap(v, vmcb); + + return cpl; } =20 static void cf_check svm_get_segment_register( struct vcpu *v, enum x86_segment seg, struct segment_register *reg) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); =20 ASSERT((v =3D=3D current) || !vcpu_runnable(v)); =20 @@ -645,12 +669,14 @@ static void cf_check svm_get_segment_register( *reg =3D (struct segment_register){}; break; } + + svm_vmcb_unmap(v, vmcb); } =20 static void cf_check svm_set_segment_register( struct vcpu *v, enum x86_segment seg, struct segment_register *reg) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); =20 ASSERT((v =3D=3D current) || !vcpu_runnable(v)); =20 @@ -679,6 +705,7 @@ static void cf_check svm_set_segment_register( default: ASSERT_UNREACHABLE(); domain_crash(v->domain); + svm_vmcb_unmap(v, vmcb); return; } =20 @@ -716,33 +743,41 @@ static void cf_check svm_set_segment_register( ASSERT_UNREACHABLE(); break; } + + svm_vmcb_unmap(v, vmcb); } =20 static int cf_check svm_set_guest_pat(struct vcpu *v, u64 gpat) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb; =20 if ( paging_mode_shadow(v->domain) ) return 0; =20 + vmcb =3D svm_vmcb_map(v); vmcb_set_g_pat(vmcb, gpat); + svm_vmcb_unmap(v, vmcb); + return 1; } =20 static int cf_check svm_get_guest_pat(struct vcpu *v, u64 *gpat) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + const struct vmcb_struct *vmcb; =20 if ( paging_mode_shadow(v->domain) ) return 0; =20 + vmcb =3D svm_vmcb_map(v); *gpat =3D vmcb_get_g_pat(vmcb); + svm_vmcb_unmap(v, vmcb); + return 1; } =20 static void cf_check svm_set_tsc_offset(struct vcpu *v, uint64_t offset) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); struct vmcb_struct *n1vmcb, *n2vmcb; uint64_t n2_tsc_offset =3D 0; struct domain *d =3D v->domain; @@ -750,25 +785,36 @@ static void cf_check svm_set_tsc_offset(struct vcpu *= v, uint64_t offset) if ( !nestedhvm_enabled(d) ) { vmcb_set_tsc_offset(vmcb, offset); + svm_vmcb_unmap(v, vmcb); return; } =20 - n1vmcb =3D vcpu_nestedhvm(v).nv_n1vmcx; - n2vmcb =3D vcpu_nestedhvm(v).nv_n2vmcx; - + /* + * A vCPU stays in nested guest mode across a deschedule or a pause, so + * this can run remotely with the L2 VMCB active: map both explicitly. + */ if ( nestedhvm_vcpu_in_guestmode(v) ) { + const struct nestedvcpu *nv =3D &vcpu_nestedhvm(v); + + n1vmcb =3D svm_vmcb_map_one(v, nv->nv_n1vmcx, nv->nv_n1vmcx_pa); + n2vmcb =3D svm_vmcb_map_one(v, nv->nv_n2vmcx, nv->nv_n2vmcx_pa); + n2_tsc_offset =3D vmcb_get_tsc_offset(n2vmcb) - vmcb_get_tsc_offset(n1vmcb); vmcb_set_tsc_offset(n1vmcb, offset); + + svm_vmcb_unmap(v, n2vmcb); + svm_vmcb_unmap(v, n1vmcb); } =20 vmcb_set_tsc_offset(vmcb, offset + n2_tsc_offset); + svm_vmcb_unmap(v, vmcb); } =20 static void cf_check svm_set_rdtsc_exiting(struct vcpu *v, bool enable) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); u32 general1_intercepts =3D vmcb_get_general1_intercepts(vmcb); u32 general2_intercepts =3D vmcb_get_general2_intercepts(vmcb); =20 @@ -783,13 +829,14 @@ static void cf_check svm_set_rdtsc_exiting(struct vcp= u *v, bool enable) =20 vmcb_set_general1_intercepts(vmcb, general1_intercepts); vmcb_set_general2_intercepts(vmcb, general2_intercepts); + svm_vmcb_unmap(v, vmcb); } =20 #ifdef CONFIG_VM_EVENT static void cf_check svm_set_descriptor_access_exiting( struct vcpu *v, bool enable) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); u32 general1_intercepts =3D vmcb_get_general1_intercepts(vmcb); u32 mask =3D GENERAL1_INTERCEPT_IDTR_READ | GENERAL1_INTERCEPT_GDTR_RE= AD | GENERAL1_INTERCEPT_LDTR_READ | GENERAL1_INTERCEPT_TR_READ @@ -802,6 +849,7 @@ static void cf_check svm_set_descriptor_access_exiting( general1_intercepts &=3D ~mask; =20 vmcb_set_general1_intercepts(vmcb, general1_intercepts); + svm_vmcb_unmap(v, vmcb); } #endif /* CONFIG_VM_EVENT */ =20 @@ -1360,7 +1408,12 @@ static void cf_check svm_inject_event(const struct x= 86_event *event) =20 static bool cf_check svm_event_pending(const struct vcpu *v) { - return v->arch.hvm.svm.vmcb->event_inj.v; + const struct vmcb_struct *vmcb =3D svm_vmcb_map(v); + bool pending =3D vmcb->event_inj.v; + + svm_vmcb_unmap(v, vmcb); + + return pending; } =20 static void cf_check svm_cpu_dead(unsigned int cpu) @@ -2318,45 +2371,55 @@ static void cf_check svm_invlpg(struct vcpu *v, uns= igned long linear) static bool cf_check svm_get_pending_event( struct vcpu *v, struct x86_event *info) { - const struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + const struct vmcb_struct *vmcb =3D svm_vmcb_map(v); + bool pending =3D vmcb->event_inj.v; =20 - if ( !vmcb->event_inj.v ) - return false; + if ( pending ) + { + info->vector =3D vmcb->event_inj.vector; + info->type =3D vmcb->event_inj.type; + info->error_code =3D vmcb->event_inj.ec; + } =20 - info->vector =3D vmcb->event_inj.vector; - info->type =3D vmcb->event_inj.type; - info->error_code =3D vmcb->event_inj.ec; + svm_vmcb_unmap(v, vmcb); =20 - return true; + return pending; } =20 static uint64_t cf_check svm_get_reg(struct vcpu *v, unsigned int reg) { struct vcpu *curr =3D current; - const struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + const struct vmcb_struct *vmcb =3D svm_vmcb_map(v); struct domain *d =3D v->domain; + uint64_t val =3D 0; =20 switch ( reg ) { case MSR_SPEC_CTRL: - return vmcb->spec_ctrl; + val =3D vmcb->spec_ctrl; + break; =20 case MSR_SHADOW_GS_BASE: if ( v =3D=3D curr ) svm_sync_vmcb(v, vmcb_in_sync); - return vmcb->kerngsbase; + val =3D vmcb->kerngsbase; + break; =20 default: printk(XENLOG_G_ERR "%s(%pv, 0x%08x) Bad register\n", __func__, v, reg); domain_crash(d); - return 0; + break; } + + svm_vmcb_unmap(v, vmcb); + + return val; } =20 static void cf_check svm_set_reg(struct vcpu *v, unsigned int reg, uint64_= t val) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; + struct vmcb_struct *vmcb =3D svm_vmcb_map(v); struct domain *d =3D v->domain; =20 switch ( reg ) @@ -2371,6 +2434,8 @@ static void cf_check svm_set_reg(struct vcpu *v, unsi= gned int reg, uint64_t val) domain_crash(d); break; } + + svm_vmcb_unmap(v, vmcb); } =20 static struct hvm_function_table __initdata_cf_clobber svm_function_table = =3D { diff --git a/xen/arch/x86/hvm/svm/vmcb.c b/xen/arch/x86/hvm/svm/vmcb.c index a6f09672a7..4c48bbc76d 100644 --- a/xen/arch/x86/hvm/svm/vmcb.c +++ b/xen/arch/x86/hvm/svm/vmcb.c @@ -43,11 +43,25 @@ void free_vmcb(struct vmcb_struct *vmcb) free_xenheap_page(vmcb); } =20 +struct vmcb_struct *svm_vmcb_map_one(const struct vcpu *v, + struct vmcb_struct *va, paddr_t pa) +{ + return va; +} + +struct vmcb_struct *svm_vmcb_map(const struct vcpu *v) +{ + return svm_vmcb_map_one(v, v->arch.hvm.svm.vmcb, v->arch.hvm.svm.vmcb_= pa); +} + +void svm_vmcb_unmap(const struct vcpu *v, const struct vmcb_struct *vmcb) +{ +} + /* This function can directly access fields which are covered by clean bit= s. */ -static int construct_vmcb(struct vcpu *v) +static int construct_vmcb(struct vcpu *v, struct vmcb_struct *vmcb) { struct svm_vcpu *svm =3D &v->arch.hvm.svm; - struct vmcb_struct *vmcb =3D svm->vmcb; =20 vmcb->_general1_intercepts =3D GENERAL1_INTERCEPT_INTR | GENERAL1_INTERCEPT_NMI | @@ -201,6 +215,7 @@ int svm_create_vmcb(struct vcpu *v) { struct nestedvcpu *nv =3D &vcpu_nestedhvm(v); struct svm_vcpu *svm =3D &v->arch.hvm.svm; + struct vmcb_struct *vmcb; int rc; =20 if ( (nv->nv_n1vmcx =3D=3D NULL) && @@ -211,7 +226,10 @@ int svm_create_vmcb(struct vcpu *v) } =20 svm->vmcb =3D nv->nv_n1vmcx; - rc =3D construct_vmcb(v); + + vmcb =3D svm_vmcb_map(v); + rc =3D construct_vmcb(v, vmcb); + svm_vmcb_unmap(v, vmcb); if ( rc !=3D 0 ) { free_vmcb(nv->nv_n1vmcx); @@ -407,6 +425,7 @@ static void cf_check vmcb_dump(unsigned char ch) { struct domain *d; struct vcpu *v; + struct vmcb_struct *vmcb; =20 printk("*********** VMCB Areas **************\n"); =20 @@ -425,7 +444,9 @@ static void cf_check vmcb_dump(unsigned char ch) continue; } printk("\tVCPU %d\n", v->vcpu_id); - svm_vmcb_dump("key_handler", v->arch.hvm.svm.vmcb); + vmcb =3D svm_vmcb_map(v); + svm_vmcb_dump("key_handler", vmcb); + svm_vmcb_unmap(v, vmcb); =20 process_pending_softirqs(); } diff --git a/xen/arch/x86/hvm/svm/vmcb.h b/xen/arch/x86/hvm/svm/vmcb.h index 28dae325ce..9d61767ec7 100644 --- a/xen/arch/x86/hvm/svm/vmcb.h +++ b/xen/arch/x86/hvm/svm/vmcb.h @@ -559,6 +559,15 @@ struct vmcb_struct { struct vmcb_struct *alloc_vmcb(void); void free_vmcb(struct vmcb_struct *vmcb); =20 +/* + * Access a VMCB of a vCPU that may not be current. svm_vmcb_map() takes + * the active VMCB, svm_vmcb_map_one() a specific one. + */ +struct vmcb_struct *svm_vmcb_map_one(const struct vcpu *v, + struct vmcb_struct *va, paddr_t pa); +struct vmcb_struct *svm_vmcb_map(const struct vcpu *v); +void svm_vmcb_unmap(const struct vcpu *v, const struct vmcb_struct *vmcb); + int svm_create_vmcb(struct vcpu *v); void svm_destroy_vmcb(struct vcpu *v); =20 --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439741; cv=none; d=zohomail.com; s=zohoarc; b=OEOCWULKAblh6GDcMyTDVRyRS2PHpXKCygsQrm+7c2XIy5lPAW66pQil9GhIrpWVUJCuiwvz9o70vRODvnO8Vmx6SDPtV7InqtkPJwu/JLEqPvmtwBC59sZXAyQ+3SxVJr6lTghumVI284XueHXEO5kQ2zbxEGFVCKmCtvbMlfw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439741; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=aVhlCyy1J7G7Zme04HVYXzoAzI/t0O7TFZbPngGRa5g=; b=FK6qT4Jy6LZ9wdGr4ha6sFgL2ZKudcB8+LIu3VjOFrGYEN78l7V/mDUZAWPwBJCiTzX7HjGlBUWT4BRxjdZ0B5wBL0rxezB3JpQ5CKMBVRpq0XS9vkf1wbHoaluihqkKmchPm4Pxi7Ht707FpK8fkuALordZy1QHTz4zTJpLh0U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439741846789.2133416761226; Mon, 19 Oct 2026 12:55:41 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454847.1668478 (Exim 4.92) (envelope-from ) id 1xItSB-0006F2-3X; Mon, 19 Oct 2026 19:55:27 +0000 Received: by outflank-mailman (output) from mailman id 1454847.1668478; Mon, 19 Oct 2026 19:55:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS9-0006AJ-Rr; Mon, 19 Oct 2026 19:55:25 +0000 Received: by outflank-mailman (input) for mailman id 1454847; Mon, 19 Oct 2026 19:55:16 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003iE-TE for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yu-2vQR; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGp-00000001FjN-0OtQ; Mon, 19 Oct 2026 19:43:43 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=aVhlCyy1J7G7Zme04HVYXzoAzI/t0O7TFZbPngGRa5g=; b=EN4gmx/0hUfhB7xCXkyaw9ale0 t0EUyoIuGCLpJcLAxcWCGGsWJFwh1lV7D1AcYkl4wkuKzTrc8r+JAhVjQoiPt4Qa8+KAqZOQ4VGVS MSd19vyuoGn1n9CaL/F+WcMN5aB11vvtTSETYnkIc5OOFc8TvD3GVjgVDejlCjvBq2BE=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jason Andryuk , Teddy Astie Subject: [PATCH v4 26/30] x86/svm: clear the VMCB clean bits in svm_ctxt_switch_from() Date: Mon, 19 Oct 2026 20:42:57 +0100 Message-ID: <20261019-asi-hvm-v4-26-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439743297158500 Content-Type: text/plain; charset="utf-8" A VMCB's clean bits must all be clear when its vCPU next runs after a deschedule, as other VMCBs run on the pCPU in the meantime. svm_ctxt_switch_to() clears them as the vCPU is scheduled in; clear them in svm_ctxt_switch_from() instead, as it is scheduled out. This is equivalent. While the vCPU is descheduled, accessors reaching its VMCB only ever clear clean bits: svm_vmexit_handler(), which sets them all, runs only in the vCPU's own context. So do the nested-SVM paths that change the active VMCB, nsvm_vcpu_hostrestore() and nsvm_vcpu_vmentry(), and they clear the incoming VMCB's clean bits themselves. This prepares for moving VMCBs off always-mapped memory: svm_ctxt_switch_to() runs before write_ptbase(), where the incoming vCPU's VMCB will not be mapped. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - New in this version. --- xen/arch/x86/hvm/svm/svm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 241843d8c0..4e19df9865 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -919,6 +919,8 @@ static void cf_check svm_ctxt_switch_from(struct vcpu *= v) svm_save_dr(v); svm_tsc_ratio_save(v); =20 + v->arch.hvm.svm.vmcb->cleanbits.raw =3D 0; + svm_sync_vmcb(v, vmcb_needs_vmload); svm_vmload_pa(per_cpu(host_vmcb, cpu)); =20 @@ -938,7 +940,6 @@ static void cf_check svm_ctxt_switch_from(struct vcpu *= v) =20 static void cf_check svm_ctxt_switch_to(struct vcpu *v) { - struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; int cpu =3D smp_processor_id(); =20 /* @@ -956,7 +957,6 @@ static void cf_check svm_ctxt_switch_to(struct vcpu *v) =20 svm_restore_dr(v); =20 - vmcb->cleanbits.raw =3D 0; svm_tsc_ratio_load(v); =20 if ( cpu_has_msr_tsc_aux ) --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439740; cv=none; d=zohomail.com; s=zohoarc; b=aDzRw9+Fnn37rdtxLy5yq/e4Jl88v48/K+sVMJzQ0N3qCkp/BWeQ6NyAwNQAoA93J5Lu8zHag6nW+MzSu53aW0eBu5FtBPlj885FCpS5r0tuoKk4V5KvXspV+tqviyoyr9n31LvJ8uZ3TmQ5Sl8Z9UN05t06Mxogez29WGmVF3k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439740; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0xjjqZgBgY8JRdyUnikpudSp7wTX9GMpnA2R61WibAk=; b=XdgaQrgjcR/hUNEe+g5t8ZBCA0TfEi6J4jCjSN75jyLs8sGWbSgvQ1SoPr1ntuu5A+3ksO4xugMi+4+WSmOpycAd3vE73xO4dZZhjtjTZbYs8Q2bLFH6wOgtn1Y6/8G64EAcysHpOk+RuxHlZXo0pfLC5lt9VddYWcC4aIPg2jc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439740364450.1781700633554; Mon, 19 Oct 2026 12:55:40 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454837.1668392 (Exim 4.92) (envelope-from ) id 1xItS2-0004C2-EE; Mon, 19 Oct 2026 19:55:18 +0000 Received: by outflank-mailman (output) from mailman id 1454837.1668392; Mon, 19 Oct 2026 19:55:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS2-0004B0-8q; Mon, 19 Oct 2026 19:55:18 +0000 Received: by outflank-mailman (input) for mailman id 1454837; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003h6-Gj for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yM-1Ug1; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGq-00000001FjN-0Vjv; Mon, 19 Oct 2026 19:43:44 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=0xjjqZgBgY8JRdyUnikpudSp7wTX9GMpnA2R61WibAk=; b=xPX+XWlYED+O8f0wmVmXbJ+ABA bqnIl+GYHkHaw8Dd22+y2MvFJtgs/ZRlwdNNkp59SedwJB/if5e+lcATu+XfNtbFFXjEYtCQcpQ5h Fya36z2ymJvg60Nws0PluJUGixANVpNBLBDUk8hpfSOLdmdFASHEDzvQ91Z7iBy3Ii7E=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jason Andryuk , Teddy Astie Subject: [PATCH v4 27/30] x86/svm: restore guest debug registers in svm_do_resume() Date: Mon, 19 Oct 2026 20:42:58 +0100 Message-ID: <20261019-asi-hvm-v4-27-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439741316158500 Content-Type: text/plain; charset="utf-8" svm_ctxt_switch_to() restores the guest's debug registers when its DR7 enables any of them, which also updates the VMCB. Do it in svm_do_resume() instead, which follows it whenever the vCPU is scheduled in. svm_do_resume() also runs when a vCPU keeps running on its pCPU (continue_running()), without svm_ctxt_switch_to(). Where the guest's debug registers are already loaded, the restore does nothing: flag_dr_dirty records that they are, and only svm_save_dr() clears it, from svm_ctxt_switch_from(). This prepares for moving VMCBs off always-mapped memory: svm_ctxt_switch_to() runs before write_ptbase(), where the incoming vCPU's VMCB will not be mapped. Not yet run with a guest using debug registers. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - New in this version. --- xen/arch/x86/hvm/svm/svm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 4e19df9865..3de8da7ea3 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -955,8 +955,6 @@ static void cf_check svm_ctxt_switch_to(struct vcpu *v) */ disable_each_ist(per_cpu(idt, cpu)); =20 - svm_restore_dr(v); - svm_tsc_ratio_load(v); =20 if ( cpu_has_msr_tsc_aux ) @@ -980,6 +978,8 @@ static void noreturn cf_check svm_do_resume(void) bool vcpu_guestmode =3D false; struct vlapic *vlapic =3D vcpu_vlapic(v); =20 + svm_restore_dr(v); + if ( nestedhvm_enabled(v->domain) && nestedhvm_vcpu_in_guestmode(v) ) vcpu_guestmode =3D 1; =20 --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439733; cv=none; d=zohomail.com; s=zohoarc; b=ZZYIuDO3NvBdE2t//l9FXTCVIQOytuckvRWKcRYoZGo+2wlDz2WL0PiKaUpTPqDJfmlwN4Vj/T5KCQlEAXkklwLoIOy/xkMlw7ekYEB54CNMdNkhLOuWNMFTGjFelYYNplcSHH9HQHdrnsTUhXSY97NYT+6BMEd0DVQzbG4vFWo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439733; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=WXccehmoUo316LnD9XW5/ndVjuvHxgbNQjKqxh2kxOI=; b=j6OrJINt6yXikQ1uocBoYByArRa/Leavd5a8hvWxfywxXmJ3MWLcO2q7uFiHMINwaBaZ3qccqOPt9QoiDLE5Z3eX0I1wY2gIdWq+hDpwhtjzC2aNVVHCP672PaK2DSxMEroxYbgW7rCRgJQKSd7wd+VMYcUvDWYC6vRRTYs+/4w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439733600284.4396098156067; Mon, 19 Oct 2026 12:55:33 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454841.1668424 (Exim 4.92) (envelope-from ) id 1xItS4-0004r4-QY; Mon, 19 Oct 2026 19:55:20 +0000 Received: by outflank-mailman (output) from mailman id 1454841.1668424; Mon, 19 Oct 2026 19:55:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS4-0004lt-AI; Mon, 19 Oct 2026 19:55:20 +0000 Received: by outflank-mailman (input) for mailman id 1454841; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003hY-Jt for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yW-2GmY; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGr-00000001FjN-1qeb; Mon, 19 Oct 2026 19:43:45 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=WXccehmoUo316LnD9XW5/ndVjuvHxgbNQjKqxh2kxOI=; b=tod2/hnaQFr59FD07ZjAE2NgIJ LraLVV9STvXUDhkjdyGXCiHO9lB/NP4MKcMy0CE3a1LD8ZezfoxsWBWBn/wefms9gzlOhFt3QuGNX D755nVMWCflmOJfbIDgLm0Sc5w7RiozLi28V9xBe1plK9CY+Ze6uiHdfER7nx5uUHhuw=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jason Andryuk , Teddy Astie Subject: [PATCH v4 28/30] x86/svm: drop svm_vmcb_dump()'s paddr and the unused VCPU_svm_vmcb Date: Mon, 19 Oct 2026 20:42:59 +0100 Message-ID: <20261019-asi-hvm-v4-28-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439735279158501 Content-Type: text/plain; charset="utf-8" svm_vmcb_dump() prints virt_to_maddr() of the VMCB, which is right only while VMCBs are reached through the directmap. Print the virtual address alone. Nothing uses the VCPU_svm_vmcb asm-offset: assembly only consumes VCPU_svm_vmcb_pa. Drop it. Both prepare for moving VMCBs off always-mapped memory. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - New in this version. --- xen/arch/x86/hvm/svm/vmcb.c | 4 ++-- xen/arch/x86/x86_64/asm-offsets.c | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/xen/arch/x86/hvm/svm/vmcb.c b/xen/arch/x86/hvm/svm/vmcb.c index 4c48bbc76d..f5321b03fb 100644 --- a/xen/arch/x86/hvm/svm/vmcb.c +++ b/xen/arch/x86/hvm/svm/vmcb.c @@ -280,8 +280,8 @@ void svm_vmcb_dump(const char *from, const struct vmcb_= struct *vmcb) svm_sync_vmcb(curr, vmcb_in_sync); =20 printk("Dumping guest's current state at %s...\n", from); - printk("Size of VMCB =3D %zu, paddr =3D %"PRIpaddr", vaddr =3D %p\n", - sizeof(struct vmcb_struct), virt_to_maddr(vmcb), vmcb); + printk("Size of VMCB =3D %zu, vaddr =3D %p\n", + sizeof(struct vmcb_struct), vmcb); =20 printk("cr_intercepts =3D %#x dr_intercepts =3D %#x " "exception_intercepts =3D %#x\n", diff --git a/xen/arch/x86/x86_64/asm-offsets.c b/xen/arch/x86/x86_64/asm-of= fsets.c index 0549fd5e8d..c5259de604 100644 --- a/xen/arch/x86/x86_64/asm-offsets.c +++ b/xen/arch/x86/x86_64/asm-offsets.c @@ -118,7 +118,6 @@ void __dummy__(void) =20 #ifdef CONFIG_HVM OFFSET(VCPU_svm_vmcb_pa, struct vcpu, arch.hvm.svm.vmcb_pa); - OFFSET(VCPU_svm_vmcb, struct vcpu, arch.hvm.svm.vmcb); BLANK(); =20 OFFSET(VCPU_vmx_launched, struct vcpu, arch.hvm.vmx.launched); --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439759; cv=none; d=zohomail.com; s=zohoarc; b=KOc0RfiAEqr0YCBq9GVw0ehqXkhMxgxxG9x1TTmEY7bfzZaId/wA5HEOVLU45LH+TnmwpPRggRP+UlBE18Spd0rbvqmSvusD069iw/v0cVyKiumCiTfo3titpnoL6pLNn1iOia9W+1ECyx/Ox3ZPOAgUfrq2j4YiiIg3clQ3L88= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439759; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=SZ4nI3B+cfhVuPJjziTGQrpW0Hyptt7NpOiIzFtpX7o=; b=K765Pr1+h9NHz6LdbZxn8+HSPXqI7Gn1Z+meeN/sn1pFESJmESCGYOBG865PD6IGUyroOIXb6ovsZaurXXxsjKTqJIgEyYtAMGOujH7IYu5Z3UnVgJ9hfm9nIvG8ukbzjbFx4D1PnXioaatUwPG8j4JzidbYBUZpvx1WAszthzk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439759363641.9416336191922; Mon, 19 Oct 2026 12:55:59 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454856.1668524 (Exim 4.92) (envelope-from ) id 1xItSN-0000KN-Uy; Mon, 19 Oct 2026 19:55:39 +0000 Received: by outflank-mailman (output) from mailman id 1454856.1668524; Mon, 19 Oct 2026 19:55:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItSM-0000Ez-A2; Mon, 19 Oct 2026 19:55:38 +0000 Received: by outflank-mailman (input) for mailman id 1454856; Mon, 19 Oct 2026 19:55:26 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS9-00069w-Ox for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:25 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022zc-3Ntj; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGs-00000001FjN-1xGS; Mon, 19 Oct 2026 19:43:46 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=SZ4nI3B+cfhVuPJjziTGQrpW0Hyptt7NpOiIzFtpX7o=; b=W+PqlqQpK+K3Lw7iJrW1j/i/j5 QO+dlveS1cxCuWZyhrteNvtWuE79JLCnXXahUMQtT0RNOZA/zKjWsoQzTcKl7hnz4VU7HyHfqGfAI CrWEh1pFPLrtdmWHWk0NWHdx7r/hOJZKvaaqvJpygjgQ7jjVOkYL6wJv/F27eCH5ZtRI=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jason Andryuk , Teddy Astie Subject: [PATCH v4 29/30] x86/svm: allocate VMCBs from the domain heap behind the vCPU state window Date: Mon, 19 Oct 2026 20:43:00 +0100 Message-ID: <20261019-asi-hvm-v4-29-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439759694158500 Content-Type: text/plain; charset="utf-8" The VMCB holds the full guest register save area, but lives on the xenheap, where it stays reachable from every context. Move it to the domain heap and map it at a fixed slot in the owning vCPU's state window, so that under Address Space Isolation it is only mapped while that vCPU's page tables are loaded. Hardware is unaffected: VMRUN, VMLOAD and VMSAVE address the VMCB physically, and vmcb_pa now derives from the page rather than from virt_to_maddr() on a directmap VA. A vCPU can have two VMCBs: the primary (nv_n1vmcx) and the nested L2 one (nv_n2vmcx). Both move, taking the first two slots in the window stride. The nested code's swapping of svm->vmcb / svm->vmcb_pa between the two works unchanged, now swapping between two window VAs. free_vmcb() takes a VMCB out of the window with unmap_vcpu_state() before freeing the page: without per-vCPU page tables the window is in the domain-wide area, which outlives a vCPU whose creation fails. The window VA is only valid in the owning vCPU's context. svm_vmcb_map() and svm_vmcb_map_one() return it for v =3D=3D current, and a transient map_domain_page() mapping otherwise, which svm_vmcb_unmap() drops with unmap_vcpu_state_page(), along with this CPU's translations of it. svm_ctxt_switch_to() no longer touches the VMCB, as it runs before write_ptbase(); svm_ctxt_switch_from() keeps dereferencing the window VA, as the outgoing page tables are still loaded at that point. Runtime-tested under QEMU's SVM emulation with a PVH dom0 on two vCPUs, with and without per-vCPU page-tables: VMCB allocation, VMRUN from the window and context switches. Nested SVM, save/restore, the 'v' debug key and a guest using debug registers remain to be exercised. Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - New in this version. --- xen/arch/x86/hvm/svm/nestedsvm.c | 8 +++--- xen/arch/x86/hvm/svm/svm.c | 10 ++++++++ xen/arch/x86/hvm/svm/vmcb.c | 43 ++++++++++++++++++++++---------- xen/arch/x86/hvm/svm/vmcb.h | 15 ++++++++--- 4 files changed, 56 insertions(+), 20 deletions(-) diff --git a/xen/arch/x86/hvm/svm/nestedsvm.c b/xen/arch/x86/hvm/svm/nested= svm.c index dc853e5f1a..1599a9908d 100644 --- a/xen/arch/x86/hvm/svm/nestedsvm.c +++ b/xen/arch/x86/hvm/svm/nestedsvm.c @@ -71,10 +71,9 @@ int cf_check nsvm_vcpu_initialise(struct vcpu *v) goto err; memset(msrpm, 0x0, MSRPM_SIZE); =20 - nv->nv_n2vmcx =3D alloc_vmcb(); + nv->nv_n2vmcx =3D alloc_vmcb(v, VCPU_STATE_IDX_N2VMCB, &nv->nv_n2vmcx_= pa); if ( nv->nv_n2vmcx =3D=3D NULL ) goto err; - nv->nv_n2vmcx_pa =3D virt_to_maddr(nv->nv_n2vmcx); =20 return 0; =20 @@ -95,7 +94,10 @@ void cf_check nsvm_vcpu_destroy(struct vcpu *v) * of l1 vmcb page. */ if ( nv->nv_n1vmcx ) + { v->arch.hvm.svm.vmcb =3D nv->nv_n1vmcx; + v->arch.hvm.svm.vmcb_pa =3D nv->nv_n1vmcx_pa; + } =20 if ( svm->ns_cached_msrpm ) { @@ -113,7 +115,7 @@ void cf_check nsvm_vcpu_destroy(struct vcpu *v) =20 if ( nv->nv_n2vmcx ) { - free_vmcb(nv->nv_n2vmcx); + free_vmcb(v, VCPU_STATE_IDX_N2VMCB, nv->nv_n2vmcx_pa); nv->nv_n2vmcx =3D NULL; nv->nv_n2vmcx_pa =3D INVALID_PADDR; } diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 3de8da7ea3..6cb3ff4258 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -265,6 +265,10 @@ static void cf_check svm_enable_msr_interception(struc= t domain *d, uint32_t msr) =20 static void svm_save_dr(struct vcpu *v) { + /* + * @v is the outgoing vCPU, whose page tables are still loaded: the + * VMCB's window VA serves, although current has moved on. + */ struct vmcb_struct *vmcb =3D v->arch.hvm.svm.vmcb; unsigned int flag_dr_dirty =3D v->arch.hvm.flag_dr_dirty; =20 @@ -942,6 +946,12 @@ static void cf_check svm_ctxt_switch_to(struct vcpu *v) { int cpu =3D smp_processor_id(); =20 + /* + * NB we cannot access the VMCB here: this runs before + * write_ptbase(), so the incoming vCPU's state window is not + * reachable yet. + */ + /* * This is required, because VMRUN does consistency check and some of = the * DOM0 selectors are pointing to invalid GDT locations, and cause AMD diff --git a/xen/arch/x86/hvm/svm/vmcb.c b/xen/arch/x86/hvm/svm/vmcb.c index f5321b03fb..4b398742a9 100644 --- a/xen/arch/x86/hvm/svm/vmcb.c +++ b/xen/arch/x86/hvm/svm/vmcb.c @@ -6,6 +6,7 @@ * */ =20 +#include #include #include #include @@ -23,30 +24,40 @@ #include "svm.h" #include "vmcb.h" =20 -struct vmcb_struct *alloc_vmcb(void) +struct vmcb_struct *alloc_vmcb(struct vcpu *v, unsigned int idx, paddr_t *= pa) { - struct vmcb_struct *vmcb; + struct page_info *pg =3D alloc_domheap_page(v->domain, MEMF_no_owner); + mfn_t mfn; + + ASSERT(idx < VCPU_STATE_NR_PAGES); =20 - vmcb =3D alloc_xenheap_page(); - if ( vmcb =3D=3D NULL ) + if ( pg =3D=3D NULL ) { printk(XENLOG_WARNING "Warning: failed to allocate vmcb.\n"); return NULL; } =20 - clear_page(vmcb); - return vmcb; + mfn =3D page_to_mfn(pg); + clear_domain_page(mfn); + populate_perdomain_mapping(v, VCPU_STATE_ADDR(v, idx), &mfn, 1, + __PAGE_HYPERVISOR_RW); + *pa =3D mfn_to_maddr(mfn); + + return (struct vmcb_struct *)VCPU_STATE_ADDR(v, idx); } =20 -void free_vmcb(struct vmcb_struct *vmcb) +void free_vmcb(struct vcpu *v, unsigned int idx, paddr_t pa) { - free_xenheap_page(vmcb); + ASSERT(idx < VCPU_STATE_NR_PAGES); + + unmap_vcpu_state(v, idx, 1); + free_domheap_page(maddr_to_page(pa)); } =20 struct vmcb_struct *svm_vmcb_map_one(const struct vcpu *v, struct vmcb_struct *va, paddr_t pa) { - return va; + return v =3D=3D current ? va : map_domain_page(maddr_to_mfn(pa)); } =20 struct vmcb_struct *svm_vmcb_map(const struct vcpu *v) @@ -56,6 +67,8 @@ struct vmcb_struct *svm_vmcb_map(const struct vcpu *v) =20 void svm_vmcb_unmap(const struct vcpu *v, const struct vmcb_struct *vmcb) { + if ( v !=3D current ) + unmap_vcpu_state_page(vmcb); } =20 /* This function can directly access fields which are covered by clean bit= s. */ @@ -219,26 +232,29 @@ int svm_create_vmcb(struct vcpu *v) int rc; =20 if ( (nv->nv_n1vmcx =3D=3D NULL) && - (nv->nv_n1vmcx =3D alloc_vmcb()) =3D=3D NULL ) + (nv->nv_n1vmcx =3D alloc_vmcb(v, VCPU_STATE_IDX_VMCB, + &nv->nv_n1vmcx_pa)) =3D=3D NULL ) { printk("Failed to create a new VMCB\n"); return -ENOMEM; } =20 svm->vmcb =3D nv->nv_n1vmcx; + svm->vmcb_pa =3D nv->nv_n1vmcx_pa; =20 vmcb =3D svm_vmcb_map(v); rc =3D construct_vmcb(v, vmcb); svm_vmcb_unmap(v, vmcb); if ( rc !=3D 0 ) { - free_vmcb(nv->nv_n1vmcx); + free_vmcb(v, VCPU_STATE_IDX_VMCB, nv->nv_n1vmcx_pa); nv->nv_n1vmcx =3D NULL; + nv->nv_n1vmcx_pa =3D INVALID_PADDR; svm->vmcb =3D NULL; + svm->vmcb_pa =3D INVALID_PADDR; return rc; } =20 - svm->vmcb_pa =3D nv->nv_n1vmcx_pa =3D virt_to_maddr(svm->vmcb); return 0; } =20 @@ -248,7 +264,7 @@ void svm_destroy_vmcb(struct vcpu *v) struct svm_vcpu *svm =3D &v->arch.hvm.svm; =20 if ( nv->nv_n1vmcx !=3D NULL ) - free_vmcb(nv->nv_n1vmcx); + free_vmcb(v, VCPU_STATE_IDX_VMCB, nv->nv_n1vmcx_pa); =20 if ( svm->msrpm !=3D NULL ) { @@ -260,6 +276,7 @@ void svm_destroy_vmcb(struct vcpu *v) nv->nv_n1vmcx =3D NULL; nv->nv_n1vmcx_pa =3D INVALID_PADDR; svm->vmcb =3D NULL; + svm->vmcb_pa =3D INVALID_PADDR; } =20 static void svm_dump_sel(const char *name, const struct segment_register *= s) diff --git a/xen/arch/x86/hvm/svm/vmcb.h b/xen/arch/x86/hvm/svm/vmcb.h index 9d61767ec7..d899beb46e 100644 --- a/xen/arch/x86/hvm/svm/vmcb.h +++ b/xen/arch/x86/hvm/svm/vmcb.h @@ -556,12 +556,19 @@ struct vmcb_struct { uint64_t res18[291]; }; =20 -struct vmcb_struct *alloc_vmcb(void); -void free_vmcb(struct vmcb_struct *vmcb); +/* + * VMCBs are domain-heap pages: hardware addresses them physically, and + * Xen maps each one at a fixed slot in the owning vCPU's state window, + * so the returned virtual address is valid only in that vCPU's context. + * @idx is the VCPU_STATE_IDX_* slot to place the mapping in. + */ +struct vmcb_struct *alloc_vmcb(struct vcpu *v, unsigned int idx, paddr_t *= pa); +void free_vmcb(struct vcpu *v, unsigned int idx, paddr_t pa); =20 /* - * Access a VMCB of a vCPU that may not be current. svm_vmcb_map() takes - * the active VMCB, svm_vmcb_map_one() a specific one. + * Access a VMCB of a vCPU that may not be current: the window VA for + * current, a transient mapping otherwise. svm_vmcb_map() takes the active + * VMCB, svm_vmcb_map_one() a specific one. */ struct vmcb_struct *svm_vmcb_map_one(const struct vcpu *v, struct vmcb_struct *va, paddr_t pa); --=20 2.55.0 From nobody Mon Nov 23 22:39:57 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1792439734; cv=none; d=zohomail.com; s=zohoarc; b=GR1103C3FmEH3k73zOxWg5BsEbnM5Kj3OVfRSPnyawLSTtmasim4rFIrXB9XYIKchNYNeybfdGRaLM9w10u5Kvb/hZtWCcTXRGBqJUyJ2CffcWU0fPsUkmvpxTk+Kly97HpjMVPjlYAGmDikTenzn12isqgjNY6GZhCSgD+1KtY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792439734; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=bMNvYGbpwti5AIsINLtLfKcXfjOXZ5N/yCUXzZrTb18=; b=UnEAZ8YpAxOnCBBG3DLGKxqwclNsd9r6WsD1luBOTsL0bG1vwglvW+qP3QbJnd4LNbiVV21SJUbhmX6R5LW/BL7hum3hH08TXT0SU38zFaz1RTfAB5qGZTbzXtCoR+xB2Utrq/pjhCdd8e3MU01WMlKBbVkndhpXgbxXk/f5l+c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792439734039908.1780625690293; Mon, 19 Oct 2026 12:55:34 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1454840.1668410 (Exim 4.92) (envelope-from ) id 1xItS3-0004Qv-Ms; Mon, 19 Oct 2026 19:55:19 +0000 Received: by outflank-mailman (output) from mailman id 1454840.1668410; Mon, 19 Oct 2026 19:55:19 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItS3-0004OX-5e; Mon, 19 Oct 2026 19:55:19 +0000 Received: by outflank-mailman (input) for mailman id 1454840; Mon, 19 Oct 2026 19:55:15 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xItRz-0003hR-Id for xen-devel@lists.xenproject.org; Mon, 19 Oct 2026 19:55:15 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xItRz-000000022yQ-1q4P; Mon, 19 Oct 2026 19:55:15 +0000 Received: from [217.155.165.12] (helo=Georges-MacBook-Pro-2.tail87ea19.ts.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xItGt-00000001FjN-1fBH; Mon, 19 Oct 2026 19:43:47 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=bMNvYGbpwti5AIsINLtLfKcXfjOXZ5N/yCUXzZrTb18=; b=BuaVhJuzRwN+Qj/B48mVkgsgNe rD1Sn9VjswIz47T2CRWAfPdGzLXajbTE2d8UZSKzONXlg6v+YU342J61wYRD3TIZdY20+3YnehBK2 /6garerc3YHryFbe5yxV4UPedTzgfyrKnFyLUZ3XGDKbesGTSwgbFPiRXFrbxz0QDZSo=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH v4 30/30] x86/vmx: move the guest MSR load/save list off always-mapped memory Date: Mon, 19 Oct 2026 20:43:01 +0100 Message-ID: <20261019-asi-hvm-v4-30-b68b66652266@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> References: <20261019-asi-hvm-v4-0-b68b66652266@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1792439735359158500 The guest MSR load/save list holds guest MSR values (SPEC_CTRL, LBR, PT, vPMU global control, and EFER on hardware without dedicated controls), but lives on the xenheap, where it stays reachable from every context. Move the page to the domain heap and map it at its fixed slot in the owning vCPU's state window, so that under Address Space Isolation it is only mapped while that vCPU's page tables are loaded. Hardware is unaffected: the VMCS MSR-area pointers hold the physical address, now derived from the page rather than from virt_to_maddr() on a directmap VA. The host MSR load list is left on the xenheap: it holds Xen's own exit-time values, not guest data, so relocating it would serve no isolation purpose. The window VA is only valid in the owning vCPU's context. Almost all accesses have v =3D=3D current and keep going through vmx->msr_area unchanged (the vmentry-path LBR fixups, the failed-vmentry dump, vPMU, and the MSR intercepts). The remote accessors =E2=80=94 vmx_add_msr() / vmx_del_msr() from vCPU construction, policy updates and context load, and single-entry reads/writes from the HVM context save/load and get/set-reg paths, all of which already require the target vCPU to be paused =E2=80=94 dispatch through a transient mapping of the page, dropped with unmap_vcpu_state_page(), which also drops this CPU's translations of it: * vmx_add_msr() and vmx_del_msr() map internally around their search/insert/remove. * vmx_read_guest_msr(), vmx_read_guest_loadonly_msr() and vmx_write_guest_msr() move out of line and do the same, via vmx_find_msr_in(), the existing lookup parameterised on the mapped area. vmx_find_msr() itself is unchanged for its remaining callers, which all run in current context and dereference the returned pointer through the window. vmx_destroy_vmcs() takes the page out of the window before freeing it, with unmap_vcpu_state(): without per-vCPU page tables the window is in the domain-wide area, which outlives a vCPU whose creation fails. The new page pointer goes with the existing page_info pointers after pi_desc rather than next to msr_area: struct pi_desc is 64-byte aligned, so inserting eight bytes ahead of it would have added 56 bytes of padding, growing struct vmx_vcpu from 448 to 512 bytes and, via the union in struct arch_vcpu, every struct vcpu by 64 bytes (x86_64 defconfig). Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v4: - New in this version. --- xen/arch/x86/hvm/vmx/vmcs.c | 134 +++++++++++++++++++++--- xen/arch/x86/include/asm/hvm/vmx/vmcs.h | 63 +++-------- 2 files changed, 139 insertions(+), 58 deletions(-) diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 8ec7ae7403..02ad89e864 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -1416,8 +1416,11 @@ static struct vmx_msr_entry *locate_msr_entry( return start; } =20 -struct vmx_msr_entry *vmx_find_msr(const struct vcpu *v, uint32_t msr, - enum vmx_msr_list_type type) +/* As vmx_find_msr(), but reaching the guest MSR area through @area. */ +static struct vmx_msr_entry *vmx_find_msr_in(const struct vcpu *v, + struct vmx_msr_entry *area, + uint32_t msr, + enum vmx_msr_list_type type) { const struct vmx_vcpu *vmx =3D &v->arch.hvm.vmx; struct vmx_msr_entry *start =3D NULL, *ent, *end; @@ -1435,11 +1438,11 @@ struct vmx_msr_entry *vmx_find_msr(const struct vcp= u *v, uint32_t msr, break; =20 case VMX_MSR_GUEST: - start =3D vmx->msr_area; + start =3D area; break; =20 case VMX_MSR_GUEST_LOADONLY: - start =3D vmx->msr_area; + start =3D area; substart =3D subend; subend =3D total; break; @@ -1458,6 +1461,69 @@ struct vmx_msr_entry *vmx_find_msr(const struct vcpu= *v, uint32_t msr, return ((ent < end) && (ent->index =3D=3D msr)) ? ent : NULL; } =20 +struct vmx_msr_entry *vmx_find_msr(const struct vcpu *v, uint32_t msr, + enum vmx_msr_list_type type) +{ + ASSERT(v =3D=3D current || type =3D=3D VMX_MSR_HOST); + + return vmx_find_msr_in(v, v->arch.hvm.vmx.msr_area, msr, type); +} + +static struct vmx_msr_entry *vmx_msr_area_map(const struct vcpu *v) +{ + const struct vmx_vcpu *vmx =3D &v->arch.hvm.vmx; + + if ( v =3D=3D current || !vmx->msr_area ) + return vmx->msr_area; + + return map_domain_page(page_to_mfn(vmx->msr_area_pg)); +} + +static void vmx_msr_area_unmap(const struct vcpu *v, + const struct vmx_msr_entry *area) +{ + if ( v !=3D current && area ) + unmap_vcpu_state_page(area); +} + +int vmx_read_guest_msr(const struct vcpu *v, uint32_t msr, uint64_t *val) +{ + struct vmx_msr_entry *area =3D vmx_msr_area_map(v); + const struct vmx_msr_entry *ent =3D + area ? vmx_find_msr_in(v, area, msr, VMX_MSR_GUEST) : NULL; + + *val =3D ent ? ent->data : 0; + vmx_msr_area_unmap(v, area); + + return ent ? 0 : -ESRCH; +} + +int vmx_read_guest_loadonly_msr(const struct vcpu *v, uint32_t msr, + uint64_t *val) +{ + struct vmx_msr_entry *area =3D vmx_msr_area_map(v); + const struct vmx_msr_entry *ent =3D + area ? vmx_find_msr_in(v, area, msr, VMX_MSR_GUEST_LOADONLY) : NUL= L; + + *val =3D ent ? ent->data : 0; + vmx_msr_area_unmap(v, area); + + return ent ? 0 : -ESRCH; +} + +int vmx_write_guest_msr(struct vcpu *v, uint32_t msr, uint64_t val) +{ + struct vmx_msr_entry *area =3D vmx_msr_area_map(v); + struct vmx_msr_entry *ent =3D + area ? vmx_find_msr_in(v, area, msr, VMX_MSR_GUEST) : NULL; + + if ( ent ) + ent->data =3D val; + vmx_msr_area_unmap(v, area); + + return ent ? 0 : -ESRCH; +} + int vmx_add_msr(struct vcpu *v, uint32_t msr, uint64_t val, enum vmx_msr_list_type type) { @@ -1503,13 +1569,38 @@ int vmx_add_msr(struct vcpu *v, uint32_t msr, uint6= 4_t val, { paddr_t addr; =20 - if ( (*ptr =3D alloc_xenheap_page()) =3D=3D NULL ) + if ( type =3D=3D VMX_MSR_HOST ) { - rc =3D -ENOMEM; - goto out; + if ( (*ptr =3D alloc_xenheap_page()) =3D=3D NULL ) + { + rc =3D -ENOMEM; + goto out; + } + + addr =3D virt_to_maddr(*ptr); } + else + { + struct page_info *pg =3D alloc_domheap_page(v->domain, + MEMF_no_owner); + mfn_t mfn; + + if ( pg =3D=3D NULL ) + { + rc =3D -ENOMEM; + goto out; + } =20 - addr =3D virt_to_maddr(*ptr); + mfn =3D page_to_mfn(pg); + clear_domain_page(mfn); + populate_perdomain_mapping(v, + VCPU_STATE_ADDR(v, + VCPU_STATE_IDX_VMX_= MSR), + &mfn, 1, __PAGE_HYPERVISOR_RW); + vmx->msr_area_pg =3D pg; + *ptr =3D (void *)VCPU_STATE_ADDR(v, VCPU_STATE_IDX_VMX_MSR); + addr =3D mfn_to_maddr(mfn); + } =20 switch ( type ) { @@ -1525,7 +1616,7 @@ int vmx_add_msr(struct vcpu *v, uint32_t msr, uint64_= t val, } } =20 - start =3D *ptr; + start =3D type =3D=3D VMX_MSR_HOST ? *ptr : vmx_msr_area_map(v); end =3D start + total; ent =3D locate_msr_entry(start + substart, start + subend, msr); =20 @@ -1565,6 +1656,8 @@ int vmx_add_msr(struct vcpu *v, uint32_t msr, uint64_= t val, rc =3D 0; =20 out: + if ( type !=3D VMX_MSR_HOST ) + vmx_msr_area_unmap(v, start); vmx_vmcs_exit(v); =20 return rc; @@ -1576,6 +1669,7 @@ int vmx_del_msr(struct vcpu *v, uint32_t msr, enum vm= x_msr_list_type type) struct vmx_msr_entry *start =3D NULL, *ent, *end; unsigned int substart =3D 0, subend =3D vmx->msr_save_count; unsigned int total =3D vmx->msr_load_count; + int rc =3D 0; =20 ASSERT(v =3D=3D current || !vcpu_runnable(v)); =20 @@ -1605,11 +1699,17 @@ int vmx_del_msr(struct vcpu *v, uint32_t msr, enum = vmx_msr_list_type type) if ( !start ) return -ESRCH; =20 + if ( type !=3D VMX_MSR_HOST ) + start =3D vmx_msr_area_map(v); + end =3D start + total; ent =3D locate_msr_entry(start + substart, start + subend, msr); =20 if ( (ent =3D=3D end) || (ent->index !=3D msr) ) - return -ESRCH; + { + rc =3D -ESRCH; + goto out; + } =20 memmove(ent, ent + 1, sizeof(*ent) * (end - ent - 1)); =20 @@ -1632,7 +1732,11 @@ int vmx_del_msr(struct vcpu *v, uint32_t msr, enum v= mx_msr_list_type type) =20 vmx_vmcs_exit(v); =20 - return 0; + out: + if ( type !=3D VMX_MSR_HOST ) + vmx_msr_area_unmap(v, start); + + return rc; } =20 void vmx_set_eoi_exit_bitmap(struct vcpu *v, u8 vector) @@ -1829,7 +1933,13 @@ void vmx_destroy_vmcs(struct vcpu *v) vmx_free_vmcs(vmx->vmcs_pa); =20 free_xenheap_page(v->arch.hvm.vmx.host_msr_area); - free_xenheap_page(v->arch.hvm.vmx.msr_area); + if ( v->arch.hvm.vmx.msr_area_pg ) + { + unmap_vcpu_state(v, VCPU_STATE_IDX_VMX_MSR, 1); + free_domheap_page(v->arch.hvm.vmx.msr_area_pg); + v->arch.hvm.vmx.msr_area_pg =3D NULL; + v->arch.hvm.vmx.msr_area =3D NULL; + } free_xenheap_page(v->arch.hvm.vmx.msr_bitmap); } =20 diff --git a/xen/arch/x86/include/asm/hvm/vmx/vmcs.h b/xen/arch/x86/include= /asm/hvm/vmx/vmcs.h index d0716e97d2..e5730780e2 100644 --- a/xen/arch/x86/include/asm/hvm/vmx/vmcs.h +++ b/xen/arch/x86/include/asm/hvm/vmx/vmcs.h @@ -140,6 +140,10 @@ struct vmx_vcpu { * context. However, the data can be modified by toolstack/migration * actions. Remote access is only permitted for paused vcpus, and is * protected under the domctl lock. + * + * The guest list is a domain-heap page (msr_area_pg, below) mapped at= a + * fixed slot in the owning vCPU's state window: msr_area holds that + * window VA, which is only valid in the owning vCPU's context. */ struct vmx_msr_entry *msr_area; struct vmx_msr_entry *host_msr_area; @@ -185,6 +189,9 @@ struct vmx_vcpu { =20 struct page_info *pml_pg; =20 + /* Backing page of msr_area; see the comment there. */ + struct page_info *msr_area_pg; + /* Bitmask of trapped CR4 bits. */ unsigned long cr4_host_mask; =20 @@ -669,55 +676,19 @@ static inline int vmx_add_host_load_msr(struct vcpu *= v, uint32_t msr, return vmx_add_msr(v, msr, val, VMX_MSR_HOST); } =20 +/* + * Look an MSR up in the guest or host list of @v. The returned pointer + * reaches the guest lists through the vCPU state window, so for those it + * is only valid (and the call only permitted) in current context; the + * single-entry accessors below handle paused remote vCPUs. + */ struct vmx_msr_entry *vmx_find_msr(const struct vcpu *v, uint32_t msr, enum vmx_msr_list_type type); =20 -static inline int vmx_read_guest_msr(const struct vcpu *v, uint32_t msr, - uint64_t *val) -{ - const struct vmx_msr_entry *ent =3D vmx_find_msr(v, msr, VMX_MSR_GUEST= ); - - if ( !ent ) - { - *val =3D 0; - return -ESRCH; - } - - *val =3D ent->data; - - return 0; -} - -static inline int vmx_read_guest_loadonly_msr( - const struct vcpu *v, uint32_t msr, uint64_t *val) -{ - const struct vmx_msr_entry *ent =3D - vmx_find_msr(v, msr, VMX_MSR_GUEST_LOADONLY); - - if ( !ent ) - { - *val =3D 0; - return -ESRCH; - } - - *val =3D ent->data; - - return 0; -} - -static inline int vmx_write_guest_msr(struct vcpu *v, uint32_t msr, - uint64_t val) -{ - struct vmx_msr_entry *ent =3D vmx_find_msr(v, msr, VMX_MSR_GUEST); - - if ( !ent ) - return -ESRCH; - - ent->data =3D val; - - return 0; -} - +int vmx_read_guest_msr(const struct vcpu *v, uint32_t msr, uint64_t *val); +int vmx_read_guest_loadonly_msr(const struct vcpu *v, uint32_t msr, + uint64_t *val); +int vmx_write_guest_msr(struct vcpu *v, uint32_t msr, uint64_t val); =20 /* MSR intercept bitmap infrastructure. */ enum vmx_msr_intercept_type { --=20 2.55.0