rust: std_vendor: add `likely()`, `unlikely()` and `cold_path()`
Miguel Ojeda
maintainerPATCH v3 1/2 rust: std_vendor: add `likely()`, `unlikely()` and `cold_path()`
{un,}likely() are commonly used macros on the C side -- there are more than 25k occurrences in the tree. Rust is likely going to provide functions (not macros) for the same purpose: core::hint::{un,}likely(). Currently they are unstable under feature(likely_unlikely) [1].
For instance, given:
fn f1(a: i32) -> i32 {
if a < 0 {
return 123;
}
42
}
fn f2(a: i32) -> i32 {
if likely(a < 0) {
return 124;
}
42
}
fn f3(a: i32) -> i32 {
if unlikely(a < 0) {
return 125;
}
42
}LLVM emits code equivalent to the one it would for similar C functions:
f1:
test %edi,%edi
mov $0x7b,%ecx
mov $0x2a,%eax
cmovs %ecx,%eax
ret
f2:
mov $0x7c,%eax
test %edi,%edi
/-- jns <f2+0xa>
| ret
\-> mov $0x2a,%eax
ret
f3:
mov $0x2a,%eax
test %edi,%edi
/-- js <f3+0xa>
| ret
\-> mov $0x7d,%eax
retThus vendor (essentially) the core library code to provide the same API (so that we may use core's directly in the future).
We do so by implementing {un,}likely() like core's intrinsics do (to save a layer) and based on calling our own cold_path() instead of using core's (which is stable since 1.95.0 [2][3]).
It turns out that simply using #[cold] to emulate cold_path(), suggested by Gary [4], works in the cases I tested across all supported versions so far, so we can avoid the complexity of having one more Kconfig and conditionally enabling the feature in callers (since cold_path() was added in Rust 1.86.0 [5]).
We could optionally call cold_path() for Rust >= 1.86.0 within our own cold_path(), but it seems that it is not needed, and anyway since Rust 1.98.0 cold_path() is itself #[cold] so that it works even if the MIR inliner can't inline it [6].
Even if we find that it is not optimal in some cases, it can be improved, and meanwhile we will have the means to annotate these cases already.
Apart from that, the only differences with core's version are the usual changes (pr_*! calls and their \n, imports, attributes, lint cleanup), plus a fix for a polarity typo (I sent a fix upstream [7]) and removal of unneeded code documentation.
In addition, add them to the prelude (inlined, so that the documentation is rendered at least there, until we can use core's).
---
v3: I used Gary's #[cold] suggestion, and thus removed the Kconfig. I rebased and made sure the code is up to date with core, tested a bunch of things, compared again with C and the assembly generated, found a strange behavior on Clang I will report separately, a polarity typo in core (fixed here) and inlined the docs in the prelude to get it rendered somewhere until we bump the MSRV. I think the potential issue I found in v2's discussion may have been with -Os, but it happens the same way with core's, so it does not matter.
Alice, Gary: I removed your Reviewed-by's just in case. It is pretty similar to last time, but the commit message is reworded, and the commit is simpler.
rust/kernel/prelude.rs | 7 ++ rust/kernel/std_vendor.rs | 136 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+)
rust/kernel/prelude.rs +7 -0
@@ -133,3 +133,10 @@// `super::std_vendor` is hidden, which makes the macro inline for some reason. #[doc(no_inline)] pub use super ::dbg; + +#[doc(inline)] +pub use super ::std_vendor::{ + cold_path, + likely, + unlikely,// +};
rust/kernel/std_vendor.rs +136 -0
139 lines not on this page · raw message
base-commit: 7c306f00835a237a70024a8f968dbc3bc8db8fcf --
2.56.0
Miguel Ojeda
maintainerPATCH v3 2/2 rust: bug: hint `warn_on!` conditions as unlikely
The C side WARN_ON() macro hints the compiler that its condition is unlikely to be true, both for the branch that reports the warning and for the value the macro evaluates to, so that callers such as:
if (WARN_ON(x))
return -EINVAL;get the warning path laid out of line.
Now that unlikely() is available, do the same in warn_on!.
--- rust/kernel/bug.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
rust/kernel/bug.rs +2 -2
@@ -147,12 +147,12 @@ macro_rules! warn_on {#[cfg(not( CONFIG_DEBUG_BUGVERBOSE_DETAILED ))] const COND_STR : &str =file! (); -if cond { +if $crate::prelude::unlikely (cond) {const WARN_ON_FLAGS :u32 = $crate::bug::bugflag_taint ($crate::bindings::TAINT_WARN ); $crate::warn_flags!(COND_STR ,WARN_ON_FLAGS ); } - cond + $crate::prelude::unlikely (cond) }}; }
Signature · 1 line
2.56.0
Re: [PATCH v3 2/2] rust: bug: hint `warn_on!` conditions as unlikely
Quoted 11 lines · On Fri, 16 Oct 2026 18:24:46 +0200 Miguel Ojeda <[email protected]> wrote:
The C side
WARN_ON()macro hints the compiler that its condition is unlikely to be true, both for the branch that reports the warning and for the value the macro evaluates to, so that callers such as:if (WARN_ON(x)) return -EINVAL;get the warning path laid out of line.
Now that
unlikely()is available, do the same inwarn_on!.
Quoted 7 lines
Suggested-by Gary Guo <[email protected]>Assisted-by LLMSigned-off-by Miguel Ojeda <[email protected]>--- rust/kernel/bug.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 85 lines · Miguel Ojeda
{un,}likely()are commonly used macros on the C side -- there are more than 25k occurrences in the tree. Rust is likely going to provide functions (not macros) for the same purpose:core::hint::{un,}likely(). Currently they are unstable underfeature(likely_unlikely)[1].For instance, given:
fn f1(a: i32) -> i32 { if a < 0 { return 123; } 42 } fn f2(a: i32) -> i32 { if likely(a < 0) { return 124; } 42 } fn f3(a: i32) -> i32 { if unlikely(a < 0) { return 125; } 42 }LLVM emits code equivalent to the one it would for similar C functions:
f1: test %edi,%edi mov $0x7b,%ecx mov $0x2a,%eax cmovs %ecx,%eax ret f2: mov $0x7c,%eax test %edi,%edi /-- jns <f2+0xa> | ret \-> mov $0x2a,%eax ret f3: mov $0x2a,%eax test %edi,%edi /-- js <f3+0xa> | ret \-> mov $0x7d,%eax retThus vendor (essentially) the
corelibrary code to provide the same API (so that we may usecore's directly in the future).We do so by implementing
{un,}likely()likecore'sintrinsicsdo (to save a layer) and based on calling our owncold_path()instead of usingcore's (which is stable since 1.95.0 [2][3]).It turns out that simply using
#[cold]to emulatecold_path(), suggested by Gary [4], works in the cases I tested across all supported versions so far, so we can avoid the complexity of having one moreKconfigand conditionally enabling the feature in callers (sincecold_path()was added in Rust 1.86.0 [5]).We could optionally call
cold_path()for Rust >= 1.86.0 within our owncold_path(), but it seems that it is not needed, and anyway since Rust 1.98.0cold_path()is itself#[cold]so that it works even if the MIR inliner can't inline it [6].Even if we find that it is not optimal in some cases, it can be improved, and meanwhile we will have the means to annotate these cases already.
Apart from that, the only differences with
core's version are the usual changes (pr_*!calls and their\n, imports, attributes, lint cleanup), plus a fix for a polarity typo (I sent a fix upstream [7]) and removal of unneeded code documentation.In addition, add them to the prelude (inlined, so that the documentation is rendered at least there, until we can use
core's).
Quoted 9 lines
Assisted-by LLMSigned-off-by Miguel Ojeda <[email protected]>
Quoted 17 lines
---
v3: I used Gary's#[cold]suggestion, and thus removed theKconfig. I rebased and made sure the code is up to date withcore, tested a bunch of things, compared again with C and the assembly generated, found a strange behavior on Clang I will report separately, a polarity typo incore(fixed here) and inlined the docs in the prelude to get it rendered somewhere until we bump the MSRV. I think the potential issue I found in v2's discussion may have been with-Os, but it happens the same way withcore's, so it does not matter.Alice, Gary: I removed your Reviewed-by's just in case. It is pretty similar to last time, but the commit message is reworded, and the commit is simpler.
rust/kernel/prelude.rs | 7 ++ rust/kernel/std_vendor.rs | 136 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+)
Quoted 4 lines · On Fri, 16 Oct 2026 18:24:45 +0200 Miguel Ojeda <[email protected]> wrote:
{un,}likely()are commonly used macros on the C side -- there are more than 25k occurrences in the tree. Rust is likely going to provide functions (not macros) for the same purpose:core::hint::{un,}likely(). Currently they are unstable underfeature(likely_unlikely)[1].
[...]
Quoted 1 line
Signed-off-by Miguel Ojeda <[email protected]>