From: Danilo Krummrich <dakr@kernel.org>
To: Benno Lossin <lossin@kernel.org>
Cc: gregkh@linuxfoundation.org, rafael@kernel.org, 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,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 4/4] rust: devres: implement register_foreign_release()
Date: Sun, 22 Jun 2025 14:46:57 +0200 [thread overview]
Message-ID: <aFf7QVhqE6dhp0m2@pollux> (raw)
In-Reply-To: <DASVTOJU2OE8.GU1NH3MZ3SX@kernel.org>
On Sun, Jun 22, 2025 at 09:26:33AM +0200, Benno Lossin wrote:
> On Thu Jun 12, 2025 at 4:51 PM CEST, Danilo Krummrich wrote:
> > +/// To be implemented by an object passed to [`register_foreign_release`].
>
> /// [`Devres`]-releaseable resource.
> ///
> /// Register an object implementing this trait with [`register_foreign_release`]. It's `release`
> /// function will be called once the device unbinds.
Sounds good, thanks!
> > +pub trait Release {
> > + /// Called once the [`Device`] given to [`register_foreign_release`] is unbound.
> > + fn release(&self);
>
> Would it make sense to also supply the `Device` that this is attached
> to? In case you have one object in multiple `register_foreign_release`
> calls with different devices, or is that something that doesn't happen?
No, doing that wouldn't make any sense. A resource should only be bound to the
lifetime of a single device.
> > +}
> > +
> > +impl<T: Release> Release for crate::sync::ArcBorrow<'_, T> {
> > + fn release(&self) {
> > + self.deref().release();
> > + }
> > +}
> > +
> > +impl<T: Release> Release for Pin<&'_ T> {
> > + fn release(&self) {
> > + self.deref().release();
> > + }
> > +}
>
> We should also implement it for `&T`, since that is `Box`'s `Borrowed`.
That should implicitly be the case when T: Release, where T is P<T>.
> > +
> > +/// Consume the `data`, [`Release::release`] and [`Drop::drop`] `data` once `dev` is unbound.
> > +///
> > +/// # Examples
> > +///
> > +/// ```no_run
> > +/// use kernel::{device::{Bound, Device}, devres, devres::Release, sync::Arc};
> > +///
> > +/// struct Registration<T> {
>
> Maybe add some explanation above/below this example. It looks like a new
> bus registration?
*class device registration, see Registration::new() below. But I can also add a
brief comment to the struct. It's indeed a bit subtly this way. :)
> > +/// data: T,
> > +/// }
> > +///
> > +/// impl<T> Registration<T> {
> > +/// fn new(data: T) -> Result<Arc<Self>> {
> > +/// // register (e.g. class device, IRQ, etc.)
> > +///
> > +/// Ok(Arc::new(Self { data }, GFP_KERNEL)?)
> > +/// }
> > +/// }
> > +///
> > +/// impl<T> Release for Registration<T> {
> > +/// fn release(&self) {
> > +/// // unregister
> > +/// }
> > +/// }
> > +///
> > +/// fn from_bound_context(dev: &Device<Bound>) -> Result {
> > +/// let reg = Registration::new(0x42)?;
> > +///
> > +/// devres::register_foreign_release(dev, reg.clone())
> > +/// }
> > +/// ```
> > +pub fn register_foreign_release<P>(dev: &Device<Bound>, data: P) -> Result
> > +where
> > + P: ForeignOwnable,
> > + for<'a> P::Borrowed<'a>: Release,
> > +{
> > + let ptr = data.into_foreign();
> > +
> > + #[allow(clippy::missing_safety_doc)]
> > + unsafe extern "C" fn callback<P>(ptr: *mut kernel::ffi::c_void)
> > + where
> > + P: ForeignOwnable,
> > + for<'a> P::Borrowed<'a>: Release,
> > + {
> > + // SAFETY: `ptr` is the pointer to the `ForeignOwnable` leaked above and hence valid.
> > + unsafe { P::borrow(ptr.cast()) }.release();
> > +
> > + // SAFETY: `ptr` is the pointer to the `ForeignOwnable` leaked above and hence valid.
> > + let _ = unsafe { P::from_foreign(ptr.cast()) };
> > + }
> > +
> > + // SAFETY:
> > + // - `dev.as_raw()` is a pointer to a valid and bound device.
> > + // - `ptr` is a valid pointer the `ForeignOwnable` devres takes ownership of.
> > + to_result(unsafe {
> > + // `devm_add_action_or_reset()` also calls `callback` on failure, such that the
> > + // `ForeignOwnable` is released eventually.
> > + bindings::devm_add_action_or_reset(dev.as_raw(), Some(callback::<P>), ptr.cast())
> > + })
> > +}
>
next prev parent reply other threads:[~2025-06-22 12:47 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-12 14:51 [PATCH 0/4] Improvements for Devres Danilo Krummrich
2025-06-12 14:51 ` [PATCH 1/4] rust: revocable: support fallible PinInit types Danilo Krummrich
2025-06-12 15:48 ` Benno Lossin
2025-06-12 15:58 ` Danilo Krummrich
2025-06-12 16:17 ` Benno Lossin
2025-06-12 16:20 ` Danilo Krummrich
2025-06-12 14:51 ` [PATCH 2/4] rust: devres: replace Devres::new_foreign_owned() Danilo Krummrich
2025-06-13 3:14 ` Viresh Kumar
2025-06-21 21:10 ` Benno Lossin
2025-06-21 21:45 ` Danilo Krummrich
2025-06-22 7:42 ` Benno Lossin
2025-06-22 9:55 ` Danilo Krummrich
2025-06-22 20:18 ` Benno Lossin
2025-06-12 14:51 ` [PATCH 3/4] rust: devres: get rid of Devres' inner Arc Danilo Krummrich
2025-06-22 7:05 ` Benno Lossin
2025-06-22 12:08 ` Danilo Krummrich
2025-06-22 20:16 ` Benno Lossin
2025-06-22 15:45 ` Danilo Krummrich
2025-06-22 20:15 ` Benno Lossin
2025-06-12 14:51 ` [PATCH 4/4] rust: devres: implement register_foreign_release() Danilo Krummrich
2025-06-22 7:26 ` Benno Lossin
2025-06-22 12:46 ` Danilo Krummrich [this message]
2025-06-22 20:14 ` Benno Lossin
2025-06-22 20:25 ` 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=aFf7QVhqE6dhp0m2@pollux \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--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=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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).