From nobody Mon Nov 23 22:45:27 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=1791369682; cv=none; d=zohomail.com; s=zohoarc; b=QdQ5ghMK6ObpFMqBPvyZYaQB85DaMcUFsEbuxr2UcnbYamoe7DbYL6+292Z4uqyLAnYTJt5OMxhG3kN2SgelR7XpfMzmlWKCoUbFbbrsO47jeIdvKJ7hmW9s63L/8Wud7xjJqwDv+GQoNTopSF+XSfHAJfYoxN2b62ovgt1FRew= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791369682; 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=2RJgLX/T1ocfyreFYfWOaC+F5mQipEttx11jUlqCQM0=; b=clFSOfv6dXZIfyb9hUrkCIWKlRz28X0iVbQa4P2hjWQeAR+Jfr2r/OPuGM2rPOFWpJouTOiXTiPw0k0uL/hd3d/ASaVVSPDdHuM7X1R0sKn7wcMa+UprUD8s36dpeo9Zqo26YMwZJcQdaNS9hBdUuDyU1T6rq8bqcjC8W9kyNyU= 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 1791369682418903.7093431485661; Wed, 7 Oct 2026 03:41:22 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442100.1659628 (Exim 4.92) (envelope-from ) id 1xEP55-0006EA-L6; Wed, 07 Oct 2026 10:41:03 +0000 Received: by outflank-mailman (output) from mailman id 1442100.1659628; Wed, 07 Oct 2026 10:41:03 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP55-0006E3-I5; Wed, 07 Oct 2026 10:41:03 +0000 Received: by outflank-mailman (input) for mailman id 1442100; Wed, 07 Oct 2026 10:41:02 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP54-0006DR-1M for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 10:41:02 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEP53-001bi9-2d; Wed, 07 Oct 2026 10:41:01 +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.96) (envelope-from ) id 1xEP51-001QnT-2w; Wed, 07 Oct 2026 10:41:00 +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=2RJgLX/T1ocfyreFYfWOaC+F5mQipEttx11jUlqCQM0=; b=tMkIj0NdqAsng2M5MZct4j4SKp t5kSrOztB1EGFXGdlcQNQsrlJhJFG6bstpFNicTIGu24fE0DsX+FVmORPQUxLZkOG2diQqUqCfspd mIO06AozB/lw/DRLWPHaeogU7ko5vwI9QxYAYec2LwR9AHA7x9/Wcgtk8wiefXRAcbHo=; 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 v3 01/18] x86/PoD: map one page at a time in p2m_pod_zero_check() Date: Wed, 7 Oct 2026 11:40:34 +0100 Message-ID: <20261007104051.48841-2-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1791369682779158500 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 Reviewed-by: Jan Beulich --- 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, 35 insertions(+), 37 deletions(-) diff --git a/xen/arch/x86/mm/p2m-pod.c b/xen/arch/x86/mm/p2m-pod.c index 4602c32cff..0b0beca15a 100644 --- a/xen/arch/x86/mm/p2m-pod.c +++ b/xen/arch/x86/mm/p2m-pod.c @@ -901,7 +901,8 @@ 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]; + bool check[POD_SWEEP_STRIDE]; + const unsigned long *map; struct domain *d =3D p2m->domain; unsigned int i, j, max_ref =3D 1; =20 @@ -911,7 +912,12 @@ 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. */ + /* + * First, get the gfn list, translate to mfns, and pick the pages to + * check. They are mapped one at a time below, as in + * p2m_pod_zero_check_superpage(): a vCPU can hold only a few transient + * mappings at once, and the p2m lookups and updates here need some to= o. + */ for ( i =3D 0; i < count; i++ ) { p2m_access_t a; @@ -921,18 +927,17 @@ 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; + check[i] =3D false; if ( p2m_is_ram(types[i]) ) { const struct page_info *pg =3D mfn_to_page(mfns[i]); =20 - if ( !is_special_page(pg) && - (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[i] =3D !is_special_page(pg) && + (pg->count_info & PGC_allocated) && + !(pg->count_info & PGC_shadowed_pt) && + ((pg->count_info & PGC_count_mask) <=3D max_ref); } } =20 @@ -942,18 +947,24 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_= t *gfns, unsigned int count */ for ( i =3D 0; i < count; i++ ) { - if ( !map[i] ) + if ( !check[i] ) continue; =20 /* 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[i] =3D false; + continue; + } =20 /* * See if the page was successfully unmapped. (Allow one refcount @@ -961,6 +972,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[i] =3D false; + /* * 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,14 +983,8 @@ 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 @@ -986,22 +993,20 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_= t *gfns, unsigned int count /* Now check each page for real */ for ( i =3D 0; i < count; i++ ) { - if ( !map[i] ) + if ( !check[i] ) continue; =20 - 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 +1017,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:45:27 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=1791369686; cv=none; d=zohomail.com; s=zohoarc; b=HAg8sXmK9SzZmtvlozAlAZcs2rSIi51DBdNtEWdNYY6kTvi+gjErXhwpCWY1qTdMv41ylUMyB/PaaYtd3e3jeSa/IU+0/MX1HDxcxZpwhAijZqZRxwUMB0Qz7nz32FkvTEIrwoHWy2uafvFP0CqNPRQpw1875mEcgTxA0ffxk1E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791369686; 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=6F4975xROA4Reejah9p3olstS3Fw/yLAgfx1ings2V8=; b=n7BBA9KCi0a1L6BO+pSx01IV2VGkGEcbA0fPklSIOwmdJDXOAdL9PzH59PXnHVfwS8eC+eLqh2ffBflxie2G80gifRG+cCFCZq0K7iU8iOdDvFL4HKkfIjK5tWD6l9eMfiskm1iPF9oW87qgHdELdIoFt6FZGFun5B2vpYUgK6s= 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 1791369686519757.6249470114541; Wed, 7 Oct 2026 03:41:26 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442101.1659635 (Exim 4.92) (envelope-from ) id 1xEP55-0006GT-Uu; Wed, 07 Oct 2026 10:41:03 +0000 Received: by outflank-mailman (output) from mailman id 1442101.1659635; Wed, 07 Oct 2026 10:41:03 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP55-0006FP-Od; Wed, 07 Oct 2026 10:41:03 +0000 Received: by outflank-mailman (input) for mailman id 1442101; Wed, 07 Oct 2026 10:41:02 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP54-0006DW-2I for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 10:41:02 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEP53-001biB-2h; Wed, 07 Oct 2026 10:41:01 +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.96) (envelope-from ) id 1xEP52-001QnT-30; Wed, 07 Oct 2026 10:41:01 +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=6F4975xROA4Reejah9p3olstS3Fw/yLAgfx1ings2V8=; b=nmlrjvuu3lPXyJn0kGJ9MeqIEV sg9DtgM5em1DTeXrSTinPasdGpSch/Kl1QigB5IDM+SaHf6YbwS+2DcxpFqzjCYD2ZI+srlGZphO4 2QgwM//vGv8UvussFgbA258MQMW903xs/KfYOBXKWQg8H17b8oiudJFwgRaubM6Hteb0=; 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 v3 02/18] x86/mm: purge unneeded destroy_perdomain_mapping() Date: Wed, 7 Oct 2026 11:40:35 +0100 Message-ID: <20261007104051.48841-3-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@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: 1791369686777158500 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: in addition to vCPU teardown, it serves switch_compat()'s undo path, where the domain lives on as a 64-bit one and the argument-translation pages have to go. Add a comment to make sure it isn't missed. Signed-off-by: Roger Pau Monn=C3=A9 Assisted-by: Claude Code:claude-fable-5 Signed-off-by: George Dunlap Reviewed-by: Jan Beulich Reviewed-by: Roger Pau Monn=C3=A9 Reviewed-by; with the maintainer who did the most recent change --- 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 ---------- xen/arch/x86/x86_64/mm.c | 5 +++++ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 192309c2fc..3924c1b204 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 03262a4952..07ea4bb4d2 100644 --- a/xen/arch/x86/pv/domain.c +++ b/xen/arch/x86/pv/domain.c @@ -332,12 +332,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) ) @@ -346,7 +340,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 @@ -394,9 +387,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); diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c index 8eadab7933..2a1805964d 100644 --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -737,6 +737,11 @@ int setup_compat_arg_xlat(struct vcpu *v) NULL, NIL(struct page_info *)); } =20 +/* + * Besides vCPU teardown, which free_perdomain_mappings() would cover, this + * serves switch_compat()'s undo path: the domain lives on as a 64-bit one + * there, so the translation area has to go right away. + */ void free_compat_arg_xlat(struct vcpu *v) { destroy_perdomain_mapping(v->domain, ARG_XLAT_START(v), --=20 2.55.0 From nobody Mon Nov 23 22:45:27 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=1791369681; cv=none; d=zohomail.com; s=zohoarc; b=ddqi9YTNmEfxhMLOQ66oJa1cmdn9Rh4Zm5PfGGQLVS1R1gmLEk19UsUCi3Rp05OFB3jD1o5udVWsm2YHxzRfbun8OwuXEyDPEniQRDM5cWS78qEX33l2V6ejclCiVUjIwuc75/UxS05JgXTelxJaKTePZP6m/nMoc5wOlAFcBds= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791369681; 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=3unBXKoSIkkOI4lQHAOOYMO5jJksROtYlSF7H+YURFQ=; b=W/iiGfLtKlM0Mw27CXUELk+KXJboki6iT0B01CZhFVTJp4hl0qZ73UT5hMjc5UfjL6t8HeEteiYdPidPRqZu2WUZQo2tgXZ5qFEP/XZRL0c8OZV8XS5+MpV3zBnRPbGd5+2c1rAZ77aL2yk+HTLBNLrzFQyzpYXXN2eY1nofURo= 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 1791369681340191.5722566610333; Wed, 7 Oct 2026 03:41:21 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442102.1659638 (Exim 4.92) (envelope-from ) id 1xEP56-0006LE-4t; Wed, 07 Oct 2026 10:41:04 +0000 Received: by outflank-mailman (output) from mailman id 1442102.1659638; Wed, 07 Oct 2026 10:41:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP56-0006Jd-0O; Wed, 07 Oct 2026 10:41:04 +0000 Received: by outflank-mailman (input) for mailman id 1442102; Wed, 07 Oct 2026 10:41:02 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP54-0006Df-DW for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 10:41:02 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEP54-001biM-0e; Wed, 07 Oct 2026 10:41:02 +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.96) (envelope-from ) id 1xEP53-001QnT-34; Wed, 07 Oct 2026 10:41:02 +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=3unBXKoSIkkOI4lQHAOOYMO5jJksROtYlSF7H+YURFQ=; b=LusKlcGYZaE+yA7HIQA5d1uH1u tdRg7BXimupG6gKpn7KsvQ/MwUItTUsp4ad0jKfxmaDIrGqMNLuchRdJMifCVMgEAVIkdh9973hrE Dx/LMCkIALELlEOP7pft5tEXJ80sONnS1w9p/T5KkbQCtS4EdF4ds6chHkdi3BxnI8nI=; 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 v3 03/18] x86/mm: prepare destroy_perdomain_mapping() for per-vCPU perdomain areas Date: Wed, 7 Oct 2026 11:40:36 +0100 Message-ID: <20261007104051.48841-4-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@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: 1791369682744158500 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. 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): 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 2254a7e3fe..b19b1ae53f 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); 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 b158742408..769b9b3016 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6334,10 +6334,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 2a1805964d..8c576acdc7 100644 --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -744,7 +744,7 @@ int setup_compat_arg_xlat(struct vcpu *v) */ 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:45:27 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=1791369683; cv=none; d=zohomail.com; s=zohoarc; b=SlU+aDpmQim3TQ/HSPn6DHXW1AbOrnPVBIPpYc0PgAO0hkROlsWBYSMzAttAZh5g7MYM9Saf5QUPuq2jBXPIzMOax6Ym/h6fBkplL6DBOgASsQqAsRBqpjpsN706x28uw+WVjEI610OHWz28j0IzPTzRsaVnGf7+1kIdXvaVzy8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791369683; 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=qnQj2VEk2nRRCGqKLbeeB4D2O0uugqBN47xEZ7vScLM=; b=KCAch4ZSuMfgvnQOlKIFxH3+xAitxqfbLNNh0Ctocxhe+VKDVKuU/aRsxELl64zZhe3TuLG6E578TJwnjhCvwTGmF8lXE7xvAqy+Xt2Rbl0eMvc8o0NRLvJw5YT8xerPz2r+Zy4n/Ad/75JqyM9v/AU4hic7+7xR584ofncivZA= 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 179136968312336.841174302417244; Wed, 7 Oct 2026 03:41:23 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442103.1659647 (Exim 4.92) (envelope-from ) id 1xEP56-0006WS-Me; Wed, 07 Oct 2026 10:41:04 +0000 Received: by outflank-mailman (output) from mailman id 1442103.1659647; Wed, 07 Oct 2026 10:41:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP56-0006VA-Gp; Wed, 07 Oct 2026 10:41:04 +0000 Received: by outflank-mailman (input) for mailman id 1442103; Wed, 07 Oct 2026 10:41:03 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP55-0006Dv-6o for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 10:41:03 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEP55-001biU-0O; Wed, 07 Oct 2026 10:41:03 +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.96) (envelope-from ) id 1xEP54-001QnT-3A; Wed, 07 Oct 2026 10:41:03 +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=qnQj2VEk2nRRCGqKLbeeB4D2O0uugqBN47xEZ7vScLM=; b=69sijNqB/D1jvpPjMn/yj6zyHe cNpGtNQeMTPuL9suIhR8GXL8J9vjASriTf/Gh3F5bKl572Cv4rEjtUZVje8ClMMI10zGCgUsd+gPb qDKODXMT97MlDWyjLv7E69yp2ONtFP61/ho7jwuTQti8ds1Oc7KeS41RarEB95ef8oUA=; 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 v3 04/18] x86/domain_page: drop redundant create_perdomain_mapping() call Date: Wed, 7 Oct 2026 11:40:37 +0100 Message-ID: <20261007104051.48841-5-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@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: 1791369684794158500 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 996b50af7a..85cedc3d03 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 72c00194f3..3c90acd2bc 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 6cb76781e4..c32dec793a 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:45:27 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=1791369695; cv=none; d=zohomail.com; s=zohoarc; b=Zn27SLSuAsZDNJFezfF6wwYXDhZJLpBdo5NKy7pYCKp+AH/UDKWP57tLJ4x2HB+IdcCP4wmwvnsr8ATN1G/H/ZvXuFYKfYqOmJztfjfyN648/wgJbiKpnwkb6nf8fp+i8xk9h5prxK6Ekslt2qqb/s+PmU5NjRo9D60Bi2Kk4o4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791369695; 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=YN/PwYdSyq7VIT0fmDEyLm8kDVbqAXggB4l9ZiyBJMg=; b=RMmaIVhRG3rnX4U91/53NnbbG2sHsNwE35F9qPnVziRIBc263+npHpf/yZCYYmK/mnfb6SDrp4uhgq6AWcueD8mAelj5dFZFjNFidmzPT8b8wcK6mKRHhdqhxQVD1uPn56eDqv//XbOhGc5CX+M9iEdqYxJ5pWN1DXJHmkJpnWU= 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 1791369695022267.6483585086851; Wed, 7 Oct 2026 03:41:35 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442104.1659663 (Exim 4.92) (envelope-from ) id 1xEP57-00070b-Uz; Wed, 07 Oct 2026 10:41:05 +0000 Received: by outflank-mailman (output) from mailman id 1442104.1659663; Wed, 07 Oct 2026 10:41:05 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP57-0006zE-Q0; Wed, 07 Oct 2026 10:41:05 +0000 Received: by outflank-mailman (input) for mailman id 1442104; Wed, 07 Oct 2026 10:41:04 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP56-0006VT-I5 for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 10:41:04 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEP56-001bid-0Y; Wed, 07 Oct 2026 10:41:04 +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.96) (envelope-from ) id 1xEP55-001QnT-31; Wed, 07 Oct 2026 10:41:04 +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=YN/PwYdSyq7VIT0fmDEyLm8kDVbqAXggB4l9ZiyBJMg=; b=RkBIxN5W8+L9JPPHukfY1G4qdh Q48ollnxvI89JXACHnR91jF1kUTQ5a1Da4URTfytAyTeXBA823JWKVUpKLmApnc1N6ZNW2otEL1ot e8mY91fd61hIloJ33EmNx/MYl3uCYEI0iGcaCEeBs9+vtmO0r3Gz85ZOrGMnzVAEi6d4=; 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 v3 05/18] x86/mm: prepare create_perdomain_mapping() for per-vCPU perdomain areas Date: Wed, 7 Oct 2026 11:40:38 +0100 Message-ID: <20261007104051.48841-6-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@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: 1791369696922158501 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. vCPU creation is where the call must end up; move it there directly. 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. Teardown will move to vCPU scope only once the structure itself becomes per-vCPU. 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 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 | 10 ++++++++++ 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, 18 insertions(+), 12 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index 85cedc3d03..b670cea52b 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -517,6 +517,16 @@ 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): with + * create_perdomain_mapping() taking a vCPU this can no longer be + * done when creating the domain. + */ + 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 3c90acd2bc..9119f735f5 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 3924c1b204..5572728db3 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 b19b1ae53f..87bbe1a6f5 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -603,7 +603,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 769b9b3016..ca22107cd3 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -6211,10 +6211,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 07ea4bb4d2..9877771cd4 100644 --- a/xen/arch/x86/pv/domain.c +++ b/xen/arch/x86/pv/domain.c @@ -326,7 +326,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 8c576acdc7..0f9e0469ef 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:45:27 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=1791369684; cv=none; d=zohomail.com; s=zohoarc; b=IQLaL2IULHdwVwRWDttMfSnrv0GdH36b13Np7h3sTGQJuIp+GcrJFC1FLMtBJRh9e4Cd9gFCPxN6Ag8NRS6/Ef9WoGl6nuPwULeULL/LlAQMIoM1mxWuawyZdRBFlIspSJnX+B3gtpAs0iASTRIBqNTN5lB8Il4vacWBxT74gEQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791369684; 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=iRGNOpRPWzouzTTqvxlO4hygSWei2nMKlmUXT9MAbQg=; b=X0MaXA3eEyIEybtCCtZrrfurltnNLKbYY0evKFtNZEs5w9sCFddyv0q1zbqzbPO36PLLSM9inq+ynChWl8T/emNXCNdyGv5zPPZ53Nh9gDUzIA1Y73fj+6Wnv9Wz5cqFjOB5YRvuGcp6CvZ6Yo0sLRu6dyFKNmAPAOMcKKF5wPc= 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 1791369684359264.9295679398192; Wed, 7 Oct 2026 03:41:24 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442105.1659673 (Exim 4.92) (envelope-from ) id 1xEP59-0007Jz-Cu; Wed, 07 Oct 2026 10:41:07 +0000 Received: by outflank-mailman (output) from mailman id 1442105.1659673; Wed, 07 Oct 2026 10:41:07 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP59-0007Jp-9g; Wed, 07 Oct 2026 10:41:07 +0000 Received: by outflank-mailman (input) for mailman id 1442105; Wed, 07 Oct 2026 10:41:06 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP58-0007BF-Fw for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 10:41:06 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEP57-001bin-1w; Wed, 07 Oct 2026 10:41:05 +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.96) (envelope-from ) id 1xEP57-001QnT-1B; Wed, 07 Oct 2026 10:41:05 +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=iRGNOpRPWzouzTTqvxlO4hygSWei2nMKlmUXT9MAbQg=; b=4Hr+ztIwSfu28djVniEge4dP0f xKdXcJiuOmQymV/YnDTvZYGHn0L2DQLJKr9J3p1SkClvhDd/0QhVx7V0Zc7y8570iUjpChHtGIWUR 0IFAdwzbcDM/DaKkAnvGI78RymaiqNjsIf8io/93cfZG2a3zgfKsk4gjpaHDxMPKB2K4=; 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 v3 06/18] x86/spec-ctrl: introduce Address Space Isolation command line option Date: Wed, 7 Oct 2026 11:40:39 +0100 Message-ID: <20261007104051.48841-7-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@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: 1791369684832158500 From: Roger Pau Monn=C3=A9 Introduce the `asi=3D` command line option, and the opt_vcpu_pt_{hwdom,hvm} knobs plus the per-domain d->arch.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 taints the boot with a "not functional, development purposes only" warning. 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.vcpu_pt is therefore only ever set for HVM domains, a PVH hardware domain included; PV domains, a PV hardware domain included, are unaffected. 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 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 | 26 +++++++++ xen/arch/x86/include/asm/domain.h | 6 +++ xen/arch/x86/include/asm/spec_ctrl.h | 2 + xen/arch/x86/spec_ctrl.c | 79 ++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+) diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line= .pandoc index b2c94ae56d..785e099c0a 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -202,6 +202,32 @@ 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 [ , hvm=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 guests, including a PVH +hardware domain. PV guests, including a PV hardware domain, are unaffecte= d. + +* `hvm=3D` enables the features for HVM guests other than the hardware dom= ain, + which follows the whole-feature forms (the plain boolean, or an un-suffi= xed + `vcpu-pt=3D`). + +**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 c32dec793a..b192c86bc1 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -477,6 +477,12 @@ struct arch_domain /* Don't unconditionally inject #GP for unhandled MSRs. */ bool msr_relaxed; =20 + /* + * Give each vCPU its own per-domain area (HVM only: each vCPU already + * runs on its own monitor table). + */ + bool vcpu_pt; + /* 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..b62229ab5d 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_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..999e41aefa 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -86,6 +86,13 @@ 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). Off= by + * default until the feature is complete. + */ +bool __ro_after_init opt_vcpu_pt_hvm; +bool __ro_after_init opt_vcpu_pt_hwdom; + static int __init cf_check parse_spec_ctrl(const char *s) { const char *ss; @@ -487,6 +494,62 @@ 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_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_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D val; + break; + + default: + if ( (val =3D parse_boolean("hvm", s, ss)) >=3D 0 ) + opt_vcpu_pt_hvm =3D val; + else if ( (val =3D parse_boolean("vcpu-pt", s, ss)) !=3D -1 ) + { + switch ( val ) + { + case 1: + case 0: + opt_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; + else + 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 +743,14 @@ static void __init print_details(enum ind_thunk thunk) opt_pv_l1tf_hwdom ? "enabled" : "disabled", opt_pv_l1tf_domu ? "enabled" : "disabled"); #endif + + /* vCPU-PT is implemented for HVM only: a PV Dom0 gets none of it. */ + printk(" ASI features for Dom0:%s\n", + opt_dom0_pvh && opt_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 +1937,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.vcpu_pt =3D !pv && (is_hardware_domain(d) ? opt_vcpu_pt_hwdom + : opt_vcpu_pt_hvm); } =20 void __init init_speculation_mitigations(void) @@ -2158,6 +2232,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_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:45:27 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=1791369691; cv=none; d=zohomail.com; s=zohoarc; b=ZhTTZCiHyOc0ORtiI5kvpbWFi2zWUKEoIEjx2gGzmQ+Kgo2qB2V42xOQbl0NvOai/J7yPxewJBi4rxFR6sT1xiU84kAMP8fZuGlsPHCfeH8Q3nkTgXm690GfOQ9wY891aIuhxPaHKrQx2kscCYm0RKZGEasVgtahx+XG+kr/Vz8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791369691; 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=dxYHMPntQ+4kmk+nNJUY+2zkq3fsoiDnWTQiSZvrAmg=; b=nWCAOAgy5ZpMIZiHoUFWkVj4vRHQUBj+X0Vpz3GNn7nt3LPlTqARfRsodJiE03InziOvSvjDiFQg76BB9pBxfH66qIrpAhtIg2R8XlWaVl2AJOA5ajOsqiSgcfOEAwYixexY7pajLVrEVo3N3EY6VwmSfhF5zumbGCjM8fbgitI= 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 1791369691313551.4622572909359; Wed, 7 Oct 2026 03:41:31 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442107.1659687 (Exim 4.92) (envelope-from ) id 1xEP5C-0007fN-1N; Wed, 07 Oct 2026 10:41:10 +0000 Received: by outflank-mailman (output) from mailman id 1442107.1659687; Wed, 07 Oct 2026 10:41:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP5B-0007fA-Rz; Wed, 07 Oct 2026 10:41:09 +0000 Received: by outflank-mailman (input) for mailman id 1442107; Wed, 07 Oct 2026 10:41:09 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP5A-0007aX-Vk for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 10:41:08 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEP59-001biv-08; Wed, 07 Oct 2026 10:41:07 +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.96) (envelope-from ) id 1xEP58-001QnT-2a; Wed, 07 Oct 2026 10:41:07 +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=dxYHMPntQ+4kmk+nNJUY+2zkq3fsoiDnWTQiSZvrAmg=; b=sEIS1rJ1iqPopni0BxJAmvyFTL A66gDejbvn7BMqOQPgpPVcmtcSnkq2RP9hnvkeIP1Yh79ZDNDeiaOe9QGRsan3grlAGnCNGW4kOjJ Py3/gcQS5h7kt0B1phthmC7HKQ3+0aqZ1kfeOLuV1o0mz84nt5SeM8kPBhrgjf98RlFw=; 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 , Tim Deegan Subject: [PATCH v3 07/18] x86/hvm: introduce per-vCPU L3 page-table Date: Wed, 7 Oct 2026 11:40:40 +0100 Message-ID: <20261007104051.48841-8-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@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: 1791369692862158500 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.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() is unchanged and keeps covering non-vCPU-PT domains. Give init_xen_l4_slots() the vCPU a table is private to, and use perdomain_l3() to select the value to install in slot 260 when one is given. The HVM monitor tables and the 32-bit PV Xen-owned L4s pass their vCPU; tables the domain's vCPUs share -- promoted PV guest L4s, PV shadow L4s, and the PV dom0 L4 under construction -- pass NULL and keep the domain-wide L3, which is all a PV domain has. 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 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 | 10 +++-- xen/arch/x86/include/asm/domain.h | 12 +++++ xen/arch/x86/include/asm/mm.h | 5 ++- xen/arch/x86/mm.c | 73 ++++++++++++++++++++++--------- xen/arch/x86/mm/hap/hap.c | 2 +- xen/arch/x86/mm/shadow/hvm.c | 2 +- xen/arch/x86/mm/shadow/multi.c | 5 ++- xen/arch/x86/pv/dom0_build.c | 2 +- xen/arch/x86/pv/domain.c | 2 +- xen/arch/x86/x86_64/mm.c | 7 +-- 10 files changed, 87 insertions(+), 33 deletions(-) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index b670cea52b..0aba2cca2e 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_early; =20 if ( !is_idle_domain(d) ) { @@ -525,12 +525,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_early; =20 paging_vcpu_init(v); =20 if ( (rc =3D vcpu_init_fpu(v)) !=3D 0 ) - return rc; + goto fail_early; =20 vmce_init_vcpu(v); =20 @@ -578,6 +578,8 @@ int arch_vcpu_create(struct vcpu *v) vcpu_destroy_fpu(v); xfree(v->arch.msrs); v->arch.msrs =3D NULL; + fail_early: + free_pervcpu_mappings(v); =20 return rc; } @@ -598,6 +600,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(struct xen_domctl_createdomain *config) diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index b192c86bc1..a6c6eaf53a 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -335,6 +335,11 @@ struct monitor_write_data { =20 struct arch_domain { + /* + * Domain-wide L3 page-table for the L4 per-domain slot, used when + * the domain does not use per-vCPU page-tables (!d->arch.vcpu_pt). + * NULL otherwise (see v->arch.pervcpu_l3_pg and perdomain_l3()). + */ struct page_info *perdomain_l3_pg; =20 /* I/O-port admin-specified access capabilities. */ @@ -691,6 +696,13 @@ struct arch_vcpu =20 struct vcpu_msrs *msrs; =20 + /* + * Per-vCPU L3 page-table for the L4 per-domain slot, used when the + * domain uses per-vCPU page-tables (d->arch.vcpu_pt). NULL + * otherwise (see d->arch.perdomain_l3_pg and perdomain_l3()). + */ + 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 87bbe1a6f5..87131dd4bf 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -369,8 +369,10 @@ int devalidate_page(struct page_info *page, unsigned l= ong 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 domain *d, mfn_t sl4mfn, bool ro_mpt); + const struct domain *d, 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 @@ -609,6 +611,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 ca22107cd3..7258935528 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -1638,6 +1638,17 @@ 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.vcpu_pt ? v->arch.pervcpu_l3_pg : d->arch.perdomain_l3_= pg; +} + /* * Fill an L4 with Xen entries. * @@ -1645,12 +1656,16 @@ static int promote_l3_table(struct page_info *page) * 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. All other parameters are optional and will either fill or zero t= he - * appropriate slots. Pagetables not shared with guests will gain the + * *l4t. v names the vCPU the table is private to (an HVM monitor table, a + * 32-bit PV vCPU's Xen-owned L4), or is NULL for tables the domain's vCPUs + * share; it is mandatory for a vCPU-PT domain, whose per-domain area is + * per-vCPU. All other parameters are optional and will either fill or ze= ro + * the appropriate slots. Pagetables not shared with guests will gain the * extended directmap. */ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, - const struct domain *d, mfn_t sl4mfn, bool ro_mpt) + const struct domain *d, const struct vcpu *v, + mfn_t sl4mfn, bool ro_mpt) { /* * PV vcpus need a shortened directmap. HVM and Idle vcpus get the fu= ll @@ -1678,8 +1693,11 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mf= n, l4e_from_mfn(sl4mfn, __PAGE_HYPERVISOR_RW); =20 /* Slot 260: Per-domain mappings. */ + ASSERT(!v || v->domain =3D=3D d); + ASSERT(v || !d->arch.vcpu_pt); l4t[l4_table_offset(PERDOMAIN_VIRT_START)] =3D - l4e_from_page(d->arch.perdomain_l3_pg, __PAGE_HYPERVISOR_RW); + l4e_from_page(v ? perdomain_l3(v) : d->arch.perdomain_l3_pg, + __PAGE_HYPERVISOR_RW); =20 /* Slot 4: Per-domain mappings mirror. */ BUILD_BUG_ON(IS_ENABLED(CONFIG_PV32) && @@ -1835,7 +1853,7 @@ static int promote_l4_table(struct page_info *page) if ( !rc ) { init_xen_l4_slots(pl4e, l4mfn, - d, INVALID_MFN, VM_ASSIST(d, m2p_strict)); + d, NULL, INVALID_MFN, VM_ASSIST(d, m2p_strict)); atomic_inc(&d->arch.pv.nr_l4_pages); } unmap_domain_page(pl4e); @@ -6216,7 +6234,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; @@ -6225,14 +6243,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.vcpu_pt ) + v->arch.pervcpu_l3_pg =3D pg; + else + d->arch.perdomain_l3_pg =3D pg; if ( !nr ) { unmap_domain_page(l3tab); @@ -6242,7 +6263,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 @@ -6339,16 +6360,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 ) @@ -6387,16 +6408,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 ) { @@ -6432,10 +6448,27 @@ 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) +{ + if ( !d->arch.perdomain_l3_pg ) + return; + + free_perdomain_l3(d->arch.perdomain_l3_pg); d->arch.perdomain_l3_pg =3D NULL; } =20 +void free_pervcpu_mappings(struct vcpu *v) +{ + if ( !v->arch.pervcpu_l3_pg ) + return; + + free_perdomain_l3(v->arch.pervcpu_l3_pg); + v->arch.pervcpu_l3_pg =3D NULL; +} + static void write_sss_token(unsigned long *ptr) { /* diff --git a/xen/arch/x86/mm/hap/hap.c b/xen/arch/x86/mm/hap/hap.c index 0ede4181a0..9fc59f0702 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, d, 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..833e378e30 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, d, 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..b2017c1010 100644 --- a/xen/arch/x86/mm/shadow/multi.c +++ b/xen/arch/x86/mm/shadow/multi.c @@ -974,8 +974,9 @@ 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) = && - VM_ASSIST(d, m2p_strict= ))); + init_xen_l4_slots(l4t, gmfn, d, NULL, smfn, + (!is_pv_32bit_domain(d) && + VM_ASSIST(d, m2p_strict))); unmap_domain_page(l4t); } break; diff --git a/xen/arch/x86/pv/dom0_build.c b/xen/arch/x86/pv/dom0_build.c index ddeb144b06..bf980eba8c 100644 --- a/xen/arch/x86/pv/dom0_build.c +++ b/xen/arch/x86/pv/dom0_build.c @@ -726,7 +726,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); + d, NULL, 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 9877771cd4..c1611fdcff 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->domain, v, INVALID_MFN, false); unmap_domain_page(l4tab); =20 /* This page needs to look like a pagetable so that it can be shadowed= */ diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c index 0f9e0469ef..baee0a9cfe 100644 --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -738,9 +738,10 @@ int setup_compat_arg_xlat(struct vcpu *v) } =20 /* - * Besides vCPU teardown, which free_perdomain_mappings() would cover, this - * serves switch_compat()'s undo path: the domain lives on as a 64-bit one - * there, so the translation area has to go right away. + * Besides vCPU teardown, which the per-domain area's own teardown would + * cover (free_perdomain_mappings(), or free_pervcpu_mappings() for a + * vCPU-PT domain), this serves switch_compat()'s undo path: the domain li= ves + * on as a 64-bit one there, so the translation area has to go right away. */ void free_compat_arg_xlat(struct vcpu *v) { --=20 2.55.0 From nobody Mon Nov 23 22:45:27 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=1791369695; cv=none; d=zohomail.com; s=zohoarc; b=bOxr18h5fIJxDKypoPpOW5em+F+UIcUJe/niOHXYW+gLjASM/ArWUIg5BKB+VCrqHXtP8rLE6Ih6oBQa+PJ/smr5Fc2LE1H1ly5Jlst8V/ZcV/ykqhQ9ZY3niDwaMdtztI9wAfCbnrPxRBU3JFZzYXgXQVhGdHLU7lA/B2uz63c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791369695; 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=dhqUQmvZLvbJ486b5VhpqBi7A5iX71jVcCO1Kn2eJgk=; b=gzHnh/lapHZaOL8SM4DE1rOt4RaoqMhaR8rQS1i5zCkIK8o1CVMXqzDPJ2Pglsn0JUb9GtwcyLaYbzQh49R2OSkU+bEfFLQilZFfA4OOLmJyhlvyEsR0MhGg9CVTajPI4tSvmIo28G37rcryd7rUX5LLU8TZtHwgGWQ/odtpKbo= 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 1791369695898872.8590217903167; Wed, 7 Oct 2026 03:41:35 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442106.1659682 (Exim 4.92) (envelope-from ) id 1xEP5B-0007cF-L3; Wed, 07 Oct 2026 10:41:09 +0000 Received: by outflank-mailman (output) from mailman id 1442106.1659682; Wed, 07 Oct 2026 10:41:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP5B-0007bw-HU; Wed, 07 Oct 2026 10:41:09 +0000 Received: by outflank-mailman (input) for mailman id 1442106; Wed, 07 Oct 2026 10:41:08 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP5A-0007Tq-3D for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 10:41:08 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEP5A-001bj0-01; Wed, 07 Oct 2026 10:41:08 +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.96) (envelope-from ) id 1xEP59-001QnT-2n; Wed, 07 Oct 2026 10:41:07 +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=dhqUQmvZLvbJ486b5VhpqBi7A5iX71jVcCO1Kn2eJgk=; b=Oux8bGxlk0zuVswcqf2KM8B1xF qPJZf1b1rdYN1TRjZZy7yzL/qU9OLjTt2xD/bE79ikYVkc4o3Tpip4VDlmqIju3EeWbuxlzRr7rVj fqjl3Bo3eE+S/kuzK5eQxi9TJTUUgvsSsf530NRrAolbf6TMQQJtCtrI6zPL6EOFyA1Y=; 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 v3 08/18] x86/domain_page: prepare the mapcache for per-vCPU accounting Date: Wed, 7 Oct 2026 11:40:41 +0100 Message-ID: <20261007104051.48841-9-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@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: 1791369696922158500 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. map_domain_page() and unmap_domain_page() stop repeating the is_pv_vcpu() check mapcache_current_vcpu() has made. Move map_domain_page()'s handling of the domain-wide lock and TLB epoch into three helpers: mapcache_lock() takes the lock and catches up with a wrap another vCPU caused, mapcache_new_epoch() starts a new epoch after a wrap, and mapcache_unlock() 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 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 | 155 ++++++++++++++++++++---------- xen/arch/x86/include/asm/domain.h | 19 ++-- 2 files changed, 116 insertions(+), 58 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 9119f735f5..5d90f77bed 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -18,17 +18,23 @@ #include #include =20 +/* + * Whether v has a mapcache: PV vCPUs (whose domain-wide mapcache may stil= l be + * disabled, see mapcache_domain_init()). HVM vCPUs reach everything thro= ugh + * the directmap, which covers all physical address space for them. + */ +static bool has_mapcache(const struct vcpu *v) +{ + return is_pv_vcpu(v); +} + static inline struct vcpu *mapcache_current_vcpu(void) { struct vcpu *v =3D this_cpu(pgtable_vcpu); 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 +60,55 @@ 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; +} + +/* + * The domain-wide mapcache's lock and TLB-epoch machinery, taken around + * allocating a slot: the domain's vCPUs share the mapcache's slots, and e= ach + * catches up with a wrap another one caused, flushing its TLB if it has n= ot + * been flushed since. + */ +static void mapcache_lock(struct mapcache_domain *dcache, + struct mapcache_vcpu *vcache) +{ + 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) +{ + vcache->shadow_epoch =3D ++dcache->epoch; + dcache->tlbflush_timestamp =3D tlbflush_current_time(); +} + +static void mapcache_unlock(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 +121,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 @@ -74,12 +130,12 @@ 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; + 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 +146,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); - - /* 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(); - } - } + mapcache_lock(dcache, vcache); =20 - 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 +189,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_unlock(dcache); =20 l1e_write(&MAPCACHE_L1ENT(idx), l1e_from_mfn(mfn, __PAGE_HYPERVISOR_RW= )); =20 @@ -170,6 +214,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 @@ -179,10 +224,10 @@ 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); + 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 +250,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 +262,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 +284,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 +294,23 @@ 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)); + unsigned int ents, nr; =20 - if ( !is_pv_vcpu(v) || !dcache->inuse ) + if ( !has_mapcache(v) ) return 0; =20 - if ( ents > dcache->entries ) + cache =3D vcpu_mapcache(v, &dcache); + if ( !cache->inuse ) + /* Domain-wide mapcache disabled (fully direct-mapped build). */ + return 0; + + ents =3D d->max_vcpus * MAPCACHE_VCPU_ENTRIES; + nr =3D PFN_UP(BITS_TO_LONGS(ents) * sizeof(long)); + + if ( ents > cache->entries ) { /* Populate page tables. */ int rc =3D create_perdomain_mapping(v, MAPCACHE_VIRT_START, ents, @@ -265,16 +318,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 a6c6eaf53a..6f5873879e 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:45:27 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=1791369716; cv=none; d=zohomail.com; s=zohoarc; b=AcSFUIJ7k79jiwYezugII+fcMz/Qik7n4X0YrP6CAbMvh01Mef6NCt4frOML8T6bfMrqij7Mx4yMqQLEtL4nppWoRUWapWsrKC4fdk//AKYcj9ZG+eIoBXSwpqfRq1JqtgtxVF6X11/dRwd/i3R/5w/bzPFUt26xYE0zeK7XL0M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791369716; 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=ZVsDH654BNOr5j/nSB+hEf87XBdvPaHAdCERFcMYZ2E=; b=UuhIunIsNJvPl5j5qXK74X59gID8EKOBXs6Axb1ZlCDbTVbODYzYDNt7pzWPANMiRYbS4L0H/unItOWuzm/10WdW3qippmrsOT3ENDDzNPxfLACy3qtM/ZTjQrNabrguvYOvtx0b6Le9zaiNlrU5R99q5K7HUgYjJeg3vVfhPaU= 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 1791369716562347.4400944090379; Wed, 7 Oct 2026 03:41:56 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442108.1659693 (Exim 4.92) (envelope-from ) id 1xEP5C-0007oz-LG; Wed, 07 Oct 2026 10:41:10 +0000 Received: by outflank-mailman (output) from mailman id 1442108.1659693; Wed, 07 Oct 2026 10:41:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP5C-0007ni-Du; Wed, 07 Oct 2026 10:41:10 +0000 Received: by outflank-mailman (input) for mailman id 1442108; Wed, 07 Oct 2026 10:41:09 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEP5B-0007ay-40 for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 10:41:09 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEP5B-001bjC-0B; Wed, 07 Oct 2026 10:41:09 +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.96) (envelope-from ) id 1xEP5A-001QnT-2f; Wed, 07 Oct 2026 10:41:09 +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=ZVsDH654BNOr5j/nSB+hEf87XBdvPaHAdCERFcMYZ2E=; b=x/3yVIuNIlXPV+gDEtn/VLKHgC p0rkmckTlK1KK9lAR5ZoqZ4spaxI3w8kUcVLaQC4PMzRDJ9gkGSL8tWLkgjV44lUIx/e4cY1EgDSV CU/6qb00yjdMeKfLmL9XHFnEaz6L08Ga67CeJSkO/rVqMfuAzrwG5Yh1F9Dhfgy17DGg=; 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 v3 09/18] x86/domain_page: move struct mapcache_vcpu to struct arch_vcpu Date: Wed, 7 Oct 2026 11:40:42 +0100 Message-ID: <20261007104051.48841-10-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@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: 1791369716933158500 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 5d90f77bed..f91ec8a75a 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -134,7 +134,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 @@ -231,7 +231,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 @@ -334,7 +334,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 6f5873879e..cfa24c4e75 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -552,9 +552,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; @@ -650,6 +647,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:45:27 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=1791370855; cv=none; d=zohomail.com; s=zohoarc; b=azIovYSjWmoyBCpRpyew9YWzyQMYFoxdwwf26AFSiF2UEFSoQSyr9HlaoILWstLTnkVBBloXf/8O7QBlhC8OVYxp4DeXfWd37epa+AefqqMp+MMRSQmYgZ9zLTmK+DNnlpP8timPX6V3yy84mTr47NNCKL8Y+Fi5gNPF1vKmj/0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791370855; 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=P7gSAZkpbEQZ016S+qOjogD467NTdt8ZgNSKB5Dcc3E=; b=DYZuRXGbBRU3huMEIEfTjPicaxTcLaD9zHaKNI2ic+QsyBnDmcjWKDkpk1pIJpt2Iv32DJ3Yz8KydTeE6R0AiYHi6dwbFUeaA8PLDGNgdLdCCOker4Fea5V0PolK+l/M+GoZnDLCYs29W5SMCwVCgPwGrbNBSlpkpm7QsTjn4ck= 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 1791370855596611.5515190260967; Wed, 7 Oct 2026 04:00:55 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442173.1659738 (Exim 4.92) (envelope-from ) id 1xEPNs-00066A-2s; Wed, 07 Oct 2026 11:00:28 +0000 Received: by outflank-mailman (output) from mailman id 1442173.1659738; Wed, 07 Oct 2026 11:00:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNr-00065V-Uh; Wed, 07 Oct 2026 11:00:27 +0000 Received: by outflank-mailman (input) for mailman id 1442173; Wed, 07 Oct 2026 11:00:26 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNq-0005y8-EQ for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 11:00:26 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEPNq-001bwG-0K; Wed, 07 Oct 2026 11:00:26 +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.96) (envelope-from ) id 1xEP5B-001QnT-2O; Wed, 07 Oct 2026 10:41:09 +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=P7gSAZkpbEQZ016S+qOjogD467NTdt8ZgNSKB5Dcc3E=; b=UqZuNYEs3z/YdvEmir5hdRchl0 OrJXg/t8GcPNmh29depEDwhjTJa6JvMiKv/+nUQ6V4c3KV6dLZL8BeiHlqN6HyTFMZ68UkFI11pdt NZKTC5q+G0NtJgF4uGnNudhuW+nL27/TeaXhawt7fTju1LvjlQFXk3aHnKI1Yd3q428Y=; 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 v3 10/18] x86/domain_page: don't unmap a mapcache entry that is not present Date: Wed, 7 Oct 2026 11:40:43 +0100 Message-ID: <20261007104051.48841-11-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1791370857850158500 Content-Type: text/plain; charset="utf-8" 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 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 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index f91ec8a75a..708df90a00 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -230,6 +230,16 @@ void unmap_domain_page(const void *ptr) ASSERT(cache->inuse); =20 idx =3D PFN_DOWN(va - MAPCACHE_VIRT_START); + /* + * Mappings must be unmapped once, by the vCPU that created them. With + * nothing present there is nothing to tear down: leave the hash and t= he + * bitmaps alone rather than act on a stale or foreign pointer. + */ + 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:45:27 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=1791370855; cv=none; d=zohomail.com; s=zohoarc; b=TuEA6o+I9RHjpmYzFRjqqYSwHDSsjuFrVdepMR2Xh2NWfoDd7UvgAEeF9s7iD34vqwnqQ/t7FiOBUHW4jhZYBoTC62KSULSw5niD/i1p7JAqYdY9vFH3/8UL+vIAMNogW47OjJmoVZ3tnpH1vj22LWV87kYXCB5zzDmNMFBSfl8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791370855; 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=Y8xkRRzMKr9RKUFhQ6zYIhHgopPKevxFWSbNje8Fa5c=; b=IeP7l/qHz3lo7vOWqyTs4oOhW0xvK6iTqMqxVmgxhX/HSJNtjsyZ1hESEEWaHDsA+JQibwU98Uk8eQ2Zop+bWZpbuH1YQP4s+qlzIKuiL68m1SEcoXDXTNteiMgND5MJmse8CEsE0jVySPnZIyUxmc25xx8fQyo5C/OXQCp6HGA= 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 179137085542961.152872416886225; Wed, 7 Oct 2026 04:00:55 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442176.1659757 (Exim 4.92) (envelope-from ) id 1xEPNt-0006P1-3h; Wed, 07 Oct 2026 11:00:29 +0000 Received: by outflank-mailman (output) from mailman id 1442176.1659757; Wed, 07 Oct 2026 11:00: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 1xEPNs-0006NK-RD; Wed, 07 Oct 2026 11:00:28 +0000 Received: by outflank-mailman (input) for mailman id 1442176; Wed, 07 Oct 2026 11:00:26 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNq-0005y5-GJ for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 11:00:26 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEPNq-001bwI-0R; Wed, 07 Oct 2026 11:00:26 +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.96) (envelope-from ) id 1xEP5C-001QnT-2W; Wed, 07 Oct 2026 10:41:10 +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=Y8xkRRzMKr9RKUFhQ6zYIhHgopPKevxFWSbNje8Fa5c=; b=cKUxmqfSVUd0AHjkiUk36XseHk a5Hz9xRk0KRR/fcRIni1RYSi2yLsvgL6hQglGSmv7B+clNODYkWiyRcHm5MJIJGxiZz50LnIk48Pm GU89Erit5zkZg8njuYF8AqFCwmxlwIE8HjNSCsDrukapMWhEuNi3zJCdXDjgJi3WEv5E=; 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 v3 11/18] x86/hvm: introduce per-vCPU mapcache for vCPU-PT domains Date: Wed, 7 Oct 2026 11:40:44 +0100 Message-ID: <20261007104051.48841-12-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@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: 1791370857909158500 From: Roger Pau Monn=C3=A9 Add a struct mapcache to struct mapcache_vcpu for the per-vCPU accounting. Since MAPCACHE_VCPU_ENTRIES is 16, its inuse and garbage bitmaps are a word 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_VCPU_ENTRIES (16) 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()". 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 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 | 70 ++++++++++++++++++++++--------- xen/arch/x86/include/asm/domain.h | 6 +++ 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 708df90a00..0500bb9dba 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -20,12 +20,13 @@ =20 /* * Whether v has a mapcache: PV vCPUs (whose domain-wide mapcache may stil= l be - * disabled, see mapcache_domain_init()). HVM vCPUs reach everything thro= ugh - * the directmap, which covers all physical address space for them. + * disabled, see mapcache_domain_init()) and the vCPUs of an HVM domain us= ing + * per-vCPU page-tables. Other HVM vCPUs reach everything through the + * directmap, which covers all physical address space for them. */ static bool has_mapcache(const struct vcpu *v) { - return is_pv_vcpu(v); + return is_pv_vcpu(v) || v->domain->arch.vcpu_pt; } =20 static inline struct vcpu *mapcache_current_vcpu(void) @@ -38,10 +39,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 an + * invalidate any in-use mapcache entries. */ if ( unlikely(this_cpu(curr_vcpu) !=3D curr) ) { @@ -61,14 +62,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.vcpu_pt ) + { + *dcache =3D NULL; + return &v->arch.mapcache.cache; + } + *dcache =3D &d->arch.pv.mapcache; return &(*dcache)->cache; } @@ -77,11 +85,15 @@ static struct mapcache *vcpu_mapcache(struct vcpu *v, * The domain-wide mapcache's lock and TLB-epoch machinery, taken around * allocating a slot: the domain's vCPUs share the mapcache's slots, and e= ach * catches up with a wrap another one caused, flushing its TLB if it has n= ot - * been flushed since. + * been flushed since. A per-vCPU mapcache (no dcache) needs neither: onl= y its + * own vCPU, and so one pCPU at a time, uses it. */ static void mapcache_lock(struct mapcache_domain *dcache, struct mapcache_vcpu *vcache) { + if ( !dcache ) + return; + spin_lock(&dcache->lock); =20 /* Has some other CPU caused a wrap? We must flush if so. */ @@ -100,13 +112,17 @@ static void mapcache_lock(struct mapcache_domain *dca= che, static void mapcache_new_epoch(struct mapcache_domain *dcache, struct mapcache_vcpu *vcache) { + if ( !dcache ) + return; + vcache->shadow_epoch =3D ++dcache->epoch; dcache->tlbflush_timestamp =3D tlbflush_current_time(); } =20 static void mapcache_unlock(struct mapcache_domain *dcache) { - spin_unlock(&dcache->lock); + if ( dcache ) + spin_unlock(&dcache->lock); } =20 #define mapcache_l2_entry(e) ((e) >> PAGETABLE_ORDER) @@ -191,7 +207,12 @@ void *map_domain_page(mfn_t mfn) } BUG_ON(idx >=3D cache->entries); =20 - /* /Second/, flush TLBs. */ + /* + * /Second/, flush TLBs. A per-vCPU mapcache needs a local flush + * only: its entries are never _PAGE_GLOBAL, and this vCPU's + * page-tables can only have been loaded on another pCPU via + * switch_cr3_cr4(), which flushes non-global mappings. + */ perfc_incr(domain_page_tlb_flush); flush_tlb_local(); mapcache_new_epoch(dcache, vcache); @@ -313,11 +334,19 @@ int mapcache_vcpu_init(struct vcpu *v) return 0; =20 cache =3D vcpu_mapcache(v, &dcache); - if ( !cache->inuse ) + if ( !dcache ) + { + /* Per-vCPU bitmaps are a word each: they live in the vCPU itself.= */ + cache->inuse =3D v->arch.mapcache.inuse; + cache->garbage =3D v->arch.mapcache.garbage; + ents =3D MAPCACHE_VCPU_ENTRIES; + } + else if ( !cache->inuse ) /* Domain-wide mapcache disabled (fully direct-mapped build). */ return 0; + else + ents =3D d->max_vcpus * MAPCACHE_VCPU_ENTRIES; =20 - ents =3D d->max_vcpus * MAPCACHE_VCPU_ENTRIES; nr =3D PFN_UP(BITS_TO_LONGS(ents) * sizeof(long)); =20 if ( ents > cache->entries ) @@ -326,13 +355,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/domain.h b/xen/arch/x86/include/asm/d= omain.h index cfa24c4e75..80415ab2c2 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_VCPU_ENTRIES); + DECLARE_BITMAP(garbage, MAPCACHE_VCPU_ENTRIES); }; =20 struct mapcache_domain { --=20 2.55.0 From nobody Mon Nov 23 22:45:27 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=1791370852; cv=none; d=zohomail.com; s=zohoarc; b=FRA7Nhztjoqxsuwb29Q1SAjGAD8WZVN7wsT4567wueWZWLz2HzWMyHi6ArEpeHcfTOGhV6yJVL2qH3jJ3OObyb7lTZCHG4nNJfjLLzg4HjwXzS1L1oVj+0qfdCiQjc2o8QrdDcB+rkTzpCUKPQ2+jyxXFBqUbKrU2l1fmsg3iFI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791370852; 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=s8gKbLWFi6wgE9b8CopwbaynqyW9W4JENY9bvZeWPyQ=; b=W/5hblbzDivtZpoJM8yWk93hKrcNIgG0Br5tVxcCUxcTQGG3QtYzJL+XUN6q7um/wxLgK4IUHUjiio3fF7KceYB3Ba3AqUY4gutOu2Ios7m4Z2Y7xfYjQ7zRtwg4jMvPmUlUQ5d8yHOgJuzB+TCxgPEMZZAv39u2YGsGlUUul+w= 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 1791370852559360.2467879234347; Wed, 7 Oct 2026 04:00:52 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442172.1659733 (Exim 4.92) (envelope-from ) id 1xEPNr-00061S-R7; Wed, 07 Oct 2026 11:00:27 +0000 Received: by outflank-mailman (output) from mailman id 1442172.1659733; Wed, 07 Oct 2026 11:00:27 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNr-00060h-NC; Wed, 07 Oct 2026 11:00:27 +0000 Received: by outflank-mailman (input) for mailman id 1442172; Wed, 07 Oct 2026 11:00:26 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNq-0005y7-DU for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 11:00:26 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEPNq-001bwS-0k; Wed, 07 Oct 2026 11:00:26 +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.96) (envelope-from ) id 1xEP5D-001QnT-2H; Wed, 07 Oct 2026 10:41:11 +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=s8gKbLWFi6wgE9b8CopwbaynqyW9W4JENY9bvZeWPyQ=; b=RTJdZXiZ9hOcVUqeIYc0rzDHem KQEr6odiA8Vib09s/JCjysCQbx64bRWAttC2MnHSmlc88ttE/AMeAuvsIE0ZjTB4UDMIwGXBe9NYK sU1kymKGSoxusls3hoNU0qAaY83+5HX8cx7oASKlMn+2PFkUDmepItVd6NeNSe1c0B50=; 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 v3 12/18] x86/mm: let Xen's page-table walker operate on a given root Date: Wed, 7 Oct 2026 11:40:45 +0100 Message-ID: <20261007104051.48841-13-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1791370854172158500 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 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: it is by virtual address, and the same addresses are mapped in either hierarchy. No functional change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- 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 7258935528..94b96c3d9b 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -5201,13 +5201,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; @@ -5223,7 +5224,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 ) @@ -5234,11 +5236,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 @@ -5272,11 +5274,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 @@ -5352,7 +5354,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; @@ -5392,7 +5394,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, @@ -5443,7 +5450,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 @@ -5567,7 +5574,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 @@ -5612,7 +5619,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; } @@ -5797,6 +5804,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); @@ -5813,8 +5829,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; @@ -5841,7 +5861,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 @@ -6092,6 +6112,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:45:27 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=1791370879; cv=none; d=zohomail.com; s=zohoarc; b=CUGvo7qElJGP4oEksJ2xF3Lqw03cycWpDptqu3hS8XGa11Q0TvxwvA0B7a35lGK5NkB/gGbwKlEia/+BaOQmHyc2W9vsyww8Ip1HCKB5Fv4ExqOMrl1+ccj9yC3DDrpoKjue6vROSqoh6fewO1A31M8ntyRG7G+h4uFt73PZhfk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791370879; 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=n89simzdmROfxUVTPCbuqHlbqGiH8SNCmEeAKkxXiY8=; b=D0Y19SBvxr/bLfeSu22kLHZ8vxIAFCwWFtGFGsuOGa1ZKJvNRSiYtulqukB45BjneUD7bMJWgQn1rjFJq3/B8KGcFVdTmrWo5+KgCIFlII4Ok7aYasuBry33KDS9VGAoug8XcheT6f4kmiZpcl7kHlllboBMrHy8owrThnA4AM8= 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 1791370879920820.0349917995833; Wed, 7 Oct 2026 04:01:19 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442179.1659780 (Exim 4.92) (envelope-from ) id 1xEPNu-0006uL-MO; Wed, 07 Oct 2026 11:00:30 +0000 Received: by outflank-mailman (output) from mailman id 1442179.1659780; Wed, 07 Oct 2026 11:00:30 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNu-0006qV-7Q; Wed, 07 Oct 2026 11:00:30 +0000 Received: by outflank-mailman (input) for mailman id 1442179; Wed, 07 Oct 2026 11:00:27 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNr-0005yp-8T for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 11:00:27 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEPNp-001bw8-2z; Wed, 07 Oct 2026 11:00:25 +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.96) (envelope-from ) id 1xEP5F-001QnT-08; Wed, 07 Oct 2026 10:41:13 +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=n89simzdmROfxUVTPCbuqHlbqGiH8SNCmEeAKkxXiY8=; b=cx9kdRn6GVCDclNUoGjK+lRlMa xwFzR9Jwf8sOsBeBE3BgO5xZnuEyOjcT69S7Hne2/DGqWF47LKOqvOrRrjbREp+8ibTAxowh7hqTf vjYzT5JJ6K+umk8QmwArrYN2nGDt3tJb5i8ntzmASJwKde6MfU8gM8StGV+1dPMTM3+U=; 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 v3 13/18] x86/mm: build and maintain a sparse view of the directmap Date: Wed, 7 Oct 2026 11:40:46 +0100 Message-ID: <20261007104051.48841-14-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1791370882123158500 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, dom0 construction 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 (NUMA node heap metadata). 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 (the same reasoning as in "xen/page_alloc: Add a path for xenheap when there is no direct map" of the directmap removal series); - memory handed to the heap later (boot modules, .init, memory hot-add) 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) or hot-added memory about to be handed to the heap, which the second rule then takes out again. 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. 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 guest 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 v3: - New in this version. --- xen/arch/x86/Kconfig | 1 + xen/arch/x86/Makefile | 1 + xen/arch/x86/include/asm/mm.h | 17 +- xen/arch/x86/include/asm/sparse-directmap.h | 63 +++ xen/arch/x86/mm.c | 128 +++++- xen/arch/x86/pv/dom0_build.c | 4 + xen/arch/x86/setup.c | 4 + xen/arch/x86/sparse-directmap.c | 424 ++++++++++++++++++++ xen/common/Kconfig | 18 + xen/common/page_alloc.c | 41 +- xen/include/xen/mm.h | 35 ++ 11 files changed, 722 insertions(+), 14 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 8a205c3d27..22b3c8bf6b 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 87131dd4bf..0d1e319c09 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 /* @@ -595,6 +596,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. */ @@ -655,12 +665,17 @@ 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, as seen = from + * every context: with a sparse directmap view configured (see + * asm/sparse-directmap.h), no range is, except through a xenheap allocati= on. */ 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..48c1f3c951 --- /dev/null +++ b/xen/arch/x86/include/asm/sparse-directmap.h @@ -0,0 +1,63 @@ +/* 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) + +extern bool opt_sparse_dmap_pv, opt_sparse_dmap_hvm; + +/* 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; +} + +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 94b96c3d9b..4a1bd9d146 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -127,6 +127,7 @@ #include #include #include +#include #include #include #include @@ -5394,11 +5395,104 @@ 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. idle_pg_table's hierarchy has pages from the boot allocator, + * which the sparse directmap view maps along with every other boot-time + * allocation: such a page leaves the sparse view before it reaches the + * heap, and is kept if it cannot. That takes the sparse view's L3 lock + * with an L3 lock of idle_pg_table's hierarchy held, the only order in + * which the two nest. The sparse view's own page-tables come from the + * heap, which it does not map, and are freed directly, as the caller may + * hold the sparse view's L3 lock; so are tables freed straight after being + * allocated, having lost a race to be installed. + */ +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, @@ -5503,10 +5597,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 @@ -5604,7 +5698,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 @@ -5736,7 +5830,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); @@ -5783,7 +5877,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); @@ -5810,7 +5904,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) @@ -5833,8 +5932,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; @@ -6044,7 +6143,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); @@ -6088,7 +6187,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); @@ -6114,7 +6213,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 bf980eba8c..f0a1197e62 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) @@ -651,6 +652,9 @@ static int __init dom0_construct(const struct boot_doma= in *bd) while ( count-- ) if ( assign_pages(mfn_to_page(_mfn(mfn++)), 1, 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 a636e3205c..e52388ad28 100644 --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -54,6 +54,7 @@ #include #include #include +#include #include #include =20 @@ -1938,6 +1939,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..1eb092c3ea --- /dev/null +++ b/xen/arch/x86/sparse-directmap.c @@ -0,0 +1,424 @@ +/* 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, which is where everything Xen reaches through the directmap lives), + * mapping only what Xen needs to reach through the directmap at all times. + * + * Memory in the full directmap can be classified into three groups: + * + * - Memory never handed to the heap. This includes Xen's image, + * boot-time allocations, and firmware tables. Mapped in the sparse + * view. + * - Memory handed to the heap, and not allocated to the xenheap. Not + * mapped in the sparse view: guest memory will come from here. + * - Memory handed to the heap, then allocated to the xenheap. Mapped + * in the sparse view. + * + * Guest contexts selected on the command line run on root page-tables + * carrying the sparse view's L3s in the directmap slots. + * + * Conceptually, the sparse view starts as a copy of the full directmap + * (idle_pg_table's hierarchy), and then: + * - Removes memory when it is handed to the heap. + * - Adds pages back when they are allocated as xenheap pages, and removes + * them again when they are freed. + * - Mirrors changes to the full directmap (map_pages_to_xen(), + * modify_xen_mappings()). + * - Removes memory handed to a domain without passing through the heap + * (a PV dom0's initrd, used in place), and page-tables the boot + * allocator provided when idle_pg_table's hierarchy frees them. + * + * Every virtual address mapping in the sparse view should be either + * identical to the corresponding full view mapping, or empty. + * + * The sparse view is built once the boot allocator has handed its + * memory to the heap (sparse_dmap_init()). Until then, the hooks record + * the ranges handed to the heap and the xenheap allocations made from + * them, which sparse_dmap_init() leaves out and puts back. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +bool __ro_after_init opt_sparse_dmap_pv; +bool __ro_after_init opt_sparse_dmap_hvm; + +/* 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 meanwhile (heap metadat= a of + * NUMA nodes), which it puts back. + */ +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 va =3D (unsigned long)mfn_to_virt(mfn); + unsigned long end =3D va + (nr << PAGE_SHIFT); + + while ( va < end ) + { + pte_attr_t flags; + unsigned long n; + mfn_t m =3D xen_mapping_lookup(idle_pg_table, va, &flags, &n); + int rc; + + n =3D min(n, (end - va) >> PAGE_SHIFT); + if ( !mfn_eq(m, INVALID_MFN) && + (rc =3D map_pages_in(root, va, m, 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: they + * must not bring along what a previous owner left in them, such as a dying + * domain's data still awaiting the scrubber. Pages needing scrubbing are + * scrubbed as they are allocated then; clean ones need nothing. + */ +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 m =3D xen_mapping_lookup(sparse_dmap_root, va, &flags, &n); + int rc; + + n =3D min(n, (e - va) >> PAGE_SHIFT); + if ( !mfn_eq(m, 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; + + return l->s < r->s ? -1 : l->s > r->s; +} + +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, j; + + 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 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, so the cursor into the sorted (and disjoint) heap + * ranges only moves forward. + */ + for ( va =3D SPARSE_DMAP_START, j =3D 0; va < SPARSE_DMAP_END; ) + { + pte_attr_t flags; + unsigned long n, s, e; + mfn_t m =3D xen_mapping_lookup(idle_pg_table, va, &flags, &n); + + n =3D min(n, (SPARSE_DMAP_END - va) >> PAGE_SHIFT); + if ( mfn_eq(m, INVALID_MFN) ) + { + va +=3D n << PAGE_SHIFT; + continue; + } + + for ( s =3D mfn_x(m), e =3D s + n; s < e; ) + { + unsigned long upto =3D e; + + while ( j < nr_early_heap && early_heap[j].e <=3D s ) + j++; + if ( j < nr_early_heap && early_heap[j].s <=3D s ) + { + s =3D min(e, early_heap[j].e); + continue; + } + if ( j < nr_early_heap ) + upto =3D min(e, early_heap[j].s); + + if ( map_pages_in(sparse_l4, va + ((s - mfn_x(m)) << PAGE_SHIF= T), + _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; + } + + sparse_dmap_root =3D sparse_l4; + + printk(XENLOG_INFO "Sparse directmap: %lu pages mapped\n", mapped); +} 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:45:27 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=1791370857; cv=none; d=zohomail.com; s=zohoarc; b=Smd2RFzDmaCDEIMU9fQ1mhzPy47qUlt7or4jM3NHRPM3/a4IAfhk0d3ulWEUMyQxrd3aFr0Eki/WVc5JbhQB7RQzhI0reuIoKUCq7sey8U/yqoya7C3LBLLAu5wB+MTvuqsyxQtjoI/Pmp8sIpsqEl+mQHsRajzbUtov3VRrSQw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791370857; 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=TM6vht0L2x66h0TuR6VE5MmWXE909ZBf8rVHG8TYJUE=; b=ifRPOGBO9crl/u5d97mr3/48U5Tj8N2ot4wapg8ZPpOBnZ/h8PN8wTxFy03QCVrwBCLoKp9Az8ddHAsAeskx2rZ8Ybtae6vy39oIF4mSfxrCVjZfgowdfSzaf14TOZjRL6CYHNPyFB16v7dEH2Nkhz+lPvDji1nfI7OX7wtPJxo= 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 1791370857809879.4658050230727; Wed, 7 Oct 2026 04:00:57 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442171.1659728 (Exim 4.92) (envelope-from ) id 1xEPNr-0005z2-Kr; Wed, 07 Oct 2026 11:00:27 +0000 Received: by outflank-mailman (output) from mailman id 1442171.1659728; Wed, 07 Oct 2026 11:00:27 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNr-0005yv-I1; Wed, 07 Oct 2026 11:00:27 +0000 Received: by outflank-mailman (input) for mailman id 1442171; Wed, 07 Oct 2026 11:00:26 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNq-0005y4-CP for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 11:00:26 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEPNq-001bwE-0E; Wed, 07 Oct 2026 11:00:26 +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.96) (envelope-from ) id 1xEP5F-001QnT-3A; Wed, 07 Oct 2026 10:41:14 +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=TM6vht0L2x66h0TuR6VE5MmWXE909ZBf8rVHG8TYJUE=; b=WOyYOzYsAKYp3mrMUOmh1+fLW6 mX0fITo87iQr26jqqSPOH2gVRr4e8mf17UerDfMSQAP9TKX8LdR6sjnAWT5Txoz9PgzSNRWp8ugxd uZ2asj2H3zjdeewbgiqWxHmTCbMNKEZSVnUgDtRfW0lHMM/BzLX8wdr2NWGK1ZlqZGNo=; 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 v3 14/18] x86/domain_page: key the directmap fast paths on the loaded view Date: Wed, 7 Oct 2026 11:40:47 +0100 Message-ID: <20261007104051.48841-15-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1791370859841158501 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. mapcache_current_vcpu() may complete the lazy context switch (sync_local_execstate()) and switch us onto the idle vCPU (which has a full view), so check again if we can use the directmap afterwards. 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.sparse_dmap and sparse_dmap_active for this; nothing sets them yet. No functional change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v3: - New in this version. --- xen/arch/x86/domain_page.c | 44 +++++++++++++++++++-- xen/arch/x86/include/asm/domain.h | 6 +++ xen/arch/x86/include/asm/sparse-directmap.h | 8 ++++ xen/arch/x86/sparse-directmap.c | 1 + 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 0500bb9dba..48f85b670d 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -61,6 +61,16 @@ static inline struct vcpu *mapcache_current_vcpu(void) return ACCESS_ONCE(this_cpu(pgtable_vcpu)); } =20 +/* + * Whether the directmap in @v's page-tables is the sparse view, which maps + * none of the heap's memory beyond the xenheap: the mapcache must serve + * every mapping then. + */ +static bool on_sparse_dmap(const struct vcpu *v) +{ + return sparse_dmap_active && v->domain->arch.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 @@ -142,17 +152,39 @@ 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. Don't complete a lazy context switch to + * find out: it would only move this CPU to the idle vCPU's + * page-tables, which map @mfn too. + */ + 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)); =20 +#ifdef NDEBUG + /* The lookup may have completed a lazy switch off the sparse view. */ + if ( !on_sparse_dmap(v) && + mfn_x(mfn) <=3D PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) ) + return mfn_to_virt(mfn_x(mfn)); +#endif + 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 @@ -307,7 +339,9 @@ 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.sparse_dmap && !mem_hotplug && + max_page <=3D PFN_DOWN(__pa(HYPERVISOR_VIRT_END - 1)) ) return; #endif =20 @@ -392,8 +426,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 80415ab2c2..76a22b4040 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -499,6 +499,12 @@ struct arch_domain */ bool vcpu_pt; =20 + /* + * Root page-tables carry the sparse view of the directmap, once it is + * active (see asm/sparse-directmap.h). + */ + bool sparse_dmap; + /* Emulated devices enabled bitmap. */ uint32_t emulation_flags; } __cacheline_aligned; diff --git a/xen/arch/x86/include/asm/sparse-directmap.h b/xen/arch/x86/inc= lude/asm/sparse-directmap.h index 48c1f3c951..512f8064d6 100644 --- a/xen/arch/x86/include/asm/sparse-directmap.h +++ b/xen/arch/x86/include/asm/sparse-directmap.h @@ -22,6 +22,12 @@ extern bool opt_sparse_dmap_pv, opt_sparse_dmap_hvm; /* Root of the sparse view, once built (NULL before, or if not configured)= . */ extern l4_pgentry_t *sparse_dmap_root; =20 +/* + * Set once guest contexts run on the sparse view: from then on, the root + * page-tables of domains with d->arch.sparse_dmap carry it. + */ +extern bool sparse_dmap_active; + static inline bool sparse_dmap_configured(void) { return opt_sparse_dmap_pv || opt_sparse_dmap_hvm; @@ -37,6 +43,8 @@ int sparse_dmap_drop_pagetable(mfn_t mfn); =20 #else /* !CONFIG_SPARSE_DIRECTMAP */ =20 +#define sparse_dmap_active false + 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, diff --git a/xen/arch/x86/sparse-directmap.c b/xen/arch/x86/sparse-directma= p.c index 1eb092c3ea..a41731ccfa 100644 --- a/xen/arch/x86/sparse-directmap.c +++ b/xen/arch/x86/sparse-directmap.c @@ -57,6 +57,7 @@ bool __ro_after_init opt_sparse_dmap_hvm; /* 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; +bool __read_mostly sparse_dmap_active; =20 /* * Before the sparse view is built: the MFN ranges handed to the heap, whi= ch --=20 2.55.0 From nobody Mon Nov 23 22:45:27 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=1791370865; cv=none; d=zohomail.com; s=zohoarc; b=PIkCiCcDPWcxGucQ0mEHK6jrSLkK5ADcP4Ge7gBlo4EDvgz9KKek1zZGbA0P0ATSVxwRULqlKRTO/2IgHmMjH5/jLBTJKrmkAK2GCj/kCkH4NfkHYZjk81lRo8M8FJLjj8HCst6FP5A0Yc3KJpMn6QMJ14+PFuEoc75WTIuHCeQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791370865; 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=lKK7xvuX4KaLB6q8iiI3K8qFh2zb9vOgIFdBlslvfS4=; b=OPoBE4+oqGpeOFHEVOO3bbI0fCC13XdsgXHCv3JBiOlrtwH2UsSHwzjywYhkNd3HIakMUPTHVDM4qpGZp5aeGziKhEzQOXdAPwKl50OZvcbrWXdxtYxfbwzux2TNiurJ8XFYoPUubuiHy9eCYLYtqQyCIs2vjfNnddJye+WZObU= 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 1791370865224618.9814543723666; Wed, 7 Oct 2026 04:01:05 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442177.1659764 (Exim 4.92) (envelope-from ) id 1xEPNt-0006ZL-LN; Wed, 07 Oct 2026 11:00:29 +0000 Received: by outflank-mailman (output) from mailman id 1442177.1659764; Wed, 07 Oct 2026 11:00: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 1xEPNt-0006XJ-Ad; Wed, 07 Oct 2026 11:00:29 +0000 Received: by outflank-mailman (input) for mailman id 1442177; Wed, 07 Oct 2026 11:00:26 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNq-0005yd-QZ for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 11:00:26 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEPNp-001bwA-3B; Wed, 07 Oct 2026 11:00:25 +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.96) (envelope-from ) id 1xEP5H-001QnT-13; Wed, 07 Oct 2026 10:41:15 +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=lKK7xvuX4KaLB6q8iiI3K8qFh2zb9vOgIFdBlslvfS4=; b=SbnMG71qmDmAShULXYcfZDKMO/ 7vaer8XkcruYMK9QQs0whBB9/ckI9o485iK2WNcUMFTGg93/lxcbg1aBssljhsgj2UYISaPVV/5NY xybQ0No0nYsvGRooTrlSDKRFw6CRiBJwrAykq4na0ANReJ2hxhm4XRV2jRgaRVqXkPQE=; 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 v3 15/18] x86/mm: flush owned pages before reuse with the sparse directmap view Date: Wed, 7 Oct 2026 11:40:48 +0100 Message-ID: <20261007104051.48841-16-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1791370868031158500 Content-Type: text/plain; charset="utf-8" Since d7bf1b8ea275 ("x86/mm: limit deferred TLB flushing to PV domain support") a page freed by its owner is flushed from TLBs before it is handed out again only in builds with PV support, on the premise that PV domains, with their (limited) control over the host MMU and over when flushes happen, are the only ones to leave stale entries behind. Xen's transient mappings of domain pages are another source. The mapcache zaps a slot when it is unmapped but defers the flush: the slot's translation can stay in the TLB of the CPU which used it until that CPU next flushes, by which time the page may have been freed and handed to another domain. Before "x86/hvm: introduce per-vCPU mapcache for vCPU-PT domains", only PV vCPUs use the mapcache, so the PV condition covers it. Now that the vCPUs of an HVM domain with per-vCPU page-tables have one as well. While the full directmap is loaded in every context, stale transient mappings only map what's already mapped in the full directmap. On the sparse view of the directmap, a stale translation of a page handed to another domain would keep it reachable from a context which has nothing to do with it. So flush owned pages before reuse wherever a sparse view is configured, as before d7bf1b8ea275. In builds without PV support, mark_page_free() now also asks a new hook, arch_freed_pages_need_tlbflush(), declared with the xenheap hooks the sparse view already uses. x86 answers with sparse_dmap_configured(); without CONFIG_SPARSE_DIRECTMAP, and on other architectures, the answer is no. Builds with PV support flush as before, and HVM-only builds keep d7bf1b8ea275's saving unless a sparse view is configured, which nothing does until a later patch. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- Changes in v3: - New in this version. --- xen/arch/x86/sparse-directmap.c | 14 ++++++++++++++ xen/common/page_alloc.c | 14 +++++++++----- xen/include/xen/mm.h | 6 ++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/xen/arch/x86/sparse-directmap.c b/xen/arch/x86/sparse-directma= p.c index a41731ccfa..ef13f9e95d 100644 --- a/xen/arch/x86/sparse-directmap.c +++ b/xen/arch/x86/sparse-directmap.c @@ -185,6 +185,20 @@ bool arch_xenheap_needs_scrub(void) return sparse_dmap_configured(); } =20 +/* + * Pages freed by their owner are flushed from TLBs before reuse once a + * sparse view is configured. Contexts on the sparse view reach domain-he= ap + * memory only through mappings made for the purpose, and the mapcache zap= s a + * transient mapping when it is dropped but defers the flush: a translation + * left behind would keep the page reachable after it has been handed to + * another owner. With PV support, owned pages are flushed before reuse + * anyway. + */ +bool arch_freed_pages_need_tlbflush(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) { diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 69e8e982b6..477301ef7c 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -1539,12 +1539,16 @@ static bool mark_page_free(struct page_info *pg, mf= n_t mfn) } =20 /* - * If a page has no owner and there's no PV domain support it does not= need - * a safety TLB flush. PV domains are the only domain types that can = keep - * stale entries on the TLB, as they have (limited) control over the h= ost - * MMU and when flushes are performed. + * If a page has no owner, or there's no PV domain support and the + * architecture doesn't ask for one, it does not need a safety TLB flu= sh. + * PV domains can keep stale entries on the TLB, as they have (limited) + * control over the host MMU and when flushes are performed. On x86, + * while a sparse directmap view is configured, so can Xen's transient + * mappings of the page, whose translations may outlive the mappings. */ - pg->u.free.need_tlbflush =3D IS_ENABLED(CONFIG_PV) && page_get_owner(p= g); + pg->u.free.need_tlbflush =3D page_get_owner(pg) && + (IS_ENABLED(CONFIG_PV) || + arch_freed_pages_need_tlbflush()); if ( pg->u.free.need_tlbflush ) page_set_tlbflush_timestamp(pg); =20 diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index 2bcc45cf58..1628f7a897 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -110,6 +110,8 @@ bool arch_xenheap_needs_scrub(void); 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); +/* Pages freed by their owner need a TLB flush before they are reused. */ +bool arch_freed_pages_need_tlbflush(void); #else static inline bool arch_heap_pages_init(mfn_t mfn, unsigned long nr) { @@ -127,6 +129,10 @@ static inline int arch_xenheap_pages_free(mfn_t mfn, u= nsigned int order) { return 0; } +static inline bool arch_freed_pages_need_tlbflush(void) +{ + return false; +} #endif =20 /* Free an allocation, and zero the pointer to it. */ --=20 2.55.0 From nobody Mon Nov 23 22:45:27 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=1791370871; cv=none; d=zohomail.com; s=zohoarc; b=OkDjc+ryXkOqe92r0jl5F2b6SnNJmNi0dZ3bYP0OSDL4XVPOCQSS0WfX4qer8lbK+i3GAJPSZy3ERwXP4sEkEsIKNN4AtxZNw+Ll7OECKsIckfXZl6WfHSC+MQeys3++I/P34Vgm/4mBS5gSm/l2uxOBblfzmXk8qSJa8clzgSs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791370871; 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=uTtFastgCN5QzSIxMSLM2WRMVTyMNwx08i8QJomPSZ4=; b=F8utcazX5/0X8/0fhZdqlLK2i2IwDn0nkRaBQu6xT8MtNQ3VeBMfpqcU2rUOAx8wM3mlpC1PAYzJlVVRpeaeHMp4lk/4gS6v2y7EqHNuXPiH4Pooe5xzow5Bh4v/85RTCVS2tp99wXisrJ6e8a9pQYlP10dAuw0lpNGLOl1LSBc= 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 1791370871144718.6892043522057; Wed, 7 Oct 2026 04:01:11 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442175.1659750 (Exim 4.92) (envelope-from ) id 1xEPNs-0006JX-NV; Wed, 07 Oct 2026 11:00:28 +0000 Received: by outflank-mailman (output) from mailman id 1442175.1659750; Wed, 07 Oct 2026 11:00:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNs-0006HI-IQ; Wed, 07 Oct 2026 11:00:28 +0000 Received: by outflank-mailman (input) for mailman id 1442175; Wed, 07 Oct 2026 11:00:26 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNq-0005y3-FD for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 11:00:26 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEPNq-001bwM-0X; Wed, 07 Oct 2026 11:00:26 +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.96) (envelope-from ) id 1xEP5I-001QnT-0o; Wed, 07 Oct 2026 10:41: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=uTtFastgCN5QzSIxMSLM2WRMVTyMNwx08i8QJomPSZ4=; b=E/3A8IsJsQ8CdM5jaS3m1u6wCc MAfj8CUS0mhwRfZtW0E/DI7wswq0NEUGyP/mGzmDs0gQa8MbsiBpzeVG4BJ4Fl4KTHd/KsRRYK2B7 OR+YgZwi/PQbLU07SpnvZIXi+fke+SaQGfI+nElTTto0z3tCI092ceDbBw/t7+hO93b8=; 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 v3 16/18] x86/domain_page: keep the maphash's slots, not their mappings Date: Wed, 7 Oct 2026 11:40:49 +0100 Message-ID: <20261007104051.48841-17-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1791370871971158500 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. A page freed meanwhile and handed to another domain stays mapped in the previous user's mapcache: in the vCPU's own area for a domain with per-vCPU page-tables, in the domain-wide area of a PV domain, where all of its vCPUs can reach it. 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, and a mapping kept in the maphash would leave another domain's page reachable from a context which has nothing to do with it. Keep the slot, not the mapping: unmapping the last reference zaps the PTE and keeps the hash entry, 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. A translation of the zapped PTE left in a TLB is covered, for owned pages, by the flush the page allocator arranges before a freed page is reused wherever a sparse view is configured ("x86/mm: flush owned pages before reuse with the sparse directmap view"). The cost is one PTE write per unmap into the maphash and per remap from it. 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: in debug builds for every page, in release builds for pages beyond the reach of the full directmap in their page-tables (5TiB, 3.5TiB with CONFIG_BIGMEM). For them the change adds the PTE writes above. It also 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 v3: - New in this version. --- xen/arch/x86/domain_page.c | 34 +++++++++++++++++++++++-------- xen/arch/x86/include/asm/domain.h | 5 ++++- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c index 48f85b670d..eee66b848c 100644 --- a/xen/arch/x86/domain_page.c +++ b/xen/arch/x86/domain_page.c @@ -195,6 +195,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)); @@ -227,8 +235,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; @@ -298,25 +306,35 @@ void unmap_domain_page(const void *ptr) =20 local_irq_save(flags); =20 + /* + * The maphash keeps recently freed slots for a quick remap of the same + * page, but not their mappings: the page may change hands meanwhile, = and + * must not stay reachable from this context then. A translation left= in + * a TLB is covered, for owned pages, by the flush the page allocator + * arranges before a freed page is handed out again wherever a sparse + * directmap view is configured; without one, the directmap maps the p= age + * anyway. + */ if ( hashent->idx =3D=3D idx ) { ASSERT(hashent->mfn =3D=3D mfn); ASSERT(hashent->refcnt); hashent->refcnt--; + if ( !hashent->refcnt ) + l1e_write(&MAPCACHE_L1ENT(idx), l1e_empty()); } 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. */ + l1e_write(&MAPCACHE_L1ENT(idx), l1e_empty()); hashent->mfn =3D mfn; hashent->idx =3D idx; } diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 76a22b4040..51741f9d42 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:45:27 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=1791370850; cv=none; d=zohomail.com; s=zohoarc; b=mVkQIaoXW5DCodB5w0izqJHQVAKYUMXlLj9vP2OrVqKmVAG7Te9hq+lapVN3z6VpbG1cS+UDk+Nix7rBzonsxb8RW4p3NYmeoJeM9z3iCJn3nrjLcldYS/BTBlyT9GccDadr7aP6nrtzinqxgNmqGrLJIlBvBwSbgEihVeCDkGM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791370850; 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=oxwTS9MOb2PIX3bMecdpJFmahOt3FYv9Hqo2e2XCx8U=; b=ILf9CQH0azoWmaandHOHmaYHp9xUpIxuzrWwJRmEX1skd4CAt5+equutfKV4ZZe50vvMhySP3+CLkHM4NCA2sLbCblz8+xliSxbtEfOBcTzJvdZc908rc4NxhCPUZAQnD6nS0O5l7hmCGjc6HMhLpB7XOgcvvbcPbqCIeiADrYI= 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 1791370850078449.3039861240634; Wed, 7 Oct 2026 04:00:50 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442174.1659744 (Exim 4.92) (envelope-from ) id 1xEPNs-0006BR-Ck; Wed, 07 Oct 2026 11:00:28 +0000 Received: by outflank-mailman (output) from mailman id 1442174.1659744; Wed, 07 Oct 2026 11:00:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNs-0006AJ-6J; Wed, 07 Oct 2026 11:00:28 +0000 Received: by outflank-mailman (input) for mailman id 1442174; Wed, 07 Oct 2026 11:00:26 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNq-0005y6-FB for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 11:00:26 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEPNq-001bwO-0d; Wed, 07 Oct 2026 11:00:26 +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.96) (envelope-from ) id 1xEP5J-001QnT-0V; Wed, 07 Oct 2026 10:41: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:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From; bh=oxwTS9MOb2PIX3bMecdpJFmahOt3FYv9Hqo2e2XCx8U=; b=oRHmf+2uFw+h+kuvyOl50xczl3 eS5WBPGkfTapjD4dO5FbUguoCgNPpmVty2iWkybo1VvOh9IaRXt7UVwvQ7/vgeQaKKxJqHOqeSEqB DAT5ldXrpxNsualX40jTSCVfQtLQzMIubjQ50hvMcVVZzN5eZyZkqvTVu7ZZ0ugHZJvg=; 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 v3 17/18] x86/mm: run guest contexts on the sparse directmap view Date: Wed, 7 Oct 2026 11:40:50 +0100 Message-ID: <20261007104051.48841-18-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1791370851748158500 Content-Type: text/plain; charset="utf-8" In init_xen_l4_slots(), if d->arch.sparse_dmap is set, point the directmap L3 at the sparse view. d->arch.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 becomes active once dom0 is built, before it first runs: dom0's construction runs on the full directmap, which it needs, and its root page-tables so far (a PV dom0's first L4s, a PVH dom0's monitor tables) are switched over then. A PV dom0 booted with dom0=3Dshadow is switched as well: shadow mode is enabled only later, from a tasklet, and its shadow L4s then get the sparse view from init_xen_l4_slots(), as for any PV domain entering shadow mode. Anything built later gets the sparse view from init_xen_l4_slots(). The sparse view's slots are marked NX at activation, as the full directmap's slots are. 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 v3: - New in this version. --- xen/arch/x86/include/asm/sparse-directmap.h | 11 ++ xen/arch/x86/mm.c | 9 +- xen/arch/x86/setup.c | 3 + xen/arch/x86/sparse-directmap.c | 129 ++++++++++++++++++++ xen/arch/x86/spec_ctrl.c | 7 ++ 5 files changed, 157 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/include/asm/sparse-directmap.h b/xen/arch/x86/inc= lude/asm/sparse-directmap.h index 512f8064d6..41866b77e9 100644 --- a/xen/arch/x86/include/asm/sparse-directmap.h +++ b/xen/arch/x86/include/asm/sparse-directmap.h @@ -33,7 +33,15 @@ static inline bool sparse_dmap_configured(void) return opt_sparse_dmap_pv || opt_sparse_dmap_hvm; } =20 +/* Whether a new domain of the given type is to use the sparse view. */ +static inline bool sparse_dmap_wanted(bool pv) +{ + return sparse_dmap_root && (pv ? opt_sparse_dmap_pv : opt_sparse_dmap_= hvm); +} + void sparse_dmap_init(void); +void sparse_dmap_activate(void); +void sparse_dmap_install(l4_pgentry_t *l4t, bool pv); 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, @@ -46,7 +54,10 @@ int sparse_dmap_drop_pagetable(mfn_t mfn); #define sparse_dmap_active false =20 static inline bool sparse_dmap_configured(void) { return false; } +static inline bool sparse_dmap_wanted(bool pv) { return false; } static inline void sparse_dmap_init(void) {} +static inline void sparse_dmap_activate(void) {} +static inline void sparse_dmap_install(l4_pgentry_t *l4t, bool pv) {} 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 4a1bd9d146..4e2f9e1f12 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -1662,7 +1662,8 @@ struct page_info *perdomain_l3(const struct vcpu *v) * share; it is mandatory for a vCPU-PT domain, whose per-domain area is * per-vCPU. All other parameters are optional and will either fill or ze= ro * the 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 (see sparse_dmap_install(= )). */ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, const struct domain *d, const struct vcpu *v, @@ -1670,7 +1671,7 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn, { /* * 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 @@ -1743,6 +1744,10 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mf= n, (ROOT_PAGETABLE_FIRST_XEN_SLOT + slots - l4_table_offset(XEN_VIRT_START)) * sizeof(*l4t)); } + + /* The domain's contexts may run on the sparse view of the directmap. = */ + if ( sparse_dmap_active && d->arch.sparse_dmap ) + sparse_dmap_install(l4t, short_directmap); } =20 bool fill_ro_mpt(mfn_t mfn) diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c index e52388ad28..21edab9cea 100644 --- a/xen/arch/x86/setup.c +++ b/xen/arch/x86/setup.c @@ -2216,6 +2216,9 @@ void asmlinkage __init noreturn __start_xen(void) if ( !dom0 ) panic("Could not set up DOM0 guest OS\n"); =20 + /* Before dom0 first runs, and after it was built on the full directma= p. */ + sparse_dmap_activate(); + heap_init_late(); =20 init_constructors(); diff --git a/xen/arch/x86/sparse-directmap.c b/xen/arch/x86/sparse-directma= p.c index ef13f9e95d..54a6e36132 100644 --- a/xen/arch/x86/sparse-directmap.c +++ b/xen/arch/x86/sparse-directmap.c @@ -46,8 +46,10 @@ #include #include #include +#include #include =20 +#include #include #include =20 @@ -437,3 +439,130 @@ void __init sparse_dmap_init(void) =20 printk(XENLOG_INFO "Sparse directmap: %lu pages mapped\n", mapped); } + +/* + * Point the directmap slots of the root page-table @l4t at the sparse vie= w. + * Every slot it 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. + * + * Directmap mappings may be global, in either hierarchy. Translations of + * the full directmap must not remain usable on a root carrying the sparse + * view: every CR3 write which can move a CPU from the one to the other go= es + * through switch_cr3_cr4(), which flushes what the old root left in the T= LB, + * global entries included, before it returns. The other CR3 writes, which + * may keep global or PCID-tagged entries (_toggle_guest_pt(), XPTI's entry + * and exit paths), switch between one vCPU's roots, which carry the same + * directmap, or to XPTI's per-CPU root, which maps only this CPU's stack, + * IDT and TSS there. Removals from the sparse view flush all CPUs, global + * entries included. + */ +void sparse_dmap_install(l4_pgentry_t *l4t, bool pv) +{ + unsigned int i; + + ASSERT(sparse_dmap_root); + + for ( i =3D l4_table_offset(SPARSE_DMAP_START); + i < l4_table_offset(SPARSE_DMAP_END); i++ ) + l4t[i] =3D sparse_dmap_root[i]; + + if ( !pv ) + for ( ; i < l4_table_offset(DIRECTMAP_VIRT_END); i++ ) + l4t[i] =3D l4e_empty(); +} + +static void __init sparse_dmap_install_mfn(mfn_t mfn, bool pv) +{ + l4_pgentry_t *l4t =3D map_domain_page(mfn); + + sparse_dmap_install(l4t, pv); + unmap_domain_page(l4t); +} + +/* + * Let guest contexts run on the sparse view. Root page-tables built from + * here on carry it (init_xen_l4_slots()); those of the domains built so f= ar + * (dom0, which has not run yet) are switched over here. Its construction= ran + * on the full directmap, and needed to. + */ +void __init sparse_dmap_activate(void) +{ + struct domain *d; + struct vcpu *v; + unsigned long va, mapped =3D 0; + unsigned int i, switched =3D 0; + + if ( !sparse_dmap_root ) + return; + + /* + * Nothing in the directmap is executed: as subarch_init_memory() does + * for the full directmap, mark the sparse view's slots NX, before any + * root page-table takes a copy of them. + */ + 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_dmap_root[i], _PAGE_NX_BIT); + + rcu_read_lock(&domlist_read_lock); + + for_each_domain ( d ) + { + if ( !d->arch.sparse_dmap ) + continue; + + /* + * A PV domain runs on shadow L4s only in shadow mode, which boot + * does not enter before this point: dom0=3Dshadow enables it late= r, + * from a tasklet, and the shadow L4s then get the sparse view from + * init_xen_l4_slots(), as for any PV domain entering shadow mode. + */ + switched++; + for_each_vcpu ( d, v ) + { + if ( is_hvm_vcpu(v) ) + { + if ( !pagetable_is_null(v->arch.hvm.monitor_table) ) + sparse_dmap_install_mfn( + pagetable_get_mfn(v->arch.hvm.monitor_table), fals= e); + continue; + } + + if ( !pagetable_is_null(v->arch.guest_table) ) + sparse_dmap_install_mfn( + pagetable_get_mfn(v->arch.guest_table), true); + if ( !pagetable_is_null(v->arch.guest_table_user) ) + sparse_dmap_install_mfn( + pagetable_get_mfn(v->arch.guest_table_user), true); + } + } + + rcu_read_unlock(&domlist_read_lock); + + /* + * Belt and braces: nothing runs on these page-tables yet, and PV dom0= 's + * construction, which ran on its own, returned to the idle page-tables + * through switch_cr3_cr4(), dropping what it left in the TLB. + */ + flush_all(FLUSH_TLB_GLOBAL); + + sparse_dmap_active =3D true; + + for ( va =3D SPARSE_DMAP_START; va < SPARSE_DMAP_END; ) + { + pte_attr_t flags; + unsigned long n; + + if ( !mfn_eq(xen_mapping_lookup(sparse_dmap_root, va, &flags, &n), + INVALID_MFN) ) + mapped +=3D n; + va +=3D n << PAGE_SHIFT; + } + + printk(XENLOG_INFO + "Sparse directmap: active, %lu pages mapped, %u domain(s) on it= \n", + mapped, switched); +} diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index 999e41aefa..46105ef185 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -1940,6 +1940,13 @@ void spec_ctrl_init_domain(struct domain *d) =20 d->arch.vcpu_pt =3D !pv && (is_hardware_domain(d) ? opt_vcpu_pt_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.sparse_dmap =3D pv ? sparse_dmap_wanted(true) + : d->arch.vcpu_pt && sparse_dmap_wanted(false= ); } =20 void __init init_speculation_mitigations(void) --=20 2.55.0 From nobody Mon Nov 23 22:45:27 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=1791370847; cv=none; d=zohomail.com; s=zohoarc; b=YdqOE2rdzKq7shGBjettoO0C3nFmZobmQUQzidhjZWtOVMR6D/aJ/SMWNVQGmEeBr/ijjnF3pjInStBdqGfPnJGoMpw4ryQuJyJVZ+N3ywNQ83JL2agRr/vo4heu23ryiu8AJxJoELhDYYWeYebFY9aEq0sGh/AVVo6qIYKS79I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791370847; 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=rWZqp3rjAtDLkijlzZwCExLStfGu/Llce80YZdzxLFY=; b=g93BzKTPb67k/H6oZD+E/myuOUf8pPAwpu58b+AgClbJUOmswrrY3oR9BdbEY/fT/yUuNH1OCMJOn0e+XmxRABJFP7VFNvT8oj7hBrA5AyQmVan8+W8pY4XnR7R/elgDrTcpFTPF2AP7lV9Kb7cyXIT+9gPrED26BeauzXZN4kk= 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 1791370847963690.9851429248503; Wed, 7 Oct 2026 04:00:47 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1442178.1659773 (Exim 4.92) (envelope-from ) id 1xEPNu-0006iG-42; Wed, 07 Oct 2026 11:00:30 +0000 Received: by outflank-mailman (output) from mailman id 1442178.1659773; Wed, 07 Oct 2026 11:00:30 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNt-0006eF-O9; Wed, 07 Oct 2026 11:00:29 +0000 Received: by outflank-mailman (input) for mailman id 1442178; Wed, 07 Oct 2026 11:00:26 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xEPNq-0005yi-Sz for xen-devel@lists.xenproject.org; Wed, 07 Oct 2026 11:00:26 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.96) (envelope-from ) id 1xEPNq-001bwC-07; Wed, 07 Oct 2026 11:00:26 +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.96) (envelope-from ) id 1xEP5K-001QnT-1b; Wed, 07 Oct 2026 10:41:18 +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=rWZqp3rjAtDLkijlzZwCExLStfGu/Llce80YZdzxLFY=; b=i6PnpxQ0OgUkwF639pPSmIortD 8SrC4BOQZcV4rxOqROAjq/XF9XHFXydG5Dnsi1Oln0j1LW8qc0pceXrqU3JgyUouL7zUuWtzPVJIr w8FH0oqmVJ8ZhGTsgUsk0mybYa8s/98OKNujGCB6wVwPZa7SW0nzEho4jzW8b6ZEwJ7E=; 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 v3 18/18] x86/spec-ctrl: wire the sparse directmap view into the asi= option Date: Wed, 7 Oct 2026 11:40:51 +0100 Message-ID: <20261007104051.48841-19-gwd@xenproject.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261007104051.48841-1-gwd@xenproject.org> References: <20261007104051.48841-1-gwd@xenproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1791370849894158500 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: HVM and PV guests, and the hardware domain, PV or PVH. HVM guests, a PVH hardware domain included, use the sparse view together with vCPU-PT, which gives them the mapcache it needs; PV guests all have one. A PV guest 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`, and new `asi=3Dpv=3D` and `asi=3Ddom0=3D`, do the same for HVM or PV guests 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 ignored with a warning. A guest type the build lacks is ignored, so that the sparse view is not built for nothing. * Only one of XPTI and the sparse view are enabled. 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. 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 guests 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 v3: - New in this version. --- docs/misc/xen-command-line.pandoc | 35 +++++- xen/arch/x86/include/asm/sparse-directmap.h | 28 ++++- xen/arch/x86/sparse-directmap.c | 4 +- xen/arch/x86/spec_ctrl.c | 130 ++++++++++++++++++-- 4 files changed, 171 insertions(+), 26 deletions(-) diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line= .pandoc index 785e099c0a..0bbc8ebeff 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -203,7 +203,8 @@ 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 [ , hvm=3D, vcpu-pt=3D | vcpu-pt=3Dhvm=3D= ]` +> `=3D List of [ , {pv,hvm,dom0}=3D, directmap=3D, +> vcpu-pt=3D | vcpu-pt=3Dhvm=3D ]` =20 > Default: `false` =20 @@ -214,12 +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 guests, including a PVH -hardware domain. PV guests, including a PV hardware domain, are unaffecte= d. +The mechanisms currently implemented are `vcpu-pt`, for HVM guests includi= ng +a PVH hardware domain, and `directmap`, for all guests including the hardw= are +domain, PV or PVH. The plain boolean enables or disables both, wherever t= hey +apply. =20 -* `hvm=3D` enables the features for HVM guests other than the hardware dom= ain, - which follows the whole-feature forms (the plain boolean, or an un-suffi= xed - `vcpu-pt=3D`). +* `pv=3D`, `hvm=3D` and `dom0=3D` enable the features for PV guests or HVM= guests + 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 guest 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 @@ -228,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 guest type, along with the other features. HVM guests u= se + the sparse view only together with `vcpu-pt`, and PV guests 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, and `directmap` is ignored with a warning. + ### asid (x86) > `=3D ` =20 @@ -3118,6 +3135,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 guest. Enabled explicitly, as a whole or with `dom0` or +`domu`, XPTI keeps the PV guests it covers off the sparse view, and the bo= ot +log says so. Left to its default, XPTI is disabled for the PV guests using +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 41866b77e9..5349b2ba62 100644 --- a/xen/arch/x86/include/asm/sparse-directmap.h +++ b/xen/arch/x86/include/asm/sparse-directmap.h @@ -17,7 +17,11 @@ #define SPARSE_DMAP_END HYPERVISOR_VIRT_END #define SPARSE_DMAP_END_MFN (virt_to_mfn(SPARSE_DMAP_END - 1) + 1) =20 -extern bool opt_sparse_dmap_pv, opt_sparse_dmap_hvm; +/* + * Which domains use the sparse view: PV guests and HVM guests 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; =20 /* Root of the sparse view, once built (NULL before, or if not configured)= . */ extern l4_pgentry_t *sparse_dmap_root; @@ -30,13 +34,22 @@ extern bool sparse_dmap_active; =20 static inline bool sparse_dmap_configured(void) { - return opt_sparse_dmap_pv || opt_sparse_dmap_hvm; + 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) +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. The + * hardware domain, PV or PVH, has a knob of its own. + */ +static inline bool sparse_dmap_wanted(bool pv, bool hwdom) { - return sparse_dmap_root && (pv ? opt_sparse_dmap_pv : opt_sparse_dmap_= hvm); + return sparse_dmap_root && + (hwdom ? opt_sparse_dmap_hwdom + : pv ? opt_sparse_dmap_pv : opt_sparse_dmap_hvm); } =20 void sparse_dmap_init(void); @@ -54,7 +67,10 @@ int sparse_dmap_drop_pagetable(mfn_t mfn); #define sparse_dmap_active false =20 static inline bool sparse_dmap_configured(void) { return false; } -static inline bool sparse_dmap_wanted(bool pv) { 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 void sparse_dmap_activate(void) {} static inline void sparse_dmap_install(l4_pgentry_t *l4t, bool pv) {} diff --git a/xen/arch/x86/sparse-directmap.c b/xen/arch/x86/sparse-directma= p.c index 54a6e36132..4fe160dad3 100644 --- a/xen/arch/x86/sparse-directmap.c +++ b/xen/arch/x86/sparse-directmap.c @@ -55,6 +55,7 @@ =20 bool __ro_after_init opt_sparse_dmap_pv; bool __ro_after_init opt_sparse_dmap_hvm; +bool __ro_after_init opt_sparse_dmap_hwdom; =20 /* 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]; @@ -358,7 +359,8 @@ void __init sparse_dmap_init(void) { printk(XENLOG_WARNING "Sparse directmap: too many early heap ranges, disabled\n"); - opt_sparse_dmap_pv =3D opt_sparse_dmap_hvm =3D false; + opt_sparse_dmap_pv =3D opt_sparse_dmap_hvm =3D opt_sparse_dmap_hwd= om =3D + false; return; } =20 diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index 46105ef185..65e1d14861 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -390,6 +390,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 ) { @@ -400,10 +413,23 @@ 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 is on here, so alert the user when 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 @@ -494,6 +520,33 @@ static int __init cf_check parse_pv_l1tf(const char *s) } custom_param("pv-l1tf", parse_pv_l1tf); =20 +/* + * The sparse directmap view, for PV guests and HVM guests other than the + * hardware domain, and for the hardware domain, PV or PVH. A guest type = the + * build lacks can't use the sparse view, and doesn't get it built. Witho= ut + * CONFIG_SPARSE_DIRECTMAP there is no sparse view: the forms enabling all + * features, generally or for one guest type, leave it 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 +} + +/* + * An explicit asi=3Ddirectmap on a build without the sparse directmap vie= w. + * Warned about from init_speculation_mitigations(): printed while the com= mand + * line is parsed, the message would stay in the console ring, and not rea= ch + * the console. + */ +static bool __initdata asi_directmap_ignored; + static int __init cf_check parse_asi(const char *s) { const char *ss; @@ -501,7 +554,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_vcpu_pt_hwdom =3D opt_vcpu_pt_hvm =3D true; + set_sparse_dmap(true, true, true, true); + } =20 do { ss =3D strchr(s, ','); @@ -514,11 +570,29 @@ static int __init cf_check parse_asi(const char *s) case 0: case 1: opt_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("hvm", s, ss)) >=3D 0 ) + if ( (val =3D parse_boolean("pv", s, ss)) >=3D 0 ) + 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_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 guest type, the hardware domain too.= */ + set_sparse_dmap(true, true, true, val); + asi_directmap_ignored =3D val && + !IS_ENABLED(CONFIG_SPARSE_DIRECTMA= P); + } else if ( (val =3D parse_boolean("vcpu-pt", s, ss)) !=3D -1 ) { switch ( val ) @@ -744,13 +818,35 @@ static void __init print_details(enum ind_thunk thunk) opt_pv_l1tf_domu ? "enabled" : "disabled"); #endif =20 - /* vCPU-PT is implemented for HVM only: a PV Dom0 gets none of it. */ - printk(" ASI features for Dom0:%s\n", - opt_dom0_pvh && opt_vcpu_pt_hwdom ? " vCPU-PT" : " None"); + /* + * vCPU-PT is implemented for HVM only, and HVM guests use the sparse + * directmap only with it; PV guests for which XPTI is enabled don't u= se + * it (see xpti_init_default()). Dom0 is listed as its type makes it. + */ + { + bool hwdom_pt =3D opt_dom0_pvh && opt_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) @@ -1942,11 +2038,14 @@ void spec_ctrl_init_domain(struct domain *d) : opt_vcpu_pt_hvm); =20 /* - * The sparse directmap view needs a mapcache: PV domains all have one, - * HVM domains through per-vCPU page-tables. + * The sparse directmap view needs a mapcache: PV domains all have + * one, HVM domains through per-vCPU page-tables. Left to its + * default, XPTI is off for the domains meant for the sparse view, + * so this happens only where it was enabled explicitly + * (xpti_init_default()). */ - d->arch.sparse_dmap =3D pv ? sparse_dmap_wanted(true) - : d->arch.vcpu_pt && sparse_dmap_wanted(false= ); + d->arch.sparse_dmap =3D (pv ? !d->arch.pv.xpti : d->arch.vcpu_pt) && + sparse_dmap_wanted(pv, is_hardware_domain(d)); } =20 void __init init_speculation_mitigations(void) @@ -2239,7 +2338,12 @@ 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_vcpu_pt_hwdom || opt_vcpu_pt_hvm ) + if ( asi_directmap_ignored ) + warning_add( + "asi: directmap ignored, Xen built without\n" + "CONFIG_SPARSE_DIRECTMAP.\n"); + + if ( opt_vcpu_pt_hwdom || opt_vcpu_pt_hvm || sparse_dmap_configured() ) 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