Skip to contentSkip to conversation

KVM: x86: Fix recording of guest steal time / preempted status

PATCH In review Rishabh Bhatnagar 2 participants 5 messages Oct 14, 2022 – Oct 15, 2022 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 2/9 KVM: x86: Fix recording of guest steal time / preempted status

From: David Woodhouse <[email protected]>

commit 7e2175ebd695f17860c5bd4ad7616cce12ed4591 upstream.

In commit b043138246a4 ("x86/KVM: Make sure KVM_VCPU_FLUSH_TLB flag is not missed") we switched to using a gfn_to_pfn_cache for accessing the guest steal time structure in order to allow for an atomic xchg of the preempted field. This has a couple of problems.

Firstly, kvm_map_gfn() doesn't work at all for IOMEM pages when the atomic flag is set, which it is in kvm_steal_time_set_preempted(). So a guest vCPU using an IOMEM page for its steal time would never have its preempted field set.

Secondly, the gfn_to_pfn_cache is not invalidated in all cases where it should have been. There are two stages to the GFN->PFN conversion; first the GFN is converted to a userspace HVA, and then that HVA is looked up in the process page tables to find the underlying host PFN. Correct invalidation of the latter would require being hooked up to the MMU notifiers, but that doesn't happen---so it just keeps mapping and unmapping the *wrong* PFN after the userspace page tables change.

In the !IOMEM case at least the stale page *is* pinned all the time it's cached, so it won't be freed and reused by anyone else while still receiving the steal time updates. The map/unmap dance only takes care of the KVM administrivia such as marking the page dirty.

Until the gfn_to_pfn cache handles the remapping automatically by integrating with the MMU notifiers, we might as well not get a kernel mapping of it, and use the perfectly serviceable userspace HVA that we already have. We just need to implement the atomic xchg on the userspace address with appropriate exception handling, which is fairly trivial.

Fixes b043138246a4 ("x86/KVM: Make sure KVM_VCPU_FLUSH_TLB flag is not missed")

Message-Id: <[email protected]> [I didn't entirely agree with David's assessment of the
usefulness of the gfn_to_pfn cache, and integrated the outcome of the discussion in the above commit message. - Paolo]

[[email protected]: Use the older mark_page_dirty_in_slot api without kvm argument]

---
 arch/x86/include/asm/kvm_host.h |   2 +-
 arch/x86/kvm/x86.c              | 105 +++++++++++++++++++++++---------
 2 files changed, 76 insertions(+), 31 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index 38c63a78aba6..2b35f8139f15 100644
@@ -663,7 +663,7 @@ struct kvm_vcpu_arch {
 		u8 preempted;
 		u64 msr_val;
 		u64 last_steal;
-		struct gfn_to_pfn_cache cache;
+		struct gfn_to_hva_cache cache;
 	} st;
 
 	u64 l1_tsc_offset;
arch/x86/kvm/x86.c +75 -30

163 lines not on this page · raw message

Signature · 1 line
2.37.1
maintainer
#2 lore raw
Quoted 41 lines · Rishabh Bhatnagar

From: David Woodhouse <[email protected]>

commit 7e2175ebd695f17860c5bd4ad7616cce12ed4591 upstream.

In commit b043138246a4 ("x86/KVM: Make sure KVM_VCPU_FLUSH_TLB flag is not missed") we switched to using a gfn_to_pfn_cache for accessing the guest steal time structure in order to allow for an atomic xchg of the preempted field. This has a couple of problems.

Firstly, kvm_map_gfn() doesn't work at all for IOMEM pages when the atomic flag is set, which it is in kvm_steal_time_set_preempted(). So a guest vCPU using an IOMEM page for its steal time would never have its preempted field set.

Secondly, the gfn_to_pfn_cache is not invalidated in all cases where it should have been. There are two stages to the GFN->PFN conversion; first the GFN is converted to a userspace HVA, and then that HVA is looked up in the process page tables to find the underlying host PFN. Correct invalidation of the latter would require being hooked up to the MMU notifiers, but that doesn't happen---so it just keeps mapping and unmapping the *wrong* PFN after the userspace page tables change.

