mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Armin Wolf <W_Armin@gmx.de>
To: "Nick Dowsett" <nick42d@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	platform-driver-x86@vger.kernel.org
Subject: Re: [PATCH v2 4/4] rust: wmi: add Buffer and Device::invoke_method()
Date: Fri, 4 Dec 2026 21:44:01 +0100	[thread overview]
Message-ID: <0ea7f228-9668-46e4-93b3-30b895cf08e9@gmx.de> (raw)
In-Reply-To: <DN7D44I6DC6B.2NFTVC7KACBSD@gmail.com>

Am 29.11.26 um 04:08 schrieb Nick Dowsett:
> 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.

Hi,

of course you can add a doc comment to document this, i didnt do it at 
the start because i initial did not consider this a important detail.

>>> +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
> 

You are right, calling the type "Buffer" is fine. I agree that the R4L 
guys should decide whether or not KBox is suitable here.

Thanks,
Armin Wolf


      reply	other threads:[~2026-12-04 20:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 11:02 [PATCH v2 0/4] rust: wmi: add ACPI WMI bus abstractions Nick Dowsett via B4 Relay
2026-11-27 11:02 ` [PATCH v2 1/4] rust: wmi: add basic WMI device abstraction Nick Dowsett via B4 Relay
2026-11-27 11:02 ` [PATCH v2 2/4] rust: wmi: add DeviceId and wmi_device_table! macro Nick Dowsett via B4 Relay
2026-11-27 11:02 ` [PATCH v2 3/4] rust: wmi: add Driver trait, Adapter and implement RegistrationOps Nick Dowsett via B4 Relay
2026-11-28 15:57   ` Armin Wolf
2026-11-29  1:32     ` Nick Dowsett
2026-12-04 20:41       ` Armin Wolf
2026-11-27 11:02 ` [PATCH v2 4/4] rust: wmi: add Buffer and Device::invoke_method() Nick Dowsett via B4 Relay
2026-11-28 16:05   ` Armin Wolf
2026-11-29  3:08     ` Nick Dowsett
2026-12-04 20:44       ` Armin Wolf [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0ea7f228-9668-46e4-93b3-30b895cf08e9@gmx.de \
    --to=w_armin@gmx.de \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nick42d@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®