From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=117.135.210.7 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796019375; b=i+RiW6j6QFhTo+vPvI67fKbOg7URnrUywTw0UxsonppmPTyT9dKJ7hcyS5kC7bPyCwh0 Oa4WDYAy0Nk5qGYHubMZtKdrdzrfrz21rPUJfCI4Tum9IkRxK/cXFMgsGRm0tt4+SsCdl i8BmsbbiEixAutH9Zdl1ggToeV/DG1HSRXQdTHQT7h19MQxbHF/yDf86X1xB4ZSJPSiI3 JbooXF6CwtP7UVDEa8Lzu/FA0IT67lrQPXMgjJ1rNzr0QLiGDSMs+EI8NOlPzdKHH1rf5 I58WZmyiPKOn5saZ8x470ZSzm/2QZvjU8wYnDp7Oe4djlsnJtZdzq9E3A507rZYWz3g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796019375; h=DMARC-Filter:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID: Content-Type:Content-Transfer-Encoding:MIME-Version:X-CM-TRANSID: X-Coremail-Antispam:X-CM-SenderInfo; bh=1bUu4yB+kVBxZERtJ8dCUTCBELVX3jW40yvNzJluW8Y=; b=0wKKvcZUMstCV7rXzfwM3AIzHUelgMuZRU5UTG59VlCIFtQ9CN+6u48Qvq7BLNhHOTNW XGG4qoRabL7mFE8J6VPH72Q2HYh/x18Gr70nlMZU+Yv5j/loKpNOVhOMaTUhUuIk+X7LD aumrzF7gCxkwOQY84DiaRHPSeuYmlFOekNN+NF2AokCpQRXhG65XWZzkN4sYWZ+DvZXfz eZijRjA9hGrr0ka+m6VVT3GEJsvbE0JwVOW7CnkxXVcUsoJ4UAPKX2zJFZEv18qfvqS9M yb7CVNoJFz2kZ1xH8IKLahocq0s/PZRRyITGFhqgUnzLU3OLNBNCub1gr0tU3vWASjg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=126.com; spf=pass smtp.mailfrom=126.com; dkim=pass header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=FP+XjCwd; arc=none smtp.remote-ip=117.135.210.7 Received: from m16.mail.126.com (m16.mail.126.com [117.135.210.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC0B7606A320; Mon, 30 Nov 2026 06:16:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AC0B7606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=126.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=126.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=FP+XjCwd DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:To:Subject:Date:Message-ID:Content-Type: MIME-Version; bh=1bUu4yB+kVBxZERtJ8dCUTCBELVX3jW40yvNzJluW8Y=; b=FP+XjCwdIygqZW0qFVW4mnBx/jEph71K11dMFJpIjqMbBjcsBpysc2eUgkjBOC z7tVJTs8luFDfhWzuAL+E3E+uNEgrbngdxhnM1Oa7H0SpT8Ny0tGJxUXCQcDPD2A L/H/MyfbZopxeejAVnhlUV1LIDxEDYdkLSFbvWjNZFRPc= From: Bilal Hashim To: Viresh Kumar , "Rafael J. Wysocki" Cc: Zhongqiu Han , Miguel Ojeda , Alice Ryhl , linux-pm@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] rust: cpufreq: mark Policy::data() as unsafe Date: Mon, 30 Nov 2026 14:15:14 +0800 Message-ID: <179601931477.53916.8421457569118687551@126.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-CM-TRANSID:_____wD3h7F1FA1rIKcoEQ--.57299S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxXw47Wr48Ary8tw4rAFWDArb_yoW5WrWkpF WUWrWjyry8tFsrtwsrCr409a4Ykan7XrWUKryDG3sYvrn8AF1jqw1DKryrWF9Y9r4kKr4a yFn8Z3Waqan8tr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UfxhJUUUUU= X-CM-SenderInfo: 5dhh2vbf6rjloofrz/xtbBrBlB22sNFHnTQAAA33 Policy::data(&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::()` 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 --- 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::() 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::() else { + // SAFETY: `policy`'s private data was initialized with `Self::PData` in `Self::init()`. + let Some(data) = (unsafe { policy.data::() }) 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(&mut self) -> Option<::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(&mut self) -> Option<::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()) }) } }