From 8b064aeb5cfadf2f570dafe53f04bf8150e87d0e Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Sun, 1 Nov 2026 22:14:17 +0000 Subject: [PATCH 1/2] tools/kernel-pr-check: fail when checkpatch doesn't run to the end checkpatch's stderr went to /dev/null and its exit status was never read: only ": ERROR:" lines in its report could fail the check. So a checker the merge base didn't have, one that didn't compile, died (as the real one does when --root isn't a kernel tree) or was killed printed nothing, and the empty report passed as "checkpatch --strict: no errors". Read the status as checkpatch sets it: 0 when it reports nothing, 1 when it reports something. Anything else, or a 1 with no diagnostic in the report, fails the check as "didn't run to the end", with the commit, the exit status and what checkpatch said: its stderr, and any stdout line that isn't a diagnostic (it prints some of its own failures there, such as a perl too old). Clean commits, warnings and errors read as before. tests/kernel-pr-check's stand-in now exits as checkpatch does, and covers a warning-only commit (still a pass) and a checker that crashes, doesn't compile, exits 1 with no report, dies on a signal or isn't in the merge base. --help prints the whole header again: it stopped a line short. Co-Authored-By: Claude Opus 5.5 --- tests/kernel-pr-check | 54 +++++++++++++++++++++++++++++++++++++------ tools/kernel-pr-check | 25 ++++++++++++++++---- 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/tests/kernel-pr-check b/tests/kernel-pr-check index 4b7497dd7..1724ed8c5 100755 --- a/tests/kernel-pr-check +++ b/tests/kernel-pr-check @@ -1,8 +1,9 @@ #!/bin/bash # SPDX-License-Identifier: MIT # tools/kernel-pr-check's commit checks against a stand-in kernel tree (a -# git repository with a fake scripts/checkpatch.pl): sign-offs, prefixes and -# checkpatch errors. Its device-tree comparison runs against a second +# git repository with a fake scripts/checkpatch.pl): sign-offs, prefixes, +# checkpatch errors and warnings, and a checker that is missing or doesn't +# run to the end. Its device-tree comparison runs against a second # stand-in whose make prints the messages each board file asks for; the # real builds (clang, dtc, dt-validate) need a real tree and are run by hand. set -euo pipefail @@ -13,10 +14,17 @@ fail() { echo "not ok - $*" >&2; exit 1; } k=$t/linux mkdir -p "$k/scripts" "$k/drivers" -# checkpatch's stand-in: an ERROR for any added line with "BADSTYLE". +# checkpatch's stand-in: an ERROR for any added line with "BADSTYLE", a +# WARNING for "WARNSTYLE", and checkpatch's exit status: 1 when it reported +# something. cat > "$k/scripts/checkpatch.pl" <<'EOF' #!/usr/bin/perl -while () { print "-:1: ERROR:CODE_STYLE: bad style\n" if /^\+.*BADSTYLE/; } +my $st = 0; +while () { + if (/^\+.*BADSTYLE/) { print "-:1: ERROR:CODE_STYLE: bad style\n"; $st = 1; } + if (/^\+.*WARNSTYLE/) { print "-:1: WARNING:CODE_STYLE: odd style\n"; $st = 1; } +} +exit $st; EOF chmod +x "$k/scripts/checkpatch.pl" cd "$k" @@ -26,11 +34,12 @@ git config user.email human@example.org echo base > drivers/a.c git add -A && git commit -q -m "base" -run() { # name expect pattern +run() { # name expect [pattern] [absent pattern]; $rbase and $rhead stand in for omamobile and HEAD local out status=0 - out=$("$here/tools/kernel-pr-check" -C "$k" omamobile HEAD 2>&1) || status=$? + out=$("$here/tools/kernel-pr-check" -C "$k" "${rbase:-omamobile}" "${rhead:-HEAD}" 2>&1) || status=$? [[ $status == "$2" ]] || { echo "$out"; fail "$1: exit $status, expected $2"; } [[ -z ${3:-} ]] || grep -c -- "$3" <<<"$out" >/dev/null || { echo "$out"; fail "$1: says '$3'"; } + [[ -z ${4:-} ]] || ! grep -c -- "$4" <<<"$out" >/dev/null || { echo "$out"; fail "$1: says '$4'"; } } branch() { git checkout -q omamobile && git checkout -q -b "$1"; } @@ -67,9 +76,40 @@ echo "ok - a subject without a subsystem prefix fails" branch style echo "BADSTYLE" >> drivers/a.c git commit -q -am "drivers: a: style" -m "Signed-off-by: A Human " -run "a checkpatch error" 1 "checkpatch --strict errors" +run "a checkpatch error" 1 "checkpatch --strict errors" "didn't run to the end" echo "ok - a checkpatch error fails" +branch warn +echo "WARNSTYLE" >> drivers/a.c +git commit -q -am "drivers: a: warn" -m "Signed-off-by: A Human " +run "a checkpatch warning" 0 "checkpatch --strict: no errors (1 warnings" +echo "ok - a checkpatch warning (exit 1) is listed, not failed" + +# A checker that doesn't run to the end fails, and what it said on stderr +# is shown. checkpatch comes from the merge base, so each one is committed +# on its own base, with a clean change on top. +cpbase() { # name [checker] (no checker: the base deletes it) + branch "cp-$1" + if [[ -n ${2:-} ]]; then printf '%s\n' "$2" > scripts/checkpatch.pl; else git rm -q scripts/checkpatch.pl; fi + git commit -q -am "base: $1 checker" + git checkout -q -b "cp-$1-change" + echo x >> drivers/a.c + git commit -q -am "drivers: a: a clean change" -m "Signed-off-by: A Human " + git checkout -q omamobile # the tree the tool reads always has a checker +} +cpbase crash 'while () {} print STDERR "checkpatch: crashed here\n"; exit 255;' +rbase=cp-crash rhead=cp-crash-change run "a crashing checker" 1 "^ checkpatch: crashed here" "no errors" +cpbase syntax 'sub {' +rbase=cp-syntax rhead=cp-syntax-change run "a checker that doesn't compile" 1 "didn't run to the end" "no errors" +cpbase stdout 'while () {} print "checkpatch: needs a newer perl\n"; exit 1;' +rbase=cp-stdout rhead=cp-stdout-change run "a checker that exits 1 reporting nothing" 1 "^ checkpatch: needs a newer perl" "no errors" +cpbase signal 'kill "TERM", $$;' +rbase=cp-signal rhead=cp-signal-change run "a checker killed by a signal" 1 ": exit 143$" "no errors" +echo "ok - a checker that crashes, doesn't compile, exits 1 with no report or dies on a signal fails, with what it said" +cpbase missing +rbase=cp-missing rhead=cp-missing-change run "a merge base without a checker" 1 "Can't open perl script" "no errors" +echo "ok - a merge base without scripts/checkpatch.pl fails" + # CI runs `kernel-pr-check -C linux` from the directory that contains the # kernel checkout. The path has to still work after the tool cds in. rel_status=0 diff --git a/tools/kernel-pr-check b/tools/kernel-pr-check index efca37ea9..ccd1820fe 100755 --- a/tools/kernel-pr-check +++ b/tools/kernel-pr-check @@ -9,7 +9,8 @@ # - every subject has a subsystem prefix ("clk: samsung: ..."); # - scripts/checkpatch.pl --strict on each commit, against that commit's # bindings and MAINTAINERS: errors fail, warnings -# are listed; +# are listed, and a checker that is missing or doesn't run to the end +# fails with what it printed; # - tools/pr-check's policy checks: identifiers, blobs; # - with --build OUT: each touched C file builds with W=1 (arm64, LLVM, # defconfig plus kernel/configs/omamobile.config), every touched @@ -53,7 +54,7 @@ while (($#)); do -C) kernel=${2:?-C needs a kernel tree}; shift 2 ;; --build) out=${2:?--build needs an output directory}; shift 2 ;; --merge) merge=1; shift ;; - -h|--help) sed -n '3,35p' "$self"; exit 0 ;; + -h|--help) sed -n '3,37p' "$self"; exit 0 ;; -*) echo "kernel-pr-check: unknown option $1" >&2; exit 2 ;; *) break ;; esac @@ -160,12 +161,15 @@ done for e in "$kernel"/Documentation/*; do [[ ${e##*/} == devicetree ]] || ln -s "$e" "$cpdir/tree/Documentation/${e##*/}" done -cp_errors="" cp_warnings="" cp_count=0 +# A checker that is missing, can't run or crashes fails the check, with what +# it said: its empty report is not a clean one. +cp_errors="" cp_warnings="" cp_count=0 cp_broken="" for c in $commits; do rm -rf "$cpdir/tree/Documentation/devicetree" "$cpdir/tree/MAINTAINERS" docs=$(git ls-tree --name-only "$c" Documentation/devicetree MAINTAINERS) [[ -n $docs ]] && git archive "$c" $docs | tar -x -C "$cpdir/tree" - report=$(git format-patch -1 --stdout "$c" | perl "$cpdir/checker/scripts/checkpatch.pl" --root="$cpdir/tree" --strict --no-signoff --terse --show-types - 2>/dev/null) + report=$({ git format-patch -1 --stdout "$c" | perl "$cpdir/checker/scripts/checkpatch.pl" --root="$cpdir/tree" --strict --no-signoff --terse --show-types -; } 2>"$cpdir/stderr") + cp_status=$? short=$(git log -1 --format='%h %s' "$c") errors=$(grep -E ': ERROR:' <<<"$report" || true) [[ -n $errors ]] && cp_errors+=" $short"$'\n'"$(sed 's/^/ /' <<<"$errors")"$'\n' @@ -174,9 +178,20 @@ for c in $commits; do cp_count=$((cp_count + $(wc -l <<<"$warnings"))) cp_warnings+="# $short"$'\n'"$(sed -E -e 's/^-:[0-9]+: /# /' -e "s,$cpdir/tree/,," <<<"$warnings")"$'\n' fi + # checkpatch exits 1 when it reports something, 0 when it doesn't. + # Anything else, or a 1 with nothing reported, means it never finished: + # show what it said, on stderr and in any stdout line that isn't a + # diagnostic (some of its own failures print there). + if ((cp_status > 1)) || { ((cp_status == 1)) && [[ -z $errors$warnings ]]; }; then + cp_said=$({ grep -vE ': (ERROR|WARNING|CHECK):' <<<"$report"; cat "$cpdir/stderr"; } | sed -e '/^$/d' -e "s,$cpdir/,,g" -e 's/^/ /') + cp_broken+=" $short: exit $cp_status"$'\n'"${cp_said:+$cp_said$'\n'}" + fi done -[[ -z $cp_errors ]] && pass "checkpatch --strict: no errors ($cp_count warnings and checks to read)" || fail "checkpatch --strict errors: +[[ -n $cp_broken ]] && fail "checkpatch --strict didn't run to the end: +${cp_broken%$'\n'}" +[[ -n $cp_errors ]] && fail "checkpatch --strict errors: ${cp_errors%$'\n'}" +[[ -z $cp_broken$cp_errors ]] && pass "checkpatch --strict: no errors ($cp_count warnings and checks to read)" # The warnings themselves, so a reviewer reads them without rerunning it. [[ -n $cp_warnings ]] && printf '%s' "$cp_warnings" From 8455c9aba3c924b5b1d9cceb5b06b5047ffb15c7 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Sun, 1 Nov 2026 22:14:14 +0000 Subject: [PATCH 2/2] tools/kernel-pr-check: refuse a --build that holds the tree where it deletes The builds delete and rewrite OUT/src, OUT/src-base, OUT/build and OUT/build-base, but --build was only refused inside the kernel tree. An OUT one level above a tree named src (-C /x/src --build /x) passed, and the builds' rm -rf of OUT/src deleted the checkout the tool promises never to touch. An OUT/src that holds a worktree's git directory went the same way, and a symlink at OUT/build into the tree had make build in the tree and the device-tree step delete its arch/arm64/boot/dts. Resolve every directory the builds delete or rewrite, symlinks included, and refuse an OUT where one of them is the tree or its git directory, holds it or lies inside it. The tree's own path is now resolved physically too, so a symlinked -C is compared by what it points at. An OUT beside the tree, or holding a tree under another name, still builds. The guard and the deletes share one list of what is deleted, so a new delete can't slip past it. The header says where the builds go, and --help now prints all of it (it stopped a line short before). tests/kernel-pr-check covers these layouts, and a symlink deeper in OUT/build, with an rm that records what it is asked to delete under the fixtures and deletes none of it. Co-Authored-By: Claude Opus 5.5 --- tests/kernel-pr-check | 33 +++++++++++++++++++++++++++++++++ tools/kernel-pr-check | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/tests/kernel-pr-check b/tests/kernel-pr-check index 4b7497dd7..659276d2d 100755 --- a/tests/kernel-pr-check +++ b/tests/kernel-pr-check @@ -93,6 +93,39 @@ rel_build_out=$(cd "$k" && "$here/tools/kernel-pr-check" -C . --build out omamob [[ $rel_build_status == 2 ]] || { echo "$rel_build_out"; fail "a relative --build inside the tree: exit $rel_build_status, expected 2"; } echo "ok - --build is refused inside the kernel tree, relative or not" +# Nor where the builds would delete the tree or its git directory. clang and +# ld.lld stubs let the tool reach the builds, and its rm records what under +# $c (resolved, as the rm compares resolved paths) it is asked to delete and +# deletes none of it: a regression reports the deletion instead of making it. +c=$(cd -- "$t" && pwd -P)/collide real_rm=$(command -v rm) +mkdir -p "$c/bin" "$c/link-out" "$c/deep-out/build" +printf '#!/bin/sh\nexit 0\n' > "$c/bin/clang" +cp "$c/bin/clang" "$c/bin/ld.lld" +cat > "$c/bin/rm" <<'EOF' +#!/bin/bash +for a; do [[ $a == -* ]] || [[ $(realpath -m -- "$a")/ != "$COLLIDE"/* ]] || { echo "$a" >> "$COLLIDE/rm.log"; exit 1; }; done +exec "$REAL_RM" "$@" +EOF +chmod +x "$c/bin/clang" "$c/bin/ld.lld" "$c/bin/rm" +git clone -q "$k" "$c/src" +git clone -q "$k" "$c/main/src" +git -C "$c/main/src" worktree add -q "$c/main/wt" HEAD +ln -s "$c/src" "$c/link-out/build" +ln -s "$c/src/arch" "$c/deep-out/build/arch" +refused() { # name tree out + local out status=0 + out=$(PATH=$c/bin:$PATH COLLIDE=$c REAL_RM=$real_rm "$here/tools/kernel-pr-check" -C "$2" --build "$3" origin/omamobile HEAD 2>&1) || status=$? + [[ ! -e $c/rm.log ]] || { echo "$out"; fail "$1: deleted $(paste -sd' ' "$c/rm.log")"; } + [[ $status == 2 ]] || { echo "$out"; fail "$1: exit $status, expected 2"; } + grep -c 'overlaps the kernel tree' <<<"$out" >/dev/null || { echo "$out"; fail "$1: silent"; } +} +refused "OUT holds a tree named src" "$c/src" "$c" +refused "OUT/build links to the tree" "$c/src" "$c/link-out" +refused "OUT/build/arch links into the tree" "$c/src" "$c/deep-out" +refused "OUT/src holds a worktree's git directory" "$c/main/wt" "$c/main" +[[ -x $c/src/scripts/checkpatch.pl && -d $c/main/src/.git ]] || fail "a collision fixture lost its tree" +echo "ok - --build is refused where its src, src-base, build or build-base would delete the tree or its git directory" + # --merge: the base moves on after the branch is cut. A clean merge passes # and says so; a conflict fails as "rebase needed" while the commit checks # still run. diff --git a/tools/kernel-pr-check b/tools/kernel-pr-check index efca37ea9..720b4103a 100755 --- a/tools/kernel-pr-check +++ b/tools/kernel-pr-check @@ -25,9 +25,10 @@ # tools/kernel-pr-check [-C KERNEL] [--build OUT] [--merge] BASE [HEAD] # # KERNEL defaults to $KERNEL_BASE/linux (~/omamobile-kernel/linux, or -# ~/android/mainline/linux on older setups), HEAD to HEAD. It reads git and -# builds in OUT (and OUT-base, for the base's device trees); it never -# touches the tree's checkout or anything else. +# ~/android/mainline/linux on older setups), HEAD to HEAD. It reads git, +# exports what it builds to OUT/src and OUT/src-base and builds in OUT/build +# and OUT/build-base; it never touches the tree's checkout or anything else, +# and refuses an OUT that would. # # --merge builds what would land: HEAD merged onto BASE (a test merge, # written as git objects only), compared with BASE itself. The commit checks @@ -53,7 +54,7 @@ while (($#)); do -C) kernel=${2:?-C needs a kernel tree}; shift 2 ;; --build) out=${2:?--build needs an output directory}; shift 2 ;; --merge) merge=1; shift ;; - -h|--help) sed -n '3,35p' "$self"; exit 0 ;; + -h|--help) sed -n '3,37p' "$self"; exit 0 ;; -*) echo "kernel-pr-check: unknown option $1" >&2; exit 2 ;; *) break ;; esac @@ -67,14 +68,30 @@ if [[ ! -d $kernel ]]; then echo "kernel-pr-check: no such tree $kernel" >&2 exit 2 fi -kernel=$(CDPATH='' cd -- "$kernel" && pwd) -# --build too: the builds run after the cd, and they delete and re-extract -# directories under OUT, so OUT must never resolve inside the kernel tree. +# Symlinks resolved too, so the --build check below sees the real tree. +kernel=$(CDPATH='' cd -- "$kernel" && pwd -P) +# --build too: the builds run after the cd. They replace OUT/src and +# OUT/src-base, build in OUT/build and OUT/build-base and delete $stale there. +# None of these, symlinks resolved, may be the tree or its git directory (a +# worktree's lies outside it), hold it or lie inside it: an OUT that was the +# parent of a tree named src had the tree deleted. if [[ -n $out ]]; then out=$(realpath -m -- "$out") - case $out/ in - "$kernel"/*) echo "kernel-pr-check: --build $out is inside the kernel tree; use a directory outside it" >&2; exit 2 ;; - esac + src=$out/src srcb=$out/src-base + stale=("$out"/build{,-base}/{arch/arm64/boot/dts,Documentation/devicetree/bindings/processed-schema.json}) + inside() { [[ ${1%/}/ == "${2%/}"/* ]]; } # $1 is $2 or lies inside it + inside "$out" "$kernel" && { echo "kernel-pr-check: --build $out is inside the kernel tree; use a directory outside it" >&2; exit 2; } + gitdir=$(git -C "$kernel" rev-parse --path-format=absolute --git-common-dir 2>/dev/null) && gitdir=$(realpath -m -- "$gitdir") + for d in "$src" "$srcb" "$out"/build{,-base} "${stale[@]}"; do + r=$(realpath -m -- "$d") || { echo "kernel-pr-check: can't resolve $d" >&2; exit 2; } + for p in "$kernel" ${gitdir:+"$gitdir"}; do + inside "$r" "$p" || inside "$p" "$r" || continue + [[ $p == "$kernel" ]] && where="the kernel tree $p" || where="the kernel tree's git directory $p" + [[ $r == "$d" ]] || d+=" (that is, $r)" + echo "kernel-pr-check: --build $out overlaps $where: the builds replace $d; use another directory" >&2 + exit 2 + done + done mkdir -p -- "$out" || { echo "kernel-pr-check: can't create $out" >&2; exit 2; } fi cd -- "$kernel" || exit 2 @@ -201,7 +218,6 @@ if [[ -n $out ]]; then skip "builds: no clang or ld.lld" else # A clean export of what's built and its base, so the tree's own checkout is never touched. - src=$out/src srcb=$out/src-base rm -rf "$src" "$srcb"; mkdir -p "$src" "$srcb" git archive "$build_head" | tar -x -C "$src" k=(make -s -C "$src" O="$out/build" ARCH=arm64 LLVM=1 -j"$(nproc)") @@ -230,8 +246,7 @@ ${bad%$'\n'}"; fi # outputs so make rebuilds, and prints, every warning. The processed # schema goes too, or a reused OUT validates against an earlier # tree's bindings (kernel #32's own binding went unseen that way). - rm -rf "$out/build/arch/arm64/boot/dts" "$out/build-base/arch/arm64/boot/dts" \ - "$out"/build{,-base}/Documentation/devicetree/bindings/processed-schema.json + rm -rf "${stale[@]}" log=$("${k[@]}" W=1 $check $targets 2>&1); st=$? if ((st)); then fail "device trees don't build: