Skip to contentSkip to conversation

Use invpcid

PATCH In review Wei Liu 4 participants 37 messages Mar 5, 2018 – Mar 13, 2018 6 messages not in the archive

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

cover Use invpcid

Wei Liu (2):
x86: report if PCID and INVPCID are supported
x86: use invpcid to do global flushing

 xen/arch/x86/flushtlb.c | 22 ++++++++++++++++++----
 xen/arch/x86/setup.c    |  7 +++++++
 2 files changed, 25 insertions(+), 4 deletions(-)
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#1 lore raw

PATCH 1/2 x86: report if PCID and INVPCID are supported

---
 xen/arch/x86/setup.c | 7 +++++++
 1 file changed, 7 insertions(+)
xen/arch/x86/setup.c +7 -0
index ac530ece2c..89e42865a4 100644
@@ -1701,6 +1701,13 @@ void __init noreturn __start_xen(unsigned long mbi_p)
            cpu_has_nx ? XENLOG_INFO : XENLOG_WARNING "Warning: ",
            cpu_has_nx ? "" : "not ");
 
+
+    printk(XENLOG_INFO
+           "PCID (Process-Context IDentifier) %ssupported\n",
+           cpu_has_pcid ? "" : "not ");
+
+    printk(XENLOG_INFO "INVPCID %ssupported\n", cpu_has_invpcid ? "" : "not ");
+
     /*
      * We're going to setup domain0 using the module(s) that we stashed safely
      * above our heap. The second module, if present, is an initrd ramdisk.
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author
#13 lore raw

PATCH 2/2 x86: use invpcid to do global flushing

In reply to Wei Liu
---
 xen/arch/x86/flushtlb.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
xen/arch/x86/flushtlb.c +18 -4
index 8a7a76b8ff..e4ea4f3297 100644
@@ -9,6 +9,7 @@
 
 #include <xen/sched.h>
 #include <xen/softirq.h>
+#include <asm/invpcid.h>
 #include <asm/flushtlb.h>
 #include <asm/page.h>
 
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
+                 * It is faster than read-modify-write CR4.
+                 */
+                invpcid_flush_all();
+            }
 
             post_flush(t);
         }
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw

Re: [PATCH 1/2] x86: report if PCID and INVPCID are supported

In reply to Wei Liu

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1709

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/setup.c +6 -0
@@ -1701,6 +1701,13 @@ void __init noreturn __start_xen(unsigned long mbi_p)
            cpu_has_nx ? XENLOG_INFO : XENLOG_WARNING "Warning: ",
            cpu_has_nx ? "" : "not ");
 
+
+    printk(XENLOG_INFO
+           "PCID (Process-Context IDentifier) %ssupported\n",
+           cpu_has_pcid ? "" : "not ");
+
+    printk(XENLOG_INFO "INVPCID %ssupported\n", cpu_has_invpcid ? "" : "not ");

Do we really need this? We log a message for NX as an exception, we don't do so for other features (and things would get pretty unwieldy if we did).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#15 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to Wei Liu
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:

No description at all? I'd at least expect mention of how much of a performance win this is (for whichever hardware you happen to know that).

Quoted 16 lines
xen/arch/x86/flushtlb.c +7 -4
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 141

Quoted 10 lines
xen/arch/x86/flushtlb.c +10 -0
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
+                 * It is faster than read-modify-write CR4.
+                 */
+                invpcid_flush_all();
+            }

The reference to PCID in the comment isn't really meaningful imo. PCID and INVPCID are independent features anyway. Also please don't create artificially short comment lines.

Generally I also think such if() conditions would better be inverted:
There's no reason to make the legacy form look as if it was preferred.

And then - what about the use in write_cr3() and the two uses that remain after my XPTI follow-up series (which sadly looks to be stuck for whatever reason), or (without that series) the write_cr3 assembler macro?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#4 lore raw

Re: [PATCH 1/2] x86: report if PCID and INVPCID are supported

In reply to Jan Beulich
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/setup.c +0 -0
@@ -1701,6 +1701,13 @@ void __init noreturn __start_xen(unsigned long mbi_p)
            cpu_has_nx ? XENLOG_INFO : XENLOG_WARNING "Warning: ",
            cpu_has_nx ? "" : "not ");
 
Quoted 11 lines

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1709

Quoted 6 lines
xen/arch/x86/setup.c +6 -0
+
+    printk(XENLOG_INFO
+           "PCID (Process-Context IDentifier) %ssupported\n",
+           cpu_has_pcid ? "" : "not ");
+
+    printk(XENLOG_INFO "INVPCID %ssupported\n", cpu_has_invpcid ? "" : "not ");

Do we really need this? We log a message for NX as an exception, we don't do so for other features (and things would get pretty unwieldy if we did).

I'm not too fuss really. It helped me to pick a machine when I didn't have the exact spec of the pool of machines. The same information can certainly be obtained from hardware cpuid info.

On another note, what is the most reliable way to extract hardware cpuid info from Xen? AIUI now cpuid faulting can be enabled for both PV and HVM guests so running cpuid in PV guests might not return the real bits (which is a good thing).

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw

Re: [PATCH 1/2] x86: report if PCID and INVPCID are supported

In reply to a message not in the archive
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/setup.c +0 -0
@@ -1701,6 +1701,13 @@ void __init noreturn __start_xen(unsigned long mbi_p)
            cpu_has_nx ? XENLOG_INFO : XENLOG_WARNING "Warning: ",
            cpu_has_nx ? "" : "not ");
 
Quoted 10 lines

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1709

Quoted 6 lines
xen/arch/x86/setup.c +6 -0
+
+    printk(XENLOG_INFO
+           "PCID (Process-Context IDentifier) %ssupported\n",
+           cpu_has_pcid ? "" : "not ");
+
+    printk(XENLOG_INFO "INVPCID %ssupported\n", cpu_has_invpcid ? "" : "not ");

Do we really need this? We log a message for NX as an exception, we don't do so for other features (and things would get pretty unwieldy if we did).

I'd rather keep this message. As we are hiding PCID and INPCID from dom0 this is the only indicator of those features being supported. In case of an error related to TLB consistency this information is important IMO.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

Re: [PATCH 1/2] x86: report if PCID and INVPCID are supported

In reply to Wei Liu
Quoted 18 lines · Wei Liu
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/setup.c +0 -0
@@ -1701,6 +1701,13 @@ void __init noreturn __start_xen(unsigned long mbi_p)
            cpu_has_nx ? XENLOG_INFO : XENLOG_WARNING "Warning: ",
            cpu_has_nx ? "" : "not ");
 
Quoted 10 lines

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1709

Quoted 6 lines
xen/arch/x86/setup.c +6 -0
+
+    printk(XENLOG_INFO
+           "PCID (Process-Context IDentifier) %ssupported\n",
+           cpu_has_pcid ? "" : "not ");
+
+    printk(XENLOG_INFO "INVPCID %ssupported\n", cpu_has_invpcid ? "" : "not ");

Do we really need this? We log a message for NX as an exception, we don't do so for other features (and things would get pretty unwieldy if we did).

Quoted 8 lines

I'm not too fuss really. It helped me to pick a machine when I didn't have the exact spec of the pool of machines. The same information can certainly be obtained from hardware cpuid info.

On another note, what is the most reliable way to extract hardware cpuid info from Xen? AIUI now cpuid faulting can be enabled for both PV and HVM guests so running cpuid in PV guests might not return the real bits (which is a good thing).

For now, xen-cpuid, which uses SYSCTL_get_cpufeatureset internally.

This will shortly be changing to get the fully cpuid/msr policies rather than just the feature bitmaps.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#16 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to Jan Beulich
Quoted 22 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:

No description at all? I'd at least expect mention of how much of a performance win this is (for whichever hardware you happen to know that).

Quoted 16 lines
xen/arch/x86/flushtlb.c +7 -4
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);
Quoted 8 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 139

Quoted 8 lines
xen/arch/x86/flushtlb.c +8 -0
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
+                 * It is faster than read-modify-write CR4.
+                 */

Its a cr4 double write, rather than RMW.  We read from a cached value anyway, not from hardware.

Quoted 6 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 141

Quoted 2 lines
xen/arch/x86/flushtlb.c +2 -0
+                invpcid_flush_all();
+            }

The reference to PCID in the comment isn't really meaningful imo. PCID and INVPCID are independent features anyway. Also please don't create artificially short comment lines.

Quoted 8 lines

Generally I also think such if() conditions would better be inverted:
There's no reason to make the legacy form look as if it was preferred.

And then - what about the use in write_cr3() and the two uses that remain after my XPTI follow-up series (which sadly looks to be stuck for whatever reason), or (without that series) the write_cr3 assembler macro?

I don't think it is safe to use invpcid when we're also switching cr3. The new cr3 may have global pages with different translations, as they are guest controlled.

(In fact - I'm considering using this property to try and allow us to deliberately trigger MCEs on demand, for testing purposes).

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#17 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

Quoted 45 lines · Andrew Cooper
Quoted 22 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:

No description at all? I'd at least expect mention of how much of a performance win this is (for whichever hardware you happen to know that).

Quoted 16 lines
xen/arch/x86/flushtlb.c +7 -4
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);
Quoted 8 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 139

Quoted 8 lines
xen/arch/x86/flushtlb.c +8 -0
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
+                 * It is faster than read-modify-write CR4.
+                 */

Its a cr4 double write, rather than RMW.  We read from a cached value anyway, not from hardware.

Quoted 10 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 141

Quoted 2 lines
xen/arch/x86/flushtlb.c +2 -0
+                invpcid_flush_all();
+            }

The reference to PCID in the comment isn't really meaningful imo. PCID and INVPCID are independent features anyway. Also please don't create artificially short comment lines.

Generally I also think such if() conditions would better be inverted:
There's no reason to make the legacy form look as if it was preferred.

Quoted 8 lines
Quoted 4 lines

And then - what about the use in write_cr3() and the two uses that remain after my XPTI follow-up series (which sadly looks to be stuck for whatever reason), or (without that series) the write_cr3 assembler macro?

I don't think it is safe to use invpcid when we're also switching cr3. The new cr3 may have global pages with different translations, as they are guest controlled.

Can you elaborate a little bit more?

How can a guest control any hypervisor mappings? As long as the new cr3 is being loaded before the TLB is flushed via INVPCID I can't see how a problem should occur.

In fact my series does exactly what Jan is asking above: it is replacing the remaining cr4 based TLB flushing by INVPCID if possible. So in case there is a flaw in my design please tell me.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#18 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

Quoted 54 lines · Juergen Gross
Quoted 44 lines · Andrew Cooper
Quoted 22 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:

No description at all? I'd at least expect mention of how much of a performance win this is (for whichever hardware you happen to know that).

Quoted 16 lines
xen/arch/x86/flushtlb.c +7 -4
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);
Quoted 8 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 139

Quoted 8 lines
xen/arch/x86/flushtlb.c +8 -0
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
+                 * It is faster than read-modify-write CR4.
+                 */

Its a cr4 double write, rather than RMW.  We read from a cached value anyway, not from hardware.

Quoted 10 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 141

Quoted 2 lines
xen/arch/x86/flushtlb.c +2 -0
+                invpcid_flush_all();
+            }

The reference to PCID in the comment isn't really meaningful imo. PCID and INVPCID are independent features anyway. Also please don't create artificially short comment lines.

Generally I also think such if() conditions would better be inverted:
There's no reason to make the legacy form look as if it was preferred.

Quoted 7 lines
Quoted 4 lines

And then - what about the use in write_cr3() and the two uses that remain after my XPTI follow-up series (which sadly looks to be stuck for whatever reason), or (without that series) the write_cr3 assembler macro?

I don't think it is safe to use invpcid when we're also switching cr3. The new cr3 may have global pages with different translations, as they are guest controlled.

Can you elaborate a little bit more?

Quoted 7 lines

How can a guest control any hypervisor mappings? As long as the new cr3 is being loaded before the TLB is flushed via INVPCID I can't see how a problem should occur.

In fact my series does exactly what Jan is asking above: it is replacing the remaining cr4 based TLB flushing by INVPCID if possible. So in case there is a flaw in my design please tell me.

At the moment, we have guest and hypervisor controlled global mappings.

The current switch is:
cr4 &= ~PGE;
cr3 = new_cr3;
cr4 |= PGE;

which means that all global mappings are flushed by the first action, and no new global mappings can come into existence.  We then switch to the new cr3 (again with global fully disabled), then allow global mappings to come back into existence.

With the invpcid route, we switch via:

cr3 = new_cr3;
invpcid all+global;

This has a race window where global mappings are active, and could mismatch what is in cr3.  This yields #MC on at least some hardware, and is specified to have undefined behaviour.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

Re: [PATCH 1/2] x86: report if PCID and INVPCID are supported

In reply to Juergen Gross
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 12:43, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/setup.c +0 -0
@@ -1701,6 +1701,13 @@ void __init noreturn __start_xen(unsigned long mbi_p)
            cpu_has_nx ? XENLOG_INFO : XENLOG_WARNING "Warning: ",
            cpu_has_nx ? "" : "not ");
 
Quoted 10 lines

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1709

Quoted 6 lines
xen/arch/x86/setup.c +6 -0
+
+    printk(XENLOG_INFO
+           "PCID (Process-Context IDentifier) %ssupported\n",
+           cpu_has_pcid ? "" : "not ");
+
+    printk(XENLOG_INFO "INVPCID %ssupported\n", cpu_has_invpcid ? "" : "not ");

Do we really need this? We log a message for NX as an exception, we don't do so for other features (and things would get pretty unwieldy if we did).

Quoted 3 lines

I'd rather keep this message. As we are hiding PCID and INPCID from dom0 this is the only indicator of those features being supported. In case of an error related to TLB consistency this information is important IMO.

Well, this argument would hold for almost every other advanced feature we use.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

Re: [PATCH 1/2] x86: report if PCID and INVPCID are supported

In reply to a message not in the archive
Quoted 20 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 12:43, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1709

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/setup.c +6 -0
@@ -1701,6 +1701,13 @@ void __init noreturn __start_xen(unsigned long mbi_p)
            cpu_has_nx ? XENLOG_INFO : XENLOG_WARNING "Warning: ",
            cpu_has_nx ? "" : "not ");
 
+
+    printk(XENLOG_INFO
+           "PCID (Process-Context IDentifier) %ssupported\n",
+           cpu_has_pcid ? "" : "not ");
+
+    printk(XENLOG_INFO "INVPCID %ssupported\n", cpu_has_invpcid ? "" : "not ");

Do we really need this? We log a message for NX as an exception, we don't do so for other features (and things would get pretty unwieldy if we did).

Quoted 6 lines
Quoted 3 lines

I'd rather keep this message. As we are hiding PCID and INPCID from dom0 this is the only indicator of those features being supported. In case of an error related to TLB consistency this information is important IMO.

Well, this argument would hold for almost every other advanced feature we use.

Then I'd suggest to either add individual messages for those features or to have a single message (or a few) to list all features like the flags in /proc/cpuinfo. This list could even be part of the "xl info" output.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#19 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to Andrew Cooper
Quoted 77 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 13:35, <[email protected]> wrote:
Quoted 9 lines · Juergen Gross
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:

No description at all? I'd at least expect mention of how much of a performance win this is (for whichever hardware you happen to know that).

Review unsigned int flush_area_local(const void *va, unsigned

Quoted 1 line
(quoted diff) +0 -0
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned 

int flags)

Quoted 45 lines
Quoted 36 lines
Quoted 15 lines
Quoted 15 lines
xen/arch/x86/flushtlb.c +7 -4
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);
Quoted 8 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 139

Quoted 8 lines
xen/arch/x86/flushtlb.c +8 -0
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
+                 * It is faster than read-modify-write CR4.
+                 */

Its a cr4 double write, rather than RMW. We read from a cached value anyway, not from hardware.

Quoted 10 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 141

Quoted 2 lines
xen/arch/x86/flushtlb.c +2 -0
+                invpcid_flush_all();
+            }

The reference to PCID in the comment isn't really meaningful imo. PCID and INVPCID are independent features anyway. Also please don't create artificially short comment lines.

Generally I also think such if() conditions would better be inverted:
There's no reason to make the legacy form look as if it was preferred.

Quoted 7 lines
Quoted 4 lines

And then - what about the use in write_cr3() and the two uses that remain after my XPTI follow-up series (which sadly looks to be stuck for whatever reason), or (without that series) the write_cr3 assembler macro?

I don't think it is safe to use invpcid when we're also switching cr3. The new cr3 may have global pages with different translations, as they are guest controlled.

Can you elaborate a little bit more?

Quoted 7 lines

How can a guest control any hypervisor mappings? As long as the new cr3 is being loaded before the TLB is flushed via INVPCID I can't see how a problem should occur.

In fact my series does exactly what Jan is asking above: it is replacing the remaining cr4 based TLB flushing by INVPCID if possible. So in case there is a flaw in my design please tell me.

At the moment, we have guest and hypervisor controlled global mappings.

The current switch is:
cr4 &= ~PGE;
cr3 = new_cr3;
cr4 |= PGE;

which means that all global mappings are flushed by the first action, and no new global mappings can come into existence. We then switch to the new cr3 (again with global fully disabled), then allow global mappings to come back into existence.

Quoted 8 lines

With the invpcid route, we switch via:

cr3 = new_cr3;
invpcid all+global;

This has a race window where global mappings are active, and could mismatch what is in cr3. This yields #MC on at least some hardware, and is specified to have undefined behaviour.

Oh, right, this would be okay only without what used to be named USER_MAPPINGS_ARE_GLOBAL (and what is now implied).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw

Re: [PATCH 1/2] x86: report if PCID and INVPCID are supported

In reply to Juergen Gross
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 13:49, <[email protected]> wrote:
Quoted 24 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 12:43, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/setup.c +0 -0
@@ -1701,6 +1701,13 @@ void __init noreturn __start_xen(unsigned long mbi_p)
            cpu_has_nx ? XENLOG_INFO : XENLOG_WARNING "Warning: ",
            cpu_has_nx ? "" : "not ");
 
Quoted 10 lines

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1709

Quoted 6 lines
xen/arch/x86/setup.c +6 -0
+
+    printk(XENLOG_INFO
+           "PCID (Process-Context IDentifier) %ssupported\n",
+           cpu_has_pcid ? "" : "not ");
+
+    printk(XENLOG_INFO "INVPCID %ssupported\n", cpu_has_invpcid ? "" : "not ");

Do we really need this? We log a message for NX as an exception, we don't do so for other features (and things would get pretty unwieldy if we did).

I'd rather keep this message. As we are hiding PCID and INPCID from dom0 this is the only indicator of those features being supported. In case of an error related to TLB consistency this information is important IMO.

Quoted 6 lines
Quoted 2 lines

Well, this argument would hold for almost every other advanced feature we use.

Then I'd suggest to either add individual messages for those features or to have a single message (or a few) to list all features like the flags in /proc/cpuinfo. This list could even be part of the "xl info" output.

I have no idea whether there are plans to make this part of "xl info" output; xen-cpuid certainly can tell you.

Wei, btw - for the case of there being problems with this new use of INVPCID, perhaps you will also want to extend parse_xen_cpuid() so that the use can be disabled from the command line.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#20 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to Jan Beulich
Quoted 78 lines · Jan Beulich
Quoted 78 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 13:35, <[email protected]> wrote:
Quoted 9 lines · Juergen Gross
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:

No description at all? I'd at least expect mention of how much of a performance win this is (for whichever hardware you happen to know that).

Review unsigned int flush_area_local(const void *va, unsigned

Quoted 1 line
(quoted diff) +0 -0
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned 

int flags)

Quoted 45 lines
Quoted 36 lines
Quoted 15 lines
Quoted 15 lines
xen/arch/x86/flushtlb.c +7 -4
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);
Quoted 8 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 139

Quoted 8 lines
xen/arch/x86/flushtlb.c +8 -0
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
+                 * It is faster than read-modify-write CR4.
+                 */

Its a cr4 double write, rather than RMW. We read from a cached value anyway, not from hardware.

Quoted 10 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 141

Quoted 2 lines
xen/arch/x86/flushtlb.c +2 -0
+                invpcid_flush_all();
+            }

The reference to PCID in the comment isn't really meaningful imo. PCID and INVPCID are independent features anyway. Also please don't create artificially short comment lines.

Generally I also think such if() conditions would better be inverted:
There's no reason to make the legacy form look as if it was preferred.

Quoted 7 lines
Quoted 4 lines

And then - what about the use in write_cr3() and the two uses that remain after my XPTI follow-up series (which sadly looks to be stuck for whatever reason), or (without that series) the write_cr3 assembler macro?

I don't think it is safe to use invpcid when we're also switching cr3. The new cr3 may have global pages with different translations, as they are guest controlled.

Can you elaborate a little bit more?

Quoted 7 lines

How can a guest control any hypervisor mappings? As long as the new cr3 is being loaded before the TLB is flushed via INVPCID I can't see how a problem should occur.

In fact my series does exactly what Jan is asking above: it is replacing the remaining cr4 based TLB flushing by INVPCID if possible. So in case there is a flaw in my design please tell me.

At the moment, we have guest and hypervisor controlled global mappings.

The current switch is:
cr4 &= ~PGE;
cr3 = new_cr3;
cr4 |= PGE;

which means that all global mappings are flushed by the first action, and no new global mappings can come into existence. We then switch to the new cr3 (again with global fully disabled), then allow global mappings to come back into existence.

With the invpcid route, we switch via:

Quoted 8 lines
Quoted 6 lines

cr3 = new_cr3;
invpcid all+global;

This has a race window where global mappings are active, and could mismatch what is in cr3. This yields #MC on at least some hardware, and is specified to have undefined behaviour.

Oh, right, this would be okay only without what used to be named USER_MAPPINGS_ARE_GLOBAL (and what is now implied).

When we start using PCID for user mappings, then we don't need them to be global, at which point we can require/expect that the only global mappings are hypervisor ones which we expect to remain correct across a write to cr3.  However, if we do this, then we need to use a bit other than PAGE_GLOBAL to signify guest user mappings.

I think this is doable, but I don't think it is going to be trivial to get correct.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#21 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

Quoted 88 lines · Andrew Cooper
Quoted 78 lines · Jan Beulich
Quoted 78 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 13:35, <[email protected]> wrote:
Quoted 9 lines · Juergen Gross
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:

No description at all? I'd at least expect mention of how much of a performance win this is (for whichever hardware you happen to know that).

Review unsigned int flush_area_local(const void *va, unsigned

Quoted 1 line
(quoted diff) +0 -0
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned 

int flags)

Quoted 45 lines
Quoted 36 lines
Quoted 15 lines
Quoted 15 lines
(quoted diff) +7 -4
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);
Quoted 8 lines

Review unsigned int flush_area_local(const void *va, unsigned · line 139

Quoted 8 lines
(quoted diff) +8 -0
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
+                 * It is faster than read-modify-write CR4.
+                 */

Its a cr4 double write, rather than RMW. We read from a cached value anyway, not from hardware.

Quoted 10 lines

Review unsigned int flush_area_local(const void *va, unsigned · line 141

Quoted 2 lines
(quoted diff) +2 -0
+                invpcid_flush_all();
+            }

The reference to PCID in the comment isn't really meaningful imo. PCID and INVPCID are independent features anyway. Also please don't create artificially short comment lines.

Generally I also think such if() conditions would better be inverted:
There's no reason to make the legacy form look as if it was preferred.

Quoted 7 lines
Quoted 4 lines

And then - what about the use in write_cr3() and the two uses that remain after my XPTI follow-up series (which sadly looks to be stuck for whatever reason), or (without that series) the write_cr3 assembler macro?

I don't think it is safe to use invpcid when we're also switching cr3. The new cr3 may have global pages with different translations, as they are guest controlled.

Can you elaborate a little bit more?

Quoted 7 lines

How can a guest control any hypervisor mappings? As long as the new cr3 is being loaded before the TLB is flushed via INVPCID I can't see how a problem should occur.

In fact my series does exactly what Jan is asking above: it is replacing the remaining cr4 based TLB flushing by INVPCID if possible. So in case there is a flaw in my design please tell me.

At the moment, we have guest and hypervisor controlled global mappings.

The current switch is:
cr4 &= ~PGE;
cr3 = new_cr3;
cr4 |= PGE;

which means that all global mappings are flushed by the first action, and no new global mappings can come into existence. We then switch to the new cr3 (again with global fully disabled), then allow global mappings to come back into existence.

With the invpcid route, we switch via:

Quoted 8 lines
Quoted 6 lines

cr3 = new_cr3;
invpcid all+global;

This has a race window where global mappings are active, and could mismatch what is in cr3. This yields #MC on at least some hardware, and is specified to have undefined behaviour.

Oh, right, this would be okay only without what used to be named USER_MAPPINGS_ARE_GLOBAL (and what is now implied).

Quoted 8 lines

When we start using PCID for user mappings, then we don't need them to be global, at which point we can require/expect that the only global mappings are hypervisor ones which we expect to remain correct across a write to cr3.  However, if we do this, then we need to use a bit other than PAGE_GLOBAL to signify guest user mappings.

I think this is doable, but I don't think it is going to be trivial to get correct.

Why would we want to keep any global mappings at all? What are they good for? Today the only case I could find where they make sense at all is for 64-bit pv-guests to keep hypervisor mappings in the TLB when the guest is switching between user and kernel mode. In all other cases the complete TLB is flushed when cr3 is loaded with a new value (either due to a Xen context switch or a guest cr3 load being performed by Xen).

With using PCID we can just keep cr4.pge being 0 and everything is fine. No additional work required, we can just keep the G bit in the PTEs as a flag which doesn't affect the TLB at all.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#22 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 14:11, <[email protected]> wrote:
Quoted 6 lines · Andrew Cooper

When we start using PCID for user mappings, then we don't need them to be global, at which point we can require/expect that the only global mappings are hypervisor ones which we expect to remain correct across a write to cr3. However, if we do this, then we need to use a bit other than PAGE_GLOBAL to signify guest user mappings.

Quoted 7 lines
Quoted 2 lines

I think this is doable, but I don't think it is going to be trivial to get correct.

Why would we want to keep any global mappings at all? What are they good for? Today the only case I could find where they make sense at all is for 64-bit pv-guests to keep hypervisor mappings in the TLB when the guest is switching between user and kernel mode.

Hypervisor and guest user mappings, that is.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#12 lore raw

Re: [PATCH 1/2] x86: report if PCID and INVPCID are supported

In reply to Jan Beulich
Quoted 33 lines · Jan Beulich
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 13:49, <[email protected]> wrote:
Quoted 24 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 12:43, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/setup.c +0 -0
@@ -1701,6 +1701,13 @@ void __init noreturn __start_xen(unsigned long mbi_p)
            cpu_has_nx ? XENLOG_INFO : XENLOG_WARNING "Warning: ",
            cpu_has_nx ? "" : "not ");
 
Quoted 10 lines

xen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 1709

Quoted 6 lines
xen/arch/x86/setup.c +6 -0
+
+    printk(XENLOG_INFO
+           "PCID (Process-Context IDentifier) %ssupported\n",
+           cpu_has_pcid ? "" : "not ");
+
+    printk(XENLOG_INFO "INVPCID %ssupported\n", cpu_has_invpcid ? "" : "not ");

Do we really need this? We log a message for NX as an exception, we don't do so for other features (and things would get pretty unwieldy if we did).

I'd rather keep this message. As we are hiding PCID and INPCID from dom0 this is the only indicator of those features being supported. In case of an error related to TLB consistency this information is important IMO.

Quoted 6 lines
Quoted 2 lines

Well, this argument would hold for almost every other advanced feature we use.

Then I'd suggest to either add individual messages for those features or to have a single message (or a few) to list all features like the flags in /proc/cpuinfo. This list could even be part of the "xl info" output.

Quoted 7 lines

I have no idea whether there are plans to make this part of "xl info" output; xen-cpuid certainly can tell you.

Wei, btw - for the case of there being problems with this new use of INVPCID, perhaps you will also want to extend parse_xen_cpuid() so that the use can be disabled from the command line.

Noted.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

author
#23 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 14:11, <[email protected]> wrote:
Quoted 8 lines · Andrew Cooper

When we start using PCID for user mappings, then we don't need them to be global, at which point we can require/expect that the only global mappings are hypervisor ones which we expect to remain correct across a write to cr3. However, if we do this, then we need to use a bit other than PAGE_GLOBAL to signify guest user mappings.

I think this is doable, but I don't think it is going to be trivial to get correct.

Quoted 7 lines
Quoted 4 lines

Why would we want to keep any global mappings at all? What are they good for? Today the only case I could find where they make sense at all is for 64-bit pv-guests to keep hypervisor mappings in the TLB when the guest is switching between user and kernel mode.

Hypervisor and guest user mappings, that is.

I'm not sure I understand the rationale behind global guest mappings. Is it to keep guest user mappings when switching to guest kernel mode?

(A normal kernel like Linux makes global kernel mappings, but obv. we can't do that in 64bit PV guests because kernel is also in ring3.)

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#24 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

Quoted 11 lines · Wei Liu
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 14:11, <[email protected]> wrote:
Quoted 8 lines · Andrew Cooper

When we start using PCID for user mappings, then we don't need them to be global, at which point we can require/expect that the only global mappings are hypervisor ones which we expect to remain correct across a write to cr3. However, if we do this, then we need to use a bit other than PAGE_GLOBAL to signify guest user mappings.

I think this is doable, but I don't think it is going to be trivial to get correct.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Why would we want to keep any global mappings at all? What are they good for? Today the only case I could find where they make sense at all is for 64-bit pv-guests to keep hypervisor mappings in the TLB when the guest is switching between user and kernel mode.

Hypervisor and guest user mappings, that is.

I'm not sure I understand the rationale behind global guest mappings. Is it to keep guest user mappings when switching to guest kernel mode?

Yes - it is a performance optimisation so on guest user => guest kernel context switch, guest user mappings are still present in the TLB.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#25 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to Wei Liu
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 14:31, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 14:11, <[email protected]> wrote:
Quoted 8 lines · Andrew Cooper

When we start using PCID for user mappings, then we don't need them to be global, at which point we can require/expect that the only global mappings are hypervisor ones which we expect to remain correct across a write to cr3. However, if we do this, then we need to use a bit other than PAGE_GLOBAL to signify guest user mappings.

I think this is doable, but I don't think it is going to be trivial to get correct.

Why would we want to keep any global mappings at all? What are they good for? Today the only case I could find where they make sense at all is for 64-bit pv-guests to keep hypervisor mappings in the TLB when the guest is switching between user and kernel mode.

Quoted 5 lines
Quoted 2 lines

Hypervisor and guest user mappings, that is.

I'm not sure I understand the rationale behind global guest mappings. Is it to keep guest user mappings when switching to guest kernel mode?

Yes, exactly (and especially when - like for some syscalls - there's a fast user->kernel->user round trip). The goal is to avoid at least some of the TLB reloads, when we already can't have global guest kernel mappings.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#26 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to Andrew Cooper
Quoted 75 lines · Andrew Cooper
Quoted 54 lines · Juergen Gross
Quoted 44 lines · Andrew Cooper
Quoted 22 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:

No description at all? I'd at least expect mention of how much of a performance win this is (for whichever hardware you happen to know that).

Quoted 16 lines
xen/arch/x86/flushtlb.c +7 -4
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);
Quoted 8 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 139

Quoted 8 lines
xen/arch/x86/flushtlb.c +8 -0
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
+                 * It is faster than read-modify-write CR4.
+                 */

Its a cr4 double write, rather than RMW.  We read from a cached value anyway, not from hardware.

Quoted 10 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 141

Quoted 2 lines
xen/arch/x86/flushtlb.c +2 -0
+                invpcid_flush_all();
+            }

The reference to PCID in the comment isn't really meaningful imo. PCID and INVPCID are independent features anyway. Also please don't create artificially short comment lines.

Generally I also think such if() conditions would better be inverted:
There's no reason to make the legacy form look as if it was preferred.

Quoted 7 lines
Quoted 4 lines

And then - what about the use in write_cr3() and the two uses that remain after my XPTI follow-up series (which sadly looks to be stuck for whatever reason), or (without that series) the write_cr3 assembler macro?

I don't think it is safe to use invpcid when we're also switching cr3. The new cr3 may have global pages with different translations, as they are guest controlled.

Can you elaborate a little bit more?

Quoted 7 lines

How can a guest control any hypervisor mappings? As long as the new cr3 is being loaded before the TLB is flushed via INVPCID I can't see how a problem should occur.

In fact my series does exactly what Jan is asking above: it is replacing the remaining cr4 based TLB flushing by INVPCID if possible. So in case there is a flaw in my design please tell me.

At the moment, we have guest and hypervisor controlled global mappings.

The current switch is:
cr4 &= ~PGE;
cr3 = new_cr3;
cr4 |= PGE;

which means that all global mappings are flushed by the first action, and no new global mappings can come into existence.  We then switch to the new cr3 (again with global fully disabled), then allow global mappings to come back into existence.

Quoted 8 lines

With the invpcid route, we switch via:

cr3 = new_cr3;
invpcid all+global;

This has a race window where global mappings are active, and could mismatch what is in cr3.  This yields #MC on at least some hardware, and is specified to have undefined behaviour.

Okay, so I will modify my patch series to use INVPCID only when global mappings are disabled. With PCID available I'll use it for the non-XPTI case, too. This will enable us to disable global mappings completely, so INVPCID can always be used for flushing the TLB.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#27 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to Wei Liu
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/flushtlb.c +7 -4
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 141

Quoted 10 lines
xen/arch/x86/flushtlb.c +10 -0
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
+                 * It is faster than read-modify-write CR4.
+                 */
+                invpcid_flush_all();
+            }

As just validly indicated by Jürgen, this is where my comment I gave to one of his patches actually belongs: This is correct for FLUSH_TLB_GLOBAL, but goes too far for FLUSH_TLB.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#29 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to a message not in the archive
Quoted 23 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/flushtlb.c +13 -4
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
Quoted 8 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 141

Quoted 4 lines
xen/arch/x86/flushtlb.c +4 -0
+                 * It is faster than read-modify-write CR4.
+                 */
+                invpcid_flush_all();
+            }

As just validly indicated by Jürgen, this is where my comment I gave to one of his patches actually belongs: This is correct for FLUSH_TLB_GLOBAL, but goes too far for FLUSH_TLB.

And again it was so even before this patch.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#30 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

Quoted 34 lines
Quoted 1 line
Quoted 1 line

Juergen Gross <[email protected]> 03/09/18 7:05 PM >>>

Quoted 23 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/flushtlb.c +13 -4
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
Quoted 8 lines

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 141

Quoted 4 lines
xen/arch/x86/flushtlb.c +4 -0
+                 * It is faster than read-modify-write CR4.
+                 */
+                invpcid_flush_all();
+            }

As just validly indicated by Jürgen, this is where my comment I gave to one of his patches actually belongs: This is correct for FLUSH_TLB_GLOBAL, but goes too far for FLUSH_TLB.

Quoted 1 line

And again it was so even before this patch.

Not exactly - "before this patch" should include the state things were in before 32-bit code got removed. And that's where we had a proper separation between flushes including and excluding global entries. And now that we regain that ability, we should leverage it.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#31 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

Quoted 34 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line

Juergen Gross <[email protected]> 03/09/18 7:05 PM >>>

