xref: /openbmc/linux/arch/x86/kvm/debugfs.c (revision b54a2377)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Kernel-based Virtual Machine driver for Linux
4  *
5  * Copyright 2016 Red Hat, Inc. and/or its affiliates.
6  */
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/kvm_host.h>
10 #include <linux/debugfs.h>
11 #include "lapic.h"
12 #include "mmu.h"
13 #include "mmu/mmu_internal.h"
14 
15 static int vcpu_get_timer_advance_ns(void *data, u64 *val)
16 {
17 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data;
18 	*val = vcpu->arch.apic->lapic_timer.timer_advance_ns;
19 	return 0;
20 }
21 
22 DEFINE_SIMPLE_ATTRIBUTE(vcpu_timer_advance_ns_fops, vcpu_get_timer_advance_ns, NULL, "%llu\n");
23 
24 static int vcpu_get_guest_mode(void *data, u64 *val)
25 {
26 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data;
27 	*val = vcpu->stat.guest_mode;
28 	return 0;
29 }
30 
31 DEFINE_SIMPLE_ATTRIBUTE(vcpu_guest_mode_fops, vcpu_get_guest_mode, NULL, "%lld\n");
32 
33 static int vcpu_get_tsc_offset(void *data, u64 *val)
34 {
35 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data;
36 	*val = vcpu->arch.tsc_offset;
37 	return 0;
38 }
39 
40 DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_offset_fops, vcpu_get_tsc_offset, NULL, "%lld\n");
41 
42 static int vcpu_get_tsc_scaling_ratio(void *data, u64 *val)
43 {
44 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data;
45 	*val = vcpu->arch.tsc_scaling_ratio;
46 	return 0;
47 }
48 
49 DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_scaling_fops, vcpu_get_tsc_scaling_ratio, NULL, "%llu\n");
50 
51 static int vcpu_get_tsc_scaling_frac_bits(void *data, u64 *val)
52 {
53 	*val = kvm_caps.tsc_scaling_ratio_frac_bits;
54 	return 0;
55 }
56 
57 DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_scaling_frac_fops, vcpu_get_tsc_scaling_frac_bits, NULL, "%llu\n");
58 
59 void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry)
60 {
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);
65 
66 	if (lapic_in_kernel(vcpu))
67 		debugfs_create_file("lapic_timer_advance_ns", 0444,
68 				    debugfs_dentry, vcpu,
69 				    &vcpu_timer_advance_ns_fops);
70 
71 	if (kvm_caps.has_tsc_control) {
72 		debugfs_create_file("tsc-scaling-ratio", 0444,
73 				    debugfs_dentry, vcpu,
74 				    &vcpu_tsc_scaling_fops);
75 		debugfs_create_file("tsc-scaling-ratio-frac-bits", 0444,
76 				    debugfs_dentry, vcpu,
77 				    &vcpu_tsc_scaling_frac_fops);
78 	}
79 }
80 
81 /*
82  * This covers statistics <1024 (11=log(1024)+1), which should be enough to
83  * cover RMAP_RECYCLE_THRESHOLD.
84  */
85 #define  RMAP_LOG_SIZE  11
86 
87 static const char *kvm_lpage_str[KVM_NR_PAGE_SIZES] = { "4K", "2M", "1G" };
88 
89 static int kvm_mmu_rmaps_stat_show(struct seq_file *m, void *v)
90 {
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;
98 	int i, j, k, l, ret;
99 
100 	if (!kvm_memslots_have_rmaps(kvm))
101 		return 0;
102 
103 	ret = -ENOMEM;
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);
107 		if (!log[i])
108 			goto out;
109 	}
110 
111 	mutex_lock(&kvm->slots_lock);
112 	write_lock(&kvm->mmu_lock);
113 
114 	for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
115 		int bkt;
116 
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);
122 				cur = log[k];
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;
127 					cur[index]++;
128 				}
129 			}
130 	}
131 
132 	write_unlock(&kvm->mmu_lock);
133 	mutex_unlock(&kvm->slots_lock);
134 
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++) {
138 		j = 1 << (i - 1);
139 		k = (1 << i) - 1;
140 		seq_printf(m, "%d-%d\t", j, k);
141 	}
142 	seq_printf(m, "\n");
143 
144 	for (i = 0; i < KVM_NR_PAGE_SIZES; i++) {
145 		seq_printf(m, "Level=%s:\t", kvm_lpage_str[i]);
146 		cur = log[i];
147 		for (j = 0; j < RMAP_LOG_SIZE; j++)
148 			seq_printf(m, "%d\t", cur[j]);
149 		seq_printf(m, "\n");
150 	}
151 
152 	ret = 0;
153 out:
154 	for (i = 0; i < KVM_NR_PAGE_SIZES; i++)
155 		kfree(log[i]);
156 
157 	return ret;
158 }
159 
160 static int kvm_mmu_rmaps_stat_open(struct inode *inode, struct file *file)
161 {
162 	struct kvm *kvm = inode->i_private;
163 	int r;
164 
165 	if (!kvm_get_kvm_safe(kvm))
166 		return -ENOENT;
167 
168 	r = single_open(file, kvm_mmu_rmaps_stat_show, kvm);
169 	if (r < 0)
170 		kvm_put_kvm(kvm);
171 
172 	return r;
173 }
174 
175 static int kvm_mmu_rmaps_stat_release(struct inode *inode, struct file *file)
176 {
177 	struct kvm *kvm = inode->i_private;
178 
179 	kvm_put_kvm(kvm);
180 
181 	return single_release(inode, file);
182 }
183 
184 static const struct file_operations mmu_rmaps_stat_fops = {
185 	.open		= kvm_mmu_rmaps_stat_open,
186 	.read		= seq_read,
187 	.llseek		= seq_lseek,
188 	.release	= kvm_mmu_rmaps_stat_release,
189 };
190 
191 int kvm_arch_create_vm_debugfs(struct kvm *kvm)
192 {
193 	debugfs_create_file("mmu_rmaps_stat", 0644, kvm->debugfs_dentry, kvm,
194 			    &mmu_rmaps_stat_fops);
195 	return 0;
196 }
197