Rust for Linux List
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Onur Özkan" <work@onurozkan.dev>, "Gary Guo" <gary@kernel.org>
Cc: "Benno Lossin" <lossin@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: pin_init: internal: use `loop {}` to produce never value
Date: Sat, 09 May 2026 13:15:38 +0100	[thread overview]
Message-ID: <DIE4ZW566U0Y.MNANAA8G8W6C@garyguo.net> (raw)
In-Reply-To: <20260509064905.536777-1-work@onurozkan.dev>

On Sat May 9, 2026 at 7:48 AM BST, Onur Özkan wrote:
> On Fri, 08 May 2026 16:29:49 +0100
> Gary Guo <gary@kernel.org> wrote:
>
>> From: Gary Guo <gary@garyguo.net>
>> 
>> In the `init!`/`pin_init!` macros, we rely on a trick that assigns never
>> (`!`) values to all mentioned fields in never-executed code to let the
>> compiler check that all fields have been initialized.
>> 
>> Currently we use `::core::panic!()` to produce this value, but before Rust
>> 1.91.0, it creates outlined `panic_cold_explicit` functions which do not
>> get removed by the optimizer, thus leaving dead code behind in the binary.
>> This has been fixed by [1], which lands in Rust 1.91.0+, higher than the
>> kernel minimum version 1.85.0.
>> 
>> This causes ~200 dead `panic_cold_explicit` instances being included in the
>> binary, with ~90 of them from nova-core's usage of pin-init.
>> 
>> Work around the issue by using `loop {}` which creates the never value
>> without macro expansion or function call at all. All instances of
>> `panic_cold_explicit` outside libcore are removed by this change in my
>> kernel build.
>> 
>> Link: https://github.com/rust-lang/rust/pull/145304 [1]
>> Signed-off-by: Gary Guo <gary@garyguo.net>
>> ---
>>  rust/pin-init/internal/src/init.rs | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/rust/pin-init/internal/src/init.rs b/rust/pin-init/internal/src/init.rs
>> index fbc8286263b2..b1cb2e53ee6f 100644
>> --- a/rust/pin-init/internal/src/init.rs
>> +++ b/rust/pin-init/internal/src/init.rs
>> @@ -338,7 +338,7 @@ fn make_field_check(
>>              ::core::ptr::write(slot, #path {
>>                  #(
>>                      #(#field_attrs)*
>> -                    #field_name: ::core::panic!(),
>> +                    #field_name: loop {},
>>                  )*
>>                  #zeroing_trailer
>>              })
>> 
>> -- 
>> 2.51.2
>> 
>
> I don't know where this patch is based on (on current upstream there's no
> zeroing_trailer thing), but at least on the upstream side there's another use of
> ::core::panic in the same module.

zeroing_trailer is introduced in
https://lore.kernel.org/rust-for-linux/20260427-pin-init-fix-v3-0-496a699674dd@garyguo.net/
which is currently in rust-fixes.

Best,
Gary

      reply	other threads:[~2026-05-09 12:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-08 15:29 [PATCH] rust: pin_init: internal: use `loop {}` to produce never value Gary Guo
2026-05-09  6:48 ` Onur Özkan
2026-05-09 12:15   ` Gary Guo [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DIE4ZW566U0Y.MNANAA8G8W6C@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=gary@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox