From nobody Mon Nov 23 22:31:30 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1795341314; cv=none; d=zohomail.com; s=zohoarc; b=VzA7lBVjVYRHrUhfXnM0ev7vrOs7IhzHUw2ICz0kR0jjJXqxF3cVo3Wt18I7bQB3XkEHrLagKZObQR6Uj5DzNOhGyhLRXmRAVhVbTqi1jIMDghfXi1gAFdToXxNJ0ejjsGbzHs9ucR2aQsE6852bPf4tIjEuvEOCWBJ4WHB1RWA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795341314; 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=F45nlTbvn7TAkfWyNo2QK2nZ+G6tc7lSCTyxrLV0R8k=; b=SO//XKeZTFDzjwRIaB+CTBAlAtiFzTMOQ8vCmfCANuldST1I3I32f5XaHc9u57ymyKahrK7CLMe4zTzmwvyTMRZ0gNVk5a8k7kWoBJx0z5Puaou8oHkRYscidtoRE4ogBw0t6+zUmgBbXbkE0+iaSsm9TgjRO7DkDa4SFcIHyfQ= 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 1795341314276592.8324766132752; Sun, 22 Nov 2026 01:55:14 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1499535.1696290 (Exim 4.92) (envelope-from ) id 1xV4HL-0004kn-FA; Sun, 22 Nov 2026 09:54:35 +0000 Received: by outflank-mailman (output) from mailman id 1499535.1696290; Sun, 22 Nov 2026 09:54:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HL-0004kZ-AX; Sun, 22 Nov 2026 09:54:35 +0000 Received: by outflank-mailman (input) for mailman id 1499535; Sun, 22 Nov 2026 09:54:34 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HK-0004iC-Hr for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 09:54:34 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xV4HI-008zYS-Jp for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 10:54:32 +0100 Received: from [10.42.69.10] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b02bb5d-2eae-0a2a0a5409dd-0a2a450a9242-34 for ; Sun, 22 Nov 2026 10:54:32 +0100 Received: from [209.85.128.47] (helo=mail-wm1-f47.google.com) by tlsNG-4011c0.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b02bbd8-f2d2-0a2a450a0019-d155802fd145-3 for ; Sun, 22 Nov 2026 10:54:32 +0100 Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4a214c5c4c0so20859305e9.3 for ; Sun, 22 Nov 2026 01:54:32 -0800 (PST) Received: from PKL-HASEEBA-LT.. ([39.36.59.78]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8849e598sm218409605e9.8.2026.11.22.01.54.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Nov 2026 01:54:31 -0800 (PST) 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" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795341272; x=1795946072; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=F45nlTbvn7TAkfWyNo2QK2nZ+G6tc7lSCTyxrLV0R8k=; b=V1ndHlNHRaSNYfhsb0xm+9Z8ZMXr/0ySMkVFPuHVvz4yR7Wq0/rHiWIk7gCUKQnkxf eHRKcU+N1gmhbfqag6AtaKgWoJwTV7rJBWZQnpAASJsVoYTUcZwmWsvlGM40Q8UXzubb XAZeiKIn7sI+BAR0mxJPvlAEaLlkPfhi8mOcp/xcceeGnILtg/P+4DKWCd5xLGNGS29W IdaPaG86bhfv6WsqXke5ffsGTcjcZSkmPZGg4jfzfwf32cB6rLxKo4/eqXFSwU8k+9yU dnXmWjtcMPi1uLNUKScLkC2k3WMGA0zQzNooKXTbKkel2PlNDsY5R9WLIiCmYUGZk94M Bc5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795341272; x=1795946072; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=F45nlTbvn7TAkfWyNo2QK2nZ+G6tc7lSCTyxrLV0R8k=; b=fwgbfUEaoh0vHDnv6iggAEh1UaBnYIfLrPt5NkZcfujAvlXcc0NSO+nyARK/vH/mVu 29WTVgdUjtJSUxGTmgupLNh4P5wyMyFXZNRO1xWlz1OHEaNcAENxYed9ANXUkTw/eRys ESySGj3UHpOmdFj5lq/GO5QY5NOFOAX5XHTajlt+LnqsFCioY3Qap2M5O3w2h29xGHa8 3hVDgxPJ/yUE2NLk5woOjmPSABT5AJ8yusodbET3ZHsmup1DdkeIp0TdMwTHYkRSycC5 i+3hamR5xy4hXdCzvt5+QYF9dLbPg69XammBUXTfwNwleKKRhRpV6p51lNSkcxI84NNA AJkg== X-Gm-Message-State: APuU4Z9eHUcdVnMXIc7Dh8SsLydAIMWXDfljLivWASrYz/WIGRW2NGsw fUb87SDX8vRIOCJjdNO/F9308WBJF4pRHscW7S2+yv99ftk3AOemVRrCBkQjagpGH5w= X-Gm-Gg: AZFdTZeZ2i8F6ctaU60zfU/ydWpAQ7GS+HAueqXBcqAPvSyloNjMHHJ/PTcNnFVCv8C BAWpe0VrxsYwOcTTcsvJlOCJ74xtiAIf4tydUpjLPeT3/gQJpfHzp51oqsZw7MGnUaA8cmekC67 IRMpgY0iYTVx1PHq5WSj0v6LAQXMKa9K2hPMitLUADVhF9PLU4w4j2DkIbcpNWlru8mv+bw9LXB FwL7ykdDjhcVGzA5S0ggYKZcK3tPdwWbK4/MssN6LdS82cFf6THZs53su0qaEicEpiwodFvXvk7 s4kDNhs9qMa6zDth1zsI3QFyHB41J8MzDQE2A8tlBzMc+61SQgJl+OyTqSgYBHzXMOnw8ZPRFoL ebVNIfzbrYfq7+Udf/WXDOha8WDhRTyoHXw0QYivR0uA0QGd73XuSViMo0wwS6wzkfAUDtm9Xf9 qk1b+yXJQtN2esBp9+IiIdCIgU2h9lsErIyM6m7GGtlxAH8EmNw1lhwqpENtxl2Db6y7k8cqzH2 92fm96M+/KemB5nLmMzRkCNdiSW+Mfckce0+y3ulqQkLYzTgSX8LoTIpXcZLjPxUdho0PEQ0dqN SNJts3vZ6gk+G6SwGpe9i5/jOCyNqwKdb2XHSN22D6aewuzRofVQ27O+0+BIcDc2xcvNC5Vne3k VHp/kmO+3Z0sRp13AOsg6fAP6 X-Received: by 2002:a05:600c:4f15:b0:4bb:6861:657d with SMTP id 5b1f17b1804b1-4bb88923954mr198879805e9.21.1795341271760; Sun, 22 Nov 2026 01:54:31 -0800 (PST) From: Haseeb Ashraf To: xen-devel@lists.xenproject.org Cc: Haseeb Ashraf , sstabellini@kernel.org, julien@xen.org, bertrand.marquis@arm.com, michal.orzel@amd.com, Volodymyr_Babchuk@epam.com Subject: [XEN][PATCH v5 1/8] xen/arm/p2m: perform IPA-based TLBI when IPA is known Date: Sun, 22 Nov 2026 14:54:15 +0500 Message-ID: <536b83c4b9b22c6be55aad4e3d1c9d13be2aa31a.1795336377.git.haseebashraf091@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-4011c0/1795341272-528DDCFC-DDA21309/0/0 X-purgate-type: clean X-purgate-size: 17343 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1795341316327048800 Content-Type: text/plain; charset="utf-8" From: Haseeb Ashraf VMALLS12E1IS invalidates all stage-1 and stage-2 translations for the current VMID across the inner-shareable domain, regardless of which mapping changed. This can be particularly costly when every TLBI is emulated by an outer hypervisor. Narrow the invalidation where Xen knows what changed: - When a pCPU switches between vCPUs of a domain, use a local flush to drop the prior vCPU's stage-1 translations. Arm64 uses a stage-1-only TLBI; on Arm32 the available local guest-TLB instruction can also invalidate stage-2 entries, depending on execution context. - When p2m_remove_mapping() removes a range, invalidate that IPA range instead of the whole VMID. - When a valid entry is replaced, invalidate its IPA range. When a superpage is split, invalidate the whole old block at its aligned base. The IPA sequence issues one broadcast TLBI per page and is limited to fewer than TLBI_MAX_PAGE_OPS pages (512 with 4KB pages), following Linux's MAX_DVM_OPS limit. A larger range keeps the whole-VMID invalidation. The limit bounds the number of broadcast operations; it is not a performance crossover. Existing deferred flushes remain unchanged: teardown of all mappings of a dying domain, permission-only memaccess changes, and removals by callers that do not know the range are still flushed by p2m_write_unlock(). Complete stage-2 invalidation before stage-1 invalidation, and complete the broadcast sequence before the repeat-TLBI workaround. On Arm32, the range helper keeps the full flush; patch 3 adds the capability-gated IPA implementation. Linux references: - arm64: KVM: Implement TLB handling, for the IPA TLBI followed by a stage-1 invalidation: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?= id=3D5eec0a91e32a2862e86265532ae773820e0afd77 - arm64: tlbi: Set MAX_TLBI_OPS to PTRS_PER_PTE, for the per-flush operation limit: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?= id=3D3d65b6bbc01ecece8142e62a8a5f1d48ba41a240 - The later rename of that limit to MAX_DVM_OPS: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?= id=3Dec1c3b9ff16082f880b304be40992568f4eee6a7 Suggested-by: Julien Grall Signed-off-by: Haseeb Ashraf --- Changes in v5: - Replace the 256MB limit with TLBI_MAX_PAGE_OPS, following Linux. - Flush the known range in p2m_remove_mapping(); other removals and permission-only memaccess changes remain deferred. - Invalidate the whole old block when a superpage is split. - Complete stage-2 invalidation before stage-1 invalidation, and complete the first DSB before the repeat-TLBI workaround. - Fix the Arm64 helper macros so this patch builds on its own; they did not build on their own in v4. - Keep TLBI instructions in one-instruction helpers and add the MPU stub for p2m_force_tlb_flush_range_sync(). Cite DDI 0487 M.d. xen/arch/arm/include/asm/arm32/flushtlb.h | 33 ++++++++ xen/arch/arm/include/asm/arm64/flushtlb.h | 78 ++++++++++++++++++ xen/arch/arm/include/asm/flushtlb.h | 12 +++ xen/arch/arm/include/asm/mmu/p2m.h | 2 + xen/arch/arm/include/asm/mpu/p2m.h | 4 + xen/arch/arm/mmu/p2m.c | 99 +++++++++++++++++++---- xen/arch/arm/p2m.c | 16 +++- 7 files changed, 226 insertions(+), 18 deletions(-) diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/inclu= de/asm/arm32/flushtlb.h index 5483be08fb..374b8bfe54 100644 --- a/xen/arch/arm/include/asm/arm32/flushtlb.h +++ b/xen/arch/arm/include/asm/arm32/flushtlb.h @@ -45,6 +45,23 @@ TLB_HELPER(flush_xen_tlb_local, TLBIALLH, nsh) =20 #undef TLB_HELPER =20 +/* + * Flush TLB of local processor. Use when flush for only stage-1 is intend= ed. + * + * The following function should be used where intention is to clear only + * stage-1 TLBs. + */ +static inline void flush_guest_tlb_s1_local(void) +{ + /* + * Same instruction can invalidate both stage-1 and stage-2 TLBs depen= ding + * upon the execution context. + * + * See Arm ARM (DDI 0487 M.d), G5.9.5, Table G5-23. + */ + flush_guest_tlb_local(); +} + /* Flush TLB of local processor for address va. */ static inline void __flush_xen_tlb_one_local(vaddr_t va) { @@ -60,6 +77,22 @@ static inline void __flush_xen_tlb_one(vaddr_t va) /* Only for ARM64_WORKAROUND_REPEAT_TLBI */ static inline void __tlb_repeat_sync(void) {} =20 +/* + * Flush a range of IPA's mappings from the TLB of all processors in the + * inner-shareable domain. + */ +static inline void flush_guest_tlb_range_ipa(paddr_t ipa, + unsigned long size) +{ + /* + * Following can invalidate both stage-1 and stage-2 TLBs depending up= on + * the execution mode. + * + * See Arm ARM (DDI 0487 M.d), G5.9.5, Table G5-23. + */ + flush_guest_tlb(); +} + #endif /* __ASM_ARM_ARM32_FLUSHTLB_H__ */ /* * Local variables: diff --git a/xen/arch/arm/include/asm/arm64/flushtlb.h b/xen/arch/arm/inclu= de/asm/arm64/flushtlb.h index 1606b26bf2..b3bce7baf2 100644 --- a/xen/arch/arm/include/asm/arm64/flushtlb.h +++ b/xen/arch/arm/include/asm/arm64/flushtlb.h @@ -67,6 +67,9 @@ TLB_HELPER_LOCAL(flush_guest_tlb_local, vmalls12e1) /* Flush innershareable TLBs, current VMID only */ TLB_HELPER(flush_guest_tlb, vmalls12e1is) =20 +/* Flush local TLBs, current VMID, stage-1 only */ +TLB_HELPER_LOCAL(flush_guest_tlb_s1_local, vmalle1) + /* Flush local TLBs, all VMIDs, non-hypervisor mode */ TLB_HELPER_LOCAL(flush_all_guests_tlb_local, alle1) =20 @@ -116,6 +119,81 @@ static inline void __tlb_repeat_sync(void) : : : "memory"); } =20 +/* + * Complete a broadcast TLBI sequence. + * + * The first DSB completes the TLBIs issued so far. For all relevant errata + * it is then only necessary to execute a single additional TLBI;DSB seque= nce, + * which __tlb_repeat_sync() does when the workaround is enabled. + */ +static inline void tlbi_complete_broadcast(void) +{ + dsb(ish); + __tlb_repeat_sync(); + isb(); +} + +/* + * TLBI IPAS2E1IS: invalidate the stage-2 and combined entries of one IPA = of + * the current VMID, in the inner-shareable domain. The caller issues the + * barriers. + */ +static inline void tlbi_ipas2e1is(paddr_t ipa) +{ + asm_inline volatile( + "tlbi ipas2e1is, %0" + : : "r" (ipa >> PAGE_SHIFT) : "memory"); +} + +/* TLBI VMALLE1IS: invalidate the stage-1 entries of the current VMID. */ +static inline void tlbi_vmalle1is(void) +{ + asm_inline volatile("tlbi vmalle1is" : : : "memory"); +} + +/* + * Flush a range of IPA's mappings from the TLB of all processors in the + * inner-shareable domain. + * + * A range of TLBI_MAX_PAGE_OPS pages or more is flushed with the whole + * VMID. Each page of the range costs a broadcast TLBI, and the limit boun= ds + * their number like MAX_DVM_OPS does in Linux. It is not a tuned value. + */ +static inline void flush_guest_tlb_range_ipa(paddr_t ipa, unsigned long si= ze) +{ + unsigned long pages =3D size >> PAGE_SHIFT; + paddr_t end =3D ipa + size; + + if ( pages >=3D TLBI_MAX_PAGE_OPS ) + { + flush_guest_tlb(); + return; + } + + /* + * See Arm ARM (DDI 0487 M.d), D8.17.6.1 (Invalidating TLB entries from + * stage 2 translations). + */ + dsb(ishst); /* Ensure prior page-table updates have completed */ + for ( ; ipa < end; ipa +=3D PAGE_SIZE ) + { + /* Flush stage-2 TLBs for IPA address */ + tlbi_ipas2e1is(ipa); + } + + /* + * The stage-1 entries and walks that went through the removed stage-2 + * entries have to be invalidated too. The stage-2 invalidation has to= be + * complete first, or a walk on another CPU could still refill the TLBs + * with stage-1 and the old stage-2 translation. Linux does the same (= see + * __kvm_tlb_flush_vmid_ipa()). + */ + dsb(ish); + tlbi_vmalle1is(); + + tlbi_complete_broadcast(); +} + #endif /* __ASM_ARM_ARM64_FLUSHTLB_H__ */ /* * Local variables: diff --git a/xen/arch/arm/include/asm/flushtlb.h b/xen/arch/arm/include/asm= /flushtlb.h index c292c3c00d..2711b76611 100644 --- a/xen/arch/arm/include/asm/flushtlb.h +++ b/xen/arch/arm/include/asm/flushtlb.h @@ -17,6 +17,18 @@ static inline void page_set_tlbflush_timestamp(struct pa= ge_info *page) page->tlbflush_timestamp =3D tlbflush_current_time(); } =20 +/* + * Upper bound on the number of broadcast TLBI operations issued, one per + * page, to invalidate a range. A range of this many pages or more is + * invalidated with the whole VMID instead. + * + * This is the number of entries of a page table, which is the limit Linux + * uses for the same purpose (MAX_DVM_OPS in arch/arm64/include/asm/ + * tlbflush.h). It bounds the burst of broadcast operations, and the time + * spent with the p2m lock held. It is not a tuned value. + */ +#define TLBI_MAX_PAGE_OPS (1UL << (PAGE_SHIFT - 3)) + #if defined(CONFIG_ARM_32) # include #elif defined(CONFIG_ARM_64) diff --git a/xen/arch/arm/include/asm/mmu/p2m.h b/xen/arch/arm/include/asm/= mmu/p2m.h index 58496c0b09..8a16722b82 100644 --- a/xen/arch/arm/include/asm/mmu/p2m.h +++ b/xen/arch/arm/include/asm/mmu/p2m.h @@ -10,6 +10,8 @@ extern unsigned int p2m_root_level; =20 struct p2m_domain; void p2m_force_tlb_flush_sync(struct p2m_domain *p2m); +void p2m_force_tlb_flush_range_sync(struct p2m_domain *p2m, uint64_t start= _ipa, + uint64_t page_count); void p2m_tlb_flush_sync(struct p2m_domain *p2m); =20 void p2m_clear_root_pages(struct p2m_domain *p2m); diff --git a/xen/arch/arm/include/asm/mpu/p2m.h b/xen/arch/arm/include/asm/= mpu/p2m.h index 1484c75b55..b799738314 100644 --- a/xen/arch/arm/include/asm/mpu/p2m.h +++ b/xen/arch/arm/include/asm/mpu/p2m.h @@ -17,6 +17,10 @@ static inline void p2m_clear_root_pages(struct p2m_domai= n *p2m) {} =20 static inline void p2m_tlb_flush_sync(struct p2m_domain *p2m) {} =20 +static inline void p2m_force_tlb_flush_range_sync(struct p2m_domain *p2m, + uint64_t start_ipa, + uint64_t page_count) {} + #endif /* __ARM_MPU_P2M_H__ */ =20 /* diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c index 53d28935af..b93ead6c46 100644 --- a/xen/arch/arm/mmu/p2m.c +++ b/xen/arch/arm/mmu/p2m.c @@ -233,37 +233,33 @@ void p2m_restore_state(struct vcpu *n) isb(); =20 /* - * Flush local TLB for the domain to prevent wrong TLB translation - * when running multiple vCPU of the same domain on a single pCPU. + * All vCPUs of the domain share the same p2m and the same effective + * stage-2 translation regime. The stage-1 translations of the vCPU th= at + * ran last on this pCPU must not be used by this one. */ if ( *last_vcpu_ran !=3D INVALID_VCPU_ID && *last_vcpu_ran !=3D n->vcp= u_id ) - flush_guest_tlb_local(); + flush_guest_tlb_s1_local(); =20 *last_vcpu_ran =3D n->vcpu_id; } =20 /* - * Force a synchronous P2M TLB flush. + * Loads VTTBR from given P2M. * * Must be called with the p2m lock held. + * + * This returns switched out VTTBR. */ -void p2m_force_tlb_flush_sync(struct p2m_domain *p2m) +static uint64_t p2m_load_vttbr(struct p2m_domain *p2m, unsigned long *flag= s) { - unsigned long flags =3D 0; uint64_t ovttbr; =20 - ASSERT(p2m_is_write_locked(p2m)); - - /* - * ARM only provides an instruction to flush TLBs for the current - * VMID. So switch to the VTTBR of a given P2M if different. - */ ovttbr =3D READ_SYSREG64(VTTBR_EL2); if ( ovttbr !=3D p2m->vttbr ) { uint64_t vttbr; =20 - local_irq_save(flags); + local_irq_save(*flags); =20 /* * ARM64_WORKAROUND_AT_SPECULATE: We need to stop AT to allocate @@ -282,8 +278,14 @@ void p2m_force_tlb_flush_sync(struct p2m_domain *p2m) isb(); } =20 - flush_guest_tlb(); + return ovttbr; +} =20 +/* + * Restores VTTBR which was switched out as a result of p2m_load_vttbr(). + */ +static void p2m_restore_vttbr(uint64_t ovttbr, unsigned long flags) +{ if ( ovttbr !=3D READ_SYSREG64(VTTBR_EL2) ) { WRITE_SYSREG64(ovttbr, VTTBR_EL2); @@ -291,10 +293,64 @@ void p2m_force_tlb_flush_sync(struct p2m_domain *p2m) isb(); local_irq_restore(flags); } +} + +/* + * Force a synchronous P2M TLB flush. + * + * Must be called with the p2m lock held. + */ +void p2m_force_tlb_flush_sync(struct p2m_domain *p2m) +{ + unsigned long flags =3D 0; + uint64_t ovttbr; + + ASSERT(p2m_is_write_locked(p2m)); + + /* + * ARM only provides an instruction to flush TLBs for the current + * VMID. So switch to the VTTBR of a given P2M if different. + */ + ovttbr =3D p2m_load_vttbr(p2m, &flags); + + flush_guest_tlb(); + + p2m_restore_vttbr(ovttbr, flags); =20 p2m->need_flush =3D false; } =20 +/* + * Force a synchronous P2M TLB flush on a range of addresses. + * + * Must be called with the p2m lock held. + */ +void p2m_force_tlb_flush_range_sync(struct p2m_domain *p2m, uint64_t start= _ipa, + uint64_t page_count) +{ + unsigned long flags =3D 0; + uint64_t ovttbr; + + ASSERT(p2m_is_write_locked(p2m)); + + if ( !page_count ) + return; + + /* + * ARM only provides an instruction to flush TLBs for the current + * VMID. So switch to the VTTBR of a given P2M if different. + */ + ovttbr =3D p2m_load_vttbr(p2m, &flags); + + /* Invalidate TLB entries by IPA range */ + if ( unlikely(page_count > ULONG_MAX / PAGE_SIZE) ) + flush_guest_tlb(); + else + flush_guest_tlb_range_ipa(start_ipa, page_count * PAGE_SIZE); + + p2m_restore_vttbr(ovttbr, flags); +} + void p2m_tlb_flush_sync(struct p2m_domain *p2m) { if ( p2m->need_flush ) @@ -1013,6 +1069,7 @@ static int __p2m_set_entry(struct p2m_domain *p2m, { /* We need to split the original page. */ lpae_t split_pte =3D *entry; + unsigned long nr =3D 1UL << XEN_PT_LEVEL_ORDER(level); =20 ASSERT(p2m_is_superpage(*entry, level)); =20 @@ -1036,7 +1093,9 @@ static int __p2m_set_entry(struct p2m_domain *p2m, * For more details see (D4.7.1 in ARM DDI 0487A.j). */ p2m_remove_pte(entry, p2m->clean_pte); - p2m_force_tlb_flush_sync(p2m); + p2m_force_tlb_flush_range_sync(p2m, + pfn_to_paddr(gfn_x(sgfn) & ~(nr - 1= )), + nr); =20 p2m_write_pte(entry, split_pte, p2m->clean_pte); =20 @@ -1092,7 +1151,12 @@ static int __p2m_set_entry(struct p2m_domain *p2m, p2m_remove_pte(entry, p2m->clean_pte); =20 if ( removing_mapping ) - /* Flush can be deferred if the entry is removed */ + /* + * Flush can be deferred if the entry is removed. A caller that kn= ows + * the range it removes flushes it by IPA when it is done, see + * p2m_remove_mapping(). Otherwise p2m_write_unlock() flushes all + * the TLBs of the domain, once for all the entries removed. + */ p2m->need_flush |=3D !!lpae_is_valid(orig_pte); else { @@ -1112,7 +1176,8 @@ static int __p2m_set_entry(struct p2m_domain *p2m, { if ( likely(!p2m->mem_access_enabled) || P2M_CLEAR_PERM(pte) !=3D P2M_CLEAR_PERM(orig_pte) ) - p2m_force_tlb_flush_sync(p2m); + p2m_force_tlb_flush_range_sync(p2m, gfn_to_gaddr(sgfn), + 1UL << page_order); else p2m->need_flush =3D true; } diff --git a/xen/arch/arm/p2m.c b/xen/arch/arm/p2m.c index fb03978a19..a39351ede9 100644 --- a/xen/arch/arm/p2m.c +++ b/xen/arch/arm/p2m.c @@ -133,6 +133,7 @@ static inline int p2m_remove_mapping(struct domain *d, int rc; =20 p2m_write_lock(p2m); + /* * Before removing the GFN - MFN mapping for any RAM pages make sure * that there is no difference between what is already mapped and what @@ -161,7 +162,20 @@ static inline int p2m_remove_mapping(struct domain *d, rc =3D p2m_set_entry(p2m, start_gfn, nr, INVALID_MFN, p2m_invalid, p2m_access_rwx); =20 -out: + /* + * The range that may have changed is known. Invalidate it instead of + * all the TLBs of the domain, which p2m_write_unlock() would do. The + * whole domain is still flushed when the range is too large for an + * invalidation by IPA. A failure after some entries were removed is + * covered by the same range. + */ + if ( p2m->need_flush ) + { + p2m_force_tlb_flush_range_sync(p2m, gfn_to_gaddr(start_gfn), nr); + p2m->need_flush =3D false; + } + + out: p2m_write_unlock(p2m); =20 return rc; From nobody Mon Nov 23 22:31:30 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1795341302; cv=none; d=zohomail.com; s=zohoarc; b=EbkIKzNiIvQqVtoe8U6THL7Nwby0hv5bzhMExBNI79TDLDQLDb+qMDqrxH4tW0q8QBJmgfXzBFzR+XiXv4DUE3zAIMiGNFlrIaiB54chTeTBa4BkU0NeckSbct7BVNShRQdD1fKRpgcCC4qRwuav/z/j45zUMS1c3aDhPyGI4W8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795341302; 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=RLiorzuc0P93TyP+5tAC8yc+LVzrxd1znALUxbig4V0=; b=i3rnCUyyZUVglLNxbF1mBF73LSdkD/5977vxW8A84aTfoqN3ZxHiY+9wMaOGO0QmYgWsvTqf6vZ/NGkC0IgPjtxL1PLYkA4z60yfmpHuixDhhm552vyvn4iKgOBaidKTeoIJQt3v07ul590nkmODFfMUU+w4yDTdDfx54+Tn8i0= 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 1795341302894128.4408015164979; Sun, 22 Nov 2026 01:55:02 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1499536.1696305 (Exim 4.92) (envelope-from ) id 1xV4HN-00057T-RC; Sun, 22 Nov 2026 09:54:37 +0000 Received: by outflank-mailman (output) from mailman id 1499536.1696305; Sun, 22 Nov 2026 09:54:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HN-00057M-Nt; Sun, 22 Nov 2026 09:54:37 +0000 Received: by outflank-mailman (input) for mailman id 1499536; Sun, 22 Nov 2026 09:54:36 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HL-0004mg-VM for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 09:54:36 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xV4HL-00ExLX-4z for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 10:54:35 +0100 Received: from [10.42.69.5] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b02bbc6-bab6-0a2a0a5309dd-0a2a4505c3ae-6 for ; Sun, 22 Nov 2026 10:54:35 +0100 Received: from [209.85.221.45] (helo=mail-wr1-f45.google.com) by tlsNG-c201ff.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b02bbda-4cb1-0a2a45050019-d155dd2dc49f-3 for ; Sun, 22 Nov 2026 10:54:35 +0100 Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-4a7b8abe209so1735413f8f.0 for ; Sun, 22 Nov 2026 01:54:35 -0800 (PST) Received: from PKL-HASEEBA-LT.. ([39.36.59.78]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8849e598sm218409605e9.8.2026.11.22.01.54.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Nov 2026 01:54:34 -0800 (PST) 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" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795341274; x=1795946074; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=RLiorzuc0P93TyP+5tAC8yc+LVzrxd1znALUxbig4V0=; b=LIhYJXO8YkYcw0DWuVNlGXNqORzSvZWiovfe0sNnrWFxxFm/8jN2i+QZekToOgHyQJ ZCq0RE+fW4f5Dk7CVVEHqhrC45VmAkR6CKCZ8fGXmZVvObvMKrJAlmuuoLXri6GAycFT pIYWjOwGam3AIdV1jNcl8cUqHJKaqvTp6A25AkN+qW/nbszBKP0ikIu3VHKOR6VU7qzF sGzaMWiJ+rCSRj9c08N7aPCMHBOlAENM816k0itHNLaV09tK1KPN+9gZtAMMid36J43/ ugiYmfRYnu5e1RIvd5XpIh7G+/S+Ex3aaSA2NPZRyA1qdHni5O4tFnOD0finTs6687QH 2ySg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795341274; x=1795946074; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=RLiorzuc0P93TyP+5tAC8yc+LVzrxd1znALUxbig4V0=; b=OanK3suIei0T3aC70tBFCU0yKLM3WtnHSOnnY8K/nZjBU7m9oNdMWZDY6wNMwLOiVj gxb0gtDNYHMozoIqzp9moVmF526nSHKmogjhgAnx4G96Oj8zGOnBv8X6CXKZ97QBU5De pK8XeiVROWlZdU4gLMAwLJrmH43qCOAChKGxVGOKHfNr3lbr7tgfX85xRgmmhZJnS2UE lub2QTfbJtjXbHT0OQlyiz/U/zNjJDA1Dx7U20VPzU5ifqrXGs7qVfFQQ9Tw9xhfsRYZ QKtlFs8f1qBFn3bOo+eeywQxeUK0z011C/NTuzoCJeuG3Y2NKuDzc/6uyHz1qPWHBHEA Kzlw== X-Gm-Message-State: APuU4Z+63HArqqGJ/rvcVo9gVHC9yu5/lkXboieHR6nKJeqcYlfGe/7g 3bxgADsthVhBcXBtYB3uBAUaYbd3r/5j5Yj6ot44BXhTHgOkjPq2oGfgxEImn31S/g4= X-Gm-Gg: AZFdTZc1jMtq5ejhwhQrxkc6Yk7/CHd5RwfEm/9U3uf3TFVgn8su/dZBGmtraKJ2jtU gUcKUanZungixXjwdpk3Hpdt///EFy8oG1moDstZfZ0R98x0oqR+Ol7kVu5ay2Op0PBxHJpUh0m X67nFLkpwQM5fRrEi8iwoFmdFrSgTDx2lkhzYmO1Z7qxjG6NnN+4Za7HokG3lQpfVvOLFTy4t09 KUP6oyb89yAdxZUmGQ/KVYNSMWMxLOpZJwuKaqV0Mq23uUfFqwqyIqZ3aeCRdSooDKmv9QS2QE1 4SCTdvfHcXkk9Klu1ojgMiYtVUH4dkCflL/R3ngJV2x1eb+wSjEiodTJd+e1Rz+hXqSvcaf96kU SASXUffPljvpieSA21/jReWwpPzW/QYSlr8WaLB0HxVsJV1xcymRSiX1JE8gCNCSHauw5DFqm33 nloGfg32e2KXf5J+jr9sAHoUe1jLmLrNUZgk/Gxb136mDcSrPDIRC+7NuNx8CGFU4g6iJCWnt7b uS7WC55bLCkCrRtQT2IJWTB6GT2i3BZerzlDDJtndASx9G1L6BmShnsnbTN2/Zc97X950pavC0S DW2Z5YKx8p9NfQ8eKQ3quGu39EWV+efbki4Ujv+kaH+zWrWSMdSexlOIQSFs73Wwn/UUnVPTB/J YTPqVVRy4SJPknUKUAOQ9Gh2j X-Received: by 2002:a05:600c:a11:b0:4bb:69f2:87f7 with SMTP id 5b1f17b1804b1-4bb88812c92mr185131325e9.18.1795341274467; Sun, 22 Nov 2026 01:54:34 -0800 (PST) From: Haseeb Ashraf To: xen-devel@lists.xenproject.org Cc: Haseeb Ashraf , sstabellini@kernel.org, julien@xen.org, bertrand.marquis@arm.com, michal.orzel@amd.com, Volodymyr_Babchuk@epam.com, Mohamed Mediouni Subject: [XEN][PATCH v5 2/8] xen/arm: skip the stage-1 TLBI after IPA flush with FEAT_nTLBPA Date: Sun, 22 Nov 2026 14:54:16 +0500 Message-ID: <784add5436718a89c30d1ce78d5ae5f644d986f6.1795336377.git.haseebashraf091@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-c201ff/1795341275-251182A1-32F69E8B/0/0 X-purgate-type: clean X-purgate-size: 5238 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1795341304096048800 Content-Type: text/plain; charset="utf-8" From: Haseeb Ashraf Without FEAT_nTLBPA, intermediate caching of translation table walks might include non-coherent physical translation caches. These caches can hold previously valid entries indexed by the physical address of a table entry or, for stage-1 walks, by its intermediate physical address. An IPA-based stage-2 invalidation does not remove the stage-1 entries, so it must be followed by a stage-1 invalidation. FEAT_nTLBPA (ID_AA64MMFR1_EL1.nTLBPA) says that the implementation has no such caches. Detect it as a CPU capability and skip the follow-up stage-1 invalidation in flush_guest_tlb_range_ipa() when it is present. The broadcast TLBI completion sequence remains explicit. The local flush used when a pCPU switches between vCPUs of a domain stays unconditional. It drops the prior vCPU's stage-1 translations; FEAT_nTLBPA does not change that requirement. Suggested-by: Mohamed Mediouni Signed-off-by: Haseeb Ashraf --- Changes in v5: - Skip only the stage-1 invalidation that follows IPA invalidation. - Keep the local vCPU-switch flush unconditional. - Base the description on the ID register field. xen/arch/arm/cpufeature.c | 12 ++++++++++++ xen/arch/arm/include/asm/arm64/flushtlb.h | 7 +++++-- xen/arch/arm/include/asm/cpufeature.h | 6 ++++-- xen/arch/arm/include/asm/processor.h | 2 ++ xen/arch/arm/mmu/p2m.c | 2 ++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/xen/arch/arm/cpufeature.c b/xen/arch/arm/cpufeature.c index 8d8cd19a96..0a87186a7e 100644 --- a/xen/arch/arm/cpufeature.c +++ b/xen/arch/arm/cpufeature.c @@ -18,6 +18,11 @@ DECLARE_BITMAP(cpu_hwcaps, ARM_NCAPS); struct cpuinfo_arm __read_mostly domain_cpuinfo; =20 #ifdef CONFIG_ARM_64 +static bool has_ntlbpa(const struct arm_cpu_capabilities *entry) +{ + return system_cpuinfo.mm64.ntlbpa =3D=3D MM64_NTLBPA_SUPPORT_IMP; +} + static bool has_sb_instruction(const struct arm_cpu_capabilities *entry) { return system_cpuinfo.isa64.sb; @@ -25,6 +30,13 @@ static bool has_sb_instruction(const struct arm_cpu_capa= bilities *entry) #endif =20 static const struct arm_cpu_capabilities arm_features[] =3D { +#ifdef CONFIG_ARM_64 + { + .desc =3D "Intermediate caching of translation table walks (nTLBPA= )", + .capability =3D ARM_HAS_NTLBPA, + .matches =3D has_ntlbpa, + }, +#endif #ifdef CONFIG_ARM_64 { .desc =3D "Speculation barrier instruction (SB)", diff --git a/xen/arch/arm/include/asm/arm64/flushtlb.h b/xen/arch/arm/inclu= de/asm/arm64/flushtlb.h index b3bce7baf2..ff2ce2bae2 100644 --- a/xen/arch/arm/include/asm/arm64/flushtlb.h +++ b/xen/arch/arm/include/asm/arm64/flushtlb.h @@ -188,8 +188,11 @@ static inline void flush_guest_tlb_range_ipa(paddr_t i= pa, unsigned long size) * with stage-1 and the old stage-2 translation. Linux does the same (= see * __kvm_tlb_flush_vmid_ipa()). */ - dsb(ish); - tlbi_vmalle1is(); + if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) ) + { + dsb(ish); + tlbi_vmalle1is(); + } =20 tlbi_complete_broadcast(); } diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/a= sm/cpufeature.h index c554686415..97e371724f 100644 --- a/xen/arch/arm/include/asm/cpufeature.h +++ b/xen/arch/arm/include/asm/cpufeature.h @@ -76,8 +76,9 @@ #define ARM_WORKAROUND_BHB_SMCC_3 15 #define ARM_HAS_SB 16 #define ARM64_WORKAROUND_1508412 17 +#define ARM_HAS_NTLBPA 18 =20 -#define ARM_NCAPS 18 +#define ARM_NCAPS 19 =20 #ifndef __ASSEMBLER__ =20 @@ -288,7 +289,8 @@ struct cpuinfo_arm { unsigned long ets:4; unsigned long __res2:4; unsigned long afp:4; - unsigned long __res3:12; + unsigned long ntlbpa:4; + unsigned long __res3:8; unsigned long ecbhb:4; =20 /* MMFR2 */ diff --git a/xen/arch/arm/include/asm/processor.h b/xen/arch/arm/include/as= m/processor.h index 8ee8f88fb4..49774b65eb 100644 --- a/xen/arch/arm/include/asm/processor.h +++ b/xen/arch/arm/include/asm/processor.h @@ -487,6 +487,8 @@ #ifdef CONFIG_ARM_64 #define MM64_VMID_8_BITS_SUPPORT 0x0 #define MM64_VMID_16_BITS_SUPPORT 0x2 +#define MM64_NTLBPA_SUPPORT_NI 0x0 +#define MM64_NTLBPA_SUPPORT_IMP 0x1 #endif =20 #define MM64_MSA_PMSA_SUPPORT 0xf diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c index b93ead6c46..3b1b9b394c 100644 --- a/xen/arch/arm/mmu/p2m.c +++ b/xen/arch/arm/mmu/p2m.c @@ -236,6 +236,8 @@ void p2m_restore_state(struct vcpu *n) * All vCPUs of the domain share the same p2m and the same effective * stage-2 translation regime. The stage-1 translations of the vCPU th= at * ran last on this pCPU must not be used by this one. + * This remains necessary with FEAT_nTLBPA, which only removes the + * stage-1 invalidation following an IPA-based stage-2 invalidation. */ if ( *last_vcpu_ran !=3D INVALID_VCPU_ID && *last_vcpu_ran !=3D n->vcp= u_id ) flush_guest_tlb_s1_local(); From nobody Mon Nov 23 22:31:30 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1795341312; cv=none; d=zohomail.com; s=zohoarc; b=Cc71c1qWR/kQCTnExx0wCFlPRBKqmJbi2BFn9rlaV+LT2BK2tj/kqxKk35T/eBoUMibx07GIv85laB1V2wjMiC3qiBkRT3fn13QelpUL34k0bxzOcuISc4Amuue/ZuY+erKOjFkTw3uOfP1AVBOJgXJ38toj6tq1k3sYU/tl4FQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795341312; 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=I1BLtEUMvehhpFQ6Z0pgP6yp1celKNfoPEoGbciJo5M=; b=JfHdQv3iJbyTuPmvDaSzNUv/TqdE7j3dMCSqaw8Sx6XfKl1QQ8G0TqXou8O1mNOw3DRESxwHd/I6LqmI2kfeK3i5bN1KxHiQLahIJkYjpg9qBlsM0SBm6Jxn7RG71dM+Drv8S9g9W8lfryr220ijuFlW3V/j+XpZ4Tj6NY7fRkg= 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 1795341312513387.15583631343634; Sun, 22 Nov 2026 01:55:12 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1499537.1696315 (Exim 4.92) (envelope-from ) id 1xV4HQ-0005LZ-7h; Sun, 22 Nov 2026 09:54:40 +0000 Received: by outflank-mailman (output) from mailman id 1499537.1696315; Sun, 22 Nov 2026 09:54:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HQ-0005LR-3h; Sun, 22 Nov 2026 09:54:40 +0000 Received: by outflank-mailman (input) for mailman id 1499537; Sun, 22 Nov 2026 09:54:38 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HO-0005Gw-OH for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 09:54:38 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xV4HO-00ExLX-4y for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 10:54:38 +0100 Received: from [10.42.69.5] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b02bbc6-bab6-0a2a0a5309dd-0a2a4505c3ae-12 for ; Sun, 22 Nov 2026 10:54:38 +0100 Received: from [209.85.128.54] (helo=mail-wm1-f54.google.com) by tlsNG-c201ff.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b02bbdd-4cb1-0a2a45050019-d1558036eda8-3 for ; Sun, 22 Nov 2026 10:54:37 +0100 Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4bb85e07aadso26963025e9.1 for ; Sun, 22 Nov 2026 01:54:37 -0800 (PST) Received: from PKL-HASEEBA-LT.. ([39.36.59.78]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8849e598sm218409605e9.8.2026.11.22.01.54.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Nov 2026 01:54:36 -0800 (PST) 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" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795341277; x=1795946077; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=I1BLtEUMvehhpFQ6Z0pgP6yp1celKNfoPEoGbciJo5M=; b=tpo40UgBm/63eXt8y2jn5ahZWOP+I1L8NFnRLv81Wj8qqn1J83JLMFYnh83ZobsI5Y skD4OjVib1diVKBJAjkDImS6b/WNOvz8kiaD1VAt0pCsB7AjCai8IVrGp1hB3DEfeh5U IDHw2VVdjjH+VfOm2S8Ngg6xUT7PuLmvKRvERh3TrMZN476PvE5aovT4iSD6K2TF/YQR D2jW73h6vGPRTjfl1MN89DD2WgpgzkUDRNpgWS189k1RXwJ1W1pgvrGLnbLmMixg9qGH JF+KsM73YPJoBAuQ7LPeVGaVHLgmrhQBgi6bHr87c3+lMvjzEfcfNJNWBrYDg1gYUygx JBig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795341277; x=1795946077; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=I1BLtEUMvehhpFQ6Z0pgP6yp1celKNfoPEoGbciJo5M=; b=m8OrjUmS7Nsklf5o8hYEgb32RdMN85UjZypWUs0qLHHaxsRGa5uZ8BTrvDvknJcFH5 ec7IJSxlLFRKdliT3VAl/lURAzNmqabTrf0ZHaLPyZG1mUi7SY43G2QO+yHVtUUsvQcH 6yzbsTRdekMXVJ8sRE57GQgyvJB2lvXgYKznm5DhC7Xwz93diIrsyxmhkdR5b+jZ8mQJ BqPejVC8hLHUMaQIewmpsSlXNtQhMEQEI/o6UEhw6qk2hVQILnDdl0T9hYGh9/j6y4VT pL+Q47NSoYPJkXxgNjqrNX3Vuen+edj3iecBf88kHlLgxrBaWum/U8reFx0eGWoxg6mW sABQ== X-Gm-Message-State: APuU4Z8/ggZ0Id2DiNV4Nr9BozYpop3+2waPaI9dNkkZPMStXdp/VW7V i4jcuhFmrsDIVg+eo5oRd2mEH6NT+Mjb820rWwX4I6dKEj2fh45zC2/PYXf+dbf54/g= X-Gm-Gg: AZFdTZfU+Ctrk2rhCKChLTJd2cWRTlpI/c6Cd70XeAQr8KHV+N/LP7+8oeO4rrXPjsB RLO6AtCtj5WWRhfVXRGL1SCw8uV4f2zk/n0f5vj4L8QOqJbqlMU4Ki02Cz6FENSvixJJGm5Nulp kyvoVbrT1YCb3D1IB4SJJAcndk2iJpoL+oD/y0rEaZJrBHLw+mhWXVp8gBF0WTvlWPjShOOrQWS NCiN64hG4VzQHmtoxcBIIKEu0AqcDfMQcQYgEkBSlerVNumfTFyIF80ATvezN2W2WBjirql9iz0 sqKuG3GQZCcINmtsMnFiiug31LpzX3qbkfvEYmRNcgDbCa5mC+63/hCqnWjRt42nhlcH0b43csE L5PzrHLeznVvTF6kxp6+UGsORUgoeoyAsVgTVkRm9aO1z0g+ObwDym/kTAcPl39rjsw1NdjqE3P ZAHd4R6YBD9WngTSgO1QRPJuv8v2uXDpaU7v7zz+sWMTfWxkn/0mbg9jCdk8IrxAwrfCmmAaKCJ pR/77lViufjeRP28+UjTHfJL49zUSJ9qatuqSidQ2At7O00n0rOsJe0b3c/Xmqtlcp36thJzK2U 5fjmRSw+4auOdRXOUwiXMQvqmWFSC5SoRXVCKWd/Ihz4StYhGP5pbmWAbvyfao5vRqk8Gj3vUVD +jMcsbyUrg1hTEh09a9CjFe7p/w== X-Received: by 2002:a05:600c:3f0a:b0:4a5:75d2:c104 with SMTP id 5b1f17b1804b1-4bb8880cde7mr169167775e9.22.1795341277368; Sun, 22 Nov 2026 01:54:37 -0800 (PST) From: Haseeb Ashraf To: xen-devel@lists.xenproject.org Cc: Haseeb Ashraf , sstabellini@kernel.org, julien@xen.org, bertrand.marquis@arm.com, michal.orzel@amd.com, Volodymyr_Babchuk@epam.com Subject: [XEN][PATCH v5 3/8] xen/arm32: use IPA-based TLBI when supported with FEAT_nTLBPA Date: Sun, 22 Nov 2026 14:54:17 +0500 Message-ID: <9bf1e84e4ef124fc11fe5c2e58d09ee1003f9434.1795336377.git.haseebashraf091@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-c201ff/1795341278-F5CA82A1-5419D372/0/0 X-purgate-type: clean X-purgate-size: 7495 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1795341314156048800 Content-Type: text/plain; charset="utf-8" From: Haseeb Ashraf Detect TLBI by IPA from ID_MMFR2.UniTLB, which reports the Arm32 instruction support. ID_MMFR5.nTLBPA reports whether intermediate translation-table walk caches are absent. Use TLBI IPAS2IS in the p2m range-flush helper only when both capabilities are present. Without FEAT_nTLBPA, a stage-2-only invalidation would need a separate stage-1 invalidation, which Arm32 cannot issue for the guest; keep the full guest flush in that case. Bound the per-page loop with TLBI_MAX_PAGE_OPS, like the Arm64 fallback. Suggested-by: Julien Grall Signed-off-by: Haseeb Ashraf --- Changes in v5: - Bound the loop with TLBI_MAX_PAGE_OPS. - Put the TLBI instruction in a helper and cite DDI 0487 M.d. xen/arch/arm/cpufeature.c | 21 +++++++++++- xen/arch/arm/include/asm/arm32/flushtlb.h | 42 ++++++++++++++++++++--- xen/arch/arm/include/asm/cpregs.h | 1 + xen/arch/arm/include/asm/cpufeature.h | 21 +++++++++++- xen/arch/arm/include/asm/processor.h | 7 ++++ 5 files changed, 86 insertions(+), 6 deletions(-) diff --git a/xen/arch/arm/cpufeature.c b/xen/arch/arm/cpufeature.c index 0a87186a7e..e4e0dd0922 100644 --- a/xen/arch/arm/cpufeature.c +++ b/xen/arch/arm/cpufeature.c @@ -17,6 +17,18 @@ DECLARE_BITMAP(cpu_hwcaps, ARM_NCAPS); =20 struct cpuinfo_arm __read_mostly domain_cpuinfo; =20 +#ifdef CONFIG_ARM_32 +static bool has_tlb_ipa_instruction(const struct arm_cpu_capabilities *ent= ry) +{ + return system_cpuinfo.mm32.unitlb =3D=3D MM32_UNITLB_BY_IPA; +} + +static bool has_ntlbpa(const struct arm_cpu_capabilities *entry) +{ + return system_cpuinfo.mm32.ntlbpa =3D=3D MM32_NTLBPA_SUPPORT_IMP; +} +#endif + #ifdef CONFIG_ARM_64 static bool has_ntlbpa(const struct arm_cpu_capabilities *entry) { @@ -30,7 +42,14 @@ static bool has_sb_instruction(const struct arm_cpu_capa= bilities *entry) #endif =20 static const struct arm_cpu_capabilities arm_features[] =3D { -#ifdef CONFIG_ARM_64 +#ifdef CONFIG_ARM_32 + { + .desc =3D "IPA-based TLB Invalidation", + .capability =3D ARM32_HAS_TLB_IPA, + .matches =3D has_tlb_ipa_instruction, + }, +#endif +#if defined(CONFIG_ARM_32) || defined(CONFIG_ARM_64) { .desc =3D "Intermediate caching of translation table walks (nTLBPA= )", .capability =3D ARM_HAS_NTLBPA, diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/inclu= de/asm/arm32/flushtlb.h index 374b8bfe54..b2a96372a4 100644 --- a/xen/arch/arm/include/asm/arm32/flushtlb.h +++ b/xen/arch/arm/include/asm/arm32/flushtlb.h @@ -77,6 +77,13 @@ static inline void __flush_xen_tlb_one(vaddr_t va) /* Only for ARM64_WORKAROUND_REPEAT_TLBI */ static inline void __tlb_repeat_sync(void) {} =20 +/* TLBI IPAS2IS: invalidate the stage-2 entries of one IPA of the VMID. */ +static inline void tlbi_ipas2is(paddr_t ipa) +{ + asm volatile ( STORE_CP32(0, TLBIIPAS2IS) + : : "r" (ipa >> PAGE_SHIFT) : "memory" ); +} + /* * Flush a range of IPA's mappings from the TLB of all processors in the * inner-shareable domain. @@ -85,12 +92,39 @@ static inline void flush_guest_tlb_range_ipa(paddr_t ip= a, unsigned long size) { /* - * Following can invalidate both stage-1 and stage-2 TLBs depending up= on - * the execution mode. + * IPA-based stage-2 invalidation is only useful when FEAT_nTLBPA is + * present. Without FEAT_nTLBPA, invalidating stage-2 alone may still + * require discarding associated stage-1 side effects, and Arm32 does = not + * provide a separate guest stage-1 TLBI for that case. * - * See Arm ARM (DDI 0487 M.d), G5.9.5, Table G5-23. + * A range of TLBI_MAX_PAGE_OPS pages or more is flushed with the whole + * VMID, to bound the number of broadcast TLBIs like on Arm64. */ - flush_guest_tlb(); + if ( cpus_have_const_cap(ARM_HAS_NTLBPA) && + cpus_have_const_cap(ARM32_HAS_TLB_IPA) && + (size >> PAGE_SHIFT) < TLBI_MAX_PAGE_OPS ) + { + paddr_t end =3D ipa + size; + + dsb(ishst); /* Ensure prior page-tables updates have completed */ + for ( ; ipa < end; ipa +=3D PAGE_SIZE ) + { + /* Flush stage-2 TLBs for this IPA. */ + tlbi_ipas2is(ipa); + } + dsb(ish); + isb(); + } + else + { + /* + * Following can invalidate both stage-1 and stage-2 TLBs depending + * upon the execution mode. + * + * See Arm ARM (DDI 0487 M.d), G5.9.5, Table G5-23. + */ + flush_guest_tlb(); + } } =20 #endif /* __ASM_ARM_ARM32_FLUSHTLB_H__ */ diff --git a/xen/arch/arm/include/asm/cpregs.h b/xen/arch/arm/include/asm/c= pregs.h index 7e559253d7..9869bc7d3c 100644 --- a/xen/arch/arm/include/asm/cpregs.h +++ b/xen/arch/arm/include/asm/cpregs.h @@ -223,6 +223,7 @@ #define TLBIMVA p15,0,c8,c7,1 /* invalidate unified TLB entry by= MVA */ #define TLBIASID p15,0,c8,c7,2 /* invalid unified TLB by ASID mat= ch */ #define TLBIMVAA p15,0,c8,c7,3 /* invalidate unified TLB entries = by MVA all ASID */ +#define TLBIIPAS2IS p15,4,c8,c0,1 /* Invalidate unified TLB entry fo= r stage 2 by IPA inner shareable */ #define TLBIALLHIS p15,4,c8,c3,0 /* Invalidate Entire Hyp. Unified = TLB inner shareable */ #define TLBIMVAHIS p15,4,c8,c3,1 /* Invalidate Unified Hyp. TLB by = MVA inner shareable */ #define TLBIALLNSNHIS p15,4,c8,c3,4 /* Invalidate Entire Non-Secure No= n-Hyp. Unified TLB inner shareable */ diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/a= sm/cpufeature.h index 97e371724f..38e01aa94d 100644 --- a/xen/arch/arm/include/asm/cpufeature.h +++ b/xen/arch/arm/include/asm/cpufeature.h @@ -77,8 +77,9 @@ #define ARM_HAS_SB 16 #define ARM64_WORKAROUND_1508412 17 #define ARM_HAS_NTLBPA 18 +#define ARM32_HAS_TLB_IPA 19 =20 -#define ARM_NCAPS 19 +#define ARM_NCAPS 20 =20 #ifndef __ASSEMBLER__ =20 @@ -454,6 +455,24 @@ struct cpuinfo_arm { =20 struct { register_t bits[6]; + struct { + /* MMFR0 */ + unsigned long __res0:32; + /* MMFR1 */ + unsigned long __res1:32; + /* MMFR2 */ + unsigned long __res2:16; + unsigned long unitlb:4; + unsigned long __res3:12; + /* MMFR3 */ + unsigned long __res4:32; + /* MMFR4 */ + unsigned long __res5:32; + /* MMFR5 */ + unsigned long __res6:4; + unsigned long ntlbpa:4; + unsigned long __res7:24; + }; } mm32; =20 struct { diff --git a/xen/arch/arm/include/asm/processor.h b/xen/arch/arm/include/as= m/processor.h index 49774b65eb..6899f91ef0 100644 --- a/xen/arch/arm/include/asm/processor.h +++ b/xen/arch/arm/include/asm/processor.h @@ -484,6 +484,13 @@ /* FSR long format */ #define FSRL_STATUS_DEBUG (_AC(0x22,UL)<<0) =20 +#ifdef CONFIG_ARM_32 +#define MM32_UNITLB_NI 0x0 +#define MM32_UNITLB_BY_IPA 0x6 + +#define MM32_NTLBPA_SUPPORT_NI 0x0 +#define MM32_NTLBPA_SUPPORT_IMP 0x1 +#endif #ifdef CONFIG_ARM_64 #define MM64_VMID_8_BITS_SUPPORT 0x0 #define MM64_VMID_16_BITS_SUPPORT 0x2 From nobody Mon Nov 23 22:31:30 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1795341297; cv=none; d=zohomail.com; s=zohoarc; b=DooNMDYBiBmSAsC2Qoeem3s6x/8I+jfX5o7tBkAbghzuioAdMEQmJl68N5DraoCZo0Fohj9qUVMr2guhj3ahUdLa2yVq+D3B82UlQLEZZlxt+PqaLYjJi5Vqfl4+cs6qQhZ9B/ohy5ga0Bavus93+e73vuLNWv8yfRxD4d0lzhg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795341297; 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=fM8Pt+w103yss8koq0I3JGySpUK8NMJUieRlgYIlzMA=; b=D3xymlbERUacdF4P3FtK/cl+Yzzbv/3+SeMtStSQMm2fZOPC0lGlSTrI4Xfu2DlLTFNsJD1qLKxXlYADUU6kBWs9vV2qis+sptcqF9zlAEzE9hRqD+Lz/faDLInhP4EDMTT3byPIwORHAs7yjNNcKedjcsYoaUIpBdRqbfB+fbc= 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 1795341297642578.1615595644662; Sun, 22 Nov 2026 01:54:57 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1499538.1696323 (Exim 4.92) (envelope-from ) id 1xV4HS-0005a2-DK; Sun, 22 Nov 2026 09:54:42 +0000 Received: by outflank-mailman (output) from mailman id 1499538.1696323; Sun, 22 Nov 2026 09:54:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HS-0005Zv-AK; Sun, 22 Nov 2026 09:54:42 +0000 Received: by outflank-mailman (input) for mailman id 1499538; Sun, 22 Nov 2026 09:54:41 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HR-0005Yj-Eb for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 09:54:41 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xV4HQ-00ExLX-RN for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 10:54:40 +0100 Received: from [10.42.69.5] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b02bbc6-bab6-0a2a0a5309dd-0a2a4505c3ae-18 for ; Sun, 22 Nov 2026 10:54:40 +0100 Received: from [209.85.128.44] (helo=mail-wm1-f44.google.com) by tlsNG-c201ff.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b02bbe0-4cb1-0a2a45050019-d155802cbdf4-3 for ; Sun, 22 Nov 2026 10:54:40 +0100 Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4bb702a7548so16642405e9.1 for ; Sun, 22 Nov 2026 01:54:40 -0800 (PST) Received: from PKL-HASEEBA-LT.. ([39.36.59.78]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8849e598sm218409605e9.8.2026.11.22.01.54.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Nov 2026 01:54:39 -0800 (PST) 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" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795341280; x=1795946080; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=fM8Pt+w103yss8koq0I3JGySpUK8NMJUieRlgYIlzMA=; b=OxB7gloYsZVVZuRy3Pxv2Z9tl7oPEgzSfTDJqtK016rnNJUN+1R0jWakaz+M7i/Omt J+izBJ1BBk2ZusYkzCm44hbAHyAdvJEkZ6D06Ldrri/jSC7l0gttQQf/OKytOb39HfUU VT2lqaNfCiNum1Ib6L+qSTzmU1VHlHW04bHqtUjD2nVQFE4tIDWmgiJDL2Vxl76PGa8c ABQTfAd66qaSGQJUv9e4sEBROjuo+MrIbIX+uA6eI6jq9sSPyS9QbyGiSlqRUXNC6FpP 4LNMXoLPOBfiJunTVrGjMmSX5Yeuad1/sJfq21U9/mtVv/AP4E5JHdfJTHwGiXeJSRv3 Umfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795341280; x=1795946080; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=fM8Pt+w103yss8koq0I3JGySpUK8NMJUieRlgYIlzMA=; b=HyzJY4NW147mHDZXhx4EBlDD/7/3cFoy1Qv3ZH5rnmeoLH1jZatZUdHvad0R+jE3B7 Kl1JjKUzaVuSEs9QyVWfPakj71gK8TDbr0Vo+goSjcRkyT+sVyEI3z+VMQuI0yIZxC0c 3weRz9FmQ807+Y0N514fq2oYPmtox5kpcmD59NKG03fwcu1ZbSo0Yo5VZpdrmN3kT2pB p6H2ebNkFECO8/VjguV+aUoE8pSp8rU8Lv4Fo/m3xn8n712tQo8/42Emj2Jnu2hPn2yt 4DWHAhHxu/rkirOKFQa4xdBH4+HVlygv0ocCVV0Y9AleFFP1rhLT8LcUNieyLfbpMlO/ uLkA== X-Gm-Message-State: APuU4Z8fonDlHwP67266Q05+WDjdFvIb9xStmnn6sXiKbCkITfHjnNj0 ch8otALWUidzhnrMcwMyOeArrct1HKDvRoUY/fIuSok1TPqT9VyGDkQ2DQVcSzx9tEA= X-Gm-Gg: AZFdTZcYP9apDI6tVn9FehhxJCw8AdQXzcQu0vIx0LKdsrWMASF6BMfmprDcNthT1Hw LbO8i/XpcuEtmz9vQ98UF+AmgAxksMrDB/BQ4UqsRPlEirKPEmsBQamxedA63haKXDQt3JZWhzV ASwYoXaoK0gT0+36HV6iIjm+cx8Zj2ShATDjyvlhTzzt55C33YckYwnQf8wGs6RvxnCkFdhWDky XJeNszWTC6AeJsKyckeORXG3KuxY54wjSa/DsibPIZVe9MP/l5bSAS6gIQ6B44oXYaSkq8Gm72d mYsA8+okXGPPbY5N/YFFrNismHuad4kAD1X2jKdhb+ZqKvW7P+JPXxAssu+8XlJjuft7Xuo7Dvp BkJw73Ur+5AG9ouKsAUMTlNBYdudOIN4/enyVsOx+SuvUnOsoD0VN6mBDJG7LlNjHmg3+fQ/YwN Vi8HnxSN8mJuc5MZzZrfW0DNZzAIycd1hDu4nSn6jNLV54EFA/uxJwq4PreE4Ei2eqnETAKJkyL bUg+rw5b5tw4E0cV0iGHT+AmclPVDuJ4M/NsFc2l37TYNkGnY9wZou5CnVfzZJw/Ie+i2ljiPEp Wo7dhl4N8xo7n7Bc8CgWfe2xrdJwWTQk7TkHlcBTehx+pNShCcvh//BRcE0g/dfBs+wwlFhI8jp K9CTU32dmvihQkmq3KmEMPKse X-Received: by 2002:a05:600c:1c1b:b0:4a7:a5db:a93e with SMTP id 5b1f17b1804b1-4bb887e2677mr202835235e9.4.1795341280198; Sun, 22 Nov 2026 01:54:40 -0800 (PST) From: Haseeb Ashraf To: xen-devel@lists.xenproject.org Cc: Haseeb Ashraf , sstabellini@kernel.org, julien@xen.org, bertrand.marquis@arm.com, michal.orzel@amd.com, Volodymyr_Babchuk@epam.com Subject: [XEN][PATCH v5 4/8] xen/arm64: use range TLBI for IPA flush with FEAT_TLBIRANGE Date: Sun, 22 Nov 2026 14:54:18 +0500 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-c201ff/1795341280-73ABF2A1-D5A54D98/0/0 X-purgate-type: clean X-purgate-size: 9019 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1795341298134048800 Content-Type: text/plain; charset="utf-8" From: Haseeb Ashraf Invalidating a range of IPAs with TLBI IPAS2E1IS takes one broadcast instruction per page. With FEAT_TLBIRANGE, TLBI RIPAS2E1IS invalidates a range with at most four range operations when it fits the range limit of the instruction. Larger ranges fall back to a full VMID invalidation. When Xen runs on top of another hypervisor, each instruction also traps to it. A range of one page costs one instruction either way. The gain is for multi-page ranges: the whole old block of a split superpage, a removal of several pages, and the batched removal added by patches 6 and 7. Without the feature, a block of 512 pages or more is flushed with the whole VMID, as before. Detect the feature as a CPU capability, from ID_AA64ISAR0_EL1.TLB. When it is present, flush_guest_tlb_range_ipa() covers the range with one operation per scale, the largest first. Only a range that does not fit the largest operation is flushed with VMALLS12E1IS, which is the limit Linux uses (MAX_TLBI_RANGE_PAGES). Without the feature the per-page loop and its TLBI_MAX_PAGE_OPS bound are unchanged. A range operation covers an even number of pages. Extend an odd range by one page to the next even boundary instead of ending with a single page TLBI IPAS2E1IS, which saves one instruction per range. A page more is always safe to invalidate, and extending to the neighbor that makes the range 2 page aligned never leaves the IPA space. The operation is encoded with SYS so that no assembler support for FEAT_TLBIRANGE is required. Stage-2 of Xen does not use FEAT_LPA2, so BADDR holds IPA[48:12]. Linux references: - arm64: tlb: Use the TLBI RANGE feature in arm64, for the range encoding and the scale decomposition: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?= id=3Dd1d3aa98b1d4826a19adfefb69b96142a0cac633 - KVM: arm64: Implement __kvm_tlb_flush_vmid_range(), for the stage-2 IPA range invalidation: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?= id=3D6354d15052ec88273c24beae4c99e31c3d3889b6 Signed-off-by: Haseeb Ashraf --- xen/arch/arm/cpufeature.c | 14 +++ xen/arch/arm/include/asm/arm64/flushtlb.h | 113 ++++++++++++++++++++-- xen/arch/arm/include/asm/cpufeature.h | 3 +- 3 files changed, 121 insertions(+), 9 deletions(-) diff --git a/xen/arch/arm/cpufeature.c b/xen/arch/arm/cpufeature.c index e4e0dd0922..05c4393324 100644 --- a/xen/arch/arm/cpufeature.c +++ b/xen/arch/arm/cpufeature.c @@ -39,6 +39,15 @@ static bool has_sb_instruction(const struct arm_cpu_capa= bilities *entry) { return system_cpuinfo.isa64.sb; } + +static bool has_tlbi_range(const struct arm_cpu_capabilities *entry) +{ + /* + * ID_AA64ISAR0_EL1.TLB: 0b0001 is FEAT_TLBIOS, 0b0010 adds + * FEAT_TLBIRANGE. + */ + return system_cpuinfo.isa64.tlb >=3D 2; +} #endif =20 static const struct arm_cpu_capabilities arm_features[] =3D { @@ -62,6 +71,11 @@ static const struct arm_cpu_capabilities arm_features[] = =3D { .capability =3D ARM_HAS_SB, .matches =3D has_sb_instruction, }, + { + .desc =3D "Range-based TLB invalidation (FEAT_TLBIRANGE)", + .capability =3D ARM64_HAS_TLBI_RANGE, + .matches =3D has_tlbi_range, + }, #endif {}, }; diff --git a/xen/arch/arm/include/asm/arm64/flushtlb.h b/xen/arch/arm/inclu= de/asm/arm64/flushtlb.h index ff2ce2bae2..7dc8ae608a 100644 --- a/xen/arch/arm/include/asm/arm64/flushtlb.h +++ b/xen/arch/arm/include/asm/arm64/flushtlb.h @@ -133,6 +133,70 @@ static inline void tlbi_complete_broadcast(void) isb(); } =20 +/* + * Range-based TLB invalidation (FEAT_TLBIRANGE), see Arm ARM + * (DDI 0487 M.d), C5.5.22 (TLBI RIPAS2E1IS). + * + * The operand of TLBI RIPAS2E1IS is: + * [47:46] TG translation granule, 0b01 for 4KB + * [45:44] SCALE + * [43:39] NUM + * [38:37] TTL 0b00, no level hint + * [36:0] BADDR IPA[48:12], Xen does not use FEAT_LPA2 for stage-2 + * and the instruction invalidates (NUM + 1) * 2^(5 * SCALE + 1) pages. + * + * These are the same definitions as in Linux, see + * arch/arm64/include/asm/tlbflush.h. + */ +#define TLBI_RANGE_MAX_SCALE 3 +#define TLBI_RANGE_PAGES(num, scale) \ + ((((unsigned long)(num)) + 1) << (5 * (scale) + 1)) +#define TLBI_RANGE_MAX_PAGES TLBI_RANGE_PAGES(31, TLBI_RANGE_MAX_SCALE) +/* Gives -1 to 31 for a range of up to TLBI_RANGE_MAX_PAGES pages. */ +#define TLBI_RANGE_NUM(pages, scale) \ + ((long)((pages) >> (5 * (scale) + 1)) - 1) + +/* + * TLBI RIPAS2E1IS: invalidate a range of IPAs of the current VMID, in the + * inner-shareable domain. It is encoded as SYS #4, C8, C0, #2 so that no + * assembler support for FEAT_TLBIRANGE is required. + */ +static inline void tlbi_ripas2e1is(uint64_t op) +{ + asm_inline volatile( + "sys #4, c8, c0, #2, %0" + : : "r" (op) : "memory"); +} + +/* + * Invalidate an even number @pages, of at most TLBI_RANGE_MAX_PAGES, of + * stage-2 pages starting at @ipa, using at most one range operation per + * scale. The caller issues the barriers. + */ +static inline void tlbi_range_ipas2e1is(paddr_t ipa, unsigned long pages) +{ + int scale =3D TLBI_RANGE_MAX_SCALE; + + while ( pages > 1 && scale >=3D 0 ) + { + long num =3D TLBI_RANGE_NUM(pages, scale); + + if ( num >=3D 0 ) + { + uint64_t op =3D ((ipa >> PAGE_SHIFT) & 0x1fffffffffULL) | + ((uint64_t)num << 39) | + ((uint64_t)scale << 44) | + (1ULL << 46); + + tlbi_ripas2e1is(op); + + ipa +=3D (paddr_t)TLBI_RANGE_PAGES(num, scale) << PAGE_SHIFT; + pages -=3D TLBI_RANGE_PAGES(num, scale); + } + scale--; + } +} + /* * TLBI IPAS2E1IS: invalidate the stage-2 and combined entries of one IPA = of * the current VMID, in the inner-shareable domain. The caller issues the @@ -155,16 +219,42 @@ static inline void tlbi_vmalle1is(void) * Flush a range of IPA's mappings from the TLB of all processors in the * inner-shareable domain. * - * A range of TLBI_MAX_PAGE_OPS pages or more is flushed with the whole - * VMID. Each page of the range costs a broadcast TLBI, and the limit boun= ds - * their number like MAX_DVM_OPS does in Linux. It is not a tuned value. + * With FEAT_TLBIRANGE, ranges larger than TLBI_RANGE_MAX_PAGES are flushed + * with the whole VMID. Without it, a range of TLBI_MAX_PAGE_OPS pages or + * more uses the whole VMID; each smaller range costs one broadcast TLBI p= er + * page. These limits bound instruction count; neither is a tuned crossove= r. */ static inline void flush_guest_tlb_range_ipa(paddr_t ipa, unsigned long si= ze) { unsigned long pages =3D size >> PAGE_SHIFT; - paddr_t end =3D ipa + size; + bool range =3D cpus_have_const_cap(ARM64_HAS_TLBI_RANGE); + + if ( !pages ) + return; =20 - if ( pages >=3D TLBI_MAX_PAGE_OPS ) + if ( range ) + { + /* + * A range operation covers an even number of pages. Extend an odd + * range by one page, on the side that makes it 2 pages aligned: t= he + * neighbor is always inside the IPA space, and a page more is safe + * to invalidate. + */ + if ( (pages & 1UL) !=3D 0UL ) + { + if ( (ipa & PAGE_SIZE) !=3D 0UL ) + ipa -=3D PAGE_SIZE; + pages++; + } + + /* Same limit as Linux, one range operation per scale at most. */ + if ( pages > TLBI_RANGE_MAX_PAGES ) + { + flush_guest_tlb(); + return; + } + } + else if ( pages >=3D TLBI_MAX_PAGE_OPS ) { flush_guest_tlb(); return; @@ -175,10 +265,17 @@ static inline void flush_guest_tlb_range_ipa(paddr_t = ipa, unsigned long size) * stage 2 translations). */ dsb(ishst); /* Ensure prior page-table updates have completed */ - for ( ; ipa < end; ipa +=3D PAGE_SIZE ) + if ( range ) + tlbi_range_ipas2e1is(ipa, pages); + else { - /* Flush stage-2 TLBs for IPA address */ - tlbi_ipas2e1is(ipa); + paddr_t end =3D ipa + size; + + for ( ; ipa < end; ipa +=3D PAGE_SIZE ) + { + /* Flush stage-2 TLBs for IPA address */ + tlbi_ipas2e1is(ipa); + } } =20 /* diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/a= sm/cpufeature.h index 38e01aa94d..a4f7bcbe3e 100644 --- a/xen/arch/arm/include/asm/cpufeature.h +++ b/xen/arch/arm/include/asm/cpufeature.h @@ -78,8 +78,9 @@ #define ARM64_WORKAROUND_1508412 17 #define ARM_HAS_NTLBPA 18 #define ARM32_HAS_TLB_IPA 19 +#define ARM64_HAS_TLBI_RANGE 20 =20 -#define ARM_NCAPS 20 +#define ARM_NCAPS 21 =20 #ifndef __ASSEMBLER__ From nobody Mon Nov 23 22:31:30 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1795341307; cv=none; d=zohomail.com; s=zohoarc; b=lDK7G2kXWI6S6WL5dzJIcPGW2PyyUsgkE0fwemn+cqBpLR+uP8GJMA82Y5HJufLG6/tTcO4aIaEHnpGbNc7Mw/VJ4Qtr0SOm2CQNuoWoWQ0aUgpBEdm8UXX8ex7KQqYx3EOxFxNFj25ve8HpLMwwwjVqxI6gthP7Bvxqi7t9je4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795341307; 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=+06NjaglZq8mk0awRMY/bToi0Cqo9lQZF8+CVYtFmQE=; b=QFsekgdQ2g6/pigdrD+AqR/GrTtbYw6GxFIOGA6TgTruf93s1pxa/LqEPtQnutjC4+UbX676MyeX5p8wQZGdR8Ggdqna4m/oDgVn3LdAWgRTPwyLpZJNv8R7fXtdu5B6poSfxfEDE2PPH2n7G9YJm+S+K16ahcFE087AdqyFpds= 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 1795341307903170.67122722442514; Sun, 22 Nov 2026 01:55:07 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1499540.1696331 (Exim 4.92) (envelope-from ) id 1xV4HV-0005r8-N9; Sun, 22 Nov 2026 09:54:45 +0000 Received: by outflank-mailman (output) from mailman id 1499540.1696331; Sun, 22 Nov 2026 09:54:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HV-0005qs-KJ; Sun, 22 Nov 2026 09:54:45 +0000 Received: by outflank-mailman (input) for mailman id 1499540; Sun, 22 Nov 2026 09:54:44 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HU-0005ou-3e for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 09:54:44 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xV4HT-00ExLX-GR for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 10:54:43 +0100 Received: from [10.42.69.11] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b02bbe3-bab6-0a2a0a5309dd-0a2a450ba4b0-0 for ; Sun, 22 Nov 2026 10:54:43 +0100 Received: from [209.85.128.45] (helo=mail-wm1-f45.google.com) by tlsNG-42698a.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b02bbe3-b7e8-0a2a450b0019-d155802dc960-3 for ; Sun, 22 Nov 2026 10:54:43 +0100 Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4bb6057b333so8751125e9.3 for ; Sun, 22 Nov 2026 01:54:43 -0800 (PST) Received: from PKL-HASEEBA-LT.. ([39.36.59.78]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8849e598sm218409605e9.8.2026.11.22.01.54.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Nov 2026 01:54:42 -0800 (PST) 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" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795341283; x=1795946083; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+06NjaglZq8mk0awRMY/bToi0Cqo9lQZF8+CVYtFmQE=; b=omWGEhJfNifLZdfvPzDV0WWUucrknd6HuWCct2All5AyFNIKbbSKFgBdOFgCqbmUjG n5wYpCu8mu41Dw+GEp/34Tu3SdZfIruuXPsHOeJ09El/j9DKcQnWwm83fHlxm6hXcRUY NXBck0YElVYnQhYVppV17920XSa7ODnZKRqjwkdlHz3vMm6Ja8cN6urE/I3ezLvMTE8O cWXWvozx9UuoqoSW9bExpC7+PPHpo5PNCPmGc2fQVe1wYKgQ6ePMNIZAlrl1ve6oiIuE 37GLv0jDFlhSPQaR4S6T/A9Nr9Y+0wOK/iq9//TxzimsOi5nCSsQ4Cvfvw9dTKVi/JXo L8bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795341283; x=1795946083; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=+06NjaglZq8mk0awRMY/bToi0Cqo9lQZF8+CVYtFmQE=; b=zJb7iVO0BsUMygYMg5oW/Y5LkRTULpRSsr6OVBOEG4B6VRBinM+SrIzQ/yujtYvExY y+2fnrNhc4l2oKc5fGEEAmjTQ3aWfAFQDhgBu8oFH0sgEPkIY1LkzecWFJoTx+yB0Ppp ntauwa4dPw5yyXqJaM+YuCijzbohWAPbTLcK6XEJw4d5440xNcPBoa75IV/LpspMHvDH uVkHUiOJcaCuNPi59yiotUL6KuS4ETpl9wyly86L8y/UboLcGySu9kMnTYisaSn3Ab6a /QcA7Zk05vO/sZETrgivP3vcBkUXEzJgmztNt9g8eXe58jQU8QobJYrjbwx56Hp+ecSD VO0w== X-Gm-Message-State: APuU4Z9mqa74oyDAlJT0WBytgIkJ03UptU+9jyuTYuBQ04vNR/Auj4bQ IWq+u0OTlOHfY5KoooPyXRYTFSb183pDDFKVA2K7+NPse21ec1qOuHA77IehNMd7ET8= X-Gm-Gg: AZFdTZdyqbfgacPFNQqVzt36TckOzgtruXKtxl8K/bKOLNegMJWaBh2o5dNSnLin0dJ fzxqnWTRyCUIwB9QefQOS5mT4owQ4qs8ZYd20QImA2eqsNdbjAUiWfGGhCzP/AGHqOtucdHopTR 1PMv1QFd7oRHKaE04m8BTpxUMh+3j2adeGPZ/y/YXBNe1Ofd7VuOaqTVfKxVnuz/sLWpP4sKzDA ndBNgwz5qeIePk3dzvf0aaBPJH1yChqkrMdBIFahDE+h16djWQ1UYNZaEEoxc0X2frD5CWEZaVP ZdGi1eeRipP86mID0iKKwvTk87SvxlTf9mn4EdgMpcTH12NSqn/OOEpt23/bliPpIedKXflVIYi IoUxxPkas4Z3MaYtuY7WUGXt/22WDpuVoaBkNQl5FglbHy178+NNBEPT+K/QsFPtjgtwQgEYz8j zFRECLdP5HloeCy5jDIJctACN3dEkHi+sDL0qlHCf+o6Xz2as4VoTEWqforeVFG0i92HDEQWtDg hhO8YJIteh5kWz0MoDyCC4NzfewlWAumtcRazmdxhGcAyh1ttj+22blAhvGHcp9/xLAYsIA+4g6 zDLHBlhniJn8Dgnvv7VYorXJl5042OP0el/uOD4emL2mXCDe4BQZJsrT9TsXRuMeFHOHmnLMVLS zexIKv9UZLrYdAK6qVeRoIwT5zg== X-Received: by 2002:a05:600c:5303:b0:4a2:1a:a11d with SMTP id 5b1f17b1804b1-4bb88785d24mr169700165e9.0.1795341282825; Sun, 22 Nov 2026 01:54:42 -0800 (PST) From: Haseeb Ashraf To: xen-devel@lists.xenproject.org Cc: Haseeb Ashraf , sstabellini@kernel.org, julien@xen.org, bertrand.marquis@arm.com, michal.orzel@amd.com, Volodymyr_Babchuk@epam.com Subject: [XEN][PATCH v5 5/8] xen/arm: do not flush the TLBs when resolving a translation fault Date: Sun, 22 Nov 2026 14:54:19 +0500 Message-ID: <7a82afdf1e02b837a8ffc23e61dd68c5bda63265.1795336377.git.haseebashraf091@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-42698a/1795341283-AB2DC9EA-D9855835/0/0 X-purgate-type: clean X-purgate-size: 2214 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1795341308076048800 Content-Type: text/plain; charset="utf-8" From: Haseeb Ashraf After p2m_flush_vm() invalidates the root of the p2m, the first access to every table of the p2m faults. Then p2m_resolve_translation_fault() sets the valid bit of the entry and clears the valid bits of the table below it with p2m_invalidate_table(). That function asks for a TLB flush, so p2m_write_unlock() flushes all the TLBs of the domain, once for every table that is walked for the first time. The flush is not needed. The entry has had its valid bit unset since the TLBs were flushed after the invalidation, so no walk went through it and nothing from the table below can be cached. This does not depend on what Xen runs on. Under nested virtualization, a global broadcast can be especially expensive and hold the p2m lock while the fault is being resolved. No separate measurement is available for this patch. Keep the flush that was already pending, if any. Signed-off-by: Haseeb Ashraf --- New in v5. xen/arch/arm/mmu/p2m.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c index 3b1b9b394c..8cbd504fdc 100644 --- a/xen/arch/arm/mmu/p2m.c +++ b/xen/arch/arm/mmu/p2m.c @@ -1450,7 +1450,20 @@ bool p2m_resolve_translation_fault(struct domain *d,= gfn_t gfn) * If an entry is pointing to a block/page, no work to do for now. */ if ( lpae_is_table(entry, level) ) + { + /* + * The table below an entry whose valid bit is unset cannot be in = the + * TLBs: no walk went through that entry since the TLBs were flush= ed + * after the entry was invalidated. So clearing the valid bits of = the + * table does not need a flush, and p2m_invalidate_table() must not + * ask for one. It would be a flush of all the TLBs of the domain = at + * p2m_write_unlock(), for the first access to every table. + */ + bool need_flush =3D p2m->need_flush; + p2m_invalidate_table(p2m, lpae_get_mfn(entry)); + p2m->need_flush =3D need_flush; + } =20 /* * Now that the work on the entry is done, set the valid bit to prevent From nobody Mon Nov 23 22:31:30 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1795341309; cv=none; d=zohomail.com; s=zohoarc; b=N3ueg/qcdlQhm+Hpk2B4LOjYRMRSSbY4auiUBudR20FUni/s5TiPLUMQgX1aiGKjPolh9CeVCtPzjyAwI/u7YxSgOtuPC/v0LndZoHqzhl4b0XHazEFpvrkxkdCwVO9RpmC5fqyRfcG0yI5+1/HVt8RbeKHqAe33V7aPWp3kIeY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795341309; 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=uRWbLZw3/yEQ5PXPC7OpQnHirRM8EvYxh4g9KylG2S8=; b=EfuPProzEw/7PdkxcQpDgKKbwyBTs4+aql/IUAsnnO51ntRHOko400BASGRMTVeGqenwfewRF1PZ2D4Dk1meBxC+NHszPvTa83Xk5fQHD8O8N/xItf/d+oJoZ8TiyrLfpT1Y0xb7KEP+WbjVDAn5Dp2KZ4Q0dhNthMLO9RHaKE0= 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 179534130957081.93530660859676; Sun, 22 Nov 2026 01:55:09 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1499544.1696341 (Exim 4.92) (envelope-from ) id 1xV4HZ-0006Ab-7h; Sun, 22 Nov 2026 09:54:49 +0000 Received: by outflank-mailman (output) from mailman id 1499544.1696341; Sun, 22 Nov 2026 09:54:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HZ-0006AN-3U; Sun, 22 Nov 2026 09:54:49 +0000 Received: by outflank-mailman (input) for mailman id 1499544; Sun, 22 Nov 2026 09:54:47 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4HX-00066H-5o for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 09:54:47 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xV4HW-008zYS-Ib for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 10:54:46 +0100 Received: from [10.42.69.4] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b02bb91-2eae-0a2a0a5409dd-0a2a4504dd18-46 for ; Sun, 22 Nov 2026 10:54:46 +0100 Received: from [209.85.128.51] (helo=mail-wm1-f51.google.com) by tlsNG-ebf023.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b02bbe6-b57f-0a2a45040019-d1558033c164-3 for ; Sun, 22 Nov 2026 10:54:46 +0100 Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4bb606491faso12391375e9.3 for ; Sun, 22 Nov 2026 01:54:46 -0800 (PST) Received: from PKL-HASEEBA-LT.. ([39.36.59.78]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8849e598sm218409605e9.8.2026.11.22.01.54.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Nov 2026 01:54:45 -0800 (PST) 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" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795341286; x=1795946086; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uRWbLZw3/yEQ5PXPC7OpQnHirRM8EvYxh4g9KylG2S8=; b=OW2+reAWCeotFmyMeKdQhb03XTSIA2kgaY3KfiVWtzi+1QRtbKNv+OXXVurq4MN2Po Z3nMNMNNDChBInZCPsdxtlakEygID5StDUiOXmuin5OEergmCNX4VX0bo1A/t8yMJ5j8 NdqVPriIBJmZUr5NiSPDA5EGRMd4iGwuA5YL2sFlMxqKyLaLStycWj8/h6pcpZzl/ghP 97dyr8vPI4LdY8gTtLA89inGdA+nqmZxEy80RP/08Jvox5MtPR0yoKKfCm9F8K/xifVN Bl3OW7bYHx0gDlAXopazZ7FHEaPsfzIBf+tMfgEA5pEzkRSh9vlx1I5u7b99TBczC3DX FLew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795341286; x=1795946086; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=uRWbLZw3/yEQ5PXPC7OpQnHirRM8EvYxh4g9KylG2S8=; b=r5gQ7xdeG33uTj0l2lKbLL/SM/pLp4RX9G7Tux6jCIxZoTIBofFf79zDnSfQ/xKEes TV1ZYHRpy3Nsl4GDYaFyVa/s4IYplSJ/YCNegGdzhC7/+TJK5GbPmR8Mm5on46gs7fuB mbbd6IMzFl7qEKz+lkzj6ibHd1cgO3Nw9P74BOrs6uZ0g7oh2IntNKxfK4+ov8qFcXWS I0RuzVo7Uya6zL3YmuIMQNFW+GQl0fDLVdnU1qYN4AFBvvVQ+Hygfhe0Y/LtKDQkVta6 bjbAUBHH7dFENKscpT35n0H3DOVnBpW0TXw7Owq4KjSTKJ2WkXSLI6UXOXWp6JBxIUP4 IVYQ== X-Gm-Message-State: APuU4Z+EA2rIIqlIJ/Id7JNnQONqqPkpHAo4dufbFT39FTi60cCBn+IP yOj1SaG8+OC2NQpU0AzGt7z/P3TOEqdtlIC8TSBGWb1ZhROyKBUZUyjkmvuaDy+Pzgg= X-Gm-Gg: AZFdTZedVX4aRVFGpbAhQezTBO4/EfGJf2fsOjXOOMzo5JKU7xWZJ5S2xVXmYb4mNsM Cp0JziU6DiIzgPQtmzclfI6otm9K8mZxCMoAPOxbEBx+NSWPuBtulCdyt8c45Kn6mMUdWbPpm+8 6bSR64c9qLkWOqu1/C1kRZxCXKnQLN2DRWmGEGYO6FyDmztBl0N6RUSzpTMF2tRNjwBQuUe4Zuy yRMpzinLCNqxQSUqMZjotTsjekij8hbzgM6Xuql09+i2LZWToD4Wwuwx0PXB5ezQjZ9tzXG4OFl 7rzvUGiN2d3bF+7OiOaTRSyeC/7zLQXRNGwtJYLOkiKs3qAeWRbrCjoQFDhMezwstAR65VC2XzV FJOfN16t4b5MhwqdUnmSNSx373RB6HTHlL5JAU9/HmFyHn1thqwFAkuLoywpUf5/OQpemphQQtS TrstBKyTkMgUzL8HpPEsjf//NF5rmPE21lXHW+IY5H7200HVUIsw716uU4D0j6m9o1/bXaIPNIH chZlpiiZmMBUeNCkGPEMPrVlAImxoP6lN5eZ3NCYCL0xvFf+AzPCg6ZHThvb8bOdak1GGi/5TfM j7V8iiocygWc5tjsOPu5ShCKZCV+nSF8v2xWmA1/lb3urMut3FDM72IDtnEMM/hWh67xbENVW1D QEWJfnPWMvHH+t5xiVAXtQmzw X-Received: by 2002:a05:600c:674b:b0:4bb:6847:791f with SMTP id 5b1f17b1804b1-4bb8880f267mr170364285e9.7.1795341285907; Sun, 22 Nov 2026 01:54:45 -0800 (PST) From: Haseeb Ashraf To: xen-devel@lists.xenproject.org Cc: Haseeb Ashraf , sstabellini@kernel.org, julien@xen.org, bertrand.marquis@arm.com, michal.orzel@amd.com, Volodymyr_Babchuk@epam.com, andrew.cooper3@citrix.com, anthony.perard@vates.tech, jbeulich@suse.com, roger@xenproject.org Subject: [XEN][PATCH v5 6/8] xen/arm: batch stage-2 removal with range flushes Date: Sun, 22 Nov 2026 14:54:20 +0500 Message-ID: <4dbfc8f1332054d9883d2b6632741f5ee99aa6d1.1795336377.git.haseebashraf091@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-ebf023/1795341286-C04DBB50-26F9318F/0/0 X-purgate-type: clean X-purgate-size: 8123 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1795341310156048800 Content-Type: text/plain; charset="utf-8" From: Haseeb Ashraf Removing pages from a guest p2m requires TLB invalidation. The cost is particularly high when Xen runs under another hypervisor, where each invalidation can trap to the outer hypervisor. Add guest_physmap_remove_page_batch() to remove a list of pages under a single p2m write lock and invalidate each contiguous run of changed GFNs with one range-flush call. The range helper selects the TLBI method and uses a full-VMID flush when a range exceeds the supported limit. Patch 7 adds the public hypercall that calls this helper. An entry is checked at removal time, so a duplicate or missing GFN is reported as -ENOENT, like single-page removal. Entries that were not changed are excluded from invalidation; a removal that fails after changing an entry is included. The caller must hold a reference on each page until the invalidation is complete. HAS_PHYSMAP_REMOVE_BATCH is selected for Arm MMU only; the MPU p2m has no TLB invalidation by range. Linux reference: - KVM: arm64: Use TLBI range-based instructions for unmap, which similarly defers the stage-2 invalidation and coalesces it into ranges: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?= id=3D7657ea920c54218f123ddc1b572821695b669c13 Signed-off-by: Haseeb Ashraf --- New in v5. xen/arch/arm/Kconfig | 1 + xen/arch/arm/p2m.c | 97 ++++++++++++++++++++++++++++++++---- xen/common/Kconfig | 3 ++ xen/include/xen/p2m-common.h | 16 ++++++ 4 files changed, 106 insertions(+), 11 deletions(-) diff --git a/xen/arch/arm/Kconfig b/xen/arch/arm/Kconfig index b9c5c40678..7ad676940b 100644 --- a/xen/arch/arm/Kconfig +++ b/xen/arch/arm/Kconfig @@ -85,6 +85,7 @@ config MMU bool "MMU" select ARCH_PAGING_MEMPOOL select HAS_LLC_COLORING if !NUMA && ARM_64 + select HAS_PHYSMAP_REMOVE_BATCH select HAS_PMAP select HAS_VMAP select HAS_PASSTHROUGH diff --git a/xen/arch/arm/p2m.c b/xen/arch/arm/p2m.c index a39351ede9..c4f1f3709c 100644 --- a/xen/arch/arm/p2m.c +++ b/xen/arch/arm/p2m.c @@ -123,16 +123,18 @@ int p2m_insert_mapping(struct domain *d, gfn_t start_= gfn, unsigned long nr, return rc; } =20 -static inline int p2m_remove_mapping(struct domain *d, +/* + * Remove [start_gfn, start_gfn + nr) from the p2m. The caller must hold t= he + * p2m write lock and is responsible for flushing any removed entries. + */ +static int p2m_remove_mapping_locked(struct p2m_domain *p2m, gfn_t start_gfn, unsigned long nr, mfn_t mfn) { - struct p2m_domain *p2m =3D p2m_get_hostp2m(d); unsigned long i; - int rc; =20 - p2m_write_lock(p2m); + ASSERT(p2m_is_write_locked(p2m)); =20 /* * Before removing the GFN - MFN mapping for any RAM pages make sure @@ -150,17 +152,27 @@ static inline int p2m_remove_mapping(struct domain *d, =20 if ( p2m_is_any_ram(t) && (!mfn_valid(mfn) || !mfn_eq(mfn_add(mfn, i), mfn_return)) ) - { - rc =3D -EILSEQ; - goto out; - } + return -EILSEQ; =20 i +=3D (1UL << cur_order) - ((gfn_x(start_gfn) + i) & ((1UL << cur_order) - 1)); } =20 - rc =3D p2m_set_entry(p2m, start_gfn, nr, INVALID_MFN, - p2m_invalid, p2m_access_rwx); + return p2m_set_entry(p2m, start_gfn, nr, INVALID_MFN, + p2m_invalid, p2m_access_rwx); +} + +static inline int p2m_remove_mapping(struct domain *d, + gfn_t start_gfn, + unsigned long nr, + mfn_t mfn) +{ + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + int rc; + + p2m_write_lock(p2m); + + rc =3D p2m_remove_mapping_locked(p2m, start_gfn, nr, mfn); =20 /* * The range that may have changed is known. Invalidate it instead of @@ -175,12 +187,75 @@ static inline int p2m_remove_mapping(struct domain *d, p2m->need_flush =3D false; } =20 - out: p2m_write_unlock(p2m); =20 return rc; } =20 +#ifdef CONFIG_HAS_PHYSMAP_REMOVE_BATCH +/* Whether the removal of an entry may have changed the p2m. */ +static bool removal_touched_p2m(int err) +{ + /* These are returned before the p2m is modified. */ + return err !=3D -ENOENT && err !=3D -EILSEQ; +} + +void guest_physmap_remove_page_batch(struct domain *d, + const xen_pfn_t *gpfns, + const mfn_t *mfns, + int *errs, + unsigned int count) +{ + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + unsigned int i; + + p2m_write_lock(p2m); + + /* A flush that is still pending covers more than this batch. */ + p2m_tlb_flush_sync(p2m); + + /* __p2m_set_entry() only sets p2m->need_flush for a removed entry. */ + for ( i =3D 0; i < count; i++ ) + { + if ( mfn_eq(mfns[i], INVALID_MFN) || + mfn_eq(p2m_get_entry(p2m, _gfn(gpfns[i]), NULL, NULL, NULL, N= ULL), + INVALID_MFN) ) + errs[i] =3D -ENOENT; + else + errs[i] =3D p2m_remove_mapping_locked(p2m, _gfn(gpfns[i]), 1, + mfns[i]); + } + + /* + * Invalidate each run of consecutive entries that may have changed. + * The range helper selects the TLBI method and falls back to a full-V= MID + * flush for ranges beyond its per-CPU limit. Include an entry whose + * removal failed after changing the p2m. + */ + if ( p2m->need_flush ) + { + for ( i =3D 0; i < count; i++ ) + { + unsigned int first =3D i; + + if ( !removal_touched_p2m(errs[i]) ) + continue; + + while ( i + 1 < count && removal_touched_p2m(errs[i + 1]) && + gpfns[i + 1] =3D=3D gpfns[i] + 1 ) + i++; + + p2m_force_tlb_flush_range_sync(p2m, pfn_to_paddr(gpfns[first]), + i - first + 1); + } + + p2m->need_flush =3D false; + } + + p2m_write_unlock(p2m); +} +#endif /* CONFIG_HAS_PHYSMAP_REMOVE_BATCH */ + int map_regions_p2mt(struct domain *d, gfn_t gfn, unsigned long nr, diff --git a/xen/common/Kconfig b/xen/common/Kconfig index a619f2cef1..e687b59e8f 100644 --- a/xen/common/Kconfig +++ b/xen/common/Kconfig @@ -152,6 +152,9 @@ config HAS_KEXEC config HAS_LLC_COLORING bool =20 +config HAS_PHYSMAP_REMOVE_BATCH + bool + config HAS_PIRQ bool =20 diff --git a/xen/include/xen/p2m-common.h b/xen/include/xen/p2m-common.h index 0eb0619912..7a78c8b421 100644 --- a/xen/include/xen/p2m-common.h +++ b/xen/include/xen/p2m-common.h @@ -12,6 +12,22 @@ int __must_check guest_physmap_remove_page(struct domain *d, gfn_t gfn, mfn_t mfn, unsigned int page_order); =20 +#ifdef CONFIG_HAS_PHYSMAP_REMOVE_BATCH +/* + * Remove a batch of pages from a domain's p2m table. + * + * gpfns[] lists the frames. mfns[] holds, for each page the caller holds a + * reference on, its frame, or INVALID_MFN if there is no page. errs[] + * receives a result per entry. All entries are removed under one p2m write + * lock; changed contiguous runs are invalidated after processing the batc= h. + */ +void guest_physmap_remove_page_batch(struct domain *d, + const xen_pfn_t *gpfns, + const mfn_t *mfns, + int *errs, + unsigned int count); +#endif + /* Map MMIO regions in the p2m: start_gfn and nr describe the range in * * the guest physical address space to map, starting from the machine * * frame number mfn. */ From nobody Mon Nov 23 22:31:30 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1795341326; cv=none; d=zohomail.com; s=zohoarc; b=LCvw9jrnGMpZLZzIxRbb5zDf7Mziyur9QzWN51V9d+oAZjmZAHpUJP/8G+HQwdcC9AqVB5ilIfbLHF1T8KVaw2udnxp1ubKOGusTNV6L9cibmF6w8MOXx+iNTEF2USWtLE/klPTln5Ao8awNVewTVKdc/22QcKpjXUobJ7Tutkk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795341326; 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=GjkumpSVhpOXhxhIrkLoN9WgteeDVe8/g1zeEGiUhXo=; b=L8Qn/0xKATKzLMespDNHlQHKEpl/Ro5hPBBntr9dRg1FNmDaHgtedS+Eh8o6RkpPaj2SvgzIvOfKcGza6CtMejNrh0KbUgVj+2VHqw9urhg4QWfYqYfOAQwXzsMvyzWwIC3dQe/zsX8ZgbOF9iob9lTSjdiK7cqdGSK5/OIK+1I= 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 1795341326922460.1933107126831; Sun, 22 Nov 2026 01:55:26 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1499546.1696350 (Exim 4.92) (envelope-from ) id 1xV4Hc-0006TU-EG; Sun, 22 Nov 2026 09:54:52 +0000 Received: by outflank-mailman (output) from mailman id 1499546.1696350; Sun, 22 Nov 2026 09:54:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4Hc-0006TG-AP; Sun, 22 Nov 2026 09:54:52 +0000 Received: by outflank-mailman (input) for mailman id 1499546; Sun, 22 Nov 2026 09:54:50 +0000 Received: from mx.expurgate.net ([194.145.224.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4Ha-0006Pm-Nu for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 09:54:50 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xV4Ha-00G0sz-4S for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 10:54:50 +0100 Received: from [10.42.69.2] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b02bb9e-e002-0a2a0a5209dd-0a2a450293f2-30 for ; Sun, 22 Nov 2026 10:54:50 +0100 Received: from [209.85.128.48] (helo=mail-wm1-f48.google.com) by tlsNG-720697.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b02bbe9-4900-0a2a45020019-d1558030cce6-3 for ; Sun, 22 Nov 2026 10:54:50 +0100 Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4a1fbd52ab6so15736085e9.0 for ; Sun, 22 Nov 2026 01:54:50 -0800 (PST) Received: from PKL-HASEEBA-LT.. ([39.36.59.78]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8849e598sm218409605e9.8.2026.11.22.01.54.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Nov 2026 01:54:49 -0800 (PST) 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" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795341289; x=1795946089; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=GjkumpSVhpOXhxhIrkLoN9WgteeDVe8/g1zeEGiUhXo=; b=AX6LX7PRnqgyx++sySJhXo9ZLD/sjiDVVmRSqfNN8+LZl7rLngkMaWdOdasmQAdFeU gElzJkRXZrYFKkuHFaq5DrVCuS84LOehY7xbw9hCMiplxVtcUFVeZtiqOceXUoyJkxQ+ lIBjM5gvFB8Acel5+Y1GjLtvcXfhgtQiK8d0nNnWfa6Q2fY4VZEdCtAbHyj2K5VRMm4a jYvp+6SGfD68zKHh/woxslawfgbzUElRXIDyiOdfBLHlJAd9AWf7wa3nxuMzIjxjptP9 scqBq886L/Aqs7Gbe44JMrogOOXj9cbWicQviKEPhPQCdq1qF0TI3bduMNNE+HyktZ25 HmHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795341289; x=1795946089; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=GjkumpSVhpOXhxhIrkLoN9WgteeDVe8/g1zeEGiUhXo=; b=NqTcYnT7EmrOsccE5tgmf9zMUs/jL4EIntw5B7X8lu8Hf2IssNt7WbN/QbmyIA4O6S O3nt3bmoRPxM2q1EejD01mgpQ3F/I6pz5AmHvjCxNlvtsdaQNX3pwq/AYRs7U1mbCKtg j0AV9WUN7/JLtIKvhSR9Vh+pKCMYe+WgGFe8sw9bI/4I0RKcwlBYF/ux1NCfMzZQ+YRd WkrnJ6Yxv3d3Rko+Ym9OE371KLoPaSvmxnRpIA4Rc1IMPbCFJO8kx0DNet+wugR7+hHC RPbr0vP0DuJPFULL5hglgTTaCdLcI2mfF0YCKnIhGWCm1uoZeqDhpgwK7WE7V6eoQ6BK X7MA== X-Gm-Message-State: APuU4Z8f7FE8qeuK/0WPktT204rXUH3+Kl4/wa5gD+Ce19b3qF7e6vl5 7NCDZ81jD8HDrcCw5HROz5gTtdtKeSVqNXW90SvVZ/9f1ndMqW5rqu+CUrJB8coLNRc= X-Gm-Gg: AZFdTZeoEv19ut34dUQWAsRtaHtZlT0/i0KL9Sf8EFk+z1h9dxGMEyqh8cENz9CsB1Q VnrV9kgqWrFxnZiRbRkxhCKZ41s2pNN/Fb32mrYz5HweRUaH8wImGJBx65SA7oa/Q98UT02AqFk qZu+0oLfV7rNCC2b7XDmvtGDgLNjmHMk7hYfVBvYCl6p4ngeKyzGhIm1bsIJYLpa3jrxENJ2dfU 3tXPu4GxYt0dSnKNgKrXZYE41ToNTGbSKlDWWkZeHWXCfLibMhJLe16yOHhkma+G4Z1T0mhXVmH wu1h2plj33GwitQmsWQk0mRKowSsBwSFsbfAseX4gYzVTXtnCIyRZpMRdMuKzetOscpDVC80+va GTe5hFsvGCo/x3aYOL2yz3YmTgAho4rSwMk8/wY34Rjks7U06MAN4BNoZHw7oGiIh/46/z1zV/q Z4lhlosiP4V8m8t3fjzjmKyw2xr4UGY/97pg6Ioqp0jnE7Y1gfm+5bPA9xdNraD94dMJ7ucxSTn uuNPacPIvoI/d5Lm17y4P4S1DkCf5f1fpYAOwO0U+O3IAtI8sj37XcZAvOTxBIosBiZY2PKCMdn 40JpqvlDbBWlE25vM9MMCW1E3e4P2Esamz6rQEeI9TiawjvL0+Cw6X+V8qJXmbPXYDr5g+59u+G HQVaRaGxNrfnRbLYVpPGxCvGc X-Received: by 2002:a05:600c:4e87:b0:4a7:6bc7:f778 with SMTP id 5b1f17b1804b1-4bb88938d38mr186443365e9.30.1795341289432; Sun, 22 Nov 2026 01:54:49 -0800 (PST) From: Haseeb Ashraf To: xen-devel@lists.xenproject.org Cc: Haseeb Ashraf , oleksii.kurochko@gmail.com, community.manager@xenproject.org, andrew.cooper3@citrix.com, anthony.perard@vates.tech, michal.orzel@amd.com, jbeulich@suse.com, julien@xen.org, roger@xenproject.org, sstabellini@kernel.org, dpsmith@apertussolutions.com Subject: [XEN][PATCH v5 7/8] xen/memory: add XENMEM_remove_from_physmap_batch Date: Sun, 22 Nov 2026 14:54:21 +0500 Message-ID: <2edf2a0cf5b1b8c29648f47331ea7bac9aee0600.1795336377.git.haseebashraf091@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-720697/1795341290-32D906C6-F177A82D/0/0 X-purgate-type: clean X-purgate-size: 9738 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1795341328168048800 Content-Type: text/plain; charset="utf-8" From: Haseeb Ashraf Add a batched hypercall to remove pages from a domain's physical address space, like XENMEM_add_to_physmap_batch does for adding. Each entry gets the result of XENMEM_remove_from_physmap. The p2m helper invalidates contiguous runs once per chunk of 256 entries, using the TLBI method supported by the CPU and range size. Stage guest handles into per-hypercall buffers because they cannot be accessed while holding the p2m lock and the target may be the caller. Take a reference on each page before removal so it remains allocated until TLB invalidation completes. Copy each chunk's results back, and allow the hypercall to be preempted between chunks. The staging buffers are allocated and not placed on the stack: 256 entries need 28 bytes each (7 KiB), a large part of the 32 KiB Arm stack. Each buffer stays within PAGE_SIZE, so xvmalloc_array() uses the xmalloc pool. A vmalloc area would be unmapped when it is freed, which flushes Xen's own TLB. The chunk also bounds the p2m lock hold time and the invalidation work between preemption points. This is a resource bound, not a measured throughput threshold. Only architectures that select HAS_PHYSMAP_REMOVE_BATCH implement this hypercall; other architectures return -ENOSYS and callers can fall back to XENMEM_remove_from_physmap. The XSM check is the same as for the single-page operation. No in-tree caller is added; the hypercall is meant for a Dom0 kernel or toolstack that unmaps many guest pages. Signed-off-by: Haseeb Ashraf --- New in v5. CHANGELOG.md | 5 ++ xen/common/memory.c | 152 ++++++++++++++++++++++++++++++++++++ xen/include/public/memory.h | 35 +++++++++ 3 files changed, 192 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 225397f128..39b3d16928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,11 @@ The format is based on [Keep a Changelog](https://keepac= hangelog.com/en/1.0.0/) afterwards. =20 ### Added + - On Arm: + - XENMEM_remove_from_physmap_batch, which removes several pages from the + physical address space of a domain with one hypercall and a TLB + invalidation per run of pages, instead of one hypercall and one + invalidation per page. Other architectures return -ENOSYS for it. =20 ### Removed - On Arm: diff --git a/xen/common/memory.c b/xen/common/memory.c index 4afa2271b7..b5256bf6ec 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1147,6 +1147,117 @@ static int xenmem_add_to_physmap_batch(struct domai= n *d, return 0; } =20 +#ifdef CONFIG_HAS_PHYSMAP_REMOVE_BATCH +/* + * Number of entries removed under one p2m write lock and TLB invalidation. + * Entries are staged in per hypercall buffers because the guest handles + * cannot be accessed with the p2m lock held: the target may be the caller. + * The buffers stay below PAGE_SIZE so that they come from the xmalloc poo= l, + * a vmalloc area is unmapped, and flushed from the TLBs, when it is freed. + */ +#define XENMEM_RFPB_CHUNK 256U + +static int xenmem_remove_from_physmap_batch( + struct domain *d, struct xen_remove_from_physmap_batch *xrfpb, + unsigned int extent) +{ + struct page_info **pages =3D NULL; + xen_pfn_t *gpfns =3D NULL; + mfn_t *mfns =3D NULL; + int *errs =3D NULL; + unsigned int chunk, i; + int rc =3D 0; + + BUILD_BUG_ON(XENMEM_RFPB_CHUNK * sizeof(*pages) > PAGE_SIZE); + BUILD_BUG_ON(XENMEM_RFPB_CHUNK * sizeof(*gpfns) > PAGE_SIZE); + BUILD_BUG_ON(XENMEM_RFPB_CHUNK * sizeof(*mfns) > PAGE_SIZE); + + if ( unlikely(xrfpb->size < extent) ) + return -EILSEQ; + + if ( unlikely(xrfpb->size =3D=3D extent) ) + return extent ? -EILSEQ : 0; + + if ( !guest_handle_subrange_okay(xrfpb->gpfns, extent, xrfpb->size - 1= ) || + !guest_handle_subrange_okay(xrfpb->errs, extent, xrfpb->size - 1)= ) + return -EFAULT; + + chunk =3D min_t(unsigned int, xrfpb->size - extent, XENMEM_RFPB_CHUNK); + + pages =3D xvmalloc_array(struct page_info *, chunk); + gpfns =3D xvmalloc_array(xen_pfn_t, chunk); + mfns =3D xvmalloc_array(mfn_t, chunk); + errs =3D xvmalloc_array(int, chunk); + if ( !pages || !gpfns || !mfns || !errs ) + { + rc =3D -ENOMEM; + goto out; + } + + while ( xrfpb->size > extent ) + { + unsigned int n =3D min_t(unsigned int, xrfpb->size - extent, chunk= ); + + if ( __copy_from_guest_offset(gpfns, xrfpb->gpfns, extent, n) ) + { + rc =3D -EFAULT; + goto out; + } + + /* + * Take a reference on each page first: this needs the p2m read + * lock, which the removal below holds for writing. The references + * also keep the pages from being freed before the TLBs are flushe= d. + * A frame without a page is reported as XENMEM_remove_from_physmap + * does. + */ + for ( i =3D 0; i < n; i++ ) + { + pages[i] =3D get_page_from_gfn(d, gpfns[i], NULL, P2M_ALLOC); + if ( pages[i] !=3D NULL ) + { + mfns[i] =3D page_to_mfn(pages[i]); + errs[i] =3D 0; + } + else + { + mfns[i] =3D INVALID_MFN; + errs[i] =3D -ENOENT; + } + } + + guest_physmap_remove_page_batch(d, gpfns, mfns, errs, n); + + for ( i =3D 0; i < n; i++ ) + if ( pages[i] !=3D NULL ) + put_page(pages[i]); + + if ( __copy_to_guest_offset(xrfpb->errs, extent, errs, n) ) + { + rc =3D -EFAULT; + goto out; + } + + extent +=3D n; + + /* Ask for a continuation if there is more work to do. */ + if ( xrfpb->size > extent && hypercall_preempt_check() ) + { + rc =3D extent; + goto out; + } + } + + out: + XVFREE(errs); + XVFREE(mfns); + XVFREE(gpfns); + XVFREE(pages); + + return rc; +} +#endif /* CONFIG_HAS_PHYSMAP_REMOVE_BATCH */ + static int construct_memop_from_reservation( const struct xen_memory_reservation *r, struct memop_args *a) @@ -1787,6 +1898,47 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDL= E_PARAM(void) arg) break; } =20 +#ifdef CONFIG_HAS_PHYSMAP_REMOVE_BATCH + case XENMEM_remove_from_physmap_batch: + { + struct xen_remove_from_physmap_batch xrfpb; + + BUILD_BUG_ON((typeof(xrfpb.size))-1 > + (UINT_MAX >> MEMOP_EXTENT_SHIFT)); + + /* Check for malicious or buggy input. */ + if ( start_extent !=3D (typeof(xrfpb.size))start_extent ) + return -EDOM; + + if ( copy_from_guest(&xrfpb, arg, 1) ) + return -EFAULT; + + d =3D rcu_lock_domain_by_any_id(xrfpb.domid); + if ( d =3D=3D NULL ) + return -ESRCH; + + rc =3D paging_mode_translate(d) + ? xsm_remove_from_physmap(XSM_TARGET, curr_d, d) + : -EACCES; + if ( rc !=3D 0 ) + { + rcu_unlock_domain(d); + return rc; + } + + rc =3D xenmem_remove_from_physmap_batch(d, &xrfpb, start_extent); + + rcu_unlock_domain(d); + + if ( rc > 0 ) + rc =3D hypercall_create_continuation( + __HYPERVISOR_memory_op, "lh", + op | (rc << MEMOP_EXTENT_SHIFT), arg); + + return rc; + } +#endif /* CONFIG_HAS_PHYSMAP_REMOVE_BATCH */ + case XENMEM_access_op: rc =3D mem_access_memop(cmd, guest_handle_cast(arg, xen_mem_access= _op_t)); break; diff --git a/xen/include/public/memory.h b/xen/include/public/memory.h index bd9fc37b52..4de16cd59c 100644 --- a/xen/include/public/memory.h +++ b/xen/include/public/memory.h @@ -302,6 +302,41 @@ struct xen_remove_from_physmap { typedef struct xen_remove_from_physmap xen_remove_from_physmap_t; DEFINE_XEN_GUEST_HANDLE(xen_remove_from_physmap_t); =20 +/* + * A batched version of XENMEM_remove_from_physmap: remove several pages f= rom + * the guest physical address space of a domain with a single hypercall. + * Each entry gets the result XENMEM_remove_from_physmap would have return= ed. + * The TLBs are invalidated once per run of consecutive pages in a chunk of + * entries the hypervisor processes at a time, so this is much cheaper than + * one XENMEM_remove_from_physmap per page, especially for a contiguous ra= nge. + * + * The return value is 0 if the list was processed, and then errs[] holds = the + * per entry results, or a negative error. The hypercall returns -ENOSYS w= here + * it is not implemented, callers must fall back to XENMEM_remove_from_phy= smap. + * Processing is not transactional: a negative return may follow completed + * removals, including a chunk whose result copy failed. + * + * arg =3D=3D addr of xen_remove_from_physmap_batch_t. + */ +#define XENMEM_remove_from_physmap_batch 29 +struct xen_remove_from_physmap_batch { + /* IN */ + /* Which domain to change the mapping for. */ + domid_t domid; + + /* Number of pages to unmap. */ + uint16_t size; + + /* GPFNs of the current mappings of the pages. */ + XEN_GUEST_HANDLE(xen_pfn_t) gpfns; + + /* OUT */ + /* Per entry error code, 0 on success. */ + XEN_GUEST_HANDLE(int) errs; +}; +typedef struct xen_remove_from_physmap_batch xen_remove_from_physmap_batch= _t; +DEFINE_XEN_GUEST_HANDLE(xen_remove_from_physmap_batch_t); + /*** REMOVED ***/ /*#define XENMEM_translate_gpfn_list 8*/ From nobody Mon Nov 23 22:31:30 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1795341311; cv=none; d=zohomail.com; s=zohoarc; b=UZJGWcb0bAVWvPzIg95kcxrSNjZTVesQZoe1koj/rhf5J0IQ5UtxnAyN9BEohq+siwK6E0JhUCysKYxwyrlSTjprpZveAs2gW+//acHa4u2kiHk0XtMguIM6OM/wS+KmBAKeU5HS1Za97hfHZwOjzb2Iv/Rk2NZ7PiLS78e+/0Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795341311; 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=e6trpaEtWAD6+j1RHGJjI0LLsXCCawx/gvwKZrYkknU=; b=asw5Fv2QgVfJknB1fQyv+01LIWJRZ6p0BsoIheBrokoVSTPbKId6iT2FEShAKa33uXv31sLs5Yp2D2QT0wiFfQLYMwMI1O97wO6xALPkdOuG9QidpNKhoAuyToCT5OsuEghAlPArwkmQNRAhzgnV7VrZ7LB6cR/YLPcP254kgQ0= 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 1795341311935999.1691359145672; Sun, 22 Nov 2026 01:55:11 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1499548.1696359 (Exim 4.92) (envelope-from ) id 1xV4He-0006ne-Oj; Sun, 22 Nov 2026 09:54:54 +0000 Received: by outflank-mailman (output) from mailman id 1499548.1696359; Sun, 22 Nov 2026 09:54:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4He-0006nS-Jm; Sun, 22 Nov 2026 09:54:54 +0000 Received: by outflank-mailman (input) for mailman id 1499548; Sun, 22 Nov 2026 09:54:53 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xV4Hd-0006fy-Hs for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 09:54:53 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xV4Hc-00ExLX-UG for xen-devel@lists.xenproject.org; Sun, 22 Nov 2026 10:54:52 +0100 Received: from [10.42.69.5] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b02bbc6-bab6-0a2a0a5309dd-0a2a4505c3ae-30 for ; Sun, 22 Nov 2026 10:54:52 +0100 Received: from [209.85.128.47] (helo=mail-wm1-f47.google.com) by tlsNG-c201ff.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b02bbec-4cb1-0a2a45050019-d155802fcd15-3 for ; Sun, 22 Nov 2026 10:54:52 +0100 Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4bb64ef5a14so24335425e9.1 for ; Sun, 22 Nov 2026 01:54:52 -0800 (PST) Received: from PKL-HASEEBA-LT.. ([39.36.59.78]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8849e598sm218409605e9.8.2026.11.22.01.54.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Nov 2026 01:54:51 -0800 (PST) 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" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795341292; x=1795946092; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=e6trpaEtWAD6+j1RHGJjI0LLsXCCawx/gvwKZrYkknU=; b=Vom/WIceQXbV/LJtzdAvTSt0fH4NsHMcyrv8oq1FWHnc0iGt9N82xSNaTOSB5SoYVU JT9lja5Jn2esyWskj6bH6WwZxlLn6sgioAeXrohAZdeqWZ4q6kW7c1G0ed7tagrBUcnH Bb6D38DjoWSFcn82BCIpGB7gLPSrHSKqb/AywX0gSjaGnGzY5bQWOe3J5RQsmn2NDuhK Y7dPbdWPHVqJzkqYiC2rQKlM2o2Zck5uaYW0CQC/dyYhPuKCO/CwrlOvMGl1Pq2vjAkS VjoSMQVvM+DvNtAgcPzpLpOuRV9v77tDmaiwnNZy8Iso/BhT/AVqwd/00yxZ1ES19Lii WKzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795341292; x=1795946092; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=e6trpaEtWAD6+j1RHGJjI0LLsXCCawx/gvwKZrYkknU=; b=l7TB1SLOq2fG+q3a8GosGzMuC35H26IY12jGfdPn03KTlpKJPsbqkK23JItW9VYVq7 PoY92ZVgpa+4xh+zq0mYqO/U+jlfUeXutXHp+e4OyNOkQ4Y6k5rnEnPafVDr5Zycdq3H XPlFG0UwJZS6fe3oPp9QPvR8fFfC/XT/NCzp4kePwkOper7YddrgUvctD2CuWm4WkACI 8X9FcggA4XrSSGfghxFvwaIzHTKZ+GO7T1dBl4nGkxL9ADsVFqg2ZqxyVGZyoMaQ2vqU kDLjo286L6McU/knpXf54x4O0I82kyGBJFZ77uv75cWqZ7VijON95fa1Qn0XhPMY7t7O mJPA== X-Gm-Message-State: APuU4Z95R8oXJeS0jVrkJp8L9lN4I02rm/VefYlTzTv4qz9hiNNAGA6l 9HdGHzpXOMxOI/pGVyDiSZ7uSSPxKhS/bLaCGteuHl61Y3dd0xP+yy6atbwSp5yB3eg= X-Gm-Gg: AZFdTZdRjbFroq1tXPKJwraU6YcK8gUvOulFGHZmzZkWvl5VH5gde0oEfecN6NcZN8s /b+qNQK5r2SYOtKJRzV4TjY6UZgIkjHbyctN1T+0qGbVTsWrk5Zv+ut9VWuzcLt21twSHO/9RCb Jv5Lzt2o6U1dycwtVvvY8MuOIX2Nhr1/qYBfYYys3I27ftJwh1WnI3s7k6/997mAgZ1EPkitiSG u3xu2rQThz6TrqQm3ilf6m1EdVhrN6CGTo3HebjnyFyra91BLSsU1NGi7oVYwG+hSTOVbZThat0 foJ7X1Xx1vfLtIkwlZPQgbWwhpdlsu9DNJ5f5d5LsldUqQUrCvI7V0pHqRQDMnaCDFPN4+f+ccs N9gG6X5RHuA+w2KvpXG1zYYs7aXMBnE3sjx8FfQZ+anvw3QNrTzOJLqWHtIByFsvTgUWTeocIVr xh8FoP/lDjdHWyeUuioVUaipvj65uJyGGVJBLENDw9X3DA9o1O8U1KwiWiqwF6kQGW3PZF7/eSk nSn0mVHClj4qxXpx4n0eqy9yk6GNQj4pHrYfbo7fMc3UxSrX+Y89Vxsx4VFaS0HHliqKxB87ng9 Q8RtwaPQTk2I32fGDFjLbpRIS+l7UAzRZ+V2+MDDW+/XU7ohYvawWEQA4OQURuTlig55W7wcC0J wuVT1N2pOgjA/X3FTcpnLMU6/ X-Received: by 2002:a05:600c:638f:b0:4bb:7581:2bb9 with SMTP id 5b1f17b1804b1-4bb8892692dmr154376135e9.22.1795341292298; Sun, 22 Nov 2026 01:54:52 -0800 (PST) From: Haseeb Ashraf To: xen-devel@lists.xenproject.org Cc: Haseeb Ashraf , andrew.cooper3@citrix.com, anthony.perard@vates.tech, michal.orzel@amd.com, jbeulich@suse.com, julien@xen.org, roger@xenproject.org, sstabellini@kernel.org Subject: [XEN][PATCH v5 8/8] xen/memory: copy add_to_physmap batch entries in bulk Date: Sun, 22 Nov 2026 14:54:22 +0500 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-c201ff/1795341292-728B62A1-D1B3583F/0/0 X-purgate-type: clean X-purgate-size: 4576 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1795341314106048800 Content-Type: text/plain; charset="utf-8" From: Haseeb Ashraf xenmem_add_to_physmap_batch() copies two inputs and one result for every entry. On Arm every access to guest memory translates the guest address with an AT instruction, which traps to the outer hypervisor when Xen runs under nested virtualization. Stage up to 128 input entries and copy the results in bulk. Entries are still handled in order, an INVALID_GFN stops the loop after the entries before it, and the hypercall can still be preempted between two entries and continues with the first one that was not handled. At 128 entries the three buffers use 2.5 KiB of stack (8 + 8 + 4 =3D 20 bytes per entry), about 8% of the 32 KiB Arm stack. This bounds stack use while amortizing the two input copies and the result copy over each chunk; it is not a measured throughput threshold. Input copies precede the processing of each chunk and result copies follow it. EFAULT is not transactional: mappings may already have changed, and the order of partial copies and overlapping buffers is not the same as with the element at a time implementation. Signed-off-by: Haseeb Ashraf --- xen/common/memory.c | 63 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/xen/common/memory.c b/xen/common/memory.c index b5256bf6ec..c493766abd 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1077,6 +1077,14 @@ int xenmem_add_to_physmap(struct domain *d, struct x= en_add_to_physmap *xatp, return rc; } =20 +/* + * Number of entries copied from and to the guest at a time. Every access = to + * guest memory needs a guest virtual to machine address translation, which + * is very slow when Xen itself runs on top of another hypervisor, so it is + * not done once per entry. + */ +#define XENMEM_ATPB_CHUNK 128U + static int xenmem_add_to_physmap_batch(struct domain *d, struct xen_add_to_physmap_batch *xa= tpb, unsigned int extent) @@ -1120,27 +1128,52 @@ static int xenmem_add_to_physmap_batch(struct domai= n *d, =20 while ( xatpb->size > extent ) { - xen_ulong_t idx; - xen_pfn_t gpfn; - int rc; - - if ( unlikely(__copy_from_guest_offset(&idx, xatpb->idxs, - extent, 1)) || - unlikely(__copy_from_guest_offset(&gpfn, xatpb->gpfns, - extent, 1)) ) + xen_ulong_t idxs[XENMEM_ATPB_CHUNK]; + xen_pfn_t gpfns[XENMEM_ATPB_CHUNK]; + int errs[XENMEM_ATPB_CHUNK]; + unsigned int n =3D min_t(unsigned int, xatpb->size - extent, + XENMEM_ATPB_CHUNK); + unsigned int i; + bool preempt =3D false; + int rc =3D 0; + + if ( unlikely(__copy_from_guest_offset(idxs, xatpb->idxs, + extent, n)) || + unlikely(__copy_from_guest_offset(gpfns, xatpb->gpfns, + extent, n)) ) return -EFAULT; =20 - if ( gfn_eq(_gfn(gpfn), INVALID_GFN) ) - return -EINVAL; + for ( i =3D 0; i < n; i++ ) + { + if ( gfn_eq(_gfn(gpfns[i]), INVALID_GFN) ) + { + rc =3D -EINVAL; + break; + } =20 - rc =3D xenmem_add_to_physmap_one(d, xatpb->space, extra, - idx, _gfn(gpfn)); + errs[i] =3D xenmem_add_to_physmap_one(d, xatpb->space, extra, + idxs[i], _gfn(gpfns[i])); =20 - if ( unlikely(__copy_to_guest_offset(xatpb->errs, extent, &rc, 1))= ) + /* Check for continuation if it's not the last iteration. */ + if ( xatpb->size > extent + i + 1 && hypercall_preempt_check()= ) + { + preempt =3D true; + i++; + break; + } + } + + /* i entries have a result. */ + if ( (i !=3D 0U) && + unlikely(__copy_to_guest_offset(xatpb->errs, extent, errs, i)= ) ) return -EFAULT; =20 - /* Check for continuation if it's not the last iteration. */ - if ( xatpb->size > ++extent && hypercall_preempt_check() ) + extent +=3D i; + + if ( rc !=3D 0 ) + return rc; + + if ( preempt ) return extent; }