Skip to contentSkip to conversation

rust: cpufreq: streamline find_supply_names

PATCH Applied Thorsten Blum 3 participants 4 messages Sep 15, 2025 – Sep 29, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: cpufreq: streamline find_supply_names

Remove local variables from find_supply_names() and use .and_then() with the more concise kernel::kvec![] macro, instead of KVec::with_capacity() followed by .push() and Some().

No functional changes intended.

---
 drivers/cpufreq/rcpufreq_dt.rs | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/cpufreq/rcpufreq_dt.rs +3 -7
index 7e1fbf9a091f..224d063c7cec 100644
@@ -28,15 +28,11 @@ fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> {
 /// Finds supply name for the CPU from DT.
 fn find_supply_names(dev: &Device, cpu: cpu::CpuId) -> Option<KVec<CString>> {
     // Try "cpu0" for older DTs, fallback to "cpu".
-    let name = (cpu.as_u32() == 0)
+    (cpu.as_u32() == 0)
         .then(|| find_supply_name_exact(dev, "cpu0"))
         .flatten()
-        .or_else(|| find_supply_name_exact(dev, "cpu"))?;
-
-    let mut list = KVec::with_capacity(1, GFP_KERNEL).ok()?;
-    list.push(name, GFP_KERNEL).ok()?;
-
-    Some(list)
+        .or_else(|| find_supply_name_exact(dev, "cpu"))
+        .and_then(|name| kernel::kvec![name].ok())
 }
 
 /// Represents the cpufreq dt device.
Signature · 1 line
2.51.0
maintainer Applied
#1 lore raw
Quoted 24 lines · Thorsten Blum

Remove local variables from find_supply_names() and use .and_then() with the more concise kernel::kvec![] macro, instead of KVec::with_capacity() followed by .push() and Some().

No functional changes intended.

---
 drivers/cpufreq/rcpufreq_dt.rs | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/cpufreq/rcpufreq_dt.rs +1 -2
index 7e1fbf9a091f..224d063c7cec 100644
@@ -28,15 +28,11 @@ fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> {
 /// Finds supply name for the CPU from DT.
 fn find_supply_names(dev: &Device, cpu: cpu::CpuId) -> Option<KVec<CString>> {
     // Try "cpu0" for older DTs, fallback to "cpu".
-    let name = (cpu.as_u32() == 0)
+    (cpu.as_u32() == 0)
         .then(|| find_supply_name_exact(dev, "cpu0"))
         .flatten()
-        .or_else(|| find_supply_name_exact(dev, "cpu"))?;

drivers/cpufreq/rcpufreq_dt.rs fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> { · line 38

Quoted 10 lines
drivers/cpufreq/rcpufreq_dt.rs +2 -5
-
-    let mut list = KVec::with_capacity(1, GFP_KERNEL).ok()?;
-    list.push(name, GFP_KERNEL).ok()?;
-
-    Some(list)
+        .or_else(|| find_supply_name_exact(dev, "cpu"))
+        .and_then(|name| kernel::kvec![name].ok())
 }
 
 /// Represents the cpufreq dt device.

Applied. Thanks.

Signature · 1 line
viresh
reviewer
#2 lore raw
In reply to Thorsten Blum
Quoted 22 lines · Thorsten Blum

Remove local variables from find_supply_names() and use .and_then() with the more concise kernel::kvec![] macro, instead of KVec::with_capacity() followed by .push() and Some().

No functional changes intended.

---
 drivers/cpufreq/rcpufreq_dt.rs | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/cpufreq/rcpufreq_dt.rs +1 -1
index 7e1fbf9a091f..224d063c7cec 100644
@@ -28,15 +28,11 @@ fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> {
 /// Finds supply name for the CPU from DT.
 fn find_supply_names(dev: &Device, cpu: cpu::CpuId) -> Option<KVec<CString>> {
     // Try "cpu0" for older DTs, fallback to "cpu".
-    let name = (cpu.as_u32() == 0)
+    (cpu.as_u32() == 0)

drivers/cpufreq/rcpufreq_dt.rs fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> { · line 35

Quoted 10 lines
drivers/cpufreq/rcpufreq_dt.rs +2 -6
         .then(|| find_supply_name_exact(dev, "cpu0"))
         .flatten()
-        .or_else(|| find_supply_name_exact(dev, "cpu"))?;
-
-    let mut list = KVec::with_capacity(1, GFP_KERNEL).ok()?;
-    list.push(name, GFP_KERNEL).ok()?;
-
-    Some(list)
+        .or_else(|| find_supply_name_exact(dev, "cpu"))
+        .and_then(|name| kernel::kvec![name].ok())

This is a pre-existing issue, but ... this treats allocation failure and non-existence the same way. That sounds wrong.

Alice

maintainer
#3 lore raw
Quoted 2 lines · Alice Ryhl

This is a pre-existing issue, but ... this treats allocation failure and non-existence the same way. That sounds wrong.

What about this over the current patch:

drivers/cpufreq/rcpufreq_dt.rs +9 -5
index 224d063c7cec..e509b46b64c7 100644
@@ -26,13 +26,17 @@ fn find_supply_name_exact(dev: &Device, name: &str) -> Option<CString> {
 }
 
 /// Finds supply name for the CPU from DT.
-fn find_supply_names(dev: &Device, cpu: cpu::CpuId) -> Option<KVec<CString>> {
+fn find_supply_names(dev: &Device, cpu: cpu::CpuId) -> Result<Option<KVec<CString>>> {
     // Try "cpu0" for older DTs, fallback to "cpu".
-    (cpu.as_u32() == 0)
+    let name = (cpu.as_u32() == 0)
         .then(|| find_supply_name_exact(dev, "cpu0"))
         .flatten()
-        .or_else(|| find_supply_name_exact(dev, "cpu"))
-        .and_then(|name| kernel::kvec![name].ok())
+        .or_else(|| find_supply_name_exact(dev, "cpu"));
+
+    Ok(match name {
+        None => None,
+        Some(n) => Some(kernel::kvec![n]?),
+    })
 }
 
 /// Represents the cpufreq dt device.
@@ -68,7 +72,7 @@ fn init(policy: &mut cpufreq::Policy) -> Result<Self::PData> {
 
         mask.set(cpu);
 
-        let token = find_supply_names(dev, cpu)
+        let token = find_supply_names(dev, cpu)?
             .map(|names| {
                 opp::Config::<Self>::new()
                     .set_regulator_names(names)?
Signature · 1 line
viresh