Skip to contentSkip to conversation

x86/clear_page: Update clear_page_sse2() after dropping 32bit Xen

PATCH In review Andrew Cooper 2 participants 4 messages Jun 17, 2019 – Jun 18, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/clear_page: Update clear_page_sse2() after dropping 32bit Xen

This code was never updated when the 32bit build of Xen was dropped.

* Expand the now-redundant ptr_reg macro.
* The number of iterations in the loop can be halfed by using 64bit writes, without consuming any extra execution resource in the pipeline. Adjust all numbers/offsets appropriately.
* Replace dec with sub to avoid a eflags stall, and position it to be macro-fused with the related jmp.
* With no need to preserve eflags across the body of the loop, replace lea with add which has 1/3'rd the latency on basically all 64bit hardware.

A quick userspace perf test on my Haswell dev box indicates that the old version takes ~1385 cycles on average (ignoring outliers), and the new version takes ~1060 cyles, or about 77% of the time.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Edwin Török <[email protected]>

There is almost certainly better room for improvement, especially now that we have alternatives, but this is substantial improvement which is very safe for backport.

---
 xen/arch/x86/clear_page.S | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
xen/arch/x86/clear_page.S +7 -9
index 243a767..0817610 100644
@@ -2,18 +2,16 @@
 
 #include <asm/page.h>
 
-#define ptr_reg %rdi
-
 ENTRY(clear_page_sse2)
-        mov     $PAGE_SIZE/16, %ecx
+        mov     $PAGE_SIZE/32, %ecx
         xor     %eax,%eax
 
-0:      dec     %ecx
-        movnti  %eax, (ptr_reg)
-        movnti  %eax, 4(ptr_reg)
-        movnti  %eax, 8(ptr_reg)
-        movnti  %eax, 12(ptr_reg)
-        lea     16(ptr_reg), ptr_reg
+0:      movnti  %rax,  0(%rdi)
+        movnti  %rax,  8(%rdi)
+        movnti  %rax, 16(%rdi)
+        movnti  %rax, 24(%rdi)
+        add     $32, %rdi
+        sub     $1, %ecx
         jnz     0b
 
         sfence
Signature · 7 lines
2.1.4


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

This code was never updated when the 32bit build of Xen was dropped.

* Expand the now-redundant ptr_reg macro.
* The number of iterations in the loop can be halfed by using 64bit writes, without consuming any extra execution resource in the pipeline. Adjust all numbers/offsets appropriately.
* Replace dec with sub to avoid a eflags stall, and position it to be macro-fused with the related jmp.
* With no need to preserve eflags across the body of the loop, replace lea with add which has 1/3'rd the latency on basically all 64bit hardware.

Quoted 3 lines

A quick userspace perf test on my Haswell dev box indicates that the old version takes ~1385 cycles on average (ignoring outliers), and the new version takes ~1060 cyles, or about 77% of the time.

And just for giggles, a rep stosq loop on this hardware is ~180 cycles, which is more than 5 times better than the result of this patch (16% of the time).

In some copious free time, clear/copy page ought to be come runtime dependent on FastString being enabled, but I don't have time to organise this right now.

~Andrew

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

Reviewed-by on v1 1/1
#2 lore raw
In reply to Andrew Cooper
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 17.06.19 at 21:49, <[email protected]> wrote:

This code was never updated when the 32bit build of Xen was dropped.

* Expand the now-redundant ptr_reg macro.
* The number of iterations in the loop can be halfed by using 64bit writes, without consuming any extra execution resource in the pipeline. Adjust all numbers/offsets appropriately.
* Replace dec with sub to avoid a eflags stall, and position it to be macro-fused with the related jmp.
* With no need to preserve eflags across the body of the loop, replace lea with add which has 1/3'rd the latency on basically all 64bit hardware.

Quoted 6 lines

A quick userspace perf test on my Haswell dev box indicates that the old version takes ~1385 cycles on average (ignoring outliers), and the new version takes ~1060 cyles, or about 77% of the time.

Assuming you want this to go in despite your REP STOSQ remark later on,

with one remark:

Quoted 10 lines
xen/arch/x86/clear_page.S +0 -3
@@ -2,18 +2,16 @@
 
 #include <asm/page.h>
 
-#define ptr_reg %rdi
-
 ENTRY(clear_page_sse2)
-        mov     $PAGE_SIZE/16, %ecx

xen/arch/x86/clear_page.S line 9

Quoted 10 lines
xen/arch/x86/clear_page.S +2 -6
+        mov     $PAGE_SIZE/32, %ecx
         xor     %eax,%eax
 
-0:      dec     %ecx
-        movnti  %eax, (ptr_reg)
-        movnti  %eax, 4(ptr_reg)
-        movnti  %eax, 8(ptr_reg)
-        movnti  %eax, 12(ptr_reg)
-        lea     16(ptr_reg), ptr_reg
+0:      movnti  %rax,  0(%rdi)

Could I talk you into leaving out this 0? Rather old gas actually emits an 8-bit displacement when it's spelled like this.

Jan

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

#3 lore raw
Quoted 36 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 17.06.19 at 21:49, <[email protected]> wrote:

This code was never updated when the 32bit build of Xen was dropped.

* Expand the now-redundant ptr_reg macro.
* The number of iterations in the loop can be halfed by using 64bit writes, without consuming any extra execution resource in the pipeline. Adjust all numbers/offsets appropriately.
* Replace dec with sub to avoid a eflags stall, and position it to be macro-fused with the related jmp.
* With no need to preserve eflags across the body of the loop, replace lea with add which has 1/3'rd the latency on basically all 64bit hardware.

Quoted 6 lines

A quick userspace perf test on my Haswell dev box indicates that the old version takes ~1385 cycles on average (ignoring outliers), and the new version takes ~1060 cyles, or about 77% of the time.

Assuming you want this to go in despite your REP STOSQ remark later on,

with one remark:

Quoted 13 lines
xen/arch/x86/clear_page.S +1 -3
@@ -2,18 +2,16 @@
 
 #include <asm/page.h>
 
-#define ptr_reg %rdi
-
 ENTRY(clear_page_sse2)
-        mov     $PAGE_SIZE/16, %ecx
+        mov     $PAGE_SIZE/32, %ecx
         xor     %eax,%eax
 
Quoted 9 lines

xen/arch/x86/clear_page.S line 9

Quoted 7 lines
xen/arch/x86/clear_page.S +1 -6
-0:      dec     %ecx
-        movnti  %eax, (ptr_reg)
-        movnti  %eax, 4(ptr_reg)
-        movnti  %eax, 8(ptr_reg)
-        movnti  %eax, 12(ptr_reg)
-        lea     16(ptr_reg), ptr_reg
+0:      movnti  %rax,  0(%rdi)

Could I talk you into leaving out this 0? Rather old gas actually emits an 8-bit displacement when it's spelled like this.

Oh ok.  I'll still align the (%rdi) though to make the column easier to read.

I'll put this in now, and see if I can find some time before 4.13 ships to make some alternatives-based better options.

~Andrew

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