Skip to content

Commit aed9373

Browse files
BennoLossingregkh
authored andcommitted
rust: pin-init: internal: init: document load-bearing fact of field accessors
[ Upstream commit 580cc37 ] The functions `[Pin]Init::__[pinned_]init` and `ptr::write` called from the `init!` macro require the passed pointer to be aligned. This fact is ensured by the creation of field accessors to previously initialized fields. Since we missed this very important fact from the beginning [1], document it in the code. Link: https://rust-for-linux.zulipchat.com/#narrow/channel/561532-pin-init/topic/initialized.20field.20accessor.20detection/with/576210658 [1] Fixes: 90e53c5 ("rust: add pin-init API core") Cc: <stable@vger.kernel.org> # 6.6.y, 6.12.y: 42415d1: rust: pin-init: add references to previously initialized fields Cc: <stable@vger.kernel.org> # 6.6.y, 6.12.y, 6.18.y, 6.19.y Signed-off-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Gary Guo <gary@garyguo.net> Link: https://patch.msgid.link/20260302140424.4097655-2-lossin@kernel.org [ Updated Cc: stable@ tags as discussed. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org> [ Moved changes to the declarative macro, because 6.19.y and earlier do not have `syn`. Also duplicated the comment for all field accessor creations. - Benno ] Signed-off-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 1b247cd commit aed9373

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

rust/pin-init/src/macros.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,10 @@ macro_rules! __init_internal {
13121312
// return when an error/panic occurs.
13131313
// We also use the `data` to require the correct trait (`Init` or `PinInit`) for `$field`.
13141314
unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), init)? };
1315+
// NOTE: the field accessor ensures that the initialized field is properly aligned.
1316+
// Unaligned fields will cause the compiler to emit E0793. We do not support
1317+
// unaligned fields since `Init::__init` requires an aligned pointer; the call to
1318+
// `ptr::write` below has the same requirement.
13151319
// SAFETY:
13161320
// - the project function does the correct field projection,
13171321
// - the field has been initialized,
@@ -1351,6 +1355,10 @@ macro_rules! __init_internal {
13511355
// return when an error/panic occurs.
13521356
unsafe { $crate::Init::__init(init, ::core::ptr::addr_of_mut!((*$slot).$field))? };
13531357

1358+
// NOTE: the field accessor ensures that the initialized field is properly aligned.
1359+
// Unaligned fields will cause the compiler to emit E0793. We do not support
1360+
// unaligned fields since `Init::__init` requires an aligned pointer; the call to
1361+
// `ptr::write` below has the same requirement.
13541362
// SAFETY:
13551363
// - the field is not structurally pinned, since the line above must compile,
13561364
// - the field has been initialized,
@@ -1391,6 +1399,10 @@ macro_rules! __init_internal {
13911399
unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) };
13921400
}
13931401

1402+
// NOTE: the field accessor ensures that the initialized field is properly aligned.
1403+
// Unaligned fields will cause the compiler to emit E0793. We do not support
1404+
// unaligned fields since `Init::__init` requires an aligned pointer; the call to
1405+
// `ptr::write` below has the same requirement.
13941406
#[allow(unused_variables)]
13951407
// SAFETY:
13961408
// - the field is not structurally pinned, since no `use_data` was required to create this
@@ -1431,6 +1443,10 @@ macro_rules! __init_internal {
14311443
// SAFETY: The memory at `slot` is uninitialized.
14321444
unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) };
14331445
}
1446+
// NOTE: the field accessor ensures that the initialized field is properly aligned.
1447+
// Unaligned fields will cause the compiler to emit E0793. We do not support
1448+
// unaligned fields since `Init::__init` requires an aligned pointer; the call to
1449+
// `ptr::write` below has the same requirement.
14341450
// SAFETY:
14351451
// - the project function does the correct field projection,
14361452
// - the field has been initialized,

0 commit comments

Comments
 (0)