KVM: Allow cross page reads and writes from cached translations.
This patch adds support for kvm_gfn_to_hva_cache_init functions for reads and writes that will cross a page. If the range falls within the same memslot, then this will be a fast operation. If the range is split between two memslots, then the slower kvm_read_guest and kvm_write_guest are used. Tested: Test against kvm_clock unit tests. Signed-off-by: Andrew Honig <ahonig@google.com> Signed-off-by: Gleb Natapov <gleb@redhat.com>
This commit is contained in:

committed by
Gleb Natapov

parent
09a6e1f4ad
commit
8f964525a1
@@ -1541,21 +1541,38 @@ int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
|
||||
}
|
||||
|
||||
int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
|
||||
gpa_t gpa)
|
||||
gpa_t gpa, unsigned long len)
|
||||
{
|
||||
struct kvm_memslots *slots = kvm_memslots(kvm);
|
||||
int offset = offset_in_page(gpa);
|
||||
gfn_t gfn = gpa >> PAGE_SHIFT;
|
||||
gfn_t start_gfn = gpa >> PAGE_SHIFT;
|
||||
gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
|
||||
gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
|
||||
gfn_t nr_pages_avail;
|
||||
|
||||
ghc->gpa = gpa;
|
||||
ghc->generation = slots->generation;
|
||||
ghc->memslot = gfn_to_memslot(kvm, gfn);
|
||||
ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
|
||||
if (!kvm_is_error_hva(ghc->hva))
|
||||
ghc->len = len;
|
||||
ghc->memslot = gfn_to_memslot(kvm, start_gfn);
|
||||
ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, &nr_pages_avail);
|
||||
if (!kvm_is_error_hva(ghc->hva) && nr_pages_avail >= nr_pages_needed) {
|
||||
ghc->hva += offset;
|
||||
else
|
||||
return -EFAULT;
|
||||
|
||||
} else {
|
||||
/*
|
||||
* If the requested region crosses two memslots, we still
|
||||
* verify that the entire region is valid here.
|
||||
*/
|
||||
while (start_gfn <= end_gfn) {
|
||||
ghc->memslot = gfn_to_memslot(kvm, start_gfn);
|
||||
ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
|
||||
&nr_pages_avail);
|
||||
if (kvm_is_error_hva(ghc->hva))
|
||||
return -EFAULT;
|
||||
start_gfn += nr_pages_avail;
|
||||
}
|
||||
/* Use the slow path for cross page reads and writes. */
|
||||
ghc->memslot = NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
|
||||
@@ -1566,8 +1583,13 @@ int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
|
||||
struct kvm_memslots *slots = kvm_memslots(kvm);
|
||||
int r;
|
||||
|
||||
BUG_ON(len > ghc->len);
|
||||
|
||||
if (slots->generation != ghc->generation)
|
||||
kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
|
||||
kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
|
||||
|
||||
if (unlikely(!ghc->memslot))
|
||||
return kvm_write_guest(kvm, ghc->gpa, data, len);
|
||||
|
||||
if (kvm_is_error_hva(ghc->hva))
|
||||
return -EFAULT;
|
||||
@@ -1587,8 +1609,13 @@ int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
|
||||
struct kvm_memslots *slots = kvm_memslots(kvm);
|
||||
int r;
|
||||
|
||||
BUG_ON(len > ghc->len);
|
||||
|
||||
if (slots->generation != ghc->generation)
|
||||
kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
|
||||
kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
|
||||
|
||||
if (unlikely(!ghc->memslot))
|
||||
return kvm_read_guest(kvm, ghc->gpa, data, len);
|
||||
|
||||
if (kvm_is_error_hva(ghc->hva))
|
||||
return -EFAULT;
|
||||
|
Reference in New Issue
Block a user