Skip to contentSkip to conversation

kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+

PATCH In review Nathan Chancellor 9 participants 18 messages Aug 17, 2021 – Aug 25, 2021 Kbuild

lore
Share & tools
lore

Nathan Chancellor

author maintainer
#0 lore raw

PATCH 1/1 kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+

Clang prior to 14.0.0 warns when a fallthrough annotation is in an unreachable spot, which can occur when IS_ENABLED(CONFIG_...) in a conditional statement prior to the fallthrough annotation such as

if (IS_ENABLED(CONFIG_...))

      break;

fallthrough;

which to clang looks like

break;
fallthrough;

if CONFIG_... is enabled due to the control flow graph. Example of the warning in practice:

sound/core/pcm_native.c:3812:3: warning: fallthrough annotation in unreachable code [-Wimplicit-fallthrough]

                fallthrough;
                ^

Warning on unreachable annotations makes the warning too noisy and pointless for the kernel due to the nature of guarding some code on configuration options so it was disabled in commit d936eb238744 ("Revert "Makefile: Enable -Wimplicit-fallthrough for Clang"").

This has been resolved in clang 14.0.0 by moving the unreachable warning to its own flag under -Wunreachable-code, which the kernel will most likely never enable due to situations like this.

Enable -Wimplicit-fallthrough for clang 14+ so that issues such as the one in commit 652b44453ea9 ("habanalabs/gaudi: fix missing code in ECC handling") can be caught before they enter the tree.

---
 Makefile | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
Makefile +8 -2
index c19d1638da25..91a4a80409e1 100644
@@ -797,11 +797,17 @@ 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.
+# 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 ($(shell test $(CONFIG_CLANG_VERSION) -ge 140000; echo $$?),0)
+KBUILD_CFLAGS += -Wimplicit-fallthrough
+endif
 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
 

base-commit: a2824f19e6065a0d3735acd9fe7155b104e7edf5

Signature · 1 line
2.33.0
#1 lore raw
I love your patch! Perhaps something to improve:

Hi Nathan,

I love your patch! Perhaps something to improve:

[auto build test WARNING on a2824f19e6065a0d3735acd9fe7155b104e7edf5]

url:    https://github.com/0day-ci/linux/commits/Nathan-Chancellor/kbuild-Enable-Wimplicit-fallthrough-for-clang-14-0-0/20210817-085926
base:   a2824f19e6065a0d3735acd9fe7155b104e7edf5
config: mips-randconfig-r003-20210816 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 2c6448cdc2f68f8c28fd0bd9404182b81306e6e6)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install mips cross compiling tool for clang build
# apt-get install binutils-mips-linux-gnu
# https://github.com/0day-ci/linux/commit/358205a0573f713b532173c9cf3c9efa052dc9d0
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Nathan-Chancellor/kbuild-Enable-Wimplicit-fallthrough-for-clang-14-0-0/20210817-085926 git checkout 358205a0573f713b532173c9cf3c9efa052dc9d0
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=mips

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

sound/core/pcm_native.c:273:18: warning: variable 'old_mask' set but not used [-Wunused-but-set-variable]

           struct snd_mask old_mask;
                           ^
   sound/core/pcm_native.c:309:22: warning: variable 'old_interval' set but not used [-Wunused-but-set-variable]
           struct snd_interval old_interval;
                               ^
   sound/core/pcm_native.c:350:22: warning: variable 'old_interval' set but not used [-Wunused-but-set-variable]
           struct snd_interval old_interval;
                               ^
   sound/core/pcm_native.c:349:18: warning: variable 'old_mask' set but not used [-Wunused-but-set-variable]
           struct snd_mask old_mask;
                           ^
   sound/core/pcm_native.c:633:18: warning: variable 'old_mask' set but not used [-Wunused-but-set-variable]
           struct snd_mask old_mask;
                           ^
   sound/core/pcm_native.c:634:22: warning: variable 'old_interval' set but not used [-Wunused-but-set-variable]
           struct snd_interval old_interval;
                               ^
>> sound/core/pcm_native.c:3815:3: warning: fallthrough annotation in unreachable code [-Wimplicit-fallthrough]
                   fallthrough;
                   ^
   include/linux/compiler_attributes.h:210:41: note: expanded from macro 'fallthrough'
   # define fallthrough                    __attribute__((__fallthrough__))
                                           ^
   sound/core/pcm_native.c:3823:3: warning: fallthrough annotation in unreachable code [-Wimplicit-fallthrough]
                   fallthrough;
                   ^
   include/linux/compiler_attributes.h:210:41: note: expanded from macro 'fallthrough'
   # define fallthrough                    __attribute__((__fallthrough__))
                                           ^
   8 warnings generated.

vim +3815 sound/core/pcm_native.c

e88e8ae639a490 Takashi Iwai        2006-04-28  3798  
^1da177e4c3f41 Linus Torvalds      2005-04-16  3799  static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
^1da177e4c3f41 Linus Torvalds      2005-04-16  3800  {
877211f5e1b119 Takashi Iwai        2005-11-17  3801  	struct snd_pcm_file * pcm_file;
877211f5e1b119 Takashi Iwai        2005-11-17  3802  	struct snd_pcm_substream *substream;	
^1da177e4c3f41 Linus Torvalds      2005-04-16  3803  	unsigned long offset;
^1da177e4c3f41 Linus Torvalds      2005-04-16  3804  	
^1da177e4c3f41 Linus Torvalds      2005-04-16  3805  	pcm_file = file->private_data;
^1da177e4c3f41 Linus Torvalds      2005-04-16  3806  	substream = pcm_file->substream;
7eaa943c8ed8e9 Takashi Iwai        2008-08-08  3807  	if (PCM_RUNTIME_CHECK(substream))
7eaa943c8ed8e9 Takashi Iwai        2008-08-08  3808  		return -ENXIO;
^1da177e4c3f41 Linus Torvalds      2005-04-16  3809  
^1da177e4c3f41 Linus Torvalds      2005-04-16  3810  	offset = area->vm_pgoff << PAGE_SHIFT;
^1da177e4c3f41 Linus Torvalds      2005-04-16  3811  	switch (offset) {
80fe7430c70859 Arnd Bergmann       2018-04-24  3812  	case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD:
80fe7430c70859 Arnd Bergmann       2018-04-24  3813  		if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
80fe7430c70859 Arnd Bergmann       2018-04-24  3814  			return -ENXIO;
c0dbbdad4e11f8 Gustavo A. R. Silva 2020-07-08 @3815  		fallthrough;
80fe7430c70859 Arnd Bergmann       2018-04-24  3816  	case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
42f945970af9df Takashi Iwai        2017-06-19  3817  		if (!pcm_status_mmap_allowed(pcm_file))
^1da177e4c3f41 Linus Torvalds      2005-04-16  3818  			return -ENXIO;
^1da177e4c3f41 Linus Torvalds      2005-04-16  3819  		return snd_pcm_mmap_status(substream, file, area);
80fe7430c70859 Arnd Bergmann       2018-04-24  3820  	case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD:
80fe7430c70859 Arnd Bergmann       2018-04-24  3821  		if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
80fe7430c70859 Arnd Bergmann       2018-04-24  3822  			return -ENXIO;
c0dbbdad4e11f8 Gustavo A. R. Silva 2020-07-08  3823  		fallthrough;
80fe7430c70859 Arnd Bergmann       2018-04-24  3824  	case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
b602aa8eb1a0f5 Takashi Iwai        2017-06-27  3825  		if (!pcm_control_mmap_allowed(pcm_file))
^1da177e4c3f41 Linus Torvalds      2005-04-16  3826  			return -ENXIO;
^1da177e4c3f41 Linus Torvalds      2005-04-16  3827  		return snd_pcm_mmap_control(substream, file, area);
^1da177e4c3f41 Linus Torvalds      2005-04-16  3828  	default:
^1da177e4c3f41 Linus Torvalds      2005-04-16  3829  		return snd_pcm_mmap_data(substream, file, area);
^1da177e4c3f41 Linus Torvalds      2005-04-16  3830  	}
^1da177e4c3f41 Linus Torvalds      2005-04-16  3831  	return 0;
^1da177e4c3f41 Linus Torvalds      2005-04-16  3832  }

^1da177e4c3f41 Linus Torvalds 2005-04-16 3833

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author maintainer
#2 lore raw
In reply to Nathan Chancellor
Quoted 66 lines · Nathan Chancellor

Clang prior to 14.0.0 warns when a fallthrough annotation is in an unreachable spot, which can occur when IS_ENABLED(CONFIG_...) in a conditional statement prior to the fallthrough annotation such as

   if (IS_ENABLED(CONFIG_...))
       break;
   fallthrough;

which to clang looks like

   break;
   fallthrough;

if CONFIG_... is enabled due to the control flow graph. Example of the warning in practice:

sound/core/pcm_native.c:3812:3: warning: fallthrough annotation in unreachable code [-Wimplicit-fallthrough]

                 fallthrough;
                 ^

Warning on unreachable annotations makes the warning too noisy and pointless for the kernel due to the nature of guarding some code on configuration options so it was disabled in commit d936eb238744 ("Revert "Makefile: Enable -Wimplicit-fallthrough for Clang"").

This has been resolved in clang 14.0.0 by moving the unreachable warning to its own flag under -Wunreachable-code, which the kernel will most likely never enable due to situations like this.

Enable -Wimplicit-fallthrough for clang 14+ so that issues such as the one in commit 652b44453ea9 ("habanalabs/gaudi: fix missing code in ECC handling") can be caught before they enter the tree.

---
  Makefile | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
Makefile +8 -2
index c19d1638da25..91a4a80409e1 100644
@@ -797,11 +797,17 @@ 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.
+# 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 ($(shell test $(CONFIG_CLANG_VERSION) -ge 140000; echo $$?),0)
+KBUILD_CFLAGS += -Wimplicit-fallthrough
+endif
  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
 
Quoted 2 lines

base-commit: a2824f19e6065a0d3735acd9fe7155b104e7edf5

Please do not apply this patch in its current form, as it does not properly credit Gustavo for all of the hard work he has done for enabling this warning.

Additionally, there should be some time for the CI systems to update their clang-14 builds, as the recent 0day report shows.

Cheers,
Nathan

#3 lore raw
Quoted 3 lines · Nathan Chancellor

Additionally, there should be some time for the CI systems to update their clang-14 builds, as the recent 0day report shows.

What?

No, the 0day report shows that the patch is buggy, and that the

  ifeq ($(shell test $(CONFIG_CLANG_VERSION) -ge 140000; echo $$?),0)

clearly doesn't work at all, since the flag is enabled on those systems with old clang versions.

Alternatively, the test works, but the 140000 version is not enough.

So no. This patch is simply completely wrong, and doesn't fix the problem with Clang's buggy -Wimplicit-fallthrough flag.

Linus

author maintainer
#4 lore raw
Quoted 7 lines · Linus Torvalds
Quoted 3 lines · Nathan Chancellor

Additionally, there should be some time for the CI systems to update their clang-14 builds, as the recent 0day report shows.

What?

Quoted 8 lines

No, the 0day report shows that the patch is buggy, and that the

   ifeq ($(shell test $(CONFIG_CLANG_VERSION) -ge 140000; echo $$?),0)

clearly doesn't work at all, since the flag is enabled on those systems with old clang versions.

Alternatively, the test works, but the 140000 version is not enough.

So technically speaking, the 140000 is not enough at this very moment for the fact that there are certain systems that test with clang-14 builds that do not have my clang patch in it yet; however, those systems do update clang regularly (the 0day version is just seven hours old at the time of writing this) so they will have a version that contains my patch shortly, making the check work just fine. We have done this in the past with checks that are gated on clang versions that are in development, with the expectation that if someone is using a development release of clang, they are keeping it up to date so that they get fixes that we push there; otherwise, it is just better to stick with the release branches.

Quoted 2 lines

So no. This patch is simply completely wrong, and doesn't fix the problem with Clang's buggy -Wimplicit-fallthrough flag.

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.

Cheers,
Nathan

#5 lore raw
Quoted 6 lines · Nathan Chancellor

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):

Makefile +3 -3
index b5fd51e68ae9..9845ea50a368 100644
@@ -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
 
Signature · 1 line
Kees Cook
author maintainer
#6 lore raw
Quoted 5 lines · Kees Cook
Quoted 4 lines · Nathan Chancellor

If you/Gustavo would prefer, I can upgrade that check to

ifneq ($(call cc-option, -Wunreachable-code-fallthrough),)

Quoted 8 lines
Quoted 2 lines

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

Quoted 20 lines
Makefile +3 -3
index b5fd51e68ae9..9845ea50a368 100644
@@ -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
 
#7 lore raw
Quoted 14 lines · Nathan Chancellor
Quoted 12 lines · Kees Cook
Quoted 6 lines · Nathan Chancellor

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").

Quoted 7 lines
Quoted 1 line

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.

Quoted 23 lines

Cheers,
Nathan

Quoted 20 lines
Makefile +3 -3
index b5fd51e68ae9..9845ea50a368 100644
@@ -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
 
Signature · 2 lines
Best Regards
Masahiro Yamada
#8 lore raw
Quoted 65 lines · Masahiro Yamada
Quoted 14 lines · Nathan Chancellor
Quoted 12 lines · Kees Cook
Quoted 6 lines · Nathan Chancellor

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").

Quoted 7 lines
Quoted 1 line

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

Quoted 8 lines

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

#9 lore raw
Quoted 40 lines · Gustavo A. R. Silva
Quoted 37 lines · Masahiro Yamada
Quoted 14 lines · Nathan Chancellor
Quoted 12 lines · Kees Cook
Quoted 6 lines · Nathan Chancellor

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").

Quoted 7 lines
Quoted 1 line

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

Quoted 7 lines
Quoted 7 lines

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.

Quoted 8 lines
Quoted 8 lines

# 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

Quoted 8 lines
Quoted 8 lines

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. :)

Quoted 3 lines

I like this. :)

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

Sounds good to me!

Signature · 1 line
Kees Cook
author maintainer Reviewed-by Tested-by
#10 lore raw
Quoted 46 lines · Kees Cook
Quoted 45 lines · Gustavo A. R. Silva
Quoted 42 lines · Masahiro Yamada
Quoted 14 lines · Nathan Chancellor
Quoted 12 lines · Kees Cook
Quoted 6 lines · Nathan Chancellor

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").

Quoted 7 lines
Quoted 1 line

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.

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

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.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

# 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

if you so desire.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines

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. :)

Quoted 3 lines

I like this. :)

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

Sounds good to me!

#11 lore raw
Quoted 8 lines · Nathan Chancellor
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

# 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

Quoted 6 lines

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

if you so desire.

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

Thanks
--
Gustavo

#12 lore raw
In reply to Nathan Chancellor
Quoted 73 lines · Nathan Chancellor
Quoted 46 lines · Kees Cook
Quoted 45 lines · Gustavo A. R. Silva
Quoted 42 lines · Masahiro Yamada
Quoted 14 lines · Nathan Chancellor
Quoted 12 lines · Kees Cook
Quoted 6 lines · Nathan Chancellor

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").

Quoted 7 lines
Quoted 1 line

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.

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

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.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

# 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

Quoted 2 lines

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

Signature · 2 lines
Best Regards
Masahiro Yamada
#13 lore raw
In reply to Nathan Chancellor
Quoted 55 lines · Nathan Chancellor
Quoted 46 lines · Kees Cook
Quoted 45 lines · Gustavo A. R. Silva
Quoted 42 lines · Masahiro Yamada
Quoted 14 lines · Nathan Chancellor
Quoted 12 lines · Kees Cook
Quoted 6 lines · Nathan Chancellor

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").

Quoted 7 lines
Quoted 1 line

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.

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

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.

Quoted 4 lines

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

Quoted 39 lines

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.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

# 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

if you so desire.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines

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. :)

Quoted 3 lines

I like this. :)

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

Sounds good to me!

#14 lore raw
Quoted 9 lines · Philip Li
Quoted 4 lines

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

#15 lore raw
Quoted 8 lines · Gustavo A. R. Silva
Quoted 4 lines · Philip Li
Quoted 4 lines

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?

Quoted 7 lines
Quoted 5 lines

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 <[email protected]>
Date:   Tue Aug 17 16:54:07 2021 +0200

tsan: test: Initialize all fields of Params struct

Thanks

Quoted 2 lines

--
Gustavo

#16 lore raw
In reply to Nathan Chancellor
Quoted 3 lines · Nathan Chancellor

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.

#17 lore raw
In reply to Masahiro Yamada
Quoted 66 lines · Masahiro Yamada
Quoted 14 lines · Nathan Chancellor
Quoted 12 lines · Kees Cook
Quoted 6 lines · Nathan Chancellor

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").

Quoted 7 lines
Quoted 1 line

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

Quoted 8 lines

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.

Signature · 2 lines
Thanks,
~Nick Desaulniers