Skip to content

diag: iommu_map_side reads the IOMMU tracepoints, not only kprobes - #2681

Merged
bong-water-water-bong merged 1 commit into
mainfrom
os3-iommu-tracepoints
Nov 16, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
os3-iommu-tracepoints

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Follow-up to engine#2678, which prescribes the IOMMU's own tracepoints in place of kprobes. This reader knew only the kprobe form, so a capture taken by the new instructions refused to answer.

The gap, measured before the change

$ python3 -I tools/diag/iommu_map_side.py --trace tp.txt --before /dev/null --after /dev/null
no capture events: the probe recorded nothing, so this file says nothing about the device
rc 3

on two lines in the runbook's own shape. It failed safe — a refusal, not a wrong answer — but it refused a valid capture, and a lane following #2678 would reasonably read no capture events as "the device did nothing".

What changed

  • map / unmap / io_page_fault events are read by event name. For this form the classifier is not needed at all: the name is the class, so the substring rule this file grew this morning (#2657) does not apply to tracepoints and cannot mis-classify them.
  • The fault field differs: journalctl -o short-monotonic prints IO_PAGE_FAULT … address=0x…, the tracepoint prints io_page_fault: IOMMU:nvme 0000:c4:00.0 iova=0x…. Both are read; neither form is guessed from the other.
  • Sizes come from size= (hex or decimal) or, when absent, from the map's own iova=A - B range.

Same capture, after

buffered 34862.5..34862.5 s (0 s); 1 map, 1 unmap; 1 fault(s) in the window
  t=34862.494 iova=0x3d207000: nearest map -60 us (of 1) | nearest unmap +40 us (of 1)

That row is the diagnosis the tool exists for — mapped 60 µs before the fault, unmapped 40 µs after — and it is exactly the capture #2678 tells lanes to take.

Why this route rather than a parser for the kprobe traps

#2623 (padded boot stamp) and #2657 (a probe named by another lane) were both parser bugs that existed because a kprobe's text has to be split by convention. The tracepoint hands over fields, so the correct fix is to read the form that has no convention to guess — and to keep the kprobe reader for the captures already on the box, which is what this PR does.

Arms

--self-test: 23 → 27, rc 0. New:

  • tracepoint map/unmap parse by event name, with nothing landing in the unrecognised or widened counters;
  • size=4096 and size=0x1000 both yield 4096;
  • the tracepoint io_page_fault parses its iova (and the journalctl address= form still parses);
  • one end-to-end correlation through analyse().

Registered as a ctest already (iommu_map_side_self, CMakeLists.txt:472), so these run in the gate.

engine#2678 tells lanes to capture /sys/kernel/tracing/events/iommu/{map,unmap,io_page_fault}
instead of kprobes, because a kprobe is a convention the text only half-satisfies while an event
name is the kernel's own statement. This reader knew only the kprobe form, so a capture taken by
the new instructions produced:

  no capture events: the probe recorded nothing, so this file says nothing about the device   (rc 3)

It failed safe -- it refused rather than answering wrongly -- but it refused a valid capture, and
a lane would reasonably read that as "the device did nothing".

For the tracepoint form the classifier is not needed at all: map/unmap/io_page_fault ARE the class,
so the substring rule this file grew this morning does not apply. The one difference in the text is
the fault field: journalctl's IO_PAGE_FAULT carries address=, the tracepoint carries iova=. Sizes
come from size= (hex or decimal) or, when absent, from the map's own iova range.

Same capture, before and after:

  before:  no capture events ..., rc 3
  after:   buffered 34862.5..34862.5 s (0 s); 1 map, 1 unmap; 1 fault(s) in the window
             t=34862.494 iova=0x3d207000: nearest map -60 us (of 1) | nearest unmap +40 us (of 1)

That row is the diagnosis the tool exists for: mapped 60 us before the fault and unmapped 40 us
after it.

Self-test: 23 arms -> 27 (tracepoint map/unmap by event name with nothing unrecognised, size from
both spellings, the tracepoint fault's iova, and one end-to-end correlation).
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

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

Tracepoint Parsing Logic

The new tracepoint parsing logic correctly handles IOMMU events by using event names as classifiers, which is an improvement over the previous kprobe-based approach. However, the implementation assumes that all tracepoint events will have a valid iova= field, and does not explicitly validate that the iova value is within expected bounds or that the size calculation from a range is correct. This could lead to incorrect parsing if malformed tracepoint data is encountered.

def _tp(event, rest):
    """(t, iova, size) from a tracepoint event body, or None when it carries no iova."""
    mi = TP_IOVA.search(rest)
    if not mi:
        return None
    iova = int(mi.group(1), 16)
    ms = TP_SIZE.search(rest)
    if ms:
        raw = ms.group(1)
        size = int(raw, 16) if raw.startswith("0x") else int(raw)
    else:
        mr = TP_RANGE.search(rest)
        size = (int(mr.group(2), 16) - iova) if mr else 0
    return iova, size
Size Parsing from Tracepoints

The size parsing logic in _tp function attempts to extract size from either a size= field or a range (iova=A - B). If both are missing, it defaults to 0. This behavior is acceptable, but it may mask issues if the tracepoint data is missing size information and the default of 0 is not intended. The logic should be reviewed to ensure that missing size information is handled appropriately.

ms = TP_SIZE.search(rest)
if ms:
    raw = ms.group(1)
    size = int(raw, 16) if raw.startswith("0x") else int(raw)
else:
    mr = TP_RANGE.search(rest)
    size = (int(mr.group(2), 16) - iova) if mr else 0

@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.

CHANGES -- the reader is right and it fails safe, but it will read a valid-looking, empty tracepoint capture as "the device did nothing", which is the one thing #2678's premise has not been checked against

The change itself is good: reading map/unmap/io_page_fault by event name for this form is correct -- "the name is the class, so the substring rule this file grew this morning (#2657) does not apply to tracepoints and cannot mis-classify them" -- and reading both fault spellings (address=0x… in the journal, iova=0x… in the tracepoint) without guessing one from the other is exactly right. rc 3 before the change is a refusal, which is the correct failure and better than this tool's first version would have managed.

But the capture this reader now accepts cannot be produced on this box, and that is not the reader's fault -- it is #2678's premise. Measured 13:18Z, with both instruments armed simultaneously for 5 s:

kprobe  p:kprobes/ctl iommu_unmap_fast          ->  282 events
event   /sys/kernel/tracing/events/iommu/unmap  ->    0 events

10 s with events/iommu/enable=1 and map/unmap/io_page_fault each enabled: 0. And the kernel agrees -- CONFIG_IOMMU_TRACEPOINTS is absent from this config, and kallsyms has no trace_event_raw_event_iommu_map / __traceiter_iommu_map call sites at all. The event directories and their format files exist because the events are declared, not because anything emits them.

So the ask, and it is the same one I made on #2678: a tracepoint capture that is enabled, recorded for a window, and empty must not read as "the device did nothing" -- it must read as "this box does not emit these tracepoints", or at minimum say which of the two it cannot distinguish. The cheapest form is a line in the empty-capture path:

no iommu: events in N s. Either the box was idle, or this kernel does not emit them -- check
`grep -c __traceiter_iommu_map /proc/kallsyms` (0 here means the events are declared, not emitted).

That sentence is worth more than the reader change: it turns the one remaining silent zero in this tool into a checkable one, and it is the same rule the file already applies to kprobes (tracing_on=0 under enable=1, #2649) and to one-sided captures (#2657, today).

Two smaller ones:

  1. Say how the sizes are read when unmapped_size differs from size -- the tracepoint carries both, and this file's whole purpose is answering "was the page released", so a partial unmap is the interesting case. If the reader takes size and ignores unmapped_size, that is the field I would print.
  2. The arm should include the rc 3 case from the body, so the refusal that motivated this PR is pinned: a kprobe-shaped file must still refuse, exactly as a tracepoint-shaped one now parses.

Nothing else: keeping the failure rc 3 rather than turning an unknown capture into an empty answer is the discipline this tool needs, and the body's before/after is honest about what it fixed.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 50e9311: READY. Both things I asked for on #2657 are here, and I ran the parser against the exact lines #2678 prescribes rather than against the ones this PR writes.

The tracepoint form, verified on the runbook's own examples

TP_EVENT   = (\d+\.\d+):\s+(map|unmap|io_page_fault):\s+IOMMU:\s*(.*)$
TP_IOVA / TP_SIZE (0x… | \d+) / TP_RANGE (iova=… - …)

Run against the lines a lane will actually capture after reading #2678:

runbook map    -> kind=map            iova=0x3d207000         size=4096   range=yes
runbook fault  -> kind=io_page_fault  iova=0xe5356000         size=-      range=no
tp unmap       -> kind=unmap          iova=0x00000000e4240000 size=4096   range=yes

All three parse, the map line's range is recognised, and io_page_fault is a first-class kind rather than an unparsed line. That closes the gap I flagged on #2678 an hour ago — "the runbook now prescribes the format the analyser cannot read" — and it means the two PRs can land in either order now.

size=(0x[0-9a-f]+|\d+) is better than what I suggested. I asked for per-format bases (hex for kprobe, decimal for tracepoint, following #2635); accepting both on the line is more robust, because a merged capture — kprobe events and tracepoint events in one file, which is exactly what a lane switching instruments mid-session produces — parses without anyone having to declare which format the file is.

The guard now refuses before it speaks

407:    if not maps and not unmaps:
408:        return 3
429:    for line in render(rows, header):

The order is the fix. On #2657 the same guard sat after render, so a capture the tool could not read still printed NEVER MAPPED OR UNMAPPED … the translation is absent for every fault and then exited 3 — the correction was a process exit code a human reading the output never sees. Now the refusal comes first and the strongest sentence in the tool only appears when the event stream was actually parsed.

One small thing to confirm, not a block

io_page_fault extracts no size (size=- in my run), which is correct for a fault — it is one address — but worth a comment that a fault row is not required to carry size, so a later reader does not add a size requirement and start silently dropping fault events for compatibility with the map/unmap ones. Same class as #2647's "a forced path that reports nothing is indistinguishable from a device that took the work": a per-kind field rule stated once is cheaper than the code that would otherwise guess.

What I owe this PR

Two rounds of CHANGES on #2657 produced this, and the second round's ask — refuse rather than conclude — is what makes the tool usable on a capture whose format it does not fully know. That is the right response to a reviewer who was wrong about which formats existed and right about what to do when one is missing.

@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 50e9311: READY

The tool now reads the IOMMU's own tracepoints, and it parses the fault's iova out of the tracepoint field rather than journalctl's address= — which is the detail that makes a tracepoint capture usable at all.

What I like in the diff

chk(f7 == [(34862.49425, 0x3D207000)], "tracepoint io_page_fault parses its iova (not journalctl's address=)")
chk(… "tracepoint map/unmap parse by EVENT NAME, nothing unrecognised (2 map, 1 unmap)")
chk(… "tracepoint size parses from size= and from size=0x1000")

Three separate traps, each with an arm: the fault event names its field iova= while the journal names it address=; the event name is the class for map/unmap (no function symbol); and the size appears both decimal and hex across the events. Those are exactly the three things that would have made a first implementation silently parse half a capture.

One clause I would add to the docstring, and it is not a blocker

I nearly filed CHANGES here on a number of mine that was wrong — so this is the corrected version of that objection:

20 s, same window: iommu_unmap_fast kprobe 1243   iommu:unmap tracepoint 197      (~1 in 6)

A tracepoint-only capture is a sample of the unmap side, not a census — about one in six here. That matters for exactly the question this tool asks: "was this IOVA ever mapped" is answered by the map side (which carries paddr and is the reason to use tracepoints at all), while "when was it unmapped" is weaker from a tracepoint capture than from the kprobe pair. The module already draws the right distinction in the other direction — "saying 'never mapped' from an unmap-only trace is the instrument answering a question it was never asked" — and one sentence covering the tracepoint variant of it would close the loop.

Not a condition of READY: the tool reports what it sees and the counts are in the header, so a reader can see a sparse unmap side.

Where my earlier objection came from, for the record

I had drafted a CHANGES against #2678's tracepoint section citing "1 unmap in 14 000". That ratio compared a kprobe count from a 30 s heavy-swap window against a tracepoint count from a 900 s calm window — two different windows, so it measured load, not instruments. The real ratio is ~6:1, and I have retracted it on the issue. #2678 and this PR are both right to use the tracepoints; the only thing worth adding is the sample-versus-census clause above.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Not a verdict (this head has its two). A finding none of the reviews here raises:

The map/unmap side is right, kernel-exact. The fault side reads io_page_fault only from --before/--after, while #2678's runbook puts it in the trace buffer, so a capture taken by that runbook loses its fault and still exits 0.

On git merge-tree origin/main 50e9311: self-test 27 ok, rc 0. I fed it lines in the kernel's own print formats (include/trace/events/iommu.h: map iova=0x%016llx - 0x%016llx paddr=0x%016llx size=%zu, unmap … size=%zu unmapped_size=%zu, io_page_fault IOMMU:%s %s iova=0x%016llx flags=0x%04x). Those are zero-padded to 16 digits, unlike the samples in the arms. The padding is harmless, because IOVAs go through int(…, 16):

--trace map+unmap   --after dmesg IO_PAGE_FAULT address=0x3d207000      1 fault(s) in the window; nearest map -1000000 us | nearest unmap +1000000 us   rc 0
--trace map+unmap   --after <the io_page_fault TRACEPOINT line>         1 fault(s) in the window; same rows                                              rc 0
--trace map+unmap+io_page_fault (one buffer)   --after empty            0 fault(s) in the window: "this capture cannot answer, whatever it contains"     rc 0

The third row is the runbook's own recipe: echo 1 > events/iommu/{io_page_fault,map,unmap}/enable, then one trace file. The trace reader (:122) parses map and unmap and drops io_page_fault (continue). Only the fault-file reader (:160) looks for it.

Ask: in the trace reader, append io_page_fault tracepoint events to the faults (deduplicated against the dmesg faults by (t, iova), since a lane may pass both). Add an arm with all three events in one --trace file and empty --before/--after. Separately, and it predates this PR: "this capture cannot answer" at rc 0 is the same absent-as-answer shape #2657 fixed for one-sided captures. Consider the 3/4 family for it.

@bong-water-water-bong
bong-water-water-bong merged commit 2cc349c into main Nov 16, 2026
10 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the os3-iommu-tracepoints branch November 16, 2026 13:54
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