mirror-linux/rust
Gary Guo 68bf102226 rust: pin-init: fix incorrect accessor reference lifetime
When a field has been initialized, `init!`/`pin_init!` create a reference
or pinned reference to the field so it can be accessed later during the
initialization of other fields. However, the reference it created is
incorrectly `&'static` rather than just the scope of the initializer.

This means that you can do

    init!(Foo {
        a: 1,
        _: {
            let b: &'static u32 = a;
        }
    })

which is unsound.

This is caused by `&mut (*#slot).#ident`, which actually allows arbitrary
lifetime, so this is effectively `'static`. Somewhat ironically, the safety
justification of creating the accessor is.. "SAFETY: TODO".

Fix it by adding `let_binding` method on `DropGuard` to shorten lifetime.
This results in exactly what we want for these accessors. The safety and
invariant comments of `DropGuard` have been reworked; instead of reasoning
about what caller can do with the guard, express it in a way that the
ownership is transferred to the guard and `forget` takes it back, so the
unsafe operations within the `DropGuard` can be more easily justified.

Fixes: 42415d163e ("rust: pin-init: add references to previously initialized fields")
Cc: stable@vger.kernel.org
Signed-off-by: Gary Guo <gary@garyguo.net>
Link: https://patch.msgid.link/20260427-pin-init-fix-v3-2-496a699674dd@garyguo.net
[ Reworded for missing word. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2026-04-30 22:43:32 +02:00
..
bindings drm for v7.1-rc1 2026-04-15 08:45:00 -07:00
helpers drm for v7.1-rc1 2026-04-15 08:45:00 -07:00
kernel Char/Misc/IIO/and others driver updates for 7.1-rc1 2026-04-24 13:23:50 -07:00
macros rust: macros: simplify code using `feature(extract_if)` 2026-04-07 10:00:24 +02:00
pin-init rust: pin-init: fix incorrect accessor reference lifetime 2026-04-30 22:43:32 +02:00
proc-macro2 rust: proc-macro2: rebuild if the version text changes 2026-01-23 10:44:27 +01:00
quote rust: quote: add `README.md` 2025-11-24 17:15:43 +01:00
syn rust: syn: add `README.md` 2025-11-24 17:15:47 +01:00
uapi rust: declare cfi_encoding for lru_status 2026-04-07 10:00:25 +02:00
.gitignore rust: support running Rust documentation tests as KUnit ones 2023-07-19 09:32:53 -06:00
.kunitconfig rust: add kunitconfig 2025-03-19 23:22:21 +01:00
Makefile rust: arch: um: Fix building 32-bit UML with GCC 2026-04-30 22:40:57 +02:00
bindgen_parameters rust: declare cfi_encoding for lru_status 2026-04-07 10:00:25 +02:00
build_error.rs rust: add `build_error` crate 2022-12-04 01:59:16 +01:00
compiler_builtins.rs ARM: 9441/1: rust: Enable Rust support for ARMv7 2025-03-26 13:31:16 +00:00
exports.c kbuild: rust: provide an option to inline C helpers into Rust 2026-03-30 02:03:52 +02:00
ffi.rs rust: replace `CStr` with `core::ffi::CStr` 2025-10-22 07:47:27 +02:00