powerpc/vdso: Avoid link stack corruption in __get_datapage()
PATCH 1/1 powerpc/vdso: Avoid link stack corruption in __get_datapage()
The 32 and 64 bit variants of __get_datapage() use a "bcl; mflr" to determine the loaded address of the VDSO. The current version of these attempt to use the special bcl variant which avoids pushing to the link stack.
Unfortunately it uses bcl+8 rather than the required bcl+4. Hence the current code results in link stack corruption and the resulting performance degradation (due to branch mis-prediction).
This patch moves us to bcl+4 by moving __kernel_datapage_offset out of __get_datapage().
With this patch, running the below benchmark we get a bump in performance on POWER8 for gettimeofday() (which uses __get_datapage()).
64bit gets ~4% improvement:
Without patch:
# ./tb
time =3D 0.180321
With patch:
# ./tb
time =3D 0.18740832bit gets ~9% improvement:
Without patch:
# ./tb
time =3D 0.276551
With patch:
# ./tb
time =3D 0.252767Testcase tb.c (stolen from Anton)
/* gcc -O2 tb.c -o tb */
#include <sys/time.h>
#include <stdio.h>
int main()
{
int i;
struct timeval tv_start, tv_end;
gettimeofday(&tv_start, NULL);
for(i =3D 0; i < 10000000; i++) {
gettimeofday(&tv_end, NULL);
}
printf("time =3D %.6f\n", tv_end.tv_sec - tv_start.tv_sec + (tv_end.tv_u=sec - tv_start.tv_usec) * 1e-6);
return 0;
}
arch/powerpc/kernel/vd= renamed from arch/powerpc/kernel/vdso32/datapage.S +0 -0
so32/datapage.S
index dc21e89..59cf5f4 100644
arch/powerpc/kernel/vdso32/datapage.S +0 -0
@@ -16,6 +16,10 @@ #include <asm/vdso.h>
=20
.text
+ .global __kernel_datapage_offset;
+__kernel_datapage_offset:
+ .long 0
+
V_FUNCTION_BEGIN(__get_datapage)
.cfi_startproc
/* We don't want that exposed or overridable as we want other objects
@@ -27,13 +31,11 @@ V_FUNCTION_BEGIN(__get_datapage)
mflr r0
.cfi_register lr,r0
=20
- bcl 20,31,1f
- .global __kernel_datapage_offset;
-__kernel_datapage_offset:
- .long 0
-1:
+ bcl 20,31,data_page_branch
+data_page_branch:
mflr r3
mtlr r0
+ addi r3, r3, __kernel_datapage_offset-data_page_branch
lwz r0,0(r3)
add r3,r0,r3
blr
arch/powerpc/kernel/vd= renamed from arch/powerpc/kernel/vdso64/datapage.S +0 -0
so64/datapage.S
index 79796de..2f01c4a 100644
arch/powerpc/kernel/vdso64/datapage.S +0 -0
@@ -16,6 +16,10 @@ #include <asm/vdso.h>
=20
.text
+.global __kernel_datapage_offset;
+__kernel_datapage_offset:
+ .long 0
+
V_FUNCTION_BEGIN(__get_datapage)
.cfi_startproc
/* We don't want that exposed or overridable as we want other objects
@@ -27,13 +31,11 @@ V_FUNCTION_BEGIN(__get_datapage)
mflr r0
.cfi_register lr,r0
=20
- bcl 20,31,1f
- .global __kernel_datapage_offset;
-__kernel_datapage_offset:
- .long 0
-1:
+ bcl 20,31,data_page_branch
+data_page_branch:
mflr r3
mtlr r0
+ addi r3, r3, __kernel_datapage_offset-data_page_branch
lwz r0,0(r3)
add r3,r0,r3
blr
Quoted 24 lines · Michael Neuling
The 32 and 64 bit variants of
__get_datapage()use a "bcl; mflr" to determine the loaded address of the VDSO. The current version of these attempt to use the special bcl variant which avoids pushing to the link stack.Unfortunately it uses bcl+8 rather than the required bcl+4. Hence the current code results in link stack corruption and the resulting performance degradation (due to branch mis-prediction).
This patch moves us to bcl+4 by moving
__kernel_datapage_offsetout of__get_datapage().With this patch, running the below benchmark we get a bump in performance on POWER8 for
gettimeofday()(which uses__get_datapage()).64bit gets ~4% improvement:
Without patch:# ./tb time = 0.180321 With patch: # ./tb time = 0.187408
Quoted 7 lines
32bit gets ~9% improvement:
Without patch:# ./tb time = 0.276551 With patch: # ./tb time = 0.252767
I've got the following results on POWER7 64bit
without the patch:
# ./tb time = 0.263337 # ./tb time = 0.251273 # ./tb time = 0.258453 # ./tb time = 0.260189
with the patch:
# ./tb time = 0.241517 # ./tb time = 0.241973 # ./tb time = 0.239365 # ./tb time = 0.240109
Quoted 99 lines
Testcase
tb.c(stolen from Anton)
/* gcc -O2tb.c-o tb */
#include <sys/time.h>
#include <stdio.h>int
main(){ int i; struct timeval tv_start, tv_end; gettimeofday(&tv_start, NULL); for(i = 0; i < 10000000; i++) { gettimeofday(&tv_end, NULL); } printf("time = %.6f\n", tv_end.tv_sec - tv_start.tv_sec + (tv_end.tv_usec- tv_start.tv_usec) * 1e-6);
return 0;}
Signed-off-by Michael Neuling <[email protected]>Reported-by Aaron Sawdey <[email protected]>arch/powerpc/kernel/vdso32/datapage.S +7
-5@@ -16,6 +16,10 @@ #include <asm/vdso.h> .text + .global __kernel_datapage_offset; +__kernel_datapage_offset: + .long 0 + V_FUNCTION_BEGIN(__get_datapage) .cfi_startproc /* We don't want that exposed or overridable as we want other objects@@ -27,13 +31,11 @@ V_FUNCTION_BEGIN(__get_datapage) mflr r0 .cfi_register lr,r0 - bcl 20,31,1f - .global __kernel_datapage_offset; -__kernel_datapage_offset: - .long 0 -1: + bcl 20,31,data_page_branch +data_page_branch: mflr r3 mtlr r0 + addi r3, r3, __kernel_datapage_offset-data_page_branch lwz r0,0(r3) add r3,r0,r3 blrarch/powerpc/kernel/vdso64/datapage.S +7
-5@@ -16,6 +16,10 @@ #include <asm/vdso.h> .text +.global __kernel_datapage_offset; +__kernel_datapage_offset: + .long 0 + V_FUNCTION_BEGIN(__get_datapage) .cfi_startproc /* We don't want that exposed or overridable as we want other objects@@ -27,13 +31,11 @@ V_FUNCTION_BEGIN(__get_datapage) mflr r0 .cfi_register lr,r0 - bcl 20,31,1f - .global __kernel_datapage_offset; -__kernel_datapage_offset: - .long 0 -1: + bcl 20,31,data_page_branch +data_page_branch: mflr r3 mtlr r0 + addi r3, r3, __kernel_datapage_offset-data_page_branch lwz r0,0(r3) add r3,r0,r3 blr_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
One thing that occurred to me was that since the __kernel_datapage_offset is located immediately before the function, the offset is small and you could get rid of the addi and just fold it into the lwz.
Aaron Sawdey, Ph.D. [email protected]
050-2/C113 (507) 253-7520 TL: 553-7520 home: 507/263-0782 IBM Linux Technology Center - PPC Toolchain
From: Denis Kirjanov <[email protected]>
To: Michael Neuling <[email protected]>
Cc: Michael Ellerman <[email protected]>, benh
<[email protected]>, Aaron Sawdey/Rochester/IBM@IBMUS, linuxppc-dev <[email protected]>, Anton Blanchard <[email protected]>, Steve Munroe/Rochester/IBM@IBMUS
Date: 09/23/2015 01:22 PM
Subject: Re: [PATCH] powerpc/vdso: Avoid link stack corruption in
__get_datapage()
Quoted 24 lines · Michael Neuling
The 32 and 64 bit variants of
__get_datapage()use a "bcl; mflr" to determine the loaded address of the VDSO. The current version of these attempt to use the special bcl variant which avoids pushing to the link stack.Unfortunately it uses bcl+8 rather than the required bcl+4. Hence the current code results in link stack corruption and the resulting performance degradation (due to branch mis-prediction).
This patch moves us to bcl+4 by moving
__kernel_datapage_offsetout of__get_datapage().With this patch, running the below benchmark we get a bump in performance on POWER8 for
gettimeofday()(which uses__get_datapage()).64bit gets ~4% improvement:
Without patch:# ./tb time = 0.180321 With patch: # ./tb time = 0.187408
Quoted 7 lines
32bit gets ~9% improvement:
Without patch:# ./tb time = 0.276551 With patch: # ./tb time = 0.252767
I've got the following results on POWER7 64bit
without the patch:
# ./tb time = 0.263337 # ./tb time = 0.251273 # ./tb time = 0.258453 # ./tb time = 0.260189
with the patch:
# ./tb time = 0.241517 # ./tb time = 0.241973 # ./tb time = 0.239365 # ./tb time = 0.240109
Quoted 7 lines
Testcase
tb.c(stolen from Anton)
/* gcc -O2tb.c-o tb */
#include <sys/time.h>
#include <stdio.h>int
main()
Quoted 12 lines
{
int i;
struct timeval tv_start, tv_end;
gettimeofday(&tv_start, NULL);
for(i = 0; i < 10000000; i++) {
gettimeofday(&tv_end, NULL);
}
printf("time = %.6f\n", tv_end.tv_sec - tv_start.tv_sec +(tv_end.tv_usec
Quoted 14 lines
- tv_start.tv_usec) * 1e-6);
return 0;}
Signed-off-by Michael Neuling <[email protected]>Reported-by Aaron Sawdey <[email protected]>arch/powerpc/kernel/vdso32/datapage.S +0
-0
arch/powerpc/kernel/vdso32/datapage.S line 25
Quoted 10 lines
arch/powerpc/kernel/vdso32/datapage.S +4
-0@@ -16,6 +16,10 @@ #include <asm/vdso.h> .text + .global __kernel_datapage_offset; +__kernel_datapage_offset: + .long 0 + V_FUNCTION_BEGIN(__get_datapage) .cfi_startproc /* We don't want that exposed or overridable as we want other
objects
Quoted 23 lines
arch/powerpc/kernel/vdso32/datapage.S +3
-5@@ -27,13 +31,11 @@ V_FUNCTION_BEGIN(__get_datapage) mflr r0 .cfi_register lr,r0 - bcl 20,31,1f - .global __kernel_datapage_offset; -__kernel_datapage_offset: - .long 0 -1: + bcl 20,31,data_page_branch +data_page_branch: mflr r3 mtlr r0 + addi r3, r3, __kernel_datapage_offset-data_page_branch lwz r0,0(r3) add r3,r0,r3 blrarch/powerpc/kernel/vdso64/datapage.S +0
-0
arch/powerpc/kernel/vdso64/datapage.S line 25
Quoted 10 lines
arch/powerpc/kernel/vdso64/datapage.S +4
-0@@ -16,6 +16,10 @@ #include <asm/vdso.h> .text +.global __kernel_datapage_offset; +__kernel_datapage_offset: + .long 0 + V_FUNCTION_BEGIN(__get_datapage) .cfi_startproc /* We don't want that exposed or overridable as we want other
objects
Quoted 23 lines
arch/powerpc/kernel/vdso64/datapage.S +3
-5@@ -27,13 +31,11 @@ V_FUNCTION_BEGIN(__get_datapage) mflr r0 .cfi_register lr,r0 - bcl 20,31,1f - .global __kernel_datapage_offset; -__kernel_datapage_offset: - .long 0 -1: + bcl 20,31,data_page_branch +data_page_branch: mflr r3 mtlr r0 + addi r3, r3, __kernel_datapage_offset-data_page_branch lwz r0,0(r3) add r3,r0,r3 blr_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
Quoted 50 lines · Michael Neuling
The 32 and 64 bit variants of
__get_datapage()use a "bcl; mflr" to determine the loaded address of the VDSO. The current version of these attempt to use the special bcl variant which avoids pushing to the link stack.Unfortunately it uses bcl+8 rather than the required bcl+4. Hence the current code results in link stack corruption and the resulting performance degradation (due to branch mis-prediction).
This patch moves us to bcl+4 by moving
__kernel_datapage_offsetout of__get_datapage().With this patch, running the below benchmark we get a bump in performance on POWER8 for
gettimeofday()(which uses__get_datapage()).64bit gets ~4% improvement:
Without patch:# ./tb time = 0.180321 With patch: # ./tb time = 0.18740832bit gets ~9% improvement:
Without patch:# ./tb time = 0.276551 With patch: # ./tb time = 0.252767Testcase
tb.c(stolen from Anton)
/* gcc -O2tb.c-o tb */
#include <sys/time.h>
#include <stdio.h>int
main(){ int i; struct timeval tv_start, tv_end; gettimeofday(&tv_start, NULL); for(i = 0; i < 10000000; i++) { gettimeofday(&tv_end, NULL); } printf("time = %.6f\n", tv_end.tv_sec - tv_start.tv_sec +
Quoted 4 lines
(tv_end.tv_usec - tv_start.tv_usec) * 1e-6);
return 0;}
You know where test cases are supposed to go.
I know it's not a pass/fail test, but it's still useful. If it's in the tree it will get run as part of automated test runs and we will have a record of the result over time.
cheers
Signature · 1 line
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
Quoted 4 lines · Aaron Sawdey
One thing that occurred to me was that since the
__kernel_datapage_offsetis located immediately before the function, the offset is small and you could get rid of the addi and just fold it into the lwz.
r3 is reused at the end to add to r0, so if r3 doesn't have the right offset for __kernel_datapage_offset it doesn't work.
Mikey
Quoted 192 lines
=20
=20
Aaron Sawdey, Ph.D. [email protected]
050-2/C113 (507) 253-7520 TL: 553-7520 home: 507/263-0782 IBM Linux Technology Center - PPC Toolchain
=20
Inactive hide details for Denis Kirjanov ---09/23/2015 01:22:39 PM---On 9/23/15, Michael Neuling <[email protected]> wrote: > TDenis Kirjanov ---09/23/2015 01:22:39 PM---On 9/23/15, Michael Neuling <[email protected]> wrote: > The 32 and 64 bit variants of__get_datapag
=20
From: Denis Kirjanov <[email protected]>
To: Michael Neuling <[email protected]>
Cc: Michael Ellerman <[email protected]>, benh <[email protected]>, Aaron Sawdey/Rochester/IBM@IBMUS, linuxppc-dev <[email protected]>, Anton Blanchard <[email protected]>, Steve Munroe/Rochester/IBM@IBMUS
Date: 09/23/2015 01:22 PM
Subject: Re: [PATCH] powerpc/vdso: Avoid link stack corruption in__get_datapage()
=20
=20
=20
______________________________________________________________________
=20
=20
=20Quoted 2 lines · Michael Neuling
The 32 and 64 bit variants of
__get_datapage()use a "bcl; mflr" to determine the loaded address of the VDSO. The current version ofthese
Quoted 4 lines
attempt to use the special bcl variant which avoids pushing to the link stack.
Unfortunately it uses bcl+8 rather than the required bcl+4. Hence
the
Quoted 18 lines
current code results in link stack corruption and the resulting performance degradation (due to branch mis-prediction).
This patch moves us to bcl+4 by moving
__kernel_datapage_offsetout of__get_datapage().With this patch, running the below benchmark we get a bump in performance on POWER8 for
gettimeofday()(which uses__get_datapage()).64bit gets ~4% improvement:
Without patch:# ./tb time =3D 0.180321 With patch: # ./tb time =3D 0.187408Quoted 7 lines
32bit gets ~9% improvement:
Without patch:# ./tb time =3D 0.276551 With patch: # ./tb time =3D 0.252767=20
I've got the following results on POWER7 64bit
without the patch:# ./tb time =3D 0.263337 # ./tb time =3D 0.251273 # ./tb time =3D 0.258453 # ./tb time =3D 0.260189 =20 with the patch: # ./tb time =3D 0.241517 # ./tb time =3D 0.241973 # ./tb time =3D 0.239365 # ./tb time =3D 0.240109 =20 =20 =20Quoted 12 lines
Testcase
tb.c(stolen from Anton)
/* gcc -O2tb.c-o tb */
#include <sys/time.h>
#include <stdio.h>int main() { int i;struct timeval
tv_start,tv_end;Quoted 7 lines
gettimeofday(&tv_start, NULL); for(i =3D 0; i < 10000000; i++) { gettimeofday(&tv_end, NULL); }printf("time =3D %.6f\n", tv_end.tv_sec - tv_start.tv_sec +
(tv_end.tv_usec
Quoted 14 lines
- tv_start.tv_usec) * 1e-6);
return 0; }Signed-off-by Michael Neuling <[email protected]>Reported-by Aaron Sawdey <[email protected]>arch/powerpc/kernel/vdso32/datapage.S +0
-0arch/powerpc/kernel/vdso32/datapage.S line 25
Quoted 10 lines
arch/powerpc/kernel/vdso32/datapage.S +4
-0@@ -16,6 +16,10 @@ #include <asm/vdso.h> .text + .global __kernel_datapage_offset; +__kernel_datapage_offset: + .long 0 + V_FUNCTION_BEGIN(__get_datapage) .cfi_startproc /* We don't want that exposed or overridable as we want otherobjects
Quoted 23 lines
arch/powerpc/kernel/vdso32/datapage.S +3
-5@@ -27,13 +31,11 @@ V_FUNCTION_BEGIN(__get_datapage) mflr r0 .cfi_register lr,r0 - bcl 20,31,1f - .global __kernel_datapage_offset; -__kernel_datapage_offset: - .long 0 -1: + bcl 20,31,data_page_branch +data_page_branch: mflr r3 mtlr r0 + addi r3, r3, __kernel_datapage_offset-data_page_branch lwz r0,0(r3) add r3,r0,r3 blrarch/powerpc/kernel/vdso64/datapage.S +0
-0arch/powerpc/kernel/vdso64/datapage.S line 25
Quoted 10 lines
arch/powerpc/kernel/vdso64/datapage.S +4
-0@@ -16,6 +16,10 @@ #include <asm/vdso.h> .text +.global __kernel_datapage_offset; +__kernel_datapage_offset: + .long 0 + V_FUNCTION_BEGIN(__get_datapage) .cfi_startproc /* We don't want that exposed or overridable as we want otherobjects
Quoted 19 lines
arch/powerpc/kernel/vdso64/datapage.S +3
-5@@ -27,13 +31,11 @@ V_FUNCTION_BEGIN(__get_datapage) mflr r0 .cfi_register lr,r0 - bcl 20,31,1f - .global __kernel_datapage_offset; -__kernel_datapage_offset: - .long 0 -1: + bcl 20,31,data_page_branch +data_page_branch: mflr r3 mtlr r0 + addi r3, r3, __kernel_datapage_offset-data_page_branch lwz r0,0(r3) add r3,r0,r3 blrQuoted 4 lines
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev=20
=20
=20
=20
Quoted 13 lines
I've got the following results on POWER7 64bit
without the patch:# ./tb time =3D 0.263337 # ./tb time =3D 0.251273 # ./tb time =3D 0.258453 # ./tb time =3D 0.260189 =20 with the patch:
Quoted 8 lines
# ./tb time =3D 0.241517 # ./tb time =3D 0.241973 # ./tb time =3D 0.239365 # ./tb time =3D 0.240109
Nice, I was expecting a bump there too but didn't test.. Looks like about 7% to me. =20
Thanks,
Mikey
Quoted 56 lines · Michael Ellerman
Quoted 52 lines · Michael Neuling
The 32 and 64 bit variants of
__get_datapage()use a "bcl; mflr" to determine the loaded address of the VDSO. The current version of these attempt to use the special bcl variant which avoids pushing to the link stack.Unfortunately it uses bcl+8 rather than the required bcl+4. Hence the current code results in link stack corruption and the resulting performance degradation (due to branch mis-prediction).
This patch moves us to bcl+4 by moving
__kernel_datapage_offsetout of__get_datapage().With this patch, running the below benchmark we get a bump in performance on POWER8 for
gettimeofday()(which uses__get_datapage()).64bit gets ~4% improvement:
Without patch:# ./tb time = 0.180321 With patch: # ./tb time = 0.18740832bit gets ~9% improvement:
Without patch:# ./tb time = 0.276551 With patch: # ./tb time = 0.252767Testcase
tb.c(stolen from Anton)
/* gcc -O2tb.c-o tb */
#include <sys/time.h>
#include <stdio.h>int
main(){ int i; struct timeval tv_start, tv_end; gettimeofday(&tv_start, NULL); for(i = 0; i < 10000000; i++) { gettimeofday(&tv_end, NULL); } printf("time = %.6f\n", tv_end.tv_sec - tv_start.tv_sec +(tv_end.tv_usec - tv_start.tv_usec) * 1e-6);
Quoted 8 lines
Quoted 2 lines
return 0;}
You know where test cases are supposed to go.
I know it's not a pass/fail test, but it's still useful. If it's in the tree it will get run as part of automated test runs and we will have a record of the result over time.
I can send a patch for it.
Quoted 8 lines
cheers
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
Michael Ellerman
reviewerQuoted 27 lines · Denis Kirjanov
Quoted 26 lines · Michael Ellerman
Quoted 23 lines · Michael Neuling
Testcase
tb.c(stolen from Anton)
/* gcc -O2tb.c-o tb */
#include <sys/time.h>
#include <stdio.h>int
main(){ int i; struct timeval tv_start, tv_end; gettimeofday(&tv_start, NULL); for(i = 0; i < 10000000; i++) { gettimeofday(&tv_end, NULL); } printf("time = %.6f\n", tv_end.tv_sec - tv_start.tv_sec +(tv_end.tv_usec - tv_start.tv_usec) * 1e-6);
return 0;}
Quoted 7 lines
Quoted 5 lines
You know where test cases are supposed to go.
I know it's not a pass/fail test, but it's still useful. If it's in the tree it will get run as part of automated test runs and we will have a record of the result over time.
I can send a patch for it.
Mikey was working on it I think.
cheers