2024-04-01 21:23:53

by Miguel Ojeda

[permalink] [raw]
Subject: [PATCH 0/3] 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).

Link: https://lore.kernel.org/rust-for-linux/[email protected]/ [1]

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


2024-04-01 21:24:09

by Miguel Ojeda

[permalink] [raw]
Subject: [PATCH 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.

Link: https://github.com/rust-lang/rust-clippy/pull/10903 [1]
Signed-off-by: Miguel Ojeda <[email protected]>
---
rust/kernel/sync.rs | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs
index c983f63fd56e..0ab20975a3b5 100644
--- a/rust/kernel/sync.rs
+++ b/rust/kernel/sync.rs
@@ -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]
--
2.44.0


2024-04-01 21:24:29

by Miguel Ojeda

[permalink] [raw]
Subject: [PATCH 2/3] rust: kernel: remove redundant imports

Rust'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 prelude

Most 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.

Link: https://github.com/rust-lang/rust/pull/117772 [1]
Signed-off-by: Miguel Ojeda <[email protected]>
---
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(-)

diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs
index f1c2c4aa22d2..531b5e471cb1 100644
--- a/rust/kernel/alloc.rs
+++ b/rust/kernel/alloc.rs
@@ -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.
///
diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index ff88bce04fd4..229642960cd1 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -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.
diff --git a/rust/kernel/alloc/box_ext.rs b/rust/kernel/alloc/box_ext.rs
index 3b48cfc70deb..2d7254219957 100644
--- a/rust/kernel/alloc/box_ext.rs
+++ b/rust/kernel/alloc/box_ext.rs
@@ -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 {
diff --git a/rust/kernel/alloc/vec_ext.rs b/rust/kernel/alloc/vec_ext.rs
index 6a916fcf8bf1..25025a36e250 100644
--- a/rust/kernel/alloc/vec_ext.rs
+++ b/rust/kernel/alloc/vec_ext.rs
@@ -4,7 +4,6 @@

use super::{AllocError, Flags};
use alloc::vec::Vec;
-use core::result::Result;

/// Extensions to [`Vec`].
pub trait VecExt<T>: Sized {
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index fc986bc24c6d..55280ae9fe40 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -8,7 +8,6 @@

use alloc::alloc::LayoutError;

-use core::convert::From;
use core::fmt;
use core::num::TryFromIntError;
use core::str::Utf8Error;
diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
index 96e09c6e8530..fba19165aa64 100644
--- a/rust/kernel/net/phy.rs
+++ b/rust/kernel/net/phy.rs
@@ -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;

diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs
index 9b13aca832c2..a78aa3514a0a 100644
--- a/rust/kernel/print.rs
+++ b/rust/kernel/print.rs
@@ -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;

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 27641c3e4df8..bb8d4f41475b 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -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)]
diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index c2a3a2c7cbc5..b19645f93f16 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -17,7 +17,6 @@

use crate::{
alloc::{box_ext::BoxExt, AllocError, Flags},
- bindings,
error::{self, Error},
init::{self, InPlaceInit, Init, PinInit},
try_init,
diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs
index ef6ffef0aa88..2b306afbe56d 100644
--- a/rust/kernel/sync/condvar.rs
+++ b/rust/kernel/sync/condvar.rs
@@ -7,7 +7,6 @@

use super::{lock::Backend, lock::Guard, LockClassKey};
use crate::{
- bindings,
init::PinInit,
pin_init,
str::CStr,
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index 5b5c8efe427a..f6c34ca4d819 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -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;

diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs
index 93e1c982facf..30632070ee67 100644
--- a/rust/kernel/sync/lock/mutex.rs
+++ b/rust/kernel/sync/lock/mutex.rs
@@ -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
diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spinlock.rs
index 6e900807d3b7..ea5c5bc1ce12 100644
--- a/rust/kernel/sync/lock/spinlock.rs
+++ b/rust/kernel/sync/lock/spinlock.rs
@@ -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
diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index ca6e7e31d71c..55dff7e088bf 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -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,
diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs
index dfbf1080ccec..403801324172 100644
--- a/rust/kernel/workqueue.rs
+++ b/rust/kernel/workqueue.rs
@@ -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]
--
2.44.0


2024-04-01 21:27:40

by Miguel Ojeda

[permalink] [raw]
Subject: [PATCH 3/3] rust: upgrade to Rust 1.78.0

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 `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_*` `cfg`s 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].

