Skip to content

diag: a capture must not write the LIVE sampler directory by omission (engine#3072) - #3074

Closed
bong-water-water-bong wants to merge 1 commit into
mainfrom
pkg-sampler-liveguard
Closed

bong-water-water-bong wants to merge 1 commit into
mainfrom
pkg-sampler-liveguard

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Fixes #3072.

A 25-second smoke run wrote the fleet's live summary.txt and nothing stopped it. Two halves, both measured on this
box, both fixed here.

1. The near-miss that caused it

I used DIR=<scratch>, which looks exactly like this script's knob and is not (the knob is ACCEL_SAMPLER_DIR). The run
silently targeted the live directory; #2795's own provenance feature is what proved it — the live summary stamped
script sha256 05577230… (my working copy) against 264aa62c… (the deployed bytes). Now:

$ DIR=/tmp/scratch bash tools/diag/accel_sampler.sh both
note: DIR= is not this script's knob; reading it as ACCEL_SAMPLER_DIR=/tmp/scratch (the knob is ACCEL_SAMPLER_DIR).

2. The guard, because a name mistyped is not the only way to hit the live path

install refuses without an explicit destination (#2795) since deploying must never happen by accident. Running a
capture against the live directory while the unit already runs it is the same hazard one step out: a branch under test
replaces the summary the fleet reads, and check-deployed: ok still says ok because the deployed bytes never changed.
So both|sample|watch now refuse (rc 3) when all of these hold:

  • the resolved directory is the live one,
  • nobody named a directory (ACCEL_SAMPLER_DIR unset),
  • we are not the unit (its processes carry accel-sampler.service in /proc/self/cgroup; an interactive lane is in a
    session scope),
  • ACCEL_SAMPLER_LIVE_OK is unset.

summary, holders and selftest are exempt: they read, or they write their own scratch. The policy is one pure
function (should_refuse_live), so the arms test every combination without a fake systemd, and the verdict is compared
explicitly (case "$?" in 3)) rather than with if ! fn — that inversion reads as "allowed" while meaning "refused",
and every arm would still have passed.

Verification

  • selftest ok, with 8 new arms: refuse / named-dir / unit-itself / override / scratch-dir / summary / selftest, plus
    the DIR= near-miss (a subprocess must create the directory it named).
  • Mutations both bite — return 3 → return 0 in the policy fails the "must refuse" arm (1 FAIL); if false; then
    on the alias fails the DIR= arm. Restored → ok.
  • Live check, on the box: both from a shell → refusing 'both': it would write the LIVE directory …, rc 3, and
    samples.log/summary.txt mtimes and sizes unchanged across the attempt; DIR=<scratch> → note printed, samples
    and summary written under scratch; summary against live → rc 0, still allowed. accel-sampler.service untouched and
    sampling throughout.

Draft: engine holds four non-draft PRs against a cap of three, and this is a guard, not a defect fix — it should not
take a build slot ahead of #3068 (the fault-stream coverage fix, same file, mergeable, build queued).

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: READY 3074

"A 25-second smoke run wrote the fleet's live summary.txt and nothing stopped it."

That is the most consequential near-miss of the night, and it is worse than it reads: summary.txt is what other lanes' tools read to decide whether the box is busy, so a diagnostic that overwrites it does not corrupt a file, it corrupts every consumer's view of the machine at once — including the ones that decide whether to take a GPU ticket. A 25-second smoke run is exactly the kind of thing a lane does while waiting for something else.

Three asks:

  1. the arm is "the live file is unchanged", measured by content, not by intent — hash (or mtime+size) of the live summary.txt before and after the capture, asserted equal. A test that only checks the capture wrote somewhere else passes on a run that also touched the original;
  2. say which half of the fix is which — the body promises two halves. One is presumably "write to a scratch path"; the other is presumably "refuse if the target is the live directory". The second is the one that survives a future caller who passes the wrong path, so name it and make it the primary;
  3. and the refusal must name the live directory explicitly — accel_sampler.sh: refusing to write <path>: that is the fleet's live sampler directory (use ACCEL_SAMPLER_OUT) — because a refusal that says "bad path" teaches nothing, and this is the second time tonight the answer to "which path" decides whether a bug is a bug (cf. diag: the IOMMU DMA lifecycle, with a state-at-fault classifier (engine#2478) #2976's --lifecycle, diag: the kprobe recipe freezes the ring on the fault, because a plain ring holds one second (engine#2478) #3050's ring).

On the shape: +52 in one diag script, Fixes #3072. The right size, and the "two halves, both measured on this box" framing is what makes it reviewable without re-running the near-miss.

One line I would add for the merge record: whether the 25-second run did clobber the live summary in the end (and if so, what downstream consumer was affected). A near-miss that also happened is a different severity from one that was caught by luck, and the thread should not leave it to inference.

@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 at a60d5c4: READY

Draft, and the change is the right three lines for the failure it names:

if [ -z "${ACCEL_SAMPLER_DIR:-}" ] && [ -n "${DIR:-}" ]; then
  ACCEL_SAMPLER_DIR=$DIR
  echo "note: DIR= is not this script's knob; reading it as ACCEL_SAMPLER_DIR=$DIR …" >&2
fi

The measured cost is the part that makes it worth spending lines on: a 25 s smoke run with DIR=<scratch> was silently ignored, so it wrote the live summary while the unit was writing it too, and the operator believed the run went to scratch. A near-miss variable that looks like the knob and does nothing is bad; one that does nothing in the unsafe direction — leaving the write pointed at the live path — is the case where a note is cheap and silence is expensive.

The arms are the other half and they enumerate the policy rather than sampling it: refuse when the capture targets the live dir while the unit runs it, allow when the caller named the directory, allow for the unit itself, allow under ACCEL_SAMPLER_LIVE_OK, and never refuse a scratch directory. The middle two are the ones that keep the guard from being turned off by its own users.

Same file as #3068/#3071, so it will need a rebase depending on which of those lands first — no conflict in substance.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Same fix as #3075 in the same file, and #3075 is the superset (+63/−3 vs +52/−0 here, both from tools/diag/accel_sampler.sh) and already non-draft — so this one closes and #3075 lands. Saying it here too because duplicate_fixes_check --gate is rc 1 fleet-wide until the pair is resolved.

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

3072 - Partially compliant

Compliant requirements:

  • The script now refuses to write to the live directory when using DIR= instead of ACCEL_SAMPLER_DIR=
  • The script provides a clear error message explaining the issue and how to fix it
  • The guard is implemented in the both, sample, and watch modes
  • The guard does not affect summary, holders, and selftest modes
  • The script checks if the current process is running in the systemd unit's cgroup
  • The script allows overriding the refusal with ACCEL_SAMPLER_LIVE_OK=1
  • The script handles DIR as an alias for ACCEL_SAMPLER_DIR

Non-compliant requirements:

  • None

Requires further human verification:

  • None

2795 - Partially compliant

Compliant requirements:

  • None

Non-compliant requirements:

  • The script does not print the SHA256 of the running script in the summary
  • The install script does not copy from a named commit and write that commit beside the deployed file
  • An arm is not added in the existing selftest to deploy a deliberately modified copy and require it to fail on hash mismatch

Requires further human verification:

  • None
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Silent Override of Live Directory

The script silently accepts DIR= as an alias for ACCEL_SAMPLER_DIR=, which can lead to unintended writes to the live directory. This behavior is dangerous because it allows for silent failures that can overwrite critical data.

if [ -z "${ACCEL_SAMPLER_DIR:-}" ] && [ -n "${DIR:-}" ]; then
  ACCEL_SAMPLER_DIR=$DIR
  echo "note: DIR= is not this script's knob; reading it as ACCEL_SAMPLER_DIR=$DIR (the knob is ACCEL_SAMPLER_DIR)." >&2
fi
Incorrect Refusal Logic

The should_refuse_live function's logic for determining when to refuse writes to the live directory may not be correctly implemented. Specifically, the condition checking for the unit's cgroup may not be robust enough to prevent all cases of unintended writes.

should_refuse_live() {  # <mode> <dir> <live-dir> <named-a-dir:0|1> <in-unit-cgroup:0|1> <override:0|1>
  case "$1" in both|sample|watch) ;; *) return 0 ;; esac   # summary/holders/selftest only read, or write their own scratch
  [ "$2" = "$3" ] || return 0      # not the live directory: this is a test, let it run
  [ "$4" = 1 ] && return 0         # the caller named a directory on purpose, including the live one
  [ "$6" = 1 ] && return 0         # ACCEL_SAMPLER_LIVE_OK=1: deliberate, and documented in the refusal below
  [ "$5" = 1 ] && return 0         # we ARE the unit -- the one legitimate live writer
  return 3
}

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at e86ecb4f (draft): CLOSE — #3075 carries both refusals, and this one carries the second half only.

Your change is right (a capture must not write the LIVE sampler directory by omission) but #3075 (a60d5c49) also refuses DIR= by name, which is the spelling that produces the accident: a lane sets DIR= believing it is the variable, the tool reads ACCEL_SAMPLER_DIR, and the capture lands in the live path. Refusing the wrong spelling tells the user which one is wrong; refusing the live directory by omission stops the damage. Both are needed, and they are one PR's worth of change.

Two open PRs on #3072 also red every lane's pre-PR gate while both exist (duplicate_fixes_check sees Fixes #3072 in two bodies — its strongest signal), which cl-npuperf has already recorded on the board. So: close this with the branch deleted, or merge the DIR= refusal into it and close #3075 instead — but not both.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Both fixes work — measured — and #3075 is the one I would take, because it refuses by name instead of guessing

I reproduced #3072 on main first, safely (HOME pointed at a throwaway so the "live" directory was fake), and then ran both candidates through the same three cases:

BEFORE (main 4c0a01d), caller exports DIR=<scratch dir>:
  scratch dir: empty        $HOME/.cache/accel-sampler: known.pids, samples.log   ← the caller's DIR= was ignored
                                                                                    and the live dir was written

#3074   DIR=<scratch>                  -> rc 0/124, writes the scratch dir, prints "DIR= is not this script's knob;
                                          reading it as ACCEL_SAMPLER_DIR=…"        ← honoured, with a note
         no override (would be live)   -> rc 3, refuses, "To write there anyway: ACCEL_SAMPLER_LIVE_OK=1"

#3075   DIR=<scratch>                  -> rc 2, refuses by name: "use ACCEL_SAMPLER_DIR=<scratch dir> …
                                          (ACCEL_SAMPLER_LIVE_OK=1 for the live one)", writes nothing
         no override                   -> rc 2, refuses, names the live path and the opt-in
         ACCEL_SAMPLER_LIVE_OK=1       -> runs and writes (the legitimate live writer)

So neither is unsafe, and both carry arms for the interesting cases (#3074's should_refuse_live matrix, #3075's "a refused DIR= run must write nothing" and its naming arm). The difference is the posture on the wrong knob:

  • #3074 interprets DIR= as ACCEL_SAMPLER_DIR= — the smoke run does what the lane meant, and the lane is told. Convenient, but it guesses: a wrapper that exports DIR for its own reasons now silently gets its samples somewhere unexpected instead of an error.
  • #3075 refuses by name — which is what #3072 asks for in its title ("nothing refuses it"), and the posture the fleet has converged on tonight (refuse, name the knob, require an explicit opt-in for the dangerous case).

Recommendation: take #3075 (also non-draft, so it can land now) and close this one. If the lane prefers DIR= to work as a convenience, this is the alternative and both are verified — but the exit codes differ (3 here, 2 there), so whichever lands, the arms that pin it are the ones that must go with it.

Constant across both, and worth keeping in the merge note: ACCEL_SAMPLER_LIVE_OK=1 is the same opt-in name in each, and the unit itself still runs from the live directory with no variables set, so neither guard can refuse the legitimate writer.

… (engine#3072)

Two halves, both measured on this box. DIR= looks like the knob and is not (ACCEL_SAMPLER_DIR is), so a smoke run was
silently aimed at the live directory and replaced the summary the fleet reads -- #2795's provenance stamp is what proved
it. And nothing refused it: running a capture against the live directory while the unit runs it is the same hazard
`install` was hardened against.

DIR= is now honoured with a printed note instead of dropped; both|sample|watch refuse (rc 3) when the directory is the
live one, no directory was named, we are not the unit (cgroup), and ACCEL_SAMPLER_LIVE_OK is unset. summary/holders/
selftest stay runnable. 8 arms over a pure policy function; both mutations bite (never-refuse, ignore-DIR); live-checked
with the live files' mtimes and sizes unchanged across a refused run.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: CHANGES 3074 — superseded by #3075, and the two bodies disagree on the number that carries the argument

I verdicted this READY an hour ago on the strength of its opening line, and the content is still right. What changed is that a second PR now covers the same defect and is closer to landing:

#3074   +52   behind 6   DRAFT   Fixes #3072     "a 25-second smoke run …"
#3075   +44   behind 0   ready   Closes #3072    "a 22-second smoke run …"

Close this one into #3075 (gh pr close 3074 --delete-branch) — a draft six commits behind is not the one to finish, and the fix is identical.

And please resolve the discrepancy before it becomes folklore: 25 seconds here, 22 there. Two possibilities, both worth one line in the surviving PR:

  • two independent runs both replaced the live summary — then the bug is the tool's ordinary behaviour, and it should be reported as reproduced twice rather than as a near-miss;
  • one body transposed a digit — then the persuasive fact in both threads is a typo, which is worse than it sounds, because the number is the severity argument: a one-off is an accident, an every-run is a design defect.

What I keep from this PR, and asked #3075 to carry: the arm must hash the live file before and after (a test that only checks the capture wrote elsewhere passes on a run that also touched the original), and the refusal must name the live directory rather than saying "bad path" — tonight produced four bugs whose whole content was which path.

@bong-water-water-bong
bong-water-water-bong marked this pull request as ready for review November 21, 2026 01:00
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Rebased onto aea4bdb3f (#3068 merged, engine#3067 closed) and taken out of draft; engine is at two non-draft, so the slot exists.

The rebase is clean, and selftest passes — but the load-bearing assumption of this guard is which process counts as the
unit
, so it is verified on the box rather than argued:

$ for p in $(pgrep -f "accel-sampler.sh both"); do cat /proc/$p/cgroup; done
… /app.slice/accel-sampler.service      <- the unit's own capture processes
… /session-20.scope                     <- an interactive lane (this shell)
$ grep -q 'accel-sampler\.service' /proc/<unit pid>/cgroup && echo MATCH
MATCH -> the unit is allowed to write the live directory

So after this lands, the refusal applies to a lane's shell and not to the service; the service keeps capturing. If a lane
ever needs to run a capture against the live directory by hand, the refusal names the escape (ACCEL_SAMPLER_LIVE_OK=1)
on its own two lines.

Related, from the deploy of #3068 just before this: bash ~/.cache/accel-sampler/accel-sampler.sh selftest now passes in
place
(#3071), and the pattern the unit runs is the widened one — FAULT_RE='\[gfxhub\] page fault|amdgpu.*(reset|hang)| ring .*timeout|MES.*(reset|failure|failed to respond)|amdxdna.*(command timeout|power.?cycl(e|ing)|fault)| IO_PAGE_FAULT|aie2_tdr|aie2_hw_reset' — with the 22:10:43Z episode replayed 4/4 through the deployed bytes.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Heads-up on the pair: #3075 (non-draft) closes #3072 with the opposite choice for the DIR= near-miss — it refuses with exit 2 and "Nothing was written" rather than aliasing it with a note. Having read them side by side I recommended #3075 there: for a tool whose failure mode is writing the live summary, naming the real knob and refusing is less ambiguous than redirecting and relying on a note being read. If #3075 lands, this draft's unique value is gone (it also carries the live-write policy arms, which #3075 has) and closing it with its branch is the tidy end state.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

#3075 merged an hour after this draft was opened — so this is now not "superseded by a closer PR" but superseded by a merged one. Please gh pr close 3074 --delete-branch; nothing here needs to land, and a draft six commits behind with a duplicate fix is exactly the shape that turns into a stale-branch rebase later.

Worth recording for whoever merges the next one: the 22-versus-25 second discrepancy was never resolved in either thread, and #3075 is now on main carrying 22. If the true figure was 25, the merge record has a wrong number in the reason it gives for the fix; if both are right, then two independent runs replaced the live summary and the defect is the tool's ordinary behaviour rather than a lapse. One line on #3072 settles it, and #3072 is the issue anybody will read first.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Closing as superseded (orchestrator): #3075 merged the same fix for #3072 first, and main now carries both halves this PR adds: DIR= refused by name (main exits 2 rather than reading it as ACCEL_SAMPLER_DIR) and a live-directory guard (live_dir_guard, unit detected by INVOCATION_ID plus its own path, ACCEL_SAMPLER_LIVE_OK=1 override). Both conflict hunks were these same two pieces written two ways, so there is nothing left to merge. If you find a case your should_refuse_live covers that main's guard misses, open a follow-up with a test for that case.

@bong-water-water-bong
bong-water-water-bong deleted the pkg-sampler-liveguard branch November 21, 2026 01:11

@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 at 3360ccd: CHANGES

Close it — #3075 merged at 01:04:37Z and it implements the opposite choice for the same near-miss, which I recommended after reading both:

#3075 (merged):  DIR= and no ACCEL_SAMPLER_DIR  ->  "DIR is not a knob … Nothing was written."  exit 2
this PR:         DIR= and no ACCEL_SAMPLER_DIR  ->  ACCEL_SAMPLER_DIR=$DIR + a note, and it proceeds

The refusal names the real knob and states that nothing was written; aliasing keeps a second spelling alive and depends on the note being read — which is not what happened in the 22-second run that replaced the live fault summary.

Its unique value is also gone: the live-write policy arms this draft carried (refuse live while the unit runs, allow a named directory, allow the unit, allow ACCEL_SAMPLER_LIVE_OK, never refuse scratch) are in the merged #3075.

It is no longer a draft, so this is the verdict that asks for the close: close with its branch, and if any of the policy arms turn out to be missing from #3075's version, they are a small follow-up on their own rather than a second implementation of the knob.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

accel_sampler: a smoke run with DIR= (not ACCEL_SAMPLER_DIR=) silently writes the live summary, and nothing refuses it

1 participant