Skip to content

Commit

Permalink
KVM: Explicitly disallow activatating a gfn_to_pfn_cache with INVALID…
Browse files Browse the repository at this point in the history
…_GPA

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.

Reviewed-by: David Woodhouse <[email protected]>
Reviewed-by: Paul Durrant <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Sean Christopherson <[email protected]>
  • Loading branch information
sean-jc committed Apr 8, 2024
1 parent 5c9ca4e commit fc62a4e
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions virt/kvm/pfncache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit fc62a4e

Please sign in to comment.