linux/arch/x86/kvm/mmu
Sean Christopherson 0df9dab891 KVM: x86/mmu: Stop zapping invalidated TDP MMU roots asynchronously
Stop zapping invalidate TDP MMU roots via work queue now that KVM
preserves TDP MMU roots until they are explicitly invalidated.  Zapping
roots asynchronously was effectively a workaround to avoid stalling a vCPU
for an extended during if a vCPU unloaded a root, which at the time
happened whenever the guest toggled CR0.WP (a frequent operation for some
guest kernels).

While a clever hack, zapping roots via an unbound worker had subtle,
unintended consequences on host scheduling, especially when zapping
multiple roots, e.g. as part of a memslot.  Because the work of zapping a
root is no longer bound to the task that initiated the zap, things like
the CPU affinity and priority of the original task get lost.  Losing the
affinity and priority can be especially problematic if unbound workqueues
aren't affined to a small number of CPUs, as zapping multiple roots can
cause KVM to heavily utilize the majority of CPUs in the system, *beyond*
the CPUs KVM is already using to run vCPUs.

When deleting a memslot via KVM_SET_USER_MEMORY_REGION, the async root
zap can result in KVM occupying all logical CPUs for ~8ms, and result in
high priority tasks not being scheduled in in a timely manner.  In v5.15,
which doesn't preserve unloaded roots, the issues were even more noticeable
as KVM would zap roots more frequently and could occupy all CPUs for 50ms+.

Consuming all CPUs for an extended duration can lead to significant jitter
throughout the system, e.g. on ChromeOS with virtio-gpu, deleting memslots
is a semi-frequent operation as memslots are deleted and recreated with
different host virtual addresses to react to host GPU drivers allocating
and freeing GPU blobs.  On ChromeOS, the jitter manifests as audio blips
during games due to the audio server's tasks not getting scheduled in
promptly, despite the tasks having a high realtime priority.

Deleting memslots isn't exactly a fast path and should be avoided when
possible, and ChromeOS is working towards utilizing MAP_FIXED to avoid the
memslot shenanigans, but KVM is squarely in the wrong.  Not to mention
that removing the async zapping eliminates a non-trivial amount of
complexity.

Note, one of the subtle behaviors hidden behind the async zapping is that
KVM would zap invalidated roots only once (ignoring partial zaps from
things like mmu_notifier events).  Preserve this behavior by adding a flag
to identify roots that are scheduled to be zapped versus roots that have
already been zapped but not yet freed.

Add a comment calling out why kvm_tdp_mmu_invalidate_all_roots() can
encounter invalid roots, as it's not at all obvious why zapping
invalidated roots shouldn't simply zap all invalid roots.

Reported-by: Pattara Teerapong <pteerapong@google.com>
Cc: David Stevens <stevensd@google.com>
Cc: Yiwei Zhang<zzyiwei@google.com>
Cc: Paul Hsia <paulhsia@google.com>
Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-Id: <20230916003916.2545000-4-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2023-09-23 05:35:48 -04:00
..
mmu_internal.h KVM: x86/mmu: Stop zapping invalidated TDP MMU roots asynchronously 2023-09-23 05:35:48 -04:00
mmu.c KVM: x86/mmu: Stop zapping invalidated TDP MMU roots asynchronously 2023-09-23 05:35:48 -04:00
mmutrace.h KVM: x86/mmu: Add RET_PF_CONTINUE to eliminate bool+int* "returns" 2022-05-12 09:51:42 -04:00
page_track.c KVM: x86/mmu: Handle KVM bookkeeping in page-track APIs, not callers 2023-08-31 14:08:19 -04:00
page_track.h KVM: x86/mmu: Drop @slot param from exported/external page-track APIs 2023-08-31 14:08:18 -04:00
paging_tmpl.h KVM: x86/mmu: Use dummy root, backed by zero page, for !visible guest roots 2023-08-31 14:08:24 -04:00
spte.c KVM: x86/mmu: Convert "runtime" WARN_ON() assertions to WARN_ON_ONCE() 2023-08-31 13:48:44 -04:00
spte.h KVM: x86/mmu: Include mmu.h in spte.h 2023-08-31 14:08:25 -04:00
tdp_iter.c KVM: x86/mmu: Harden TDP MMU iteration against root w/o shadow page 2023-08-31 14:08:22 -04:00
tdp_iter.h KVM: x86/mmu: Atomically clear SPTE dirty state in the clear-dirty-log flow 2023-04-04 12:37:30 -07:00
tdp_mmu.c KVM: x86/mmu: Stop zapping invalidated TDP MMU roots asynchronously 2023-09-23 05:35:48 -04:00
tdp_mmu.h KVM: x86/mmu: Stop zapping invalidated TDP MMU roots asynchronously 2023-09-23 05:35:48 -04:00