2012-06-23 16:14:50

by H.J. Lu

[permalink] [raw]
Subject: PATCH] Use -mpreferred-stack-boundary=3 on x86-64 if supported

Hi,

-mpreferred-stack-boundary=3 can be used on x86-64 with GCC 4.8.0:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53383

I backported it to hjl/x32/gcc-4_7-branch branch at

http://gcc.gnu.org/git/?p=gcc.git;a=summary

http://gcc.gnu.org/git/?p=gcc.git;a=commit;h=96828b1f869a31e3a1227afa420b8f28e71
e006c

This patch uses -mpreferred-stack-boundary=3 on x86-64 if it is supported.
I have been running 3.4 kernel compiled with this option for weeks.


H.J.


Attachments:
0001-Use-mpreferred-stack-boundary-3-if-supported.patch (884.00 B)

2012-06-23 16:13:32

by Andi Kleen

[permalink] [raw]
Subject: Re: PATCH] Use -mpreferred-stack-boundary=3 on x86-64 if supported

> This patch uses -mpreferred-stack-boundary=3 on x86-64 if it is supported.
> I have been running 3.4 kernel compiled with this option for weeks.

But why?

All the SSE code in the kernel should adjust manually. And the other
code doesn't need it.

Besides the entry points do not guarantee alignment.

For the kernel it would be probably more useful to have a attribute
per function that forces alignment.

-Andi
--
[email protected] -- Speaking for myself only.

2012-06-23 16:17:24

by H. Peter Anvin

[permalink] [raw]
Subject: Re: PATCH] Use -mpreferred-stack-boundary=3 on x86-64 if supported

To keep gcc from paddong the stack to maintain a nonexistent alignment.

Andi Kleen <[email protected]> wrote:

>> This patch uses -mpreferred-stack-boundary=3 on x86-64 if it is
>supported.
>> I have been running 3.4 kernel compiled with this option for weeks.
>
>But why?
>
>All the SSE code in the kernel should adjust manually. And the other
>code doesn't need it.
>
>Besides the entry points do not guarantee alignment.
>
>For the kernel it would be probably more useful to have a attribute
>per function that forces alignment.
>
>-Andi
>--
>[email protected] -- Speaking for myself only.

--
Sent from my mobile phone. Please excuse brevity and lack of formatting.

2012-06-23 16:49:24

by H.J. Lu

[permalink] [raw]
Subject: Re: PATCH] Use -mpreferred-stack-boundary=3 on x86-64 if supported

On Sat, Jun 23, 2012 at 9:17 AM, H. Peter Anvin <[email protected]> wrote:
> To keep gcc from paddong the stack to maintain a nonexistent alignment.
>
> Andi Kleen <[email protected]> wrote:
>
>>> This patch uses -mpreferred-stack-boundary=3 on x86-64 if it is
>>supported.
>>> I have been running 3.4 kernel compiled with this option for weeks.
>>
>>But why?
>>
>>All the SSE code in the kernel should adjust manually. And the other
>>code doesn't need it.
>>
>>Besides the entry points do not guarantee alignment.
>>
>>For the kernel it would be probably more useful to have a attribute
>>per function that forces alignment.

There was an attempt from you to do it in 2007:

http://lkml.indiana.edu/hypermail/linux/kernel/0708.2/2604.html
http://gcc.gnu.org/ml/gcc-patches/2007-07/msg01697.html

But the gcc patch was never checked in.


--
H.J.

2012-06-23 19:27:12

by Andi Kleen

[permalink] [raw]
Subject: Re: PATCH] Use -mpreferred-stack-boundary=3 on x86-64 if supported

On Sat, Jun 23, 2012 at 09:17:06AM -0700, H. Peter Anvin wrote:
> To keep gcc from paddong the stack to maintain a nonexistent alignment.


Ah ok. 3 instead of 4.

I thought i had done that a long time ago, but now it somehow
only seems to be there for 32bit.

Makes sense.

-Andi

2012-06-23 19:46:17

by H. Peter Anvin

[permalink] [raw]
Subject: Re: PATCH] Use -mpreferred-stack-boundary=3 on x86-64 if supported

Yes, it didn't work on 64 bits until now.

Andi Kleen <[email protected]> wrote:

>On Sat, Jun 23, 2012 at 09:17:06AM -0700, H. Peter Anvin wrote:
>> To keep gcc from paddong the stack to maintain a nonexistent
>alignment.
>
>
>Ah ok. 3 instead of 4.
>
>I thought i had done that a long time ago, but now it somehow
>only seems to be there for 32bit.
>
>Makes sense.
>
>-Andi

--
Sent from my mobile phone. Please excuse brevity and lack of formatting.

2012-06-23 22:58:13

by Randy Dunlap

[permalink] [raw]
Subject: Re: PATCH] Use -mpreferred-stack-boundary=3 on x86-64 if supported

On 06/23/2012 09:49 AM, H.J. Lu wrote:

> On Sat, Jun 23, 2012 at 9:17 AM, H. Peter Anvin <[email protected]> wrote:
>> To keep gcc from paddong the stack to maintain a nonexistent alignment.
>>
>> Andi Kleen <[email protected]> wrote:
>>
>>>> This patch uses -mpreferred-stack-boundary=3 on x86-64 if it is
>>> supported.
>>>> I have been running 3.4 kernel compiled with this option for weeks.
>>>
>>> But why?
>>>
>>> All the SSE code in the kernel should adjust manually. And the other
>>> code doesn't need it.
>>>
>>> Besides the entry points do not guarantee alignment.
>>>
>>> For the kernel it would be probably more useful to have a attribute
>>> per function that forces alignment.
>
> There was an attempt from you to do it in 2007:
>
> http://lkml.indiana.edu/hypermail/linux/kernel/0708.2/2604.html
> http://gcc.gnu.org/ml/gcc-patches/2007-07/msg01697.html
>
> But the gcc patch was never checked in.
>
>


Some (most) of this explanation needs to be in the patch description.


--
~Randy

2012-06-24 03:13:24

by H.J. Lu

[permalink] [raw]
Subject: [tip:x86/build] x86-64, gcc: Use -mpreferred-stack-boundary= 3 if supported

Commit-ID: d9b0cde91c60da0ed5f92cdc3ac878142e6b5f27
Gitweb: http://git.kernel.org/tip/d9b0cde91c60da0ed5f92cdc3ac878142e6b5f27
Author: H.J. Lu <[email protected]>
AuthorDate: Tue, 29 May 2012 14:31:23 -0700
Committer: H. Peter Anvin <[email protected]>
CommitDate: Sat, 23 Jun 2012 19:25:22 -0700

x86-64, gcc: Use -mpreferred-stack-boundary=3 if supported

On x86-64, the standard ABI requires alignment to 16 bytes. However,
this is not actually necessary in the kernel (we don't do SSE except
in very controlled ways); and furthermore, the standard kernel entry
on x86-64 actually leaves the stack on an odd 8-byte boundary, which
means that gcc will generate extra instructions to keep the stack
*mis*aligned!

gcc 4.8 adds an -mpreferred-stack-boundary=3 option to override this
and lets us save some stack space and a handful of instructions.

Note that this causes us to pass -mno-sse twice; this is redundant,
but necessary since the cc-option test will fail unless -mno-sse is
passed on the same command line.

[ hpa: rewrote the patch description ]

Signed-off-by: H.J. Lu <[email protected]>
Link: http://lkml.kernel.org/r/CAMe9rOqPfy3JcZRLaUeCjBe9BVY-P6e0uaSbMi5hvS-6WwQueg@mail.gmail.com
Signed-off-by: H. Peter Anvin <[email protected]>
---
arch/x86/Makefile | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 1f25214..b0c5276 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -49,6 +49,9 @@ else
KBUILD_AFLAGS += -m64
KBUILD_CFLAGS += -m64

+ # Use -mpreferred-stack-boundary=3 if supported.
+ KBUILD_CFLAGS += $(call cc-option,-mno-sse -mpreferred-stack-boundary=3)
+
# FIXME - should be integrated in Makefile.cpu (Makefile_32.cpu)
cflags-$(CONFIG_MK8) += $(call cc-option,-march=k8)
cflags-$(CONFIG_MPSC) += $(call cc-option,-march=nocona)