linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
       [not found]     ` <5c856f36-69a7-e274-f72a-c3aef195adeb@kernel.org>
@ 2021-08-17 18:03       ` Kees Cook
  2021-08-17 18:25         ` Nathan Chancellor
  0 siblings, 1 reply; 13+ messages in thread
From: Kees Cook @ 2021-08-17 18:03 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Linus Torvalds, Gustavo A. R. Silva, Nick Desaulniers,
	Masahiro Yamada, Linux Kbuild mailing list,
	Linux Kernel Mailing List, clang-built-linux, linux-hardening

On Mon, Aug 16, 2021 at 09:55:28PM -0700, Nathan Chancellor wrote:
> If you/Gustavo would prefer, I can upgrade that check to
> 
> ifneq ($(call cc-option, -Wunreachable-code-fallthrough),)
> 
> I was just trying to save a call to the compiler, as that is more expensive
> than a shell test call.

I prefer the option test -- this means no changes are needed on the
kernel build side if it ever finds itself backported to earlier versions
(and it handles the current case of "14" not meaning "absolute latest").

More specifically, I think you want this (untested):

diff --git a/Makefile b/Makefile
index b5fd51e68ae9..9845ea50a368 100644
--- a/Makefile
+++ b/Makefile
@@ -859,11 +859,11 @@ KBUILD_CFLAGS += -Wno-gnu
 # source of a reference will be _MergedGlobals and not on of the whitelisted names.
 # See modpost pattern 2
 KBUILD_CFLAGS += -mno-global-merge
+# Warn about unmarked fall-throughs in switch statement only if we can also
+# disable the bogus unreachable code warnings.
+KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough -Wno-unreachable-code-fallthrough,)
 else
-
 # Warn about unmarked fall-throughs in switch statement.
-# Disabled for clang while comment to attribute conversion happens and
-# https://github.com/ClangBuiltLinux/linux/issues/636 is discussed.
 KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=5,)
 endif
 

-- 
Kees Cook

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17 18:03       ` [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+ Kees Cook
@ 2021-08-17 18:25         ` Nathan Chancellor
  2021-08-17 21:17           ` Masahiro Yamada
  0 siblings, 1 reply; 13+ messages in thread
From: Nathan Chancellor @ 2021-08-17 18:25 UTC (permalink / raw)
  To: Kees Cook
  Cc: Linus Torvalds, Gustavo A. R. Silva, Nick Desaulniers,
	Masahiro Yamada, Linux Kbuild mailing list,
	Linux Kernel Mailing List, clang-built-linux, linux-hardening

On 8/17/2021 11:03 AM, Kees Cook wrote:
> On Mon, Aug 16, 2021 at 09:55:28PM -0700, Nathan Chancellor wrote:
>> If you/Gustavo would prefer, I can upgrade that check to
>>
>> ifneq ($(call cc-option, -Wunreachable-code-fallthrough),)
>>
>> I was just trying to save a call to the compiler, as that is more expensive
>> than a shell test call.
> 
> I prefer the option test -- this means no changes are needed on the
> kernel build side if it ever finds itself backported to earlier versions
> (and it handles the current case of "14" not meaning "absolute latest").
> 
> More specifically, I think you want this (untested):

That should work but since -Wunreachable-code-fallthrough is off by 
default, I did not really see a reason to include it in KBUILD_CFLAGS. I 
do not have a strong opinion though, your version is smaller than mine 
is so we can just go with that. I'll defer to Gustavo on it since he has 
put in all of the work cleaning up the warnings.

Cheers,
Nathan

> diff --git a/Makefile b/Makefile
> index b5fd51e68ae9..9845ea50a368 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -859,11 +859,11 @@ KBUILD_CFLAGS += -Wno-gnu
>   # source of a reference will be _MergedGlobals and not on of the whitelisted names.
>   # See modpost pattern 2
>   KBUILD_CFLAGS += -mno-global-merge
> +# Warn about unmarked fall-throughs in switch statement only if we can also
> +# disable the bogus unreachable code warnings.
> +KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough -Wno-unreachable-code-fallthrough,)
>   else
> -
>   # Warn about unmarked fall-throughs in switch statement.
> -# Disabled for clang while comment to attribute conversion happens and
> -# https://github.com/ClangBuiltLinux/linux/issues/636 is discussed.
>   KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=5,)
>   endif
>   
> 

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17 18:25         ` Nathan Chancellor
@ 2021-08-17 21:17           ` Masahiro Yamada
  2021-08-17 21:33             ` Gustavo A. R. Silva
  2021-08-25 21:09             ` Nick Desaulniers
  0 siblings, 2 replies; 13+ messages in thread
From: Masahiro Yamada @ 2021-08-17 21:17 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Kees Cook, Linus Torvalds, Gustavo A. R. Silva, Nick Desaulniers,
	Linux Kbuild mailing list, Linux Kernel Mailing List,
	clang-built-linux, linux-hardening

On Wed, Aug 18, 2021 at 3:25 AM Nathan Chancellor <nathan@kernel.org> wrote:
>
> On 8/17/2021 11:03 AM, Kees Cook wrote:
> > On Mon, Aug 16, 2021 at 09:55:28PM -0700, Nathan Chancellor wrote:
> >> If you/Gustavo would prefer, I can upgrade that check to
> >>
> >> ifneq ($(call cc-option, -Wunreachable-code-fallthrough),)
> >>
> >> I was just trying to save a call to the compiler, as that is more expensive
> >> than a shell test call.
> >
> > I prefer the option test -- this means no changes are needed on the
> > kernel build side if it ever finds itself backported to earlier versions
> > (and it handles the current case of "14" not meaning "absolute latest").
> >
> > More specifically, I think you want this (untested):
>
> That should work but since -Wunreachable-code-fallthrough is off by
> default, I did not really see a reason to include it in KBUILD_CFLAGS. I
> do not have a strong opinion though, your version is smaller than mine
> is so we can just go with that. I'll defer to Gustavo on it since he has
> put in all of the work cleaning up the warnings.



https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8

   did two things:

 (1) Change the -Wimplicit-fallthrough behavior so that it fits
      to our use in the kernel

 (2) Add a new option -Wunreachable-code-fallthrough
      that works like the previous -Wimplicit-fallthrough of
      Clang <= 13.0.0


They are separate things.

Checking the presence of -Wunreachable-code-fallthrough
does not make sense since we are only interested in (1) here.



So, checking the Clang version is sensible and matches
the explanation in the comment block.


Moreover, using $(shell test ...) is less expensive than cc-option.


If you want to make it even faster, you can use only
built-in functions, like this:


# Warn about unmarked fall-throughs in switch statement.
# Clang prior to 14.0.0 warned on unreachable fallthroughs with
# -Wimplicit-fallthrough, which is unacceptable due to IS_ENABLED().
# https://bugs.llvm.org/show_bug.cgi?id=51094
ifeq ($(firstword $(sort $(CONFIG_CLANG_VERSION) 140000)),140000)
KBUILD_CFLAGS += -Wimplicit-fallthrough
endif



The $(sort ...) is alphabetical sort, not numeric sort.
It works for us because the minimum Clang version is 10.0.1
(that is CONFIG_CLANG_VERSION is always 6-digit)

It will break when Clang version 100.0.0 is released.

But, before that, we will raise the minimum supported clang version,
and this conditional will go away.




> Cheers,
> Nathan
>
> > diff --git a/Makefile b/Makefile
> > index b5fd51e68ae9..9845ea50a368 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -859,11 +859,11 @@ KBUILD_CFLAGS += -Wno-gnu
> >   # source of a reference will be _MergedGlobals and not on of the whitelisted names.
> >   # See modpost pattern 2
> >   KBUILD_CFLAGS += -mno-global-merge
> > +# Warn about unmarked fall-throughs in switch statement only if we can also
> > +# disable the bogus unreachable code warnings.
> > +KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough -Wno-unreachable-code-fallthrough,)
> >   else
> > -
> >   # Warn about unmarked fall-throughs in switch statement.
> > -# Disabled for clang while comment to attribute conversion happens and
> > -# https://github.com/ClangBuiltLinux/linux/issues/636 is discussed.
> >   KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=5,)
> >   endif
> >
> >



-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17 21:17           ` Masahiro Yamada
@ 2021-08-17 21:33             ` Gustavo A. R. Silva
  2021-08-17 23:06               ` Kees Cook
  2021-08-25 21:09             ` Nick Desaulniers
  1 sibling, 1 reply; 13+ messages in thread
From: Gustavo A. R. Silva @ 2021-08-17 21:33 UTC (permalink / raw)
  To: Masahiro Yamada, Nathan Chancellor
  Cc: Kees Cook, Linus Torvalds, Gustavo A. R. Silva, Nick Desaulniers,
	Linux Kbuild mailing list, Linux Kernel Mailing List,
	clang-built-linux, linux-hardening



On 8/17/21 16:17, Masahiro Yamada wrote:
> On Wed, Aug 18, 2021 at 3:25 AM Nathan Chancellor <nathan@kernel.org> wrote:
>>
>> On 8/17/2021 11:03 AM, Kees Cook wrote:
>>> On Mon, Aug 16, 2021 at 09:55:28PM -0700, Nathan Chancellor wrote:
>>>> If you/Gustavo would prefer, I can upgrade that check to
>>>>
>>>> ifneq ($(call cc-option, -Wunreachable-code-fallthrough),)
>>>>
>>>> I was just trying to save a call to the compiler, as that is more expensive
>>>> than a shell test call.
>>>
>>> I prefer the option test -- this means no changes are needed on the
>>> kernel build side if it ever finds itself backported to earlier versions
>>> (and it handles the current case of "14" not meaning "absolute latest").
>>>
>>> More specifically, I think you want this (untested):
>>
>> That should work but since -Wunreachable-code-fallthrough is off by
>> default, I did not really see a reason to include it in KBUILD_CFLAGS. I
>> do not have a strong opinion though, your version is smaller than mine
>> is so we can just go with that. I'll defer to Gustavo on it since he has
>> put in all of the work cleaning up the warnings.
> 
> 
> 
> https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
> 
>    did two things:
> 
>  (1) Change the -Wimplicit-fallthrough behavior so that it fits
>       to our use in the kernel
> 
>  (2) Add a new option -Wunreachable-code-fallthrough
>       that works like the previous -Wimplicit-fallthrough of
>       Clang <= 13.0.0
> 
> 
> They are separate things.
> 
> Checking the presence of -Wunreachable-code-fallthrough
> does not make sense since we are only interested in (1) here.
> 
> 
> 
> So, checking the Clang version is sensible and matches
> the explanation in the comment block.
> 
> 
> Moreover, using $(shell test ...) is less expensive than cc-option.
> 
> 
> If you want to make it even faster, you can use only
> built-in functions, like this:
> 
> 
> # Warn about unmarked fall-throughs in switch statement.
> # Clang prior to 14.0.0 warned on unreachable fallthroughs with
> # -Wimplicit-fallthrough, which is unacceptable due to IS_ENABLED().
> # https://bugs.llvm.org/show_bug.cgi?id=51094
> ifeq ($(firstword $(sort $(CONFIG_CLANG_VERSION) 140000)),140000)
> KBUILD_CFLAGS += -Wimplicit-fallthrough
> endif
> 
> 
> 
> The $(sort ...) is alphabetical sort, not numeric sort.
> It works for us because the minimum Clang version is 10.0.1
> (that is CONFIG_CLANG_VERSION is always 6-digit)
> 
> It will break when Clang version 100.0.0 is released.
> 
> But, before that, we will raise the minimum supported clang version,
> and this conditional will go away.

I like this. :)

I'm going to make the 0-day robot test it in my tree, first.

Thanks!
--
Gustavo



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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17 21:33             ` Gustavo A. R. Silva
@ 2021-08-17 23:06               ` Kees Cook
  2021-08-17 23:23                 ` Nathan Chancellor
  0 siblings, 1 reply; 13+ messages in thread
From: Kees Cook @ 2021-08-17 23:06 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Masahiro Yamada, Nathan Chancellor, Linus Torvalds,
	Gustavo A. R. Silva, Nick Desaulniers, Linux Kbuild mailing list,
	Linux Kernel Mailing List, clang-built-linux, linux-hardening

On Tue, Aug 17, 2021 at 04:33:25PM -0500, Gustavo A. R. Silva wrote:
> 
> 
> On 8/17/21 16:17, Masahiro Yamada wrote:
> > On Wed, Aug 18, 2021 at 3:25 AM Nathan Chancellor <nathan@kernel.org> wrote:
> >>
> >> On 8/17/2021 11:03 AM, Kees Cook wrote:
> >>> On Mon, Aug 16, 2021 at 09:55:28PM -0700, Nathan Chancellor wrote:
> >>>> If you/Gustavo would prefer, I can upgrade that check to
> >>>>
> >>>> ifneq ($(call cc-option, -Wunreachable-code-fallthrough),)
> >>>>
> >>>> I was just trying to save a call to the compiler, as that is more expensive
> >>>> than a shell test call.
> >>>
> >>> I prefer the option test -- this means no changes are needed on the
> >>> kernel build side if it ever finds itself backported to earlier versions
> >>> (and it handles the current case of "14" not meaning "absolute latest").
> >>>
> >>> More specifically, I think you want this (untested):
> >>
> >> That should work but since -Wunreachable-code-fallthrough is off by
> >> default, I did not really see a reason to include it in KBUILD_CFLAGS. I
> >> do not have a strong opinion though, your version is smaller than mine
> >> is so we can just go with that. I'll defer to Gustavo on it since he has
> >> put in all of the work cleaning up the warnings.
> > 
> > 
> > 
> > https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
> > 
> >    did two things:
> > 
> >  (1) Change the -Wimplicit-fallthrough behavior so that it fits
> >       to our use in the kernel
> > 
> >  (2) Add a new option -Wunreachable-code-fallthrough
> >       that works like the previous -Wimplicit-fallthrough of
> >       Clang <= 13.0.0
> > 
> > 
> > They are separate things.
> > 
> > Checking the presence of -Wunreachable-code-fallthrough
> > does not make sense since we are only interested in (1) here.
> > 
> > So, checking the Clang version is sensible and matches
> > the explanation in the comment block.

I thought one of the problems (which is quickly draining away) that
needed to be solved here is that some Clang trunk builds (that report
as version 14) don't yet have support for -Wunreachable-code-fallthrough
since they aren't new enough.

> > # Warn about unmarked fall-throughs in switch statement.
> > # Clang prior to 14.0.0 warned on unreachable fallthroughs with
> > # -Wimplicit-fallthrough, which is unacceptable due to IS_ENABLED().
> > # https://bugs.llvm.org/show_bug.cgi?id=51094
> > ifeq ($(firstword $(sort $(CONFIG_CLANG_VERSION) 140000)),140000)
> > KBUILD_CFLAGS += -Wimplicit-fallthrough
> > endif
> > 
> > The $(sort ...) is alphabetical sort, not numeric sort.
> > It works for us because the minimum Clang version is 10.0.1
> > (that is CONFIG_CLANG_VERSION is always 6-digit)
> > 
> > It will break when Clang version 100.0.0 is released.
> > 
> > But, before that, we will raise the minimum supported clang version,
> > and this conditional will go away.

If a version test is preferred, cool; this is a nice trick. :)

> I like this. :)
> 
> I'm going to make the 0-day robot test it in my tree, first.

Sounds good to me!

-- 
Kees Cook

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17 23:06               ` Kees Cook
@ 2021-08-17 23:23                 ` Nathan Chancellor
  2021-08-17 23:40                   ` Gustavo A. R. Silva
                                     ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Nathan Chancellor @ 2021-08-17 23:23 UTC (permalink / raw)
  To: Kees Cook, Gustavo A. R. Silva, Philip Li
  Cc: Masahiro Yamada, Linus Torvalds, Gustavo A. R. Silva,
	Nick Desaulniers, Linux Kbuild mailing list,
	Linux Kernel Mailing List, clang-built-linux, linux-hardening

On 8/17/2021 4:06 PM, Kees Cook wrote:
> On Tue, Aug 17, 2021 at 04:33:25PM -0500, Gustavo A. R. Silva wrote:
>>
>>
>> On 8/17/21 16:17, Masahiro Yamada wrote:
>>> On Wed, Aug 18, 2021 at 3:25 AM Nathan Chancellor <nathan@kernel.org> wrote:
>>>>
>>>> On 8/17/2021 11:03 AM, Kees Cook wrote:
>>>>> On Mon, Aug 16, 2021 at 09:55:28PM -0700, Nathan Chancellor wrote:
>>>>>> If you/Gustavo would prefer, I can upgrade that check to
>>>>>>
>>>>>> ifneq ($(call cc-option, -Wunreachable-code-fallthrough),)
>>>>>>
>>>>>> I was just trying to save a call to the compiler, as that is more expensive
>>>>>> than a shell test call.
>>>>>
>>>>> I prefer the option test -- this means no changes are needed on the
>>>>> kernel build side if it ever finds itself backported to earlier versions
>>>>> (and it handles the current case of "14" not meaning "absolute latest").
>>>>>
>>>>> More specifically, I think you want this (untested):
>>>>
>>>> That should work but since -Wunreachable-code-fallthrough is off by
>>>> default, I did not really see a reason to include it in KBUILD_CFLAGS. I
>>>> do not have a strong opinion though, your version is smaller than mine
>>>> is so we can just go with that. I'll defer to Gustavo on it since he has
>>>> put in all of the work cleaning up the warnings.
>>>
>>>
>>>
>>> https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
>>>
>>>     did two things:
>>>
>>>   (1) Change the -Wimplicit-fallthrough behavior so that it fits
>>>        to our use in the kernel
>>>
>>>   (2) Add a new option -Wunreachable-code-fallthrough
>>>        that works like the previous -Wimplicit-fallthrough of
>>>        Clang <= 13.0.0
>>>
>>>
>>> They are separate things.
>>>
>>> Checking the presence of -Wunreachable-code-fallthrough
>>> does not make sense since we are only interested in (1) here.
>>>
>>> So, checking the Clang version is sensible and matches
>>> the explanation in the comment block.
> 
> I thought one of the problems (which is quickly draining away) that
> needed to be solved here is that some Clang trunk builds (that report
> as version 14) don't yet have support for -Wunreachable-code-fallthrough
> since they aren't new enough.

Philip, how often is the kernel test robot's clang version rebuilt? 
Would it be possible to bump it to latest ToT or at least 
9ed4a94d6451046a51ef393cd62f00710820a7e8 so that we do not get bit by 
this warning when we go to enable this flag?

I do not know of any other CI aside from ours that is testing with tip 
of tree clang and ours should already have a clang that includes my 
patch since it comes from apt.llvm.org.

>>> # Warn about unmarked fall-throughs in switch statement.
>>> # Clang prior to 14.0.0 warned on unreachable fallthroughs with
>>> # -Wimplicit-fallthrough, which is unacceptable due to IS_ENABLED().
>>> # https://bugs.llvm.org/show_bug.cgi?id=51094
>>> ifeq ($(firstword $(sort $(CONFIG_CLANG_VERSION) 140000)),140000)
>>> KBUILD_CFLAGS += -Wimplicit-fallthrough
>>> endif

Very clever and nifty trick! I have verified that it works for clang 13 
and 14 along with a theoretical clang 15. Gustavo, feel free to stick a

Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Tested-by: Nathan Chancellor <nathan@kernel.org>

if you so desire.

>>>
>>> The $(sort ...) is alphabetical sort, not numeric sort.
>>> It works for us because the minimum Clang version is 10.0.1
>>> (that is CONFIG_CLANG_VERSION is always 6-digit)
>>>
>>> It will break when Clang version 100.0.0 is released.
>>>
>>> But, before that, we will raise the minimum supported clang version,
>>> and this conditional will go away.
> 
> If a version test is preferred, cool; this is a nice trick. :)
> 
>> I like this. :)
>>
>> I'm going to make the 0-day robot test it in my tree, first.
> 
> Sounds good to me!
> 

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17 23:23                 ` Nathan Chancellor
@ 2021-08-17 23:40                   ` Gustavo A. R. Silva
  2021-08-18  4:15                   ` Masahiro Yamada
                                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 13+ messages in thread
From: Gustavo A. R. Silva @ 2021-08-17 23:40 UTC (permalink / raw)
  To: Nathan Chancellor, Kees Cook, Philip Li
  Cc: Masahiro Yamada, Linus Torvalds, Gustavo A. R. Silva,
	Nick Desaulniers, Linux Kbuild mailing list,
	Linux Kernel Mailing List, clang-built-linux, linux-hardening



On 8/17/21 18:23, Nathan Chancellor wrote:
>>>> # Warn about unmarked fall-throughs in switch statement.
>>>> # Clang prior to 14.0.0 warned on unreachable fallthroughs with
>>>> # -Wimplicit-fallthrough, which is unacceptable due to IS_ENABLED().
>>>> # https://bugs.llvm.org/show_bug.cgi?id=51094
>>>> ifeq ($(firstword $(sort $(CONFIG_CLANG_VERSION) 140000)),140000)
>>>> KBUILD_CFLAGS += -Wimplicit-fallthrough
>>>> endif
> 
> Very clever and nifty trick! I have verified that it works for clang 13 and 14 along with a theoretical clang 15. Gustavo, feel free to stick a
> 
> Reviewed-by: Nathan Chancellor <nathan@kernel.org>
> Tested-by: Nathan Chancellor <nathan@kernel.org>
> 
> if you so desire.

Yep; I just tested it locally with clang 13 and 14, too.

Thanks
--
Gustavo



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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17 23:23                 ` Nathan Chancellor
  2021-08-17 23:40                   ` Gustavo A. R. Silva
@ 2021-08-18  4:15                   ` Masahiro Yamada
  2021-08-18  4:27                   ` Philip Li
  2021-08-18 12:12                   ` Mark Brown
  3 siblings, 0 replies; 13+ messages in thread
From: Masahiro Yamada @ 2021-08-18  4:15 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Kees Cook, Gustavo A. R. Silva, Philip Li, Linus Torvalds,
	Gustavo A. R. Silva, Nick Desaulniers, Linux Kbuild mailing list,
	Linux Kernel Mailing List, clang-built-linux, linux-hardening

On Wed, Aug 18, 2021 at 8:23 AM Nathan Chancellor <nathan@kernel.org> wrote:
>
> On 8/17/2021 4:06 PM, Kees Cook wrote:
> > On Tue, Aug 17, 2021 at 04:33:25PM -0500, Gustavo A. R. Silva wrote:
> >>
> >>
> >> On 8/17/21 16:17, Masahiro Yamada wrote:
> >>> On Wed, Aug 18, 2021 at 3:25 AM Nathan Chancellor <nathan@kernel.org> wrote:
> >>>>
> >>>> On 8/17/2021 11:03 AM, Kees Cook wrote:
> >>>>> On Mon, Aug 16, 2021 at 09:55:28PM -0700, Nathan Chancellor wrote:
> >>>>>> If you/Gustavo would prefer, I can upgrade that check to
> >>>>>>
> >>>>>> ifneq ($(call cc-option, -Wunreachable-code-fallthrough),)
> >>>>>>
> >>>>>> I was just trying to save a call to the compiler, as that is more expensive
> >>>>>> than a shell test call.
> >>>>>
> >>>>> I prefer the option test -- this means no changes are needed on the
> >>>>> kernel build side if it ever finds itself backported to earlier versions
> >>>>> (and it handles the current case of "14" not meaning "absolute latest").
> >>>>>
> >>>>> More specifically, I think you want this (untested):
> >>>>
> >>>> That should work but since -Wunreachable-code-fallthrough is off by
> >>>> default, I did not really see a reason to include it in KBUILD_CFLAGS. I
> >>>> do not have a strong opinion though, your version is smaller than mine
> >>>> is so we can just go with that. I'll defer to Gustavo on it since he has
> >>>> put in all of the work cleaning up the warnings.
> >>>
> >>>
> >>>
> >>> https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
> >>>
> >>>     did two things:
> >>>
> >>>   (1) Change the -Wimplicit-fallthrough behavior so that it fits
> >>>        to our use in the kernel
> >>>
> >>>   (2) Add a new option -Wunreachable-code-fallthrough
> >>>        that works like the previous -Wimplicit-fallthrough of
> >>>        Clang <= 13.0.0
> >>>
> >>>
> >>> They are separate things.
> >>>
> >>> Checking the presence of -Wunreachable-code-fallthrough
> >>> does not make sense since we are only interested in (1) here.
> >>>
> >>> So, checking the Clang version is sensible and matches
> >>> the explanation in the comment block.
> >
> > I thought one of the problems (which is quickly draining away) that
> > needed to be solved here is that some Clang trunk builds (that report
> > as version 14) don't yet have support for -Wunreachable-code-fallthrough
> > since they aren't new enough.
>
> Philip, how often is the kernel test robot's clang version rebuilt?
> Would it be possible to bump it to latest ToT or at least
> 9ed4a94d6451046a51ef393cd62f00710820a7e8 so that we do not get bit by
> this warning when we go to enable this flag?
>
> I do not know of any other CI aside from ours that is testing with tip
> of tree clang and ours should already have a clang that includes my
> patch since it comes from apt.llvm.org.
>
> >>> # Warn about unmarked fall-throughs in switch statement.
> >>> # Clang prior to 14.0.0 warned on unreachable fallthroughs with
> >>> # -Wimplicit-fallthrough, which is unacceptable due to IS_ENABLED().
> >>> # https://bugs.llvm.org/show_bug.cgi?id=51094
> >>> ifeq ($(firstword $(sort $(CONFIG_CLANG_VERSION) 140000)),140000)
> >>> KBUILD_CFLAGS += -Wimplicit-fallthrough
> >>> endif
>
> Very clever and nifty trick! I have verified that it works for clang 13
> and 14 along with a theoretical clang 15. Gustavo, feel free to stick a


I am not the inventor of this code, though :-)

I mimicked the code in Buildroot:
https://github.com/buildroot/buildroot/blob/2021.05/Makefile#L104





-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17 23:23                 ` Nathan Chancellor
  2021-08-17 23:40                   ` Gustavo A. R. Silva
  2021-08-18  4:15                   ` Masahiro Yamada
@ 2021-08-18  4:27                   ` Philip Li
  2021-08-18  4:45                     ` Gustavo A. R. Silva
  2021-08-18 12:12                   ` Mark Brown
  3 siblings, 1 reply; 13+ messages in thread
From: Philip Li @ 2021-08-18  4:27 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Kees Cook, Gustavo A. R. Silva, Masahiro Yamada, Linus Torvalds,
	Gustavo A. R. Silva, Nick Desaulniers, Linux Kbuild mailing list,
	Linux Kernel Mailing List, clang-built-linux, linux-hardening

On Tue, Aug 17, 2021 at 04:23:41PM -0700, Nathan Chancellor wrote:
> On 8/17/2021 4:06 PM, Kees Cook wrote:
> > On Tue, Aug 17, 2021 at 04:33:25PM -0500, Gustavo A. R. Silva wrote:
> > > 
> > > 
> > > On 8/17/21 16:17, Masahiro Yamada wrote:
> > > > On Wed, Aug 18, 2021 at 3:25 AM Nathan Chancellor <nathan@kernel.org> wrote:
> > > > > 
> > > > > On 8/17/2021 11:03 AM, Kees Cook wrote:
> > > > > > On Mon, Aug 16, 2021 at 09:55:28PM -0700, Nathan Chancellor wrote:
> > > > > > > If you/Gustavo would prefer, I can upgrade that check to
> > > > > > > 
> > > > > > > ifneq ($(call cc-option, -Wunreachable-code-fallthrough),)
> > > > > > > 
> > > > > > > I was just trying to save a call to the compiler, as that is more expensive
> > > > > > > than a shell test call.
> > > > > > 
> > > > > > I prefer the option test -- this means no changes are needed on the
> > > > > > kernel build side if it ever finds itself backported to earlier versions
> > > > > > (and it handles the current case of "14" not meaning "absolute latest").
> > > > > > 
> > > > > > More specifically, I think you want this (untested):
> > > > > 
> > > > > That should work but since -Wunreachable-code-fallthrough is off by
> > > > > default, I did not really see a reason to include it in KBUILD_CFLAGS. I
> > > > > do not have a strong opinion though, your version is smaller than mine
> > > > > is so we can just go with that. I'll defer to Gustavo on it since he has
> > > > > put in all of the work cleaning up the warnings.
> > > > 
> > > > 
> > > > 
> > > > https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
> > > > 
> > > >     did two things:
> > > > 
> > > >   (1) Change the -Wimplicit-fallthrough behavior so that it fits
> > > >        to our use in the kernel
> > > > 
> > > >   (2) Add a new option -Wunreachable-code-fallthrough
> > > >        that works like the previous -Wimplicit-fallthrough of
> > > >        Clang <= 13.0.0
> > > > 
> > > > 
> > > > They are separate things.
> > > > 
> > > > Checking the presence of -Wunreachable-code-fallthrough
> > > > does not make sense since we are only interested in (1) here.
> > > > 
> > > > So, checking the Clang version is sensible and matches
> > > > the explanation in the comment block.
> > 
> > I thought one of the problems (which is quickly draining away) that
> > needed to be solved here is that some Clang trunk builds (that report
> > as version 14) don't yet have support for -Wunreachable-code-fallthrough
> > since they aren't new enough.
> 
> Philip, how often is the kernel test robot's clang version rebuilt? Would it
> be possible to bump it to latest ToT or at least
> 9ed4a94d6451046a51ef393cd62f00710820a7e8 so that we do not get bit by this
> warning when we go to enable this flag?
Got it, currently we do upgrade in weekly cadence (but it may fall behind sometimes),
and the one we use now is clang version 14.0.0 (https://github.com/llvm/llvm-project 
2c6448cdc2f68f8c28fd0bd9404182b81306e6e6)

We will ugrade to the head of llvm-project master today.

Thanks

> 
> I do not know of any other CI aside from ours that is testing with tip of
> tree clang and ours should already have a clang that includes my patch since
> it comes from apt.llvm.org.
> 
> > > > # Warn about unmarked fall-throughs in switch statement.
> > > > # Clang prior to 14.0.0 warned on unreachable fallthroughs with
> > > > # -Wimplicit-fallthrough, which is unacceptable due to IS_ENABLED().
> > > > # https://bugs.llvm.org/show_bug.cgi?id=51094
> > > > ifeq ($(firstword $(sort $(CONFIG_CLANG_VERSION) 140000)),140000)
> > > > KBUILD_CFLAGS += -Wimplicit-fallthrough
> > > > endif
> 
> Very clever and nifty trick! I have verified that it works for clang 13 and
> 14 along with a theoretical clang 15. Gustavo, feel free to stick a
> 
> Reviewed-by: Nathan Chancellor <nathan@kernel.org>
> Tested-by: Nathan Chancellor <nathan@kernel.org>
> 
> if you so desire.
> 
> > > > 
> > > > The $(sort ...) is alphabetical sort, not numeric sort.
> > > > It works for us because the minimum Clang version is 10.0.1
> > > > (that is CONFIG_CLANG_VERSION is always 6-digit)
> > > > 
> > > > It will break when Clang version 100.0.0 is released.
> > > > 
> > > > But, before that, we will raise the minimum supported clang version,
> > > > and this conditional will go away.
> > 
> > If a version test is preferred, cool; this is a nice trick. :)
> > 
> > > I like this. :)
> > > 
> > > I'm going to make the 0-day robot test it in my tree, first.
> > 
> > Sounds good to me!
> > 

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-18  4:27                   ` Philip Li
@ 2021-08-18  4:45                     ` Gustavo A. R. Silva
  2021-08-18  7:31                       ` Philip Li
  0 siblings, 1 reply; 13+ messages in thread
From: Gustavo A. R. Silva @ 2021-08-18  4:45 UTC (permalink / raw)
  To: Philip Li, Nathan Chancellor
  Cc: Kees Cook, Masahiro Yamada, Linus Torvalds, Gustavo A. R. Silva,
	Nick Desaulniers, Linux Kbuild mailing list,
	Linux Kernel Mailing List, clang-built-linux, linux-hardening



On 8/17/21 23:27, Philip Li wrote:

>> Philip, how often is the kernel test robot's clang version rebuilt? Would it
>> be possible to bump it to latest ToT or at least
>> 9ed4a94d6451046a51ef393cd62f00710820a7e8 so that we do not get bit by this
>> warning when we go to enable this flag?
> Got it, currently we do upgrade in weekly cadence (but it may fall behind sometimes),
> and the one we use now is clang version 14.0.0 (https://github.com/llvm/llvm-project 
> 2c6448cdc2f68f8c28fd0bd9404182b81306e6e6)
> 
> We will ugrade to the head of llvm-project master today.

Thanks, Philip. We really appreciate it.
--
Gustavo

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-18  4:45                     ` Gustavo A. R. Silva
@ 2021-08-18  7:31                       ` Philip Li
  0 siblings, 0 replies; 13+ messages in thread
From: Philip Li @ 2021-08-18  7:31 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Nathan Chancellor, Kees Cook, Masahiro Yamada, Linus Torvalds,
	Gustavo A. R. Silva, Nick Desaulniers, Linux Kbuild mailing list,
	Linux Kernel Mailing List, clang-built-linux, linux-hardening

On Tue, Aug 17, 2021 at 11:45:48PM -0500, Gustavo A. R. Silva wrote:
> 
> 
> On 8/17/21 23:27, Philip Li wrote:
> 
> >> Philip, how often is the kernel test robot's clang version rebuilt? Would it
> >> be possible to bump it to latest ToT or at least
> >> 9ed4a94d6451046a51ef393cd62f00710820a7e8 so that we do not get bit by this
> >> warning when we go to enable this flag?
> > Got it, currently we do upgrade in weekly cadence (but it may fall behind sometimes),
> > and the one we use now is clang version 14.0.0 (https://github.com/llvm/llvm-project 
> > 2c6448cdc2f68f8c28fd0bd9404182b81306e6e6)
> > 
> > We will ugrade to the head of llvm-project master today.
> 
> Thanks, Philip. We really appreciate it.
you are welcome. Per the upgrade in this noon. Now we start to use below commit to
do further clang build test (which is after the required 9ed4a94d6451)

commit d2b574a4dea5b718e4386bf2e26af0126e5978ce
Author: Marco Elver <elver@google.com>
Date:   Tue Aug 17 16:54:07 2021 +0200

    tsan: test: Initialize all fields of Params struct

Thanks

> --
> Gustavo

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17 23:23                 ` Nathan Chancellor
                                     ` (2 preceding siblings ...)
  2021-08-18  4:27                   ` Philip Li
@ 2021-08-18 12:12                   ` Mark Brown
  3 siblings, 0 replies; 13+ messages in thread
From: Mark Brown @ 2021-08-18 12:12 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Kees Cook, Gustavo A. R. Silva, Philip Li, Masahiro Yamada,
	Linus Torvalds, Gustavo A. R. Silva, Nick Desaulniers,
	Linux Kbuild mailing list, Linux Kernel Mailing List,
	clang-built-linux, linux-hardening

[-- Attachment #1: Type: text/plain, Size: 359 bytes --]

On Tue, Aug 17, 2021 at 04:23:41PM -0700, Nathan Chancellor wrote:

> I do not know of any other CI aside from ours that is testing with tip of
> tree clang and ours should already have a clang that includes my patch since
> it comes from apt.llvm.org.

FWIW we have some testing internally at Arm but that's building from
source so it's not an issue for us.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17 21:17           ` Masahiro Yamada
  2021-08-17 21:33             ` Gustavo A. R. Silva
@ 2021-08-25 21:09             ` Nick Desaulniers
  1 sibling, 0 replies; 13+ messages in thread
From: Nick Desaulniers @ 2021-08-25 21:09 UTC (permalink / raw)
  To: Masahiro Yamada, Gustavo A. R. Silva, Nathan Chancellor
  Cc: Kees Cook, Linus Torvalds, Linux Kbuild mailing list,
	Linux Kernel Mailing List, clang-built-linux, linux-hardening

On Tue, Aug 17, 2021 at 2:18 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> On Wed, Aug 18, 2021 at 3:25 AM Nathan Chancellor <nathan@kernel.org> wrote:
> >
> > On 8/17/2021 11:03 AM, Kees Cook wrote:
> > > On Mon, Aug 16, 2021 at 09:55:28PM -0700, Nathan Chancellor wrote:
> > >> If you/Gustavo would prefer, I can upgrade that check to
> > >>
> > >> ifneq ($(call cc-option, -Wunreachable-code-fallthrough),)
> > >>
> > >> I was just trying to save a call to the compiler, as that is more expensive
> > >> than a shell test call.
> > >
> > > I prefer the option test -- this means no changes are needed on the
> > > kernel build side if it ever finds itself backported to earlier versions
> > > (and it handles the current case of "14" not meaning "absolute latest").
> > >
> > > More specifically, I think you want this (untested):
> >
> > That should work but since -Wunreachable-code-fallthrough is off by
> > default, I did not really see a reason to include it in KBUILD_CFLAGS. I
> > do not have a strong opinion though, your version is smaller than mine
> > is so we can just go with that. I'll defer to Gustavo on it since he has
> > put in all of the work cleaning up the warnings.
>
>
>
> https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
>
>    did two things:
>
>  (1) Change the -Wimplicit-fallthrough behavior so that it fits
>       to our use in the kernel
>
>  (2) Add a new option -Wunreachable-code-fallthrough
>       that works like the previous -Wimplicit-fallthrough of
>       Clang <= 13.0.0
>
>
> They are separate things.
>
> Checking the presence of -Wunreachable-code-fallthrough
> does not make sense since we are only interested in (1) here.
>
>
>
> So, checking the Clang version is sensible and matches
> the explanation in the comment block.
>
>
> Moreover, using $(shell test ...) is less expensive than cc-option.
>
>
> If you want to make it even faster, you can use only
> built-in functions, like this:
>
>
> # Warn about unmarked fall-throughs in switch statement.
> # Clang prior to 14.0.0 warned on unreachable fallthroughs with
> # -Wimplicit-fallthrough, which is unacceptable due to IS_ENABLED().
> # https://bugs.llvm.org/show_bug.cgi?id=51094
> ifeq ($(firstword $(sort $(CONFIG_CLANG_VERSION) 140000)),140000)
> KBUILD_CFLAGS += -Wimplicit-fallthrough
> endif
>
>
>
> The $(sort ...) is alphabetical sort, not numeric sort.
> It works for us because the minimum Clang version is 10.0.1
> (that is CONFIG_CLANG_VERSION is always 6-digit)
>
> It will break when Clang version 100.0.0 is released.
>
> But, before that, we will raise the minimum supported clang version,
> and this conditional will go away.

I'd much rather pay the cost of cc-option to have a more precise
check; Linus is right: when I upgrade AOSP's fork of LLVM, it may not
be the fully released version of clang-14 though we have already moved
the version numbers upstream to clang-14.  I think we should strive to
prefer feature tests over version tests, which are brittle.

```
# Clang would warn about unreachable fall throughs until clang-14.
ifdef CONFIG_CC_IS_CLANG
ifneq ($(call cc-option,-Wunreachable-code-fallthrough),)
KBUILD_CFLAGS += -Wimplicit-fallthrough
endif
endif
```

Is precisely what we want.
-- 
Thanks,
~Nick Desaulniers

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

end of thread, other threads:[~2021-08-25 21:10 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20210817005624.1455428-1-nathan@kernel.org>
     [not found] ` <80fa539a-b767-76ed-dafa-4d8d1a6b063e@kernel.org>
     [not found]   ` <CAHk-=wgFXOf9OUh3+vmWjhp1PC47RVsUkL0NszBxSWhbGzx4tw@mail.gmail.com>
     [not found]     ` <5c856f36-69a7-e274-f72a-c3aef195adeb@kernel.org>
2021-08-17 18:03       ` [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+ Kees Cook
2021-08-17 18:25         ` Nathan Chancellor
2021-08-17 21:17           ` Masahiro Yamada
2021-08-17 21:33             ` Gustavo A. R. Silva
2021-08-17 23:06               ` Kees Cook
2021-08-17 23:23                 ` Nathan Chancellor
2021-08-17 23:40                   ` Gustavo A. R. Silva
2021-08-18  4:15                   ` Masahiro Yamada
2021-08-18  4:27                   ` Philip Li
2021-08-18  4:45                     ` Gustavo A. R. Silva
2021-08-18  7:31                       ` Philip Li
2021-08-18 12:12                   ` Mark Brown
2021-08-25 21:09             ` Nick Desaulniers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).