All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] xen/arm{32,64}: perform IPA-based TLBI when IPA is
@ 2026-01-18 13:33 ` Haseeb Ashraf
  0 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2025-12-08 13:55 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk, Dan Driscoll, Noor Ahsan Khawaja,
	Fahad Arslan, Andrew Bachtel

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

This patch series addresses a major issue for running Xen on KVM i.e.
costly emulation of VMALLS12E1IS which becomes worse when this TLBI
is invoked too many times. There are mainly two places where this is
problematic:
(a) When vCPUs switch on a pCPU or pCPUs
(b) When domu mapped pages onto dom0, are to be unmapped, then each
    page being removed by XENMEM_remove_from_physmap has its TLBs
    invalidated by the TLBI variant that flushes the whole range.

This patch series prefers usage of IPA-based TLBIs wherever possible
instead of complete flushing of TLBs every time.

It consists of three patches where the first one address the issue
being discussed for Arm64. Second patch further optimizes the
combined stage-1,2 TLB flushes by leveraging FEAT_nTLBPA. Third patch
introduces IPA-based TLBI for Arm32 in presence of FEAT_nTLBPA.

Haseeb Ashraf (3):
  xen/arm/p2m: perform IPA-based TLBI when IPA is known
  xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA
  xen/arm32: add CPU capability for IPA-based TLBI

Changes in v3:
- Mainly the handling of repeat TLBI workaround with IPA-based TLBI,
  so that the extra TLBI and DSB are repeated only for the final TLBI
  and DSB of the whole sequence.
- Updated code comments as per feedback. Further details are
  available in each commit's changelog.
- Minor updates to code as per feedback. Further details are
  available in each commit's changelog.

Changes in v2:
- Split up the commit in 3 commits. First commit implements the
  baseline implementation without any addition of new CPU
  capabilities. Implemented new CPU caps in separate features to
  emphasize how each of it optimizes the TLB invalidation.
- Moved ARM32 and ARM64 specific implementations of TLBIs to
  architecture specific flushtlb.h.
- Added references of ARM ARM in code comments.
- Evaluated and added a threshold to select between IPA-based TLB
  invalidation vs fallback to full stage TLB invalidation above
  the threshold.
- Introduced ARM_HAS_NTLBPA CPU capability which leverages
  FEAT_nTLBPA for arm32 as well as arm64.
- Introduced ARM_HAS_TLB_IPA CPU capability for IPA-based TLBI
  for arm32.

Haseeb Ashraf (3):
  xen/arm/p2m: perform IPA-based TLBI when IPA is known
  xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA
  xen/arm32: add CPU capability for IPA-based TLBI

 xen/arch/arm/cpufeature.c                 | 31 ++++++++
 xen/arch/arm/include/asm/arm32/flushtlb.h | 87 +++++++++++++++++++++
 xen/arch/arm/include/asm/arm64/flushtlb.h | 77 +++++++++++++++++++
 xen/arch/arm/include/asm/cpregs.h         |  4 +
 xen/arch/arm/include/asm/cpufeature.h     | 27 ++++++-
 xen/arch/arm/include/asm/mmu/p2m.h        |  2 +
 xen/arch/arm/include/asm/processor.h      | 10 +++
 xen/arch/arm/mmu/p2m.c                    | 92 +++++++++++++++++------
 8 files changed, 302 insertions(+), 28 deletions(-)

-- 
2.43.0



^ permalink raw reply	[flat|nested] 25+ messages in thread

* [PATCH 1/3] xen/arm/p2m: perform IPA-based TLBI when IPA is known
@ 2026-01-18 13:33   ` Haseeb Ashraf
  0 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2025-12-08 13:55 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk, Dan Driscoll, Noor Ahsan Khawaja,
	Fahad Arslan, Andrew Bachtel

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

This commit addresses a major issue for running Xen on KVM i.e.
costly emulation of VMALLS12E1IS which becomes worse when this TLBI
is invoked too many times. There are mainly two places where this is
problematic:
(a) When vCPUs switch on a pCPU or pCPUs
(b) When domu mapped pages onto dom0, are to be unmapped, then each
    page being removed by XENMEM_remove_from_physmap has its TLBs
    invalidated by VMALLS12E1IS.

The first one is addressed by relaxing VMALLS12E1 -> VMALLE1 as the
stage-2 is common between all the vCPUs of a VM. Since each CPU has
its own private TLBs, so flush between vCPU of the same domains is
still required to avoid translations from vCPUx to "leak" to the
vCPUy which can be achieved by using VMALLE1.

The second one is addressed by using IPA-based TLBI (IPAS2E1) in
combination with VMALLE1 whenever the IPA range is known instead of
using VMALLS12E1. There is an upper cap placed on number of IPA-based
TLBI. This factor for execution time of VMALLS12E1 vs IPAS2E1 is
found to be 70K on Graviton4 in Xen on KVM virtualization. So,
64K * 4KB = 256MB is set as the threshold.

For arm32, TLBIALL instruction can invalidate both stage-1 and
stage-2 entries, so using IPA-based TLBI would be redundant as
TLBIALL is required in any case to invalidate corresponding cached
entries from stage-1.

Suggested-by: Julien Grall <julien@xen.org>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Changes in v3:
- Updated IPA-based TLBI sequence to apply ARM64 repeat TLBI
  workaround to only final TLBI and DSB of the sequence.
- Removed TLB_HELPER_IPA and instead directly used the TLBI
  instruction where needed as that was the only instance where it is
  being used.
- Removed flush_guest_tlb_range_ipa_local() as it was not being used.
- Updated comments as per feedback in v2 about holding lock before
  p2m_load_vttbr.
- Updated references of ARM ARM to use newer version DDI 0487L.b
  instead of older version DDI 0487A.e.

Changes in v2:
- This commit implements the basline implementation to address the
  problem at hand. Removed the FEAT_nTLBPA implementation from this
  commit which will be implemented in following commit using CPU
  capability.
- Moved ARM32 and ARM64 specific implementations of TLBIs to
  architecture specific flushtlb.h.
- Added references of ARM ARM in code comments.
- Evaluated and added a threshold to select between IPA-based TLB
  invalidation vs fallback to full stage TLB invalidation above
  the threshold.
---
 xen/arch/arm/include/asm/arm32/flushtlb.h | 53 +++++++++++++
 xen/arch/arm/include/asm/arm64/flushtlb.h | 46 ++++++++++++
 xen/arch/arm/include/asm/mmu/p2m.h        |  2 +
 xen/arch/arm/mmu/p2m.c                    | 92 +++++++++++++++++------
 4 files changed, 168 insertions(+), 25 deletions(-)

diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/include/asm/arm32/flushtlb.h
index 61c25a3189..3c0c2123d4 100644
--- a/xen/arch/arm/include/asm/arm32/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm32/flushtlb.h
@@ -45,6 +45,43 @@ TLB_HELPER(flush_xen_tlb_local, TLBIALLH, nsh)
 
 #undef TLB_HELPER
 
+/*
+ * Flush TLB of local processor. Use when flush for only stage-1 is intended.
+ *
+ * The following function should be used where intention is to clear only
+ * stage-1 TLBs. This would be helpful in future in identifying which stage-1
+ * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ */
+static inline void flush_guest_tlb_s1_local(void)
+{
+    /*
+     * Same instruction can invalidate both stage-1 and stage-2 TLBs depending
+     * upon the execution context.
+     *
+     * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+     */
+    return flush_guest_tlb_local();
+}
+
+/*
+ * Flush TLB of inner-shareable processor domain. Use when flush for only
+ * stage-1 is intended.
+ *
+ * The following function should be used where intention is to clear only
+ * stage-1 TLBs. This would be helpful in future in identifying which stage-1
+ * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ */
+static inline void flush_guest_tlb_s1(void)
+{
+    /*
+     * Same instruction can invalidate both stage-1 and stage-2 TLBs depending
+     * upon the execution context.
+     *
+     * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+     */
+    return flush_guest_tlb();
+}
+
 /* Flush TLB of local processor for address va. */
 static inline void __flush_xen_tlb_one_local(vaddr_t va)
 {
@@ -57,6 +94,22 @@ static inline void __flush_xen_tlb_one(vaddr_t va)
     asm volatile(STORE_CP32(0, TLBIMVAHIS) : : "r" (va) : "memory");
 }
 
+/*
+ * 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 upon
+     * the execution mode.
+     *
+     * See ARMv8 (DDI 0487L.b): G5-11698 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/include/asm/arm64/flushtlb.h
index 3b99c11b50..67ae616993 100644
--- a/xen/arch/arm/include/asm/arm64/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm64/flushtlb.h
@@ -1,6 +1,8 @@
 #ifndef __ASM_ARM_ARM64_FLUSHTLB_H__
 #define __ASM_ARM_ARM64_FLUSHTLB_H__
 
+#include <xen/sizes.h> /* For SZ_* macros. */
+
 /*
  * Every invalidation operation use the following patterns:
  *
@@ -72,6 +74,12 @@ TLB_HELPER(flush_guest_tlb_local, vmalls12e1, nsh)
 /* Flush innershareable TLBs, current VMID only */
 TLB_HELPER(flush_guest_tlb, vmalls12e1is, ish)
 
+/* Flush local TLBs, current VMID, stage-1 only */
+TLB_HELPER(flush_guest_tlb_s1_local, vmalle1, nsh)
+
+/* Flush innershareable TLBs, current VMID, stage-1 only */
+TLB_HELPER(flush_guest_tlb_s1, vmalle1is, ish)
+
 /* Flush local TLBs, all VMIDs, non-hypervisor mode */
 TLB_HELPER(flush_all_guests_tlb_local, alle1, nsh)
 
@@ -90,6 +98,44 @@ TLB_HELPER_VA(__flush_xen_tlb_one, vae2is)
 #undef TLB_HELPER
 #undef TLB_HELPER_VA
 
+/*
+ * 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)
+{
+    paddr_t end;
+
+    /*
+     * If IPA range is too big (empirically found to be 256M), then fallback to
+     * full TLB flush.
+     */
+    if ( size > SZ_256M )
+        return flush_guest_tlb();
+
+    end = ipa + size;
+
+    /*
+     * See ARM ARM DDI 0487L.b D8.17.6.1 (Invalidating TLB entries from stage 2
+     * translations) for details of TLBI sequence.
+     */
+    dsb(ishst); /* Ensure prior page-tables updates have completed */
+    while ( ipa < end )
+    {
+        /* Flush stage-2 TLBs for ipa address */
+        asm_inline volatile (
+            "tlbi ipas2e1is, %0;" : : "r" (ipa >> PAGE_SHIFT) : "memory" );
+        ipa += PAGE_SIZE;
+    }
+    /*
+     * As ARM64_WORKAROUND_REPEAT_TLBI is required to be applied to last TLBI
+     * of the sequence, it is only needed to be handled in the following
+     * invocation. Final dsb() and isb() are also applied in the following
+     * invocation.
+     */
+    flush_guest_tlb_s1();
+}
+
 #endif /* __ASM_ARM_ARM64_FLUSHTLB_H__ */
 /*
  * Local variables:
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;
 
 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);
 
 void p2m_clear_root_pages(struct p2m_domain *p2m);
diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c
index 51abf3504f..eec59056fa 100644
--- a/xen/arch/arm/mmu/p2m.c
+++ b/xen/arch/arm/mmu/p2m.c
@@ -235,33 +235,28 @@ void p2m_restore_state(struct vcpu *n)
      * when running multiple vCPU of the same domain on a single pCPU.
      */
     if ( *last_vcpu_ran != INVALID_VCPU_ID && *last_vcpu_ran != n->vcpu_id )
-        flush_guest_tlb_local();
+        flush_guest_tlb_s1_local();
 
     *last_vcpu_ran = n->vcpu_id;
 }
 
 /*
- * 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 *flags)
 {
-    unsigned long flags = 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 = READ_SYSREG64(VTTBR_EL2);
     if ( ovttbr != p2m->vttbr )
     {
         uint64_t vttbr;
 
-        local_irq_save(flags);
+        local_irq_save(*flags);
 
         /*
          * ARM64_WORKAROUND_AT_SPECULATE: We need to stop AT to allocate
@@ -280,8 +275,14 @@ void p2m_force_tlb_flush_sync(struct p2m_domain *p2m)
         isb();
     }
 
-    flush_guest_tlb();
+    return ovttbr;
+}
 
+/*
+ * 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 != READ_SYSREG64(VTTBR_EL2) )
     {
         WRITE_SYSREG64(ovttbr, VTTBR_EL2);
@@ -289,10 +290,58 @@ 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 = 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 = p2m_load_vttbr(p2m, &flags);
+
+    flush_guest_tlb();
+
+    p2m_restore_vttbr(ovttbr, flags);
 
     p2m->need_flush = false;
 }
 
+/*
+ * 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 = 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 = p2m_load_vttbr(p2m, &flags);
+
+    /* Invalidate TLB entries by IPA range */
+    flush_guest_tlb_range_ipa(start_ipa, PAGE_SIZE * page_count);
+
+    p2m_restore_vttbr(ovttbr, flags);
+}
+
 void p2m_tlb_flush_sync(struct p2m_domain *p2m)
 {
     if ( p2m->need_flush )
@@ -1034,7 +1083,8 @@ 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, gfn_x(sgfn) << PAGE_SHIFT,
+                                       1UL << page_order);
 
         p2m_write_pte(entry, split_pte, p2m->clean_pte);
 
@@ -1090,8 +1140,8 @@ static int __p2m_set_entry(struct p2m_domain *p2m,
         p2m_remove_pte(entry, p2m->clean_pte);
 
     if ( removing_mapping )
-        /* Flush can be deferred if the entry is removed */
-        p2m->need_flush |= !!lpae_is_valid(orig_pte);
+        p2m_force_tlb_flush_range_sync(p2m, gfn_x(sgfn) << PAGE_SHIFT,
+                                       1UL << page_order);
     else
     {
         lpae_t pte = mfn_to_p2m_entry(smfn, t, a);
@@ -1102,18 +1152,10 @@ static int __p2m_set_entry(struct p2m_domain *p2m,
         /*
          * It is necessary to flush the TLB before writing the new entry
          * to keep coherency when the previous entry was valid.
-         *
-         * Although, it could be defered when only the permissions are
-         * changed (e.g in case of memaccess).
          */
         if ( lpae_is_valid(orig_pte) )
-        {
-            if ( likely(!p2m->mem_access_enabled) ||
-                 P2M_CLEAR_PERM(pte) != P2M_CLEAR_PERM(orig_pte) )
-                p2m_force_tlb_flush_sync(p2m);
-            else
-                p2m->need_flush = true;
-        }
+            p2m_force_tlb_flush_range_sync(p2m, gfn_x(sgfn) << PAGE_SHIFT,
+                                           1UL << page_order);
         else if ( !p2m_is_valid(orig_pte) ) /* new mapping */
             p2m->stats.mappings[level]++;
 
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH 2/3] xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA
@ 2026-01-18 13:33   ` Haseeb Ashraf
  0 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2025-12-08 13:55 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk, Dan Driscoll, Noor Ahsan Khawaja,
	Fahad Arslan, Andrew Bachtel, Mohamed Mediouni

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

FEAT_nTLBPA (quoting definition) introduces a mechanism to identify
if the intermediate caching of translation table walks does not
include non-coherent caches of previous valid translation table
entries since the last completed TLBI applicable to the PE.

As there won't be any non-coherent caches since the last completed
TLBI, stage-1 TLBI won't be required while performing stage-2 TLBI.

This feature is optionally available in both arm32 and arm64.

Suggested-by: Mohamed Mediouni <mohamed@unpredictable.fr>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Changes in v3:
- This commit has no functional change in v3, only rebasing changes
  due to updates in commit-1.

Changes in v2:
- This commit is implemented in v2 and is splitted from commit-1 in
  v1. This is implemented by using CPU capability.
---
 xen/arch/arm/cpufeature.c                 | 19 ++++++
 xen/arch/arm/include/asm/arm32/flushtlb.h | 14 +++--
 xen/arch/arm/include/asm/arm64/flushtlb.h | 77 ++++++++++++++++-------
 xen/arch/arm/include/asm/cpufeature.h     | 24 ++++++-
 xen/arch/arm/include/asm/processor.h      |  7 +++
 5 files changed, 109 insertions(+), 32 deletions(-)

diff --git a/xen/arch/arm/cpufeature.c b/xen/arch/arm/cpufeature.c
index 1a80738571..9fa1c45869 100644
--- a/xen/arch/arm/cpufeature.c
+++ b/xen/arch/arm/cpufeature.c
@@ -17,7 +17,19 @@ DECLARE_BITMAP(cpu_hwcaps, ARM_NCAPS);
 
 struct cpuinfo_arm __read_mostly domain_cpuinfo;
 
+#ifdef CONFIG_ARM_32
+static bool has_ntlbpa(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm32.ntlbpa == MM32_NTLBPA_SUPPORT_IMP;
+}
+#endif
+
 #ifdef CONFIG_ARM_64
+static bool has_ntlbpa(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm64.ntlbpa == MM64_NTLBPA_SUPPORT_IMP;
+}
+
 static bool has_sb_instruction(const struct arm_cpu_capabilities *entry)
 {
     return system_cpuinfo.isa64.sb;
@@ -25,6 +37,13 @@ static bool has_sb_instruction(const struct arm_cpu_capabilities *entry)
 #endif
 
 static const struct arm_cpu_capabilities arm_features[] = {
+#if defined(CONFIG_ARM_32) || defined(CONFIG_ARM_64)
+    {
+        .desc = "Intermediate caching of translation table walks (nTLBPA)",
+        .capability = ARM_HAS_NTLBPA,
+        .matches = has_ntlbpa,
+    },
+#endif
 #ifdef CONFIG_ARM_64
     {
         .desc = "Speculation barrier instruction (SB)",
diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/include/asm/arm32/flushtlb.h
index 3c0c2123d4..7cff042508 100644
--- a/xen/arch/arm/include/asm/arm32/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm32/flushtlb.h
@@ -49,8 +49,8 @@ TLB_HELPER(flush_xen_tlb_local, TLBIALLH, nsh)
  * Flush TLB of local processor. Use when flush for only stage-1 is intended.
  *
  * The following function should be used where intention is to clear only
- * stage-1 TLBs. This would be helpful in future in identifying which stage-1
- * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ * stage-1 TLBs. This would be helpful in identifying which stage-1 TLB flushes
+ * can be skipped such as in present of FEAT_nTLBPA.
  */
 static inline void flush_guest_tlb_s1_local(void)
 {
@@ -60,7 +60,8 @@ static inline void flush_guest_tlb_s1_local(void)
      *
      * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
      */
-    return flush_guest_tlb_local();
+    if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) )
+        flush_guest_tlb_local();
 }
 
 /*
@@ -68,8 +69,8 @@ static inline void flush_guest_tlb_s1_local(void)
  * stage-1 is intended.
  *
  * The following function should be used where intention is to clear only
- * stage-1 TLBs. This would be helpful in future in identifying which stage-1
- * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ * stage-1 TLBs. This would be helpful in identifying which stage-1 TLB flushes
+ * can be skipped such as in present of FEAT_nTLBPA.
  */
 static inline void flush_guest_tlb_s1(void)
 {
@@ -79,7 +80,8 @@ static inline void flush_guest_tlb_s1(void)
      *
      * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
      */
-    return flush_guest_tlb();
+    if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) )
+        flush_guest_tlb();
 }
 
 /* Flush TLB of local processor for address va. */
diff --git a/xen/arch/arm/include/asm/arm64/flushtlb.h b/xen/arch/arm/include/asm/arm64/flushtlb.h
index 67ae616993..0f0d5050e5 100644
--- a/xen/arch/arm/include/asm/arm64/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm64/flushtlb.h
@@ -47,6 +47,24 @@ static inline void name(void)                    \
         : : : "memory");                         \
 }
 
+#define TLB_HELPER_NTLBPA(name, tlbop, sh)           \
+static inline void name(void)                        \
+{                                                    \
+    if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) )      \
+        asm_inline volatile (                        \
+            "dsb  "  # sh  "st;"                     \
+            "tlbi "  # tlbop  ";"                    \
+            ALTERNATIVE(                             \
+                "nop; nop;",                         \
+                "dsb  ish;"                          \
+                "tlbi "  # tlbop  ";",               \
+                ARM64_WORKAROUND_REPEAT_TLBI,        \
+                CONFIG_ARM64_WORKAROUND_REPEAT_TLBI) \
+            "dsb  "  # sh  ";"                       \
+            "isb;"                                   \
+            : : : "memory");                         \
+}
+
 /*
  * FLush TLB by VA. This will likely be used in a loop, so the caller
  * is responsible to use the appropriate memory barriers before/after
@@ -75,10 +93,10 @@ TLB_HELPER(flush_guest_tlb_local, vmalls12e1, nsh)
 TLB_HELPER(flush_guest_tlb, vmalls12e1is, ish)
 
 /* Flush local TLBs, current VMID, stage-1 only */
-TLB_HELPER(flush_guest_tlb_s1_local, vmalle1, nsh)
+TLB_HELPER_NTLBPA(flush_guest_tlb_s1_local, vmalle1, nsh)
 
 /* Flush innershareable TLBs, current VMID, stage-1 only */
-TLB_HELPER(flush_guest_tlb_s1, vmalle1is, ish)
+TLB_HELPER_NTLBPA(flush_guest_tlb_s1, vmalle1is, ish)
 
 /* Flush local TLBs, all VMIDs, non-hypervisor mode */
 TLB_HELPER(flush_all_guests_tlb_local, alle1, nsh)
@@ -104,8 +122,6 @@ TLB_HELPER_VA(__flush_xen_tlb_one, vae2is)
  */
 static inline void flush_guest_tlb_range_ipa(paddr_t ipa, unsigned long size)
 {
-    paddr_t end;
-
     /*
      * If IPA range is too big (empirically found to be 256M), then fallback to
      * full TLB flush.
@@ -113,27 +129,42 @@ static inline void flush_guest_tlb_range_ipa(paddr_t ipa, unsigned long size)
     if ( size > SZ_256M )
         return flush_guest_tlb();
 
-    end = ipa + size;
-
-    /*
-     * See ARM ARM DDI 0487L.b D8.17.6.1 (Invalidating TLB entries from stage 2
-     * translations) for details of TLBI sequence.
-     */
-    dsb(ishst); /* Ensure prior page-tables updates have completed */
-    while ( ipa < end )
+    else if ( size > 0 )
     {
-        /* Flush stage-2 TLBs for ipa address */
-        asm_inline volatile (
-            "tlbi ipas2e1is, %0;" : : "r" (ipa >> PAGE_SHIFT) : "memory" );
-        ipa += PAGE_SIZE;
+        paddr_t end = ipa + size;
+
+        /*
+         * See ARM ARM DDI 0487L.b D8.17.6.1 (Invalidating TLB entries from
+         * stage 2 translations) for details on TLBI sequence.
+         */
+        dsb(ishst); /* Ensure prior page-tables updates have completed */
+        while ( ipa < end )
+        {
+            /* Flush stage-2 TLBs for ipa address */
+            asm_inline volatile (
+                "tlbi ipas2e1is, %0;" : : "r" (ipa >> PAGE_SHIFT) : "memory" );
+            ipa += PAGE_SIZE;
+        }
+        if ( cpus_have_const_cap(ARM_HAS_NTLBPA) )
+            asm_inline volatile (
+                ALTERNATIVE(
+                    "nop; nop;",
+                    "dsb  ish;"
+                    "tlbi ipas2e1is, %0;",
+                    ARM64_WORKAROUND_REPEAT_TLBI,
+                    CONFIG_ARM64_WORKAROUND_REPEAT_TLBI)
+                "dsb  ish;"
+                "isb;"
+                : : "r" ((ipa - PAGE_SIZE) >> PAGE_SHIFT) : "memory" );
+        else
+            /*
+             * As ARM64_WORKAROUND_REPEAT_TLBI is required to be applied to
+             * last TLBI of the sequence, it is only needed to be handled in
+             * the following invocation. Final dsb() and isb() are also applied
+             * in the following invocation.
+             */
+            flush_guest_tlb_s1();
     }
-    /*
-     * As ARM64_WORKAROUND_REPEAT_TLBI is required to be applied to last TLBI
-     * of the sequence, it is only needed to be handled in the following
-     * invocation. Final dsb() and isb() are also applied in the following
-     * invocation.
-     */
-    flush_guest_tlb_s1();
 }
 
 #endif /* __ASM_ARM_ARM64_FLUSHTLB_H__ */
diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/asm/cpufeature.h
index 13353c8e1a..9f796ed4c1 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
 
-#define ARM_NCAPS           18
+#define ARM_NCAPS           19
 
 #ifndef __ASSEMBLER__
 
@@ -269,7 +270,8 @@ struct cpuinfo_arm {
             unsigned long ets:4;
             unsigned long __res1:4;
             unsigned long afp:4;
-            unsigned long __res2:12;
+            unsigned long ntlbpa:4;
+            unsigned long __res2:8;
             unsigned long ecbhb:4;
 
             /* MMFR2 */
@@ -430,8 +432,24 @@ struct cpuinfo_arm {
         register_t bits[1];
     } aux32;
 
-    struct {
+    union {
         register_t bits[6];
+        struct {
+            /* MMFR0 */
+            unsigned long __res0:32;
+            /* MMFR1 */
+            unsigned long __res1:32;
+            /* MMFR2 */
+            unsigned long __res2:32;
+            /* MMFR3 */
+            unsigned long __res3:32;
+            /* MMFR4 */
+            unsigned long __res4:32;
+            /* MMFR5 */
+            unsigned long __res5:4;
+            unsigned long ntlbpa:4;
+            unsigned long __res6:24;
+        };
     } mm32;
 
     struct {
diff --git a/xen/arch/arm/include/asm/processor.h b/xen/arch/arm/include/asm/processor.h
index 1a48c9ff3b..85f3b643a0 100644
--- a/xen/arch/arm/include/asm/processor.h
+++ b/xen/arch/arm/include/asm/processor.h
@@ -459,9 +459,16 @@
 /* FSR long format */
 #define FSRL_STATUS_DEBUG       (_AC(0x22,UL)<<0)
 
+#ifdef CONFIG_ARM_32
+#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
+#define MM64_NTLBPA_SUPPORT_NI      0x0
+#define MM64_NTLBPA_SUPPORT_IMP     0x1
 #endif
 
 #ifndef __ASSEMBLER__
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [PATCH 3/3] xen/arm32: add CPU capability for IPA-based TLBI
@ 2026-01-18 13:33   ` Haseeb Ashraf
  0 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2025-12-08 13:55 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk, Dan Driscoll, Noor Ahsan Khawaja,
	Fahad Arslan, Andrew Bachtel

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

This feature is available since armv8 and can be used to perform
IPA-based TLBI for arm32. XENMEM_remove_from_physmap performs this
invalidation in each hypercall so this code path will be optimized,
instead of performing a TLBIALL each time in presence of nTLBPA.

Suggested-by: Julien Grall <julien@xen.org>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Changes in v3:
- There are no functional changes in this version. There are minor
  code updates and comment updates as per the feedback on v2.
- The cpregs are defined in order as per Coprocessor-> CRn-> Opcode 1
  -> CRm-> Opcode 2.
- Added comment to explain why IPA-based TLBI is added only in
  presence of FEAT_nTLBPA.
- Replaced `goto default_tlbi` with if...else.
- Removed extra definitions of MM32_UNITLB_* macros which were not
  being used.

Changes in v2:
- This commit is implemented in v2 as per the feedback to implement
  IPA-based TLBI for Arm32 in addition to Arm64.
---
 xen/arch/arm/cpufeature.c                 | 12 +++++++
 xen/arch/arm/include/asm/arm32/flushtlb.h | 42 ++++++++++++++++++++---
 xen/arch/arm/include/asm/cpregs.h         |  4 +++
 xen/arch/arm/include/asm/cpufeature.h     | 15 ++++----
 xen/arch/arm/include/asm/processor.h      |  3 ++
 5 files changed, 65 insertions(+), 11 deletions(-)

diff --git a/xen/arch/arm/cpufeature.c b/xen/arch/arm/cpufeature.c
index 9fa1c45869..d18c6449c6 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;
 
 #ifdef CONFIG_ARM_32
+static bool has_tlb_ipa_instruction(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm32.unitlb == MM32_UNITLB_BY_IPA;
+}
+
 static bool has_ntlbpa(const struct arm_cpu_capabilities *entry)
 {
     return system_cpuinfo.mm32.ntlbpa == MM32_NTLBPA_SUPPORT_IMP;
@@ -37,6 +42,13 @@ static bool has_sb_instruction(const struct arm_cpu_capabilities *entry)
 #endif
 
 static const struct arm_cpu_capabilities arm_features[] = {
+#ifdef CONFIG_ARM_32
+    {
+        .desc = "IPA-based TLB Invalidation",
+        .capability = ARM32_HAS_TLB_IPA,
+        .matches = has_tlb_ipa_instruction,
+    },
+#endif
 #if defined(CONFIG_ARM_32) || defined(CONFIG_ARM_64)
     {
         .desc = "Intermediate caching of translation table walks (nTLBPA)",
diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/include/asm/arm32/flushtlb.h
index 7cff042508..3e6f86f6d2 100644
--- a/xen/arch/arm/include/asm/arm32/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm32/flushtlb.h
@@ -1,6 +1,8 @@
 #ifndef __ASM_ARM_ARM32_FLUSHTLB_H__
 #define __ASM_ARM_ARM32_FLUSHTLB_H__
 
+#include <xen/sizes.h> /* For SZ_* macros. */
+
 /*
  * Every invalidation operation use the following patterns:
  *
@@ -104,12 +106,42 @@ 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 upon
-     * the execution mode.
-     *
-     * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+     * IPA-based TLBI is used only in presence of nTLBPA, otherwise, stage-1
+     * invalidation would still be required and there is no separate TLBI for
+     * stage-1 on Arm32. So in absence of nTLBPA, it is pointless to flush by
+     * IPA.
      */
-    flush_guest_tlb();
+    if ( cpus_have_const_cap(ARM_HAS_NTLBPA) &&
+         cpus_have_const_cap(ARM32_HAS_TLB_IPA) )
+    {
+        /*
+         * If IPA range is too big (empirically found to be 256M), then
+         * fallback to full TLB flush
+         */
+        if ( size > SZ_256M )
+            /*
+             * Following can invalidate both stage-1 and stage-2 TLBs depending
+             * upon the execution mode.
+             *
+             * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+             */
+            flush_guest_tlb();
+        else
+        {
+            paddr_t end = ipa + size;
+
+            dsb(ishst); /* Ensure prior page-tables updates have completed */
+            while ( ipa < end )
+            {
+                /* Flush stage-2 TLBs for ipa address. */
+                asm volatile(STORE_CP32(0, TLBIIPAS2IS)
+                             : : "r" (ipa >> PAGE_SHIFT) : "memory");
+                ipa += PAGE_SIZE;
+            }
+            dsb(ish);
+            isb();
+        }
+    }
 }
 
 #endif /* __ASM_ARM_ARM32_FLUSHTLB_H__ */
diff --git a/xen/arch/arm/include/asm/cpregs.h b/xen/arch/arm/include/asm/cpregs.h
index a7503a190f..51f091dace 100644
--- a/xen/arch/arm/include/asm/cpregs.h
+++ b/xen/arch/arm/include/asm/cpregs.h
@@ -223,9 +223,13 @@
 #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 match */
 #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 for stage 2 by IPA inner shareable */
+#define TLBIIPAS2LIS    p15,4,c8,c0,5   /* Invalidate unified TLB entry for stage 2 last level 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 Non-Hyp. Unified TLB inner shareable */
+#define TLBIIPAS2       p15,4,c8,c4,1   /* Invalidate unified TLB entry for stage 2 by IPA */
+#define TLBIIPAS2L      p15,4,c8,c4,5   /* Invalidate unified TLB entry for stage 2 last level by IPA */
 #define TLBIALLH        p15,4,c8,c7,0   /* Invalidate Entire Hyp. Unified TLB */
 #define TLBIMVAH        p15,4,c8,c7,1   /* Invalidate Unified Hyp. TLB by MVA */
 #define TLBIALLNSNH     p15,4,c8,c7,4   /* Invalidate Entire Non-Secure Non-Hyp. Unified TLB */
diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/asm/cpufeature.h
index 9f796ed4c1..07f1d770b3 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
 
-#define ARM_NCAPS           19
+#define ARM_NCAPS           20
 
 #ifndef __ASSEMBLER__
 
@@ -440,15 +441,17 @@ struct cpuinfo_arm {
             /* MMFR1 */
             unsigned long __res1:32;
             /* MMFR2 */
-            unsigned long __res2:32;
+            unsigned long __res2:16;
+            unsigned long unitlb:4;
+            unsigned long __res3:12;
             /* MMFR3 */
-            unsigned long __res3:32;
-            /* MMFR4 */
             unsigned long __res4:32;
+            /* MMFR4 */
+            unsigned long __res5:32;
             /* MMFR5 */
-            unsigned long __res5:4;
+            unsigned long __res6:4;
             unsigned long ntlbpa:4;
-            unsigned long __res6:24;
+            unsigned long __res7:24;
         };
     } mm32;
 
diff --git a/xen/arch/arm/include/asm/processor.h b/xen/arch/arm/include/asm/processor.h
index 85f3b643a0..eda39566e1 100644
--- a/xen/arch/arm/include/asm/processor.h
+++ b/xen/arch/arm/include/asm/processor.h
@@ -460,6 +460,9 @@
 #define FSRL_STATUS_DEBUG       (_AC(0x22,UL)<<0)
 
 #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
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 25+ messages in thread

* Re: [PATCH 0/3] xen/arm{32,64}: perform IPA-based TLBI when IPA is
  2026-01-18 13:33 ` [XEN PATCH v3 " Haseeb Ashraf
                   ` (4 preceding siblings ...)
  (?)
@ 2025-12-16 12:08 ` Ashraf, Haseeb
  2026-01-06  6:40   ` Ashraf, Haseeb
  -1 siblings, 1 reply; 25+ messages in thread
From: Ashraf, Haseeb @ 2025-12-16 12:08 UTC (permalink / raw)
  To: Haseeb Ashraf, xen-devel@lists.xenproject.org, Julien Grall
  Cc: Stefano Stabellini, Bertrand Marquis, Michal Orzel,
	Volodymyr Babchuk, Driscoll, Dan, Ahsan Khawaja, Noor,
	Arslan, Fahad, Bachtel, Andrew

[-- Attachment #1: Type: text/plain, Size: 188 bytes --]

Hi @Julien Grall<mailto:julien@xen.org>,

Bringing up this patch series. Please have a look at it, and let me if there is any comment on v3 of this series.

Thanks & Regards,
Haseeb

[-- Attachment #2: Type: text/html, Size: 1442 bytes --]

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH 0/3] xen/arm{32,64}: perform IPA-based TLBI when IPA is
  2025-12-16 12:08 ` [PATCH 0/3] xen/arm{32,64}: perform IPA-based TLBI when IPA is Ashraf, Haseeb
@ 2026-01-06  6:40   ` Ashraf, Haseeb
  0 siblings, 0 replies; 25+ messages in thread
From: Ashraf, Haseeb @ 2026-01-06  6:40 UTC (permalink / raw)
  To: Haseeb Ashraf, xen-devel@lists.xenproject.org, Julien Grall
  Cc: Stefano Stabellini, Bertrand Marquis, Michal Orzel,
	Volodymyr Babchuk, Driscoll, Dan, Ahsan Khawaja, Noor,
	Arslan, Fahad, Bachtel, Andrew

[-- Attachment #1: Type: text/plain, Size: 1201 bytes --]

Hello everyone,

Is there any comment on this patch series? I want to get this merged as it is a blocker for supporting Xen on KVM.

Regards,
Haseeb
________________________________
From: Ashraf, Haseeb (DI SW EDA HAV SLS EPS RTOS LIN) <haseeb.ashraf@siemens.com>
Sent: Tuesday, December 16, 2025 5:08 PM
To: Haseeb Ashraf <haseebashraf091@gmail.com>; xen-devel@lists.xenproject.org <xen-devel@lists.xenproject.org>; Julien Grall <julien@xen.org>
Cc: Stefano Stabellini <sstabellini@kernel.org>; Bertrand Marquis <bertrand.marquis@arm.com>; Michal Orzel <michal.orzel@amd.com>; Volodymyr Babchuk <Volodymyr_Babchuk@epam.com>; Driscoll, Dan (DI SW EDA HAV SLS EPS TOA) <dan.driscoll@siemens.com>; Ahsan Khawaja, Noor (DI SW EDA HAV SLS EPS RTOS LIN) <noor.ahsan@siemens.com>; Arslan, Fahad (DI SW EDA HAV SLS EPS RTOS LIN) <fahad.arslan@siemens.com>; Bachtel, Andrew (DI SW EDA HAV SLS EPS TOA) <andrew.bachtel@siemens.com>
Subject: Re: [PATCH 0/3] xen/arm{32,64}: perform IPA-based TLBI when IPA is

Hi @Julien Grall<mailto:julien@xen.org>,

Bringing up this patch series. Please have a look at it, and let me if there is any comment on v3 of this series.

Thanks & Regards,
Haseeb

[-- Attachment #2: Type: text/html, Size: 3622 bytes --]

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [XEN PATCH v3 0/3] xen/arm{32,64}: perform IPA-based TLBI when IPA is known
@ 2026-01-18 13:33 ` Haseeb Ashraf
  0 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-01-18 13:33 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

This patch series addresses a major issue for running Xen on KVM i.e.
costly emulation of VMALLS12E1IS which becomes worse when this TLBI
is invoked too many times. There are mainly two places where this is
problematic:
(a) When vCPUs switch on a pCPU or pCPUs
(b) When domu mapped pages onto dom0, are to be unmapped, then each
    page being removed by XENMEM_remove_from_physmap has its TLBs
    invalidated by the TLBI variant that flushes the whole range.

This patch series prefers usage of IPA-based TLBIs wherever possible
instead of complete flushing of TLBs every time.

It consists of three patches where the first one address the issue
being discussed for Arm64. Second patch further optimizes the
combined stage-1,2 TLB flushes by leveraging FEAT_nTLBPA. Third patch
introduces IPA-based TLBI for Arm32 in presence of FEAT_nTLBPA.

Haseeb Ashraf (3):
  xen/arm/p2m: perform IPA-based TLBI when IPA is known
  xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA
  xen/arm32: add CPU capability for IPA-based TLBI

Changes in v3:
- Mainly the handling of repeat TLBI workaround with IPA-based TLBI,
  so that the extra TLBI and DSB are repeated only for the final TLBI
  and DSB of the whole sequence.
- Updated code comments as per feedback. Further details are
  available in each commit's changelog.
- Minor updates to code as per feedback. Further details are
  available in each commit's changelog.

Changes in v2:
- Split up the commit in 3 commits. First commit implements the
  baseline implementation without any addition of new CPU
  capabilities. Implemented new CPU caps in separate features to
  emphasize how each of it optimizes the TLB invalidation.
- Moved ARM32 and ARM64 specific implementations of TLBIs to
  architecture specific flushtlb.h.
- Added references of ARM ARM in code comments.
- Evaluated and added a threshold to select between IPA-based TLB
  invalidation vs fallback to full stage TLB invalidation above
  the threshold.
- Introduced ARM_HAS_NTLBPA CPU capability which leverages
  FEAT_nTLBPA for arm32 as well as arm64.
- Introduced ARM_HAS_TLB_IPA CPU capability for IPA-based TLBI
  for arm32.

Haseeb Ashraf (3):
  xen/arm/p2m: perform IPA-based TLBI when IPA is known
  xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA
  xen/arm32: add CPU capability for IPA-based TLBI

 xen/arch/arm/cpufeature.c                 | 31 ++++++++
 xen/arch/arm/include/asm/arm32/flushtlb.h | 87 +++++++++++++++++++++
 xen/arch/arm/include/asm/arm64/flushtlb.h | 77 +++++++++++++++++++
 xen/arch/arm/include/asm/cpregs.h         |  4 +
 xen/arch/arm/include/asm/cpufeature.h     | 27 ++++++-
 xen/arch/arm/include/asm/mmu/p2m.h        |  2 +
 xen/arch/arm/include/asm/processor.h      | 10 +++
 xen/arch/arm/mmu/p2m.c                    | 92 +++++++++++++++++------
 8 files changed, 302 insertions(+), 28 deletions(-)

-- 
2.43.0



^ permalink raw reply	[flat|nested] 25+ messages in thread

* [XEN PATCH v3 1/3] xen/arm/p2m: perform IPA-based TLBI when IPA is known
@ 2026-01-18 13:33   ` Haseeb Ashraf
  0 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-01-18 13:33 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

This commit addresses a major issue for running Xen on KVM i.e.
costly emulation of VMALLS12E1IS which becomes worse when this TLBI
is invoked too many times. There are mainly two places where this is
problematic:
(a) When vCPUs switch on a pCPU or pCPUs
(b) When domu mapped pages onto dom0, are to be unmapped, then each
    page being removed by XENMEM_remove_from_physmap has its TLBs
    invalidated by VMALLS12E1IS.

The first one is addressed by relaxing VMALLS12E1 -> VMALLE1 as the
stage-2 is common between all the vCPUs of a VM. Since each CPU has
its own private TLBs, so flush between vCPU of the same domains is
still required to avoid translations from vCPUx to "leak" to the
vCPUy which can be achieved by using VMALLE1.

The second one is addressed by using IPA-based TLBI (IPAS2E1) in
combination with VMALLE1 whenever the IPA range is known instead of
using VMALLS12E1. There is an upper cap placed on number of IPA-based
TLBI. This factor for execution time of VMALLS12E1 vs IPAS2E1 is
found to be 70K on Graviton4 in Xen on KVM virtualization. So,
64K * 4KB = 256MB is set as the threshold.

For arm32, TLBIALL instruction can invalidate both stage-1 and
stage-2 entries, so using IPA-based TLBI would be redundant as
TLBIALL is required in any case to invalidate corresponding cached
entries from stage-1.

Suggested-by: Julien Grall <julien@xen.org>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Changes in v3:
- Updated IPA-based TLBI sequence to apply ARM64 repeat TLBI
  workaround to only final TLBI and DSB of the sequence.
- Removed TLB_HELPER_IPA and instead directly used the TLBI
  instruction where needed as that was the only instance where it is
  being used.
- Removed flush_guest_tlb_range_ipa_local() as it was not being used.
- Updated comments as per feedback in v2 about holding lock before
  p2m_load_vttbr.
- Updated references of ARM ARM to use newer version DDI 0487L.b
  instead of older version DDI 0487A.e.

Changes in v2:
- This commit implements the basline implementation to address the
  problem at hand. Removed the FEAT_nTLBPA implementation from this
  commit which will be implemented in following commit using CPU
  capability.
- Moved ARM32 and ARM64 specific implementations of TLBIs to
  architecture specific flushtlb.h.
- Added references of ARM ARM in code comments.
- Evaluated and added a threshold to select between IPA-based TLB
  invalidation vs fallback to full stage TLB invalidation above
  the threshold.
---
 xen/arch/arm/include/asm/arm32/flushtlb.h | 53 +++++++++++++
 xen/arch/arm/include/asm/arm64/flushtlb.h | 46 ++++++++++++
 xen/arch/arm/include/asm/mmu/p2m.h        |  2 +
 xen/arch/arm/mmu/p2m.c                    | 92 +++++++++++++++++------
 4 files changed, 168 insertions(+), 25 deletions(-)

diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/include/asm/arm32/flushtlb.h
index 61c25a3189..3c0c2123d4 100644
--- a/xen/arch/arm/include/asm/arm32/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm32/flushtlb.h
@@ -45,6 +45,43 @@ TLB_HELPER(flush_xen_tlb_local, TLBIALLH, nsh)
 
 #undef TLB_HELPER
 
+/*
+ * Flush TLB of local processor. Use when flush for only stage-1 is intended.
+ *
+ * The following function should be used where intention is to clear only
+ * stage-1 TLBs. This would be helpful in future in identifying which stage-1
+ * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ */
+static inline void flush_guest_tlb_s1_local(void)
+{
+    /*
+     * Same instruction can invalidate both stage-1 and stage-2 TLBs depending
+     * upon the execution context.
+     *
+     * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+     */
+    return flush_guest_tlb_local();
+}
+
+/*
+ * Flush TLB of inner-shareable processor domain. Use when flush for only
+ * stage-1 is intended.
+ *
+ * The following function should be used where intention is to clear only
+ * stage-1 TLBs. This would be helpful in future in identifying which stage-1
+ * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ */
+static inline void flush_guest_tlb_s1(void)
+{
+    /*
+     * Same instruction can invalidate both stage-1 and stage-2 TLBs depending
+     * upon the execution context.
+     *
+     * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+     */
+    return flush_guest_tlb();
+}
+
 /* Flush TLB of local processor for address va. */
 static inline void __flush_xen_tlb_one_local(vaddr_t va)
 {
@@ -57,6 +94,22 @@ static inline void __flush_xen_tlb_one(vaddr_t va)
     asm volatile(STORE_CP32(0, TLBIMVAHIS) : : "r" (va) : "memory");
 }
 
+/*
+ * 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 upon
+     * the execution mode.
+     *
+     * See ARMv8 (DDI 0487L.b): G5-11698 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/include/asm/arm64/flushtlb.h
index 3b99c11b50..67ae616993 100644
--- a/xen/arch/arm/include/asm/arm64/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm64/flushtlb.h
@@ -1,6 +1,8 @@
 #ifndef __ASM_ARM_ARM64_FLUSHTLB_H__
 #define __ASM_ARM_ARM64_FLUSHTLB_H__
 
+#include <xen/sizes.h> /* For SZ_* macros. */
+
 /*
  * Every invalidation operation use the following patterns:
  *
@@ -72,6 +74,12 @@ TLB_HELPER(flush_guest_tlb_local, vmalls12e1, nsh)
 /* Flush innershareable TLBs, current VMID only */
 TLB_HELPER(flush_guest_tlb, vmalls12e1is, ish)
 
+/* Flush local TLBs, current VMID, stage-1 only */
+TLB_HELPER(flush_guest_tlb_s1_local, vmalle1, nsh)
+
+/* Flush innershareable TLBs, current VMID, stage-1 only */
+TLB_HELPER(flush_guest_tlb_s1, vmalle1is, ish)
+
 /* Flush local TLBs, all VMIDs, non-hypervisor mode */
 TLB_HELPER(flush_all_guests_tlb_local, alle1, nsh)
 
@@ -90,6 +98,44 @@ TLB_HELPER_VA(__flush_xen_tlb_one, vae2is)
 #undef TLB_HELPER
 #undef TLB_HELPER_VA
 
+/*
+ * 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)
+{
+    paddr_t end;
+
+    /*
+     * If IPA range is too big (empirically found to be 256M), then fallback to
+     * full TLB flush.
+     */
+    if ( size > SZ_256M )
+        return flush_guest_tlb();
+
+    end = ipa + size;
+
+    /*
+     * See ARM ARM DDI 0487L.b D8.17.6.1 (Invalidating TLB entries from stage 2
+     * translations) for details of TLBI sequence.
+     */
+    dsb(ishst); /* Ensure prior page-tables updates have completed */
+    while ( ipa < end )
+    {
+        /* Flush stage-2 TLBs for ipa address */
+        asm_inline volatile (
+            "tlbi ipas2e1is, %0;" : : "r" (ipa >> PAGE_SHIFT) : "memory" );
+        ipa += PAGE_SIZE;
+    }
+    /*
+     * As ARM64_WORKAROUND_REPEAT_TLBI is required to be applied to last TLBI
+     * of the sequence, it is only needed to be handled in the following
+     * invocation. Final dsb() and isb() are also applied in the following
+     * invocation.
+     */
+    flush_guest_tlb_s1();
+}
+
 #endif /* __ASM_ARM_ARM64_FLUSHTLB_H__ */
 /*
  * Local variables:
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;
 
 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);
 
 void p2m_clear_root_pages(struct p2m_domain *p2m);
diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c
index 51abf3504f..eec59056fa 100644
--- a/xen/arch/arm/mmu/p2m.c
+++ b/xen/arch/arm/mmu/p2m.c
@@ -235,33 +235,28 @@ void p2m_restore_state(struct vcpu *n)
      * when running multiple vCPU of the same domain on a single pCPU.
      */
     if ( *last_vcpu_ran != INVALID_VCPU_ID && *last_vcpu_ran != n->vcpu_id )
-        flush_guest_tlb_local();
+        flush_guest_tlb_s1_local();
 
     *last_vcpu_ran = n->vcpu_id;
 }
 
 /*
- * 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 *flags)
 {
-    unsigned long flags = 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 = READ_SYSREG64(VTTBR_EL2);
     if ( ovttbr != p2m->vttbr )
     {
         uint64_t vttbr;
 
-        local_irq_save(flags);
+        local_irq_save(*flags);
 
         /*
          * ARM64_WORKAROUND_AT_SPECULATE: We need to stop AT to allocate
@@ -280,8 +275,14 @@ void p2m_force_tlb_flush_sync(struct p2m_domain *p2m)
         isb();
     }
 
-    flush_guest_tlb();
+    return ovttbr;
+}
 
+/*
+ * 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 != READ_SYSREG64(VTTBR_EL2) )
     {
         WRITE_SYSREG64(ovttbr, VTTBR_EL2);
@@ -289,10 +290,58 @@ 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 = 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 = p2m_load_vttbr(p2m, &flags);
+
+    flush_guest_tlb();
+
+    p2m_restore_vttbr(ovttbr, flags);
 
     p2m->need_flush = false;
 }
 
+/*
+ * 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 = 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 = p2m_load_vttbr(p2m, &flags);
+
+    /* Invalidate TLB entries by IPA range */
+    flush_guest_tlb_range_ipa(start_ipa, PAGE_SIZE * page_count);
+
+    p2m_restore_vttbr(ovttbr, flags);
+}
+
 void p2m_tlb_flush_sync(struct p2m_domain *p2m)
 {
     if ( p2m->need_flush )
@@ -1034,7 +1083,8 @@ 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, gfn_x(sgfn) << PAGE_SHIFT,
+                                       1UL << page_order);
 
         p2m_write_pte(entry, split_pte, p2m->clean_pte);
 
@@ -1090,8 +1140,8 @@ static int __p2m_set_entry(struct p2m_domain *p2m,
         p2m_remove_pte(entry, p2m->clean_pte);
 
     if ( removing_mapping )
-        /* Flush can be deferred if the entry is removed */
-        p2m->need_flush |= !!lpae_is_valid(orig_pte);
+        p2m_force_tlb_flush_range_sync(p2m, gfn_x(sgfn) << PAGE_SHIFT,
+                                       1UL << page_order);
     else
     {
         lpae_t pte = mfn_to_p2m_entry(smfn, t, a);
@@ -1102,18 +1152,10 @@ static int __p2m_set_entry(struct p2m_domain *p2m,
         /*
          * It is necessary to flush the TLB before writing the new entry
          * to keep coherency when the previous entry was valid.
-         *
-         * Although, it could be defered when only the permissions are
-         * changed (e.g in case of memaccess).
          */
         if ( lpae_is_valid(orig_pte) )
-        {
-            if ( likely(!p2m->mem_access_enabled) ||
-                 P2M_CLEAR_PERM(pte) != P2M_CLEAR_PERM(orig_pte) )
-                p2m_force_tlb_flush_sync(p2m);
-            else
-                p2m->need_flush = true;
-        }
+            p2m_force_tlb_flush_range_sync(p2m, gfn_x(sgfn) << PAGE_SHIFT,
+                                           1UL << page_order);
         else if ( !p2m_is_valid(orig_pte) ) /* new mapping */
             p2m->stats.mappings[level]++;
 
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN PATCH v3 2/3] xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA
@ 2026-01-18 13:33   ` Haseeb Ashraf
  0 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-01-18 13:33 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk, Mohamed Mediouni

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

FEAT_nTLBPA (quoting definition) introduces a mechanism to identify
if the intermediate caching of translation table walks does not
include non-coherent caches of previous valid translation table
entries since the last completed TLBI applicable to the PE.

As there won't be any non-coherent caches since the last completed
TLBI, stage-1 TLBI won't be required while performing stage-2 TLBI.

This feature is optionally available in both arm32 and arm64.

Suggested-by: Mohamed Mediouni <mohamed@unpredictable.fr>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Changes in v3:
- This commit has no functional change in v3, only rebasing changes
  due to updates in commit-1.

Changes in v2:
- This commit is implemented in v2 and is splitted from commit-1 in
  v1. This is implemented by using CPU capability.
---
 xen/arch/arm/cpufeature.c                 | 19 ++++++
 xen/arch/arm/include/asm/arm32/flushtlb.h | 14 +++--
 xen/arch/arm/include/asm/arm64/flushtlb.h | 77 ++++++++++++++++-------
 xen/arch/arm/include/asm/cpufeature.h     | 24 ++++++-
 xen/arch/arm/include/asm/processor.h      |  7 +++
 5 files changed, 109 insertions(+), 32 deletions(-)

diff --git a/xen/arch/arm/cpufeature.c b/xen/arch/arm/cpufeature.c
index 1a80738571..9fa1c45869 100644
--- a/xen/arch/arm/cpufeature.c
+++ b/xen/arch/arm/cpufeature.c
@@ -17,7 +17,19 @@ DECLARE_BITMAP(cpu_hwcaps, ARM_NCAPS);
 
 struct cpuinfo_arm __read_mostly domain_cpuinfo;
 
+#ifdef CONFIG_ARM_32
+static bool has_ntlbpa(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm32.ntlbpa == MM32_NTLBPA_SUPPORT_IMP;
+}
+#endif
+
 #ifdef CONFIG_ARM_64
+static bool has_ntlbpa(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm64.ntlbpa == MM64_NTLBPA_SUPPORT_IMP;
+}
+
 static bool has_sb_instruction(const struct arm_cpu_capabilities *entry)
 {
     return system_cpuinfo.isa64.sb;
@@ -25,6 +37,13 @@ static bool has_sb_instruction(const struct arm_cpu_capabilities *entry)
 #endif
 
 static const struct arm_cpu_capabilities arm_features[] = {
+#if defined(CONFIG_ARM_32) || defined(CONFIG_ARM_64)
+    {
+        .desc = "Intermediate caching of translation table walks (nTLBPA)",
+        .capability = ARM_HAS_NTLBPA,
+        .matches = has_ntlbpa,
+    },
+#endif
 #ifdef CONFIG_ARM_64
     {
         .desc = "Speculation barrier instruction (SB)",
diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/include/asm/arm32/flushtlb.h
index 3c0c2123d4..7cff042508 100644
--- a/xen/arch/arm/include/asm/arm32/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm32/flushtlb.h
@@ -49,8 +49,8 @@ TLB_HELPER(flush_xen_tlb_local, TLBIALLH, nsh)
  * Flush TLB of local processor. Use when flush for only stage-1 is intended.
  *
  * The following function should be used where intention is to clear only
- * stage-1 TLBs. This would be helpful in future in identifying which stage-1
- * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ * stage-1 TLBs. This would be helpful in identifying which stage-1 TLB flushes
+ * can be skipped such as in present of FEAT_nTLBPA.
  */
 static inline void flush_guest_tlb_s1_local(void)
 {
@@ -60,7 +60,8 @@ static inline void flush_guest_tlb_s1_local(void)
      *
      * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
      */
-    return flush_guest_tlb_local();
+    if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) )
+        flush_guest_tlb_local();
 }
 
 /*
@@ -68,8 +69,8 @@ static inline void flush_guest_tlb_s1_local(void)
  * stage-1 is intended.
  *
  * The following function should be used where intention is to clear only
- * stage-1 TLBs. This would be helpful in future in identifying which stage-1
- * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ * stage-1 TLBs. This would be helpful in identifying which stage-1 TLB flushes
+ * can be skipped such as in present of FEAT_nTLBPA.
  */
 static inline void flush_guest_tlb_s1(void)
 {
@@ -79,7 +80,8 @@ static inline void flush_guest_tlb_s1(void)
      *
      * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
      */
-    return flush_guest_tlb();
+    if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) )
+        flush_guest_tlb();
 }
 
 /* Flush TLB of local processor for address va. */
diff --git a/xen/arch/arm/include/asm/arm64/flushtlb.h b/xen/arch/arm/include/asm/arm64/flushtlb.h
index 67ae616993..0f0d5050e5 100644
--- a/xen/arch/arm/include/asm/arm64/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm64/flushtlb.h
@@ -47,6 +47,24 @@ static inline void name(void)                    \
         : : : "memory");                         \
 }
 
+#define TLB_HELPER_NTLBPA(name, tlbop, sh)           \
+static inline void name(void)                        \
+{                                                    \
+    if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) )      \
+        asm_inline volatile (                        \
+            "dsb  "  # sh  "st;"                     \
+            "tlbi "  # tlbop  ";"                    \
+            ALTERNATIVE(                             \
+                "nop; nop;",                         \
+                "dsb  ish;"                          \
+                "tlbi "  # tlbop  ";",               \
+                ARM64_WORKAROUND_REPEAT_TLBI,        \
+                CONFIG_ARM64_WORKAROUND_REPEAT_TLBI) \
+            "dsb  "  # sh  ";"                       \
+            "isb;"                                   \
+            : : : "memory");                         \
+}
+
 /*
  * FLush TLB by VA. This will likely be used in a loop, so the caller
  * is responsible to use the appropriate memory barriers before/after
@@ -75,10 +93,10 @@ TLB_HELPER(flush_guest_tlb_local, vmalls12e1, nsh)
 TLB_HELPER(flush_guest_tlb, vmalls12e1is, ish)
 
 /* Flush local TLBs, current VMID, stage-1 only */
-TLB_HELPER(flush_guest_tlb_s1_local, vmalle1, nsh)
+TLB_HELPER_NTLBPA(flush_guest_tlb_s1_local, vmalle1, nsh)
 
 /* Flush innershareable TLBs, current VMID, stage-1 only */
-TLB_HELPER(flush_guest_tlb_s1, vmalle1is, ish)
+TLB_HELPER_NTLBPA(flush_guest_tlb_s1, vmalle1is, ish)
 
 /* Flush local TLBs, all VMIDs, non-hypervisor mode */
 TLB_HELPER(flush_all_guests_tlb_local, alle1, nsh)
@@ -104,8 +122,6 @@ TLB_HELPER_VA(__flush_xen_tlb_one, vae2is)
  */
 static inline void flush_guest_tlb_range_ipa(paddr_t ipa, unsigned long size)
 {
-    paddr_t end;
-
     /*
      * If IPA range is too big (empirically found to be 256M), then fallback to
      * full TLB flush.
@@ -113,27 +129,42 @@ static inline void flush_guest_tlb_range_ipa(paddr_t ipa, unsigned long size)
     if ( size > SZ_256M )
         return flush_guest_tlb();
 
-    end = ipa + size;
-
-    /*
-     * See ARM ARM DDI 0487L.b D8.17.6.1 (Invalidating TLB entries from stage 2
-     * translations) for details of TLBI sequence.
-     */
-    dsb(ishst); /* Ensure prior page-tables updates have completed */
-    while ( ipa < end )
+    else if ( size > 0 )
     {
-        /* Flush stage-2 TLBs for ipa address */
-        asm_inline volatile (
-            "tlbi ipas2e1is, %0;" : : "r" (ipa >> PAGE_SHIFT) : "memory" );
-        ipa += PAGE_SIZE;
+        paddr_t end = ipa + size;
+
+        /*
+         * See ARM ARM DDI 0487L.b D8.17.6.1 (Invalidating TLB entries from
+         * stage 2 translations) for details on TLBI sequence.
+         */
+        dsb(ishst); /* Ensure prior page-tables updates have completed */
+        while ( ipa < end )
+        {
+            /* Flush stage-2 TLBs for ipa address */
+            asm_inline volatile (
+                "tlbi ipas2e1is, %0;" : : "r" (ipa >> PAGE_SHIFT) : "memory" );
+            ipa += PAGE_SIZE;
+        }
+        if ( cpus_have_const_cap(ARM_HAS_NTLBPA) )
+            asm_inline volatile (
+                ALTERNATIVE(
+                    "nop; nop;",
+                    "dsb  ish;"
+                    "tlbi ipas2e1is, %0;",
+                    ARM64_WORKAROUND_REPEAT_TLBI,
+                    CONFIG_ARM64_WORKAROUND_REPEAT_TLBI)
+                "dsb  ish;"
+                "isb;"
+                : : "r" ((ipa - PAGE_SIZE) >> PAGE_SHIFT) : "memory" );
+        else
+            /*
+             * As ARM64_WORKAROUND_REPEAT_TLBI is required to be applied to
+             * last TLBI of the sequence, it is only needed to be handled in
+             * the following invocation. Final dsb() and isb() are also applied
+             * in the following invocation.
+             */
+            flush_guest_tlb_s1();
     }
-    /*
-     * As ARM64_WORKAROUND_REPEAT_TLBI is required to be applied to last TLBI
-     * of the sequence, it is only needed to be handled in the following
-     * invocation. Final dsb() and isb() are also applied in the following
-     * invocation.
-     */
-    flush_guest_tlb_s1();
 }
 
 #endif /* __ASM_ARM_ARM64_FLUSHTLB_H__ */
diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/asm/cpufeature.h
index 13353c8e1a..9f796ed4c1 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
 
-#define ARM_NCAPS           18
+#define ARM_NCAPS           19
 
 #ifndef __ASSEMBLER__
 
@@ -269,7 +270,8 @@ struct cpuinfo_arm {
             unsigned long ets:4;
             unsigned long __res1:4;
             unsigned long afp:4;
-            unsigned long __res2:12;
+            unsigned long ntlbpa:4;
+            unsigned long __res2:8;
             unsigned long ecbhb:4;
 
             /* MMFR2 */
@@ -430,8 +432,24 @@ struct cpuinfo_arm {
         register_t bits[1];
     } aux32;
 
-    struct {
+    union {
         register_t bits[6];
+        struct {
+            /* MMFR0 */
+            unsigned long __res0:32;
+            /* MMFR1 */
+            unsigned long __res1:32;
+            /* MMFR2 */
+            unsigned long __res2:32;
+            /* MMFR3 */
+            unsigned long __res3:32;
+            /* MMFR4 */
+            unsigned long __res4:32;
+            /* MMFR5 */
+            unsigned long __res5:4;
+            unsigned long ntlbpa:4;
+            unsigned long __res6:24;
+        };
     } mm32;
 
     struct {
diff --git a/xen/arch/arm/include/asm/processor.h b/xen/arch/arm/include/asm/processor.h
index 1a48c9ff3b..85f3b643a0 100644
--- a/xen/arch/arm/include/asm/processor.h
+++ b/xen/arch/arm/include/asm/processor.h
@@ -459,9 +459,16 @@
 /* FSR long format */
 #define FSRL_STATUS_DEBUG       (_AC(0x22,UL)<<0)
 
+#ifdef CONFIG_ARM_32
+#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
+#define MM64_NTLBPA_SUPPORT_NI      0x0
+#define MM64_NTLBPA_SUPPORT_IMP     0x1
 #endif
 
 #ifndef __ASSEMBLER__
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN PATCH v3 3/3] xen/arm32: add CPU capability for IPA-based TLBI
@ 2026-01-18 13:33   ` Haseeb Ashraf
  0 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-01-18 13:33 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

This feature is available since armv8 and can be used to perform
IPA-based TLBI for arm32. XENMEM_remove_from_physmap performs this
invalidation in each hypercall so this code path will be optimized,
instead of performing a TLBIALL each time in presence of nTLBPA.

Suggested-by: Julien Grall <julien@xen.org>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Changes in v3:
- There are no functional changes in this version. There are minor
  code updates and comment updates as per the feedback on v2.
- The cpregs are defined in order as per Coprocessor-> CRn-> Opcode 1
  -> CRm-> Opcode 2.
- Added comment to explain why IPA-based TLBI is added only in
  presence of FEAT_nTLBPA.
- Replaced `goto default_tlbi` with if...else.
- Removed extra definitions of MM32_UNITLB_* macros which were not
  being used.

Changes in v2:
- This commit is implemented in v2 as per the feedback to implement
  IPA-based TLBI for Arm32 in addition to Arm64.
---
 xen/arch/arm/cpufeature.c                 | 12 +++++++
 xen/arch/arm/include/asm/arm32/flushtlb.h | 42 ++++++++++++++++++++---
 xen/arch/arm/include/asm/cpregs.h         |  4 +++
 xen/arch/arm/include/asm/cpufeature.h     | 15 ++++----
 xen/arch/arm/include/asm/processor.h      |  3 ++
 5 files changed, 65 insertions(+), 11 deletions(-)

diff --git a/xen/arch/arm/cpufeature.c b/xen/arch/arm/cpufeature.c
index 9fa1c45869..d18c6449c6 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;
 
 #ifdef CONFIG_ARM_32
+static bool has_tlb_ipa_instruction(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm32.unitlb == MM32_UNITLB_BY_IPA;
+}
+
 static bool has_ntlbpa(const struct arm_cpu_capabilities *entry)
 {
     return system_cpuinfo.mm32.ntlbpa == MM32_NTLBPA_SUPPORT_IMP;
@@ -37,6 +42,13 @@ static bool has_sb_instruction(const struct arm_cpu_capabilities *entry)
 #endif
 
 static const struct arm_cpu_capabilities arm_features[] = {
+#ifdef CONFIG_ARM_32
+    {
+        .desc = "IPA-based TLB Invalidation",
+        .capability = ARM32_HAS_TLB_IPA,
+        .matches = has_tlb_ipa_instruction,
+    },
+#endif
 #if defined(CONFIG_ARM_32) || defined(CONFIG_ARM_64)
     {
         .desc = "Intermediate caching of translation table walks (nTLBPA)",
diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/include/asm/arm32/flushtlb.h
index 7cff042508..3e6f86f6d2 100644
--- a/xen/arch/arm/include/asm/arm32/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm32/flushtlb.h
@@ -1,6 +1,8 @@
 #ifndef __ASM_ARM_ARM32_FLUSHTLB_H__
 #define __ASM_ARM_ARM32_FLUSHTLB_H__
 
+#include <xen/sizes.h> /* For SZ_* macros. */
+
 /*
  * Every invalidation operation use the following patterns:
  *
@@ -104,12 +106,42 @@ 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 upon
-     * the execution mode.
-     *
-     * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+     * IPA-based TLBI is used only in presence of nTLBPA, otherwise, stage-1
+     * invalidation would still be required and there is no separate TLBI for
+     * stage-1 on Arm32. So in absence of nTLBPA, it is pointless to flush by
+     * IPA.
      */
-    flush_guest_tlb();
+    if ( cpus_have_const_cap(ARM_HAS_NTLBPA) &&
+         cpus_have_const_cap(ARM32_HAS_TLB_IPA) )
+    {
+        /*
+         * If IPA range is too big (empirically found to be 256M), then
+         * fallback to full TLB flush
+         */
+        if ( size > SZ_256M )
+            /*
+             * Following can invalidate both stage-1 and stage-2 TLBs depending
+             * upon the execution mode.
+             *
+             * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+             */
+            flush_guest_tlb();
+        else
+        {
+            paddr_t end = ipa + size;
+
+            dsb(ishst); /* Ensure prior page-tables updates have completed */
+            while ( ipa < end )
+            {
+                /* Flush stage-2 TLBs for ipa address. */
+                asm volatile(STORE_CP32(0, TLBIIPAS2IS)
+                             : : "r" (ipa >> PAGE_SHIFT) : "memory");
+                ipa += PAGE_SIZE;
+            }
+            dsb(ish);
+            isb();
+        }
+    }
 }
 
 #endif /* __ASM_ARM_ARM32_FLUSHTLB_H__ */
diff --git a/xen/arch/arm/include/asm/cpregs.h b/xen/arch/arm/include/asm/cpregs.h
index a7503a190f..51f091dace 100644
--- a/xen/arch/arm/include/asm/cpregs.h
+++ b/xen/arch/arm/include/asm/cpregs.h
@@ -223,9 +223,13 @@
 #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 match */
 #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 for stage 2 by IPA inner shareable */
+#define TLBIIPAS2LIS    p15,4,c8,c0,5   /* Invalidate unified TLB entry for stage 2 last level 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 Non-Hyp. Unified TLB inner shareable */
+#define TLBIIPAS2       p15,4,c8,c4,1   /* Invalidate unified TLB entry for stage 2 by IPA */
+#define TLBIIPAS2L      p15,4,c8,c4,5   /* Invalidate unified TLB entry for stage 2 last level by IPA */
 #define TLBIALLH        p15,4,c8,c7,0   /* Invalidate Entire Hyp. Unified TLB */
 #define TLBIMVAH        p15,4,c8,c7,1   /* Invalidate Unified Hyp. TLB by MVA */
 #define TLBIALLNSNH     p15,4,c8,c7,4   /* Invalidate Entire Non-Secure Non-Hyp. Unified TLB */
diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/asm/cpufeature.h
index 9f796ed4c1..07f1d770b3 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
 
-#define ARM_NCAPS           19
+#define ARM_NCAPS           20
 
 #ifndef __ASSEMBLER__
 
@@ -440,15 +441,17 @@ struct cpuinfo_arm {
             /* MMFR1 */
             unsigned long __res1:32;
             /* MMFR2 */
-            unsigned long __res2:32;
+            unsigned long __res2:16;
+            unsigned long unitlb:4;
+            unsigned long __res3:12;
             /* MMFR3 */
-            unsigned long __res3:32;
-            /* MMFR4 */
             unsigned long __res4:32;
+            /* MMFR4 */
+            unsigned long __res5:32;
             /* MMFR5 */
-            unsigned long __res5:4;
+            unsigned long __res6:4;
             unsigned long ntlbpa:4;
-            unsigned long __res6:24;
+            unsigned long __res7:24;
         };
     } mm32;
 
diff --git a/xen/arch/arm/include/asm/processor.h b/xen/arch/arm/include/asm/processor.h
index 85f3b643a0..eda39566e1 100644
--- a/xen/arch/arm/include/asm/processor.h
+++ b/xen/arch/arm/include/asm/processor.h
@@ -460,6 +460,9 @@
 #define FSRL_STATUS_DEBUG       (_AC(0x22,UL)<<0)
 
 #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
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN][RESEND][PATCH v4 0/3] xen/arm{32,64}: perform IPA-based TLBI when IPA is known
@ 2026-01-18 13:33 ` Haseeb Ashraf
  0 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-07-21  7:54 UTC (permalink / raw)
  To: xen-devel
  Cc: haseeb.ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

This patch series addresses a major performance issue when running Xen as
an L1 hypervisor on KVM, where emulation of VMALLS12E1IS is significantly
more expensive than narrower invalidation sequences and becomes especially
problematic when invoked frequently.

Two common cases are affected:

  (a) vCPU migration on a pCPU, where Xen currently flushes combined
      stage-1+stage-2 guest translations even though the stage-2 regime is
      shared at the domain p2m level.

  (b) teardown of DomU mappings exported into Dom0, where
      XENMEM_remove_from_physmap can trigger repeated full guest TLB
      invalidation even when the affected IPA range is known.

This series reduces reliance on full guest TLB invalidation and prefers
IPA-based invalidation where possible.

Patch 1 introduces IPA-based invalidation in the p2m code when the
affected IPA range is known, and uses stage-1-only invalidation for the
vCPU migration case.

Patch 2 optimizes combined stage-1/stage-2 invalidation in the presence
of FEAT_nTLBPA by avoiding redundant stage-1 invalidation.

Patch 3 adds Arm32 capability detection for TLBI-by-IPA and uses it in
the IPA-range helper when FEAT_nTLBPA is also present. When that
combination is unavailable, Arm32 retains the conservative full-flush
behaviour.

The Arm64 IPA-range helper has also been reworked in v4 to make the TLBI
sequence more explicit:
  - stage-2 invalidation by IPA is performed in a loop,
  - stage-1 invalidation is issued separately only when required,
  - completion of the broadcast TLBI sequence (including the repeat-TLBI
    workaround) is handled explicitly rather than being piggybacked onto a
    stage-1 helper.

This makes the intended semantics clearer and avoids relying on hidden
barrier/workaround side effects.

Measured on Graviton4 with Xen-on-KVM, a full guest TLBI was observed to
be roughly comparable in cost to tens of thousands of single-page
IPA-based invalidations, which motivates using a bounded IPA-based loop
for smaller ranges and falling back to full invalidation above a
threshold.

For Arm64, the 256MB threshold is based on those measurements. For Arm32,
the same cutoff is reused as a conservative practical bound rather than
as an Arm32-specific tuned value.

Haseeb Ashraf (3):
  xen/arm/p2m: perform IPA-based TLBI when IPA is known
  xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA
  xen/arm32: use IPA-based TLBI when supported with FEAT_nTLBPA

Changes in v4:
- Rebased on master branch
- Reworked the Arm64 IPA-based TLBI sequence to make completion of the
  broadcast TLBI explicit instead of relying on a stage-1 helper for the
  final barriers/workaround.
- Reused __tlb_repeat_sync() for repeat-TLBI workaround handling in the
  Arm64 completion path.
- Fixed the Arm32 IPA-based TLBI helper to fall back to a full guest TLB
  flush when the required capability combination is unavailable, instead
  of becoming a no-op.
- Added a small robustness check in p2m_force_tlb_flush_range_sync().
- Refined comments and commit messages to make the architectural intent
  clearer.

Changes in v3:
- Mainly reworked handling of the repeat-TLBI workaround with IPA-based
  TLBI so that the extra TLBI and DSB are applied only at completion of
  the overall sequence.
- Updated code comments as per feedback. Further details are available in
  each commit's changelog.
- Minor code updates as per feedback. Further details are available in
  each commit's changelog.

Changes in v2:
- Split the original change into three commits. The first commit
  implements the baseline implementation without adding new CPU
  capabilities. CPU capability handling is introduced in follow-up
  commits to make each optimization clearer.
- Moved Arm32 and Arm64 specific TLBI implementations to the respective
  architecture-specific flushtlb.h.
- Added ARM ARM references in code comments.
- Added a threshold to select between IPA-based invalidation and
  fallback to full guest invalidation above that threshold.
- Introduced ARM_HAS_NTLBPA CPU capability for both Arm32 and Arm64.
- Introduced ARM32_HAS_TLB_IPA CPU capability for Arm32 IPA-based TLBI.

-- 
2.43.0


^ permalink raw reply	[flat|nested] 25+ messages in thread

* [XEN][RESEND][PATCH 1/3] xen/arm/p2m: perform IPA-based TLBI when IPA is known
  2026-01-18 13:33 ` [XEN PATCH v3 " Haseeb Ashraf
                   ` (5 preceding siblings ...)
  (?)
@ 2026-07-21  7:54 ` Haseeb Ashraf
  2026-07-21  7:54   ` [XEN][RESEND][PATCH 2/3] xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA Haseeb Ashraf
  2026-07-21  7:54   ` [XEN][RESEND][PATCH 3/3] xen/arm32: use IPA-based TLBI when supported with FEAT_nTLBPA Haseeb Ashraf
  -1 siblings, 2 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-07-21  7:54 UTC (permalink / raw)
  To: xen-devel
  Cc: haseeb.ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Running Xen as an L1 hypervisor on KVM exposes the high cost of
emulating VMALLS12E1IS, especially when it is issued repeatedly.

Two p2m-related paths are particularly affected:

  (a) vCPU migration on a pCPU, where the flush is intended to avoid
      stage-1 translations associated with one vCPU leaking into
      another, even though the effective stage-2 translation regime is
      shared at the domain p2m level.

  (b) p2m updates where the affected IPA range is known, for example
      when XENMEM_remove_from_physmap removes mappings one page at a
      time.

Address both cases by narrowing the invalidation where possible.

For the vCPU migration case, replace the combined guest flush with a
stage-1-only invalidation. This preserves the required separation
between per-vCPU stage-1 translations while avoiding unnecessary
stage-2 invalidation.

For p2m updates where the affected IPA range is known, invalidate by
IPA range instead of always flushing the whole guest TLB. Since the
Arm64 TLBI-by-IPA sequence loops per page, cap the range at an
empirically chosen threshold and fall back to the full guest flush
above that point.

On Graviton4 running Xen-on-KVM, the cost of a full guest TLBI was
measured to be roughly comparable to tens of thousands of single-page
IPA invalidations, so a 256MB threshold is used as a practical cutoff.

On Arm32 this patch keeps the conservative full-flush behaviour for the
IPA-range helper. Follow-up patches will refine that path further.

Suggested-by: Julien Grall <julien@xen.org>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Changes in v4:
- Reworked the Arm64 IPA-based TLBI sequence to make completion of the
  broadcast TLBI explicit instead of relying on a stage-1 helper for
  the final barriers/workaround.
- Reused __tlb_repeat_sync() for the repeat-TLBI workaround handling.
- Added a small robustness check in p2m_force_tlb_flush_range_sync().
- Refined comments and commit message wording.

Changes in v3:
- Updated IPA-based TLBI sequence to apply ARM64 repeat TLBI
  workaround to only final TLBI and DSB of the sequence.
- Removed TLB_HELPER_IPA and instead directly used the TLBI
  instruction where needed as that was the only instance where it is
  being used.
- Removed flush_guest_tlb_range_ipa_local() as it was not being used.
- Updated comments as per feedback in v2 about holding lock before
  p2m_load_vttbr.
- Updated references of ARM ARM to use newer version DDI 0487L.b
  instead of older version DDI 0487A.e.

Changes in v2:
- This commit implements the baseline implementation to address the
  problem at hand. Removed the FEAT_nTLBPA implementation from this
  commit which will be implemented in following commit using CPU
  capability.
- Moved ARM32 and ARM64 specific implementations of TLBIs to
  architecture specific flushtlb.h.
- Added references of ARM ARM in code comments.
- Evaluated and added a threshold to select between IPA-based TLB
  invalidation vs fallback to full stage TLB invalidation above
  the threshold.
---
 xen/arch/arm/include/asm/arm32/flushtlb.h |  53 +++++++++++
 xen/arch/arm/include/asm/arm64/flushtlb.h |  62 +++++++++++++
 xen/arch/arm/include/asm/mmu/p2m.h        |   2 +
 xen/arch/arm/mmu/p2m.c                    | 105 ++++++++++++++++------
 4 files changed, 195 insertions(+), 27 deletions(-)

diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/include/asm/arm32/flushtlb.h
index 5483be08fb..1497f16ba5 100644
--- a/xen/arch/arm/include/asm/arm32/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm32/flushtlb.h
@@ -45,6 +45,43 @@ TLB_HELPER(flush_xen_tlb_local, TLBIALLH, nsh)
 
 #undef TLB_HELPER
 
+/*
+ * Flush TLB of local processor. Use when flush for only stage-1 is intended.
+ *
+ * The following function should be used where intention is to clear only
+ * stage-1 TLBs. This would be helpful in future in identifying which stage-1
+ * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ */
+static inline void flush_guest_tlb_s1_local(void)
+{
+    /*
+     * Same instruction can invalidate both stage-1 and stage-2 TLBs depending
+     * upon the execution context.
+     *
+     * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+     */
+    return flush_guest_tlb_local();
+}
+
+/*
+ * Flush TLB of inner-shareable processor domain. Use when flush for only
+ * stage-1 is intended.
+ *
+ * The following function should be used where intention is to clear only
+ * stage-1 TLBs. This would be helpful in future in identifying which stage-1
+ * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ */
+static inline void flush_guest_tlb_s1(void)
+{
+    /*
+     * Same instruction can invalidate both stage-1 and stage-2 TLBs depending
+     * upon the execution context.
+     *
+     * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+     */
+    return flush_guest_tlb();
+}
+
 /* Flush TLB of local processor for address va. */
 static inline void __flush_xen_tlb_one_local(vaddr_t va)
 {
@@ -60,6 +97,22 @@ static inline void __flush_xen_tlb_one(vaddr_t va)
 /* Only for ARM64_WORKAROUND_REPEAT_TLBI */
 static inline void __tlb_repeat_sync(void) {}
 
+/*
+ * 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 upon
+     * the execution mode.
+     *
+     * See ARMv8 (DDI 0487L.b): G5-11698 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/include/asm/arm64/flushtlb.h
index 1606b26bf2..cbe2a0da57 100644
--- a/xen/arch/arm/include/asm/arm64/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm64/flushtlb.h
@@ -1,6 +1,8 @@
 #ifndef __ASM_ARM_ARM64_FLUSHTLB_H__
 #define __ASM_ARM_ARM64_FLUSHTLB_H__
 
+#include <xen/sizes.h> /* For SZ_* macros. */
+
 /*
  * Every invalidation operation use the following patterns:
  *
@@ -67,6 +69,12 @@ TLB_HELPER_LOCAL(flush_guest_tlb_local, vmalls12e1)
 /* Flush innershareable TLBs, current VMID only */
 TLB_HELPER(flush_guest_tlb, vmalls12e1is)
 
+/* Flush local TLBs, current VMID, stage-1 only */
+TLB_HELPER(flush_guest_tlb_s1_local, vmalle1, nsh)
+
+/* Flush innershareable TLBs, current VMID, stage-1 only */
+TLB_HELPER(flush_guest_tlb_s1, vmalle1is, ish)
+
 /* Flush local TLBs, all VMIDs, non-hypervisor mode */
 TLB_HELPER_LOCAL(flush_all_guests_tlb_local, alle1)
 
@@ -116,6 +124,60 @@ static inline void __tlb_repeat_sync(void)
         : : : "memory");
 }
 
+/*
+ * Complete a broadcast TLBI sequence.
+ *
+ * For all relevant errata it is only necessary to execute a single
+ * additional TLBI;DSB sequence after the invalidation sequence has completed.
+ */
+static inline void tlbi_complete_broadcast(void)
+{
+    __tlb_repeat_sync();
+    dsb(ish);
+    isb();
+}
+
+/*
+ * 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)
+{
+    paddr_t end;
+
+    /*
+     * If IPA range is too big (empirically found to be 256M), then fallback to
+     * full TLB flush.
+     */
+    if ( size > SZ_256M )
+        return flush_guest_tlb();
+
+    end = ipa + size;
+
+    /*
+     * See ARM ARM DDI 0487L.b D8.17.6.1 (Invalidating TLB entries from stage 2
+     * translations) for details of TLBI sequence.
+     */
+    dsb(ishst); /* Ensure prior page-table updates have completed */
+    while ( ipa < end )
+    {
+        /* Flush stage-2 TLBs for IPA address */
+        asm_inline volatile(
+            "tlbi ipas2e1is, %0"
+            : : "r" (ipa >> PAGE_SHIFT) : "memory");
+        ipa += PAGE_SIZE;
+    }
+
+    /*
+     * Invalidate stage-1 entries for the current VMID as well. Keep this
+     * separate from completion of the broadcast TLBI sequence so that the
+     * final barriers/workaround are explicit.
+     */
+    asm_inline volatile("tlbi vmalle1is" : : : "memory");
+
+    tlbi_complete_broadcast();
+}
+
 #endif /* __ASM_ARM_ARM64_FLUSHTLB_H__ */
 /*
  * Local variables:
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;
 
 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);
 
 void p2m_clear_root_pages(struct p2m_domain *p2m);
diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c
index 51abf3504f..525afcc210 100644
--- a/xen/arch/arm/mmu/p2m.c
+++ b/xen/arch/arm/mmu/p2m.c
@@ -231,37 +231,33 @@ void p2m_restore_state(struct vcpu *n)
     isb();
 
     /*
-     * Flush local TLB for the domain to prevent wrong TLB translation
-     * when running multiple vCPU of the same domain on a single pCPU.
+     * This only needs to invalidate stage-1 TLB state. All vCPUs of the
+     * domain share the same p2m, and therefore the same effective stage-2
+     * translation regime.
      */
     if ( *last_vcpu_ran != INVALID_VCPU_ID && *last_vcpu_ran != n->vcpu_id )
-        flush_guest_tlb_local();
+        flush_guest_tlb_s1_local();
 
     *last_vcpu_ran = n->vcpu_id;
 }
 
 /*
- * 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 *flags)
 {
-    unsigned long flags = 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 = READ_SYSREG64(VTTBR_EL2);
     if ( ovttbr != p2m->vttbr )
     {
         uint64_t vttbr;
 
-        local_irq_save(flags);
+        local_irq_save(*flags);
 
         /*
          * ARM64_WORKAROUND_AT_SPECULATE: We need to stop AT to allocate
@@ -280,8 +276,14 @@ void p2m_force_tlb_flush_sync(struct p2m_domain *p2m)
         isb();
     }
 
-    flush_guest_tlb();
+    return ovttbr;
+}
 
+/*
+ * 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 != READ_SYSREG64(VTTBR_EL2) )
     {
         WRITE_SYSREG64(ovttbr, VTTBR_EL2);
@@ -289,10 +291,66 @@ 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 = 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 = p2m_load_vttbr(p2m, &flags);
+
+    flush_guest_tlb();
+
+    p2m_restore_vttbr(ovttbr, flags);
 
     p2m->need_flush = false;
 }
 
+/*
+ * 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 = 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 = 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 )
@@ -1034,7 +1092,8 @@ 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, gfn_x(sgfn) << PAGE_SHIFT,
+                                       1UL << page_order);
 
         p2m_write_pte(entry, split_pte, p2m->clean_pte);
 
@@ -1090,8 +1149,8 @@ static int __p2m_set_entry(struct p2m_domain *p2m,
         p2m_remove_pte(entry, p2m->clean_pte);
 
     if ( removing_mapping )
-        /* Flush can be deferred if the entry is removed */
-        p2m->need_flush |= !!lpae_is_valid(orig_pte);
+        p2m_force_tlb_flush_range_sync(p2m, gfn_x(sgfn) << PAGE_SHIFT,
+                                       1UL << page_order);
     else
     {
         lpae_t pte = mfn_to_p2m_entry(smfn, t, a);
@@ -1102,18 +1161,10 @@ static int __p2m_set_entry(struct p2m_domain *p2m,
         /*
          * It is necessary to flush the TLB before writing the new entry
          * to keep coherency when the previous entry was valid.
-         *
-         * Although, it could be defered when only the permissions are
-         * changed (e.g in case of memaccess).
          */
         if ( lpae_is_valid(orig_pte) )
-        {
-            if ( likely(!p2m->mem_access_enabled) ||
-                 P2M_CLEAR_PERM(pte) != P2M_CLEAR_PERM(orig_pte) )
-                p2m_force_tlb_flush_sync(p2m);
-            else
-                p2m->need_flush = true;
-        }
+            p2m_force_tlb_flush_range_sync(p2m, gfn_x(sgfn) << PAGE_SHIFT,
+                                           1UL << page_order);
         else if ( !p2m_is_valid(orig_pte) ) /* new mapping */
             p2m->stats.mappings[level]++;
 
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN][RESEND][PATCH 2/3] xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA
  2026-07-21  7:54 ` [XEN][RESEND][PATCH 1/3] xen/arm/p2m: perform IPA-based TLBI when IPA is known Haseeb Ashraf
@ 2026-07-21  7:54   ` Haseeb Ashraf
  2026-07-21  7:54   ` [XEN][RESEND][PATCH 3/3] xen/arm32: use IPA-based TLBI when supported with FEAT_nTLBPA Haseeb Ashraf
  1 sibling, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-07-21  7:54 UTC (permalink / raw)
  To: xen-devel
  Cc: haseeb.ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk, Mohamed Mediouni

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

FEAT_nTLBPA guarantees that intermediate caching of translation table
walks does not retain non-coherent cached copies of previously valid
translation table entries since the last completed applicable TLBI.

When FEAT_nTLBPA is present, a stage-2 invalidation no longer needs to
be paired with an additional stage-1 invalidation solely to discard
such side effects. Use this to avoid redundant stage-1 invalidation in
the combined guest TLBI paths.

Update the stage-1-only guest TLBI helpers so that they become no-ops
when FEAT_nTLBPA is present. This allows existing call sites that
conservatively issue stage-1 invalidation after stage-2 invalidation to
benefit without further restructuring.

Also update the Arm64 IPA-based invalidation helper so that:
  - stage-2 invalidation by IPA is performed explicitly,
  - stage-1 invalidation is issued separately only when FEAT_nTLBPA is
    absent,
  - completion of the broadcast TLBI sequence remains explicit.

This makes the architectural intent clearer and avoids redundant
invalidations on systems that implement FEAT_nTLBPA.

Suggested-by: Mohamed Mediouni <mohamed@unpredictable.fr>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Changes in v4:
- Reworked the Arm64 IPA-based invalidation helper so that stage-2,
  optional stage-1, and final TLBI completion are expressed
  separately and explicitly.
- Simplified the FEAT_nTLBPA optimization by reusing the explicit TLBI
  completion helper instead of open-coding a special final IPA TLBI
  sequence.
- Refined comments and commit message wording.

Changes in v3:
- This commit has no functional change in v3, only rebasing changes
  due to updates in commit-1.

Changes in v2:
- This commit is implemented in v2 and is splitted from commit-1 in
  v1. This is implemented by using CPU capability.
---
 xen/arch/arm/cpufeature.c                 | 19 ++++++++++
 xen/arch/arm/include/asm/arm32/flushtlb.h | 14 ++++----
 xen/arch/arm/include/asm/arm64/flushtlb.h | 44 +++++++++++++++++++----
 xen/arch/arm/include/asm/cpufeature.h     | 24 +++++++++++--
 xen/arch/arm/include/asm/processor.h      |  7 ++++
 5 files changed, 93 insertions(+), 15 deletions(-)

diff --git a/xen/arch/arm/cpufeature.c b/xen/arch/arm/cpufeature.c
index 94d14fb6a9..86720c10a3 100644
--- a/xen/arch/arm/cpufeature.c
+++ b/xen/arch/arm/cpufeature.c
@@ -17,7 +17,19 @@ DECLARE_BITMAP(cpu_hwcaps, ARM_NCAPS);
 
 struct cpuinfo_arm __read_mostly domain_cpuinfo;
 
+#ifdef CONFIG_ARM_32
+static bool has_ntlbpa(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm32.ntlbpa == MM32_NTLBPA_SUPPORT_IMP;
+}
+#endif
+
 #ifdef CONFIG_ARM_64
+static bool has_ntlbpa(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm64.ntlbpa == MM64_NTLBPA_SUPPORT_IMP;
+}
+
 static bool has_sb_instruction(const struct arm_cpu_capabilities *entry)
 {
     return system_cpuinfo.isa64.sb;
@@ -25,6 +37,13 @@ static bool has_sb_instruction(const struct arm_cpu_capabilities *entry)
 #endif
 
 static const struct arm_cpu_capabilities arm_features[] = {
+#if defined(CONFIG_ARM_32) || defined(CONFIG_ARM_64)
+    {
+        .desc = "Intermediate caching of translation table walks (nTLBPA)",
+        .capability = ARM_HAS_NTLBPA,
+        .matches = has_ntlbpa,
+    },
+#endif
 #ifdef CONFIG_ARM_64
     {
         .desc = "Speculation barrier instruction (SB)",
diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/include/asm/arm32/flushtlb.h
index 1497f16ba5..30df161323 100644
--- a/xen/arch/arm/include/asm/arm32/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm32/flushtlb.h
@@ -49,8 +49,8 @@ TLB_HELPER(flush_xen_tlb_local, TLBIALLH, nsh)
  * Flush TLB of local processor. Use when flush for only stage-1 is intended.
  *
  * The following function should be used where intention is to clear only
- * stage-1 TLBs. This would be helpful in future in identifying which stage-1
- * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ * stage-1 TLBs. This would be helpful in identifying which stage-1 TLB flushes
+ * can be skipped such as in present of FEAT_nTLBPA.
  */
 static inline void flush_guest_tlb_s1_local(void)
 {
@@ -60,7 +60,8 @@ static inline void flush_guest_tlb_s1_local(void)
      *
      * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
      */
-    return flush_guest_tlb_local();
+    if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) )
+        flush_guest_tlb_local();
 }
 
 /*
@@ -68,8 +69,8 @@ static inline void flush_guest_tlb_s1_local(void)
  * stage-1 is intended.
  *
  * The following function should be used where intention is to clear only
- * stage-1 TLBs. This would be helpful in future in identifying which stage-1
- * TLB flushes can be skipped such as in present of FEAT_nTLBPA.
+ * stage-1 TLBs. This would be helpful in identifying which stage-1 TLB flushes
+ * can be skipped such as in present of FEAT_nTLBPA.
  */
 static inline void flush_guest_tlb_s1(void)
 {
@@ -79,7 +80,8 @@ static inline void flush_guest_tlb_s1(void)
      *
      * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
      */
-    return flush_guest_tlb();
+    if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) )
+        flush_guest_tlb();
 }
 
 /* Flush TLB of local processor for address va. */
diff --git a/xen/arch/arm/include/asm/arm64/flushtlb.h b/xen/arch/arm/include/asm/arm64/flushtlb.h
index cbe2a0da57..de266b0e11 100644
--- a/xen/arch/arm/include/asm/arm64/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm64/flushtlb.h
@@ -63,6 +63,36 @@ static inline void name(void)                         \
         : : : "memory"); \
 }
 
+#define TLB_HELPER_NTLBPA_LOCAL(name, tlbop)     \
+static inline void name(void)                    \
+{                                                \
+    if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) )  \
+        asm_inline volatile (                    \
+            "dsb  nshst;"                        \
+            "tlbi "  # tlbop  ";"                \
+            "dsb  nsh;"                          \
+            "isb;"                               \
+            : : : "memory");                     \
+}
+
+#define TLB_HELPER_NTLBPA(name, tlbop)                \
+static inline void name(void)                         \
+{                                                     \
+    if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) )       \
+        asm_inline volatile (                         \
+            "dsb  ishst;"                             \
+            "tlbi "  # tlbop  ";"                     \
+            ALTERNATIVE(                              \
+                "nop; nop;",                          \
+                "dsb  ish;"                           \
+                "tlbi vale2is, xzr;",                 \
+                ARM64_WORKAROUND_REPEAT_TLBI,         \
+                CONFIG_ARM64_WORKAROUND_REPEAT_TLBI)  \
+            "dsb  ish;"                               \
+            "isb;"                                    \
+            : : : "memory");                          \
+}
+
 /* Flush local TLBs, current VMID only. */
 TLB_HELPER_LOCAL(flush_guest_tlb_local, vmalls12e1)
 
@@ -70,10 +100,10 @@ TLB_HELPER_LOCAL(flush_guest_tlb_local, vmalls12e1)
 TLB_HELPER(flush_guest_tlb, vmalls12e1is)
 
 /* Flush local TLBs, current VMID, stage-1 only */
-TLB_HELPER(flush_guest_tlb_s1_local, vmalle1, nsh)
+TLB_HELPER_NTLBPA_LOCAL(flush_guest_tlb_s1_local, vmalle1)
 
 /* Flush innershareable TLBs, current VMID, stage-1 only */
-TLB_HELPER(flush_guest_tlb_s1, vmalle1is, ish)
+TLB_HELPER_NTLBPA(flush_guest_tlb_s1, vmalle1is)
 
 /* Flush local TLBs, all VMIDs, non-hypervisor mode */
 TLB_HELPER_LOCAL(flush_all_guests_tlb_local, alle1)
@@ -86,6 +116,8 @@ TLB_HELPER_LOCAL(flush_xen_tlb_local, alle2)
 
 #undef TLB_HELPER_LOCAL
 #undef TLB_HELPER
+#undef TLB_HELPER_NTLBPA_LOCAL
+#undef TLB_HELPER_NTLBPA
 
 /*
  * FLush TLB by VA. This will likely be used in a loop, so the caller
@@ -169,11 +201,11 @@ static inline void flush_guest_tlb_range_ipa(paddr_t ipa, unsigned long size)
     }
 
     /*
-     * Invalidate stage-1 entries for the current VMID as well. Keep this
-     * separate from completion of the broadcast TLBI sequence so that the
-     * final barriers/workaround are explicit.
+     * Without FEAT_nTLBPA, invalidate corresponding stage-1 side effects
+     * separately. Completion of the broadcast TLBI sequence remains explicit.
      */
-    asm_inline volatile("tlbi vmalle1is" : : : "memory");
+    if ( !cpus_have_const_cap(ARM_HAS_NTLBPA) )
+        asm_inline volatile("tlbi vmalle1is" : : : "memory");
 
     tlbi_complete_broadcast();
 }
diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/asm/cpufeature.h
index bf902a3970..3e56dd27ef 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
 
-#define ARM_NCAPS           18
+#define ARM_NCAPS           19
 
 #ifndef __ASSEMBLER__
 
@@ -276,7 +277,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;
 
             /* MMFR2 */
@@ -437,8 +439,24 @@ struct cpuinfo_arm {
         register_t bits[1];
     } aux32;
 
-    struct {
+    union {
         register_t bits[6];
+        struct {
+            /* MMFR0 */
+            unsigned long __res0:32;
+            /* MMFR1 */
+            unsigned long __res1:32;
+            /* MMFR2 */
+            unsigned long __res2:32;
+            /* MMFR3 */
+            unsigned long __res3:32;
+            /* MMFR4 */
+            unsigned long __res4:32;
+            /* MMFR5 */
+            unsigned long __res5:4;
+            unsigned long ntlbpa:4;
+            unsigned long __res6:24;
+        };
     } mm32;
 
     struct {
diff --git a/xen/arch/arm/include/asm/processor.h b/xen/arch/arm/include/asm/processor.h
index a3753c317f..dbf9471a7a 100644
--- a/xen/arch/arm/include/asm/processor.h
+++ b/xen/arch/arm/include/asm/processor.h
@@ -484,9 +484,16 @@
 /* FSR long format */
 #define FSRL_STATUS_DEBUG       (_AC(0x22,UL)<<0)
 
+#ifdef CONFIG_ARM_32
+#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
+#define MM64_NTLBPA_SUPPORT_NI      0x0
+#define MM64_NTLBPA_SUPPORT_IMP     0x1
 #endif
 
 #define MM64_MSA_PMSA_SUPPORT       0xf
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN][RESEND][PATCH 3/3] xen/arm32: use IPA-based TLBI when supported with FEAT_nTLBPA
  2026-07-21  7:54 ` [XEN][RESEND][PATCH 1/3] xen/arm/p2m: perform IPA-based TLBI when IPA is known Haseeb Ashraf
  2026-07-21  7:54   ` [XEN][RESEND][PATCH 2/3] xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA Haseeb Ashraf
@ 2026-07-21  7:54   ` Haseeb Ashraf
  1 sibling, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-07-21  7:54 UTC (permalink / raw)
  To: xen-devel
  Cc: haseeb.ashraf, Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Michal Orzel, Volodymyr Babchuk

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Arm32 provides TLBI-by-IPA instructions on CPUs that implement the
relevant stage-2 invalidation support. Detect this as a CPU capability
and use it to perform IPA-based invalidation in the p2m range flush
helper.

On Arm32, however, IPA-based stage-2 invalidation is only useful when
FEAT_nTLBPA is also present. Without FEAT_nTLBPA, a stage-2 invalidation
may still require discarding associated stage-1 side effects, and Arm32
does not provide a separate stage-1 guest TLBI that Xen can use here.
In that case, falling back to the existing full guest flush remains the
correct approach.

Therefore, enable Arm32 IPA-based invalidation only when both:
  - TLBI-by-IPA is supported, and
  - FEAT_nTLBPA is present.

As on Arm64, the helper bounds the per-page invalidation loop with a
256MB cutoff and falls back to a full guest flush for larger ranges.
For Arm32 this threshold is used as a conservative practical limit,
reusing the Arm64 cutoff, rather than as an Arm32-specific tuned value.

Suggested-by: Julien Grall <julien@xen.org>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Changes in v4:
- Clarified that Arm32 IPA-based TLBI is only enabled when both
  TLBI-by-IPA support and FEAT_nTLBPA are present.
- Clarified that the 256MB threshold is reused as a conservative
  practical cutoff and is not based on Arm32-specific measurements.
- Refined comments and commit message wording.

Changes in v3:
- There are no functional changes in this version. There are minor
  code updates and comment updates as per the feedback on v2.
- The cpregs are defined in order as per Coprocessor-> CRn-> Opcode 1
  -> CRm-> Opcode 2.
- Added comment to explain why IPA-based TLBI is added only in
  presence of FEAT_nTLBPA.
- Replaced `goto default_tlbi` with if...else.
- Removed extra definitions of MM32_UNITLB_* macros which were not
  being used.

Changes in v2:
- This commit is implemented in v2 as per the feedback to implement
  IPA-based TLBI for Arm32 in addition to Arm64.
---
 xen/arch/arm/cpufeature.c                 | 12 ++++++
 xen/arch/arm/include/asm/arm32/flushtlb.h | 47 ++++++++++++++++++++---
 xen/arch/arm/include/asm/cpregs.h         |  1 +
 xen/arch/arm/include/asm/cpufeature.h     | 15 +++++---
 xen/arch/arm/include/asm/processor.h      |  3 ++
 5 files changed, 67 insertions(+), 11 deletions(-)

diff --git a/xen/arch/arm/cpufeature.c b/xen/arch/arm/cpufeature.c
index 86720c10a3..9ca449a551 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;
 
 #ifdef CONFIG_ARM_32
+static bool has_tlb_ipa_instruction(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm32.unitlb == MM32_UNITLB_BY_IPA;
+}
+
 static bool has_ntlbpa(const struct arm_cpu_capabilities *entry)
 {
     return system_cpuinfo.mm32.ntlbpa == MM32_NTLBPA_SUPPORT_IMP;
@@ -37,6 +42,13 @@ static bool has_sb_instruction(const struct arm_cpu_capabilities *entry)
 #endif
 
 static const struct arm_cpu_capabilities arm_features[] = {
+#ifdef CONFIG_ARM_32
+    {
+        .desc = "IPA-based TLB Invalidation",
+        .capability = ARM32_HAS_TLB_IPA,
+        .matches = has_tlb_ipa_instruction,
+    },
+#endif
 #if defined(CONFIG_ARM_32) || defined(CONFIG_ARM_64)
     {
         .desc = "Intermediate caching of translation table walks (nTLBPA)",
diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/include/asm/arm32/flushtlb.h
index 30df161323..1db6b8410e 100644
--- a/xen/arch/arm/include/asm/arm32/flushtlb.h
+++ b/xen/arch/arm/include/asm/arm32/flushtlb.h
@@ -1,6 +1,8 @@
 #ifndef __ASM_ARM_ARM32_FLUSHTLB_H__
 #define __ASM_ARM_ARM32_FLUSHTLB_H__
 
+#include <xen/sizes.h> /* For SZ_* macros. */
+
 /*
  * Every invalidation operation use the following patterns:
  *
@@ -107,12 +109,47 @@ 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 upon
-     * the execution mode.
-     *
-     * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+     * 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.
      */
-    flush_guest_tlb();
+    if ( cpus_have_const_cap(ARM_HAS_NTLBPA) &&
+         cpus_have_const_cap(ARM32_HAS_TLB_IPA) )
+    {
+        /*
+         * If the IPA range is too large, fall back to a full guest flush
+         * rather than issuing a long per-page TLBI sequence. Reuse the same
+         * practical cutoff as Arm64.
+         */
+        if ( size > SZ_256M )
+            flush_guest_tlb();
+        else
+        {
+            paddr_t end = ipa + size;
+
+            dsb(ishst); /* Ensure prior page-tables updates have completed */
+            while ( ipa < end )
+            {
+                /* Flush stage-2 TLBs for this IPA. */
+                asm volatile ( STORE_CP32(0, TLBIIPAS2IS)
+                               : : "r" (ipa >> PAGE_SHIFT) : "memory" );
+                ipa += PAGE_SIZE;
+            }
+            dsb(ish);
+            isb();
+        }
+    }
+    else
+    {
+        /*
+         * Following can invalidate both stage-1 and stage-2 TLBs depending
+         * upon the execution mode.
+         *
+         * See ARMv8 (DDI 0487L.b): G5-11698 Table G5-23.
+         */
+        flush_guest_tlb();
+    }
 }
 
 #endif /* __ASM_ARM_ARM32_FLUSHTLB_H__ */
diff --git a/xen/arch/arm/include/asm/cpregs.h b/xen/arch/arm/include/asm/cpregs.h
index a7503a190f..26b5c08d0e 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 match */
 #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 for 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 Non-Hyp. Unified TLB inner shareable */
diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/asm/cpufeature.h
index 3e56dd27ef..09a516fe49 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
 
-#define ARM_NCAPS           19
+#define ARM_NCAPS           20
 
 #ifndef __ASSEMBLER__
 
@@ -447,15 +448,17 @@ struct cpuinfo_arm {
             /* MMFR1 */
             unsigned long __res1:32;
             /* MMFR2 */
-            unsigned long __res2:32;
+            unsigned long __res2:16;
+            unsigned long unitlb:4;
+            unsigned long __res3:12;
             /* MMFR3 */
-            unsigned long __res3:32;
-            /* MMFR4 */
             unsigned long __res4:32;
+            /* MMFR4 */
+            unsigned long __res5:32;
             /* MMFR5 */
-            unsigned long __res5:4;
+            unsigned long __res6:4;
             unsigned long ntlbpa:4;
-            unsigned long __res6:24;
+            unsigned long __res7:24;
         };
     } mm32;
 
diff --git a/xen/arch/arm/include/asm/processor.h b/xen/arch/arm/include/asm/processor.h
index dbf9471a7a..0c47225de0 100644
--- a/xen/arch/arm/include/asm/processor.h
+++ b/xen/arch/arm/include/asm/processor.h
@@ -485,6 +485,9 @@
 #define FSRL_STATUS_DEBUG       (_AC(0x22,UL)<<0)
 
 #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
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN][PATCH v5 0/8] xen/arm: narrow stage-2 TLB invalidation and batch physmap updates
  2026-01-18 13:33 ` [XEN PATCH v3 " Haseeb Ashraf
                   ` (6 preceding siblings ...)
  (?)
@ 2026-11-22  9:54 ` Haseeb Ashraf
  2026-11-22  9:54   ` [XEN][PATCH v5 1/8] xen/arm/p2m: perform IPA-based TLBI when IPA is known Haseeb Ashraf
                     ` (7 more replies)
  -1 siblings, 8 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-11-22  9:54 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, sstabellini, julien, bertrand.marquis,
	michal.orzel, Volodymyr_Babchuk, andrew.cooper3, anthony.perard,
	jbeulich, roger, oleksii.kurochko, community.manager, dpsmith

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

Base commit: ada7fa3491668bbcf1afe525057c470903625d26

Previous version:
https://patchew.org/Xen/cover.1765197209.git.haseeb.ashraf@siemens.com/

This series depends on a complementary Linux KVM change [1][2], "KVM:
arm64: Resume stage-2 range walks where they stopped". Without it, a DomU
cannot be run in practice on Xen as a guest (L1) of KVM. The cost of
emulating TLBI VMALLS12E1IS and ALLE1IS is the reason: each trap walks
the whole IPA space of the shadow stage-2 in 2^18 separate walks, and on
the AmpereOne host used below it took 905.8 ms on average. One create and
destroy cycle of an 8GB guest caused 226 of them, about 205 s of
emulation (measurements in [1]). With the Linux change a trap takes a
few milliseconds (4.06 ms on average in [1], 1.8 ms in Table 2 below).
All the results below were taken with it applied on L0, so it is the
baseline. This Xen series does not depend on it to be correct. It
addresses a separate cost that remains: a trap still takes milliseconds,
and many full invalidations are not optimal when a narrower one is
enough.

[1] https://lore.kernel.org/all/20261118051648.74580-1-haseebashraf091@gmail.com/  (cover letter)
[2] https://lore.kernel.org/all/20261118051648.74580-2-haseebashraf091@gmail.com/  (patch)

Updates of the Arm stage-2 page tables are followed by a TLB
invalidation that is much broader than the update. Most paths issue
VMALLS12E1IS, which discards all the stage-1 and stage-2 translations
of the VMID on every CPU, whatever was changed. Removing guest pages one
at a time also costs a hypercall, a p2m lock round trip and an
invalidation per page.

This series narrows the invalidation to what changed where Xen knows it,
drops an invalidation that has no work to do, uses FEAT_TLBIRANGE and
FEAT_nTLBPA when the CPU has them, and adds batched interfaces that
spread the per-page cost of hypercalls, locking and invalidation. These
are general improvements of the Arm p2m code and do not depend on how
Xen is run. The effect is largest where TLB maintenance traps, for
example when Xen itself runs as a guest (L1) of another hypervisor: the
results below show it on the creation and the destruction of an L2
guest.

Three common cases are affected:

  (a) vCPU migration on a pCPU, where Xen flushes the combined stage-1
      and stage-2 translations of the guest, although the stage-2
      regime is shared by all the vCPUs of the domain.

  (b) teardown of DomU mappings exported into Dom0, where
      XENMEM_remove_from_physmap flushes all the TLBs of the guest,
      although the affected IPA range is known.

  (c) device models that map and unmap many guest pages: removing pages
      individually takes a hypercall, a p2m lock round trip and a TLB
      invalidation per page, and XENMEM_add_to_physmap_batch copies
      three guest values per entry.

Patches:

  #  Change                            Condition and bound
  -- --------------------------------- --------------------------------
  1  Invalidate by IPA when the range  Fewer than 512 pages (4KB
     is known; local flush on a vCPU   granule), else the whole VMID
     switch on the same pCPU (stage-1
     only on Arm64)
  2  Skip the stage-1 invalidation     FEAT_nTLBPA only
     that follows an IPA invalidation
  3  IPA-based invalidation on Arm32   FEAT_nTLBPA and TLBI by IPA,
                                       same 512 page bound
  4  Range invalidation (RIPAS2E1IS)   FEAT_TLBIRANGE, at most four
                                       range operations
  5  Drop the flush when resolving a   None
     translation fault
  6  Arm helper: remove a list of      Arm with an MMU
     pages under one p2m lock, one     (HAS_PHYSMAP_REMOVE_BATCH)
     range flush per run of GFNs
  7  XENMEM_remove_from_physmap_batch  Opt-in for callers, Arm with an
     hypercall using patch 6           MMU, -ENOSYS elsewhere
  8  Copy add_to_physmap_batch         Common code, 128 entries per
     entries in chunks                 chunk

Results: Xen as L1, creation and destruction of an L2 guest

Setup: an AmpereOne host runs Linux (L0) with KVM nested virtualization
and the Linux change above, in the baseline and in v5. Xen 4.23-dev and
its Dom0 run as an L1 guest, and a DomU is the L2 guest (8GiB of RAM, 4
vCPUs). Five back-to-back cycles per run: create the DomU, boot it to a
login prompt, then "xl destroy". "Baseline" is Xen master and "v5" is
master with this series. A bpftrace script on L0 times the KVM handlers
(handle_vmalls12e1is, handle_ripas2e1is and handle_tlbi_el1) of the TLBI
instructions that L1 traps on, over all five cycles.

Table 1: L2 guest lifecycle, wall clock and system time

  Cycle   VM creation        xl destroy (wall)       xl destroy (sys)
          base      v5       base        v5          base       v5
  ------- --------- -------- ----------- ----------- ---------- --------
  1       27 s      9 s      106.411 s   2.693 s     87.877 s   0.627 s
  2       20 s      3 s      107.549 s   2.690 s     88.966 s   0.635 s
  3       23 s      2 s      114.707 s   2.703 s     95.182 s   0.654 s
  4       27 s      2 s      107.946 s   2.702 s     89.189 s   0.651 s
  5       20 s      3 s       92.778 s   2.697 s     76.813 s   0.645 s
  ------- --------- -------- ----------- ----------- ---------- --------
  mean    23.4 s    3.8 s    105.878 s   2.697 s     87.605 s   0.642 s
  median  23 s      3 s      107.549 s   2.697 s     88.966 s   0.645 s
  change  6.2x faster        39x faster              136x less

Table 2: TLBI instructions emulated by L0 KVM (five cycles)

  Run       Instruction   Entries   Cumulative   Mean       Max
  --------- ------------- --------- ------------ ---------- ----------
  Baseline  VMALLS12E1IS  448,959 * 824.771 s    1.843 ms   39.429 ms
  v5        VMALLS12E1IS         15  26.863 ms   1.791 ms    3.787 ms
  v5        RIPAS2E1IS       24,854 525.967 ms   21.16 us   30.475 ms
  v5        VMALLE1IS        24,812  66.215 ms    2.67 us    0.019 ms
  v5        all              49,681 619.045 ms

  * 447,539 of these calls returned in the trace. The cumulative time
    and the mean are over the returned calls.

  Traps: 9.0x fewer (448,959 against 49,681). Cumulative handler time:
  about 1,330x less (824.8 s against 0.619 s).

Table 3: number of handler calls by duration (five cycles)

  Duration          Baseline      v5            v5          v5
                    VMALLS12E1IS  RIPAS2E1IS    VMALLE1IS   VMALLS12E1IS
  ----------------- ------------- ------------- ----------- ------------
  below 4 us                    0           871      22,831            0
  4 us to 16 us                 3        18,982       1,967            1
  16 us to 64 us                1         4,981          14            1
  64 us to 1 ms               149             0           0            3
  1 ms to 2 ms            247,974             0           0            6
  2 ms to 4 ms            197,756             9           0            4
  4 ms and more             1,656            11           0            0

How to read it: the baseline needs about 90,000 emulated VMALLS12E1IS
per cycle, each about 1.8 ms, and this is what the 106 s of "xl destroy"
and most of the creation time are spent on. With the series the same
work needs about 5,000 RIPAS2E1IS and 5,000 VMALLE1IS per cycle. A
RIPAS2E1IS takes about 21 us and is followed by a VMALLE1IS of about
2.7 us, because the CPU has no FEAT_nTLBPA: the pair costs about 24 us,
against 1.8 ms for the full flush. "xl destroy" takes the same 2.7 s in
every cycle of the v5 run, while the baseline takes between 93 s and
115 s. The 15 remaining VMALLS12E1IS (three per cycle) are for ranges
that are too large for an IPA invalidation. They still take about 1.8 ms
each, and are 4% of the cumulative handler time of v5.

The lifecycle runs do not exercise the batch interfaces in patches 6 to
8. The tables therefore provide no performance result for those
patches; dedicated device-model workloads using the batch interfaces
are needed to quantify their benefit.

Impact when Xen is not nested:

 - The series changes common Arm p2m invalidations. No bare metal
   performance measurement is available yet.
 - Without FEAT_TLBIRANGE, Arm64 ranges under 512 pages can issue up to
   511 broadcast IPA TLBIs, and ranges of 512 pages or more use the
   whole VMID. Arm32 uses the same bound only with FEAT_nTLBPA and TLBI
   by IPA, and the whole VMID otherwise. With FEAT_TLBIRANGE, ranges
   that can be encoded use at most four range operations, and larger
   ranges use the whole VMID. This bounds the number of instructions,
   not the latency.
 - The flushes that are deferred to p2m_write_unlock() are unchanged.
   The FEAT_nTLBPA and FEAT_TLBIRANGE paths depend on the capability.
 - The batched removal hypercall is opt-in for callers and exists for
   Arm with an MMU. Patch 8 changes common code, so its effect on bare
   metal also needs a measurement.

Testing: the runs above were done on Arm64 (AmpereOne) with this series.
An x86_64 build completed with CONFIG_COMPAT=y. The Arm64 Cppcheck MISRA
add-on reported no diagnostics on changed lines.
Not yet validated: Xen on bare metal, the FEAT_nTLBPA path, Arm32 at run
time.

Notes:
 - Like XENMEM_add_to_physmap_batch, the batched removal reports per-entry
   errors in errs[] and is not transactional: -EFAULT can follow removals
   that completed.
 - Patch 8 keeps that semantics, but reads and writes the entries in
   chunks of 128, so more entries may be processed before an -EFAULT.

Changes in v5:
- Replace the 256MB limit, measured on one platform, with
  TLBI_MAX_PAGE_OPS, the limit of Linux, and use the Linux limit for
  range operations (patches 1 and 4).
- Keep the deferred flush of removals: p2m_remove_mapping() flushes
  its range once, other removals are flushed by p2m_write_unlock() as
  before. Keep the deferred flush of permission-only changes with
  memaccess (patch 1).
- Keep patch 1 independently usable with the MPU stub for
  p2m_force_tlb_flush_range_sync(); defer the locked-helper refactor
  until patch 6, its first user.
- Move Arm32 nTLBPA and UniTLB capability detection from patch 2 to
  patch 3, where the Arm32 IPA-based invalidation first uses it.
- Invalidate the whole old block when a superpage is split (patch 1).
- Do not skip the stage-1 invalidation of the vCPU switch with
  FEAT_nTLBPA: it separates the vCPUs, it is not about walk caches
  (patch 2).
- Complete the stage-2 invalidation before the stage-1 one, as the Arm
  ARM requires, and complete the broadcast sequence before the
  repeat-TLBI workaround (patch 1).
- Fix the Arm64 helper macros of patch 1 so that it builds on its own.
- Put the TLBI instructions in one-instruction helpers (MISRA C
  Directive 4.3) and drop the unused inner-shareable stage-1 helper.
- Add patches 4 to 8. Cite DDI 0487 M.d.

Changes in v4:
- Rebased on master branch
- Reworked the Arm64 IPA-based TLBI sequence to make completion of the
  broadcast TLBI explicit instead of relying on a stage-1 helper for the
  final barriers/workaround.
- Reused __tlb_repeat_sync() for repeat-TLBI workaround handling in the
  Arm64 completion path.
- Fixed the Arm32 IPA-based TLBI helper to fall back to a full guest TLB
  flush when the required capability combination is unavailable, instead
  of becoming a no-op.
- Added a small robustness check in p2m_force_tlb_flush_range_sync().
- Refined comments and commit messages to make the architectural intent
  clearer.

Changes in v3:
- Mainly reworked handling of the repeat-TLBI workaround with IPA-based
  TLBI so that the extra TLBI and DSB are applied only at completion of
  the overall sequence.
- Updated code comments as per feedback. Further details are available in
  each commit's changelog.
- Minor code updates as per feedback. Further details are available in
  each commit's changelog.

Changes in v2:
- Split the original change into three commits. The first commit
  implements the baseline implementation without adding new CPU
  capabilities. CPU capability handling is introduced in follow-up
  commits to make each optimization clearer.
- Moved Arm32 and Arm64 specific TLBI implementations to the respective
  architecture-specific flushtlb.h.
- Added ARM ARM references in code comments.
- Added a threshold to select between IPA-based invalidation and
  fallback to full guest invalidation above that threshold.
- Introduced ARM_HAS_NTLBPA CPU capability for both Arm32 and Arm64.
- Introduced ARM32_HAS_TLB_IPA CPU capability for Arm32 IPA-based TLBI.

Haseeb Ashraf (8):
  xen/arm/p2m: perform IPA-based TLBI when IPA is known
  xen/arm: skip the stage-1 TLBI after IPA flush with FEAT_nTLBPA
  xen/arm32: use IPA-based TLBI when supported with FEAT_nTLBPA
  xen/arm64: use range TLBI for IPA flush with FEAT_TLBIRANGE
  xen/arm: do not flush the TLBs when resolving a translation fault
  xen/arm: batch stage-2 removal with range flushes
  xen/memory: add XENMEM_remove_from_physmap_batch
  xen/memory: copy add_to_physmap batch entries in bulk

 CHANGELOG.md                              |   5 +
 xen/arch/arm/Kconfig                      |   1 +
 xen/arch/arm/cpufeature.c                 |  45 +++++
 xen/arch/arm/include/asm/arm32/flushtlb.h |  67 +++++++
 xen/arch/arm/include/asm/arm64/flushtlb.h | 178 ++++++++++++++++++
 xen/arch/arm/include/asm/cpregs.h         |   1 +
 xen/arch/arm/include/asm/cpufeature.h     |  26 ++-
 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/include/asm/processor.h      |   9 +
 xen/arch/arm/mmu/p2m.c                    | 114 ++++++++++--
 xen/arch/arm/p2m.c                        | 111 +++++++++--
 xen/common/Kconfig                        |   3 +
 xen/common/memory.c                       | 215 ++++++++++++++++++++--
 xen/include/public/memory.h               |  35 ++++
 xen/include/xen/p2m-common.h              |  16 ++
 17 files changed, 799 insertions(+), 45 deletions(-)



^ permalink raw reply	[flat|nested] 25+ messages in thread

* [XEN][PATCH v5 1/8] xen/arm/p2m: perform IPA-based TLBI when IPA is known
  2026-11-22  9:54 ` [XEN][PATCH v5 0/8] xen/arm: narrow stage-2 TLB invalidation and batch physmap updates Haseeb Ashraf
@ 2026-11-22  9:54   ` Haseeb Ashraf
  2026-11-22  9:54   ` [XEN][PATCH v5 2/8] xen/arm: skip the stage-1 TLBI after IPA flush with FEAT_nTLBPA Haseeb Ashraf
                     ` (6 subsequent siblings)
  7 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-11-22  9:54 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, sstabellini, julien, bertrand.marquis,
	michal.orzel, Volodymyr_Babchuk

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

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=5eec0a91e32a2862e86265532ae773820e0afd77
- 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=3d65b6bbc01ecece8142e62a8a5f1d48ba41a240
- The later rename of that limit to MAX_DVM_OPS:
  https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=ec1c3b9ff16082f880b304be40992568f4eee6a7

Suggested-by: Julien Grall <julien@xen.org>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>
---
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/include/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)
 
 #undef TLB_HELPER
 
+/*
+ * Flush TLB of local processor. Use when flush for only stage-1 is intended.
+ *
+ * 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 depending
+     * 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) {}
 
+/*
+ * 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 upon
+     * 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/include/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)
 
+/* 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)
 
@@ -116,6 +119,81 @@ static inline void __tlb_repeat_sync(void)
         : : : "memory");
 }
 
+/*
+ * 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 sequence,
+ * 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 bounds
+ * 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 size)
+{
+    unsigned long pages = size >> PAGE_SHIFT;
+    paddr_t end = ipa + size;
+
+    if ( pages >= 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 += 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 page_info *page)
     page->tlbflush_timestamp = tlbflush_current_time();
 }
 
+/*
+ * 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 <asm/arm32/flushtlb.h>
 #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;
 
 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);
 
 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_domain *p2m) {}
 
 static inline void p2m_tlb_flush_sync(struct p2m_domain *p2m) {}
 
+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__ */
 
 /*
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();
 
     /*
-     * 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 that
+     * ran last on this pCPU must not be used by this one.
      */
     if ( *last_vcpu_ran != INVALID_VCPU_ID && *last_vcpu_ran != n->vcpu_id )
-        flush_guest_tlb_local();
+        flush_guest_tlb_s1_local();
 
     *last_vcpu_ran = n->vcpu_id;
 }
 
 /*
- * 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 *flags)
 {
-    unsigned long flags = 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 = READ_SYSREG64(VTTBR_EL2);
     if ( ovttbr != p2m->vttbr )
     {
         uint64_t vttbr;
 
-        local_irq_save(flags);
+        local_irq_save(*flags);
 
         /*
          * 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();
     }
 
-    flush_guest_tlb();
+    return ovttbr;
+}
 
+/*
+ * 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 != 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 = 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 = p2m_load_vttbr(p2m, &flags);
+
+    flush_guest_tlb();
+
+    p2m_restore_vttbr(ovttbr, flags);
 
     p2m->need_flush = false;
 }
 
+/*
+ * 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 = 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 = 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 = *entry;
+        unsigned long nr = 1UL << XEN_PT_LEVEL_ORDER(level);
 
         ASSERT(p2m_is_superpage(*entry, level));
 
@@ -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);
 
         p2m_write_pte(entry, split_pte, p2m->clean_pte);
 
@@ -1092,7 +1151,12 @@ static int __p2m_set_entry(struct p2m_domain *p2m,
         p2m_remove_pte(entry, p2m->clean_pte);
 
     if ( removing_mapping )
-        /* Flush can be deferred if the entry is removed */
+        /*
+         * Flush can be deferred if the entry is removed. A caller that knows
+         * 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 |= !!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) != 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 = 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;
 
     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 = p2m_set_entry(p2m, start_gfn, nr, INVALID_MFN,
                        p2m_invalid, p2m_access_rwx);
 
-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 = false;
+    }
+
+ out:
     p2m_write_unlock(p2m);
 
     return rc;


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN][PATCH v5 2/8] xen/arm: skip the stage-1 TLBI after IPA flush with FEAT_nTLBPA
  2026-11-22  9:54 ` [XEN][PATCH v5 0/8] xen/arm: narrow stage-2 TLB invalidation and batch physmap updates Haseeb Ashraf
  2026-11-22  9:54   ` [XEN][PATCH v5 1/8] xen/arm/p2m: perform IPA-based TLBI when IPA is known Haseeb Ashraf
@ 2026-11-22  9:54   ` Haseeb Ashraf
  2026-11-22  9:54   ` [XEN][PATCH v5 3/8] xen/arm32: use IPA-based TLBI when supported " Haseeb Ashraf
                     ` (5 subsequent siblings)
  7 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-11-22  9:54 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, sstabellini, julien, bertrand.marquis,
	michal.orzel, Volodymyr_Babchuk, Mohamed Mediouni

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

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 <mohamed@unpredictable.fr>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>
---
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;
 
 #ifdef CONFIG_ARM_64
+static bool has_ntlbpa(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm64.ntlbpa == 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_capabilities *entry)
 #endif
 
 static const struct arm_cpu_capabilities arm_features[] = {
+#ifdef CONFIG_ARM_64
+    {
+        .desc = "Intermediate caching of translation table walks (nTLBPA)",
+        .capability = ARM_HAS_NTLBPA,
+        .matches = has_ntlbpa,
+    },
+#endif
 #ifdef CONFIG_ARM_64
     {
         .desc = "Speculation barrier instruction (SB)",
diff --git a/xen/arch/arm/include/asm/arm64/flushtlb.h b/xen/arch/arm/include/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 ipa, 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();
+    }
 
     tlbi_complete_broadcast();
 }
diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/asm/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
 
-#define ARM_NCAPS           18
+#define ARM_NCAPS           19
 
 #ifndef __ASSEMBLER__
 
@@ -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;
 
             /* MMFR2 */
diff --git a/xen/arch/arm/include/asm/processor.h b/xen/arch/arm/include/asm/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
 
 #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 that
      * 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 != INVALID_VCPU_ID && *last_vcpu_ran != n->vcpu_id )
         flush_guest_tlb_s1_local();


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN][PATCH v5 3/8] xen/arm32: use IPA-based TLBI when supported with FEAT_nTLBPA
  2026-11-22  9:54 ` [XEN][PATCH v5 0/8] xen/arm: narrow stage-2 TLB invalidation and batch physmap updates Haseeb Ashraf
  2026-11-22  9:54   ` [XEN][PATCH v5 1/8] xen/arm/p2m: perform IPA-based TLBI when IPA is known Haseeb Ashraf
  2026-11-22  9:54   ` [XEN][PATCH v5 2/8] xen/arm: skip the stage-1 TLBI after IPA flush with FEAT_nTLBPA Haseeb Ashraf
@ 2026-11-22  9:54   ` Haseeb Ashraf
  2026-11-22  9:54   ` [XEN][PATCH v5 4/8] xen/arm64: use range TLBI for IPA flush with FEAT_TLBIRANGE Haseeb Ashraf
                     ` (4 subsequent siblings)
  7 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-11-22  9:54 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, sstabellini, julien, bertrand.marquis,
	michal.orzel, Volodymyr_Babchuk

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

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 <julien@xen.org>
Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>
---
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);
 
 struct cpuinfo_arm __read_mostly domain_cpuinfo;
 
+#ifdef CONFIG_ARM_32
+static bool has_tlb_ipa_instruction(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm32.unitlb == MM32_UNITLB_BY_IPA;
+}
+
+static bool has_ntlbpa(const struct arm_cpu_capabilities *entry)
+{
+    return system_cpuinfo.mm32.ntlbpa == 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_capabilities *entry)
 #endif
 
 static const struct arm_cpu_capabilities arm_features[] = {
-#ifdef CONFIG_ARM_64
+#ifdef CONFIG_ARM_32
+    {
+        .desc = "IPA-based TLB Invalidation",
+        .capability = ARM32_HAS_TLB_IPA,
+        .matches = has_tlb_ipa_instruction,
+    },
+#endif
+#if defined(CONFIG_ARM_32) || defined(CONFIG_ARM_64)
     {
         .desc = "Intermediate caching of translation table walks (nTLBPA)",
         .capability = ARM_HAS_NTLBPA,
diff --git a/xen/arch/arm/include/asm/arm32/flushtlb.h b/xen/arch/arm/include/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) {}
 
+/* 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 ipa,
                                              unsigned long size)
 {
     /*
-     * Following can invalidate both stage-1 and stage-2 TLBs depending upon
-     * 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 = ipa + size;
+
+        dsb(ishst); /* Ensure prior page-tables updates have completed */
+        for ( ; ipa < end; ipa += 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();
+    }
 }
 
 #endif /* __ASM_ARM_ARM32_FLUSHTLB_H__ */
diff --git a/xen/arch/arm/include/asm/cpregs.h b/xen/arch/arm/include/asm/cpregs.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 match */
 #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 for 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 Non-Hyp. Unified TLB inner shareable */
diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/asm/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
 
-#define ARM_NCAPS           19
+#define ARM_NCAPS           20
 
 #ifndef __ASSEMBLER__
 
@@ -454,6 +455,24 @@ struct cpuinfo_arm {
 
     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;
 
     struct {
diff --git a/xen/arch/arm/include/asm/processor.h b/xen/arch/arm/include/asm/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)
 
+#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


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN][PATCH v5 4/8] xen/arm64: use range TLBI for IPA flush with FEAT_TLBIRANGE
  2026-11-22  9:54 ` [XEN][PATCH v5 0/8] xen/arm: narrow stage-2 TLB invalidation and batch physmap updates Haseeb Ashraf
                     ` (2 preceding siblings ...)
  2026-11-22  9:54   ` [XEN][PATCH v5 3/8] xen/arm32: use IPA-based TLBI when supported " Haseeb Ashraf
@ 2026-11-22  9:54   ` Haseeb Ashraf
  2026-11-22  9:54   ` [XEN][PATCH v5 5/8] xen/arm: do not flush the TLBs when resolving a translation fault Haseeb Ashraf
                     ` (3 subsequent siblings)
  7 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-11-22  9:54 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, sstabellini, julien, bertrand.marquis,
	michal.orzel, Volodymyr_Babchuk

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

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=d1d3aa98b1d4826a19adfefb69b96142a0cac633
- 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=6354d15052ec88273c24beae4c99e31c3d3889b6

Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>
---
 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_capabilities *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 >= 2;
+}
 #endif
 
 static const struct arm_cpu_capabilities arm_features[] = {
@@ -62,6 +71,11 @@ static const struct arm_cpu_capabilities arm_features[] = {
         .capability = ARM_HAS_SB,
         .matches = has_sb_instruction,
     },
+    {
+        .desc = "Range-based TLB invalidation (FEAT_TLBIRANGE)",
+        .capability = ARM64_HAS_TLBI_RANGE,
+        .matches = has_tlbi_range,
+    },
 #endif
     {},
 };
diff --git a/xen/arch/arm/include/asm/arm64/flushtlb.h b/xen/arch/arm/include/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();
 }
 
+/*
+ * 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 = TLBI_RANGE_MAX_SCALE;
+
+    while ( pages > 1 && scale >= 0 )
+    {
+        long num = TLBI_RANGE_NUM(pages, scale);
+
+        if ( num >= 0 )
+        {
+            uint64_t op = ((ipa >> PAGE_SHIFT) & 0x1fffffffffULL) |
+                          ((uint64_t)num << 39) |
+                          ((uint64_t)scale << 44) |
+                          (1ULL << 46);
+
+            tlbi_ripas2e1is(op);
+
+            ipa += (paddr_t)TLBI_RANGE_PAGES(num, scale) << PAGE_SHIFT;
+            pages -= 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 bounds
- * 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 per
+ * page. These limits bound instruction count; neither is a tuned crossover.
  */
 static inline void flush_guest_tlb_range_ipa(paddr_t ipa, unsigned long size)
 {
     unsigned long pages = size >> PAGE_SHIFT;
-    paddr_t end = ipa + size;
+    bool range = cpus_have_const_cap(ARM64_HAS_TLBI_RANGE);
+
+    if ( !pages )
+        return;
 
-    if ( pages >= 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: the
+         * neighbor is always inside the IPA space, and a page more is safe
+         * to invalidate.
+         */
+        if ( (pages & 1UL) != 0UL )
+        {
+            if ( (ipa & PAGE_SIZE) != 0UL )
+                ipa -= 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 >= 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 += PAGE_SIZE )
+    if ( range )
+        tlbi_range_ipas2e1is(ipa, pages);
+    else
     {
-        /* Flush stage-2 TLBs for IPA address */
-        tlbi_ipas2e1is(ipa);
+        paddr_t end = ipa + size;
+
+        for ( ; ipa < end; ipa += PAGE_SIZE )
+        {
+            /* Flush stage-2 TLBs for IPA address */
+            tlbi_ipas2e1is(ipa);
+        }
     }
 
     /*
diff --git a/xen/arch/arm/include/asm/cpufeature.h b/xen/arch/arm/include/asm/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
 
-#define ARM_NCAPS           20
+#define ARM_NCAPS           21
 
 #ifndef __ASSEMBLER__
 


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN][PATCH v5 5/8] xen/arm: do not flush the TLBs when resolving a translation fault
  2026-11-22  9:54 ` [XEN][PATCH v5 0/8] xen/arm: narrow stage-2 TLB invalidation and batch physmap updates Haseeb Ashraf
                     ` (3 preceding siblings ...)
  2026-11-22  9:54   ` [XEN][PATCH v5 4/8] xen/arm64: use range TLBI for IPA flush with FEAT_TLBIRANGE Haseeb Ashraf
@ 2026-11-22  9:54   ` Haseeb Ashraf
  2026-11-22  9:54   ` [XEN][PATCH v5 6/8] xen/arm: batch stage-2 removal with range flushes Haseeb Ashraf
                     ` (2 subsequent siblings)
  7 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-11-22  9:54 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, sstabellini, julien, bertrand.marquis,
	michal.orzel, Volodymyr_Babchuk

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

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 <haseeb.ashraf@siemens.com>
---
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 flushed
+         * 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 = p2m->need_flush;
+
         p2m_invalidate_table(p2m, lpae_get_mfn(entry));
+        p2m->need_flush = need_flush;
+    }
 
     /*
      * Now that the work on the entry is done, set the valid bit to prevent


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN][PATCH v5 6/8] xen/arm: batch stage-2 removal with range flushes
  2026-11-22  9:54 ` [XEN][PATCH v5 0/8] xen/arm: narrow stage-2 TLB invalidation and batch physmap updates Haseeb Ashraf
                     ` (4 preceding siblings ...)
  2026-11-22  9:54   ` [XEN][PATCH v5 5/8] xen/arm: do not flush the TLBs when resolving a translation fault Haseeb Ashraf
@ 2026-11-22  9:54   ` Haseeb Ashraf
  2026-11-22  9:54   ` [XEN][PATCH v5 7/8] xen/memory: add XENMEM_remove_from_physmap_batch Haseeb Ashraf
  2026-11-22  9:54   ` [XEN][PATCH v5 8/8] xen/memory: copy add_to_physmap batch entries in bulk Haseeb Ashraf
  7 siblings, 0 replies; 25+ messages in thread
From: Haseeb Ashraf @ 2026-11-22  9:54 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, sstabellini, julien, bertrand.marquis,
	michal.orzel, Volodymyr_Babchuk, andrew.cooper3, anthony.perard,
	jbeulich, roger

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

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=7657ea920c54218f123ddc1b572821695b669c13

Signed-off-by: Haseeb Ashraf <haseeb.ashraf@siemens.com>
---
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;
 }
 
-static inline int p2m_remove_mapping(struct domain *d,
+/*
+ * Remove [start_gfn, start_gfn + nr) from the p2m. The caller must hold the
+ * 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 = p2m_get_hostp2m(d);
     unsigned long i;
-    int rc;
 
-    p2m_write_lock(p2m);
+    ASSERT(p2m_is_write_locked(p2m));
 
     /*
      * 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,
 
         if ( p2m_is_any_ram(t) &&
              (!mfn_valid(mfn) || !mfn_eq(mfn_add(mfn, i), mfn_return)) )
-        {
-            rc = -EILSEQ;
-            goto out;
-        }
+            return -EILSEQ;
 
         i += (1UL << cur_order) -
              ((gfn_x(start_gfn) + i) & ((1UL << cur_order) - 1));
     }
 
-    rc = 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 = p2m_get_hostp2m(d);
+    int rc;
+
+    p2m_write_lock(p2m);
+
+    rc = p2m_remove_mapping_locked(p2m, start_gfn, nr, mfn);
 
     /*
      * 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 = false;
     }
 
- out:
     p2m_write_unlock(p2m);
 
     return rc;
 }
 
+#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 != -ENOENT && err != -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 = 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 = 0; i < count; i++ )
+    {
+        if ( mfn_eq(mfns[i], INVALID_MFN) ||
+             mfn_eq(p2m_get_entry(p2m, _gfn(gpfns[i]), NULL, NULL, NULL, NULL),
+                    INVALID_MFN) )
+            errs[i] = -ENOENT;
+        else
+            errs[i] = 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-VMID
+     * flush for ranges beyond its per-CPU limit. Include an entry whose
+     * removal failed after changing the p2m.
+     */
+    if ( p2m->need_flush )
+    {
+        for ( i = 0; i < count; i++ )
+        {
+            unsigned int first = i;
+
+            if ( !removal_touched_p2m(errs[i]) )
+                continue;
+
+            while ( i + 1 < count && removal_touched_p2m(errs[i + 1]) &&
+                    gpfns[i + 1] == gpfns[i] + 1 )
+                i++;
+
+            p2m_force_tlb_flush_range_sync(p2m, pfn_to_paddr(gpfns[first]),
+                                           i - first + 1);
+        }
+
+        p2m->need_flush = 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
 
+config HAS_PHYSMAP_REMOVE_BATCH
+	bool
+
 config HAS_PIRQ
 	bool
 
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);
 
+#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 batch.
+ */
+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. */


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN][PATCH v5 7/8] xen/memory: add XENMEM_remove_from_physmap_batch
  2026-11-22  9:54 ` [XEN][PATCH v5 0/8] xen/arm: narrow stage-2 TLB invalidation and batch physmap updates Haseeb Ashraf
                     ` (5 preceding siblings ...)
  2026-11-22  9:54   ` [XEN][PATCH v5 6/8] xen/arm: batch stage-2 removal with range flushes Haseeb Ashraf
@ 2026-11-22  9:54   ` Haseeb Ashraf
  2026-12-01 15:59     ` Jan Beulich
  2026-11-22  9:54   ` [XEN][PATCH v5 8/8] xen/memory: copy add_to_physmap batch entries in bulk Haseeb Ashraf
  7 siblings, 1 reply; 25+ messages in thread
From: Haseeb Ashraf @ 2026-11-22  9:54 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, oleksii.kurochko, community.manager,
	andrew.cooper3, anthony.perard, michal.orzel, jbeulich, julien,
	roger, sstabellini, dpsmith

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

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 <haseeb.ashraf@siemens.com>
---
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://keepachangelog.com/en/1.0.0/)
      afterwards.
 
 ### 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.
 
 ### 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 domain *d,
     return 0;
 }
 
