From 74b66e2f3a6b35f1f142d35eed1b34132c19b34f Mon Sep 17 00:00:00 2001 From: Nikhil Unni Date: Mon, 6 Jul 2026 18:51:15 -0700 Subject: [PATCH] fix(vsock): kick() must not arm the RX gate on a plain vm resume MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit kick() runs on EVERY vm resume (Vmm::resume_vm -> kick_virtio_devices), not just the restore-resume it was written for, and it unconditionally armed pending_event_ack. On a PLAIN pause->resume (no snapshot anywhere in between) no TRANSPORT_RESET was queued, so the guest has nothing to ack and the gate never clears: every host->guest vsock delivery black-holes (process_rx short-circuits) and the event loop busy-spins at 100% CPU on the backend's undeliverable pending-RX (level-triggered epoll). Hit in production by ADR 0074 rung-2 parked-paused eviction, whose un-pause is exactly a plain PATCH /vm Paused -> Resumed cycle; also latent on the ADR 0045 admin pause/resume and migration-abort resume paths. Fix: kick() signals (evq + TX-notification replay) only when pending_event_ack is ALREADY armed, and never arms it itself — the two paths that queue a reset own the arming: prepare_save (in-process, the diff-checkpoint source resume) and now restore() (cross-snapshot, re-armed from the saved activation flag since the gate flag itself is not persisted; an activated snapshot always carries a queued reset because prepare_save runs on every save). Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_014jJi2vqAaxt3Q5UKxbe4Gx --- src/vmm/src/devices/virtio/vsock/device.rs | 74 +++++++++++++++++---- src/vmm/src/devices/virtio/vsock/persist.rs | 53 +++++++++++++++ 2 files changed, 113 insertions(+), 14 deletions(-) diff --git a/src/vmm/src/devices/virtio/vsock/device.rs b/src/vmm/src/devices/virtio/vsock/device.rs index d855523e3..4ae8d6826 100644 --- a/src/vmm/src/devices/virtio/vsock/device.rs +++ b/src/vmm/src/devices/virtio/vsock/device.rs @@ -403,9 +403,23 @@ where } fn kick(&mut self) { - if self.is_activated() { - self.pending_event_ack = true; - + // `kick()` runs on EVERY vm resume (`Vmm::resume_vm` -> `kick_virtio_devices`), + // which covers three distinct paths: + // - snapshot-restore resume: the source's `prepare_save` queued a + // `TRANSPORT_RESET_EVENT` into the (snapshotted) used ring, and `restore()` + // re-armed `pending_event_ack` from the saved activation flag; + // - post-snapshot source resume (diff checkpoints): `prepare_save` queued the + // reset and armed the gate in this very process; + // - PLAIN pause->resume (no snapshot anywhere): nothing was queued and nothing + // must be signaled. + // Arming the gate *here* used to break the third path: with no reset queued + // there is nothing for the guest to ack, so the gate never cleared — all + // host->guest RX black-holed and the event loop busy-spun on the undeliverable + // backend backlog (the ADR 0074 rung-2 parked-paused un-pause wedge). So: + // signal only when a reset is genuinely awaiting the guest's ack, and never arm + // from here — `prepare_save` (in-process) and `restore()` (cross-snapshot) own + // the arming. + if self.is_activated() && self.pending_event_ack { // Vsock has a complicated protocol that isn't resilient to any packet loss, // so for Vsock we don't support connection persistence through snapshot. Any // in-flight packets or events are simply lost and Vsock is restored 'empty'. @@ -618,39 +632,71 @@ mod tests { } #[test] - fn test_kick_when_active_arms_pending_event_ack() { - // Restore path: kick() is invoked after the snapshot is loaded to re-deliver the - // TRANSPORT_RESET interrupt. It must arm the RX gate so the post-restore RX/EVQ - // race cannot deliver data ahead of the guest ack. + fn test_kick_when_armed_keeps_gate_and_suppresses_rx() { + // Restore / post-snapshot-source resume: a TRANSPORT_RESET is awaiting the + // guest's ack (`pending_event_ack` was armed by prepare_save or restore()). + // kick() must re-signal while KEEPING the gate armed, so the post-restore + // RX/EVQ race cannot deliver data ahead of the guest ack. let test_ctx = TestContext::new(); let mut ctx = test_ctx.create_event_handler_context(); ctx.mock_activate(test_ctx.mem.clone(), test_ctx.interrupt.clone()); - ctx.device.pending_event_ack = false; + ctx.device.pending_event_ack = true; ctx.device.kick(); assert!( ctx.device.pending_event_ack, - "kick() on an active device must arm the RX gate" + "kick() must keep the RX gate armed while the reset awaits its ack" ); - // After kick(), the gate must actually suppress RX delivery. + // With the gate armed, RX delivery must stay suppressed. ctx.device.backend.set_pending_rx(true); let progressed = ctx.device.process_rx().unwrap(); assert!(!progressed); assert_eq!(ctx.guest_rxvq.used.idx.get(), 0); } + #[test] + fn test_kick_plain_resume_unarmed_is_a_noop() { + // PLAIN pause->resume (no snapshot anywhere): no TRANSPORT_RESET was queued, so + // kick() must neither arm the gate nor signal any queue. Arming here is the + // ADR 0074 rung-2 wedge: nothing for the guest to ack -> the gate never clears + // -> all host->guest RX black-holes and the event loop busy-spins. + let test_ctx = TestContext::new(); + let mut ctx = test_ctx.create_event_handler_context(); + ctx.mock_activate(test_ctx.mem.clone(), test_ctx.interrupt.clone()); + + assert!(!ctx.device.pending_event_ack); + ctx.device.kick(); + + assert!( + !ctx.device.pending_event_ack, + "kick() must never arm the RX gate itself" + ); + // No queue eventfd may be signaled on an unarmed kick (non-blocking read + // errors when the eventfd has no pending count). + ctx.device.queue_events[TXQ_INDEX].read().unwrap_err(); + ctx.device.queue_events[RXQ_INDEX].read().unwrap_err(); + ctx.device.queue_events[EVQ_INDEX].read().unwrap_err(); + + // And RX must still flow: the un-paused guest's connections are intact. + ctx.device.backend.set_pending_rx(true); + let progressed = ctx.device.process_rx().unwrap(); + assert!(progressed, "RX must not be gated after a plain resume"); + } + #[test] fn test_kick_replays_tx_notification_only() { - // On restore, kick() must replay only the TX data queue (to re-process in-flight - // TX and re-arm avail_event). RX is gated by pending_event_ack so it needs no - // replay, and the event queue's data eventfd must not be notified -- that is the - // guest's TRANSPORT_RESET ack path; the event queue is signaled host->guest. + // On restore (gate armed), kick() must replay only the TX data queue (to + // re-process in-flight TX and re-arm avail_event). RX is gated by + // pending_event_ack so it needs no replay, and the event queue's data eventfd + // must not be notified -- that is the guest's TRANSPORT_RESET ack path; the + // event queue is signaled host->guest. let test_ctx = TestContext::new(); let mut ctx = test_ctx.create_event_handler_context(); ctx.mock_activate(test_ctx.mem.clone(), test_ctx.interrupt.clone()); + ctx.device.pending_event_ack = true; ctx.device.kick(); // TX queue eventfd was replayed for re-processing. diff --git a/src/vmm/src/devices/virtio/vsock/persist.rs b/src/vmm/src/devices/virtio/vsock/persist.rs index 42909b58d..2ed7ade12 100644 --- a/src/vmm/src/devices/virtio/vsock/persist.rs +++ b/src/vmm/src/devices/virtio/vsock/persist.rs @@ -114,6 +114,15 @@ where vsock.acked_features = state.virtio_state.acked_features; vsock.avail_features = state.virtio_state.avail_features; vsock.device_state = DeviceState::Inactive; + // Every snapshot of an *activated* vsock device went through `prepare_save`, + // which queued a `TRANSPORT_RESET_EVENT` into the (snapshotted) used ring and + // armed the RX gate. The gate flag itself is not persisted, so re-arm it from + // the saved activation flag: the restore-side `kick()` then re-signals the evq + // and RX stays gated until the restored guest acks the reset. (The device is + // still `Inactive` here — activation is re-applied later by the device manager + // — so this must read the saved flag, not `is_activated()`.) A never-activated + // snapshot queued no reset: leave the gate disarmed or RX would gate forever. + vsock.pending_event_ack = state.virtio_state.activated; Ok(vsock) } } @@ -145,6 +154,50 @@ pub(crate) mod tests { } } + #[test] + fn test_restore_arms_rx_gate_iff_snapshot_was_activated() { + // An activated snapshot always carries a queued TRANSPORT_RESET (prepare_save + // runs on every save), so restore() must re-arm the RX gate for the guest to + // ack. A never-activated snapshot queued no reset — arming would gate RX + // forever. + let ctx = TestContext::new(); + + let mut state = VsockState { + backend: ctx.device.backend().save(), + frontend: ctx.device.save(), + }; + + // Unactivated snapshot -> gate stays disarmed. + assert!(!state.frontend.virtio_state.activated); + let restored = Vsock::restore( + VsockConstructorArgs { + mem: ctx.mem.clone(), + backend: TestBackend::new(), + }, + &state.frontend, + ) + .unwrap(); + assert!( + !restored.pending_event_ack, + "restore of a never-activated snapshot must not arm the RX gate" + ); + + // Activated snapshot -> gate re-armed. + state.frontend.virtio_state.activated = true; + let restored = Vsock::restore( + VsockConstructorArgs { + mem: ctx.mem.clone(), + backend: TestBackend::new(), + }, + &state.frontend, + ) + .unwrap(); + assert!( + restored.pending_event_ack, + "restore of an activated snapshot must re-arm the RX gate" + ); + } + #[test] fn test_persist_uds_backend() { let ctx = TestContext::new();