Re: [PATCH v2 4/4] rust: wmi: add Buffer and Device::invoke_method()

From: Nick Dowsett

Date: Sat Nov 28 2026 - 22:08:46 EST


Hi Armin,

>> +impl Deref for Buffer {
>> + type Target = [u8];
>> + #[inline]
>> + fn deref(&self) -> &[u8] {
>> + if self.0.length == 0 {
>> + return &[];
>> + }
>
> Hi,
>
> this might not be necessary, because an empty WMI buffer from the subsystem will
> never contain NULL for .data, instead said field will be set to ZERO_SIZE_PTR.

Good to know - thanks! Would you be open to me adding a doc comment in C
as part of this series to reflect this - or is it kind of implied already?
I'm not super experienced in C.

>> +impl Drop for Buffer {
>> + #[inline]
>> + fn drop(&mut self) {
>> + // SAFETY: By the type invariant, `self.0.data` is exclusively owned and
>> + // freeable with `kfree()`.
>> + unsafe { bindings::kfree(self.0.data) };
>> + }
>
> This is wrong for WMI buffers used as input for wmidev_invoke_method(), because the data
> might be statically allocated. I suggest that you keep struct wmi_buffer internal and
> instead return a KBox<[u8]> from invoke_method().
>
> IMHO struct wmi_buffer only exists because C does not define a standard slice type,
> so there is no reason for exposing struct wmi_buffer as a separate public rust type.
>
> Thanks,
> Armin Wolf

Note that the Rust implementation of `invoke_method()` actually takes a Rust
byte slice as input and converts it to a `struct wmi_buffer` internally,
so the `wmi::Buffer` type here was just to be used for the allocated return
type case.

It was my initial thinking to use KBox here, but some warnings from an LLM
steered me away from this. From my own research, for regular Rust Box types
it can be a bit of a safety minefield to convert from C types (see
https://doc.rust-lang.org/std/boxed/index.html#memory-layout) and I am
unsure if KBox is in the same boat. I also couldn't see anywhere else in the
kernel where we construct a KBox from a C type either. So my preference
for now is to keep this unless someone from R4L is able to advise otherwise.
If the naming is confusing because wmi_buffer is also used as input on C
side - would you prefer this to be named BufferOwned?

I agree that looked to be your intent so it would be very nice to return
KBox in future if possible! I was a bit surpised there isn't a generic C
slice type in the kernel btw and wmi has needed to define it itself.

Cheers Nick