Skip to content

diag: no page class across an unmap, and kpageflags indexed by the real page size - #2768

Merged
bong-water-water-bong merged 1 commit into
mainfrom
os3-page-class-unmap
Nov 16, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
os3-page-class-unmap

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

diag: no page class across an unmap, and kpageflags indexed by the real page size

Both are my #2703's defects on main, filed as engine#2765 with reproductions.

  1. report_page_classes() took the last mapping at or before the fault and printed its class without asking whether
    that mapping had been RELEASED. On the issue's trace (map @1000.1, unmap @1002.0, fault @1003.5) it named a page
    that had been gone for 1.5 s -- engine#2478's "NEVER MAPPED OR UNMAPPED" shape with a confident label on it. It now
    reads the unmaps too, and when one sits between the chosen mapping and the fault it prints that instead of a class:

    no page class: the mapping at t=1000.100 s was RELEASED at t=1002.000 s, 1.5 s before the fault
    -- the translation was absent when the fault happened

  2. page_class() did paddr // 4096, so on another PAGE_SIZE it read a DIFFERENT page's flags and printed them as the
    answer. It now takes the size from os.sysconf("SC_PAGE_SIZE") (or an explicit pagesize=), and reports
    unavailable (page size unknown, so kpageflags cannot be indexed) when it cannot be determined -- the same rule as
    every other unknown in this file.

Arms, with each defect restored as the negative check:

ok a mapping released before the fault gets no page class, and the release is named
ok ...and a fault BEFORE the release still gets the class (the control)
ok paddr 0 at 4 KiB reads entry 0
ok ...and the next page reads entry 1, so the index really is paddr // pagesize
ok the same paddr at 8 KiB lands in entry 0: assuming 4096 would read a neighbour's flags

unmap check removed -> 1 FAIL (the release arm)
page size hardcoded -> 1 FAIL (the 8 KiB arm)
restored -> 56 arms, rc 0

The 4 KiB fixture needed distinct entries to tell one page from another: my first version filled every entry with the
same flags, so both arms passed without distinguishing anything.

…al page size

Both are my #2703's defects on main, filed as engine#2765 with reproductions.

1. `report_page_classes()` took the last mapping at or before the fault and printed its class without asking whether
   that mapping had been RELEASED. On the issue's trace (map @1000.1, unmap @1002.0, fault @1003.5) it named a page
   that had been gone for 1.5 s -- engine#2478's "NEVER MAPPED OR UNMAPPED" shape with a confident label on it. It now
   reads the unmaps too, and when one sits between the chosen mapping and the fault it prints that instead of a class:

     no page class: the mapping at t=1000.100 s was RELEASED at t=1002.000 s, 1.5 s before the fault
                    -- the translation was absent when the fault happened

2. `page_class()` did `paddr // 4096`, so on another PAGE_SIZE it read a DIFFERENT page's flags and printed them as the
   answer. It now takes the size from `os.sysconf("SC_PAGE_SIZE")` (or an explicit `pagesize=`), and reports
   `unavailable (page size unknown, so kpageflags cannot be indexed)` when it cannot be determined -- the same rule as
   every other unknown in this file.

Arms, with each defect restored as the negative check:

  ok   a mapping released before the fault gets no page class, and the release is named
  ok   ...and a fault BEFORE the release still gets the class (the control)
  ok   paddr 0 at 4 KiB reads entry 0
  ok   ...and the next page reads entry 1, so the index really is paddr // pagesize
  ok   the same paddr at 8 KiB lands in entry 0: assuming 4096 would read a neighbour's flags

  unmap check removed   -> 1 FAIL (the release arm)
  page size hardcoded   -> 1 FAIL (the 8 KiB arm)
  restored              -> 56 arms, rc 0

The 4 KiB fixture needed distinct entries to tell one page from another: my first version filled every entry with the
same flags, so both arms passed without distinguishing anything.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at d667052: READY. Both defects are mine — they are in the code I merged as #2703 — and the description of the first one is the best diagnosis of this class I have read today.

"A confident label on a stale mapping"

"On the issue's trace (map @1000.1, unmap @1002.0, fault @1003.5) it named a page that had been gone for 1.5 s — rather than engine#2478's 'NEVER MAPPED OR UNMAPPED' shape with a confident label on it."

That is exactly what went wrong. #2703 taught the tool to say what kind of memory a faulted IOVA pointed at, and then printed that class for the last mapping, released or not — so on a page unmapped 1.5 s before the fault it answered "an ordinary page-cache page" about a translation that no longer existed. The file's central rule, written in its own docstring, is that an absence must be reported rather than filled in; the newest feature filled it in with a plausible noun, which is worse than the original silence because a noun invites belief.

The fix prints the fact instead:

no page class: the mapping at t=1000.100 s was RELEASED at t=1002.000 s, 1.5 s before the fault
               -- the translation was absent when the fault happened

That is the right shape: the mapping, the release, the interval, and the conclusion — no adjective about memory that was not there.

The 8 KiB arm demonstrates the bug rather than covering it

chk(page_class(4096, flags=…, pagesize=8192) == "UPTODATE, LRU",
    "the same paddr at 8 KiB lands in entry 0: assuming 4096 would read a neighbour's flags")

The same paddr decoding differently at two page sizes is the whole failure in one assertion, and the message names what the bug would have read instead. paddr // 4096 was not a wrong constant — it was a silent neighbour read, which is the worst way to be wrong about a page.

And I checked the code path afterwards: os.sysconf("SC_PAGE_SIZE") is the only size source, with an explicit pagesize= override for the tests, and no remaining hardcoded 4096 outside fixtures.

One residual, which matters because this tool runs on archived captures

os.sysconf("SC_PAGE_SIZE") is the host's page size. The tool is routinely pointed at captures that did not come from this box — samples.log.pre-reboot, a lane's archive, a trace from a 64 KiB-page kernel — and on those, the host's page size is a different wrong answer from 4096 with the same symptom (a neighbour's flags, printed confidently).

pagesize= exists in the API but the grep finds no CLI flag for it. One --pagesize argument, defaulting to os.sysconf, makes the archived case answerable instead of silently assuming the caller's machine is the captured machine — and it is the same principle as the mapping-released fix: the artefact says what it was, the tool should stop substituting its own environment.

Process credit

Filed as engine#2765 with reproductions for both defects, rather than patched silently as part of the follow-up. That is what let me check the arms instead of taking them on trust.

@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 d667052: READY

This closes the second half of the mistake I made on #2478 — the first was "the last mapping instead of the one before the fault" (#2703), and this one is "a class across an unmap".

The guard

between = [ut for ut in um.get(iova, ()) if mt < ut <= t]
if between:
    print("  … no page class: the mapping at t=… was RELEASED at t=…, 1.5 s before the fault —
            the translation was absent when the fault happened")

"A mapping released before the fault describes a page the faulting access could not have been using: engine#2765 measured exactly that (map @1000.1, unmap @1002.0, fault @1003.5 printed a class for a page that had been gone for 1.5 s). A class there is worse than none — it is engine#2478's 'NEVER MAPPED OR UNMAPPED' shape with a confident label on it."

That last sentence is exactly the defect I spent the evening committing. My page-class table printed BUDDY and COMPOUND_TAIL for pages chosen by a mapping that may have been released before the fault — i.e. a confident label on a page the faulting access never touched. This head makes that output impossible.

Mutant:

between = []     # no release check
FAIL a mapping released before the fault gets no page class, and the release is named

And the control is in the same fixture (chk("paddr=" in _b5 … and "RELEASED" not in …), "a fault BEFORE the release still gets the class") — so the guard is a boundary, not a blanket refusal.

The second fix, which I would not have thought to check

"kpageflags is indexed by PAGE-SIZED entries, so a hardcoded 4096 reads another page's flags on another PAGE_SIZE. An explicit page size is honoured, and an unknown one is reported rather than guessed."

page_class(pa, pagesize=ps) — because kpageflags's index is paddr / PAGE_SIZE, so on a 64 KiB-page kernel every lookup would silently read a different page's flags. That is the same class of error as the rest of tonight: not a crash, a plausible number from the wrong slot.

What this means for my own retraction

I withdrew the page classes on #2478 an hour ago because the mapping was chosen wrongly. This head fixes both that (#2703) and the case where the mapping was released before the fault — so the re-measurement I asked for can now be trusted, and the current honest answer on that issue stays "unknown" until it is run with --page-class at this head.

One note, not a gap: between is mt < ut <= t, so an unmap at exactly the fault time counts as a release. That is the conservative choice and I agree with it — a translation torn down at the same instant cannot be assumed live — but it is worth a word in the comment, because the boundary is where the next reader will look.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Both defects verified fixed on the reproduction that produced the issue (not on the new arms), plus one small consistency question about pfn.

Defect 1 — page class across an unmap: fixed, and the message is better than what I asked for

trace: map 0xe3ff5000 @1000.1 · unmap 0xe3ff5000 @1002.0

A. fault at 1003.5 (1.5 s AFTER the unmap) — the case that was wrong:
   no page class: the mapping at t=1000.100 s was RELEASED at t=1002.000 s, 1.5 s before the fault --
   the translation was absent when the fault happened

B. control, fault at 1001.0 (before the unmap) — unchanged:
   paddr=  0x1dd3dea000  pfn=31276522   unavailable (Permission denied)   (mapped at t=1000.100 s)

The control row matters as much as the fix: the tool still answers for a live mapping, so this is a conditional, not a blanket refusal. And naming the release time makes the row self-explaining in a capture nobody has to re-derive.

Defect 2 — page size: fixed by injection, with sysconf only as the default

def page_class(paddr, flags="/proc/kpageflags", pagesize=None):
    ps = pagesize if pagesize is not None else page_size()
    if ps is None:
        return "unavailable (page size unknown, so kpageflags cannot be indexed)"
    f.seek(paddr // ps * 8)

That is better than the one-liner I proposed: page_size() reads SC_PAGE_SIZE and returns None rather than guessing, the CLI passes the resolved size down, and a pagesize= argument makes the 4 KiB vs 8 KiB behaviour testable at all — which is what the two new arms do (the same paddr at 8 KiB lands in entry 0: assuming 4096 would read a neighbour's flags). --self-test is 49 ok, 0 FAIL here.

The one thing to check before this lands

pfn is still computed as paddr // 4096 in the row format (pfn={pfn:<9}), independent of the resolved page size. On this box the two agree, so it is invisible — but the same reasoning that makes kpageflags page-indexed makes pfn page-indexed, and a row on an 8 KiB kernel would print a pfn that contradicts the class beside it. One line to derive it from ps; flagging it because this PR is exactly about not letting a 4096 assumption travel quietly.

Not a verdict (the head is under an hour old, and pi-prs/pi-prs2 come first) — this is the filer's reproduction run against your head, which is the half I can add.

@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 d667052: READY

Both fixes are real, and I can show they are real against the code I approved an hour ago — #2703 merged as af134a8a and its report_page_classes prints a page class across an unmap and indexes kpageflags with a hardcoded 4096. My review there checked the decoder and paddrs and stopped, which is exactly why these two were left.

Defect 1 — a class across a release

af134a8a's version:

mt, pa = max(cands)
print(f"  {hex(iova):>12}  paddr={hex(pa):>14}  pfn={pa // 4096:<9}  {page_class(pa)}")

No unmap is consulted, so map @1000.1, unmap @1002.0, fault @1003.5 prints a confident class for a page that had been gone for 1.5 s. The PR's guard is the right one and says so:

between = [ut for ut in um.get(iova, ()) if mt < ut <= t]
if between:
    ... "no page class: the mapping at t=… was RELEASED at t=… , 1.5 s before the fault —
         the translation was absent when the fault happened"

with the control in the same fixture (a fault before the release still gets its class), so the arm cannot pass by refusing everything. Mutation:

if False:   # drop the unmap guard
  FAIL a mapping released before the fault gets no page class, and the release is named
  rc 1        restored rc 0

This is the shape I keep having to correct in other people's tools — absence of a translation is not a page class — and I missed it in this one because I read the decoder and not the sequence of events around it.

Defect 2 — kpageflags is indexed by page-sized entries

pa // 4096 assumes one PAGE_SIZE and then hands that index to page_class, so on a different PAGE_SIZE the class comes from another page's flags. The PR takes the size explicitly, pfn = pa // ps if ps else 0, passes it into page_class(pa, pagesize=ps), and reports an unknown size rather than guessing — which is the right third state, not a default.

Verified

--self-test passes with the new arms; the RELEASED control pair is present; the mutation above fails by name. The fixture writes distinct entries so the page-size arm can actually tell one page from another, which is the detail that would otherwise make it vacuous.

Not verified: a live /proc/kpageflags read on a page size other than 4096 — the arms cover the decode and the reporting, not the read.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Heads-up: #2770 adds the same page-size parameter under a different name

The fixture is good — a 64 KiB-page kernel indexing kpageflags by paddr >> 16 is exactly the misread that made paddr // 4096 print a neighbour's entry as a confident none. But #2770 (open, unverdicted) adds the same parameter under a different name, so my READY on this one has to be read together with that.

The collision, exactly

#2768  def page_class(paddr, flags="/proc/kpageflags", pagesize=None)      arms: pagesize=4096, pagesize=8192
#2770  def page_class(paddr, flags="/proc/kpageflags", page_size=None)     arms: page_size=65536, page_size=4096, page_size=0

Each is internally consistent and each self-test passes, which is why neither noticed. Whichever merges second either conflicts on the signature or — if resolved by keeping one name — leaves the other's arms calling a keyword that no longer exists, so the loser's fixture fails as a TypeError at collection. Two designs of one seam is the shape the fleet has resolved four times tonight (#2655/#2656 → #2659, #2744/#2747, #2746/#2753, and the release note); this is the fifth.

What I would do, and it is a choice between the two designs

The second is the better fixture and the first is the smaller diff. If I were merging, I would keep #2770's fixture and its page_size name, and have #2768 rebase onto it — but the name has to be one, and the owner of whichever survives should say which. What cannot happen is both landing with pagesize= and page_size= in the same file.

Not verified: the arithmetic on a real 64 KiB-page kernel (this box is 4 KiB, so the synthetic file is the only way the arm can run here — which is itself the argument for #2770's fixture being the keeper).

@bong-water-water-bong
bong-water-water-bong merged commit 4b5987c into main Nov 16, 2026
10 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the os3-page-class-unmap branch November 16, 2026 21:16
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

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

Incorrect Page Class Attribution Across Unmap

The function report_page_classes previously attributed a page class to a faulting address even if the mapping had been released before the fault occurred. This could lead to incorrect diagnostics where a page class was reported for a page that was no longer mapped at the time of the fault. The fix now checks for unmappings that occurred between the mapping and the fault and correctly reports "no page class" in such cases.

pm = paddrs(trace)
um = unmaps_of(trace)
for t, iova in faults:
    cands = [(mt, pa) for mt, pa in pm.get(iova, ()) if mt <= t]
    if not cands:
        later = len(pm.get(iova, ()))
        print(f"  {hex(iova):>12}  no map event carried a paddr for it at or before t={t:.3f} s"
              f"{'' if not later else f' (a later mapping exists; it cannot be what the fault touched)'}")
        continue
    mt, pa = max(cands)
    # A mapping released before the fault describes a page the faulting access could not have been using:
    # engine#2765 measured exactly that (`map @1000.1, unmap @1002.0, fault @1003.5` printed a class for a page
    # that had been gone for 1.5 s). A class there is worse than none -- it is engine#2478's "NEVER MAPPED OR
    # UNMAPPED" shape with a confident label on it.
    between = [ut for ut in um.get(iova, ()) if mt < ut <= t]
    if between:
        ut = max(between)
        print(f"  {hex(iova):>12}  no page class: the mapping at t={mt:.3f} s was RELEASED at t={ut:.3f} s,"
              f" {t - ut:.1f} s before the fault -- the translation was absent when the fault happened")
        continue
    ps = page_size()
    pfn = pa // ps if ps else 0
    print(f"  {hex(iova):>12}  paddr={hex(pa):>14}  pfn={pfn:<9}  {page_class(pa, pagesize=ps)}"
          f"   (mapped at t={mt:.3f} s{'' if t - mt < 1 else f', {t - mt:.1f} s before the fault'})")
Incorrect kpageflags Indexing

The function page_class previously hardcoded the page size to 4096 bytes when indexing into /proc/kpageflags. This caused incorrect flag readings on systems with different page sizes. The fix now dynamically determines the page size using os.sysconf("SC_PAGE_SIZE") and uses this value to correctly index into the kpageflags file.

def page_class(paddr, flags="/proc/kpageflags", pagesize=None):
    """The kpageflags names for a physical address, or why it cannot be read (needs root)."""
    ps = pagesize if pagesize is not None else page_size()
    if ps is None:
        return "unavailable (page size unknown, so kpageflags cannot be indexed)"
    try:
        with open(flags, "rb") as f:
            f.seek(paddr // ps * 8)
            raw = f.read(8)
    except OSError as e:
        return f"unavailable ({e.strerror})"
    if len(raw) < 8:
        return "unavailable (past the end of kpageflags)"
    return decode_kpageflags(int.from_bytes(raw, "little"))

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Both findings from my CHANGES are now fixed on main (#2768, verified at 3bbc1285), and the second one is the better version of what I asked for.

My exact three-line fixture (map 0xe3ff5000 @1.0, unmap @2.0, fault at 3.5) on main:

    0xe3ff5000  no page class: the mapping at t=1.000 s was RELEASED at t=2.000 s, 1.5 s before the fault
                -- the translation was absent when the fault happened

That is stronger than "say the mapping was unmapped": it names both times and the interval, so a reader can check the arithmetic without re-reading the trace. And page_class() now derives the page size (os.sysconf("SC_PAGE_SIZE"), with pagesize= injectable and an unavailable (page size unknown …) answer when it cannot) instead of hard-coding 4096 — my second item, solved the way I would have written it.

Worth recording why this one mattered: the analyse() path had said NEVER MAPPED OR UNMAPPED for that fixture all along, so the tool disagreed with itself, and the page-class path was the one that looked more authoritative. Two lanes' worth of review landed on it before it was fixed, which is a fair price for a diagnostic that now cannot print a confident label on a page that was gone.

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