All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
@ 2021-08-17  0:56 Nathan Chancellor
  2021-08-17  4:16   ` kernel test robot
  2021-08-17  4:20 ` Nathan Chancellor
  0 siblings, 2 replies; 19+ messages in thread
From: Nathan Chancellor @ 2021-08-17  0:56 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Linus Torvalds
  Cc: Kees Cook, Nick Desaulniers, Masahiro Yamada, linux-kbuild,
	linux-kernel, clang-built-linux, 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.

Link: https://github.com/ClangBuiltLinux/linux/issues/236
Link: https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
 Makefile | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index c19d1638da25..91a4a80409e1 100644
--- a/Makefile
+++ b/Makefile
@@ -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
-- 
2.33.0


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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17  0:56 [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+ Nathan Chancellor
@ 2021-08-17  4:16   ` kernel test robot
  2021-08-17  4:20 ` Nathan Chancellor
  1 sibling, 0 replies; 19+ messages in thread
From: kernel test robot @ 2021-08-17  4:16 UTC (permalink / raw)
  To: Nathan Chancellor, Gustavo A. R. Silva, Linus Torvalds
  Cc: clang-built-linux, kbuild-all, LKML, Kees Cook, Nick Desaulniers,
	Masahiro Yamada, linux-kbuild, Nathan Chancellor

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

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
Reported-by: kernel test robot <lkp@intel.com>

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@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 34833 bytes --]

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
@ 2021-08-17  4:16   ` kernel test robot
  0 siblings, 0 replies; 19+ messages in thread
From: kernel test robot @ 2021-08-17  4:16 UTC (permalink / raw)
  To: kbuild-all

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

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
Reported-by: kernel test robot <lkp@intel.com>

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

[-- Attachment #2: config.gz --]
[-- Type: application/gzip, Size: 34833 bytes --]

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17  0:56 [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+ Nathan Chancellor
  2021-08-17  4:16   ` kernel test robot
@ 2021-08-17  4:20 ` Nathan Chancellor
  2021-08-17  4:37   ` Linus Torvalds
  1 sibling, 1 reply; 19+ messages in thread
From: Nathan Chancellor @ 2021-08-17  4:20 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Linus Torvalds
  Cc: Kees Cook, Nick Desaulniers, Masahiro Yamada, linux-kbuild,
	linux-kernel, clang-built-linux


On 8/16/2021 5:56 PM, Nathan Chancellor wrote:
> 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.
> 
> Link: https://github.com/ClangBuiltLinux/linux/issues/236
> Link: https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>
> ---
>   Makefile | 10 ++++++++--
>   1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index c19d1638da25..91a4a80409e1 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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
> 

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

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

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

On Mon, Aug 16, 2021 at 6:20 PM Nathan Chancellor <nathan@kernel.org> wrote:
>
> 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

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

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

On 8/16/2021 9:37 PM, Linus Torvalds wrote:
> On Mon, Aug 16, 2021 at 6:20 PM Nathan Chancellor <nathan@kernel.org> wrote:
>>
>> 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 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.

> 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

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

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17  4:55     ` Nathan Chancellor
@ 2021-08-17 18:03       ` Kees Cook
  2021-08-17 18:25         ` Nathan Chancellor
  0 siblings, 1 reply; 19+ 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] 19+ messages in thread

* Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+
  2021-08-17 18:03       ` Kees Cook
@ 2021-08-17 18:25         ` Nathan Chancellor
  2021-08-17 21:17           ` Masahiro Yamada
  0 siblings, 1 reply; 19+ 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] 19+ 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; 19+ 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] 19+ 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; 19+ 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] 19+ 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; 19+ 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] 19+ 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; 19+ 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] 19+ 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; 19+ 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] 19+ 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; 19+ 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] 19+ 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; 19+ 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] 19+ 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; 19+ 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] 19+ 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; 19+ 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] 19+ 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; 19+ 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] 19+ 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; 19+ 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] 19+ messages in thread

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

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-17  0:56 [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+ Nathan Chancellor
2021-08-17  4:16 ` kernel test robot
2021-08-17  4:16   ` kernel test robot
2021-08-17  4:20 ` Nathan Chancellor
2021-08-17  4:37   ` Linus Torvalds
2021-08-17  4:55     ` Nathan Chancellor
2021-08-17 18:03       ` 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 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.