Linux Power Management development
 help / color / mirror / Atom feed
From: Priya Bala Govindasamy <pgovind2@uci.edu>
To: rafael@kernel.org, viresh.kumar@linaro.org, ojeda@kernel.org,
	boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com,
	lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com,
	tmgross@umich.edu, dakr@kernel.org, daniel.almeida@collabora.com,
	tamird@kernel.org, acourbot@nvidia.com, work@onurozkan.dev,
	nm@ti.com, sboyd@kernel.org, zhongqiu.han@oss.qualcomm.com
Cc: linux-pm@vger.kernel.org, rust-for-linux@vger.kernel.org,
	ardalan@uci.edu, zhiyunq@cs.ucr.edu, dzueck@uci.edu,
	pgovind2@uci.edu
Subject: [PATCH v2] rust: cpufreq: Add `Send` and `Sync` bounds for `Driver::PData`
Date: Fri,  4 Dec 2026 01:08:23 +0000	[thread overview]
Message-ID: <20261204010823.10596-1-pgovind2@uci.edu> (raw)

`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


                 reply	other threads:[~2026-12-04  1:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204010823.10596-1-pgovind2@uci.edu \
    --to=pgovind2@uci.edu \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=ardalan@uci.edu \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dzueck@uci.edu \
    --cc=gary@garyguo.net \
    --cc=linux-pm@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nm@ti.com \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=viresh.kumar@linaro.org \
    --cc=work@onurozkan.dev \
    --cc=zhiyunq@cs.ucr.edu \
    --cc=zhongqiu.han@oss.qualcomm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox