Re: [PATCH v3 1/1] rust: introduce abstractions for fwctl
From: Jason Gunthorpe
Date: Tue Mar 03 2026 - 15:16:58 EST
On Tue, Feb 17, 2026 at 10:49:06PM +0200, Zhi Wang wrote:
> Introduce safe wrappers around `struct fwctl_device` and
> `struct fwctl_uctx`, allowing rust drivers to register fwctl devices and
> implement their control and RPC logic in safe rust.
>
> Cc: Danilo Krummrich <dakr@xxxxxxxxxx>
> Cc: Gary Guo <gary@xxxxxxxxxxx>
> Cc: Jason Gunthorpe <jgg@xxxxxxxxxx>
> Cc: Joel Fernandes <joelagnelf@xxxxxxxxxx>
> Signed-off-by: Zhi Wang <zhiw@xxxxxxxxxx>
> ---
> drivers/fwctl/Kconfig | 12 +
> rust/bindings/bindings_helper.h | 1 +
> rust/helpers/fwctl.c | 17 ++
> rust/helpers/helpers.c | 3 +-
> rust/kernel/fwctl.rs | 449 ++++++++++++++++++++++++++++++++
The binding is larger than the subystem file:
$ wc -l drivers/fwctl/main.c
421 drivers/fwctl/main.c
:\
Anyhow is someone waiting for me to do something with it?
> +impl<T: Operations> Device<T> {
> + /// Allocate a new fwctl device with embedded driver data.
> + ///
> + /// Returns an [`ARef`] that can be passed to [`Registration::new()`]
> + /// to make the device visible to userspace. The caller may inspect or
> + /// configure the device between allocation and registration.
> + pub fn new(
> + parent: &device::Device<device::Bound>,
> + data: impl PinInit<T::DeviceData, Error>,
> + ) -> Result<ARef<Self>> {
> + let ops = core::ptr::from_ref::<bindings::fwctl_ops>(&VTable::<T>::VTABLE).cast_mut();
> +
> + // SAFETY: `_fwctl_alloc_device` allocates `size` bytes via kzalloc and
> + // initialises the embedded fwctl_device. `ops` points to a static vtable
> + // that outlives the device. `parent` is bound.
> + let raw = unsafe {
> + bindings::_fwctl_alloc_device(parent.as_raw(), ops, core::mem::size_of::<Self>())
> + };
> +
> + if raw.is_null() {
> + return Err(ENOMEM);
> + }
> +
> + // CAST: Device<T> is repr(C) with fwctl_device at offset 0.
> + let this = raw as *mut Self;
Should this have some helper? It looks a bit fragile, in C we have a
static_assert on offsetof to prevent errors.
Jason