Link: https://github.com/rust-lang/rust/blob/stable/RELEASES.md#version-1780-2024-05-02 [1]
Link: https://rust-for-linux.com/rust-version-policy [2]
Link: https://lore.kernel.org/rust-for-linux/[email protected]/ [3]
Link: https://github.com/Rust-for-Linux/linux/issues/2 [4]
Link: https://github.com/rust-lang/rust/pull/120062 [5]
Link: https://github.com/rust-lang/rust/pull/120055 [6]
Link: https://reviews.llvm.org/D86310 [7]
Link: https://github.com/rust-lang/rust/pull/117772 [8]
Link: https://github.com/rust-lang/rust-clippy/pull/10903 [9]
Link: https://github.com/rust-lang/rust/pull/119365 [10]
Link: https://github.com/rust-lang/rust/issues/119364 [11]
Link: https://lore.kernel.org/rust-for-linux/[email protected]/ [12]
Link: https://github.com/rust-lang/rust/pull/120932 [13]
Link: https://github.com/rust-lang/rust/issues/120969 [14]
Link: https://github.com/rust-lang/rust/pull/121202 [15]
Link: https://github.com/rust-lang/rust/pull/121237 [16]
Signed-off-by: Miguel Ojeda <[email protected]>
---
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(-)

diff --git a/Documentation/process/changes.rst b/Documentation/process/changes.rst
index b5d3107c6734..5d83958888e0 100644
--- a/Documentation/process/changes.rst
+++ b/Documentation/process/changes.rst
@@ -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
diff --git a/scripts/generate_rust_target.rs b/scripts/generate_rust_target.rs
index 54919cf48621..3fcbc3737b2e 100644
--- a/scripts/generate_rust_target.rs
+++ b/scripts/generate_rust_target.rs
@@ -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") {
diff --git a/scripts/min-tool-version.sh b/scripts/min-tool-version.sh
index 6086e00e640e..91c91201212c 100755
--- a/scripts/min-tool-version.sh
+++ b/scripts/min-tool-version.sh
@@ -33,7 +33,7 @@ llvm)
fi
;;
rustc)
- echo 1.77.1
+ echo 1.78.0
;;
bindgen)
echo 0.65.1
--
2.44.0

2024-04-01 21:52:58

by Miguel Ojeda

[permalink] [raw]
Subject: Re: [PATCH 3/3] rust: upgrade to Rust 1.78.0

On Mon, Apr 1, 2024 at 11:23 PM Miguel Ojeda <[email protected]> wrote:
>
> 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_*` `cfg`s we pass when compiling `core`/`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.

> 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].

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.

> 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].

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.

> 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

2024-04-01 22:29:52

by Boqun Feng

[permalink] [raw]
Subject: Re: [PATCH 1/3] rust: sync: implement `Default` for `LockClassKey`

On Mon, Apr 01, 2024 at 11:23:01PM +0200, Miguel Ojeda wrote:
> 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.
>
> Link: https://github.com/rust-lang/rust-clippy/pull/10903 [1]
> Signed-off-by: Miguel Ojeda <[email protected]>

Reviewed-by: Boqun Feng <[email protected]>

Regards,
Boqun

> ---
> rust/kernel/sync.rs | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs
> index c983f63fd56e..0ab20975a3b5 100644
> --- a/rust/kernel/sync.rs
> +++ b/rust/kernel/sync.rs
> @@ -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]
> --
> 2.44.0
>

2024-04-02 09:47:01

by Alice Ryhl

[permalink] [raw]
Subject: Re: [PATCH 1/3] rust: sync: implement `Default` for `LockClassKey`

On Mon, Apr 1, 2024 at 11:23 PM Miguel Ojeda <[email protected]> wrote:
>
> 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.
>
> Link: https://github.com/rust-lang/rust-clippy/pull/10903 [1]
> Signed-off-by: Miguel Ojeda <[email protected]>

Reviewed-by: Alice Ryhl <[email protected]>

2024-04-02 09:57:32

by Alice Ryhl

[permalink] [raw]
Subject: Re: [PATCH 2/3] rust: kernel: remove redundant imports

On Mon, Apr 1, 2024 at 11:23 PM Miguel Ojeda <[email protected]> wrote:
>
> Rust'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 prelude
>
> Most 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.
>
> Link: https://github.com/rust-lang/rust/pull/117772 [1]
> Signed-off-by: Miguel Ojeda <[email protected]>

Reviewed-by: Alice Ryhl <[email protected]>

2024-04-02 23:15:03

by Benno Lossin

[permalink] [raw]
Subject: Re: [PATCH 1/3] rust: sync: implement `Default` for `LockClassKey`

On 01.04.24 23:23, Miguel Ojeda wrote:
> 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.
>
> Link: https://github.com/rust-lang/rust-clippy/pull/10903 [1]
> Signed-off-by: Miguel Ojeda <[email protected]>
> ---
> rust/kernel/sync.rs | 6 ++++++
> 1 file changed, 6 insertions(+)

Reviewed-by: Benno Lossin <[email protected]>

--
Cheers,
Benno


2024-04-04 12:42:53

by Alice Ryhl

[permalink] [raw]
Subject: Re: [PATCH 3/3] rust: upgrade to Rust 1.78.0

On Mon, Apr 1, 2024 at 11:23 PM Miguel Ojeda <[email protected]> wrote:
>
> 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 `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_*` `cfg`s 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].
>
> Link: https://github.com/rust-lang/rust/blob/stable/RELEASES.md#version-1780-2024-05-02 [1]
> Link: https://rust-for-linux.com/rust-version-policy [2]
> Link: https://lore.kernel.org/rust-for-linux/[email protected]/ [3]
> Link: https://github.com/Rust-for-Linux/linux/issues/2 [4]
> Link: https://github.com/rust-lang/rust/pull/120062 [5]
> Link: https://github.com/rust-lang/rust/pull/120055 [6]
> Link: https://reviews.llvm.org/D86310 [7]
> Link: https://github.com/rust-lang/rust/pull/117772 [8]
> Link: https://github.com/rust-lang/rust-clippy/pull/10903 [9]
> Link: https://github.com/rust-lang/rust/pull/119365 [10]
> Link: https://github.com/rust-lang/rust/issues/119364 [11]
> Link: https://lore.kernel.org/rust-for-linux/[email protected]/ [12]
> Link: https://github.com/rust-lang/rust/pull/120932 [13]
> Link: https://github.com/rust-lang/rust/issues/120969 [14]
> Link: https://github.com/rust-lang/rust/pull/121202 [15]
> Link: https://github.com/rust-lang/rust/pull/121237 [16]
> Signed-off-by: Miguel Ojeda <[email protected]>

Reviewed-by: Alice Ryhl <[email protected]>

2024-05-05 22:35:24

by Miguel Ojeda

[permalink] [raw]
Subject: Re: [PATCH 0/3] Rust 1.78.0 upgrade

On Mon, Apr 1, 2024 at 11:23 PM Miguel Ojeda <[email protected]> wrote:
>
> 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).

[ Added a few more details and links I mentioned in the list. - Miguel ]

Applied to `rust-next` -- thanks everyone!

Cheers,
Miguel