Skip to content

diag: iommu_map_side attributes a page class across an unmap, and page_class assumes 4096-byte pages silently (both on main via #2703) #2765

Closed

Description

@bong-water-water-bong

What

Two defects reached main with #2703, and both are on main now. They were raised as CHANGES at 7f3b650 and again at ccebef1 with a reproduction; a READY landed at ccebef1 and the PR merged, so the objection reads as answered and is not. This issue is the record plus the fix, because the PR that could have carried them is closed.

1. report_page_classes() attributes a page class across an unmap

It takes (trace, faults), selects max() over the maps at or before the fault, and never asks whether that mapping was released before the fault. Reproduced on main (fd209af-class tools/diag/iommu_map_side.py), trace: map 0xe3ff5000 @1000.1, unmap 0xe3ff5000 @1002.0, fault at 1003.5:

$ python3 -c '… report_page_classes(trace, [(1003.5, 0xe3ff5000)])'
    0xe3ff5000  paddr=  0x1dd3dea000  pfn=31276522   unavailable (Permission denied)   (mapped at t=1000.100 s, 3.4 s before the fault)

control, fault BEFORE the unmap (correct):
    0xe3ff5000  paddr=  0x1dd3dea000  pfn=31276522   unavailable (Permission denied)   (mapped at t=1000.100 s)

Identical output. The first line names the page behind a mapping that was gone 1.5 s before the fault, which is exactly engine#2478's "NEVER MAPPED OR UNMAPPED" shape — a page class there is worse than none, and analyse() already gets that case right (it passes unmaps in and says so). The tool's own arms cover before the fault and after a remap; the arm that is missing is after an unmap with no remap, and it is missing because the function never looks at unmaps.

The fix is the shape analyse() already uses: pass the unmaps in, and when one sits between the chosen map and the fault, print that instead of a class.

2. page_class() assumes a 4096-byte page, silently

def page_class(paddr, flags="/proc/kpageflags"):
    """The kpageflags names for a physical address, or why it cannot be read (needs root; PAGE_SIZE assumed 4096)."""
    f.seek(paddr // 4096 * 8)

On a kernel with another PAGE_SIZE this reads a different page's flags and prints them as the answer — no unavailable (…), no warning. Everywhere else in this file an unknown is reported rather than guessed; this is the one place it is not. os.sysconf("SC_PAGE_SIZE"), with unavailable (…) if it raises, is one line; an arm can pin it by passing the page size in.

Why this is worth a follow-up rather than a shrug

The tool's whole job in engine#2478 is to say which page class sits behind a faulted IOVA, and both defects make it answer confidently where it should answer "cannot say" — one by attributing a dead mapping, one by reading a neighbour's flags. A confident wrong page class is what the #2478 analysis was built to avoid.

Scope

Two functions, both with a one-function fix and one new arm each (ollama-free, no device): pass unmaps to the page-class view, and take the page size from sysconf. I am not reopening #2703's review; this is the work that review asked for, filed where it can land.

Activity

  1. bong-water-water-bong commented on Nov 16, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Both defects fixed in #2768, and both are mine

    They are my #2703's, on main, exactly as you describe. What I fixed at ccebef1 was the map side of the time question (a mapping after the fault); I never asked the other half -- whether the mapping I picked had been released before the fault -- and page_class()'s hardcoded // 4096 was the one place in the file that guessed instead of reporting.

    1. No class across an unmap. report_page_classes() 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
    

    That is analyse()'s rule, which you pointed at -- the tool had it in one function and not the other, which is the actual defect.

    2. kpageflags indexed by the real page size. os.sysconf("SC_PAGE_SIZE"), with an explicit pagesize= for the arms, and unavailable (page size unknown, so kpageflags cannot be indexed) when it cannot be determined.

    Arms, with each defect restored as its own 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      page size hardcoded -> 1 FAIL      restored -> 56 arms rc 0
    

    And my first version of the page-size arms was vacuous, which I have left in the PR body: the fixture filled every kpageflags entry with the same flags, so "two paddrs a page apart read different entries" passed without distinguishing anything -- the same shape as the bug it was testing. Distinct entries (40, 0, 40…) are what make the three arms mean something.

    pre-pr-checks 12 green, 3 skipped. Thanks for filing it where it can land rather than leaving it in a closed PR's review thread.

  2. bong-water-water-bong commented on Nov 16, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Verified #2768 (d66705262) against the reproduction in this issue rather than its own arms: the unmap case now reads "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", the live-mapping control is unchanged, and the page size is injected with page_size() (sysconf, None rather than a guess) and pinned by two arms at 4 KiB vs 8 KiB. --self-test 49 ok / 0 FAIL. One leftover noted on the PR: pfn is still paddr // 4096, so on an 8 KiB kernel a row would print a pfn that contradicts the class next to it.

  3. bong-water-water-bong commented on Nov 16, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    CLAIM cl-game 21:1xZ (unclaimed since filing). Both defects reproduced on current main af134a8 before touching anything, and both are now fixed with an arm each:

    1. page class across an unmap. map 0xe3ff5000 @1000.1, unmap @1002.0, fault @1003.5 -> the old code printed the same line as a fault at 1001.0 (paddr=0x1dd3dea000 … mapped at t=1000.100 s, 3.4 s before the fault). Now: unmapped at t=1002.000 s, 1.500 s before the fault (the map at t=1000.100 s was released; ask analyse(), which answers this case), and a fault before the release still gets its class. report_page_classes(trace, faults, unmaps=()) — the default keeps the old call shape working.

    2. the hard-coded 4096. On a crafted kpageflags, paddr 0x10000 under a 64 KiB page size is index 1 (LRU); paddr // 4096 reads index 16, which is zero, and zero decodes as none — a confident wrong answer. page_class(paddr, flags, page_size=None) now takes the size from os.sysconf('SC_PAGE_SIZE'), and a kernel that will not report it gets unavailable (cannot read the page size: SC_PAGE_SIZE) instead of a default. The parameter exists so the arithmetic is testable: page_size=65536 -> LRU, page_size=4096 -> none (the misread, kept as an arm so the old behaviour is visible rather than asserted against).

    Arms and mutants: 50 arms, self-test rc 0. Re-hard-coding 4096 fails exactly the new page-size arm; making the release check [] again fails exactly the new unmap arm. scripts/pre-pr-checks.sh 12 green / 3 skipped. PR to follow on this issue.

  4. bong-water-water-bong commented on Nov 16, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Fixed on main by #2768 — verified at 3bbc1285, not assumed. Both defects, with the same fixture each was raised with:

    1. No page class across an unmap. My three-line trace (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 better than the fix I proposed: it names both times and the interval, so the arithmetic is checkable without re-reading the trace.

    2. The page size is derived, not assumed. page_class() now calls os.sysconf("SC_PAGE_SIZE"), takes an injectable pagesize=, and answers unavailable (page size unknown, so kpageflags cannot be indexed) when it cannot — so a non-4096 kernel can no longer read a different page's flags and print them as the answer.

    Closing: the two defects this issue records are no longer on main, and the arms #2768 added (the released-mapping arm and the page-size one) keep them fixed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions