Notes about KVM coalesced MMIO/PIO
本文将mark下KVM coalesced MMIO/PIO相关notes。
背景
https://elixir.bootlin.com/linux/v6.0/source/virt/kvm
在KVM源码中经常看到coalesced MMIO,本文将一探究竟。当然本文只侧重于high level层面,不涉及源码中的细节。
Motivation
When kernel has to send MMIO writes to userspace, it stores them in memory until it has to pass the hand to userspace for another reason. This avoids to have too many context switches on operations that can wait.
Coalesced I/O is used if one or more write accesses to a hardware register can be deferred until a read or a write to another hardware register on the same device. This last access will cause a vmexit and userspace will process accesses from the ring buffer before emulating it. That will avoid exiting to userspace on repeated writes.
Without KVM coalesced MMIO/PIO
- 可deferred的MMIO/PIO write导致Non-root mode VM Exit -> KVM -> QEMU(处理可deferred的MMIO/PIO write) -> KVM -> 返回到Non-root mode
With KVM coalesced MMIO/PIO
- 可deferred的MMIO/PIO write导致Non-root mode VM Exit -> KVM(将可deferred的MMIO/PIO write记录到ring buffer) -> 返回到Non-root mode
- 不可deferred的MMIO/PIO导致Non-root mode VM Exit -> KVM -> QEMU(先处理完ring buffer中记录的可deferred的MMIO/PIO write,再处理这次不可deferred的MMIO/PIO) -> KVM -> 返回到Non-root mode
从上述的对比可知,KVM coalesced MMIO/PIO可以在KVM中记录可deferred的MMIO/PIO write,不用退出的qemu来处理,可以将这些请求dealy到下一次不可deferred的MMIO/PIO来处理。That will avoid exiting to userspace on repeated writes.
API
1 | 4.116 KVM_(UN)REGISTER_COALESCED_MMIO |
QEMU example
1 | // https://gitlab.com/qemu-project/qemu/-/blob/stable-6.0/hw/net/e1000.c#L1631-L1647 |
本case的memory_region_add_coalescing
注册了coalesced MMIO region。对于细节,需要结合e1000的spec与qemu、kvm相关代码了,在此不再描述。
参考资料: