[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <DDYZS5131FR7.282DNJW2DUOAH@kernel.org>
Date: Mon, 03 Nov 2025 11:36:32 +0100
From: "Danilo Krummrich" <dakr@...nel.org>
To: "Zhi Wang" <zhiw@...dia.com>
Cc: <rust-for-linux@...r.kernel.org>, <bhelgaas@...gle.com>,
<kwilczynski@...nel.org>, <ojeda@...nel.org>, <alex.gaynor@...il.com>,
<boqun.feng@...il.com>, <gary@...yguo.net>, <bjorn3_gh@...tonmail.com>,
<lossin@...nel.org>, <a.hindborg@...nel.org>, <aliceryhl@...gle.com>,
<tmgross@...ch.edu>, <linux-kernel@...r.kernel.org>, <cjia@...dia.com>,
<smitra@...dia.com>, <ankita@...dia.com>, <aniketa@...dia.com>,
<kwankhede@...dia.com>, <targupta@...dia.com>, <zhiwang@...nel.org>,
<alwilliamson@...dia.com>, <acourbot@...dia.com>, <joelagnelf@...dia.com>,
<jhubbard@...dia.com>, <jgg@...dia.com>
Subject: Re: [RFC 1/2] rust: introduce abstractions for fwctl
On Mon Nov 3, 2025 at 10:55 AM CET, Zhi Wang wrote:
> On Sun, 02 Nov 2025 19:33:10 +0100
> "Danilo Krummrich" <dakr@...nel.org> 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.
>>
>
> FwCtlUCtx is defined as below:
>
> +#[repr(C)]
> +#[pin_data]
> +pub struct FwCtlUCtx<T> {
> + /// The core fwctl user context shared with the C implementation.
> + #[pin]
> + pub fwctl_uctx: bindings::fwctl_uctx,
> + /// Driver-specific data associated with this user context.
> + pub uctx: T,
> +}
>
> I assume it should be equal to C structure as below and with #[repr(C)],
> the handling of layout and the alignment should be as the same as C
> structure.
>
> struct FwCtlUCtx {
> struct fwctl_uctx base;
> struct my_driver_data data;
> };
>
> uctx_size: core::mem::size_of::<FwCtlUCtx<T::UCtx>>() should be:
That's indeed correct then, I think I read uctx_size as the size of T::UCtx
only. (I've recently come across subsystems that do exacly that and hence run
into the problem in [2].)
Anyways, I suggest to not give out a FwCtlUCtx<T> to the driver at all, since in
open() we can't (for the discussed reasons) and in the other callbacks it
doesn't seem very useful either.
Instead, I think we should have the following callback arguments:
impl fwctl::Operations for MyDriverContext {
fn open(
fdev: &fwctl::Device,
parent: &Device<Bound>
) -> impl PinInit<Self, Error>;
fn close(
this: Pin<&mut Self>,
fdev: &fwctl::Device,
parent: &Device<Bound>
) -> impl PinInit<Self, Error>;
}
with
#[repr(transparent)]
struct Device(Opaque<bindings::fwctl_device);
Note this also gets us rid of the Self::UCtx type alias, which seems
unnecessary.
You could still keep a FwCtlUCtx<T> type internally since it might make the
implementation easier.
[2] https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=806c1a9a53a174ef39acff8ae5bb0e68
Powered by blists - more mailing lists