From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796103963; b=YVcGpi8Eg+cq5/hn1DgEgTwjq7L4fYJ9cu++7Ao2mSsRQIL0CQ/z0hlM34d7WmOQ3522 TXXpRuBzaL9p5SXvzKWSZ30dnWzhod/nN/cfOHsmQ8gqXj5Ko7lve4npoW2dKuq9qoINB b0y7vxhpADxWlGGfDHE3zjl20ID9CngO60LPulUi2L/eHW4w0SBXfRf6TB+8DH4oM2ALb uLYg0+6pZizAIQ0YPC3pSnWYWujJLtKPkMlluXtFBkuBV5g5Pj6oaGZJbb2dJ7XbUt0hc oHNYrFU5jJjxM98UUY9V31EHzbLAWYPEaARK+TBzlgqWB5t1XzpcDbCPddGF2Afo0Mw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796103963; h=DMARC-Filter:Received:DKIM-Signature:Received:Received: DKIM-Signature:X-Google-DKIM-Signature:X-Forwarded-Encrypted: X-Gm-Message-State:X-Gm-Gg:X-Received:X-Received:Received:Message-ID: Date:MIME-Version:User-Agent:Subject:To:Cc:References: Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding:X-Proofpoint-ORIG-GUID: X-Proofpoint-Spam-Details-Enc:X-Proofpoint-Spam-Info: X-Proofpoint-GUID:X-Authority-Analysis:X-Proofpoint-Virus-Version: X-Proofpoint-Spam-Details; bh=MKnq9we5tzYLZ/8jAB5T74iQsrvDDh4hb050EiPrDNw=; b=xIBRAN6pF4eGV4hiJ7PlNm/GEmwvB1OX6wB21KbPhRLdhcmTKlleXi4TCJNONqMQlKUl eCbH4F31XikVhcEFHFtg6eVwMmEI4Bp4zL+ZvMIYY7Tmv8e+HPVmppFJHH51LCGqzsp0d 4djYv4OCvwknzkm/eMOJo6KYYk6eBjTrJ8p/2SNKucgDQDRno5kfmbUhtRv4zxCzlj4g9 IvJQ6LrtfMlF1JpH3i7r2Gc7lIQFYouOu9ZBwU9PUEMeymTcoTQuzEAxlgglm7jFhvD0p yzJ+Ho89Zhh8P+FbWZTy8RGOekPx0U0IRwRobL/T1nyIp/orGFd/K8XTVWCfVfToMmQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=FQYr9r78; dkim=pass header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b="A//tlNaL"; arc=none smtp.remote-ip=205.220.180.131 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (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 EA217606A31A for ; Tue, 1 Dec 2026 05:46:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EA217606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=FQYr9r78; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=A//tlNaL Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B12L3t32580361 for ; Tue, 1 Dec 2026 05:46:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= MKnq9we5tzYLZ/8jAB5T74iQsrvDDh4hb050EiPrDNw=; b=FQYr9r78B7zFeHE/ GKtYt3//u/Wt14E0c6AQGkiKs6XgQZilGed6057Bwv/ZSoFqLg+dN16y4lWaOvZr iA8mId6wqd1V7LPBrCxDovGpdEtvenRThszcpny8EtIU9/fV801fQWKfopv+4GgJ ZwC5Ueu3xwlOk1q/v/jDKymW0tT+oU5pBI/tc+0EP/+XIQ65dhZc4kBJjkFRSRKx wxSZgk6gTp19CMFGNjIOcQs+X/rUCnNRJWmFzuYJ3EbrWHKEHD0IUXGUJF9PUgqd uODd96L07GG/S+yiTpMkcXsxu9QrMV9kr8tg4CZ+bpII1JGvl0QCakDovrJqSsGU i2Khfw== Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4j9cd3jsug-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Tue, 01 Dec 2026 05:46:01 +0000 (GMT) Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-3b4b76a2b95so9897126a91.1 for ; Mon, 30 Nov 2026 21:46:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1796103961; x=1796708761; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=MKnq9we5tzYLZ/8jAB5T74iQsrvDDh4hb050EiPrDNw=; b=A//tlNaL6JORXGsr34syRXEX4BirmsskXShFqRRA0XMc++NVLO7KuE03LhGapn8uG8 MXiJPIXjcobOD7b2tsods5NsE4OKkVqGGouFolN7s6i0B18D3hDV22eVqtLE1t7Qoy4/ s1ET2QunNyxQGa89SzGWoCGUWCo5Hbcpxh0GonpD46xem0fuleinkuWwqGlU9o/UWv0V teBRfeNrVIu5CSTuX/RsMlzdtaY9QEWIhWfsJH3z/kQf2ft7iANTMnBMw0iliSZJTSx4 O/5EBSd0THRgnN3Ewu8oPb/nM8cdzXYDWFyr8RCCvSwrqErB7H2d7bflmOz8F8Ev/FDx GxuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796103961; x=1796708761; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=MKnq9we5tzYLZ/8jAB5T74iQsrvDDh4hb050EiPrDNw=; b=k3XyMe9842W2Dm/u62lcEztqR4mSh2PECl7V//t8Hi/DMOHWLccYTMsgxY6opQ/8kS NHnHd7hKoRfBvL8SNrwVX9ykYjubMFZHGvQ8W2UkUhC1kmo2T7yeUiT6WukWKzz5M7cf Np693WIYwMj1K9E9pMDEyhzyuOPXWeleamF/gjbndHFh7a3E2rZvhRTRgr7PK9JBrCUO pkM3ljbK/Mr14aplSV3etNVn8fjGdefbEd+uXYQs9630ohdPv7Cpanr3TqtnnrPAGfEr jXvWgkGPw9kAoZclBK8nd6wPSFG2XCKERKuu/IEjY7iqgHJAwFy57lsiSbgtNiKnxND/ bYUQ== X-Forwarded-Encrypted: i=1; AL+hErFLFneG9LYHQunzUOuA1/fTKpTHNYtCuVKgOSEY3D9MTXVOKtoDVW6McibZHCUgIsmy1drvrSwH63nQJck=@vger.kernel.org X-Gm-Message-State: APuU4Z+Jj0JdOvIHWRNi2HyKVAUnYodKbSoloA+2rBgO7KKdXUPRKg81 bVerNrc7VVOkNsv08yoRryLXjkpt2XAd+P+qnxQaJX9EiZQNLlN9ftLsXwNvnMH+7rNW7qptFsH sdPo7Fxf0e7k3hF2o119+LSqHz2wgQCRUWdsZWsPOOysOFQysb7M2pGTtAh26dSRV5aQ= X-Gm-Gg: AZFdTZfeAXcutjgIgOhhncJ3zxz8BLsBkaLscxjkagePaJIGl08+F79ui25x+qD5Df/ 0LjBPNQg33fBYWac/e1DNl4aW6FU0mvvEAVwpFnTi2r+riWjueGNCIw+qvMcPrlU81O1WFXogF1 DPnESeTx2t/B0mzNjdwHywu4h4uiEWsynKEAVS/i2fC+7prXWyxdwIWp7quX358myCRUzgstxvo w8ycXDvZ212E2bFA2qYQFkBUssY9j6w8mdjC1IYiSRE8L1Rqs3b233q+8s5cjHI4JBTYfxBvRxZ itmCiW/SUrkA93lQw61kDGycwe9FQCE/PRx8XhwPk9rZ69iALIxI+SaEglNLLxILEPpB3kIPlOk SYHe5IyfoD8pCEGY4Of/laZ1cf4WoMRVcNx/LQr+mARdeL6nfVUyIjr+VxKxoSqPPRzZHTdtqwN hv43jN2CV6tADjZshX9JhlKCH5K0rmyyx0jWjxZMJWXRUE X-Received: by 2002:a17:90a:ec88:b0:3b4:127a:2bb9 with SMTP id 98e67ed59e1d1-3b41c8d9275mr35827966a91.13.1796103960687; Mon, 30 Nov 2026 21:46:00 -0800 (PST) X-Received: by 2002:a17:90a:ec88:b0:3b4:127a:2bb9 with SMTP id 98e67ed59e1d1-3b41c8d9275mr35827942a91.13.1796103960160; Mon, 30 Nov 2026 21:46:00 -0800 (PST) Received: from [10.133.33.245] (tpe-colo-wan-fw-bordernet.qualcomm.com. [103.229.16.4]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b40578dca0sm59121401a91.2.2026.11.30.21.45.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Nov 2026 21:45:59 -0800 (PST) Message-ID: Date: Tue, 1 Dec 2026 13:45:55 +0800 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 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3] rust: cpufreq: mark Policy::data() as unsafe To: Bilal Hashim , Viresh Kumar , "Rafael J. Wysocki" Cc: Miguel Ojeda , Alice Ryhl , linux-pm@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Zhongqiu Han References: <179601931477.53916.8421457569118687551@126.com> Content-Language: en-US From: Zhongqiu Han In-Reply-To: <179601931477.53916.8421457569118687551@126.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Proofpoint-ORIG-GUID: 5GpdHfq10eMp46JuUobAwi9YaFDe_sEL X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjAxMDAyMiBTYWx0ZWRfX0shZdsAaUFAz KktmZhsRo2RCmcw5lPufExrZQwAB1USr5199Ej+sK/S1wW3LArsWdXcTgN5kzBnWZgwY7mUE4on y1ZCsLEAHHal7o/iMtC9/+PUZtwrlsOJnngFutfizot4xP5rvjh9WIBtlIAChHNhkp+/dqs6ynY KRg8sRS0W8InIXgZObNRSX7/pYnZK8coqJ/kUjX2HhJsyejoTe2u0pPEtj8ezEsyrP3hi678MqW F/eESTXrCvS7ZJO4jdjLA2mqoCkGmOT3XdUGCQFqpu/BVsh58K0fkCUdPjimj47iUX7hIJfont+ xP4+rhsbTno28XDkCI2Gq7gyMywBZhRb0fl7HA7tFQ56oWhfe5Tq4DwDeWb7tzHKElhdF25rhpV REqNvB8bLBaYbhWPx9WIi/hui9dEu6SQAsARhndowHyNSQ94ZtrKAJmsJ0E34RqPjvpqTwIFCDM gwznvaeO+3OWdcTP8eQ== X-Proofpoint-Spam-Info: AW1haW4tMjYxMjAxMDAyMiBTYWx0ZWRfXyIgcXBPbiSxA 43Cq38B9wsuh+Tk9gudgK7xMx7IRStiucsSyQ0UUWC9xvQuamiVY9fesIe/yIx+Kv5kls5f8JuT wE/GwmB+GjpeWgWi9sAc7G0FhV0K1Bc= X-Proofpoint-GUID: 5GpdHfq10eMp46JuUobAwi9YaFDe_sEL X-Authority-Analysis: v=2.4 cv=HIhWhYtv c=1 sm=1 tr=0 ts=6b0e5f19 cx=c_pps a=vVfyC5vLCtgYJKYeQD43oA==:117 a=nuhDOHQX5FNHPW3J6Bj6AA==:17 a=IkcTkHD0fZMA:10 a=pUM-KoujPXoA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=rJkE3RaqiGZ5pbrm-msn:22 a=gnbuhwpKAAAA:8 a=EUspDBNiAAAA:8 a=ZLrEuAr-wMdrsATRgLgA:9 a=QEXdDO2ut3YA:10 a=rl5im9kqc5Lf4LNbBjHf:22 a=HL_VsaX-4L8esMsM0cMV:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-01_01,2026-11-30_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 suspectscore=0 priorityscore=1501 malwarescore=0 bulkscore=0 lowpriorityscore=0 spamscore=0 clxscore=1015 phishscore=0 adultscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612010022 On 11/30/2026 2:15 PM, Bilal Hashim wrote: > 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 Reviewed-by: Zhongqiu Han > --- > 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()) }) > } > } > -- Thx and BRs, Zhongqiu Han