rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Benno Lossin" <lossin@kernel.org>
To: "Janne Grunau" <j@jannau.net>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Fiona Behrens" <me@kloenk.dev>, "Alban Kurti" <kurti@invicto.ai>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	asahi@lists.linux.de
Subject: Re: [PATCH] rust: pin-init: add references to previously initialized fields
Date: Sat, 06 Dec 2025 09:23:08 +0100	[thread overview]
Message-ID: <DEQZLZ9AVCZE.380X3OF521Y5L@kernel.org> (raw)
In-Reply-To: <20251203220516.GA592465@robin.jannau.net>

On Wed Dec 3, 2025 at 11:05 PM CET, Janne Grunau wrote:
> On Fri, Sep 05, 2025 at 04:00:46PM +0200, Benno Lossin wrote:
>> After initializing a field in an initializer macro, create a variable
>> holding a reference that points at that field. The type is either
>> `Pin<&mut T>` or `&mut T` depending on the field's structural pinning
>> kind.
>
> just as a heads up: creating references broke part of the agx firmware
> init structs which uses a `#[repr(C, packed)]` struct as field in
> another struct. This fails with
>
> | error[E0793]: reference to packed field is unaligned
> |    --> ../drivers/gpu/drm/asahi/initdata.rs:722:28
> |     |
> | 722 |                       sub <- try_init!(raw::GlobalsSub::ver {
> |     |  ____________________________^
> | 723 | |                         unk_54: cfg.global_unk_54,
> | 724 | |                         unk_56: 40,
> | 725 | |                         unk_58: 0xffff,
> | ...   |
> | 731 | |                         ..Zeroable::init_zeroed()
> | 732 | |                     }),
> |     | |______________________^
> |     |
> |     = note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
> |     = note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
> |     = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
> |     = note: this error originates in the macro `$crate::__init_internal` which comes from the expansion of the macro `try_init` (in Nightly builds, run with -Z macro-backtrace for more info)
>
> This was easy enough to work around, I don't see how this embedding of a
> `#[repr(C, packed)]` struct was necessary or at least helpful. The code
> is not expected to be included in the upstream driver so it no worth
> spending effort on this.
>
> I don't think it's likely that anyone else will run into this but I
> thought I mention it at least.
>
> The asahi driver also ran into the discussed variable shadowing issue (a
> variable was used to initialize a field of the same name and was later
> used to initialize another field). This was trivially fixed by renaming
> the variable.

Thanks for the report, I expected the latter kind of error, but was not
aware of the packed struct issue. If anyone needs a proper workaround
from pin-init, let me know.

Cheers,
Benno

  reply	other threads:[~2025-12-06  8:23 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-05 14:00 [PATCH] rust: pin-init: add references to previously initialized fields Benno Lossin
2025-09-05 17:18 ` Benno Lossin
2025-09-05 17:44   ` Boqun Feng
2025-09-06 10:52     ` Danilo Krummrich
2025-09-07  1:57       ` Boqun Feng
2025-09-07  2:07         ` Boqun Feng
2025-09-07  8:41           ` Benno Lossin
2025-09-07 17:29             ` Danilo Krummrich
2025-09-07 21:06               ` Benno Lossin
2025-09-07 21:39                 ` Danilo Krummrich
2025-09-07 22:51                   ` Benno Lossin
2025-09-07 23:33                     ` Danilo Krummrich
2025-09-08  2:08                       ` Boqun Feng
2025-09-08  8:27                         ` Benno Lossin
2025-09-08  8:57                           ` Danilo Krummrich
2025-09-08 19:38                             ` Boqun Feng
2025-09-08 20:31                               ` Danilo Krummrich
2025-09-10 10:12                               ` Benno Lossin
2025-09-05 17:21 ` Boqun Feng
2025-09-05 18:38   ` Danilo Krummrich
2025-09-06 14:23 ` kernel test robot
2025-09-11 21:35 ` Benno Lossin
2025-12-03 22:05 ` Janne Grunau
2025-12-06  8:23   ` Benno Lossin [this message]
2025-12-06 17:02     ` Janne Grunau

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=DEQZLZ9AVCZE.380X3OF521Y5L@kernel.org \
    --to=lossin@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=asahi@lists.linux.de \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=j@jannau.net \
    --cc=kurti@invicto.ai \
    --cc=linux-kernel@vger.kernel.org \
    --cc=me@kloenk.dev \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).