From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B37051D88AC for ; Wed, 18 Mar 2026 12:40:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773837655; cv=none; b=L4FMz1+z0IDZGEtfS0/YIwg3jQvRpmG5CYUUguoh1zUo5e5Tn8XJ9OR3cJ27cuReS40qN6GaTX4CIX+M3WQwKU0T2RrDjchOevDeIANTSXVwoo68F5Wz7nxd2G0qcVol1Kxr1SL6KUTs68g8aODv3EMYICAWpsDj+WI7VK7k0DI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773837655; c=relaxed/simple; bh=j0Vq7i3vktXeaCWe+C2NzpUZZ6xObpycugCND5M/L60=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aIM2pzpS1lLHaemThOY/A0XOAjCmWTriNYyYg0Rw+EbUmK8Rjk49cSrwI1wft4Bv8iKTzL3+tDhhlHHto0cHZFZjEfgP9aIv7ZyODno/zWDqhCtP4PryKwJ0WgrOJ98luMZzhLW+GC1xtiz70JrDj7n5BdfX5WVWkP49k8TZP04= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=e3rio40p; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="e3rio40p" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-43b3a0d25d8so2641798f8f.3 for ; Wed, 18 Mar 2026 05:40:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773837651; x=1774442451; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=nWiNwiZcBmYWVTTl9iPT0XLfb7VjL0dMPy7l1+Lqx3k=; b=e3rio40ptIgc8aCiCTt90OzwTJeMTzR0qA5AzcgAZIPACXZYQbs6MpQrcpxXYTvVTg d99jTrE/dd5UbItVwBw+eFPYnDZ28xifnkJrtjgj4c10fi518Qi7fPwnTAJTWWsy1pZ+ +820Vm8puTj7d6xmrWvPFyy9oIVtOKsrrLkSxEQZFh8VUM8Y119ldGvR9GC8hwb6KcMV ZQWYGXU3qnziTc1AqncbflGjNSJl7rz6fsoHvKWDnHtN3jSAU0pZ68oU20juVsjGmJbw LGx/D0YKOCHe/Ng2g72HahhGKj6PEZjVOM9SJE3lp6tR3z6AyyHKkBAfH8+XUyY4wgVF qFbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773837651; x=1774442451; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nWiNwiZcBmYWVTTl9iPT0XLfb7VjL0dMPy7l1+Lqx3k=; b=FD2Nr501B5q/MbIBmn/oV+ghgrTpjA99DTI1oGsuMVTH7OYFGJiEUwvrzQK0KqXIoq a2PEi2PRimSm5EkfZ1gZmOcDvEhF9fgeov0mImPwiXzXPrzOMvQHMoFrI5AbiIVJnuFj MNDj2/tScT1dl7OJ04jw9nN7Zg0kHzFDnaD7Zt6Xk+z7Y4y+4F5bS7psJ4eGCbADDhQk wz9R/KpHtuyOU+vfOoKSwvi6Q7NHpMP0BR2YY7dZxtCuBzv0wm1+ngWGEOjzYfJwF56U CyqKksV8VgnbAEaK8i/WKXAuQVPRkKfyqXpg0PjxFfo4j32Ntu6gbxtOObrIbAgk6b6/ yzRg== X-Gm-Message-State: AOJu0YzWyjEJ0oq4mFOcEW5mpLbyK4lCpJOBCX1bi0XEJvgpMGiT9mMt O1aPCyOAcnUW8sr3GDnMvYbLG5kz4iZs3te5PsbBWaUPyabTyWRjCKpqQcrvlmFcaiOws+dCB7z /mWBTyiyOYEJDGQvmTA== X-Received: from wmnp7.prod.google.com ([2002:a05:600c:2e87:b0:485:6c28:2360]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5491:b0:485:3983:aba2 with SMTP id 5b1f17b1804b1-486f4451354mr52065225e9.23.1773837650627; Wed, 18 Mar 2026 05:40:50 -0700 (PDT) Date: Wed, 18 Mar 2026 12:40:48 +0000 In-Reply-To: <20260317201710.934932-2-joelagnelf@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260317201710.934932-1-joelagnelf@nvidia.com> <20260317201710.934932-2-joelagnelf@nvidia.com> Message-ID: Subject: Re: [PATCH v13 1/1] rust: interop: Add list module for C linked list interface From: Alice Ryhl To: Joel Fernandes Cc: linux-kernel@vger.kernel.org, Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Andreas Hindborg , Trevor Gross , Alex Gaynor , Danilo Krummrich , Dave Airlie , David Airlie , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Simona Vetter , Daniel Almeida , Koen Koning , Nikola Djukic , Alexandre Courbot , Philipp Stanner , Elle Rhumsaa , Jonathan Corbet , Alex Deucher , "Christian =?utf-8?B?S8O2bmln?=" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , Huang Rui , Matthew Auld , Matthew Brost , Lucas De Marchi , "Thomas =?utf-8?Q?Hellstr=C3=B6m?=" , Helge Deller , John Hubbard , Alistair Popple , Timur Tabi , Edwin Peer , Andrea Righi , Andy Ritger , Zhi Wang , Balbir Singh , alexeyi@nvidia.com, Eliot Courtney , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, linux-doc@vger.kernel.org, amd-gfx@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, intel-xe@lists.freedesktop.org, linux-fbdev@vger.kernel.org Content-Type: text/plain; charset="utf-8" On Tue, Mar 17, 2026 at 04:17:10PM -0400, Joel Fernandes wrote: > Add a new module `kernel::interop::list` for working with C's doubly > circular linked lists. Provide low-level iteration over list nodes. > > Typed iteration over actual items is provided with a `clist_create` > macro to assist in creation of the `CList` type. > > Cc: Nikola Djukic > Reviewed-by: Daniel Almeida > Reviewed-by: Alexandre Courbot > Acked-by: Alexandre Courbot > Acked-by: Gary Guo > Acked-by: Miguel Ojeda > Signed-off-by: Joel Fernandes > --- > MAINTAINERS | 8 + > rust/helpers/helpers.c | 1 + > rust/helpers/list.c | 17 ++ > rust/kernel/interop.rs | 9 + > rust/kernel/interop/list.rs | 342 ++++++++++++++++++++++++++++++++++++ > rust/kernel/lib.rs | 2 + > 6 files changed, 379 insertions(+) > create mode 100644 rust/helpers/list.c > create mode 100644 rust/kernel/interop.rs > create mode 100644 rust/kernel/interop/list.rs > > diff --git a/MAINTAINERS b/MAINTAINERS > index 4bd6b538a51f..e847099efcc2 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -23251,6 +23251,14 @@ T: git https://github.com/Rust-for-Linux/linux.git alloc-next > F: rust/kernel/alloc.rs > F: rust/kernel/alloc/ > > +RUST [INTEROP] > +M: Joel Fernandes > +M: Alexandre Courbot > +L: rust-for-linux@vger.kernel.org > +S: Maintained > +T: git https://github.com/Rust-for-Linux/linux.git interop-next > +F: rust/kernel/interop/ > + > RUST [NUM] > M: Alexandre Courbot > R: Yury Norov > diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c > index a3c42e51f00a..724fcb8240ac 100644 > --- a/rust/helpers/helpers.c > +++ b/rust/helpers/helpers.c > @@ -35,6 +35,7 @@ > #include "io.c" > #include "jump_label.c" > #include "kunit.c" > +#include "list.c" > #include "maple_tree.c" > #include "mm.c" > #include "mutex.c" > diff --git a/rust/helpers/list.c b/rust/helpers/list.c > new file mode 100644 > index 000000000000..18095a5593c5 > --- /dev/null > +++ b/rust/helpers/list.c > @@ -0,0 +1,17 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +/* > + * Helpers for C circular doubly linked list implementation. > + */ > + > +#include > + > +__rust_helper void rust_helper_INIT_LIST_HEAD(struct list_head *list) > +{ > + INIT_LIST_HEAD(list); > +} > + > +__rust_helper void rust_helper_list_add_tail(struct list_head *new, struct list_head *head) > +{ > + list_add_tail(new, head); > +} > diff --git a/rust/kernel/interop.rs b/rust/kernel/interop.rs > new file mode 100644 > index 000000000000..b88140cf76dc > --- /dev/null > +++ b/rust/kernel/interop.rs > @@ -0,0 +1,9 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Infrastructure for interfacing Rust code with C kernel subsystems. > +//! > +//! This module is intended for low-level, unsafe Rust infrastructure code > +//! that interoperates between Rust and C. It is NOT for use directly in > +//! Rust drivers. > + > +pub mod list; > diff --git a/rust/kernel/interop/list.rs b/rust/kernel/interop/list.rs > new file mode 100644 > index 000000000000..328f6b0de2ce > --- /dev/null > +++ b/rust/kernel/interop/list.rs > @@ -0,0 +1,342 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Rust interface for C doubly circular intrusive linked lists. > +//! > +//! This module provides Rust abstractions for iterating over C `list_head`-based > +//! linked lists. It should only be used for cases where C and Rust code share > +//! direct access to the same linked list through a C interop interface. > +//! > +//! Note: This *must not* be used by Rust components that just need a linked list > +//! primitive. Use [`kernel::list::List`] instead. > +//! > +//! # Examples > +//! > +//! ``` > +//! use kernel::{ > +//! bindings, > +//! clist_create, > +//! types::Opaque, > +//! }; > +//! # // Create test list with values (0, 10, 20) - normally done by C code but it is > +//! # // emulated here for doctests using the C bindings. > +//! # use core::mem::MaybeUninit; > +//! # > +//! # /// C struct with embedded `list_head` (typically will be allocated by C code). > +//! # #[repr(C)] > +//! # pub struct SampleItemC { > +//! # pub value: i32, > +//! # pub link: bindings::list_head, > +//! # } > +//! # > +//! # let mut head = MaybeUninit::::uninit(); > +//! # > +//! # let head = head.as_mut_ptr(); > +//! # // SAFETY: `head` and all the items are test objects allocated in this scope. > +//! # unsafe { bindings::INIT_LIST_HEAD(head) }; > +//! # > +//! # let mut items = [ > +//! # MaybeUninit::::uninit(), > +//! # MaybeUninit::::uninit(), > +//! # MaybeUninit::::uninit(), > +//! # ]; > +//! # > +//! # for (i, item) in items.iter_mut().enumerate() { > +//! # let ptr = item.as_mut_ptr(); > +//! # // SAFETY: `ptr` points to a valid `MaybeUninit`. > +//! # unsafe { (*ptr).value = i as i32 * 10 }; > +//! # // SAFETY: `&raw mut` creates a pointer valid for `INIT_LIST_HEAD`. > +//! # unsafe { bindings::INIT_LIST_HEAD(&raw mut (*ptr).link) }; > +//! # // SAFETY: `link` was just initialized and `head` is a valid list head. > +//! # unsafe { bindings::list_add_tail(&mut (*ptr).link, head) }; > +//! # } > +//! > +//! // > +//! /// Rust wrapper for the C struct. > +//! /// > +//! /// The list item struct in this example is defined in C code as: > +//! /// > +//! /// ```c > +//! /// struct SampleItemC { > +//! /// int value; > +//! /// struct list_head link; > +//! /// }; > +//! /// ``` > +//! #[repr(transparent)] > +//! pub struct Item(Opaque); > +//! > +//! impl Item { > +//! pub fn value(&self) -> i32 { > +//! // SAFETY: `Item` has same layout as `SampleItemC`. > +//! unsafe { (*self.0.get()).value } > +//! } > +//! } > +//! > +//! > +//! // Create typed [`CList`] from sentinel head. > +//! // SAFETY: `head` is valid and initialized, items are `SampleItemC` with > +//! // embedded `link` field, and `Item` is `#[repr(transparent)]` over `SampleItemC`. > +//! let list = clist_create!(unsafe { head, Item, SampleItemC, link }); Bad news. My build triggers this warning: error: statement has unnecessary safety comment --> rust/doctests_kernel_generated.rs:7103:1 | 7103 | let list = clist_create!(unsafe { head, Item, SampleItemC, link }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: consider removing the safety comment --> rust/doctests_kernel_generated.rs:7101:4 | 7101 | // SAFETY: `head` is valid and initialized, items are `SampleItemC` with | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.94.0/index.html#unnecessary_safety_comment = note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::unnecessary_safety_comment)]` This probably needs to be: unsafe { clist_create!(head, Item, SampleItemC, link) } Alice