Skip to contentSkip to conversation

rust: delete `ForeignOwnable::borrow_mut`

PATCH Applied Alice Ryhl 5 participants 5 messages Jul 6, 2023 – Aug 2, 2023

Subsystems (2)
lore
Share & tools
lore

Alice Ryhl

author maintainer
#0 lore raw

PATCH 1/1 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.

---
 rust/kernel/sync/arc.rs |  3 +--
 rust/kernel/types.rs    | 22 ++--------------------
 2 files changed, 3 insertions(+), 22 deletions(-)
rust/kernel/sync/arc.rs +1 -2
index a89843cacaad..172f563976a9 100644
@@ -243,8 +243,7 @@ unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> {
         let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();
 
         // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
-        // for the lifetime of the returned value. Additionally, the safety requirements of
-        // `ForeignOwnable::borrow_mut` ensure that no new mutable references are created.
+        // for the lifetime of the returned value.
         unsafe { ArcBorrow::new(inner) }
     }
 
rust/kernel/types.rs +2 -20
index 1e5380b16ed5..d479f8da8f38 100644
@@ -35,34 +35,16 @@ pub trait ForeignOwnable: Sized {
     ///
     /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
     /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
-    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow_mut`]
-    /// for this object must have been dropped.
     unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a>;
 
-    /// Mutably borrows a foreign-owned object.
-    ///
-    /// # Safety
-    ///
-    /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
-    /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
-    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
-    /// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
-    unsafe fn borrow_mut(ptr: *const core::ffi::c_void) -> ScopeGuard<Self, fn(Self)> {
-        // SAFETY: The safety requirements ensure that `ptr` came from a previous call to
-        // `into_foreign`.
-        ScopeGuard::new_with_data(unsafe { Self::from_foreign(ptr) }, |d| {
-            d.into_foreign();
-        })
-    }
-
     /// Converts a foreign-owned object back to a Rust-owned one.
     ///
     /// # Safety
     ///
     /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
     /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
-    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
-    /// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
+    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for
+    /// this object must have been dropped.
     unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self;
 }
 

base-commit: d2e3115d717197cb2bc020dd1f06b06538474ac3

Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Alice Ryhl

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.

Quoted 65 lines
---
  rust/kernel/sync/arc.rs |  3 +--
  rust/kernel/types.rs    | 22 ++--------------------
  2 files changed, 3 insertions(+), 22 deletions(-)
rust/kernel/sync/arc.rs +1 -2
index a89843cacaad..172f563976a9 100644
@@ -243,8 +243,7 @@ unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> {
          let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();
 
          // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
-        // for the lifetime of the returned value. Additionally, the safety requirements of
-        // `ForeignOwnable::borrow_mut` ensure that no new mutable references are created.
+        // for the lifetime of the returned value.
          unsafe { ArcBorrow::new(inner) }
      }
 
rust/kernel/types.rs +2 -20
index 1e5380b16ed5..d479f8da8f38 100644
@@ -35,34 +35,16 @@ pub trait ForeignOwnable: Sized {
      ///
      /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
      /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
-    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow_mut`]
-    /// for this object must have been dropped.
      unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a>;
 
-    /// Mutably borrows a foreign-owned object.
-    ///
-    /// # Safety
-    ///
-    /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
-    /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
-    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
-    /// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
-    unsafe fn borrow_mut(ptr: *const core::ffi::c_void) -> ScopeGuard<Self, fn(Self)> {
-        // SAFETY: The safety requirements ensure that `ptr` came from a previous call to
-        // `into_foreign`.
-        ScopeGuard::new_with_data(unsafe { Self::from_foreign(ptr) }, |d| {
-            d.into_foreign();
-        })
-    }
-
      /// Converts a foreign-owned object back to a Rust-owned one.
      ///
      /// # Safety
      ///
      /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
      /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
-    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
-    /// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
+    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for
+    /// this object must have been dropped.
      unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self;
  }
 

base-commit: d2e3115d717197cb2bc020dd1f06b06538474ac3 --
2.41.0.255.g8b1d071c50-goog

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Alice Ryhl
Quoted 9 lines · On Thu, 6 Jul 2023 09:46:15 +0000 Alice Ryhl <[email protected]> wrote:

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.

Quoted 62 lines
---
 rust/kernel/sync/arc.rs |  3 +--
 rust/kernel/types.rs    | 22 ++--------------------
 2 files changed, 3 insertions(+), 22 deletions(-)
rust/kernel/sync/arc.rs +1 -2
index a89843cacaad..172f563976a9 100644
@@ -243,8 +243,7 @@ unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> {
         let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();
 
         // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
-        // for the lifetime of the returned value. Additionally, the safety requirements of
-        // `ForeignOwnable::borrow_mut` ensure that no new mutable references are created.
+        // for the lifetime of the returned value.
         unsafe { ArcBorrow::new(inner) }
     }
 
rust/kernel/types.rs +2 -20
index 1e5380b16ed5..d479f8da8f38 100644
@@ -35,34 +35,16 @@ pub trait ForeignOwnable: Sized {
     ///
     /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
     /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
-    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow_mut`]
-    /// for this object must have been dropped.
     unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a>;
 
-    /// Mutably borrows a foreign-owned object.
-    ///
-    /// # Safety
-    ///
-    /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
-    /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
-    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
-    /// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
-    unsafe fn borrow_mut(ptr: *const core::ffi::c_void) -> ScopeGuard<Self, fn(Self)> {
-        // SAFETY: The safety requirements ensure that `ptr` came from a previous call to
-        // `into_foreign`.
-        ScopeGuard::new_with_data(unsafe { Self::from_foreign(ptr) }, |d| {
-            d.into_foreign();
-        })
-    }
-
     /// Converts a foreign-owned object back to a Rust-owned one.
     ///
     /// # Safety
     ///
     /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
     /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
-    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] and
-    /// [`ForeignOwnable::borrow_mut`] for this object must have been dropped.
+    /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for
+    /// this object must have been dropped.
     unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self;
 }
 

base-commit: d2e3115d717197cb2bc020dd1f06b06538474ac3

Reviewed-by on v1 1/1
#3 lore raw
In reply to Alice Ryhl
Quoted 11 lines · Alice Ryhl

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.

---
[...]

Applied
#4 lore raw
In reply to Alice Ryhl
Quoted 10 lines · Alice Ryhl

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.

Applied to rust-fixes, thanks! I added a Fixes tag.

Cheers,
Miguel