From: Alice Ryhl <aliceryhl@google.com>
To: Danilo Krummrich <dakr@kernel.org>
Cc: gregkh@linuxfoundation.org, rafael@kernel.org,
david.m.ertman@intel.com, ira.weiny@intel.com, 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, tmgross@umich.edu, airlied@gmail.com,
acourbot@nvidia.com, jhubbard@nvidia.com,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v4 4/5] rust: auxiliary: add auxiliary registration
Date: Tue, 15 Apr 2025 12:11:16 +0000 [thread overview]
Message-ID: <Z_5M5Auqj2KK-rPz@google.com> (raw)
In-Reply-To: <20250414131934.28418-5-dakr@kernel.org>
On Mon, Apr 14, 2025 at 03:18:07PM +0200, Danilo Krummrich wrote:
> Implement the `auxiliary::Registration` type, which provides an API to
> create and register new auxiliary devices in the system.
>
> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
> rust/kernel/auxiliary.rs | 88 +++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 87 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs
> index 75423737032a..b40d663b42eb 100644
> --- a/rust/kernel/auxiliary.rs
> +++ b/rust/kernel/auxiliary.rs
> @@ -5,7 +5,7 @@
> //! C header: [`include/linux/auxiliary_bus.h`](srctree/include/linux/auxiliary_bus.h)
>
> use crate::{
> - bindings, device,
> + bindings, container_of, device,
> device_id::RawDeviceId,
> driver,
> error::{to_result, Result},
> @@ -230,6 +230,18 @@ pub fn parent(&self) -> Option<&device::Device> {
> }
> }
>
> +impl Device {
> + extern "C" fn release(dev: *mut bindings::device) {
> + // SAFETY: By the type invariant `self.0.as_raw` is a pointer to the `struct device`
> + // embedded in `struct auxiliary_device`.
> + let adev = unsafe { container_of!(dev, bindings::auxiliary_device, dev) }.cast_mut();
FYI, Tamir's patch makes this cast_mut() unnecessary.
> + // SAFETY: `adev` points to the memory that has been allocated in `Registration::new`, via
> + // `KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)`.
> + let _ = unsafe { KBox::<Opaque<bindings::auxiliary_device>>::from_raw(adev.cast()) };
> + }
> +}
> +
> // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
> // argument.
> kernel::impl_device_context_deref!(unsafe { Device });
> @@ -272,3 +284,77 @@ unsafe impl Send for Device {}
> // SAFETY: `Device` can be shared among threads because all methods of `Device`
> // (i.e. `Device<Normal>) are thread safe.
> unsafe impl Sync for Device {}
> +
> +/// The registration of an auxiliary device.
> +///
> +/// This type represents the registration of a [`struct auxiliary_device`]. When an instance of this
> +/// type is dropped, its respective auxiliary device will be unregistered from the system.
> +///
> +/// # Invariants
> +///
> +/// `self.0` always holds a valid pointer to an initialized and registered
> +/// [`struct auxiliary_device`].
> +pub struct Registration(NonNull<bindings::auxiliary_device>);
> +
> +impl Registration {
> + /// Create and register a new auxiliary device.
> + pub fn new(parent: &device::Device, name: &CStr, id: u32, modname: &CStr) -> Result<Self> {
> + let boxed = KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)?;
> + let adev = boxed.get();
> +
> + // SAFETY: It's safe to set the fields of `struct auxiliary_device` on initialization.
> + unsafe {
> + (*adev).dev.parent = parent.as_raw();
> + (*adev).dev.release = Some(Device::release);
> + (*adev).name = name.as_char_ptr();
> + (*adev).id = id;
> + }
> +
> + // SAFETY: `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`,
> + // which has not been initialized yet.
> + unsafe { bindings::auxiliary_device_init(adev) };
> +
> + // Now that `adev` is initialized, leak the `Box`; the corresponding memory will be freed
> + // by `Device::release` when the last reference to the `struct auxiliary_device` is dropped.
> + let _ = KBox::into_raw(boxed);
> +
> + // SAFETY:
> + // - `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`, which has
> + // been initialialized,
> + // - `modname.as_char_ptr()` is a NULL terminated string.
> + let ret = unsafe { bindings::__auxiliary_device_add(adev, modname.as_char_ptr()) };
> + if ret != 0 {
> + // SAFETY: `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`,
> + // which has been initialialized.
> + unsafe { bindings::auxiliary_device_uninit(adev) };
Does this error-path actually free the box?
Alice
> + return Err(Error::from_errno(ret));
> + }
> +
> + // SAFETY: `adev` is guaranteed to be non-null, since the `KBox` was allocated successfully.
> + //
> + // INVARIANT: The device will remain registered until `auxiliary_device_delete()` is called,
> + // which happens in `Self::drop()`.
> + Ok(Self(unsafe { NonNull::new_unchecked(adev) }))
> + }
> +}
> +
> +impl Drop for Registration {
> + fn drop(&mut self) {
> + // SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered
> + // `struct auxiliary_device`.
> + unsafe { bindings::auxiliary_device_delete(self.0.as_ptr()) };
> +
> + // This drops the reference we acquired through `auxiliary_device_init()`.
> + //
> + // SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered
> + // `struct auxiliary_device`.
> + unsafe { bindings::auxiliary_device_uninit(self.0.as_ptr()) };
> + }
> +}
> +
> +// SAFETY: A `Registration` of a `struct auxiliary_device` can be released from any thread.
> +unsafe impl Send for Registration {}
> +
> +// SAFETY: `Registration` does not expose any methods or fields that need synchronization.
> +unsafe impl Sync for Registration {}
> --
> 2.49.0
>
next prev parent reply other threads:[~2025-04-15 12:11 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-14 13:18 [PATCH v4 0/5] Auxiliary bus Rust abstractions Danilo Krummrich
2025-04-14 13:18 ` [PATCH v4 1/5] rust: types: add `Opaque::zeroed` Danilo Krummrich
2025-04-15 11:59 ` Alice Ryhl
2025-04-15 12:43 ` Danilo Krummrich
2025-04-15 15:17 ` Miguel Ojeda
2025-04-14 13:18 ` [PATCH v4 2/5] rust: device: implement Device::parent() Danilo Krummrich
2025-04-14 13:18 ` [PATCH v4 3/5] rust: auxiliary: add auxiliary device / driver abstractions Danilo Krummrich
2025-04-15 12:08 ` Alice Ryhl
2025-04-15 12:45 ` Danilo Krummrich
2025-04-14 13:18 ` [PATCH v4 4/5] rust: auxiliary: add auxiliary registration Danilo Krummrich
2025-04-15 12:11 ` Alice Ryhl [this message]
2025-04-15 12:50 ` Danilo Krummrich
2025-04-16 10:38 ` Alice Ryhl
2025-04-14 13:18 ` [PATCH v4 5/5] samples: rust: add Rust auxiliary driver sample Danilo Krummrich
2025-04-15 11:38 ` [PATCH v4 0/5] Auxiliary bus Rust abstractions Greg KH
2025-04-15 12:42 ` Danilo Krummrich
2025-04-15 12:54 ` Greg KH
2025-04-19 13:01 ` 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=Z_5M5Auqj2KK-rPz@google.com \
--to=aliceryhl@google.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=david.m.ertman@intel.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=ira.weiny@intel.com \
--cc=jhubbard@nvidia.com \
--cc=linux-kernel@vger.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).