1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
5 * Copyright 2016 Red Hat, Inc. and/or its affiliates.
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/kvm_host.h>
10 #include <linux/debugfs.h>
13 #include "mmu/mmu_internal.h"
15 static int vcpu_get_timer_advance_ns(void *data
, u64
*val
)
17 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*) data
;
18 *val
= vcpu
->arch
.apic
->lapic_timer
.timer_advance_ns
;
22 DEFINE_SIMPLE_ATTRIBUTE(vcpu_timer_advance_ns_fops
, vcpu_get_timer_advance_ns
, NULL
, "%llu\n");
24 static int vcpu_get_guest_mode(void *data
, u64
*val
)
26 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*) data
;
27 *val
= vcpu
->stat
.guest_mode
;
31 DEFINE_SIMPLE_ATTRIBUTE(vcpu_guest_mode_fops
, vcpu_get_guest_mode
, NULL
, "%lld\n");
33 static int vcpu_get_tsc_offset(void *data
, u64
*val
)
35 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*) data
;
36 *val
= vcpu
->arch
.tsc_offset
;
40 DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_offset_fops
, vcpu_get_tsc_offset
, NULL
, "%lld\n");
42 static int vcpu_get_tsc_scaling_ratio(void *data
, u64
*val
)
44 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*) data
;
45 *val
= vcpu
->arch
.tsc_scaling_ratio
;
49 DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_scaling_fops
, vcpu_get_tsc_scaling_ratio
, NULL
, "%llu\n");
51 static int vcpu_get_tsc_scaling_frac_bits(void *data
, u64
*val
)
53 *val
= kvm_caps
.tsc_scaling_ratio_frac_bits
;
57 DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_scaling_frac_fops
, vcpu_get_tsc_scaling_frac_bits
, NULL
, "%llu\n");
59 void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu
*vcpu
, struct dentry
*debugfs_dentry
)
61 debugfs_create_file("guest_mode", 0444, debugfs_dentry
, vcpu
,
62 &vcpu_guest_mode_fops
);
63 debugfs_create_file("tsc-offset", 0444, debugfs_dentry
, vcpu
,
64 &vcpu_tsc_offset_fops
);
66 if (lapic_in_kernel(vcpu
))
67 debugfs_create_file("lapic_timer_advance_ns", 0444,
69 &vcpu_timer_advance_ns_fops
);
71 if (kvm_caps
.has_tsc_control
) {
72 debugfs_create_file("tsc-scaling-ratio", 0444,
74 &vcpu_tsc_scaling_fops
);
75 debugfs_create_file("tsc-scaling-ratio-frac-bits", 0444,
77 &vcpu_tsc_scaling_frac_fops
);
82 * This covers statistics <1024 (11=log(1024)+1), which should be enough to
83 * cover RMAP_RECYCLE_THRESHOLD.
85 #define RMAP_LOG_SIZE 11
87 static const char *kvm_lpage_str
[KVM_NR_PAGE_SIZES
] = { "4K", "2M", "1G" };
89 static int kvm_mmu_rmaps_stat_show(struct seq_file
*m
, void *v
)
91 struct kvm_rmap_head
*rmap
;
92 struct kvm
*kvm
= m
->private;
93 struct kvm_memory_slot
*slot
;
94 struct kvm_memslots
*slots
;
95 unsigned int lpage_size
, index
;
96 /* Still small enough to be on the stack */
97 unsigned int *log
[KVM_NR_PAGE_SIZES
], *cur
;
100 if (!kvm_memslots_have_rmaps(kvm
))
104 memset(log
, 0, sizeof(log
));
105 for (i
= 0; i
< KVM_NR_PAGE_SIZES
; i
++) {
106 log
[i
] = kcalloc(RMAP_LOG_SIZE
, sizeof(unsigned int), GFP_KERNEL
);
111 mutex_lock(&kvm
->slots_lock
);
112 write_lock(&kvm
->mmu_lock
);
114 for (i
= 0; i
< kvm_arch_nr_memslot_as_ids(kvm
); i
++) {
117 slots
= __kvm_memslots(kvm
, i
);
118 kvm_for_each_memslot(slot
, bkt
, slots
)
119 for (k
= 0; k
< KVM_NR_PAGE_SIZES
; k
++) {
120 rmap
= slot
->arch
.rmap
[k
];
121 lpage_size
= kvm_mmu_slot_lpages(slot
, k
+ 1);
123 for (l
= 0; l
< lpage_size
; l
++) {
124 index
= ffs(pte_list_count(&rmap
[l
]));
125 if (WARN_ON_ONCE(index
>= RMAP_LOG_SIZE
))
126 index
= RMAP_LOG_SIZE
- 1;
132 write_unlock(&kvm
->mmu_lock
);
133 mutex_unlock(&kvm
->slots_lock
);
135 /* index=0 counts no rmap; index=1 counts 1 rmap */
136 seq_printf(m
, "Rmap_Count:\t0\t1\t");
137 for (i
= 2; i
< RMAP_LOG_SIZE
; i
++) {
140 seq_printf(m
, "%d-%d\t", j
, k
);
144 for (i
= 0; i
< KVM_NR_PAGE_SIZES
; i
++) {
145 seq_printf(m
, "Level=%s:\t", kvm_lpage_str
[i
]);
147 for (j
= 0; j
< RMAP_LOG_SIZE
; j
++)
148 seq_printf(m
, "%d\t", cur
[j
]);
154 for (i
= 0; i
< KVM_NR_PAGE_SIZES
; i
++)
160 static int kvm_mmu_rmaps_stat_open(struct inode
*inode
, struct file
*file
)
162 struct kvm
*kvm
= inode
->i_private
;
165 if (!kvm_get_kvm_safe(kvm
))
168 r
= single_open(file
, kvm_mmu_rmaps_stat_show
, kvm
);
175 static int kvm_mmu_rmaps_stat_release(struct inode
*inode
, struct file
*file
)
177 struct kvm
*kvm
= inode
->i_private
;
181 return single_release(inode
, file
);
184 static const struct file_operations mmu_rmaps_stat_fops
= {
185 .owner
= THIS_MODULE
,
186 .open
= kvm_mmu_rmaps_stat_open
,
189 .release
= kvm_mmu_rmaps_stat_release
,
192 void kvm_arch_create_vm_debugfs(struct kvm
*kvm
)
194 debugfs_create_file("mmu_rmaps_stat", 0644, kvm
->debugfs_dentry
, kvm
,
195 &mmu_rmaps_stat_fops
);