From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796171330; b=AKwhKXPlCKH0vi4MQ45+XFF4UWearmhL4upX1Wyq3hNUFedfHFLeRnpQ4qrYuxbUGC30 05S2+oy2Xy13PWVzda0U2Gw/DXR3jjaTJDIXM4hiGNm3w4p5UCUWaJc42S1Weqeutffir tOIB3D4R71HNqXhvqReHwikk6LcNUF3ynRaXK0gRpAjuPHTvGkBU9V8vs3satvChP0LSL cO46h7jSkiGMj1eg7dj5ZHaBUIslwtqxBXQadDMGnKpEOlhNj32tog4ZC9aIoFKE2CyYU gnCmA34ugft+VE8BZ6nG1IMIveeogsvEKb2q8iLKxiF1LtBVjcxDnv4Dg/BrxTKtUnA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796171330; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted: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=VoKuDjA1gVBf0ebkmsJ2bYLEM9HKYlxnih1ADYGZqvz+t6451Y+BaIaT0c6RATVyepxG HsiRYCLDy0frbpLMAgRtvgIFEnlCL4ahOgEMjaxE0y/kiEXVMeYEtYkV2JFS41IueqBQL Sl8AGfQqM6+337XpZawuxQ2N/Ani23IqTPjKOmyavGhtPfHwuqMPewOMwbMyVmHCe4JxR eOHko7G+PFCDa0iTCohaRh5lcq7LsLmfg8QZhnG0xtNgjC783a7ZDGHC5SyvFIozSVNz2 4jgtymSjIbNOnRk/btNZvgV//Npcdk3XmatJ8k43u2J6JUJR1fhWlmpUpPrs0e9R71g== 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.42 Received: from mail-dl1-f42.google.com (mail-dl1-f42.google.com [74.125.82.42]) (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 58FDA606A31A for ; Wed, 2 Dec 2026 00:28:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 58FDA606A31A 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, unprotected) header.d=uci.edu header.i=@uci.edu header.a=rsa-sha256 header.s=google header.b=DoTJNNA+ Received: by mail-dl1-f42.google.com with SMTP id a92af1059eb24-1a2af4c8d00so1559063c88.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=QQ94qf9F830me2WvFdtRm/x819ftMDMcjHvtZJ4p3zZz+Xl7slsItNu552vR/RPqGx OaQ5A5kfKQl638AVbnAlgBtjReVE+LeRgmhrF2187+aVPlHVsT+MoyiKtd3ZaaU11y7L 2CZ7K+A5SkAx2hcWtWNvabQtSHlRAKD0yHwilOzVkK3tTxc4vs0fU+7aujZAhbJHfOle SPp/tQ1UhSggQQ/MJIdqw+ogMqrHLj689ByDKRjrel7p8N2i+NkcPjLojE6qw8p5aes5 SQwITFtI3ioAFXebXIWsDxCTlzxGA+PqMk9JKAU1hc6ZZwZkhA5BmU3LWyL0IFVfG6DI J79A== X-Forwarded-Encrypted: i=1; AL+hErEXcjofyxrNgMbV7fsGyZ57x+gCFQJTl5PON9q9S+QOdoTe6Q4MQZNj/ucOK/2+67+Lq0nOn5Q47G3CHhwEsQ==@vger.kernel.org X-Gm-Message-State: APuU4Z9yhESbi5EXwiY5vyl9eXX4KcteyixW6mftdfpo4bv8J7YklPXp +b+flrWOZrQgozbenrNf7YVZQmu4idYpboYkbKt05beEnhQTXCBpkhsnnRErCCx0BUs= X-Gm-Gg: AZFdTZc0C0C6LW4HlbEAVw5hTPjZXYct1h4iE139wnNn31jCdWfmvrNJOHIHQDxj4uz rNi3bdSuaouFLdmIWM3JdPRLajf7yV1xsDIDtobGFFBTc0kHTexl6fOJWqQal8bYwral2PATA5u thVLgQrVHUrsqxR/wkxKbRxOkjECNLLa4uIRREj0EAXJb2ZUjLgomZWfLOGfPprPS7hZXAS9qVw 2km+nIwH+xA1IzzUBXyNW0WKPx/ZM6YEk0SJGKOd9JRUqYFT/LMmTLiYQ8FZeMdPRS4mnxB0YEV MKUjPJSyQc4kS7mqwFEvz7GEjSCkKbG28S6GF4W4jvwRJ3RZ/heETvwJ8owfDk8/Lx9cF6SfgxX viHvb6cC2z/7u4/MxwcAcHyMeEn9Z0C0eeYBOyRTdAMAW157VrETOHcUZu1gd2NSV41L9RowKs0 l+j3uvSFu1pL/DlvbRa3Zp9035cKvii5+TDSyPJKFAbFIuSBKyP+yOkaulyCi9WlwH+aBKB74Rc 49ZWHJnpcZJDiSeG2+imKJ6E9iqz+z898lQLuxf5w1A0R2wuG3VSFsVttGu+OyLr7LUtqSCdibi sGEqZPjNsLbzWNBQ0EsM8WO44LtRPZto6GXh5ycX5+MlnjpxU5ZzPLm5RvdiLO49C3O/ 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: rust-for-linux@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