Skip to contentSkip to conversation

rust: cpufreq: fix `undocumented_unsafe_blocks` in `PolicyCpu::from_cpu()`

PATCH Applied Miguel Ojeda 4 participants 7 messages Oct 18 – Nov 2

Subsystems (2)
lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

PATCH 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.

Fixes 02c5d7bfa872 ("rust: cpufreq: reject NULL from cpufreq_cpu_get()")
Assisted-by LLM
---
 rust/kernel/cpufreq.rs | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
rust/kernel/cpufreq.rs +5 -3
index b75d692846a2..5f5beedec88b 100644
@@ -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
#1 lore raw

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

Reviewed-by on v1 1/1
#2 lore raw
In reply to Miguel Ojeda
Quoted 13 lines · Miguel Ojeda

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`
Quoted 5 lines

Thus clean it up.

Fixes 02c5d7bfa872 ("rust: cpufreq: reject NULL from cpufreq_cpu_get()")
Assisted-by LLM

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
index b75d692846a2..5f5beedec88b 100644
@@ -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 · 2 lines
Thx and BRs,
Zhongqiu Han
maintainer
#3 lore raw
Quoted 16 lines · Zhongqiu Han
Quoted 15 lines · Miguel Ojeda

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.

Quoted 8 lines
Quoted 3 lines
Fixes 02c5d7bfa872 ("rust: cpufreq: reject NULL from cpufreq_cpu_get()")
Assisted-by LLM

FWIW, splitting it into two statements as you mentioned would read better to me.

I agree.

Signature · 1 line
viresh
#4 lore raw
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

maintainer Acked-by
#5 lore raw
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
Applied
#6 lore raw
In reply to Miguel Ojeda
Quoted 14 lines · Miguel Ojeda

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`
Quoted 5 lines

Thus clean it up.

Fixes 02c5d7bfa872 ("rust: cpufreq: reject NULL from cpufreq_cpu_get()")
Assisted-by LLM

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