mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
To: Bilal Hashim <AeeSec@126.com>,
	Viresh Kumar <viresh.kumar@linaro.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>
Cc: Miguel Ojeda <ojeda@kernel.org>,
	Alice Ryhl <aliceryhl@google.com>,
	linux-pm@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
Subject: Re: [PATCH v3] rust: cpufreq: mark Policy::data() as unsafe
Date: Tue, 1 Dec 2026 13:45:55 +0800	[thread overview]
Message-ID: <be8af4cd-3617-40ad-beb9-1e9fa538fdc4@oss.qualcomm.com> (raw)
In-Reply-To: <179601931477.53916.8421457569118687551@126.com>

On 11/30/2026 2:15 PM, Bilal Hashim wrote:
> Policy::data<T: ForeignOwnable>(&mut self) is currently a safe public
> method that casts the untyped C pointer `policy->driver_data` via
> `T::borrow(...)` for any caller-chosen type `T`.
> 
> Because `Policy` is not parameterized by `Driver::PData` and performs no
> runtime type verification, any safe Rust caller with `&mut Policy` can
> invoke `policy.data::<WrongType>()` and reinterpret `driver_data` as an
> arbitrary `ForeignOwnable` type, causing type confusion and undefined
> behavior in safe Rust.
> 
> Mark Policy::data() as `unsafe`, document the safety precondition that
> `T` must match the exact `ForeignOwnable` type previously stored in the
> policy's `driver_data`, and update the in-tree caller in
> drivers/cpufreq/rcpufreq_dt.rs.
> 
> Fixes: 6ebdd7c93177 ("rust: cpufreq: Extend abstractions for policy and driver ops")
> Signed-off-by: Bilal Hashim <AeeSec@126.com>

Reviewed-by: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>

> ---
> Changes in v3:
> - Regenerate patch with git diff to fix corrupted hunk line counts in v2
>    (thanks to Zhongqiu Han for pointing this out).
> 
> Changes in v2:
> - Update caller in drivers/cpufreq/rcpufreq_dt.rs to wrap
>    policy.data::<Self::PData>() in an unsafe block with a SAFETY comment.
> - Add Fixes: tag.
> ---
>   drivers/cpufreq/rcpufreq_dt.rs |  3 ++-
>   rust/kernel/cpufreq.rs         | 10 ++++++++--
>   2 files changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/cpufreq/rcpufreq_dt.rs b/drivers/cpufreq/rcpufreq_dt.rs
> index 2b1f18b..32d8ce8 100644
> --- a/drivers/cpufreq/rcpufreq_dt.rs
> +++ b/drivers/cpufreq/rcpufreq_dt.rs
> @@ -178,7 +178,8 @@ impl cpufreq::Driver for CPUFreqDTDriver {
>       }
>   
>       fn target_index(policy: &mut cpufreq::Policy, index: cpufreq::TableIndex) -> Result {
> -        let Some(data) = policy.data::<Self::PData>() else {
> +        // SAFETY: `policy`'s private data was initialized with `Self::PData` in `Self::init()`.
> +        let Some(data) = (unsafe { policy.data::<Self::PData>() }) else {
>               return Err(ENOENT);
>           };
>   
> diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs
> index f316711..e6b13d5 100644
> --- a/rust/kernel/cpufreq.rs
> +++ b/rust/kernel/cpufreq.rs
> @@ -646,11 +646,17 @@ impl Policy {
>       }
>   
>       /// Returns the [`Policy`]'s private data.
> -    pub fn data<T: ForeignOwnable>(&mut self) -> Option<<T>::Borrowed<'_>> {
> +    ///
> +    /// # Safety
> +    ///
> +    /// The caller must ensure that `T` is the exact [`ForeignOwnable`] type previously stored in
> +    /// this [`Policy`]'s `driver_data`.
> +    pub unsafe fn data<T: ForeignOwnable>(&mut self) -> Option<<T>::Borrowed<'_>> {
>           if self.as_ref().driver_data.is_null() {
>               None
>           } else {
> -            // SAFETY: The data is earlier set from [`set_data`].
> +            // SAFETY: The caller guarantees that `T` matches the type previously stored via
> +            // [`set_data`].
>               Some(unsafe { T::borrow(self.as_ref().driver_data.cast()) })
>           }
>       }
> 


-- 
Thx and BRs,
Zhongqiu Han

  reply	other threads:[~2026-12-01  5:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  6:15 Bilal Hashim
2026-12-01  5:45 ` Zhongqiu Han [this message]
2026-12-01  7:40   ` Viresh Kumar

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=be8af4cd-3617-40ad-beb9-1e9fa538fdc4@oss.qualcomm.com \
    --to=zhongqiu.han@oss.qualcomm.com \
    --cc=AeeSec@126.com \
    --cc=aliceryhl@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=viresh.kumar@linaro.org \
    /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

all inboxes | Powered by JetHome®