Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # recursive read_lock on cpufreq_driver_lock. Run from the kernel source root.
- #
- # TIMEOUT=100 ./reproduce.sh bug reproduce the deadlock
- #
- set -euo pipefail
- GCC=${GCC:-aarch64-linux-gnu-gcc-14}
- HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
- OUT=finding-out; mkdir -p $OUT
- MODE=${1:-bug}
- # The guest stays wedged once it deadlocks, so this is how long we wait before killing it,
- # not how long reproduction takes -- the verdict lands within a couple of seconds of guest
- # time. Raise it only if the log has boot output but no verdict.
- TIMEOUT=${TIMEOUT:-900}
- M=(make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- CC="$GCC")
- command -v "$GCC" >/dev/null || { echo "missing $GCC"; exit 1; }
- rustup target list --installed | grep -qx aarch64-unknown-none || rustup target add aarch64-unknown-none
- cleanup() {
- git checkout -- drivers/cpufreq/rcpufreq_dt.rs rust/kernel/cpufreq.rs samples/rust/Makefile 2>/dev/null || true
- rm -f samples/rust/rust_poc_race.rs
- }
- trap cleanup EXIT
- # 1. device tree ---------------------------------------------------------------
- qemu-system-aarch64 -machine virt,gic-version=3,dumpdtb=$OUT/virt.dtb -smp 4 -nographic >/dev/null 2>&1
- scripts/dtc/dtc -I dtb -O dts -o $OUT/virt.dts $OUT/virt.dtb 2>/dev/null
- python3 - "$OUT" <<'PY'
- import sys, re
- o = sys.argv[1]; s = open(f"{o}/virt.dts").read(); n = 0
- def fix(m):
- global n; n += 1
- return m.group(0).replace('device_type = "cpu";',
- 'device_type = "cpu";\n\t\t\tclocks = <0x9000>;\n\t\t\toperating-points-v2 = <0x9001>;')
- s = re.sub(r'cpu@[0-9a-f]+ \{[^}]*device_type = "cpu";[^}]*\}', fix, s)
- i = s.rstrip().rfind("\n};")
- s = s[:i] + '''
- \tcpu-clk { phandle = <0x9000>; compatible = "fixed-clock"; #clock-cells = <0x00>; clock-frequency = <0x3b9aca00>; };
- \topp-table { phandle = <0x9001>; compatible = "operating-points-v2"; opp-shared;
- \t\topp-1000000000 { opp-hz = <0x00 0x3b9aca00>; }; };
- ''' + s[i:]
- open(f"{o}/virt-opp.dts","w").write(s)
- print(f"dt: patched {n} cpu node(s)")
- PY
- scripts/dtc/dtc -I dts -O dtb -o $OUT/virt-opp.dtb $OUT/virt-opp.dts 2>/dev/null
- # 2. patches + module ---------------------------------------------------------
- # 01 makes rcpufreq_dt a setpolicy driver, which is what opens the gate at
- # cpufreq.c:1850 so cpufreq_quick_get() dispatches to ->get() under the read lock.
- git apply "$HERE/01-enable-rcpufreq_dt-setpolicy.patch"
- # [ "$MODE" = fix ] && git apply "$HERE/02-fix-recursive-read_lock.patch"
- cp "$HERE/rust_poc_race.rs" samples/rust/
- grep -q rust_poc_race samples/rust/Makefile || echo 'obj-y += rust_poc_race.o' >> samples/rust/Makefile
- # 3. build --------------------------------------------------------------------
- "${M[@]}" defconfig
- ./scripts/config --enable RUST --enable SAMPLES --enable SAMPLES_RUST \
- --disable CPUFREQ_DT --enable CPUFREQ_DT_RUST \
- --enable ARM64_PSEUDO_NMI
- "${M[@]}" olddefconfig
- grep -q '^CONFIG_QUEUED_RWLOCKS=y' .config || { echo "need QUEUED_RWLOCKS"; exit 1; }
- grep -q '^CONFIG_CPUFREQ_DT_RUST=y' .config || { echo "CPUFREQ_DT_RUST off"; exit 1; }
- grep -q '^CONFIG_ARM64_PSEUDO_NMI=y' .config || echo "warn: no pseudo-NMI, backtraces will be missing"
- "${M[@]}" -j"$(nproc)" Image
- # 4. boot ---------------------------------------------------------------------
- echo "=== booting ($MODE); watch it with: tail -f $OUT/$MODE.log ==="
- { echo "started: $(date -Is)"
- echo "qemu: $(command -v qemu-system-aarch64)"
- echo "version: $(qemu-system-aarch64 --version | head -1)"
- echo "loadavg: $(cat /proc/loadavg)"
- echo "timeout: ${TIMEOUT}s"
- echo "kernel: $(stat -c '%y %s' arch/arm64/boot/Image)"
- echo "dtb: $(stat -c '%y %s' $OUT/virt-opp.dtb)"
- echo "poc syms: $(grep -c rust_poc_race System.map || true)"
- } > $OUT/$MODE.env
- qrc=0
- timeout "$TIMEOUT" qemu-system-aarch64 -machine virt,gic-version=3 -cpu cortex-a57 -m 512 -smp 4 \
- -dtb $OUT/virt-opp.dtb -kernel arch/arm64/boot/Image -nographic -no-reboot \
- -append "console=ttyAMA0 cpufreq.default_governor=performance poc_race.run=1 \
- irqchip.gicv3_pseudo_nmi=1 rcupdate.rcu_cpu_stall_timeout=10" \
- < /dev/null > $OUT/$MODE.log 2>&1 || qrc=$?
- { echo "finished: $(date -Is)"; echo "qemu exit: $qrc (124 = killed by timeout)"
- echo "log bytes: $(stat -c %s $OUT/$MODE.log)"; } >> $OUT/$MODE.env
- # The two labelled backtraces are the point: each side's frames spell out its half of the
- # cycle. get_callback/rcpufreq_dt appear inside the Rust mangled symbols.
- 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" \
- $OUT/$MODE.log | sed 's/^\[[^]]*\] //' || true
- if ! grep -aqE "DEADLOCK|no deadlock within|is shut|inconclusive" "$OUT/$MODE.log"; then
- if [ ! -s "$OUT/$MODE.log" ]; then
- echo "!! the log is EMPTY: qemu printed nothing at all (exit=$qrc)."
- echo "!! run conditions were recorded in $OUT/$MODE.env -- send that along:"
- sed 's/^/!! /' $OUT/$MODE.env
- echo "!! exit=124 -> it ran the full ${TIMEOUT}s and stayed mute; the guest never"
- echo "!! reached its first console write, or qemu was stopped (SIGTTOU)."
- echo "!! otherwise -> qemu failed to start; the reason should be in the log."
- else
- echo "!! the guest booted ($(wc -l < "$OUT/$MODE.log") lines) but never reached a verdict."
- echo "!! re-run with a bigger budget, e.g. TIMEOUT=200 $0 $MODE"
- fi
- fi
- echo "--- full log: $OUT/$MODE.log ---"
Advertisement
Add Comment
Please, Sign In to add comment