All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joel Fernandes <joelagnelf@nvidia.com>
To: Danilo Krummrich <dakr@kernel.org>
Cc: linux-kernel@vger.kernel.org, "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>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Dave Airlie" <airlied@redhat.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Koen Koning" <koen.koning@linux.intel.com>,
	dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org,
	rust-for-linux@vger.kernel.org,
	"Nikola Djukic" <ndjukic@nvidia.com>
Subject: Re: [PATCH v10 5/8] rust: clist: Add support to interface with C linked lists
Date: Thu, 19 Feb 2026 10:27:36 -0500	[thread overview]
Message-ID: <eadfa4662f403fa01f19c1c17a435c1a@nvidia.com> (raw)
In-Reply-To: <DGIWDQTR76Y5.34L9IHKU2SEKI@kernel.org>

On Thu, Feb 19, 2026 at 12:21:56PM +0100, Danilo Krummrich wrote:
> On Wed Feb 18, 2026 at 9:55 PM CET, Joel Fernandes wrote:
> > +RUST TO C LIST INTERFACES
>
> Maybe this should just be "RUST [FFI]" instead (in case Alex and you want to
> sign up for looking after FFI helper infrastructure in general)?

Good idea, done.

> > +F:	rust/kernel/ffi/clist.rs
>
> <snip>
>
> > +//! This module provides Rust abstractions for iterating over C `list_head`-based
> > +//! linked lists. It is intended for FFI use-cases where a C subsystem manages a
> > +//! circular linked list that Rust code needs to read. This is generally required
> > +//! only for special cases and should be avoided by drivers.
>
> Maybe generalize the statement a bit and say that this should only be used for
> cases where C and Rust code share direct access to the same linked list through
> an FFI interface.
>
> Additionally, add a separate note that this *must not* be used by Rust
> components that just aim for a linked list primitive and instead refer to the
> Rust linked list implementation with an intra-doc link.

Done. Updated the module doc to say "It should only be used for cases
where C and Rust code share direct access to the same linked list
through an FFI interface" and added a separate note:

  Note: This *must not* be used by Rust components that just need a
  linked list primitive. Use [`kernel::list::List`] instead.

> > +//!     types::Opaque, //
>
> Examples don't necessarily need '//' at the end, as they are not automatically
> formatted anyways.

Removed from the example. Non-example imports keep the '//' as a
rustfmt guard.

> > +//! #     // SAFETY: pointers are to allocated test objects with a list_head field.
> > +//! #     unsafe {
>
> I understand that this is just setup code for a doc-test, but I still think we
> should hold it to the same standards, i.e. let's separate the different unsafe
> calls into their own unsafe blocks and add proper safety comments.

Done. Split into three separate unsafe blocks with individual SAFETY
comments for the value write, INIT_LIST_HEAD, and list_add_tail calls.

> > +    PinInit //
>
> Should be 'PinInit, //'.

Fixed.

> > +/// - [`CListHead`] represents an allocated and valid `list_head` structure.
>
> What does "allocated" mean in this context? (Dynamic allocations, stack, .data
> section of the binary, any of those?)
>
> In case of the latter, I'd just remove "allocated".

Removed "allocated". The invariant now reads:

  The underlying `list_head` has been initialized (e.g. via
  `INIT_LIST_HEAD()`) and its `next`/`prev` pointers are valid and
  non-NULL.

> > +    /// - `ptr` must be a valid pointer to an allocated and initialized `list_head` structure.
>
> Same here, what exactly is meant by "allocated"?

Removed "allocated" from from_raw() safety docs as well. Updated to:

  `ptr` must be a valid pointer to an initialized `list_head` (e.g.
  via `INIT_LIST_HEAD()`), with valid non-NULL `next`/`prev` pointers.

> > +    /// - The list and all linked `list_head` nodes must not be modified by non-Rust code
> > +    ///   for the lifetime `'a`.
>
> This is a bit vague I think, concurrent modifications of (other) Rust code are
> not OK either.

Fixed. Changed to "must not be concurrently modified for the lifetime
`'a`" which covers both Rust and C code.

> > +        // SAFETY:
> > +        // - `self.as_raw()` is valid per type invariants.
> > +        // - The `next` pointer is guaranteed to be non-NULL.
>
> I'm not sure whether "valid" in the type invariant implies that the struct
> list_head is initialized. From a language point of view it is also valid if the
> pointers are NULL.
>
> So, I think the invariant (and the safety requirements of from_raw()) have to
> ensure that the struct list_head is initialized in the sense of
> INIT_LIST_HEAD().

Agreed. The invariant and from_raw() safety requirements now explicitly
require INIT_LIST_HEAD() initialization with valid non-NULL next/prev
pointers. The next() SAFETY comment now reads:

  - `self.as_raw()` is valid and initialized per type invariants.
  - The `next` pointer is valid and non-NULL per type invariants
    (initialized via `INIT_LIST_HEAD()` or equivalent).

> > +/// - The [`CListHead`] is an allocated and valid sentinel C `list_head` structure.
> > +/// - `OFFSET` is the byte offset of the `list_head` field within the struct that `T` wraps.
> > +/// - All the list's `list_head` nodes are allocated and have valid next/prev pointers.
>
> Comments from CListHead also apply here.

Updated CList invariants and from_raw() safety docs to match the
CListHead pattern (removed "allocated", added INIT_LIST_HEAD, non-NULL
pointers, "concurrently modified").

> > +/// This is an unsafe macro. The caller must ensure:
>
> Given that, we should probably use the same (or a similar) trick as in [1].
>
> [1] https://rust.docs.kernel.org/src/kernel/device.rs.html#665-688

Done. Applied the device.rs pattern - the macro now requires
`clist_create!(unsafe { ... })` syntax, which forces callers to
acknowledge the safety requirements at the call site. The macro
internally wraps the `CList::from_raw` call in an unsafe block.

Thanks for the review!

Joel

WARNING: multiple messages have this Message-ID (diff)
From: Joel Fernandes <joelagnelf@nvidia.com>
To: Danilo Krummrich <dakr@kernel.org>
Cc: linux-kernel@vger.kernel.org, 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>,
	Alexandre Courbot <acourbot@nvidia.com>,
	Dave Airlie <airlied@redhat.com>,
	Daniel Almeida <daniel.almeida@collabora.com>,
	Koen Koning <koen.koning@linux.intel.com>,
	dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org,
	rust-for-linux@vger.kernel.org,
	Nikola Djukic <ndjukic@nvidia.com>
Subject: Re: [PATCH v10 5/8] rust: clist: Add support to interface with C linked lists
Date: Thu, 19 Feb 2026 10:27:36 -0500	[thread overview]
Message-ID: <eadfa4662f403fa01f19c1c17a435c1a@nvidia.com> (raw)
In-Reply-To: <DGIWDQTR76Y5.34L9IHKU2SEKI@kernel.org>

On Thu, Feb 19, 2026 at 12:21:56PM +0100, Danilo Krummrich wrote:
> On Wed Feb 18, 2026 at 9:55 PM CET, Joel Fernandes wrote:
> > +RUST TO C LIST INTERFACES
>
> Maybe this should just be "RUST [FFI]" instead (in case Alex and you want to
> sign up for looking after FFI helper infrastructure in general)?

Good idea, done.

> > +F:	rust/kernel/ffi/clist.rs
>
> <snip>
>
> > +//! This module provides Rust abstractions for iterating over C `list_head`-based
> > +//! linked lists. It is intended for FFI use-cases where a C subsystem manages a
> > +//! circular linked list that Rust code needs to read. This is generally required
> > +//! only for special cases and should be avoided by drivers.
>
> Maybe generalize the statement a bit and say that this should only be used for
> cases where C and Rust code share direct access to the same linked list through
> an FFI interface.
>
> Additionally, add a separate note that this *must not* be used by Rust
> components that just aim for a linked list primitive and instead refer to the
> Rust linked list implementation with an intra-doc link.

Done. Updated the module doc to say "It should only be used for cases
where C and Rust code share direct access to the same linked list
through an FFI interface" and added a separate note:

  Note: This *must not* be used by Rust components that just need a
  linked list primitive. Use [`kernel::list::List`] instead.

> > +//!     types::Opaque, //
>
> Examples don't necessarily need '//' at the end, as they are not automatically
> formatted anyways.

Removed from the example. Non-example imports keep the '//' as a
rustfmt guard.

> > +//! #     // SAFETY: pointers are to allocated test objects with a list_head field.
> > +//! #     unsafe {
>
> I understand that this is just setup code for a doc-test, but I still think we
> should hold it to the same standards, i.e. let's separate the different unsafe
> calls into their own unsafe blocks and add proper safety comments.

Done. Split into three separate unsafe blocks with individual SAFETY
comments for the value write, INIT_LIST_HEAD, and list_add_tail calls.

> > +    PinInit //
>
> Should be 'PinInit, //'.

Fixed.

> > +/// - [`CListHead`] represents an allocated and valid `list_head` structure.
>
> What does "allocated" mean in this context? (Dynamic allocations, stack, .data
> section of the binary, any of those?)
>
> In case of the latter, I'd just remove "allocated".

Removed "allocated". The invariant now reads:

  The underlying `list_head` has been initialized (e.g. via
  `INIT_LIST_HEAD()`) and its `next`/`prev` pointers are valid and
  non-NULL.

> > +    /// - `ptr` must be a valid pointer to an allocated and initialized `list_head` structure.
>
> Same here, what exactly is meant by "allocated"?

Removed "allocated" from from_raw() safety docs as well. Updated to:

  `ptr` must be a valid pointer to an initialized `list_head` (e.g.
  via `INIT_LIST_HEAD()`), with valid non-NULL `next`/`prev` pointers.

> > +    /// - The list and all linked `list_head` nodes must not be modified by non-Rust code
> > +    ///   for the lifetime `'a`.
>
> This is a bit vague I think, concurrent modifications of (other) Rust code are
> not OK either.

Fixed. Changed to "must not be concurrently modified for the lifetime
`'a`" which covers both Rust and C code.

> > +        // SAFETY:
> > +        // - `self.as_raw()` is valid per type invariants.
> > +        // - The `next` pointer is guaranteed to be non-NULL.
>
> I'm not sure whether "valid" in the type invariant implies that the struct
> list_head is initialized. From a language point of view it is also valid if the
> pointers are NULL.
>
> So, I think the invariant (and the safety requirements of from_raw()) have to
> ensure that the struct list_head is initialized in the sense of
> INIT_LIST_HEAD().

Agreed. The invariant and from_raw() safety requirements now explicitly
require INIT_LIST_HEAD() initialization with valid non-NULL next/prev
pointers. The next() SAFETY comment now reads:

  - `self.as_raw()` is valid and initialized per type invariants.
  - The `next` pointer is valid and non-NULL per type invariants
    (initialized via `INIT_LIST_HEAD()` or equivalent).

> > +/// - The [`CListHead`] is an allocated and valid sentinel C `list_head` structure.
> > +/// - `OFFSET` is the byte offset of the `list_head` field within the struct that `T` wraps.
> > +/// - All the list's `list_head` nodes are allocated and have valid next/prev pointers.
>
> Comments from CListHead also apply here.

Updated CList invariants and from_raw() safety docs to match the
CListHead pattern (removed "allocated", added INIT_LIST_HEAD, non-NULL
pointers, "concurrently modified").

> > +/// This is an unsafe macro. The caller must ensure:
>
> Given that, we should probably use the same (or a similar) trick as in [1].
>
> [1] https://rust.docs.kernel.org/src/kernel/device.rs.html#665-688

Done. Applied the device.rs pattern - the macro now requires
`clist_create!(unsafe { ... })` syntax, which forces callers to
acknowledge the safety requirements at the call site. The macro
internally wraps the `CList::from_raw` call in an unsafe block.

Thanks for the review!

Joel

  parent reply	other threads:[~2026-02-19 15:27 UTC|newest]

Thread overview: 90+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-18 20:54 [PATCH v10 0/8] Preparatory patches for nova-core memory management Joel Fernandes
2026-02-18 20:54 ` [PATCH v10 1/8] gpu: Move DRM buddy allocator one level up (part one) Joel Fernandes
2026-02-18 20:55 ` [PATCH v10 2/8] gpu: Move DRM buddy allocator one level up (part two) Joel Fernandes
2026-02-19  3:18   ` Alexandre Courbot
2026-02-19  3:18     ` Alexandre Courbot
2026-02-19 15:31     ` Joel Fernandes
2026-02-18 20:55 ` [PATCH v10 3/8] gpu: Fix uninitialized buddy for built-in drivers Joel Fernandes
2026-02-19 10:09   ` Danilo Krummrich
2026-02-19 10:09     ` Danilo Krummrich
2026-02-19 15:31     ` Joel Fernandes
2026-02-19 16:24       ` Joel Fernandes
2026-02-18 20:55 ` [PATCH v10 4/8] rust: ffi: Convert pub use to pub mod and create ffi module Joel Fernandes
2026-02-19  3:18   ` Alexandre Courbot
2026-02-18 20:55 ` [PATCH v10 5/8] rust: clist: Add support to interface with C linked lists Joel Fernandes
2026-02-19  4:26   ` Alexandre Courbot
2026-02-19 15:27     ` Joel Fernandes
2026-02-19 15:27       ` Joel Fernandes
2026-02-19  9:58   ` Danilo Krummrich
2026-02-19 15:28     ` Joel Fernandes
2026-02-19 15:28       ` Joel Fernandes
2026-02-19 11:21   ` Danilo Krummrich
2026-02-19 14:37     ` Gary Guo
2026-02-19 15:27     ` Joel Fernandes [this message]
2026-02-19 15:27       ` Joel Fernandes
2026-02-19 15:44       ` Joel Fernandes
2026-02-19 16:24         ` Danilo Krummrich
2026-02-19 18:07           ` Joel Fernandes
2026-02-19 18:38             ` Miguel Ojeda
2026-02-19 19:28               ` Joel Fernandes
2026-02-19 22:55                 ` Miguel Ojeda
2026-02-20  4:00                   ` Joel Fernandes
2026-02-20  1:56             ` Alexandre Courbot
2026-02-20  1:09           ` Gary Guo
2026-02-20  1:19             ` Miguel Ojeda
2026-02-20 16:48             ` Danilo Krummrich
2026-02-23  0:54               ` Joel Fernandes
2026-02-24 16:15                 ` Miguel Ojeda
2026-02-24 16:15                   ` Miguel Ojeda
2026-02-25 19:48               ` Boqun Feng
2026-02-25 20:20                 ` Joel Fernandes
2026-02-26  0:32                   ` Joel Fernandes
2026-02-20  8:16   ` Eliot Courtney
2026-02-20  8:16     ` Eliot Courtney
2026-02-23  1:13     ` Joel Fernandes
2026-02-24  2:08       ` Eliot Courtney
2026-02-24  2:08         ` Eliot Courtney
2026-02-24  7:28       ` Alice Ryhl
2026-02-24 16:00         ` Joel Fernandes
2026-02-24 16:11           ` Miguel Ojeda
2026-02-21  8:59   ` Alice Ryhl
2026-02-23  0:41     ` Joel Fernandes
2026-02-23  9:38       ` Alice Ryhl
2026-02-24  0:32         ` Joel Fernandes
2026-02-18 20:55 ` [PATCH v10 6/8] rust: gpu: Add GPU buddy allocator bindings Joel Fernandes
2026-02-19  5:13   ` Alexandre Courbot
2026-02-19  8:54     ` Miguel Ojeda
2026-02-19 15:31       ` Joel Fernandes
2026-03-01 13:23         ` Gary Guo
2026-03-01 13:23           ` Gary Guo
2026-03-01 17:53           ` Miguel Ojeda
2026-02-19 15:31     ` Joel Fernandes
2026-02-20  1:56       ` Alexandre Courbot
2026-02-20  1:56         ` Alexandre Courbot
2026-02-23  1:02         ` Joel Fernandes
2026-02-19 13:18   ` Danilo Krummrich
2026-02-19 15:31     ` Joel Fernandes
2026-02-20  8:22   ` Eliot Courtney
2026-02-20  8:22     ` Eliot Courtney
2026-02-20 14:54     ` Joel Fernandes
2026-02-20 15:50       ` Joel Fernandes
2026-02-20 15:53       ` Danilo Krummrich
2026-02-20 21:20         ` Joel Fernandes
2026-02-20 23:43           ` Danilo Krummrich
2026-02-23  0:34             ` Joel Fernandes
2026-02-18 20:55 ` [PATCH v10 7/8] nova-core: mm: Select GPU_BUDDY for VRAM allocation Joel Fernandes
2026-02-19  0:44   ` Alexandre Courbot
2026-02-19  0:44     ` Alexandre Courbot
2026-02-19  1:14     ` John Hubbard
2026-02-19  1:14       ` John Hubbard
2026-02-19 15:31       ` Joel Fernandes
2026-02-19  2:06     ` Joel Fernandes
2026-02-19  2:06       ` Joel Fernandes
2026-02-19 15:31     ` Joel Fernandes
2026-02-19 15:31       ` Joel Fernandes
2026-02-18 20:55 ` [PATCH v10 8/8] nova-core: Kconfig: Sort select statements alphabetically Joel Fernandes
2026-02-18 20:59 ` [PATCH v10 0/8] Preparatory patches for nova-core memory management Joel Fernandes
2026-02-18 20:59   ` Joel Fernandes
2026-02-18 22:24 ` Danilo Krummrich
2026-02-18 23:46   ` Joel Fernandes
2026-02-18 23:59     ` Joel Fernandes

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=eadfa4662f403fa01f19c1c17a435c1a@nvidia.com \
    --to=joelagnelf@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@redhat.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=koen.koning@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ndjukic@nvidia.com \
    --cc=nouveau@lists.freedesktop.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.