Skip to contentSkip to conversation

Various improvements on clock abstractions

PATCH In review onur-ozkan 4 participants 7 messages Jun 16, 2025 – Jun 19, 2025 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

onur-ozkan

author reviewer
#0 lore raw

PATCH 1/1 Various improvements on clock abstractions

A few changes to improve the clock abstractions and make them a little more idiomatic:

1. impl Hertz functions are now constant and compile-time evaluable.
2. Hertz conversions are now done with constant variables, which should make them more readable.
3. con_id is handled in a single line using map_or instead of using nested if-else blocks.

---
 rust/kernel/clk.rs | 42 +++++++++++++++++++-----------------------
 1 file changed, 19 insertions(+), 23 deletions(-)
rust/kernel/clk.rs +19 -23
index 6041c6d07527..fbcea31dbcca 100644
@@ -30,39 +30,43 @@
 pub struct Hertz(pub c_ulong);
 
 impl Hertz {
+    const KHZ_TO_HZ: c_ulong = 1_000;
+    const MHZ_TO_HZ: c_ulong = 1_000_000;
+    const GHZ_TO_HZ: c_ulong = 1_000_000_000;
+
     /// Create a new instance from kilohertz (kHz)
-    pub fn from_khz(khz: c_ulong) -> Self {
-        Self(khz * 1_000)
+    pub const fn from_khz(khz: c_ulong) -> Self {
+        Self(khz * Self::KHZ_TO_HZ)
     }
 
     /// Create a new instance from megahertz (MHz)
-    pub fn from_mhz(mhz: c_ulong) -> Self {
-        Self(mhz * 1_000_000)
+    pub const fn from_mhz(mhz: c_ulong) -> Self {
+        Self(mhz * Self::MHZ_TO_HZ)
     }
 
     /// Create a new instance from gigahertz (GHz)
-    pub fn from_ghz(ghz: c_ulong) -> Self {
-        Self(ghz * 1_000_000_000)
+    pub const fn from_ghz(ghz: c_ulong) -> Self {
+        Self(ghz * Self::GHZ_TO_HZ)
     }
 
     /// Get the frequency in hertz
-    pub fn as_hz(&self) -> c_ulong {
+    pub const fn as_hz(&self) -> c_ulong {
         self.0
     }
 
     /// Get the frequency in kilohertz
-    pub fn as_khz(&self) -> c_ulong {
-        self.0 / 1_000
+    pub const fn as_khz(&self) -> c_ulong {
+        self.0 / Self::KHZ_TO_HZ
     }
 
     /// Get the frequency in megahertz
-    pub fn as_mhz(&self) -> c_ulong {
-        self.0 / 1_000_000
+    pub const fn as_mhz(&self) -> c_ulong {
+        self.0 / Self::MHZ_TO_HZ
     }
 
     /// Get the frequency in gigahertz
-    pub fn as_ghz(&self) -> c_ulong {
-        self.0 / 1_000_000_000
+    pub const fn as_ghz(&self) -> c_ulong {
+        self.0 / Self::GHZ_TO_HZ
     }
 }
 
@@ -132,11 +136,7 @@ impl Clk {
         ///
         /// [`clk_get`]: https://docs.kernel.org/core-api/kernel-api.html#c.clk_get
         pub fn get(dev: &Device, name: Option<&CStr>) -> Result<Self> {
-            let con_id = if let Some(name) = name {
-                name.as_ptr()
-            } else {
-                ptr::null()
-            };
+            let con_id = name.map_or(ptr::null(), |n| n.as_ptr());
 
             // SAFETY: It is safe to call [`clk_get`] for a valid device pointer.
             //
@@ -304,11 +304,7 @@ impl OptionalClk {
         /// [`clk_get_optional`]:
         /// https://docs.kernel.org/core-api/kernel-api.html#c.clk_get_optional
         pub fn get(dev: &Device, name: Option<&CStr>) -> Result<Self> {
-            let con_id = if let Some(name) = name {
-                name.as_ptr()
-            } else {
-                ptr::null()
-            };
+            let con_id = name.map_or(ptr::null(), |n| n.as_ptr());
 
             // SAFETY: It is safe to call [`clk_get_optional`] for a valid device pointer.
             //
Signature · 1 line
2.49.0
#1 lore raw
Quoted 2 lines · onur-ozkan

Should this be "Onur Özkan"? Apologies if not -- I am just asking because the SoB typically uses the full name as usually written, and I saw it in Zulip spelled differently.

Cc'ing Viresh, since he introduced the file.

On the changes themselves, the const fn one seems obvious.

The constants one is OK, though we could perhaps have a units.h equivalent eventually.

The map_or is up to style, though I am not sure why you say "nested if-else blocks" in the commit message, i.e. what is nested?

(By the way, this could (or not) have been split into different patches -- it is up to the maintainers/reviewers, and it depends on how complex a given patch is and other factors, but I thought I would mention it just in case you thought it needed to be one.)

Thanks!

Cheers,
Miguel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 2 lines · Onur Özkan

Yes, it should be "Onur Özkan", sorry. Should I update that part and re-send the patch?

I would suggest to wait for other feedback, and then you can send a v2 if needed.

Quoted 1 line

where my patch converts this into a single straight line which I think makes it more idiomatic.

Up to the maintainers :) So far we have both styles around.

Thanks!

Cheers,
Miguel

Acked-by
#4 lore raw
Quoted 10 lines · Miguel Ojeda
Quoted 2 lines · Onur Özkan

Yes, it should be "Onur Özkan", sorry. Should I update that part and re-send the patch?

I would suggest to wait for other feedback, and then you can send a v2 if needed.

Quoted 1 line

where my patch converts this into a single straight line which I think makes it more idiomatic.

Up to the maintainers :) So far we have both styles around.

I am okay with all the changes, the commit log can be improved as you mentioned earlier.

Signature · 1 line
viresh
author reviewer
#5 lore raw
Quoted 13 lines · On Thu, 19 Jun 2025 12:15:34 +0530 Viresh Kumar <[email protected]> wrote:
Quoted 12 lines · Miguel Ojeda
Quoted 3 lines · Onur Özkan

Yes, it should be "Onur Özkan", sorry. Should I update that part and re-send the patch?

I would suggest to wait for other feedback, and then you can send a v2 if needed.

Quoted 2 lines

where my patch converts this into a single straight line which I think makes it more idiomatic.

Quoted 7 lines
Quoted 1 line

Up to the maintainers :) So far we have both styles around.

I am okay with all the changes, the commit log can be improved as you mentioned earlier.

FWIW I split this patch into 3 parts and sent them separately as it was suggested earlier by Alexandre.

Here are the patches I sent yesterday:

- https://lore.kernel.org/all/[email protected]/
- https://lore.kernel.org/all/[email protected]/
- https://lore.kernel.org/all/[email protected]/

Regards,
Onur

#6 lore raw
Quoted 24 lines · Onur
Quoted 13 lines · On Thu, 19 Jun 2025 12:15:34 +0530 Viresh Kumar <[email protected]> wrote:
Quoted 12 lines · Miguel Ojeda
Quoted 3 lines · Onur Özkan

Yes, it should be "Onur Özkan", sorry. Should I update that part and re-send the patch?

I would suggest to wait for other feedback, and then you can send a v2 if needed.

Quoted 2 lines

where my patch converts this into a single straight line which I think makes it more idiomatic.

Quoted 7 lines
Quoted 1 line

Up to the maintainers :) So far we have both styles around.

I am okay with all the changes, the commit log can be improved as you mentioned earlier.

Quoted 8 lines

FWIW I split this patch into 3 parts and sent them separately as it was suggested earlier by Alexandre.

Here are the patches I sent yesterday:

- https://lore.kernel.org/all/[email protected]/
- https://lore.kernel.org/all/[email protected]/
- https://lore.kernel.org/all/[email protected]/

These look good! Small nit for next time: since they are closely related, please send them as a series to make review (and merging, as they will conflict if not applied in the right order) easier.

#7 lore raw
In reply to onur-ozkan
Quoted 5 lines · onur-ozkan

A few changes to improve the clock abstractions and make them a little more idiomatic:

1. impl Hertz functions are now constant and compile-time evaluable.
2. Hertz conversions are now done with constant variables, which should

"constant variable" is an oxymoron. :) I think you just want to say "constant" here.

Quoted 3 lines

make them more readable.
3. con_id is handled in a single line using map_or instead of using nested if-else blocks.

Please split these 3 changes into 3 patches, I agree that they are trivial but a patch should do a single thing. This makes review simpler and allows to apply only part of the changes if e.g. one of them needs further discussion or is rejected.

The changes in themselves look good though!