diff mbox series

[3/3] KVM: Explicitly disallow activatating a gfn_to_pfn_cache with INVALID_GPA

Message ID 20240320001542.3203871-4-seanjc@google.com (mailing list archive)
State New, archived
Headers show
Series KVM: Fix for a mostly benign gpc WARN | expand

Commit Message

Sean Christopherson March 20, 2024, 12:15 a.m. UTC
Explicit disallow activating a gfn_to_pfn_cache with an error gpa, i.e.
INVALID_GPA, to ensure that KVM doesn't mistake a GPA-based cache for an
HVA-based cache (KVM uses INVALID_GPA as a magic value to differentiate
between GPA-based and HVA-based caches).

WARN if KVM attempts to activate a cache with INVALID_GPA, purely so that
new caches need to at least consider what to do with a "bad" GPA, as all
existing usage of kvm_gpc_activate() guarantees gpa != INVALID_GPA.  I.e.
removing the WARN in the future is completely reasonable if doing so would
yield cleaner/better code overall.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 virt/kvm/pfncache.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

David Woodhouse March 20, 2024, 8:20 a.m. UTC | #1
On Tue, 2024-03-19 at 17:15 -0700, Sean Christopherson wrote:
> Explicit disallow activating a gfn_to_pfn_cache with an error gpa, i.e.
> INVALID_GPA, to ensure that KVM doesn't mistake a GPA-based cache for an
> HVA-based cache (KVM uses INVALID_GPA as a magic value to differentiate
> between GPA-based and HVA-based caches).
> 
> WARN if KVM attempts to activate a cache with INVALID_GPA, purely so that
> new caches need to at least consider what to do with a "bad" GPA, as all
> existing usage of kvm_gpc_activate() guarantees gpa != INVALID_GPA.  I.e.
> removing the WARN in the future is completely reasonable if doing so would
> yield cleaner/better code overall.
> 
> Signed-off-by: Sean Christopherson <seanjc@google.com>


Reviewed-by: David Woodhouse <dwmw@amazon.co.uk>
Paul Durrant March 21, 2024, 11:13 a.m. UTC | #2
On 20/03/2024 00:15, Sean Christopherson wrote:
> Explicit disallow activating a gfn_to_pfn_cache with an error gpa, i.e.
> INVALID_GPA, to ensure that KVM doesn't mistake a GPA-based cache for an
> HVA-based cache (KVM uses INVALID_GPA as a magic value to differentiate
> between GPA-based and HVA-based caches).
> 
> WARN if KVM attempts to activate a cache with INVALID_GPA, purely so that
> new caches need to at least consider what to do with a "bad" GPA, as all
> existing usage of kvm_gpc_activate() guarantees gpa != INVALID_GPA.  I.e.
> removing the WARN in the future is completely reasonable if doing so would
> yield cleaner/better code overall.
> 
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>   virt/kvm/pfncache.c | 7 +++++++
>   1 file changed, 7 insertions(+)
> 

Reviewed-by: Paul Durrant <paul@xen.org>
diff mbox series

Patch

diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c
index 91b0e329006b..f618719644e0 100644
--- a/virt/kvm/pfncache.c
+++ b/virt/kvm/pfncache.c
@@ -418,6 +418,13 @@  static int __kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned
 
 int kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned long len)
 {
+	/*
+	 * Explicitly disallow INVALID_GPA so that the magic value can be used
+	 * by KVM to differentiate between GPA-based and HVA-based caches.
+	 */
+	if (WARN_ON_ONCE(kvm_is_error_gpa(gpa)))
+		return -EINVAL;
+
 	return __kvm_gpc_activate(gpc, gpa, KVM_HVA_ERR_BAD, len);
 }