Release 4.14 arch/x86/kvm/mmu_audit.c
/*
* mmu_audit.c:
*
* Audit code for KVM MMU
*
* Copyright (C) 2006 Qumranet, Inc.
* Copyright 2010 Red Hat, Inc. and/or its affiliates.
*
* Authors:
* Yaniv Kamay <yaniv@qumranet.com>
* Avi Kivity <avi@qumranet.com>
* Marcelo Tosatti <mtosatti@redhat.com>
* Xiao Guangrong <xiaoguangrong@cn.fujitsu.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/ratelimit.h>
char const *audit_point_name[] = {
"pre page fault",
"post page fault",
"pre pte write",
"post pte write",
"pre sync",
"post sync"
};
#define audit_printk(kvm, fmt, args...) \
printk(KERN_ERR "audit: (%s) error: " \
fmt, audit_point_name[kvm->arch.audit_point], ##args)
typedef void (*inspect_spte_fn) (struct kvm_vcpu *vcpu, u64 *sptep, int level);
static void __mmu_spte_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
inspect_spte_fn fn, int level)
{
int i;
for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
u64 *ent = sp->spt;
fn(vcpu, ent + i, level);
if (is_shadow_present_pte(ent[i]) &&
!is_last_spte(ent[i], level)) {
struct kvm_mmu_page *child;
child = page_header(ent[i] & PT64_BASE_ADDR_MASK);
__mmu_spte_walk(vcpu, child, fn, level - 1);
}
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 112 | 100.00% | 2 | 100.00% |
Total | 112 | 100.00% | 2 | 100.00% |
static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
{
int i;
struct kvm_mmu_page *sp;
if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
return;
if (vcpu->arch.mmu.root_level >= PT64_ROOT_4LEVEL) {
hpa_t root = vcpu->arch.mmu.root_hpa;
sp = page_header(root);
__mmu_spte_walk(vcpu, sp, fn, vcpu->arch.mmu.root_level);
return;
}
for (i = 0; i < 4; ++i) {
hpa_t root = vcpu->arch.mmu.pae_root[i];
if (root && VALID_PAGE(root)) {
root &= PT64_BASE_ADDR_MASK;
sp = page_header(root);
__mmu_spte_walk(vcpu, sp, fn, 2);
}
}
return;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 141 | 94.00% | 3 | 60.00% |
Yu Zhang | 9 | 6.00% | 2 | 40.00% |
Total | 150 | 100.00% | 5 | 100.00% |
typedef void (*sp_handler) (struct kvm *kvm, struct kvm_mmu_page *sp);
static void walk_all_active_sps(struct kvm *kvm, sp_handler fn)
{
struct kvm_mmu_page *sp;
list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link)
fn(kvm, sp);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 34 | 100.00% | 1 | 100.00% |
Total | 34 | 100.00% | 1 | 100.00% |
static void audit_mappings(struct kvm_vcpu *vcpu, u64 *sptep, int level)
{
struct kvm_mmu_page *sp;
gfn_t gfn;
kvm_pfn_t pfn;
hpa_t hpa;
sp = page_header(__pa(sptep));
if (sp->unsync) {
if (level != PT_PAGE_TABLE_LEVEL) {
audit_printk(vcpu->kvm, "unsync sp: %p "
"level = %d\n", sp, level);
return;
}
}
if (!is_shadow_present_pte(*sptep) || !is_last_spte(*sptep, level))
return;
gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
pfn = kvm_vcpu_gfn_to_pfn_atomic(vcpu, gfn);
if (is_error_pfn(pfn))
return;
hpa = pfn << PAGE_SHIFT;
if ((*sptep & PT64_BASE_ADDR_MASK) != hpa)
audit_printk(vcpu->kvm, "levels %d pfn %llx hpa %llx "
"ent %llxn", vcpu->arch.mmu.root_level, pfn,
hpa, *sptep);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 162 | 98.78% | 4 | 66.67% |
Paolo Bonzini | 1 | 0.61% | 1 | 16.67% |
Dan J Williams | 1 | 0.61% | 1 | 16.67% |
Total | 164 | 100.00% | 6 | 100.00% |
static void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
{
static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
struct kvm_rmap_head *rmap_head;
struct kvm_mmu_page *rev_sp;
struct kvm_memslots *slots;
struct kvm_memory_slot *slot;
gfn_t gfn;
rev_sp = page_header(__pa(sptep));
gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
slots = kvm_memslots_for_spte_role(kvm, rev_sp->role);
slot = __gfn_to_memslot(slots, gfn);
if (!slot) {
if (!__ratelimit(&ratelimit_state))
return;
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;
}
rmap_head = __gfn_to_rmap(gfn, rev_sp->role.level, slot);
if (!rmap_head->val) {
if (!__ratelimit(&ratelimit_state))
return;
audit_printk(kvm, "no rmap for writable spte %llx\n",
*sptep);
dump_stack();
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 128 | 66.32% | 4 | 57.14% |
Paolo Bonzini | 36 | 18.65% | 1 | 14.29% |
Jan Kiszka | 22 | 11.40% | 1 | 14.29% |
Takuya Yoshikawa | 7 | 3.63% | 1 | 14.29% |
Total | 193 | 100.00% | 7 | 100.00% |
static void audit_sptes_have_rmaps(struct kvm_vcpu *vcpu, u64 *sptep, int level)
{
if (is_shadow_present_pte(*sptep) && is_last_spte(*sptep, level))
inspect_spte_has_rmap(vcpu->kvm, sptep);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 43 | 100.00% | 2 | 100.00% |
Total | 43 | 100.00% | 2 | 100.00% |
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);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 59 | 100.00% | 2 | 100.00% |
Total | 59 | 100.00% | 2 | 100.00% |
static void check_mappings_rmap(struct kvm *kvm, struct kvm_mmu_page *sp)
{
int i;
if (sp->role.level != PT_PAGE_TABLE_LEVEL)
return;
for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
if (!is_shadow_present_pte(sp->spt[i]))
continue;
inspect_spte_has_rmap(kvm, sp->spt + i);
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 69 | 98.57% | 2 | 66.67% |
Takuya Yoshikawa | 1 | 1.43% | 1 | 33.33% |
Total | 70 | 100.00% | 3 | 100.00% |
static void audit_write_protection(struct kvm *kvm, struct kvm_mmu_page *sp)
{
struct kvm_rmap_head *rmap_head;
u64 *sptep;
struct rmap_iterator iter;
struct kvm_memslots *slots;
struct kvm_memory_slot *slot;
if (sp->role.direct || sp->unsync || sp->role.invalid)
return;
slots = kvm_memslots_for_spte_role(kvm, sp->role);
slot = __gfn_to_memslot(slots, sp->gfn);
rmap_head = __gfn_to_rmap(sp->gfn, PT_PAGE_TABLE_LEVEL, slot);
for_each_rmap_spte(rmap_head, &iter, sptep) {
if (is_writable_pte(*sptep))
audit_printk(kvm, "shadow page has writable "
"mappings: gfn %llx role %x\n",
sp->gfn, sp->role.word);
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 80 | 62.02% | 6 | 54.55% |
Paolo Bonzini | 33 | 25.58% | 2 | 18.18% |
Takuya Yoshikawa | 16 | 12.40% | 3 | 27.27% |
Total | 129 | 100.00% | 11 | 100.00% |
static void audit_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
{
check_mappings_rmap(kvm, sp);
audit_write_protection(kvm, sp);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 30 | 100.00% | 1 | 100.00% |
Total | 30 | 100.00% | 1 | 100.00% |
static void audit_all_active_sps(struct kvm *kvm)
{
walk_all_active_sps(kvm, audit_sp);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 18 | 100.00% | 2 | 100.00% |
Total | 18 | 100.00% | 2 | 100.00% |
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);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 45 | 100.00% | 2 | 100.00% |
Total | 45 | 100.00% | 2 | 100.00% |
static void audit_vcpu_spte(struct kvm_vcpu *vcpu)
{
mmu_spte_walk(vcpu, audit_spte);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 18 | 100.00% | 1 | 100.00% |
Total | 18 | 100.00% | 1 | 100.00% |
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;
vcpu->kvm->arch.audit_point = point;
audit_all_active_sps(vcpu->kvm);
audit_vcpu_spte(vcpu);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 58 | 100.00% | 7 | 100.00% |
Total | 58 | 100.00% | 7 | 100.00% |
static inline void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point)
{
if (static_key_false((&mmu_audit_key)))
__kvm_mmu_audit(vcpu, point);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 31 | 96.88% | 2 | 66.67% |
Ingo Molnar | 1 | 3.12% | 1 | 33.33% |
Total | 32 | 100.00% | 3 | 100.00% |
static void mmu_audit_enable(void)
{
if (mmu_audit)
return;
static_key_slow_inc(&mmu_audit_key);
mmu_audit = true;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 22 | 95.65% | 2 | 66.67% |
Ingo Molnar | 1 | 4.35% | 1 | 33.33% |
Total | 23 | 100.00% | 3 | 100.00% |
static void mmu_audit_disable(void)
{
if (!mmu_audit)
return;
static_key_slow_dec(&mmu_audit_key);
mmu_audit = false;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 23 | 95.83% | 2 | 66.67% |
Ingo Molnar | 1 | 4.17% | 1 | 33.33% |
Total | 24 | 100.00% | 3 | 100.00% |
static int mmu_audit_set(const char *val, const struct kernel_param *kp)
{
int ret;
unsigned long enable;
ret = kstrtoul(val, 10, &enable);
if (ret < 0)
return -EINVAL;
switch (enable) {
case 0:
mmu_audit_disable();
break;
case 1:
mmu_audit_enable();
break;
default:
return -EINVAL;
}
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 73 | 98.65% | 1 | 50.00% |
Daniel Walter | 1 | 1.35% | 1 | 50.00% |
Total | 74 | 100.00% | 2 | 100.00% |
static const struct kernel_param_ops audit_param_ops = {
.set = mmu_audit_set,
.get = param_get_bool,
};
arch_param_cb(mmu_audit, &audit_param_ops, &mmu_audit, 0644);
Overall Contributors
Person | Tokens | Prop | Commits | CommitProp |
Xiao Guangrong | 1257 | 90.37% | 11 | 40.74% |
Paolo Bonzini | 70 | 5.03% | 3 | 11.11% |
Takuya Yoshikawa | 24 | 1.73% | 4 | 14.81% |
Jan Kiszka | 22 | 1.58% | 1 | 3.70% |
Yu Zhang | 9 | 0.65% | 2 | 7.41% |
Ingo Molnar | 4 | 0.29% | 1 | 3.70% |
Sasha Levin | 1 | 0.07% | 1 | 3.70% |
Dan J Williams | 1 | 0.07% | 1 | 3.70% |
Luis R. Rodriguez | 1 | 0.07% | 1 | 3.70% |
Daniel Walter | 1 | 0.07% | 1 | 3.70% |
Nicolas Kaiser | 1 | 0.07% | 1 | 3.70% |
Total | 1391 | 100.00% | 27 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.