Guest User

reproduce.sh

a guest
Oct 1st, 2026
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.53 KB | Source Code | 0 0
  1. #!/bin/bash
  2. # recursive read_lock on cpufreq_driver_lock. Run from the kernel source root.
  3. #
  4. #   TIMEOUT=100 ./reproduce.sh bug         reproduce the deadlock
  5. #
  6. set -euo pipefail
  7.  
  8. GCC=${GCC:-aarch64-linux-gnu-gcc-14}
  9. HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
  10. OUT=finding-out; mkdir -p $OUT
  11. MODE=${1:-bug}
  12. # The guest stays wedged once it deadlocks, so this is how long we wait before killing it,
  13. # not how long reproduction takes -- the verdict lands within a couple of seconds of guest
  14. # time. Raise it only if the log has boot output but no verdict.
  15. TIMEOUT=${TIMEOUT:-900}
  16. M=(make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- CC="$GCC")
  17.  
  18. command -v "$GCC" >/dev/null || { echo "missing $GCC"; exit 1; }
  19. rustup target list --installed | grep -qx aarch64-unknown-none || rustup target add aarch64-unknown-none
  20.  
  21. cleanup() {
  22.     git checkout -- drivers/cpufreq/rcpufreq_dt.rs rust/kernel/cpufreq.rs samples/rust/Makefile 2>/dev/null || true
  23.     rm -f samples/rust/rust_poc_race.rs
  24. }
  25. trap cleanup EXIT
  26.  
  27. # 1. device tree ---------------------------------------------------------------
  28. qemu-system-aarch64 -machine virt,gic-version=3,dumpdtb=$OUT/virt.dtb -smp 4 -nographic >/dev/null 2>&1
  29. scripts/dtc/dtc -I dtb -O dts -o $OUT/virt.dts $OUT/virt.dtb 2>/dev/null
  30. python3 - "$OUT" <<'PY'
  31. import sys, re
  32. o = sys.argv[1]; s = open(f"{o}/virt.dts").read(); n = 0
  33. def fix(m):
  34.     global n; n += 1
  35.     return m.group(0).replace('device_type = "cpu";',
  36.         'device_type = "cpu";\n\t\t\tclocks = <0x9000>;\n\t\t\toperating-points-v2 = <0x9001>;')
  37. s = re.sub(r'cpu@[0-9a-f]+ \{[^}]*device_type = "cpu";[^}]*\}', fix, s)
  38. i = s.rstrip().rfind("\n};")
  39. s = s[:i] + '''
  40. \tcpu-clk { phandle = <0x9000>; compatible = "fixed-clock"; #clock-cells = <0x00>; clock-frequency = <0x3b9aca00>; };
  41. \topp-table { phandle = <0x9001>; compatible = "operating-points-v2"; opp-shared;
  42. \t\topp-1000000000 { opp-hz = <0x00 0x3b9aca00>; }; };
  43. ''' + s[i:]
  44. open(f"{o}/virt-opp.dts","w").write(s)
  45. print(f"dt: patched {n} cpu node(s)")
  46. PY
  47. scripts/dtc/dtc -I dts -O dtb -o $OUT/virt-opp.dtb $OUT/virt-opp.dts 2>/dev/null
  48.  
  49. # 2. patches + module ---------------------------------------------------------
  50. # 01 makes rcpufreq_dt a setpolicy driver, which is what opens the gate at
  51. # cpufreq.c:1850 so cpufreq_quick_get() dispatches to ->get() under the read lock.
  52. git apply "$HERE/01-enable-rcpufreq_dt-setpolicy.patch"
  53. # [ "$MODE" = fix ] && git apply "$HERE/02-fix-recursive-read_lock.patch"
  54. cp "$HERE/rust_poc_race.rs" samples/rust/
  55. grep -q rust_poc_race samples/rust/Makefile || echo 'obj-y += rust_poc_race.o' >> samples/rust/Makefile
  56.  
  57. # 3. build --------------------------------------------------------------------
  58. "${M[@]}" defconfig
  59. ./scripts/config --enable RUST --enable SAMPLES --enable SAMPLES_RUST \
  60.                  --disable CPUFREQ_DT --enable CPUFREQ_DT_RUST \
  61.                  --enable ARM64_PSEUDO_NMI
  62. "${M[@]}" olddefconfig
  63. grep -q '^CONFIG_QUEUED_RWLOCKS=y'  .config || { echo "need QUEUED_RWLOCKS"; exit 1; }
  64. grep -q '^CONFIG_CPUFREQ_DT_RUST=y' .config || { echo "CPUFREQ_DT_RUST off"; exit 1; }
  65. grep -q '^CONFIG_ARM64_PSEUDO_NMI=y' .config || echo "warn: no pseudo-NMI, backtraces will be missing"
  66. "${M[@]}" -j"$(nproc)" Image
  67.  
  68. # 4. boot ---------------------------------------------------------------------
  69. echo "=== booting ($MODE); watch it with: tail -f $OUT/$MODE.log ==="
  70.  
  71. {   echo "started:  $(date -Is)"
  72.     echo "qemu:     $(command -v qemu-system-aarch64)"
  73.     echo "version:  $(qemu-system-aarch64 --version | head -1)"
  74.     echo "loadavg:  $(cat /proc/loadavg)"
  75.     echo "timeout:  ${TIMEOUT}s"
  76.     echo "kernel:   $(stat -c '%y %s' arch/arm64/boot/Image)"
  77.     echo "dtb:      $(stat -c '%y %s' $OUT/virt-opp.dtb)"
  78.     echo "poc syms: $(grep -c rust_poc_race System.map || true)"
  79. } > $OUT/$MODE.env
  80. qrc=0
  81. timeout "$TIMEOUT" qemu-system-aarch64 -machine virt,gic-version=3 -cpu cortex-a57 -m 512 -smp 4 \
  82.   -dtb $OUT/virt-opp.dtb -kernel arch/arm64/boot/Image -nographic -no-reboot \
  83.   -append "console=ttyAMA0 cpufreq.default_governor=performance poc_race.run=1 \
  84.           irqchip.gicv3_pseudo_nmi=1 rcupdate.rcu_cpu_stall_timeout=10" \
  85.   < /dev/null > $OUT/$MODE.log 2>&1 || qrc=$?
  86. { echo "finished: $(date -Is)"; echo "qemu exit: $qrc (124 = killed by timeout)"
  87.   echo "log bytes: $(stat -c %s $OUT/$MODE.log)"; } >> $OUT/$MODE.env
  88.  
  89. # The two labelled backtraces are the point: each side's frames spell out its half of the
  90. # cycle. get_callback/rcpufreq_dt appear inside the Rust mangled symbols.
  91. grep -aE "poc_race|DEADLOCK|detected stalls|backtrace for cpu|queued_(read|write)_lock_slowpath|cpufreq_(cpu_get|quick_get|register_driver)|get_callback|rcpufreq_dt" \
  92.   $OUT/$MODE.log | sed 's/^\[[^]]*\] //' || true
  93. if ! grep -aqE "DEADLOCK|no deadlock within|is shut|inconclusive" "$OUT/$MODE.log"; then
  94.     if [ ! -s "$OUT/$MODE.log" ]; then
  95.         echo "!! the log is EMPTY: qemu printed nothing at all (exit=$qrc)."
  96.         echo "!! run conditions were recorded in $OUT/$MODE.env -- send that along:"
  97.         sed 's/^/!!   /' $OUT/$MODE.env
  98.         echo "!!   exit=124 -> it ran the full ${TIMEOUT}s and stayed mute; the guest never"
  99.         echo "!!              reached its first console write, or qemu was stopped (SIGTTOU)."
  100.         echo "!!   otherwise -> qemu failed to start; the reason should be in the log."
  101.     else
  102.         echo "!! the guest booted ($(wc -l < "$OUT/$MODE.log") lines) but never reached a verdict."
  103.         echo "!! re-run with a bigger budget, e.g. TIMEOUT=200 $0 $MODE"
  104.     fi
  105. fi
  106. echo "--- full log: $OUT/$MODE.log ---"
  107.  
Advertisement
Add Comment
Please, Sign In to add comment