Skip to content

third_party: carry the two amdxdna patches every build of ours runs (bo_invalid_list UAF, TDR array recovery) - #1038

Merged
bong-water-water-bong merged 2 commits into
mainfrom
patches/amdxdna-tdr-recovery
Nov 5, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
patches/amdxdna-tdr-recovery

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Root cause

third_party/xdna-driver pins amd/xdna-driver at 526c6081, and two defects that both take the NPU down on this
hardware are not in that pin and not upstream:

  1. bo_invalid_list use-after-free at close — a client that closes while a BO sits on client->bo_invalid_list
    frees the client and leaves the BO linked into it (amdxdna_hmm_invalidate copied abo->client and took
    client->hwctx_srcu first) → kernel BUG at lib/list_debug.c:32. This is the 2026-11-04 01:48 panic, and the
    reason os/v3/puppy: the image builds amdxdna from pin 526c6081, which predates the bo_invalid_list fix, so the next stick ships the panicking module #971 exists: the image builds the module from the pin, so the next stick ships the panicking driver.
  2. No recovery after a TDR — the restart is gated on a condition a fully-wedged context never satisfies, so
    aie2_hwctx_restart() fails -22 and every later request hangs until a reboot (amdxdna: one TDR leaves the NPU array unrecoverable on the appliance (hwctx_restart -22, 'restore data not reclaimed'), so a single wedged request needs a reboot #1005).

Both fixes have been running on the box since 2026-11-04, but only as uncommitted working-tree changes in scratch
trees on that one box
— which is exactly how the fix went missing from an image build before. Nothing in git records
which source the shipping amdxdna.ko came from, and g3-probe.sh cannot tell (it asserts presence/vermagic/signer).

What this PR does

  • third_party/patches/amdxdna/0001-…-fix-the-bo_invalid_list-use-after-free.patch (4 files, +48/−4)
  • third_party/patches/amdxdna/0002-…-recover-the-array-after-a-TDR.patch (5 files, +91/−19)
  • third_party/patches/amdxdna/README.md — why they are a series, the build/sign/install/initramfs recipe (the
    driver's own modules target with ARCH, which generates config_kernel.h), the srcversion each build must
    produce, the loaded-vs-file check, and what a pin bump invalidates.
  • docs/npu.md — the pin paragraph now points at the directory instead of leaving the patches invisible.

No pin change, no kernel change, nothing applied automatically: this makes the driver build reproducible and the
missing-fix failure mode detectable (cat /sys/module/amdxdna/srcversion), and gives #971's option A a home.

Measured, before/after

claim evidence
the series reproduces the running driver's source apply both patches to a clean 526c6081 checkout → every drivers/accel/amdxdna/{*.c,*.h} is byte-identical (cmp) to the tree the loaded module was built from
…and its fingerprint rebuild that tree with MODULE_VER_STR='2.21.0_20261104,526c6081974e…' → srcversion 56AB94758C18FEEC838D18C = cat /sys/module/amdxdna/srcversion on the box (measured 2026-11-04 20:17)
0002 actually recovers the array provocation with tdr_timeout_ms=1: aie2_tdr_recover_all: Array stopped dispatching, power-cycling NPU → aie2_hw_reset → the array answers again, no reboot (2026-11-04 10:48–10:50, 3 prior attempts never entered the branch)
0001 fixes the panic the 01:48 panic replay no longer panics; the box has run this build across six reboots since
firmware/pin/board the c22 and c23 sticks carry this same build (srcversion 56AB9475… in their zdrv), and the c23 board run passed its ladder 400/600/1000 with 0 TDRs

Notes for review:

closes #971
closes #1005

…loses #971, #1005)

The xdna-driver pin (526c6081) is pre-fix and neither fix is upstream, so a
module built from the pin alone panics on one close path (bo_invalid_list
use-after-free) and cannot bring the array back after a TDR (hwctx_restart
-22, every later request hangs until a reboot). Until now both patches
existed only as uncommitted working-tree changes in scratch trees on one
box, which is how the c22 image ended up shipping the panicking driver.

Add third_party/patches/amdxdna/: the two patches as a series against the
pin, and a README with the build (ARCH + the driver's own modules target,
which generates config_kernel.h), sign, install and initramfs recipe, the
srcversion each build must produce, and what a pin bump invalidates.

Verified: applying both patches to a clean 526c6081 checkout reproduces the
amdxdna sources of the module this box runs byte for byte (cmp over every
.c/.h), and rebuilding that tree with MODULE_VER_STR='2.21.0_20261104,<pin>'
produces srcversion 56AB94758C18FEEC838D18C = /sys/module/amdxdna/srcversion
on the box. docs/npu.md points at it from the pin paragraph.
@bong-water-water-bong bong-water-water-bong added the tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied label Nov 5, 2026

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY at 964af2235

This is the panic fix and the stick's dependency, so I checked the carrier itself rather than the prose.

The series applies to the pin, verified from a fresh upstream clone (not from any tree of ours):

git clone https://github.com/amd/xdna-driver && git checkout 526c6081
git apply --check 0001-*.patch  -> clean
git apply          0001-*.patch
git apply --check 0002-*.patch  -> clean
git apply          0002-*.patch
git diff --stat                 -> 9 files changed, 139 insertions(+), 23 deletions(-)

9 files / +139/−23 is exactly the README's 4 files, +48/−4 plus 5 files, +91/−19, and "order matters" is real — 0002's hunks sit on 0001's post-state. The patches are ours (From: pi-hrx-fabric <[email protected]>), git-format, and nothing in the engine tree re-heads an AMD file in place, which is the pattern this repo wants.

The box is running the documented build. /sys/module/amdxdna/srcversion = 56AB94758C18FEEC838D18C, which is the README's "pin + 0001 + 0002 (what the box and the c22/c23 images run)" row, and modinfo -F srcversion /lib/modules/7.4.0-rc1-1bit/updates/amdxdna.ko reports the same value here, so the file and the loaded module agree on this boot. That is the strongest claim in the PR and it re-derives.

tools/copyright.py --check → rc=0, so the two .patch files pass while keeping their From <sha> first line (no header is prepended to a patch — that would corrupt it).

One thing to be explicit about, because it is the difference between a fix and a plan: nothing in this tree applies the series. The only reference outside third_party/patches/amdxdna/ is docs/npu.md:1208, which points at the README — no os/mini/mkimage/container-build.sh hook. So merging this changes what the next driver build can consume, not what the c24 stick ships; the image-side half is #971, as the README itself says ("#971 carries the image-side half of shipping it"). Worth stating in the merge commit or the owner's note, so nobody reads "carried" as "shipped".

Two nits:

  1. The fingerprint table has no pin-only row, so a build that comes out at an unexpected srcversion cannot be told apart from one built without the patches at a glance. If the pre-fix value is recorded anywhere (the 01:48 panic's boot would have had it), add it — the README already explains that the version string is part of the hash, which is exactly why a "this means the patches are missing" row is worth having.
  2. ## When the pin moves says git apply --3way is "the sane way to see whether upstream moved under a hunk" — right, but it is worth one line on what to do when it succeeds with conflicts resolved: the fingerprint table then has no row at all, and the srcversion check silently becomes unverifiable until the 0002 provocation is re-run. Say that the table must be regenerated before the pin bump merges (which the paragraph implies but does not require).

Not re-run: the 0002 provocation (a TDR must power-cycle the NPU and the array must answer afterwards). That needs an npu-hold ticket, a wedge, and the driver loaded from this build — the author's 2026-11-04 10:48-10:50 run is the record, and it is the one measurement here that cannot be reproduced on demand without taking the device down deliberately.

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 97ad764)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

971 - Partially compliant

Compliant requirements:

  • The PR introduces patches for the bo_invalid_list use-after-free bug
  • The patches are applied during the build process
  • The build process now includes the fix, ensuring that the module built from the pin includes the fix

Non-compliant requirements:

  • The PR does not update the submodule pin to a version that includes the fix upstream

Requires further human verification:

  • Verification that the patches are correctly applied and the module built with the correct srcversion

1005 - Partially compliant

Compliant requirements:

  • The PR introduces a patch to recover the array after a TDR by power-cycling the NPU
  • The recovery mechanism ensures that new contexts and submissions are refused with -ENODEV instead of hanging
  • The mechanism logs a clear error message indicating that the array is unusable until reload

Non-compliant requirements:

  • The PR does not include a mechanism to detect and handle the race condition mentioned in the ticket description

Requires further human verification:

  • Verification that the TDR recovery mechanism works as expected in a real-world scenario
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Potential UAF Fix

The patch introduces a dying flag to prevent use-after-free in bo_invalid_list during client cleanup. While the fix appears correct, it's important to ensure that all paths that could lead to a BO being freed while still linked into client->bo_invalid_list are properly handled. The change modifies amdxdna_hmm_invalidate and amdxdna_mark_mapp_invalid to check for abo->dying before adding the BO to the list, and also ensures that list_del_init is called under notifier_lock in the free path.

+	if (client && !abo->dying && list_empty(&abo->node))
 		list_add_tail(&abo->node, &client->bo_invalid_list);
TDR Recovery Mechanism

The patch modifies the TDR recovery mechanism to power-cycle the NPU when a TDR occurs and the restart fails. This is intended to prevent the array from becoming unrecoverable. However, the change in aie2_hw_reset to return an int instead of void and the introduction of hw_dead flag need careful review to ensure that the recovery logic is robust and does not introduce new failure modes.

+int aie2_hw_reset(struct amdxdna_dev *xdna)

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 97ad764

@bong-water-water-bong
bong-water-water-bong merged commit 070225f into main Nov 5, 2026
9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the patches/amdxdna-tdr-recovery branch November 5, 2026 00:48
bong-water-water-bong pushed a commit that referenced this pull request Nov 5, 2026
…0008, not 0007' is answered by the artifact

Owner: amdxdna 0008 is being built and c25 must carry it. Nothing in the gate could tell the two apart: vermagic and the
MOK signature are identical for every module we have ever built, and the depmod path only says which FILE wins. The
srcversion is the only field that names the source (that is what #1038 established the other way round).

Measured both arms on the c25 ISO:
  --srcversion 56AB94758C18FEEC838D18C (what it carries)  -> ok, rc=0
  --srcversion DEADBEEF0000000000000000 (a stale cut)    -> FAIL 'this cut must carry ... (a stale module)', rc=1
so the failure mode is a red gate, not a note.
bong-water-water-bong added a commit that referenced this pull request Nov 5, 2026
…, lemond debug, a 32M ring, thermals) for c25 (#1057)

* os/v3 puppy: the live USB logs it all as proof - boot proof, lemond debug, a sized ring, thermals and a proof bundle

Cycle 25 (owner: "make sure the live usb log verbose it all for proof that it works"). Five pieces, each with the
measurement that decided it:

1. 1bit-proof: boot-proof.txt once per boot (cmdline, uname, Secure Boot via mokutil AND the efivar byte, modules
   with srcversions, amdxdna's tdr_timeout_ms, firmware hashes, PCI/accel devices, RAM/GTT/VRAM, the boot timeline,
   RELEASE-TAG and the sha256sums result), and a bundle of the boot's log directory + SHA256SUMS as
   proof-<tag>-<date>.tar.zst with its sha256 printed. A value that cannot be read is written 'n/a <reason>' and
   never omitted - a missing line and a zero look identical to a reviewer.
2. 10lemonade seeds log_level=debug (+ 8 MB x 3 rotation) into lemond's config.json when the key is absent and never
   overwrites a level somebody set. Measured on the pinned build: lemond has no level flag and no env var (only
   -v/--version); the accepted strings are trace|debug|info|warning|error|fatal|none, and debug is what prints
   'Debug logging enabled - subprocess output will be visible' - the engine's own lines then land in lemond.log.
   trace is refused on purpose: per-token chatter would spend the whole evidence budget on itself.
3. 1bit-logs samples Tctl / fan rpm / package W every 10 s BUT ONLY while an engine child exists, in one loop with
   the 15 s log copy (two cadences, one sleeper, so they cannot drift). Unreadable sensors are 'n/a', not 0.
4. mkimage adds log_buf_len=32M to the shared cmdline and refuses to write an image without it: c23's ring wrapped
   and 1bit-logs could only mark the loss.
5. vm-check gains four rows: the ring size, the boot proof's labels (a file that exists but says nothing is what a
   stub produces), the thermal gate in both directions (engaged when serving, absent when idle), and the bundle
   being READABLE through zstd|tar - not merely present.

Checks: bash -n, shellcheck -S warning and tools/copyright.py --check clean; 1bit-proof is exercised on this host as
root (real ring, tdr_timeout_ms=15000, per-file firmware hashes, SB byte=1); the lemond seed is exercised in all three
states (absent / no key / owner-set) and log_level=debug was confirmed by a live lemond run; the thermal sampler is
exercised with a stubbed 'loaded' model (tctl=61 fan=n/a pkg_w=104.2) and with the real gate (no line written).

* os/v3 puppy: the thermal-gate row counts children portably (pgrep -c is not in every build)

* os/v3 puppy: 1bit-proof counts SHA256SUMS entries, not lines (the WARNING summary made 1 bad file read as 2)

* os/v3 puppy: the boot-proof row asserts instead of reporting (want=None made it INFO, so it could never fire)

Measured on the c25 image: the run came back 62 PASS / 0 FAIL / 3 SKIP with this row as INFO - the very 'a sentence is
not a check' shape this row exists to remove. It now requires every label to be present AND at least 20 lines, so a stub
that writes only the labels cannot pass either.

* os/v3 puppy: g3-probe takes --srcversion, so 'this cut carries patch 0008, not 0007' is answered by the artifact

Owner: amdxdna 0008 is being built and c25 must carry it. Nothing in the gate could tell the two apart: vermagic and the
MOK signature are identical for every module we have ever built, and the depmod path only says which FILE wins. The
srcversion is the only field that names the source (that is what #1038 established the other way round).

Measured both arms on the c25 ISO:
  --srcversion 56AB94758C18FEEC838D18C (what it carries)  -> ok, rc=0
  --srcversion DEADBEEF0000000000000000 (a stale cut)    -> FAIL 'this cut must carry ... (a stale module)', rc=1
so the failure mode is a red gate, not a note.

---------

Co-authored-by: pi-os3 <pi-os3@local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review effort 4/5 tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied

Projects

None yet

1 participant