KVM: page track: add the framework of guest page tracking
The array, gfn_track[mode][gfn], is introduced in memory slot for every
guest page, this is the tracking count for the gust page on different
modes. If the page is tracked then the count is increased, the page is
not tracked after the count reaches zero
We use 'unsigned short' as the tracking count which should be enough as
shadow page table only can use 2^14 (2^3 for level, 2^1 for cr4_pae, 2^2
for quadrant, 2^3 for access, 2^1 for nxe, 2^1 for cr0_wp, 2^1 for
smep_andnot_wp, 2^1 for smap_andnot_wp, and 2^1 for smm) at most, there
is enough room for other trackers
Two callbacks, kvm_page_track_create_memslot() and
kvm_page_track_free_memslot() are implemented in this patch, they are
internally used to initialize and reclaim the memory of the array
Currently, only write track mode is supported
Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-02-24 12:51:09 +03:00
|
|
|
/*
|
|
|
|
* Support KVM gust page tracking
|
|
|
|
*
|
|
|
|
* This feature allows us to track page access in guest. Currently, only
|
|
|
|
* write access is tracked.
|
|
|
|
*
|
|
|
|
* Copyright(C) 2015 Intel Corporation.
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Xiao Guangrong <guangrong.xiao@linux.intel.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kvm_host.h>
|
|
|
|
#include <asm/kvm_host.h>
|
|
|
|
#include <asm/kvm_page_track.h>
|
|
|
|
|
|
|
|
#include "mmu.h"
|
|
|
|
|
|
|
|
void kvm_page_track_free_memslot(struct kvm_memory_slot *free,
|
|
|
|
struct kvm_memory_slot *dont)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < KVM_PAGE_TRACK_MAX; i++)
|
|
|
|
if (!dont || free->arch.gfn_track[i] !=
|
|
|
|
dont->arch.gfn_track[i]) {
|
|
|
|
kvfree(free->arch.gfn_track[i]);
|
|
|
|
free->arch.gfn_track[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int kvm_page_track_create_memslot(struct kvm_memory_slot *slot,
|
|
|
|
unsigned long npages)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
|
|
|
|
slot->arch.gfn_track[i] = kvm_kvzalloc(npages *
|
|
|
|
sizeof(*slot->arch.gfn_track[i]));
|
|
|
|
if (!slot->arch.gfn_track[i])
|
|
|
|
goto track_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
track_free:
|
|
|
|
kvm_page_track_free_memslot(slot, NULL);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2016-02-24 12:51:10 +03:00
|
|
|
|
|
|
|
static inline bool page_track_mode_is_valid(enum kvm_page_track_mode mode)
|
|
|
|
{
|
|
|
|
if (mode < 0 || mode >= KVM_PAGE_TRACK_MAX)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_gfn_track(struct kvm_memory_slot *slot, gfn_t gfn,
|
|
|
|
enum kvm_page_track_mode mode, short count)
|
|
|
|
{
|
|
|
|
int index, val;
|
|
|
|
|
|
|
|
index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
|
|
|
|
|
|
|
|
val = slot->arch.gfn_track[mode][index];
|
|
|
|
|
|
|
|
if (WARN_ON(val + count < 0 || val + count > USHRT_MAX))
|
|
|
|
return;
|
|
|
|
|
|
|
|
slot->arch.gfn_track[mode][index] += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add guest page to the tracking pool so that corresponding access on that
|
|
|
|
* page will be intercepted.
|
|
|
|
*
|
|
|
|
* It should be called under the protection both of mmu-lock and kvm->srcu
|
|
|
|
* or kvm->slots_lock.
|
|
|
|
*
|
|
|
|
* @kvm: the guest instance we are interested in.
|
|
|
|
* @slot: the @gfn belongs to.
|
|
|
|
* @gfn: the guest page.
|
|
|
|
* @mode: tracking mode, currently only write track is supported.
|
|
|
|
*/
|
|
|
|
void kvm_slot_page_track_add_page(struct kvm *kvm,
|
|
|
|
struct kvm_memory_slot *slot, gfn_t gfn,
|
|
|
|
enum kvm_page_track_mode mode)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (WARN_ON(!page_track_mode_is_valid(mode)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
update_gfn_track(slot, gfn, mode, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* new track stops large page mapping for the
|
|
|
|
* tracked page.
|
|
|
|
*/
|
|
|
|
kvm_mmu_gfn_disallow_lpage(slot, gfn);
|
|
|
|
|
|
|
|
if (mode == KVM_PAGE_TRACK_WRITE)
|
|
|
|
if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn))
|
|
|
|
kvm_flush_remote_tlbs(kvm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remove the guest page from the tracking pool which stops the interception
|
|
|
|
* of corresponding access on that page. It is the opposed operation of
|
|
|
|
* kvm_slot_page_track_add_page().
|
|
|
|
*
|
|
|
|
* It should be called under the protection both of mmu-lock and kvm->srcu
|
|
|
|
* or kvm->slots_lock.
|
|
|
|
*
|
|
|
|
* @kvm: the guest instance we are interested in.
|
|
|
|
* @slot: the @gfn belongs to.
|
|
|
|
* @gfn: the guest page.
|
|
|
|
* @mode: tracking mode, currently only write track is supported.
|
|
|
|
*/
|
|
|
|
void kvm_slot_page_track_remove_page(struct kvm *kvm,
|
|
|
|
struct kvm_memory_slot *slot, gfn_t gfn,
|
|
|
|
enum kvm_page_track_mode mode)
|
|
|
|
{
|
|
|
|
if (WARN_ON(!page_track_mode_is_valid(mode)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
update_gfn_track(slot, gfn, mode, -1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allow large page mapping for the tracked page
|
|
|
|
* after the tracker is gone.
|
|
|
|
*/
|
|
|
|
kvm_mmu_gfn_allow_lpage(slot, gfn);
|
|
|
|
}
|
2016-02-24 12:51:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check if the corresponding access on the specified guest page is tracked.
|
|
|
|
*/
|
|
|
|
bool kvm_page_track_is_active(struct kvm_vcpu *vcpu, gfn_t gfn,
|
|
|
|
enum kvm_page_track_mode mode)
|
|
|
|
{
|
|
|
|
struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
|
|
|
|
int index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
|
|
|
|
|
|
|
|
if (WARN_ON(!page_track_mode_is_valid(mode)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !!ACCESS_ONCE(slot->arch.gfn_track[mode][index]);
|
|
|
|
}
|
2016-02-24 12:51:13 +03:00
|
|
|
|
|
|
|
void kvm_page_track_init(struct kvm *kvm)
|
|
|
|
{
|
|
|
|
struct kvm_page_track_notifier_head *head;
|
|
|
|
|
|
|
|
head = &kvm->arch.track_notifier_head;
|
|
|
|
init_srcu_struct(&head->track_srcu);
|
|
|
|
INIT_HLIST_HEAD(&head->track_notifier_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* register the notifier so that event interception for the tracked guest
|
|
|
|
* pages can be received.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
kvm_page_track_register_notifier(struct kvm *kvm,
|
|
|
|
struct kvm_page_track_notifier_node *n)
|
|
|
|
{
|
|
|
|
struct kvm_page_track_notifier_head *head;
|
|
|
|
|
|
|
|
head = &kvm->arch.track_notifier_head;
|
|
|
|
|
|
|
|
spin_lock(&kvm->mmu_lock);
|
|
|
|
hlist_add_head_rcu(&n->node, &head->track_notifier_list);
|
|
|
|
spin_unlock(&kvm->mmu_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* stop receiving the event interception. It is the opposed operation of
|
|
|
|
* kvm_page_track_register_notifier().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
kvm_page_track_unregister_notifier(struct kvm *kvm,
|
|
|
|
struct kvm_page_track_notifier_node *n)
|
|
|
|
{
|
|
|
|
struct kvm_page_track_notifier_head *head;
|
|
|
|
|
|
|
|
head = &kvm->arch.track_notifier_head;
|
|
|
|
|
|
|
|
spin_lock(&kvm->mmu_lock);
|
|
|
|
hlist_del_rcu(&n->node);
|
|
|
|
spin_unlock(&kvm->mmu_lock);
|
|
|
|
synchronize_srcu(&head->track_srcu);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notify the node that write access is intercepted and write emulation is
|
|
|
|
* finished at this time.
|
|
|
|
*
|
|
|
|
* The node should figure out if the written page is the one that node is
|
|
|
|
* interested in by itself.
|
|
|
|
*/
|
|
|
|
void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
|
|
|
|
int bytes)
|
|
|
|
{
|
|
|
|
struct kvm_page_track_notifier_head *head;
|
|
|
|
struct kvm_page_track_notifier_node *n;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
head = &vcpu->kvm->arch.track_notifier_head;
|
|
|
|
|
|
|
|
if (hlist_empty(&head->track_notifier_list))
|
|
|
|
return;
|
|
|
|
|
|
|
|
idx = srcu_read_lock(&head->track_srcu);
|
|
|
|
hlist_for_each_entry_rcu(n, &head->track_notifier_list, node)
|
|
|
|
if (n->track_write)
|
|
|
|
n->track_write(vcpu, gpa, new, bytes);
|
|
|
|
srcu_read_unlock(&head->track_srcu, idx);
|
|
|
|
}
|