Guest User

rust_poc_race.rs

a guest
Oct 1st, 2026
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 8.75 KB | Source Code | 0 0
  1. // SPDX-License-Identifier: GPL-2.0
  2.  
  3. //! Recursive read acquisition of `cpufreq_driver_lock`, via the in-tree Rust cpufreq
  4. //! abstraction and the in-tree `rcpufreq_dt` driver.
  5.  
  6. use core::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};
  7. use kernel::bindings;
  8. use kernel::cpu::CpuId;
  9. use kernel::cpumask::CpumaskVar;
  10. use kernel::prelude::*;
  11. use kernel::time::delay::udelay;
  12. use kernel::time::Delta;
  13. use kernel::workqueue;
  14.  
  15. module! {
  16.     type: PocRace,
  17.     name: "poc_race",
  18.     authors: ["spidermana"],
  19.     description: "recursive read_lock on cpufreq_driver_lock via rcpufreq_dt",
  20.     license: "GPL",
  21.     params: {
  22.         run: u32 {
  23.             default: 0,
  24.             description: "1 = hammer both sides (HANGS THE GUEST)",
  25.         },
  26.     },
  27. }
  28.  
  29. struct PocRace;
  30.  
  31. const NO_CPU: u32 = u32::MAX;
  32.  
  33. static READS: AtomicU64 = AtomicU64::new(0);
  34. static WRITES: AtomicU64 = AtomicU64::new(0);
  35. static READER_CPU: AtomicU32 = AtomicU32::new(NO_CPU);
  36. static WRITER_CPU: AtomicU32 = AtomicU32::new(NO_CPU);
  37. static STOP: AtomicBool = AtomicBool::new(false);
  38.  
  39. unsafe extern "C" fn dummy_verify(_: *mut bindings::cpufreq_policy_data) -> kernel::ffi::c_int {
  40.     0
  41. }
  42. unsafe extern "C" fn dummy_init(_: *mut bindings::cpufreq_policy) -> kernel::ffi::c_int {
  43.     -1
  44. }
  45. unsafe extern "C" fn dummy_setpolicy(_: *mut bindings::cpufreq_policy) -> kernel::ffi::c_int {
  46.     0
  47. }
  48.  
  49. /// Busy-waits for `ms` milliseconds instead of sleeping.
  50. ///
  51. /// `msleep()` arms a timer on the current CPU and blocks. If a worker then wedges that CPU
  52. /// spinning with interrupts masked, the timer never fires and the sleeper never wakes.
  53. fn spin_ms(ms: u32) {
  54.     // `udelay()` is only valid up to MAX_UDELAY_MS (5ms), so step in 1ms units.
  55.     for _ in 0..ms {
  56.         udelay(Delta::from_millis(1));
  57.     }
  58. }
  59.  
  60. /// Waits for `rcpufreq_dt` to bind;
  61. fn wait_for_policy() -> bool {
  62.     let mut ms = 0;
  63.     loop {
  64.         // SAFETY: FFI call, valid for any CPU number.
  65.         let p = unsafe { bindings::cpufreq_cpu_get(0) };
  66.         if !p.is_null() {
  67.             // SAFETY: `p` is the policy `cpufreq_cpu_get()` just referenced.
  68.             unsafe { bindings::cpufreq_cpu_put(p) };
  69.             return true;
  70.         }
  71.         if ms >= 5000 {
  72.             return false;
  73.         }
  74.         // SAFETY: FFI call, process context.
  75.         unsafe { bindings::msleep(50) };
  76.         ms += 50;
  77.     }
  78. }
  79.  
  80. /// NMI-backtraces exactly one CPU, so the reader's stack and the writer's stack land in the
  81. /// log as two separately labelled chains instead of one dump of everything online.
  82. fn backtrace_cpu(cpu: u32) {
  83.     let Some(id) = CpuId::from_u32(cpu) else {
  84.         pr_err!("  (no CPU recorded for this side; nothing to backtrace)\n");
  85.         return;
  86.     };
  87.     let Ok(mut mask) = CpumaskVar::new_zero(GFP_KERNEL) else {
  88.         pr_err!("  (out of memory, cannot build the cpumask)\n");
  89.         return;
  90.     };
  91.     mask.set(id);
  92.     // SAFETY: FFI call; `mask` is a valid cpumask and the callee only reads it.
  93.     unsafe { bindings::arch_trigger_cpumask_backtrace(mask.as_raw() as *const _, -1) };
  94.     // `nmi_trigger_cpumask_backtrace()` waits for the NMI to be taken, but the printk of the
  95.     // stack itself can still be in flight. Keep this short: see the sampling comment below.
  96.     spin_ms(200);
  97. }
  98.  
  99. fn report(reader_cpu: u32, writer_cpu: u32, reads: u64, writes: u64, ms: u32) {
  100.     pr_err!("*** DEADLOCK on cpufreq_driver_lock ***\n");
  101.     // The verdict can land before the first periodic progress line, so carry the evidence
  102.     // it would have shown: both counters stopped, and stayed stopped.
  103.     pr_err!(
  104.         "    reads={} writes={}, both flat for the last {}ms\n",
  105.         reads,
  106.         writes,
  107.         ms
  108.     );
  109.     pr_err!(
  110.         "    reader cpu{} holds it for read and is blocked taking it for read again;\n",
  111.         reader_cpu
  112.     );
  113.     pr_err!(
  114.         "    writer cpu{} is queued for write between the two acquisitions.\n",
  115.         writer_cpu
  116.     );
  117.  
  118.     pr_err!("--- [reader] cpu{}, expected chain: ---\n", reader_cpu);
  119.     pr_err!("      cpufreq_quick_get -> read_lock #1 -> ->get() -> get_callback\n");
  120.     pr_err!("        -> PolicyCpu::from_cpu -> cpufreq_cpu_get -> read_lock #2 (wedged)\n");
  121.     backtrace_cpu(reader_cpu);
  122.  
  123.     pr_err!("--- [writer] cpu{}, expected chain: ---\n", writer_cpu);
  124.     pr_err!("      cpufreq_register_driver -> write_lock (wedged)\n");
  125.     backtrace_cpu(writer_cpu);
  126. }
  127.  
  128. impl kernel::Module for PocRace {
  129.     fn init(_module: &'static ThisModule) -> Result<Self> {
  130.        pr_info!("poc_race: hammering cpufreq_driver_lock with recursive read_lock\n");
  131.  
  132.        if !wait_for_policy() {
  133.            pr_err!("rcpufreq_dt did not bind within 5s; cannot hammer cpufreq_driver_lock\n");
  134.            return Ok(PocRace);
  135.        }
  136.  
  137.        if module_parameters::run.value() != 1 {
  138.            pr_info!("pass poc_race.run=1 to hammer (this WILL hang the guest)\n");
  139.            return Ok(PocRace);
  140.        }
  141.  
  142.        // Reader: cpufreq_quick_get -> read_lock -> ->get() -> cpufreq_cpu_get -> read_lock.
  143.        workqueue::system_unbound().try_spawn(GFP_KERNEL, || {
  144.            pr_info!("[reader] entered\n");
  145.            while !STOP.load(Ordering::Relaxed) {
  146.                // Recorded before the call, so once wedged it names the CPU spinning in
  147.                // `queued_read_lock_slowpath()`. Preemption cannot move a task that is
  148.                // spinning with interrupts off, so the last value stays true.
  149.                READER_CPU.store(CpuId::current().as_u32(), Ordering::Relaxed);
  150.                // SAFETY: FFI call, valid for any CPU number.
  151.                unsafe { bindings::cpufreq_quick_get(0) };
  152.                READS.fetch_add(1, Ordering::Relaxed);
  153.            }
  154.        })?;
  155.  
  156.        // Writer.
  157.        workqueue::system_unbound().try_spawn(GFP_KERNEL, || {
  158.            pr_info!("[writer] entered\n");
  159.            // SAFETY: a plain C struct of pointers; all-zero is a valid starting value.
  160.            let mut drv = unsafe { core::mem::zeroed::<bindings::cpufreq_driver>() };
  161.            drv.verify = Some(dummy_verify);
  162.            drv.init = Some(dummy_init);
  163.            drv.setpolicy = Some(dummy_setpolicy);
  164.            for (d, s) in drv.name.iter_mut().zip(b"poc-race-dummy\0") {
  165.                *d = *s as _;
  166.            }
  167.            while !STOP.load(Ordering::Relaxed) {
  168.                WRITER_CPU.store(CpuId::current().as_u32(), Ordering::Relaxed);
  169.                // SAFETY: `drv` is a valid vtable; the call returns -EEXIST after taking
  170.                // the write lock, because rcpufreq_dt is already registered.
  171.                unsafe { bindings::cpufreq_register_driver(&mut drv) };
  172.                WRITES.fetch_add(1, Ordering::Relaxed);
  173.            }
  174.        })?;
  175.  
  176.        // a wedged CPU freezes the whole guest, this thread included, before it can report anything.
  177.        //  Sampling at 50ms reaches a verdict
  178.        const TICK_MS: u32 = 50;
  179.        let (mut lr, mut lw, mut stuck) = (0u64, 0u64, 0u32);
  180.        for i in 1..=(20_000 / TICK_MS) {
  181.            spin_ms(TICK_MS);
  182.            let (r, w) = (
  183.                READS.load(Ordering::Relaxed),
  184.                WRITES.load(Ordering::Relaxed),
  185.            );
  186.            let (rc, wc) = (
  187.                READER_CPU.load(Ordering::Relaxed),
  188.                WRITER_CPU.load(Ordering::Relaxed),
  189.            );
  190.            // One line per 100ms
  191.            if i % (100 / TICK_MS) == 0 {
  192.                pr_info!(
  193.                    "t={}ms reads={} (cpu{}) writes={} (cpu{})\n",
  194.                    i * TICK_MS,
  195.                    r,
  196.                    rc as i32,
  197.                    w,
  198.                    wc as i32
  199.                );
  200.            }
  201.  
  202.            // Both sides must have entered their loop: counters alone cannot tell a lock
  203.            // cycle apart from a worker that never started.
  204.            if rc == NO_CPU || wc == NO_CPU || r != lr || w != lw {
  205.                stuck = 0;
  206.                (lr, lw) = (r, w);
  207.                continue;
  208.            }
  209.            stuck += 1;
  210.            if stuck < 3 {
  211.                continue;
  212.            }
  213.            if rc == wc {
  214.                // Two workers on one CPU wedge each other by starving the runqueue, which
  215.                // looks identical in the counters and is not this bug.
  216.                pr_err!(
  217.                    "both workers landed on cpu{}; inconclusive, not the AA deadlock\n",
  218.                    rc
  219.                );
  220.                STOP.store(true, Ordering::Relaxed);
  221.                return Ok(PocRace);
  222.            }
  223.            report(rc, wc, r, w, stuck * TICK_MS);
  224.            return Ok(PocRace);
  225.        }
  226.        pr_info!("no deadlock within 20s\n");
  227.        STOP.store(true, Ordering::Relaxed);
  228.        Ok(PocRace)
  229.    }
  230. }
  231.  
Advertisement
Add Comment
Please, Sign In to add comment