On Thu, Jun 04, 2026 at 10:11:14PM +0200, Andreas Hindborg wrote: > From: Asahi Lina <[email protected]> > > By analogy to `AlwaysRefCounted` and `ARef`, an `Ownable` type is a > (typically C FFI) type that *may* be owned by Rust, but need not be. Unlike > `AlwaysRefCounted`, this mechanism expects the reference to be unique > within Rust, and does not allow cloning. > > Conceptually, this is similar to a `KBox<T>`, except that it delegates > resource management to the `T` instead of using a generic allocator. > > [ om: > - Split code into separate file and `pub use` it from types.rs. > - Make from_raw() and into_raw() public. > - Remove OwnableMut, and make DerefMut dependent on Unpin instead. > - Usage example/doctest for Ownable/Owned. > - Fixes to documentation and commit message. > ] > > Link: > https://lore.kernel.org/all/[email protected]/ > Signed-off-by: Asahi Lina <[email protected]> > Co-developed-by: Oliver Mangold <[email protected]> > Signed-off-by: Oliver Mangold <[email protected]> > Reviewed-by: Boqun Feng <[email protected]> > Reviewed-by: Daniel Almeida <[email protected]> > [ Andreas: Updated documentation, examples, and formatting. Change safety > requirements, safety comments. Use a reference for `release`. ] > Reviewed-by: Gary Guo <[email protected]> > Co-developed-by: Andreas Hindborg <[email protected]> > Signed-off-by: Andreas Hindborg <[email protected]>
Overall looks good to me, but two nits below. With them fixed: Reviewed-by: Alice Ryhl <[email protected]> > +pub trait Ownable { > + /// Tear down this `Ownable`. > + /// > + /// Implementers of `Ownable` can use this function to clean up the use > of `Self`. This can > + /// include freeing the underlying object. > + /// > + /// # Safety > + /// > + /// Callers must ensure that the caller has exclusive ownership of `T`, > and this ownership can > + /// be transferred to the `release` method. > + unsafe fn release(&mut self); I'd make this take a raw pointer because the pointer can be freed during the execution of release(), which references don't allow. > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs > index 4329d3c2c2e5..4aec7b699269 100644 > --- a/rust/kernel/types.rs > +++ b/rust/kernel/types.rs > @@ -11,6 +11,17 @@ > }; > use pin_init::{PinInit, Wrapper, Zeroable}; > > +pub use crate::{ > + owned::{ > + Ownable, > + Owned, // > + }, > + sync::aref::{ > + ARef, > + AlwaysRefCounted, // > + }, // > +}; We removed the types::ARef re-export, so you shouldn't add it back. Alice
