rebind_bytes_no_copy (Buffer and Heap) may now run while other threads encode and
commit command buffers that use the buffer or heap. Before, the Safety contract
excluded that; this lifts it.
The windows found:
- W1: an encoder call reads a buffer's pages before it holds the buffer. A
dispatch builds its bind state, a draw its tables, and a blit reads the
VkBuffer for a copy run that records in a later call. A rebind can land
between that read and the hold. The hold then pins the new pages while the
commands name the old ones.
- W2: what lies over the bytes (aliasing images and their views, texel views,
the heap sets' elements) is made over the new pages only after `replace` has
let go of the registry's lock. A hold in that gap pins the new pages over
half-made state.
- W3: the render lowering thread does the same reads, later.
- W4: an encoding again (`replay.rs`) can run under a concurrent rebind of
another thread. This one is a live defect under today's contract: a commit's
rate-map or split re-encode, and one rebind's turn racing another buffer's
rebind.
- W5: a texture made over the bytes during the call stays excluded.
The change: detect, then encode again. Nothing waits on the encode path.
- Each rebind's publish runs from `Rebinds::replace` to a new
`Rebinds::published`. `published` is the close, after `renew_over`. It is
tracked by a per-device count of closed publishes (`published`), a count of
open ones (`open`), and `Entry::publishing` / `Entry::published`.
- Every encoder call loads that count as it begins (`Holds::begin_call`: one
Acquire load of an AtomicU64). The sites: compute and blit `logged`,
`end_encoding`, the five predication calls, render `Lowering::logged`, the
deferral's `at_call_unit`, the replay dispatcher and `open_render`, which
reads the count before it reads the attachments.
- A hold or pin finds the call raced when the entry's publish is open, or
closed after the call began. A raced Taken counts as rebound
(`Submitting::rebound`), so the commit encodes its encoders again before the
first submission (`State::raced_again`), as for a rate-map reset.
- `reencode` loops: when an encoding again itself raced, it waits until no
publish is open (`Rebinds::wait_published`, a condvar on `buffers`) and
encodes again.
- The close marks again the command buffers committed while the publish was
open, so their raced encoders are encoded again over finished state.
- An encoder the runtime cannot encode again whose call raced fails
EncodedUseRebound at its submission. These are the existing list: a compute
encoder its predication unit reaches past, one that left a GPU-written
indirect dispatch to its host step, a blit or render encoder opened while no
buffer over host pages existed, one whose commit fails it, and one with a
call that answers otherwise. A rebind that no encode overlaps changes
nothing for them.
- New WorkCounts::encoders_raced_rebind counts the encoders marked stale this
way.
- `blit_at` holds the buffer before it reads its VkBuffer, because a copy run
records in a later call.
- `replace` refuses a second rebind of a key whose publish is open
(EncodedUses).
What the publish window covers:
- Inside it: `live_buffers.rebound`, the alias leave/hold/place, the image
swaps and parking, the texel views, `forget_stale` and `renew_over`.
- Moved after the close: dropping the read twins (`drop(twins)`), dropping
the old pages (`drop(old)`, which reaches vkDestroyBuffer/vkFreeMemory and
the deallocator), `release_held_back`, `heap_sets::retire` and the
encodings again.
- What a later pin judges against: the old pages' twins and the stand-ins.
- Not covered: the import of the new pages and the images made again over
them before `replace`. These are the rebind's own and are invisible to
other threads until `replace`.
Deadlock audit, by lock:
- `rebinds.buffers`:
- `hold` and `pin` take `buffers`, then the cell's `pages` (`lying`), and
hold nothing across them.
- `replace` takes `buffers` -> `waiting` (`mark_waiting`) and `buffers` ->
`pages` (`cell.replace`).
- `published` takes `buffers` -> `waiting`. `mark_reencode` is an atomic.
- No thread holds `waiting` or `pages` and then takes `buffers`.
- The existing `state` -> `buffers` order (`encode_moved`'s `submitting`)
is unchanged. Neither publish step takes a command buffer's state lock.
- `wait_published`: its callers hold only the command buffer's `reencoding`
turn. These are the commit (after its state and slot locks are gone) and a
rebind's or commit's `reencode`. No publish step takes a `reencoding` lock
or waits for one. A publish waits for nothing: these take no lock an
encoder holds across a hold:
- `live_buffers.inner`;
- `aliases`;
- the image swap and view locks (`Image::swap_in`);
- HostPages `parked`;
- `rebinds.stale`;
- the texel views;
- `heap_sets`: `passing_hold` and `heaps`, whose writes are deferred while
`heaps.holds > 0`, with no wait.
So every wait ends as soon as the open publishes close.
- The test pause points (`rebind::pause`, cfg test) stop at the hold, with
no runtime lock held, and in the publish after `replace` has let go of
`buffers`.
Docs:
- The Safety text of Buffer::rebind_bytes_no_copy and
Heap::rebind_bytes_no_copy states the new contract and outcomes. Still
excluded: another rebind or a detach of the same buffer or heap, a texture
made over its bytes, and a resource placed in the heap, during the call.
- The rebind.rs module docs gain an **Other threads** bullet.
- Also updated: the EncodedUseRebound declaration and its docs/API.md
catalogue entry (the cost text is unchanged), the `renew_over` doc and the
runtime README.
- api_signatures.tsv gains the new field.
Tests (src/tests/rebind_races.rs, device tests):
- A pause-point control at the hold: a dispatch that read the bound buffer
before its hold, with a second buffer keeping the old import. Expected: the
new page's output and one raced encoder.
- A pause-point control in the publish: a dispatch reading a texture over the
buffer.
- A threaded run: four families (a dispatch of a texture over K, a draw with
K as a vertex buffer, a blit copy from K, and a dispatch bound a call
before) against a rebinding thread. Each output must be wholly one page of
those K lay in meanwhile, or EncodedUseRebound.
Hot path: one Acquire load per encoder call (begin_call) plus one compare
under the lock the hold already takes. Per-call numbers follow in the bench
commit or report.
Also folded: the threaded run reports the pages its outputs read and each wrong output's reason
(060e8219f0); and a publish's close counts as a move (buffer::host_cells_settled), so a binding read
during the publish (a texel view for one) is not kept under the post-replace count and reused
by a later encoder that did not race; debug checks (check_reading, compute check_whole,
KeptSpaces::of) hold off where a call may have raced (rebind::call_may_have_raced, debug only)
(0c7a7a5c98).
Verified (lavapipe, msi): m3-max-37154 at 0c7a7a5c98: clippy plain and --cfg m2v_cpu_icd 0;
rebind*, pending_rebind, degradation*, argument_table, live_bind 41 passed; the hold and publish
controls raced 1 and completed; the threaded run (4 threads x 25 command buffers: a dispatch over
a texture over the buffer, a draw with it as vertex buffer 1, a blit from it, a dispatch bound a
call before) read every output wholly from one page, 0 EncodedUseRebound, 63 rebinds made, 1,537
refused (EncodedUses), 5 encoders raced, 11 encoded again. Negative control m3-max-37156 with
Entry::raced forced false: the hold control fails (the dispatch read the pages the buffer left).