mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Almeida <daniel.almeida@collabora.com>
To: Lyude Paul <lyude@redhat.com>
Cc: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org,
	"Asahi Lina" <lina@asahilina.net>,
	"Danilo Krummrich" <dakr@kernel.org>,
	mcanal@igalia.com, airlied@redhat.com, zhiw@nvidia.com,
	cjia@nvidia.com, jhubbard@nvidia.com,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"open list" <linux-kernel@vger.kernel.org>
Subject: Re: [WIP RFC v2 10/35] rust: drm/kms: Add DriverConnector::get_mode callback
Date: Wed, 27 Nov 2024 12:03:14 -0300	[thread overview]
Message-ID: <36E11299-A121-46DB-8F8D-45AC4B99BCE6@collabora.com> (raw)
In-Reply-To: <20240930233257.1189730-11-lyude@redhat.com>

Hi Lyude,

> On 30 Sep 2024, at 20:09, Lyude Paul <lyude@redhat.com> wrote:
> 
> Next up is filling out some of the basic connector hotplugging callbacks -
> which we'll need for setting up the fbdev helpers for KMS devices. Note
> that connector hotplugging in DRM follows a BFL scheme: pretty much all

A what scheme?

> probing is protected under the mighty drm_device->mode_config.lock, which
> of course is a bit counter-intuitive to rust's locking schemes where data
> is always associated with its lock.
> 
> Since that lock is embedded in an FFI type and not a rust type, we need to
> introduce our own wrapper type that acts as a lock acquisition for this.
> This brings us to introducing a few new types:
> 
> * ModeConfigGuard - the most basic lock guard, as long as this object is
>  alive we are guaranteed to be holding drm_device->mode_config.lock. This
>  object doesn't do much else on its own currently.
> * ConnectorGuard - an object which corresponds to a specific typed DRM
>  connector. This can only be acquired with a ModeConfigGuard, and will be
>  used to allow calling methods that are only safe to call with
>  drm_device->mode_config.lock held. Since it implements
>  Deref<Target=Connector<T>> as well, it can also be used for any other
>  operations that would normally be available on a DRM connector.
> 
> And finally, we add the DriverConnector::get_modes() trait method which
> drivers can use to implement the drm_connector_helper_funcs.get_modes
> callback. Note that while we make this trait method mandatory, we only do
> so for the time being since VKMS doesn't do very much with DRM connectors -
> and as such we have no need yet to implement alternative connector probing
> schemes outside of get_modes().
> 
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
> rust/kernel/drm/kms.rs           | 70 ++++++++++++++++++++++++++++++++
> rust/kernel/drm/kms/connector.rs | 57 ++++++++++++++++++++++++--
> 2 files changed, 124 insertions(+), 3 deletions(-)
> 
> diff --git a/rust/kernel/drm/kms.rs b/rust/kernel/drm/kms.rs
> index d5cad598f016f..d74267c78864f 100644
> --- a/rust/kernel/drm/kms.rs
> +++ b/rust/kernel/drm/kms.rs
> @@ -18,6 +18,8 @@
>     types::*,
>     error::to_result,
>     private::Sealed,
> +    sync::{Mutex, MutexGuard},
> +    container_of
> };
> use core::{
>     ops::Deref,
> @@ -233,6 +235,21 @@ impl<T, K> KmsDriver for T
>     T: Driver<Kms = K>,
>     K: Kms<Driver = T> {}
> 
> +impl<T: KmsDriver> Device<T> {
> +    /// Retrieve a pointer to the mode_config mutex
> +    #[inline]
> +    pub(crate) fn mode_config_mutex(&self) -> &Mutex<()> {
> +        // SAFETY: This lock is initialized for as long as `Device<T>` is exposed to users
> +        unsafe { Mutex::from_raw(&mut (*self.as_raw()).mode_config.mutex) }

Again, a bit hard to understand what’s going on here, since everything is on a single line.

> +    }
> +
> +    /// Acquire the [`mode_config.mutex`] for this [`Device`].
> +    #[inline]
> +    pub fn mode_config_lock(&self) -> ModeConfigGuard<'_, T> {
> +        ModeConfigGuard(self.mode_config_mutex().lock(), PhantomData)
> +    }
> +}
> +
> /// A modesetting object in DRM.
> ///
> /// This is any type of object where the underlying C object contains a [`struct drm_mode_object`].
> @@ -339,3 +356,56 @@ unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
>         unsafe { bindings::drm_mode_object_put(obj.as_ref().raw_mode_obj()) }
>     }
> }
> +
> +/// A mode config guard.
> +///
> +/// This is an exclusive primitive that represents when [`drm_device.mode_config.mutex`] is held - as
> +/// some modesetting operations (particularly ones related to [`connectors`](connector)) are still
> +/// protected under this single lock. The lock will be dropped once this object is dropped.
> +///
> +/// # Invariants
> +///
> +/// - `self.0` is contained within a [`struct drm_mode_config`], which is contained within a
> +///   [`struct drm_device`].
> +/// - The [`KmsDriver`] implementation of that [`struct drm_device`] is always `T`.
> +/// - This type proves that [`drm_device.mode_config.mutex`] is acquired.
> +///
> +/// [`struct drm_mode_config`]: (srctree/include/drm/drm_device.h)
> +/// [`drm_device.mode_config.mutex`]: (srctree/include/drm/drm_device.h)
> +/// [`struct drm_device`]: (srctree/include/drm/drm_device.h)
> +pub struct ModeConfigGuard<'a, T: KmsDriver>(MutexGuard<'a, ()>, PhantomData<T>);
> +
> +impl<'a, T: KmsDriver> ModeConfigGuard<'a, T> {
> +    /// Construct a new [`ModeConfigGuard`].
> +    ///
> +    /// # Safety
> +    ///
> +    /// The caller must ensure that [`drm_device.mode_config.mutex`] is acquired.
> +    ///
> +    /// [`drm_device.mode_config.mutex`]: (srctree/include/drm/drm_device.h)
> +    pub(crate) unsafe fn new(drm: &'a Device<T>) -> Self {
> +        // SAFETY: Our safety contract fulfills the requirements of `MutexGuard::new()`
> +        Self(unsafe { MutexGuard::new(drm.mode_config_mutex(), ()) }, PhantomData)
> +    }
> +
> +    /// Return the [`Device`] that this [`ModeConfigGuard`] belongs to.
> +    pub fn drm_dev(&self) -> &'a Device<T> {
> +        // SAFETY:
> +        // - `self` is embedded within a `drm_mode_config` via our type invariants
> +        // - `self.0.lock` has an equivalent data type to `mutex` via its type invariants.
> +        let mode_config = unsafe { container_of!(self.0.lock, bindings::drm_mode_config, mutex) };
> +
> +        // SAFETY: And that `drm_mode_config` lives in a `drm_device` via type invariants.
> +        unsafe { Device::borrow(container_of!(mode_config, bindings::drm_device, mode_config)) }
> +    }
> +
> +    /// Assert that the given device is the owner of this mode config guard.
> +    ///
> +    /// # Panics
> +    ///
> +    /// Panics if `dev` is different from the owning device for this mode config guard.
> +    #[inline]
> +    pub(crate) fn assert_owner(&self, dev: &Device<T>) {
> +        assert!(ptr::eq(self.drm_dev(), dev));
> +    }
> +}
> diff --git a/rust/kernel/drm/kms/connector.rs b/rust/kernel/drm/kms/connector.rs
> index 54457b327c365..57ab29473c344 100644
> --- a/rust/kernel/drm/kms/connector.rs
> +++ b/rust/kernel/drm/kms/connector.rs
> @@ -19,7 +19,7 @@
> use core::{
>     marker::*,
>     ptr::null_mut,
> -    mem,
> +    mem::{self, ManuallyDrop},
>     ptr::{self, NonNull, addr_of_mut},
>     ffi::*,
>     ops::*,
> @@ -28,6 +28,7 @@
> use super::{
>     ModeObject,
>     RcModeObject,
> +    ModeConfigGuard,
>     encoder::*,
>     KmsDriver,
> };
> @@ -93,7 +94,7 @@ pub trait DriverConnector: Send + Sync + Sized {
>             destroy: Some(connector_destroy_callback::<Self>),
>             force: None,
>             detect: None,
> -            fill_modes: None,
> +            fill_modes: Some(bindings::drm_helper_probe_single_connector_modes),
>             debugfs_init: None,
>             oob_hotplug_event: None,
>             atomic_duplicate_state: Some(atomic_duplicate_state_callback::<Self::State>),
> @@ -101,7 +102,7 @@ pub trait DriverConnector: Send + Sync + Sized {
>         helper_funcs: bindings::drm_connector_helper_funcs {
>             mode_valid: None,
>             atomic_check: None,
> -            get_modes: None,
> +            get_modes: Some(get_modes_callback::<Self>),
>             detect_ctx: None,
>             enable_hpd: None,
>             disable_hpd: None,
> @@ -132,6 +133,12 @@ pub trait DriverConnector: Send + Sync + Sized {
>     ///
>     /// Drivers may use this to instantiate their [`DriverConnector`] object.
>     fn new(device: &Device<Self::Driver>, args: Self::Args) -> impl PinInit<Self, Error>;
> +
> +    /// Retrieve a list of available display modes for this [`Connector`].
> +    fn get_modes<'a>(
> +        connector: ConnectorGuard<'a, Self>,
> +        guard: &ModeConfigGuard<'a, Self::Driver>
> +    ) -> i32;
> }
> 
> /// The generated C vtable for a [`DriverConnector`].
> @@ -229,6 +236,19 @@ pub fn new(
>         })
>     }
> 
> +    /// Acquire a [`ConnectorGuard`] for this connector from a [`ModeConfigGuard`].
> +    ///
> +    /// This verifies using the provided reference that the given guard is actually for the same
> +    /// device as this connector's parent.
> +    ///
> +    /// # Panics
> +    ///
> +    /// Panics if `guard` is not a [`ModeConfigGuard`] for this connector's parent [`Device`].
> +    pub fn guard<'a>(&'a self, guard: &ModeConfigGuard<'a, T::Driver>) -> ConnectorGuard<'a, T> {
> +        guard.assert_owner(self.drm_dev());
> +        ConnectorGuard(self)
> +    }
> +
>     /// Attach an encoder to this [`Connector`].
>     ///
>     /// TODO: Move this to an `UnregisteredConnector` interface somehow…
> @@ -327,6 +347,37 @@ unsafe fn from_raw<'a>(ptr: *mut bindings::drm_connector) -> &'a Self {
>     drop(unsafe { Box::from_raw(connector as *mut Connector<T>) });
> }
> 
> +unsafe extern "C" fn get_modes_callback<T: DriverConnector>(
> +    connector: *mut bindings::drm_connector,
> +) -> c_int {
> +    // SAFETY: This is safe via `DriverConnector`s type invariants.
> +    let connector = unsafe { Connector::<T>::from_raw(connector) };
> +
> +    // SAFETY: This FFI callback is only called while `mode_config.lock` is held
> +    let guard = ManuallyDrop::new(unsafe { ModeConfigGuard::new(connector.drm_dev()) });

I’m confused. Can you explain what this ManuallyDrop is being used for?

> +
> +    T::get_modes(connector.guard(&guard), &guard)
> +}
> +
> +/// A privileged [`Connector`] obtained while holding a [`ModeConfigGuard`].
> +///
> +/// This provides access to various methods for [`Connector`] that must happen under lock, such as
> +/// setting resolution preferences and adding display modes.
> +///
> +/// # Invariants
> +///
> +/// Shares the invariants of [`ModeConfigGuard`].
> +#[derive(Copy, Clone)]
> +pub struct ConnectorGuard<'a, T: DriverConnector>(&'a Connector<T>);
> +
> +impl<T: DriverConnector> Deref for ConnectorGuard<'_, T> {
> +    type Target = Connector<T>;
> +
> +    fn deref(&self) -> &Self::Target {
> +        self.0
> +    }
> +}
> +
> // SAFETY: DRM expects this struct to be zero-initialized
> unsafe impl Zeroable for bindings::drm_connector_state {}
> 
> -- 
> 2.46.1
> 

— Daniel


  reply	other threads:[~2024-11-27 15:04 UTC|newest]

Thread overview: 109+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20240930233257.1189730-1-lyude@redhat.com>
2024-09-30 23:09 ` [WIP RFC v2 01/35] WIP: rust/drm: Add fourcc bindings Lyude Paul
2024-10-01  9:25   ` Jani Nikula
2024-10-01 15:18     ` Miguel Ojeda
2024-10-03  8:33   ` Louis Chauvet
2024-10-03 20:16     ` Lyude Paul
2024-11-26 17:40   ` Daniel Almeida
2024-11-27 21:11     ` Lyude Paul
2025-01-14 18:54       ` Daniel Almeida
2024-09-30 23:09 ` [WIP RFC v2 02/35] WIP: rust: drm: Add traits for registering KMS devices Lyude Paul
2024-11-26 18:18   ` Daniel Almeida
2024-11-27 21:21     ` Lyude Paul
2024-12-05 14:03       ` Daniel Almeida
2024-12-03 22:41     ` Lyude Paul
2024-12-05 13:43       ` Daniel Almeida
2024-12-06 15:23   ` Alice Ryhl
2024-12-09 23:20     ` Lyude Paul
2024-09-30 23:09 ` [WIP RFC v2 03/35] rust: drm/kms/fbdev: Add FbdevShmem Lyude Paul
2024-11-26 19:58   ` Daniel Almeida
2024-09-30 23:09 ` [WIP RFC v2 04/35] rust: drm/kms: Introduce the main ModeConfigObject traits Lyude Paul
2024-11-26 20:34   ` Daniel Almeida
2024-09-30 23:09 ` [WIP RFC v2 05/35] rust: drm/kms: Add bindings for drm_connector Lyude Paul
2024-11-26 21:25   ` Daniel Almeida
2024-12-04 21:16     ` Lyude Paul
2024-12-04 21:18       ` Lyude Paul
2024-12-10 23:41     ` Lyude Paul
2024-12-11  8:43       ` Simona Vetter
2024-12-12  0:34         ` Lyude Paul
2024-12-12 10:03           ` Simona Vetter
2024-09-30 23:09 ` [WIP RFC v2 06/35] rust: drm/kms: Add drm_plane bindings Lyude Paul
2024-10-03  8:30   ` Louis Chauvet
2024-10-03 20:06     ` Lyude Paul
2024-11-27 14:05   ` Daniel Almeida
2024-12-12 21:28     ` Lyude Paul
2024-09-30 23:09 ` [WIP RFC v2 07/35] WIP: rust: drm/kms: Add drm_crtc bindings Lyude Paul
2024-11-27 14:36   ` Daniel Almeida
2024-12-12 22:25     ` Lyude Paul
2024-09-30 23:09 ` [WIP RFC v2 08/35] rust: drm/kms: Add bindings for drm_encoder Lyude Paul
2024-09-30 23:09 ` [WIP RFC v2 09/35] WIP: rust: drm/kms: Add Connector.attach_encoder() Lyude Paul
2024-11-27 14:43   ` Daniel Almeida
2024-09-30 23:09 ` [WIP RFC v2 10/35] rust: drm/kms: Add DriverConnector::get_mode callback Lyude Paul
2024-11-27 15:03   ` Daniel Almeida [this message]
2024-12-12 22:37     ` Lyude Paul
2024-09-30 23:09 ` [WIP RFC v2 11/35] rust: drm/kms: Add ConnectorGuard::add_modes_noedid() Lyude Paul
2024-11-27 15:06   ` Daniel Almeida
2024-09-30 23:09 ` [WIP RFC v2 12/35] rust: drm/kms: Add ConnectorGuard::set_preferred_mode Lyude Paul
2024-11-27 15:11   ` Daniel Almeida
2024-09-30 23:09 ` [WIP RFC v2 13/35] WIP: rust: drm/kms: Add OpaqueConnector and OpaqueConnectorState Lyude Paul
2024-11-27 15:51   ` Daniel Almeida
2024-12-05 23:25     ` Lyude Paul
2024-09-30 23:09 ` [WIP RFC v2 14/35] WIP: rust: drm/kms: Add OpaqueCrtc and OpaqueCrtcState Lyude Paul
2024-11-27 16:00   ` Daniel Almeida
2024-12-12 23:01     ` Lyude Paul
2024-09-30 23:09 ` [WIP RFC v2 15/35] WIP: rust: drm/kms: Add OpaquePlane and OpaquePlaneState Lyude Paul
2024-11-27 17:03   ` Daniel Almeida
2024-09-30 23:09 ` [WIP RFC v2 16/35] rust: drm/kms: Add RawConnector and RawConnectorState Lyude Paul
2024-11-27 19:26   ` Daniel Almeida
2024-09-30 23:10 ` [WIP RFC v2 17/35] rust: drm/kms: Add RawCrtc and RawCrtcState Lyude Paul
2024-11-27 19:29   ` Daniel Almeida
2024-09-30 23:10 ` [WIP RFC v2 18/35] rust: drm/kms: Add RawPlane and RawPlaneState Lyude Paul
2024-11-27 19:30   ` Daniel Almeida
2024-09-30 23:10 ` [WIP RFC v2 19/35] WIP: rust: drm/kms: Add OpaqueEncoder Lyude Paul
2024-11-27 19:35   ` Daniel Almeida
2024-09-30 23:10 ` [WIP RFC v2 20/35] WIP: rust: drm/kms: Add drm_atomic_state bindings Lyude Paul
2024-11-27 20:54   ` Daniel Almeida
2024-12-12 23:37     ` Lyude Paul
2024-09-30 23:10 ` [WIP RFC v2 21/35] rust: drm/kms: Introduce DriverCrtc::atomic_check() Lyude Paul
2024-11-28 13:37   ` Daniel Almeida
2025-01-13 23:43     ` Lyude Paul
2024-09-30 23:10 ` [WIP RFC v2 22/35] rust: drm/kms: Add DriverPlane::atomic_update() Lyude Paul
2024-11-28 13:38   ` Daniel Almeida
2025-01-13 23:47     ` Lyude Paul
2024-11-28 13:51   ` Daniel Almeida
2025-01-13 23:53     ` Lyude Paul
2024-09-30 23:10 ` [WIP RFC v2 23/35] rust: drm/kms: Add DriverPlane::atomic_check() Lyude Paul
2024-11-28 13:49   ` Daniel Almeida
2025-01-13 23:51     ` Lyude Paul
2024-09-30 23:10 ` [WIP RFC v2 24/35] rust: drm/kms: Add RawCrtcState::active() Lyude Paul
2024-11-28 13:54   ` Daniel Almeida
2024-09-30 23:10 ` [WIP RFC v2 25/35] rust: drm/kms: Add RawPlaneState::crtc() Lyude Paul
2024-11-28 13:58   ` Daniel Almeida
2024-09-30 23:10 ` [WIP RFC v2 26/35] WIP: rust: drm/kms: Add RawPlaneState::atomic_helper_check() Lyude Paul
2024-11-28 14:04   ` Daniel Almeida
2025-01-13 23:57     ` Lyude Paul
2025-01-14 14:07       ` Simona Vetter
2024-09-30 23:10 ` [WIP RFC v2 27/35] rust: drm/kms: Add drm_framebuffer bindings Lyude Paul
2024-11-28 14:26   ` Daniel Almeida
2024-09-30 23:10 ` [WIP RFC v2 28/35] rust: drm/kms: Add RawPlane::framebuffer() Lyude Paul
2024-11-28 14:29   ` Daniel Almeida
2025-01-14  0:03     ` Lyude Paul
2025-01-14 14:09       ` Simona Vetter
2024-09-30 23:10 ` [WIP RFC v2 29/35] rust: drm/kms: Add DriverCrtc::atomic_begin() and atomic_flush() Lyude Paul
2024-11-28 14:31   ` Daniel Almeida
2024-09-30 23:10 ` [WIP RFC v2 30/35] rust: drm/kms: Add DriverCrtc::atomic_enable() and atomic_disable() Lyude Paul
2024-11-28 14:33   ` Daniel Almeida
2024-09-30 23:10 ` [WIP RFC v2 31/35] rust: drm: Add Device::event_lock() Lyude Paul
2024-11-28 14:35   ` Daniel Almeida
2024-09-30 23:10 ` [WIP RFC v2 32/35] rust: drm/kms: Add Device::num_crtcs() Lyude Paul
2024-11-28 14:38   ` Daniel Almeida
2025-01-14  0:05     ` Lyude Paul
2024-09-30 23:10 ` [WIP RFC v2 33/35] WIP: rust: drm/kms: Add VblankSupport Lyude Paul
2024-12-05 15:29   ` Daniel Almeida
2025-01-14  0:43     ` Lyude Paul
2025-01-14 14:24       ` Simona Vetter
2025-01-14 15:04         ` Miguel Ojeda
2025-01-14 15:38           ` Simona Vetter
2024-09-30 23:10 ` [WIP RFC v2 34/35] WIP: rust: drm/kms: Add Kms::atomic_commit_tail Lyude Paul
2024-12-05 16:09   ` Daniel Almeida
2024-09-30 23:10 ` [WIP RFC v2 35/35] WIP: drm: Introduce RVKMS! Lyude Paul
2024-12-05 16:36   ` Daniel Almeida

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=36E11299-A121-46DB-8F8D-45AC4B99BCE6@collabora.com \
    --to=daniel.almeida@collabora.com \
    --cc=a.hindborg@samsung.com \
    --cc=airlied@redhat.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=cjia@nvidia.com \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=jhubbard@nvidia.com \
    --cc=lina@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lyude@redhat.com \
    --cc=mcanal@igalia.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=wedsonaf@gmail.com \
    --cc=zhiw@nvidia.com \
    /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

all inboxes | Powered by JetHome®