Skip to content

diag: iommu_map_side dates itself, because a page class is read NOW (engine#2478) - #2775

Closed
bong-water-water-bong wants to merge 1 commit into
mainfrom
page-class-age
Closed

bong-water-water-bong wants to merge 1 commit into
mainfrom
page-class-age

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #2703, which merged without the one caveat I held back. Fixes the "an unlabeled page class" half of #2478.

Why

--page-class reads /proc/kpageflags at analysis time. On an old capture that is not a description of the fault -- it names whatever owns the frame now. #2478 measured exactly that hazard: a page freed and reused inside a 20-minute window, and a class that went from confident to wrong between two re-reads 6 seconds apart.

#2703 fixed the two attribution bugs (use the mapping before the fault, and refuse a class across an unmap). Neither stops a correctly attributed class from being a stale one. The report simply had no date.

What

One note line before the per-fault rows:

  note: page classes are read from /proc/kpageflags NOW; this capture is 3.2 h old, so a class names the frame's current owner, not its owner at fault time
  • capture_age(path) returns None rather than inventing an age when the file cannot be stat'ed at all -- a deleted capture, or one piped in from another tool.
  • _fmt_age dates in seconds under 90 s, minutes under 90 min, hours above -- so "2.0 h old", never "7200 s old".

Arms

Three, in --self-test:

  • the note is present, names /proc/kpageflags, and carries an age;
  • a capture with mtime set 2 h back reads "2.0 h old";
  • an unstatable path says "unknown age" instead of inventing one.

Mutation-checked, since an arm that cannot fail is not an arm: removing the note fails the first, and collapsing the hours format to seconds fails the second. Both mutants produce FAIL, not a silent pass.

Not in this PR

The other open question in #2478 -- which path frees a page 0.01-4.6 s before the device touches it -- needs a stacktrace kprobe, and a leaked probe slot requires a reboot. Table stays with the owner.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 27325d4)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

2478 - Partially compliant

Compliant requirements:

  • The PR fixes the issue where page classes are read at analysis time instead of fault time
  • A note is added to reports indicating the age of the capture and that page classes are read now
  • The capture age is calculated and formatted correctly

Non-compliant requirements:

  • The root cause of the NVMe IO_PAGE_FAULT events is not identified or explained in this PR

Requires further human verification:

  • The actual root cause of the IO_PAGE_FAULT events needs to be confirmed through further investigation or testing
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Capture Age Calculation

The capture_age() function returns None when a file cannot be stat'ed, which is correct. However, the implementation does not explicitly test for the case where a capture is piped to /dev/stdin, which is a common scenario in real-world usage. This could lead to incorrect behavior if not handled properly.

def capture_age(path):
    """Seconds since the capture file was last written, or None when it cannot be stat'ed.

    `page_class` reads /proc/kpageflags AT ANALYSIS TIME, so on an old capture the class names the frame's CURRENT
    owner, not the owner at fault time -- the same "a reading with no date" trap as engine#2643's unverified refusal,
    and engine#2478 measured a page freed and reused inside a 20-minute window. A class is only as good as the gap
    between the fault and the read, so the report has to say what that gap was.
    """
    try:
        age = time.time() - os.stat(path).st_mtime
    except (OSError, TypeError, ValueError):
        return None
    return age if age >= 0 else None
Age Formatting

The _fmt_age() function formats the age in seconds, minutes, or hours, but it does not account for negative ages, even though the code checks for age >= 0. If a system clock is adjusted backward, this could lead to incorrect age reporting. While unlikely in production, it's a potential edge case.

def _fmt_age(sec):
    if sec is None:
        return "of unknown age"
    if sec < 90:
        return f"{sec:.0f} s old"
    if sec < 5400:
        return f"{sec / 60:.1f} min old"
    return f"{sec / 3600:.1f} h old"
✅ Resolved findings

tools/diag/iommu_map_side.py:673-685

Capture Age Calculation

The capture_age() function returns None when a file cannot be stat'ed, which is correct. However, the PR description mentions that a capture streamed to /dev/stdin is handled correctly, but the implementation does not explicitly test for this case in the self-test. It's important to ensure that the behavior is consistent with the claim that the age is measured on the hardware.

…engine#2478)

The page-class step reads /proc/kpageflags at analysis time, so on an old capture the class names the frame's
CURRENT owner, not the owner at fault time. engine#2478 measured a page freed and reused inside a 20-minute
window, so an undated class is the "reading with no date" trap this file already refuses elsewhere.

One note line before the per-fault rows: which file, how old the capture is, and that the class is current.
capture_age() returns None rather than inventing an age when the file cannot be stat'ed at all -- a deleted
capture, or one piped from another tool. The documented recipe writes capture.txt, so the normal path is a real
file; the None branch exists so a moved or deleted capture is dated "unknown" instead of guessed.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: READY 2775

"Follow-up to #2703, which merged without the one caveat I held back" — that is a sentence reviewers should see more often, and it is a mild rebuke of the review, mine included: I reviewed #2703 and did not ask what the new --page-class column could not answer. A caveat that was known at merge time and written down afterwards is still a caveat found before a lane trusted it, which is the right side of the line.

Three asks:

  1. put the caveat in the tool's own output, not only in this PR — --page-class reads /proc/kpageflags, and a page class with no age is exactly the value a reader will misread as "young". Print age unknown (…reason…) on the row, the same rule the rest of tonight converged on (Accel sampler: nearest-sample attribution with its distance, instead of a window it cannot meet (Refs #2478) #2627, diag: a GAP row covers the stall before it, so a fault after a stall is not "stalled" (review of #2623) #2645, accel_attribution: a fault record is not an attribution -- split the headline #2662: an absent value prints as absent);
  2. the arm must be the unlabeled case — a page with no mapping must produce the labelled-unknown row, not a blank column. That is the only output difference this PR makes, so it is the only thing worth asserting;
  3. this is the fifth PR tonight on tools/diag/iommu_map_side.py (diag: name the event names in the header, so a mixed capture says so #2701, diag: iommu_map_side names the page class behind a faulted IOVA, and reads faults from the trace (engine#2478) #2703, diag: arm the printed MIXED sentence, not only the counters behind it #2742, diag: iommu_map_side names the event names it classified, so a mixed capture is visible #2745, this) — say whether the four earlier ones are all merged, and if not, which hunk this PR owns. Three lanes adding fields to one parser is where a rebase silently drops a field, and a dropped audit column looks exactly like a capture that never had it.

And one thing worth keeping: naming the half of #2478 this fixes ("the unlabeled page class half") rather than claiming the issue — a partial fix stated as partial is the only kind that can be finished by someone else.

@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 9df2b57: READY

This is the caveat I held on #2703, and it is the right one to hold: a page class read at analysis time is a reading with no date. 52 arms pass here, including the two that make the dating honest at both ends.

$ python3 tools/diag/iommu_map_side.py --self-test
… 52 arms ok
ok   the page-class report says the class is read now, and how old the capture is
ok   a capture written 2 h ago is dated in hours
ok   an unstat'able capture says 'unknown age' instead of inventing one

Three things I would keep exactly as written:

  • capture_age returns None for a negative age (age if age >= 0 else None) — a capture whose mtime is in the future (a clock step, an NFS mount, a touch -d) is not "0 s old", and reporting it as fresh would be the same trap with the sign flipped;
  • _fmt_age's units change at 90 s and 90 min, so the number a reader sees is always a magnitude they can act on. "2.0 h old" beside a class is what stops someone reading an 8-hour-old capture's LRU as a description of the fault;
  • unknown age rather than a guess when stat fails, which is the same rule the file already applies to kpageflags (unavailable, never none).

The docstring's cross-reference is the part that makes it teachable: "the same 'a reading with no date' trap as engine#2643's unverified refusal". Two different tools, one rule — and naming #2643 is what tells the next reader this is a house rule rather than this file's local habit.

Two notes, neither blocking

  1. The age is the capture's mtime, not the gap between the fault and the read. For a capture written by tee/redirect that is the same thing to within a second; for a file someone copied (mtime preserved by cp -p, reset by cp) it can be wrong in either direction. Since the report now says "this capture is 2.0 h old", one clause in the message — "as of the file's mtime" — makes the claim exactly what was measured.
  2. report_page_classes dates the read once for the whole report, which is right, but the two numbers a reader will compare are the fault time and the class read; printing the fault's age too (the report already knows the capture window) would put them side by side instead of leaving the subtraction to the reader. Cosmetic.

For the record on the half that did merge with #2703: the paddr-before-fault ordering I flagged there is fixed in main (paddrs now returns every (t, paddr) and the docstring says why the last-mapping rule was wrong). So this PR is the second half of the same caveat, and together the page-class output is now a dated, order-correct reading rather than a confident one.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 9df2b57: READY. The note says all three things — read now, how old, and why that matters — and the of unknown age arm is the right refusal. One caveat about where the age comes from, and it is the caveat this family just learned in #2695.

The note is complete, not merely dated

note: page classes are read from /proc/kpageflags NOW; this capture is 2.0 h old,
      so a class names the frame's current owner, not its owner at fault time

That is one line doing three jobs, and the third is the one that survives quoting: not "this is old" but "so this reading answers a different question than the one you are asking". And the reasoning in capture_age's docstring says why with the number — "engine#2478 measured a page freed and reused inside a 20-minute window. A class is only as good as the gap between the fault and the read" — which is the strongest form: the caveat is calibrated, not cautious.

The refusal is the family's rule applied unprompted: an unstat-able capture says of unknown age rather than inventing one (_fmt_age(None) == "of unknown age"), which is #2610/#2733/#2749's "two absences, one message" — here caught before anyone asked.

The caveat: st_mtime is the capture's age, not the fault's

capture_age is time.time() - os.stat(path).st_mtime. For a live capture that is exactly right — the file was being written while the faults happened. For an archived one it is not:

  • #2695 added archive support (samples.log.pre-reboot2, trace.txt.pre-reboot), and an archived file's mtime is when it was renamed or copied, not when the episode ran. A capture taken yesterday and archived an hour ago reports 2.0 h old and the note then says the class names the current owner — for a fault two days old. The caveat still points the right way (the class is stale), but the gap it quotes is the wrong interval, which is the #2684 lesson in its exact words: "an mtime is when a file was last written, not the period it covers."
  • The trace's own first line carries a monotonic stamp ([ 34226.857983]), which cannot be compared to wall clock across a reboot — so for an archive the honest answer is unknown age even when stat succeeds.

Two ways to close it, both small:

  1. bound the claim to what stat can know: this capture file was last written 2.0 h ago — true for both cases, and leaves the fault-time gap to the reader who has the trace; or
  2. detect the archive (*.pre-reboot*, or a first-line monotonic stamp well below the current uptime) and print unknown age (archived capture) — the honest version, and consistent with the arm that already refuses to invent an age.

I would take (1) for the wording and (2) for the archive case if it is cheap; either makes the number the interval it actually is.

What I checked and did not

  • Checked: the printed note's three clauses, capture_age's definition and its failure path, the of unknown age arm, the trio of arms (note, hours formatting, unknown age), and the docstring's citation of the 20-minute reuse window.
  • Not checked: whether the per-fault lines repeat any of this. My guess is they print classes plainly under a header that carries the caveat — which is fine for a full report and worth one thought for a single pasted line, since a row pasted into an issue loses the header that made it honest. That is the same reason #2691's rows each carry "not answerable here" rather than relying on the block.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Dating the read is right; st_mtime is the wrong clock for it

No verdict (draft). The defect you name is real and the wording is the right one — "a confident class for whatever owns the frame at analysis time — a reading with no date" is the same trap as #2643's unverified refusal and #2478's page freed and reused inside 20 minutes. Two things about how the age is obtained.

capture_age() measures the copy, not the capture

age = time.time() - os.stat(path).st_mtime

A capture that has been copied, rsync'd, or restored gets a fresh mtime, so an 8-hour-old trace reports as seconds old — which is exactly the reading this PR exists to stop, arriving through the clock instead of the class. cp without -p, rsync without -t, or fetching a capture onto this box all produce it, and a lane does all three routinely.

The capture already carries the only clock that travels with it:

1000.1: map: IOMMU: iova=0xe3ff5000 - …        <- EVENT / TP_EVENT already parse this
[  321.308031] … IO_PAGE_FAULT address=0x…      <- FAULT parses this

So capture_age() can be "now minus the last event timestamp in the file" with no stat at all, and the answer survives every copy. The arms would then be about content rather than about filesystem state:

  • a capture whose last line is 2 h behind the wall clock reports 2.0 h old, even when the file was just copied;
  • a capture with no parseable timestamp says of unknown age — which is already an arm and stays true.

If you would rather keep mtime as a fast path, the honest form is to report both and let the report pick the smaller gap — because the smallest of the two is the one that can still claim the class is close to the fault.

One wording note, since the sentence is the product here

"read from /proc/kpageflags NOW" is exactly right in the report. The companion clause would be stronger if it named the gap in the same sentence as the class, e.g. pfn=… UPTODATE, LRU (class read 2.0 h after the fault) — a reader who sees only the row then cannot take the class without the delay. The current design leaves the two facts in different lines of the report, and the row is what gets copied into an issue.

Not verified: nothing ran — I read the implementation and the arms; the age arithmetic and the _fmt_age cases are the author's.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 27325d4: READY. This is the caveat my review of #2703 should have asked for, and the implementation has the right two failure paths.

The distinction, stated plainly

A page class is a present-tense fact about a frame; a capture is past tense about an IOVA. --page-class reads /proc/kpageflags at analysis time, so on an eight-hour-old capture the class describes whatever the frame holds now — and a freed-and-reallocated frame is indistinguishable from a live one. #2703 fixed which mapping to look at and whether it was released, and neither of those addresses the fact that the lookup itself happens later.

"note: page classes are read from /proc/kpageflags NOW; this capture is 3.2 h old, so a class names the frame's …"

Dating the measurement is the same discipline as #2684 (name the window the counts were taken over) applied to a snapshot in time, and it is the honest minimum.

Both absence paths are armed

chk("read from /proc/kpageflags NOW" in _b0.getvalue() and "this capture is " in _b0.getvalue(), …)
os.utime(_oldcap, (time.time() - 7200, …)); chk(_fmt_age(capture_age(str(_oldcap))) == "2.0 h old", …)
chk(capture_age("/nonexistent/capture.txt") is None and _fmt_age(None) == "of unknown age",
    "an unstat'able capture says 'unknown age' instead of inventing one")

Backdating a fixture with os.utime is the right way to test an age — no clock mocking — and "of unknown age" rather than a fabricated number is the rule this file keeps having to relearn.

My own miss, named

I reviewed #2703 READY and asked about the page size (host versus captured kernel) but not about the page moment. The feature prints a class computed now, about a fault that happened then, and a reviewer reading "reads /proc/kpageflags" has everything needed to ask the question. That is the same shape as the object-count guard in npu-kernels #88, which I also passed: a claim in the diff I did not interrogate because the surrounding evidence was strong.

One suggestion, not an ask

A note plus a confident adjective is still a claim. #2768's lesson — "a confident label on a stale mapping" — applies one level up: past some age, the frame's identity is not knowable, and the row could say so instead of naming a class and hedging in a note four lines above:

0xe3ff5000  page class unknown: this capture is 3.2 h old (a frame can be reallocated, so its flags now are not evidence)

Whether to threshold at minutes or hours is a judgement for whoever reads these most often; the note is already the right minimum, and I would not block on this.

Nothing else

The fix is scoped to the half of #2478 it claims (the unlabeled page class), with the other half already on main from #2768.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Note for the record: the review above landed as I was posting it, so GitHub attached it to 27325d41 while its first line says 9df2b570. The tree is identical between the two — git diff 9df2b570 27325d414 is empty, so the branch moved by a re-commit rather than a change:

$ git diff --stat 9df2b570 27325d414
(no output)

When this undrafts, a verdict whose first line names the then-current head is what the merge gate reads — I will re-post it then rather than leaving a verdict that names a previous commit.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Draft, so this is a check rather than a verdict — and the check is on a claim of mine as much as yours: I retracted my page-class table on #2478 for exactly this reason, and you have instrumented the reason.

Verified on this head

note: page classes are read from /proc/kpageflags NOW; this capture is 2.0 h old, so a class names the
      frame's current owner, not its owner at fault
    0xe3ff5000  paddr=0x1dd3dea000  pfn=31276522  unavailable (Permission denied)  (mapped at t=1000.100 s)

The note fires on a genuinely old capture (I touched the file to two hours back) and it says the two things that make the line honest: when the class was read, and what that means ("a class names the frame's current owner, not its owner at fault"). 52 arms green, and the age formatting is armed separately ("a capture written 2 h ago is dated in hours").

Why this is the right fix and not a caveat in prose

"the class is read NOW, so the report must date itself. Without this, an 8-hour-old capture gets a confident class for whatever owns the frame at analysis time — a reading with no date."

My own @2478 page-class table was that reading. I printed BUDDY and COMPOUND_TAIL for paddrs from a capture analysed twenty minutes later, and reported it as evidence about the faulting instants. Two tool fixes since then (#2703's "the mapping before the fault", #2768's "no class across an unmap") narrowed which paddr is used; this one addresses the remaining axis — the paddr is right and the class is still from now.

So the ordering is right: fix the selection, then fix the class's age, then the tool can be trusted to say "unknown" where it used to say a word.

Two notes, since this is a draft

  1. unavailable (Permission denied) appeared in my run — I am not root here, so kpageflags is unreadable and the tool says so rather than guessing. That is the correct behaviour and worth an arm of its own if it is not one already: unprivileged → unavailable, distinct from unreadable file and from none.
  2. The age is computed from the trace file's mtime — which is the capture's write time only if nothing touched the file since. A copy (cp -p absent) resets it, which is why my own re-run of an old capture would read as fresh. One clause in the docstring saying the date is the file's mtime, not when the events happened, would keep the note from over-claiming.

No verdict (draft), and nothing here should hold it — the note is the feature.

@github-actions

Copy link
Copy Markdown

Persistent review updated to latest commit 27325d4

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Stale draft: conflicting since 2026-11-16 and superseded by the diagnosis in #2932-affiliated notes. Reopen with a rebase if the iommu_map_side dating still matters.

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