All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] kbuild: Add macros cc-option-3 and __cc-option-3
@ 2017-07-21 21:56 Matthias Kaehlcke
  2017-07-21 21:56 ` [PATCH 2/2] x86/build: Fix stack alignment for CLang Matthias Kaehlcke
  2017-08-02 16:46 ` [PATCH 1/2] kbuild: Add macros cc-option-3 and __cc-option-3 Matthias Kaehlcke
  0 siblings, 2 replies; 5+ messages in thread
From: Matthias Kaehlcke @ 2017-07-21 21:56 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, H . Peter Anvin, Masahiro Yamada,
	Michal Marek
  Cc: x86, linux-kbuild, linux-kernel, dianders, Michael Davidson,
	Greg Hackmann, Nick Desaulniers, Stephen Hines, Kees Cook,
	Arnd Bergmann, Bernhard.Rosenkranzer, Matthias Kaehlcke

The macro cc-option receives two parameters (the second may be empty). It
returns the first parameter if it is a valid compiler option, otherwise
the second one. It is not evaluated if the second parameter is a valid
compiler option. This seems to be fine in virtually all cases, however
there are scenarios where the second paramater needs to be evaluated too,
and an empty value (or a third option) should be returned if it is not
valid.

The macro cc-option-3 receives three parameters and returns parameter 1
or 2 (in this order) if one of them is found to be a valid compiler
option, and otherwise paramater 3. The macro __cc-option-3 works
analogously.

Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---
 scripts/Kbuild.include | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index dd8e2dde0b34..dc83635f2317 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -113,6 +113,11 @@ as-instr = $(call try-run,\
 __cc-option = $(call try-run,\
 	$(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
 
+# __cc-option-3
+# Usage: MY_CFLAGS += $(call __cc-option-3,$(CC),$(MY_CFLAGS),\
+#	-mpreferred-stack-boundary=2,-mstack-alignment=4,)
+__cc-option-3 = $(call __cc-option,$(1),$(2),$(3),$(call __cc-option,$(1),$(2),$(4),$(5)))
+
 # Do not attempt to build with gcc plugins during cc-option tests.
 # (And this uses delayed resolution so the flags will be up to date.)
 CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
@@ -123,6 +128,10 @@ CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
 cc-option = $(call __cc-option, $(CC),\
 	$(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS),$(1),$(2))
 
+# cc-option-3
+# Usage: cflags-y += $(call cc-option-3,-mpreferred-stack-boundary=3,-mstack-alignment=8,)
+cc-option-3 = $(call cc-option,$(1),$(call cc-option,$(2),$(3)))
+
 # hostcc-option
 # Usage: cflags-y += $(call hostcc-option,-march=winchip-c6,-march=i586)
 hostcc-option = $(call __cc-option, $(HOSTCC),\
-- 
2.14.0.rc0.284.gd933b75aa4-goog

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] x86/build: Fix stack alignment for CLang
  2017-07-21 21:56 [PATCH 1/2] kbuild: Add macros cc-option-3 and __cc-option-3 Matthias Kaehlcke
@ 2017-07-21 21:56 ` Matthias Kaehlcke
  2017-08-02 16:46 ` [PATCH 1/2] kbuild: Add macros cc-option-3 and __cc-option-3 Matthias Kaehlcke
  1 sibling, 0 replies; 5+ messages in thread
From: Matthias Kaehlcke @ 2017-07-21 21:56 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, H . Peter Anvin, Masahiro Yamada,
	Michal Marek
  Cc: x86, linux-kbuild, linux-kernel, dianders, Michael Davidson,
	Greg Hackmann, Nick Desaulniers, Stephen Hines, Kees Cook,
	Arnd Bergmann, Bernhard.Rosenkranzer, Matthias Kaehlcke

Commit:

  d77698df39a5 ("x86/build: Specify stack alignment for clang")

intended to use the same stack alignment for clang as with gcc.

The two compilers use different options to configure the stack alignment
(gcc: -mpreferred-stack-boundary=n, clang: -mstack-alignment=n).

The above commit assumes that the clang option uses the same parameter
type as gcc, i.e. that the alignment is specified as 2^n. However clang
interprets the value of this option literally to use an alignment of n,
in consequence the stack remains misaligned.

Change the values used with -mstack-alignment to be the actual alignment
instead of a power of two.

Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---
 arch/x86/Makefile | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 1e902f926be3..2b2f59c1a5f0 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -11,14 +11,6 @@ else
         KBUILD_DEFCONFIG := $(ARCH)_defconfig
 endif
 
-# For gcc stack alignment is specified with -mpreferred-stack-boundary,
-# clang has the option -mstack-alignment for that purpose.
-ifneq ($(call cc-option, -mpreferred-stack-boundary=4),)
-        cc_stack_align_opt := -mpreferred-stack-boundary
-else ifneq ($(call cc-option, -mstack-alignment=4),)
-        cc_stack_align_opt := -mstack-alignment
-endif
-
 # How to compile the 16-bit code.  Note we always compile for -march=i386;
 # that way we can complain to the user if the CPU is insufficient.
 #
@@ -36,7 +28,9 @@ REALMODE_CFLAGS	:= $(M16_CFLAGS) -g -Os -D__KERNEL__ \
 
 REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), -ffreestanding)
 REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), -fno-stack-protector)
-REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), $(cc_stack_align_opt)=2)
+REALMODE_CFLAGS += $(call __cc-option-3, $(CC), $(REALMODE_CFLAGS),\
+	-mpreferred-stack-boundary=2,-mstack-alignment=4,)
+
 export REALMODE_CFLAGS
 
 # BITS is used as extension for files which are available in a 32 bit
@@ -76,7 +70,7 @@ ifeq ($(CONFIG_X86_32),y)
         # Align the stack to the register width instead of using the default
         # alignment of 16 bytes. This reduces stack usage and the number of
         # alignment instructions.
-        KBUILD_CFLAGS += $(call cc-option,$(cc_stack_align_opt)=2)
+        KBUILD_CFLAGS += $(call cc-option-3,-mpreferred-stack-boundary=2,-mstack-alignment=4,)
 
         # Disable unit-at-a-time mode on pre-gcc-4.0 compilers, it makes gcc use
         # a lot more stack due to the lack of sharing of stacklots:
@@ -115,7 +109,7 @@ else
         # default alignment which keep the stack *mis*aligned.
         # Furthermore an alignment to the register width reduces stack usage
         # and the number of alignment instructions.
-        KBUILD_CFLAGS += $(call cc-option,$(cc_stack_align_opt)=3)
+        KBUILD_CFLAGS += $(call cc-option-3,-mpreferred-stack-boundary=3,-mstack-alignment=8,)
 
 	# Use -mskip-rax-setup if supported.
 	KBUILD_CFLAGS += $(call cc-option,-mskip-rax-setup)
-- 
2.14.0.rc0.284.gd933b75aa4-goog

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] kbuild: Add macros cc-option-3 and __cc-option-3
  2017-07-21 21:56 [PATCH 1/2] kbuild: Add macros cc-option-3 and __cc-option-3 Matthias Kaehlcke
  2017-07-21 21:56 ` [PATCH 2/2] x86/build: Fix stack alignment for CLang Matthias Kaehlcke
@ 2017-08-02 16:46 ` Matthias Kaehlcke
  2017-08-07  1:01   ` Masahiro Yamada
  1 sibling, 1 reply; 5+ messages in thread
From: Matthias Kaehlcke @ 2017-08-02 16:46 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, H . Peter Anvin, Masahiro Yamada,
	Michal Marek
  Cc: x86, linux-kbuild, linux-kernel, dianders, Michael Davidson,
	Greg Hackmann, Nick Desaulniers, Stephen Hines, Kees Cook,
	Arnd Bergmann, Bernhard.Rosenkranzer

El Fri, Jul 21, 2017 at 02:56:56PM -0700 Matthias Kaehlcke ha dit:

> The macro cc-option receives two parameters (the second may be empty). It
> returns the first parameter if it is a valid compiler option, otherwise
> the second one. It is not evaluated if the second parameter is a valid
> compiler option. This seems to be fine in virtually all cases, however
> there are scenarios where the second paramater needs to be evaluated too,
> and an empty value (or a third option) should be returned if it is not
> valid.
> 
> The macro cc-option-3 receives three parameters and returns parameter 1
> or 2 (in this order) if one of them is found to be a valid compiler
> option, and otherwise paramater 3. The macro __cc-option-3 works
> analogously.

Any comment on this?

Thanks

Matthias

> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> ---
>  scripts/Kbuild.include | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
> index dd8e2dde0b34..dc83635f2317 100644
> --- a/scripts/Kbuild.include
> +++ b/scripts/Kbuild.include
> @@ -113,6 +113,11 @@ as-instr = $(call try-run,\
>  __cc-option = $(call try-run,\
>  	$(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
>  
> +# __cc-option-3
> +# Usage: MY_CFLAGS += $(call __cc-option-3,$(CC),$(MY_CFLAGS),\
> +#	-mpreferred-stack-boundary=2,-mstack-alignment=4,)
> +__cc-option-3 = $(call __cc-option,$(1),$(2),$(3),$(call __cc-option,$(1),$(2),$(4),$(5)))
> +
>  # Do not attempt to build with gcc plugins during cc-option tests.
>  # (And this uses delayed resolution so the flags will be up to date.)
>  CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
> @@ -123,6 +128,10 @@ CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
>  cc-option = $(call __cc-option, $(CC),\
>  	$(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS),$(1),$(2))
>  
> +# cc-option-3
> +# Usage: cflags-y += $(call cc-option-3,-mpreferred-stack-boundary=3,-mstack-alignment=8,)
> +cc-option-3 = $(call cc-option,$(1),$(call cc-option,$(2),$(3)))
> +
>  # hostcc-option
>  # Usage: cflags-y += $(call hostcc-option,-march=winchip-c6,-march=i586)
>  hostcc-option = $(call __cc-option, $(HOSTCC),\

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] kbuild: Add macros cc-option-3 and __cc-option-3
  2017-08-02 16:46 ` [PATCH 1/2] kbuild: Add macros cc-option-3 and __cc-option-3 Matthias Kaehlcke
@ 2017-08-07  1:01   ` Masahiro Yamada
  2017-08-07 18:33     ` Matthias Kaehlcke
  0 siblings, 1 reply; 5+ messages in thread
From: Masahiro Yamada @ 2017-08-07  1:01 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: Thomas Gleixner, Ingo Molnar, H . Peter Anvin, Michal Marek,
	X86 ML, Linux Kbuild mailing list, Linux Kernel Mailing List,
	Douglas Anderson, Michael Davidson, Greg Hackmann,
	Nick Desaulniers, Stephen Hines, Kees Cook, Arnd Bergmann,
	Bernhard Rosenkränzer

Hi Matthias,

Sorry for my late reply.

