public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Danilo Krummrich <dakr@kernel.org>
To: Maxime Ripard <mripard@kernel.org>
Cc: airlied@gmail.com, simona@ffwll.ch,
	maarten.lankhorst@linux.intel.com, tzimmermann@suse.de,
	lyude@redhat.com, acurrid@nvidia.com, lina@asahilina.net,
	daniel.almeida@collabora.com, j@jannau.net, 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 5/8] rust: drm: add DRM driver registration
Date: Fri, 28 Mar 2025 15:50:13 +0100	[thread overview]
Message-ID: <Z-a3JY4ytvqHWDWL@pollux> (raw)
In-Reply-To: <20250328-impossible-locust-of-merriment-b625bd@houat>

On Fri, Mar 28, 2025 at 03:28:04PM +0100, Maxime Ripard wrote:
> On Wed, Mar 26, 2025 at 11:46:54AM +0100, Danilo Krummrich wrote:
> > On Wed, Mar 26, 2025 at 10:24:43AM +0100, Maxime Ripard wrote:
> > > On Wed, Mar 26, 2025 at 12:54:32AM +0100, Danilo Krummrich wrote:
> 
> > > drm_dev_unregister also have an hotplug-aware variant in
> > > drm_dev_unplug(). However, most devices are hotpluggable, even if only
> > > through sysfs. So drm_dev_unplug() is generally a better option. Should
> > > we use it here, and / or should we provide multiple options still?
> > > 
> > > Another thing worth mentioning I think is that drm_dev_unplug() works by
> > > setting a flag, and drivers are expected to check that their access to
> > > device-bound resources (so registers mapping, clocks, regulators, etc.)
> > > are still there through drm_dev_enter/drm_dev_exit. It's pretty fragile
> > > overall, so I wonder if it's something we could abstract away in Rust.
> > 
> > DRM should not have to take care of the lifetime of device resources of the
> > parent device. This is the responsibility of the driver core abstractions.
> >
> > At least for the device resources we directly give out to drivers, it has to be,
> > since otherwise it would mean that the driver core abstraction is unsound.
> > Drivers could otherwise extend the lifetime of device resources arbitrarily.
> 
> Sure.
> 
> > For instance, I/O memory is only ever given out by bus abstractions embedded in
> > a Devres container (e.g. Devres<pci::Bar>). Once the parent device is unbound
> > the pci::Bar within the Devres container won't be accessible any more and will
> > be dropped internally. So, that's nothing DRM has to take care of.
> > 
> > However, there are cases where it's better to let subsystem abstractions manage
> > the lifetime of device resources, (e.g. DMA mappings). The relevant thing is,
> > that we never hand out device resources to a driver in a way that the driver can
> > extend their lifetime arbitrarily.
> 
> I was talking about the opposite though: when the driver is still around
> but the device (and its resources) aren't anymore.

Well, that's what I was talking about too. :)

> It makes total sense to tie the lifetime of the device resources to the
> device. However, the DRM device and driver will far outlive the device
> it was bound to so it needs to deal with that kind of degraded "the DRM
> driver can still be called by userspace, but it doesn't have a device
> anymore" scenario. That's what I was talking about.

This scenario should be covered by the things I mentioned above. Let's take the
I/O memory example.

If you call into the DRM driver from userspace when the underlying bus device
has already been unbound, the DRM driver may still hold a Devres<pci::Bar>
instance.

If the DRM driver then calls bar.try_access() (in order to subsequently call
writeX() or readX()) the try_access() call will fail, since the corresponding
PCI device has been unbound already.

In other words the pci::Bar instance within the Devres container will be dropped
(which includes unmapping the bar and releasing the resource region) when the
PCI device is unbound internally and the Devres container will restrict
subsequent accesses from drivers.

It pretty much does the same thing as drm_dev_enter() / drm_dev_exit(), but
additionally prevents access to the (meanwhile) invalid pointer to the device
resource and ensures that the driver can't make device resources out-live device
unbind.

As mentioned above, the Devres container is just one example of how we prevent
such things; it depends on the exact scenario. In either case, I don't want the
driver itself to be responsible to setup the corresponding guards, that would
make the corresponding abstractions unsound.

  reply	other threads:[~2025-03-28 14:50 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-25 23:54 [PATCH 0/8] DRM Rust abstractions Danilo Krummrich
2025-03-25 23:54 ` [PATCH 1/8] drm: drv: implement __drm_dev_alloc() Danilo Krummrich
2025-03-26  8:46   ` Maxime Ripard
2025-03-25 23:54 ` [PATCH 2/8] rust: drm: ioctl: Add DRM ioctl abstraction Danilo Krummrich
2025-03-25 23:54 ` [PATCH 3/8] rust: drm: add driver abstractions Danilo Krummrich
2025-03-26  9:05   ` Maxime Ripard
2025-03-28 22:00   ` Lyude Paul
2025-03-28 22:46     ` Danilo Krummrich
2025-03-25 23:54 ` [PATCH 4/8] rust: drm: add device abstraction Danilo Krummrich
2025-03-26  9:12   ` Maxime Ripard
2025-03-25 23:54 ` [PATCH 5/8] rust: drm: add DRM driver registration Danilo Krummrich
2025-03-26  9:24   ` Maxime Ripard
2025-03-26 10:46     ` Danilo Krummrich
2025-03-28 14:28       ` Maxime Ripard
2025-03-28 14:50         ` Danilo Krummrich [this message]
2025-04-10  9:23           ` Maxime Ripard
2025-03-25 23:54 ` [PATCH 6/8] rust: drm: file: Add File abstraction Danilo Krummrich
2025-03-28 14:42   ` Maxime Ripard
2025-03-25 23:54 ` [PATCH 7/8] rust: drm: gem: Add GEM object abstraction Danilo Krummrich
2025-03-25 23:54 ` [PATCH 8/8] MAINTAINERS: add DRM Rust source files to DRM DRIVERS Danilo Krummrich
2025-03-28 14:49   ` Maxime Ripard
2025-03-28 14:50     ` Danilo Krummrich
2025-04-08 16:29 ` [PATCH 0/8] DRM Rust abstractions Asahi Lina
2025-04-08 17:04   ` Danilo Krummrich
2025-04-08 18:06     ` Asahi Lina
2025-04-08 19:17       ` Danilo Krummrich
2025-04-09  7:49         ` Asahi Lina
2025-04-09 21:29           ` Dave Airlie
2025-04-10  4:33             ` Asahi Lina
2025-04-10  7:12               ` Asahi Lina
2025-04-10 10:23                 ` Danilo Krummrich
2025-04-10 12:37                   ` Asahi Lina
2025-04-10 13:33                     ` Danilo Krummrich
2025-04-11  0:43                     ` Dave Airlie
2025-04-11  6:53                       ` Asahi Lina
2025-04-10  6:44             ` Simona Vetter
2025-04-10  8:14 ` Asahi Lina

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=Z-a3JY4ytvqHWDWL@pollux \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acurrid@nvidia.com \
    --cc=airlied@gmail.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=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