Rust bindings for cpufreq and OPP core + sample driver
Viresh Kumar
maintainercover Rust bindings for cpufreq and OPP core + sample driver
Hello,
This RFC adds initial rust bindings for two subsystems, cpufreq and operating performance points (OPP). The bindings are provided for most of the interface these subsystems expose.
This series also provides a sample cpufreq driver rcpufreq-dt, which is a duplicate of the merged cpufreq-dt driver (A generic platform agnostic device tree based cpufreq driver) used on most of the ARM platforms.
This is tested with the help of QEMU for now and frequency transitions and configurations work as expected. No performance measurement is done as of now with this.
These patches (along with a lot of other dependencies) are pushed here for anyone to give them a try:
git://git.kernel.org/pub/scm/linux/kernel/git/vireshk/linux.git rust/cpufreq-dt
I understand that there is a long road ahead before these patches can get merged as a lot of other stuff needs to get in first, i.e. Rust bindings for:
- generic device / driver
- platform device / driver
- clk, regulator, OF, cpumask, etc
Because of the missing bindings for above APIs, lot of calls are made directly into the bindgen generated bindings for now, to make this work. Hopefully we can have a lot less unsafe code once we get all those things merged.
Based over v6.9-rc1.
Thanks.
Viresh Kumar (3):
rust: Add bindings for OPP framework
rust: Add bindings for cpufreq framework
cpufreq: Add Rust based cpufreq-dt driver
drivers/cpufreq/Kconfig | 12 + drivers/cpufreq/Makefile | 1 + drivers/cpufreq/rcpufreq_dt.rs | 264 ++++++++ rust/bindings/bindings_helper.h | 2 + rust/helpers.c | 15 + rust/kernel/cpufreq.rs | 1090 +++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 4 + rust/kernel/opp.rs | 895 +++++++++++++++++++++++++ 8 files changed, 2283 insertions(+) create mode 100644 drivers/cpufreq/rcpufreq_dt.rs create mode 100644 rust/kernel/cpufreq.rs create mode 100644 rust/kernel/opp.rs
Signature · 1 line
2.31.1.272.g89b43f80a514
Viresh Kumar
maintainerRFC PATCH 1/3 rust: Add bindings for OPP framework
This commit adds Rust bindings for the Operating performance points (OPP) core. The current implementation doesn't implement Rust wrappers for all the APIs, but mostly the ones usable by the cpufreq-dt driver.
The missing APIs will be added later once required.
--- rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 2 + rust/kernel/opp.rs | 895 ++++++++++++++++++++++++++++++++ 3 files changed, 898 insertions(+) create mode 100644 rust/kernel/opp.rs
rust/bindings/bindings_helper.h +1 -0
@@ -22,6 +22,7 @@#include <linux/platform_device.h> #include <linux/phy.h> #include <linux/pid_namespace.h> +#include <linux/pm_opp.h> #include <linux/poll.h> #include <linux/refcount.h> #include <linux/sched.h>
rust/kernel/lib.rs +2 -0
@@ -46,6 +46,8 @@#[cfg( CONFIG_NET )] pub mod net;pub mod of; +#[cfg( CONFIG_PM_OPP )] +pub mod opp;pub mod platform;pub mod prelude;pub mod print;
rust/kernel/opp.rs new file +895 -0
895 lines not on this page · raw message
Signature · 1 line
2.31.1.272.g89b43f80a514
Re: [RFC PATCH 1/3] rust: Add bindings for OPP framework
Hi,
I took a quick look and left some comments from the Rust side of view.
rust/kernel/opp.rs line 84
Quoted 4 lines · Viresh Kumar
rust/kernel/opp.rs new file +4
-0+/// Equivalent to `struct dev_pm_opp_config` in the C Code. +pub struct Config <T :ConfigOps > { + token:Option <i32 >, + clk_names:Option <Pin <Vec <CString >>>,
Why are you using Pin<Vec<_>>? The vector may reallocate the backing storage at any point in time.
Quoted 7 lines
rust/kernel/opp.rs new file +7
-0+ prop_name: Option<Pin<CString>>, + regulator_names: Option<Pin<Vec<CString>>>, + genpd_names: Option<Pin<Vec<CString>>>, + supported_hw: Option<Pin<Vec<u32>>>, + required_devs: Option<Pin<Vec<Device>>>, + _data: PhantomData<T>, +}
[...]
rust/kernel/opp.rs line 228
Quoted 4 lines
rust/kernel/opp.rs new file +4
-0+/// Sets the configuration with the OPP core. +pub fn set (&mut self, dev: &Device ) ->Result <()> { +// Already configured. +if self.token.is_some () {
Why does the config hold onto this token? Would it make sense to consume the config and return a Handle or Token abstraction? Then you don't need to check if the config has been "used" before.
Quoted 11 lines
rust/kernel/opp.rs new file +11
-0+return Err (EBUSY ); + } + +let (_clk_list, clk_names) =match &self.clk_names { +Some (x) => { +let list =to_c_str_array (x)?; +let ptr = list.as_ptr (); + (Some (list), ptr) + } +None => (None , ptr::null ()), + };
[...]
rust/kernel/opp.rs line 796
Quoted 8 lines
rust/kernel/opp.rs new file +8
-0+/// Operating performance point (OPP). +/// +/// # Invariants +/// +/// `ptr` is valid, non-null, and has a non-zero reference count. One of the references is owned by +/// `self`, and will be decremented when `self` is dropped. +#[repr(transparent)] +pub struct OPP (*mut bindings::dev_pm_opp );
I think you should use the ARef pattern instead:
#[repr(transparent)]
pub struct OPP(Opaque<bindings::dev_pm_opp>);
unsafe impl AlwaysRefCounted for OPP {
// ...
}Then you can use ARef<OPP> everywhere you use OPP currently.
Signature · 2 lines
Cheers, Benno
Viresh Kumar
maintainerRe: [RFC PATCH 1/3] rust: Add bindings for OPP framework
Thanks for the review Benno. I was waiting for more review comments to come in and hence didn't reply earlier with an update.
Quoted 20 lines · Benno Lossin
Quoted 1 line · Viresh Kumar
+
clk_names: Option<Pin<Vec<CString>>>,Why are you using
Pin<Vec<_>>? The vector may reallocate the backing storage at any point in time.Review
Quoted 4 lines
(quoted diff) +4
-0+ /// Sets the configuration with the OPP core. + pub fn set(&mut self, dev: &Device) -> Result<()> { + // Already configured. + if self.token.is_some() {Why does the config hold onto this token? Would it make sense to consume the config and return a
HandleorTokenabstraction? Then you don't need to check if the config has been "used" before.Review
Quoted 2 lines
(quoted diff) +2
-0+#[repr(transparent)] +pub struct OPP(*mut bindings::dev_pm_opp);I think you should use the
ARefpattern instead:
Quoted 8 lines
#[repr(transparent)] pub struct OPP(Opaque<bindings::dev_pm_opp>); unsafe impl AlwaysRefCounted for OPP { // ... }Then you can use
ARef<OPP>everywhere you useOPPcurrently.
All these comments look good to me. Updated the code with them (not reposting them for now)
-------------------------8<-------------------------
rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 2 + rust/kernel/opp.rs | 888 ++++++++++++++++++++++++++++++++ 3 files changed, 891 insertions(+) create mode 100644 rust/kernel/opp.rs
rust/bindings/bindings_helper.h +1 -0
@@ -22,6 +22,7 @@#include <linux/platform_device.h> #include <linux/phy.h> #include <linux/pid_namespace.h> +#include <linux/pm_opp.h> #include <linux/poll.h> #include <linux/refcount.h> #include <linux/sched.h>
rust/kernel/lib.rs +2 -0
@@ -46,6 +46,8 @@#[cfg( CONFIG_NET )] pub mod net;pub mod of; +#[cfg( CONFIG_PM_OPP )] +pub mod opp;pub mod platform;pub mod prelude;pub mod print;
rust/kernel/opp.rs new file +888 -0
888 lines not on this page · raw message
Re: [RFC PATCH 1/3] rust: Add bindings for OPP framework
Quoted 27 lines · Viresh Kumar
Thanks for the review Benno. I was waiting for more review comments to come in and hence didn't reply earlier with an update.
Quoted 23 lines · Benno Lossin
Quoted 1 line · Viresh Kumar
+
clk_names: Option<Pin<Vec<CString>>>,Why are you using
Pin<Vec<_>>? The vector may reallocate the backing storage at any point in time.Review
Quoted 4 lines
(quoted diff) +4
-0+ /// Sets the configuration with the OPP core. + pub fn set(&mut self, dev: &Device) -> Result<()> { + // Already configured. + if self.token.is_some() {Why does the config hold onto this token? Would it make sense to consume the config and return a
HandleorTokenabstraction? Then you don't need to check if the config has been "used" before.Review
Quoted 2 lines
(quoted diff) +2
-0+#[repr(transparent)] +pub struct OPP(*mut bindings::dev_pm_opp);I think you should use the
ARefpattern instead:#[repr(transparent)] pub struct OPP(Opaque<bindings::dev_pm_opp>);
Quoted 8 lines
Quoted 5 lines
unsafe impl AlwaysRefCounted for OPP { // ... }Then you can use
ARef<OPP>everywhere you useOPPcurrently.All these comments look good to me. Updated the code with them (not reposting them for now)
I currently do not have a lot of time, so I will re-review it when you post a new version.
Quoted 5 lines
-------------------------8<-------------------------
rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 2 + rust/kernel/opp.rs | 888 ++++++++++++++++++++++++++++++++
One other nice thing that you could do would be to split this patch into multiple smaller ones. That makes understanding the pieces a lot easier.
Signature · 2 lines
Cheers, Benno
Viresh Kumar
maintainerRFC PATCH 2/3 rust: Add bindings for cpufreq framework
In reply to Viresh KumarThis commit adds Rust bindings for the cpufreq core. The current implementation doesn't implement Rust wrappers for all the APIs, but mostly the ones usable by the cpufreq-dt driver.
The missing APIs will be added later once required.
--- rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 15 + rust/kernel/cpufreq.rs | 1090 +++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 4 files changed, 1108 insertions(+) create mode 100644 rust/kernel/cpufreq.rs
rust/bindings/bindings_helper.h +1 -0
@@ -9,6 +9,7 @@#include <kunit/test.h> #include <linux/cred.h> #include <linux/cpu.h> +#include <linux/cpufreq.h> #include <linux/device.h> #include <linux/errname.h> #include <linux/ethtool.h>
rust/helpers.c +15 -0
@@ -24,6 +24,7 @@#include <linux/bug.h> #include <linux/build_bug.h> #include <linux/cpumask.h> +#include <linux/cpufreq.h> #include <linux/cred.h> #include <linux/device.h> #include <linux/err.h>
@@ -315,6 +316,20 @@ void rust_helper_cpumask_setall(struct cpumask *dstp) }EXPORT_SYMBOL_GPL (rust_helper_cpumask_setall); +#ifdef CONFIG_CPU_FREQ +unsigned int rust_helper_cpufreq_table_len (struct cpufreq_frequency_table *freq_table) +{ +return cpufreq_table_len (freq_table); +} +EXPORT_SYMBOL_GPL (rust_helper_cpufreq_table_len); + +void rust_helper_cpufreq_register_em_with_opp (struct cpufreq_policy *policy) +{ +cpufreq_register_em_with_opp (policy); +} +EXPORT_SYMBOL_GPL (rust_helper_cpufreq_register_em_with_opp); +#endif +#ifndef CONFIG_OF_DYNAMIC struct device_node *rust_helper_of_node_get (struct device_node *node) {
rust/kernel/cpufreq.rs new file +1090 -0
1,090 lines not on this page · raw message
rust/kernel/lib.rs +2 -0
@@ -34,6 +34,8 @@#[cfg(not(testlib))] mod allocator;mod build_assert; +#[cfg( CONFIG_CPU_FREQ )] +pub mod cpufreq;pub mod cred;pub mod device;pub mod driver;
Signature · 1 line
2.31.1.272.g89b43f80a514
Viresh Kumar
maintainerRFC PATCH 3/3 cpufreq: Add Rust based cpufreq-dt driver
In reply to Viresh KumarThis commit adds a Rust based cpufreq-dt driver, which covers most of the functionality of the existing C based driver. Only a handful of things are left, like fetching platform data from cpufreq-dt-platdev.c.
This is tested with the help of QEMU for now and switching of frequencies work as expected.
--- drivers/cpufreq/Kconfig | 12 ++ drivers/cpufreq/Makefile | 1 + drivers/cpufreq/rcpufreq_dt.rs | 264 +++++++++++++++++++++++++++++++++ 3 files changed, 277 insertions(+) create mode 100644 drivers/cpufreq/rcpufreq_dt.rs
drivers/cpufreq/Kconfig +12 -0
@@ -217,6 +217,18 @@ config CPUFREQ_DT If in doubt, say N. +config CPUFREQ_DT_RUST + tristate "Rust based Generic DT based cpufreq driver" + depends on HAVE_CLK && OF && RUST + select CPUFREQ_DT_PLATDEV + select PM_OPP + help + This adds a Rust based generic DT based cpufreq driver for frequency + management. It supports both uniprocessor (UP) and symmetric + multiprocessor (SMP) systems. + + If in doubt, say N. + config CPUFREQ_DT_PLATDEV tristate "Generic DT based cpufreq platdev driver" depends on OF
drivers/cpufreq/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-$(CONFIG_CPU_FREQ_GOV_COMMON) += cpufreq_governor.o obj-$(CONFIG_CPU_FREQ_GOV_ATTR_SET) += cpufreq_governor_attr_set.o obj-$(CONFIG_CPUFREQ_DT) += cpufreq-dt.o +obj-$(CONFIG_CPUFREQ_DT_RUST) += rcpufreq_dt.o obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o # Traces
drivers/cpufreq/rcpufreq_dt.rs new file +264 -0
264 lines not on this page · raw message
Signature · 1 line
2.31.1.272.g89b43f80a514
Re: [RFC PATCH 3/3] cpufreq: Add Rust based cpufreq-dt driver
drivers/cpufreq/rcpufreq_dt.rs line 24
Quoted 6 lines · Viresh Kumar
drivers/cpufreq/rcpufreq_dt.rs new file +6
-0+// Finds exact supply name from the OF node. +fn find_supply_name_exact (np: *mut bindings::device_node , name: &str ) ->Option <CString > { +let sname =CString ::try_from_fmt (fmt! ("{}-supply" , name)).ok ()?; + +// SAFETY: The OF node is guaranteed by the C code to be valid. +let pp =unsafe { bindings::of_find_property (np, sname.as_ptr ()as *mut _ , ptr::null_mut ()) };
Drivers should avoid calling unsafe code as much as possible. They also should not be calling bindings code directly. Please write (or find) abstractions for these unsafe calls.
Signature · 2 lines
Cheers, Benno
Quoted 6 lines
drivers/cpufreq/rcpufreq_dt.rs new file +6
-0+if pp.is_null () { +None + }else { +CString ::try_from_fmt (fmt! ("{}" , name)).ok () + } +}
Re: [RFC PATCH 3/3] cpufreq: Add Rust based cpufreq-dt driver
Quoted 11 lines · Benno Lossin
drivers/cpufreq/rcpufreq_dt.rs line 24
Quoted 6 lines · Viresh Kumar
drivers/cpufreq/rcpufreq_dt.rs new file +6
-0+// Finds exact supply name from the OF node. +fn find_supply_name_exact (np: *mut bindings::device_node , name: &str ) ->Option <CString > { +let sname =CString ::try_from_fmt (fmt! ("{}-supply" , name)).ok ()?; + +// SAFETY: The OF node is guaranteed by the C code to be valid. +let pp =unsafe { bindings::of_find_property (np, sname.as_ptr ()as *mut _ , ptr::null_mut ()) };Drivers should avoid calling
unsafecode as much as possible. They also should not be callingbindingscode directly. Please write (or find) abstractions for theseunsafecalls.
Having re-read the cover letter, I see that you are already aware of this. If you need any help with creating the abstractions, feel free to reach out!
Signature · 2 lines
Cheers, Benno
Viresh Kumar
maintainerRe: [RFC PATCH 3/3] cpufreq: Add Rust based cpufreq-dt driver
Quoted 13 lines · Benno Lossin
Quoted 11 lines · Benno Lossin
drivers/cpufreq/rcpufreq_dt.rs line 24
Quoted 6 lines · Viresh Kumar
drivers/cpufreq/rcpufreq_dt.rs new file +6
-0+// Finds exact supply name from the OF node. +fn find_supply_name_exact (np: *mut bindings::device_node , name: &str ) ->Option <CString > { +let sname =CString ::try_from_fmt (fmt! ("{}-supply" , name)).ok ()?; + +// SAFETY: The OF node is guaranteed by the C code to be valid. +let pp =unsafe { bindings::of_find_property (np, sname.as_ptr ()as *mut _ , ptr::null_mut ()) };Drivers should avoid calling
unsafecode as much as possible. They also should not be callingbindingscode directly. Please write (or find) abstractions for theseunsafecalls.
Quoted 3 lines
Having re-read the cover letter, I see that you are already aware of this. If you need any help with creating the abstractions, feel free to reach out!
Thanks Benno. I am not sure what's the right approach here as there are so many missing things (frameworks) I need. Though I don't need full support for them but just a handful of APIs.
And then there is dependency on the generic support for device/driver, platform device/driver, etc.
Signature · 1 line
viresh
Re: [RFC PATCH 3/3] cpufreq: Add Rust based cpufreq-dt driver
Quoted 18 lines · Viresh Kumar
Quoted 13 lines · Benno Lossin
Quoted 11 lines · Benno Lossin
drivers/cpufreq/rcpufreq_dt.rs line 24
Quoted 6 lines · Viresh Kumar
drivers/cpufreq/rcpufreq_dt.rs new file +6
-0+// Finds exact supply name from the OF node. +fn find_supply_name_exact (np: *mut bindings::device_node , name: &str ) ->Option <CString > { +let sname =CString ::try_from_fmt (fmt! ("{}-supply" , name)).ok ()?; + +// SAFETY: The OF node is guaranteed by the C code to be valid. +let pp =unsafe { bindings::of_find_property (np, sname.as_ptr ()as *mut _ , ptr::null_mut ()) };Drivers should avoid calling
unsafecode as much as possible. They also should not be callingbindingscode directly. Please write (or find) abstractions for theseunsafecalls.Quoted 3 lines
Having re-read the cover letter, I see that you are already aware of this. If you need any help with creating the abstractions, feel free to reach out!
Quoted 6 lines
Thanks Benno. I am not sure what's the right approach here as there are so many missing things (frameworks) I need. Though I don't need full support for them but just a handful of APIs.
And then there is dependency on the generic support for device/driver, platform device/driver, etc.
I don't know exactly what you are referring to, but you can try to reach out to other people on our zulip. Splitting the work with others could help you :)
Also if the frameworks that you need are particularly difficult to upstream, because they have a lot of dependencies, then you can try to create something similar to [1].
[1]: https://github.com/Rust-for-Linux/linux/issues/1004
Signature · 2 lines
Cheers, Benno