Re: [RFC 1/2] rust: introduce abstractions for fwctl

Jason Gunthorpe
2025-11-02T22:55:25Z

On Sun, Nov 02, 2025 at 07:33:10PM +0100, Danilo Krummrich wrote:
> On Thu Oct 30, 2025 at 5:03 PM CET, Zhi Wang wrote:
> > +/// Static vtable mapping Rust trait methods to C callbacks.
> > +pub struct FwCtlVTable<T: FwCtlOps>(PhantomData<T>);
> > +
> > +impl<T: FwCtlOps> FwCtlVTable<T> {
> > + /// Static instance of `fwctl_ops` used by the C core to call into Rust.
> > + pub const VTABLE: bindings::fwctl_ops = bindings::fwctl_ops {
> > + device_type: T::DEVICE_TYPE,
> > + uctx_size: core::mem::size_of::<FwCtlUCtx<T::UCtx>>(),
>
> The fwctl code uses this size to allocate memory for both, struct fwctl_uctx and
> the driver's private data at the end of the allocation.
>
> This means that it is not enough to just consider the size of T::UCtx, you also
> have to consider its required alignment, and, if required, allocate more memory.

Yes, the container_of() relationship must be such that the core struct
is first in the memory and any driver data is trailing. The C version
has a static_assertion to constrain this.

> All other callbacks should be correct as they are once the alignment is
> considered.

Yes, only alloc and put/free make this assumption.

Jason