+#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 pool,
+ * 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 = NULL;
+    xen_pfn_t *gpfns = NULL;
+    mfn_t *mfns = NULL;
+    int *errs = NULL;
+    unsigned int chunk, i;
+    int rc = 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 == 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 = min_t(unsigned int, xrfpb->size - extent, XENMEM_RFPB_CHUNK);
+
+    pages = xvmalloc_array(struct page_info *, chunk);
+    gpfns = xvmalloc_array(xen_pfn_t, chunk);
+    mfns = xvmalloc_array(mfn_t, chunk);
+    errs = xvmalloc_array(int, chunk);
+    if ( !pages || !gpfns || !mfns || !errs )
+    {
+        rc = -ENOMEM;
+        goto out;
+    }
+
+    while ( xrfpb->size > extent )
+    {
+        unsigned int n = min_t(unsigned int, xrfpb->size - extent, chunk);
+
+        if ( __copy_from_guest_offset(gpfns, xrfpb->gpfns, extent, n) )
+        {
+            rc = -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 flushed.
+         * A frame without a page is reported as XENMEM_remove_from_physmap
+         * does.
+         */
+        for ( i = 0; i < n; i++ )
+        {
+            pages[i] = get_page_from_gfn(d, gpfns[i], NULL, P2M_ALLOC);
+            if ( pages[i] != NULL )
+            {
+                mfns[i] = page_to_mfn(pages[i]);
+                errs[i] = 0;
+            }
+            else
+            {
+                mfns[i] = INVALID_MFN;
+                errs[i] = -ENOENT;
+            }
+        }
+
+        guest_physmap_remove_page_batch(d, gpfns, mfns, errs, n);
+
+        for ( i = 0; i < n; i++ )
+            if ( pages[i] != NULL )
+                put_page(pages[i]);
+
+        if ( __copy_to_guest_offset(xrfpb->errs, extent, errs, n) )
+        {
+            rc = -EFAULT;
+            goto out;
+        }
+
+        extent += n;
+
+        /* Ask for a continuation if there is more work to do. */
+        if ( xrfpb->size > extent && hypercall_preempt_check() )
+        {
+            rc = 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_HANDLE_PARAM(void) arg)
         break;
     }
 
+#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 != (typeof(xrfpb.size))start_extent )
+            return -EDOM;
+
+        if ( copy_from_guest(&xrfpb, arg, 1) )
+            return -EFAULT;
+
+        d = rcu_lock_domain_by_any_id(xrfpb.domid);
+        if ( d == NULL )
+            return -ESRCH;
+
+        rc = paging_mode_translate(d)
+             ? xsm_remove_from_physmap(XSM_TARGET, curr_d, d)
+             : -EACCES;
+        if ( rc != 0 )
+        {
+            rcu_unlock_domain(d);
+            return rc;
+        }
+
+        rc = xenmem_remove_from_physmap_batch(d, &xrfpb, start_extent);
+
+        rcu_unlock_domain(d);
+
+        if ( rc > 0 )
+            rc = 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 = 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);
 
+/*
+ * A batched version of XENMEM_remove_from_physmap: remove several pages from
+ * the guest physical address space of a domain with a single hypercall.
+ * Each entry gets the result XENMEM_remove_from_physmap would have returned.
+ * 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 range.
+ *
+ * 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 where
+ * it is not implemented, callers must fall back to XENMEM_remove_from_physmap.
+ * Processing is not transactional: a negative return may follow completed
+ * removals, including a chunk whose result copy failed.
+ *
+ * arg == 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*/
 


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* [XEN][PATCH v5 8/8] xen/memory: copy add_to_physmap batch entries in bulk
  2026-11-22  9:54 ` [XEN][PATCH v5 0/8] xen/arm: narrow stage-2 TLB invalidation and batch physmap updates Haseeb Ashraf
                     ` (6 preceding siblings ...)
  2026-11-22  9:54   ` [XEN][PATCH v5 7/8] xen/memory: add XENMEM_remove_from_physmap_batch Haseeb Ashraf
@ 2026-11-22  9:54   ` Haseeb Ashraf
  2026-12-01 17:20     ` Jan Beulich
  7 siblings, 1 reply; 25+ messages in thread
From: Haseeb Ashraf @ 2026-11-22  9:54 UTC (permalink / raw)
  To: xen-devel
  Cc: Haseeb Ashraf, andrew.cooper3, anthony.perard, michal.orzel,
	jbeulich, julien, roger, sstabellini

From: Haseeb Ashraf <haseeb.ashraf@siemens.com>

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 = 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 <haseeb.ashraf@siemens.com>
---
 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 xen_add_to_physmap *xatp,
     return rc;
 }
 
+/*
+ * 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 *xatpb,
                                        unsigned int extent)
@@ -1120,27 +1128,52 @@ static int xenmem_add_to_physmap_batch(struct domain *d,
 
     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 = min_t(unsigned int, xatpb->size - extent,
+                               XENMEM_ATPB_CHUNK);
+        unsigned int i;
+        bool preempt = false;
+        int rc = 0;
+
+        if ( unlikely(__copy_from_guest_offset(idxs, xatpb->idxs,
+                                               extent, n)) ||
+             unlikely(__copy_from_guest_offset(gpfns, xatpb->gpfns,
+                                               extent, n)) )
             return -EFAULT;
 
-        if ( gfn_eq(_gfn(gpfn), INVALID_GFN) )
-            return -EINVAL;
+        for ( i = 0; i < n; i++ )
+        {
+            if ( gfn_eq(_gfn(gpfns[i]), INVALID_GFN) )
+            {
+                rc = -EINVAL;
+                break;
+            }
 
-        rc = xenmem_add_to_physmap_one(d, xatpb->space, extra,
-                                       idx, _gfn(gpfn));
+            errs[i] = xenmem_add_to_physmap_one(d, xatpb->space, extra,
+                                                idxs[i], _gfn(gpfns[i]));
 
-        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 = true;
+                i++;
+                break;
+            }
+        }
+
+        /* i entries have a result. */
+        if ( (i != 0U) &&
+             unlikely(__copy_to_guest_offset(xatpb->errs, extent, errs, i)) )
             return -EFAULT;
 
-        /* Check for continuation if it's not the last iteration. */
-        if ( xatpb->size > ++extent && hypercall_preempt_check() )
+        extent += i;
+
+        if ( rc != 0 )
+            return rc;
+
+        if ( preempt )
             return extent;
     }
 


^ permalink raw reply related	[flat|nested] 25+ messages in thread

* Re: [XEN][PATCH v5 7/8] xen/memory: add XENMEM_remove_from_physmap_batch
  2026-11-22  9:54   ` [XEN][PATCH v5 7/8] xen/memory: add XENMEM_remove_from_physmap_batch Haseeb Ashraf
