🐛 Park the device before each doorbell answer a test asserts

The tests that assert a doorbell's thread answers a landed download or a
command buffer wrote the doorbell as soon as they saw the answer before it,
`asleep` or `consumer_idle`. The doorbell's paths only try their locks, and
each of those signs comes before its thread lets go: a ring-0 thread posts
its answer and then takes the lanes' lock twice more before it waits for a
lane, the control thread sets `asleep` before it looks at its wake, and a
queue's thread sets `consumer_idle` before it sleeps. Under load the write
found a lock held and left the message to its thread, still in order:
a_landed_download_is_answered_on_its_doorbell_s_thread failed once on the B92
XTX leg (um790), and an_answered_command_buffer_waits_for_no_later_ring_0_message failed once in 2000 runs beside CPU spinners on um870 (m3-max-31819).

`Device::parked` now also holds each ring-0 thread inside its wait for a
lane (a count the tests keep in the lanes), and each test waits for it before
such a write; the test of a buffer behind a held create waits for the device
parked before the create, then for the control thread parked past its hand.
The device is unchanged.
This commit is contained in:
Anees Iqbal committed 2026-11-15 06:10:35 -08:00
1 parent 75f61c05e4
commit 63df7c3959
4 files changed
+76 -13

No files matched your search

+15 -5
View File
@@ -1308,14 +1308,24 @@ impl Device {
self.shared.queues.lock().by_obj.len()
}
/// Whether the control thread and every queue's service thread sleep with no wake pending
/// ([`Wake::parked`]), and no lane has a message to carry out: what they write of the rings
/// stays as it is until the guest acts.
/// Whether the control thread sleeps with no wake pending ([`Wake::parked`]): it takes no lock
/// until it is woken.
#[cfg(test)]
pub(crate) fn parked(&self) -> bool {
pub(crate) fn control_parked(&self) -> bool {
let doorbell = self.shared.doorbell.get().map(wake::raw);
self.shared.wake.parked(doorbell)
&& self.shared.lanes.idle()
}
/// Whether the control thread and every queue's service thread sleep with no wake pending
/// ([`Wake::parked`]), no lane has a message to carry out, and each of ring 0's threads waits
/// for a lane with the lanes' lock let go ([`Lanes::parked`]): what they write of the rings
/// stays as it is until the guest acts, and until then none takes a lock that a doorbell's
/// thread tries ([`Shared::download_on_doorbell`], [`Shared::answer_on_doorbell`]) but a ring-0
/// thread that has waited [`crate::keep::KEEP`] for a lane, and goes.
#[cfg(test)]
pub(crate) fn parked(&self) -> bool {
self.control_parked()
&& self.shared.lanes.parked()
&& self
.shared
.queues
@@ -332,6 +332,10 @@ struct State {
/// Ring 0's threads take no job while set ([`Lanes::hold_threads`], the tests).
#[cfg(test)]
held: bool,
/// How many of ring 0's threads wait for a lane inside [`Lanes::park`]'s wait, the lanes'
/// lock let go ([`Lanes::parked`], the tests).
#[cfg(test)]
parked: usize,
}
impl State {
@@ -964,7 +968,15 @@ impl Lanes {
}
return None;
}
#[cfg(test)]
{
state.parked += 1;
}
kept = self.keep.wait(&wake, &mut state, since);
#[cfg(test)]
{
state.parked -= 1;
}
}
}
@@ -1088,6 +1100,20 @@ impl Lanes {
.values()
.all(|lane| lane.unfinished(epoch).next().is_none())
}
/// Whether no lane has a job not yet carried out and each of ring 0's threads waits for a
/// lane inside [`Self::park`]'s wait, the lanes' lock let go: none takes the lock again until
/// a lane is given to it or it has waited [`KEEP`] (the tests). [`Self::idle`] holds from a
/// thread's [`Self::finished`] of its lane's last job, before the thread posts the job's
/// answer, and the thread takes the lock twice more before it waits ([`Self::next`], then
/// [`Self::park`]).
#[cfg(test)]
pub(super) fn parked(&self) -> bool {
let state = self.state.lock();
let epoch = state.epoch;
state.parked == state.threads.iter().flatten().count()
&& (state.lanes.values()).all(|lane| lane.unfinished(epoch).next().is_none())
}
}
/// Ring 0's threads, as [`Lanes::threads`] counts them (the tests).
+31 -8
View File
@@ -3418,6 +3418,7 @@ fn a_doorbell_write_answers_a_command_buffer_on_its_thread() {
0,
&[command_buffer(QUEUE, 100, 1), command_buffer(QUEUE, 101, 2)],
);
wait_parked(&rig);
rig.device
.mmio_write_taking(RGFX_REG_DOORBELL.into(), QUEUE_SUBMIT as u32);
let here = thread_name();
@@ -3487,6 +3488,7 @@ fn an_answered_command_buffer_waits_for_no_later_ring_0_message() {
(region.atomic_u64(RGFX_CONTROL_SUBMIT_OFFSET)).store(produced, Ordering::SeqCst);
}));
publish_unrung(&rig, 0, &[command_buffer(QUEUE, 100, 1)]);
wait_parked(&rig);
rig.device
.mmio_write_taking(RGFX_REG_DOORBELL.into(), QUEUE_SUBMIT as u32);
assert_eq!(*taking.answered.lock(), HashMap::from([(1, true)]));
@@ -3608,10 +3610,14 @@ fn a_doorbell_write_answers_a_command_buffer_behind_another_process_s_create() {
taking.takes.store(true, Ordering::SeqCst);
let mut rig = taking_rig(&taking, QUEUE);
taking.hold_creates.store(true, Ordering::SeqCst);
wait_parked(&rig);
rig.send(object_message(RGFX_CMD_BUFFER_CREATE, OTHER));
let created = |taking: &Taking| taking.handed.lock().last() == Some(&RGFX_CMD_BUFFER_CREATE);
eventually(|| created(&taking));
assert!(created(&taking), "the create never reached the host");
// The create's lane's thread waits in the host, holding no lock of the device's; the rest
// parked before the create, and the control thread sleeps again past its hand of it.
wait_control_parked(&rig);
publish_unrung(&rig, 0, &[command_buffer(QUEUE, 100, 1)]);
rig.device
.mmio_write_taking(RGFX_REG_DOORBELL.into(), QUEUE_SUBMIT as u32);
@@ -3725,6 +3731,7 @@ fn only_an_asking_doorbell_write_is_answered_at_the_doorbell() {
wait_queue_idle(&rig);
publish_unrung(&rig, at, &[command_buffer(QUEUE, 101, 2)]);
wait_parked(&rig);
rig.device.mmio_write_taking(
RGFX_REG_DOORBELL.into(),
QUEUE_SUBMIT as u32 | RGFX_DOORBELL_ASKS,
@@ -3758,6 +3765,7 @@ fn a_guest_without_doorbell_asks_is_answered_at_any_doorbell_and_not_routed() {
assert!(taking.routes.lock().is_empty(), "a route was asked");
publish_unrung(&rig, 0, &[command_buffer(QUEUE, 100, 1)]);
wait_parked(&rig);
rig.device
.mmio_write_taking(RGFX_REG_DOORBELL.into(), QUEUE_SUBMIT as u32);
assert_eq!(*taking.answered.lock(), HashMap::from([(1, true)]));
@@ -4795,14 +4803,29 @@ fn a_download_behind_its_block_s_pages_on_the_lane_is_carried_out_after_them_the
}
/// Waits, [`PATIENCE`] polls at most, for the control thread to sleep with nothing in flight
/// (`Submit::asleep`).
fn wait_control_asleep(rig: &Rig) {
/// (`Submit::asleep`) and no wake pending ([`Device::control_parked`]): it takes no lock a
/// doorbell's thread tries until it is woken. `asleep` alone is set before the thread's look at
/// its wake, which may find one pending and wake it at once, its ring then held.
fn wait_control_parked(rig: &Rig) {
assert!(
eventually(|| rig.device.shared.submit.lock().asleep),
eventually(|| rig.device.shared.submit.lock().asleep && rig.device.control_parked()),
"the control thread did not go to sleep"
);
}
/// Waits, [`PATIENCE`] polls at most, for the control thread to sleep with nothing in flight
/// (`Submit::asleep`) and every thread of the device's to be parked ([`Device::parked`]): none
/// takes a lock a doorbell's thread tries until the test acts, so the doorbell's thread finds each
/// free. A lane's thread posts its answer, then takes the lanes' lock twice more before it waits
/// for a lane ([`Lanes::parked`]), and the control thread sets `asleep` before its look at its
/// wake: a write made as soon as an answer or `asleep` is seen may find a lock held, under load.
fn wait_parked(rig: &Rig) {
assert!(
eventually(|| rig.device.shared.submit.lock().asleep && rig.device.parked()),
"the device's threads did not park"
);
}
/// Publishes `command` on the control ring, its seqno the next, without a doorbell: the seqno.
fn publish_next(rig: &mut Rig, mut command: Vec<u8>) -> u64 {
rig.seqno += 1;
@@ -4834,7 +4857,7 @@ fn a_landed_download_is_answered_on_its_doorbell_s_thread() {
let pages = rig.send(surface_pages_message(5, BLOCK, RGFX_SURFACE_PAGES_CACHED));
assert_eq!(rig.complete(), (pages, RGFX_STATUS_OK));
wait_block_lane_idle(&rig, BLOCK);
wait_control_asleep(&rig);
wait_parked(&rig);
let download = publish_next(&mut rig, download_message(BLOCK));
rig.device
.mmio_write_taking(RGFX_REG_DOORBELL.into(), RGFX_CONTROL_RING_ID);
@@ -4850,7 +4873,7 @@ fn a_landed_download_is_answered_on_its_doorbell_s_thread() {
// plain write.
let present = rig.send(present(0));
assert_eq!(rig.complete(), (present, RGFX_STATUS_OK));
wait_control_asleep(&rig);
wait_parked(&rig);
let plain = publish_next(&mut rig, download_message(BLOCK));
rig.device
.mmio_write(RGFX_REG_DOORBELL.into(), RGFX_CONTROL_RING_ID);
@@ -4872,7 +4895,7 @@ fn a_doorbell_s_thread_leaves_a_download_the_control_thread_must_take() {
let control = Some("reims-vgpu.graphics-control".to_owned());
// Not carried out yet.
wait_control_asleep(&rig);
wait_parked(&rig);
let first = publish_next(&mut rig, download_message(BLOCK));
rig.device
.mmio_write_taking(RGFX_REG_DOORBELL.into(), RGFX_CONTROL_RING_ID);
@@ -4881,7 +4904,7 @@ fn a_doorbell_s_thread_leaves_a_download_the_control_thread_must_take() {
// Behind a present at the head.
seen.at_doorbell.store(true, Ordering::SeqCst);
wait_control_asleep(&rig);
wait_parked(&rig);
let shown = publish_next(&mut rig, present(0));
let behind = publish_next(&mut rig, download_message(BLOCK));
rig.device
@@ -4894,7 +4917,7 @@ fn a_doorbell_s_thread_leaves_a_download_the_control_thread_must_take() {
// The control thread awake in a download it was offered: the next waits for it.
seen.at_doorbell.store(false, Ordering::SeqCst);
seen.hold_here.store(true, Ordering::SeqCst);
wait_control_asleep(&rig);
wait_parked(&rig);
let held = rig.send(download_message(BLOCK));
eventually(|| !rig.device.shared.submit.lock().asleep);
settle();
@@ -130,6 +130,7 @@ fn a_doorbell_page_wakes_only_the_rings_of_the_context_it_is_bound_to() {
// Its own context's ring: taken, and answered on the writing thread.
let first = publish_unrung(&rig, 0, &[command_buffer(QUEUE, 100, 1)]);
wait_parked(&rig);
rig.device.doorbell_write(page_at(3), QUEUE_SUBMIT as u32);
assert_eq!(taking.answered.lock().get(&1), Some(&true));
assert_eq!(
@@ -166,6 +167,7 @@ fn a_doorbell_page_wakes_only_the_rings_of_the_context_it_is_bound_to() {
rig.device.doorbell_write(page_at(3), QUEUE_SUBMIT as u32);
assert_eq!(dropped(&rig), 3);
assert!(!taking.answered.lock().contains_key(&2));
wait_parked(&rig);
rig.device.doorbell_write(page_at(3), OTHER_SUBMIT as u32);
assert_eq!(taking.answered.lock().get(&7), Some(&true));
assert!(eventually(|| consumer(&rig, OTHER_SUBMIT) > 0));
@@ -186,6 +188,7 @@ fn a_doorbell_page_wakes_only_the_rings_of_the_context_it_is_bound_to() {
);
// Its own doorbell takes the buffer the dropped writes left.
wait_parked(&rig);
rig.device
.mmio_write_taking(RGFX_REG_DOORBELL.into(), QUEUE_SUBMIT as u32);
assert_eq!(taking.answered.lock().get(&2), Some(&true));
@@ -240,6 +243,7 @@ fn without_doorbell_pages_the_doorbells_are_the_register_s() {
assert_eq!(*taking.routed_at.lock(), vec![DoorbellAt::Register]);
publish_unrung(&rig, 0, &[command_buffer(QUEUE, 100, 1)]);
wait_parked(&rig);
rig.device.mmio_write_taking(
RGFX_REG_DOORBELL.into(),
QUEUE_SUBMIT as u32 | RGFX_DOORBELL_ASKS,