Skip to content

Commit 1d24eb2

Browse files
Darksonnojeda
authored andcommitted
rust: delete ForeignOwnable::borrow_mut
We discovered that the current design of `borrow_mut` is problematic. This patch removes it until a better solution can be found. Specifically, the current design gives you access to a `&mut T`, which lets you change where the `ForeignOwnable` points (e.g., with `core::mem::swap`). No upcoming user of this API intended to make that possible, making all of them unsound. Signed-off-by: Alice Ryhl <[email protected]> Reviewed-by: Gary Guo <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Fixes: 0fc4424 ("rust: types: introduce `ForeignOwnable`") Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent b3d8aa8 commit 1d24eb2

File tree

2 files changed

+3
-22
lines changed

2 files changed

+3
-22
lines changed

rust/kernel/sync/arc.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ impl<T: 'static> ForeignOwnable for Arc<T> {
243243
let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();
244244

245245
// SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
246-
// for the lifetime of the returned value. Additionally, the safety requirements of
247-
// `ForeignOwnable::borrow_mut` ensure that no new mutable references are created.
246+
// for the lifetime of the returned value.
248247
unsafe { ArcBorrow::new(inner) }
249248
}
250249

rust/kernel/types.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,16 @@ pub trait ForeignOwnable: Sized {
3535
///
3636
/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
3737
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
38-
/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow_mut`]
39-
/// for this object must have been dropped.
4038
unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a>;
4139

42-
/// Mutably borrows a foreign-owned object.
43-
///
44-
/// # Safety
45-
///
46-
/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
47-
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
48-
/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
49-
/// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
50-
unsafe fn borrow_mut(ptr: *const core::ffi::c_void) -> ScopeGuard<Self, fn(Self)> {
51-
// SAFETY: The safety requirements ensure that `ptr` came from a previous call to
52-
// `into_foreign`.
53-
ScopeGuard::new_with_data(unsafe { Self::from_foreign(ptr) }, |d| {
54-
d.into_foreign();
55-
})
56-
}
57-
5840
/// Converts a foreign-owned object back to a Rust-owned one.
5941
///
6042
/// # Safety
6143
///
6244
/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
6345
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
64-
/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
65-
/// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
46+
/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for
47+
/// this object must have been dropped.
6648
unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self;
6749
}
6850

0 commit comments

Comments
 (0)