public inbox for linux-fbdev@vger.kernel.org
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Joel Fernandes" <joelagnelf@nvidia.com>
Cc: "Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
	"Alejandra González" <blyxyas@gmail.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	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>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Dave Airlie" <airlied@redhat.com>,
	"David Airlie" <airlied@gmail.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Koen Koning" <koen.koning@linux.intel.com>,
	"Nikola Djukic" <ndjukic@nvidia.com>,
	"Philipp Stanner" <phasta@kernel.org>,
	"Elle Rhumsaa" <elle@weathered-steel.dev>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Alex Deucher" <alexander.deucher@amd.com>,
	"Christian König" <christian.koenig@amd.com>,
	"Jani Nikula" <jani.nikula@linux.intel.com>,
	"Joonas Lahtinen" <joonas.lahtinen@linux.intel.com>,
	"Rodrigo Vivi" <rodrigo.vivi@intel.com>,
	"Tvrtko Ursulin" <tursulin@ursulin.net>,
	"Huang Rui" <ray.huang@amd.com>,
	"Matthew Auld" <matthew.auld@intel.com>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Lucas De Marchi" <lucas.demarchi@intel.com>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Helge Deller" <deller@gmx.de>,
	"John Hubbard" <jhubbard@nvidia.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Timur Tabi" <ttabi@nvidia.com>, "Edwin Peer" <epeer@nvidia.com>,
	"Andrea Righi" <arighi@nvidia.com>,
	"Andy Ritger" <aritger@nvidia.com>, "Zhi Wang" <zhiw@nvidia.com>,
	"Balbir Singh" <balbirs@nvidia.com>,
	alexeyi@nvidia.com, "Eliot Courtney" <ecourtney@nvidia.com>,
	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
Subject: Re: [PATCH v13 1/1] rust: interop: Add list module for C linked list interface
Date: Thu, 19 Mar 2026 20:59:42 +0900	[thread overview]
Message-ID: <DH6QPWOTC3LG.F0RS2U7GDJDW@nvidia.com> (raw)
In-Reply-To: <20260318192446.GA591541@joelbox2>

On Thu Mar 19, 2026 at 4:24 AM JST, Joel Fernandes wrote:
> On Wed, Mar 18, 2026 at 07:57:14PM +0100, Miguel Ojeda wrote:
>> On Wed, Mar 18, 2026 at 7:31 PM Joel Fernandes <joelagnelf@nvidia.com> wrote:
>> >
>> > Anyway, the fix is simple, just need to do // SAFETY*: as Miguel suggests
>> > here, instead of // SAFETY:
>> > https://lore.kernel.org/all/CANiq72kEnDyUpnWMZmheJytjioeiJUK_C-yQJk77dPid89LExw@mail.gmail.com/
>> 
>> So, to clarify, I suggested it as a temporary thing we could do if we
>> want to use that "fake `unsafe` block in macro matcher" pattern more
>> and more.
>> 
>> i.e. if we plan to use the pattern more, then I am happy to ask
>> upstream if it would make sense for Clippy to recognize it (or perhaps
>> it is just a false negative instead of a false positive, given
>> `impl_device_context_deref`), so that we don't need a hacked safety
>> tag (Cc'ing Alejandra).
>> 
>> But if we could put it outside, then we wouldn't need any of that.
>> Unsafe macros support could help perhaps here, which I have had it in
>> our wishlist too (https://github.com/Rust-for-Linux/linux/issues/354),
>> but I guess the fake block could still be useful to make only certain
>> macro arms unsafe? (Perhaps Rust could allow `unsafe` just at the
>> start of each arm for that...).
>
> Even if I reworked the macro to be outisde, it doesn't work as below, still
> need the 'disabled' comment on the macro's generate unsafe { } block below.
>
> If we don't want the SAFETY*: hack, we could do the following.
>
> Perhaps, we can file the github bug and also do the below. Once the
> github bug is fixed, we could remove the 'disable lint' below.
>
> Thoughts? 
>
> ---8<-----------------------
>
> diff --git a/rust/kernel/interop/list.rs b/rust/kernel/interop/list.rs
> index 495497f0405e..dfa2e1490202 100644
> --- a/rust/kernel/interop/list.rs
> +++ b/rust/kernel/interop/list.rs
> @@ -73,7 +73,7 @@
>  //!
>  //!
>  //! // Create typed [`CList`] from sentinel head.
> -//! // SAFETY*: `head` is valid and initialized, items are `SampleItemC` with
> +//! // 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 });
>  //!
> @@ -328,17 +328,19 @@ impl<'a, T, const OFFSET: usize> FusedIterator for CListIter<'a, T, OFFSET> {}
>  /// Refer to the examples in the [`crate::interop::list`] module documentation.
>  #[macro_export]
>  macro_rules! clist_create {
> -    (unsafe { $head:ident, $rust_type:ty, $c_type:ty, $($field:tt).+ }) => {{
> +    (unsafe { $head:ident, $rust_type:ty, $c_type:ty, $($field:tt).+ }) => (
> +        // SAFETY: disable lint.
> +        unsafe { {{
>          // Compile-time check that field path is a `list_head`.
>          let _: fn(*const $c_type) -> *const $crate::bindings::list_head = |p| {
>              // SAFETY: `p` is a valid pointer to `$c_type`.
> -            unsafe { &raw const (*p).$($field).+ }
> +            &raw const (*p).$($field).+
>          };
>  
>          // Calculate offset and create `CList`.
>          const OFFSET: usize = ::core::mem::offset_of!($c_type, $($field).+);
>          // SAFETY: The caller of this macro is responsible for ensuring safety.
> -        unsafe { $crate::interop::list::CList::<$rust_type, OFFSET>::from_raw($head) }
> -    }};
> +        $crate::interop::list::CList::<$rust_type, OFFSET>::from_raw($head)
> +    } }});
>  }
>  pub use clist_create;

I think I like this, it preserves the expected use of `SAFETY:` without
that confusing `*`. The unsafe blocks is a bit larger that it should, be
we are in a controlled environment.

Even after using the `SAFETY*:` I was still getting errors because the
in-macro SAFETY comment wasn't at the right place:

    warning: unsafe block missing a safety comment
      --> ../rust/kernel/interop/list.rs:335:17
        |
    335 |               |p| unsafe { &raw const (*p).$($field).+ };
        |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
      ::: ../rust/kernel/gpu/buddy.rs:527:21
        |
    527 |           let clist = clist_create!(unsafe {
        |  _____________________-
    528 | |             head,
    529 | |             Block,
    530 | |             bindings::gpu_buddy_block,
    531 | |             __bindgen_anon_1.link
    532 | |         });
        | |__________- in this macro invocation

The diff below fixes that, but I believe your proposal should as well on
top of letting callers use the expected SAFETY statement.

--- a/rust/kernel/interop/list.rs
+++ b/rust/kernel/interop/list.rs
@@ -330,8 +330,8 @@ impl<'a, T, const OFFSET: usize> FusedIterator for CListIter<'a, T, OFFSET> {}
 macro_rules! clist_create {
     (unsafe { $head:ident, $rust_type:ty, $c_type:ty, $($field:tt).+ }) => {{
         // Compile-time check that field path is a `list_head`.
-        // SAFETY: `p` is a valid pointer to `$c_type`.
         let _: fn(*const $c_type) -> *const $crate::bindings::list_head =
+            // SAFETY: `p` is a valid pointer to `$c_type`.
             |p| unsafe { &raw const (*p).$($field).+ };

         // Calculate offset and create `CList`.


  reply	other threads:[~2026-03-19 11:59 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-17 20:17 [PATCH v13 0/1] rust: interop: Add list module for C linked list interface Joel Fernandes
2026-03-17 20:17 ` [PATCH v13 1/1] " Joel Fernandes
2026-03-17 20:18   ` Joel Fernandes
2026-03-17 20:20     ` Danilo Krummrich
2026-03-17 20:27       ` Joel Fernandes
2026-03-18  8:59     ` Alice Ryhl
2026-03-18 10:53       ` Alexandre Courbot
2026-03-18 10:59         ` Alice Ryhl
2026-03-18 14:30           ` Miguel Ojeda
2026-03-18 14:21         ` Miguel Ojeda
2026-03-18 14:31           ` Alice Ryhl
2026-03-18 14:41             ` Miguel Ojeda
2026-03-18 14:49               ` Danilo Krummrich
2026-03-18 18:57                 ` Miguel Ojeda
2026-03-18 14:43             ` Danilo Krummrich
2026-03-18 13:31       ` Gary Guo
2026-03-18 17:58         ` Joel Fernandes
2026-03-18  9:10   ` Alice Ryhl
2026-03-18 18:55     ` Joel Fernandes
2026-03-18 12:40   ` Alice Ryhl
2026-03-18 14:03     ` Miguel Ojeda
2026-03-18 18:31     ` Joel Fernandes
2026-03-18 18:43       ` Joel Fernandes
2026-03-18 18:57       ` Miguel Ojeda
2026-03-18 19:24         ` Joel Fernandes
2026-03-19 11:59           ` Alexandre Courbot [this message]
2026-03-23  0:07         ` Alejandra González
2026-03-19 11:39   ` Gary Guo
2026-03-19 12:05     ` Danilo Krummrich
2026-03-19 12:21       ` Gary Guo
2026-03-19 12:51         ` Danilo Krummrich
2026-03-19 16:56         ` 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=DH6QPWOTC3LG.F0RS2U7GDJDW@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=airlied@gmail.com \
    --cc=airlied@redhat.com \
    --cc=alex.gaynor@gmail.com \
    --cc=alexander.deucher@amd.com \
    --cc=alexeyi@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=apopple@nvidia.com \
    --cc=arighi@nvidia.com \
    --cc=aritger@nvidia.com \
    --cc=balbirs@nvidia.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=blyxyas@gmail.com \
    --cc=boqun@kernel.org \
    --cc=christian.koenig@amd.com \
    --cc=corbet@lwn.net \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=deller@gmx.de \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=ecourtney@nvidia.com \
    --cc=elle@weathered-steel.dev \
    --cc=epeer@nvidia.com \
    --cc=gary@garyguo.net \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=jani.nikula@linux.intel.com \
    --cc=jhubbard@nvidia.com \
    --cc=joelagnelf@nvidia.com \
    --cc=joonas.lahtinen@linux.intel.com \
    --cc=koen.koning@linux.intel.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fbdev@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=lucas.demarchi@intel.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=matthew.auld@intel.com \
    --cc=matthew.brost@intel.com \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=mripard@kernel.org \
    --cc=ndjukic@nvidia.com \
    --cc=ojeda@kernel.org \
    --cc=phasta@kernel.org \
    --cc=ray.huang@amd.com \
    --cc=rodrigo.vivi@intel.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=tursulin@ursulin.net \
    --cc=tzimmermann@suse.de \
    --cc=zhiw@nvidia.com \
    /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