Devres optimization with bound devices
Subsystems (4)
Danilo Krummrich
maintainerv2 cover Devres optimization with bound devices
This patch series implements a direct accessor for the data stored within a Devres container for cases where we can prove 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 prove 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.
The patches of this series are also available in [1].
[1] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/devres
Changes in v2:
- Revocable::access(): remvoe explicit lifetimes; don't refer to 'a in the safety requirement
- Devres::access()
- rename Devres::access_with() to Devres::access()
- add missing '```' at the end of the example
- remove 's lifetime
- add # Errors section
Danilo Krummrich (3):
rust: revocable: implement Revocable::access()
rust: devres: implement Devres::access_with()
samples: rust: pci: take advantage of Devres::access_with()
rust/kernel/devres.rs | 38 +++++++++++++++++++++++++++++++++ rust/kernel/revocable.rs | 12 +++++++++++ samples/rust/rust_driver_pci.rs | 12 +++++------ 3 files changed, 56 insertions(+), 6 deletions(-)
base-commit: 3be746ebc1e6e32f499a65afe405df9030153a63
Signature · 1 line
2.49.0
Danilo Krummrich
maintainerPATCH v2 1/3 rust: revocable: implement Revocable::access()
Implement an unsafe direct accessor for the data stored within the Revocable.
This is useful for cases where we can prove that the data stored within the Revocable is not and cannot be revoked for the duration of the lifetime of the returned reference.
--- rust/kernel/revocable.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+)
rust/kernel/revocable.rs +12 -0
@@ -139,6 +139,18 @@ pub fn try_access_with<R, F: FnOnce(&T) -> R>(&self, f: F) -> Option<R> { self.try_access ().map (|t|f (&*t)) } +/// Directly access the revocable wrapped object. +/// +/// # Safety +/// +/// The caller must ensure this [`Revocable`] instance hasn't been revoked and won't be revoked +/// as long as the returned `&T` lives. +pub unsafe fn access (&self) -> &T { +// SAFETY: By the safety requirement of this function it is guaranteed that +// `self.data.get()` is a valid pointer to an instance of `T`. +unsafe { &*self.data.get () } + } +/// # Safety /// /// Callers must ensure that there are no more concurrent users of the revocable object.
Signature · 1 line
2.49.0
Re: [PATCH v2 1/3] rust: revocable: implement Revocable::access()
Quoted 11 lines · On Mon, 28 Apr 2025 16:00:27 +0200 Danilo Krummrich <[email protected]> wrote:
Implement an unsafe direct accessor for the data stored within the Revocable.
This is useful for cases where we can prove that the data stored within the Revocable is not and cannot be revoked for the duration of the lifetime of the returned reference.
Reviewed-by Christian Schrefl <[email protected]>Reviewed-by Benno Lossin <[email protected]>Signed-off-by Danilo Krummrich <[email protected]>
Cheers,
Miguel
Danilo Krummrich
maintainerPATCH v2 2/3 rust: devres: implement Devres::access_with()
In reply to Danilo KrummrichImplement a direct accessor for the data stored within the Devres for cases where we can prove 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 prove 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.
--- rust/kernel/devres.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
rust/kernel/devres.rs +38 -0
@@ -181,6 +181,44 @@ 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. +/// +/// # Errors +/// +/// 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(dev.as_ref())?; +/// +/// let _ = bar.read32(0x0); +/// +/// // might_sleep() +/// +/// bar.write32(0x42, 0x0); +/// +/// Ok(()) +/// } +/// ``` +pub fn access <'a>(&'a self, dev: &'aDevice <Bound >) ->Result <&'aT > { +if self.0 .dev.as_raw () != dev.as_raw () { +return Err (EINVAL ); + } + +// SAFETY: `dev` being the same device as the device this `Devres` has been created for +// proves that `self.0.data` hasn't been revoked and is guaranteed to not be revoked as +// long as `dev` lives; `dev` lives at least as long as `self`. +Ok (unsafe { self.deref ().access () }) + } }impl <T >Deref for Devres <T > {
Signature · 1 line
2.49.0
Re: [PATCH v2 2/3] rust: devres: implement Devres::access_with()
rust/kernel/devres.rs pub fn new_foreign_owned(dev: &Device<Bound>, data: T, flags: Flags) -> Result { · line 200
Quoted 4 lines · Danilo Krummrich
rust/kernel/devres.rs +3
-0+/// # use kernel::{device::Core, devres::Devres, pci}; +/// +/// fn from_core(dev: &pci::Device<Core>, devres: Devres<pci::Bar<0x4>>) -> Result<()> {
We need to skip this one when !PCI -- quick patch at:
https://lore.kernel.org/rust-for-linux/[email protected]/
Cheers,
Miguel
Danilo Krummrich
maintainerPATCH v2 3/3 samples: rust: pci: take advantage of Devres::access_with()
In reply to Danilo KrummrichFor the I/O operations executed from the probe() method, take advantage of Devres::access_with(), avoiding the atomic check and RCU read lock required otherwise entirely.
--- samples/rust/rust_driver_pci.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
samples/rust/rust_driver_pci.rs +6 -6
@@ -83,12 +83,12 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self> GFP_KERNEL, )?; -let res = drvdata - .bar - .try_access_with (|b|Self ::testdev (info, b)) - .ok_or (ENXIO )??; - -dev_info! (pdev.as_ref (),"pci-testdev data-match count: {}\n" , res); +let bar = drvdata.bar.access (pdev.as_ref ())?; +dev_info! ( + pdev.as_ref (), +"pci-testdev data-match count: {}\n" , +Self ::testdev (info, bar)? + );Ok (drvdata.into ()) }
Signature · 1 line
2.49.0
Quoted 11 lines · Danilo Krummrich
This patch series implements a direct accessor for the data stored within a Devres container for cases where we can prove 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.
Quoted 9 lines
However, when we can prove 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.
You would need to, however, change the titles for patch #2 and #3 because there is no Devres::access_with() any more.
Regards,
Boqun
Quoted 28 lines
The patches of this series are also available in [1].
[1] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/devres
Changes in v2:
-Revocable::access(): remvoe explicit lifetimes; don't refer to 'a in the safety requirement
-Devres::access()
- renameDevres::access_with()toDevres::access()
- add missing '```' at the end of the example
- remove 's lifetime
- add # Errors sectionDanilo Krummrich (3):
rust: revocable: implementRevocable::access()
rust: devres: implementDevres::access_with()
samples: rust: pci: take advantage ofDevres::access_with()rust/kernel/devres.rs | 38 +++++++++++++++++++++++++++++++++ rust/kernel/revocable.rs | 12 +++++++++++ samples/rust/rust_driver_pci.rs | 12 +++++------ 3 files changed, 56 insertions(+), 6 deletions(-)base-commit:
3be746ebc1e6e32f499a65afe405df9030153a63Signature · 1 line
2.49.0
Danilo Krummrich
maintainerQuoted 18 lines · Boqun Feng
Quoted 17 lines · Danilo Krummrich
This patch series implements a direct accessor for the data stored within a Devres container for cases where we can prove 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 prove 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.
Quoted 8 lines
Quoted 3 lines
Therefore, in this case, we can bypass the atomic check and the RCU read lock, which is a great optimization and simplification for drivers.
Acked-by Boqun Feng <[email protected]>You would need to, however, change the titles for patch #2 and #3 because there is no
Devres::access_with()any more.
Thanks, good catch! Unless we need a v3 for something else, I'll fix it when I apply the series.
Quoted 11 lines · Danilo Krummrich
This patch series implements a direct accessor for the data stored within a Devres container for cases where we can prove 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.
Quoted 8 lines
However, when we can prove 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.
Thanks, this removes one of my pain points with the way revocable resources were accessed and will allow to write drivers in a much more natural way.
FWIW, the series
Joel Fernandes
Reviewed-byQuoted 25 lines · Alexandre Courbot
Quoted 11 lines · Danilo Krummrich
This patch series implements a direct accessor for the data stored within a Devres container for cases where we can prove 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.
Quoted 8 lines
However, when we can prove 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.
Quoted 7 lines
Thanks, this removes one of my pain points with the way revocable resources were accessed and will allow to write drivers in a much more natural way.
FWIW, the series
Reviewed-by Alexandre Courbot <[email protected]>
Nice, I like it. Quite a readability improvement too on the caller side!
Thanks.
Quoted 4 lines · Danilo Krummrich
This patch series implements a direct accessor for the data stored within a Devres container for cases where we can prove 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.
Applied to nova-next, thanks!