From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796171331; b=JcRQb/Pt+uklKauyZtn2vogU20mEDSOnWhvUqHxmLbNrqX55j/lg8XHSWXvtOrs8Z55O sL8VyBpwDFJfKcO6Qjuq+SgooZyORwCqE728QMljIfx+ftvhDxw/jdsdB2xBnAF3jfFyt 40HJxKc7hfAsPs1NF8Tf0JkGUcFiWTlcDvoSWpnBxVgU+IPd9r2wKX8C7WHlMcdwHhzY+ 9SqtOI6d+vQkICKegJeB7fm0h88twl3kUJ2nsEMgXO3hyqd31BjrWmVoF7iBWiuRVpNUz hKSigtTBhdq3iZPiMrjMjkH9qntqhYYltq+HUiM/en2I9HELbHmphRHRgb9lt04tgag== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796171331; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-Id:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=H/jwEBfYCYn7M0YSsdA0/wwMJEZQekuoy11kZpUH0TM=; b=264w7vwhmyUtqkUflfZbwZ2sgVaZXo/lCd6BleVQ6XTtmvEHJ2eVnWFwdgPgSX9wRXLf Ib+Y9MjldDj9T63JF3UmP7lkwoNbF1CbL81az2XuJRwlnGPs12tyhhb88t9KT3dTHTLPQ ByEWeMsxDohzT2fYIjUnVp5zUn+P1vPThXLoWcHjW1qzFogQn5VfL8sYzWok8+CGfy1T5 LWwhhnJIHH/qya2WqRpGoY1ZMiC8HDgvYVRK4pHdibPGFzRz6ZGnq0IHij8uCsN5xwq2J b8guCsNsnzOKwEc87ubQ1eaNNuNQDqrQDEKZDRNzoIGWhQ9pzPu46hagxkklse4h6FA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=uci.edu; spf=pass smtp.mailfrom=uci.edu; dkim=pass header.d=uci.edu header.i=@uci.edu header.a=rsa-sha256 header.s=google header.b=DoTJNNA+; arc=none smtp.remote-ip=74.125.82.43 Received: from mail-dl1-f43.google.com (mail-dl1-f43.google.com [74.125.82.43]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E5F1606A320 for ; Wed, 2 Dec 2026 00:28:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5E5F1606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uci.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=uci.edu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=uci.edu header.i=@uci.edu header.a=rsa-sha256 header.s=google header.b=DoTJNNA+ Received: by mail-dl1-f43.google.com with SMTP id a92af1059eb24-1a2af4c8d00so1559070c88.0 for ; Tue, 01 Dec 2026 16:28:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uci.edu; s=google; t=1796171329; x=1796776129; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=H/jwEBfYCYn7M0YSsdA0/wwMJEZQekuoy11kZpUH0TM=; b=DoTJNNA+CYUGnH3GaGxiUZzFr6N5BkQTbzy8USj2msL9KDsAwMt4ex31lAtzlQ+lsj 9Efnk2tjeBYAjRIzgGJLfC1WpIy35+Dn1hStAx2/Cd/HNZamvAoC+dOIBY+RpIVVhoNW 2EX6Y0jhXMvqll6jEY0InYO4mTVWu02+yVTOuX5cYDPltTnbnW6lpsX77cTjiAleWffU xnegWPofGkJLmIprFKNGDoI3HPl+Zf/VuAofih2Y9zw4aKZ6GoBQhlX4Eha39whPnoTd Zfb062nqaw8DHb088FaDzH225JenV0ArFyzgH/FJ86JmN4W9C1gV6g66NSMTv0V167Kq TINQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796171329; x=1796776129; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=H/jwEBfYCYn7M0YSsdA0/wwMJEZQekuoy11kZpUH0TM=; b=R8taah5eXmAvadgaAPxmmsPyjNr6E4CEVRPu1fmcIulHEVHz9LLglynPSkxfUCSsTL A0+Eg6IxTiKgNQIkFiqTHyXeoD9rMi0N/HmXN7cyzciNWDinMJLvQ0wA9E9x512MVunU 96gEsaTsq462cGsFcCC4fCN1B1XQsZgQOsKCuIh98LXKLLWPJyQtReiyKi5pXXO/fVYy ThnGSMBDQW4+aM3knCCPWNg/WPv5iblGT8me8ZMVcugR1qVgK2NBi2U/tMPvs9FtcEtO d94if53M8Mt5LNbqCKJt0XU5uAQhDeygG/VFFKcjiBmW6NOMkPolXY+BJzor/BWbRnOq UqBw== X-Gm-Message-State: APuU4Z+s8Puykhf8P1YQdDNHhxyOLu6+co+vRW7gL1KaNP3UYY1GFB6W Exr09W4sx9uKyfRiZhKc9WYl40fcWp+viio/ZUn/a6mrYx0PJSmZ5PfwsteA98Q5eTU= X-Gm-Gg: AZFdTZegqULzugp40hxyimJc+DEza1KIdAdnCjNyiSRtmYJKUbwA2GbI9l1P5Oh87jh ER1L3m+I/AAWJ76fgby5nhiuSJQnVNT6Tos0A38lRBJd2fclZXZGLzLI5qFV/iofCRaVXLuM4kQ gBd7mXZNFz6BWxASMVsTnx3YMPbUBf9uNLFQGZ/jMuQagpwCrBc/0glavGsGztv29PRSxyF06J2 1HFdkDj1Yn5pRIH9VpLfr1khN47IWWxqplJ6DoPd/P4OfNi+x21iR4ZTyzlTwG4Qyob3CjgK13l ScyU8XgpQs9OBJic9AkAf0pgQWfL+HwVzphLj5v/QLWVJ0KptsTQcvlLRKm5BbTmE0Jxs5011J4 B+jjvXxQhYLvl/6v1BQn0ZSKQYTjDrgvsljl6A9f+gRoBEEjEs4U3cG9xzbDZiWWP8mZotXmCJd V/AAhMdp+5FJjYzJXqWvioWWIjK877J4mXgk/J0xTa+WZnLCvQDTQrXt8+UiKeqDfORBA7hVL1b 92ZPPOjak4w658wlIySh/o20ZVbtzrewCbHSCpNkfBrpCAo61As2V8iq2mNy4cBgOZip2U+Q4PY fe7Fr7GK0v8kfjDrOi8LyS2lL/gr9p1becYqt2/SQclZge2ZI/Y7GS3VjdxFO4DYUoG4 X-Received: by 2002:a05:7022:23aa:b0:1b0:166c:6a94 with SMTP id a92af1059eb24-1b5568e5983mr50198512c88.28.1796171329476; Tue, 01 Dec 2026 16:28:49 -0800 (PST) Received: from guest1.. (charm.ics.uci.edu. [128.195.4.118]) by smtp.googlemail.com with ESMTPSA id a92af1059eb24-1b55684c85fsm138884772c88.14.2026.12.01.16.28.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 16:28:49 -0800 (PST) From: Priya Bala Govindasamy 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 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 0/1] rust: cpufreq: Add Send bound for Driver::PData Date: Wed, 2 Dec 2026 00:28:47 +0000 Message-Id: X-Mailer: git-send-email 2.34.1 X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Dear Linux kernel maintainers, We are developing a tool called FerroLens to detect potential unsound behavior in Rust code in the Linux kernel. The tool internally uses an LLM to detect bugs. We then perform manual analysis to verify these reports. FerroLens reported the following bug in rust/kernel/cpufreq.rs: `Driver::PData` is required to implement `ForeignOwnable`, but currently is not required to implement `Send`. The `Registration` type is `Send`. During registration, the `init_callback` obtains `PData` and transfers it to the C-owned `cpufreq_policy`. While `Registration` does not itself directly contain `PData`, moving the `Registration` to a different thread could then allow the other thread to run `Registration`'s `Drop` implementation. The `Drop` implementation calls `cpufreq_unregister_driver` which may invoke Rust's `exit_callback`, which can lead to gaining ownership of the `PData` on another thread and passing it `Driver::exit`. Therefore, `PData`, a type which may not implement `Send` could be accessed from a different thread and potentially dropped there. This is unsound. Below is a PoC kernel module that demonstrates the issue. `SampleDevice` is not `Send`, due to the presence of the `NotThreadSafe` marker, but is used as `Driver::PData`. This module registers a cpufreq driver and then moves the registration to a worker thread. The worker thread then drops the registration, which invokes the `exit_callback` and accesses the `SampleDevice` instance from a different thread, which is unsound. --- use core::sync::atomic::{AtomicU32, Ordering}; use kernel::{ cpufreq, macros::vtable, clk::Hertz, sync::{Arc, Completion}, str::CStr, workqueue, prelude::*, types::NotThreadSafe, }; module! { type: CpuFreqPocModule, name: "cpufreq_poc", authors: ["Priya Govindasamy"], description: "PoC cpufreq driver for the Rust Driver::Pdata bug", license: "GPL", } struct CpuFreqPocModule { worker: Arc, } #[pin_data] struct WorkerState { #[pin] started: Completion, #[pin] stop: Completion, #[pin] stopped: Completion, } struct SampleDevice{ table: cpufreq::TableBox, current_freq_khz: AtomicU32, marker: NotThreadSafe, } impl Drop for SampleDevice { fn drop(&mut self) { pr_info!("cpufreq_poc: SampleDevice dropped on pid {}\n", kernel::current!().pid()); } } #[derive(Default)] struct CpuFreqPocDriver; const LOW_FREQ: Hertz = Hertz::from_mhz(1000); const HIGH_FREQ: Hertz = Hertz::from_mhz(2000); #[vtable] impl cpufreq::Driver for CpuFreqPocDriver { const NAME: &'static CStr = c"cpufreq-sample"; const FLAGS: u16 = cpufreq::flags::NEED_INITIAL_FREQ_CHECK | cpufreq::flags::IS_COOLING_DEV; const BOOST_ENABLED: bool = true; type PData = Arc; fn init(policy: &mut cpufreq::Policy) -> Result { let mut builder = cpufreq::TableBuilder::new(); builder.add(LOW_FREQ, 0, 0)?; builder.add(HIGH_FREQ, 0, 1)?; let table = builder.to_table()?; policy.cpus().setall(); policy .set_min(LOW_FREQ) .set_max(HIGH_FREQ) .set_cpuinfo_min_freq(LOW_FREQ) .set_cpuinfo_max_freq(HIGH_FREQ) .set_suspend_freq(LOW_FREQ) .set_transition_latency_ns(cpufreq::DEFAULT_TRANSITION_LATENCY_NS); // SAFETY: The table is valid and will live until cpufreq teardown. unsafe { policy.set_freq_table(&table) }; Ok(Arc::new(SampleDevice { table, current_freq_khz: AtomicU32::new(HIGH_FREQ.as_khz() as u32), marker: NotThreadSafe, }, GFP_KERNEL)?) } fn exit(_policy: &mut cpufreq::Policy, _data: Option) -> Result { Ok(()) } fn suspend(policy: &mut cpufreq::Policy) -> Result { policy.generic_suspend() } fn verify(data: &mut cpufreq::PolicyData) -> Result { data.generic_verify() } fn target_index(policy: &mut cpufreq::Policy, index: cpufreq::TableIndex) -> Result { let Some(data) = policy.data::() else { return Err(ENOENT); }; let table = &data.table; let freq = table.freq(index)?; let freq_khz = freq.as_khz() as u32; data.current_freq_khz.store(freq_khz, Ordering::Relaxed); Ok(()) } fn get(policy: &mut cpufreq::Policy) -> Result { let Some(data) = policy.data::() else { return Err(ENOENT); }; let freq = data.current_freq_khz.load(Ordering::Relaxed); Ok(freq) } } impl kernel::Module for CpuFreqPocModule { fn init(_module: &'static ThisModule) -> Result { fn assert_send(_: &T) {} let driver = match cpufreq::Registration::::new() { Ok(driver) => driver, Err(err) => { pr_err!( "cpufreq_poc: cpufreq driver registration failed: errno={}\n", err.to_errno() ); return Err(err); } }; assert_send(&driver); let worker = Arc::pin_init( pin_init!(WorkerState { started <- Completion::new(), stop <- Completion::new(), stopped <- Completion::new(), }), GFP_KERNEL, )?; let worker_task = worker.clone(); pr_info!( "cpufreq_poc: moving registration from pid {} to a worker thread\n", kernel::current!().pid() ); workqueue::system_long().try_spawn(GFP_KERNEL, move || { // Moving `driver` into this `Send + 'static` closure is the point of the PoC. // The registration remains owned by this worker until module unload. pr_info!( "cpufreq_poc: registration is now owned by worker pid {}\n", kernel::current!().pid() ); worker_task.started.complete_all(); worker_task.stop.wait_for_completion(); drop(driver); pr_info!("cpufreq_poc: registration dropped by worker thread\n"); worker_task.stopped.complete_all(); })?; // Do not finish loading until the worker has actually taken ownership. worker.started.wait_for_completion(); Ok(Self { worker }) } } impl Drop for CpuFreqPocModule { fn drop(&mut self) { self.worker.stop.complete_all(); self.worker.stopped.wait_for_completion(); pr_info!("cpufreq_poc: unloaded\n"); } } Output: [664276.708338] cpufreq_poc: cpufreq_poc: moving registration from pid 102668 to a worker thread [664276.710582] cpufreq_poc: cpufreq_poc: registration is now owned by worker pid 68702 [664280.102311] cpufreq_poc: cpufreq_poc: SampleDevice dropped on pid 68702 [664280.106921] cpufreq_poc: cpufreq_poc: registration dropped by worker thread [664280.107033] cpufreq_poc: cpufreq_poc: unloaded Priya Bala Govindasamy (1): rust: cpufreq: Add Send bound for Driver::PData drivers/cpufreq/rcpufreq_dt.rs | 7 ++----- rust/kernel/cpufreq.rs | 10 +++++++--- rust/kernel/opp.rs | 5 +++++ 3 files changed, 14 insertions(+), 8 deletions(-) -- 2.34.1