From patchwork Tue Jul 9 16:05:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 13728280 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B44AF19D076; Tue, 9 Jul 2024 16:06:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720541202; cv=none; b=mP4j/Pc0ct7fQDpSFouFoRp1ncNUuLs/Qy00rutrqVYHcCAnIK3CyBkHhvyQ8yPnm76BaPvkmmatwrTyonb2KNCYKkV6B+M5OGhWbXHaaetnFytesHiN6GTFQ/t9YmP5UZ0GlImUXe4tzt8q3bQBeFPPpftnWbFg2xXKgdl/VdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720541202; c=relaxed/simple; bh=87ryJx7ZGIPxiVj7ZhWT3AKYThcfaUwoUvDdB5EI3Mo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=sneFH/TVERjA96SLhZ8FNsdUeuL471k4f/BFCKmwC73uNVZ7MrJXN1QpE/UL+qPSXXgCp+2QpbLCAqEfyI3sF4mPqEchBT7Mb1DVk1uWeFZSVm4B1cfPxLhL+7RWh5rFltF0j+o3wsYtmMZkdA5DvWD008ZQZF6vhnVam5040VM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Glsumsql; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Glsumsql" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EC9DEC3277B; Tue, 9 Jul 2024 16:06:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720541202; bh=87ryJx7ZGIPxiVj7ZhWT3AKYThcfaUwoUvDdB5EI3Mo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Glsumsql4QoDnJCzAN4/HsASRkloOYyTlIYukEwsGS09jafPAQQ6cDaLbOeVeCM3D hDx/39aHpwfvAZSnuQkwcvh3VYqqww9xp/OImDh7Xk6y8Fo5HM4cNP6Lo8sVLVsYTz u2XcjUPgGEmpFAwvjagTVGoPFcy0T6yXPQVymZfty4vSB5z5lzBAOaXIc5tgh0gt4b ma7dLvi4z6Tfo7XpHO3Mf9a2V9SbqDDC/oo6FQw+VNrR4tanWDP19J9JWEh2qpFc4U 8E/FQjMA73cgxV/cnaORpVueKKR13+R9iRPow8NNdWowOsPAvXGSB3YtYi1tQVTZM9 MPTuy3DlqCYGw== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Finn Behrens , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , linux-kbuild@vger.kernel.org Subject: [PATCH v2 04/13] rust: relax most deny-level lints to warnings Date: Tue, 9 Jul 2024 18:05:59 +0200 Message-ID: <20240709160615.998336-5-ojeda@kernel.org> In-Reply-To: <20240709160615.998336-1-ojeda@kernel.org> References: <20240709160615.998336-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Tested-by: Benno Lossin Tested-by: Andreas Hindborg Signed-off-by: Miguel Ojeda --- v2: - Kept `non_ascii_idents` as an error. (Björn, Finn) Makefile | 24 +++++++++++++----------- rust/Makefile | 4 ++-- 2 files changed, 15 insertions(+), 13 deletions(-) -- 2.45.2 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) From patchwork Tue Jul 9 16:06:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 13728281 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80FDC19DF5C; Tue, 9 Jul 2024 16:06:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720541207; cv=none; b=DwxayeMUlaA2FoI2DjqExFXDAqNm+LJSir+VLEdxcdcNGQqp7Bsz51i/7XJt9ZUOwHP2SLjJKHIOt2OFCJk2LxrEE/dq6Qf5NDcgPYZapzMYd05vAlQvlJ0uTKEb902uVvWpQGxB0ihzBePNCFEgCdCzk73/jjqnFE9DjIYBExs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720541207; c=relaxed/simple; bh=Thuob+LZEz9c03OnGu6BVeMz2jDrDUQ5FhyHil9JAoU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oXl04l4R61DCfHrP+IMzTW+XRScHbsCpYvZxOf7qsCQYZeshKfB6I/WZz1a6SlpGxILhACbO6wwGd9XsXgh/w3nq9LnLsF+c41yodJhna4DB+Mh4cpQT1mqOePy/1RJy9p8YFYtzU4A9dKOv89q+YgCbAUHj+VzAXGDgbJfNUlE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PH3P7f0c; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PH3P7f0c" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E99D8C3277B; Tue, 9 Jul 2024 16:06:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720541207; bh=Thuob+LZEz9c03OnGu6BVeMz2jDrDUQ5FhyHil9JAoU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PH3P7f0c07sg6E4d0AgtQVqiIVSGXnEV7f8MVwfBUeSHZ1KNQoKBlm6aA2TPBKNvn hTj4cLFIlRnZLakhWBdUCDihSq2nknWMbOsMnZtf2WZ9l44P+r6VXI2TSpKpg/WVyI T5wdeD6fdlQjcd/FA/AFgdGnyhpytr35OQGj90Ns4iL4wTb0oj9YUT4hfSte1wisMl 19tHD8d3Yp60pVRIN/tTuI4fVnYwraeKcDfCIdwugCpDpy3Yq+CudPb26+NOTpRfY1 +9+EPR1xoHAIVxoird+dwr/il8jFYQ33wTiMqWxbpsMY7MVjyG6h89M974WNPpAMAb 4I3dhSF7eZRgg== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Finn Behrens , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , linux-kbuild@vger.kernel.org Subject: [PATCH v2 05/13] rust: simplify Clippy warning flags set Date: Tue, 9 Jul 2024 18:06:00 +0200 Message-ID: <20240709160615.998336-6-ojeda@kernel.org> In-Reply-To: <20240709160615.998336-1-ojeda@kernel.org> References: <20240709160615.998336-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Tested-by: Benno Lossin Tested-by: Andreas Hindborg Signed-off-by: Miguel Ojeda --- 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 \ From patchwork Tue Jul 9 16:06:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 13728282 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B28DA1A01D9; Tue, 9 Jul 2024 16:07:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720541233; cv=none; b=G0ksmIhm3iOleDHoiAZbiVfuj/SYCBVHL8YPJzynGBQdRolA52jMXiRih8E4VCZ3gH4AKaAoBpsIKDqlUlLxVyiM1WLGUgjnkKg7jzc3ysEDW9d1LaTjbZXQcF8MX269A7CIcwvlzfu3WkAHhJYPcMrkkB/jrKbFGqVzvSveRpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720541233; c=relaxed/simple; bh=yCLwDz7odLvzkn1cxL2z7lRFeJQYCgvIXeCLGgJDTvc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Bz/nwC8Jhx+eMHqblxxhsF4IIiq81k1r3bpdMhsDpEE3VJ44w+khLYVWclf9gzJF3IB81DwW1AY61SFrz0cs4WOMDgFQVXusUpOrSrXhBk4c+GWGiBAQ0X7Mtg12wvaUxsyn37WI7cP2D9KED5L7twz4gvZsVJ6fX8nGRnXASDs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HAOjWsSA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HAOjWsSA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 46D12C32786; Tue, 9 Jul 2024 16:07:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720541233; bh=yCLwDz7odLvzkn1cxL2z7lRFeJQYCgvIXeCLGgJDTvc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HAOjWsSAFUw6DAMjZdC587oVL6sn4UYd28rpjXXKnT6UIJpgdgNljuu+I8y7Hbmld Xk32fdkuA3NMI7mahRQBReMuQf54f/CAd46u4HA+rjiFlO9YhYTqpV09oYjS/V7hgA aTOdj4El/PWZDIXmjDLTSdZtNDkPpdE5NDZSbHixm7L8muI21FanPAsVkbGD7DN4fG FidViTfoxYf69yCmxcF8CRtrPt+fv41f+BP5LZTgYVbeUM+VUcAplykO+dRtBe8lkj hm6Aga3Vd8s1ZZpJaERR+XiDhwmHNcC+2gZoFxII1+/MFTXDifTnkd611n1lXKpN+N zUUp9y4usGRGA== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Finn Behrens , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , linux-kbuild@vger.kernel.org Subject: [PATCH v2 11/13] kbuild: rust: add `rustc-version` support Date: Tue, 9 Jul 2024 18:06:06 +0200 Message-ID: <20240709160615.998336-12-ojeda@kernel.org> In-Reply-To: <20240709160615.998336-1-ojeda@kernel.org> References: <20240709160615.998336-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Tested-by: Benno Lossin Tested-by: Andreas Hindborg Signed-off-by: Miguel Ojeda --- 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 -- 2.45.2 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 From patchwork Tue Jul 9 16:06:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 13728283 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25A5F19E7EA; Tue, 9 Jul 2024 16:07:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720541239; cv=none; b=g73/zgg9umW9vRX2sJiMwllmFgBWNeslnNBU5tq1o1y4ZPyH2iw7NjG6eNppusBGwVB4Os4f9tvRFzF1pv1IsrFysZ9irj7H1qtqSZYUzEzQx17ynkxw50UQqH9I5/DZJ00u54J0sPuNjLJM8McVzLjeLpMUAkx6rl0Rap0zMas= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720541239; c=relaxed/simple; bh=qwFmGLxVLsc8TBEvebCvtLHS2SudgK+Vr4hd0YC8QJE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sqD+1ywTUOJR7ZFZPEBcGTj2mOMqGoAcGfptgvYAfG+wd0tpg52Lt5l+Ks9kMJr5gefC4POifG8AqArW9yDbaI+k8XJqEzk7z6hOIi37aJV8klN4Xl4tHRSEc9XIvzZjxN/qgMQTxCLyjv0cfd1yIBlKFJct4oZ0ejnNiiQdPKk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZXtQIjKU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZXtQIjKU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E029C4AF0B; Tue, 9 Jul 2024 16:07:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720541238; bh=qwFmGLxVLsc8TBEvebCvtLHS2SudgK+Vr4hd0YC8QJE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZXtQIjKUAWzRTRbCnOlFdtNnquVf0BuV/FYLMvs/iwZdua4UOzTt06fh1hQn0kpao A5nMPfq/79g1TI5vOL7yT4Q4lBgtuS5WvbBE+/Sh2lm3Q5FpLg5Lbgn0Nf4WDLAE4l ci7c7WwObCnNUzs/v2bX6tD7w7J5l1l9Wl3kQamQvMTJsfLTKBXFkrHMT/q1WbPe9g KF/IMAHjD50LGDn09zdTEwLFQ3OxdcgApJ6de961qYjVS6POgm+cX8gtdy5JBzLIJr 8hGfaytjKWlwhShFZOtRuFKo8w1lImQVFvCDVQq0Si508l7CKsyV5yYp/DhIFN+gni TuH3WPMz8CHvg== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Finn Behrens , Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Andrew Morton , linux-kbuild@vger.kernel.org Subject: [PATCH v2 12/13] rust: support the new `-Zub-checks` flag Date: Tue, 9 Jul 2024 18:06:07 +0200 Message-ID: <20240709160615.998336-13-ojeda@kernel.org> In-Reply-To: <20240709160615.998336-1-ojeda@kernel.org> References: <20240709160615.998336-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Tested-by: Benno Lossin Tested-by: Andreas Hindborg Signed-off-by: Miguel Ojeda --- 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