@ 2026-12-01 15:59     ` Jan Beulich
  0 siblings, 0 replies; 25+ messages in thread
From: Jan Beulich @ 2026-12-01 15:59 UTC (permalink / raw)
  To: Haseeb Ashraf
  Cc: Haseeb Ashraf, oleksii.kurochko, community.manager,
	andrew.cooper3, anthony.perard, michal.orzel, julien, roger,
	sstabellini, dpsmith, xen-devel

On 22.11.2026 10:54, Haseeb Ashraf wrote:
> From: Haseeb Ashraf <haseeb.ashraf@siemens.com>
> 
> 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.

Just that -ENOSYS isn't quite right then. It would better be -EOPNOTSUPP.

> --- a/xen/common/memory.c
> +++ b/xen/common/memory.c
> @@ -1147,6 +1147,117 @@ static int xenmem_add_to_physmap_batch(struct domain *d,
>      return 0;
>  }
>  
> +#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 pool,
> + * 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,

As I can't spot any write through xrfpb - pointer-to-const?

> +    unsigned int extent)
> +{
> +    struct page_info **pages = NULL;
> +    xen_pfn_t *gpfns = NULL;
> +    mfn_t *mfns = NULL;
> +    int *errs = NULL;
> +    unsigned int chunk, i;
> +    int rc = 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 == 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 = min_t(unsigned int, xrfpb->size - extent, XENMEM_RFPB_CHUNK);
> +
> +    pages = xvmalloc_array(struct page_info *, chunk);
> +    gpfns = xvmalloc_array(xen_pfn_t, chunk);
> +    mfns = xvmalloc_array(mfn_t, chunk);
> +    errs = xvmalloc_array(int, chunk);
> +    if ( !pages || !gpfns || !mfns || !errs )
> +    {
> +        rc = -ENOMEM;
> +        goto out;
> +    }
> +
> +    while ( xrfpb->size > extent )
> +    {
> +        unsigned int n = min_t(unsigned int, xrfpb->size - extent, chunk);
> +
> +        if ( __copy_from_guest_offset(gpfns, xrfpb->gpfns, extent, n) )
> +        {
> +            rc = -EFAULT;
> +            goto out;
> +        }
> +
> +        /*
> +         * Take a reference on each page first: this needs the p2m read
> +         * lock, which the removal below holds for writing.

You touch on locking, yet then the code still doesn't look quite right:
Without continuously holding the lock (at least for reading), how do
you ensure ...

> The references
> +         * also keep the pages from being freed before the TLBs are flushed.
> +         * A frame without a page is reported as XENMEM_remove_from_physmap
> +         * does.
> +         */
> +        for ( i = 0; i < n; i++ )
> +        {
> +            pages[i] = get_page_from_gfn(d, gpfns[i], NULL, P2M_ALLOC);
> +            if ( pages[i] != NULL )
> +            {
> +                mfns[i] = page_to_mfn(pages[i]);
> +                errs[i] = 0;
> +            }
> +            else
> +            {
> +                mfns[i] = INVALID_MFN;
> +                errs[i] = -ENOENT;
> +            }
> +        }
> +
> +        guest_physmap_remove_page_batch(d, gpfns, mfns, errs, n);

... this operates on what you did obtain references for? Other changes
could have occurred while e.g. the loop above is still running. Or is
guest_physmap_remove_page_batch() supposed to be checking that? Yet
even then this doesn't feel quite right: With multiple operations
having gone on in between, such a check may succeed but you may still
not remove exactly what the caller intended.

> +        for ( i = 0; i < n; i++ )
> +            if ( pages[i] != NULL )

Nit (also further up): We prefer to omit such " != NULL".

> +                put_page(pages[i]);
> +
> +        if ( __copy_to_guest_offset(xrfpb->errs, extent, errs, n) )
> +        {
> +            rc = -EFAULT;
> +            goto out;
> +        }
> +
> +        extent += n;
> +
> +        /* Ask for a continuation if there is more work to do. */
> +        if ( xrfpb->size > extent && hypercall_preempt_check() )
> +        {
> +            rc = extent;
> +            goto out;
> +        }
> +    }
> +
> + out:
> +    XVFREE(errs);
> +    XVFREE(mfns);
> +    XVFREE(gpfns);
> +    XVFREE(pages);

All four variables are about to go out of scope, hence the simpler
xvfree() will do here-

> @@ -1787,6 +1898,47 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
>          break;
>      }
>  
> +#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 != (typeof(xrfpb.size))start_extent )
> +            return -EDOM;
> +
> +        if ( copy_from_guest(&xrfpb, arg, 1) )
> +            return -EFAULT;
> +
> +        d = rcu_lock_domain_by_any_id(xrfpb.domid);
> +        if ( d == NULL )
> +            return -ESRCH;
> +
> +        rc = paging_mode_translate(d)
> +             ? xsm_remove_from_physmap(XSM_TARGET, curr_d, d)
> +             : -EACCES;
> +        if ( rc != 0 )
> +        {
> +            rcu_unlock_domain(d);
> +            return rc;
> +        }

To avoid the separate return and cleanup, maybe better ...

> +        rc = xenmem_remove_from_physmap_batch(d, &xrfpb, start_extent);

        if ( !rc )
            rc = xenmem_remove_from_physmap_batch(d, &xrfpb, start_extent);

? (Possibly with ASSERT(rc < 0) in an else.)

> +        rcu_unlock_domain(d);
> +
> +        if ( rc > 0 )
> +            rc = 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 = mem_access_memop(cmd, guest_handle_cast(arg, xen_mem_access_op_t));
>          break;
> --- 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);
>  
> +/*
> + * A batched version of XENMEM_remove_from_physmap: remove several pages from
> + * the guest physical address space of a domain with a single hypercall.
> + * Each entry gets the result XENMEM_remove_from_physmap would have returned.
> + * 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 range.

How does contiguous or not matter here?

Jan

> + * 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 where
> + * it is not implemented, callers must fall back to XENMEM_remove_from_physmap.
> + * Processing is not transactional: a negative return may follow completed
> + * removals, including a chunk whose result copy failed.
> + *
> + * arg == 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*/
>  



^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [XEN][PATCH v5 8/8] xen/memory: copy add_to_physmap batch entries in bulk
  2026-11-22  9:54   ` [XEN][PATCH v5 8/8] xen/memory: copy add_to_physmap batch entries in bulk Haseeb Ashraf
@ 2026-12-01 17:20     ` Jan Beulich
  0 siblings, 0 replies; 25+ messages in thread
From: Jan Beulich @ 2026-12-01 17:20 UTC (permalink / raw)
  To: Haseeb Ashraf
  Cc: Haseeb Ashraf, andrew.cooper3, anthony.perard, michal.orzel,
	julien, roger, sstabellini, xen-devel

On 22.11.2026 10:54, Haseeb Ashraf wrote:
> --- a/xen/common/memory.c
> +++ b/xen/common/memory.c
> @@ -1077,6 +1077,14 @@ int xenmem_add_to_physmap(struct domain *d, struct xen_add_to_physmap *xatp,
>      return rc;
>  }
>  
> +/*
> + * 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.
> + */

This is common code, and an equivalent to AT doesn't exist on e.g. x86.
Comments here want writing in a way to applies to all arch-es.

> @@ -1120,27 +1128,52 @@ static int xenmem_add_to_physmap_batch(struct domain *d,
>  
>      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];

This looks like a lot of data you put on the stack for something which
may not help non-Arm very much. I think if such an optimization is
wanted in the first place, it wants either demonstrating that it helps
all arch-es, or it wants making conditional, for an arch to opt in.

Jan


^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2026-12-01 17:20 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-08 13:55 [PATCH 0/3] xen/arm{32,64}: perform IPA-based TLBI when IPA is Haseeb Ashraf
2026-07-21  7:54 ` [XEN][RESEND][PATCH v4 0/3] xen/arm{32,64}: perform IPA-based TLBI when IPA is known Haseeb Ashraf
2026-01-18 13:33 ` [XEN PATCH v3 " Haseeb Ashraf
2025-12-08 13:55 ` [PATCH 1/3] xen/arm/p2m: " Haseeb Ashraf
2026-01-18 13:33   ` [XEN PATCH v3 " Haseeb Ashraf
2025-12-08 13:55 ` [PATCH 2/3] xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA Haseeb Ashraf
2026-01-18 13:33   ` [XEN PATCH v3 " Haseeb Ashraf
2025-12-08 13:55 ` [PATCH 3/3] xen/arm32: add CPU capability for IPA-based TLBI Haseeb Ashraf
2026-01-18 13:33   ` [XEN PATCH v3 " Haseeb Ashraf
2025-12-16 12:08 ` [PATCH 0/3] xen/arm{32,64}: perform IPA-based TLBI when IPA is Ashraf, Haseeb
2026-01-06  6:40   ` Ashraf, Haseeb
2026-07-21  7:54 ` [XEN][RESEND][PATCH 1/3] xen/arm/p2m: perform IPA-based TLBI when IPA is known Haseeb Ashraf
2026-07-21  7:54   ` [XEN][RESEND][PATCH 2/3] xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA Haseeb Ashraf
2026-07-21  7:54   ` [XEN][RESEND][PATCH 3/3] xen/arm32: use IPA-based TLBI when supported with FEAT_nTLBPA Haseeb Ashraf
2026-11-22  9:54 ` [XEN][PATCH v5 0/8] xen/arm: narrow stage-2 TLB invalidation and batch physmap updates Haseeb Ashraf
2026-11-22  9:54   ` [XEN][PATCH v5 1/8] xen/arm/p2m: perform IPA-based TLBI when IPA is known Haseeb Ashraf
2026-11-22  9:54   ` [XEN][PATCH v5 2/8] xen/arm: skip the stage-1 TLBI after IPA flush with FEAT_nTLBPA Haseeb Ashraf
2026-11-22  9:54   ` [XEN][PATCH v5 3/8] xen/arm32: use IPA-based TLBI when supported " Haseeb Ashraf
2026-11-22  9:54   ` [XEN][PATCH v5 4/8] xen/arm64: use range TLBI for IPA flush with FEAT_TLBIRANGE Haseeb Ashraf
2026-11-22  9:54   ` [XEN][PATCH v5 5/8] xen/arm: do not flush the TLBs when resolving a translation fault Haseeb Ashraf
2026-11-22  9:54   ` [XEN][PATCH v5 6/8] xen/arm: batch stage-2 removal with range flushes Haseeb Ashraf
2026-11-22  9:54   ` [XEN][PATCH v5 7/8] xen/memory: add XENMEM_remove_from_physmap_batch Haseeb Ashraf
2026-12-01 15:59     ` Jan Beulich
2026-11-22  9:54   ` [XEN][PATCH v5 8/8] xen/memory: copy add_to_physmap batch entries in bulk Haseeb Ashraf
2026-12-01 17:20     ` Jan Beulich

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.