In the !IOMEM case at least the stale page *is* pinned all the time it's cached, so it won't be freed and reused by anyone else while still receiving the steal time updates. The map/unmap dance only takes care of the KVM administrivia such as marking the page dirty.

Until the gfn_to_pfn cache handles the remapping automatically by integrating with the MMU notifiers, we might as well not get a kernel mapping of it, and use the perfectly serviceable userspace HVA that we already have. We just need to implement the atomic xchg on the userspace address with appropriate exception handling, which is fairly trivial.

Fixes b043138246a4 ("x86/KVM: Make sure KVM_VCPU_FLUSH_TLB flag is not missed")

Message-Id: <[email protected]> [I didn't entirely agree with David's assessment of the
usefulness of the gfn_to_pfn cache, and integrated the outcome of the discussion in the above commit message. - Paolo]

Quoted 8 lines

[[email protected]: Use the older mark_page_dirty_in_slot api without kvm argument]

---
 arch/x86/include/asm/kvm_host.h |   2 +-
 arch/x86/kvm/x86.c              | 105 +++++++++++++++++++++++---------
 2 files changed, 76 insertions(+), 31 deletions(-)

This is already in some stable kernels, why send it again and what tree(s) are you asking for it to be applied to?

Same for the other backports you sent.

confused,

greg k-h

#3 lore raw

Please ignore/drop this patch series. CC list expanded from patch by mistake. Planning to get some more reviews/testing before resending it later.

On 10/14/22, 10:45 PM, "Greg KH" <[email protected]> wrote:

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.

On Fri, Oct 14, 2022 at 10:26:36PM +0000, Rishabh Bhatnagar wrote:
> From: David Woodhouse <[email protected]>
>
> commit 7e2175ebd695f17860c5bd4ad7616cce12ed4591 upstream.
>
> In commit b043138246a4 ("x86/KVM: Make sure KVM_VCPU_FLUSH_TLB flag is > not missed") we switched to using a gfn_to_pfn_cache for accessing the > guest steal time structure in order to allow for an atomic xchg of the > preempted field. This has a couple of problems.
>
> Firstly, kvm_map_gfn() doesn't work at all for IOMEM pages when the > atomic flag is set, which it is in kvm_steal_time_set_preempted(). So a > guest vCPU using an IOMEM page for its steal time would never have its > preempted field set.
>
> Secondly, the gfn_to_pfn_cache is not invalidated in all cases where it > should have been. There are two stages to the GFN->PFN conversion; > first the GFN is converted to a userspace HVA, and then that HVA is > looked up in the process page tables to find the underlying host PFN. > Correct invalidation of the latter would require being hooked up to the > MMU notifiers, but that doesn't happen---so it just keeps mapping and > unmapping the *wrong* PFN after the userspace page tables change. >
> In the !IOMEM case at least the stale page *is* pinned all the time it's > cached, so it won't be freed and reused by anyone else while still > receiving the steal time updates. The map/unmap dance only takes care > of the KVM administrivia such as marking the page dirty.
>
> Until the gfn_to_pfn cache handles the remapping automatically by > integrating with the MMU notifiers, we might as well not get a
> kernel mapping of it, and use the perfectly serviceable userspace HVA > that we already have. We just need to implement the atomic xchg on > the userspace address with appropriate exception handling, which is > fairly trivial.
>
> Cc: [email protected]
> Fixes: b043138246a4 ("x86/KVM: Make sure KVM_VCPU_FLUSH_TLB flag is not missed") > Signed-off-by: David Woodhouse <[email protected]>
> Message-Id: <[email protected]> > [I didn't entirely agree with David's assessment of the
> usefulness of the gfn_to_pfn cache, and integrated the outcome
> of the discussion in the above commit message. - Paolo]
> Signed-off-by: Paolo Bonzini <[email protected]>
> [[email protected]: Use the older mark_page_dirty_in_slot api without > kvm argument]
> Signed-off-by: Rishabh Bhatnagar <[email protected]>
> ---
> arch/x86/include/asm/kvm_host.h | 2 +-
> arch/x86/kvm/x86.c | 105 +++++++++++++++++++++++---------
> 2 files changed, 76 insertions(+), 31 deletions(-)

This is already in some stable kernels, why send it again and what tree(s) are you asking for it to be applied to?

Same for the other backports you sent.

confused,

greg k-h

#4 lore raw

PATCH 7/9 KVM: x86: revalidate steal time cache if MSR value changes

In reply to a message not in the archive

From: Paolo Bonzini <[email protected]>

commit 901d3765fa804ce42812f1d5b1f3de2dfbb26723 upstream.

Commit 7e2175ebd695 ("KVM: x86: Fix recording of guest steal time / preempted status", 2021-11-11) open coded the previous call to kvm_map_gfn, but in doing so it dropped the comparison between the cached guest physical address and the one in the MSR. This cause an incorrect cache hit if the guest modifies the steal time address while the memslots remain the same. This can happen with kexec, in which case the steal time data is written at the address used by the old kernel instead of the old one.

While at it, rename the variable from gfn to gpa since it is a plain physical address and not a right-shifted one.

Cc David Woodhouse <[email protected]>
Fixes 7e2175ebd695 ("KVM: x86: Fix recording of guest steal time / preempted status")
---
 arch/x86/kvm/x86.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +3 -3
index 75494b3c2d1e..111aa95f3de3 100644
@@ -3018,6 +3018,7 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
 	struct gfn_to_hva_cache *ghc = &vcpu->arch.st.cache;
 	struct kvm_steal_time __user *st;
 	struct kvm_memslots *slots;
+	gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS;
 	u64 steal;
 	u32 version;
 
@@ -3030,13 +3031,12 @@ static void record_steal_time(struct kvm_vcpu *vcpu)
 	slots = kvm_memslots(vcpu->kvm);
 
 	if (unlikely(slots->generation != ghc->generation ||
+		     gpa != ghc->gpa ||
 		     kvm_is_error_hva(ghc->hva) || !ghc->memslot)) {
-		gfn_t gfn = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS;
-
 		/* We rely on the fact that it fits in a single page. */
 		BUILD_BUG_ON((sizeof(*st) - 1) & KVM_STEAL_VALID_BITS);
 
-		if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gfn, sizeof(*st)) ||
+		if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st)) ||
 		    kvm_is_error_hva(ghc->hva) || !ghc->memslot)
 			return;
 	}
Signature · 1 line
2.37.1
#5 lore raw

PATCH 8/9 KVM: x86: do not report preemption if the steal time cache is stale

In reply to a message not in the archive

From: Paolo Bonzini <[email protected]>

commit c3c28d24d910a746b02f496d190e0e8c6560224b upstream.

Commit 7e2175ebd695 ("KVM: x86: Fix recording of guest steal time / preempted status", 2021-11-11) open coded the previous call to kvm_map_gfn, but in doing so it dropped the comparison between the cached guest physical address and the one in the MSR. This cause an incorrect cache hit if the guest modifies the steal time address while the memslots remain the same. This can happen with kexec, in which case the preempted bit is written at the address used by the old kernel instead of the old one.

Cc David Woodhouse <[email protected]>
Fixes 7e2175ebd695 ("KVM: x86: Fix recording of guest steal time / preempted status")
---
 arch/x86/kvm/x86.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/x86.c +2 -0
index 111aa95f3de3..9e9298c333c8 100644
@@ -4089,6 +4089,7 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
 	struct kvm_steal_time __user *st;
 	struct kvm_memslots *slots;
 	static const u8 preempted = KVM_VCPU_PREEMPTED;
+	gpa_t gpa = vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS;
 
 	/*
 	 * The vCPU can be marked preempted if and only if the VM-Exit was on
@@ -4116,6 +4117,7 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
 	slots = kvm_memslots(vcpu->kvm);
 
 	if (unlikely(slots->generation != ghc->generation ||
+		     gpa != ghc->gpa ||
 		     kvm_is_error_hva(ghc->hva) || !ghc->memslot))
 		return;
 
Signature · 1 line
2.37.1