Re: [PATCH v2 1/2] KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full

From: Fuad Tabba

Date: Tue Nov 17 2026 - 12:06:36 EST


Hi Lorenzo,

On Tue, 17 Nov 2026 14:23:09 +0000, "Lorenzo Stoakes (ARM)"
<ljs@xxxxxxxxxx> wrote:
[...]
> > The check also sets run->exit_reason, and arm64's KVM_RUN completes a
> > pending MMIO exit only if exit_reason is still KVM_EXIT_MMIO. If a
> > pre-fault stops on a full ring while an MMIO exit is pending, the next
> > KVM_RUN doesn't complete the exit and the guest repeats the MMIO
> > access. Userspace can avoid that by completing the exit with KVM_RUN
> > before it pre-faults.
>
> That's also true for riscv:
>
> switch (run->exit_reason) {
> case KVM_EXIT_MMIO:
> /* Process MMIO value returned from user-space */
> ret = kvm_riscv_vcpu_mmio_return(vcpu, vcpu->run);
> break;
> case KVM_EXIT_RISCV_SBI:
> /* Process SBI value returned from user-space */
> ret = kvm_riscv_vcpu_sbi_return(vcpu, vcpu->run);
> break;
> case KVM_EXIT_RISCV_CSR:
> /* Process CSR value returned from user-space */
> ret = kvm_riscv_vcpu_csr_return(vcpu, vcpu->run);
> break;
> }
>
> (note that it skips CSR, SBI also with a changed exit_reason).

Ack.

> But the overflow bug only applies to x86 (see below), so is this something
> we should worry about at all?

The ring overflows on arm64 too (below). As for exit_reason, I asked
Sean about the arm64 MMIO case on v1, and his answer was that a VMM
that pre-faults between a KVM_EXIT_MMIO return and the next KVM_RUN
"gets to deal with the fallout" [1]. I think the same goes for riscv's
exits.

[...]
> > diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
[...]
> > The dirty ring can get full. When it happens, the KVM_RUN of the
> > -vcpu will return with exit reason KVM_EXIT_DIRTY_RING_FULL.
> > +vcpu will return with exit reason KVM_EXIT_DIRTY_RING_FULL, and
> > +KVM_PRE_FAULT_MEMORY on the vcpu will process no more pages until the
> > +ring has been harvested and reset.
>
> Maybe worth mentioning the exit_reason thing if we keep this arch-generic?

Sure, I'll document that userspace should complete a pending exit with
KVM_RUN before it pre-faults on that vCPU.

[...]
> I wonder if this shouldn't just be an x86-only fix, since no other arch is
> impacted AFAICT.
>
> arm64 and riscv never map a dirty-logged memslot writable, so they're never
> pushed to the rin, and s390 doesn't have a dirty ring at all:
>
> arm64:
>
> static int kvm_s2_fault_compute_prot(const struct kvm_s2_fault_desc *s2fd,
> const struct kvm_s2_fault_vma_info *s2vi,
> enum kvm_pgtable_prot *prot)
> {
> ...
> if (s2vi->map_writable && (s2vi->device ||
> !memslot_is_logging(s2fd->memslot) ||
> kvm_s2_fault_is_write(s2fd)))
> *prot |= KVM_PGTABLE_PROT_W;
> ...
> }

arm64 does map a logged slot as writable today. memslot_is_logging()
tests memslot->dirty_bitmap, which isn't allocated when only the dirty
ring is enabled, and patch 2's selftest triggers the WARN in
kvm_dirty_ring_push() on arm64 without patch 1. Wei-Lin's fix to
memslot_is_logging() [2] is in kvmarm/fixes, not in mainline yet.

With it, the s2vi->device case above still maps a device page writable
on a read fault in a logged slot. kvm_arch_prepare_memory_region()
rejects logging on a VM_PFNMAP VMA, so that requires userspace to map
the device under the slot after creating it (I haven't tested it).

So I think the check should be in the generic loop.

[...]
> An x86-only version would be pretty simple, something like the below.
>
> This also covers off the retry (RET_PF_RETRY) loop which a generic version
> doesn't.

True. To overflow there, the retries for one page would have to push
more entries than the ring has in reserve (at least 64), and I don't
see a way to get that, so I left it alone.

Cheers,
/fuad

[1] https://lore.kernel.org/all/avs5UD79FB_DG1Vs@xxxxxxxxxxxxxxx.internal/
[2] https://lore.kernel.org/all/20261029175238.3214788-2-weilin.chang@xxxxxxx/