Skip to contentSkip to conversation

x86: Add support for STAC/CLAC instructions

PATCH In review Feng Wu 4 participants 13 messages Apr 15, 2014 – Apr 23, 2014

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

PATCH 1/6 x86: Add support for STAC/CLAC instructions

The STAC/CLAC instructions are only available when SMAP is enabled, but on the other hand they aren't needed if SMAP is not available, or before we start to run userspace, in that case, the functions and macros do nothing.

---
 xen/arch/x86/x86_64/asm-offsets.c      |  1 +
 xen/include/asm-x86/x86_64/asm_defns.h | 70 ++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+)
xen/arch/x86/x86_64/asm-offsets.c +1 -0
index b0098b3..fa4cbb6 100644
@@ -160,6 +160,7 @@ void __dummy__(void)
     BLANK();
 
     OFFSET(CPUINFO86_ext_features, struct cpuinfo_x86, x86_capability[1]);
+    OFFSET(CPUINFO86_leaf7_features, struct cpuinfo_x86, x86_capability[7]);
     BLANK();
 
     OFFSET(MB_flags, multiboot_info_t, flags);
xen/include/asm-x86/x86_64/asm_defns.h +70 -0

74 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw

xen/include/asm-x86/x86_64/asm_defns.h

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 15.04.14 at 15:01, <[email protected]> wrote:
xen/include/asm-x86/x86_64/asm_defns.h +0 -0

These changes should go into xen/include/asm-x86/asm_defns.h; the x86-64 one should get merged into the previously common one sooner or later, so let's not force that merging patch to be bigger than it needs to be.

Quoted 4 lines
xen/include/asm-x86/x86_64/asm_defns.h +0 -0
@@ -228,4 +228,74 @@ __asm__(                                        \
 # define _ASM_EX(p) #p "-."
 #endif
 

xen/include/asm-x86/x86_64/asm_defns.h __asm__( \ · line 240

Quoted 10 lines
xen/include/asm-x86/x86_64/asm_defns.h +10 -0
+/* "Raw" instruction opcodes */
+#define __ASM_CLAC      .byte 0x0f,0x01,0xca
+#define __ASM_STAC      .byte 0x0f,0x01,0xcb
+
+/* Indirect stringification.  Doing two levels allows the parameter to be a
+ * macro itself.  For example, compile with -DFOO=bar, __stringify(FOO)
+ * converts to "bar".
+ */
+#define __stringify_1(x...)     #x
+#define __stringify(x...)       __stringify_1(x)

Please use xen/stringify.h rather than repeating its defintions.

xen/include/asm-x86/x86_64/asm_defns.h __asm__( \ · line 243

Quoted 3 lines
xen/include/asm-x86/x86_64/asm_defns.h +3 -0
+
+#ifdef __ASSEMBLY__
+#define X86_FEATURE_SMAP    (7*32+20)

No - how would be spot this if we were to re-arrange the feature array? Either use (or make usable) the definitions in cpufeature.h, or propagate the necessary values through asm-offsets.h.

Quoted 11 lines
xen/include/asm-x86/x86_64/asm_defns.h +11 -0
+#define ASM_STAC                                         \
+        pushq %rax;                                      \
+        leaq boot_cpu_data(%rip),%rax;                   \
+        btl $X86_FEATURE_SMAP-7*32, CPUINFO86_leaf7_features(%rax);  \
+        jnc 881f;                                        \
+        movq %cr4,%rax;                                  \
+        testl $X86_CR4_SMAP,%eax;                        \
+        jz 881f;                                         \
+        __ASM_STAC;                                      \
+881:    popq %rax
+

xen/include/asm-x86/x86_64/asm_defns.h __asm__( \ · line 264

Quoted 10 lines
xen/include/asm-x86/x86_64/asm_defns.h +10 -0
+#define ASM_CLAC                                         \
+        pushq %rax;                                      \
+        leaq boot_cpu_data(%rip),%rax;                   \
+        btl $X86_FEATURE_SMAP-7*32, CPUINFO86_leaf7_features(%rax);  \
+        jnc 881f;                                        \
+        movq %cr4,%rax;                                  \
+        testl $X86_CR4_SMAP,%eax;                        \
+        jz 881f;                                         \
+        __ASM_CLAC;                                      \
+881:    popq %rax

The only difference between the two macros appears to be the final instruction - please define one macro with an argument, and then make the two definitions here simple wrappers around that macro.

That said, the macro contents itself is horrible too: A control register access and two conditional branches in code intended to be used in fast paths? Definitely not an option. Even the simplest possible solution - adding a global flag to be checked here - would already be questionable. Hence I think you should at least consider porting over proper instruction patching abstraction from Linux.

Quoted 14 lines
xen/include/asm-x86/x86_64/asm_defns.h +14 -0
+#else
+#define ASM_STAC                                         \
+        "\npushq %%rax\n\t"                              \
+        "leaq boot_cpu_data(%%rip),%%rax\n\t"            \
+        "btl $" __stringify(X86_FEATURE_SMAP) "-7*32,"   \
+        __stringify(CPUINFO86_leaf7_features) "(%%rax)\n\t"  \
+        "jnc 881f\n\t"                                   \
+        "movq %%cr4,%%rax\n\t"                           \
+        "testl $" __stringify(X86_CR4_SMAP) ",%%eax\n\t" \
+        "jz 881f\n\t"                                    \
+         __stringify(__ASM_STAC) "\n\t"                  \
+"881:    popq %%rax"
+
+#define ASM_CLAC                                         \

xen/include/asm-x86/x86_64/asm_defns.h __asm__( \ · line 288

Quoted 10 lines
xen/include/asm-x86/x86_64/asm_defns.h +10 -0
+        "\npushq %%rax\n\t"                              \
+        "leaq boot_cpu_data(%%rip),%%rax\n\t"            \
+        "btl $" __stringify(X86_FEATURE_SMAP) "-7*32,"   \
+        __stringify(CPUINFO86_leaf7_features) "(%%rax)\n\t"  \
+        "jnc 881f\n\t"                                   \
+        "movq %%cr4,%%rax\n\t"                           \
+        "testl $" __stringify(X86_CR4_SMAP) ",%%eax\n\t" \
+        "jz 881f\n\t"                                    \
+         __stringify(__ASM_CLAC) "\n\t"                  \
+"881:   popq %%rax"

All the same applies to these of course.

Jan

author
#2 lore raw
Quoted 73 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, April 15, 2014 4:40 PM
To: Wu, Feng
Cc: [email protected]; Dong, Eddie; Nakajima, Jun; [email protected]
Subject: Re: [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions

xen/include/asm-x86/x86_64/asm_defns.h

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 15.04.14 at 15:01, <[email protected]> wrote:
xen/include/asm-x86/x86_64/asm_defns.h +0 -0

These changes should go into xen/include/asm-x86/asm_defns.h; the x86-64 one should get merged into the previously common one sooner or later, so let's not force that merging patch to be bigger than it needs to be.

xen/include/asm-x86/x86_64/asm_defns.h

Quoted 1 line
xen/include/asm-x86/x86_64/asm_defns.h +0 -0
@@ -228,4 +228,74 @@

__asm__( \

xen/include/asm-x86/x86_64/asm_defns.h __asm__( \ · line 240

Quoted 13 lines
xen/include/asm-x86/x86_64/asm_defns.h +10 -0
 # define _ASM_EX(p) #p "-."
 #endif
 
+/* "Raw" instruction opcodes */
+#define __ASM_CLAC      .byte 0x0f,0x01,0xca
+#define __ASM_STAC      .byte 0x0f,0x01,0xcb
+
+/* Indirect stringification.  Doing two levels allows the parameter to be a
+ * macro itself.  For example, compile with -DFOO=bar, __stringify(FOO)
+ * converts to "bar".
+ */
+#define __stringify_1(x...)     #x
+#define __stringify(x...)       __stringify_1(x)

Please use xen/stringify.h rather than repeating its defintions.

xen/include/asm-x86/x86_64/asm_defns.h __asm__( \ · line 243

Quoted 3 lines
xen/include/asm-x86/x86_64/asm_defns.h +3 -0
+
+#ifdef __ASSEMBLY__
+#define X86_FEATURE_SMAP    (7*32+20)

No - how would be spot this if we were to re-arrange the feature array? Either use (or make usable) the definitions in cpufeature.h, or propagate the necessary values through asm-offsets.h.

xen/include/asm-x86/x86_64/asm_defns.h __asm__( \

Quoted 4 lines
xen/include/asm-x86/x86_64/asm_defns.h +4 -0
+#define ASM_STAC                                         \
+        pushq %rax;                                      \
+        leaq boot_cpu_data(%rip),%rax;                   \
+        btl $X86_FEATURE_SMAP-7*32, CPUINFO86_leaf7_features(%rax);

\

xen/include/asm-x86/x86_64/asm_defns.h __asm__( \

Quoted 11 lines
xen/include/asm-x86/x86_64/asm_defns.h +11 -0
+        jnc 881f;                                        \
+        movq %cr4,%rax;                                  \
+        testl $X86_CR4_SMAP,%eax;                        \
+        jz 881f;                                         \
+        __ASM_STAC;                                      \
+881:    popq %rax
+
+#define ASM_CLAC                                         \
+        pushq %rax;                                      \
+        leaq boot_cpu_data(%rip),%rax;                   \
+        btl $X86_FEATURE_SMAP-7*32, CPUINFO86_leaf7_features(%rax);

\

xen/include/asm-x86/x86_64/asm_defns.h __asm__( \ · line 264

Quoted 6 lines
xen/include/asm-x86/x86_64/asm_defns.h +6 -0
+        jnc 881f;                                        \
+        movq %cr4,%rax;                                  \
+        testl $X86_CR4_SMAP,%eax;                        \
+        jz 881f;                                         \
+        __ASM_CLAC;                                      \
+881:    popq %rax

The only difference between the two macros appears to be the final instruction - please define one macro with an argument, and then make the two definitions here simple wrappers around that macro.

Quoted 7 lines

That said, the macro contents itself is horrible too: A control register access and two conditional branches in code intended to be used in fast paths? Definitely not an option. Even the simplest possible solution - adding a global flag to be checked here - would already be questionable. Hence I think you should at least consider porting over proper instruction patching abstraction from Linux.

Jan, I did some investigation about how to handle this two instructions in Linux, basically, it uses the alternatives mechanism to handle these kind of cases. Let's take the following definition of ASM_STAC in Linux for example:

#define ASM_CLAC                                                        \
        661: ASM_NOP3 ;                                                 \
        .pushsection .altinstr_replacement, "ax" ;                      \
        662: __ASM_CLAC ;                                               \
        .popsection ;                                                   \
        .pushsection .altinstructions, "a" ;                            \
        altinstruction_entry 661b, 662b, X86_FEATURE_SMAP, 3, 3 ;       \

.popsection

ASM_CLAC is defined as NOP by default, it puts the real CLAC instruction in section "altinstr_replacement" and the needed information to " altinstructions " section, which is useful to replace the default
definition by the alternative one. Here is the routine call path: start_kernel () --> check_bugs() --> alternative_instructions().

In function alternative_instructions(), it will check the related features in CPU, if it exists, the alternative definition will overwrite the default one. So there is no conditional branches after this replacement when the Macro is being used.

Do you think we need to port this whole mechanism to Xen to support CLAC/STAC? I am not sure if it is a little overkilled.

BTW, from the Linux implementation, I think we don't need to check the 'cr4' for the macros, we just need to check whether the feature exists in the CPU. So is it acceptable to use the original code by eliminating the cr4 check? Thanks a lot in advance!

Quoted 27 lines
Quoted 14 lines
xen/include/asm-x86/x86_64/asm_defns.h +14 -0
+#else
+#define ASM_STAC                                         \
+        "\npushq %%rax\n\t"                              \
+        "leaq boot_cpu_data(%%rip),%%rax\n\t"            \
+        "btl $" __stringify(X86_FEATURE_SMAP) "-7*32,"   \
+        __stringify(CPUINFO86_leaf7_features) "(%%rax)\n\t"  \
+        "jnc 881f\n\t"                                   \
+        "movq %%cr4,%%rax\n\t"                           \
+        "testl $" __stringify(X86_CR4_SMAP) ",%%eax\n\t" \
+        "jz 881f\n\t"                                    \
+         __stringify(__ASM_STAC) "\n\t"                  \
+"881:    popq %%rax"
+
+#define ASM_CLAC                                         \

xen/include/asm-x86/x86_64/asm_defns.h __asm__( \ · line 288

Quoted 10 lines
xen/include/asm-x86/x86_64/asm_defns.h +10 -0
+        "\npushq %%rax\n\t"                              \
+        "leaq boot_cpu_data(%%rip),%%rax\n\t"            \
+        "btl $" __stringify(X86_FEATURE_SMAP) "-7*32,"   \
+        __stringify(CPUINFO86_leaf7_features) "(%%rax)\n\t"  \
+        "jnc 881f\n\t"                                   \
+        "movq %%cr4,%%rax\n\t"                           \
+        "testl $" __stringify(X86_CR4_SMAP) ",%%eax\n\t" \
+        "jz 881f\n\t"                                    \
+         __stringify(__ASM_CLAC) "\n\t"                  \
+"881:   popq %%rax"

All the same applies to these of course.

Quoted 1 line

Jan

Thanks,
Feng

#3 lore raw
Quoted 31 lines
Quoted 9 lines
Quoted 1 line · On 22.04.14 at 09:41, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
That said, the macro contents itself is horrible too: A control register access and two conditional branches in code intended to be used in fast paths? Definitely not an option. Even the simplest possible solution - adding a global flag to be checked here - would already be questionable. Hence I think you should at least consider porting over proper instruction patching abstraction from Linux.

Jan, I did some investigation about how to handle this two instructions in Linux, basically, it uses the alternatives mechanism to handle these kind of cases. Let's take the following definition of ASM_STAC in Linux for example:

#define ASM_CLAC                                                        \
        661: ASM_NOP3 ;                                                 \
        .pushsection .altinstr_replacement, "ax" ;                      \
        662: __ASM_CLAC ;                                               \
        .popsection ;                                                   \
        .pushsection .altinstructions, "a" ;                            \
        altinstruction_entry 661b, 662b, X86_FEATURE_SMAP, 3, 3 ;       \

.popsection

ASM_CLAC is defined as NOP by default, it puts the real CLAC instruction in section "altinstr_replacement" and
the needed information to " altinstructions " section, which is useful to replace the default
definition by the alternative one. Here is the routine call path:
start_kernel () --> check_bugs() --> alternative_instructions().

Quoted 7 lines

In function alternative_instructions(), it will check the related features in CPU, if it exists, the alternative definition will overwrite the default one. So there is no conditional branches after this replacement when the Macro is being used.

Do you think we need to port this whole mechanism to Xen to support CLAC/STAC? I am not sure if it is a little overkilled.

Obviously we could use this machinery for other things. But whether it's needed here depends on the alternatives.

Quoted 4 lines

BTW, from the Linux implementation, I think we don't need to check the 'cr4' for the macros, we just need
to check whether the feature exists in the CPU. So is it acceptable to use the original code by eliminating the cr4 check?

That _might_ be acceptable if you bring it down to just the three really necessary instructions: BT, JNC, CLAC/STAC. But the "might" has to stand - this, after all, remains an addition of a conditional branch (and for the performance of STAC/CLAC I haven't seen any documentation so far either) to several fast paths, and hence the patching alternative can't be discarded as the potentially better one.

Jan

author
#4 lore raw
Quoted 64 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, April 22, 2014 4:07 PM
To: Wu, Feng
Cc: [email protected]; Dong, Eddie; Nakajima, Jun; [email protected]
Subject: RE: [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions

Quoted 10 lines
Quoted 9 lines
Quoted 1 line · On 22.04.14 at 09:41, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
That said, the macro contents itself is horrible too: A control register access and two conditional branches in code intended to be used in fast paths? Definitely not an option. Even the simplest possible solution - adding a global flag to be checked here - would already be questionable. Hence I think you should at least consider porting over proper instruction patching abstraction from Linux.

Quoted 6 lines

Jan, I did some investigation about how to handle this two instructions in Linux, basically, it uses the alternatives mechanism to handle these kind of cases. Let's take the following definition of ASM_STAC in Linux for example:

#define ASM_CLAC

\

Quoted 1 line
        661: ASM_NOP3 ;

\

Quoted 1 line
        .pushsection .altinstr_replacement, "ax" ;

\

Quoted 1 line
        662: __ASM_CLAC ;

\

Quoted 1 line
        .popsection ;

\

Quoted 1 line
        .pushsection .altinstructions, "a" ;

\

Quoted 1 line
        altinstruction_entry 661b, 662b, X86_FEATURE_SMAP, 3, 3 ;

\

Quoted 9 lines

.popsection

ASM_CLAC is defined as NOP by default, it puts the real CLAC instruction in section "altinstr_replacement" and
the needed information to " altinstructions " section, which is useful to replace the default
definition by the alternative one. Here is the routine call path:
start_kernel () --> check_bugs() --> alternative_instructions().

Quoted 7 lines

In function alternative_instructions(), it will check the related features in CPU, if it exists, the alternative definition will overwrite the default one. So there is no conditional branches after this replacement when the Macro is being used.

Do you think we need to port this whole mechanism to Xen to support CLAC/STAC? I am not sure if it is a little overkilled.

Obviously we could use this machinery for other things. But whether it's needed here depends on the alternatives.

Quoted 4 lines

BTW, from the Linux implementation, I think we don't need to check the 'cr4' for the macros, we just need
to check whether the feature exists in the CPU. So is it acceptable to use the original code by eliminating the cr4 check?

Quoted 7 lines

That _might_ be acceptable if you bring it down to just the three really necessary instructions: BT, JNC, CLAC/STAC. But the "might" has to stand - this, after all, remains an addition of a conditional branch (and for the performance of STAC/CLAC I haven't seen any documentation so far either) to several fast paths, and hence the patching alternative can't be discarded as the potentially better one.

Since the alternatives mechanism in Linux is something common and independent and needs a bit more efforts to be ported to Xen, can we use the method I mentioned above at the current stage. After that I will have a fully think about how to port the alternatives mechanism Xen.

What do you think about this?

Quoted 1 line

Jan

Thanks,
Feng

#5 lore raw
Quoted 24 lines
Quoted 8 lines
Quoted 1 line · On 22.04.14 at 10:46, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 22.04.14 at 09:41, <[email protected]> wrote:

BTW, from the Linux implementation, I think we don't need to check the 'cr4' for the macros, we just need
to check whether the feature exists in the CPU. So is it acceptable to use the original code by eliminating the cr4 check?

Quoted 7 lines

That _might_ be acceptable if you bring it down to just the three really necessary instructions: BT, JNC, CLAC/STAC. But the "might" has to stand - this, after all, remains an addition of a conditional branch (and for the performance of STAC/CLAC I haven't seen any documentation so far either) to several fast paths, and hence the patching alternative can't be discarded as the potentially better one.

Since the alternatives mechanism in Linux is something common and independent and needs
a bit more efforts to be ported to Xen, can we use the method I mentioned above
at the current stage. After that I will have a fully think about how to port the
alternatives mechanism Xen.

Quoted 1 line

What do you think about this?

Generally this would seem acceptable (as long as you give at least a rough estimate on when to expect that second step), but then we have this sad experience with promises by Intel engineers to work on certain things...

Jan

author
#6 lore raw
Quoted 35 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, April 22, 2014 5:17 PM
To: Wu, Feng
Cc: [email protected]; Dong, Eddie; Nakajima, Jun; [email protected]
Subject: RE: [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions

Quoted 4 lines
Quoted 4 lines
Quoted 1 line · On 22.04.14 at 10:46, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.04.14 at 09:41, <[email protected]> wrote:

BTW, from the Linux implementation, I think we don't need to check the

'cr4'

Quoted 20 lines
Quoted 11 lines
Quoted 3 lines

for the macros, we just need
to check whether the feature exists in the CPU. So is it acceptable to use the original code by eliminating the cr4 check?

That _might_ be acceptable if you bring it down to just the three really necessary instructions: BT, JNC, CLAC/STAC. But the "might" has to stand - this, after all, remains an addition of a conditional branch (and for the performance of STAC/CLAC I haven't seen any documentation so far either) to several fast paths, and hence the patching alternative can't be discarded as the potentially better one.

Since the alternatives mechanism in Linux is something common and independent and needs
a bit more efforts to be ported to Xen, can we use the method I mentioned above
at the current stage. After that I will have a fully think about how to port the
alternatives mechanism Xen.

Quoted 7 lines
Quoted 1 line

What do you think about this?

Generally this would seem acceptable (as long as you give at least a rough estimate on when to expect that second step), but then we have this sad experience with promises by Intel engineers to work on certain things...

Thanks a lot!
I think I can work on the alternative mechanism after this SMAP patch is finished.

Quoted 1 line

Jan

Thanks,
Feng

#7 lore raw
Quoted 37 lines · Feng
Quoted 37 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, April 22, 2014 5:17 PM
To: Wu, Feng
Cc: [email protected]; Dong, Eddie; Nakajima, Jun; [email protected]
Subject: RE: [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions

Quoted 4 lines
Quoted 4 lines
Quoted 1 line · On 22.04.14 at 10:46, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.04.14 at 09:41, <[email protected]> wrote:

BTW, from the Linux implementation, I think we don't need to check the

'cr4'

Quoted 21 lines
Quoted 11 lines
Quoted 3 lines

for the macros, we just need
to check whether the feature exists in the CPU. So is it acceptable to use the original code by eliminating the cr4 check?

That _might_ be acceptable if you bring it down to just the three really necessary instructions: BT, JNC, CLAC/STAC. But the "might" has to stand - this, after all, remains an addition of a conditional branch (and for the performance of STAC/CLAC I haven't seen any documentation so far either) to several fast paths, and hence the patching alternative can't be discarded as the potentially better one.

Since the alternatives mechanism in Linux is something common and independent and needs
a bit more efforts to be ported to Xen, can we use the method I mentioned above
at the current stage. After that I will have a fully think about how to port the
alternatives mechanism Xen.

What do you think about this?

Quoted 8 lines
Quoted 5 lines

Generally this would seem acceptable (as long as you give at least a rough estimate on when to expect that second step), but then we have this sad experience with promises by Intel engineers to work on certain things...

Thanks a lot!
I think I can work on the alternative mechanism after this SMAP patch is finished.

Any time estimates when the alternative patching mechanism would be done? Asking because it seems to me that we would want that in Xen 4.5 - so need to figure out your timeline to fold it in the release time-frame.

Quoted 10 lines
Quoted 2 lines

Jan

Thanks,
Feng

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#8 lore raw
Quoted 60 lines

-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Tuesday, April 22, 2014 9:10 PM
To: Wu, Feng
Cc: Jan Beulich; Dong, Eddie; [email protected]; Nakajima, Jun; [email protected]
Subject: Re: [Xen-devel] [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions

Quoted 10 lines · Feng
Quoted 10 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, April 22, 2014 5:17 PM
To: Wu, Feng
Cc: [email protected]; Dong, Eddie; Nakajima, Jun; [email protected]
Subject: RE: [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line · On 22.04.14 at 10:46, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.04.14 at 09:41, <[email protected]> wrote:

BTW, from the Linux implementation, I think we don't need to check

the

Quoted 3 lines
Quoted 3 lines

'cr4'

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

for the macros, we just need
to check whether the feature exists in the CPU. So is it acceptable to

use

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 1 line

the original code by eliminating the cr4 check?

That _might_ be acceptable if you bring it down to just the three really necessary instructions: BT, JNC, CLAC/STAC. But the "might" has to stand - this, after all, remains an addition of a conditional branch (and for the performance of STAC/CLAC I haven't seen any documentation so far either) to several fast paths, and hence the patching alternative can't be discarded as the potentially better one.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Since the alternatives mechanism in Linux is something common and independent and needs
a bit more efforts to be ported to Xen, can we use the method I

mentioned

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

above
at the current stage. After that I will have a fully think about how to port the
alternatives mechanism Xen.

What do you think about this?

Quoted 8 lines
Quoted 5 lines

Generally this would seem acceptable (as long as you give at least a rough estimate on when to expect that second step), but then we have this sad experience with promises by Intel engineers to work on certain things...

Thanks a lot!
I think I can work on the alternative mechanism after this SMAP patch is

Quoted 7 lines

finished.

Any time estimates when the alternative patching mechanism would be done? Asking
because it seems to me that we would want that in Xen 4.5 - so need to figure out
your timeline to fold it in the release time-frame.

I am sorry I feel it is a little hard for me to say when the patch will be done, since I am not quite clear about how big the effort would be right now. But I can start porting it ASAP after SMAP is done. BTW, do you know when will Xen 4.5 be released? About 4~5 months later?

Quoted 11 lines
Quoted 11 lines
Quoted 3 lines

Jan

Thanks,
Feng

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Thanks,
Feng

#9 lore raw

Is: alternative_asm as dependency for STAC/CLAC/new features? Was:Re: [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions

Quoted 68 lines · Feng
Quoted 60 lines

-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Tuesday, April 22, 2014 9:10 PM
To: Wu, Feng
Cc: Jan Beulich; Dong, Eddie; [email protected]; Nakajima, Jun; [email protected]
Subject: Re: [Xen-devel] [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions

Quoted 10 lines · Feng
Quoted 10 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, April 22, 2014 5:17 PM
To: Wu, Feng
Cc: [email protected]; Dong, Eddie; Nakajima, Jun; [email protected]
Subject: RE: [PATCH v1 1/6] x86: Add support for STAC/CLAC instructions

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line · On 22.04.14 at 10:46, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.04.14 at 09:41, <[email protected]> wrote:

BTW, from the Linux implementation, I think we don't need to check

the

Quoted 3 lines
Quoted 3 lines

'cr4'

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

for the macros, we just need
to check whether the feature exists in the CPU. So is it acceptable to

use

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 1 line

the original code by eliminating the cr4 check?

That _might_ be acceptable if you bring it down to just the three really necessary instructions: BT, JNC, CLAC/STAC. But the "might" has to stand - this, after all, remains an addition of a conditional branch (and for the performance of STAC/CLAC I haven't seen any documentation so far either) to several fast paths, and hence the patching alternative can't be discarded as the potentially better one.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Since the alternatives mechanism in Linux is something common and independent and needs
a bit more efforts to be ported to Xen, can we use the method I

mentioned

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

above
at the current stage. After that I will have a fully think about how to port the
alternatives mechanism Xen.

What do you think about this?

Quoted 8 lines
Quoted 5 lines

Generally this would seem acceptable (as long as you give at least a rough estimate on when to expect that second step), but then we have this sad experience with promises by Intel engineers to work on certain things...

Thanks a lot!
I think I can work on the alternative mechanism after this SMAP patch is

Quoted 7 lines

finished.

Any time estimates when the alternative patching mechanism would be done? Asking
because it seems to me that we would want that in Xen 4.5 - so need to figure out
your timeline to fold it in the release time-frame.

Quoted 3 lines

I am sorry I feel it is a little hard for me to say when the patch will be done, since I am not quite clear about how big the effort would be right now. But I can start porting it ASAP after SMAP is done. BTW, do you know when will Xen 4.5 be released? About 4~5 months later?

The 4.5 roadmap is not yet clear. I hope that at the Xen Hackathon it will be discussed by the release manager.

I think that without this runtime patching the hypervisor will suffer a performance penalty on hosts that don't support this.

I say *think* becuase I don't have any hard numbers. And as such this argument might be completly wrong if testing shows otherwise (say, running this on AMD hardware with and without these patches).

Bearing that in mind if I this patching is not done by the time Xen 4.5 hits feature freeze window it might be neccessary to:
- #ifdef out the code so that it only is compiled in for folks who really want this and are OK with the potential performance setback.
- But this #ifdef is a maintaince nightmare - code often bitrots, another QA matrix row, etc.
In which case reverting the code is a better option and then it can be targetted for Xen 4.6

Perhaps reorganizing your deliverables would be better. As in, focus on getting alternative_asm first in, and _then_ on this feature.

Aka, alternative_asm is a dependency and this work requires that in addition.

Or all of this pointless and testing on AMD/Intel hardware with or without these patches (and with / without the CPU feature) shows no performance degradation.

#10 lore raw

Is: alternative_asm as dependency for STAC/CLAC/new features?

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 23.04.14 at 16:52, <[email protected]> wrote:

I think that without this runtime patching the hypervisor will suffer a performance penalty on hosts that don't support this.

Host that do support this would suffer too - a conditional branch doesn't come for free even if not taken.

Also I don't think there's a question whether performance would be affected, but only the one by how much (or little).

Jan

#11 lore raw
In reply to Jan Beulich
Quoted 44 lines · Jan Beulich
Quoted 30 lines
Quoted 9 lines
Quoted 1 line · On 22.04.14 at 09:41, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
That said, the macro contents itself is horrible too: A control register access and two conditional branches in code intended to be used in fast paths? Definitely not an option. Even the simplest possible solution - adding a global flag to be checked here - would already be questionable. Hence I think you should at least consider porting over proper instruction patching abstraction from Linux.

Jan, I did some investigation about how to handle this two instructions in Linux, basically, it uses the alternatives mechanism to handle these kind of cases. Let's take the following definition of ASM_STAC in Linux for example:

#define ASM_CLAC                                                        \
        661: ASM_NOP3 ;                                                 \
        .pushsection .altinstr_replacement, "ax" ;                      \
        662: __ASM_CLAC ;                                               \
        .popsection ;                                                   \
        .pushsection .altinstructions, "a" ;                            \
        altinstruction_entry 661b, 662b, X86_FEATURE_SMAP, 3, 3 ;       \

.popsection

ASM_CLAC is defined as NOP by default, it puts the real CLAC instruction in section "altinstr_replacement" and
the needed information to " altinstructions " section, which is useful to replace the default
definition by the alternative one. Here is the routine call path:
start_kernel () --> check_bugs() --> alternative_instructions().

Quoted 7 lines

In function alternative_instructions(), it will check the related features in CPU, if it exists, the alternative definition will overwrite the default one. So there is no conditional branches after this replacement when the Macro is being used.

Do you think we need to port this whole mechanism to Xen to support CLAC/STAC? I am not sure if it is a little overkilled.

Obviously we could use this machinery for other things. But whether it's needed here depends on the alternatives.

Quoted 4 lines

BTW, from the Linux implementation, I think we don't need to check the 'cr4' for the macros, we just need
to check whether the feature exists in the CPU. So is it acceptable to use the original code by eliminating the cr4 check?

Quoted 8 lines

That _might_ be acceptable if you bring it down to just the three really necessary instructions: BT, JNC, CLAC/STAC. But the "might" has to stand - this, after all, remains an addition of a conditional branch (and for the performance of STAC/CLAC I haven't seen any documentation so far either) to several fast paths, and hence the patching alternative can't be discarded as the potentially better one.

Jan

copy_{to,from}_guest() are already long paths (particularly for HVM) so a single extra conditional is not going to be too bad (and as after boot it will remain constant, the branch predictor will have a reliable time with it). It would certainly be fine for a v1 to get SMAP support working.

~Andrew

#12 lore raw
Quoted 54 lines
Quoted 1 line
Quoted 1 line · On 22.04.14 at 11:43, <[email protected]> wrote:
Quoted 45 lines · Jan Beulich
Quoted 30 lines
Quoted 9 lines
Quoted 1 line · On 22.04.14 at 09:41, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
That said, the macro contents itself is horrible too: A control register access and two conditional branches in code intended to be used in fast paths? Definitely not an option. Even the simplest possible solution - adding a global flag to be checked here - would already be questionable. Hence I think you should at least consider porting over proper instruction patching abstraction from Linux.

Jan, I did some investigation about how to handle this two instructions in Linux, basically, it uses the alternatives mechanism to handle these kind of cases. Let's take the following definition of ASM_STAC in Linux for example:

#define ASM_CLAC                                                        \
        661: ASM_NOP3 ;                                                 \
        .pushsection .altinstr_replacement, "ax" ;                      \
        662: __ASM_CLAC ;                                               \
        .popsection ;                                                   \
        .pushsection .altinstructions, "a" ;                            \
        altinstruction_entry 661b, 662b, X86_FEATURE_SMAP, 3, 3 ;       \

.popsection

ASM_CLAC is defined as NOP by default, it puts the real CLAC instruction in section "altinstr_replacement" and
the needed information to " altinstructions " section, which is useful to replace the default
definition by the alternative one. Here is the routine call path:
start_kernel () --> check_bugs() --> alternative_instructions().

Quoted 7 lines

In function alternative_instructions(), it will check the related features in CPU, if it exists, the alternative definition will overwrite the default one. So there is no conditional branches after this replacement when the Macro is being used.

Do you think we need to port this whole mechanism to Xen to support CLAC/STAC? I am not sure if it is a little overkilled.

Obviously we could use this machinery for other things. But whether it's needed here depends on the alternatives.

Quoted 5 lines

BTW, from the Linux implementation, I think we don't need to check the 'cr4'

for the macros, we just need
to check whether the feature exists in the CPU. So is it acceptable to use the original code by eliminating the cr4 check?

Quoted 6 lines

That _might_ be acceptable if you bring it down to just the three really necessary instructions: BT, JNC, CLAC/STAC. But the "might" has to stand - this, after all, remains an addition of a conditional branch (and for the performance of STAC/CLAC I haven't seen any documentation so far either) to several fast paths, and hence the patching alternative can't be discarded as the potentially better one.

Quoted 4 lines

copy_{to,from}_guest() are already long paths (particularly for HVM) so a single extra conditional is not going to be too bad (and as after boot it will remain constant, the branch predictor will have a reliable time with it). It would certainly be fine for a v1 to get SMAP support working.

But that's not the paths I'm concerned about. There's going to be a CLAC at exception, interrupt, and hypercall entry points. Those are the ones I'm concerned about.

Jan