Skip to content

Commit

Permalink
rust: sync: arc: Add lockdep integration
Browse files Browse the repository at this point in the history
Signed-off-by: Asahi Lina <lina@asahilina.net>
  • Loading branch information
asahilina committed May 28, 2023
1 parent 8830532 commit 98f5852
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/Kconfig.debug
Original file line number Diff line number Diff line change
Expand Up @@ -2880,6 +2880,14 @@ config RUST_BUILD_ASSERT_ALLOW

If unsure, say N.

config RUST_EXTRA_LOCKDEP
bool "Extra lockdep checking"
depends on RUST && PROVE_LOCKING
help
Enabled additional lockdep integration with certain Rust types.

If unsure, say N.

endmenu # "Rust"

endmenu # Kernel hacking
6 changes: 6 additions & 0 deletions rust/kernel/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,7 @@ pub trait InPlaceInit<T>: Sized {
/// type.
///
/// If `T: !Unpin` it will not be able to move afterwards.
#[track_caller]
fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
where
E: From<AllocError>;
Expand All @@ -1497,6 +1498,7 @@ pub trait InPlaceInit<T>: Sized {
/// type.
///
/// If `T: !Unpin` it will not be able to move afterwards.
#[track_caller]
fn pin_init<E>(init: impl PinInit<T, E>) -> error::Result<Pin<Self>>
where
Error: From<E>,
Expand All @@ -1509,11 +1511,13 @@ pub trait InPlaceInit<T>: Sized {
}

/// Use the given initializer to in-place initialize a `T`.
#[track_caller]
fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>
where
E: From<AllocError>;

/// Use the given initializer to in-place initialize a `T`.
#[track_caller]
fn init<E>(init: impl Init<T, E>) -> error::Result<Self>
where
Error: From<E>,
Expand Down Expand Up @@ -1558,6 +1562,7 @@ impl<T> InPlaceInit<T> for Box<T> {

impl<T> InPlaceInit<T> for UniqueArc<T> {
#[inline]
#[track_caller]
fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
where
E: From<AllocError>,
Expand All @@ -1572,6 +1577,7 @@ impl<T> InPlaceInit<T> for UniqueArc<T> {
}

#[inline]
#[track_caller]
fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>
where
E: From<AllocError>,
Expand Down
71 changes: 68 additions & 3 deletions rust/kernel/sync/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ use core::{
};
use macros::pin_data;

#[cfg(CONFIG_RUST_EXTRA_LOCKDEP)]
use crate::sync::lockdep::LockdepMap;

mod std_vendor;

/// A reference-counted pointer to an instance of `T`.
Expand Down Expand Up @@ -127,6 +130,17 @@ pub struct Arc<T: ?Sized> {
_p: PhantomData<ArcInner<T>>,
}

#[cfg(CONFIG_RUST_EXTRA_LOCKDEP)]
#[pin_data]
#[repr(C)]
struct ArcInner<T: ?Sized> {
refcount: Opaque<bindings::refcount_t>,
lockdep_map: LockdepMap,
data: T,
}

// FIXME: pin_data does not work well with cfg attributes within the struct definition.
#[cfg(not(CONFIG_RUST_EXTRA_LOCKDEP))]
#[pin_data]
#[repr(C)]
struct ArcInner<T: ?Sized> {
Expand Down Expand Up @@ -157,11 +171,14 @@ unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}

impl<T> Arc<T> {
/// Constructs a new reference counted instance of `T`.
#[track_caller]
pub fn try_new(contents: T) -> Result<Self, AllocError> {
// INVARIANT: The refcount is initialised to a non-zero value.
let value = ArcInner {
// SAFETY: There are no safety requirements for this FFI call.
refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
#[cfg(CONFIG_RUST_EXTRA_LOCKDEP)]
lockdep_map: LockdepMap::new(),
data: contents,
};

Expand All @@ -176,6 +193,7 @@ impl<T> Arc<T> {
///
/// If `T: !Unpin` it will not be able to move afterwards.
#[inline]
#[track_caller]
pub fn pin_init<E>(init: impl PinInit<T, E>) -> error::Result<Self>
where
Error: From<E>,
Expand All @@ -187,6 +205,7 @@ impl<T> Arc<T> {
///
/// This is equivalent to [`pin_init`], since an [`Arc`] is always pinned.
#[inline]
#[track_caller]
pub fn init<E>(init: impl Init<T, E>) -> error::Result<Self>
where
Error: From<E>,
Expand Down Expand Up @@ -279,15 +298,46 @@ impl<T: ?Sized> Drop for Arc<T> {
// freed/invalid memory as long as it is never dereferenced.
let refcount = unsafe { self.ptr.as_ref() }.refcount.get();

// SAFETY: By the type invariant, there is necessarily a reference to the object.
// We cannot hold the map lock across the reference decrement, as we might race
// another thread. Therefore, we lock and immediately drop the guard here. This
// only serves to inform lockdep of the dependency up the call stack.
#[cfg(CONFIG_RUST_EXTRA_LOCKDEP)]
unsafe { self.ptr.as_ref() }.lockdep_map.lock();

// INVARIANT: If the refcount reaches zero, there are no other instances of `Arc`, and
// this instance is being dropped, so the broken invariant is not observable.
// SAFETY: Also by the type invariant, we are allowed to decrement the refcount.
let is_zero = unsafe { bindings::refcount_dec_and_test(refcount) };

if is_zero {
// The count reached zero, we must free the memory.
//
// SAFETY: The pointer was initialised from the result of `Box::leak`.
unsafe { Box::from_raw(self.ptr.as_ptr()) };

// SAFETY: If we get this far, we had the last reference to the object.
// That means we are responsible for freeing it, so we can safely lock
// the fake lock again. This wraps dropping the inner object, which
// informs lockdep of the dependencies down the call stack.
#[cfg(CONFIG_RUST_EXTRA_LOCKDEP)]
let guard = unsafe { self.ptr.as_ref() }.lockdep_map.lock();

// SAFETY: The pointer was initialised from the result of `Box::leak`,
// and the value is valid.
unsafe { core::ptr::drop_in_place(&mut self.ptr.as_mut().data) };

// We need to drop the lock guard before freeing the LockdepMap itself
#[cfg(CONFIG_RUST_EXTRA_LOCKDEP)]
core::mem::drop(guard);

// SAFETY: The pointer was initialised from the result of `Box::leak`,
// and the lockdep map is valid.
#[cfg(CONFIG_RUST_EXTRA_LOCKDEP)]
unsafe {
core::ptr::drop_in_place(&mut self.ptr.as_mut().lockdep_map)
};

// SAFETY: The pointer was initialised from the result of `Box::leak`, and
// a ManuallyDrop<T> is compatible. We already dropped the contents above.
unsafe { Box::from_raw(self.ptr.as_ptr() as *mut ManuallyDrop<ArcInner<T>>) };
}
}
}
Expand Down Expand Up @@ -499,6 +549,7 @@ pub struct UniqueArc<T: ?Sized> {

impl<T> UniqueArc<T> {
/// Tries to allocate a new [`UniqueArc`] instance.
#[track_caller]
pub fn try_new(value: T) -> Result<Self, AllocError> {
Ok(Self {
// INVARIANT: The newly-created object has a ref-count of 1.
Expand All @@ -507,13 +558,27 @@ impl<T> UniqueArc<T> {
}

/// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet.
#[track_caller]
pub fn try_new_uninit() -> Result<UniqueArc<MaybeUninit<T>>, AllocError> {
// INVARIANT: The refcount is initialised to a non-zero value.
#[cfg(CONFIG_RUST_EXTRA_LOCKDEP)]
let inner = {
let map = LockdepMap::new();
Box::try_init::<AllocError>(try_init!(ArcInner {
// SAFETY: There are no safety requirements for this FFI call.
refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
lockdep_map: map,
data <- init::uninit::<T, AllocError>(),
}? AllocError))?
};
// FIXME: try_init!() does not work with cfg attributes.
#[cfg(not(CONFIG_RUST_EXTRA_LOCKDEP))]
let inner = Box::try_init::<AllocError>(try_init!(ArcInner {
// SAFETY: There are no safety requirements for this FFI call.
refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
data <- init::uninit::<T, AllocError>(),
}? AllocError))?;

Ok(UniqueArc {
// INVARIANT: The newly-created object has a ref-count of 1.
// SAFETY: The pointer from the `Box` is valid.
Expand Down

0 comments on commit 98f5852

Please sign in to comment.