rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Danilo Krummrich <dakr@kernel.org>
To: Lyude Paul <lyude@redhat.com>
Cc: airlied@gmail.com, simona@ffwll.ch,
	maarten.lankhorst@linux.intel.com, mripard@kernel.org,
	tzimmermann@suse.de, lina@asahilina.net,
	daniel.almeida@collabora.com, j@jannau.net, alyssa@rosenzweig.io,
	ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com,
	gary@garyguo.net, bjorn3_gh@protonmail.com,
	benno.lossin@proton.me, a.hindborg@kernel.org,
	aliceryhl@google.com, tmgross@umich.edu,
	dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v2 7/8] rust: drm: gem: Add GEM object abstraction
Date: Thu, 17 Apr 2025 22:31:46 +0200	[thread overview]
Message-ID: <aAFlMmZxa8V-SFn7@cassiopeiae> (raw)
In-Reply-To: <1ea450fdef728a5c783738c0770ea38ba6db39f2.camel@redhat.com>

On Thu, Apr 17, 2025 at 02:42:24PM -0400, Lyude Paul wrote:
> On Fri, 2025-04-11 at 01:55 +0200, Danilo Krummrich wrote:
> > +/// A base GEM object.
> > +///
> > +/// Invariants
> > +///
> > +/// `self.dev` is always a valid pointer to a `struct drm_device`.
> > +#[repr(C)]
> > +#[pin_data]
> > +pub struct Object<T: DriverObject + Send + Sync> {
> > +    obj: Opaque<bindings::drm_gem_object>,
> > +    dev: ptr::NonNull<bindings::drm_device>,
> 
> Not a huge deal but why don't we just use NonNull<device::Device<T::Driver>>
> here?

Yeah, we could indeed also use NonNull<drm::Device<T::Driver>> instead, but I
think it doesn't really make a difference.

We only need it in Object::dev(), and the unsafe call would change from

	unsafe { drm::Device::as_ref(self.dev.as_ptr()) }

to
	unsafe { &*self.dev.as_ptr() }

I'm fine either way.

> > +// 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: 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()) }
> > +    }
> > +}
> 
> So - as far as I can tell pretty much every gem object is going to be using
> the same object_get/object_put() functions - so instead of implementing
> AlwaysRefCounted for Object<T> why not handle this the other way around?
> 
> unsafe impl<T: IntoGEMObject> AlwaysRefCounted for T {
>   /* ... */
> }
> 
> That way you can also make IntoGEMObject a super-trait of AlwaysRefCounted, so
> the AlwaysRefCounted trait bound will be implied instead of having to specify
> it manually all over the place.

That is a great idea!

Since the current implementation should be correct, do you want to implement
this improvement in a subsequent patch? :)

  reply	other threads:[~2025-04-17 20:31 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-10 23:55 [PATCH v2 0/8] DRM Rust abstractions Danilo Krummrich
2025-04-10 23:55 ` [PATCH v2 1/8] drm: drv: implement __drm_dev_alloc() Danilo Krummrich
2025-04-14 13:27   ` Alyssa Rosenzweig
2025-04-18 21:00   ` Lyude Paul
2025-04-10 23:55 ` [PATCH v2 2/8] rust: drm: ioctl: Add DRM ioctl abstraction Danilo Krummrich
2025-04-14 13:54   ` Alyssa Rosenzweig
2025-04-18 21:03   ` Lyude Paul
2025-04-10 23:55 ` [PATCH v2 3/8] rust: drm: add driver abstractions Danilo Krummrich
2025-04-14 14:00   ` Alyssa Rosenzweig
2025-04-18 21:06   ` Lyude Paul
2025-04-10 23:55 ` [PATCH v2 4/8] rust: drm: add device abstraction Danilo Krummrich
2025-04-14 14:07   ` Alyssa Rosenzweig
2025-04-17 18:53   ` Lyude Paul
2025-04-17 20:20     ` Danilo Krummrich
2025-04-10 23:55 ` [PATCH v2 5/8] rust: drm: add DRM driver registration Danilo Krummrich
2025-04-14 14:11   ` Alyssa Rosenzweig
2025-04-18 21:12   ` Lyude Paul
2025-04-10 23:55 ` [PATCH v2 6/8] rust: drm: file: Add File abstraction Danilo Krummrich
2025-04-14 14:28   ` Alyssa Rosenzweig
2025-04-18 21:15   ` Lyude Paul
2025-04-10 23:55 ` [PATCH v2 7/8] rust: drm: gem: Add GEM object abstraction Danilo Krummrich
2025-04-14 15:07   ` Alyssa Rosenzweig
2025-04-17 18:42   ` Lyude Paul
2025-04-17 20:31     ` Danilo Krummrich [this message]
2025-04-17 22:33       ` Lyude Paul
2025-04-18  5:31         ` Danilo Krummrich
2025-05-12 11:41   ` Miguel Ojeda
2025-05-12 11:48     ` Miguel Ojeda
2025-05-12 12:09     ` Danilo Krummrich
2025-05-12 12:38       ` Miguel Ojeda
2025-04-10 23:55 ` [PATCH v2 8/8] MAINTAINERS: add DRM Rust source files to DRM DRIVERS Danilo Krummrich
2025-04-14 15:08   ` Alyssa Rosenzweig
2025-04-18 21:16 ` [PATCH v2 0/8] DRM Rust abstractions Lyude Paul
2025-04-24 13:59 ` Danilo Krummrich
  -- strict thread matches above, loose matches on Subject: below --
2024-06-18 23:31 [PATCH v2 0/8] DRM Rust abstractions and Nova Danilo Krummrich
2024-06-18 23:31 ` [PATCH v2 7/8] rust: drm: gem: Add GEM object abstraction Danilo Krummrich

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=aAFlMmZxa8V-SFn7@cassiopeiae \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=airlied@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=alyssa@rosenzweig.io \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=j@jannau.net \
    --cc=lina@asahilina.net \
    --cc=lyude@redhat.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tmgross@umich.edu \
    --cc=tzimmermann@suse.de \
    /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).