public inbox for linux-kbuild@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 04/13] rust: relax most deny-level lints to warnings
       [not found] <20240701183625.665574-1-ojeda@kernel.org>
@ 2024-07-01 18:36 ` Miguel Ojeda
  2024-07-01 19:48   ` Björn Roy Baron
  2024-07-04 14:34   ` Finn Behrens
  2024-07-01 18:36 ` [PATCH 05/13] rust: simplify Clippy warning flags set Miguel Ojeda
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 10+ messages in thread
From: Miguel Ojeda @ 2024-07-01 18:36 UTC (permalink / raw)
  To: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, rust-for-linux, linux-kernel,
	patches, Masahiro Yamada, Nathan Chancellor, Nicolas Schier,
	linux-kbuild

Since we are starting to support several Rust toolchains, lints (including
Clippy ones) now may behave differently and lint groups may include
new lints.

Therefore, to maximize the chances a given version works, relax some
deny-level lints to warnings. It may also make our lives a bit easier
while developing new code or refactoring.

To be clear, the requirements for in-tree code are still the same, since
Rust code still needs to be warning-free (patches should be clean under
`WERROR=y`) and the set of lints is not changed.

`unsafe_op_in_unsafe_fn` is left unmodified, i.e. as an error, since
1) it is simple enough that it should not have false positives (unlike
e.g. `rust_2018_idioms`'s `explicit_outlives_requirements`) and 2) it is
becoming the default in the language (warn-by-default in Rust 2024 [1] and
ideally an error later on) and thus it should also be very well tested.

Link: https://github.com/rust-lang/rust/pull/112038 [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 Makefile      | 22 +++++++++++-----------
 rust/Makefile |  4 ++--
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/Makefile b/Makefile
index 4d36f943b3b1..056176a55d63 100644
--- a/Makefile
+++ b/Makefile
@@ -461,17 +461,17 @@ KBUILD_USERLDFLAGS := $(USERLDFLAGS)
 # host programs.
 export rust_common_flags := --edition=2021 \
 			    -Zbinary_dep_depinfo=y \
-			    -Dunsafe_op_in_unsafe_fn -Drust_2018_idioms \
-			    -Dunreachable_pub -Dnon_ascii_idents \
+			    -Dunsafe_op_in_unsafe_fn -Wrust_2018_idioms \
+			    -Wunreachable_pub -Wnon_ascii_idents \
 			    -Wmissing_docs \
-			    -Drustdoc::missing_crate_level_docs \
-			    -Dclippy::correctness -Dclippy::style \
-			    -Dclippy::suspicious -Dclippy::complexity \
-			    -Dclippy::perf \
-			    -Dclippy::let_unit_value -Dclippy::mut_mut \
-			    -Dclippy::needless_bitwise_bool \
-			    -Dclippy::needless_continue \
-			    -Dclippy::no_mangle_with_rust_abi \
+			    -Wrustdoc::missing_crate_level_docs \
+			    -Wclippy::correctness -Wclippy::style \
+			    -Wclippy::suspicious -Wclippy::complexity \
+			    -Wclippy::perf \
+			    -Wclippy::let_unit_value -Wclippy::mut_mut \
+			    -Wclippy::needless_bitwise_bool \
+			    -Wclippy::needless_continue \
+			    -Wclippy::no_mangle_with_rust_abi \
 			    -Wclippy::dbg_macro
 
 KBUILD_HOSTCFLAGS   := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
@@ -573,7 +573,7 @@ KBUILD_RUSTFLAGS := $(rust_common_flags) \
 		    -Csymbol-mangling-version=v0 \
 		    -Crelocation-model=static \
 		    -Zfunction-sections=n \
-		    -Dclippy::float_arithmetic
+		    -Wclippy::float_arithmetic
 
 KBUILD_AFLAGS_KERNEL :=
 KBUILD_CFLAGS_KERNEL :=
diff --git a/rust/Makefile b/rust/Makefile
index f70d5e244fee..4c03e53d3084 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -421,7 +421,7 @@ ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
 endif
 
 $(obj)/core.o: private skip_clippy = 1
-$(obj)/core.o: private skip_flags = -Dunreachable_pub
+$(obj)/core.o: private skip_flags = -Wunreachable_pub
 $(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
 $(obj)/core.o: private rustc_target_flags = $(core-cfgs)
 $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
@@ -435,7 +435,7 @@ $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
 	+$(call if_changed_dep,rustc_library)
 
 $(obj)/alloc.o: private skip_clippy = 1
-$(obj)/alloc.o: private skip_flags = -Dunreachable_pub
+$(obj)/alloc.o: private skip_flags = -Wunreachable_pub
 $(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
 $(obj)/alloc.o: $(RUST_LIB_SRC)/alloc/src/lib.rs $(obj)/compiler_builtins.o FORCE
 	+$(call if_changed_dep,rustc_library)
-- 
2.45.2


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

* [PATCH 05/13] rust: simplify Clippy warning flags set
       [not found] <20240701183625.665574-1-ojeda@kernel.org>
  2024-07-01 18:36 ` [PATCH 04/13] rust: relax most deny-level lints to warnings Miguel Ojeda
@ 2024-07-01 18:36 ` Miguel Ojeda
  2024-07-04 14:37   ` Finn Behrens
  2024-07-01 18:36 ` [PATCH 11/13] kbuild: rust: add `rustc-version` support Miguel Ojeda
  2024-07-01 18:36 ` [PATCH 12/13] rust: support the new `-Zub-checks` flag Miguel Ojeda
  3 siblings, 1 reply; 10+ messages in thread
From: Miguel Ojeda @ 2024-07-01 18:36 UTC (permalink / raw)
  To: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, rust-for-linux, linux-kernel,
	patches, Masahiro Yamada, Nathan Chancellor, Nicolas Schier,
	linux-kbuild

All Clippy lint groups that we enable, except `correctness`, have a
default `warn` level, thus they may be removed now that we relaxed all
lints to `warn`.

Moreover, Clippy provides an `all` lint group that covers the groups
we enable by default. Thus just use `all` instead -- the only change is
that, if Clippy introduces a new lint group or splits an existing one,
we will cover that one automatically.

In addition, `let_unit_value` is in `style` since Rust 1.62.0, thus it
does not need to be enabled manually.

Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 Makefile | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index 056176a55d63..3f43f03f855e 100644
--- a/Makefile
+++ b/Makefile
@@ -465,10 +465,8 @@ export rust_common_flags := --edition=2021 \
 			    -Wunreachable_pub -Wnon_ascii_idents \
 			    -Wmissing_docs \
 			    -Wrustdoc::missing_crate_level_docs \
-			    -Wclippy::correctness -Wclippy::style \
-			    -Wclippy::suspicious -Wclippy::complexity \
-			    -Wclippy::perf \
-			    -Wclippy::let_unit_value -Wclippy::mut_mut \
+			    -Wclippy::all \
+			    -Wclippy::mut_mut \
 			    -Wclippy::needless_bitwise_bool \
 			    -Wclippy::needless_continue \
 			    -Wclippy::no_mangle_with_rust_abi \
-- 
2.45.2


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

* [PATCH 11/13] kbuild: rust: add `rustc-version` support
       [not found] <20240701183625.665574-1-ojeda@kernel.org>
  2024-07-01 18:36 ` [PATCH 04/13] rust: relax most deny-level lints to warnings Miguel Ojeda
  2024-07-01 18:36 ` [PATCH 05/13] rust: simplify Clippy warning flags set Miguel Ojeda
@ 2024-07-01 18:36 ` Miguel Ojeda
  2024-07-04 15:05   ` Finn Behrens
  2024-07-01 18:36 ` [PATCH 12/13] rust: support the new `-Zub-checks` flag Miguel Ojeda
  3 siblings, 1 reply; 10+ messages in thread
From: Miguel Ojeda @ 2024-07-01 18:36 UTC (permalink / raw)
  To: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, rust-for-linux, linux-kernel,
	patches, Masahiro Yamada, Nathan Chancellor, Nicolas Schier,
	linux-kbuild

Now that we are starting to support several Rust versions, introduce
`rustc-version` support, mimicking the C side:

  - `scripts/rustc-version.sh`, that mimics the other version scripts
     (with one more digit, e.g. Rust 1.79.0 is 107900).

  - `rustc-{info,name,version}` Kbuild macros.

  - `CONFIG_RUSTC_VERSION` Kconfig symbol that calls `rustc-version`.

  - `rustc-min-version` Kbuild macro that uses `CONFIG_RUSTC_VERSION`.

With these, we can easily support flags conditionally depending on
`rustc`'s version -- a user comes in the next patch.

Another user will be the `-Ctarget-feature=+reserve-x18`/`-Zfixed-x18`
arm64 flags [1].

Link: https://lore.kernel.org/rust-for-linux/20240305-shadow-call-stack-v2-1-c7b4a3f4d616@google.com/ [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 init/Kconfig              |  6 +++++
 scripts/Kconfig.include   |  6 +++++
 scripts/Makefile.compiler |  4 +++
 scripts/rustc-version.sh  | 52 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 68 insertions(+)
 create mode 100755 scripts/rustc-version.sh

diff --git a/init/Kconfig b/init/Kconfig
index 94e20d3b99d4..7d344f248785 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1920,6 +1920,12 @@ config RUST
 
 	  If unsure, say N.
 
+config RUSTC_VERSION
+	int
+	depends on RUST
+	default $(rustc-version)
+	default 0
+
 config RUSTC_VERSION_TEXT
 	string
 	depends on RUST
diff --git a/scripts/Kconfig.include b/scripts/Kconfig.include
index 3ee8ecfb8c04..82ab889725db 100644
--- a/scripts/Kconfig.include
+++ b/scripts/Kconfig.include
@@ -45,6 +45,12 @@ $(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this C compiler is not
 cc-name := $(shell,set -- $(cc-info) && echo $1)
 cc-version := $(shell,set -- $(cc-info) && echo $2)
 
+# Get the Rust compiler name, version, and error out if it is not supported.
+rustc-info := $(shell,$(srctree)/scripts/rustc-version.sh $(RUSTC))
+$(error-if,$(success,test -z "$(rustc-info)"),Sorry$(comma) this Rust compiler is not supported.)
+rustc-name := $(shell,set -- $(rustc-info) && echo $1)
+rustc-version := $(shell,set -- $(rustc-info) && echo $2)
+
 # Get the assembler name, version, and error out if it is not supported.
 as-info := $(shell,$(srctree)/scripts/as-version.sh $(CC) $(CLANG_FLAGS))
 $(error-if,$(success,test -z "$(as-info)"),Sorry$(comma) this assembler is not supported.)
diff --git a/scripts/Makefile.compiler b/scripts/Makefile.compiler
index 92be0c9a13ee..17eaa085b59c 100644
--- a/scripts/Makefile.compiler
+++ b/scripts/Makefile.compiler
@@ -69,6 +69,10 @@ gcc-min-version = $(call test-ge, $(CONFIG_GCC_VERSION), $1)
 # Usage: cflags-$(call clang-min-version, 110000) += -foo
 clang-min-version = $(call test-ge, $(CONFIG_CLANG_VERSION), $1)
 
+# rustc-min-version
+# Usage: rustflags-$(call rustc-min-version, 107900) += -foo
+rustc-min-version = $(call test-ge, $(CONFIG_RUSTC_VERSION), $1)
+
 # ld-option
 # Usage: KBUILD_LDFLAGS += $(call ld-option, -X, -Y)
 ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS) $(1) -v,$(1),$(2),$(3))
diff --git a/scripts/rustc-version.sh b/scripts/rustc-version.sh
new file mode 100755
index 000000000000..4e658fd55ae6
--- /dev/null
+++ b/scripts/rustc-version.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# Print the Rust compiler name and its version in a 5 or 6-digit form.
+# Also, perform the minimum version check.
+
+set -e
+
+# Convert the version string x.y.z to a canonical up-to-7-digits form.
+#
+# Note that this function uses one more digit (compared to other
+# instances in other version scripts) to give a bit more space to
+# `rustc` since it will reach 1.100.0 in late 2026.
+get_canonical_version()
+{
+	IFS=.
+	set -- $1
+	echo $((100000 * $1 + 100 * $2 + $3))
+}
+
+orig_args="$@"
+
+set -- $("$@" --version)
+
+name=$1
+
+min_tool_version=$(dirname $0)/min-tool-version.sh
+
+case "$name" in
+rustc)
+	version=$2
+	min_version=$($min_tool_version rustc)
+	;;
+*)
+	echo "$orig_args: unknown Rust compiler" >&2
+	exit 1
+	;;
+esac
+
+rustcversion=$(get_canonical_version $version)
+min_rustcversion=$(get_canonical_version $min_version)
+
+if [ "$rustcversion" -lt "$min_rustcversion" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Rust compiler is too old."
+	echo >&2 "***   Your $name version:    $version"
+	echo >&2 "***   Minimum $name version: $min_version"
+	echo >&2 "***"
+	exit 1
+fi
+
+echo $name $rustcversion
-- 
2.45.2


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

* [PATCH 12/13] rust: support the new `-Zub-checks` flag
       [not found] <20240701183625.665574-1-ojeda@kernel.org>
                   ` (2 preceding siblings ...)
  2024-07-01 18:36 ` [PATCH 11/13] kbuild: rust: add `rustc-version` support Miguel Ojeda
@ 2024-07-01 18:36 ` Miguel Ojeda
  2024-07-04 15:07   ` Finn Behrens
  3 siblings, 1 reply; 10+ messages in thread
From: Miguel Ojeda @ 2024-07-01 18:36 UTC (permalink / raw)
  To: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, rust-for-linux, linux-kernel,
	patches, Masahiro Yamada, Nathan Chancellor, Nicolas Schier,
	Andrew Morton, linux-kbuild

Rust 1.79.0 has introduced a new codegen flag, `-Zub-checks` [1], to
allow to independently configure (from `-Cdebug-assertions`) whether the
extra runtime checks for UB are emitted, in a similar fashion to
`-Coverflow-checks`.

This allows to configure the kernel with only the UB checks enabled,
but not the `debug_assert!`s; or vice versa, e.g. [2].

It also showcases how `RUSTC_VERSION` and the Kbuild macros, introduced
in the previous commit, can be used.

Link: https://github.com/rust-lang/compiler-team/issues/725 [1]
Link: https://godbolt.org/z/jY69ezx5K [2]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 Makefile          |  9 +++++++--
 lib/Kconfig.debug | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 3f43f03f855e..c0cb5c237c26 100644
--- a/Makefile
+++ b/Makefile
@@ -820,10 +820,15 @@ KBUILD_CFLAGS += -Os
 KBUILD_RUSTFLAGS += -Copt-level=s
 endif
 
-# Always set `debug-assertions` and `overflow-checks` because their default
-# depends on `opt-level` and `debug-assertions`, respectively.
+# Always set `debug-assertions` because its default depends on `opt-level`.
 KBUILD_RUSTFLAGS += -Cdebug-assertions=$(if $(CONFIG_RUST_DEBUG_ASSERTIONS),y,n)
+
+# Always set `overflow-checks` and `ub-checks` because their default depends on
+# `debug-assertions`.
 KBUILD_RUSTFLAGS += -Coverflow-checks=$(if $(CONFIG_RUST_OVERFLOW_CHECKS),y,n)
+ifeq ($(call rustc-min-version, 107900),y)
+KBUILD_RUSTFLAGS += -Zub-checks=$(if $(CONFIG_RUST_UNDEFINED_BEHAVIOR_CHECKS),y,n)
+endif
 
 # Tell gcc to never replace conditional load with a non-conditional one
 ifdef CONFIG_CC_IS_GCC
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 59b6765d86b8..6b4f512f9e13 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -3020,6 +3020,24 @@ config RUST_OVERFLOW_CHECKS
 
 	  If unsure, say Y.
 
+config RUST_UNDEFINED_BEHAVIOR_CHECKS
+	bool "Undefined Behavior checks"
+	depends on RUST && RUSTC_VERSION >= 107900
+	help
+	  Enables rustc's `-Zub-checks` codegen option.
+
+	  This flag allows you to control whether additional runtime checks that
+	  detect some causes of Undefined Behavior at runtime will be emitted.
+	  When enabled, a Rust panic will occur if UB is detected.
+
+	  All checks are generated on a best-effort basis; even if there is a check
+	  implemented for some cause of Undefined Behavior, it may be possible for
+	  the check to not fire.
+
+	  Note that this will apply to all Rust code, including `core`.
+
+	  If unsure, say N.
+
 config RUST_BUILD_ASSERT_ALLOW
 	bool "Allow unoptimized build-time assertions"
 	depends on RUST
-- 
2.45.2


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

* Re: [PATCH 04/13] rust: relax most deny-level lints to warnings
  2024-07-01 18:36 ` [PATCH 04/13] rust: relax most deny-level lints to warnings Miguel Ojeda
@ 2024-07-01 19:48   ` Björn Roy Baron
  2024-07-01 21:58     ` Miguel Ojeda
  2024-07-04 14:34   ` Finn Behrens
  1 sibling, 1 reply; 10+ messages in thread
From: Björn Roy Baron @ 2024-07-01 19:48 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Wedson Almeida Filho, Alex Gaynor, Boqun Feng, Gary Guo,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, rust-for-linux,
	linux-kernel, patches, Masahiro Yamada, Nathan Chancellor,
	Nicolas Schier, linux-kbuild

On Monday, July 1st, 2024 at 20:36, Miguel Ojeda <ojeda@kernel.org> wrote:

> Since we are starting to support several Rust toolchains, lints (including
> Clippy ones) now may behave differently and lint groups may include
> new lints.
> 
> Therefore, to maximize the chances a given version works, relax some
> deny-level lints to warnings. It may also make our lives a bit easier
> while developing new code or refactoring.
> 
> To be clear, the requirements for in-tree code are still the same, since
> Rust code still needs to be warning-free (patches should be clean under
> `WERROR=y`) and the set of lints is not changed.
> 
> `unsafe_op_in_unsafe_fn` is left unmodified, i.e. as an error, since
> 1) it is simple enough that it should not have false positives (unlike
> e.g. `rust_2018_idioms`'s `explicit_outlives_requirements`) and 2) it is
> becoming the default in the language (warn-by-default in Rust 2024 [1] and
> ideally an error later on) and thus it should also be very well tested.
> 
> Link: https://github.com/rust-lang/rust/pull/112038 [1]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> ---
>  Makefile      | 22 +++++++++++-----------
>  rust/Makefile |  4 ++--
>  2 files changed, 13 insertions(+), 13 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 4d36f943b3b1..056176a55d63 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -461,17 +461,17 @@ KBUILD_USERLDFLAGS := $(USERLDFLAGS)
>  # host programs.
>  export rust_common_flags := --edition=2021 \
>  			    -Zbinary_dep_depinfo=y \
> -			    -Dunsafe_op_in_unsafe_fn -Drust_2018_idioms \
> -			    -Dunreachable_pub -Dnon_ascii_idents \
> +			    -Dunsafe_op_in_unsafe_fn -Wrust_2018_idioms \
> +			    -Wunreachable_pub -Wnon_ascii_idents \

Maybe also keep non_ascii_idents as error? It shouldn't have any false positives.

>  			    -Wmissing_docs \
> -			    -Drustdoc::missing_crate_level_docs \
> -			    -Dclippy::correctness -Dclippy::style \
> -			    -Dclippy::suspicious -Dclippy::complexity \
> -			    -Dclippy::perf \
> -			    -Dclippy::let_unit_value -Dclippy::mut_mut \
> -			    -Dclippy::needless_bitwise_bool \
> -			    -Dclippy::needless_continue \
> -			    -Dclippy::no_mangle_with_rust_abi \
> +			    -Wrustdoc::missing_crate_level_docs \
> +			    -Wclippy::correctness -Wclippy::style \
> +			    -Wclippy::suspicious -Wclippy::complexity \
> +			    -Wclippy::perf \
> +			    -Wclippy::let_unit_value -Wclippy::mut_mut \
> +			    -Wclippy::needless_bitwise_bool \
> +			    -Wclippy::needless_continue \
> +			    -Wclippy::no_mangle_with_rust_abi \
>  			    -Wclippy::dbg_macro
> 
>  KBUILD_HOSTCFLAGS   := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
> @@ -573,7 +573,7 @@ KBUILD_RUSTFLAGS := $(rust_common_flags) \
>  		    -Csymbol-mangling-version=v0 \
>  		    -Crelocation-model=static \
>  		    -Zfunction-sections=n \
> -		    -Dclippy::float_arithmetic
> +		    -Wclippy::float_arithmetic
> 
>  KBUILD_AFLAGS_KERNEL :=
>  KBUILD_CFLAGS_KERNEL :=
> diff --git a/rust/Makefile b/rust/Makefile
> index f70d5e244fee..4c03e53d3084 100644
> --- a/rust/Makefile
> +++ b/rust/Makefile
> @@ -421,7 +421,7 @@ ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
>  endif
> 
>  $(obj)/core.o: private skip_clippy = 1
> -$(obj)/core.o: private skip_flags = -Dunreachable_pub
> +$(obj)/core.o: private skip_flags = -Wunreachable_pub
>  $(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
>  $(obj)/core.o: private rustc_target_flags = $(core-cfgs)
>  $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
> @@ -435,7 +435,7 @@ $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
>  	+$(call if_changed_dep,rustc_library)
> 
>  $(obj)/alloc.o: private skip_clippy = 1
> -$(obj)/alloc.o: private skip_flags = -Dunreachable_pub
> +$(obj)/alloc.o: private skip_flags = -Wunreachable_pub
>  $(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
>  $(obj)/alloc.o: $(RUST_LIB_SRC)/alloc/src/lib.rs $(obj)/compiler_builtins.o FORCE
>  	+$(call if_changed_dep,rustc_library)
> --
> 2.45.2

Cheers,
Bjorn

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

* Re: [PATCH 04/13] rust: relax most deny-level lints to warnings
  2024-07-01 19:48   ` Björn Roy Baron
@ 2024-07-01 21:58     ` Miguel Ojeda
  0 siblings, 0 replies; 10+ messages in thread
From: Miguel Ojeda @ 2024-07-01 21:58 UTC (permalink / raw)
  To: Björn Roy Baron
  Cc: Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor, Boqun Feng,
	Gary Guo, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	rust-for-linux, linux-kernel, patches, Masahiro Yamada,
	Nathan Chancellor, Nicolas Schier, linux-kbuild

On Mon, Jul 1, 2024 at 9:49 PM Björn Roy Baron <bjorn3_gh@protonmail.com> wrote:
>
> Maybe also keep non_ascii_idents as error? It shouldn't have any false positives.

I was on the fence for that one too. It is also unlikely that having
it as `-W` helps "productivity", i.e. I don't expect anybody to be
helped by being able to type non-ASCII identifiers during development.
:)

Happy either way.

Thanks for the quick review, too!

Cheers,
Miguel

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

* Re: [PATCH 04/13] rust: relax most deny-level lints to warnings
  2024-07-01 18:36 ` [PATCH 04/13] rust: relax most deny-level lints to warnings Miguel Ojeda
  2024-07-01 19:48   ` Björn Roy Baron
@ 2024-07-04 14:34   ` Finn Behrens
  1 sibling, 0 replies; 10+ messages in thread
From: Finn Behrens @ 2024-07-04 14:34 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Wedson Almeida Filho, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	rust-for-linux, linux-kernel, patches, Masahiro Yamada,
	Nathan Chancellor, Nicolas Schier, linux-kbuild



On 1 Jul 2024, at 20:36, Miguel Ojeda wrote:

> Since we are starting to support several Rust toolchains, lints (including
> Clippy ones) now may behave differently and lint groups may include
> new lints.
>
> Therefore, to maximize the chances a given version works, relax some
> deny-level lints to warnings. It may also make our lives a bit easier
> while developing new code or refactoring.
>
> To be clear, the requirements for in-tree code are still the same, since
> Rust code still needs to be warning-free (patches should be clean under
> `WERROR=y`) and the set of lints is not changed.
>
> `unsafe_op_in_unsafe_fn` is left unmodified, i.e. as an error, since
> 1) it is simple enough that it should not have false positives (unlike
> e.g. `rust_2018_idioms`'s `explicit_outlives_requirements`) and 2) it is
> becoming the default in the language (warn-by-default in Rust 2024 [1] and
> ideally an error later on) and thus it should also be very well tested.
>
> Link: https://github.com/rust-lang/rust/pull/112038 [1]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Also in favour of disallowing non ASCII idents. Either way

Reviewed-by: Finn Behrens <me@kloenk.dev>

> ---
>  Makefile      | 22 +++++++++++-----------
>  rust/Makefile |  4 ++--
>  2 files changed, 13 insertions(+), 13 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 4d36f943b3b1..056176a55d63 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -461,17 +461,17 @@ KBUILD_USERLDFLAGS := $(USERLDFLAGS)
>  # host programs.
>  export rust_common_flags := --edition=2021 \
>  			    -Zbinary_dep_depinfo=y \
> -			    -Dunsafe_op_in_unsafe_fn -Drust_2018_idioms \
> -			    -Dunreachable_pub -Dnon_ascii_idents \
> +			    -Dunsafe_op_in_unsafe_fn -Wrust_2018_idioms \
> +			    -Wunreachable_pub -Wnon_ascii_idents \
>  			    -Wmissing_docs \
> -			    -Drustdoc::missing_crate_level_docs \
> -			    -Dclippy::correctness -Dclippy::style \
> -			    -Dclippy::suspicious -Dclippy::complexity \
> -			    -Dclippy::perf \
> -			    -Dclippy::let_unit_value -Dclippy::mut_mut \
> -			    -Dclippy::needless_bitwise_bool \
> -			    -Dclippy::needless_continue \
> -			    -Dclippy::no_mangle_with_rust_abi \
> +			    -Wrustdoc::missing_crate_level_docs \
> +			    -Wclippy::correctness -Wclippy::style \
> +			    -Wclippy::suspicious -Wclippy::complexity \
> +			    -Wclippy::perf \
> +			    -Wclippy::let_unit_value -Wclippy::mut_mut \
> +			    -Wclippy::needless_bitwise_bool \
> +			    -Wclippy::needless_continue \
> +			    -Wclippy::no_mangle_with_rust_abi \
>  			    -Wclippy::dbg_macro
>
>  KBUILD_HOSTCFLAGS   := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
> @@ -573,7 +573,7 @@ KBUILD_RUSTFLAGS := $(rust_common_flags) \
>  		    -Csymbol-mangling-version=v0 \
>  		    -Crelocation-model=static \
>  		    -Zfunction-sections=n \
> -		    -Dclippy::float_arithmetic
> +		    -Wclippy::float_arithmetic
>
>  KBUILD_AFLAGS_KERNEL :=
>  KBUILD_CFLAGS_KERNEL :=
> diff --git a/rust/Makefile b/rust/Makefile
> index f70d5e244fee..4c03e53d3084 100644
> --- a/rust/Makefile
> +++ b/rust/Makefile
> @@ -421,7 +421,7 @@ ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
>  endif
>
>  $(obj)/core.o: private skip_clippy = 1
> -$(obj)/core.o: private skip_flags = -Dunreachable_pub
> +$(obj)/core.o: private skip_flags = -Wunreachable_pub
>  $(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
>  $(obj)/core.o: private rustc_target_flags = $(core-cfgs)
>  $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
> @@ -435,7 +435,7 @@ $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
>  	+$(call if_changed_dep,rustc_library)
>
>  $(obj)/alloc.o: private skip_clippy = 1
> -$(obj)/alloc.o: private skip_flags = -Dunreachable_pub
> +$(obj)/alloc.o: private skip_flags = -Wunreachable_pub
>  $(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
>  $(obj)/alloc.o: $(RUST_LIB_SRC)/alloc/src/lib.rs $(obj)/compiler_builtins.o FORCE
>  	+$(call if_changed_dep,rustc_library)
> -- 
> 2.45.2

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

* Re: [PATCH 05/13] rust: simplify Clippy warning flags set
  2024-07-01 18:36 ` [PATCH 05/13] rust: simplify Clippy warning flags set Miguel Ojeda
@ 2024-07-04 14:37   ` Finn Behrens
  0 siblings, 0 replies; 10+ messages in thread
From: Finn Behrens @ 2024-07-04 14:37 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Wedson Almeida Filho, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	rust-for-linux, linux-kernel, patches, Masahiro Yamada,
	Nathan Chancellor, Nicolas Schier, linux-kbuild



On 1 Jul 2024, at 20:36, Miguel Ojeda wrote:

> All Clippy lint groups that we enable, except `correctness`, have a
> default `warn` level, thus they may be removed now that we relaxed all
> lints to `warn`.
>
> Moreover, Clippy provides an `all` lint group that covers the groups
> we enable by default. Thus just use `all` instead -- the only change is
> that, if Clippy introduces a new lint group or splits an existing one,
> we will cover that one automatically.
>
> In addition, `let_unit_value` is in `style` since Rust 1.62.0, thus it
> does not need to be enabled manually.
>
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Finn Behrens <me@kloenk.dev>

> ---
>  Makefile | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 056176a55d63..3f43f03f855e 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -465,10 +465,8 @@ export rust_common_flags := --edition=2021 \
>  			    -Wunreachable_pub -Wnon_ascii_idents \
>  			    -Wmissing_docs \
>  			    -Wrustdoc::missing_crate_level_docs \
> -			    -Wclippy::correctness -Wclippy::style \
> -			    -Wclippy::suspicious -Wclippy::complexity \
> -			    -Wclippy::perf \
> -			    -Wclippy::let_unit_value -Wclippy::mut_mut \
> +			    -Wclippy::all \
> +			    -Wclippy::mut_mut \
>  			    -Wclippy::needless_bitwise_bool \
>  			    -Wclippy::needless_continue \
>  			    -Wclippy::no_mangle_with_rust_abi \
> -- 
> 2.45.2

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

* Re: [PATCH 11/13] kbuild: rust: add `rustc-version` support
  2024-07-01 18:36 ` [PATCH 11/13] kbuild: rust: add `rustc-version` support Miguel Ojeda
@ 2024-07-04 15:05   ` Finn Behrens
  0 siblings, 0 replies; 10+ messages in thread
From: Finn Behrens @ 2024-07-04 15:05 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Wedson Almeida Filho, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	rust-for-linux, linux-kernel, patches, Masahiro Yamada,
	Nathan Chancellor, Nicolas Schier, linux-kbuild



On 1 Jul 2024, at 20:36, Miguel Ojeda wrote:

> Now that we are starting to support several Rust versions, introduce
> `rustc-version` support, mimicking the C side:
>
>   - `scripts/rustc-version.sh`, that mimics the other version scripts
>      (with one more digit, e.g. Rust 1.79.0 is 107900).
>
>   - `rustc-{info,name,version}` Kbuild macros.
>
>   - `CONFIG_RUSTC_VERSION` Kconfig symbol that calls `rustc-version`.
>
>   - `rustc-min-version` Kbuild macro that uses `CONFIG_RUSTC_VERSION`.
>
> With these, we can easily support flags conditionally depending on
> `rustc`'s version -- a user comes in the next patch.
>
> Another user will be the `-Ctarget-feature=+reserve-x18`/`-Zfixed-x18`
> arm64 flags [1].
>
> Link: https://lore.kernel.org/rust-for-linux/20240305-shadow-call-stack-v2-1-c7b4a3f4d616@google.com/ [1]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Finn Behrens <me@kloenk.dev>

> ---
>  init/Kconfig              |  6 +++++
>  scripts/Kconfig.include   |  6 +++++
>  scripts/Makefile.compiler |  4 +++
>  scripts/rustc-version.sh  | 52 +++++++++++++++++++++++++++++++++++++++
>  4 files changed, 68 insertions(+)
>  create mode 100755 scripts/rustc-version.sh
>
> diff --git a/init/Kconfig b/init/Kconfig
> index 94e20d3b99d4..7d344f248785 100644
> --- a/init/Kconfig
> +++ b/init/Kconfig
> @@ -1920,6 +1920,12 @@ config RUST
>
>  	  If unsure, say N.
>
> +config RUSTC_VERSION
> +	int
> +	depends on RUST
> +	default $(rustc-version)
> +	default 0
> +
>  config RUSTC_VERSION_TEXT
>  	string
>  	depends on RUST
> diff --git a/scripts/Kconfig.include b/scripts/Kconfig.include
> index 3ee8ecfb8c04..82ab889725db 100644
> --- a/scripts/Kconfig.include
> +++ b/scripts/Kconfig.include
> @@ -45,6 +45,12 @@ $(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this C compiler is not
>  cc-name := $(shell,set -- $(cc-info) && echo $1)
>  cc-version := $(shell,set -- $(cc-info) && echo $2)
>
> +# Get the Rust compiler name, version, and error out if it is not supported.
> +rustc-info := $(shell,$(srctree)/scripts/rustc-version.sh $(RUSTC))
> +$(error-if,$(success,test -z "$(rustc-info)"),Sorry$(comma) this Rust compiler is not supported.)
> +rustc-name := $(shell,set -- $(rustc-info) && echo $1)
> +rustc-version := $(shell,set -- $(rustc-info) && echo $2)
> +
>  # Get the assembler name, version, and error out if it is not supported.
>  as-info := $(shell,$(srctree)/scripts/as-version.sh $(CC) $(CLANG_FLAGS))
>  $(error-if,$(success,test -z "$(as-info)"),Sorry$(comma) this assembler is not supported.)
> diff --git a/scripts/Makefile.compiler b/scripts/Makefile.compiler
> index 92be0c9a13ee..17eaa085b59c 100644
> --- a/scripts/Makefile.compiler
> +++ b/scripts/Makefile.compiler
> @@ -69,6 +69,10 @@ gcc-min-version = $(call test-ge, $(CONFIG_GCC_VERSION), $1)
>  # Usage: cflags-$(call clang-min-version, 110000) += -foo
>  clang-min-version = $(call test-ge, $(CONFIG_CLANG_VERSION), $1)
>
> +# rustc-min-version
> +# Usage: rustflags-$(call rustc-min-version, 107900) += -foo
> +rustc-min-version = $(call test-ge, $(CONFIG_RUSTC_VERSION), $1)
> +
>  # ld-option
>  # Usage: KBUILD_LDFLAGS += $(call ld-option, -X, -Y)
>  ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS) $(1) -v,$(1),$(2),$(3))
> diff --git a/scripts/rustc-version.sh b/scripts/rustc-version.sh
> new file mode 100755
> index 000000000000..4e658fd55ae6
> --- /dev/null
> +++ b/scripts/rustc-version.sh
> @@ -0,0 +1,52 @@
> +#!/bin/sh
> +# SPDX-License-Identifier: GPL-2.0
> +#
> +# Print the Rust compiler name and its version in a 5 or 6-digit form.
> +# Also, perform the minimum version check.
> +
> +set -e
> +
> +# Convert the version string x.y.z to a canonical up-to-7-digits form.
> +#
> +# Note that this function uses one more digit (compared to other
> +# instances in other version scripts) to give a bit more space to
> +# `rustc` since it will reach 1.100.0 in late 2026.
> +get_canonical_version()
> +{
> +	IFS=.
> +	set -- $1
> +	echo $((100000 * $1 + 100 * $2 + $3))
> +}
> +
> +orig_args="$@"
> +
> +set -- $("$@" --version)
> +
> +name=$1
> +
> +min_tool_version=$(dirname $0)/min-tool-version.sh
> +
> +case "$name" in
> +rustc)
> +	version=$2
> +	min_version=$($min_tool_version rustc)
> +	;;
> +*)
> +	echo "$orig_args: unknown Rust compiler" >&2
> +	exit 1
> +	;;
> +esac
> +
> +rustcversion=$(get_canonical_version $version)
> +min_rustcversion=$(get_canonical_version $min_version)
> +
> +if [ "$rustcversion" -lt "$min_rustcversion" ]; then
> +	echo >&2 "***"
> +	echo >&2 "*** Rust compiler is too old."
> +	echo >&2 "***   Your $name version:    $version"
> +	echo >&2 "***   Minimum $name version: $min_version"
> +	echo >&2 "***"
> +	exit 1
> +fi
> +
> +echo $name $rustcversion
> -- 
> 2.45.2

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

* Re: [PATCH 12/13] rust: support the new `-Zub-checks` flag
  2024-07-01 18:36 ` [PATCH 12/13] rust: support the new `-Zub-checks` flag Miguel Ojeda
@ 2024-07-04 15:07   ` Finn Behrens
  0 siblings, 0 replies; 10+ messages in thread
From: Finn Behrens @ 2024-07-04 15:07 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Wedson Almeida Filho, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	rust-for-linux, linux-kernel, patches, Masahiro Yamada,
	Nathan Chancellor, Nicolas Schier, Andrew Morton, linux-kbuild



On 1 Jul 2024, at 20:36, Miguel Ojeda wrote:

> Rust 1.79.0 has introduced a new codegen flag, `-Zub-checks` [1], to
> allow to independently configure (from `-Cdebug-assertions`) whether the
> extra runtime checks for UB are emitted, in a similar fashion to
> `-Coverflow-checks`.
>
> This allows to configure the kernel with only the UB checks enabled,
> but not the `debug_assert!`s; or vice versa, e.g. [2].
>
> It also showcases how `RUSTC_VERSION` and the Kbuild macros, introduced
> in the previous commit, can be used.
>
> Link: https://github.com/rust-lang/compiler-team/issues/725 [1]
> Link: https://godbolt.org/z/jY69ezx5K [2]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Finn Behrens <me@kloenk.dev>

> ---
>  Makefile          |  9 +++++++--
>  lib/Kconfig.debug | 18 ++++++++++++++++++
>  2 files changed, 25 insertions(+), 2 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 3f43f03f855e..c0cb5c237c26 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -820,10 +820,15 @@ KBUILD_CFLAGS += -Os
>  KBUILD_RUSTFLAGS += -Copt-level=s
>  endif
>
> -# Always set `debug-assertions` and `overflow-checks` because their default
> -# depends on `opt-level` and `debug-assertions`, respectively.
> +# Always set `debug-assertions` because its default depends on `opt-level`.
>  KBUILD_RUSTFLAGS += -Cdebug-assertions=$(if $(CONFIG_RUST_DEBUG_ASSERTIONS),y,n)
> +
> +# Always set `overflow-checks` and `ub-checks` because their default depends on
> +# `debug-assertions`.
>  KBUILD_RUSTFLAGS += -Coverflow-checks=$(if $(CONFIG_RUST_OVERFLOW_CHECKS),y,n)
> +ifeq ($(call rustc-min-version, 107900),y)
> +KBUILD_RUSTFLAGS += -Zub-checks=$(if $(CONFIG_RUST_UNDEFINED_BEHAVIOR_CHECKS),y,n)
> +endif
>
>  # Tell gcc to never replace conditional load with a non-conditional one
>  ifdef CONFIG_CC_IS_GCC
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index 59b6765d86b8..6b4f512f9e13 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -3020,6 +3020,24 @@ config RUST_OVERFLOW_CHECKS
>
>  	  If unsure, say Y.
>
> +config RUST_UNDEFINED_BEHAVIOR_CHECKS
> +	bool "Undefined Behavior checks"
> +	depends on RUST && RUSTC_VERSION >= 107900
> +	help
> +	  Enables rustc's `-Zub-checks` codegen option.
> +
> +	  This flag allows you to control whether additional runtime checks that
> +	  detect some causes of Undefined Behavior at runtime will be emitted.
> +	  When enabled, a Rust panic will occur if UB is detected.
> +
> +	  All checks are generated on a best-effort basis; even if there is a check
> +	  implemented for some cause of Undefined Behavior, it may be possible for
> +	  the check to not fire.
> +
> +	  Note that this will apply to all Rust code, including `core`.
> +
> +	  If unsure, say N.
> +
>  config RUST_BUILD_ASSERT_ALLOW
>  	bool "Allow unoptimized build-time assertions"
>  	depends on RUST
> -- 
> 2.45.2

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

end of thread, other threads:[~2024-07-04 15:07 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20240701183625.665574-1-ojeda@kernel.org>
2024-07-01 18:36 ` [PATCH 04/13] rust: relax most deny-level lints to warnings Miguel Ojeda
2024-07-01 19:48   ` Björn Roy Baron
2024-07-01 21:58     ` Miguel Ojeda
2024-07-04 14:34   ` Finn Behrens
2024-07-01 18:36 ` [PATCH 05/13] rust: simplify Clippy warning flags set Miguel Ojeda
2024-07-04 14:37   ` Finn Behrens
2024-07-01 18:36 ` [PATCH 11/13] kbuild: rust: add `rustc-version` support Miguel Ojeda
2024-07-04 15:05   ` Finn Behrens
2024-07-01 18:36 ` [PATCH 12/13] rust: support the new `-Zub-checks` flag Miguel Ojeda
2024-07-04 15:07   ` Finn Behrens

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox