[PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments

Alexandre Courbot posted 7 patches 1 week, 4 days ago
[PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Posted by Alexandre Courbot 1 week, 4 days ago
`build_assert` relies on the compiler to optimize out its error path.
Functions using it with its arguments must thus always be inlined,
otherwise the error path of `build_assert` might not be optimized out,
triggering a build error.

Cc: stable@vger.kernel.org
Fixes: c6af9a1191d0 ("rust: cpufreq: Extend abstractions for driver registration")
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 rust/kernel/cpufreq.rs | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs
index f968fbd22890..0879a79485f8 100644
--- a/rust/kernel/cpufreq.rs
+++ b/rust/kernel/cpufreq.rs
@@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> {
         ..pin_init::zeroed()
     };
 
+    // Always inline to optimize out error path of `build_assert`.
+    #[inline(always)]
     const fn copy_name(name: &'static CStr) -> [c_char; CPUFREQ_NAME_LEN] {
         let src = name.to_bytes_with_nul();
         let mut dst = [0; CPUFREQ_NAME_LEN];

-- 
2.52.0
Re: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Posted by Gary Guo 1 week, 3 days ago
On Mon, 08 Dec 2025 11:47:01 +0900
Alexandre Courbot <acourbot@nvidia.com> wrote:

> `build_assert` relies on the compiler to optimize out its error path.
> Functions using it with its arguments must thus always be inlined,
> otherwise the error path of `build_assert` might not be optimized out,
> triggering a build error.
> 
> Cc: stable@vger.kernel.org
> Fixes: c6af9a1191d0 ("rust: cpufreq: Extend abstractions for driver registration")
> Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/cpufreq.rs | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs
> index f968fbd22890..0879a79485f8 100644
> --- a/rust/kernel/cpufreq.rs
> +++ b/rust/kernel/cpufreq.rs
> @@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> {
>          ..pin_init::zeroed()
>      };
>  
> +    // Always inline to optimize out error path of `build_assert`.
> +    #[inline(always)]
>      const fn copy_name(name: &'static CStr) -> [c_char; CPUFREQ_NAME_LEN] {
>          let src = name.to_bytes_with_nul();
>          let mut dst = [0; CPUFREQ_NAME_LEN];
> 

This change is not needed as this is a private function only used in
const-eval only.

I wonder if I should add another macro to assert that the function is
only used in const eval instead? Do you think it might be useful to have
something like:

	#[const_only]
	const fn foo() {}

or

	const fn foo() {
	    const_only!();
	}

? If so, I can send a patch that adds this feature. 

Implementation-wise, this will behave similar to build_error, where a
function is going to be added that is never-linked but has a body for
const eval.

Best,
Gary
Re: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Posted by Viresh Kumar 4 days, 6 hours ago
On Mon, 08 Dec 2025 11:47:01 +0900
Alexandre Courbot <acourbot@nvidia.com> wrote:

> `build_assert` relies on the compiler to optimize out its error path.
> Functions using it with its arguments must thus always be inlined,
> otherwise the error path of `build_assert` might not be optimized out,
> triggering a build error.
> 
> Cc: stable@vger.kernel.org
> Fixes: c6af9a1191d0 ("rust: cpufreq: Extend abstractions for driver registration")
> Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  rust/kernel/cpufreq.rs | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs
> index f968fbd22890..0879a79485f8 100644
> --- a/rust/kernel/cpufreq.rs
> +++ b/rust/kernel/cpufreq.rs
> @@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> {
>          ..pin_init::zeroed()
>      };
>  
> +    // Always inline to optimize out error path of `build_assert`.
> +    #[inline(always)]
>      const fn copy_name(name: &'static CStr) -> [c_char; CPUFREQ_NAME_LEN] {
>          let src = name.to_bytes_with_nul();
>          let mut dst = [0; CPUFREQ_NAME_LEN];
> 
 
> This change is not needed as this is a private function only used in
> const-eval only.
> 
> I wonder if I should add another macro to assert that the function is
> only used in const eval instead? Do you think it might be useful to have
> something like:
> 
> 	#[const_only]
> 	const fn foo() {}
> 
> or
> 
> 	const fn foo() {
> 	    const_only!();
> 	}
> 
> ? If so, I can send a patch that adds this feature. 
> 
> Implementation-wise, this will behave similar to build_error, where a
> function is going to be added that is never-linked but has a body for
> const eval.

I already applied this from V2, should I drop this change ?

-- 
viresh
Re: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Posted by Gary Guo 4 days ago
On Mon, 15 Dec 2025 10:36:55 +0530
Viresh Kumar <viresh.kumar@linaro.org> wrote:

> On Mon, 08 Dec 2025 11:47:01 +0900
> Alexandre Courbot <acourbot@nvidia.com> wrote:
> 
> > `build_assert` relies on the compiler to optimize out its error path.
> > Functions using it with its arguments must thus always be inlined,
> > otherwise the error path of `build_assert` might not be optimized out,
> > triggering a build error.
> > 
> > Cc: stable@vger.kernel.org
> > Fixes: c6af9a1191d0 ("rust: cpufreq: Extend abstractions for driver registration")
> > Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
> > Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
> > Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> > ---
> >  rust/kernel/cpufreq.rs | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs
> > index f968fbd22890..0879a79485f8 100644
> > --- a/rust/kernel/cpufreq.rs
> > +++ b/rust/kernel/cpufreq.rs
> > @@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> {
> >          ..pin_init::zeroed()
> >      };
> >  
> > +    // Always inline to optimize out error path of `build_assert`.
> > +    #[inline(always)]
> >      const fn copy_name(name: &'static CStr) -> [c_char; CPUFREQ_NAME_LEN] {
> >          let src = name.to_bytes_with_nul();
> >          let mut dst = [0; CPUFREQ_NAME_LEN];
> >   
>  
> > This change is not needed as this is a private function only used in
> > const-eval only.
> > 
> > I wonder if I should add another macro to assert that the function is
> > only used in const eval instead? Do you think it might be useful to have
> > something like:
> > 
> > 	#[const_only]
> > 	const fn foo() {}
> > 
> > or
> > 
> > 	const fn foo() {
> > 	    const_only!();
> > 	}
> > 
> > ? If so, I can send a patch that adds this feature. 
> > 
> > Implementation-wise, this will behave similar to build_error, where a
> > function is going to be added that is never-linked but has a body for
> > const eval.  
> 
> I already applied this from V2, should I drop this change ?
> 

Thinking again about this I think `#[inline(always)]` is fine to keep as
it can also be used to indicate "this function shall never be codegenned".

However I do still think the comment is confusing per-se as there is no
"optimization" for this function at all.

RE: the patch I am fine either without this patch picked or having this
patch in and fix the comment later.

Best,
Gary
Re: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Posted by Viresh Kumar 3 days, 5 hours ago
On 15-12-25, 11:14, Gary Guo wrote:
> Thinking again about this I think `#[inline(always)]` is fine to keep as
> it can also be used to indicate "this function shall never be codegenned".
> 
> However I do still think the comment is confusing per-se as there is no
> "optimization" for this function at all.
> 
> RE: the patch I am fine either without this patch picked or having this
> patch in and fix the comment later.

Thanks Gary. I will keep the patch then and apply add-ons later.

-- 
viresh
Re: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Posted by Miguel Ojeda 1 week, 3 days ago
On Mon, Dec 8, 2025 at 2:55 PM Gary Guo <gary@garyguo.net> wrote:
>
> ? If so, I can send a patch that adds this feature.

Sounds like `consteval` in C++20, which is useful from time to time.

If we add it, then the attribute form may make a bit more "sense"
conceptually (and we already also added the `export` one).

Cheers,
Miguel
Re: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Posted by Alexandre Courbot 1 week, 3 days ago
On Mon Dec 8, 2025 at 10:55 PM JST, Gary Guo wrote:
> On Mon, 08 Dec 2025 11:47:01 +0900
> Alexandre Courbot <acourbot@nvidia.com> wrote:
>
>> `build_assert` relies on the compiler to optimize out its error path.
>> Functions using it with its arguments must thus always be inlined,
>> otherwise the error path of `build_assert` might not be optimized out,
>> triggering a build error.
>> 
>> Cc: stable@vger.kernel.org
>> Fixes: c6af9a1191d0 ("rust: cpufreq: Extend abstractions for driver registration")
>> Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
>> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>> ---
>>  rust/kernel/cpufreq.rs | 2 ++
>>  1 file changed, 2 insertions(+)
>> 
>> diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs
>> index f968fbd22890..0879a79485f8 100644
>> --- a/rust/kernel/cpufreq.rs
>> +++ b/rust/kernel/cpufreq.rs
>> @@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> {
>>          ..pin_init::zeroed()
>>      };
>>  
>> +    // Always inline to optimize out error path of `build_assert`.
>> +    #[inline(always)]
>>      const fn copy_name(name: &'static CStr) -> [c_char; CPUFREQ_NAME_LEN] {
>>          let src = name.to_bytes_with_nul();
>>          let mut dst = [0; CPUFREQ_NAME_LEN];
>> 
>
> This change is not needed as this is a private function only used in
> const-eval only.

... for now. :)

>
> I wonder if I should add another macro to assert that the function is
> only used in const eval instead? Do you think it might be useful to have
> something like:
>
> 	#[const_only]
> 	const fn foo() {}
>
> or
>
> 	const fn foo() {
> 	    const_only!();
> 	}
>
> ? If so, I can send a patch that adds this feature. 
>
> Implementation-wise, this will behave similar to build_error, where a
> function is going to be added that is never-linked but has a body for
> const eval.

It could be useful in the general sense, but for this particular case
the rule "if you do build_assert on a function argument, then always
inline it" also covers us in case `copy_name` gets used outside of const
context, so isn't it the preferable workaround?
Re: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Posted by Gary Guo 1 week, 2 days ago
On Tue, 09 Dec 2025 09:52:13 +0900
"Alexandre Courbot" <acourbot@nvidia.com> wrote:

> On Mon Dec 8, 2025 at 10:55 PM JST, Gary Guo wrote:
> > On Mon, 08 Dec 2025 11:47:01 +0900
> > Alexandre Courbot <acourbot@nvidia.com> wrote:
> >  
> >> `build_assert` relies on the compiler to optimize out its error path.
> >> Functions using it with its arguments must thus always be inlined,
> >> otherwise the error path of `build_assert` might not be optimized out,
> >> triggering a build error.
> >> 
> >> Cc: stable@vger.kernel.org
> >> Fixes: c6af9a1191d0 ("rust: cpufreq: Extend abstractions for driver registration")
> >> Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
> >> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
> >> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> >> ---
> >>  rust/kernel/cpufreq.rs | 2 ++
> >>  1 file changed, 2 insertions(+)
> >> 
> >> diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs
> >> index f968fbd22890..0879a79485f8 100644
> >> --- a/rust/kernel/cpufreq.rs
> >> +++ b/rust/kernel/cpufreq.rs
> >> @@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> {
> >>          ..pin_init::zeroed()
> >>      };
> >>  
> >> +    // Always inline to optimize out error path of `build_assert`.
> >> +    #[inline(always)]
> >>      const fn copy_name(name: &'static CStr) -> [c_char; CPUFREQ_NAME_LEN] {
> >>          let src = name.to_bytes_with_nul();
> >>          let mut dst = [0; CPUFREQ_NAME_LEN];
> >>   
> >
> > This change is not needed as this is a private function only used in
> > const-eval only.  
> 
> ... for now. :)
> 
> >
> > I wonder if I should add another macro to assert that the function is
> > only used in const eval instead? Do you think it might be useful to have
> > something like:
> >
> > 	#[const_only]
> > 	const fn foo() {}
> >
> > or
> >
> > 	const fn foo() {
> > 	    const_only!();
> > 	}
> >
> > ? If so, I can send a patch that adds this feature. 
> >
> > Implementation-wise, this will behave similar to build_error, where a
> > function is going to be added that is never-linked but has a body for
> > const eval.  
> 
> It could be useful in the general sense, but for this particular case
> the rule "if you do build_assert on a function argument, then always
> inline it" also covers us in case `copy_name` gets used outside of const
> context, so isn't it the preferable workaround?

In this particular case the `copy_name` shouldn't be used at all
outside const eval. It's specificially for building a table during
const eval. It's a bug if it's outside, hence I think
`#[inline(always)]` adds confusion to the reader of this code.

I get that you want to have a general rule of "if you're using
something with `build_assert!`, then use `#[inline(always)]`", but I
think applying that rule here is detrimental.

Hence I suggested adding a marker to indicate const-eval only function,
so we can either say const-eval-only functions are fine without inline
markers, or perhaps just use normal panicking-assertion inside these
functions (as `build_assert!` behave identical to just `assert!` in
const-eval).

Best,
Gary