From: "Danilo Krummrich" <dakr@kernel.org>
To: "Alice Ryhl" <aliceryhl@google.com>
Cc: "Benno Lossin" <lossin@kernel.org>,
"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>,
"Trevor Gross" <tmgross@umich.edu>,
"Fiona Behrens" <me@kloenk.dev>, "Alban Kurti" <kurti@invicto.ai>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] rust: pin-init: add `#[bind]` attribute to access previously initialized fields
Date: Wed, 10 Sep 2025 13:15:50 +0200 [thread overview]
Message-ID: <DCP2STI9M1XX.3RHBQDPQOC3JO@kernel.org> (raw)
In-Reply-To: <CAH5fLgg+-oz_cP9=ke+ukp9qYZAsD=hKqvvGfkJWRcCKdrTQ-g@mail.gmail.com>
On Wed Sep 10, 2025 at 12:40 PM CEST, Alice Ryhl wrote:
> On Wed, Sep 10, 2025 at 12:36 PM Benno Lossin <lossin@kernel.org> wrote:
>>
>> On Wed Sep 10, 2025 at 12:17 PM CEST, Alice Ryhl wrote:
>> > On Wed, Sep 10, 2025 at 12:07:53PM +0200, Benno Lossin wrote:
>> >> Assigning a field a value in an initializer macro can be marked with the
>> >> `#[bind]` attribute. Doing so creates a `let` binding with the same
>> >> name. This `let` binding has the type `Pin<&mut T>` if the field is
>> >> structurally pinned or `&mut T` otherwise (where `T` is the type of the
>> >> field).
>> >>
>> >> Signed-off-by: Benno Lossin <lossin@kernel.org>
>> >
>> > Is there a reason we can't apply this to all fields and avoid the
>> > attribute?
>>
>> Adding the attribute was due to Boqun's concern on v1 [1]. I think it
>> might be surprising too, but I'm also happy with no attribute.
>>
>> [1]: https://lore.kernel.org/all/aLshd0_C-1rh3FAg@tardis-2.local
>
> IMO the ideal is if it works without an attribute. Perhaps trying that
> in the kernel is a reasonable experiment to find out whether that's
> reasonable to do for the general language feature?
>
>> > Do we have a place that might be able to use this?
>>
>> I didn't find one, but Danilo plans to base some changes on top this
>> cycle that need this.
We can use it in devres right away:
diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs
index d04e3fcebafb..97c616a1733d 100644
--- a/rust/kernel/devres.rs
+++ b/rust/kernel/devres.rs
@@ -137,10 +137,11 @@ pub fn new<'a, E>(
{
let callback = Self::devres_callback;
- try_pin_init!(&this in Self {
+ try_pin_init!(Self {
dev: dev.into(),
callback,
// INVARIANT: `inner` is properly initialized.
+ #[bind]
inner <- Opaque::pin_init(try_pin_init!(Inner {
devm <- Completion::new(),
revoke <- Completion::new(),
@@ -150,8 +151,7 @@ pub fn new<'a, E>(
//
// [1] https://github.com/Rust-for-Linux/pin-init/pull/69
_add_action: {
- // SAFETY: `this` is a valid pointer to uninitialized memory.
- let inner = unsafe { &raw mut (*this.as_ptr()).inner };
+ let inner = core::ptr::from_ref(inner.into_ref().get_ref());
// SAFETY:
// - `dev.as_raw()` is a pointer to a valid bound device.
@@ -160,7 +160,7 @@ pub fn new<'a, E>(
// properly initialized, because we require `dev` (i.e. the *bound* device) to
// live at least as long as the returned `impl PinInit<Self, Error>`.
to_result(unsafe {
- bindings::devm_add_action(dev.as_raw(), Some(callback), inner.cast())
+ bindings::devm_add_action(dev.as_raw(), Some(callback), inner.cast_mut().cast())
}).inspect_err(|_| {
let inner = Opaque::cast_into(inner);
Together with the initializer code blocks this becomes quite nice. :)
> Danilo, what plans do you have?
Besides that, the plan is [1].
[1] https://lore.kernel.org/all/DCL32RUQ6Z56.1ERY7JBK6O1J6@kernel.org/
next prev parent reply other threads:[~2025-09-10 11:15 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-10 10:07 [PATCH v2] rust: pin-init: add `#[bind]` attribute to access previously initialized fields Benno Lossin
2025-09-10 10:17 ` Alice Ryhl
2025-09-10 10:35 ` Benno Lossin
2025-09-10 10:40 ` Alice Ryhl
2025-09-10 11:15 ` Danilo Krummrich [this message]
2025-09-10 11:52 ` Alice Ryhl
2025-09-10 13:09 ` Danilo Krummrich
2025-09-10 12:19 ` Benno Lossin
2025-09-10 13:55 ` Danilo Krummrich
2025-09-10 14:02 ` Boqun Feng
2025-09-10 14:59 ` Benno Lossin
2025-09-10 15:06 ` Boqun Feng
2025-09-10 15:39 ` Benno Lossin
2025-09-10 15:08 ` Danilo Krummrich
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=DCP2STI9M1XX.3RHBQDPQOC3JO@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=kurti@invicto.ai \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@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 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.