Skip to contentSkip to conversation

xen/arm{32,64}: perform IPA-based TLBI when IPA is

PATCH In review Haseeb Ashraf 2 participants 18 messages Dec 8, 2025 – Nov 22

lore
Share & tools
lore
#0 lore raw

cover xen/arm{32,64}: perform IPA-based TLBI when IPA is

From: Haseeb Ashraf <[email protected]>

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(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 xen/arm/p2m: perform IPA-based TLBI when IPA is known

From: Haseeb Ashraf <[email protected]>

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.

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(-)
xen/arch/arm/include/asm/arm32/flushtlb.h +53 -0
index 61c25a3189..3c0c2123d4 100644
@@ -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:
xen/arch/arm/include/asm/arm64/flushtlb.h +46 -0

64 lines not on this page · raw message

xen/arch/arm/include/asm/mmu/p2m.h +2 -0
index 58496c0b09..8a16722b82 100644
@@ -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);
xen/arch/arm/mmu/p2m.c +67 -25

151 lines not on this page · raw message

Signature · 1 line
2.43.0
#5 lore raw

PATCH 2/3 xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

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.

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(-)
xen/arch/arm/cpufeature.c +19 -0
index 1a80738571..9fa1c45869 100644
@@ -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)",
xen/arch/arm/include/asm/arm32/flushtlb.h +8 -6
index 3c0c2123d4..7cff042508 100644
@@ -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. */
xen/arch/arm/include/asm/arm64/flushtlb.h +54 -23

105 lines not on this page · raw message

xen/arch/arm/include/asm/cpufeature.h +21 -3
index 13353c8e1a..9f796ed4c1 100644
@@ -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 {
xen/arch/arm/include/asm/processor.h +7 -0
index 1a48c9ff3b..85f3b643a0 100644
@@ -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__
Signature · 1 line
2.43.0
#8 lore raw

PATCH 3/3 xen/arm32: add CPU capability for IPA-based TLBI

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

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.

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(-)
xen/arch/arm/cpufeature.c +12 -0
index 9fa1c45869..d18c6449c6 100644
@@ -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)",
xen/arch/arm/include/asm/arm32/flushtlb.h +37 -5

55 lines not on this page · raw message

xen/arch/arm/include/asm/cpregs.h +4 -0
index a7503a190f..51f091dace 100644
@@ -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 */
xen/arch/arm/include/asm/cpufeature.h +9 -6
index 9f796ed4c1..07f1d770b3 100644
@@ -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;
 
xen/arch/arm/include/asm/processor.h +3 -0
index 85f3b643a0..eda39566e1 100644
@@ -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
Signature · 1 line
2.43.0
#7 lore raw

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) <[email protected]>
Sent: Tuesday, December 16, 2025 5:08 PM
To: Haseeb Ashraf <[email protected]>; [email protected] <[email protected]>; Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Michal Orzel <[email protected]>; Volodymyr Babchuk <[email protected]>; Driscoll, Dan (DI SW EDA HAV SLS EPS TOA) <[email protected]>; Ahsan Khawaja, Noor (DI SW EDA HAV SLS EPS RTOS LIN) <[email protected]>; Arslan, Fahad (DI SW EDA HAV SLS EPS RTOS LIN) <[email protected]>; Bachtel, Andrew (DI SW EDA HAV SLS EPS TOA) <[email protected]>
Subject: Re: [PATCH 0/3] xen/arm{32,64}: perform IPA-based TLBI when IPA is

Hi @Julien Grall<mailto:[email protected]>,

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

#2 lore raw

PATCH RESEND 1/3 xen/arm/p2m: perform IPA-based TLBI when IPA is known

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

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.

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(-)
xen/arch/arm/include/asm/arm32/flushtlb.h +53 -0
index 5483be08fb..1497f16ba5 100644
@@ -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:
xen/arch/arm/include/asm/arm64/flushtlb.h +62 -0

80 lines not on this page · raw message

xen/arch/arm/include/asm/mmu/p2m.h +2 -0
index 58496c0b09..8a16722b82 100644
@@ -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);
xen/arch/arm/mmu/p2m.c +78 -27

166 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

PATCH RESEND 2/3 xen/arm: optimize stage-1,2 combined TLBI in presence of FEAT_nTLBPA

From: Haseeb Ashraf <[email protected]>

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.

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(-)
xen/arch/arm/cpufeature.c +19 -0
index 94d14fb6a9..86720c10a3 100644
@@ -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)",
xen/arch/arm/include/asm/arm32/flushtlb.h +8 -6
index 1497f16ba5..30df161323 100644
@@ -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. */
xen/arch/arm/include/asm/arm64/flushtlb.h +38 -6

71 lines not on this page · raw message

xen/arch/arm/include/asm/cpufeature.h +21 -3
index bf902a3970..3e56dd27ef 100644
@@ -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 {
xen/arch/arm/include/asm/processor.h +7 -0
index a3753c317f..dbf9471a7a 100644
@@ -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
Signature · 1 line
2.43.0
#4 lore raw

PATCH RESEND 3/3 xen/arm32: use IPA-based TLBI when supported with FEAT_nTLBPA

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

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.

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(-)
xen/arch/arm/cpufeature.c +12 -0
index 86720c10a3..9ca449a551 100644
@@ -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)",
xen/arch/arm/include/asm/arm32/flushtlb.h +42 -5

60 lines not on this page · raw message

xen/arch/arm/include/asm/cpregs.h +1 -0
index a7503a190f..26b5c08d0e 100644
@@ -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 */
xen/arch/arm/include/asm/cpufeature.h +9 -6
index 3e56dd27ef..09a516fe49 100644
@@ -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;
 
xen/arch/arm/include/asm/processor.h +3 -0
index dbf9471a7a..0c47225de0 100644
@@ -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
Signature · 1 line
2.43.0
#13 lore raw

PATCH v5 4/8 xen/arm64: use range TLBI for IPA flush with FEAT_TLBIRANGE

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

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

---
 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(-)
xen/arch/arm/cpufeature.c +14 -0
index e4e0dd0922..05c4393324 100644
@@ -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
     {},
 };
xen/arch/arm/include/asm/arm64/flushtlb.h +105 -8

137 lines not on this page · raw message

xen/arch/arm/include/asm/cpufeature.h +2 -1
index 38e01aa94d..a4f7bcbe3e 100644
@@ -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__
 
#9 lore raw

v5 cover xen/arm: narrow stage-2 TLB invalidation and batch physmap updates

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

Base commit: ada7fa3491668bbcf1afe525057c470903625d26

Previous version:
https://patchew.org/Xen/[email protected]/

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/[email protected]/ (cover letter)
[2] https://lore.kernel.org/all/[email protected]/ (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(-)
#11 lore raw

PATCH v5 2/8 xen/arm: skip the stage-1 TLBI after IPA flush with FEAT_nTLBPA

From: Haseeb Ashraf <[email protected]>

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.

---
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(-)
xen/arch/arm/cpufeature.c +12 -0
index 8d8cd19a96..0a87186a7e 100644
@@ -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)",
xen/arch/arm/include/asm/arm64/flushtlb.h +5 -2
index b3bce7baf2..ff2ce2bae2 100644
@@ -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();
 }
xen/arch/arm/include/asm/cpufeature.h +4 -2
index c554686415..97e371724f 100644
@@ -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 */
xen/arch/arm/include/asm/processor.h +2 -0
index 8ee8f88fb4..49774b65eb 100644
@@ -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
xen/arch/arm/mmu/p2m.c +2 -0
index b93ead6c46..3b1b9b394c 100644
@@ -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();
#15 lore raw

PATCH v5 6/8 xen/arm: batch stage-2 removal with range flushes

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

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

---
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(-)
xen/arch/arm/Kconfig +1 -0
index b9c5c40678..7ad676940b 100644
@@ -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
xen/arch/arm/p2m.c +86 -11

131 lines not on this page · raw message

xen/common/Kconfig +3 -0
index a619f2cef1..e687b59e8f 100644
@@ -152,6 +152,9 @@ config HAS_KEXEC
 config HAS_LLC_COLORING
 	bool
 
+config HAS_PHYSMAP_REMOVE_BATCH
+	bool
+
 config HAS_PIRQ
 	bool
 
xen/include/xen/p2m-common.h +16 -0
index 0eb0619912..7a78c8b421 100644
@@ -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. */
#14 lore raw

PATCH v5 5/8 xen/arm: do not flush the TLBs when resolving a translation fault

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

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.

---
New in v5.

 xen/arch/arm/mmu/p2m.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
xen/arch/arm/mmu/p2m.c +13 -0
index 3b1b9b394c..8cbd504fdc 100644
@@ -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
#17 lore raw

PATCH v5 8/8 xen/memory: copy add_to_physmap batch entries in bulk

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

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.

---
 xen/common/memory.c | 63 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 48 insertions(+), 15 deletions(-)
xen/common/memory.c +48 -15
index b5256bf6ec..c493766abd 100644
@@ -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;
     }
 
#12 lore raw

PATCH v5 3/8 xen/arm32: use IPA-based TLBI when supported with FEAT_nTLBPA

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

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.

---
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(-)
xen/arch/arm/cpufeature.c +20 -1
index 0a87186a7e..e4e0dd0922 100644
@@ -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,
xen/arch/arm/include/asm/arm32/flushtlb.h +38 -4

56 lines not on this page · raw message

xen/arch/arm/include/asm/cpregs.h +1 -0
index 7e559253d7..9869bc7d3c 100644
@@ -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 */
xen/arch/arm/include/asm/cpufeature.h +20 -1
index 97e371724f..38e01aa94d 100644
@@ -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 {
xen/arch/arm/include/asm/processor.h +7 -0
index 49774b65eb..6899f91ef0 100644
@@ -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
#10 lore raw

PATCH v5 1/8 xen/arm/p2m: perform IPA-based TLBI when IPA is known

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

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

---
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(-)
xen/arch/arm/include/asm/arm32/flushtlb.h +33 -0
index 5483be08fb..374b8bfe54 100644
@@ -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:
xen/arch/arm/include/asm/arm64/flushtlb.h +78 -0

90 lines not on this page · raw message

xen/arch/arm/include/asm/flushtlb.h +12 -0
index c292c3c00d..2711b76611 100644
@@ -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)
xen/arch/arm/include/asm/mmu/p2m.h +2 -0
index 58496c0b09..8a16722b82 100644
@@ -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);
xen/arch/arm/include/asm/mpu/p2m.h +4 -0
index 1484c75b55..b799738314 100644
@@ -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__ */
 
 /*
xen/arch/arm/mmu/p2m.c +82 -17

164 lines not on this page · raw message

xen/arch/arm/p2m.c +15 -1
index fb03978a19..a39351ede9 100644
@@ -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;
#16 lore raw

PATCH v5 7/8 xen/memory: add XENMEM_remove_from_physmap_batch

In reply to Haseeb Ashraf

From: Haseeb Ashraf <[email protected]>

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.

---
New in v5.

 CHANGELOG.md                |   5 ++
 xen/common/memory.c         | 152 ++++++++++++++++++++++++++++++++++++
 xen/include/public/memory.h |  35 +++++++++
 3 files changed, 192 insertions(+)
CHANGELOG.md +5 -0
index 225397f128..39b3d16928 100644
@@ -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:
xen/common/memory.c +152 -0

164 lines not on this page · raw message

xen/include/public/memory.h +35 -0
index bd9fc37b52..4de16cd59c 100644
@@ -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*/