Skip to contentSkip to conversation

pin-init upstream sync for v7.0

PATCH Applied Benno Lossin 2 participants 13 messages Mar 19 – Mar 26

Subsystems (2)
lore
Share & tools
lore

Benno Lossin

author maintainer
#0 lore raw

cover pin-init upstream sync for v7.0

This cycle we have no big changes in pin-init. This series is a collection of smaller improvements already merged upstream:

- https://github.com/Rust-for-Linux/pin-init/pull/105
- https://github.com/Rust-for-Linux/pin-init/pull/110
- https://github.com/Rust-for-Linux/pin-init/pull/101
- https://github.com/Rust-for-Linux/pin-init/pull/102
- https://github.com/Rust-for-Linux/pin-init/pull/108

Next cycle might contain bigger changes, mainly support for tuple structs. And I'm also thinking of already starting with integrating zerocopy.

While testing for the kernel, I noticed the following issue (already fixed in the patch in this series):

- https://github.com/Rust-for-Linux/pin-init/pull/123

This cycle we also had some unsoundness fixes that are already in rc4:

- https://github.com/Rust-for-Linux/pin-init/pull/111
- https://github.com/Rust-for-Linux/pin-init/pull/117

Lastly, there were some upstream-only changes:

- https://github.com/Rust-for-Linux/pin-init/pull/114
- https://github.com/Rust-for-Linux/pin-init/pull/109

Cheers,
Benno

Antonio Hickey (1):
rust: pin-init: replace addr_of_mut! with &raw mut

Benno Lossin (1):
rust: pin-init: properly document let binding workaround

Gary Guo (2):
rust: pin-init: build: simplify use of nightly features
rust: pin-init: doc: de-clutter documentation with fake-variadics

Hamdan-Khan (1):
rust: pin-init: implement ZeroableOption for NonZero* integer types

 rust/Makefile                                 |  4 +-
 rust/pin-init/README.md                       |  3 +-
 rust/pin-init/examples/big_struct_in_place.rs |  3 +
 rust/pin-init/examples/linked_list.rs         |  3 +-
 rust/pin-init/examples/mutex.rs               |  3 +-
 rust/pin-init/examples/pthread_mutex.rs       |  3 +-
 rust/pin-init/examples/static_init.rs         |  3 +-
 rust/pin-init/internal/src/init.rs            | 14 +++-
 rust/pin-init/internal/src/lib.rs             |  2 +-
 rust/pin-init/src/lib.rs                      | 73 +++++++++++++------
 10 files changed, 75 insertions(+), 36 deletions(-)

base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/5 rust: pin-init: build: simplify use of nightly features

From: Gary Guo <[email protected]>

We use some features that are already stable in later versions of Rust, but only available as unstable features in older Rust versions that the kernel needs to support.

Instead of checking if a feature is already stable, simply enable them and allow the warning if the feature is already stable. This avoids the need of hardcoding whether a feature has been stabilized at a given version.

#[feature(...)] is used when cfg USE_RUSTC_FEATURES is enabled. The build script automatically does this when a nightly compiler is detected or RUSTC_BOOTSTRAP is set.

[ Added kernel build system changes to always enable USE_RUSTC_FEATURES.
Moved this commit earlier (swapped with the next one) to avoid a build error. - Benno ]

---
 rust/Makefile                           | 4 ++--
 rust/pin-init/examples/linked_list.rs   | 2 +-
 rust/pin-init/examples/mutex.rs         | 2 +-
 rust/pin-init/examples/pthread_mutex.rs | 2 +-
 rust/pin-init/examples/static_init.rs   | 2 +-
 rust/pin-init/internal/src/lib.rs       | 2 +-
 rust/pin-init/src/lib.rs                | 7 ++-----
 7 files changed, 9 insertions(+), 12 deletions(-)
rust/Makefile +2 -2
index 9801af2e1e02..e92daeb3542b 100644
@@ -118,7 +118,7 @@ syn-flags := \
     $(call cfgs-to-flags,$(syn-cfgs))
 
 pin_init_internal-cfgs := \
-    kernel
+    kernel USE_RUSTC_FEATURES
 
 pin_init_internal-flags := \
     --extern proc_macro2 \
@@ -127,7 +127,7 @@ pin_init_internal-flags := \
     $(call cfgs-to-flags,$(pin_init_internal-cfgs))
 
 pin_init-cfgs := \
-    kernel
+    kernel USE_RUSTC_FEATURES
 
 pin_init-flags := \
     --extern pin_init_internal \
rust/pin-init/examples/linked_list.rs +1 -1
index 8445a5890cb7..226e33e4a957 100644
@@ -2,7 +2,7 @@
 
 #![allow(clippy::undocumented_unsafe_blocks)]
 #![cfg_attr(feature = "alloc", feature(allocator_api))]
-#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
 
 use core::{
     cell::Cell,
rust/pin-init/examples/mutex.rs +1 -1
index 9f295226cd64..e534c367f644 100644
@@ -2,7 +2,7 @@
 
 #![allow(clippy::undocumented_unsafe_blocks)]
 #![cfg_attr(feature = "alloc", feature(allocator_api))]
-#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
 #![allow(clippy::missing_safety_doc)]
 
 use core::{
rust/pin-init/examples/pthread_mutex.rs +1 -1
index 4e082ec7d5de..562ca5d3d08c 100644
@@ -3,7 +3,7 @@
 // inspired by <https://github.com/nbdd0121/pin-init/blob/trunk/examples/pthread_mutex.rs>
 #![allow(clippy::undocumented_unsafe_blocks)]
 #![cfg_attr(feature = "alloc", feature(allocator_api))]
-#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
 
 #[cfg(not(windows))]
 mod pthread_mtx {
rust/pin-init/examples/static_init.rs +1 -1
index 0e165daa9798..df562134a53c 100644
@@ -2,7 +2,7 @@
 
 #![allow(clippy::undocumented_unsafe_blocks)]
 #![cfg_attr(feature = "alloc", feature(allocator_api))]
-#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
 #![allow(unused_imports)]
 
 use core::{
rust/pin-init/internal/src/lib.rs +1 -1
index 08372c8f65f0..b08dfe003031 100644
@@ -6,7 +6,7 @@
 
 //! `pin-init` proc macros.
 
-#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
 // Documentation is done in the pin-init crate instead.
 #![allow(missing_docs)]
 
rust/pin-init/src/lib.rs +2 -5
index fe4c85ae3f02..b1de166b5626 100644
@@ -264,12 +264,9 @@
 //! [`impl Init<T, E>`]: crate::Init
 //! [Rust-for-Linux]: https://rust-for-linux.com/
 
-#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
 #![cfg_attr(
-    all(
-        any(feature = "alloc", feature = "std"),
-        not(RUSTC_NEW_UNINIT_IS_STABLE)
-    ),
+    all(any(feature = "alloc", feature = "std"), USE_RUSTC_FEATURES),
     feature(new_uninit)
 )]
 #![forbid(missing_docs, unsafe_op_in_unsafe_fn)]
Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH 2/5 rust: pin-init: properly document let binding workaround

In reply to Benno Lossin

The three let bindings (in the bodies of cast_init, cast_pin_init and the init! macro) are used to avoid the following compiler error in Rust 1.78.0, 1.79.0, 1.80.0, 1.80.1, and 1.81.0 (just showing the one for cast_init, the others are similar):

    error[E0391]: cycle detected when computing type of opaque `cast_init::{opaque#0}`
        --> src/lib.rs:1160:66
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         |                                                                  ^^^^^^^^^^^^^^^
         |
    note: ...which requires borrow-checking `cast_init`...
        --> src/lib.rs:1160:1
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    note: ...which requires const checking `cast_init`...
        --> src/lib.rs:1160:1
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         = note: ...which requires computing whether `cast_init::{opaque#0}` is freeze...
         = note: ...which requires evaluating trait selection obligation `cast_init::{opaque#0}: core::marker::Freeze`...
         = note: ...which again requires computing type of opaque `cast_init::{opaque#0}`, completing the cycle
    note: cycle used when computing type of `cast_init::{opaque#0}`
        --> src/lib.rs:1160:66
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         |                                                                  ^^^^^^^^^^^^^^^
         = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

Once we raise the nightly-MSRV above 1.81, we can remove this workaround.

[ Moved this commit after the previous one to avoid a build failure due
to unstable features. Changed the cfg to use USE_RUSTC_FEAUTURES.
- Benno ]

---
 rust/pin-init/examples/big_struct_in_place.rs |  2 ++
 rust/pin-init/internal/src/init.rs            |  6 ++++++
 rust/pin-init/src/lib.rs                      | 18 ++++++++++++------
 3 files changed, 20 insertions(+), 6 deletions(-)
rust/pin-init/examples/big_struct_in_place.rs +2 -0
index c05139927486..de8612a5e27d 100644
@@ -1,5 +1,7 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
+#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
+
 use pin_init::*;
 
 // Struct with size over 1GiB
rust/pin-init/internal/src/init.rs +6 -0
index 2fe918f4d82a..c1c9400b090a 100644
@@ -173,6 +173,12 @@ fn assert_zeroable<T: ?::core::marker::Sized>(_: *mut T)
         };
         // SAFETY: TODO
         let init = unsafe { ::pin_init::#init_from_closure::<_, #error>(init) };
+        // FIXME: this let binding is required to avoid a compiler error (cycle when computing the
+        // opaque type returned by this function) before Rust 1.81. Remove after MSRV bump.
+        #[allow(
+            clippy::let_and_return,
+            reason = "some clippy versions warn about the let binding"
+        )]
         init
     }})
 }
rust/pin-init/src/lib.rs +12 -6
index b1de166b5626..a513930ee01a 100644
@@ -1144,9 +1144,12 @@ unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
     // SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
     // requirements.
     let res = unsafe { pin_init_from_closure(|ptr: *mut U| init.__pinned_init(ptr.cast::<T>())) };
-    // FIXME: remove the let statement once the nightly-MSRV allows it (1.78 otherwise encounters a
-    // cycle when computing the type returned by this function)
-    #[allow(clippy::let_and_return)]
+    // FIXME: this let binding is required to avoid a compiler error (cycle when computing the opaque
+    // type returned by this function) before Rust 1.81. Remove after MSRV bump.
+    #[allow(
+        clippy::let_and_return,
+        reason = "some clippy versions warn about the let binding"
+    )]
     res
 }
 
@@ -1160,9 +1163,12 @@ unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
     // SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
     // requirements.
     let res = unsafe { init_from_closure(|ptr: *mut U| init.__init(ptr.cast::<T>())) };
-    // FIXME: remove the let statement once the nightly-MSRV allows it (1.78 otherwise encounters a
-    // cycle when computing the type returned by this function)
-    #[allow(clippy::let_and_return)]
+    // FIXME: this let binding is required to avoid a compiler error (cycle when computing the opaque
+    // type returned by this function) before Rust 1.81. Remove after MSRV bump.
+    #[allow(
+        clippy::let_and_return,
+        reason = "some clippy versions warn about the let binding"
+    )]
     res
 }
 
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 2/5
#3 lore raw

Re: [PATCH 2/5] rust: pin-init: properly document let binding workaround

Quoted 31 lines · Benno Lossin

The three let bindings (in the bodies of cast_init, cast_pin_init and the init! macro) are used to avoid the following compiler error in Rust 1.78.0, 1.79.0, 1.80.0, 1.80.1, and 1.81.0 (just showing the one for cast_init, the others are similar):

    error[E0391]: cycle detected when computing type of opaque `cast_init::{opaque#0}`
        --> src/lib.rs:1160:66
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         |                                                                  ^^^^^^^^^^^^^^^
         |
    note: ...which requires borrow-checking `cast_init`...
        --> src/lib.rs:1160:1
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    note: ...which requires const checking `cast_init`...
        --> src/lib.rs:1160:1
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         = note: ...which requires computing whether `cast_init::{opaque#0}` is freeze...
         = note: ...which requires evaluating trait selection obligation `cast_init::{opaque#0}: core::marker::Freeze`...
         = note: ...which again requires computing type of opaque `cast_init::{opaque#0}`, completing the cycle
    note: cycle used when computing type of `cast_init::{opaque#0}`
        --> src/lib.rs:1160:66
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         |                                                                  ^^^^^^^^^^^^^^^
         = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
Quoted 8 lines

Once we raise the nightly-MSRV above 1.81, we can remove this workaround.

[ Moved this commit after the previous one to avoid a build failure due
to unstable features. Changed the cfg to use USE_RUSTC_FEAUTURES.
- Benno ]

Perhaps we should import review approvals on GitHub as R-bs.

Best,
Gary

Quoted 5 lines
---
 rust/pin-init/examples/big_struct_in_place.rs |  2 ++
 rust/pin-init/internal/src/init.rs            |  6 ++++++
 rust/pin-init/src/lib.rs                      | 18 ++++++++++++------
 3 files changed, 20 insertions(+), 6 deletions(-)
author maintainer
#4 lore raw

Re: [PATCH 2/5] rust: pin-init: properly document let binding workaround

Quoted 41 lines · Gary Guo
Quoted 31 lines · Benno Lossin

The three let bindings (in the bodies of cast_init, cast_pin_init and the init! macro) are used to avoid the following compiler error in Rust 1.78.0, 1.79.0, 1.80.0, 1.80.1, and 1.81.0 (just showing the one for cast_init, the others are similar):

    error[E0391]: cycle detected when computing type of opaque `cast_init::{opaque#0}`
        --> src/lib.rs:1160:66
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         |                                                                  ^^^^^^^^^^^^^^^
         |
    note: ...which requires borrow-checking `cast_init`...
        --> src/lib.rs:1160:1
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    note: ...which requires const checking `cast_init`...
        --> src/lib.rs:1160:1
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         = note: ...which requires computing whether `cast_init::{opaque#0}` is freeze...
         = note: ...which requires evaluating trait selection obligation `cast_init::{opaque#0}: core::marker::Freeze`...
         = note: ...which again requires computing type of opaque `cast_init::{opaque#0}`, completing the cycle
    note: cycle used when computing type of `cast_init::{opaque#0}`
        --> src/lib.rs:1160:66
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         |                                                                  ^^^^^^^^^^^^^^^
         = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
Quoted 8 lines

Once we raise the nightly-MSRV above 1.81, we can remove this workaround.

[ Moved this commit after the previous one to avoid a build failure due
to unstable features. Changed the cfg to use USE_RUSTC_FEAUTURES.
- Benno ]

Quoted 3 lines

Perhaps we should import review approvals on GitHub as R-bs.

Yeah we should. A quick internet search didn't reveal a magic tool to me. Do you know of any?

Cheers,
Benno

maintainer
#5 lore raw

Re: [PATCH 2/5] rust: pin-init: properly document let binding workaround

Quoted 42 lines · Benno Lossin
Quoted 41 lines · Gary Guo
Quoted 39 lines · Benno Lossin

The three let bindings (in the bodies of cast_init, cast_pin_init and the init! macro) are used to avoid the following compiler error in Rust 1.78.0, 1.79.0, 1.80.0, 1.80.1, and 1.81.0 (just showing the one for cast_init, the others are similar):

    error[E0391]: cycle detected when computing type of opaque `cast_init::{opaque#0}`
        --> src/lib.rs:1160:66
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         |                                                                  ^^^^^^^^^^^^^^^
         |
    note: ...which requires borrow-checking `cast_init`...
        --> src/lib.rs:1160:1
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    note: ...which requires const checking `cast_init`...
        --> src/lib.rs:1160:1
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         = note: ...which requires computing whether `cast_init::{opaque#0}` is freeze...
         = note: ...which requires evaluating trait selection obligation `cast_init::{opaque#0}: core::marker::Freeze`...
         = note: ...which again requires computing type of opaque `cast_init::{opaque#0}`, completing the cycle
    note: cycle used when computing type of `cast_init::{opaque#0}`
        --> src/lib.rs:1160:66
         |
    1160 | pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
         |                                                                  ^^^^^^^^^^^^^^^
         = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

Once we raise the nightly-MSRV above 1.81, we can remove this workaround.

[ Moved this commit after the previous one to avoid a build failure due
to unstable features. Changed the cfg to use USE_RUSTC_FEAUTURES.
- Benno ]

Quoted 6 lines
Quoted 3 lines

Perhaps we should import review approvals on GitHub as R-bs.

Yeah we should. A quick internet search didn't reveal a magic tool to me. Do you know of any?

One idea that I have:
* Re-target a PR to a temporary branch and merge it
* Have a GitHub action that walks through PR in staging branch and applies tagging before putting it into the main branch.

Fighting with branch protection rule might be needed:
https://github.com/orgs/community/discussions/13836

Best,
Gary

author maintainer
#6 lore raw

PATCH 3/5 rust: pin-init: doc: de-clutter documentation with fake-variadics

In reply to Benno Lossin

From: Gary Guo <[email protected]>

Currently the doc for Zeroable and ZeroableOption are filled with the generated impl of tuples and fn pointers. Use the internal "fake_variadics" feature to improve the rendered quality.

This makes use of an internal feature, however this is of minimal risk as it's for documentation only, not activated during normal build, gated behind USE_RUSTC_FEATURES, and can be removed at any time. This feature is already used by serde and bevy to improve documentation quality.

For compilers that cannot use this feature, we still hide most generated impls, and the existence of them are hinted by doc comments on the single non-hidden impl.

[ Reordered #[doc] attributes and safety comments to avoid errors in
older versions of clippy. - Benno ]

---
 rust/pin-init/src/lib.rs | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
rust/pin-init/src/lib.rs +18 -1
index a513930ee01a..7e79f75089df 100644
@@ -276,6 +276,8 @@
     all(feature = "unsafe-pinned", CONFIG_RUSTC_HAS_UNSAFE_PINNED),
     feature(unsafe_pinned)
 )]
+#![cfg_attr(all(USE_RUSTC_FEATURES, doc), allow(internal_features))]
+#![cfg_attr(all(USE_RUSTC_FEATURES, doc), feature(rustdoc_internals))]
 
 use core::{
     cell::UnsafeCell,
@@ -1638,8 +1640,14 @@ macro_rules! impl_zeroable {
 }
 
 macro_rules! impl_tuple_zeroable {
-    ($(,)?) => {};
+    ($first:ident, $(,)?) => {
+        #[cfg_attr(all(USE_RUSTC_FEATURES, doc), doc(fake_variadic))]
+        /// Implemented for tuples up to 10 items long.
+        // SAFETY: All elements are zeroable and padding can be zero.
+        unsafe impl<$first: Zeroable> Zeroable for ($first,) {}
+    };
     ($first:ident, $($t:ident),* $(,)?) => {
+        #[cfg_attr(doc, doc(hidden))]
         // SAFETY: All elements are zeroable and padding can be zero.
         unsafe impl<$first: Zeroable, $($t: Zeroable),*> Zeroable for ($first, $($t),*) {}
         impl_tuple_zeroable!($($t),* ,);
@@ -1654,7 +1662,16 @@ macro_rules! impl_fn_zeroable_option {
         $(impl_fn_zeroable_option!({unsafe extern $abi} $args);)*
     };
     ({$($prefix:tt)*} {$(,)?}) => {};
+    ({$($prefix:tt)*} {$ret:ident, $arg:ident $(,)?}) => {
+        #[cfg_attr(all(USE_RUSTC_FEATURES, doc), doc(fake_variadic))]
+        /// Implemented for function pointers with up to 20 arity.
+        // SAFETY: function pointers are part of the option layout optimization:
+        // <https://doc.rust-lang.org/stable/std/option/index.html#representation>.
+        unsafe impl<$ret, $arg> ZeroableOption for $($prefix)* fn($arg) -> $ret {}
+        impl_fn_zeroable_option!({$($prefix)*} {$arg,});
+    };
     ({$($prefix:tt)*} {$ret:ident, $($rest:ident),* $(,)?}) => {
+        #[cfg_attr(doc, doc(hidden))]
         // SAFETY: function pointers are part of the option layout optimization:
         // <https://doc.rust-lang.org/stable/std/option/index.html#representation>.
         unsafe impl<$ret, $($rest),*> ZeroableOption for $($prefix)* fn($($rest),*) -> $ret {}
Signature · 1 line
2.53.0
author maintainer
#7 lore raw

PATCH 4/5 rust: pin-init: implement ZeroableOption for NonZero* integer types

In reply to Benno Lossin

From: Hamdan-Khan <[email protected]>

Add a macro for implementing ZeroableOption for NonZero* types.

Option<NonZero*> now automatically implements Zeroable trait by implementing ZeroableOption for NonZero* types, which serves as a blanket impl.

---
 rust/pin-init/src/lib.rs | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
rust/pin-init/src/lib.rs +14 -7
index 7e79f75089df..d09e7fe97eae 100644
@@ -1615,13 +1615,6 @@ macro_rules! impl_zeroable {
     // SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`.
     {<T: ?Sized + Zeroable>} UnsafeCell<T>,
 
-    // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee:
-    // <https://doc.rust-lang.org/stable/std/option/index.html#representation>).
-    Option<NonZeroU8>, Option<NonZeroU16>, Option<NonZeroU32>, Option<NonZeroU64>,
-    Option<NonZeroU128>, Option<NonZeroUsize>,
-    Option<NonZeroI8>, Option<NonZeroI16>, Option<NonZeroI32>, Option<NonZeroI64>,
-    Option<NonZeroI128>, Option<NonZeroIsize>,
-
     // SAFETY: `null` pointer is valid.
     //
     // We cannot use `T: ?Sized`, since the VTABLE pointer part of fat pointers is not allowed to be
@@ -1681,6 +1674,20 @@ unsafe impl<$ret, $($rest),*> ZeroableOption for $($prefix)* fn($($rest),*) -> $
 
 impl_fn_zeroable_option!(["Rust", "C"] { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U });
 
+macro_rules! impl_non_zero_int_zeroable_option {
+    ($($int:ty),* $(,)?) => {
+        // SAFETY: Safety comment written in the macro invocation.
+        $(unsafe impl ZeroableOption for $int {})*
+    };
+}
+
+impl_non_zero_int_zeroable_option! {
+    // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee:
+    // <https://doc.rust-lang.org/stable/std/option/index.html#representation>).
+    NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
+    NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize,
+}
+
 /// This trait allows creating an instance of `Self` which contains exactly one
 /// [structurally pinned value](https://doc.rust-lang.org/std/pin/index.html#projections-and-structural-pinning).
 ///
Signature · 1 line
2.53.0
author maintainer
#8 lore raw

PATCH 5/5 rust: pin-init: replace `addr_of_mut!` with `&raw mut`

In reply to Benno Lossin

From: Antonio Hickey <[email protected]>

feature(raw_ref_op) became stable in Rust 1.82.0 which is the current MSRV of pin-init with no default features. Earlier Rust versions will now need to enable raw_ref_op to continue to work with pin-init.

This reduces visual complexity and improves consistency with existing reference syntax.

---
 rust/pin-init/README.md                       | 3 +--
 rust/pin-init/examples/big_struct_in_place.rs | 1 +
 rust/pin-init/examples/linked_list.rs         | 1 +
 rust/pin-init/examples/mutex.rs               | 1 +
 rust/pin-init/examples/pthread_mutex.rs       | 1 +
 rust/pin-init/examples/static_init.rs         | 1 +
 rust/pin-init/internal/src/init.rs            | 8 ++++----
 rust/pin-init/src/lib.rs                      | 8 ++++----
 8 files changed, 14 insertions(+), 10 deletions(-)
rust/pin-init/README.md +1 -2
index 6cee6ab1eb57..9095d6661ff6 100644
@@ -160,7 +160,6 @@ actually does the initialization in the correct way. Here are the things to look
 ```rust
 use pin_init::{pin_data, pinned_drop, PinInit, PinnedDrop, pin_init_from_closure};
 use core::{
-    ptr::addr_of_mut,
     marker::PhantomPinned,
     cell::UnsafeCell,
     pin::Pin,
@@ -199,7 +198,7 @@ impl RawFoo {
         unsafe {
             pin_init_from_closure(move |slot: *mut Self| {
                 // `slot` contains uninit memory, avoid creating a reference.
-                let foo = addr_of_mut!((*slot).foo);
+                let foo = &raw mut (*slot).foo;
                 let foo = UnsafeCell::raw_get(foo).cast::<bindings::foo>();
 
                 // Initialize the `foo`
rust/pin-init/examples/big_struct_in_place.rs +1 -0
index de8612a5e27d..80f89b5f8fd6 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: Apache-2.0 OR MIT
 
 #![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
 
 use pin_init::*;
 
rust/pin-init/examples/linked_list.rs +1 -0
index 226e33e4a957..119169e4dc41 100644
@@ -3,6 +3,7 @@
 #![allow(clippy::undocumented_unsafe_blocks)]
 #![cfg_attr(feature = "alloc", feature(allocator_api))]
 #![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
 
 use core::{
     cell::Cell,
rust/pin-init/examples/mutex.rs +1 -0
index e534c367f644..d53671f0edb8 100644
@@ -3,6 +3,7 @@
 #![allow(clippy::undocumented_unsafe_blocks)]
 #![cfg_attr(feature = "alloc", feature(allocator_api))]
 #![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
 #![allow(clippy::missing_safety_doc)]
 
 use core::{
rust/pin-init/examples/pthread_mutex.rs +1 -0
index 562ca5d3d08c..f3b5cc9b7134 100644
@@ -4,6 +4,7 @@
 #![allow(clippy::undocumented_unsafe_blocks)]
 #![cfg_attr(feature = "alloc", feature(allocator_api))]
 #![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
 
 #[cfg(not(windows))]
 mod pthread_mtx {
rust/pin-init/examples/static_init.rs +1 -0
index df562134a53c..f7e53d1a5ae6 100644
@@ -3,6 +3,7 @@
 #![allow(clippy::undocumented_unsafe_blocks)]
 #![cfg_attr(feature = "alloc", feature(allocator_api))]
 #![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
 #![allow(unused_imports)]
 
 use core::{
rust/pin-init/internal/src/init.rs +4 -4
index c1c9400b090a..daa3f1c6466e 100644
@@ -270,7 +270,7 @@ fn init_fields(
                     {
                         #value_prep
                         // SAFETY: TODO
-                        unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
+                        unsafe { #write(&raw mut (*#slot).#ident, #value_ident) };
                     }
                     #(#cfgs)*
                     #[allow(unused_variables)]
@@ -293,7 +293,7 @@ fn init_fields(
                             //   return when an error/panic occurs.
                             // - We also use `#data` to require the correct trait (`Init` or `PinInit`)
                             //   for `#ident`.
-                            unsafe { #data.#ident(::core::ptr::addr_of_mut!((*#slot).#ident), #init)? };
+                            unsafe { #data.#ident(&raw mut (*#slot).#ident, #init)? };
                         },
                         quote! {
                             // SAFETY: TODO
@@ -308,7 +308,7 @@ fn init_fields(
                             unsafe {
                                 ::pin_init::Init::__init(
                                     #init,
-                                    ::core::ptr::addr_of_mut!((*#slot).#ident),
+                                    &raw mut (*#slot).#ident,
                                 )?
                             };
                         },
@@ -348,7 +348,7 @@ fn init_fields(
                 // SAFETY: We forget the guard later when initialization has succeeded.
                 let #guard = unsafe {
                     ::pin_init::__internal::DropGuard::new(
-                        ::core::ptr::addr_of_mut!((*slot).#ident)
+                        &raw mut (*slot).#ident
                     )
                 };
             });
rust/pin-init/src/lib.rs +4 -4
index d09e7fe97eae..64eec095c859 100644
@@ -172,7 +172,6 @@
 //! # #![feature(extern_types)]
 //! use pin_init::{pin_data, pinned_drop, PinInit, PinnedDrop, pin_init_from_closure};
 //! use core::{
-//!     ptr::addr_of_mut,
 //!     marker::PhantomPinned,
 //!     cell::UnsafeCell,
 //!     pin::Pin,
@@ -211,7 +210,7 @@
 //!         unsafe {
 //!             pin_init_from_closure(move |slot: *mut Self| {
 //!                 // `slot` contains uninit memory, avoid creating a reference.
-//!                 let foo = addr_of_mut!((*slot).foo);
+//!                 let foo = &raw mut (*slot).foo;
 //!                 let foo = UnsafeCell::raw_get(foo).cast::<bindings::foo>();
 //!
 //!                 // Initialize the `foo`
@@ -265,6 +264,7 @@
 //! [Rust-for-Linux]: https://rust-for-linux.com/
 
 #![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
+#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
 #![cfg_attr(
     all(any(feature = "alloc", feature = "std"), USE_RUSTC_FEATURES),
     feature(new_uninit)
@@ -754,7 +754,7 @@ macro_rules! stack_try_pin_init {
 ///
 /// ```rust
 /// # use pin_init::*;
-/// # use core::{ptr::addr_of_mut, marker::PhantomPinned};
+/// # use core::marker::PhantomPinned;
 /// #[pin_data]
 /// #[derive(Zeroable)]
 /// struct Buf {
@@ -768,7 +768,7 @@ macro_rules! stack_try_pin_init {
 /// let init = pin_init!(&this in Buf {
 ///     buf: [0; 64],
 ///     // SAFETY: TODO.
-///     ptr: unsafe { addr_of_mut!((*this.as_ptr()).buf).cast() },
+///     ptr: unsafe { (&raw mut (*this.as_ptr()).buf).cast() },
 ///     pin: PhantomPinned,
 /// });
 /// let init = pin_init!(Buf {
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 5/5
#9 lore raw

Re: [PATCH 5/5] rust: pin-init: replace `addr_of_mut!` with `&raw mut`

Quoted 9 lines · Benno Lossin

From: Antonio Hickey <[email protected]>

feature(raw_ref_op) became stable in Rust 1.82.0 which is the current MSRV of pin-init with no default features. Earlier Rust versions will now need to enable raw_ref_op to continue to work with pin-init.

This reduces visual complexity and improves consistency with existing reference syntax.

Quoted 7 lines

This version looks okay to me. Why is the linked upstream version adding RUSTC_RAW_REF_OP_IS_STABLE cfg?

Best,
Gary

Quoted 10 lines
---
 rust/pin-init/README.md                       | 3 +--
 rust/pin-init/examples/big_struct_in_place.rs | 1 +
 rust/pin-init/examples/linked_list.rs         | 1 +
 rust/pin-init/examples/mutex.rs               | 1 +
 rust/pin-init/examples/pthread_mutex.rs       | 1 +
 rust/pin-init/examples/static_init.rs         | 1 +
 rust/pin-init/internal/src/init.rs            | 8 ++++----
 rust/pin-init/src/lib.rs                      | 8 ++++----
 8 files changed, 14 insertions(+), 10 deletions(-)
author maintainer
#10 lore raw

Re: [PATCH 5/5] rust: pin-init: replace `addr_of_mut!` with `&raw mut`

Quoted 18 lines · Gary Guo
Quoted 9 lines · Benno Lossin

From: Antonio Hickey <[email protected]>

feature(raw_ref_op) became stable in Rust 1.82.0 which is the current MSRV of pin-init with no default features. Earlier Rust versions will now need to enable raw_ref_op to continue to work with pin-init.

This reduces visual complexity and improves consistency with existing reference syntax.

Quoted 7 lines
Quoted 4 lines

This version looks okay to me. Why is the linked upstream version adding RUSTC_RAW_REF_OP_IS_STABLE cfg?

Ah good catch. That's only in the build.rs file, but it's not needed any more, I'll remove it.

Cheers,
Benno

author maintainer
#11 lore raw
In reply to Benno Lossin
Quoted 58 lines · Benno Lossin

This cycle we have no big changes in pin-init. This series is a collection of smaller improvements already merged upstream:

- https://github.com/Rust-for-Linux/pin-init/pull/105
- https://github.com/Rust-for-Linux/pin-init/pull/110
- https://github.com/Rust-for-Linux/pin-init/pull/101
- https://github.com/Rust-for-Linux/pin-init/pull/102
- https://github.com/Rust-for-Linux/pin-init/pull/108

Next cycle might contain bigger changes, mainly support for tuple structs. And I'm also thinking of already starting with integrating zerocopy.

While testing for the kernel, I noticed the following issue (already fixed in the patch in this series):

- https://github.com/Rust-for-Linux/pin-init/pull/123

This cycle we also had some unsoundness fixes that are already in rc4:

- https://github.com/Rust-for-Linux/pin-init/pull/111
- https://github.com/Rust-for-Linux/pin-init/pull/117

Lastly, there were some upstream-only changes:

- https://github.com/Rust-for-Linux/pin-init/pull/114
- https://github.com/Rust-for-Linux/pin-init/pull/109

Cheers,
Benno

Antonio Hickey (1):
rust: pin-init: replace addr_of_mut! with &raw mut

Benno Lossin (1):
rust: pin-init: properly document let binding workaround

Gary Guo (2):
rust: pin-init: build: simplify use of nightly features
rust: pin-init: doc: de-clutter documentation with fake-variadics

Hamdan-Khan (1):
rust: pin-init: implement ZeroableOption for NonZero* integer types

 rust/Makefile                                 |  4 +-
 rust/pin-init/README.md                       |  3 +-
 rust/pin-init/examples/big_struct_in_place.rs |  3 +
 rust/pin-init/examples/linked_list.rs         |  3 +-
 rust/pin-init/examples/mutex.rs               |  3 +-
 rust/pin-init/examples/pthread_mutex.rs       |  3 +-
 rust/pin-init/examples/static_init.rs         |  3 +-
 rust/pin-init/internal/src/init.rs            | 14 +++-
 rust/pin-init/internal/src/lib.rs             |  2 +-
 rust/pin-init/src/lib.rs                      | 73 +++++++++++++------
 10 files changed, 75 insertions(+), 36 deletions(-)
Quoted 1 line

base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c

The title is of course wrong, it's supposed to be v7.1.

Cheers,
Benno

author maintainer Applied
#12 lore raw
In reply to Benno Lossin
Quoted 38 lines · Benno Lossin

This cycle we have no big changes in pin-init. This series is a collection of smaller improvements already merged upstream:

- https://github.com/Rust-for-Linux/pin-init/pull/105
- https://github.com/Rust-for-Linux/pin-init/pull/110
- https://github.com/Rust-for-Linux/pin-init/pull/101
- https://github.com/Rust-for-Linux/pin-init/pull/102
- https://github.com/Rust-for-Linux/pin-init/pull/108

Next cycle might contain bigger changes, mainly support for tuple structs. And I'm also thinking of already starting with integrating zerocopy.

While testing for the kernel, I noticed the following issue (already fixed in the patch in this series):

- https://github.com/Rust-for-Linux/pin-init/pull/123

This cycle we also had some unsoundness fixes that are already in rc4:

- https://github.com/Rust-for-Linux/pin-init/pull/111
- https://github.com/Rust-for-Linux/pin-init/pull/117

Lastly, there were some upstream-only changes:

- https://github.com/Rust-for-Linux/pin-init/pull/114
- https://github.com/Rust-for-Linux/pin-init/pull/109

Cheers,
Benno

Antonio Hickey (1):
rust: pin-init: replace addr_of_mut! with &raw mut

Benno Lossin (1):
rust: pin-init: properly document let binding workaround

Quoted 6 lines

Gary Guo (2):
rust: pin-init: build: simplify use of nightly features
rust: pin-init: doc: de-clutter documentation with fake-variadics

Hamdan-Khan (1):
rust: pin-init: implement ZeroableOption for NonZero* integer types

Applied to pin-init-next yesterday, thanks everyone for contributing!

Cheers,
Benno

Quoted 14 lines
 rust/Makefile                                 |  4 +-
 rust/pin-init/README.md                       |  3 +-
 rust/pin-init/examples/big_struct_in_place.rs |  3 +
 rust/pin-init/examples/linked_list.rs         |  3 +-
 rust/pin-init/examples/mutex.rs               |  3 +-
 rust/pin-init/examples/pthread_mutex.rs       |  3 +-
 rust/pin-init/examples/static_init.rs         |  3 +-
 rust/pin-init/internal/src/init.rs            | 14 +++-
 rust/pin-init/internal/src/lib.rs             |  2 +-
 rust/pin-init/src/lib.rs                      | 73 +++++++++++++------
 10 files changed, 75 insertions(+), 36 deletions(-)

base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c