Re: [PATCH 2/3] rust: devres: implement Devres::access_with()

Danilo Krummrich
2025-04-27T14:17:42Z

On Sun, Apr 27, 2025 at 10:15:17PM +0900, Alexandre Courbot wrote:
> On Sat Apr 26, 2025 at 10:30 PM JST, Danilo Krummrich wrote:
> > Implement a direct accessor for the data stored within the Devres for
> > cases where we can proof that we own a reference to a Device<Bound>
> > (i.e. a bound device) of the same device that was used to create the
> > corresponding Devres container.
> >
> > Usually, when accessing the data stored within a Devres container, it is
> > not clear whether the data has been revoked already due to the device
> > being unbound and, hence, we have to try whether the access is possible
> > and subsequently keep holding the RCU read lock for the duration of the
> > access.
> >
> > However, when we can proof that we hold a reference to Device<Bound>
> > matching the device the Devres container has been created with, we can
> > guarantee that the device is not unbound for the duration of the
> > lifetime of the Device<Bound> reference and, hence, it is not possible
> > for the data within the Devres container to be revoked.
> >
> > Therefore, in this case, we can bypass the atomic check and the RCU read
> > lock, which is a great optimization and simplification for drivers.
> >
> > Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> > ---
> > rust/kernel/devres.rs | 35 +++++++++++++++++++++++++++++++++++
> > 1 file changed, 35 insertions(+)
> >
> > diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs
> > index 1e58f5d22044..ec2cd9cdda8b 100644
> > --- a/rust/kernel/devres.rs
> > +++ b/rust/kernel/devres.rs
> > @@ -181,6 +181,41 @@ pub fn new_foreign_owned(dev: &Device<Bound>, data: T, flags: Flags) -> Result {
> >
> > Ok(())
> > }
> > +
> > + /// Obtain `&'a T`, bypassing the [`Revocable`].
> > + ///
> > + /// This method allows to directly obtain a `&'a T`, bypassing the [`Revocable`], by presenting
> > + /// a `&'a Device<Bound>` of the same [`Device`] this [`Devres`] instance has been created with.
> > + ///
> > + /// An error is returned if `dev` does not match the same [`Device`] this [`Devres`] instance
> > + /// has been created with.
> > + ///
> > + /// # Example
> > + ///
> > + /// ```no_run
> > + /// # use kernel::{device::Core, devres::Devres, pci};
> > + ///
> > + /// fn from_core(dev: &pci::Device<Core>, devres: Devres<pci::Bar<0x4>>) -> Result<()> {
> > + /// let bar = devres.access_with(dev.as_ref())?;
> > + ///
> > + /// let _ = bar.read32(0x0);
> > + ///
> > + /// // might_sleep()
> > + ///
> > + /// bar.write32(0x42, 0x0);
> > + ///
> > + /// Ok(())
> > + /// }
> > + pub fn access_with<'s, 'd: 's>(&'s self, dev: &'d Device<Bound>) -> Result<&'s T> {
>
> Coming from `Revocable::try_access_with` (and the standard library in
> general), the name of this method made me think that it would take a
> closure to run while the resource is held. Maybe we should differenciate
> the names a bit more? Maybe just `access` is fine, or
> `access_with_device`?

Seems reasonable -- access() it is then.