* [PATCH v2 04/13] rust: relax most deny-level lints to warnings
2024-07-09 16:05 [PATCH v2 00/13] Support several Rust toolchain versions Miguel Ojeda
@ 2024-07-09 16:05 ` Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 05/13] rust: simplify Clippy warning flags set Miguel Ojeda
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Miguel Ojeda @ 2024-07-09 16:05 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, Finn Behrens, 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 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. In
addition, it is simple enough that it should not have false positives
(unlike e.g. `rust_2018_idioms`'s `explicit_outlives_requirements`).
`non_ascii_idents` is left unmodified as well, i.e. as an error, since
it is unlikely one gains any productivity during development if it
were a warning (in fact, it may be worse, since it is likely one made
a typo). In addition, it should not have false positives.
Finally, put the two `-D` ones at the top and take the chance to do one
per line.
Link: https://github.com/rust-lang/rust/pull/112038 [1]
Reviewed-by: Finn Behrens <me@kloenk.dev>
Tested-by: Benno Lossin <benno.lossin@proton.me>
Tested-by: Andreas Hindborg <a.hindborg@samsung.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
v2:
- Kept `non_ascii_idents` as an error. (Björn, Finn)
Makefile | 24 +++++++++++++-----------
rust/Makefile | 4 ++--
2 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/Makefile b/Makefile
index fea263aaa492..7ea526814fdb 100644
--- a/Makefile
+++ b/Makefile
@@ -461,17 +461,19 @@ 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 \
+ -Dnon_ascii_idents \
+ -Wrust_2018_idioms \
+ -Wunreachable_pub \
-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)
@@ -572,7 +574,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 385378311322..bf05e65365da 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -367,7 +367,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
@@ -381,7 +381,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] 7+ messages in thread* [PATCH v2 05/13] rust: simplify Clippy warning flags set
2024-07-09 16:05 [PATCH v2 00/13] Support several Rust toolchain versions Miguel Ojeda
2024-07-09 16:05 ` [PATCH v2 04/13] rust: relax most deny-level lints to warnings Miguel Ojeda
@ 2024-07-09 16:06 ` Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 11/13] kbuild: rust: add `rustc-version` support Miguel Ojeda
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Miguel Ojeda @ 2024-07-09 16:06 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, Finn Behrens, 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.
Reviewed-by: Finn Behrens <me@kloenk.dev>
Tested-by: Benno Lossin <benno.lossin@proton.me>
Tested-by: Andreas Hindborg <a.hindborg@samsung.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
Makefile | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/Makefile b/Makefile
index 7ea526814fdb..9044fdb9adb1 100644
--- a/Makefile
+++ b/Makefile
@@ -467,10 +467,8 @@ export rust_common_flags := --edition=2021 \
-Wunreachable_pub \
-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] 7+ messages in thread* [PATCH v2 11/13] kbuild: rust: add `rustc-version` support
2024-07-09 16:05 [PATCH v2 00/13] Support several Rust toolchain versions Miguel Ojeda
2024-07-09 16:05 ` [PATCH v2 04/13] rust: relax most deny-level lints to warnings Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 05/13] rust: simplify Clippy warning flags set Miguel Ojeda
@ 2024-07-09 16:06 ` Miguel Ojeda
2024-07-09 17:26 ` Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 12/13] rust: support the new `-Zub-checks` flag Miguel Ojeda
2024-07-10 9:04 ` [PATCH v2 00/13] Support several Rust toolchain versions Miguel Ojeda
4 siblings, 1 reply; 7+ messages in thread
From: Miguel Ojeda @ 2024-07-09 16:06 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, Finn Behrens, 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]
Reviewed-by: Finn Behrens <me@kloenk.dev>
Tested-by: Benno Lossin <benno.lossin@proton.me>
Tested-by: Andreas Hindborg <a.hindborg@samsung.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
v2:
- Fix comment from "5 or 6-digit" to "6 to 7-digit".
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..7b27ab5d4ecd
--- /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 6 or 7-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] 7+ messages in thread* Re: [PATCH v2 11/13] kbuild: rust: add `rustc-version` support
2024-07-09 16:06 ` [PATCH v2 11/13] kbuild: rust: add `rustc-version` support Miguel Ojeda
@ 2024-07-09 17:26 ` Miguel Ojeda
0 siblings, 0 replies; 7+ messages in thread
From: Miguel Ojeda @ 2024-07-09 17:26 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, Finn Behrens,
Masahiro Yamada, Nathan Chancellor, Nicolas Schier, linux-kbuild
On Tue, Jul 9, 2024 at 6:07 PM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> +$(error-if,$(success,test -z "$(rustc-info)"),Sorry$(comma) this Rust compiler is not supported.)
Bah, this is broken, I just noticed in my CI that I didn't handle the
"Rust not installed" case.
Anyway, this patch (and the next one) are not important for the
series, I will just drop them and send them independently next cycle
to Kbuild instead. I should have done that anyway, even if they were
correct.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 12/13] rust: support the new `-Zub-checks` flag
2024-07-09 16:05 [PATCH v2 00/13] Support several Rust toolchain versions Miguel Ojeda
` (2 preceding siblings ...)
2024-07-09 16:06 ` [PATCH v2 11/13] kbuild: rust: add `rustc-version` support Miguel Ojeda
@ 2024-07-09 16:06 ` Miguel Ojeda
2024-07-10 9:04 ` [PATCH v2 00/13] Support several Rust toolchain versions Miguel Ojeda
4 siblings, 0 replies; 7+ messages in thread
From: Miguel Ojeda @ 2024-07-09 16:06 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, Finn Behrens, 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]
Reviewed-by: Finn Behrens <me@kloenk.dev>
Tested-by: Benno Lossin <benno.lossin@proton.me>
Tested-by: Andreas Hindborg <a.hindborg@samsung.com>
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 9044fdb9adb1..4cf3b9799ec9 100644
--- a/Makefile
+++ b/Makefile
@@ -821,10 +821,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] 7+ messages in thread* Re: [PATCH v2 00/13] Support several Rust toolchain versions
2024-07-09 16:05 [PATCH v2 00/13] Support several Rust toolchain versions Miguel Ojeda
` (3 preceding siblings ...)
2024-07-09 16:06 ` [PATCH v2 12/13] rust: support the new `-Zub-checks` flag Miguel Ojeda
@ 2024-07-10 9:04 ` Miguel Ojeda
4 siblings, 0 replies; 7+ messages in thread
From: Miguel Ojeda @ 2024-07-10 9:04 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, linux-doc, linux-kbuild,
workflows
On Tue, Jul 9, 2024 at 6:06 PM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> A few things improved here and there, and rebased on top of `rust-next`.
>
> The changelog is attached to each patch.
>
> I kept the `Tested-by`s since most of the changes are on documentation
> or comments, though I did remove them on the patch that changed the most
> just in case (even for that one, I think Benno's and Andreas' setup
> would not have made a difference).
>
> I plan to put this series into `rust-next` very soon so that it goes
> into the merge window.
Applied into `rust-next` -- thanks everyone!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 7+ messages in thread