Skip to contentSkip to conversation

target/arm: Reduce tlb_flush overhead

PATCH In review Richard Henderson 3 participants 12 messages Oct 19, 2018

lore
Share & tools
lore
#0 lore raw

v2 cover target/arm: Reduce tlb_flush overhead

While installing AArch64 Ubuntu into a new vm, I happened to notice that tlb_flush+memset was consuming 25% of the total runtime.

This patch set reduces that overhead to 10%. Full tlb flushes are down to 11k from 1.8M, when pausing the installation at the first menu.

Changes since v1:
* Rename the function in patch 3 to vmsa_ttbr_el1_write.

r~

Richard Henderson (3):
target/arm: Remove writefn from TTBR0_EL3 target/arm: Only flush tlb if ASID changes target/arm: Flush only the TLBs affected by TTBR*_EL1

 target/arm/helper.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
Signature · 1 line
2.17.2
#1 lore raw

PATCH v2 1/3 target/arm: Remove writefn from TTBR0_EL3

The EL3 version of this register does not include an ASID, and so the tlb_flush performed by vmsa_ttbr_write is not needed.

---
 target/arm/helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/helper.c +1 -1
index e3946562aa..24bbde4f76 100644
@@ -4214,7 +4214,7 @@ static const ARMCPRegInfo el3_cp_reginfo[] = {
       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
-      .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
+      .access = PL3_RW, .resetvalue = 0,
       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
Signature · 1 line
2.17.2
Reviewed-by on v2 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] target/arm: Remove writefn from TTBR0_EL3

Quoted 23 lines · Richard Henderson

The EL3 version of this register does not include an ASID, and so the tlb_flush performed by vmsa_ttbr_write is not needed.

---
 target/arm/helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/helper.c +1 -2
index e3946562aa..24bbde4f76 100644
@@ -4214,7 +4214,7 @@ static const ARMCPRegInfo el3_cp_reginfo[] = {
       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
-      .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
+      .access = PL3_RW, .resetvalue = 0,
       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
--
Quoted 1 line

2.17.2

thanks
-- PMM

#3 lore raw

PATCH v2 2/3 target/arm: Only flush tlb if ASID changes

In reply to Richard Henderson

Since QEMU does not implement ASIDs, changes to the ASID must flush the tlb. However, if the ASID does not change there is no reason to flush.

In testing a boot of the Ubuntu installer to the first menu, this reduces the number of flushes by 30%, or nearly 600k instances.

---
 target/arm/helper.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
target/arm/helper.c +3 -5
index 24bbde4f76..ed70ac645e 100644
@@ -2709,12 +2709,10 @@ static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
                             uint64_t value)
 {
-    /* 64 bit accesses to the TTBRs can change the ASID and so we
-     * must flush the TLB.
-     */
-    if (cpreg_field_is_64bit(ri)) {
+    /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
+    if (cpreg_field_is_64bit(ri) &&
+        extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
         ARMCPU *cpu = arm_env_get_cpu(env);
-
         tlb_flush(CPU(cpu));
     }
     raw_write(env, ri, value);
Signature · 1 line
2.17.2
Reviewed-by on v2 2/3
#4 lore raw

Re: [Qemu-devel] [PATCH v2 2/3] target/arm: Only flush tlb if ASID changes

Quoted 8 lines · Richard Henderson

Since QEMU does not implement ASIDs, changes to the ASID must flush the tlb. However, if the ASID does not change there is no reason to flush.

In testing a boot of the Ubuntu installer to the first menu, this reduces the number of flushes by 30%, or nearly 600k instances.

Quoted 25 lines
---
 target/arm/helper.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
target/arm/helper.c +3 -5
index 24bbde4f76..ed70ac645e 100644
@@ -2709,12 +2709,10 @@ static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
                             uint64_t value)
 {
-    /* 64 bit accesses to the TTBRs can change the ASID and so we
-     * must flush the TLB.
-     */
-    if (cpreg_field_is_64bit(ri)) {
+    /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
+    if (cpreg_field_is_64bit(ri) &&
+        extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
         ARMCPU *cpu = arm_env_get_cpu(env);
-
         tlb_flush(CPU(cpu));
     }
     raw_write(env, ri, value);
Reviewed-by on v2 2/3
#5 lore raw

Re: [Qemu-devel] [PATCH v2 2/3] target/arm: Only flush tlb if ASID changes

In reply to Richard Henderson
Quoted 33 lines · Richard Henderson

Since QEMU does not implement ASIDs, changes to the ASID must flush the tlb. However, if the ASID does not change there is no reason to flush.

In testing a boot of the Ubuntu installer to the first menu, this reduces the number of flushes by 30%, or nearly 600k instances.

---
 target/arm/helper.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
target/arm/helper.c +3 -6
index 24bbde4f76..ed70ac645e 100644
@@ -2709,12 +2709,10 @@ static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
                             uint64_t value)
 {
-    /* 64 bit accesses to the TTBRs can change the ASID and so we
-     * must flush the TLB.
-     */
-    if (cpreg_field_is_64bit(ri)) {
+    /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
+    if (cpreg_field_is_64bit(ri) &&
+        extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
         ARMCPU *cpu = arm_env_get_cpu(env);
-
         tlb_flush(CPU(cpu));
     }
     raw_write(env, ri, value);
--
Quoted 1 line

2.17.2

thanks
-- PMM

#6 lore raw

PATCH v2 3/3 target/arm: Flush only the TLBs affected by TTBR*_EL1

In reply to Richard Henderson

Only the EL0 and EL1 TLBs are affected by the EL1 register, so flush only 2 of the 8 TLBs.

In testing a boot of the Ubuntu installer to the first menu, this accounts for nearly all of the full tlb flushes: all but 11k of the 1.2M instances without the patch.

---
 target/arm/helper.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
target/arm/helper.c +9 -7
index ed70ac645e..3ba8e66487 100644
@@ -2706,14 +2706,16 @@ static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
     tcr->raw_tcr = value;
 }
 
-static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
-                            uint64_t value)
+static void vmsa_ttbr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
+                                uint64_t value)
 {
     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
     if (cpreg_field_is_64bit(ri) &&
         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
         ARMCPU *cpu = arm_env_get_cpu(env);
-        tlb_flush(CPU(cpu));
+        tlb_flush_by_mmuidx(CPU(cpu),
+                            ARMMMUIdxBit_S12NSE1 |
+                            ARMMMUIdxBit_S12NSE0);
     }
     raw_write(env, ri, value);
 }
@@ -2761,12 +2763,12 @@ static const ARMCPRegInfo vmsa_cp_reginfo[] = {
       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
-      .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
+      .access = PL1_RW, .writefn = vmsa_ttbr_el1_write, .resetvalue = 0,
       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
-      .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
+      .access = PL1_RW, .writefn = vmsa_ttbr_el1_write, .resetvalue = 0,
       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
@@ -3018,12 +3020,12 @@ static const ARMCPRegInfo lpae_cp_reginfo[] = {
       .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
                              offsetof(CPUARMState, cp15.ttbr0_ns) },
-      .writefn = vmsa_ttbr_write, },
+      .writefn = vmsa_ttbr_el1_write, },
     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
       .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
                              offsetof(CPUARMState, cp15.ttbr1_ns) },
-      .writefn = vmsa_ttbr_write, },
+      .writefn = vmsa_ttbr_el1_write, },
     REGINFO_SENTINEL
 };
 
Signature · 1 line
2.17.2
#7 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] target/arm: Flush only the TLBs affected by TTBR*_EL1

Quoted 23 lines · Richard Henderson

Only the EL0 and EL1 TLBs are affected by the EL1 register, so flush only 2 of the 8 TLBs.

In testing a boot of the Ubuntu installer to the first menu, this accounts for nearly all of the full tlb flushes: all but 11k of the 1.2M instances without the patch.

---
 target/arm/helper.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
target/arm/helper.c +1 -2
index ed70ac645e..3ba8e66487 100644
@@ -2706,14 +2706,16 @@ static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
     tcr->raw_tcr = value;
 }
 
-static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
-                            uint64_t value)
+static void vmsa_ttbr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,

target/arm/helper.c static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 2718

Quoted 10 lines
target/arm/helper.c +4 -1
+                                uint64_t value)
 {
     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
     if (cpreg_field_is_64bit(ri) &&
         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
         ARMCPU *cpu = arm_env_get_cpu(env);
-        tlb_flush(CPU(cpu));
+        tlb_flush_by_mmuidx(CPU(cpu),
+                            ARMMMUIdxBit_S12NSE1 |
+                            ARMMMUIdxBit_S12NSE0);

This isn't taking account of the possibility of secure mode. ARMMMUIdxBit_S1SE0 and ARMMMUIdxBit_S1SE1 might also be affected.

And for AArch32, this writefn is used for the secure-banked versions of TTBR0/TTBR1, which means ARMMMUIdxBit_S1E3 may also need flushing.

thanks
-- PMM

#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] target/arm: Flush only the TLBs affected by TTBR*_EL1

Quoted 22 lines · Peter Maydell
Quoted 20 lines · Richard Henderson

Only the EL0 and EL1 TLBs are affected by the EL1 register, so flush only 2 of the 8 TLBs.

In testing a boot of the Ubuntu installer to the first menu, this accounts for nearly all of the full tlb flushes: all but 11k of the 1.2M instances without the patch.

---
 target/arm/helper.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
target/arm/helper.c +0 -0
index ed70ac645e..3ba8e66487 100644
@@ -2706,14 +2706,16 @@ static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
     tcr->raw_tcr = value;
 }
 
Quoted 16 lines

target/arm/helper.c static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 2718

Quoted 13 lines
target/arm/helper.c +5 -3
-static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
-                            uint64_t value)
+static void vmsa_ttbr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
+                                uint64_t value)
 {
     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
     if (cpreg_field_is_64bit(ri) &&
         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
         ARMCPU *cpu = arm_env_get_cpu(env);
-        tlb_flush(CPU(cpu));
+        tlb_flush_by_mmuidx(CPU(cpu),
+                            ARMMMUIdxBit_S12NSE1 |
+                            ARMMMUIdxBit_S12NSE0);

This isn't taking account of the possibility of secure mode. ARMMMUIdxBit_S1SE0 and ARMMMUIdxBit_S1SE1 might also be affected.

Ah. Is there an easy way to tell if secure mode is present/enabled? It'd be nice to not flush tlbs that aren't in use...

Quoted 2 lines

And for AArch32, this writefn is used for the secure-banked versions of TTBR0/TTBR1, which means ARMMMUIdxBit_S1E3 may also need flushing.

For aarch32, we don't have an asid, and so do not flush at all.

r~

#9 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] target/arm: Flush only the TLBs affected by TTBR*_EL1

Quoted 40 lines · Richard Henderson
Quoted 22 lines · Peter Maydell
Quoted 20 lines · Richard Henderson

Only the EL0 and EL1 TLBs are affected by the EL1 register, so flush only 2 of the 8 TLBs.

In testing a boot of the Ubuntu installer to the first menu, this accounts for nearly all of the full tlb flushes: all but 11k of the 1.2M instances without the patch.

---
 target/arm/helper.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
target/arm/helper.c +0 -0
index ed70ac645e..3ba8e66487 100644
@@ -2706,14 +2706,16 @@ static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
     tcr->raw_tcr = value;
 }
 
Quoted 16 lines

target/arm/helper.c static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 2718

Quoted 13 lines
target/arm/helper.c +5 -3
-static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
-                            uint64_t value)
+static void vmsa_ttbr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
+                                uint64_t value)
 {
     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
     if (cpreg_field_is_64bit(ri) &&
         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
         ARMCPU *cpu = arm_env_get_cpu(env);
-        tlb_flush(CPU(cpu));
+        tlb_flush_by_mmuidx(CPU(cpu),
+                            ARMMMUIdxBit_S12NSE1 |
+                            ARMMMUIdxBit_S12NSE0);

This isn't taking account of the possibility of secure mode. ARMMMUIdxBit_S1SE0 and ARMMMUIdxBit_S1SE1 might also be affected.

Quoted 2 lines

Ah. Is there an easy way to tell if secure mode is present/enabled? It'd be nice to not flush tlbs that aren't in use...

If ARM_FEATURE_EL3 is set, we have an EL3. It is probably possible to look at the current state of the CPU and determine whether we need to flush the NS TLBs or the S TLBs, but I would need to think about that. For AArch32 there are banked registers here, so we could in theory distinguish writes to the S banked and NS banked regs. For AArch64 there's only one EL1 register, so we'd need to check the Arm ARM for how interleaved writes to the TTBR_EL1 and flipping between S and NS work (and when the guest is supposed to do the TLB maintenance anyway).

A conservative check is probably:
if arm_current_el() < 3

     // TTBR definitely can only be affecting the EL0/1
     // translation regime for the current security state
     if arm_is_secure_below_el3()
        if EL3 is AArch32
            flush S1SE0, S1E3
        else
            flush S1SE0, S1SE1
     else
        flush S12NSE1, S12NSE0
  else
     // err on the side of flushing more than maybe we need to
     flush S1SE0, S12NSE1, S12NSE0
     if EL3 is AArch32
          flush S1E3
     else
          flush S1SE1

(but you should check my logic ;-))

Quoted 4 lines
Quoted 2 lines

And for AArch32, this writefn is used for the secure-banked versions of TTBR0/TTBR1, which means ARMMMUIdxBit_S1E3 may also need flushing.

For aarch32, we don't have an asid, and so do not flush at all.

We do for AArch32 with TTBCR.EAE == 1 (ie LPAE, when you want to use the 64-bit form of the register, accessed via MRRC/MCRR). cpreg_field_is_64bit() is true for both "AArch64 sysreg" and "AArch32 64-bit cp reg".

thanks
-- PMM

#10 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] target/arm: Flush only the TLBs affected by TTBR*_EL1

Quoted 19 lines · Peter Maydell

A conservative check is probably:
if arm_current_el() < 3

     // TTBR definitely can only be affecting the EL0/1
     // translation regime for the current security state
     if arm_is_secure_below_el3()
        if EL3 is AArch32
            flush S1SE0, S1E3
        else
            flush S1SE0, S1SE1
     else
        flush S12NSE1, S12NSE0
  else
     // err on the side of flushing more than maybe we need to
     flush S1SE0, S12NSE1, S12NSE0
     if EL3 is AArch32
          flush S1E3
     else
          flush S1SE1
Quoted 1 line

(but you should check my logic ;-))

Riiight.

Clearly it would be simpler and safer to track unused tlbs within cputlb.c.

Quoted 4 lines

We do for AArch32 with TTBCR.EAE == 1 (ie LPAE, when you want to use the 64-bit form of the register, accessed via MRRC/MCRR). cpreg_field_is_64bit() is true for both "AArch64 sysreg" and "AArch32 64-bit cp reg".

Ok, thanks.

So drop this patch for now and I'll get back to it. The other two in this series are at least incremental improvement in the meantime.

r~

#11 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] target/arm: Flush only the TLBs affected by TTBR*_EL1

Quoted 20 lines · Richard Henderson
Quoted 19 lines · Peter Maydell

A conservative check is probably:
if arm_current_el() < 3

     // TTBR definitely can only be affecting the EL0/1
     // translation regime for the current security state
     if arm_is_secure_below_el3()
        if EL3 is AArch32
            flush S1SE0, S1E3
        else
            flush S1SE0, S1SE1
     else
        flush S12NSE1, S12NSE0
  else
     // err on the side of flushing more than maybe we need to
     flush S1SE0, S12NSE1, S12NSE0
     if EL3 is AArch32
          flush S1E3
     else
          flush S1SE1
Quoted 5 lines
Quoted 1 line

(but you should check my logic ;-))

Riiight.

Clearly it would be simpler and safer to track unused tlbs within cputlb.c.

The advantage of the above logic is that it means that even when we do have trustzone and are using the secure TLBs, we only flush the side we need to, not the whole lot. (But yeah, it's a bit hairy.)

Quoted 2 lines

So drop this patch for now and I'll get back to it. The other two in this series are at least incremental improvement in the meantime.

OK, I'll put those in target-arm.next.

thanks
-- PMM