Skip to contentSkip to conversation

KVM: x86: Use gfn_to_pfn_cache for record_steal_time

PATCH New David Woodhouse 1 participant 1 message Mar 18

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 KVM: x86: Use gfn_to_pfn_cache for record_steal_time

From: Carsten Stollmaier <[email protected]>

This largely reverts commit 7e2175ebd695 ("KVM: x86: Fix recording of guest steal time / preempted status"), which dropped the use of the gfn_to_pfn_cache because it was not integrated with the MMU notifiers at the time. That shortcoming has long since been addressed, making the GPC work correctly for this use case.

Aside from cleaning up the last open-coded assembler access to user
addresses and associated explicit asm exception fixups, moving back to the now-functional GPC also resolves an issue with contention on the mmap_lock with userfaultfd. The contention issue is as follows:

On vcpu_run, before entering the guest, the update of the steal time information causes a page-fault if the page is not present. In our scenario, this gets handled by do_user_addr_fault() and successively handle_userfault() because the region is registered to that.

Since handle_userfault() uses TASK_INTERRUPTIBLE, it is interruptible by signals. But do_user_addr_fault() then busy-retries if the pending signal is non-fatal, which leads to heavy contention of the mmap_lock.

By restoring the use of GPC for accessing the guest steal time, the contention is avoided and refreshing the GPC happens when the vCPU is next scheduled.

Since the gfn_to_pfn_cache gives a kernel mapping rather than a userspace HVA, accesses are now plain C instead of unsafe_put_user() et al. Use READ_ONCE()/WRITE_ONCE() to prevent the compiler from reordering or tearing the accesses, and add an smp_wmb() before the final version increment to ensure the data writes are ordered before the seqcount update — the old unsafe_put_user() inline assembly acted as an implicit compiler barrier.

In kvm_steal_time_set_preempted(), use read_trylock() instead of read_lock_irqsave() since this is called from the scheduler path where rwlock_t is not safe on PREEMPT_RT (it becomes sleepable). Since we only trylock and bail on failure, there is no risk of deadlock with an interrupt handler, so no need to disable interrupts at all. Setting the preempted flag is best-effort anyway.

---

v3: Add READ_ONCE/WRITE_ONCE and barriers, use read_trylock() to make
PREEMPT_RT happy (where rwlock_t is sleepable). Tested by extending the existing steal_time KVM selftest to trigger lockdep unhappiness with v2 when it's actually preempted, and validating that the problem goes away with v3.

v2: Rebase and repost.

 arch/x86/include/asm/kvm_host.h |   2 +-
 arch/x86/kvm/x86.c              | 126 ++++++++++++++++----------------
 2 files changed, 66 insertions(+), 62 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index ff07c45e3c73..5eb38976af58 100644
@@ -958,7 +958,7 @@ struct kvm_vcpu_arch {
 		u8 preempted;
 		u64 msr_val;
 		u64 last_steal;
-		struct gfn_to_hva_cache cache;
+		struct gfn_to_pfn_cache cache;
 	} st;
 
 	u64 l1_tsc_offset;
arch/x86/kvm/x86.c +65 -61

214 lines not on this page · raw message

Signature · 1 line
2.51.0