Re: [PATCH v3 1/1] rust: introduce abstractions for fwctl
From: Danilo Krummrich
Date: Tue Mar 03 2026 - 15:51:12 EST
On Tue Mar 3, 2026 at 9:15 PM CET, Jason Gunthorpe wrote:
> Anyhow is someone waiting for me to do something with it?
I still have this on my list for review, I can probably do a pass tomorrow. :)
>> +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.
We could add a helper with something like this:
const {
assert!(
core::mem::offset_of!(Self, dev) == 0,
"struct fwctl_device must be at offset 0"
)
};
But we could also just include this in this constructor.