Skip to content

Commit 701608b

Browse files
y86-devojeda
authored andcommitted
rust: sync: reduce stack usage of UniqueArc::try_new_uninit
`UniqueArc::try_new_uninit` calls `Arc::try_new(MaybeUninit::uninit())`. This results in the uninitialized memory being placed on the stack, which may be arbitrarily large due to the generic `T` and thus could cause a stack overflow for large types. Change the implementation to use the pin-init API which enables in-place initialization. In particular it avoids having to first construct and then move the uninitialized memory from the stack into the final location. Signed-off-by: Benno Lossin <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Gary Guo <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 692e893 commit 701608b

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

rust/kernel/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#[cfg(not(CONFIG_RUST))]
2929
compile_error!("Missing kernel configuration for conditional compilation");
3030

31-
#[allow(unused_extern_crates)]
3231
// Allow proc-macros to refer to `::kernel` inside the `kernel` crate (this crate).
3332
extern crate self as kernel;
3433

rust/kernel/sync/arc.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
use crate::{
1919
bindings,
2020
error::{self, Error},
21-
init::{InPlaceInit, Init, PinInit},
21+
init::{self, InPlaceInit, Init, PinInit},
22+
try_init,
2223
types::{ForeignOwnable, Opaque},
2324
};
2425
use alloc::boxed::Box;
@@ -31,6 +32,7 @@ use core::{
3132
pin::Pin,
3233
ptr::NonNull,
3334
};
35+
use macros::pin_data;
3436

3537
mod std_vendor;
3638

@@ -125,6 +127,7 @@ pub struct Arc<T: ?Sized> {
125127
_p: PhantomData<ArcInner<T>>,
126128
}
127129

130+
#[pin_data]
128131
#[repr(C)]
129132
struct ArcInner<T: ?Sized> {
130133
refcount: Opaque<bindings::refcount_t>,
@@ -505,9 +508,16 @@ impl<T> UniqueArc<T> {
505508

506509
/// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet.
507510
pub fn try_new_uninit() -> Result<UniqueArc<MaybeUninit<T>>, AllocError> {
508-
Ok(UniqueArc::<MaybeUninit<T>> {
511+
// INVARIANT: The refcount is initialised to a non-zero value.
512+
let inner = Box::try_init::<AllocError>(try_init!(ArcInner {
513+
// SAFETY: There are no safety requirements for this FFI call.
514+
refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
515+
data <- init::uninit::<T, AllocError>(),
516+
}? AllocError))?;
517+
Ok(UniqueArc {
509518
// INVARIANT: The newly-created object has a ref-count of 1.
510-
inner: Arc::try_new(MaybeUninit::uninit())?,
519+
// SAFETY: The pointer from the `Box` is valid.
520+
inner: unsafe { Arc::from_inner(Box::leak(inner).into()) },
511521
})
512522
}
513523
}

0 commit comments

Comments
 (0)