mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-12 13:55:32 +00:00
![Miguel Ojeda](/assets/img/avatar_default.png)
This is the first upgrade to the Rust toolchain since the initial Rust
merge, from 1.62.0 to 1.68.2 (i.e. the latest).
# Context
The kernel currently supports only a single Rust version [1] (rather
than a minimum) given our usage of some "unstable" Rust features [2]
which do not promise backwards compatibility.
The goal is to reach a point where we can declare a minimum version for
the toolchain. For instance, by waiting for some of the features to be
stabilized. Therefore, the first minimum Rust version that the kernel
will support is "in the future".
# Upgrade policy
Given we will eventually need to reach that minimum version, it would be
ideal to upgrade the compiler from time to time to be as close as
possible to that goal and find any issues sooner. In the extreme, we
could upgrade as soon as a new Rust release is out. Of course, upgrading
so often is in stark contrast to what one normally would need for GCC
and LLVM, especially given the release schedule: 6 weeks for Rust vs.
half a year for LLVM and a year for GCC.
Having said that, there is no particular advantage to updating slowly
either: kernel developers in "stable" distributions are unlikely to be
able to use their distribution-provided Rust toolchain for the kernel
anyway [3]. Instead, by routinely upgrading to the latest instead,
kernel developers using Linux distributions that track the latest Rust
release may be able to use those rather than Rust-provided ones,
especially if their package manager allows to pin / hold back /
downgrade the version for some days during windows where the version may
not match. For instance, Arch, Fedora, Gentoo and openSUSE all provide
and track the latest version of Rust as they get released every 6 weeks.
Then, when the minimum version is reached, we will stop upgrading and
decide how wide the window of support will be. For instance, a year of
Rust versions. We will probably want to start small, and then widen it
over time, just like the kernel did originally for LLVM, see commit
3519c4d6e0
("Documentation: add minimum clang/llvm version").
# Unstable features stabilized
This upgrade allows us to remove the following unstable features since
they were stabilized:
- `feature(explicit_generic_args_with_impl_trait)` (1.63).
- `feature(core_ffi_c)` (1.64).
- `feature(generic_associated_types)` (1.65).
- `feature(const_ptr_offset_from)` (1.65, *).
- `feature(bench_black_box)` (1.66, *).
- `feature(pin_macro)` (1.68).
The ones marked with `*` apply only to our old `rust` branch, not
mainline yet, i.e. only for code that we may potentially upstream.
With this patch applied, the only unstable feature allowed to be used
outside the `kernel` crate is `new_uninit`, though other code to be
upstreamed may increase the list.
Please see [2] for details.
# Other required changes
Since 1.63, `rustdoc` triggers the `broken_intra_doc_links` lint for
links pointing to exported (`#[macro_export]`) `macro_rules`. An issue
was opened upstream [4], but it turns out it is intended behavior. For
the moment, just add an explicit reference for each link. Later we can
revisit this if `rustdoc` removes the compatibility measure.
Nevertheless, this was helpful to discover a link that was pointing to
the wrong place unintentionally. Since that one was actually wrong, it
is fixed in a previous commit independently.
Another change was the addition of `cfg(no_rc)` and `cfg(no_sync)` in
upstream [5], thus remove our original changes for that.
Similarly, upstream now tests that it compiles successfully with
`#[cfg(not(no_global_oom_handling))]` [6], which allow us to get rid
of some changes, such as an `#[allow(dead_code)]`.
In addition, remove another `#[allow(dead_code)]` due to new uses
within the standard library.
Finally, add `try_extend_trusted` and move the code in `spec_extend.rs`
since upstream moved it for the infallible version.
# `alloc` upgrade and reviewing
There are a large amount of changes, but the vast majority of them are
due to our `alloc` fork being upgraded at once.
There are two kinds of changes to be aware of: the ones coming from
upstream, which we should follow as closely as possible, and the updates
needed in our added fallible APIs to keep them matching the newer
infallible APIs coming from upstream.
Instead of taking a look at the diff of this patch, an alternative
approach is reviewing a diff of the changes between upstream `alloc` and
the kernel's. This allows to easily inspect the kernel additions only,
especially to check if the fallible methods we already have still match
the infallible ones in the new version coming from upstream.
Another approach is reviewing the changes introduced in the additions in
the kernel fork between the two versions. This is useful to spot
potentially unintended changes to our additions.
To apply these approaches, one may follow steps similar to the following
to generate a pair of patches that show the differences between upstream
Rust and the kernel (for the subset of `alloc` we use) before and after
applying this patch:
# Get the difference with respect to the old version.
git -C rust checkout $(linux/scripts/min-tool-version.sh rustc)
git -C linux ls-tree -r --name-only HEAD -- rust/alloc |
cut -d/ -f3- |
grep -Fv README.md |
xargs -IPATH cp rust/library/alloc/src/PATH linux/rust/alloc/PATH
git -C linux diff --patch-with-stat --summary -R > old.patch
git -C linux restore rust/alloc
# Apply this patch.
git -C linux am rust-upgrade.patch
# Get the difference with respect to the new version.
git -C rust checkout $(linux/scripts/min-tool-version.sh rustc)
git -C linux ls-tree -r --name-only HEAD -- rust/alloc |
cut -d/ -f3- |
grep -Fv README.md |
xargs -IPATH cp rust/library/alloc/src/PATH linux/rust/alloc/PATH
git -C linux diff --patch-with-stat --summary -R > new.patch
git -C linux restore rust/alloc
Now one may check the `new.patch` to take a look at the additions (first
approach) or at the difference between those two patches (second
approach). For the latter, a side-by-side tool is recommended.
Link: https://rust-for-linux.com/rust-version-policy [1]
Link: https://github.com/Rust-for-Linux/linux/issues/2 [2]
Link: https://lore.kernel.org/rust-for-linux/CANiq72mT3bVDKdHgaea-6WiZazd8Mvurqmqegbe5JZxVyLR8Yg@mail.gmail.com/ [3]
Link: https://github.com/rust-lang/rust/issues/106142 [4]
Link: https://github.com/rust-lang/rust/pull/89891 [5]
Link: https://github.com/rust-lang/rust/pull/98652 [6]
Reviewed-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-By: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Tested-by: Ariel Miculas <amiculas@cisco.com>
Tested-by: David Gow <davidgow@google.com>
Tested-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20230418214347.324156-4-ojeda@kernel.org
[ Removed `feature(core_ffi_c)` from `uapi` ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
257 lines
8.8 KiB
Rust
257 lines
8.8 KiB
Rust
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
|
|
use crate::alloc::{Allocator, Global};
|
|
use core::fmt;
|
|
use core::iter::{FusedIterator, TrustedLen};
|
|
use core::mem::{self, ManuallyDrop, SizedTypeProperties};
|
|
use core::ptr::{self, NonNull};
|
|
use core::slice::{self};
|
|
|
|
use super::Vec;
|
|
|
|
/// A draining iterator for `Vec<T>`.
|
|
///
|
|
/// This `struct` is created by [`Vec::drain`].
|
|
/// See its documentation for more.
|
|
///
|
|
/// # Example
|
|
///
|
|
/// ```
|
|
/// let mut v = vec![0, 1, 2];
|
|
/// let iter: std::vec::Drain<_> = v.drain(..);
|
|
/// ```
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
|
pub struct Drain<
|
|
'a,
|
|
T: 'a,
|
|
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global,
|
|
> {
|
|
/// Index of tail to preserve
|
|
pub(super) tail_start: usize,
|
|
/// Length of tail
|
|
pub(super) tail_len: usize,
|
|
/// Current remaining range to remove
|
|
pub(super) iter: slice::Iter<'a, T>,
|
|
pub(super) vec: NonNull<Vec<T, A>>,
|
|
}
|
|
|
|
#[stable(feature = "collection_debug", since = "1.17.0")]
|
|
impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
f.debug_tuple("Drain").field(&self.iter.as_slice()).finish()
|
|
}
|
|
}
|
|
|
|
impl<'a, T, A: Allocator> Drain<'a, T, A> {
|
|
/// Returns the remaining items of this iterator as a slice.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```
|
|
/// let mut vec = vec!['a', 'b', 'c'];
|
|
/// let mut drain = vec.drain(..);
|
|
/// assert_eq!(drain.as_slice(), &['a', 'b', 'c']);
|
|
/// let _ = drain.next().unwrap();
|
|
/// assert_eq!(drain.as_slice(), &['b', 'c']);
|
|
/// ```
|
|
#[must_use]
|
|
#[stable(feature = "vec_drain_as_slice", since = "1.46.0")]
|
|
pub fn as_slice(&self) -> &[T] {
|
|
self.iter.as_slice()
|
|
}
|
|
|
|
/// Returns a reference to the underlying allocator.
|
|
#[unstable(feature = "allocator_api", issue = "32838")]
|
|
#[must_use]
|
|
#[inline]
|
|
pub fn allocator(&self) -> &A {
|
|
unsafe { self.vec.as_ref().allocator() }
|
|
}
|
|
|
|
/// Keep unyielded elements in the source `Vec`.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```
|
|
/// #![feature(drain_keep_rest)]
|
|
///
|
|
/// let mut vec = vec!['a', 'b', 'c'];
|
|
/// let mut drain = vec.drain(..);
|
|
///
|
|
/// assert_eq!(drain.next().unwrap(), 'a');
|
|
///
|
|
/// // This call keeps 'b' and 'c' in the vec.
|
|
/// drain.keep_rest();
|
|
///
|
|
/// // If we wouldn't call `keep_rest()`,
|
|
/// // `vec` would be empty.
|
|
/// assert_eq!(vec, ['b', 'c']);
|
|
/// ```
|
|
#[unstable(feature = "drain_keep_rest", issue = "101122")]
|
|
pub fn keep_rest(self) {
|
|
// At this moment layout looks like this:
|
|
//
|
|
// [head] [yielded by next] [unyielded] [yielded by next_back] [tail]
|
|
// ^-- start \_________/-- unyielded_len \____/-- self.tail_len
|
|
// ^-- unyielded_ptr ^-- tail
|
|
//
|
|
// Normally `Drop` impl would drop [unyielded] and then move [tail] to the `start`.
|
|
// Here we want to
|
|
// 1. Move [unyielded] to `start`
|
|
// 2. Move [tail] to a new start at `start + len(unyielded)`
|
|
// 3. Update length of the original vec to `len(head) + len(unyielded) + len(tail)`
|
|
// a. In case of ZST, this is the only thing we want to do
|
|
// 4. Do *not* drop self, as everything is put in a consistent state already, there is nothing to do
|
|
let mut this = ManuallyDrop::new(self);
|
|
|
|
unsafe {
|
|
let source_vec = this.vec.as_mut();
|
|
|
|
let start = source_vec.len();
|
|
let tail = this.tail_start;
|
|
|
|
let unyielded_len = this.iter.len();
|
|
let unyielded_ptr = this.iter.as_slice().as_ptr();
|
|
|
|
// ZSTs have no identity, so we don't need to move them around.
|
|
let needs_move = mem::size_of::<T>() != 0;
|
|
|
|
if needs_move {
|
|
let start_ptr = source_vec.as_mut_ptr().add(start);
|
|
|
|
// memmove back unyielded elements
|
|
if unyielded_ptr != start_ptr {
|
|
let src = unyielded_ptr;
|
|
let dst = start_ptr;
|
|
|
|
ptr::copy(src, dst, unyielded_len);
|
|
}
|
|
|
|
// memmove back untouched tail
|
|
if tail != (start + unyielded_len) {
|
|
let src = source_vec.as_ptr().add(tail);
|
|
let dst = start_ptr.add(unyielded_len);
|
|
ptr::copy(src, dst, this.tail_len);
|
|
}
|
|
}
|
|
|
|
source_vec.set_len(start + unyielded_len + this.tail_len);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[stable(feature = "vec_drain_as_slice", since = "1.46.0")]
|
|
impl<'a, T, A: Allocator> AsRef<[T]> for Drain<'a, T, A> {
|
|
fn as_ref(&self) -> &[T] {
|
|
self.as_slice()
|
|
}
|
|
}
|
|
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
|
unsafe impl<T: Sync, A: Sync + Allocator> Sync for Drain<'_, T, A> {}
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
|
unsafe impl<T: Send, A: Send + Allocator> Send for Drain<'_, T, A> {}
|
|
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
|
impl<T, A: Allocator> Iterator for Drain<'_, T, A> {
|
|
type Item = T;
|
|
|
|
#[inline]
|
|
fn next(&mut self) -> Option<T> {
|
|
self.iter.next().map(|elt| unsafe { ptr::read(elt as *const _) })
|
|
}
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
self.iter.size_hint()
|
|
}
|
|
}
|
|
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
|
impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> {
|
|
#[inline]
|
|
fn next_back(&mut self) -> Option<T> {
|
|
self.iter.next_back().map(|elt| unsafe { ptr::read(elt as *const _) })
|
|
}
|
|
}
|
|
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
|
impl<T, A: Allocator> Drop for Drain<'_, T, A> {
|
|
fn drop(&mut self) {
|
|
/// Moves back the un-`Drain`ed elements to restore the original `Vec`.
|
|
struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>);
|
|
|
|
impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> {
|
|
fn drop(&mut self) {
|
|
if self.0.tail_len > 0 {
|
|
unsafe {
|
|
let source_vec = self.0.vec.as_mut();
|
|
// memmove back untouched tail, update to new length
|
|
let start = source_vec.len();
|
|
let tail = self.0.tail_start;
|
|
if tail != start {
|
|
let src = source_vec.as_ptr().add(tail);
|
|
let dst = source_vec.as_mut_ptr().add(start);
|
|
ptr::copy(src, dst, self.0.tail_len);
|
|
}
|
|
source_vec.set_len(start + self.0.tail_len);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
let iter = mem::replace(&mut self.iter, (&mut []).iter());
|
|
let drop_len = iter.len();
|
|
|
|
let mut vec = self.vec;
|
|
|
|
if T::IS_ZST {
|
|
// ZSTs have no identity, so we don't need to move them around, we only need to drop the correct amount.
|
|
// this can be achieved by manipulating the Vec length instead of moving values out from `iter`.
|
|
unsafe {
|
|
let vec = vec.as_mut();
|
|
let old_len = vec.len();
|
|
vec.set_len(old_len + drop_len + self.tail_len);
|
|
vec.truncate(old_len + self.tail_len);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
// ensure elements are moved back into their appropriate places, even when drop_in_place panics
|
|
let _guard = DropGuard(self);
|
|
|
|
if drop_len == 0 {
|
|
return;
|
|
}
|
|
|
|
// as_slice() must only be called when iter.len() is > 0 because
|
|
// it also gets touched by vec::Splice which may turn it into a dangling pointer
|
|
// which would make it and the vec pointer point to different allocations which would
|
|
// lead to invalid pointer arithmetic below.
|
|
let drop_ptr = iter.as_slice().as_ptr();
|
|
|
|
unsafe {
|
|
// drop_ptr comes from a slice::Iter which only gives us a &[T] but for drop_in_place
|
|
// a pointer with mutable provenance is necessary. Therefore we must reconstruct
|
|
// it from the original vec but also avoid creating a &mut to the front since that could
|
|
// invalidate raw pointers to it which some unsafe code might rely on.
|
|
let vec_ptr = vec.as_mut().as_mut_ptr();
|
|
let drop_offset = drop_ptr.sub_ptr(vec_ptr);
|
|
let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len);
|
|
ptr::drop_in_place(to_drop);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
|
impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> {
|
|
fn is_empty(&self) -> bool {
|
|
self.iter.is_empty()
|
|
}
|
|
}
|
|
|
|
#[unstable(feature = "trusted_len", issue = "37572")]
|
|
unsafe impl<T, A: Allocator> TrustedLen for Drain<'_, T, A> {}
|
|
|
|
#[stable(feature = "fused", since = "1.26.0")]
|
|
impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {}
|