rust/drm: Remove blanket AlwaysRefCounted impl for gem
Subsystems (3)
cover rust/drm: Remove blanket AlwaysRefCounted impl for gem
This patch series simply drops an blanket implementation of AlwaysRefCounted for gem objects, which would cause issues if any other additional blanket implementations of AlwaysRefCounted were present within the same rust crate. While we're at it, we also introduce a macro in lieu of being able to use a blanket implementation.
Lyude Paul (2):
Partially revert "rust: drm: gem: Implement AlwaysRefCounted for all
gem objects automatically"
rust/drm: Add gem::impl_aref_for_gem_obj!
rust/kernel/drm/gem/mod.rs | 59 +++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 20 deletions(-)
base-commit: 6b35936f058d0cb9171c7be1424b62017b874913
Signature · 1 line
2.51.0
PATCH 1/2 Partially revert "rust: drm: gem: Implement AlwaysRefCounted for all gem objects automatically"
I made a very silly mistake with this commit that managed to slip by because I forgot to mzke sure rvkms was rebased before testing my work last
- we can't do blanket implementations like this due to rust's orphan rule.
The code -does- build just fine right now, but it doesn't with the ongoing bindings for gem shmem. So, just revert this and we'll introduce a macro for implementing AlwaysRefCounted individually for each type of gem implementation.
Note that we leave the IntoGEMObject since it is true that all gem objects are refcounted, so any implementations that are added should be implementing AlwaysRefCounted anyhow.
This reverts commit 38cb08c3fcd3f3b1d0225dcec8ae50fab5751549.
--- rust/kernel/drm/gem/mod.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-)
rust/kernel/drm/gem/mod.rs +16 -20
@@ -54,26 +54,6 @@ pub trait IntoGEMObject: Sized + super::private::Sealed + AlwaysRefCounted {unsafe fn from_raw <'a>(self_ptr: *mut bindings::drm_gem_object ) -> &'aSelf ; } -// SAFETY: All gem objects are refcounted. -unsafe impl <T :IntoGEMObject >AlwaysRefCounted for 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 ()) }; - } - -unsafe fn dec_ref (obj:NonNull <Self >) { -// SAFETY: We either hold the only refcount on `obj`, or one of many - meaning that no one -// else could possibly hold a mutable reference to `obj` and thus this immutable reference -// is safe. -let obj =unsafe { obj.as_ref () }.as_raw (); - -// SAFETY: -// - The safety requirements guarantee that the refcount is non-zero. -// - We hold no references to `obj` now, making it safe for us to potentially deallocate it. -unsafe { bindings::drm_gem_object_put (obj) }; - } -} -extern "C" fn open_callback <T :DriverObject >( raw_obj: *mut bindings::drm_gem_object , raw_file: *mut bindings::drm_file,
@@ -272,6 +252,22 @@ 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 > { +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 ()) }; + } + +unsafe fn dec_ref (obj:NonNull <Self >) { +// SAFETY: `obj` is a valid pointer to an `Object<T>`. +let obj =unsafe { obj.as_ref () }; + +// SAFETY: The safety requirements guarantee that the refcount is non-zero. +unsafe { bindings::drm_gem_object_put (obj.as_raw ()) } + } +} +impl <T :DriverObject >super ::private::Sealed for Object <T > {}impl <T :DriverObject >Deref for Object <T > {
Signature · 1 line
2.51.0
Re: [PATCH 1/2] Partially revert "rust: drm: gem: Implement AlwaysRefCounted for all gem objects automatically"
Hi Lyude,
Quoted 4 lines · Lyude Paul
I made a very silly mistake with this commit that managed to slip by because I forgot to mzke sure rvkms was rebased before testing my work last
- we can't do blanket implementations like this due to rust's orphan rule.
In general, I would avoid using "I did something" to describe the issue of a previous commit in the commit log of a patch, it's less objective IMO. Could you reword this a bit? Maybe more focus on why a blanket implementation is problematic.
Thanks!
Regards,
Boqun
Quoted 12 lines
The code -does- build just fine right now, but it doesn't with the ongoing bindings for gem shmem. So, just revert this and we'll introduce a macro for implementing AlwaysRefCounted individually for each type of gem implementation.
Note that we leave the IntoGEMObject since it is true that all gem objects are refcounted, so any implementations that are added should be implementing AlwaysRefCounted anyhow.
This reverts commit
38cb08c3fcd3f3b1d0225dcec8ae50fab5751549.Signed-off-by Lyude Paul <[email protected]>
[...]
PATCH 2/2 rust/drm: Add gem::impl_aref_for_gem_obj!
In reply to Lyude PaulIn the future we're going to be introducing more GEM object types in rust then just gem::Object<T>. Since all types of GEM objects have refcounting, let's introduce a macro that we can use in the gem crate in order to copy this boilerplate implementation for each type: impl_aref_for_gem_obj!().
--- rust/kernel/drm/gem/mod.rs | 53 +++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 15 deletions(-)
rust/kernel/drm/gem/mod.rs +38 -15
@@ -14,6 +14,43 @@ };use core::{ops::Deref , ptr::NonNull }; +/// A macro for implementing [`AlwaysRefCounted`] for any GEM object type. +/// +/// Since all GEM objects use the same refcounting scheme. +macro_rules! impl_aref_for_gem_obj { + ( +impl $( <$( $tparam_id:ident ),+> )?for $type:ty + $( +where + $( $bind_param:path : $bind_trait:path ),+ + )? + ) => { +// SAFETY: All gem objects are refcounted +unsafe impl $( <$( $tparam_id ),+> )?crate ::types::AlwaysRefCounted for $type + $( +where + $( $bind_param : $bind_trait ),+ + )? + { +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 ()) }; + } + +unsafe fn dec_ref (obj: core::ptr::NonNull <Self >) { +// SAFETY: `obj` is a valid pointer to an `Object<T>`. +let obj =unsafe { obj.as_ref () }; + +// SAFETY: The safety requirements guarantee that the refcount is non-zero. +unsafe { bindings::drm_gem_object_put (obj.as_raw ()) }; + } + } + }; +} + +pub (crate )use impl_aref_for_gem_obj; +/// A type alias for retrieving a [`Driver`]s [`DriverFile`] implementation from its /// [`DriverObject`] implementation. ///
@@ -252,21 +289,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 > { -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 ()) }; - } - -unsafe fn dec_ref (obj:NonNull <Self >) { -// SAFETY: `obj` is a valid pointer to an `Object<T>`. -let obj =unsafe { obj.as_ref () }; - -// SAFETY: The safety requirements guarantee that the refcount is non-zero. -unsafe { bindings::drm_gem_object_put (obj.as_raw ()) } - } -} +impl_aref_for_gem_obj! (impl <T >for Object <T >where T :DriverObject );impl <T :DriverObject >super ::private::Sealed for Object <T > {}
Signature · 1 line
2.51.0