Let held_resume ask whether held work can go on from a published copy, not from a continuation another thread frees.

vk_exec_held_ready runs without sub_lock (vk_backend.c held_resume and submit_sync). It followed evq[s].head into the
continuation and read the wait in the continuation's private copy of the recording. Meanwhile another submitting
thread, inside vk_sync_submit, could run evo_resume and cont_free that continuation. A reused block then gave
vk_exec_event_met an event address pointing anywhere, so the unlocked reader could fault, not only read a stale state.

Now the holder of sub_lock publishes what the decision needs into a per-queue sequence lock (runtime/vk_evo_peek.h),
wherever a queue's first continuation, or that continuation's wait or stop, changes: in cont_push when the pushed one
becomes the first, in evo_resume after the head moves on, and after cont_walk stops at another wait. The copy holds
the wait by value. vk_exec_held_ready decides from a copy taken from one publish. It never waits for the writer:
after three tries that overlap a publish it answers "not ready", which is what losing that race answered before.
This fixes F-1-06 without the front-end lock. F-2-01's lock is still the answer for the other unlocked front-end
state.

Not reproduced before the change. Checks that passed: the new tests/evo_peek_check (one writer publishing 1,000,000
times while three readers copy: no copy mixed two publishes, and the decision cont_ready makes, with a stand-in for
vk_exec_event_met; with the sequence check removed it reports over a million mixed copies), pass_cc_check on every
runtime file (a header was added), no new -Wall -Wextra warnings in vk_exec.c, target_frames_check, oq_hold_check,
sync_hw_check, mesh_path_check and gpa_check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:07:34 -05:00
parent b755cdc536
commit 688b250673
4 changed files with 282 additions and 2 deletions

View File

@@ -26,6 +26,7 @@
#include "tex.h"
#include "vk.h"
#include "vk_exec.h"
#include "vk_evo_peek.h"
#include "vk_target_frames.h"
#include "vk_dlaa.h"
@@ -1277,6 +1278,14 @@ typedef struct Cont {
uint8_t *st;
} Cont;
static struct { uintptr_t q; Cont *head, *tail; } evq[EVO_QUEUES];
// What vk_exec_held_ready needs of each queue's first continuation, as a copy (vk_evo_peek.h): it runs without the
// submission lock (vk_sync.c sub_lock) while another thread may free that continuation (evo_resume). Published under
// that lock by peek_publish wherever evq[s].head, or the head's at or since, changes.
static EvoPeek evq_peek[EVO_QUEUES];
_Static_assert(sizeof(EvoWait) == sizeof(p_wait_event) && offsetof(EvoWait, val) == offsetof(p_wait_event, val) &&
offsetof(EvoWait, mode) == offsetof(p_wait_event, mode) &&
offsetof(EvoWait, value) == offsetof(p_wait_event, value),
"vk_evo_peek.h EvoWait must have p_wait_event's layout");
static int evo_on = -1, evo_in; // knob; inside a continuation
static uintptr_t evo_queue; // queue of the stream being walked (0: it cannot stop at a wait)
static int evo_pending; // continuations held
@@ -1412,11 +1421,24 @@ static void cont_free(Cont *c) {
free(c->h), free(c->st), free(c);
}
// Publishes queue s's first continuation for vk_exec_held_ready (evq_peek): whether one is held, the wait it stopped
// at, copied out of its private recording, and when it stopped.
static void peek_publish(int s) {
const Cont *c = evq[s].head;
EvoPeekCopy v = {0};
if (c) {
v.has = 1, v.since = c->since;
if (c->at) v.has_at = 1, v.w = *(const EvoWait *)nvn_op_payload(c->at);
}
evo_peek_publish(&evq_peek[s], &v);
}
static void cont_push(int slot, Cont *c) {
if (evq[slot].tail) evq[slot].tail->next = c;
else evq[slot].head = c;
evq[slot].tail = c;
evo_pending++;
if (evq[slot].head == c) peek_publish(slot);
}
// Swaps the queue state in ex with buf (ST_HI - ST_LO bytes); what the swapped-in state names is bound again.
@@ -1501,9 +1523,14 @@ void vk_exec_gate_wait(const uint64_t *handles, int n) {
}
}
// Reads only the published copies (evq_peek): vk_backend.c held_resume and submit_sync call this without the
// submission lock. A copy that overlapped a publish on every try counts as not ready, as losing that race did before.
int vk_exec_held_ready(void) {
for (int s = 0; evo_pending && s < EVO_QUEUES; s++)
if (evq[s].head && cont_ready(evq[s].head)) return 1;
for (int s = 0; evo_pending && s < EVO_QUEUES; s++) {
EvoPeekCopy c;
if (evo_peek_read(&evq_peek[s], &c) && evo_peek_ready(&c, vk_exec_event_met, vk_exec_submit_seq, CONT_MAX))
return 1;
}
return 0;
}
@@ -1566,6 +1593,7 @@ static void evo_resume(VkExec *ex) {
evo_resumed++;
vk_exec_evt('r', (const void *)evq[s].q, 0);
if (cont_walk(ex, c)) { // waits again: the queue's state stays with it
peek_publish(s);
vk_exec_evt('w', NULL, 0);
vk_exec_pass_end(ex);
if (!c->st && !(c->st = malloc(ST_HI - ST_LO))) rt_fatal("vk: out of memory (queue state)");
@@ -1575,6 +1603,7 @@ static void evo_resume(VkExec *ex) {
}
evq[s].head = c->next; // done: the queue's next submission starts with the state this one left
if (!evq[s].head) evq[s].tail = NULL;
peek_publish(s); // the next one, or none; the copy names nothing inside c
evo_pending--;
vk_exec_evt('d', NULL, 0);
cont_free(c);