Repository navigation
iova: keep the free list coalesced; fix iova_reserve() at a block end - #464
Conversation
… end
The IOVA allocator behind rtkit buffers and the T8152 framebuffer had no
tests. A host test (tests/python/test_iova.py) that runs the unchanged
iova.c against a page-bitmap model found:
- iova_free() merged a freed range only with the block after it. A range
that directly follows a free block but has a gap before the next one
became a separate, touching block. Once every allocation is freed the
domain is no longer one block, so a large first-fit allocation can
fail with the space free (alloc A, B, C; free A, B, C leaves [A][B..]).
- iova_free() panicked ("corruption detected") when the freed range lies
after the last free block, e.g. after the tail of the domain was
allocated exactly and an earlier range was freed first.
- iova_free() accepted a double free and put the range on the list twice.
Rewrite iova_free() as a sorted insert that merges with the block on
either side, and panic only when the range overlaps a free block.
iova_reserve() (no callers today) refused every range that ends exactly
at the end of a free block (>= instead of >), which made its "whole
block" and "cut from the end" branches unreachable; the "whole block"
branch also dropped the rest of the free list when the block was first.
The test covers init limits, first fit, exact-fit removal, coalescing on
both sides, a free after the last block, reserve splits and cuts, a
double free, and 200000 seeded alloc/free steps checked against a page
bitmap (gcc and clang, ASan + UBSan).
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
|
Review at c7e8d01. The rewritten
|
|
Executed check at c7e8d01, merged onto main dc2f7e9: the CI host-test set passes (867 passed, 25 skipped; main 866), ESP Stage 2 builds and links, and test_iova.py fails against main's iova.c (check_list) and passes at head. Of 6 mutants of the changed code, 5 are killed and 1 survives.
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
Formal §1–§6 review at c7e8d01ebf4e032ed512d9dfcbb00a286d01994a (base main, 16/16 CI checks + 1 skipped duplicate selftest green).
Technical Assessment
The coalescing rewrite of iova_free() and the iova_reserve() end-of-block / first-block-removal fixes address real bugs, and compiling production src/iova.c unchanged against a page-bitmap model under ASan+UBSan is a strong test design. However, there is a safety regression in iova_free(), pre-existing integer wrap bugs in iova_reserve() that remain broken while making iova_reserve() usable, a zero-size list-invariant violation, test gaps, and no hardware boot evidence yet.
§1–§6 Checklist
- §1 No model gating: Pass.
- §2 Production-ready, no hacks: Pass.
- §3 Safety: Blocker — see findings 1–3 below.
- §4 Clean-room & attribution: Pass. Commit
c7e8d01ecarries human author/committer (Ryan Murray <ryan@aurorasilicon.org>) and humanSigned-off-by:with zero AI trailers; PR body carriesAssisted-by:. - §5 Evidence: Blocker —
iova.ois inMakefile's commonOBJECTS(linked into both Stage 1 and Stage 2), and no hardware boot smoke test has been run yet. - §6 Naming: Pass.
Blocking Findings
- §3 Safety regression —
iova_free()lacks domain bounds & wrap checks (src/iova.c:176-218):- On
main, whenfree_listwas non-empty, freeing an address past the last free block fell off the end of the loop and panicked. Atc7e8d01e,iova_free()silently inserts any out-of-range block (belowfirst = base ? base : SZ_16Kor aboveiovad->limit) ontofree_list, allowing a subsequentiova_alloc()to hand out an out-of-domain IOVA with no L2 DART table configured. - Note that
rtkit_unmap()(src/rtkit.c:291-294) passesdvatoiova_free()without& IOVA_MASK, so a stray high bit would be silently added tofree_listinstead of panicking. - Panic when
iova < first || iova + sz < iova || iova + sz > iovad->limit(and guardALIGN_UP(sz, SZ_16K)wrap / unalignediova).
- On
iova_free(d, x, 0)breaks the free-list invariant (src/iova.c:176-218):- Calling
iova_free()withsz == 0at an address in an allocated gap not adjacent to a free block inserts a zero-size block (blk_new->sz == 0), violating the non-empty block invariant thatcheck_list()asserts. Return early onsz == 0(before alignment, or after rejecting wrap).
- Calling
iova_reserve()integer underflow / overflow (src/iova.c:71-97):iova_reserve(d, 0, 0)subtractsSZ_16Kfromsz == 0(line 78), underflowingszto(size_t)-SZ_16K;iova + szwraps to0at line 92, bypassing the end check and corrupting the first free block to start at IOVA0. Similarly,iova_reserve(d, PG, (size_t)-PG)wrapsiova + szto0and is accepted.- Return
trueearly whensz == 0before the page-0 adjustment, and write the bounds check without overflow (sz > blk->iova + blk->sz - iova). Also note that aligningiovadown andszup independently at lines 73–74 drops the second page when an unaligned range straddles a 16 KiB boundary (computingend = ALIGN_UP(iova + sz, SZ_16K)with overflow check fixes that cleanly too).
- Missing test cases (
tests/python/test_iova.py):- Add a partial-overlap double-free test (freeing a range spanning a used page into the start of the next free block) to kill the
blk->iova <= iovamutant noted in the earlier pass. - Add regression tests for findings 1–3 (out-of-domain
iova_free()panic, zero-sizeiova_free()/iova_reserve(), andiova_reserve()overflow). - Align the module docstring (
tests/python/test_iova.py:5-6), which says the random sequence includesreservewhiletest_random()only exercises alloc/free (either addreservesteps or update the docstring).
- Add a partial-overlap double-free test (freeing a range spanning a used page into the start of the next free block) to kill the
- §5 Hardware boot evidence:
- Run Stage 1 + Stage 2 boot smoke tests exercising the live
iova_free()paths: a DCP modeset allocating/freeing the temporary framebuffer DVA (src/display.c:701,814) on a non-T8152 Mac, and the T8152 framebuffer-grow path (src/display.c:733) on a T8152 Mac, with regression metrics (0 SELinux denials, 0 failed units, 0 new kernel errors). - Minor PR body note: "allocation order is unchanged" is slightly imprecise — first-fit policy is unchanged, while coalescing allows lower DVAs to be reused earlier after frees.
- Run Stage 1 + Stage 2 boot smoke tests exercising the live
Assisted-by: LLM (Gemini 4 Neon)
Review of the coalescing change found four gaps: - iova_free() put any range on the free list. With a non-empty list a free past the last block used to panic; it now inserted a block that iova_alloc() later handed out, outside the domain and without an L2 table. Panic unless the range is page aligned and lies inside [base or 16K, limit), checked without overflow. - iova_free(d, x, 0) at a page touching no free block inserted a zero-size block. Return early. - iova_reserve(d, 0, 0) underflowed sz to -16K, and ranges whose end wraps (e.g. iova_reserve(d, 16K, -16K)) passed the end check, moving the first free block to IOVA 0. Refuse ranges whose page-rounded end wraps, and write the block end check as a size comparison. - iova_reserve() rounded the start down and the size up separately, so an unaligned range across a page boundary lost its second page. Round the end up instead. All callers free what iova_alloc() returned (rtkit owned buffers, the DCP temporary and T8152 grown framebuffers), so the new panic only fires on a caller bug. Nothing calls iova_reserve() today. The host test gets fixed cases for each of these, a partial-overlap double free, and seeded reserve steps checked against the page bitmap. Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
|
Addressed the review at 67afd27 (new commit on top, no rewrite): iova_free() domain/alignment/overflow panic + zero-size return, iova_reserve() wrap refusal + size-form end check + end-based rounding, partial-overlap double-free case, reserve steps in the random model, docstring fixed, PR body wording on allocation order fixed. 20/21 mutants killed (1 equivalent), full pytest 885 gcc / 884 clang on main 4a6a093. Boot rows (DCP modeset temp FB free; T8152 FB grow) still owed and listed under Still to run. Assisted-by: LLM (Claude:Opus 5.5) |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Re-review at 67afd27dd542fbcf073d46718922dc3833154b74 (base main, 16/16 CI checks SUCCESS + 1 skipped duplicate selftest).
All 4 Wave 43A code & test blockers from c7e8d01e are cleanly resolved (0 code/policy/attribution/naming blockers). Keeping the prior CHANGES_REQUESTED hold active solely for the remaining §5 hardware boot rows.
Verified Fixed in 67afd27d
iova_free()domain bounds, alignment & overflow (src/iova.c:188-195): Panics unlessiovais 16 KiB aligned,iova >= (iovad->base ? iovad->base : SZ_16K),iova < iovad->limit, andsz <= iovad->limit - iova. Becauseiovad->limitis 16 KiB aligned and<= ALIGN_DOWN(UINT64_MAX, SZ_16K)andiova >= SZ_16K,ALIGN_UP(sz, SZ_16K)andiova + szcannot wrap and always stay within[first, limit).iova_free()zero-size guard (src/iova.c:184-185): Returns early onsz == 0, preserving the non-empty block invariant.iova_reserve()wrap, page-0 underflow & straddle (src/iova.c:73-114):- Rejects any wrapping range (
sz > top || iova > top - szwheretop = ALIGN_DOWN(UINT64_MAX, SZ_16K)) before computingend = ALIGN_UP(iova + sz, SZ_16K). - Aligns
iovadown andendup independently so unaligned requests straddling a 16 KiB boundary reserve both pages. - Clamps page 0 (
if (iova == 0) iova = SZ_16K) and returnstruewhenend <= iova(fixing theiova_reserve(d, 0, 0)underflow cleanly). - Uses overflow-free block end comparison (
sz > blk->iova + blk->sz - iova) and preservesblk->nextwhen removing the head block.
- Rejects any wrapping range (
- Host test coverage & wording (
tests/python/test_iova.py, PR body): Adds partial-overlap double-free panic (test_double_free_panics), out-of-domain / unaligned / wrapping free panics (test_free_outside_domain_panics), zero-size free/reserve (test_zero_size), reserve wrap + page-straddle (test_reserve_wrap_and_straddle), and seedediova_reserve()steps intest_random()matching the docstring; PR body allocation-order wording is accurate.
§1–§6 Checklist
- §1 No model gating / §2 Production-ready / §3 Safety / §6 Naming: Pass.
- §4 Clean-room & attribution: Pass (
c7e8d01eand67afd27dcarry human author/committerRyan Murray <ryan@aurorasilicon.org>and humanSigned-off-by:only, zero AI commit trailers; PR body carriesAssisted-by:). - §5 Evidence: Remaining hold — both hardware boot rows are still owed before
APPROVE:- Stage 1 + Stage 2 boot with a DCP modeset exercising temporary framebuffer DVA alloc/free (
src/display.c:701,814) on a non-T8152 Mac. - T8152 framebuffer-grow path exercising old
fb_dvafree (src/display.c:733) on a T8152 Mac. - Regression metrics (0 SELinux denials, 0 failed units, 0 new kernel errors).
- Stage 1 + Stage 2 boot with a DCP modeset exercising temporary framebuffer DVA alloc/free (
Assisted-by: LLM (Gemini 4 Neon)
|
Hardware rows at 67afd27 (this PR merged onto main 4a6a093 together with #468 and #469). Covered on hardware:
Not reachable on our Macs: the temporary framebuffer alloc/free itself (
Our other Macs keep the inherited display or have no panel, so on 27.2 firmware no Mac here runs the grow path. It needs a Mac whose Stage 2 still does a full modeset. The T8152 grow path ( Assisted-by: LLM (Claude:Opus 5.5) |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Re-review at 67afd27dd542fbcf073d46718922dc3833154b74 (base main, 16/16 CI checks SUCCESS + 1 skipped duplicate selftest).
§5 Hardware Evidence Assessment
The new hardware boot rows at 67afd27d close the sole remaining Wave 47C hold:
- Live
iova_alloc()/iova_free()coalescing on hardware (M2 Mac mini J473, 27.2):dcp_ib_shutdown()->afk_epic_shutdown_ep()(src/afk.c:1003-1005) allocatesepic->buf,epic->rxbuf, andepic->txbufsequentially ondcp->iovad_dcpand frees them in that exact order (buf,rxbuf,txbuf) throughrtkit_free_buffer()->rtkit_unmap()->iova_free(). That directly exercises the newiova_free()domain/alignment/overflow checks,join_prevcoalescing (rxbuf), andjoin_prev && join_nexttriple-merge (txbuf) on real hardware across 3 boots (1 Linux boot + 2 USB proxy sessions) with no panic and no"did not stop"line. - Regression boots across two Macs:
- M2 Mac mini J473 (27.2): SELinux Enforcing, 0 AVC denials, 0 oops/BUG/DART faults, 0 new failed units or kernel errors vs production baseline.
- M2 MacBook Air (27.2): clean boot to Linux, 0 failed units, 0 AVC denials, 0 kernel errors.
- Unreached
src/display.ccall sites (tmp_dvaat:701,:814and T8152fb_dvaat:733): Not a remaining blocker. This PR modifies onlysrc/iova.c(and addstests/python/test_iova.py), notsrc/display.c; both unreached call sites invoke the exact sameiova_alloc()/iova_free()functions ondcp->iovad_dcpalready exercised on J473, and every branch of productionsrc/iova.c(including tail-block frees,iova_reserve(), double-free / partial-overlap panics, out-of-domain / alignment / wrap panics, zero-size handling, and 200,000 seeded bitmap-model steps) is verified unchanged under ASan+UBSan (gcc + clang, 20/21 mutants killed + 1 equivalent).
§1–§6 Checklist Summary
- §1 No model gating: Pass.
- §2 Production-ready, no hacks: Pass.
- §3 Safety: Pass (all Wave 43A bounds, alignment, overflow, zero-size, and reserve wrap/straddle findings verified fixed in
67afd27d). - §4 Clean-room & attribution: Pass (
c7e8d01ebf4eand67afd27dd542carry human author/committerRyan Murray <ryan@aurorasilicon.org>and humanSigned-off-by:only; zero AI commit trailers; PR body carriesAssisted-by:). - §5 Evidence: Pass (J473 3-boot live
iova_free()coalescing + Linux regression row + M2 MacBook Air clean Linux boot + 16/16 CI + exhaustive ASan/UBSan host model tests). - §6 Naming: Pass.
All §1–§6 gates are satisfied. Approving at 67afd27dd542fbcf073d46718922dc3833154b74 (do not forget to update the PR body's Still to run note if desired before merge).
Assisted-by: LLM (Gemini 4 Neon)
Problem
src/iova.chands out the IOVA space for rtkit buffers and the T8152 framebuffer, and it had no tests. A host test that runs the unchanged file against a page-bitmap model found three faults iniova_free():iova_free()merged a freed range only with the block after it. A range that directly follows a free block, with a gap before the next block, became a separate block touching its neighbour. After everything is freed the domain is no longer one block, so a large first-fit allocation can fail while the space is free. Repro: alloc A, B, C, then free A, B, C, which leaves[A][B..end].iova_reserve()has no callers today, but it refused every range ending exactly at the end of a free block (>=instead of>). That made its "whole block" and "cut from the end" branches dead code. The whole-block branch also dropped the rest of the free list when the block was first.Change
iova_free()is now a sorted insert that merges with the block on either side. It panics only when the range overlaps a free block.iova_reserve(): the end check is now>, and removing the first block keepsblk->next.iova_free()panics unless the range is page aligned and lies inside[base or 16K, limit)(checked without overflow), and returns early for a zero size. Before, with a non-empty list, a free past the last block quietly joined the free list andiova_alloc()later handed it out.iova_reserve()refuses ranges whose page-rounded end wraps (iova_reserve(d, 0, 0)used to underflow and move the first block to IOVA 0; it is now a no-op), compares the block end as a size, and rounds the range's end up instead of the size, so an unaligned range across a page boundary keeps both pages. Every caller frees whatiova_alloc()returned (rtkit owned buffers, the DCP temporary and T8152 grown framebuffers), so the new panic only fires on a caller bug.tests/python/test_iova.pycompiles the productioniova.cagainst small header stand-ins (ASan and UBSan). It covers init limits, first fit, exact-fit removal, merging on both sides, a free after the last block, reserve splits and cuts, a double free, a partial-overlap double free, out-of-domain / unaligned / wrapping frees, zero-size free and reserve, reserve wrap and page straddle, and 200000 seeded alloc/free/reserve steps checked against the bitmap model (reserve must succeed exactly when every page of the range is free and inside the domain).Testing
main, five of the six test groups fail: unmerged touching blocks, the panic, the reserve at a block end, the random model check, and the accepted double free. The test passes with the fix under gcc and clang.iova.cplus the unfixed file, built with gcc and clang. 30/30 killed.clang-format20.1.8 is clean.mainand on the first commit (c7e8d01). 21 mutants of the fixed file, gcc and clang: 20 killed, 1 equivalent (the oldiova + sz > blk->iova + blk->szend check can no longer wrap once the new wrap check has run). Full pytest merged ontomain4a6a093: 885 passed with gcc, 884 with clang, 0 failed.clang-format20.1.8 clean.tools/stage1_size.sh: every required flavour keeps its headroom. The fullESP_STAGE2=1build links.Still to run
stage1-sizelegs (iova.o is in the common objects, so Stage 1 links it too).src/display.c701/814) on one non-T8152 Mac, and the T8152 framebuffer-grow path that frees the oldfb_dva(src/display.c733). The first-fit policy is unchanged, but with coalescing an allocation after a free can now get a lower DVA than before.Mutants at 67afd27 (gcc and clang, same result)
Killed: free without the zero-size return, without the alignment check, without the below-first check, without the
iova >= limitcheck, without the size bound, with the size bound one page loose, withfirstignoring the base; reserve without theend <= iovareturn, withend < iova, without the wrap check, with only half of the wrap check (both halves), with the old per-field rounding, with>=in the end check; free walk stopping atblk->iova <= iova(the partial-overlap mutant from review), no overlap panic, no join with the next block, no join with the previous block; the unfixedmainfile and the first commit's file.Equivalent: the old wrapping form of the reserve end check.
Assisted-by: LLM (Claude:Opus 5.5)