From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: Fuad Tabba <fuad.tabba@linux.dev>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>,
kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Isaku Yamahata <isaku.yamahata@intel.com>,
Rick Edgecombe <rick.p.edgecombe@intel.com>,
Alexandru Elisei <alexandru.elisei@arm.com>,
Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
kvmarm@lists.linux.dev, Wei-Lin Chang <weilin.chang@arm.com>,
Will Deacon <will@kernel.org>,
stable@vger.kernel.org
Subject: Re: [PATCH v2 1/2] KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full
Date: Thu, 19 Nov 2026 11:18:48 +0000 [thread overview]
Message-ID: <av7S4fY7f6H_bW8t@gremlin> (raw)
In-Reply-To: <CA+EHjTzUrDxLSMEo6X66XhKtQ-=ew3zZhMdFWR6U9Ga17WLWkw@mail.gmail.com>
On Tue, Nov 17, 2026 at 05:05:49PM +0000, Fuad Tabba wrote:
> Hi Lorenzo,
>
> On Tue, 17 Nov 2026 14:23:09 +0000, "Lorenzo Stoakes (ARM)"
> <ljs@kernel.org> 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.
Probably worth referencing in commit msg I guess?
>
> > 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.
I mean generally yeah, users doing silly things can expect silly results I
guess! :)
>
> [...]
> > > 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.
Thanks!
>
> [...]
> > 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.
But Wei-Lin's fix was taken? So I don't think predicating things on the
basis of it not being there really makes any sense.
However...
>
> 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).
...Given that this possibility exists and device pages are unconditionally
marked writable (even if it's mildly contrived to get there), it's fair
enough to put it in the generic code.
>
> 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.
Yep that's fair enough!
>
> Cheers,
> /fuad
>
> [1] https://lore.kernel.org/all/avs5UD79FB_DG1Vs@seanjc-mac.roam.internal/
> [2] https://lore.kernel.org/all/20261029175238.3214788-2-weilin.chang@arm.com/
--
Cheers, Lorenzo
prev parent reply other threads:[~2026-11-19 11:18 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261117074428.7544-1-fuad.tabba@linux.dev>
2026-11-17 7:44 ` [PATCH v2 1/2] KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full Fuad Tabba
2026-11-17 14:23 ` Lorenzo Stoakes (ARM)
2026-11-17 17:05 ` Fuad Tabba
2026-11-19 0:25 ` Sean Christopherson
2026-11-19 7:54 ` Fuad Tabba
2026-11-19 11:20 ` Lorenzo Stoakes (ARM)
2026-11-19 11:18 ` Lorenzo Stoakes (ARM) [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=av7S4fY7f6H_bW8t@gremlin \
--to=ljs@kernel.org \
--cc=alexandru.elisei@arm.com \
--cc=corbet@lwn.net \
--cc=fuad.tabba@linux.dev \
--cc=isaku.yamahata@intel.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=pbonzini@redhat.com \
--cc=rick.p.edgecombe@intel.com \
--cc=seanjc@google.com \
--cc=skhan@linuxfoundation.org \
--cc=stable@vger.kernel.org \
--cc=weilin.chang@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox