Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH v2] rust: cpufreq: Add `Send` and `Sync` bounds for `Driver::PData`
@ 2026-12-04  1:08 Priya Bala Govindasamy
  0 siblings, 0 replies; only message in thread
From: Priya Bala Govindasamy @ 2026-12-04  1:08 UTC (permalink / raw)
  To: rafael, viresh.kumar, ojeda, boqun, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, dakr, daniel.almeida, tamird,
	acourbot, work, nm, sboyd, zhongqiu.han
  Cc: linux-pm, rust-for-linux, ardalan, zhiyunq, dzueck, pgovind2

`Driver::PData` is required to implement `ForeignOwnable`, but
currently is not required to implement `Send` and `Sync`. Driver
callbacks can obtain shared references to `PData` via Policy::data()
and these callbacks could occur concurrently on different threads.
Additionally, `PData` can be moved across threads through `init_callback`
and `exit_callback` which transfer ownership to and from the C cpufreq
core. Therefore, `PData`, a type which may not implement `Send` or `Sync`,
could be accessed from different threads.

This is a soundness issue and no known failure is currently
triggered by any in-tree users.

Fix this by requiring that `Driver::PData` implement `Send` and `Sync`.
Additionally, for rcpufreq_dt, remove the unused `CpumaskVar` and implement
`Send` and `Sync` for `FreqTable`.

Fixes: 6ebdd7c93177 ("rust: cpufreq: Extend abstractions for policy and driver ops")
Reported-by: Dylan Zueck <dzueck@uci.edu>
Signed-off-by: Priya Bala Govindasamy <pgovind2@uci.edu>
---
This issue was detected by FerroLens, an LLM-assisted analysis tool.

Changes since v1:
 - Add `Send` and `Sync` bounds for `Driver::PData` instead of just `Send`.
 - Implement `Sync` for `FreqTable`.

Link to v1: https://lore.kernel.org/all/10e19c83b81a221cfe2f01b3756e530b9742bf84.1796073594.git.pgovind2@uci.edu/

 drivers/cpufreq/rcpufreq_dt.rs |  2 --
 rust/kernel/cpufreq.rs         | 10 +++++++---
 rust/kernel/opp.rs             |  9 +++++++++
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/drivers/cpufreq/rcpufreq_dt.rs b/drivers/cpufreq/rcpufreq_dt.rs
index 2b1f18b7a1e7..0479f6d3ade7 100644
--- a/drivers/cpufreq/rcpufreq_dt.rs
+++ b/drivers/cpufreq/rcpufreq_dt.rs
@@ -45,7 +45,6 @@ fn find_supply_names(dev: &Device, cpu: cpu::CpuId) -> Option<KVec<CString>> {
 struct CPUFreqDTDevice {
     opp_table: opp::Table,
     freq_table: opp::FreqTable,
-    _mask: CpumaskVar,
     _token: Option<opp::ConfigToken>,
     _clk: Clk,
 }
@@ -146,7 +145,6 @@ fn init(policy: &mut cpufreq::Policy) -> Result<Self::PData> {
             CPUFreqDTDevice {
                 opp_table,
                 freq_table,
-                _mask: mask,
                 _token: token,
                 _clk: clk,
             },
diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs
index f31671129c98..2a65dafc5f23 100644
--- a/rust/kernel/cpufreq.rs
+++ b/rust/kernel/cpufreq.rs
@@ -746,9 +746,13 @@ pub trait Driver {
 
     /// Policy specific data.
     ///
-    /// Require that `PData` implements `ForeignOwnable`. We guarantee to never move the underlying
-    /// wrapped data structure.
-    type PData: ForeignOwnable;
+    /// Require that `PData` implements `ForeignOwnable` so that ownership can be transferred
+    /// to the C `cpufreq_policy`.
+    /// Require that `PData` implements `Send` and `Sync` because `PData` may be accessed
+    /// from multiple threads concurrently.
+    /// We guarantee to never move the underlying wrapped data structure while it is owned
+    /// by the C `cpufreq_policy`.
+    type PData: ForeignOwnable + Send + Sync;
 
     /// Driver's `init` callback.
     fn init(policy: &mut Policy) -> Result<Self::PData>;
diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs
index 62e44676125d..5d35a3433d1a 100644
--- a/rust/kernel/opp.rs
+++ b/rust/kernel/opp.rs
@@ -35,6 +35,15 @@ pub struct FreqTable {
         ptr: *mut bindings::cpufreq_frequency_table,
     }
 
+    // SAFETY: `FreqTable` owns the allocation referenced by `ptr`. The allocation
+    // may be freed from any thread.
+    // `dev` is an `ARef<Device>`, which is `Send`.
+    unsafe impl Send for FreqTable {}
+
+    // SAFETY: The allocation referenced by `ptr` remains alive while `FreqTable` is borrowed.
+    // `dev` is `Sync`. Safe access to shared references of `FreqTable` does not permit writes.
+    unsafe impl Sync for FreqTable {}
+
     impl FreqTable {
         /// Creates a new instance of [`FreqTable`] from [`Table`].
         pub(crate) fn new(table: &Table) -> Result<Self> {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-04  1:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  1:08 [PATCH v2] rust: cpufreq: Add `Send` and `Sync` bounds for `Driver::PData` Priya Bala Govindasamy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox