KVM: SVM: count all irq windows exit
[linux-3.10.git] / arch / x86 / kvm / mmu_audit.c
index 66219af..715da5a 100644 (file)
@@ -4,7 +4,7 @@
  * Audit code for KVM MMU
  *
  * Copyright (C) 2006 Qumranet, Inc.
- * Copyright 2010 Red Hat, Inc. and/or its affilates.
+ * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  *
  * Authors:
  *   Yaniv Kamay  <yaniv@qumranet.com>
 
 #include <linux/ratelimit.h>
 
-static int audit_point;
+char const *audit_point_name[] = {
+       "pre page fault",
+       "post page fault",
+       "pre pte write",
+       "post pte write",
+       "pre sync",
+       "post sync"
+};
 
-#define audit_printk(fmt, args...)             \
+#define audit_printk(kvm, fmt, args...)                \
        printk(KERN_ERR "audit: (%s) error: "   \
-               fmt, audit_point_name[audit_point], ##args)
+               fmt, audit_point_name[kvm->arch.audit_point], ##args)
 
 typedef void (*inspect_spte_fn) (struct kvm_vcpu *vcpu, u64 *sptep, int level);
 
@@ -97,21 +104,12 @@ static void audit_mappings(struct kvm_vcpu *vcpu, u64 *sptep, int level)
 
        if (sp->unsync) {
                if (level != PT_PAGE_TABLE_LEVEL) {
-                       audit_printk("unsync sp: %p level = %d\n", sp, level);
-                       return;
-               }
-
-               if (*sptep == shadow_notrap_nonpresent_pte) {
-                       audit_printk("notrap spte in unsync sp: %p\n", sp);
+                       audit_printk(vcpu->kvm, "unsync sp: %p "
+                                    "level = %d\n", sp, level);
                        return;
                }
        }
 
-       if (sp->role.direct && *sptep == shadow_notrap_nonpresent_pte) {
-               audit_printk("notrap spte in direct sp: %p\n", sp);
-               return;
-       }
-
        if (!is_shadow_present_pte(*sptep) || !is_last_spte(*sptep, level))
                return;
 
@@ -125,25 +123,26 @@ static void audit_mappings(struct kvm_vcpu *vcpu, u64 *sptep, int level)
 
        hpa =  pfn << PAGE_SHIFT;
        if ((*sptep & PT64_BASE_ADDR_MASK) != hpa)
-               audit_printk("levels %d pfn %llx hpa %llx ent %llxn",
-                                  vcpu->arch.mmu.root_level, pfn, hpa, *sptep);
+               audit_printk(vcpu->kvm, "levels %d pfn %llx hpa %llx "
+                            "ent %llxn", vcpu->arch.mmu.root_level, pfn,
+                            hpa, *sptep);
 }
 
 static void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
 {
+       static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
        unsigned long *rmapp;
        struct kvm_mmu_page *rev_sp;
        gfn_t gfn;
 
-
        rev_sp = page_header(__pa(sptep));
        gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
 
        if (!gfn_to_memslot(kvm, gfn)) {
-               if (!printk_ratelimit())
+               if (!__ratelimit(&ratelimit_state))
                        return;
-               audit_printk("no memslot for gfn %llx\n", gfn);
-               audit_printk("index %ld of sp (gfn=%llx)\n",
+               audit_printk(kvm, "no memslot for gfn %llx\n", gfn);
+               audit_printk(kvm, "index %ld of sp (gfn=%llx)\n",
                       (long int)(sptep - rev_sp->spt), rev_sp->gfn);
                dump_stack();
                return;
@@ -151,9 +150,10 @@ static void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
 
        rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level);
        if (!*rmapp) {
-               if (!printk_ratelimit())
+               if (!__ratelimit(&ratelimit_state))
                        return;
-               audit_printk("no rmap for writable spte %llx\n", *sptep);
+               audit_printk(kvm, "no rmap for writable spte %llx\n",
+                            *sptep);
                dump_stack();
        }
 }
@@ -164,6 +164,15 @@ static void audit_sptes_have_rmaps(struct kvm_vcpu *vcpu, u64 *sptep, int level)
                inspect_spte_has_rmap(vcpu->kvm, sptep);
 }
 
+static void audit_spte_after_sync(struct kvm_vcpu *vcpu, u64 *sptep, int level)
+{
+       struct kvm_mmu_page *sp = page_header(__pa(sptep));
+
+       if (vcpu->kvm->arch.audit_point == AUDIT_POST_SYNC && sp->unsync)
+               audit_printk(vcpu->kvm, "meet unsync sp(%p) after sync "
+                            "root.\n", sp);
+}
+
 static void check_mappings_rmap(struct kvm *kvm, struct kvm_mmu_page *sp)
 {
        int i;
@@ -179,7 +188,7 @@ static void check_mappings_rmap(struct kvm *kvm, struct kvm_mmu_page *sp)
        }
 }
 
-void audit_write_protection(struct kvm *kvm, struct kvm_mmu_page *sp)
+static void audit_write_protection(struct kvm *kvm, struct kvm_mmu_page *sp)
 {
        struct kvm_memory_slot *slot;
        unsigned long *rmapp;
@@ -191,12 +200,13 @@ void audit_write_protection(struct kvm *kvm, struct kvm_mmu_page *sp)
        slot = gfn_to_memslot(kvm, sp->gfn);
        rmapp = &slot->rmap[sp->gfn - slot->base_gfn];
 
-       spte = rmap_next(kvm, rmapp, NULL);
+       spte = rmap_next(rmapp, NULL);
        while (spte) {
                if (is_writable_pte(*spte))
-                       audit_printk("shadow page has writable mappings: gfn "
-                                    "%llx role %x\n", sp->gfn, sp->role.word);
-               spte = rmap_next(kvm, rmapp, spte);
+                       audit_printk(kvm, "shadow page has writable "
+                                    "mappings: gfn %llx role %x\n",
+                                    sp->gfn, sp->role.word);
+               spte = rmap_next(rmapp, spte);
        }
 }
 
@@ -215,6 +225,7 @@ static void audit_spte(struct kvm_vcpu *vcpu, u64 *sptep, int level)
 {
        audit_sptes_have_rmaps(vcpu, sptep, level);
        audit_mappings(vcpu, sptep, level);
+       audit_spte_after_sync(vcpu, sptep, level);
 }
 
 static void audit_vcpu_spte(struct kvm_vcpu *vcpu)
@@ -222,30 +233,33 @@ static void audit_vcpu_spte(struct kvm_vcpu *vcpu)
        mmu_spte_walk(vcpu, audit_spte);
 }
 
-static void kvm_mmu_audit(void *ignore, struct kvm_vcpu *vcpu, int point)
+static bool mmu_audit;
+static struct static_key mmu_audit_key;
+
+static void __kvm_mmu_audit(struct kvm_vcpu *vcpu, int point)
 {
        static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
 
        if (!__ratelimit(&ratelimit_state))
                return;
 
-       audit_point = point;
+       vcpu->kvm->arch.audit_point = point;
        audit_all_active_sps(vcpu->kvm);
        audit_vcpu_spte(vcpu);
 }
 
-static bool mmu_audit;
+static inline void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point)
+{
+       if (static_key_false((&mmu_audit_key)))
+               __kvm_mmu_audit(vcpu, point);
+}
 
 static void mmu_audit_enable(void)
 {
-       int ret;
-
        if (mmu_audit)
                return;
 
-       ret = register_trace_kvm_mmu_audit(kvm_mmu_audit, NULL);
-       WARN_ON(ret);
-
+       static_key_slow_inc(&mmu_audit_key);
        mmu_audit = true;
 }
 
@@ -254,8 +268,7 @@ static void mmu_audit_disable(void)
        if (!mmu_audit)
                return;
 
-       unregister_trace_kvm_mmu_audit(kvm_mmu_audit, NULL);
-       tracepoint_synchronize_unregister();
+       static_key_slow_dec(&mmu_audit_key);
        mmu_audit = false;
 }