Skip to content

virtio: reduce guest memory accesses on queue hot path#3151

Open
jstarks wants to merge 2 commits intomicrosoft:mainfrom
jstarks:virtio-delay
Open

virtio: reduce guest memory accesses on queue hot path#3151
jstarks wants to merge 2 commits intomicrosoft:mainfrom
jstarks:virtio-delay

Conversation

@jstarks
Copy link
Copy Markdown
Member

@jstarks jstarks commented Mar 28, 2026

The virtio queue implementation was doing expensive guest memory writes (kick arming/suppression) on every call to is_available(), even when the device was just draining a batch and had no intention of sleeping. This added unnecessary overhead on the hot path, particularly for high-throughput devices like virtio-net.

Restructure notification handling so that is_available() is a lightweight read-only check, kick arming is deferred to the actual sleep boundary (poll_kick), and suppression happens lazily when work is found while armed. The split queue caches avail_index to drain batches with a single read, and the packed queue reads only the 2-byte flags field instead of the full 16-byte descriptor for availability checks.

Burette TCP tx throughput improves by 31% on nested KVM, UDP tx by 48%. TCP rx is unchanged.

The virtio queue implementation was doing expensive guest memory writes
(kick arming/suppression) on every call to is_available(), even when
the device was just draining a batch and had no intention of sleeping.
This added unnecessary overhead on the hot path, particularly for
high-throughput devices like virtio-net.

Restructure notification handling so that is_available() is a
lightweight read-only check, kick arming is deferred to the actual
sleep boundary (poll_kick), and suppression happens lazily when work
is found while armed. The split queue caches avail_index to drain
batches with a single read, and the packed queue reads only the 2-byte
flags field instead of the full 16-byte descriptor for availability
checks.

Burette TCP tx throughput improves by 31% on nested KVM, UDP tx by
48%. TCP rx is unchanged.
@jstarks jstarks requested a review from a team as a code owner March 28, 2026 22:18
Copilot AI review requested due to automatic review settings March 28, 2026 22:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes virtio queue hot-path performance by restructuring notification (kick) arming/suppression so that availability checks avoid unnecessary guest-memory writes, reducing overhead for high-throughput virtio devices.

Changes:

  • Refactors split-queue availability checks to cache avail_index and defer kick arming to the sleep boundary.
  • Refactors packed-queue availability checks to read only the descriptor flags (instead of the full descriptor) and adds explicit kick arm/suppress APIs.
  • Introduces core-level tracking of whether kicks are currently armed, and updates poll_kick() to arm only when actually sleeping.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
vm/devices/virtio/virtio_spec/src/lib.rs Adds/clarifies documentation for packed-queue event suppression flags.
vm/devices/virtio/virtio/src/queue/split.rs Caches avail_index and splits “availability” from “arm/suppress kicks”.
vm/devices/virtio/virtio/src/queue/packed.rs Makes is_available() a lightweight flags-only read; adds arm/suppress kick methods.
vm/devices/virtio/virtio/src/queue.rs Tracks armed state in QueueCoreGetWork and suppresses kicks lazily when work is found.
vm/devices/virtio/virtio/src/common.rs Changes poll_kick() to arm for kick only at the sleep boundary.

/// `None` is returned, the caller must call [`arm_kick`](Self::arm_kick)
/// before sleeping to ensure the guest will send a kick when new work
/// arrives.
pub fn is_available(&mut self) -> Result<Option<u16>, QueueError> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there value in returning something other than an option to make this behavior more explicit? This sounds like a bug waiting to happen, IE caller forgot to call arm_kick.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is the intention that most callers should not call this directly, and use the poll wrappers in common.rs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants