Skip to content

nvn/upload: a staged upload whose batch was flushed never reads the flushed batch's source buffer - #2516

Closed
akratch wants to merge 1 commit into
seedds:mainfrom
akratch:contrib/upload-flushed-source-20261112
Closed

akratch wants to merge 1 commit into
seedds:mainfrom
akratch:contrib/upload-flushed-source-20261112

Conversation

@akratch

@akratch akratch commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

pkg_upload_ reads a packaged texture in a large pool through a zero-copy buffer of the open upload batch (up_big_src). The batch keeps that buffer in g_up_keep, and up_flush's completion handler removes each kept object from the residency set and releases it. Taking the texture's staging (up_alloc) can flush that batch. Since PR #2378 the upload asks for a second buffer only when the batch changed (g_up_seq != src_batch), but when that second up_big_src returns nil it keeps using the first buffer, which now belongs to the flushed batch: the new batch's deswizzle is encoded against a buffer the flushed batch retires when it completes.

up_big_src can refuse between the two calls when the pool became resident or had an address given out, when the raw memory table now holds the whole pool, or when Metal makes no buffer.

Fix: a refusal after a batch change drops the first buffer and looks the storage up as for any other pool (mtl_addr). With no buffer there either, the texture takes the existing CPU path, as for a format the GPU deswizzle refuses. It does not return nil, because the caller has already counted the texture's pages as seen. The path taken when the second buffer is made is unchanged.

Validation on head 173c3485 based on upstream 0c078130f6db7d0d32c1dc28146feaf97713f008, Apple M4 Max / macOS 27.2, Xcode 27.2, profile 0400486024DEC000:

  • make -C dm/gfx/tests GAME=0400486024DEC000 check_t_upload_src (Metal validation): 55 checks, 0 failed. The test now repeats its batch split with the second buffer refused through a test-only switch (t_up_src_refuse): no second buffer is made, the upload whose staging flushed the batch gives up the first one (g_up_src_stale = 1), and every texel matches the CPU reference.
  • Negative control: the same test with the new fallback disabled (the refusal kept, the first buffer kept as on base) fails each upload whose batch was flushed gives up the flushed batch's buffer on 3 of 3 runs (55 checks, 1 failed). Fix restored, test rebuilt and passed again.
  • check_t_upload_budget: 261 checks, 0 failed.
  • make -C dm/host GAME=0400486024DEC000 builds.
  • python3 dm/tools/tests/test_docs.py: 6 tests pass; python3 dm/tools/docs_symptoms.py --check: current.

Limits:

  • Found by reading pkg_upload_ and up_flush; no game symptom, wrong picture or GPU error is attributed to it, and no run was shown to hit the refusal (it needs the pool's state to change between the two up_big_src calls).
  • The stale read itself is timing-dependent (it needs the flushed batch to complete before the new batch's deswizzle executes) and the test does not make it visible: with the fallback disabled the texel checks still pass, and only the count check fails. The negative control shows the old code kept the flushed batch's buffer, not that it read freed memory.
  • In the test, the refused case is served by mtl_addr; the CPU fallback for a pool with no buffer is not exercised.
  • Not run: the full gfx suite, regress.py check, any game, iOS. No chain, profile or front-end interface change.

Docs: new entry in docs/reference/memory.md (after PR #2378's entry) and its symptom index line; "Found on: no game".

…lushed batch's source buffer

pkg_upload_ reads a texture in a large pool through a zero-copy buffer of
the upload batch (up_big_src), which the batch keeps and its completion
handler takes out of the residency set and releases (up_flush). Taking the
texture's staging (up_alloc) can flush that batch. The upload then asks
for a buffer of the new batch (PR seedds#2378 does so only after a batch
change), but when that was refused (the pool became resident or had an
address given out meanwhile, the raw memory table now holds the whole
pool, or Metal made no buffer) it went on with the first one: the new
batch's deswizzle read a buffer the flushed batch retires, out of the
residency set and released once that batch completes.

A refusal after a batch change now drops the first buffer and looks the
storage up as for any other pool (mtl_addr); with no buffer there the
texture is read on the CPU, as for a format the GPU deswizzle refuses.
It does not return nil: the caller has already counted the pages as seen.

t_upload_src repeats its batch split with the second buffer refused
through a test switch (t_up_src_refuse): 55 checks. With the fallback
disabled the refused case fails its count check on every run; the stale
read itself depends on when the flushed batch completes and the test does
not make it visible. Found by reading; no game symptom is attributed to it.
@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation labels Nov 12, 2026
@gnavarro24

Copy link
Copy Markdown
Contributor

Hi! This branch is based on a main from before commit c70c242 ("ci: repair host and tool test failures on main"). Most of the failing checks here (Host unit tests, Tool tests, clang-tidy, Build check) come from that older base, not from this change.

To pick up the fix, merge the current main into this branch and push:

git fetch origin
git merge origin/main
git push

If the merge has conflicts, ask here if you want help with them.

@akratch

akratch commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Closing: current main already handles the core case this PR fixed — when the second source buffer after a batch flush is refused, main now takes a new buffer and falls back to the CPU instead of reading the flushed batch's buffer (seen on main around 31fdefe). The remaining differences here (an mtl_addr retry and in-batch wrapper reuse) are minor and not worth a separate change. Thanks for the CI note!

@akratch akratch closed this Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants