Skip to contentSkip to conversation

rust: pin-init: add references to previously initialized fields

PATCH Applied Benno Lossin 5 participants 27 messages Sep 5, 2025 – Feb 27

Subsystems (4)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Benno Lossin

author maintainer
#0 lore raw

PATCH 1/1 rust: pin-init: add references to previously initialized fields

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.

---
 rust/pin-init/src/macros.rs | 149 ++++++++++++++++++++++++++++--------
 1 file changed, 115 insertions(+), 34 deletions(-)
rust/pin-init/src/macros.rs +115 -34
index 9ced630737b8..1100c5a0b3de 100644
@@ -988,38 +988,56 @@ fn drop(&mut self) {
         @pinned($($(#[$($p_attr:tt)*])* $pvis:vis $p_field:ident : $p_type:ty),* $(,)?),
         @not_pinned($($(#[$($attr:tt)*])* $fvis:vis $field:ident : $type:ty),* $(,)?),
     ) => {
-        // For every field, we create a projection function according to its projection type. If a
-        // field is structurally pinned, then it must be initialized via `PinInit`, if it is not
-        // structurally pinned, then it can be initialized via `Init`.
-        //
-        // The functions are `unsafe` to prevent accidentally calling them.
-        #[allow(dead_code)]
-        #[expect(clippy::missing_safety_doc)]
-        impl<$($impl_generics)*> $pin_data<$($ty_generics)*>
-        where $($whr)*
-        {
-            $(
-                $(#[$($p_attr)*])*
-                $pvis unsafe fn $p_field<E>(
-                    self,
-                    slot: *mut $p_type,
-                    init: impl $crate::PinInit<$p_type, E>,
-                ) -> ::core::result::Result<(), E> {
-                    // SAFETY: TODO.
-                    unsafe { $crate::PinInit::__pinned_init(init, slot) }
-                }
-            )*
-            $(
-                $(#[$($attr)*])*
-                $fvis unsafe fn $field<E>(
-                    self,
-                    slot: *mut $type,
-                    init: impl $crate::Init<$type, E>,
-                ) -> ::core::result::Result<(), E> {
-                    // SAFETY: TODO.
-                    unsafe { $crate::Init::__init(init, slot) }
-                }
-            )*
+        $crate::macros::paste! {
+            // For every field, we create a projection function according to its projection type. If a
+            // field is structurally pinned, then it must be initialized via `PinInit`, if it is not
+            // structurally pinned, then it can be initialized via `Init`.
+            //
+            // The functions are `unsafe` to prevent accidentally calling them.
+            #[allow(dead_code)]
+            #[expect(clippy::missing_safety_doc)]
+            impl<$($impl_generics)*> $pin_data<$($ty_generics)*>
+            where $($whr)*
+            {
+                $(
+                    $(#[$($p_attr)*])*
+                    $pvis unsafe fn $p_field<E>(
+                        self,
+                        slot: *mut $p_type,
+                        init: impl $crate::PinInit<$p_type, E>,
+                    ) -> ::core::result::Result<(), E> {
+                        // SAFETY: TODO.
+                        unsafe { $crate::PinInit::__pinned_init(init, slot) }
+                    }
+
+                    $(#[$($p_attr)*])*
+                    $pvis unsafe fn [<__project_ $p_field>]<'__slot>(
+                        self,
+                        slot: &'__slot mut $p_type,
+                    ) -> ::core::pin::Pin<&'__slot mut $p_type> {
+                        ::core::pin::Pin::new_unchecked(slot)
+                    }
+                )*
+                $(
+                    $(#[$($attr)*])*
+                    $fvis unsafe fn $field<E>(
+                        self,
+                        slot: *mut $type,
+                        init: impl $crate::Init<$type, E>,
+                    ) -> ::core::result::Result<(), E> {
+                        // SAFETY: TODO.
+                        unsafe { $crate::Init::__init(init, slot) }
+                    }
+
+                    $(#[$($attr)*])*
+                    $fvis unsafe fn [<__project_ $field>]<'__slot>(
+                        self,
+                        slot: &'__slot mut $type,
+                    ) -> &'__slot mut $type {
+                        slot
+                    }
+                )*
+            }
         }
     };
 }
@@ -1216,6 +1234,13 @@ fn assert_zeroable<T: $crate::Zeroable>(_: *mut T) {}
         // return when an error/panic occurs.
         // We also use the `data` to require the correct trait (`Init` or `PinInit`) for `$field`.
         unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), init)? };
+        // SAFETY:
+        // - the project function does the correct field projection,
+        // - the field has been initialized,
+        // - the reference is only valid until the end of the initializer.
+        #[allow(unused_variables)]
+        let $field = $crate::macros::paste!(unsafe { $data.[< __project_ $field >](&mut (*$slot).$field) });
+
         // Create the drop guard:
         //
         // We rely on macro hygiene to make it impossible for users to access this local variable.
@@ -1247,6 +1272,14 @@ fn assert_zeroable<T: $crate::Zeroable>(_: *mut T) {}
         // SAFETY: `slot` is valid, because we are inside of an initializer closure, we
         // return when an error/panic occurs.
         unsafe { $crate::Init::__init(init, ::core::ptr::addr_of_mut!((*$slot).$field))? };
+
+        // SAFETY:
+        // - the field is not structurally pinned, since the line above must compile,
+        // - the field has been initialized,
+        // - the reference is only valid until the end of the initializer.
+        #[allow(unused_variables)]
+        let $field = unsafe { &mut (*$slot).$field };
+
         // Create the drop guard:
         //
         // We rely on macro hygiene to make it impossible for users to access this local variable.
@@ -1265,7 +1298,48 @@ fn assert_zeroable<T: $crate::Zeroable>(_: *mut T) {}
             );
         }
     };
-    (init_slot($($use_data:ident)?):
+    (init_slot(): // No `use_data`, so all fields are not structurally pinned
+        @data($data:ident),
+        @slot($slot:ident),
+        @guards($($guards:ident,)*),
+        // Init by-value.
+        @munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*),
+    ) => {
+        {
+            $(let $field = $val;)?
+            // Initialize the field.
+            //
+            // SAFETY: The memory at `slot` is uninitialized.
+            unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) };
+        }
+
+        #[allow(unused_variables)]
+        // SAFETY:
+        // - the field is not structurally pinned, since no `use_data` was required to create this
+        //   initializer,
+        // - the field has been initialized,
+        // - the reference is only valid until the end of the initializer.
+        let $field = unsafe { &mut (*$slot).$field };
+
+        // Create the drop guard:
+        //
+        // We rely on macro hygiene to make it impossible for users to access this local variable.
+        // We use `paste!` to create new hygiene for `$field`.
+        $crate::macros::paste! {
+            // SAFETY: We forget the guard later when initialization has succeeded.
+            let [< __ $field _guard >] = unsafe {
+                $crate::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
+            };
+
+            $crate::__init_internal!(init_slot():
+                @data($data),
+                @slot($slot),
+                @guards([< __ $field _guard >], $($guards,)*),
+                @munch_fields($($rest)*),
+            );
+        }
+    };
+    (init_slot($use_data:ident):
         @data($data:ident),
         @slot($slot:ident),
         @guards($($guards:ident,)*),
@@ -1279,6 +1353,13 @@ fn assert_zeroable<T: $crate::Zeroable>(_: *mut T) {}
             // SAFETY: The memory at `slot` is uninitialized.
             unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) };
         }
+        // SAFETY:
+        // - the project function does the correct field projection,
+        // - the field has been initialized,
+        // - the reference is only valid until the end of the initializer.
+        #[allow(unused_variables)]
+        let $field = $crate::macros::paste!(unsafe { $data.[< __project_ $field >](&mut (*$slot).$field) });
+
         // Create the drop guard:
         //
         // We rely on macro hygiene to make it impossible for users to access this local variable.
@@ -1289,7 +1370,7 @@ fn assert_zeroable<T: $crate::Zeroable>(_: *mut T) {}
                 $crate::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
             };
 
-            $crate::__init_internal!(init_slot($($use_data)?):
+            $crate::__init_internal!(init_slot($use_data):
                 @data($data),
                 @slot($slot),
                 @guards([< __ $field _guard >], $($guards,)*),

base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585

Signature · 1 line
2.50.1
author maintainer
#1 lore raw
Quoted 7 lines · Benno Lossin

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.

I forgot to test with the right configuration and found some errors with existing code. Here are their fixes. If I don't need to re-send, I will add them on apply (if you want a v2, let me know).

---
Cheers,
Benno

rust/kernel/devres.rs +2 -4
index da18091143a6..91dbf3f4b166 100644
@@ -134,11 +134,9 @@ pub fn new<'a, E>(
         T: 'a,
         Error: From<E>,
     {
-        let callback = Self::devres_callback;
-
         try_pin_init!(&this in Self {
             dev: dev.into(),
-            callback,
+            callback: Self::devres_callback,
             // INVARIANT: `inner` is properly initialized.
             inner <- {
                 // SAFETY: `this` is a valid pointer to uninitialized memory.
@@ -151,7 +149,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())
                 })?;
 
                 Opaque::pin_init(try_pin_init!(Inner {
samples/rust/rust_driver_pci.rs +1 -1
index 606946ff4d7f..1ac0b06fa3b3 100644
@@ -78,8 +78,8 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self>
 
         let drvdata = KBox::pin_init(
             try_pin_init!(Self {
-                pdev: pdev.into(),
                 bar <- pdev.iomap_region_sized::<{ Regs::END }>(0, c_str!("rust_driver_pci")),
+                pdev: pdev.into(),
                 index: *info,
             }),
             GFP_KERNEL,
#2 lore raw

On Fri, Sep 05, 2025 at 07:18:25PM +0200, Benno Lossin wrote:
[...]

samples/rust/rust_driver_pci.rs fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self> · line 82

Quoted 10 lines

index 606946ff4d7f..1ac0b06fa3b3 100644

samples/rust/rust_driver_pci.rs +1 -1
@@ -78,8 +78,8 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self>
 
         let drvdata = KBox::pin_init(
             try_pin_init!(Self {
-                pdev: pdev.into(),
                 bar <- pdev.iomap_region_sized::<{ Regs::END }>(0, c_str!("rust_driver_pci")),
+                pdev: pdev.into(),

Ok, this example is good enough for me to express the concern here: the variable shadowing behavior seems not straightforward (maybe because in normal Rust initalization expression, no binding is created for previous variables, neither do we have a let here).

Would the future inplace initialization have the similar behavior? I asked because a natural resolution is adding a special syntax like:

    let a = ..;

    try_pin_init!(Self {
        b: a,
	let a = a.into(); // create the new binding here.
	c: a, // <- use the previous initalized `a`.
    }

(Since I lost tracking of the discussion a bit, maybe there is a previous discussion I've missed here?)

Regards,
Boqun

Quoted 3 lines
samples/rust/rust_driver_pci.rs +0 -0
                 index: *info,
             }),
             GFP_KERNEL,
maintainer
#3 lore raw
Quoted 21 lines · Boqun Feng

On Fri, Sep 05, 2025 at 07:18:25PM +0200, Benno Lossin wrote:
[...]

samples/rust/rust_driver_pci.rs fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self> · line 82

Quoted 10 lines

index 606946ff4d7f..1ac0b06fa3b3 100644

samples/rust/rust_driver_pci.rs +1 -1
@@ -78,8 +78,8 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self>
 
         let drvdata = KBox::pin_init(
             try_pin_init!(Self {
-                pdev: pdev.into(),
                 bar <- pdev.iomap_region_sized::<{ Regs::END }>(0, c_str!("rust_driver_pci")),
+                pdev: pdev.into(),

Ok, this example is good enough for me to express the concern here: the variable shadowing behavior seems not straightforward (maybe because in normal Rust initalization expression, no binding is created for previous variables, neither do we have a let here).

Would the future inplace initialization have the similar behavior? I asked because a natural resolution is adding a special syntax like:

Quoted 7 lines
    let a = ..;

    try_pin_init!(Self {
        b: a,
	let a = a.into(); // create the new binding here.
	c: a, // <- use the previous initalized `a`.
    }

Can you please clarify the example? I'm a bit confused that this is not a field of Self, so currently this can just be written as:

	try_pin_init!(Self {
	   b: a,
	   c: a.into,
	})

Of course assuming that a is Clone, as the code above does as well.

So, if we are concerned by the variable shadowing, which I'm less concerned about, maybe we can do this:

	// The "original" `a` and `b`.
	let a: A = ...;
	let b: B = ...;

	try_pin_init!(Self {
	   a,                   // Initialize the field only.
	   let b <- b,          // Initialize the field and create a `&B` named `b`.
	   c: a.into(),         // That's the "original" `a`.
	   d <- D::new(b),      // Not the original `b`, but the pin-init one.
	})
#4 lore raw
Quoted 24 lines · Danilo Krummrich
Quoted 23 lines · Boqun Feng

On Fri, Sep 05, 2025 at 07:18:25PM +0200, Benno Lossin wrote:
[...]

samples/rust/rust_driver_pci.rs fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self> · line 82

Quoted 10 lines

index 606946ff4d7f..1ac0b06fa3b3 100644

samples/rust/rust_driver_pci.rs +1 -1
@@ -78,8 +78,8 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self>
 
         let drvdata = KBox::pin_init(
             try_pin_init!(Self {
-                pdev: pdev.into(),
                 bar <- pdev.iomap_region_sized::<{ Regs::END }>(0, c_str!("rust_driver_pci")),
+                pdev: pdev.into(),

Ok, this example is good enough for me to express the concern here: the variable shadowing behavior seems not straightforward (maybe because in normal Rust initalization expression, no binding is created for previous variables, neither do we have a let here).

Would the future inplace initialization have the similar behavior? I asked because a natural resolution is adding a special syntax like:

    let a = ..;
Quoted 9 lines
Quoted 5 lines
    try_pin_init!(Self {
        b: a,
	let a = a.into(); // create the new binding here.
	c: a, // <- use the previous initalized `a`.
    }

Can you please clarify the example? I'm a bit confused that this is not a field of Self, so currently this can just be written as:

Oh, I could have been more clear: a is a field of Self, and the let part initalizes it.

Quoted 9 lines
	try_pin_init!(Self {
	   b: a,
	   c: a.into,
	})

Of course assuming that a is Clone, as the code above does as well.

So, if we are concerned by the variable shadowing, which I'm less concerned about, maybe we can do this:

I'm not that concerned to block this, but it does look to me like we are inventing a new way (and even a different syntax because normal Rust initialization doesn't create new bindings) to create binding, so I think I should bring it up.

Quoted 11 lines
	// The "original" `a` and `b`.
	let a: A = ...;
	let b: B = ...;

	try_pin_init!(Self {
	   a,                   // Initialize the field only.
	   let b <- b,          // Initialize the field and create a `&B` named `b`.
	   c: a.into(),         // That's the "original" `a`.
	   d <- D::new(b),      // Not the original `b`, but the pin-init one.
	})

This looks good to me as well. But I'm curious what the syntax would be like in the in-place placement language feature in the future.

Regards,
Boqun

#5 lore raw
Quoted 45 lines · Boqun Feng
Quoted 24 lines · Danilo Krummrich
Quoted 23 lines · Boqun Feng

On Fri, Sep 05, 2025 at 07:18:25PM +0200, Benno Lossin wrote:
[...]

samples/rust/rust_driver_pci.rs fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self> · line 82

Quoted 10 lines

index 606946ff4d7f..1ac0b06fa3b3 100644

samples/rust/rust_driver_pci.rs +1 -1
@@ -78,8 +78,8 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self>
 
         let drvdata = KBox::pin_init(
             try_pin_init!(Self {
-                pdev: pdev.into(),
                 bar <- pdev.iomap_region_sized::<{ Regs::END }>(0, c_str!("rust_driver_pci")),
+                pdev: pdev.into(),

Ok, this example is good enough for me to express the concern here: the variable shadowing behavior seems not straightforward (maybe because in normal Rust initalization expression, no binding is created for previous variables, neither do we have a let here).

Would the future inplace initialization have the similar behavior? I asked because a natural resolution is adding a special syntax like:

    let a = ..;
Quoted 9 lines
Quoted 5 lines
    try_pin_init!(Self {
        b: a,
	let a = a.into(); // create the new binding here.
	c: a, // <- use the previous initalized `a`.
    }

Can you please clarify the example? I'm a bit confused that this is not a field of Self, so currently this can just be written as:

Oh, I could have been more clear: a is a field of Self, and the let part initalizes it.

Quoted 7 lines
	try_pin_init!(Self {
	   b: a,
	   c: a.into,
	})

Of course assuming that a is Clone, as the code above does as well.

Quoted 19 lines
Quoted 2 lines

So, if we are concerned by the variable shadowing, which I'm less concerned about, maybe we can do this:

I'm not that concerned to block this, but it does look to me like we are inventing a new way (and even a different syntax because normal Rust initialization doesn't create new bindings) to create binding, so I think I should bring it up.

Quoted 11 lines
	// The "original" `a` and `b`.
	let a: A = ...;
	let b: B = ...;

	try_pin_init!(Self {
	   a,                   // Initialize the field only.
	   let b <- b,          // Initialize the field and create a `&B` named `b`.
	   c: a.into(),         // That's the "original" `a`.
	   d <- D::new(b),      // Not the original `b`, but the pin-init one.
	})

Another idea is using &this:

try_pin_init!(&this in Self {

 	   a,                   // Initialize the field only.
 	   b <- b,              // Initialize the field only.
 	   c: a.into(),         // That's the "original" `a`.

d <- D::new(this->b), // Not the original b, but the pin-init one.
})

, like a special field projection during initialization.

Regards,
Boqun

Quoted 6 lines

This looks good to me as well. But I'm curious what the syntax would be like in the in-place placement language feature in the future.

Regards,
Boqun

author maintainer
#6 lore raw
Quoted 75 lines · Boqun Feng
Quoted 45 lines · Boqun Feng
Quoted 24 lines · Danilo Krummrich
Quoted 23 lines · Boqun Feng

On Fri, Sep 05, 2025 at 07:18:25PM +0200, Benno Lossin wrote:
[...]

samples/rust/rust_driver_pci.rs fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self> · line 82

Quoted 10 lines

index 606946ff4d7f..1ac0b06fa3b3 100644

samples/rust/rust_driver_pci.rs +1 -1
@@ -78,8 +78,8 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self>
 
         let drvdata = KBox::pin_init(
             try_pin_init!(Self {
-                pdev: pdev.into(),
                 bar <- pdev.iomap_region_sized::<{ Regs::END }>(0, c_str!("rust_driver_pci")),
+                pdev: pdev.into(),

Ok, this example is good enough for me to express the concern here: the variable shadowing behavior seems not straightforward (maybe because in normal Rust initalization expression, no binding is created for previous variables, neither do we have a let here).

Would the future inplace initialization have the similar behavior? I asked because a natural resolution is adding a special syntax like:

    let a = ..;
Quoted 9 lines
Quoted 5 lines
    try_pin_init!(Self {
        b: a,
	let a = a.into(); // create the new binding here.
	c: a, // <- use the previous initalized `a`.
    }

Can you please clarify the example? I'm a bit confused that this is not a field of Self, so currently this can just be written as:

Oh, I could have been more clear: a is a field of Self, and the let part initalizes it.

Quoted 7 lines
	try_pin_init!(Self {
	   b: a,
	   c: a.into,
	})

Of course assuming that a is Clone, as the code above does as well.

Quoted 19 lines
Quoted 2 lines

So, if we are concerned by the variable shadowing, which I'm less concerned about, maybe we can do this:

I'm not that concerned to block this, but it does look to me like we are inventing a new way (and even a different syntax because normal Rust initialization doesn't create new bindings) to create binding, so I think I should bring it up.

Quoted 11 lines
	// The "original" `a` and `b`.
	let a: A = ...;
	let b: B = ...;

	try_pin_init!(Self {
	   a,                   // Initialize the field only.
	   let b <- b,          // Initialize the field and create a `&B` named `b`.
	   c: a.into(),         // That's the "original" `a`.
	   d <- D::new(b),      // Not the original `b`, but the pin-init one.
	})

Another idea is using &this:

(quoted diff) +0 -0
 	try_pin_init!(&this in Self {
 	   a,                   // Initialize the field only.
 	   b <- b,              // Initialize the field only.
 	   c: a.into(),         // That's the "original" `a`.
 	   d <- D::new(this->b),      // Not the original `b`, but the pin-init one.
 	})
Quoted 1 line

, like a special field projection during initialization.

The main issue with new syntax is the difficulty of implementing it. The let one is fine, but it's pretty jarring & doesn't get formatted by rustfmt (which I want to eventually have). Using this does look better IMO, but it's near impossible to implement using declarative macros (even using syn it seems difficult to me). So either we find some way to express it in existing rust syntax (maybe use an attribute?), or we just keep it this way.

Maybe Gary has some ideas on how to implement it.

---
Cheers,
Benno

maintainer
#7 lore raw
Quoted 86 lines · Benno Lossin
Quoted 75 lines · Boqun Feng
Quoted 45 lines · Boqun Feng
Quoted 24 lines · Danilo Krummrich
Quoted 23 lines · Boqun Feng

On Fri, Sep 05, 2025 at 07:18:25PM +0200, Benno Lossin wrote:
[...]

samples/rust/rust_driver_pci.rs fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self> · line 82

Quoted 10 lines

index 606946ff4d7f..1ac0b06fa3b3 100644

samples/rust/rust_driver_pci.rs +1 -1
@@ -78,8 +78,8 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self>
 
         let drvdata = KBox::pin_init(
             try_pin_init!(Self {
-                pdev: pdev.into(),
                 bar <- pdev.iomap_region_sized::<{ Regs::END }>(0, c_str!("rust_driver_pci")),
+                pdev: pdev.into(),

Ok, this example is good enough for me to express the concern here: the variable shadowing behavior seems not straightforward (maybe because in normal Rust initalization expression, no binding is created for previous variables, neither do we have a let here).

Would the future inplace initialization have the similar behavior? I asked because a natural resolution is adding a special syntax like:

    let a = ..;
Quoted 9 lines
Quoted 5 lines
    try_pin_init!(Self {
        b: a,
	let a = a.into(); // create the new binding here.
	c: a, // <- use the previous initalized `a`.
    }

Can you please clarify the example? I'm a bit confused that this is not a field of Self, so currently this can just be written as:

Oh, I could have been more clear: a is a field of Self, and the let part initalizes it.

Quoted 7 lines
	try_pin_init!(Self {
	   b: a,
	   c: a.into,
	})

Of course assuming that a is Clone, as the code above does as well.

Quoted 19 lines
Quoted 2 lines

So, if we are concerned by the variable shadowing, which I'm less concerned about, maybe we can do this:

I'm not that concerned to block this, but it does look to me like we are inventing a new way (and even a different syntax because normal Rust initialization doesn't create new bindings) to create binding, so I think I should bring it up.

Quoted 11 lines
	// The "original" `a` and `b`.
	let a: A = ...;
	let b: B = ...;

	try_pin_init!(Self {
	   a,                   // Initialize the field only.
	   let b <- b,          // Initialize the field and create a `&B` named `b`.
	   c: a.into(),         // That's the "original" `a`.
	   d <- D::new(b),      // Not the original `b`, but the pin-init one.
	})

Another idea is using &this:

(quoted diff) +0 -0
 	try_pin_init!(&this in Self {
 	   a,                   // Initialize the field only.
 	   b <- b,              // Initialize the field only.
 	   c: a.into(),         // That's the "original" `a`.
 	   d <- D::new(this->b),      // Not the original `b`, but the pin-init one.
 	})
Quoted 1 line

, like a special field projection during initialization.

The main issue with new syntax is the difficulty of implementing it. The let one is fine, but it's pretty jarring & doesn't get formatted by rustfmt (which I want to eventually have). Using this does look better IMO, but it's near impossible to implement using declarative macros (even using syn it seems difficult to me). So either we find some way to express it in existing rust syntax (maybe use an attribute?), or we just keep it this way.

Quoted 1 line

Maybe Gary has some ideas on how to implement it.

I also thought about reusing this, but I think we should not reuse it. We still need it to get pointers to uninitialized fields.

Surely, we could say that we provide this.as_ptr() to get the NonNull this is currently defined to be and otherwise make it expose only the initialized fields for a certain scope.

But as you say, that sounds tricky to implement and is probably not very intuitive either. I'd rather say keep it as it is, if we don't want something like the let b <- b syntax I proposed for formatting reasons.

author maintainer
#8 lore raw
Quoted 87 lines · Danilo Krummrich
Quoted 86 lines · Benno Lossin
Quoted 75 lines · Boqun Feng
Quoted 45 lines · Boqun Feng
Quoted 24 lines · Danilo Krummrich
Quoted 23 lines · Boqun Feng

On Fri, Sep 05, 2025 at 07:18:25PM +0200, Benno Lossin wrote:
[...]

samples/rust/rust_driver_pci.rs fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self> · line 82

Quoted 10 lines

index 606946ff4d7f..1ac0b06fa3b3 100644

samples/rust/rust_driver_pci.rs +1 -1
@@ -78,8 +78,8 @@ fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> Result<Pin<KBox<Self>
 
         let drvdata = KBox::pin_init(
             try_pin_init!(Self {
-                pdev: pdev.into(),
                 bar <- pdev.iomap_region_sized::<{ Regs::END }>(0, c_str!("rust_driver_pci")),
+                pdev: pdev.into(),

Ok, this example is good enough for me to express the concern here: the variable shadowing behavior seems not straightforward (maybe because in normal Rust initalization expression, no binding is created for previous variables, neither do we have a let here).

Would the future inplace initialization have the similar behavior? I asked because a natural resolution is adding a special syntax like:

    let a = ..;
Quoted 9 lines
Quoted 5 lines
    try_pin_init!(Self {
        b: a,
	let a = a.into(); // create the new binding here.
	c: a, // <- use the previous initalized `a`.
    }

Can you please clarify the example? I'm a bit confused that this is not a field of Self, so currently this can just be written as:

Oh, I could have been more clear: a is a field of Self, and the let part initalizes it.

Quoted 7 lines
	try_pin_init!(Self {
	   b: a,
	   c: a.into,
	})

Of course assuming that a is Clone, as the code above does as well.

Quoted 19 lines
Quoted 2 lines

So, if we are concerned by the variable shadowing, which I'm less concerned about, maybe we can do this:

I'm not that concerned to block this, but it does look to me like we are inventing a new way (and even a different syntax because normal Rust initialization doesn't create new bindings) to create binding, so I think I should bring it up.

Quoted 11 lines
	// The "original" `a` and `b`.
	let a: A = ...;
	let b: B = ...;

	try_pin_init!(Self {
	   a,                   // Initialize the field only.
	   let b <- b,          // Initialize the field and create a `&B` named `b`.
	   c: a.into(),         // That's the "original" `a`.
	   d <- D::new(b),      // Not the original `b`, but the pin-init one.
	})

Another idea is using &this:

(quoted diff) +0 -0
 	try_pin_init!(&this in Self {
 	   a,                   // Initialize the field only.
 	   b <- b,              // Initialize the field only.
 	   c: a.into(),         // That's the "original" `a`.
 	   d <- D::new(this->b),      // Not the original `b`, but the pin-init one.
 	})
Quoted 1 line

, like a special field projection during initialization.

The main issue with new syntax is the difficulty of implementing it. The let one is fine, but it's pretty jarring & doesn't get formatted by rustfmt (which I want to eventually have). Using this does look better IMO, but it's near impossible to implement using declarative macros (even using syn it seems difficult to me). So either we find some way to express it in existing rust syntax (maybe use an attribute?), or we just keep it this way.

Quoted 8 lines
Quoted 1 line

Maybe Gary has some ideas on how to implement it.

I also thought about reusing this, but I think we should not reuse it. We still need it to get pointers to uninitialized fields.

Surely, we could say that we provide this.as_ptr() to get the NonNull this is currently defined to be and otherwise make it expose only the initialized fields for a certain scope.

I have some ideas of changing the syntax to be more closure-esque:

    init!(|this| -> Result<MyStruct, Error> {
        let x = 42;
        MyStruct {
            x,
        }
    })

There we could add another parameter, that would then serve this purpose. We should also probably rename this to slot & then use this for the initialized version.

But as I said before, implementing the this thing from a macro perspective is rather difficult (I have two ideas on how to do it and both are bad...).

Quoted 3 lines

But as you say, that sounds tricky to implement and is probably not very intuitive either. I'd rather say keep it as it is, if we don't want something like the let b <- b syntax I proposed for formatting reasons.

I don't feel like that's conveying the correct thing, it looks as if you are only declaring a local variable.

---
Cheers,
Benno

maintainer
#9 lore raw
Quoted 10 lines · Benno Lossin

On Sun Sep 7, 2025 at 7:29 PM CEST, Danilo Krummrich wrote:
I have some ideas of changing the syntax to be more closure-esque:

    init!(|this| -> Result<MyStruct, Error> {
        let x = 42;
        MyStruct {
            x,
        }
    })
Quoted 3 lines

There we could add another parameter, that would then serve this purpose. We should also probably rename this to slot & then use this for the initialized version.

I think that's a pretty good idea, but the part that I think is a little bit confusing remains: this will need to have different fields depending on where it's accessed.

Quoted 10 lines

But as I said before, implementing the this thing from a macro perspective is rather difficult (I have two ideas on how to do it and both are bad...).

Quoted 3 lines

But as you say, that sounds tricky to implement and is probably not very intuitive either. I'd rather say keep it as it is, if we don't want something like the let b <- b syntax I proposed for formatting reasons.

I don't feel like that's conveying the correct thing, it looks as if you are only declaring a local variable.

Yeah, it's not great, but given that it's a custom syntax it also does not create wrong expectations I'd say.

Anyways, I'm fine with either. For now we probably want to land the version as it is and revisit once you settle on the syntax rework you mentioned above.

author maintainer
#10 lore raw
Quoted 11 lines · Danilo Krummrich
Quoted 10 lines · Benno Lossin

On Sun Sep 7, 2025 at 7:29 PM CEST, Danilo Krummrich wrote:
I have some ideas of changing the syntax to be more closure-esque:

    init!(|this| -> Result<MyStruct, Error> {
        let x = 42;
        MyStruct {
            x,
        }
    })
Quoted 7 lines
Quoted 3 lines

There we could add another parameter, that would then serve this purpose. We should also probably rename this to slot & then use this for the initialized version.

I think that's a pretty good idea, but the part that I think is a little bit confusing remains: this will need to have different fields depending on where it's accessed.

Yeah (that's also the main issue with the macro implementation).

Quoted 8 lines
Quoted 8 lines

But as I said before, implementing the this thing from a macro perspective is rather difficult (I have two ideas on how to do it and both are bad...).

Quoted 3 lines

But as you say, that sounds tricky to implement and is probably not very intuitive either. I'd rather say keep it as it is, if we don't want something like the let b <- b syntax I proposed for formatting reasons.

Quoted 5 lines
Quoted 2 lines

I don't feel like that's conveying the correct thing, it looks as if you are only declaring a local variable.

Yeah, it's not great, but given that it's a custom syntax it also does not create wrong expectations I'd say.

I'd say it looks like combining the <- operation already used by the init! macro & a let binding. Thus introducing a local variable that's (pin) initialized in-place. Not a field of the current struct.

Quoted 2 lines

Anyways, I'm fine with either. For now we probably want to land the version as it is and revisit once you settle on the syntax rework you mentioned above.

I actually came up with a third option that looks best IMO:

    init!(MyStruct {
        x: 42,
        #[with_binding]
        y: 24,
        z: *y,
    })

The #[with_binding] attribute makes the macro generate a variable y. x & z don't give access to their value. (we of course should come up with a better name).

Any thoughts?

---
Cheers,
Benno

maintainer
#11 lore raw
Quoted 9 lines · Benno Lossin

I actually came up with a third option that looks best IMO:

    init!(MyStruct {
        x: 42,
        #[with_binding]
        y: 24,
        z: *y,
    })
Quoted 5 lines

The #[with_binding] attribute makes the macro generate a variable y. x & z don't give access to their value. (we of course should come up with a better name).

Any thoughts?

It may be a bit verbose is some cases, but it makes things pretty obvious, so LGTM.

How about just #[bind] or #[access]?

#12 lore raw
Quoted 14 lines · Danilo Krummrich
Quoted 13 lines · Benno Lossin

I actually came up with a third option that looks best IMO:

    init!(MyStruct {
        x: 42,
        #[with_binding]
        y: 24,
        z: *y,
    })

The #[with_binding] attribute makes the macro generate a variable y. x & z don't give access to their value. (we of course should come up with a better name).

Quoted 6 lines
Quoted 1 line

Any thoughts?

It may be a bit verbose is some cases, but it makes things pretty obvious, so LGTM.

How about just #[bind] or #[access]?

#[shadow] or #[maybe_rebind] ? Or #[pin_ref], the last one is clear about the purpose.

Regards,
Boqun

author maintainer
#13 lore raw
Quoted 15 lines · Boqun Feng
Quoted 14 lines · Danilo Krummrich
Quoted 13 lines · Benno Lossin

I actually came up with a third option that looks best IMO:

    init!(MyStruct {
        x: 42,
        #[with_binding]
        y: 24,
        z: *y,
    })

The #[with_binding] attribute makes the macro generate a variable y. x & z don't give access to their value. (we of course should come up with a better name).

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Any thoughts?

It may be a bit verbose is some cases, but it makes things pretty obvious, so LGTM.

How about just #[bind] or #[access]?

I like #[bind].

Quoted 2 lines

#[shadow] or #[maybe_rebind] ? Or #[pin_ref], the last one is clear about the purpose.

Hmm in init! it's never pinned.

---
Cheers,
Benno

maintainer
#14 lore raw
Quoted 21 lines · Benno Lossin
Quoted 20 lines · Boqun Feng
Quoted 19 lines · Danilo Krummrich
Quoted 9 lines · Benno Lossin

I actually came up with a third option that looks best IMO:

    init!(MyStruct {
        x: 42,
        #[with_binding]
        y: 24,
        z: *y,
    })
Quoted 5 lines

The #[with_binding] attribute makes the macro generate a variable y. x & z don't give access to their value. (we of course should come up with a better name).

Any thoughts?

It may be a bit verbose is some cases, but it makes things pretty obvious, so LGTM.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

How about just #[bind] or #[access]?

I like #[bind].

Quoted 2 lines

#[shadow] or #[maybe_rebind] ? Or #[pin_ref], the last one is clear about the purpose.

Hmm in init! it's never pinned.

I thought about #[shadow] as well, but it is not really accurate I think, as we might not shadow anything. #[maybe_rebind] sounds a bit like it conditionally rebinds, as in "it may not do anything", but it always binds.

So, I think it should one clear instruction, i.e. #[bind], #[access], #[ref], #[use], #[let], etc.

#15 lore raw
Quoted 29 lines · Danilo Krummrich
Quoted 28 lines · Benno Lossin
Quoted 15 lines · Boqun Feng
Quoted 14 lines · Danilo Krummrich
Quoted 13 lines · Benno Lossin

I actually came up with a third option that looks best IMO:

    init!(MyStruct {
        x: 42,
        #[with_binding]
        y: 24,
        z: *y,
    })

The #[with_binding] attribute makes the macro generate a variable y. x & z don't give access to their value. (we of course should come up with a better name).

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Any thoughts?

It may be a bit verbose is some cases, but it makes things pretty obvious, so LGTM.

How about just #[bind] or #[access]?

I like #[bind].

Quoted 2 lines

#[shadow] or #[maybe_rebind] ? Or #[pin_ref], the last one is clear about the purpose.

Quoted 8 lines
Quoted 1 line

Hmm in init! it's never pinned.

I thought about #[shadow] as well, but it is not really accurate I think, as we might not shadow anything. #[maybe_rebind] sounds a bit like it conditionally rebinds, as in "it may not do anything", but it always binds.

So, I think it should one clear instruction, i.e. #[bind], #[access], #[ref], #[use], #[let], etc.

In that sense I think #[let] is best? Because it indicates this field initialization works as a let-statement (in term of creating a new binding), of course I don't have strong ojections against other options.

Regards,
Boqun

maintainer
#16 lore raw
Quoted 7 lines · Boqun Feng
Quoted 2 lines · Danilo Krummrich

So, I think it should one clear instruction, i.e. #[bind], #[access], #[ref], #[use], #[let], etc.

In that sense I think #[let] is best? Because it indicates this field initialization works as a let-statement (in term of creating a new binding), of course I don't have strong ojections against other options.

Same for me, I'm fine with any of this kind. :)

author maintainer
#17 lore raw
In reply to Boqun Feng
Quoted 36 lines · Boqun Feng
Quoted 35 lines · Danilo Krummrich
Quoted 21 lines · Benno Lossin
Quoted 20 lines · Boqun Feng
Quoted 19 lines · Danilo Krummrich
Quoted 9 lines · Benno Lossin

I actually came up with a third option that looks best IMO:

    init!(MyStruct {
        x: 42,
        #[with_binding]
        y: 24,
        z: *y,
    })
Quoted 5 lines

The #[with_binding] attribute makes the macro generate a variable y. x & z don't give access to their value. (we of course should come up with a better name).

Any thoughts?

It may be a bit verbose is some cases, but it makes things pretty obvious, so LGTM.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

How about just #[bind] or #[access]?

I like #[bind].

Quoted 2 lines

#[shadow] or #[maybe_rebind] ? Or #[pin_ref], the last one is clear about the purpose.

Hmm in init! it's never pinned.

I thought about #[shadow] as well, but it is not really accurate I think, as we might not shadow anything. #[maybe_rebind] sounds a bit like it conditionally rebinds, as in "it may not do anything", but it always binds.

Quoted 6 lines
Quoted 2 lines

So, I think it should one clear instruction, i.e. #[bind], #[access], #[ref], #[use], #[let], etc.

In that sense I think #[let] is best? Because it indicates this field initialization works as a let-statement (in term of creating a new binding), of course I don't have strong ojections against other options.

Ultimately I decided to go with #[bind], since I felt like #[let] might be confused with just having a let statement (ie replacing the assignment with a let binding).

#[bind] also might be confused with some device binding I guess, but we can rename it's too bad.

---
Cheers,
Benno

#18 lore raw
In reply to Benno Lossin
Quoted 5 lines · Benno Lossin

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.

It's hard to review because of lack of examples. Could you or Danilo share some sample usages? Thanks!

Regards,
Boqun

Quoted 3 lines

---

[...]

maintainer
#19 lore raw

(Cc: Alex)

Quoted 9 lines · Boqun Feng
Quoted 5 lines · Benno Lossin

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.

It's hard to review because of lack of examples. Could you or Danilo share some sample usages? Thanks!

Sure, here's an example. Eventually, it's going to be a bit more complicated, but basically that's it.

	#[pin_data(PinnedDrop)]
	pub(crate) struct Gpu {
	    spec: Spec,
	    bar: Arc<Devres<Bar0>>,
	    sysmem_flush: SysmemFlush,
	    gsp_falcon: Falcon<Gsp>,
	    sec2_falcon: Falcon<Sec2>,
	    #[pin]
	    gsp: Gsp,
	}

	impl Gpu {
	    pub(crate) fn new(
	        dev: &Device<Bound>,
	        bar: Arc<Devres<Bar0>>,
	    ) -> impl PinInit<Self, Error> + '_ {
	        try_pin_init(Self {
	            bar,
	            spec: Spec::new(bar.access(dev)?)?,
	            gsp_falcon: Falcon::<Gsp>::new(dev, spec.chipset)?,
	            sec2_falcon: Falcon::<Sec2>::new(dev, spec.chipset)?,
	            sysmem_flush: SysmemFlush::register(dev, bar.access(dev)?, spec.chipset)?
	            gsp <- Gsp::new(gsp_falcon, sec2_falcon, sysmem_flush)?,
	        })
	    }
	}

Imagine how much unsafe pointer mess this needs without this patch. :)

#20 lore raw
In reply to Benno Lossin
kernel test robot noticed the following build errors:

Hi Benno,

kernel test robot noticed the following build errors:

[auto build test ERROR on 8f5ae30d69d7543eee0d70083daf4de8fe15d585]

url:    https://github.com/intel-lab-lkp/linux/commits/Benno-Lossin/rust-pin-init-add-references-to-previously-initialized-fields/20250905-220242
base:   8f5ae30d69d7543eee0d70083daf4de8fe15d585
patch link:    https://lore.kernel.org/r/20250905140047.3325945-1-lossin%40kernel.org
patch subject: [PATCH] rust: pin-init: add references to previously initialized fields
config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20250906/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250906/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> error[E0308]: mismatched types
   --> rust/kernel/devres.rs:154:66
   |
   154 |                     bindings::devm_add_action(dev.as_raw(), Some(callback), inner.cast())
   |                                                             ---- ^^^^^^^^ expected fn pointer, found `&mut unsafe extern "C" fn(*mut c_void)`
   |                                                             |
   |                                                             arguments to this enum variant are incorrect
   |
   = note:     expected fn pointer `unsafe extern "C" fn(_)`
   found mutable reference `&mut unsafe extern "C" fn(_)`
   help: the type constructed contains `&mut unsafe extern "C" fn(*mut ffi::c_void)` due to the type of the argument passed
   --> rust/kernel/devres.rs:154:61
   |
   154 |                     bindings::devm_add_action(dev.as_raw(), Some(callback), inner.cast())
   |                                                             ^^^^^--------^
   |                                                                  |
   |                                                                  this argument influences the type of `Some`

note: tuple variant defined here
--> /opt/cross/rustc-1.88.0-bindgen-0.72.0/rustup/toolchains/1.88.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:597:5 |
597 | Some(#[stable(feature = "rust1", since = "1.0.0")] T),
| ^^^^
help: consider dereferencing the borrow
|
154 | bindings::devm_add_action(dev.as_raw(), Some(*callback), inner.cast())
| +

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer Applied
#21 lore raw
In reply to Benno Lossin
Quoted 7 lines · Benno Lossin

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.

Applied to pin-init-next, thanks everyone!

Included the fixes to devres & rust_driver_pci.

---
Cheers,
Benno

#22 lore raw
In reply to Benno Lossin
Quoted 4 lines · Benno Lossin

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.

Janne

author maintainer
#23 lore raw
Quoted 33 lines · Janne Grunau
Quoted 4 lines · Benno Lossin

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.

Quoted 7 lines

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

#24 lore raw
Quoted 37 lines · Benno Lossin
Quoted 36 lines · Janne Grunau
Quoted 4 lines · Benno Lossin

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.

Quoted 8 lines
Quoted 4 lines

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.

I spoke too soon. The packed struct issue is also present in the capture driver for Macbook microphones (aop_audio). Working around this issue there is less obvious and more effort. I think it might be enough to use unaligned u32 / u64 types already present in asahi [1].

I'm not sure how prevalent packed structs are outside of Apple's firmware interfaces. I was surprised running into the same issue in a second driver but I shouldn't have been. There are plans for another driver where this isssue will be present.

A workaround on pin-init side would be appreciated. Due to the nature of these packed structs I do not see a need to have access to previously initialized fields. An optional way to supress the references would be good enough for the cases I'm aware off.

Thanks

Janne

1: https://github.com/AsahiLinux/linux/blob/asahi-6.17.9-1/drivers/gpu/drm/asahi/fw/types.rs#L48

author maintainer
#25 lore raw
Quoted 57 lines · Janne Grunau
Quoted 37 lines · Benno Lossin
Quoted 36 lines · Janne Grunau
Quoted 4 lines · Benno Lossin

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.

Quoted 8 lines
Quoted 4 lines

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.

I spoke too soon. The packed struct issue is also present in the capture driver for Macbook microphones (aop_audio). Working around this issue there is less obvious and more effort. I think it might be enough to use unaligned u32 / u64 types already present in asahi [1].

I'm not sure how prevalent packed structs are outside of Apple's firmware interfaces. I was surprised running into the same issue in a second driver but I shouldn't have been. There are plans for another driver where this isssue will be present.

Quoted 4 lines

A workaround on pin-init side would be appreciated. Due to the nature of these packed structs I do not see a need to have access to previously initialized fields. An optional way to supress the references would be good enough for the cases I'm aware off.

FYI, I am merging a workaround this cycle, see [1]. Gary had a good idea for a future patch series which I am tracking in [2].

Cheers,
Benno

[1]: https://lore.kernel.org/all/[email protected]
[2]: https://github.com/Rust-for-Linux/pin-init/issues/98

author maintainer
#26 lore raw
Quoted 58 lines · Benno Lossin
Quoted 57 lines · Janne Grunau
Quoted 37 lines · Benno Lossin
Quoted 36 lines · Janne Grunau
Quoted 4 lines · Benno Lossin

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.

Quoted 8 lines
Quoted 4 lines

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.

I spoke too soon. The packed struct issue is also present in the capture driver for Macbook microphones (aop_audio). Working around this issue there is less obvious and more effort. I think it might be enough to use unaligned u32 / u64 types already present in asahi [1].

I'm not sure how prevalent packed structs are outside of Apple's firmware interfaces. I was surprised running into the same issue in a second driver but I shouldn't have been. There are plans for another driver where this isssue will be present.

Quoted 7 lines
Quoted 4 lines

A workaround on pin-init side would be appreciated. Due to the nature of these packed structs I do not see a need to have access to previously initialized fields. An optional way to supress the references would be good enough for the cases I'm aware off.

FYI, I am merging a workaround this cycle, see [1]. Gary had a good idea for a future patch series which I am tracking in [2].

Update on this situation, we have a problem: packed struct and the current version of pin-init are unsound, so I'll sadly have to remove the current workaround attribute. That is because the [Pin]Init trait requires an aligned pointer as the input. Fixing this requires introducing a new hierarchy for the Init trait that supports misaligned writes.

If you really require this feature, then we can work something out. Would you mind giving me a pointer to the code that you're currently using or that you would like to support?

Cheers,
Benno