rust: allow to use INIT_STACK_ALL_ZERO

Message ID 20230210172203.101331-1-andrea.righi@canonical.com
State New
Headers
Series rust: allow to use INIT_STACK_ALL_ZERO |

Commit Message

Andrea Righi Feb. 10, 2023, 5:22 p.m. UTC
  With CONFIG_INIT_STACK_ALL_ZERO enabled, bindgen is passing
-ftrivial-auto-var-init=zero to clang, that triggers the following:

  error: '-ftrivial-auto-var-init=zero' hasn't been enabled; enable it at your own peril for benchmarking purpose only with '-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang'

This flag should be dropped in clang-17, but at the moment it seems more
reasonable to add it to the bindgen CFLAGS to prevent the error above.

In this way we can enable CONFIG_INIT_STACK_ALL_ZERO with CONFIG_RUST
without triggering any build error.

Link: https://reviews.llvm.org/D125142
Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
---
 rust/Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Nathan Chancellor Feb. 10, 2023, 5:39 p.m. UTC | #1
On Fri, Feb 10, 2023 at 06:22:03PM +0100, Andrea Righi wrote:
> With CONFIG_INIT_STACK_ALL_ZERO enabled, bindgen is passing
> -ftrivial-auto-var-init=zero to clang, that triggers the following:
> 
>   error: '-ftrivial-auto-var-init=zero' hasn't been enabled; enable it at your own peril for benchmarking purpose only with '-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang'
> 
> This flag should be dropped in clang-17, but at the moment it seems more
> reasonable to add it to the bindgen CFLAGS to prevent the error above.
> 
> In this way we can enable CONFIG_INIT_STACK_ALL_ZERO with CONFIG_RUST
> without triggering any build error.
> 
> Link: https://reviews.llvm.org/D125142
> Signed-off-by: Andrea Righi <andrea.righi@canonical.com>

If this went via the kbuild tree, would we be able to use
$(CC_AUTO_VAR_INIT_ZERO_ENABLER)?

https://git.kernel.org/masahiroy/linux-kbuild/c/4e3feaad6ff8a7a57e3bf3308a93c93e3a2e17a6

Although, I suppose this could be a problem if the version of clang
being used to build the kernel does not support this flag while the
version of libclang in bindgen requires it?

> ---
>  rust/Makefile | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/rust/Makefile b/rust/Makefile
> index ff70c4c916f8..5e26db07a1dc 100644
> --- a/rust/Makefile
> +++ b/rust/Makefile
> @@ -267,7 +267,8 @@ BINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
>  # All warnings are inhibited since GCC builds are very experimental,
>  # many GCC warnings are not supported by Clang, they may only appear in
>  # some configurations, with new GCC versions, etc.
> -bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
> +bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET) \
> +			-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
>  
>  bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
>  	$(bindgen_extra_c_flags)
> -- 
> 2.37.2
>
  
Andrea Righi Feb. 10, 2023, 9:37 p.m. UTC | #2
On Fri, Feb 10, 2023 at 10:39:27AM -0700, Nathan Chancellor wrote:
> On Fri, Feb 10, 2023 at 06:22:03PM +0100, Andrea Righi wrote:
> > With CONFIG_INIT_STACK_ALL_ZERO enabled, bindgen is passing
> > -ftrivial-auto-var-init=zero to clang, that triggers the following:
> > 
> >   error: '-ftrivial-auto-var-init=zero' hasn't been enabled; enable it at your own peril for benchmarking purpose only with '-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang'
> > 
> > This flag should be dropped in clang-17, but at the moment it seems more
> > reasonable to add it to the bindgen CFLAGS to prevent the error above.
> > 
> > In this way we can enable CONFIG_INIT_STACK_ALL_ZERO with CONFIG_RUST
> > without triggering any build error.
> > 
> > Link: https://reviews.llvm.org/D125142
> > Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
> 
> If this went via the kbuild tree, would we be able to use
> $(CC_AUTO_VAR_INIT_ZERO_ENABLER)?
> 
> https://git.kernel.org/masahiroy/linux-kbuild/c/4e3feaad6ff8a7a57e3bf3308a93c93e3a2e17a6
> 
> Although, I suppose this could be a problem if the version of clang
> being used to build the kernel does not support this flag while the
> version of libclang in bindgen requires it?

I didn't have much success with CC_AUTO_VAR_INIT_ZERO_ENABLER, but maybe
a better way is to check if the version of libclang used by bindgen is
less than 17, considering that is extra clang option is supposed to be
dropped in clang-17:

https://github.com/llvm/llvm-project/issues/44842

I'll send a v2, unless we find a better way to support this auto var
zero-init feature.

-Andrea
  

Patch

diff --git a/rust/Makefile b/rust/Makefile
index ff70c4c916f8..5e26db07a1dc 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -267,7 +267,8 @@  BINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
 # All warnings are inhibited since GCC builds are very experimental,
 # many GCC warnings are not supported by Clang, they may only appear in
 # some configurations, with new GCC versions, etc.
-bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
+bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET) \
+			-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
 
 bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
 	$(bindgen_extra_c_flags)