From b4596ed7372955bcc2ac40b9a8aa2d5209475439 Mon Sep 17 00:00:00 2001 From: BFE Engineering Date: Thu, 29 Oct 2026 03:37:08 -0600 Subject: [PATCH] kernel: keep the arm32 vDSO clock off the virtual counter (#35) On the RV1106 the boot firmware does not enter the kernel in HYP mode and leaves CNTVOFF non-zero. The DT timer node lists no virtual-timer PPI, so the kernel keeps time on the physical counter, while the arm32 vDSO always reads the virtual one: on bench a clock_gettime() ran 738.9 s ahead of the kernel clock (pure offset, -0.1 ppm over 30 s) and every absolute deadline taken from it expired minutes late (flare-edge#2319, the #2311 reset loop). New patch 82-arch-timer-vdso-phys-counter.patch: - rv1106.dtsi and rv1106-warden-m2.dts: the timer node carries arm,cpu-registers-not-fw-configured. The kernel keeps the physical timer it uses today, and arch/arm/kernel/vdso.c drops the vDSO clock symbols, so libc goes straight to the syscall. - arm_arch_timer.c: on arm32, a kernel on the physical counter without HYP mode sets the counter's vdso_clock_mode to VDSO_CLOCKMODE_NONE, the safety net for a device tree without the property. QEMU -M virt (virtual PPI present) is unchanged. tests/arch-timer/check-vdso-counter.py checks both guards in the patched tree; CI's patches-apply job runs it and its 12 regression cases. Fixes #35 Refs flare-edge#2319 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KerYPVh72hhNgKi3wS5ijG --- .github/workflows/ci.yml | 7 ++ patches/82-arch-timer-vdso-phys-counter.patch | 62 ++++++++++ patches/README.md | 6 +- tests/arch-timer/check-vdso-counter.py | 109 ++++++++++++++++++ .../run-check-vdso-counter-tests.sh | 109 ++++++++++++++++++ 5 files changed, 291 insertions(+), 2 deletions(-) create mode 100644 patches/82-arch-timer-vdso-phys-counter.patch create mode 100755 tests/arch-timer/check-vdso-counter.py create mode 100755 tests/arch-timer/run-check-vdso-counter-tests.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2003ea4..40ac27a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -328,6 +328,13 @@ jobs: && echo "applied $p" || { echo "FAILED to apply $p"; exit 1; } done echo "full series applied cleanly onto pristine 6.18.46" + - name: arm32 vDSO stays off the virtual counter (bfe-core1106-sdk#35) + # The kernel times on the physical counter and the firmware leaves + # CNTVOFF non-zero, so the vDSO clock must stay off: the DT property and + # the arm_arch_timer.c fallback both have to survive in the patched tree. + run: | + bash tests/arch-timer/run-check-vdso-counter-tests.sh + python3 tests/arch-timer/check-vdso-counter.py /tmp/linux-6.18.46 # Automatic artifact GC (mirrors flare-edge). An exceeded account-wide Actions # storage quota blocks ALL new runs (startup_failure), not just uploads, so diff --git a/patches/82-arch-timer-vdso-phys-counter.patch b/patches/82-arch-timer-vdso-phys-counter.patch new file mode 100644 index 0000000..4ce5a8d --- /dev/null +++ b/patches/82-arch-timer-vdso-phys-counter.patch @@ -0,0 +1,62 @@ +diff --git a/arch/arm/boot/dts/rockchip/rv1106-warden-m2.dts b/arch/arm/boot/dts/rockchip/rv1106-warden-m2.dts +--- a/arch/arm/boot/dts/rockchip/rv1106-warden-m2.dts ++++ b/arch/arm/boot/dts/rockchip/rv1106-warden-m2.dts +@@ -74,6 +74,15 @@ + interrupts = , + ; + clock-frequency = <24000000>; ++ /* ++ * The boot firmware does not enter the kernel in HYP mode and leaves ++ * CNTVOFF non-zero (bfe-core1106-sdk#35): the virtual counter the ++ * arm32 vDSO reads runs a fixed offset away from the physical one ++ * the kernel keeps time on. This keeps the kernel on the physical ++ * timer and drops the vDSO clock calls, so clock_gettime() uses the ++ * syscall and agrees with every kernel deadline. ++ */ ++ arm,cpu-registers-not-fw-configured; + }; + + gic: interrupt-controller@ff1f0000 { +diff --git a/arch/arm/boot/dts/rockchip/rv1106.dtsi b/arch/arm/boot/dts/rockchip/rv1106.dtsi +--- a/arch/arm/boot/dts/rockchip/rv1106.dtsi ++++ b/arch/arm/boot/dts/rockchip/rv1106.dtsi +@@ -417,6 +417,15 @@ + interrupts = , + ; + clock-frequency = <24000000>; ++ /* ++ * The boot firmware does not enter the kernel in HYP mode and leaves ++ * CNTVOFF non-zero (bfe-core1106-sdk#35): the virtual counter the ++ * arm32 vDSO reads runs a fixed offset away from the physical one ++ * the kernel keeps time on. This keeps the kernel on the physical ++ * timer and drops the vDSO clock calls, so clock_gettime() uses the ++ * syscall and agrees with every kernel deadline. ++ */ ++ arm,cpu-registers-not-fw-configured; + }; + + grf: syscon@ff000000 { +diff --git a/drivers/clocksource/arm_arch_timer.c b/drivers/clocksource/arm_arch_timer.c +--- a/drivers/clocksource/arm_arch_timer.c ++++ b/drivers/clocksource/arm_arch_timer.c +@@ -933,6 +933,20 @@ static void __init arch_counter_register(void) + arch_timer_read_counter = rd; + clocksource_counter.vdso_clock_mode = vdso_default; + ++ /* ++ * The arm32 vDSO always reads the virtual counter. A kernel that keeps ++ * time on the physical counter and was not entered in HYP mode never ++ * had CNTVOFF zeroed by the hyp stub, so the vDSO clock may sit a fixed ++ * offset away from the kernel's and absolute deadlines taken from it ++ * expire late. Fall back to the syscall then. ++ */ ++ if (IS_ENABLED(CONFIG_ARM) && arch_timer_uses_ppi != ARCH_TIMER_VIRT_PPI && ++ !is_hyp_mode_available() && ++ clocksource_counter.vdso_clock_mode != VDSO_CLOCKMODE_NONE) { ++ clocksource_counter.vdso_clock_mode = VDSO_CLOCKMODE_NONE; ++ pr_info("physical counter without HYP: vDSO clock disabled\n"); ++ } ++ + width = arch_counter_get_width(); + clocksource_counter.mask = CLOCKSOURCE_MASK(width); + cyclecounter.mask = CLOCKSOURCE_MASK(width); diff --git a/patches/README.md b/patches/README.md index 632f9a0..357a2ca 100644 --- a/patches/README.md +++ b/patches/README.md @@ -1,8 +1,9 @@ # The Kernel Patch Series The delta that forward-ports the vendor Rockchip 5.10.160 RV1106 support onto a -pristine upstream **linux-6.18.46**, as a reviewable, subsystem-split series. Every -patch applies cleanly onto pristine 6.18.46 (`git apply --check`), and the whole set +pristine upstream **linux-6.18.46**, as a reviewable, subsystem-split series. The +series applies cleanly, in order, onto pristine 6.18.46 (`git apply --check`; a fix +patch such as 82 edits files that an earlier patch adds), and the whole set reproduces the 223-file / ~136K-line delta that boots and is hardware-verified on `warden-c8a3` (see `../kernel/rv1106-enablement/`). @@ -28,6 +29,7 @@ with `../build/warden_defconfig` -> `zImage` + `rv1106-warden.dtb`). | `60-wifi-aic8800.patch` | out-of-tree AIC8800 wifi/BT (the 88.5K-line vendor blob) | | `70-audio-codec.patch` | `rv1106_codec` + I2S wiring | | `80-misc-thermal-rtc-adc.patch` | tsadc, rtc-rockchip, saradc, trng, otp, gmac, goodix touch | +| `82-arch-timer-vdso-phys-counter.patch` | arch timer: keep the arm32 vDSO clock off the virtual counter while the kernel times on the physical one (CNTVOFF left non-zero by the boot firmware, #35); checked by `tests/arch-timer/check-vdso-counter.py` | | `90-kconfig-no-python-symbol.patch` | drops `DRM_MSM_VALIDATE_XML` so the expanded `.config` (and `KCONFIG_SHA256`) does not depend on the host python (#19) | ## License diff --git a/tests/arch-timer/check-vdso-counter.py b/tests/arch-timer/check-vdso-counter.py new file mode 100755 index 0000000..fb7a514 --- /dev/null +++ b/tests/arch-timer/check-vdso-counter.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +"""Check that a patched kernel tree keeps the arm32 vDSO clock off the +virtual counter when the kernel times on the physical one (bfe-core1106-sdk#35). + +On the RV1106 the boot firmware does not enter the kernel in HYP mode and +leaves CNTVOFF non-zero. The DT timer node lists no virtual-timer PPI, so the +kernel keeps time on the physical counter, while the arm32 vDSO always reads +the virtual counter: clock_gettime() then runs about 739 s ahead of the kernel +clock and every absolute deadline taken from it expires late (flare-edge#2319). + +Two guards must both be present in the patched tree: + +1. Every armv7/armv8 timer node in the RV1106 device trees that has no + virtual-timer PPI (GIC_PPI 11) carries `arm,cpu-registers-not-fw-configured`, + which makes arch/arm/kernel/vdso.c drop the vDSO clock symbols. +2. drivers/clocksource/arm_arch_timer.c sets the counter's vdso_clock_mode to + VDSO_CLOCKMODE_NONE on arm32 when the kernel uses the physical counter + without HYP mode (the safety net for a device tree without the property). + +Usage: check-vdso-counter.py +Exit 0 when both hold, 1 with one FAIL line per problem, 2 on bad usage. +""" +import pathlib +import re +import sys + +DTS_GLOB = "arch/arm/boot/dts/rockchip/rv1106*.dts*" +TIMER_C = "drivers/clocksource/arm_arch_timer.c" +PROPERTY = "arm,cpu-registers-not-fw-configured" + + +def strip_comments(text): + text = re.sub(r"/\*.*?\*/", "", text, flags=re.S) + return re.sub(r"//[^\n]*", "", text) + + +def timer_nodes(text): + """Yield the body of every node whose compatible is an arch timer.""" + for m in re.finditer(r"\b[\w@,.-]+\s*\{", text): + depth, i = 1, m.end() + while depth and i < len(text): + depth += {"{": 1, "}": -1}.get(text[i], 0) + i += 1 + body = text[m.end():i - 1] + # Only the node's own properties, not its children's. + own = re.sub(r"\{.*\}", "", body, flags=re.S) + if re.search(r'compatible\s*=\s*"arm,armv[78]-timer"', own): + yield own + + +def check_dts(root): + problems, seen = [], 0 + for path in sorted(root.glob(DTS_GLOB)): + text = strip_comments(path.read_text(errors="replace")) + for own in timer_nodes(text): + seen += 1 + has_virt = re.search(r"GIC_PPI\s+11\b", own) is not None + has_prop = re.search(r"(^|[\s;])" + re.escape(PROPERTY) + r"\s*;", own) is not None + if not has_virt and not has_prop: + problems.append(f"{path.relative_to(root)}: timer node has no virtual-timer " + f"PPI and no {PROPERTY}") + if not seen: + problems.append(f"no arm,armv7-timer node found under {DTS_GLOB}") + return problems + + +def check_timer_c(root): + path = root / TIMER_C + if not path.is_file(): + return [f"{TIMER_C} missing"] + text = strip_comments(path.read_text(errors="replace")) + m = re.search(r"static void __init arch_counter_register\(void\)\s*\{", text) + if not m: + return [f"{TIMER_C}: arch_counter_register() not found"] + depth, i = 1, m.end() + while depth and i < len(text): + depth += {"{": 1, "}": -1}.get(text[i], 0) + i += 1 + body = text[m.end():i - 1] + guard = re.search( + r"if\s*\(\s*IS_ENABLED\(CONFIG_ARM\)\s*&&\s*" + r"arch_timer_uses_ppi\s*!=\s*ARCH_TIMER_VIRT_PPI\s*&&\s*" + r"!\s*is_hyp_mode_available\(\)[^{]*\{([^}]*)\}", body) + if not guard or not re.search( + r"clocksource_counter\.vdso_clock_mode\s*=\s*VDSO_CLOCKMODE_NONE\s*;", guard.group(1)): + return [f"{TIMER_C}: arch_counter_register() lacks the arm32 physical-counter " + "vDSO fallback (vdso_clock_mode = VDSO_CLOCKMODE_NONE)"] + default_at = body.find("clocksource_counter.vdso_clock_mode = vdso_default;") + if default_at < 0 or default_at > guard.start(): + return [f"{TIMER_C}: the vDSO fallback must come after the vdso_default assignment"] + return [] + + +def main(argv): + if len(argv) != 2 or not pathlib.Path(argv[1]).is_dir(): + print("usage: check-vdso-counter.py ", file=sys.stderr) + return 2 + root = pathlib.Path(argv[1]) + problems = check_dts(root) + check_timer_c(root) + for p in problems: + print(f"FAIL: {p}") + if problems: + return 1 + print("vdso-counter: ok (DT property and arm32 physical-counter fallback present)") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/tests/arch-timer/run-check-vdso-counter-tests.sh b/tests/arch-timer/run-check-vdso-counter-tests.sh new file mode 100755 index 0000000..310ff3d --- /dev/null +++ b/tests/arch-timer/run-check-vdso-counter-tests.sh @@ -0,0 +1,109 @@ +#!/bin/bash +# Regression tests for tests/arch-timer/check-vdso-counter.py (bfe-core1106-sdk#35). +# +# The checker guards the fix for the vDSO clock running 739 s ahead of the +# kernel clock on the RV1106: the DT timer property and the arm32 fallback in +# arm_arch_timer.c. Each case builds a small fake kernel tree and expects an +# exit status; the real patched tree is checked in CI's patches-apply job. +set -uo pipefail + +HERE="$(cd "$(dirname "$0")" && pwd)" +CHECK="$HERE/check-vdso-counter.py" +TMP="$(mktemp -d)" +trap 'rm -rf "$TMP"' EXIT + +TIMER_NODE_GOOD=' timer { + compatible = "arm,armv7-timer"; + interrupts = , + ; + clock-frequency = <24000000>; + arm,cpu-registers-not-fw-configured; + };' +TIMER_NODE_BARE=' timer { + compatible = "arm,armv7-timer"; + interrupts = , + ; + clock-frequency = <24000000>; + };' +TIMER_NODE_COMMENTED=' timer { + compatible = "arm,armv7-timer"; + interrupts = , + ; + /* arm,cpu-registers-not-fw-configured; */ + };' +TIMER_NODE_VIRT=' timer { + compatible = "arm,armv7-timer"; + interrupts = , , + , ; + };' +TIMER_NODE_CHILD=' timer { + compatible = "arm,armv7-timer"; + interrupts = , ; + sub { + arm,cpu-registers-not-fw-configured; + }; + };' + +GUARD=' if (IS_ENABLED(CONFIG_ARM) && arch_timer_uses_ppi != ARCH_TIMER_VIRT_PPI && + !is_hyp_mode_available() && + clocksource_counter.vdso_clock_mode != VDSO_CLOCKMODE_NONE) { + clocksource_counter.vdso_clock_mode = VDSO_CLOCKMODE_NONE; + pr_info("physical counter without HYP: vDSO clock disabled\n"); + }' +GUARD_WRONG=' if (IS_ENABLED(CONFIG_ARM) && arch_timer_uses_ppi != ARCH_TIMER_VIRT_PPI && + !is_hyp_mode_available()) { + pr_info("physical counter without HYP\n"); + }' +DEFAULT=' clocksource_counter.vdso_clock_mode = vdso_default;' + +mk_tree() { # mk_tree + local d="$TMP/$1" + mkdir -p "$d/arch/arm/boot/dts/rockchip" "$d/drivers/clocksource" + printf '/ {\n\tcpus { cpu@0 { reg = <0>; }; };\n%s\n};\n' "$2" \ + > "$d/arch/arm/boot/dts/rockchip/rv1106.dtsi" + printf '#include "rv1106.dtsi"\n/ { model = "x"; };\n' \ + > "$d/arch/arm/boot/dts/rockchip/rv1106-warden.dts" + printf 'static void __init arch_counter_register(void)\n{\n\tu64 (*rd)(void);\n%s\n\n%s\n\twidth = 56;\n}\n' \ + "$3" "$4" > "$d/drivers/clocksource/arm_arch_timer.c" + echo "$d" +} + +fails=0 n=0 +expect() { # expect + local got out + out="$(python3 "$CHECK" "$3" 2>&1)"; got=$? + n=$((n + 1)) + if [ "$got" = "$2" ]; then + echo "PASS: $1" + else + echo "FAIL: $1 (wanted exit $2, got $got)" + while IFS= read -r line; do echo " $line"; done <<< "$out" + fails=$((fails + 1)) + fi +} + +expect "fixed tree passes" 0 "$(mk_tree good "$TIMER_NODE_GOOD" "$DEFAULT" "$GUARD")" +expect "timer node without the property fails" 1 "$(mk_tree bare "$TIMER_NODE_BARE" "$DEFAULT" "$GUARD")" +expect "property only inside a comment fails" 1 "$(mk_tree comm "$TIMER_NODE_COMMENTED" "$DEFAULT" "$GUARD")" +expect "property only in a child node fails" 1 "$(mk_tree child "$TIMER_NODE_CHILD" "$DEFAULT" "$GUARD")" +expect "timer node with a virtual PPI needs no property" 0 "$(mk_tree virt "$TIMER_NODE_VIRT" "$DEFAULT" "$GUARD")" +expect "missing arm32 fallback fails" 1 "$(mk_tree noguard "$TIMER_NODE_GOOD" "$DEFAULT" "")" +expect "fallback that does not set VDSO_CLOCKMODE_NONE fails" 1 "$(mk_tree wrong "$TIMER_NODE_GOOD" "$DEFAULT" "$GUARD_WRONG")" +expect "fallback before the vdso_default assignment fails" 1 "$(mk_tree order "$TIMER_NODE_GOOD" "$GUARD" "$DEFAULT")" +expect "no timer node at all fails" 1 "$(mk_tree none "" "$DEFAULT" "$GUARD")" + +d="$(mk_tree m2 "$TIMER_NODE_GOOD" "$DEFAULT" "$GUARD")" +printf '/ {\n%s\n};\n' "$TIMER_NODE_BARE" > "$d/arch/arm/boot/dts/rockchip/rv1106-warden-m2.dts" +expect "a second board DTS without the property fails" 1 "$d" + +rm -f "$d/drivers/clocksource/arm_arch_timer.c" +expect "missing arm_arch_timer.c fails" 1 "$d" + +n=$((n + 1)) +if python3 "$CHECK" >/dev/null 2>&1; [ $? = 2 ]; then echo "PASS: no argument is a usage error" +else echo "FAIL: no argument is a usage error"; fails=$((fails + 1)); fi + +if [ "$fails" -ne 0 ]; then + echo "check-vdso-counter: $fails of $n FAILED"; exit 1 +fi +echo "check-vdso-counter: all $n passed" -- 2.54.0