Rust 1.78.0 upgrade
Subsystems (8)
Miguel Ojeda
maintainercover Rust 1.78.0 upgrade
This is the first upgrade without the alloc fork.
In other words, it is based on top of Wedson's "Allocation APIs" series [1], applied on top of the current rust-next, i.e. commit 9ffe2a730313 ("rust: str: add {make,to}_{upper,lower}case() to CString").
Please note that Rust 1.78.0 will be released in a month (2024-05-02).
Miguel Ojeda (3):
rust: sync: implement Default for LockClassKey
rust: kernel: remove redundant imports
rust: upgrade to Rust 1.78.0
Documentation/process/changes.rst | 2 +- rust/kernel/alloc.rs | 1 - rust/kernel/alloc/allocator.rs | 2 -- rust/kernel/alloc/box_ext.rs | 1 - rust/kernel/alloc/vec_ext.rs | 1 - rust/kernel/error.rs | 1 - rust/kernel/net/phy.rs | 2 +- rust/kernel/print.rs | 5 ----- rust/kernel/str.rs | 5 +---- rust/kernel/sync.rs | 6 ++++++ rust/kernel/sync/arc.rs | 1 - rust/kernel/sync/condvar.rs | 1 - rust/kernel/sync/lock.rs | 2 +- rust/kernel/sync/lock/mutex.rs | 2 -- rust/kernel/sync/lock/spinlock.rs | 2 -- rust/kernel/task.rs | 2 +- rust/kernel/workqueue.rs | 4 +--- scripts/generate_rust_target.rs | 2 +- scripts/min-tool-version.sh | 2 +- 19 files changed, 14 insertions(+), 30 deletions(-)
--
2.44.0
Miguel Ojeda
maintainerPATCH 1/3 rust: sync: implement `Default` for `LockClassKey`
In the upcoming Rust 1.78.0, Clippy suggests to implement Default even when new() is const, since Default::default() may call const functions even if it is not const itself [1]:
error: you should consider adding a `Default` implementation for `LockClassKey`
--> rust/kernel/sync.rs:31:5
|
31 | / pub const fn new() -> Self {
32 | | Self(Opaque::uninit())
33 | | }
| |_____^Thus implement it.
--- rust/kernel/sync.rs | 6 ++++++ 1 file changed, 6 insertions(+)
rust/kernel/sync.rs +6 -0
@@ -37,6 +37,12 @@ pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key { } } +impl Default for LockClassKey { +fn default () ->Self { +Self ::new () + } +} +/// Defines a new static lock class and returns a pointer to it. #[doc(hidden)] #[macro_export]
Signature · 1 line
2.44.0
Miguel Ojeda
maintainerPATCH 2/3 rust: kernel: remove redundant imports
In reply to Miguel OjedaRust's unused_imports lint covers both unused and redundant imports. In the upcoming 1.78.0, the lint detects more cases of redundant imports [1], e.g.:
error: the item `bindings` is imported redundantly
--> rust/kernel/print.rs:38:9
|
38 | use crate::bindings;
| ^^^^^^^^^^^^^^^ the item `bindings` is already defined by preludeMost cases are use crate::bindings, plus a few other items like Box. Thus clean them up.
Note that, in the bindings case, the message "defined by prelude" above means the extern prelude, i.e. the --extern flags we pass.
--- rust/kernel/alloc.rs | 1 - rust/kernel/alloc/allocator.rs | 2 -- rust/kernel/alloc/box_ext.rs | 1 - rust/kernel/alloc/vec_ext.rs | 1 - rust/kernel/error.rs | 1 - rust/kernel/net/phy.rs | 2 +- rust/kernel/print.rs | 5 ----- rust/kernel/str.rs | 5 +---- rust/kernel/sync/arc.rs | 1 - rust/kernel/sync/condvar.rs | 1 - rust/kernel/sync/lock.rs | 2 +- rust/kernel/sync/lock/mutex.rs | 2 -- rust/kernel/sync/lock/spinlock.rs | 2 -- rust/kernel/task.rs | 2 +- rust/kernel/workqueue.rs | 4 +--- 15 files changed, 5 insertions(+), 27 deletions(-)
rust/kernel/alloc.rs +0 -1
@@ -46,7 +46,6 @@ fn not(self) -> Self::Output {/// These are meant to be used in functions that can allocate memory. pub mod flags {use super ::Flags ; -use crate ::bindings;/// Zeroes out the allocated memory. ///
rust/kernel/alloc/allocator.rs +0 -2
@@ -6,8 +6,6 @@use core::alloc::{GlobalAlloc ,Layout };use core::ptr; -use crate ::bindings; -struct KernelAllocator ;/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment.
rust/kernel/alloc/box_ext.rs +0 -1
@@ -5,7 +5,6 @@use super ::{AllocError ,Flags };use alloc::boxed::Box ;use core::mem::MaybeUninit ; -use core::result::Result ;/// Extensions to [`Box`]. pub trait BoxExt <T >:Sized {
rust/kernel/alloc/vec_ext.rs +0 -1
@@ -4,7 +4,6 @@use super ::{AllocError ,Flags };use alloc::vec::Vec ; -use core::result::Result ;/// Extensions to [`Vec`]. pub trait VecExt <T >:Sized {
rust/kernel/error.rs +0 -1
@@ -8,7 +8,6 @@use alloc::alloc::LayoutError ; -use core::convert::From ;use core::fmt;use core::num::TryFromIntError ;use core::str::Utf8Error ;
rust/kernel/net/phy.rs +1 -1
@@ -6,7 +6,7 @@//! //! C headers: [`include/linux/phy.h`](srctree/include/linux/phy.h). -use crate ::{bindings, error::*, prelude::*, str::CStr , types::Opaque }; +use crate ::{error::*, prelude::*, types::Opaque };use core::marker::PhantomData ;
rust/kernel/print.rs +0 -5
@@ -13,9 +13,6 @@use crate ::str::RawFormatter ; -#[cfg( CONFIG_PRINTK )] -use crate ::bindings; -// Called from `vsprintf` with format specifier `%pA`. #[no_mangle] unsafe extern "C" fn rust_fmt_argument(
@@ -35,8 +32,6 @@/// Public but hidden since it should only be used from public macros. #[doc(hidden)] pub mod format_strings { -use crate ::bindings; -/// The length we copy from the `KERN_*` kernel prefixes. const LENGTH_PREFIX :usize =2 ;
rust/kernel/str.rs +1 -4
@@ -7,10 +7,7 @@use core::fmt::{self,Write };use core::ops::{self,Deref ,DerefMut ,Index }; -use crate ::{ - bindings, - error::{code::*,Error }, -}; +use crate ::error::{code::*,Error };/// Byte string without UTF-8 validity guarantee. #[repr(transparent)]
rust/kernel/sync/arc.rs +0 -1
@@ -17,7 +17,6 @@use crate ::{ alloc::{box_ext::BoxExt ,AllocError ,Flags }, - bindings, error::{self,Error }, init::{self,InPlaceInit ,Init ,PinInit }, try_init,
rust/kernel/sync/condvar.rs +0 -1
@@ -7,7 +7,6 @@use super ::{lock::Backend , lock::Guard ,LockClassKey };use crate ::{ - bindings, init::PinInit , pin_init, str::CStr ,
rust/kernel/sync/lock.rs +1 -1
@@ -6,7 +6,7 @@//! spinlocks, raw spinlocks) to be provided with minimal effort. use super ::LockClassKey ; -use crate ::{bindings, init::PinInit , pin_init, str::CStr , types::Opaque , types::ScopeGuard }; +use crate ::{init::PinInit , pin_init, str::CStr , types::Opaque , types::ScopeGuard };use core::{cell::UnsafeCell , marker::PhantomData , marker::PhantomPinned };use macros::pin_data;
rust/kernel/sync/lock/mutex.rs +0 -2
@@ -4,8 +4,6 @@//! //! This module allows Rust code to use the kernel's `struct mutex`. -use crate ::bindings; -/// Creates a [`Mutex`] initialiser with the given name and a newly-created lock class. /// /// It uses the name if one is given, otherwise it generates one based on the file name and line
rust/kernel/sync/lock/spinlock.rs +0 -2
@@ -4,8 +4,6 @@//! //! This module allows Rust code to use the kernel's `spinlock_t`. -use crate ::bindings; -/// Creates a [`SpinLock`] initialiser with the given name and a newly-created lock class. /// /// It uses the name if one is given, otherwise it generates one based on the file name and line
rust/kernel/task.rs +1 -1
@@ -4,7 +4,7 @@//! //! C header: [`include/linux/sched.h`](srctree/include/linux/sched.h). -use crate ::{bindings, types::Opaque }; +use crate ::types::Opaque ;use core::{ ffi::{c_int, c_long, c_uint}, marker::PhantomData ,
rust/kernel/workqueue.rs +1 -3
@@ -133,10 +133,8 @@//! C header: [`include/linux/workqueue.h`](srctree/include/linux/workqueue.h) use crate ::alloc::{AllocError ,Flags }; -use crate ::{bindings, prelude::*, sync::Arc , sync::LockClassKey , types::Opaque }; -use alloc::boxed::Box ; +use crate ::{prelude::*, sync::Arc , sync::LockClassKey , types::Opaque };use core::marker::PhantomData ; -use core::pin::Pin ;/// Creates a [`Work`] initialiser with the given name and a newly-created lock class. #[macro_export]
Signature · 1 line
2.44.0
Miguel Ojeda
maintainerPATCH 3/3 rust: upgrade to Rust 1.78.0
In reply to Miguel OjedaThis is the next upgrade to the Rust toolchain, from 1.77.1 to 1.78.0 (i.e. the latest) [1].
See the upgrade policy [2] and the comments on the first upgrade in commit 3ed03f4da06e ("rust: upgrade to Rust 1.68.2").
# Unstable features
There have been no changes to the set of unstable features used in our own code. Therefore, the only unstable features allowed to be used outside the kernel crate is still new_uninit.
However, since we are finally dropping our alloc fork [3], all the unstable features used by alloc (~30 language ones, ~60 library ones) are not a concern anymore. This reduces the maintenance burden, increases the chances of new compiler versions working without changes and gets us closer to the goal of supporting several compiler versions.
It also means that, ignoring non-language/library features, we are currently left with just the few language features needed to implement the kernel Arc, the new_uninit library feature, the compiler_builtins marker and the few no_* cfgs we pass when compiling core/alloc.
Please see [4] for details.
# Required changes
## LLVM's data layout
Rust 1.77.0 (i.e. the previous upgrade) introduced a check for matching LLVM data layouts [5]. Then, Rust 1.78.0 upgraded LLVM's bundled major version from 17 to 18 [6], which changed the data layout in x86 [7]. Thus update the data layout in our custom target specification for x86 so that the compiler does not complain about the mismatch:
error: data-layout for target `target-5559158138856098584`,
`e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128`,
differs from LLVM target's `x86_64-linux-gnu` default layout,
`e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128`In the future, the goal is to drop the custom target specifications. Meanwhile, if we want to support other LLVM versions used in rustc (e.g. for LTO), we will need to add some extra logic (e.g. conditional on LLVM's version, or extracting the data layout from an existing built-in target specification).
## unused_imports
Rust's unused_imports lint covers both unused and redundant imports. Now, in 1.78.0, the lint detects more cases of redundant imports [8]. Thus one of the previous patches cleaned them up.
## Clippy's new_without_default
Clippy now suggests to implement Default even when new() is const, since Default::default() may call const functions even if it is not const itself [9]. Thus one of the previous patches implemented it.
# Other changes in Rust
Rust 1.78.0 introduced feature(asm_goto) [10] [11]. This feature was discussed in the past [12].
Rust 1.78.0 introduced support for mutable pointers to Rust statics, including a test case for the Linux kernel's VTABLE use case [13].
Rust 1.78.0 with debug assertions enabled (i.e. -Cdebug-assertions=y, kernel's CONFIG_RUST_DEBUG_ASSERTIONS=y) now always checks all unsafe preconditions, without a way to opt-out for particular cases [14].
Rust 1.78.0 also improved a couple issues we reported when giving feedback for the new --check-cfg feature [15] [16].
# alloc upgrade and reviewing
As mentioned above, compiler upgrades will not update alloc anymore, since we are dropping our alloc fork [3].
---
Please note that Rust 1.78.0 will be released in a month (2024-05-02).
Documentation/process/changes.rst | 2 +- scripts/generate_rust_target.rs | 2 +- scripts/min-tool-version.sh | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
Documentation/process/changes.rst +1 -1
@@ -31,7 +31,7 @@ you probably needn't concern yourself with pcmciautils. ====================== =============== ======================================== GNU C 5.1 gcc --version Clang/LLVM (optional) 13.0.1 clang --version -Rust (optional) 1.77.1 rustc --version +Rust (optional) 1.78.0 rustc --version bindgen (optional) 0.65.1 bindgen --version GNU make 3.82 make --version bash 4.2 bash --version
scripts/generate_rust_target.rs +1 -1
@@ -154,7 +154,7 @@ fn main() { ts.push ("arch" ,"x86_64" ); ts.push ("data-layout" , -"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" , +"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" , );let mut features ="-3dnow,-3dnowa,-mmx,+soft-float" .to_string ();if cfg.has ("MITIGATION_RETPOLINE" ) {
scripts/min-tool-version.sh +1 -1
@@ -33,7 +33,7 @@ llvm) fi ;; rustc) - echo 1.77.1 + echo 1.78.0 ;; bindgen) echo 0.65.1
--
2.44.0
Re: [PATCH 3/3] rust: upgrade to Rust 1.78.0
Quoted 5 lines · Miguel Ojeda
It also means that, ignoring non-language/library features, we are currently left with just the few language features needed to implement the kernel
Arc, thenew_uninitlibrary feature, thecompiler_builtinsmarker and the fewno_*cfgs we pass when compilingcore/alloc.
To be clear, by "currently" I meant in mainline, i.e. it does not mean that we will not need/add a few more language/library features in the future.
Quoted 2 lines
Rust 1.78.0 introduced support for mutable pointers to Rust statics, including a test case for the Linux kernel's
VTABLEuse case [13].
I should have mentioned constants so that it makes sense. I will link to const_refs_to_static, i.e.
https://github.com/rust-lang/rust/issues/119618.
Quoted 3 lines
Rust 1.78.0 with debug assertions enabled (i.e.
-Cdebug-assertions=y, kernel'sCONFIG_RUST_DEBUG_ASSERTIONS=y) now always checks all unsafe preconditions, without a way to opt-out for particular cases [14].
It would be ideal to have a way to selectively disable certain checks per-call site for this one (i.e. not just per check but for particular instances of a check), even if the vast majority of the checks remain in place. I discuss the details a bit more at
https://github.com/Rust-for-Linux/linux/issues/354.
Quoted 1 line
Please note that Rust 1.78.0 will be released in a month (2024-05-02).
By the way, I tested these with arm64, loongarch64 and x86_64.
Cheers,
Miguel
Re: [PATCH 1/3] rust: sync: implement `Default` for `LockClassKey`
In reply to Miguel OjedaQuoted 12 lines · Miguel Ojeda
In the upcoming Rust 1.78.0, Clippy suggests to implement
Defaulteven whennew()isconst, sinceDefault::default()may callconstfunctions even if it is notconstitself [1]:error: you should consider adding a `Default` implementation for `LockClassKey` --> rust/kernel/sync.rs:31:5 | 31 | / pub const fn new() -> Self { 32 | | Self(Opaque::uninit()) 33 | | } | |_____^
Quoted 4 lines
Thus implement it.
Signed-off-by Miguel Ojeda <[email protected]>
Regards,
Boqun
Quoted 24 lines
--- rust/kernel/sync.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/sync.rs +6
-0@@ -37,6 +37,12 @@ pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key { } } +impl Default for LockClassKey { +fn default () ->Self { +Self ::new () + } +} +/// Defines a new static lock class and returns a pointer to it. #[doc(hidden)] #[macro_export] Signature · 1 line
2.44.0
Re: [PATCH 1/3] rust: sync: implement `Default` for `LockClassKey`
In reply to Miguel OjedaQuoted 13 lines · Miguel Ojeda
In the upcoming Rust 1.78.0, Clippy suggests to implement
Defaulteven whennew()isconst, sinceDefault::default()may callconstfunctions even if it is notconstitself [1]:error: you should consider adding a `Default` implementation for `LockClassKey` --> rust/kernel/sync.rs:31:5 | 31 | / pub const fn new() -> Self { 32 | | Self(Opaque::uninit()) 33 | | } | |_____^
Quoted 4 lines
Thus implement it.
Signed-off-by Miguel Ojeda <[email protected]>
Re: [PATCH 2/3] rust: kernel: remove redundant imports
In reply to Miguel OjedaQuoted 11 lines · Miguel Ojeda
Rust's
unused_importslint covers both unused and redundant imports. In the upcoming 1.78.0, the lint detects more cases of redundant imports [1], e.g.:error: the item `bindings` is imported redundantly --> rust/kernel/print.rs:38:9 | 38 | use crate::bindings; | ^^^^^^^^^^^^^^^ the item `bindings` is already defined by prelude
Quoted 8 lines
Most cases are
use crate::bindings, plus a few other items likeBox. Thus clean them up.Note that, in the
bindingscase, the message "defined by prelude" above means the extern prelude, i.e. the--externflags we pass.Signed-off-by Miguel Ojeda <[email protected]>
Re: [PATCH 1/3] rust: sync: implement `Default` for `LockClassKey`
In reply to Miguel OjedaQuoted 12 lines · Miguel Ojeda
In the upcoming Rust 1.78.0, Clippy suggests to implement
Defaulteven whennew()isconst, sinceDefault::default()may callconstfunctions even if it is notconstitself [1]:error: you should consider adding a `Default` implementation for `LockClassKey` --> rust/kernel/sync.rs:31:5 | 31 | / pub const fn new() -> Self { 32 | | Self(Opaque::uninit()) 33 | | } | |_____^
Quoted 7 lines
Thus implement it.
Signed-off-by Miguel Ojeda <[email protected]>--- rust/kernel/sync.rs | 6 ++++++ 1 file changed, 6 insertions(+)
Signature · 2 lines
Cheers, Benno
Re: [PATCH 3/3] rust: upgrade to Rust 1.78.0
In reply to Miguel OjedaQuoted 88 lines · Miguel Ojeda
This is the next upgrade to the Rust toolchain, from 1.77.1 to 1.78.0 (i.e. the latest) [1].
See the upgrade policy [2] and the comments on the first upgrade in commit
3ed03f4da06e("rust: upgrade to Rust 1.68.2").# Unstable features
There have been no changes to the set of unstable features used in our own code. Therefore, the only unstable features allowed to be used outside the
kernelcrate is stillnew_uninit.However, since we are finally dropping our
allocfork [3], all the unstable features used byalloc(~30 language ones, ~60 library ones) are not a concern anymore. This reduces the maintenance burden, increases the chances of new compiler versions working without changes and gets us closer to the goal of supporting several compiler versions.It also means that, ignoring non-language/library features, we are currently left with just the few language features needed to implement the kernel
Arc, thenew_uninitlibrary feature, thecompiler_builtinsmarker and the fewno_*cfgs we pass when compilingcore/alloc.Please see [4] for details.
# Required changes
## LLVM's data layout
Rust 1.77.0 (i.e. the previous upgrade) introduced a check for matching LLVM data layouts [5]. Then, Rust 1.78.0 upgraded LLVM's bundled major version from 17 to 18 [6], which changed the data layout in x86 [7]. Thus update the data layout in our custom target specification for x86 so that the compiler does not complain about the mismatch:
error: data-layout for target `target-5559158138856098584`, `e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128`, differs from LLVM target's `x86_64-linux-gnu` default layout, `e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128`In the future, the goal is to drop the custom target specifications. Meanwhile, if we want to support other LLVM versions used in
rustc(e.g. for LTO), we will need to add some extra logic (e.g. conditional on LLVM's version, or extracting the data layout from an existing built-in target specification).##
unused_importsRust's
unused_importslint covers both unused and redundant imports. Now, in 1.78.0, the lint detects more cases of redundant imports [8]. Thus one of the previous patches cleaned them up.## Clippy's
new_without_defaultClippy now suggests to implement
Defaulteven whennew()isconst, sinceDefault::default()may callconstfunctions even if it is notconstitself [9]. Thus one of the previous patches implemented it.# Other changes in Rust
Rust 1.78.0 introduced
feature(asm_goto)[10] [11]. This feature was discussed in the past [12].Rust 1.78.0 introduced support for mutable pointers to Rust statics, including a test case for the Linux kernel's
VTABLEuse case [13].Rust 1.78.0 with debug assertions enabled (i.e.
-Cdebug-assertions=y, kernel'sCONFIG_RUST_DEBUG_ASSERTIONS=y) now always checks all unsafe preconditions, without a way to opt-out for particular cases [14].Rust 1.78.0 also improved a couple issues we reported when giving feedback for the new
--check-cfgfeature [15] [16].#
allocupgrade and reviewingAs mentioned above, compiler upgrades will not update
allocanymore, since we are dropping ourallocfork [3].Link https://reviews.llvm.org/D86310 [7]
Quoted 8 lines
Signed-off-by Miguel Ojeda <[email protected]>
Miguel Ojeda
AppliedQuoted 8 lines · Miguel Ojeda
This is the first upgrade without the
allocfork.In other words, it is based on top of Wedson's "Allocation APIs" series [1], applied on top of the current
rust-next, i.e. commit9ffe2a730313("rust: str: add {make,to}_{upper,lower}case() to CString").Please note that Rust 1.78.0 will be released in a month (2024-05-02).
[ Added a few more details and links I mentioned in the list. - Miguel ]
Applied to rust-next -- thanks everyone!
Cheers,
Miguel