On 11/25/2026 9:57 AM, Danilo Krummrich wrote:
> On Thu Nov 19, 2026 at 10:50 PM CET, Simon Song wrote:
>> +/// Maps a C device structure to its embedded VFIO device.
>> +///
>> +/// # Safety
>> +///
>> +/// Implementations must return the address of the `vfio_device` at the 
>> beginning of `Self`,
> 
> I think "at the beginning of `Self`" is not a safety requirement for this 
> trait,
> it's an implementation detail of the current implementation on the C side, 
> that
> could be a type invariant of DeviceBase.
> 
> Besides that, I think the abstraction we currently need with DeviceBase and
> RawDevice (which I proposed to deal with the current implementation) could be
> simplified if the C code would be more flexible regarding the layout of a 
> struct
> subclassing a struct vfio_device.
> 
> However, this could be a subsequent consideration and nothing that needs to be
> tackled now; it is mostly an implementation detail of this abstraction.

Agreed, we will leave the C side change as is for this series.

>> +#[expect(dead_code)]
>> +impl<Raw: RawDevice, R, O, T, Ctx: DeviceContext> DeviceBase<Raw, R, O, T, 
>> Ctx> {
>> +    /// Initializes the Rust fields of a newly allocated device.
>> +    ///
>> +    /// # Safety
>> +    ///
>> +    /// `this` must point to an allocation with the size and alignment of 
>> `Self`, whose `raw`
>> +    /// has been initialized. Nothing else may access the allocation during 
>> this call.
>> +    unsafe fn init(this: *mut Self) {
>> +        // SAFETY: The caller provides exclusive access to a suitable 
>> allocation.
>> +        unsafe {
>> +            (&raw mut (*this).data).write(CallbackData {
>> +                reg_data: UnsafeCell::new(None),
>> +                open_data: UnsafeCell::new(core::ptr::null_mut()),
>> +            });
>> +            (&raw mut (*this)._context).write(PhantomData);
>> +        }
>> +    }
>> +
>> +    /// Recovers `&Self` from the `vfio_device` VFIO passes to a callback.
>> +    ///
>> +    /// # Safety
>> +    ///
>> +    /// `vdev` must point to the `vfio_device` at the beginning of a `Self` 
>> that remains alive
>> +    /// for `'a`. The caller must provide the guarantees of `Ctx` 
>> throughout the borrow.
>> +    unsafe fn from_raw_vfio<'a>(vdev: *mut bindings::vfio_device) -> &'a 
>> Self {
>> +        // SAFETY: `Raw` begins with its `vfio_device` and is the first 
>> field of `Self`. The
>> +        // caller guarantees validity and the context.
>> +        unsafe { &*vdev.cast() }
> 
> If we rely on this layout guarantee here, there's no point to have the 
> RawDevice
> trait dealing with the layout.
> 
> Either we add from_raw_vfio() to RawDevice, or we just remove RawDevice and 
> make
> the layout guarantee a type invariant.
> 
> Either is fine with me, but maybe the RawDevice trait is a bit overengineered
> and a type invaraint is sufficient.

I will remove RawDevice, then DeviceBase gets the Raw which is the C struct 
that always begins
with its struct vfio_device. 

Then as_raw_vfio() function can use the same .cast() like from_raw_vfio()

Reply via email to