Skip to contentSkip to conversation

rust: pin_init: internal: use `loop {}` to produce never value

PATCH Applied Gary Guo 3 participants 4 messages May 8 – May 29

Subsystems (2)
lore
Share & tools
lore

Gary Guo

author
#0 lore raw

PATCH 1/1 rust: pin_init: internal: use `loop {}` to produce never value

From: Gary Guo <[email protected]>

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.

---
 rust/pin-init/internal/src/init.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/pin-init/internal/src/init.rs +1 -1
index fbc8286263b2..b1cb2e53ee6f 100644
@@ -338,7 +338,7 @@ fn make_field_check(
             ::core::ptr::write(slot, #path {
                 #(
                     #(#field_attrs)*
-                    #field_name: ::core::panic!(),
+                    #field_name: loop {},
                 )*
                 #zeroing_trailer
             })
Signature · 1 line
2.51.2
reviewer
#1 lore raw
Quoted 43 lines · On Fri, 08 May 2026 16:29:49 +0100 Gary Guo <[email protected]> wrote:

From: Gary Guo <[email protected]>

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.

---
 rust/pin-init/internal/src/init.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/pin-init/internal/src/init.rs +1 -1
index fbc8286263b2..b1cb2e53ee6f 100644
@@ -338,7 +338,7 @@ fn make_field_check(
             ::core::ptr::write(slot, #path {
                 #(
                     #(#field_attrs)*
-                    #field_name: ::core::panic!(),
+                    #field_name: loop {},
                 )*
                 #zeroing_trailer
             })
Signature · 2 lines
-- 
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.

-Onur

maintainer
#2 lore raw
Quoted 43 lines · Onur Özkan
Quoted 40 lines · On Fri, 08 May 2026 16:29:49 +0100 Gary Guo <[email protected]> wrote:

From: Gary Guo <[email protected]>

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.

---
 rust/pin-init/internal/src/init.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/pin-init/internal/src/init.rs +1 -1
index fbc8286263b2..b1cb2e53ee6f 100644
@@ -338,7 +338,7 @@ fn make_field_check(
             ::core::ptr::write(slot, #path {
                 #(
                     #(#field_attrs)*
-                    #field_name: ::core::panic!(),
+                    #field_name: loop {},
                 )*
                 #zeroing_trailer
             })
Quoted 7 lines
Quoted 3 lines
Signature · 1 line
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/[email protected]/
which is currently in rust-fixes.

Best,
Gary

maintainer Applied
#3 lore raw
In reply to Gary Guo
Quoted 15 lines · Gary Guo

From: Gary Guo <[email protected]>

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.

Quoted 7 lines

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.

Applied to pin-init-next.

Best,
Gary

Quoted 17 lines
---
 rust/pin-init/internal/src/init.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/pin-init/internal/src/init.rs +1 -1
index fbc8286263b2..b1cb2e53ee6f 100644
@@ -338,7 +338,7 @@ fn make_field_check(
             ::core::ptr::write(slot, #path {
                 #(
                     #(#field_attrs)*
-                    #field_name: ::core::panic!(),
+                    #field_name: loop {},
                 )*
                 #zeroing_trailer
             })