Skip to contentSkip to conversation

x86/asm: Use ASM_FLAG_OUT() to simplify atomic and bitop stubs

PATCH In review Andrew Cooper 2 participants 8 messages Feb 15, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/asm: Use ASM_FLAG_OUT() to simplify atomic and bitop stubs

bitops.h cannot include asm_defns.h, because the static inlines in cpumasks.h result in forward declarations of the bitops.h contents. Move ASM_FLAG_OUT() to a new asm/compiler.h to compensate.

While making changes, switch bool_t to bool and use named asm parameters.

No (intended) functional change. Without GCC flags, the disassembly is identical before and after this patch. With GCC flags however, this patch seems to cause GCC to make instruction scheduling decisions.

The first place with significant changes in the disassembly is the the middle of do_domctl(), which switches from:

    lock decl 0x1d8(%rax)
    sete   %r14b
    mov    $0xffffffffffffffea,%rbx
    test   %r14b,%r14b
    je     ffff82d0801034d4

to:

    lock decl 0x1d8(%rax)
    mov    $0x0,%r14d
    mov    $0xffffffffffffffea,%rbx
    jne    ffff82d0801034d4

avoiding the use of %r14d as an intermediate for calculating the conditional jump, freeing it up for later use. As a result, the compiled code is a little more efficient and smaller overall.

---
CC: Jan Beulich <[email protected]>

I have confirmed using:

xen/include/asm-x86/atomic.h +1 -1
index ef7e70b..de79443 100644
@@ -197,7 +197,7 @@ static inline int atomic_dec_and_test(atomic_t *v)
 
 #ifdef __GCC_ASM_FLAG_OUTPUTS__
     asm volatile ( "lock; decl %0"
-                   : "+m" (*(volatile int *)&v->counter), "=@ccz" (c)
+                   : "+m" (*(volatile int *)&v->counter), "=@ccnz" (c)
                    :: "memory" );
 #else
     asm volatile ( "lock; decl %0; setz %1"

that compiler explicitly chose to insert the sete instruction despite the "=@ccz" condition. The resulting hypervisor does work perfectly well, so I guess this will be down to some internal details of GCC's code generation.

---
 xen/include/asm-x86/asm_defns.h |  6 ---
 xen/include/asm-x86/atomic.h    | 64 ++++++++++++-------------------
 xen/include/asm-x86/bitops.h    | 85 ++++++++++++++---------------------------
 xen/include/asm-x86/compiler.h  | 20 ++++++++++
 xen/include/asm-x86/config.h    |  1 +
 5 files changed, 73 insertions(+), 103 deletions(-)
 create mode 100644 xen/include/asm-x86/compiler.h
xen/include/asm-x86/asm_defns.h +0 -6
index 220ae2e..f1c6fa1 100644
@@ -413,10 +413,4 @@ static always_inline void stac(void)
 #define REX64_PREFIX "rex64/"
 #endif
 
-#ifdef __GCC_ASM_FLAG_OUTPUTS__
-# define ASM_FLAG_OUT(yes, no) yes
-#else
-# define ASM_FLAG_OUT(yes, no) no
-#endif
-
 #endif /* __X86_ASM_DEFNS_H__ */
xen/include/asm-x86/atomic.h +24 -40

92 lines not on this page · raw message

xen/include/asm-x86/bitops.h +28 -57

127 lines not on this page · raw message

xen/include/asm-x86/compiler.h new file +20 -0
new file mode 100644
index 0000000..1fd6a8d
@@ -0,0 +1,20 @@
+#ifndef __ASM_X86_COMPILER_H__
+#define __ASM_X86_COMPILER_H__
+
+#ifdef __GCC_ASM_FLAG_OUTPUTS__
+# define ASM_FLAG_OUT(yes, no) yes
+#else
+# define ASM_FLAG_OUT(yes, no) no
+#endif
+
+#endif /* __ASM_X86_COMPILER_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/asm-x86/config.h +1 -0
index 6fd84e7..b48a0de 100644
@@ -107,6 +107,7 @@ extern unsigned char boot_edid_info[128];
 #define asmlinkage
 
 #include <xen/const.h>
+#include <asm/compiler.h>
 
 #define PML4_ENTRY_BITS  39
 #define PML4_ENTRY_BYTES (_AC(1,UL) << PML4_ENTRY_BITS)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 9 lines · Andrew Cooper

bitops.h cannot include asm_defns.h, because the static inlines in cpumasks.h result in forward declarations of the bitops.h contents. Move ASM_FLAG_OUT() to a new asm/compiler.h to compensate.

While making changes, switch bool_t to bool and use named asm parameters.

No (intended) functional change. Without GCC flags, the disassembly is identical before and after this patch. With GCC flags however, this patch seems to cause GCC to make instruction scheduling decisions.

This is supposed to read "make better instruction...".

~Andrew

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

#2 lore raw
In reply to Andrew Cooper
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 15.02.17 at 15:22, <[email protected]> wrote:

bitops.h cannot include asm_defns.h, because the static inlines in cpumasks.h result in forward declarations of the bitops.h contents. Move ASM_FLAG_OUT() to a new asm/compiler.h to compensate.

While making changes, switch bool_t to bool and use named asm parameters.

No (intended) functional change. Without GCC flags, the disassembly is identical before and after this patch. With GCC flags however, this patch seems to cause GCC to make instruction scheduling decisions.

The first place with significant changes in the disassembly is the the middle of do_domctl(), which switches from:

    lock decl 0x1d8(%rax)
    sete   %r14b
    mov    $0xffffffffffffffea,%rbx
    test   %r14b,%r14b
    je     ffff82d0801034d4

to:

Quoted 8 lines
    lock decl 0x1d8(%rax)
    mov    $0x0,%r14d
    mov    $0xffffffffffffffea,%rbx
    jne    ffff82d0801034d4

avoiding the use of %r14d as an intermediate for calculating the conditional jump, freeing it up for later use. As a result, the compiled code is a little more efficient and smaller overall.

I have to admit that I find this rather surprising, especially since (leaving the naming of the asm() operands aside) the preprocessed source should not change afaict. It being unexpected I think calls for better understanding what's going on here.

xen/include/asm-x86/config.h extern unsigned char boot_edid_info[128]; · line 110

Quoted 7 lines
xen/include/asm-x86/config.h +1 -0
@@ -107,6 +107,7 @@ extern unsigned char boot_edid_info[128];
 #define asmlinkage
 
 #include <xen/const.h>
+#include <asm/compiler.h>

Why here? You need it in bitops.h and maybe (it's not really used there) asm_defns.h only for now.

Jan

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

#3 lore raw
Quoted 33 lines
Quoted 3 lines
Quoted 2 lines · On 15.02.17 at 15:55, <[email protected]> wrote:
Quoted 1 line · On 15.02.17 at 15:22, <[email protected]> wrote:

bitops.h cannot include asm_defns.h, because the static inlines in

cpumasks.h

Quoted 1 line

result in forward declarations of the bitops.h contents. Move

ASM_FLAG_OUT()

Quoted 9 lines

to a new asm/compiler.h to compensate.

While making changes, switch bool_t to bool and use named asm parameters.

No (intended) functional change. Without GCC flags, the disassembly is identical before and after this patch. With GCC flags however, this patch seems to cause GCC to make instruction scheduling decisions.

The first place with significant changes in the disassembly is the the

middle

Quoted 10 lines

of do_domctl(), which switches from:

    lock decl 0x1d8(%rax)
    sete   %r14b
    mov    $0xffffffffffffffea,%rbx
    test   %r14b,%r14b
    je     ffff82d0801034d4

to:

Quoted 7 lines
    lock decl 0x1d8(%rax)
    mov    $0x0,%r14d
    mov    $0xffffffffffffffea,%rbx
    jne    ffff82d0801034d4

avoiding the use of %r14d as an intermediate for calculating the conditional jump, freeing it up for later use. As a result, the compiled code is a

Quoted 7 lines

little

Quoted 1 line

more efficient and smaller overall.

I have to admit that I find this rather surprising, especially since (leaving the naming of the asm() operands aside) the preprocessed source should not change afaict. It being unexpected I think calls for better understanding what's going on here.

This is a result from the bool_t -> bool change and has nothing to do with the use of ASM_FLAG_OUT(). There's also no difference of produced code in a non-debug build.

Jan

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

#4 lore raw
In reply to Jan Beulich
Quoted 27 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 15.02.17 at 15:22, <[email protected]> wrote:

bitops.h cannot include asm_defns.h, because the static inlines in cpumasks.h result in forward declarations of the bitops.h contents. Move ASM_FLAG_OUT() to a new asm/compiler.h to compensate.

While making changes, switch bool_t to bool and use named asm parameters.

No (intended) functional change. Without GCC flags, the disassembly is identical before and after this patch. With GCC flags however, this patch seems to cause GCC to make instruction scheduling decisions.

The first place with significant changes in the disassembly is the the middle of do_domctl(), which switches from:

    lock decl 0x1d8(%rax)
    sete   %r14b
    mov    $0xffffffffffffffea,%rbx
    test   %r14b,%r14b
    je     ffff82d0801034d4

to:

    lock decl 0x1d8(%rax)
    mov    $0x0,%r14d
    mov    $0xffffffffffffffea,%rbx
    jne    ffff82d0801034d4
Quoted 7 lines
Quoted 3 lines

avoiding the use of %r14d as an intermediate for calculating the conditional jump, freeing it up for later use. As a result, the compiled code is a little more efficient and smaller overall.

I have to admit that I find this rather surprising, especially since (leaving the naming of the asm() operands aside) the preprocessed source should not change afaict. It being unexpected I think calls for better understanding what's going on here.

I presume you are happy now given your other discovery?

Quoted 10 lines

xen/include/asm-x86/config.h extern unsigned char boot_edid_info[128]; · line 110

Quoted 8 lines
xen/include/asm-x86/config.h +1 -0
@@ -107,6 +107,7 @@ extern unsigned char boot_edid_info[128];
 #define asmlinkage
 
 #include <xen/const.h>
+#include <asm/compiler.h>

Why here? You need it in bitops.h and maybe (it's not really used there) asm_defns.h only for now.

I am looking to avoid duplicating the definition, and this seemed like an appropriate place to live.

Would you instead prefer me to duplicate it in bitops?

~Andrew

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

#5 lore raw
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 15.02.17 at 17:00, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 15.02.17 at 15:22, <[email protected]> wrote:

bitops.h cannot include asm_defns.h, because the static inlines in

cpumasks.h

Quoted 1 line
Quoted 1 line

result in forward declarations of the bitops.h contents. Move

ASM_FLAG_OUT()

Quoted 9 lines
Quoted 9 lines

to a new asm/compiler.h to compensate.

While making changes, switch bool_t to bool and use named asm parameters.

No (intended) functional change. Without GCC flags, the disassembly is identical before and after this patch. With GCC flags however, this patch seems to cause GCC to make instruction scheduling decisions.

The first place with significant changes in the disassembly is the the

middle

Quoted 10 lines
Quoted 10 lines

of do_domctl(), which switches from:

    lock decl 0x1d8(%rax)
    sete   %r14b
    mov    $0xffffffffffffffea,%rbx
    test   %r14b,%r14b
    je     ffff82d0801034d4

to:

Quoted 7 lines
Quoted 7 lines
    lock decl 0x1d8(%rax)
    mov    $0x0,%r14d
    mov    $0xffffffffffffffea,%rbx
    jne    ffff82d0801034d4

avoiding the use of %r14d as an intermediate for calculating the conditional jump, freeing it up for later use. As a result, the compiled code is a

Quoted 8 lines

little

Quoted 5 lines
Quoted 1 line

more efficient and smaller overall.

I have to admit that I find this rather surprising, especially since (leaving the naming of the asm() operands aside) the preprocessed source should not change afaict. It being unexpected I think calls for better understanding what's going on here.

I presume you are happy now given your other discovery?

Yes, albeit the commit message may want refining a little.

Quoted 10 lines
Quoted 9 lines

xen/include/asm-x86/config.h extern unsigned char boot_edid_info[128]; · line 110

Quoted 7 lines
xen/include/asm-x86/config.h +1 -0
@@ -107,6 +107,7 @@ extern unsigned char boot_edid_info[128];
 #define asmlinkage
 
 #include <xen/const.h>
+#include <asm/compiler.h>

Why here? You need it in bitops.h and maybe (it's not really used there) asm_defns.h only for now.

Quoted 4 lines

I am looking to avoid duplicating the definition, and this seemed like an appropriate place to live.

Would you instead prefer me to duplicate it in bitops?

Duplicate what? The macro? No. The new header is fine, just that I'd rather not see it included from config.h. And btw., other than when Sergey wanted to put in a non-x86 header, instead of introducing asm/compiler.h here, I think I would then agree to simply put it in xen/compiler.h and be done once and for all.

Jan

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

#6 lore raw

PATCH 1/1 x86/asm: Use ASM_FLAG_OUT() to simplify atomic and bitop stubs

In reply to Andrew Cooper

bitops.h cannot include asm_defns.h, because the static inlines in cpumasks.h result in forward declarations of the bitops.h contents. Move ASM_FLAG_OUT() to a new asm/compiler.h to compensate.

While making changes, switch bool_t to bool and use named asm parameters.

No functional change.

---
CC: Jan Beulich <[email protected]>

v2:
* Move ASM_FLAG_OUT() to xen/compiler.h rather than introducing asm-x86/compiler.h
* Drop a lot of the commit message.

---
 xen/include/asm-x86/asm_defns.h |  6 ---
 xen/include/asm-x86/atomic.h    | 64 ++++++++++++-------------------
 xen/include/asm-x86/bitops.h    | 85 ++++++++++++++---------------------------
 xen/include/xen/compiler.h      |  6 +++
 4 files changed, 58 insertions(+), 103 deletions(-)
xen/include/asm-x86/asm_defns.h +0 -6
index 220ae2e..f1c6fa1 100644
@@ -413,10 +413,4 @@ static always_inline void stac(void)
 #define REX64_PREFIX "rex64/"
 #endif
 
-#ifdef __GCC_ASM_FLAG_OUTPUTS__
-# define ASM_FLAG_OUT(yes, no) yes
-#else
-# define ASM_FLAG_OUT(yes, no) no
-#endif
-
 #endif /* __X86_ASM_DEFNS_H__ */
xen/include/asm-x86/atomic.h +24 -40

92 lines not on this page · raw message

xen/include/asm-x86/bitops.h +28 -57

127 lines not on this page · raw message

xen/include/xen/compiler.h +6 -0
index e800709..16aeeea 100644
@@ -94,4 +94,10 @@
     __asm__ ("" : "=r"(__ptr) : "0"(ptr));      \
     (typeof(ptr)) (__ptr + (off)); })
 
+#ifdef __GCC_ASM_FLAG_OUTPUTS__
+# define ASM_FLAG_OUT(yes, no) yes
+#else
+# define ASM_FLAG_OUT(yes, no) no
+#endif
+
 #endif /* __LINUX_COMPILER_H */
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 1/1
#7 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 15.02.17 at 17:53, <[email protected]> wrote:

bitops.h cannot include asm_defns.h, because the static inlines in cpumasks.h result in forward declarations of the bitops.h contents. Move ASM_FLAG_OUT()
to a new asm/compiler.h to compensate.

While making changes, switch bool_t to bool and use named asm parameters.

No functional change.

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