rust: cpufreq: fix `undocumented_unsafe_blocks` in `PolicyCpu::from_cpu()`
Subsystems (2)
Miguel Ojeda
maintainerPATCH 1/1 rust: cpufreq: fix `undocumented_unsafe_blocks` in `PolicyCpu::from_cpu()`
For Rust <= 1.86, Clippy triggers after commit 02c5d7bfa872 ("rust:
cpufreq: reject NULL from cpufreq_cpu_get()"):
warning: unsafe block missing a safety comment
--> rust/kernel/cpufreq.rs:700:26
|
700 | NonNull::new(unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) }).ok_or(ENODEV)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#undocumented_unsafe_blocks
= note: requested on the command line with `-W clippy::undocumented-unsafe-blocks`Thus clean it up.
--- rust/kernel/cpufreq.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
rust/kernel/cpufreq.rs +5 -3
@@ -695,9 +695,11 @@ fn clear_data<T: ForeignOwnable>(&mut self) -> Option<T> { impl<'a> PolicyCpu<'a> { fn from_cpu(cpu: CpuId) -> Result<Self> { - // SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU. - let ptr = - NonNull::new(unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) }).ok_or(ENODEV)?; + let ptr = NonNull::new( + // SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU. + unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) }, + ) + .ok_or(ENODEV)?; Ok(Self( // SAFETY: `ptr` is non-NULL and `cpufreq_cpu_get()` took a reference on it, so it is
base-commit: 2efb7a11f1aa4d2dc285fe2680cca22cb9f72f64
Signature · 1 line
2.56.0
rust/kernel/cpufreq.rs fn clear_data<T: ForeignOwnable>(&mut self) -> Option<T> { · line 702
Quoted 6 lines · Miguel Ojeda
rust/kernel/cpufreq.rs +5
-0+let ptr =NonNull ::new ( +// SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU. +unsafe { bindings::cpufreq_cpu_get (u32::from (cpu)) }, + ) + .ok_or (ENODEV )?;
The other option is to split the statements in two, which probably looks better, but this shows how to do it without changing the shape of the code.
Cheers,
Miguel
Quoted 13 lines · Miguel Ojeda
For Rust <= 1.86, Clippy triggers after commit
02c5d7bfa872("rust:
cpufreq: reject NULL fromcpufreq_cpu_get()"):warning: unsafe block missing a safety comment --> rust/kernel/cpufreq.rs:700:26 | 700 | NonNull::new(unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) }).ok_or(ENODEV)?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider adding a safety comment on the preceding line = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#undocumented_unsafe_blocks = note: requested on the command line with `-W clippy::undocumented-unsafe-blocks`
Quoted 5 lines
Thus clean it up.
Fixes 02c5d7bfa872 ("rust: cpufreq: reject NULL from cpufreq_cpu_get()")Assisted-by LLMSigned-off-by Miguel Ojeda <[email protected]>
FWIW, splitting it into two statements as you mentioned would read better to me.
Quoted 25 lines
--- rust/kernel/cpufreq.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)rust/kernel/cpufreq.rs +5
-3@@ -695,9 +695,11 @@ fn clear_data<T: ForeignOwnable>(&mut self) -> Option<T> { impl<'a> PolicyCpu<'a> { fn from_cpu(cpu: CpuId) -> Result<Self> { - // SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU. - let ptr = - NonNull::new(unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) }).ok_or(ENODEV)?; + let ptr = NonNull::new( + // SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU. + unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) }, + ) + .ok_or(ENODEV)?; Ok(Self( // SAFETY: `ptr` is non-NULL and `cpufreq_cpu_get()` took a reference on it, so it isbase-commit:
2efb7a11f1aa4d2dc285fe2680cca22cb9f72f64
Signature · 2 lines
Thx and BRs, Zhongqiu Han
Viresh Kumar
maintainerQuoted 16 lines · Zhongqiu Han
Quoted 15 lines · Miguel Ojeda
For Rust <= 1.86, Clippy triggers after commit
02c5d7bfa872("rust:
cpufreq: reject NULL fromcpufreq_cpu_get()"):warning: unsafe block missing a safety comment --> rust/kernel/cpufreq.rs:700:26 | 700 | NonNull::new(unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) }).ok_or(ENODEV)?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider adding a safety comment on the preceding line = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#undocumented_unsafe_blocks = note: requested on the command line with `-W clippy::undocumented-unsafe-blocks`Thus clean it up.
Quoted 8 lines
Quoted 3 lines
Fixes 02c5d7bfa872 ("rust: cpufreq: reject NULL from cpufreq_cpu_get()")Assisted-by LLMSigned-off-by Miguel Ojeda <[email protected]>Reviewed-by Zhongqiu Han <[email protected]>FWIW, splitting it into two statements as you mentioned would read better to me.
I agree.
Signature · 1 line
viresh
Quoted 2 lines · Viresh Kumar
I agree.
I think this is not in -next yet -- if it helps, I am happy to take this into rust-fixes (with the two statements):
// SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU.
let ptr = unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) };
let ptr = NonNull::new(ptr).ok_or(ENODEV)?;Cheers,
Miguel
Quoted 10 lines · Miguel Ojeda
Quoted 2 lines · Viresh Kumar
I agree.
I think this is not in -next yet -- if it helps, I am happy to take this into
rust-fixes(with the two statements):// SAFETY: It is safe to call `cpufreq_cpu_get` for any valid CPU. let ptr = unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) }; let ptr = NonNull::new(ptr).ok_or(ENODEV)?;
Sure.
Signature · 1 line
viresh
Miguel Ojeda
AppliedQuoted 14 lines · Miguel Ojeda
For Rust <= 1.86, Clippy triggers after commit
02c5d7bfa872("rust:
cpufreq: reject NULL fromcpufreq_cpu_get()"):warning: unsafe block missing a safety comment --> rust/kernel/cpufreq.rs:700:26 | 700 | NonNull::new(unsafe { bindings::cpufreq_cpu_get(u32::from(cpu)) }).ok_or(ENODEV)?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider adding a safety comment on the preceding line = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#undocumented_unsafe_blocks = note: requested on the command line with `-W clippy::undocumented-unsafe-blocks`
Quoted 5 lines
Thus clean it up.
Fixes 02c5d7bfa872 ("rust: cpufreq: reject NULL from cpufreq_cpu_get()")Assisted-by LLMSigned-off-by Miguel Ojeda <[email protected]>
Applied to rust-fixes -- thanks everyone!
I added Cc: stable@ -- while this one is not a big deal, we are backporting 02c5d7bfa872 (i.e. the one this one fixes), and thus we can take the chance to keep it aligned to mainline and clean.
Cheers,
Miguel