mirror of
git://git.yoctoproject.org/linux-yocto.git
synced 2025-07-05 05:15:23 +02:00

Toolchain and infrastructure: - Support 'MITIGATION_{RETHUNK,RETPOLINE,SLS}' (which cleans up objtool warnings), teach objtool about 'noreturn' Rust symbols and mimic '___ADDRESSABLE()' for 'module_{init,exit}'. With that, we should be objtool-warning-free, so enable it to run for all Rust object files. - KASAN (no 'SW_TAGS'), KCFI and shadow call sanitizer support. - Support 'RUSTC_VERSION', including re-config and re-build on change. - Split helpers file into several files in a folder, to avoid conflicts in it. Eventually those files will be moved to the right places with the new build system. In addition, remove the need to manually export the symbols defined there, reusing existing machinery for that. - Relax restriction on configurations with Rust + GCC plugins to just the RANDSTRUCT plugin. 'kernel' crate: - New 'list' module: doubly-linked linked list for use with reference counted values, which is heavily used by the upcoming Rust Binder. This includes 'ListArc' (a wrapper around 'Arc' that is guaranteed unique for the given ID), 'AtomicTracker' (tracks whether a 'ListArc' exists using an atomic), 'ListLinks' (the prev/next pointers for an item in a linked list), 'List' (the linked list itself), 'Iter' (an iterator over a 'List'), 'Cursor' (a cursor into a 'List' that allows to remove elements), 'ListArcField' (a field exclusively owned by a 'ListArc'), as well as support for heterogeneous lists. - New 'rbtree' module: red-black tree abstractions used by the upcoming Rust Binder. This includes 'RBTree' (the red-black tree itself), 'RBTreeNode' (a node), 'RBTreeNodeReservation' (a memory reservation for a node), 'Iter' and 'IterMut' (immutable and mutable iterators), 'Cursor' (bidirectional cursor that allows to remove elements), as well as an entry API similar to the Rust standard library one. - 'init' module: add 'write_[pin_]init' methods and the 'InPlaceWrite' trait. Add the 'assert_pinned!' macro. - 'sync' module: implement the 'InPlaceInit' trait for 'Arc' by introducing an associated type in the trait. - 'alloc' module: add 'drop_contents' method to 'BoxExt'. - 'types' module: implement the 'ForeignOwnable' trait for 'Pin<Box<T>>' and improve the trait's documentation. In addition, add the 'into_raw' method to the 'ARef' type. - 'error' module: in preparation for the upcoming Rust support for 32-bit architectures, like arm, locally allow Clippy lint for those. Documentation: - https://rust.docs.kernel.org has been announced, so link to it. - Enable rustdoc's "jump to definition" feature, making its output a bit closer to the experience in a cross-referencer. - Debian Testing now also provides recent Rust releases (outside of the freeze period), so add it to the list. MAINTAINERS: - Trevor is joining as reviewer of the "RUST" entry. And a few other small bits. -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEPjU5OPd5QIZ9jqqOGXyLc2htIW0FAmbzNz4ACgkQGXyLc2ht IW3muA/9HcPL0QqVB5+SqSRqcatmrFU/wq8Oaa6Z/No0JaynqyikK+R1WNokUd/5 WpQi4PC1OYV+ekyAuWdkooKmaSqagH5r53XlezNw+cM5zo8y7p0otVlbepQ0t3Ky pVEmfDRIeSFXsKrg91BJUKyJf70TQlgSggDVCExlanfOjPz88C1+s3EcJ/XWYGKQ cRk/XDdbF5eNaldp2MriVF0fw7XktgIrmVzxt/z0lb4PE7RaCAnO6gSQI+90Vb2d zvyOYKS4AkqE3suFvDIIUlPUv+8XbACj0c4wvBZHH5uZGTbgWUffqygJ45GqChEt c4fS/+E8VaM1z0EvxNczC0nQkfLwkTc1mgbP+sG3VZJMPVCJ2zQan1/ond7GqCpw pt6uQaGvDsAvllm7sbiAIVaAY81icqyYWKfNBXLLEL7DhY5je5Wq+E83XQ8d5u5F EuapnZhW3y12d6UCsSe9bD8W45NFoWHPXky1TzT+whTxnX1yH9YsPXbJceGSbbgd Lw3GmUtZx2bVAMToVjNFD2lPA3OmPY1e2lk0jwzTuQrEXfnZYuzbjqs3YUijb7xR AlsWfIb0IHBwHWpB7da24ezqWP2VD4eaDdD8/+LmDSj6XLngxMNWRLKmXT000eTW vIFP9GJrvag2R3YFPhrurgGpRsp8HUTLtvcZROxp2JVQGQ7Z4Ww= =52BN -----END PGP SIGNATURE----- Merge tag 'rust-6.12' of https://github.com/Rust-for-Linux/linux Pull Rust updates from Miguel Ojeda: "Toolchain and infrastructure: - Support 'MITIGATION_{RETHUNK,RETPOLINE,SLS}' (which cleans up objtool warnings), teach objtool about 'noreturn' Rust symbols and mimic '___ADDRESSABLE()' for 'module_{init,exit}'. With that, we should be objtool-warning-free, so enable it to run for all Rust object files. - KASAN (no 'SW_TAGS'), KCFI and shadow call sanitizer support. - Support 'RUSTC_VERSION', including re-config and re-build on change. - Split helpers file into several files in a folder, to avoid conflicts in it. Eventually those files will be moved to the right places with the new build system. In addition, remove the need to manually export the symbols defined there, reusing existing machinery for that. - Relax restriction on configurations with Rust + GCC plugins to just the RANDSTRUCT plugin. 'kernel' crate: - New 'list' module: doubly-linked linked list for use with reference counted values, which is heavily used by the upcoming Rust Binder. This includes 'ListArc' (a wrapper around 'Arc' that is guaranteed unique for the given ID), 'AtomicTracker' (tracks whether a 'ListArc' exists using an atomic), 'ListLinks' (the prev/next pointers for an item in a linked list), 'List' (the linked list itself), 'Iter' (an iterator over a 'List'), 'Cursor' (a cursor into a 'List' that allows to remove elements), 'ListArcField' (a field exclusively owned by a 'ListArc'), as well as support for heterogeneous lists. - New 'rbtree' module: red-black tree abstractions used by the upcoming Rust Binder. This includes 'RBTree' (the red-black tree itself), 'RBTreeNode' (a node), 'RBTreeNodeReservation' (a memory reservation for a node), 'Iter' and 'IterMut' (immutable and mutable iterators), 'Cursor' (bidirectional cursor that allows to remove elements), as well as an entry API similar to the Rust standard library one. - 'init' module: add 'write_[pin_]init' methods and the 'InPlaceWrite' trait. Add the 'assert_pinned!' macro. - 'sync' module: implement the 'InPlaceInit' trait for 'Arc' by introducing an associated type in the trait. - 'alloc' module: add 'drop_contents' method to 'BoxExt'. - 'types' module: implement the 'ForeignOwnable' trait for 'Pin<Box<T>>' and improve the trait's documentation. In addition, add the 'into_raw' method to the 'ARef' type. - 'error' module: in preparation for the upcoming Rust support for 32-bit architectures, like arm, locally allow Clippy lint for those. Documentation: - https://rust.docs.kernel.org has been announced, so link to it. - Enable rustdoc's "jump to definition" feature, making its output a bit closer to the experience in a cross-referencer. - Debian Testing now also provides recent Rust releases (outside of the freeze period), so add it to the list. MAINTAINERS: - Trevor is joining as reviewer of the "RUST" entry. And a few other small bits" * tag 'rust-6.12' of https://github.com/Rust-for-Linux/linux: (54 commits) kasan: rust: Add KASAN smoke test via UAF kbuild: rust: Enable KASAN support rust: kasan: Rust does not support KHWASAN kbuild: rust: Define probing macros for rustc kasan: simplify and clarify Makefile rust: cfi: add support for CFI_CLANG with Rust cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS rust: support for shadow call stack sanitizer docs: rust: include other expressions in conditional compilation section kbuild: rust: replace proc macros dependency on `core.o` with the version text kbuild: rust: rebuild if the version text changes kbuild: rust: re-run Kconfig if the version text changes kbuild: rust: add `CONFIG_RUSTC_VERSION` rust: avoid `box_uninit_write` feature MAINTAINERS: add Trevor Gross as Rust reviewer rust: rbtree: add `RBTree::entry` rust: rbtree: add cursor rust: rbtree: add mutable iterator rust: rbtree: add iterator rust: rbtree: add red-black tree implementation backed by the C version ...
90 lines
3.2 KiB
Rust
90 lines
3.2 KiB
Rust
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
//! Extensions to [`Box`] for fallible allocations.
|
|
|
|
use super::{AllocError, Flags};
|
|
use alloc::boxed::Box;
|
|
use core::{mem::MaybeUninit, ptr, result::Result};
|
|
|
|
/// Extensions to [`Box`].
|
|
pub trait BoxExt<T>: Sized {
|
|
/// Allocates a new box.
|
|
///
|
|
/// The allocation may fail, in which case an error is returned.
|
|
fn new(x: T, flags: Flags) -> Result<Self, AllocError>;
|
|
|
|
/// Allocates a new uninitialised box.
|
|
///
|
|
/// The allocation may fail, in which case an error is returned.
|
|
fn new_uninit(flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError>;
|
|
|
|
/// Drops the contents, but keeps the allocation.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```
|
|
/// use kernel::alloc::{flags, box_ext::BoxExt};
|
|
/// let value = Box::new([0; 32], flags::GFP_KERNEL)?;
|
|
/// assert_eq!(*value, [0; 32]);
|
|
/// let mut value = Box::drop_contents(value);
|
|
/// // Now we can re-use `value`:
|
|
/// value.write([1; 32]);
|
|
/// // SAFETY: We just wrote to it.
|
|
/// let value = unsafe { value.assume_init() };
|
|
/// assert_eq!(*value, [1; 32]);
|
|
/// # Ok::<(), Error>(())
|
|
/// ```
|
|
fn drop_contents(this: Self) -> Box<MaybeUninit<T>>;
|
|
}
|
|
|
|
impl<T> BoxExt<T> for Box<T> {
|
|
fn new(x: T, flags: Flags) -> Result<Self, AllocError> {
|
|
let mut b = <Self as BoxExt<_>>::new_uninit(flags)?;
|
|
b.write(x);
|
|
// SAFETY: We just wrote to it.
|
|
Ok(unsafe { b.assume_init() })
|
|
}
|
|
|
|
#[cfg(any(test, testlib))]
|
|
fn new_uninit(_flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError> {
|
|
Ok(Box::new_uninit())
|
|
}
|
|
|
|
#[cfg(not(any(test, testlib)))]
|
|
fn new_uninit(flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError> {
|
|
let ptr = if core::mem::size_of::<MaybeUninit<T>>() == 0 {
|
|
core::ptr::NonNull::<_>::dangling().as_ptr()
|
|
} else {
|
|
let layout = core::alloc::Layout::new::<MaybeUninit<T>>();
|
|
|
|
// SAFETY: Memory is being allocated (first arg is null). The only other source of
|
|
// safety issues is sleeping on atomic context, which is addressed by klint. Lastly,
|
|
// the type is not a SZT (checked above).
|
|
let ptr =
|
|
unsafe { super::allocator::krealloc_aligned(core::ptr::null_mut(), layout, flags) };
|
|
if ptr.is_null() {
|
|
return Err(AllocError);
|
|
}
|
|
|
|
ptr.cast::<MaybeUninit<T>>()
|
|
};
|
|
|
|
// SAFETY: For non-zero-sized types, we allocate above using the global allocator. For
|
|
// zero-sized types, we use `NonNull::dangling`.
|
|
Ok(unsafe { Box::from_raw(ptr) })
|
|
}
|
|
|
|
fn drop_contents(this: Self) -> Box<MaybeUninit<T>> {
|
|
let ptr = Box::into_raw(this);
|
|
// SAFETY: `ptr` is valid, because it came from `Box::into_raw`.
|
|
unsafe { ptr::drop_in_place(ptr) };
|
|
|
|
// CAST: `MaybeUninit<T>` is a transparent wrapper of `T`.
|
|
let ptr = ptr.cast::<MaybeUninit<T>>();
|
|
|
|
// SAFETY: `ptr` is valid for writes, because it came from `Box::into_raw` and it is valid for
|
|
// reads, since the pointer came from `Box::into_raw` and the type is `MaybeUninit<T>`.
|
|
unsafe { Box::from_raw(ptr) }
|
|
}
|
|
}
|