Quoted 31 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 05.03.18 at 10:50, <[email protected]> wrote:
xen/arch/x86/flushtlb.c +7 -4
@@ -120,11 +121,24 @@ unsigned int flush_area_local(const void *va, unsigned int flags)
         else
         {
             u32 t = pre_flush();
-            unsigned long cr4 = read_cr4();
 
-            write_cr4(cr4 & ~X86_CR4_PGE);
-            barrier();
-            write_cr4(cr4);
+            if ( !cpu_has_invpcid )
+            {
+                unsigned long cr4 = read_cr4();
+
+                write_cr4(cr4 & ~X86_CR4_PGE);
+                barrier();
+                write_cr4(cr4);

xen/arch/x86/flushtlb.c unsigned int flush_area_local(const void *va, unsigned int flags) · line 141

Quoted 10 lines
xen/arch/x86/flushtlb.c +10 -0
+            }
+            else
+            {
+                /*
+                 * Using invpcid to flush all mappings works
+                 * regardless of whether PCID is enabled or not.
+                 * It is faster than read-modify-write CR4.
+                 */
+                invpcid_flush_all();
+            }

As just validly indicated by Jürgen, this is where my comment I gave to one of his patches actually belongs: This is correct for FLUSH_TLB_GLOBAL, but goes too far for FLUSH_TLB.

Quoted 6 lines
Quoted 1 line

And again it was so even before this patch.

Not exactly - "before this patch" should include the state things were in before 32-bit code got removed. And that's where we had a proper separation between flushes including and excluding global entries. And now that we regain that ability, we should leverage it.

Already working on it in my XPTI speed-up series.

BTW: are you already working on rebasing your XPTI speed up series to current staging? I'd like my series to use your series as a base unless you are telling me you won't be able to resend your series soon.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#32 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

Quoted 4 lines
Quoted 1 line
Quoted 1 line

Juergen Gross <[email protected]> 03/12/18 2:10 PM >>>

BTW: are you already working on rebasing your XPTI speed up series to current staging? I'd like my series to use your series as a base unless you are telling me you won't be able to resend your series soon.

I hope to be able to get to this later this week.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#34 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to a message not in the archive
Quoted 6 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line

Juergen Gross <[email protected]> 03/12/18 2:10 PM >>>

BTW: are you already working on rebasing your XPTI speed up series to current staging? I'd like my series to use your series as a base unless you are telling me you won't be able to resend your series soon.

I hope to be able to get to this later this week.

Okay, thanks.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#35 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to Juergen Gross
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 12.03.18 at 14:10, <[email protected]> wrote:

BTW: are you already working on rebasing your XPTI speed up series to current staging? I'd like my series to use your series as a base unless you are telling me you won't be able to resend your series soon.

Coming back to my reply here yesterday - then again I'm a little reluctant to send out a new version that has no changes other than being re-based, when there were no comments by Andrew on most of the remaining patches yet (and in the one case where there were comments, I'm afraid I continue to disagree, but I'll see about moving that patch last in the series).

Plus staging is broken right now anyway (but of course we know now which patch to revert if need be).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#37 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to a message not in the archive
Quoted 11 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 12.03.18 at 14:10, <[email protected]> wrote:

BTW: are you already working on rebasing your XPTI speed up series to current staging? I'd like my series to use your series as a base unless you are telling me you won't be able to resend your series soon.

Coming back to my reply here yesterday - then again I'm a little reluctant to send out a new version that has no changes other than being re-based, when there were no comments by Andrew on most of the remaining patches yet (and in the one case where there were comments, I'm afraid I continue to disagree, but I'll see about moving that patch last in the series).

In order to move forward, would you prefer me to base my patches on current staging and put your patches on top of my series later? I just don't want to rebase my series on a moving target...

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#38 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 13.03.18 at 10:27, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 12.03.18 at 14:10, <[email protected]> wrote:

BTW: are you already working on rebasing your XPTI speed up series to current staging? I'd like my series to use your series as a base unless you are telling me you won't be able to resend your series soon.

Coming back to my reply here yesterday - then again I'm a little reluctant to send out a new version that has no changes other than being re-based, when there were no comments by Andrew on most of the remaining patches yet (and in the one case where there were comments, I'm afraid I continue to disagree, but I'll see about moving that patch last in the series).

Quoted 3 lines

In order to move forward, would you prefer me to base my patches on current staging and put your patches on top of my series later? I just don't want to rebase my series on a moving target...

Well, I'm similarly not intending to re-base onto a series still under development / review. That's really the bad thing with deadlines:
If we didn't freeze the tree at a given time, but at the point where previously agreed features and other non-bug-fix changes have landed, we wouldn't have such an ordering problem right now (or to be precise the ordering issue would still be there, but neither of us would be at risk of their changes not making it).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#40 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

In reply to a message not in the archive
Quoted 18 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 13.03.18 at 10:27, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 12.03.18 at 14:10, <[email protected]> wrote:

BTW: are you already working on rebasing your XPTI speed up series to current staging? I'd like my series to use your series as a base unless you are telling me you won't be able to resend your series soon.

Coming back to my reply here yesterday - then again I'm a little reluctant to send out a new version that has no changes other than being re-based, when there were no comments by Andrew on most of the remaining patches yet (and in the one case where there were comments, I'm afraid I continue to disagree, but I'll see about moving that patch last in the series).

Quoted 3 lines

In order to move forward, would you prefer me to base my patches on current staging and put your patches on top of my series later? I just don't want to rebase my series on a moving target...

Quoted 7 lines

Well, I'm similarly not intending to re-base onto a series still under development / review. That's really the bad thing with deadlines:
If we didn't freeze the tree at a given time, but at the point where previously agreed features and other non-bug-fix changes have landed, we wouldn't have such an ordering problem right now (or to be precise the ordering issue would still be there, but neither of us would be at risk of their changes not making it).

Understandable.

Could you then please repost at least patch 3? It has been approved by Andrew and just needs the formal R-b: after rebasing.

Andrew, do you think you can find some time commenting on Jan's other patches of his Meltdown series?

Speaking with my release manager hat on I'd _really_ like to have all Meltdown/Spectre related patches minimizing the performance penalties in 4.11.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#41 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 13.03.18 at 10:48, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 13.03.18 at 10:27, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 12.03.18 at 14:10, <[email protected]> wrote:

BTW: are you already working on rebasing your XPTI speed up series to current staging? I'd like my series to use your series as a base unless you are telling me you won't be able to resend your series soon.

Coming back to my reply here yesterday - then again I'm a little reluctant to send out a new version that has no changes other than being re-based, when there were no comments by Andrew on most of the remaining patches yet (and in the one case where there were comments, I'm afraid I continue to disagree, but I'll see about moving that patch last in the series).

Quoted 3 lines

In order to move forward, would you prefer me to base my patches on current staging and put your patches on top of my series later? I just don't want to rebase my series on a moving target...

Quoted 7 lines

Well, I'm similarly not intending to re-base onto a series still under development / review. That's really the bad thing with deadlines:
If we didn't freeze the tree at a given time, but at the point where previously agreed features and other non-bug-fix changes have landed, we wouldn't have such an ordering problem right now (or to be precise the ordering issue would still be there, but neither of us would be at risk of their changes not making it).

Quoted 4 lines

Understandable.

Could you then please repost at least patch 3? It has been approved by Andrew and just needs the formal R-b: after rebasing.

Well, the thing with that patch is that from what I've been able to tell so far its re-basing will consist of dropping it, moving its sole remaining hunk (the altinstruction_nop assembler macro) into what has been patch 4. None of the uses of the macro in patch 3 should be needed anymore after Andrew's changes. Or actually I think the macro won't be needed as a standalone one anymore at all, as there's only a single place where it's used, and hence it would likely better be folded into there (the ALTERNATIVE_NOP one).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#42 lore raw

Re: [PATCH 2/2] x86: use invpcid to do global flushing

Quoted 28 lines · Jan Beulich
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 13.03.18 at 10:48, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 13.03.18 at 10:27, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 12.03.18 at 14:10, <[email protected]> wrote:

BTW: are you already working on rebasing your XPTI speed up series to current staging? I'd like my series to use your series as a base unless you are telling me you won't be able to resend your series soon.

Coming back to my reply here yesterday - then again I'm a little reluctant to send out a new version that has no changes other than being re-based, when there were no comments by Andrew on most of the remaining patches yet (and in the one case where there were comments, I'm afraid I continue to disagree, but I'll see about moving that patch last in the series).

Quoted 3 lines

In order to move forward, would you prefer me to base my patches on current staging and put your patches on top of my series later? I just don't want to rebase my series on a moving target...

Quoted 7 lines

Well, I'm similarly not intending to re-base onto a series still under development / review. That's really the bad thing with deadlines:
If we didn't freeze the tree at a given time, but at the point where previously agreed features and other non-bug-fix changes have landed, we wouldn't have such an ordering problem right now (or to be precise the ordering issue would still be there, but neither of us would be at risk of their changes not making it).

Quoted 4 lines

Understandable.

Could you then please repost at least patch 3? It has been approved by Andrew and just needs the formal R-b: after rebasing.

Quoted 9 lines

Well, the thing with that patch is that from what I've been able to tell so far its re-basing will consist of dropping it, moving its sole remaining hunk (the altinstruction_nop assembler macro) into what has been patch 4. None of the uses of the macro in patch 3 should be needed anymore after Andrew's changes. Or actually I think the macro won't be needed as a standalone one anymore at all, as there's only a single place where it's used, and hence it would likely better be folded into there (the ALTERNATIVE_NOP one).

I'm sorry - I realise I'm very behind on reviews atm.  If you've got the series rebased, then it will be easier to review this version than the old version.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel