rust: add `Ownable` trait and `Owned` type
Subsystems (18)
Andreas Hindborg
maintainerPATCH v14 3/9 rust: Add missing SAFETY documentation for `ARef` example
In reply to Andreas HindborgFrom: Oliver Mangold <[email protected]>
SAFETY comment in rustdoc example was just 'TODO'. Fixed.
Original patch by Oliver Mangold <[email protected]> [1].
--- rust/kernel/sync/aref.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
rust/kernel/sync/aref.rs +6 -4
@@ -129,12 +129,14 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {/// # Examples /// /// ``` -/// use core::ptr::NonNull; -/// use kernel::sync::aref::{ARef, RefCounted}; +/// # use core::ptr::NonNull; +/// # use kernel::sync::aref::{ARef, RefCounted}; /// /// struct Empty {} /// -/// # // SAFETY: TODO. +/// // SAFETY: The `RefCounted` implementation for `Empty` does not count references and never +/// // frees the underlying object. Thus we can act as owning an increment on the refcount for +/// // the object that we pass to the newly created `ARef`. /// unsafe impl RefCounted for Empty { /// fn inc_ref(&self) {} /// unsafe fn dec_ref(_obj: NonNull<Self>) {}
@@ -142,7 +144,7 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {/// /// let mut data = Empty {}; /// let ptr = NonNull::<Empty>::new(&mut data).unwrap(); -/// # // SAFETY: TODO. +/// // SAFETY: We keep `data` around longer than the `ARef`. /// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) }; /// let raw_ptr: NonNull<Empty> = ARef::into_raw(data_ref); ///
Signature · 1 line
2.51.2
Andreas Hindborg
maintainerPATCH v14 6/9 rust: page: update formatting of `use` statements
In reply to Andreas HindborgUpdate formatting in preparation for next patch
--- rust/kernel/page.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
rust/kernel/page.rs +9 -3
@@ -3,17 +3,23 @@//! Kernel page allocation and management. use crate ::{ - alloc::{AllocError ,Flags }, + alloc::{ +AllocError , +Flags ,// + }, bindings, error::code::*, error::Result , - uaccess::UserSliceReader , + uaccess::UserSliceReader ,// };use core::{ marker::PhantomData , mem::ManuallyDrop , ops::Deref , - ptr::{self,NonNull }, + ptr::{ + self, +NonNull ,// + },// };/// A bitwise shift for the page size.
Signature · 1 line
2.51.2
Andreas Hindborg
maintainerv14 cover rust: add `Ownable` trait and `Owned` type
Add a new trait Ownable and type Owned for types that specify their own way of performing allocation and destruction. This is useful for types from the C side.
Add the trait OwnableRefCounted that allows conversion between ARef and Owned. This is analogous to conversion between Arc and UniqueArc.
Patch 2 is difficult to merge since it requires a change everywhere AlwaysRefCounted is implemented prior to application. We had new implementations of this trait for the last few releases, so we are probably going to have some when these patches are applied. Sorry for that. I would ask maintainers of all affected areas to ACK these patches as soon as possible.
This series has been underway a very long time. The work was started off by Asahi Lina, then picked up by Oliver Mangold and is now being finalized by Andreas Hindborg. For series like this, it is difficult to track what contributions came from what authors at what time. I'm going to leave authorship of the patches with the person originally submitting the patch. I will not track changes by each individual on each patch. Instead, I will leave a link to the original patch in the commit message of these multi-contributor patches.
As I cannot reach out to everyone to ask if they will sign off on the changes I have made, I will remove their tags, as these require approval.
With this approach, checkpatch.pl is complaining about missing Signed-off-by from patch author. I am not sure if we can keep it like this, if I need to change the author, or if I need to hunt down the original author to get an ACK for keeping the Signed-off-by tag?
---
Changes in v14:
- Rebase on v6.19-rc7.
- Rewrite cover letter.
- Update documentation and safety comments based on v13 feedback.
- Update commit messages.
- Reorder implementation blocks in owned.rs.
- Update example in owned.rs to use try operator rather than expect.
- Reformat use statements.
- Add patch: rust: page: convert to Ownable.
- Add patch: rust: implement ForeignOwnable for Owned.
- Add patch: rust: page: add from_raw().
- Link to v13: https://lore.kernel.org/r/[email protected]
Changes in v13:
- Rebase onto v6.18-rc1 (Andreas's work).
- Documentation and style fixes contributed by Andreas
- Link to v12: https://lore.kernel.org/r/[email protected]
Changes in v12:
-
- Rebase onto v6.17-rc1 (Andreas's work).
- moved kernel/types/ownable.rs to kernel/owned.rs
- Drop OwnableMut, make DerefMut depend on Unpin instead. I understood ML discussion as that being okay, but probably needs further scrunity.
- Lots of more documentation changes suggested by reviewers.
- Usage example for Ownable/Owned.
- Link to v11: https://lore.kernel.org/r/[email protected]
Changes in v11:
- Rework of documentation. I tried to honor all requests for changes "in spirit" plus some clearifications and corrections of my own.
- Dropping SimpleOwnedRefCounted by request from Alice, as it creates a potentially problematic blanket implementation (which a derive macro that could be created later would not have).
- Dropping Miguel's "kbuild: provide RUSTC_HAS_DO_NOT_RECOMMEND symbol" patch, as it is not needed anymore after dropping SimpleOwnedRefCounted. (I can add it again, if it is considered useful anyway).
- Link to v10: https://lore.kernel.org/r/[email protected]
Changes in v10:
- Moved kernel/ownable.rs to kernel/types/ownable.rs
- Fixes in documentation / comments as suggested by Andreas Hindborg
- Added Reviewed-by comment for Andreas Hindborg
- Fix rustfmt of pid_namespace.rs
- Link to v9: https://lore.kernel.org/r/[email protected]
Changes in v9:
- Rebase onto v6.14-rc7
- Move Ownable/OwnedRefCounted/Ownable, etc., into separate module
- Documentation fixes to Ownable/OwnableMut/OwnableRefCounted
- Add missing SAFETY documentation to ARef example
- Link to v8: https://lore.kernel.org/r/[email protected]
Changes in v8:
- Fix Co-developed-by and Suggested-by tags as suggested by Miguel and Boqun
- Some small documentation fixes in Owned/Ownable patch
- removing redundant trait constraint on DerefMut for Owned as suggested by Boqun Feng
- make SimpleOwnedRefCounted no longer implement RefCounted as suggested by Boqun Feng
- documentation for RefCounted as suggested by Boqun Feng
- Link to v7: https://lore.kernel.org/r/[email protected]
Changes in v7:
- Squash patch to make Owned::from_raw/into_raw public into parent
- Added Signed-off-by to other people's commits
- Link to v6: https://lore.kernel.org/r/[email protected]
Changes in v6:
- Changed comments/formatting as suggested by Miguel Ojeda
- Included and used new config flag RUSTC_HAS_DO_NOT_RECOMMEND,
thus no changes to types.rs will be needed when the attribute becomes available.
- Fixed commit message for Owned patch.
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- Rebase the whole thing on top of the Ownable/Owned traits by Asahi Lina.
- Rename AlwaysRefCounted to RefCounted and make AlwaysRefCounted a marker trait instead to allow to obtain an ARef<T> from an &T,
which (as Alice pointed out) is unsound when combined with UniqueRef/Owned.
- Change the Trait design and naming to implement this feature, UniqueRef/UniqueRefCounted is dropped in favor of Ownable/Owned and OwnableRefCounted is used to provide the functions to convert between Owned and ARef.
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Just a minor change in naming by request from Andreas Hindborg, try_shared_to_unique() -> try_from_shared(), unique_to_shared() -> into_shared(),
which is more in line with standard Rust naming conventions.
- Link to v3: https://lore.kernel.org/r/Z8Wuud2UQX6Yukyr@mango
---
Andreas Hindborg (4):
rust: aref: update formatting of use statements
rust: page: update formatting of use statements
rust: implement ForeignOwnable for Owned
rust: page: add from_raw()
Asahi Lina (2):
rust: types: Add Ownable/Owned types
rust: page: convert to Ownable
Oliver Mangold (3):
rust: rename AlwaysRefCounted to RefCounted.
rust: Add missing SAFETY documentation for ARef example
rust: Add OwnableRefCounted
rust/kernel/auxiliary.rs | 7 +- rust/kernel/block/mq/request.rs | 15 +- rust/kernel/cred.rs | 13 +- rust/kernel/device.rs | 10 +- rust/kernel/device/property.rs | 7 +- rust/kernel/drm/device.rs | 10 +- rust/kernel/drm/gem/mod.rs | 8 +- rust/kernel/fs/file.rs | 16 +- rust/kernel/i2c.rs | 16 +- rust/kernel/lib.rs | 1 + rust/kernel/mm.rs | 15 +- rust/kernel/mm/mmput_async.rs | 9 +- rust/kernel/opp.rs | 10 +- rust/kernel/owned.rs | 366 ++++++++++++++++++++++++++++++++++++++++ rust/kernel/page.rs | 57 +++++-- rust/kernel/pci.rs | 10 +- rust/kernel/pid_namespace.rs | 12 +- rust/kernel/platform.rs | 7 +- rust/kernel/sync/aref.rs | 80 ++++++--- rust/kernel/task.rs | 10 +- rust/kernel/types.rs | 13 +- 21 files changed, 612 insertions(+), 80 deletions(-)
---
base-commit: 63804fed149a6750ffd28610c5c1c98cce6bd377
change-id: 20250305-unique-ref-29fcd675f9e9
Best regards,
Signature · 1 line
Andreas Hindborg <[email protected]>
Andreas Hindborg
maintainerPATCH v14 8/9 rust: implement `ForeignOwnable` for `Owned`
Implement ForeignOwnable for Owned<T>. This allows use of Owned<T> in places such as the XArray.
Note that T does not need to implement ForeignOwnable for Owned<T> to implement ForeignOwnable.
--- rust/kernel/owned.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-)
rust/kernel/owned.rs +44 -1
@@ -16,7 +16,10 @@ };use kernel::{ sync::aref::ARef , - types::RefCounted ,// + types::{ +ForeignOwnable ,// +RefCounted , + },// };/// Types that specify their own way of performing allocation and destruction. Typically, this trait
@@ -120,6 +123,7 @@ pub unsafe trait Ownable {/// /// - The [`Owned<T>`] has exclusive access to the instance of `T`. /// - The instance of `T` will stay alive at least as long as the [`Owned<T>`] is alive. +#[repr(transparent)] pub struct Owned <T :Ownable > { ptr:NonNull <T >, }
@@ -201,6 +205,45 @@ fn drop(&mut self) { } } +// SAFETY: We derive the pointer to `T` from a valid `T`, so the returned +// pointer satisfy alignment requirements of `T`. +unsafe impl <T :Ownable + 'static>ForeignOwnable for Owned <T > { +const FOREIGN_ALIGN :usize = core::mem::align_of ::<Owned <T >>(); + +type Borrowed <'a> = &'aT ; +type BorrowedMut <'a> =Pin <&'amut T >; + +fn into_foreign (self) -> *mut kernel::ffi::c_void { +let ptr = self.ptr.as_ptr ().cast (); + core::mem::forget (self); + ptr + } + +unsafe fn from_foreign (ptr: *mut kernel::ffi::c_void ) ->Self { +Self { +// SAFETY: By function safety contract, `ptr` came from +// `into_foreign` and cannot be null. + ptr:unsafe {NonNull ::new_unchecked (ptr.cast ()) }, + } + } + +unsafe fn borrow <'a>(ptr: *mut kernel::ffi::c_void ) ->Self ::Borrowed <'a> { +// SAFETY: By function safety requirements, `ptr` is valid for use as a +// reference for `'a`. +unsafe { &*ptr.cast () } + } + +unsafe fn borrow_mut <'a>(ptr: *mut kernel::ffi::c_void ) ->Self ::BorrowedMut <'a> { +// SAFETY: By function safety requirements, `ptr` is valid for use as a +// unique reference for `'a`. +let inner =unsafe { &mut *ptr.cast () }; + +// SAFETY: We never move out of inner, and we do not hand out mutable +// references when `T: !Unpin`. +unsafe {Pin ::new_unchecked (inner) } + } +} +/// A trait for objects that can be wrapped in either one of the reference types [`Owned`] and /// [`ARef`]. ///
Signature · 1 line
2.51.2
Andreas Hindborg
maintainerPATCH v14 1/9 rust: types: Add Ownable/Owned types
In reply to Andreas HindborgFrom: 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.
This change is a derived work based on work by Asahi Lina <[email protected]> [1] and Oliver Mangold <[email protected]>.
--- rust/kernel/lib.rs | 1 + rust/kernel/owned.rs | 196 +++++++++++++++++++++++++++++++++++++++++++++++ rust/kernel/sync/aref.rs | 5 ++ rust/kernel/types.rs | 11 ++- 4 files changed, 212 insertions(+), 1 deletion(-)
rust/kernel/lib.rs +1 -0
@@ -119,6 +119,7 @@pub mod of;#[cfg( CONFIG_PM_OPP )] pub mod opp; +pub mod owned;pub mod page;#[cfg( CONFIG_PCI )] pub mod pci;
rust/kernel/owned.rs new file +196 -0
196 lines not on this page · raw message
rust/kernel/sync/aref.rs +5 -0
@@ -29,6 +29,11 @@/// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted /// instances of a type. /// +/// Note: Implementing this trait allows types to be wrapped in an [`ARef<Self>`]. It requires an +/// internal reference count and provides only shared references. If unique references are required +/// [`Ownable`](crate::types::Ownable) should be implemented which allows types to be wrapped in an +/// [`Owned<Self>`](crate::types::Owned). +/// /// # Safety /// /// Implementers must ensure that increments to the reference count keep the object alive in memory
rust/kernel/types.rs +10 -1
@@ -11,7 +11,16 @@ };use pin_init::{PinInit ,Wrapper ,Zeroable }; -pub use crate ::sync::aref::{ARef ,AlwaysRefCounted }; +pub use crate ::{ + owned::{ +Ownable , +Owned ,// + }, + sync::aref::{ +ARef , +AlwaysRefCounted ,// + },// +};/// Used to transfer ownership to and from foreign (non-Rust) languages. ///
Signature · 1 line
2.51.2
Andreas Hindborg
maintainerPATCH v14 7/9 rust: page: convert to `Ownable`
In reply to Andreas HindborgFrom: Asahi Lina <[email protected]>
This allows Page references to be returned as borrowed references, without necessarily owning the struct page.
Original patch by Asahi Lina <[email protected]> [1].
--- rust/kernel/page.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)
rust/kernel/page.rs +23 -11
@@ -10,6 +10,11 @@ bindings, error::code::*, error::Result, + types::{ + Opaque, + Ownable, + Owned, // + }, uaccess::UserSliceReader, // };use core::{
@@ -83,7 +88,7 @@ pub const fn page_align(addr: usize) -> usize {/// /// [`VBox`]: kernel::alloc::VBox /// [`Vmalloc`]: kernel::alloc::allocator::Vmalloc -pub struct BorrowedPage <'a>(ManuallyDrop <Page >,PhantomData <&'aPage >); +pub struct BorrowedPage <'a>(ManuallyDrop <Owned <Page >>,PhantomData <&'aOwned <Page >>);impl <'a>BorrowedPage <'a> {/// Constructs a [`BorrowedPage`] from a raw pointer to a `struct page`.
@@ -93,7 +98,9 @@ impl<'a> BorrowedPage<'a> {/// - `ptr` must point to a valid `bindings::page`. /// - `ptr` must remain valid for the entire lifetime `'a`. pub unsafe fn from_raw (ptr:NonNull <bindings::page >) ->Self { -let page =Page { page: ptr }; +let page:Owned <Page > = +// SAFETY: By function safety requirements `ptr` is non null and valid for 'a. +unsafe {Owned ::from_raw (NonNull ::new_unchecked (ptr.as_ptr ().cast ())) };// INVARIANT: The safety requirements guarantee that `ptr` is valid for the entire lifetime // `'a`.
@@ -126,8 +133,9 @@ pub trait AsPageIter {/// # Invariants /// /// The pointer is valid, and has ownership over the page. +#[repr(transparent)] pub struct Page { - page:NonNull <bindings::page >, + page:Opaque <bindings::page >, }// SAFETY: Pages have no logic that relies on them staying on a given thread, so moving them across
@@ -161,19 +169,20 @@ impl Page {/// # Ok::<(), kernel::alloc::AllocError>(()) /// ``` #[inline] -pub fn alloc_page (flags:Flags ) ->Result <Self ,AllocError > { +pub fn alloc_page (flags:Flags ) ->Result <Owned <Self >,AllocError > {// SAFETY: Depending on the value of `gfp_flags`, this call may sleep. Other than that, it // is always safe to call this method. let page =unsafe { bindings::alloc_pages (flags.as_raw (),0 ) };let page =NonNull ::new (page).ok_or (AllocError )?; -// INVARIANT: We just successfully allocated a page, so we now have ownership of the newly -// allocated page. We transfer that ownership to the new `Page` object. -Ok (Self { page }) +// SAFETY: We just successfully allocated a page, so we now have ownership of the newly +// allocated page. We transfer that ownership to the new `Owned<Page>` object. +// Since `Page` is transparent, we can cast the pointer directly. +Ok (unsafe {Owned ::from_raw (page.cast ()) }) }/// Returns a raw pointer to the page. pub fn as_ptr (&self) -> *mut bindings::page { - self.page.as_ptr () +Opaque ::cast_into (&self.page) }/// Get the node id containing this page.
@@ -348,10 +357,13 @@ pub unsafe fn copy_from_user_slice_raw( } } -impl Drop for Page { +// SAFETY: `Owned<Page>` objects returned by Page::alloc_page() follow the requirements of +// the Ownable abstraction. +unsafe impl Ownable for Page {#[inline] -fn drop (&mut self) { +unsafe fn release (this:NonNull <Self >) {// SAFETY: By the type invariants, we have ownership of the page and can free it. -unsafe { bindings::__free_pages (self.page.as_ptr (),0 ) }; +// Since Page is transparent, we can cast the raw pointer directly. +unsafe { bindings::__free_pages (this.cast ().as_ptr (),0 ) }; } }
Signature · 1 line
2.51.2
Andreas Hindborg
maintainerPATCH v14 4/9 rust: aref: update formatting of use statements
In reply to Andreas HindborgUpdate formatting if use statements in preparation for next commit.
--- rust/kernel/sync/aref.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
rust/kernel/sync/aref.rs +6 -1
@@ -17,7 +17,12 @@//! [`Arc`]: crate::sync::Arc //! [`Arc<T>`]: crate::sync::Arc -use core::{marker::PhantomData , mem::ManuallyDrop , ops::Deref , ptr::NonNull }; +use core::{ + marker::PhantomData , + mem::ManuallyDrop , + ops::Deref , + ptr::NonNull ,// +};/// Types that are internally reference counted. ///
Signature · 1 line
2.51.2
Andreas Hindborg
maintainerPATCH v14 5/9 rust: Add `OwnableRefCounted`
In reply to Andreas HindborgFrom: Oliver Mangold <[email protected]>
Types implementing one of these traits can safely convert between an ARef<T> and an Owned<T>.
This is useful for types which generally are accessed through an ARef but have methods which can only safely be called when the reference is unique, like e.g. block::mq::Request::end_ok().
Original patch by Oliver Mangold <[email protected]> [1].
--- rust/kernel/owned.rs | 143 ++++++++++++++++++++++++++++++++++++++++++++--- rust/kernel/sync/aref.rs | 15 ++++- rust/kernel/types.rs | 1 + 3 files changed, 150 insertions(+), 9 deletions(-)
rust/kernel/owned.rs +135 -8
@@ -14,18 +14,24 @@ pin::Pin, ptr::NonNull, // }; +use kernel::{ + sync::aref::ARef , + types::RefCounted ,// +};/// Types that specify their own way of performing allocation and destruction. Typically, this trait /// is implemented on types from the C side. /// -/// Implementing this trait allows types to be referenced via the [`Owned<Self>`] pointer type. This -/// is useful when it is desirable to tie the lifetime of the reference to an owned object, rather -/// than pass around a bare reference. [`Ownable`] types can define custom drop logic that is -/// executed when the owned reference [`Owned<Self>`] pointing to the object is dropped. +/// Implementing this trait allows types to be referenced via the [`Owned<Self>`] pointer type. +/// - This is useful when it is desirable to tie the lifetime of an object reference to an owned +/// object, rather than pass around a bare reference. +/// - [`Ownable`] types can define custom drop logic that is executed when the owned reference +/// of type [`Owned<_>`] pointing to the object is dropped. /// /// Note: The underlying object is not required to provide internal reference counting, because it /// represents a unique, owned reference. If reference counting (on the Rust side) is required, -/// [`RefCounted`](crate::types::RefCounted) should be implemented. +/// [`RefCounted`] should be implemented. [`OwnableRefCounted`] should be implemented if conversion +/// between unique and shared (reference counted) ownership is needed. /// /// # Safety ///
@@ -63,8 +69,7 @@/// Foo {}, /// flags::GFP_KERNEL, /// )?; -/// let result = NonNull::new(KBox::into_raw(result)) -/// .expect("Raw pointer to newly allocation KBox is null, this should never happen."); +/// let result = NonNull::new(KBox::into_raw(result)).ok_or(ENOMEM)?; /// // Count new allocation /// *FOO_ALLOC_COUNT.lock() += 1; /// // SAFETY: We just allocated the `Self`, thus it is valid and there cannot be any other
@@ -88,11 +93,12 @@/// } /// /// { -/// let foo = Foo::new().expect("Failed to allocate a Foo. This shouldn't happen"); +/// let foo = Foo::new()?; /// assert!(*FOO_ALLOC_COUNT.lock() == 1); /// } /// // `foo` is out of scope now, so we expect no live allocations. /// assert!(*FOO_ALLOC_COUNT.lock() == 0); +/// # Ok::<(), Error>(()) /// ``` pub unsafe trait Ownable {/// Releases the object.
@@ -194,3 +200,124 @@ fn drop(&mut self) {unsafe {T ::release (self.ptr) }; } } + +/// A trait for objects that can be wrapped in either one of the reference types [`Owned`] and +/// [`ARef`]. +/// +/// # Examples +/// +/// A minimal example implementation of [`OwnableRefCounted`], [`Ownable`] and its usage with +/// [`ARef`] and [`Owned`] looks like this: +/// +/// ``` +/// # #![expect(clippy::disallowed_names)] +/// # use core::cell::Cell; +/// # use core::ptr::NonNull; +/// # use kernel::alloc::{flags, kbox::KBox, AllocError}; +/// # use kernel::sync::aref::{ARef, RefCounted}; +/// # use kernel::types::{Owned, Ownable, OwnableRefCounted}; +/// +/// // An internally refcounted struct for demonstration purposes. +/// // +/// // # Invariants +/// // +/// // - `refcount` is always non-zero for a valid object. +/// // - `refcount` is >1 if there is more than one Rust reference to it. +/// // +/// struct Foo { +/// refcount: Cell<usize>, +/// } +/// +/// impl Foo { +/// fn new() -> Result<Owned<Self>> { +/// // We are just using a `KBox` here to handle the actual allocation, as our `Foo` is +/// // not actually a C-allocated object. +/// let result = KBox::new( +/// Foo { +/// refcount: Cell::new(1), +/// }, +/// flags::GFP_KERNEL, +/// )?; +/// let result = NonNull::new(KBox::into_raw(result)).ok_or(ENOMEM)?; +/// // SAFETY: We just allocated the `Self`, thus it is valid and there cannot be any other +/// // Rust references. Calling `into_raw()` makes us responsible for ownership and +/// // we won't use the raw pointer anymore, thus we can transfer ownership to the `Owned`. +/// Ok(unsafe { Owned::from_raw(result) }) +/// } +/// } +/// +/// // SAFETY: We increment and decrement each time the respective function is called and only free +/// // the `Foo` when the refcount reaches zero. +/// unsafe impl RefCounted for Foo { +/// fn inc_ref(&self) { +/// self.refcount.replace(self.refcount.get() + 1); +/// } +/// +/// unsafe fn dec_ref(this: NonNull<Self>) { +/// // SAFETY: By requirement on calling this function, the refcount is non-zero, +/// // implying the underlying object is valid. +/// let refcount = unsafe { &this.as_ref().refcount }; +/// let new_refcount = refcount.get() - 1; +/// if new_refcount == 0 { +/// // The `Foo` will be dropped when `KBox` goes out of scope. +/// // SAFETY: The [`KBox<Foo>`] is still alive as the old refcount is 1. We can pass +/// // ownership to the [`KBox`] as by requirement on calling this function, +/// // the `Self` will no longer be used by the caller. +/// unsafe { KBox::from_raw(this.as_ptr()) }; +/// } else { +/// refcount.replace(new_refcount); +/// } +/// } +/// } +/// +/// impl OwnableRefCounted for Foo { +/// fn try_from_shared(this: ARef<Self>) -> Result<Owned<Self>, ARef<Self>> { +/// if this.refcount.get() == 1 { +/// // SAFETY: The `Foo` is still alive and has no other Rust references as the refcount +/// // is 1. +/// Ok(unsafe { Owned::from_raw(ARef::into_raw(this)) }) +/// } else { +/// Err(this) +/// } +/// } +/// } +/// +/// // SAFETY: This implementation of `release()` is safe for any valid `Self`. +/// unsafe impl Ownable for Foo { +/// unsafe fn release(this: NonNull<Self>) { +/// // SAFETY: Using `dec_ref()` from [`RefCounted`] to release is okay, as the refcount is +/// // always 1 for an [`Owned<Foo>`]. +/// unsafe{ Foo::dec_ref(this) }; +/// } +/// } +/// +/// let foo = Foo::new()?; +/// let mut foo = ARef::from(foo); +/// { +/// let bar = foo.clone(); +/// assert!(Owned::try_from(bar).is_err()); +/// } +/// assert!(Owned::try_from(foo).is_ok()); +/// # Ok::<(), Error>(()) +/// ``` +pub trait OwnableRefCounted :RefCounted +Ownable +Sized { +/// Checks if the [`ARef`] is unique and converts it to an [`Owned`] if that is the case. +/// Otherwise it returns again an [`ARef`] to the same underlying object. +fn try_from_shared (this:ARef <Self >) ->Result <Owned <Self >,ARef <Self >>; + +/// Converts the [`Owned`] into an [`ARef`]. +fn into_shared (this:Owned <Self >) ->ARef <Self > { +// SAFETY: Safe by the requirements on implementing the trait. +unsafe {ARef ::from_raw (Owned ::into_raw (this)) } + } +} + +impl <T :OwnableRefCounted >TryFrom <ARef <T >>for Owned <T > { +type Error =ARef <T >; +/// Tries to convert the [`ARef`] to an [`Owned`] by calling +/// [`try_from_shared()`](OwnableRefCounted::try_from_shared). In case the [`ARef`] is not +/// unique, it returns again an [`ARef`] to the same underlying object. +fn try_from (b:ARef <T >) ->Result <Owned <T >,Self ::Error > { +T ::try_from_shared (b) + } +}
rust/kernel/sync/aref.rs +14 -1
@@ -23,6 +23,10 @@ ops::Deref, ptr::NonNull, // }; +use kernel::types::{ +OwnableRefCounted , +Owned ,// +};/// Types that are internally reference counted. ///
@@ -35,7 +39,10 @@/// Note: Implementing this trait allows types to be wrapped in an [`ARef<Self>`]. It requires an /// internal reference count and provides only shared references. If unique references are required /// [`Ownable`](crate::types::Ownable) should be implemented which allows types to be wrapped in an -/// [`Owned<Self>`](crate::types::Owned). +/// [`Owned<Self>`](crate::types::Owned). Implementing the trait +/// [`OwnableRefCounted`] allows to convert between unique and +/// shared references (i.e. [`Owned<Self>`](crate::types::Owned) and +/// [`ARef<Self>`](crate::types::Owned)). /// /// # Safety ///
@@ -185,6 +192,12 @@ fn from(b: &T) -> Self { } } +impl <T :OwnableRefCounted >From <Owned <T >>for ARef <T > { +fn from (b:Owned <T >) ->Self { +T ::into_shared (b) + } +} +impl <T :RefCounted >Drop for ARef <T > {fn drop (&mut self) {// SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to
rust/kernel/types.rs +1 -0
@@ -14,6 +14,7 @@pub use crate ::{ owned::{Ownable , +OwnableRefCounted ,Owned ,// }, sync::aref::{
Signature · 1 line
2.51.2
Andreas Hindborg
maintainerPATCH v14 2/9 rust: rename `AlwaysRefCounted` to `RefCounted`.
In reply to Andreas HindborgFrom: Oliver Mangold <[email protected]>
There are types where it may both be reference counted in some cases and owned in others. In such cases, obtaining ARef<T> from &T would be unsound as it allows creation of ARef<T> copy from &Owned<T>.
Therefore, we split AlwaysRefCounted into RefCounted (which ARef<T> would require) and a marker trait to indicate that the type is always reference counted (and not Ownable) so the &T -> ARef<T> conversion is possible.
- Rename AlwaysRefCounted to RefCounted.
- Add a new unsafe trait AlwaysRefCounted.
- Implement the new trait AlwaysRefCounted for the newly renamed RefCounted implementations. This leaves functionality of existing implementers of AlwaysRefCounted intact.
Original patch by Oliver Mangold <[email protected]> [1].
--- rust/kernel/auxiliary.rs | 7 +++++- rust/kernel/block/mq/request.rs | 15 +++++++------ rust/kernel/cred.rs | 13 ++++++++++-- rust/kernel/device.rs | 10 ++++++--- rust/kernel/device/property.rs | 7 +++++- rust/kernel/drm/device.rs | 10 ++++++--- rust/kernel/drm/gem/mod.rs | 8 ++++--- rust/kernel/fs/file.rs | 16 ++++++++++---- rust/kernel/i2c.rs | 16 +++++++++----- rust/kernel/mm.rs | 15 +++++++++---- rust/kernel/mm/mmput_async.rs | 9 ++++++-- rust/kernel/opp.rs | 10 ++++++--- rust/kernel/owned.rs | 2 +- rust/kernel/pci.rs | 10 ++++++++- rust/kernel/pid_namespace.rs | 12 +++++++++-- rust/kernel/platform.rs | 7 +++++- rust/kernel/sync/aref.rs | 47 ++++++++++++++++++++++++++--------------- rust/kernel/task.rs | 10 ++++++--- rust/kernel/types.rs | 3 ++- 19 files changed, 164 insertions(+), 63 deletions(-)
rust/kernel/auxiliary.rs +6 -1
@@ -11,6 +11,7 @@ driver, error::{from_result, to_result, Result}, prelude::*, + sync::aref::{AlwaysRefCounted, RefCounted}, types::Opaque, ThisModule, };
@@ -283,7 +284,7 @@ unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx> kernel::impl_device_context_into_aref!(Device );// SAFETY: Instances of `Device` are always reference-counted. -unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { +unsafe impl RefCounted for Device {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::get_device (self.as_ref ().as_raw ()) };
@@ -302,6 +303,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Device`. +unsafe impl AlwaysRefCounted for Device {} +impl <Ctx : device::DeviceContext >AsRef <device::Device <Ctx >>for Device <Ctx > {fn as_ref (&self) -> &device::Device <Ctx > {// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
rust/kernel/block/mq/request.rs +9 -6
@@ -9,7 +9,7 @@ block::mq::Operations, error::Result, sync::{ - aref::{ARef, AlwaysRefCounted}, + aref::{ARef, AlwaysRefCounted, RefCounted}, atomic::Relaxed, Refcount, },
@@ -229,11 +229,10 @@ unsafe impl<T: Operations> Send for Request<T> {}// mutate `self` are internally synchronized` unsafe impl <T :Operations >Sync for Request <T > {} -// SAFETY: All instances of `Request<T>` are reference counted. This -// implementation of `AlwaysRefCounted` ensure that increments to the ref count -// keeps the object alive in memory at least until a matching reference count -// decrement is executed. -unsafe impl <T :Operations >AlwaysRefCounted for Request <T > { +// SAFETY: All instances of `Request<T>` are reference counted. This implementation of `RefCounted` +// ensure that increments to the ref count keeps the object alive in memory at least until a +// matching reference count decrement is executed. +unsafe impl <T :Operations >RefCounted for Request <T > {fn inc_ref (&self) { self.wrapper_ref ().refcount ().inc (); }
@@ -255,3 +254,7 @@ unsafe fn dec_ref(obj: core::ptr::NonNull<Self>) { } } } + +// SAFETY: We currently do not implement `Ownable`, thus it is okay to obtain an `ARef<Request>` +// from a `&Request` (but this will change in the future). +unsafe impl <T :Operations >AlwaysRefCounted for Request <T > {}
rust/kernel/cred.rs +11 -2
@@ -8,7 +8,12 @@//! //! Reference: <https://www.kernel.org/doc/html/latest/security/credentials.html> -use crate ::{bindings, sync::aref::AlwaysRefCounted , task::Kuid , types::Opaque }; +use crate ::{ + bindings, + sync::aref::RefCounted , + task::Kuid , + types::{AlwaysRefCounted ,Opaque }, +};/// Wraps the kernel's `struct cred`. ///
@@ -76,7 +81,7 @@ pub fn euid(&self) -> Kuid { }// SAFETY: The type invariants guarantee that `Credential` is always ref-counted. -unsafe impl AlwaysRefCounted for Credential { +unsafe impl RefCounted for Credential {#[inline] fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero.
@@ -90,3 +95,7 @@ unsafe fn dec_ref(obj: core::ptr::NonNull<Credential>) {unsafe { bindings::put_cred (obj.cast ().as_ptr ()) }; } } + +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Credential>` from a +// `&Credential`. +unsafe impl AlwaysRefCounted for Credential {}
rust/kernel/device.rs +7 -3
@@ -7,8 +7,8 @@use crate ::{ bindings, fmt, prelude::*, - sync::aref::ARef , - types::{ForeignOwnable ,Opaque }, + sync::aref::{ARef ,RefCounted }, + types::{AlwaysRefCounted ,ForeignOwnable ,Opaque }, };use core::{any::TypeId , marker::PhantomData , ptr};
@@ -492,7 +492,7 @@ pub fn fwnode(&self) -> Option<&property::FwNode> { kernel::impl_device_context_into_aref!(Device );// SAFETY: Instances of `Device` are always reference-counted. -unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { +unsafe impl RefCounted for Device {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::get_device (self.as_raw ()) };
@@ -504,6 +504,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Device`. +unsafe impl AlwaysRefCounted for Device {} +// SAFETY: As by the type invariant `Device` can be sent to any thread. unsafe impl Send for Device {}
rust/kernel/device/property.rs +6 -1
@@ -14,6 +14,7 @@ fmt, prelude::*, str::{CStr, CString}, + sync::aref::{AlwaysRefCounted, RefCounted}, types::{ARef, Opaque}, };
@@ -359,7 +360,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { }// SAFETY: Instances of `FwNode` are always reference-counted. -unsafe impl crate ::types::AlwaysRefCounted for FwNode { +unsafe impl RefCounted for FwNode {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the // refcount is non-zero.
@@ -373,6 +374,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<FwNode>` from a +// `&FwNode`. +unsafe impl AlwaysRefCounted for FwNode {} +enum Node <'a> {Borrowed (&'aFwNode ),Owned (ARef <FwNode >),
rust/kernel/drm/device.rs +7 -3
@@ -11,8 +11,8 @@ error::from_err_ptr, error::Result, prelude::*, - sync::aref::{ARef, AlwaysRefCounted}, - types::Opaque, + sync::aref::{AlwaysRefCounted, RefCounted}, + types::{ARef, Opaque}, };use core::{alloc::Layout , mem, ops::Deref , ptr, ptr::NonNull };
@@ -198,7 +198,7 @@ fn deref(&self) -> &Self::Target {// SAFETY: DRM device objects are always reference counted and the get/put functions // satisfy the requirements. -unsafe impl <T : drm::Driver >AlwaysRefCounted for Device <T > { +unsafe impl <T : drm::Driver >RefCounted for Device <T > {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::drm_dev_get (self.as_raw ()) };
@@ -213,6 +213,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Device`. +unsafe impl <T : drm::Driver >AlwaysRefCounted for Device <T > {} +impl <T : drm::Driver >AsRef <device::Device >for Device <T > {fn as_ref (&self) -> &device::Device {// SAFETY: `bindings::drm_device::dev` is valid as long as the DRM device itself is valid,
rust/kernel/drm/gem/mod.rs +5 -3
@@ -10,8 +10,7 @@ drm::driver::{AllocImpl, AllocOps}, error::{to_result, Result}, prelude::*, - sync::aref::{ARef, AlwaysRefCounted}, - types::Opaque, + types::{ARef, AlwaysRefCounted, Opaque}, };use core::{ops::Deref , ptr::NonNull };
@@ -253,7 +252,7 @@ extern "C" fn free_callback(obj: *mut bindings::drm_gem_object) { }// SAFETY: Instances of `Object<T>` are always reference-counted. -unsafe impl <T :DriverObject >crate ::types::AlwaysRefCounted for Object <T > { +unsafe impl <T :DriverObject >crate ::types::RefCounted for Object <T > {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::drm_gem_object_get (self.as_raw ()) };
@@ -267,6 +266,9 @@ unsafe fn dec_ref(obj: NonNull<Self>) {unsafe { bindings::drm_gem_object_put (obj.as_raw ()) } } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Object`. +unsafe impl <T :DriverObject >crate ::types::AlwaysRefCounted for Object <T > {}impl <T :DriverObject >super ::private::Sealed for Object <T > {}
rust/kernel/fs/file.rs +12 -4
@@ -12,8 +12,8 @@ cred::Credential, error::{code::*, to_result, Error, Result}, fmt, - sync::aref::{ARef, AlwaysRefCounted}, - types::{NotThreadSafe, Opaque}, + sync::aref::RefCounted, + types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque}, };use core::ptr;
@@ -197,7 +197,7 @@ unsafe impl Sync for File {}// SAFETY: The type invariants guarantee that `File` is always ref-counted. This implementation // makes `ARef<File>` own a normal refcount. -unsafe impl AlwaysRefCounted for File { +unsafe impl RefCounted for File {#[inline] fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero.
@@ -212,6 +212,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<File>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<File>` from a +// `&File`. +unsafe impl AlwaysRefCounted for File {} +/// Wraps the kernel's `struct file`. Not thread safe. /// /// This type represents a file that is not known to be safe to transfer across thread boundaries.
@@ -233,7 +237,7 @@ pub struct LocalFile {// SAFETY: The type invariants guarantee that `LocalFile` is always ref-counted. This implementation // makes `ARef<LocalFile>` own a normal refcount. -unsafe impl AlwaysRefCounted for LocalFile { +unsafe impl RefCounted for LocalFile {#[inline] fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero.
@@ -249,6 +253,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<LocalFile>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<LocalFile>` from a +// `&LocalFile`. +unsafe impl AlwaysRefCounted for LocalFile {} +impl LocalFile {/// Constructs a new `struct file` wrapper from a file descriptor. ///
rust/kernel/i2c.rs +11 -5
@@ -17,8 +17,10 @@ of, prelude::*, types::{ +ARef ,AlwaysRefCounted , -Opaque ,// +Opaque , +RefCounted ,// }, // };
@@ -31,8 +33,6 @@ }, // }; -use kernel::types::ARef ; -/// An I2C device id table. #[repr(transparent)] #[derive( Clone , Copy )]
@@ -416,7 +416,7 @@ pub fn get(index: i32) -> Result<ARef<Self>> { kernel::impl_device_context_into_aref!(I2cAdapter );// SAFETY: Instances of `I2cAdapter` are always reference-counted. -unsafe impl crate ::types::AlwaysRefCounted for I2cAdapter { +unsafe impl crate ::types::RefCounted for I2cAdapter {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::i2c_get_adapter (self.index ()) };
@@ -427,6 +427,9 @@ unsafe fn dec_ref(obj: NonNull<Self>) {unsafe { bindings::i2c_put_adapter (obj.as_ref ().as_raw ()) } } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from an +// `&I2cAdapter`. +unsafe impl AlwaysRefCounted for I2cAdapter {}/// The i2c board info representation ///
@@ -492,7 +495,7 @@ unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for I2cClient<C kernel::impl_device_context_into_aref!(I2cClient );// SAFETY: Instances of `I2cClient` are always reference-counted. -unsafe impl AlwaysRefCounted for I2cClient { +unsafe impl RefCounted for I2cClient {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::get_device (self.as_ref ().as_raw ()) };
@@ -503,6 +506,9 @@ unsafe fn dec_ref(obj: NonNull<Self>) {unsafe { bindings::put_device (&raw mut (*obj.as_ref ().as_raw ()).dev) } } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from an +// `&I2cClient`. +unsafe impl AlwaysRefCounted for I2cClient {}impl <Ctx : device::DeviceContext >AsRef <device::Device <Ctx >>for I2cClient <Ctx > {fn as_ref (&self) -> &device::Device <Ctx > {
rust/kernel/mm.rs +11 -4
@@ -13,8 +13,8 @@use crate ::{ bindings, - sync::aref::{ARef ,AlwaysRefCounted }, - types::{NotThreadSafe ,Opaque }, + sync::aref::RefCounted , + types::{ARef ,AlwaysRefCounted ,NotThreadSafe ,Opaque }, };use core::{ops::Deref , ptr::NonNull };
@@ -55,7 +55,7 @@ unsafe impl Send for Mm {}unsafe impl Sync for Mm {}// SAFETY: By the type invariants, this type is always refcounted. -unsafe impl AlwaysRefCounted for Mm { +unsafe impl RefCounted for Mm {#[inline] fn inc_ref (&self) {// SAFETY: The pointer is valid since self is a reference.
@@ -69,6 +69,9 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Mm>` from a `&Mm`. +unsafe impl AlwaysRefCounted for Mm {} +/// A wrapper for the kernel's `struct mm_struct`. /// /// This type is like [`Mm`], but with non-zero `mm_users`. It can only be used when `mm_users` can
@@ -91,7 +94,7 @@ unsafe impl Send for MmWithUser {}unsafe impl Sync for MmWithUser {}// SAFETY: By the type invariants, this type is always refcounted. -unsafe impl AlwaysRefCounted for MmWithUser { +unsafe impl RefCounted for MmWithUser {#[inline] fn inc_ref (&self) {// SAFETY: The pointer is valid since self is a reference.
@@ -105,6 +108,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<MmWithUser>` from a +// `&MmWithUser`. +unsafe impl AlwaysRefCounted for MmWithUser {} +// Make all `Mm` methods available on `MmWithUser`. impl Deref for MmWithUser {type Target =Mm ;
rust/kernel/mm/mmput_async.rs +7 -2
@@ -10,7 +10,8 @@use crate ::{ bindings, mm::MmWithUser , - sync::aref::{ARef ,AlwaysRefCounted }, + sync::aref::RefCounted , + types::{ARef ,AlwaysRefCounted }, };use core::{ops::Deref , ptr::NonNull };
@@ -34,7 +35,7 @@ unsafe impl Send for MmWithUserAsync {}unsafe impl Sync for MmWithUserAsync {}// SAFETY: By the type invariants, this type is always refcounted. -unsafe impl AlwaysRefCounted for MmWithUserAsync { +unsafe impl RefCounted for MmWithUserAsync {#[inline] fn inc_ref (&self) {// SAFETY: The pointer is valid since self is a reference.
@@ -48,6 +49,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<MmWithUserAsync>` +// from a `&MmWithUserAsync`. +unsafe impl AlwaysRefCounted for MmWithUserAsync {} +// Make all `MmWithUser` methods available on `MmWithUserAsync`. impl Deref for MmWithUserAsync {type Target =MmWithUser ;
rust/kernel/opp.rs +7 -3
@@ -16,8 +16,8 @@ ffi::{c_char, c_ulong}, prelude::*, str::CString, - sync::aref::{ARef, AlwaysRefCounted}, - types::Opaque, + sync::aref::RefCounted, + types::{ARef, AlwaysRefCounted, Opaque}, };#[cfg( CONFIG_CPU_FREQ )]
@@ -1041,7 +1041,7 @@ unsafe impl Send for OPP {}unsafe impl Sync for OPP {}/// SAFETY: The type invariants guarantee that [`OPP`] is always refcounted. -unsafe impl AlwaysRefCounted for OPP { +unsafe impl RefCounted for OPP {fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. unsafe { bindings::dev_pm_opp_get (self.0 .get ()) };
@@ -1053,6 +1053,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<OPP>` from an +// `&OPP`. +unsafe impl AlwaysRefCounted for OPP {} +impl OPP {/// Creates an owned reference to a [`OPP`] from a valid pointer. ///
rust/kernel/owned.rs +1 -1
@@ -25,7 +25,7 @@/// /// Note: The underlying object is not required to provide internal reference counting, because it /// represents a unique, owned reference. If reference counting (on the Rust side) is required, -/// [`AlwaysRefCounted`](crate::types::AlwaysRefCounted) should be implemented. +/// [`RefCounted`](crate::types::RefCounted) should be implemented. /// /// # Safety ///
rust/kernel/pci.rs +9 -1
@@ -19,6 +19,10 @@ }, prelude::*, str::CStr, + sync::aref::{ + AlwaysRefCounted, + RefCounted, // + }, types::Opaque, ThisModule, // };
@@ -467,7 +471,7 @@ unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx>impl crate ::dma::Device for Device <device::Core > {}// SAFETY: Instances of `Device` are always reference-counted. -unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { +unsafe impl RefCounted for Device {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::pci_dev_get (self.as_raw ()) };
@@ -479,6 +483,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Device`. +unsafe impl AlwaysRefCounted for Device {} +impl <Ctx : device::DeviceContext >AsRef <device::Device <Ctx >>for Device <Ctx > {fn as_ref (&self) -> &device::Device <Ctx > {// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
rust/kernel/pid_namespace.rs +10 -2
@@ -7,7 +7,11 @@//! C header: [`include/linux/pid_namespace.h`](srctree/include/linux/pid_namespace.h) and //! [`include/linux/pid.h`](srctree/include/linux/pid.h) -use crate ::{bindings, sync::aref::AlwaysRefCounted , types::Opaque }; +use crate ::{ + bindings, + sync::aref::RefCounted , + types::{AlwaysRefCounted ,Opaque }, +};use core::ptr;/// Wraps the kernel's `struct pid_namespace`. Thread safe.
@@ -41,7 +45,7 @@ pub unsafe fn from_ptr<'a>(ptr: *const bindings::pid_namespace) -> &'a Self { }// SAFETY: Instances of `PidNamespace` are always reference-counted. -unsafe impl AlwaysRefCounted for PidNamespace { +unsafe impl RefCounted for PidNamespace {#[inline] fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero.
@@ -55,6 +59,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<PidNamespace>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<PidNamespace>` from +// a `&PidNamespace`. +unsafe impl AlwaysRefCounted for PidNamespace {} +// SAFETY: // - `PidNamespace::dec_ref` can be called from any thread. // - It is okay to send ownership of `PidNamespace` across thread boundaries.
rust/kernel/platform.rs +6 -1
@@ -13,6 +13,7 @@ irq::{self, IrqRequest}, of, prelude::*, + sync::aref::{AlwaysRefCounted, RefCounted}, types::Opaque, ThisModule, };
@@ -490,7 +491,7 @@ pub fn optional_irq_by_name(&self, name: &CStr) -> Result<IrqRequest<'_>> {impl crate ::dma::Device for Device <device::Core > {}// SAFETY: Instances of `Device` are always reference-counted. -unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { +unsafe impl RefCounted for Device {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::get_device (self.as_ref ().as_raw ()) };
@@ -502,6 +503,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Device`. +unsafe impl AlwaysRefCounted for Device {} +impl <Ctx : device::DeviceContext >AsRef <device::Device <Ctx >>for Device <Ctx > {fn as_ref (&self) -> &device::Device <Ctx > {// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
rust/kernel/sync/aref.rs +30 -17
117 lines not on this page · raw message
rust/kernel/task.rs +7 -3
@@ -9,8 +9,8 @@ ffi::{c_int, c_long, c_uint}, mm::MmWithUser, pid_namespace::PidNamespace, - sync::aref::ARef, - types::{NotThreadSafe, Opaque}, + sync::aref::{AlwaysRefCounted, RefCounted}, + types::{ARef, NotThreadSafe, Opaque}, };use core::{ cmp::{Eq ,PartialEq },
@@ -348,7 +348,7 @@ pub fn active_pid_ns(&self) -> Option<&PidNamespace> { }// SAFETY: The type invariants guarantee that `Task` is always refcounted. -unsafe impl crate ::sync::aref::AlwaysRefCounted for Task { +unsafe impl RefCounted for Task {#[inline] fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero.
@@ -362,6 +362,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Task>` from a +// `&Task`. +unsafe impl AlwaysRefCounted for Task {} +impl Kuid {/// Get the current euid. #[inline]
rust/kernel/types.rs +2 -1
@@ -18,7 +18,8 @@ }, sync::aref::{ARef , -AlwaysRefCounted ,// +AlwaysRefCounted , +RefCounted ,// }, // };
Signature · 1 line
2.51.2
Andreas Hindborg
maintainerPATCH v14 9/9 rust: page: add `from_raw()`
In reply to Andreas HindborgAdd a method to Page that allows construction of an instance from struct page pointer.
--- rust/kernel/page.rs | 11 +++++++++++ 1 file changed, 11 insertions(+)
rust/kernel/page.rs +11 -0
@@ -191,6 +191,17 @@ pub fn nid(&self) -> i32 {unsafe { bindings::page_to_nid (self.as_ptr ()) } } +/// Create a `&Page` from a raw `struct page` pointer +/// +/// # Safety +/// +/// `ptr` must be valid for use as a reference for the duration of `'a`. +pub unsafe fn from_raw <'a>(ptr: *const bindings::page ) -> &'aSelf { +// SAFETY: By function safety requirements, ptr is not null and is +// valid for use as a reference. +unsafe { &*Opaque ::cast_from (ptr).cast ::<Self >() } + } +/// Runs a piece of code with this page mapped to an address. /// /// The page is unmapped when this call returns.
Signature · 1 line
2.51.2
Re: [PATCH v14 2/9] rust: rename `AlwaysRefCounted` to `RefCounted`.
In reply to Andreas HindborgHello Andreas
Quoted 813 lines · Andreas Hindborg
From: Oliver Mangold <[email protected]>
There are types where it may both be reference counted in some cases and owned in others. In such cases, obtaining
ARef<T>from&Twould be unsound as it allows creation ofARef<T>copy from&Owned<T>.Therefore, we split
AlwaysRefCountedintoRefCounted(whichARef<T>would require) and a marker trait to indicate that the type is always reference counted (and notOwnable) so the&T->ARef<T>conversion is possible.- Rename
AlwaysRefCountedtoRefCounted.
- Add a new unsafe traitAlwaysRefCounted.
- Implement the new traitAlwaysRefCountedfor the newly renamedRefCountedimplementations. This leaves functionality of existing implementers ofAlwaysRefCountedintact.Original patch by Oliver Mangold <[email protected]> [1].
Suggested-by Alice Ryhl <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Andreas Hindborg <[email protected]>--- rust/kernel/auxiliary.rs | 7 +++++- rust/kernel/block/mq/request.rs | 15 +++++++------ rust/kernel/cred.rs | 13 ++++++++++-- rust/kernel/device.rs | 10 ++++++--- rust/kernel/device/property.rs | 7 +++++- rust/kernel/drm/device.rs | 10 ++++++--- rust/kernel/drm/gem/mod.rs | 8 ++++--- rust/kernel/fs/file.rs | 16 ++++++++++---- rust/kernel/i2c.rs | 16 +++++++++----- rust/kernel/mm.rs | 15 +++++++++---- rust/kernel/mm/mmput_async.rs | 9 ++++++-- rust/kernel/opp.rs | 10 ++++++--- rust/kernel/owned.rs | 2 +- rust/kernel/pci.rs | 10 ++++++++- rust/kernel/pid_namespace.rs | 12 +++++++++-- rust/kernel/platform.rs | 7 +++++- rust/kernel/sync/aref.rs | 47 ++++++++++++++++++++++++++--------------- rust/kernel/task.rs | 10 ++++++--- rust/kernel/types.rs | 3 ++- 19 files changed, 164 insertions(+), 63 deletions(-)rust/kernel/auxiliary.rs +6
-1@@ -11,6 +11,7 @@ driver, error::{from_result, to_result, Result}, prelude::*, + sync::aref::{AlwaysRefCounted, RefCounted}, types::Opaque, ThisModule, };@@ -283,7 +284,7 @@ unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx> kernel::impl_device_context_into_aref!(Device );// SAFETY: Instances of `Device` are always reference-counted. -unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { +unsafe impl RefCounted for Device {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::get_device (self.as_ref ().as_raw ()) };@@ -302,6 +303,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Device`. +unsafe impl AlwaysRefCounted for Device {} +impl <Ctx : device::DeviceContext >AsRef <device::Device <Ctx >>for Device <Ctx > {fn as_ref (&self) -> &device::Device <Ctx > {// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid rust/kernel/block/mq/request.rs +9
-6@@ -9,7 +9,7 @@ block::mq::Operations, error::Result, sync::{ - aref::{ARef, AlwaysRefCounted}, + aref::{ARef, AlwaysRefCounted, RefCounted}, atomic::Relaxed, Refcount, },@@ -229,11 +229,10 @@ unsafe impl<T: Operations> Send for Request<T> {}// mutate `self` are internally synchronized` unsafe impl <T :Operations >Sync for Request <T > {} -// SAFETY: All instances of `Request<T>` are reference counted. This -// implementation of `AlwaysRefCounted` ensure that increments to the ref count -// keeps the object alive in memory at least until a matching reference count -// decrement is executed. -unsafe impl <T :Operations >AlwaysRefCounted for Request <T > { +// SAFETY: All instances of `Request<T>` are reference counted. This implementation of `RefCounted` +// ensure that increments to the ref count keeps the object alive in memory at least until a +// matching reference count decrement is executed. +unsafe impl <T :Operations >RefCounted for Request <T > {fn inc_ref (&self) { self.wrapper_ref ().refcount ().inc (); }@@ -255,3 +254,7 @@ unsafe fn dec_ref(obj: core::ptr::NonNull<Self>) { } } } + +// SAFETY: We currently do not implement `Ownable`, thus it is okay to obtain an `ARef<Request>` +// from a `&Request` (but this will change in the future). +unsafe impl <T :Operations >AlwaysRefCounted for Request <T > {}rust/kernel/cred.rs +11
-2@@ -8,7 +8,12 @@//! //! Reference: <https://www.kernel.org/doc/html/latest/security/credentials.html> -use crate ::{bindings, sync::aref::AlwaysRefCounted , task::Kuid , types::Opaque }; +use crate ::{ + bindings, + sync::aref::RefCounted , + task::Kuid , + types::{AlwaysRefCounted ,Opaque }, +};/// Wraps the kernel's `struct cred`. /// @@ -76,7 +81,7 @@ pub fn euid(&self) -> Kuid { }// SAFETY: The type invariants guarantee that `Credential` is always ref-counted. -unsafe impl AlwaysRefCounted for Credential { +unsafe impl RefCounted for Credential {#[inline] fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. @@ -90,3 +95,7 @@ unsafe fn dec_ref(obj: core::ptr::NonNull<Credential>) {unsafe { bindings::put_cred (obj.cast ().as_ptr ()) }; } } + +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Credential>` from a +// `&Credential`. +unsafe impl AlwaysRefCounted for Credential {}rust/kernel/device.rs +7
-3@@ -7,8 +7,8 @@use crate ::{ bindings, fmt, prelude::*, - sync::aref::ARef , - types::{ForeignOwnable ,Opaque }, + sync::aref::{ARef ,RefCounted }, + types::{AlwaysRefCounted ,ForeignOwnable ,Opaque }, };use core::{any::TypeId , marker::PhantomData , ptr};@@ -492,7 +492,7 @@ pub fn fwnode(&self) -> Option<&property::FwNode> { kernel::impl_device_context_into_aref!(Device );// SAFETY: Instances of `Device` are always reference-counted. -unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { +unsafe impl RefCounted for Device {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::get_device (self.as_raw ()) };@@ -504,6 +504,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Device`. +unsafe impl AlwaysRefCounted for Device {} +// SAFETY: As by the type invariant `Device` can be sent to any thread. unsafe impl Send for Device {}rust/kernel/device/property.rs +6
-1@@ -14,6 +14,7 @@ fmt, prelude::*, str::{CStr, CString}, + sync::aref::{AlwaysRefCounted, RefCounted}, types::{ARef, Opaque}, };@@ -359,7 +360,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { }// SAFETY: Instances of `FwNode` are always reference-counted. -unsafe impl crate ::types::AlwaysRefCounted for FwNode { +unsafe impl RefCounted for FwNode {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the // refcount is non-zero. @@ -373,6 +374,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<FwNode>` from a +// `&FwNode`. +unsafe impl AlwaysRefCounted for FwNode {} +enum Node <'a> {Borrowed (&'aFwNode ),Owned (ARef <FwNode >),rust/kernel/drm/device.rs +7
-3@@ -11,8 +11,8 @@ error::from_err_ptr, error::Result, prelude::*, - sync::aref::{ARef, AlwaysRefCounted}, - types::Opaque, + sync::aref::{AlwaysRefCounted, RefCounted}, + types::{ARef, Opaque}, };use core::{alloc::Layout , mem, ops::Deref , ptr, ptr::NonNull };@@ -198,7 +198,7 @@ fn deref(&self) -> &Self::Target {// SAFETY: DRM device objects are always reference counted and the get/put functions // satisfy the requirements. -unsafe impl <T : drm::Driver >AlwaysRefCounted for Device <T > { +unsafe impl <T : drm::Driver >RefCounted for Device <T > {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::drm_dev_get (self.as_raw ()) };@@ -213,6 +213,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Device`. +unsafe impl <T : drm::Driver >AlwaysRefCounted for Device <T > {} +impl <T : drm::Driver >AsRef <device::Device >for Device <T > {fn as_ref (&self) -> &device::Device {// SAFETY: `bindings::drm_device::dev` is valid as long as the DRM device itself is valid, rust/kernel/drm/gem/mod.rs +5
-3@@ -10,8 +10,7 @@ drm::driver::{AllocImpl, AllocOps}, error::{to_result, Result}, prelude::*, - sync::aref::{ARef, AlwaysRefCounted}, - types::Opaque, + types::{ARef, AlwaysRefCounted, Opaque}, };use core::{ops::Deref , ptr::NonNull };@@ -253,7 +252,7 @@ extern "C" fn free_callback(obj: *mut bindings::drm_gem_object) { }// SAFETY: Instances of `Object<T>` are always reference-counted. -unsafe impl <T :DriverObject >crate ::types::AlwaysRefCounted for Object <T > { +unsafe impl <T :DriverObject >crate ::types::RefCounted for Object <T > {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::drm_gem_object_get (self.as_raw ()) };@@ -267,6 +266,9 @@ unsafe fn dec_ref(obj: NonNull<Self>) {unsafe { bindings::drm_gem_object_put (obj.as_raw ()) } } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Object`. +unsafe impl <T :DriverObject >crate ::types::AlwaysRefCounted for Object <T > {}impl <T :DriverObject >super ::private::Sealed for Object <T > {}rust/kernel/fs/file.rs +12
-4@@ -12,8 +12,8 @@ cred::Credential, error::{code::*, to_result, Error, Result}, fmt, - sync::aref::{ARef, AlwaysRefCounted}, - types::{NotThreadSafe, Opaque}, + sync::aref::RefCounted, + types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque}, };use core::ptr;@@ -197,7 +197,7 @@ unsafe impl Sync for File {}// SAFETY: The type invariants guarantee that `File` is always ref-counted. This implementation // makes `ARef<File>` own a normal refcount. -unsafe impl AlwaysRefCounted for File { +unsafe impl RefCounted for File {#[inline] fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. @@ -212,6 +212,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<File>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<File>` from a +// `&File`. +unsafe impl AlwaysRefCounted for File {} +/// Wraps the kernel's `struct file`. Not thread safe. /// /// This type represents a file that is not known to be safe to transfer across thread boundaries. @@ -233,7 +237,7 @@ pub struct LocalFile {// SAFETY: The type invariants guarantee that `LocalFile` is always ref-counted. This implementation // makes `ARef<LocalFile>` own a normal refcount. -unsafe impl AlwaysRefCounted for LocalFile { +unsafe impl RefCounted for LocalFile {#[inline] fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. @@ -249,6 +253,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<LocalFile>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<LocalFile>` from a +// `&LocalFile`. +unsafe impl AlwaysRefCounted for LocalFile {} +impl LocalFile {/// Constructs a new `struct file` wrapper from a file descriptor. /// rust/kernel/i2c.rs +11
-5@@ -17,8 +17,10 @@ of, prelude::*, types::{ +ARef ,AlwaysRefCounted , -Opaque ,// +Opaque , +RefCounted ,// }, // };@@ -31,8 +33,6 @@ }, // }; -use kernel::types::ARef ; -/// An I2C device id table. #[repr(transparent)] #[derive( Clone , Copy )] @@ -416,7 +416,7 @@ pub fn get(index: i32) -> Result<ARef<Self>> { kernel::impl_device_context_into_aref!(I2cAdapter );// SAFETY: Instances of `I2cAdapter` are always reference-counted. -unsafe impl crate ::types::AlwaysRefCounted for I2cAdapter { +unsafe impl crate ::types::RefCounted for I2cAdapter {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::i2c_get_adapter (self.index ()) };@@ -427,6 +427,9 @@ unsafe fn dec_ref(obj: NonNull<Self>) {unsafe { bindings::i2c_put_adapter (obj.as_ref ().as_raw ()) } } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from an +// `&I2cAdapter`. +unsafe impl AlwaysRefCounted for I2cAdapter {}/// The i2c board info representation /// @@ -492,7 +495,7 @@ unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for I2cClient<C kernel::impl_device_context_into_aref!(I2cClient );// SAFETY: Instances of `I2cClient` are always reference-counted. -unsafe impl AlwaysRefCounted for I2cClient { +unsafe impl RefCounted for I2cClient {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::get_device (self.as_ref ().as_raw ()) };@@ -503,6 +506,9 @@ unsafe fn dec_ref(obj: NonNull<Self>) {unsafe { bindings::put_device (&raw mut (*obj.as_ref ().as_raw ()).dev) } } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from an +// `&I2cClient`. +unsafe impl AlwaysRefCounted for I2cClient {}impl <Ctx : device::DeviceContext >AsRef <device::Device <Ctx >>for I2cClient <Ctx > {fn as_ref (&self) -> &device::Device <Ctx > {rust/kernel/mm.rs +11
-4@@ -13,8 +13,8 @@use crate ::{ bindings, - sync::aref::{ARef ,AlwaysRefCounted }, - types::{NotThreadSafe ,Opaque }, + sync::aref::RefCounted , + types::{ARef ,AlwaysRefCounted ,NotThreadSafe ,Opaque }, };use core::{ops::Deref , ptr::NonNull };@@ -55,7 +55,7 @@ unsafe impl Send for Mm {}unsafe impl Sync for Mm {}// SAFETY: By the type invariants, this type is always refcounted. -unsafe impl AlwaysRefCounted for Mm { +unsafe impl RefCounted for Mm {#[inline] fn inc_ref (&self) {// SAFETY: The pointer is valid since self is a reference. @@ -69,6 +69,9 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Mm>` from a `&Mm`. +unsafe impl AlwaysRefCounted for Mm {} +/// A wrapper for the kernel's `struct mm_struct`. /// /// This type is like [`Mm`], but with non-zero `mm_users`. It can only be used when `mm_users` can @@ -91,7 +94,7 @@ unsafe impl Send for MmWithUser {}unsafe impl Sync for MmWithUser {}// SAFETY: By the type invariants, this type is always refcounted. -unsafe impl AlwaysRefCounted for MmWithUser { +unsafe impl RefCounted for MmWithUser {#[inline] fn inc_ref (&self) {// SAFETY: The pointer is valid since self is a reference. @@ -105,6 +108,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<MmWithUser>` from a +// `&MmWithUser`. +unsafe impl AlwaysRefCounted for MmWithUser {} +// Make all `Mm` methods available on `MmWithUser`. impl Deref for MmWithUser {type Target =Mm ;rust/kernel/mm/mmput_async.rs +7
-2@@ -10,7 +10,8 @@use crate ::{ bindings, mm::MmWithUser , - sync::aref::{ARef ,AlwaysRefCounted }, + sync::aref::RefCounted , + types::{ARef ,AlwaysRefCounted }, };use core::{ops::Deref , ptr::NonNull };@@ -34,7 +35,7 @@ unsafe impl Send for MmWithUserAsync {}unsafe impl Sync for MmWithUserAsync {}// SAFETY: By the type invariants, this type is always refcounted. -unsafe impl AlwaysRefCounted for MmWithUserAsync { +unsafe impl RefCounted for MmWithUserAsync {#[inline] fn inc_ref (&self) {// SAFETY: The pointer is valid since self is a reference. @@ -48,6 +49,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<MmWithUserAsync>` +// from a `&MmWithUserAsync`. +unsafe impl AlwaysRefCounted for MmWithUserAsync {} +// Make all `MmWithUser` methods available on `MmWithUserAsync`. impl Deref for MmWithUserAsync {type Target =MmWithUser ;rust/kernel/opp.rs +7
-3@@ -16,8 +16,8 @@ ffi::{c_char, c_ulong}, prelude::*, str::CString, - sync::aref::{ARef, AlwaysRefCounted}, - types::Opaque, + sync::aref::RefCounted, + types::{ARef, AlwaysRefCounted, Opaque}, };#[cfg( CONFIG_CPU_FREQ )] @@ -1041,7 +1041,7 @@ unsafe impl Send for OPP {}unsafe impl Sync for OPP {}/// SAFETY: The type invariants guarantee that [`OPP`] is always refcounted. -unsafe impl AlwaysRefCounted for OPP { +unsafe impl RefCounted for OPP {fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. unsafe { bindings::dev_pm_opp_get (self.0 .get ()) };@@ -1053,6 +1053,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<OPP>` from an +// `&OPP`. +unsafe impl AlwaysRefCounted for OPP {} +impl OPP {/// Creates an owned reference to a [`OPP`] from a valid pointer. /// rust/kernel/owned.rs +1
-1@@ -25,7 +25,7 @@/// /// Note: The underlying object is not required to provide internal reference counting, because it /// represents a unique, owned reference. If reference counting (on the Rust side) is required, -/// [`AlwaysRefCounted`](crate::types::AlwaysRefCounted) should be implemented. +/// [`RefCounted`](crate::types::RefCounted) should be implemented. /// /// # Safety /// rust/kernel/pci.rs +9
-1@@ -19,6 +19,10 @@ }, prelude::*, str::CStr, + sync::aref::{ + AlwaysRefCounted, + RefCounted, // + }, types::Opaque, ThisModule, // };@@ -467,7 +471,7 @@ unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx>impl crate ::dma::Device for Device <device::Core > {}// SAFETY: Instances of `Device` are always reference-counted. -unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { +unsafe impl RefCounted for Device {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::pci_dev_get (self.as_raw ()) };@@ -479,6 +483,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Device`. +unsafe impl AlwaysRefCounted for Device {} +impl <Ctx : device::DeviceContext >AsRef <device::Device <Ctx >>for Device <Ctx > {fn as_ref (&self) -> &device::Device <Ctx > {// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid rust/kernel/pid_namespace.rs +10
-2@@ -7,7 +7,11 @@//! C header: [`include/linux/pid_namespace.h`](srctree/include/linux/pid_namespace.h) and //! [`include/linux/pid.h`](srctree/include/linux/pid.h) -use crate ::{bindings, sync::aref::AlwaysRefCounted , types::Opaque }; +use crate ::{ + bindings, + sync::aref::RefCounted , + types::{AlwaysRefCounted ,Opaque }, +};use core::ptr;/// Wraps the kernel's `struct pid_namespace`. Thread safe. @@ -41,7 +45,7 @@ pub unsafe fn from_ptr<'a>(ptr: *const bindings::pid_namespace) -> &'a Self { }// SAFETY: Instances of `PidNamespace` are always reference-counted. -unsafe impl AlwaysRefCounted for PidNamespace { +unsafe impl RefCounted for PidNamespace {#[inline] fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. @@ -55,6 +59,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<PidNamespace>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<PidNamespace>` from +// a `&PidNamespace`. +unsafe impl AlwaysRefCounted for PidNamespace {} +// SAFETY: // - `PidNamespace::dec_ref` can be called from any thread. // - It is okay to send ownership of `PidNamespace` across thread boundaries. rust/kernel/platform.rs +6
-1@@ -13,6 +13,7 @@ irq::{self, IrqRequest}, of, prelude::*, + sync::aref::{AlwaysRefCounted, RefCounted}, types::Opaque, ThisModule, };@@ -490,7 +491,7 @@ pub fn optional_irq_by_name(&self, name: &CStr) -> Result<IrqRequest<'_>> {impl crate ::dma::Device for Device <device::Core > {}// SAFETY: Instances of `Device` are always reference-counted. -unsafe impl crate ::sync::aref::AlwaysRefCounted for Device { +unsafe impl RefCounted for Device {fn inc_ref (&self) {// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. unsafe { bindings::get_device (self.as_ref ().as_raw ()) };@@ -502,6 +503,10 @@ unsafe fn dec_ref(obj: NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Device>` from a +// `&Device`. +unsafe impl AlwaysRefCounted for Device {} +impl <Ctx : device::DeviceContext >AsRef <device::Device <Ctx >>for Device <Ctx > {fn as_ref (&self) -> &device::Device <Ctx > {// SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid rust/kernel/sync/aref.rs +30
-17@@ -19,11 +19,9 @@use core::{marker::PhantomData , mem::ManuallyDrop , ops::Deref , ptr::NonNull }; -/// Types that are _always_ reference counted. +/// Types that are internally reference counted. /// /// It allows such types to define their own custom ref increment and decrement functions. -/// Additionally, it allows users to convert from a shared reference `&T` to an owned reference -/// [`ARef<T>`]. /// /// This is usually implemented by wrappers to existing structures on the C side of the code. For /// Rust code, the recommendation is to use [`Arc`](crate::sync::Arc) to create reference-counted @@ -40,9 +38,8 @@/// at least until matching decrements are performed. /// /// Implementers must also ensure that all instances are reference-counted. (Otherwise they -/// won't be able to honour the requirement that [`AlwaysRefCounted::inc_ref`] keep the object -/// alive.) -pub unsafe trait AlwaysRefCounted { +/// won't be able to honour the requirement that [`RefCounted::inc_ref`] keep the object alive.) +pub unsafe trait RefCounted {/// Increments the reference count on the object. fn inc_ref (&self);@@ -55,11 +52,27 @@ pub unsafe trait AlwaysRefCounted {/// Callers must ensure that there was a previous matching increment to the reference count, /// and that the object is no longer used after its reference count is decremented (as it may /// result in the object being freed), unless the caller owns another increment on the refcount -/// (e.g., it calls [`AlwaysRefCounted::inc_ref`] twice, then calls -/// [`AlwaysRefCounted::dec_ref`] once). +/// (e.g., it calls [`RefCounted::inc_ref`] twice, then calls [`RefCounted::dec_ref`] once). unsafe fn dec_ref (obj:NonNull <Self >); } +/// Always reference-counted type. +/// +/// It allows deriving a counted reference [`ARef<T>`] from a `&T`. +/// +/// This provides some convenience, but it allows "escaping" borrow checks on `&T`. As it +/// complicates attempts to ensure that a reference to T is unique, it is optional to provide for +/// [`RefCounted`] types. See *Safety* below. +/// +/// # Safety +/// +/// Implementers must ensure that no safety invariants are violated by upgrading an `&T` to an +/// [`ARef<T>`]. In particular that implies [`AlwaysRefCounted`] and [`crate::types::Ownable`] +/// cannot be implemented for the same type, as this would allow violating the uniqueness guarantee +/// of [`crate::types::Owned<T>`] by dereferencing it into an `&T` and obtaining an [`ARef`] from +/// that. +pub unsafe trait AlwaysRefCounted :RefCounted {} +/// An owned reference to an always-reference-counted object. /// /// The object's reference count is automatically decremented when an instance of [`ARef`] is @@ -70,7 +83,7 @@ pub unsafe trait AlwaysRefCounted {/// /// The pointer stored in `ptr` is non-null and valid for the lifetime of the [`ARef`] instance. In /// particular, the [`ARef`] instance owns an increment on the underlying object's reference count. -pub struct ARef <T :AlwaysRefCounted > { +pub struct ARef <T :RefCounted > { ptr:NonNull <T >, _p:PhantomData <T >, }@@ -79,16 +92,16 @@ pub struct ARef<T: AlwaysRefCounted> {// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs // `T` to be `Send` because any thread that has an `ARef<T>` may ultimately access `T` using a // mutable reference, for example, when the reference count reaches zero and `T` is dropped. -unsafe impl <T :AlwaysRefCounted +Sync +Send >Send for ARef <T > {} +unsafe impl <T :RefCounted +Sync +Send >Send for ARef <T > {}// SAFETY: It is safe to send `&ARef<T>` to another thread when the underlying `T` is `Sync` // because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, // it needs `T` to be `Send` because any thread that has a `&ARef<T>` may clone it and get an // `ARef<T>` on that thread, so the thread may ultimately access `T` using a mutable reference, for // example, when the reference count reaches zero and `T` is dropped. -unsafe impl <T :AlwaysRefCounted +Sync +Send >Sync for ARef <T > {} +unsafe impl <T :RefCounted +Sync +Send >Sync for ARef <T > {} -impl <T :AlwaysRefCounted >ARef <T > { +impl <T :RefCounted >ARef <T > {/// Creates a new instance of [`ARef`]. /// /// It takes over an increment of the reference count on the underlying object. @@ -117,12 +130,12 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {/// /// ``` /// use core::ptr::NonNull; -/// use kernel::sync::aref::{ARef, AlwaysRefCounted}; +/// use kernel::sync::aref::{ARef, RefCounted}; /// /// struct Empty {} /// /// # // SAFETY: TODO. -/// unsafe impl AlwaysRefCounted for Empty { +/// unsafe impl RefCounted for Empty { /// fn inc_ref(&self) {} /// unsafe fn dec_ref(_obj: NonNull<Self>) {} /// } @@ -140,7 +153,7 @@ pub fn into_raw(me: Self) -> NonNull<T> { } } -impl <T :AlwaysRefCounted >Clone for ARef <T > { +impl <T :RefCounted >Clone for ARef <T > {fn clone (&self) ->Self { self.inc_ref ();// SAFETY: We just incremented the refcount above. @@ -148,7 +161,7 @@ fn clone(&self) -> Self { } } -impl <T :AlwaysRefCounted >Deref for ARef <T > { +impl <T :RefCounted >Deref for ARef <T > {type Target =T ;fn deref (&self) -> &Self ::Target {@@ -165,7 +178,7 @@ fn from(b: &T) -> Self { } } -impl <T :AlwaysRefCounted >Drop for ARef <T > { +impl <T :RefCounted >Drop for ARef <T > {fn drop (&mut self) {// SAFETY: The type invariants guarantee that the `ARef` owns the reference we're about to // decrement. rust/kernel/task.rs +7
-3@@ -9,8 +9,8 @@ ffi::{c_int, c_long, c_uint}, mm::MmWithUser, pid_namespace::PidNamespace, - sync::aref::ARef, - types::{NotThreadSafe, Opaque}, + sync::aref::{AlwaysRefCounted, RefCounted}, + types::{ARef, NotThreadSafe, Opaque}, };use core::{ cmp::{Eq ,PartialEq },@@ -348,7 +348,7 @@ pub fn active_pid_ns(&self) -> Option<&PidNamespace> { }// SAFETY: The type invariants guarantee that `Task` is always refcounted. -unsafe impl crate ::sync::aref::AlwaysRefCounted for Task { +unsafe impl RefCounted for Task {#[inline] fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. @@ -362,6 +362,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<Task>` from a +// `&Task`. +unsafe impl AlwaysRefCounted for Task {} +impl Kuid {/// Get the current euid. #[inline] rust/kernel/types.rs +0
-0
rust/kernel/types.rs line 25
Quoted 9 lines
rust/kernel/types.rs +2
-1@@ -18,7 +18,8 @@ }, sync::aref::{ARef , -AlwaysRefCounted ,// +AlwaysRefCounted , +RefCounted ,// }, // };
For Rust I2C subsystem:
Thanks
Igor
Danilo Krummrich
maintainerRe: [PATCH v14 1/9] rust: types: Add Ownable/Owned types
In reply to Andreas HindborgQuoted 7 lines · Andreas Hindborg
From: Asahi Lina <[email protected]>
By analogy to
AlwaysRefCountedandARef, anOwnabletype is a (typically C FFI) type that *may* be owned by Rust, but need not be. UnlikeAlwaysRefCounted, this mechanism expects the reference to be unique within Rust, and does not allow cloning.
Quoted 8 lines
Conceptually, this is similar to a
KBox<T>, except that it delegates resource management to theTinstead of using a generic allocator.This change is a derived work based on work by Asahi Lina <[email protected]> [1] and Oliver Mangold <[email protected]>.
Given the From: line above, this needs Lina's SoB.
This patch was also originally posted by Abdiel and Boqun and I think we should account for this. I mentioned this in a couple of previous versions already, e.g. in [1]. I think we should account for this.
Re: [PATCH v14 2/9] rust: rename `AlwaysRefCounted` to `RefCounted`.
In reply to Andreas HindborgQuoted 7 lines · Andreas Hindborg
rust/kernel/auxiliary.rs | 7 +++++- rust/kernel/device.rs | 10 ++++++--- rust/kernel/device/property.rs | 7 +++++- rust/kernel/drm/device.rs | 10 ++++++--- rust/kernel/drm/gem/mod.rs | 8 ++++--- rust/kernel/pci.rs | 10 ++++++++- rust/kernel/platform.rs | 7 +++++-
If you apply the kernel import style, those are
However, I wonder if we want aref::* in the prelude instead.
Andreas Hindborg
maintainerRe: [PATCH v14 1/9] rust: types: Add Ownable/Owned types
In reply to Danilo KrummrichQuoted 17 lines · Danilo Krummrich
Quoted 7 lines · Andreas Hindborg
From: Asahi Lina <[email protected]>
By analogy to
AlwaysRefCountedandARef, anOwnabletype is a (typically C FFI) type that *may* be owned by Rust, but need not be. UnlikeAlwaysRefCounted, this mechanism expects the reference to be unique within Rust, and does not allow cloning.Quoted 8 lines
Conceptually, this is similar to a
KBox<T>, except that it delegates resource management to theTinstead of using a generic allocator.This change is a derived work based on work by Asahi Lina <[email protected]> [1] and Oliver Mangold <[email protected]>.
Quoted 7 lines
Given the From: line above, this needs Lina's SoB.
This patch was also originally posted by Abdiel and Boqun and I think we should account for this. I mentioned this in a couple of previous versions already, e.g. in [1]. I think we should account for this.
I had a question about this in the cover letter.
It is my understanding that the SoB needs confirmation from the author if the code was changed. I changed the code and did not want to bother the original author, because it is my understanding they do not wish to be contacted. I did not want to misrepresent the original author, and so I did not change the "From:" line.
I want to be clear that I want to submit this patch the correct way, whatever that is. I will happily take all the guidance I can get to find the correct way to represent the work of the original author(s).
How would you prefer to account for the work by Abdiel and Boqun?
Please advise about SoB and I will correct the series.
Best regards,
Andreas Hindborg
Danilo Krummrich
maintainerRe: [PATCH v14 1/9] rust: types: Add Ownable/Owned types
Quoted 5 lines · Andreas Hindborg
It is my understanding that the SoB needs confirmation from the author if the code was changed. I changed the code and did not want to bother the original author, because it is my understanding they do not wish to be contacted. I did not want to misrepresent the original author, and so I did not change the "From:" line.
Frankly, I don't know what's the correct thing to do in this case; maybe the correct thing is to just keep the SoB, but list all the changes that have been made.
Technically, the same thing is common practice when maintainers (including myself) apply (minor) changes to a patch when applying them to their tree.
Quoted 1 line
How would you prefer to account for the work by Abdiel and Boqun?
I mean, I don't have a preference and if I would have one, it wouldn't be relevant. :) I just wanted to bring it up since the very first version was sent by the two of them. So, I think we should just ask.
Gary Guo
maintainerRe: [PATCH v14 1/9] rust: types: Add Ownable/Owned types
Quoted 11 lines · Danilo Krummrich
Quoted 5 lines · Andreas Hindborg
It is my understanding that the SoB needs confirmation from the author if the code was changed. I changed the code and did not want to bother the original author, because it is my understanding they do not wish to be contacted. I did not want to misrepresent the original author, and so I did not change the "From:" line.
Frankly, I don't know what's the correct thing to do in this case; maybe the correct thing is to just keep the SoB, but list all the changes that have been made.
Quoted 8 lines
Technically, the same thing is common practice when maintainers (including myself) apply (minor) changes to a patch when applying them to their tree.
Quoted 1 line
How would you prefer to account for the work by Abdiel and Boqun?
I mean, I don't have a preference and if I would have one, it wouldn't be relevant. :) I just wanted to bring it up since the very first version was sent by the two of them. So, I think we should just ask.
It looks to me that they're independent works, there're very clear difference between the two patches. Lina's patch closely follows the ARef design, and it looks it also takes into account the dropck (by having PhantomData) and have send/sync considerations.
Best,
Gary
Gary Guo
maintainerRe: [PATCH v14 1/9] rust: types: Add Ownable/Owned types
In reply to Andreas HindborgQuoted 27 lines · Andreas Hindborg
Quoted 17 lines · Danilo Krummrich
Quoted 7 lines · Andreas Hindborg
From: Asahi Lina <[email protected]>
By analogy to
AlwaysRefCountedandARef, anOwnabletype is a (typically C FFI) type that *may* be owned by Rust, but need not be. UnlikeAlwaysRefCounted, this mechanism expects the reference to be unique within Rust, and does not allow cloning.Quoted 8 lines
Conceptually, this is similar to a
KBox<T>, except that it delegates resource management to theTinstead of using a generic allocator.This change is a derived work based on work by Asahi Lina <[email protected]> [1] and Oliver Mangold <[email protected]>.
Quoted 7 lines
Given the From: line above, this needs Lina's SoB.
This patch was also originally posted by Abdiel and Boqun and I think we should account for this. I mentioned this in a couple of previous versions already, e.g. in [1]. I think we should account for this.
Quoted 7 lines
I had a question about this in the cover letter.
It is my understanding that the SoB needs confirmation from the author if the code was changed. I changed the code and did not want to bother the original author, because it is my understanding they do not wish to be contacted. I did not want to misrepresent the original author, and so I did not change the "From:" line.
Looks like your didn't make a very substantial change (just OwnableMut -> Unpin)? So mentioning the change in commit message should be sufficient.
If it's very substantial I would change author to be you and add the original author as Co-developed-by instead.
Best,
Gary
Quoted 12 lines
I want to be clear that I want to submit this patch the correct way, whatever that is. I will happily take all the guidance I can get to find the correct way to represent the work of the original author(s).
How would you prefer to account for the work by Abdiel and Boqun?
Please advise about SoB and I will correct the series.
Best regards,
Andreas Hindborg
kernel test robot
build botRe: [PATCH v14 2/9] rust: rename `AlwaysRefCounted` to `RefCounted`.
In reply to Andreas Hindborgkernel test robot noticed the following build errors:
Hi Andreas,
kernel test robot noticed the following build errors:
[auto build test ERROR on 63804fed149a6750ffd28610c5c1c98cce6bd377]
url: https://github.com/intel-lab-lkp/linux/commits/Andreas-Hindborg/rust-types-Add-Ownable-Owned-types/20260204-200041 base: 63804fed149a6750ffd28610c5c1c98cce6bd377 patch link: https://lore.kernel.org/r/20260204-unique-ref-v14-2-17cb29ebacbb%40kernel.org patch subject: [PATCH v14 2/9] rust: rename `AlwaysRefCounted` to `RefCounted`. config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20260204/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) rustc: rustc 1.88.0 (6b00bc388 2025-06-23) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260204/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> error[E0407]: method `inc_ref` is not a member of trait `AlwaysRefCounted`
--> rust/kernel/usb.rs:378:5
|
378 | / fn inc_ref(&self) {
... |
383 | | }
| |_____^ not a member of trait AlwaysRefCounted
--
>> error[E0407]: method `dec_ref` is not a member of trait `AlwaysRefCounted`
--> rust/kernel/usb.rs:385:5
|
385 | / unsafe fn dec_ref(obj: NonNull<Self>) {
386 | | // SAFETY: The safety requirements guarantee that the refcount is non-zero.
387 | | unsafe { bindings::usb_put_intf(obj.cast().as_ptr()) }
388 | | }
| |_____^ not a member of trait `AlwaysRefCounted`
--
>> error[E0277]: the trait bound `usb::Interface: sync::aref::RefCounted` is not satisfied --> rust/kernel/device.rs:700:17
|
700 | (&**dev).into()
| ^^^^^^^^^^^^^^^ the trait sync::aref::RefCounted is not implemented for usb::Interface
|
::: rust/kernel/usb.rs:352:1
|
352 | kernel::impl_device_context_into_aref!(Interface);
| ------------------------------------------------- in this macro invocation
|
= help: the following other types implement trait sync::aref::RefCounted:
auxiliary::Device
block::mq::request::Request<T>
cred::Credential
device::Device
device::property::FwNode
fs::file::File
fs::file::LocalFile
i2c::I2cAdapter
and 8 others
note: required by a bound in sync::aref::ARef
--> rust/kernel/sync/aref.rs:86:20
|
86 | pub struct ARef<T: RefCounted> {
| ^^^^^^^^^^ required by this bound in ARef
= note: this error originates in the macro ::kernel::__impl_device_context_into_aref which comes from the expansion of the macro kernel::impl_device_context_into_aref (in Nightly builds, run with -Z macro-backtrace for more info)
--
>> error[E0277]: the trait bound `usb::Device: sync::aref::RefCounted` is not satisfied
--> rust/kernel/device.rs:700:17
|
700 | (&**dev).into()
| ^^^^^^^^^^^^^^^ the trait sync::aref::RefCounted is not implemented for usb::Device
|
::: rust/kernel/usb.rs:425:1
|
425 | kernel::impl_device_context_into_aref!(Device);
| ---------------------------------------------- in this macro invocation
|
= help: the following other types implement trait sync::aref::RefCounted:
auxiliary::Device
block::mq::request::Request<T>
cred::Credential
device::Device
device::property::FwNode
fs::file::File
fs::file::LocalFile
i2c::I2cAdapter
and 8 others
note: required by a bound in sync::aref::ARef
--> rust/kernel/sync/aref.rs:86:20
|
86 | pub struct ARef<T: RefCounted> {
| ^^^^^^^^^^ required by this bound in ARef
= note: this error originates in the macro ::kernel::__impl_device_context_into_aref which comes from the expansion of the macro kernel::impl_device_context_into_aref (in Nightly builds, run with -Z macro-backtrace for more info)
--
>> error[E0407]: method `inc_ref` is not a member of trait `AlwaysRefCounted`
--> rust/kernel/usb.rs:429:5
|
429 | / fn inc_ref(&self) {
... |
434 | | }
| |_____^ not a member of trait AlwaysRefCounted
--
>> error[E0407]: method `dec_ref` is not a member of trait `AlwaysRefCounted`
--> rust/kernel/usb.rs:436:5
|
436 | / unsafe fn dec_ref(obj: NonNull<Self>) {
437 | | // SAFETY: The safety requirements guarantee that the refcount is non-zero.
438 | | unsafe { bindings::usb_put_dev(obj.cast().as_ptr()) }
439 | | }
| |_____^ not a member of trait `AlwaysRefCounted`
--
>> error[E0277]: the trait bound `usb::Interface: sync::aref::RefCounted` is not satisfied --> rust/kernel/device.rs:698:56
|
698 | impl ::core::convert::From<&$device<$src>> for $crate::sync::aref::ARef<$device> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait sync::aref::RefCounted is not implemented for usb::Interface
|
::: rust/kernel/usb.rs:352:1
|
352 | kernel::impl_device_context_into_aref!(Interface);
| ------------------------------------------------- in this macro invocation
|
= help: the following other types implement trait sync::aref::RefCounted:
auxiliary::Device
block::mq::request::Request<T>
cred::Credential
device::Device
device::property::FwNode
fs::file::File
fs::file::LocalFile
i2c::I2cAdapter
and 8 others
note: required by a bound in sync::aref::ARef
--> rust/kernel/sync/aref.rs:86:20
|
86 | pub struct ARef<T: RefCounted> {
| ^^^^^^^^^^ required by this bound in ARef
= note: this error originates in the macro ::kernel::__impl_device_context_into_aref which comes from the expansion of the macro kernel::impl_device_context_into_aref (in Nightly builds, run with -Z macro-backtrace for more info)
--
>> error[E0277]: the trait bound `usb::Interface: sync::aref::RefCounted` is not satisfied
--> rust/kernel/usb.rs:377:34
|
377 | unsafe impl AlwaysRefCounted for Interface {
| ^^^^^^^^^ the trait sync::aref::RefCounted is not implemented for usb::Interface
|
= help: the following other types implement trait sync::aref::RefCounted:
auxiliary::Device
block::mq::request::Request<T>
cred::Credential
device::Device
device::property::FwNode
fs::file::File
fs::file::LocalFile
i2c::I2cAdapter
and 8 others
note: required by a bound in sync::aref::AlwaysRefCounted --> rust/kernel/sync/aref.rs:74:36
|
74 | pub unsafe trait AlwaysRefCounted: RefCounted {}
| ^^^^^^^^^^ required by this bound in AlwaysRefCounted
--
>> error[E0277]: the trait bound `usb::Device: sync::aref::RefCounted` is not satisfied
--> rust/kernel/device.rs:698:56
|
698 | impl ::core::convert::From<&$device<$src>> for $crate::sync::aref::ARef<$device> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait sync::aref::RefCounted is not implemented for usb::Device
|
::: rust/kernel/usb.rs:425:1
|
425 | kernel::impl_device_context_into_aref!(Device);
| ---------------------------------------------- in this macro invocation
|
= help: the following other types implement trait sync::aref::RefCounted:
auxiliary::Device
block::mq::request::Request<T>
cred::Credential
device::Device
device::property::FwNode
fs::file::File
fs::file::LocalFile
i2c::I2cAdapter
and 8 others
note: required by a bound in sync::aref::ARef
--> rust/kernel/sync/aref.rs:86:20
|
86 | pub struct ARef<T: RefCounted> {
| ^^^^^^^^^^ required by this bound in ARef
= note: this error originates in the macro ::kernel::__impl_device_context_into_aref which comes from the expansion of the macro kernel::impl_device_context_into_aref (in Nightly builds, run with -Z macro-backtrace for more info)
--
>> error[E0277]: the trait bound `usb::Device: sync::aref::RefCounted` is not satisfied
--> rust/kernel/usb.rs:428:34
|
428 | unsafe impl AlwaysRefCounted for Device {
| ^^^^^^ the trait sync::aref::RefCounted is not implemented for usb::Device
|
= help: the following other types implement trait sync::aref::RefCounted:
auxiliary::Device
block::mq::request::Request<T>
cred::Credential
device::Device
device::property::FwNode
fs::file::File
fs::file::LocalFile
i2c::I2cAdapter
and 8 others
note: required by a bound in sync::aref::AlwaysRefCounted --> rust/kernel/sync/aref.rs:74:36
|
74 | pub unsafe trait AlwaysRefCounted: RefCounted {}
| ^^^^^^^^^^ required by this bound in AlwaysRefCounted
--
>> error[E0277]: the trait bound `usb::Interface: sync::aref::RefCounted` is not satisfied
--> rust/kernel/device.rs:699:45
|
699 | fn from(dev: &$device<$src>) -> Self {
| ^^^^ the trait sync::aref::RefCounted is not implemented for usb::Interface
|
::: rust/kernel/usb.rs:352:1
|
352 | kernel::impl_device_context_into_aref!(Interface);
| ------------------------------------------------- in this macro invocation
|
= help: the following other types implement trait sync::aref::RefCounted:
auxiliary::Device
block::mq::request::Request<T>
cred::Credential
device::Device
device::property::FwNode
fs::file::File
fs::file::LocalFile
i2c::I2cAdapter
and 8 others
note: required by a bound in sync::aref::ARef
--> rust/kernel/sync/aref.rs:86:20
|
86 | pub struct ARef<T: RefCounted> {
| ^^^^^^^^^^ required by this bound in ARef
= note: this error originates in the macro ::kernel::__impl_device_context_into_aref which comes from the expansion of the macro kernel::impl_device_context_into_aref (in Nightly builds, run with -Z macro-backtrace for more info)
..
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v14 2/9] rust: rename `AlwaysRefCounted` to `RefCounted`.
In reply to Andreas HindborgQuoted 25 lines · Andreas Hindborg
rust/kernel/opp.rs +3
-3@@ -16,8 +16,8 @@ ffi::{c_char, c_ulong}, prelude::*, str::CString, - sync::aref::{ARef, AlwaysRefCounted}, - types::Opaque, + sync::aref::RefCounted, + types::{ARef, AlwaysRefCounted, Opaque}, };#[cfg( CONFIG_CPU_FREQ )] @@ -1041,7 +1041,7 @@ unsafe impl Send for OPP {}unsafe impl Sync for OPP {}/// SAFETY: The type invariants guarantee that [`OPP`] is always refcounted. -unsafe impl AlwaysRefCounted for OPP { +unsafe impl RefCounted for OPP {fn inc_ref (&self) {// SAFETY: The existence of a shared reference means that the refcount is nonzero. unsafe { bindings::dev_pm_opp_get (self.0 .get ()) };
rust/kernel/opp.rs unsafe fn dec_ref(obj: ptr::NonNull<Self>) { · line 1062
Quoted 10 lines
rust/kernel/opp.rs +4
-0@@ -1053,6 +1053,10 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) { } } +// SAFETY: We do not implement `Ownable`, thus it is okay to obtain an `ARef<OPP>` from an +// `&OPP`. +unsafe impl AlwaysRefCounted for OPP {} +impl OPP {/// Creates an owned reference to a [`OPP`] from a valid pointer. ///
Signature · 1 line
viresh
Re: [PATCH v14 1/9] rust: types: Add Ownable/Owned types
In reply to Andreas HindborgQuoted 41 lines · Andreas Hindborg
From: Asahi Lina <[email protected]>
By analogy to
AlwaysRefCountedandARef, anOwnabletype is a (typically C FFI) type that *may* be owned by Rust, but need not be. UnlikeAlwaysRefCounted, 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 theTinstead of using a generic allocator.This change is a derived work based on work by Asahi Lina <[email protected]> [1] and Oliver Mangold <[email protected]>.
Signed-off-by Andreas Hindborg <[email protected]>--- rust/kernel/lib.rs | 1 + rust/kernel/owned.rs | 196 +++++++++++++++++++++++++++++++++++++++++++++++ rust/kernel/sync/aref.rs | 5 ++ rust/kernel/types.rs | 11 ++- 4 files changed, 212 insertions(+), 1 deletion(-)rust/kernel/lib.rs +1
-0@@ -119,6 +119,7 @@pub mod of;#[cfg( CONFIG_PM_OPP )] pub mod opp; +pub mod owned;pub mod page;#[cfg( CONFIG_PCI )] pub mod pci;rust/kernel/owned.rs new file +1
-0+
rust/kernel/owned.rs line 162
Quoted 10 lines
rust/kernel/owned.rs new file +10
-0+/// Get a pinned mutable reference to the data owned by this `Owned<T>`. +pub fn get_pin_mut (&mut self) ->Pin <&mut T > { +// SAFETY: The type invariants guarantee that the object is valid, and that we can safely +// return a mutable reference to it. +let unpinned =unsafe { self.ptr.as_mut () }; + +// SAFETY: We never hand out unpinned mutable references to the data in +// `Self`, unless the contained type is `Unpin`. +unsafe {Pin ::new_unchecked (unpinned) } + }
Probably should be name as_pin_mut instead.
With name changed and SOB fixed:
Best,
Gary
Quoted 10 lines
rust/kernel/owned.rs new file +10
-0+} + +// SAFETY: It is safe to send an [`Owned<T>`] to another thread when the underlying `T` is [`Send`], +// because of the ownership invariant. Sending an [`Owned<T>`] is equivalent to sending the `T`. +unsafe impl <T :Ownable +Send >Send for Owned <T > {} + +// SAFETY: It is safe to send [`&Owned<T>`] to another thread when the underlying `T` is [`Sync`], +// because of the ownership invariant. Sending an [`&Owned<T>`] is equivalent to sending the `&T`. +unsafe impl <T :Ownable +Sync >Sync for Owned <T > {} +
Re: [PATCH v14 2/9] rust: rename `AlwaysRefCounted` to `RefCounted`.
In reply to Andreas HindborgQuoted 17 lines · Andreas Hindborg
From: Oliver Mangold <[email protected]>
There are types where it may both be reference counted in some cases and owned in others. In such cases, obtaining
ARef<T>from&Twould be unsound as it allows creation ofARef<T>copy from&Owned<T>.Therefore, we split
AlwaysRefCountedintoRefCounted(whichARef<T>would require) and a marker trait to indicate that the type is always reference counted (and notOwnable) so the&T->ARef<T>conversion is possible.- Rename
AlwaysRefCountedtoRefCounted.
- Add a new unsafe traitAlwaysRefCounted.
- Implement the new traitAlwaysRefCountedfor the newly renamedRefCountedimplementations. This leaves functionality of existing implementers ofAlwaysRefCountedintact.
Quoted 6 lines
Original patch by Oliver Mangold <[email protected]> [1].
Suggested-by Alice Ryhl <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Andreas Hindborg <[email protected]>
I think you also need to update the AlwaysRefCounted reference mentioned in the Owned patch too? (Or perhaps this patch should be moved before Owned instead?)
With that fixed:
Quoted 21 lines
--- rust/kernel/auxiliary.rs | 7 +++++- rust/kernel/block/mq/request.rs | 15 +++++++------ rust/kernel/cred.rs | 13 ++++++++++-- rust/kernel/device.rs | 10 ++++++--- rust/kernel/device/property.rs | 7 +++++- rust/kernel/drm/device.rs | 10 ++++++--- rust/kernel/drm/gem/mod.rs | 8 ++++--- rust/kernel/fs/file.rs | 16 ++++++++++---- rust/kernel/i2c.rs | 16 +++++++++----- rust/kernel/mm.rs | 15 +++++++++---- rust/kernel/mm/mmput_async.rs | 9 ++++++-- rust/kernel/opp.rs | 10 ++++++--- rust/kernel/owned.rs | 2 +- rust/kernel/pci.rs | 10 ++++++++- rust/kernel/pid_namespace.rs | 12 +++++++++-- rust/kernel/platform.rs | 7 +++++- rust/kernel/sync/aref.rs | 47 ++++++++++++++++++++++++++--------------- rust/kernel/task.rs | 10 ++++++--- rust/kernel/types.rs | 3 ++- 19 files changed, 164 insertions(+), 63 deletions(-)
Gary Guo
maintainerRe: [PATCH v14 2/9] rust: rename `AlwaysRefCounted` to `RefCounted`.
Quoted 20 lines · Gary Guo
Quoted 19 lines · Andreas Hindborg
From: Oliver Mangold <[email protected]>
There are types where it may both be reference counted in some cases and owned in others. In such cases, obtaining
ARef<T>from&Twould be unsound as it allows creation ofARef<T>copy from&Owned<T>.Therefore, we split
AlwaysRefCountedintoRefCounted(whichARef<T>would require) and a marker trait to indicate that the type is always reference counted (and notOwnable) so the&T->ARef<T>conversion is possible.- Rename
AlwaysRefCountedtoRefCounted.
- Add a new unsafe traitAlwaysRefCounted.
- Implement the new traitAlwaysRefCountedfor the newly renamedRefCountedimplementations. This leaves functionality of existing implementers ofAlwaysRefCountedintact.Original patch by Oliver Mangold <[email protected]> [1].
Quoted 8 lines
Quoted 4 lines
Suggested-by Alice Ryhl <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Andreas Hindborg <[email protected]>I think you also need to update the
AlwaysRefCountedreference mentioned in theOwnedpatch too? (Or perhaps this patch should be moved beforeOwnedinstead?)
Actually I re-read the comment in first patch, the text indeed should refer to AlwaysRefCounted. Please disregard this comment.
Best,
Gary
Quoted 26 lines
With that fixed:
Reviewed-by Gary Guo <[email protected]>Quoted 21 lines
--- rust/kernel/auxiliary.rs | 7 +++++- rust/kernel/block/mq/request.rs | 15 +++++++------ rust/kernel/cred.rs | 13 ++++++++++-- rust/kernel/device.rs | 10 ++++++--- rust/kernel/device/property.rs | 7 +++++- rust/kernel/drm/device.rs | 10 ++++++--- rust/kernel/drm/gem/mod.rs | 8 ++++--- rust/kernel/fs/file.rs | 16 ++++++++++---- rust/kernel/i2c.rs | 16 +++++++++----- rust/kernel/mm.rs | 15 +++++++++---- rust/kernel/mm/mmput_async.rs | 9 ++++++-- rust/kernel/opp.rs | 10 ++++++--- rust/kernel/owned.rs | 2 +- rust/kernel/pci.rs | 10 ++++++++- rust/kernel/pid_namespace.rs | 12 +++++++++-- rust/kernel/platform.rs | 7 +++++- rust/kernel/sync/aref.rs | 47 ++++++++++++++++++++++++++--------------- rust/kernel/task.rs | 10 ++++++--- rust/kernel/types.rs | 3 ++- 19 files changed, 164 insertions(+), 63 deletions(-)
Andreas Hindborg
maintainerRe: [PATCH v14 2/9] rust: rename `AlwaysRefCounted` to `RefCounted`.
In reply to kernel test robotQuoted 15 lines · kernel test robot
Hi Andreas,
kernel test robot noticed the following build errors:
[auto build test ERROR on
63804fed149a6750ffd28610c5c1c98cce6bd377]url: https://github.com/intel-lab-lkp/linux/commits/Andreas-Hindborg/rust-types-Add-Ownable-Owned-types/20260204-200041 base: 63804fed149a6750ffd28610c5c1c98cce6bd377 patch link: https://lore.kernel.org/r/20260204-unique-ref-v14-2-17cb29ebacbb%40kernel.org patch subject: [PATCH v14 2/9] rust: rename `AlwaysRefCounted` to `RefCounted`. config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20260204/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) rustc: rustc 1.88.0 (6b00bc388 2025-06-23) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260204/[email protected]/reproduce)
Quoted 5 lines
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
I guess I have to start building the Rust USB subsystem.
Best regards,
Andreas Hindborg
kernel test robot
build botRe: [PATCH v14 2/9] rust: rename `AlwaysRefCounted` to `RefCounted`.
In reply to Andreas Hindborgkernel test robot noticed the following build errors:
Hi Andreas,
kernel test robot noticed the following build errors:
[auto build test ERROR on 63804fed149a6750ffd28610c5c1c98cce6bd377]
url: https://github.com/intel-lab-lkp/linux/commits/Andreas-Hindborg/rust-types-Add-Ownable-Owned-types/20260204-200041 base: 63804fed149a6750ffd28610c5c1c98cce6bd377 patch link: https://lore.kernel.org/r/20260204-unique-ref-v14-2-17cb29ebacbb%40kernel.org patch subject: [PATCH v14 2/9] rust: rename `AlwaysRefCounted` to `RefCounted`. config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20260207/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) rustc: rustc 1.88.0 (6b00bc388 2025-06-23) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260207/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> error[E0407]: method `inc_ref` is not a member of trait `AlwaysRefCounted`
--> rust/kernel/usb.rs:378:5
|
378 | / fn inc_ref(&self) {
... |
383 | | }
| |_____^ not a member of trait AlwaysRefCounted
--
>> error[E0407]: method `dec_ref` is not a member of trait `AlwaysRefCounted`
--> rust/kernel/usb.rs:385:5
|
385 | / unsafe fn dec_ref(obj: NonNull<Self>) {
386 | | // SAFETY: The safety requirements guarantee that the refcount is non-zero.
387 | | unsafe { bindings::usb_put_intf(obj.cast().as_ptr()) }
388 | | }
| |_____^ not a member of trait `AlwaysRefCounted`
--
>> error[E0277]: the trait bound `Interface: RefCounted` is not satisfied --> rust/kernel/device.rs:700:17
|
700 | (&**dev).into()
| ^^^^^^^^^^^^^^^ the trait RefCounted is not implemented for Interface
|
::: rust/kernel/usb.rs:352:1
|
352 | kernel::impl_device_context_into_aref!(Interface);
| ------------------------------------------------- in this macro invocation
|
= help: the following other types implement trait RefCounted:
Credential
FwNode
I2cAdapter
I2cClient
LocalFile
Mm
MmWithUser
MmWithUserAsync
and 8 others
note: required by a bound in ARef
--> rust/kernel/sync/aref.rs:86:20
|
86 | pub struct ARef<T: RefCounted> {
| ^^^^^^^^^^ required by this bound in ARef
= note: this error originates in the macro ::kernel::__impl_device_context_into_aref which comes from the expansion of the macro kernel::impl_device_context_into_aref (in Nightly builds, run with -Z macro-backtrace for more info)
--
>> error[E0277]: the trait bound `usb::Device: RefCounted` is not satisfied
--> rust/kernel/device.rs:700:17
|
700 | (&**dev).into()
| ^^^^^^^^^^^^^^^ the trait RefCounted is not implemented for usb::Device
|
::: rust/kernel/usb.rs:425:1
|
425 | kernel::impl_device_context_into_aref!(Device);
| ---------------------------------------------- in this macro invocation
|
= help: the following other types implement trait RefCounted:
Credential
FwNode
I2cAdapter
I2cClient
LocalFile
Mm
MmWithUser
MmWithUserAsync
and 8 others
note: required by a bound in ARef
--> rust/kernel/sync/aref.rs:86:20
|
86 | pub struct ARef<T: RefCounted> {
| ^^^^^^^^^^ required by this bound in ARef
= note: this error originates in the macro ::kernel::__impl_device_context_into_aref which comes from the expansion of the macro kernel::impl_device_context_into_aref (in Nightly builds, run with -Z macro-backtrace for more info)
--
>> error[E0407]: method `inc_ref` is not a member of trait `AlwaysRefCounted`
--> rust/kernel/usb.rs:429:5
|
429 | / fn inc_ref(&self) {
... |
434 | | }
| |_____^ not a member of trait AlwaysRefCounted
--
>> error[E0407]: method `dec_ref` is not a member of trait `AlwaysRefCounted`
--> rust/kernel/usb.rs:436:5
|
436 | / unsafe fn dec_ref(obj: NonNull<Self>) {
437 | | // SAFETY: The safety requirements guarantee that the refcount is non-zero.
438 | | unsafe { bindings::usb_put_dev(obj.cast().as_ptr()) }
439 | | }
| |_____^ not a member of trait `AlwaysRefCounted`
--
>> error[E0277]: the trait bound `Interface: RefCounted` is not satisfied --> rust/kernel/device.rs:698:56
|
698 | impl ::core::convert::From<&$device<$src>> for $crate::sync::aref::ARef<$device> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait RefCounted is not implemented for Interface
|
::: rust/kernel/usb.rs:352:1
|
352 | kernel::impl_device_context_into_aref!(Interface);
| ------------------------------------------------- in this macro invocation
|
= help: the following other types implement trait RefCounted:
Credential
FwNode
I2cAdapter
I2cClient
LocalFile
Mm
MmWithUser
MmWithUserAsync
and 8 others
note: required by a bound in ARef
--> rust/kernel/sync/aref.rs:86:20
|
86 | pub struct ARef<T: RefCounted> {
| ^^^^^^^^^^ required by this bound in ARef
= note: this error originates in the macro ::kernel::__impl_device_context_into_aref which comes from the expansion of the macro kernel::impl_device_context_into_aref (in Nightly builds, run with -Z macro-backtrace for more info)
--
>> error[E0277]: the trait bound `Interface: RefCounted` is not satisfied
--> rust/kernel/usb.rs:377:34
|
377 | unsafe impl AlwaysRefCounted for Interface {
| ^^^^^^^^^ the trait RefCounted is not implemented for Interface
|
= help: the following other types implement trait RefCounted:
Credential
FwNode
I2cAdapter
I2cClient
LocalFile
Mm
MmWithUser
MmWithUserAsync
and 8 others
note: required by a bound in AlwaysRefCounted
--> rust/kernel/sync/aref.rs:74:36
|
74 | pub unsafe trait AlwaysRefCounted: RefCounted {}
| ^^^^^^^^^^ required by this bound in AlwaysRefCounted
--
>> error[E0277]: the trait bound `usb::Device: RefCounted` is not satisfied
--> rust/kernel/device.rs:698:56
|
698 | impl ::core::convert::From<&$device<$src>> for $crate::sync::aref::ARef<$device> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait RefCounted is not implemented for usb::Device
|
::: rust/kernel/usb.rs:425:1
|
425 | kernel::impl_device_context_into_aref!(Device);
| ---------------------------------------------- in this macro invocation
|
= help: the following other types implement trait RefCounted:
Credential
FwNode
I2cAdapter
I2cClient
LocalFile
Mm
MmWithUser
MmWithUserAsync
and 8 others
note: required by a bound in ARef
--> rust/kernel/sync/aref.rs:86:20
|
86 | pub struct ARef<T: RefCounted> {
| ^^^^^^^^^^ required by this bound in ARef
= note: this error originates in the macro ::kernel::__impl_device_context_into_aref which comes from the expansion of the macro kernel::impl_device_context_into_aref (in Nightly builds, run with -Z macro-backtrace for more info)
--
>> error[E0277]: the trait bound `usb::Device: RefCounted` is not satisfied
--> rust/kernel/usb.rs:428:34
|
428 | unsafe impl AlwaysRefCounted for Device {
| ^^^^^^ the trait RefCounted is not implemented for usb::Device
|
= help: the following other types implement trait RefCounted:
Credential
FwNode
I2cAdapter
I2cClient
LocalFile
Mm
MmWithUser
MmWithUserAsync
and 8 others
note: required by a bound in AlwaysRefCounted
--> rust/kernel/sync/aref.rs:74:36
|
74 | pub unsafe trait AlwaysRefCounted: RefCounted {}
| ^^^^^^^^^^ required by this bound in AlwaysRefCounted
--
>> error[E0277]: the trait bound `Interface: RefCounted` is not satisfied
--> rust/kernel/device.rs:699:45
|
699 | fn from(dev: &$device<$src>) -> Self {
| ^^^^ the trait RefCounted is not implemented for Interface
|
::: rust/kernel/usb.rs:352:1
|
352 | kernel::impl_device_context_into_aref!(Interface);
| ------------------------------------------------- in this macro invocation
|
= help: the following other types implement trait RefCounted:
Credential
FwNode
I2cAdapter
I2cClient
LocalFile
Mm
MmWithUser
MmWithUserAsync
and 8 others
note: required by a bound in ARef
--> rust/kernel/sync/aref.rs:86:20
|
86 | pub struct ARef<T: RefCounted> {
| ^^^^^^^^^^ required by this bound in ARef
= note: this error originates in the macro ::kernel::__impl_device_context_into_aref which comes from the expansion of the macro kernel::impl_device_context_into_aref (in Nightly builds, run with -Z macro-backtrace for more info)
..
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki