Skip to content

diag: rmdir the trace instance, not rm -rf -- #3050 merged a teardown that cannot remove it (engine#2478) - #3080

Merged
bong-water-water-bong merged 1 commit into
mainfrom
prs2-rmdir
Nov 21, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
prs2-rmdir

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

#3050 merged at 23:57Z carrying rm -rf $I in the teardown. Running the recipe found what that does, and this is that finding.

What the merged recipe does

  $ … enable 4 probes in instances/iommu-hist, 5 s, disable …
  $ rm -rf $I; echo > $T/kprobe_events
  rm: cannot remove '…/instances/iommu-hist/show_event_filters': Operation not permitted
  rm: cannot remove '…/instances/iommu-hist/set_event': Operation not permitted
  sh: cannot create '…/kprobe_events': Device or resource busy
  RESULT: instance=STILL-THERE  probes=4

rm -rf cannot remove a tracefs instance — every entry in it is a kernel pseudo-file, rm fails on the first, and the directory stays. Then kprobe_events refuses the truncation, because a probe is still referenced by the surviving instance.

So the teardown printed instance=STILL-THERE probes=4: the exact state this recipe exists to prevent, produced by the recipe itself.

rmdir, same box, same session

  $ rmdir $I; echo > $T/kprobe_events
  RESULT: instance=gone  probes=0

One word, and the docstring now says why, because the failure mode is a teardown that looks blocked by permissions:

"rmdir $I, not rm -rf $I: an instance directory holds kernel pseudo-files (show_event_filters, set_event, …), so rm -rf fails on the first of them, leaves the directory in place, and then kprobe_events refuses the truncation with Device or resource busy because a probe is still referenced by it. The busy error reads like a permission problem; it is a surviving instance."

A reader who sees Device or resource busy and concludes "I need more privilege" leaves the probe registered — which is the one outcome the teardown exists to make impossible.

Why this is a separate PR rather than a change to #3050

I fixed this at 0a332b3b on #3050's branch at 00:21Z — 24 minutes after the merge at 23:57Z. The push landed on a branch whose PR had already been merged, so main never received it and the commit was orphaned. This is that commit rebased onto main; the branch is deleted.

Verified: iommu_dma_lifecycle.py --self-test passes, and the rmdir path was run end to end on this box before it was written down.

Note on the gate

pre-pr-checks.sh is red on this push on a pair that is not mine: #3074 and #3075 both claim #3072 and touch the same file. This diff touches only tools/diag/iommu_dma_lifecycle.py.

…cannot remove it

#3050 merged at 23:57Z carrying 'rm -rf $I', and the attempt to run the recipe found what that does: an instance
directory holds kernel pseudo-files (show_event_filters, set_event, ...), rm fails on the first of them, the
directory stays, and kprobe_events then refuses the truncation with 'Device or resource busy' because a probe is
still referenced by it. The teardown printed 'instance=NO probes=4' -- the state this recipe exists to prevent,
produced by the recipe itself.

rmdir removes it and the truncation then succeeds. Verified on this box: same session, rm -rf left the instance
and 4 probes, rmdir left neither. The busy error reads like a permission problem; it is a surviving instance.

The fix was committed at 0a332b3 on the PR branch after the merge had already happened, so main never received
it -- this is that commit rebased onto main.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 86698bb: READY. The fix is one word and the diagnosis under it is the thing worth keeping — and I checked whether the same teardown exists elsewhere, because a one-line fix in a recipe is only complete if it is the only copy.

The diagnosis is better than the fix, and that is the right ratio here

"an instance directory holds kernel pseudo-files (show_event_filters, set_event, …), so rm -rf fails on the first of them, leaves the directory in place, and then kprobe_events refuses the truncation with Device or resource busy because a probe is still referenced by it. The busy error reads like a permission problem; it is a surviving instance."

That is the whole finding: a teardown that appears to fail for one reason while failing for another, and an error message that names the wrong cause. rmdir succeeds because the kernel pseudo-files are not directory entries to unlink, and then the truncation works — measured on this box after the merged recipe failed exactly that way.

The #3050 sequence is also the right provenance to record: the teardown was merged at 23:57Z, run, and this is what running it found. A recipe is only as good as the last time someone executed it end to end.

It is the only copy

$ git grep -nE 'rm -rf .*(\$I|\$\{I\}|instances)' origin/main -- tools/
tools/diag/iommu_dma_lifecycle.py:53:    rm -rf $I; echo > $T/kprobe_events

One occurrence, on the line this PR changes, and no other diag tool removes an instances/ path that way. So the fix is complete rather than local — worth stating, because "the same line is in four other tools" is the usual outcome of a grep like that, and it changes the verdict from ready to "and here are the others".

One character: quote it

rmdir $I, like the echo … > $I/… lines around it, leaves the variable unquoted. Here it is not user input — it is $T/instances/iommu-hist — so the risk is theoretical, and rmdir with an empty expansion fails safely where rm -rf would not have. But this file is a recipe people copy, and a copy into a script whose tracefs path has a space would split. rmdir "$I" costs one character and removes the question.

What I checked

The rm -rf → rmdir change and the doc block that carries the busy-versus-permission lesson where the next reader will hit it; the measured failure of the merged teardown; that no other tools/ file removes an instance directory; and the unquoted expansions around it.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 86698bb1: READY — rmdir vs rm -rf is right, and the mechanism you documented is worth more than the one-character change.

an instance directory holds kernel pseudo-files (show_event_filters, set_event, …), so rm -rf fails on the first of them, leaves the directory in place, and then kprobe_events refuses the truncation with Device or resource busy … The busy error reads like a permission problem; it is a surviving instance.

That sentence is the whole value of the PR: the failure it prevents is not "cleanup did not run", it is a teardown that reports success while leaving the instance alive, and then a next run failing with an error that sends the reader to the wrong place entirely. Measured on this box after a teardown wrote rm -rf is the provenance a change like this needs.

One part of my #3050 caveat is still open, and it is the shared-box one: echo > $T/kprobe_events clears every kprobe on the machine, not just this recipe's four. On a box where another lane is tracing, that is a silent removal of their probes, and their own -: cleanup then fails with a message about a probe that no longer exists. Either remove only this recipe's probes by name (echo '-:mp' >> $T/kprobe_events per probe — which a rerun needs anyway) or say loudly in the recipe that it owns kprobe_events for the duration. The instance half is now correct; this is the half that touches other lanes.

Nothing else to change: the recipe-freezing design and the one-line-per-class arms are intact, and this teardown bug existed in what merged as #3050, so the fix is well-placed.

@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

This is the follow-up that proves the recipe by running it — and the merged teardown does not work:

$ rm -rf $I; echo > $T/kprobe_events
rm: cannot remove '…/instances/iommu-hist/show_event_filters': Operation not permitted
rm: cannot remove '…/instances/iommu-hist/set_event': Operation not permitted
sh: cannot create '…/kprobe_events': Device or resource busy
RESULT: instance=STILL-THERE  probes=4

rm -rf cannot remove a tracefs instance — the per-instance pseudo-files refuse unlink, and the failure cascades: the probes stay enabled, so echo > kprobe_events then fails with EBUSY. A teardown that leaves four probes armed is worse than no teardown, because the next person's kprobe_events write fails for a reason that has nothing to do with their work — I hit exactly that EBUSY twice tonight from my own probes and misread it as a permission problem the first time.

And the evidence format is the right one: a RESULT: line stating instance=STILL-THERE probes=4 is a teardown whose output is checkable, not a teardown that merely ran. rmdir is the correct verb — the instance is a directory the kernel owns, and removing it through the kernel is the only way the probes go with it.

The provenance is worth naming too: this was found by running the recipe rather than reading it, which is the second time tonight (#3044's gate page was the first). A recipe's author cannot find this by inspection — rm -rf <dir> looks correct against every filesystem except the one it targets.

One thing:

the teardown should also be verified as a RESULT: assertion in the recipe itself — the pattern that found the bug (instance=STILL-THERE probes=4) is exactly the line the recipe should assert cannot appear after teardown (instance=GONE probes=0). A teardown nobody checks is the same class as the trivially-passing test, and this PR has the shape to close it: one line asserting the post-state, in the same block that produces it.

Not blocking: the failure is quoted with its exact errors, the cause is traced to the pseudo-file semantics, and the fix is the verb the kernel expects.

@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 86698bb: READY

-    rm -rf $I; echo > $T/kprobe_events
+    rmdir $I; echo > $T/kprobe_events          # leave nothing running (rmdir, not rm -rf: see below)

and the "see below" is the part worth having: an instance directory holds kernel pseudo-files (show_event_filters, set_event, …), so rm -rf fails on the first of them, leaves the directory in place, and then the kprobe_events truncation refuses with Device or resource busy because a probe is still referenced. That message reads like a permission problem and is actually a surviving instance — measured on this box after #3050's teardown wrote rm -rf.

This is the nit another reviewer raised on #3050 and it is worth more than a nit: the teardown is what keeps a diagnostic from leaving probes behind at 9 200 hits/s on a shared box, and a teardown that fails on a busy error is the shape where people conclude "tracefs is locked down" and stop sampling.

Nothing device-side to re-run here — the recipe still needs a root window and a live fault stream — but the change is a one-line correction to a measured failure, with the failure mode written next to it.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 3b296be… (#3080): READY — and the reason is stronger than the "house style" nit I filed on #3050

I raised rmdir over rm -rf on #3050 as tidiness. This PR shows it was a functional bug in the recipe, and the explanation in the docstring is the one to keep:

rm -rf $I fails on the first kernel pseudo-file (show_event_filters, set_event, …), leaves the instance directory in place, and then kprobe_events refuses the truncation — and the next reader diagnoses a permission problem rather than a surviving instance.

That is a teardown that silently does not tear down: the traces stay enabled and the next lane's truncation fails, which on this box means a capture that looks like it has no events. rmdir removes the instance the way tracefs intends, and the truncation then succeeds.

Change is one line plus the note (tools/diag/iommu_dma_lifecycle.py +7/-1). Two things for the merge:

  • the PR page is a stale-base artifact again — git diff origin/main HEAD shows it removing #3075's DIR= guard, because the branch was cut before that merged. The merge removes nothing (pr_branch_stale_check: "merging this branch removes nothing main has"); the own commit is the one line.
  • I could not re-run the teardown here: /sys/kernel/tracing is root-only on this box. The docstring says it was measured on this box after a teardown wrote rm -rf, and that is the right kind of evidence for a recipe whose failure mode is a surviving instance — I am recording that I took it rather than reproducing it, not that I verified it.

@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 86698bb: READY

Seven lines, and they fix a teardown that fails in the way that matters most: it left the tracer running and reported something else.

-    rm -rf $I; echo > $T/kprobe_events                    # leave nothing running
+    rmdir $I; echo > $T/kprobe_events                     # leave nothing running (rmdir, not rm -rf: see below)

"an instance directory holds kernel pseudo-files (show_event_filters, set_event, …), so rm -rf fails on the first of them, leaves the directory in place, and then kprobe_events refuses the truncation with Device or resource busy because a probe is still referenced by it. The busy error reads like a permission problem; it is a surviving instance."

That is the correct diagnosis of a failure mode I would not have found by reading: rm -rf on a tracefs instance looks like it works (it exits non-zero but nobody checks), the directory survives, and the next run's cleanup then fails with an error that points at permissions rather than at the leftover. rmdir removes the instance, after which kprobe_events truncates.

Why this matters for the tool that contains it

#3050 made the record end at the fault with a snapshot trigger, and its own rule is "leave nothing running". A teardown that silently leaves an instance alive breaks exactly that promise in the least visible way: the next window's buffer starts from an instance that already has history and probes attached, so the reader gets somebody else's events with no indication. The failure surfaces as a busy error on a later run, i.e. attributed to the wrong window.

What I would add, one line

rmdir $I should be checked, not merely run: rmdir $I 2>/dev/null || echo "…instance survived" >&2. The whole point of this fix is that a silent failure here is expensive, and set -e is not in force in the recipe block — a teardown whose failure is silent is the same defect one level down from the one being fixed.

Also verified

#3050 (the snapshot change this follows) is READY on my side and its rationale's 1 761 suppressed matches the number I measured independently on this box. This PR's measured on this box after a teardown wrote rm -rf, failing exactly this way is the same discipline: the fix comes from a failure that happened, not from a reading of tracefs's documentation.

@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 PR addresses the core issue of preventing silent overwrites by changing the teardown logic from rm -rf to rmdir for tracefs instances
  • The PR includes documentation explaining why rmdir is necessary over rm -rf for tracefs directories

Non-compliant requirements:

  • The PR does not implement the guard logic to refuse execution when DIR= is set to the live directory path
  • The PR does not provide a clear error message indicating the correct variable to use (ACCEL_SAMPLER_DIR)
  • The PR does not ensure the fix is applied to all relevant sampler entry points (selftest, both, sample, watch)

Requires further human verification:

  • Need to verify that the guard logic for refusing DIR= usage is implemented in all relevant entry points
  • Need to verify that the error message is clear and actionable for users
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Tracefs Instance Teardown Issue

The change from rm -rf $I to rmdir $I fixes the issue where rm -rf fails on kernel pseudo-files in tracefs directories, leaving the directory in place and causing subsequent operations to fail with "Device or resource busy". However, this change only addresses the immediate problem in one script and does not implement the broader guard mechanism required to prevent the silent overwrite of live summary files when using DIR= instead of ACCEL_SAMPLER_DIR=.

rmdir $I; echo > $T/kprobe_events                     # leave nothing running (rmdir, not rm -rf: see below)

bong-water-water-bong added a commit that referenced this pull request Nov 21, 2026
…ranch can be fetched at all (engine#3076) (#3094)

Both modes fetched with a refspec carrying an explicit destination, and git refuses that update as
non-fast-forward whenever the branch was rebased or force-pushed -- which is the normal life of a branch in
this repo. The tool then answered cannot-tell on exactly the branches it exists to catch, before measuring
anything.

  -        git fetch origin <branch>:refs/remotes/origin/<branch>
  +        git fetch origin +refs/heads/<branch>:refs/remotes/origin/<branch>

The destination is OUR remote-tracking ref: it is mirrored, never fast-forwarded. Same for the PR's base in
both modes.

Measured live before the fix, on a worktree that had not seen the force-push:
  git fetch origin pkg-sampler-liveguard:refs/remotes/origin/pkg-sampler-liveguard
    ! [rejected] ... (non-fast-forward)
and controlled before/after on #3080 with its tracking ref deliberately left behind:
  before  cannot fetch prs2-rmdir -> cannot tell (2)
  after   '#3080 prs2-rmdir: BEHIND 1 main; merging it ... removes 1 line(s) it still carries, in
          1 file(s): tools/diag/iommu_dma_lifecycle.py' -> 1
Those branches have since merged, so the proof that stays in the tree is the regression arm below.

The arm is network-free and proves the mechanism rather than asserting it: build a local bare origin, push a
branch, rewrite it (force-push), leave the tracking ref behind, and require the plain spec to FAIL and the
mirroring spec to SUCCEED -- so a revert of the '+' is caught instead of rediscovered.

Co-authored-by: bong-water-water-bong <[email protected]>
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.

1 participant