[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <36f2c74d-9000-4351-bc23-f164cb0d6af6@nvidia.com>
Date: Mon, 1 Dec 2025 15:57:10 -0500
From: Joel Fernandes <joelagnelf@...dia.com>
To: John Hubbard <jhubbard@...dia.com>, linux-kernel@...r.kernel.org,
rust-for-linux@...r.kernel.org, dri-devel@...ts.freedesktop.org,
nouveau@...ts.freedesktop.org, Danilo Krummrich <dakr@...nel.org>,
Dave Airlie <airlied@...il.com>
Cc: Alexandre Courbot <acourbot@...dia.com>,
Alistair Popple <apopple@...dia.com>, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>, bjorn3_gh@...tonmail.com,
Benno Lossin <lossin@...nel.org>, Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
Simona Vetter <simona@...ll.ch>,
Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>,
Maxime Ripard <mripard@...nel.org>, Thomas Zimmermann <tzimmermann@...e.de>,
Timur Tabi <ttabi@...dia.com>, Joel Fernandes <joel@...lfernandes.org>,
Lyude Paul <elle@...thered-steel.dev>,
Daniel Almeida <daniel.almeida@...labora.com>,
Andrea Righi <arighi@...dia.com>, Philipp Stanner <phasta@...nel.org>
Subject: Re: [PATCH v3] rust: clist: Add support to interface with C linked
lists
On 12/1/2025 3:32 PM, Joel Fernandes wrote:
> On 11/30/2025 7:34 PM, John Hubbard wrote:
[...]
>>> +/// Refer to the examples in the [crate::clist] module documentation.
>>> +#[macro_export]
>>> +macro_rules! clist_create {
>>> + ($head:expr, $rust_type:ty, $c_type:ty, $field:ident) => {
>>> + $crate::clist::Clist::<$rust_type>::from_raw_and_offset::<
>>> + { ::core::mem::offset_of!($c_type, $field) },
>>> + >($head)
>>> + };
>>> +}
>>
>> Unlike the corresponding C container_of() macro, this one here has no
>> compile-time verification that the field is actually a list_head.
>>
>> How about this, to add that check:
>>
>> --- a/rust/kernel/clist.rs
>> +++ b/rust/kernel/clist.rs
>> macro_rules! clist_create {
>> - ($head:expr, $rust_type:ty, $c_type:ty, $field:ident) => {
>> - $crate::clist::Clist::<$rust_type>::from_raw_and_offset::<
>> + ($head:expr, $rust_type:ty, $c_type:ty, $field:ident) => {{
>> + // Compile-time check: $field must be a list_head.
>> + const _: () = {
>> + let _check: fn(*const $c_type) -> *const $crate::bindings::list_head =
>> + |p| unsafe { ::core::ptr::addr_of!((*p).$field) };
>> + };
>> + $crate::clist::Clist::<$rust_type, { ::core::mem::offset_of!($c_type, $field) }>::from_raw(
>> $head,
>> )
>> - };
>> + }};
>
> Sure I will play with your suggested snippet and add that, thanks.
I think it works also without the 'const _:' block:
let _: fn(*const $c_type) -> *const $crate::bindings::list_head =
|p| unsafe { ::core::ptr::addr_of!((*p).$field) };
I will roll that in, thanks John.
- Joel
Powered by blists - more mailing lists