Skip to contentSkip to conversation

KVM: x86: Use gfn_to_pfn_cache for record_steal_time

PATCH Superseded David Woodhouse 3 participants 5 messages Mar 11 – Mar 17

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

From: Carsten Stollmaier <[email protected]>

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 since we have the region registered to that.

handle_userfault uses TASK_INTERRUPTIBLE, so it is interruptible by signals. do_user_addr_fault then busy-retries it if the pending signal is non-fatal. This leads to contention of the mmap_lock.

This patch replaces the use of gfn_to_hva_cache with gfn_to_pfn_cache, as gfn_to_pfn_cache ensures page presence for the memory access, preventing the contention of the mmap_lock.

As an added bonus, this removes the last open-coded assembler access to userspace from arch/x86/kvm/x86.c.

---
 arch/x86/include/asm/kvm_host.h |   2 +-
 arch/x86/kvm/x86.c              | 115 +++++++++++++++-----------------
 2 files changed, 54 insertions(+), 63 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 +53 -62

202 lines not on this page · raw message

Signature · 1 line
2.51.0
#1 lore raw
Quoted 15 lines · David Woodhouse

From: Carsten Stollmaier <[email protected]>

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 since we have the region registered to that.

handle_userfault uses TASK_INTERRUPTIBLE, so it is interruptible by signals. do_user_addr_fault then busy-retries it if the pending signal is non-fatal. This leads to contention of the mmap_lock.

This patch replaces the use of gfn_to_hva_cache with gfn_to_pfn_cache, as gfn_to_pfn_cache ensures page presence for the memory access, preventing the contention of the mmap_lock.

Quoted 5 lines

As an added bonus, this removes the last open-coded assembler access to userspace from arch/x86/kvm/x86.c.

I've just spotted this is mostly reverting my commit 7e2175ebd695 ("KVM: x86: Fix recording of guest steal time / preempted status") from 2021.

The steal time *used* to use a gfn_to_pfn_cache, back in the days when the gfn_to_pfn_cache was entirely hosed before I ripped it out and completely reimplemented it.

#2 lore raw
In reply to David Woodhouse

With some assistance from an AI review bot (well, more than "some").

arch/x86/kvm/x86.c static void record_steal_time(struct kvm_vcpu *vcpu) · line 3792

Quoted 13 lines · David Woodhouse
arch/x86/kvm/x86.c +1 -7
@@ -3806,39 +3788,32 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
 				       st_preempted & KVM_VCPU_FLUSH_TLB);
 		if (st_preempted & KVM_VCPU_FLUSH_TLB)
 			kvm_vcpu_flush_tlb_guest(vcpu);
-
-		if (!user_access_begin(st, sizeof(*st)))
-			goto dirty;
 	} else {
-		if (!user_access_begin(st, sizeof(*st)))
-			return;
-
-		unsafe_put_user(0, &st->preempted, out);
+		st->preempted = 0;

These should all be WRITE_ONCE(), correct?

arch/x86/kvm/x86.c static void record_steal_time(struct kvm_vcpu *vcpu) · line 3796

Quoted 5 lines
arch/x86/kvm/x86.c +1 -1
 		vcpu->arch.st.preempted = 0;
 	}
 
-	unsafe_get_user(version, &st->version, out);
+	version = st->version;

And then READ_ONCE()?

Quoted 10 lines
arch/x86/kvm/x86.c +1 -2
 	if (version & 1)
 		version += 1;  /* first time write, random junk */
 
 	version += 1;
-	unsafe_put_user(version, &st->version, out);
+	st->version = version;
 
 	smp_wmb();
 
-	unsafe_get_user(steal, &st->steal, out);

arch/x86/kvm/x86.c static void record_steal_time(struct kvm_vcpu *vcpu) · line 3812

Quoted 10 lines
arch/x86/kvm/x86.c +3 -2
+	steal = st->steal;
 	steal += current->sched_info.run_delay -
 		vcpu->arch.st.last_steal;
 	vcpu->arch.st.last_steal = current->sched_info.run_delay;
-	unsafe_put_user(steal, &st->steal, out);
+	st->steal = steal;
 
 	version += 1;
-	unsafe_put_user(version, &st->version, out);
+	st->version = version;

And then here, doesn't there need to be an smp_wmb() before incrementing the version again? Because I believe making this all vanilla C means the compiler can reorder those two. Per the friendly bot:

The previous code used unsafe_put_user(), which inherently acts as a compiler barrier due to its internal inline assembly

Quoted 12 lines
arch/x86/kvm/x86.c +3 -4
+
+	kvm_gpc_mark_dirty_in_slot(gpc);
 
- out:
-	user_access_end();
- dirty:
-	mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa));
+	read_unlock(&gpc->lock);
 }
 
 /*

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 4151

Quoted 6 lines
arch/x86/kvm/x86.c +1 -2
@@ -4173,8 +4148,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 		vcpu->arch.st.msr_val = data;
 
-		if (!(data & KVM_MSR_ENABLED))
-			break;
+		if (data & KVM_MSR_ENABLED) {

Curly braces aren't required.

Quoted 5 lines
arch/x86/kvm/x86.c +5 -0
+			kvm_gpc_activate(&vcpu->arch.st.cache, data & ~KVM_MSR_ENABLED,
+					sizeof(struct kvm_steal_time));
+		} else {
+			kvm_gpc_deactivate(&vcpu->arch.st.cache);
+		}

...

arch/x86/kvm/x86.c static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) · line 5247

Quoted 11 lines
arch/x86/kvm/x86.c +1 -6
@@ -5266,20 +5244,28 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
 	if (unlikely(current->mm != vcpu->kvm->mm))
 		return;
 
-	slots = kvm_memslots(vcpu->kvm);
-
-	if (unlikely(slots->generation != ghc->generation ||
-		     gpa != ghc->gpa ||
-		     kvm_is_error_hva(ghc->hva) || !ghc->memslot))
-		return;
+	read_lock_irqsave(&gpc->lock, flags);

I'm pretty sure this is going to make PROVE_LOCKING unhappy due to PREEMPT_RT making rwlock_t sleepable (when called from kvm_sched_out()). I've been content to ignore the kvm_xen_set_evtchn_fast() warning[*] because I can't imagine anyone is crazy enough to emulate Xen with an RT kernel, but I do know there are RT users that run VMs, and so this path would be more than just a PROVE_LOCKING issue.

If we want to push the gpc stuff broadly, we need a solution to that (though I'm still not 100% convinced using a gpc here is a net positive).

[*] https://lore.kernel.org/all/[email protected]

#3 lore raw
Quoted 11 lines · Sean Christopherson

I'm pretty sure this is going to make PROVE_LOCKING unhappy due to PREEMPT_RT making rwlock_t sleepable (when called from kvm_sched_out()).  I've been content to ignore the kvm_xen_set_evtchn_fast() warning[*] because I can't imagine anyone is crazy enough to emulate Xen with an RT kernel, but I do know there are RT users that run VMs, and so this path would be more than just a PROVE_LOCKING issue.

If we want to push the gpc stuff broadly, we need a solution to that (though I'm still not 100% convinced using a gpc here is a net positive).

[*] https://lore.kernel.org/all/[email protected]

I set up a test case, and you're quite right:

[  478.182893] [ BUG: Invalid wait context ]
[  478.182895] 7.0.0-rc3+ #2 Tainted: G           OE      
[  478.182896] -----------------------------
[  478.182897] steal_time/5227 is trying to lock:
[  478.182898] ff2c7aa6637b8cf8 (&gpc->lock){++++}-{3:3}, at: kvm_arch_vcpu_put+0xe8/0x240 [kvm]
[  478.182982] other info that might help us debug this:
[  478.182982] context-{5:5}
[  478.182983] 3 locks held by steal_time/5227:
[  478.182984]  #0: ff2c7aa6637b80a0 (&vcpu->mutex){+.+.}-{4:4}, at: kvm_vcpu_ioctl+0xe4/0x950 [kvm]
[  478.183026]  #1: ff2c7ac4fe0367a0 (&rq->__lock){-...}-{2:2}, at: raw_spin_rq_lock_nested+0x24/0xb0
[  478.183034]  #2: ff2c7aa67e96a4e0 (&kvm->srcu){.+.+}-{0:0}, at: kvm_arch_vcpu_put+0x7d/0x240 [kvm]

It appears to be fixable just by making it use read_trylock(). In the PREEMPT_RT case that appears to go through rt_read_trylock() → rwbase_read_trylock(), which does an atomic cmpxchg on the reader count and returns immediately if it can't get it. And, crucially, doesn't seem to whine about it.

There's absolutely no need for the irqsave in this case; this lock is never obtained from interrupt context anyway.

I think that just using read_trylock() will work for the kvm_xen_set_evtchn_fast() case too; will look at that in the morning.

@@ -5244,20 +5244,33 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)

        if (unlikely(current->mm != vcpu->kvm->mm))
                return;

- read_lock_irqsave(&gpc->lock, flags);
+ /*

+        * Use a trylock as this is called from the scheduler path (via
+        * kvm_sched_out), where rwlock_t is not safe on PREEMPT_RT (it
+        * becomes sleepable).  Setting preempted is best-effort anyway;
+        * the old HVA-based code used copy_to_user_nofault() which could
+        * also silently fail.
+        *
+        * Since we only trylock and bail on failure, there is no risk of
+        * deadlock with an interrupt handler, so no need to disable
+        * interrupts.
+        */
+       if (!read_trylock(&gpc->lock))
+               return;
+
        if (!kvm_gpc_check(gpc, sizeof(*st)))
                goto out_unlock_gpc;
#4 lore raw
In reply to Sean Christopherson
Quoted 3 lines · Sean Christopherson

I've been content
to ignore the kvm_xen_set_evtchn_fast() warning[*] because I can't imagine anyone is crazy enough to emulate Xen with an RT kernel

[...]

Quoted 1 line

[*] https://lore.kernel.org/all/[email protected]

Completely not related to this patch:

One side effort of this lockdep warning is it suppresses all further lockdep warnings. We have met in our TDX CI testing that a useful lockdep warning was suppressed because of this one. We have since then disabled CONFIG_KVM_XEN in our CI. :-)