2017-08-03 1:46 GMT+09:00 Matthias Kaehlcke <mka@chromium.org>:
> El Fri, Jul 21, 2017 at 02:56:56PM -0700 Matthias Kaehlcke ha dit:
>
>> The macro cc-option receives two parameters (the second may be empty). It
>> returns the first parameter if it is a valid compiler option, otherwise
>> the second one. It is not evaluated if the second parameter is a valid
>> compiler option. This seems to be fine in virtually all cases, however
>> there are scenarios where the second paramater needs to be evaluated too,
>> and an empty value (or a third option) should be returned if it is not
>> valid.
>>
>> The macro cc-option-3 receives three parameters and returns parameter 1
>> or 2 (in this order) if one of them is found to be a valid compiler
>> option, and otherwise paramater 3. The macro __cc-option-3 works
>> analogously.
>
> Any comment on this?
>
> Thanks
>
> Matthias
>
>> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
>> ---
>>  scripts/Kbuild.include | 9 +++++++++
>>  1 file changed, 9 insertions(+)
>>
>> diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
>> index dd8e2dde0b34..dc83635f2317 100644
>> --- a/scripts/Kbuild.include
>> +++ b/scripts/Kbuild.include
>> @@ -113,6 +113,11 @@ as-instr = $(call try-run,\
>>  __cc-option = $(call try-run,\
>>       $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
>>
>> +# __cc-option-3
>> +# Usage: MY_CFLAGS += $(call __cc-option-3,$(CC),$(MY_CFLAGS),\
>> +#    -mpreferred-stack-boundary=2,-mstack-alignment=4,)
>> +__cc-option-3 = $(call __cc-option,$(1),$(2),$(3),$(call __cc-option,$(1),$(2),$(4),$(5)))
>> +
>>  # Do not attempt to build with gcc plugins during cc-option tests.
>>  # (And this uses delayed resolution so the flags will be up to date.)
>>  CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
>> @@ -123,6 +128,10 @@ CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
>>  cc-option = $(call __cc-option, $(CC),\
>>       $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS),$(1),$(2))
>>
>> +# cc-option-3
>> +# Usage: cflags-y += $(call cc-option-3,-mpreferred-stack-boundary=3,-mstack-alignment=8,)
>> +cc-option-3 = $(call cc-option,$(1),$(call cc-option,$(2),$(3)))


I do not like this macro much for the following reasons:


[1]
I guess your motivation is to evaluate the second option,
not receive the third option.

If this is the demand, I thought it might be nicer to
change cc-option to always evaluate the second option.

(I do no have a good idea for the implementation.)


[2]

cc-option-3 = $(call cc-option,$(1),$(call cc-option,$(2),$(3)))

evaluates the inner $(call cc-option,) first.

This works a bit differently from our expectation.


For example, let's consider the following case.

 $(call cc-option-3,-Oz,-Os,-O2)


I think we generally expect -Oz, -Os are tested in this order.
(If -Oz is supported by the compiler, the test for -Os will be skipped.)


In fact, cc-option-3 tests  -Os, -Oz in this order
because inner cc-option is evaluated before the outer one.
The test for -Os may or may not be necessary.

I do not have a good idea to improve this...





-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] kbuild: Add macros cc-option-3 and __cc-option-3
  2017-08-07  1:01   ` Masahiro Yamada
@ 2017-08-07 18:33     ` Matthias Kaehlcke
  0 siblings, 0 replies; 5+ messages in thread
From: Matthias Kaehlcke @ 2017-08-07 18:33 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Thomas Gleixner, Ingo Molnar, H . Peter Anvin, Michal Marek,
	X86 ML, Linux Kbuild mailing list, Linux Kernel Mailing List,
	Douglas Anderson, Michael Davidson, Greg Hackmann,
	Nick Desaulniers, Stephen Hines, Kees Cook, Arnd Bergmann,
	Bernhard Rosenkränzer

Hi Masahiro,

El Mon, Aug 07, 2017 at 10:01:41AM +0900 Masahiro Yamada ha dit:

> Hi Matthias,
> 
> Sorry for my late reply.
> 
> 2017-08-03 1:46 GMT+09:00 Matthias Kaehlcke <mka@chromium.org>:
> > El Fri, Jul 21, 2017 at 02:56:56PM -0700 Matthias Kaehlcke ha dit:
> >
> >> The macro cc-option receives two parameters (the second may be empty). It
> >> returns the first parameter if it is a valid compiler option, otherwise
> >> the second one. It is not evaluated if the second parameter is a valid
> >> compiler option. This seems to be fine in virtually all cases, however
> >> there are scenarios where the second paramater needs to be evaluated too,
> >> and an empty value (or a third option) should be returned if it is not
> >> valid.
> >>
> >> The macro cc-option-3 receives three parameters and returns parameter 1
> >> or 2 (in this order) if one of them is found to be a valid compiler
> >> option, and otherwise paramater 3. The macro __cc-option-3 works
> >> analogously.
> >
> > Any comment on this?
> >
> > Thanks
> >
> > Matthias
> >
> >> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> >> ---
> >>  scripts/Kbuild.include | 9 +++++++++
> >>  1 file changed, 9 insertions(+)
> >>
> >> diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
> >> index dd8e2dde0b34..dc83635f2317 100644
> >> --- a/scripts/Kbuild.include
> >> +++ b/scripts/Kbuild.include
> >> @@ -113,6 +113,11 @@ as-instr = $(call try-run,\
> >>  __cc-option = $(call try-run,\
> >>       $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
> >>
> >> +# __cc-option-3
> >> +# Usage: MY_CFLAGS += $(call __cc-option-3,$(CC),$(MY_CFLAGS),\
> >> +#    -mpreferred-stack-boundary=2,-mstack-alignment=4,)
> >> +__cc-option-3 = $(call __cc-option,$(1),$(2),$(3),$(call __cc-option,$(1),$(2),$(4),$(5)))
> >> +
> >>  # Do not attempt to build with gcc plugins during cc-option tests.
> >>  # (And this uses delayed resolution so the flags will be up to date.)
> >>  CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
> >> @@ -123,6 +128,10 @@ CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
> >>  cc-option = $(call __cc-option, $(CC),\
> >>       $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS),$(1),$(2))
> >>
> >> +# cc-option-3
> >> +# Usage: cflags-y += $(call cc-option-3,-mpreferred-stack-boundary=3,-mstack-alignment=8,)
> >> +cc-option-3 = $(call cc-option,$(1),$(call cc-option,$(2),$(3)))
> 
> 
> I do not like this macro much for the following reasons:
> 
> 
> [1]
> I guess your motivation is to evaluate the second option,
> not receive the third option.

In this case yes, a future use case could be to support another
compiler with different option names, but I suppose we can focus
on the present for now.

> If this is the demand, I thought it might be nicer to
> change cc-option to always evaluate the second option.

I considered that, but was reluctant to change current behavior,
though in practice it shouldn't make a difference.

> (I do no have a good idea for the implementation.)

One option could be a variant of the try-run macro, that receives the
'base command' as first parameter:

try-run-opt = $(shell set -e;           \
        TMP="$(TMPOUT).$$$$.tmp";       \
        TMPO="$(TMPOUT).$$$$.o";        \
        if ($(1) $(2)) >/dev/null 2>&1; \
        then echo "$(2)";               \
        elif [ -n "${3}" ] && ($(1) $(3)) >/dev/null 2>&1;      \
        then echo "$(3)";               \
        else echo "";                   \
        fi;                             \
        rm -f "$$TMP" "$$TMPO")

__cc-option = $(call try-run-opt,\
        $(1) -Werror $(2) -c -x c /dev/null -o "$$TMP",$(3),$(4))

try-run-opt assumes that is is valid to append an option to the end
of the base command.

For consistency we'd probably want to adapt other suitable xx-option
macros as well.

Does this look reasonable to you?

> cc-option-3 = $(call cc-option,$(1),$(call cc-option,$(2),$(3)))
> 
> evaluates the inner $(call cc-option,) first.
> 
> This works a bit differently from our expectation.
> 
> 
> For example, let's consider the following case.
> 
>  $(call cc-option-3,-Oz,-Os,-O2)
> 
> 
> I think we generally expect -Oz, -Os are tested in this order.
> (If -Oz is supported by the compiler, the test for -Os will be skipped.)
> 
> 
> In fact, cc-option-3 tests  -Os, -Oz in this order
> because inner cc-option is evaluated before the outer one.
> The test for -Os may or may not be necessary.

I agree, running the check for the alternative options always is not
desirable.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-08-07 18:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-21 21:56 [PATCH 1/2] kbuild: Add macros cc-option-3 and __cc-option-3 Matthias Kaehlcke
2017-07-21 21:56 ` [PATCH 2/2] x86/build: Fix stack alignment for CLang Matthias Kaehlcke
2017-08-02 16:46 ` [PATCH 1/2] kbuild: Add macros cc-option-3 and __cc-option-3 Matthias Kaehlcke
2017-08-07  1:01   ` Masahiro Yamada
2017-08-07 18:33     ` Matthias Kaehlcke

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.