Skip to contentSkip to conversation

rust: Add basic clock bindings

PATCH Superseded Viresh Kumar 5 participants 15 messages Feb 21, 2025 – Mar 6, 2025

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover rust: Add basic clock bindings

Hello,

This adds initial bindings for the clk APIs. These provide the minimal functionality needed for common use cases, making them straightforward to introduce in the first iteration.

These will be used by Rust based cpufreq / OPP layers to begin with.

For now I have added them under the maintainership umbrella of the common clk framework, please let me know if I should do it differently.

This was posted earlier as part of the series implementing cpufreq/OPP Rust bindings [1] (since its V6 version). In order to make sure this gets properly reviewed and I don't accidentally miss relevant reviewers, I am posting it separately now.

If possible, I would like to get these merged via the PM tree along with cpufreq/OPP bindings, but its okay otherwise too.

--
Viresh

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

V1->V2:
- Post this as an independent series.
- Include more APIs, apart from clk_get() and clk_put().

Viresh Kumar (2):
rust: Add clk helpers
rust: Add basic bindings for clk APIs

 MAINTAINERS                     |   2 +
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/clk.c              |  57 +++++++++++++++++
 rust/helpers/helpers.c          |   1 +
 rust/kernel/clk.rs              | 104 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   1 +
 6 files changed, 166 insertions(+)
 create mode 100644 rust/helpers/clk.c
 create mode 100644 rust/kernel/clk.rs
Signature · 1 line
2.31.1.272.g89b43f80a514
#1 lore raw

PATCH v2 1/2 rust: Add clk helpers

In order to prepare for adding Rust abstractions for the clock APIs, this patch adds clock helpers required by the Rust implementation.

---
 MAINTAINERS                     |  1 +
 rust/bindings/bindings_helper.h |  1 +
 rust/helpers/clk.c              | 57 +++++++++++++++++++++++++++++++++
 rust/helpers/helpers.c          |  1 +
 4 files changed, 60 insertions(+)
 create mode 100644 rust/helpers/clk.c
MAINTAINERS +1 -0
index 25c86f47353d..726110d3c988 100644
@@ -5778,6 +5778,7 @@ F:	include/dt-bindings/clock/
 F:	include/linux/clk-pr*
 F:	include/linux/clk/
 F:	include/linux/of_clk.h
+F:	rust/helpers/clk.c
 X:	drivers/clk/clkdev.c
 
 COMMON INTERNET FILE SYSTEM CLIENT (CIFS and SMB3)
rust/bindings/bindings_helper.h +1 -0
index 55354e4dec14..4e4e16c3b479 100644
@@ -10,6 +10,7 @@
 #include <linux/blk-mq.h>
 #include <linux/blk_types.h>
 #include <linux/blkdev.h>
+#include <linux/clk.h>
 #include <linux/cred.h>
 #include <linux/errname.h>
 #include <linux/ethtool.h>
rust/helpers/clk.c new file +57 -0

57 lines not on this page · raw message

rust/helpers/helpers.c +1 -0
index 0640b7e115be..4700ee7aaf85 100644
@@ -11,6 +11,7 @@
 #include "bug.c"
 #include "build_assert.c"
 #include "build_bug.c"
+#include "clk.c"
 #include "cred.c"
 #include "device.c"
 #include "err.c"
Signature · 1 line
2.31.1.272.g89b43f80a514
#3 lore raw

PATCH v2 2/2 rust: Add basic bindings for clk APIs

In reply to Viresh Kumar

Add initial bindings for the clk APIs. These provide the minimal functionality needed for common use cases, making them straightforward to introduce in the first iteration.

These will be used by Rust based cpufreq / OPP layers to begin with.

---
 MAINTAINERS        |   1 +
 rust/kernel/clk.rs | 104 +++++++++++++++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs |   1 +
 3 files changed, 106 insertions(+)
 create mode 100644 rust/kernel/clk.rs
MAINTAINERS +1 -0
index 726110d3c988..96e2574f41c0 100644
@@ -5779,6 +5779,7 @@ F:	include/linux/clk-pr*
 F:	include/linux/clk/
 F:	include/linux/of_clk.h
 F:	rust/helpers/clk.c
+F:	rust/kernel/clk.rs
 X:	drivers/clk/clkdev.c
 
 COMMON INTERNET FILE SYSTEM CLIENT (CIFS and SMB3)
rust/kernel/clk.rs new file +104 -0

104 lines not on this page · raw message

rust/kernel/lib.rs +1 -0
index 496ed32b0911..324b86f127a0 100644
@@ -40,6 +40,7 @@
 pub mod block;
 #[doc(hidden)]
 pub mod build_assert;
+pub mod clk;
 pub mod cred;
 pub mod device;
 pub mod device_id;
Signature · 1 line
2.31.1.272.g89b43f80a514
reviewer Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH V2 1/2] rust: Add clk helpers

In reply to Viresh Kumar

Hi Viresh,

Quoted 44 lines · Viresh Kumar

In order to prepare for adding Rust abstractions for the clock APIs, this patch adds clock helpers required by the Rust implementation.

---

MAINTAINERS                     |  1 +
rust/bindings/bindings_helper.h |  1 +
rust/helpers/clk.c              | 57 +++++++++++++++++++++++++++++++++
rust/helpers/helpers.c          |  1 +
4 files changed, 60 insertions(+)

create mode 100644 rust/helpers/clk.c

MAINTAINERS +0 -0
index 25c86f47353d..726110d3c988 100644
@@ -5778,6 +5778,7 @@ F: include/dt-bindings/clock/

F: include/linux/clk-pr*
F: include/linux/clk/
F: include/linux/of_clk.h
+F: rust/helpers/clk.c
X: drivers/clk/clkdev.c

COMMON INTERNET FILE SYSTEM CLIENT (CIFS and SMB3)

rust/bindings/bindings_helper.h +1 -0
index 55354e4dec14..4e4e16c3b479 100644
@@ -10,6 +10,7 @@
 #include <linux/blk-mq.h>
 #include <linux/blk_types.h>
 #include <linux/blkdev.h>
+#include <linux/clk.h>
 #include <linux/cred.h>
 #include <linux/errname.h>
 #include <linux/ethtool.h>
rust/helpers/clk.c new file +0 -0
new file mode 100644
index 000000000000..3c63b50ad6fb

rust/helpers/clk.c line 5

Quoted 5 lines
rust/helpers/clk.c new file +5 -0
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/clk.h>
+
+#ifndef CONFIG_HAVE_CLK

This is a bit confusing. Can you add a comment explaining how we get inlined stubs if these configs are not set, thus explaining why we need to define them in helpers.c?

This will let everyone know why we have #ifndef here when the logical thing would be #ifdef. It will also make it clear why the rust code is not gated by these configs.

Quoted 22 lines
rust/helpers/clk.c new file +22 -0
+struct clk *rust_helper_clk_get(struct device *dev, const char *id)
+{
+ return clk_get(dev, id);
+}
+
+void rust_helper_clk_put(struct clk *clk)
+{
+ clk_put(clk);
+}
+
+int rust_helper_clk_enable(struct clk *clk)
+{
+ return clk_enable(clk);
+}
+
+void rust_helper_clk_disable(struct clk *clk)
+{
+ clk_disable(clk);
+}
+
+unsigned long rust_helper_clk_get_rate(struct clk *clk)
+{

rust/helpers/clk.c line 37

Quoted 10 lines
rust/helpers/clk.c new file +10 -0
+ return clk_get_rate(clk);
+}
+
+int rust_helper_clk_set_rate(struct clk *clk, unsigned long rate)
+{
+ return clk_set_rate(clk, rate);
+}
+#endif
+
+#ifndef CONFIG_HAVE_CLK_PREPARE

Same here.

Quoted 36 lines
rust/helpers/clk.c new file +20 -0
+int rust_helper_clk_prepare(struct clk *clk)
+{
+ return clk_prepare(clk);
+}
+
+void rust_helper_clk_unprepare(struct clk *clk)
+{
+ clk_unprepare(clk);
+}
+#endif
+
+int rust_helper_clk_prepare_enable(struct clk *clk)
+{
+ return clk_prepare_enable(clk);
+}
+
+void rust_helper_clk_disable_unprepare(struct clk *clk)
+{
+ clk_disable_unprepare(clk);
+}
rust/helpers/helpers.c +1 -0
index 0640b7e115be..4700ee7aaf85 100644
@@ -11,6 +11,7 @@
 #include "bug.c"
 #include "build_assert.c"
 #include "build_bug.c"
+#include "clk.c"
 #include "cred.c"
 #include "device.c"
 #include "err.c"
Signature · 1 line
2.31.1.272.g89b43f80a514

With this change,

reviewer Tested-by Reviewed-by on v2 2/2
#4 lore raw

Re: [PATCH V2 2/2] rust: Add basic bindings for clk APIs

In reply to Viresh Kumar

Hi Viresh, thank you for working on this.

Quoted 116 lines · Viresh Kumar

Add initial bindings for the clk APIs. These provide the minimal functionality needed for common use cases, making them straightforward to introduce in the first iteration.

These will be used by Rust based cpufreq / OPP layers to begin with.

---

MAINTAINERS        |   1 +
rust/kernel/clk.rs | 104 +++++++++++++++++++++++++++++++++++++++++++++
rust/kernel/lib.rs |   1 +
3 files changed, 106 insertions(+)

create mode 100644 rust/kernel/clk.rs

MAINTAINERS +0 -0
index 726110d3c988..96e2574f41c0 100644
@@ -5779,6 +5779,7 @@ F: include/linux/clk-pr*

F: include/linux/clk/
F: include/linux/of_clk.h
F: rust/helpers/clk.c
+F: rust/kernel/clk.rs
X: drivers/clk/clkdev.c

COMMON INTERNET FILE SYSTEM CLIENT (CIFS and SMB3)

rust/kernel/clk.rs new file +81 -0
new file mode 100644
index 000000000000..c212cd3167e1
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Clock abstractions.
+//!
+//! C header: [`include/linux/clk.h`](srctree/include/linux/clk.h)
+
+use crate::{
+    bindings,
+    device::Device,
+    error::{from_err_ptr, to_result, Result},
+    prelude::*,
+};
+
+use core::ptr;
+
+/// A simple implementation of `struct clk` from the C code.
+#[repr(transparent)]
+pub struct Clk(*mut bindings::clk);
 
+
+impl Clk {
+    /// Creates `Clk` instance for a device and a connection id.
+    pub fn new(dev: &Device, name: Option<&CStr>) -> Result<Self> {
+        let con_id = if let Some(name) = name {
+            name.as_ptr() as *const _
+        } else {
+            ptr::null()
+        };
+
+        // SAFETY: It is safe to call `clk_get()`, on a device pointer earlier received from the C
+        // code.
+        Ok(Self(from_err_ptr(unsafe {
+            bindings::clk_get(dev.as_raw(), con_id)
+        })?))
+    }
+
+    /// Obtain the raw `struct clk *`.
+    pub fn as_raw(&self) -> *mut bindings::clk {
+        self.0
+    }
+
+    /// Clock enable.
+    pub fn enable(&self) -> Result<()> {
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // use it now.
+        to_result(unsafe { bindings::clk_enable(self.0) })
+    }
+
+    /// Clock disable.
+    pub fn disable(&self) {
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // use it now.
+        unsafe { bindings::clk_disable(self.0) };
+    }
+
+    /// Clock prepare.
+    pub fn prepare(&self) -> Result<()> {
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // use it now.
+        to_result(unsafe { bindings::clk_prepare(self.0) })
+    }
+
+    /// Clock unprepare.
+    pub fn unprepare(&self) {
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // use it now.
+        unsafe { bindings::clk_unprepare(self.0) };
+    }
+
+    /// Clock prepare enable.
+    pub fn prepare_enable(&self) -> Result<()> {
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // use it now.
+        to_result(unsafe { bindings::clk_prepare_enable(self.0) })
+    }
+
+    /// Clock disable unprepare.
+    pub fn disable_unprepare(&self) {
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // use it now.
+        unsafe { bindings::clk_disable_unprepare(self.0) };
+    }

rust/kernel/clk.rs line 91

Quoted 10 lines
rust/kernel/clk.rs new file +10 -0
+
+    /// Clock get rate.
+    pub fn rate(&self) -> usize {
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // use it now.
+        unsafe { bindings::clk_get_rate(self.0) }
+    }
+
+    /// Clock set rate.
+    pub fn set_rate(&self, rate: usize) -> Result<()> {

It might be worth it to add a type here to make it clear what in what unit is rate expressed as.

Something like:

pub struct Hertz(pub u32);

pub fn set_rate(&self, rate: Hertz);

Assuming rate is in Hertz.

Quoted 29 lines
rust/kernel/clk.rs new file +13 -0
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // use it now.
+        to_result(unsafe { bindings::clk_set_rate(self.0, rate) })
+    }
+}
+
+impl Drop for Clk {
+    fn drop(&mut self) {
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // relinquish it now.
+        unsafe { bindings::clk_put(self.0) };
+    }
+}
rust/kernel/lib.rs +1 -0
index 496ed32b0911..324b86f127a0 100644
@@ -40,6 +40,7 @@
 pub mod block;
 #[doc(hidden)]
 pub mod build_assert;
+pub mod clk;
 pub mod cred;
 pub mod device;
 pub mod device_id;
Signature · 1 line
2.31.1.272.g89b43f80a514

This is working fine on Tyr, so:

With the minor change above:

#5 lore raw

Re: [PATCH V2 2/2] rust: Add basic bindings for clk APIs

In reply to Viresh Kumar

rust/kernel/clk.rs line 18

Quoted 3 lines · Viresh Kumar
rust/kernel/clk.rs new file +3 -0
+/// A simple implementation of `struct clk` from the C code.
+#[repr(transparent)]
+pub struct Clk(*mut bindings::clk);

I remember that Stephen explained that NULL is valid value for struct clk. As a consequence, all functions implemented for Clk have to consider this.

I wonder if it could make sense to have a transparent wrapper type MaybeNull<T> (analogous to NonNull<T>) to make this fact more obvious for cases like this?

Quoted 16 lines
rust/kernel/clk.rs new file +16 -0
+
+impl Clk {
+    /// Creates `Clk` instance for a device and a connection id.
+    pub fn new(dev: &Device, name: Option<&CStr>) -> Result<Self> {
+        let con_id = if let Some(name) = name {
+            name.as_ptr() as *const _
+        } else {
+            ptr::null()
+        };
+
+        // SAFETY: It is safe to call `clk_get()`, on a device pointer earlier received from the C
+        // code.
+        Ok(Self(from_err_ptr(unsafe {
+            bindings::clk_get(dev.as_raw(), con_id)
+        })?))
+    }

rust/kernel/clk.rs line 44

Quoted 10 lines
rust/kernel/clk.rs new file +10 -0
+
+    /// Obtain the raw `struct clk *`.
+    pub fn as_raw(&self) -> *mut bindings::clk {
+        self.0
+    }
+
+    /// Clock enable.
+    pub fn enable(&self) -> Result<()> {
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // use it now.

This is not true.

1. There is no type invariant documented for Clk.
2. The pointer contained in an instance of Clk may be NULL, hence self does not necessarily own a reference.

The same applies for all other functions in this implementation.

#6 lore raw

Re: [PATCH V2 2/2] rust: Add basic bindings for clk APIs

Quoted 11 lines · Danilo Krummrich

rust/kernel/clk.rs line 18

Quoted 3 lines · Viresh Kumar
rust/kernel/clk.rs new file +3 -0
+/// A simple implementation of `struct clk` from the C code.
+#[repr(transparent)]
+pub struct Clk(*mut bindings::clk);

I remember that Stephen explained that NULL is valid value for struct clk. As a consequence, all functions implemented for Clk have to consider this.

I am a bit confused here. If NULL is valid, then why should we have to specifically consider that in the functions? No functions so far explicitly dereferences that value, they only pass it to the clk framework.

Or are you referring to the safety comments only? In which case I do agree (sorry for the oversight by the way)

Quoted 4 lines

I wonder if it could make sense to have a transparent wrapper type MaybeNull<T> (analogous to NonNull<T>) to make this fact more obvious for cases like this?

MaybeNull<T> sounds nice.

Quoted 37 lines
Quoted 17 lines
rust/kernel/clk.rs new file +16 -0
+
+impl Clk {
+    /// Creates `Clk` instance for a device and a connection id.
+    pub fn new(dev: &Device, name: Option<&CStr>) -> Result<Self> {
+        let con_id = if let Some(name) = name {
+            name.as_ptr() as *const _
+        } else {
+            ptr::null()
+        };
+
+        // SAFETY: It is safe to call `clk_get()`, on a device pointer earlier received from the C
+        // code.
+        Ok(Self(from_err_ptr(unsafe {
+            bindings::clk_get(dev.as_raw(), con_id)
+        })?))
+    }

rust/kernel/clk.rs line 44

Quoted 10 lines
rust/kernel/clk.rs new file +10 -0
+
+    /// Obtain the raw `struct clk *`.
+    pub fn as_raw(&self) -> *mut bindings::clk {
+        self.0
+    }
+
+    /// Clock enable.
+    pub fn enable(&self) -> Result<()> {
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // use it now.

This is not true.

1. There is no type invariant documented for Clk.
2. The pointer contained in an instance of Clk may be NULL, hence self does not necessarily own a reference.

The same applies for all other functions in this implementation.

#14 lore raw

Re: [PATCH V2 2/2] rust: Add basic bindings for clk APIs

In reply to Viresh Kumar
Quoted 13 lines
rust/kernel/clk.rs new file +12 -0
+
+use crate::{
+    bindings,
+    device::Device,
+    error::{from_err_ptr, to_result, Result},
+    prelude::*,
+};
+
+use core::ptr;
+
+/// A simple implementation of `struct clk` from the C code.
+#[repr(transparent)]

rust/kernel/clk.rs line 27

Quoted 10 lines
rust/kernel/clk.rs new file +10 -0
+pub struct Clk(*mut bindings::clk);
+
+impl Clk {
+    /// Creates `Clk` instance for a device and a connection id.
+    pub fn new(dev: &Device, name: Option<&CStr>) -> Result<Self> {
+        let con_id = if let Some(name) = name {
+            name.as_ptr() as *const _
+        } else {
+            ptr::null()
+        };

Viresh, one thing I forgot to comment. Maybe it’s better if we keep the get nomenclature here instead of new.

This is to make clear that nothing is getting spawned. A reference to a system resource is (potentially) being returned instead.

This would also mean refactoring the docs for this function.

— Daniel

#7 lore raw

Re: [PATCH V2 2/2] rust: Add basic bindings for clk APIs

In reply to Daniel Almeida
Quoted 12 lines · Daniel Almeida
Quoted 11 lines · Danilo Krummrich

rust/kernel/clk.rs line 18

Quoted 3 lines · Viresh Kumar
rust/kernel/clk.rs new file +3 -0
+/// A simple implementation of `struct clk` from the C code.
+#[repr(transparent)]
+pub struct Clk(*mut bindings::clk);

I remember that Stephen explained that NULL is valid value for struct clk. As a consequence, all functions implemented for Clk have to consider this.

Quoted 3 lines

I am a bit confused here. If NULL is valid, then why should we have to specifically consider that in the functions? No functions so far explicitly dereferences that value, they only pass it to the clk framework.

This was badly phrased, the current implementation does not need to consider it indeed. What I meant is that we have to consider it potentially. Especially, when adding new functionality later on. For instance, when accessing fields of struct clk directly. Maybe this only becomes relevant once we write a clk driver itself in Rust, but still.

Quoted 10 lines

Or are you referring to the safety comments only? In which case I do agree (sorry for the oversight by the way)

Quoted 4 lines

I wonder if it could make sense to have a transparent wrapper type MaybeNull<T> (analogous to NonNull<T>) to make this fact more obvious for cases like this?

MaybeNull<T> sounds nice.

Yeah, it's probably the correct thing to do, to make things obvious.

Quoted 40 lines
Quoted 38 lines
Quoted 18 lines
rust/kernel/clk.rs new file +16 -0
+
+impl Clk {
+    /// Creates `Clk` instance for a device and a connection id.
+    pub fn new(dev: &Device, name: Option<&CStr>) -> Result<Self> {
+        let con_id = if let Some(name) = name {
+            name.as_ptr() as *const _
+        } else {
+            ptr::null()
+        };
+
+        // SAFETY: It is safe to call `clk_get()`, on a device pointer earlier received from the C
+        // code.
+        Ok(Self(from_err_ptr(unsafe {
+            bindings::clk_get(dev.as_raw(), con_id)
+        })?))
+    }

rust/kernel/clk.rs line 44

Quoted 10 lines
rust/kernel/clk.rs new file +10 -0
+
+    /// Obtain the raw `struct clk *`.
+    pub fn as_raw(&self) -> *mut bindings::clk {
+        self.0
+    }
+
+    /// Clock enable.
+    pub fn enable(&self) -> Result<()> {
+        // SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
+        // use it now.

This is not true.

1. There is no type invariant documented for Clk.
2. The pointer contained in an instance of Clk may be NULL, hence self does not necessarily own a reference.

The same applies for all other functions in this implementation.

#8 lore raw

Re: [PATCH V2 2/2] rust: Add basic bindings for clk APIs

Hi,

Quoted 10 lines · Danilo Krummrich
Quoted 9 lines · Daniel Almeida
Quoted 8 lines · Danilo Krummrich

rust/kernel/clk.rs line 18

Quoted 3 lines · Viresh Kumar
rust/kernel/clk.rs new file +3 -0
+/// A simple implementation of `struct clk` from the C code.
+#[repr(transparent)]
+pub struct Clk(*mut bindings::clk);

I remember that Stephen explained that NULL is valid value for struct clk. As a consequence, all functions implemented for Clk have to consider this.

Quoted 8 lines
Quoted 3 lines

I am a bit confused here. If NULL is valid, then why should we have to specifically consider that in the functions? No functions so far explicitly dereferences that value, they only pass it to the clk framework.

This was badly phrased, the current implementation does not need to consider it indeed. What I meant is that we have to consider it potentially. Especially, when adding new functionality later on. For instance, when accessing fields of struct clk directly.

Just a drive-by comment - the current implementation will never have a NULL clock anyways. That is only returned by the clk_get functions with the _optional() suffix. You are right now only using clk_get(), which will instead returns ERR_PTR(-ENOENT).

Quoted 2 lines

Maybe this only becomes relevant once we write a clk driver itself in Rust, but still.

For a clock provider implementation you will mainly use 'struct clk_hw', which is different from 'struct clk'.

Greetings,

-- Sebastian

#9 lore raw

Re: [PATCH V2 2/2] rust: Add basic bindings for clk APIs

Quoted 22 lines · Sebastian Reichel

Hi,

Quoted 10 lines · Danilo Krummrich
Quoted 9 lines · Daniel Almeida
Quoted 8 lines · Danilo Krummrich

rust/kernel/clk.rs line 18

Quoted 3 lines · Viresh Kumar
rust/kernel/clk.rs new file +3 -0
+/// A simple implementation of `struct clk` from the C code.
+#[repr(transparent)]
+pub struct Clk(*mut bindings::clk);

I remember that Stephen explained that NULL is valid value for struct clk. As a consequence, all functions implemented for Clk have to consider this.

Quoted 8 lines
Quoted 3 lines

I am a bit confused here. If NULL is valid, then why should we have to specifically consider that in the functions? No functions so far explicitly dereferences that value, they only pass it to the clk framework.

This was badly phrased, the current implementation does not need to consider it indeed. What I meant is that we have to consider it potentially. Especially, when adding new functionality later on. For instance, when accessing fields of struct clk directly.

Quoted 4 lines

Just a drive-by comment - the current implementation will never have a NULL clock anyways. That is only returned by the clk_get functions with the _optional() suffix. You are right now only using clk_get(), which will instead returns ERR_PTR(-ENOENT).

It would be nice to handle the optional case from the start. Otherwise, driver writers handle optional or not optional themselves. The not optional case is typically some form of error message duplicated in every driver.

Every foo_get() needs foo_get_optional(), so let's figure out the rust way to handle this once for everyone.

Rob

#13 lore raw

Re: [PATCH V2 2/2] rust: Add basic bindings for clk APIs

In reply to Danilo Krummrich
Quoted 5 lines · Danilo Krummrich

This was badly phrased, the current implementation does not need to consider it indeed. What I meant is that we have to consider it potentially. Especially, when adding new functionality later on. For instance, when accessing fields of struct clk directly. Maybe this only becomes relevant once we write a clk driver itself in Rust, but still.

I don't think we will _ever_ access fields of the struct clk directly. For the most common use, common clk API, the struct clk is defined in drivers/clk/clk.c.

Quoted 3 lines
Quoted 1 line

MaybeNull<T> sounds nice.

Yeah, it's probably the correct thing to do, to make things obvious.

Still need this ? Any example code that I can refer to implement it or if someone can help with implementing it ?

Signature · 1 line
viresh
#10 lore raw

Re: [PATCH V2 2/2] rust: Add basic bindings for clk APIs

In reply to Rob Herring
Quoted 7 lines · Rob Herring

It would be nice to handle the optional case from the start. Otherwise, driver writers handle optional or not optional themselves. The not optional case is typically some form of error message duplicated in every driver.

Every foo_get() needs foo_get_optional(), so let's figure out the rust way to handle this once for everyone.

Are we talking about adding another field here (like below code) or something else ?

impl Clk {
        pub fn get(dev: &Device, name: Option<&CStr>, optional: bool) -> Result<Self> {
                ...

                let clk = if optional {
                        bindings::clk_get(dev.as_raw(), con_id)
                else {
                        bindings::clk_get_optional(dev.as_raw(), con_id)
                };

                Ok(Self(from_err_ptr(clk)?))
        }
}
Signature · 1 line
viresh
#11 lore raw

Re: [PATCH V2 2/2] rust: Add basic bindings for clk APIs

Quoted 7 lines · Viresh Kumar
Quoted 5 lines · Rob Herring

It would be nice to handle the optional case from the start. Otherwise, driver writers handle optional or not optional themselves. The not optional case is typically some form of error message duplicated in every driver.

Quoted 5 lines
Quoted 2 lines

Every foo_get() needs foo_get_optional(), so let's figure out the rust way to handle this once for everyone.

Are we talking about adding another field here (like below code) or something else ?

Either way, but generally I think 2 functions are preferred over 1 function and flags.

The harder part here is in C we just return NULL and all subsequent functions (e.g. clk_enable()) just return with no error for a NULL struct clk. For rust, I think we'd need a dummy Clk returned and then handle comparing the passed in reference to the dummy Clk in the rust bindings.

Quoted 17 lines
impl Clk {
        pub fn get(dev: &Device, name: Option<&CStr>, optional: bool) -> Result<Self> {
                ...

                let clk = if optional {
                        bindings::clk_get(dev.as_raw(), con_id)
                else {
                        bindings::clk_get_optional(dev.as_raw(), con_id)
                };

                Ok(Self(from_err_ptr(clk)?))
        }
}

--
viresh

#12 lore raw

Re: [PATCH V2 2/2] rust: Add basic bindings for clk APIs

Quoted 8 lines · Rob Herring

Either way, but generally I think 2 functions are preferred over 1 function and flags.

The harder part here is in C we just return NULL and all subsequent functions (e.g. clk_enable()) just return with no error for a NULL struct clk. For rust, I think we'd need a dummy Clk returned and then handle comparing the passed in reference to the dummy Clk in the rust bindings.

I have implemented it differently in V3:

https://lore.kernel.org/all/023e3061cc164087b9079a9f6cb7e9fbf286794e.1740995194.git.viresh.kumar@linaro.org/

So even for a NULL value returned from clk_get_optional(), Rust users still get OptionalClk (Deref as Clk) and they keep using it as if a valid Clk is returned and will keep calling all clk APIs (which will return early for NULL clks).

Signature · 1 line
viresh