From: Lyude Paul <lyude@redhat.com>
To: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org,
Daniel Almeida <daniel.almeida@collabora.com>,
Alice Ryhl <aliceryhl@google.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Asahi Lina" <lina+kernel@asahilina.net>,
"Shankari Anand" <shankari.ak0208@gmail.com>,
linux-kernel@vger.kernel.org (open list)
Subject: [PATCH v4 1/9] Partially revert "rust: drm: gem: Implement AlwaysRefCounted for all gem objects automatically"
Date: Thu, 16 Oct 2025 17:08:14 -0400 [thread overview]
Message-ID: <20251016210955.2813186-2-lyude@redhat.com> (raw)
In-Reply-To: <20251016210955.2813186-1-lyude@redhat.com>
Currently in order to implement AlwaysRefCounted for gem objects, we use a
blanket implementation:
unsafe impl<T: IntoGEMObject> AlwaysRefCounted for T { … }
While this technically works, it comes with the rather unfortunate downside
that attempting to create a similar blanket implementation in any other
kernel crate will now fail in a rather confusing way.
Using an example from the (not yet upstream) rust DRM KMS bindings, if we
were to add:
unsafe impl<T: RcModeObject> AlwaysRefCounted for T { … }
Then the moment that both blanket implementations are present in the same
kernel tree, compilation fails with the following:
error[E0119]: conflicting implementations of trait `types::AlwaysRefCounted`
--> rust/kernel/drm/kms.rs:504:1
|
504 | unsafe impl<T: RcModeObject> AlwaysRefCounted for T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation
|
::: rust/kernel/drm/gem/mod.rs:97:1
|
97 | unsafe impl<T: IntoGEMObject> AlwaysRefCounted for T {
| ---------------------------------------------------- first implementation here
So, revert these changes for now. The proper fix for this is to introduce a
macro for copy/pasting the same implementation of AlwaysRefCounted around.
This reverts commit 38cb08c3fcd3f3b1d0225dcec8ae50fab5751549.
Signed-off-by: Lyude Paul <lyude@redhat.com>
---
V2:
* Rewrite the commit message to explain a bit more why we don't want a
blanket implementation for this.
Signed-off-by: Lyude Paul <lyude@redhat.com>
---
rust/kernel/drm/gem/mod.rs | 36 ++++++++++++++++--------------------
1 file changed, 16 insertions(+), 20 deletions(-)
diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
index 30c853988b942..20c2769a8c9d6 100644
--- a/rust/kernel/drm/gem/mod.rs
+++ b/rust/kernel/drm/gem/mod.rs
@@ -55,26 +55,6 @@ pub trait IntoGEMObject: Sized + super::private::Sealed + AlwaysRefCounted {
unsafe fn from_raw<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self;
}
-// 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,
@@ -273,6 +253,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> {
--
2.51.0
next prev parent reply other threads:[~2025-10-16 21:10 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-16 21:08 [PATCH v4 0/9] Rust bindings for gem shmem + iosys_map Lyude Paul
2025-10-16 21:08 ` Lyude Paul [this message]
2025-10-17 13:11 ` [PATCH v4 1/9] Partially revert "rust: drm: gem: Implement AlwaysRefCounted for all gem objects automatically" Alice Ryhl
2025-10-16 21:08 ` [PATCH v4 2/9] rust/drm: Add gem::impl_aref_for_gem_obj! Lyude Paul
2025-10-17 13:22 ` Alice Ryhl
2025-10-21 17:33 ` Lyude Paul
2025-10-22 8:14 ` Alice Ryhl
2025-10-16 21:08 ` [PATCH v4 3/9] rust: helpers: Add bindings/wrappers for dma_resv_lock Lyude Paul
2025-10-17 13:23 ` Alice Ryhl
2025-10-16 21:08 ` [PATCH v4 4/9] rust: drm: gem: Add raw_dma_resv() function Lyude Paul
2025-10-16 21:08 ` [PATCH v4 5/9] rust: gem: Introduce DriverObject::Args Lyude Paul
2025-10-17 13:24 ` Alice Ryhl
2025-10-16 21:08 ` [PATCH v4 6/9] rust: drm: gem: shmem: Add DRM shmem helper abstraction Lyude Paul
2025-10-17 13:30 ` Alice Ryhl
2025-10-16 21:08 ` [PATCH v4 7/9] rust: drm: gem: Introduce shmem::SGTable Lyude Paul
2025-10-16 21:08 ` [PATCH v4 8/9] rust: Introduce iosys_map bindings Lyude Paul
2025-10-16 22:26 ` Lyude Paul
2025-10-16 21:08 ` [PATCH v4 9/9] rust: drm/gem: Add vmap functions to shmem bindings Lyude Paul
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251016210955.2813186-2-lyude@redhat.com \
--to=lyude@redhat.com \
--cc=a.hindborg@kernel.org \
--cc=airlied@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=lina+kernel@asahilina.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=shankari.ak0208@gmail.com \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).