Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Luiz Capitulino | 235539b | 2016-09-07 14:47:23 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Kernel-based Virtual Machine driver for Linux |
| 4 | * |
| 5 | * Copyright 2016 Red Hat, Inc. and/or its affiliates. |
Luiz Capitulino | 235539b | 2016-09-07 14:47:23 -0400 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/kvm_host.h> |
Luiz Capitulino | 4f5758f | 2016-09-16 10:27:36 -0400 | [diff] [blame] | 8 | #include <linux/debugfs.h> |
Wanpeng Li | 16ba3ab | 2019-05-20 16:18:07 +0800 | [diff] [blame] | 9 | #include "lapic.h" |
Peter Xu | 3bcd066 | 2021-07-30 18:04:52 -0400 | [diff] [blame] | 10 | #include "mmu.h" |
| 11 | #include "mmu/mmu_internal.h" |
Luiz Capitulino | 235539b | 2016-09-07 14:47:23 -0400 | [diff] [blame] | 12 | |
Wanpeng Li | 16ba3ab | 2019-05-20 16:18:07 +0800 | [diff] [blame] | 13 | static int vcpu_get_timer_advance_ns(void *data, u64 *val) |
| 14 | { |
| 15 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data; |
| 16 | *val = vcpu->arch.apic->lapic_timer.timer_advance_ns; |
| 17 | return 0; |
| 18 | } |
| 19 | |
| 20 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_timer_advance_ns_fops, vcpu_get_timer_advance_ns, NULL, "%llu\n"); |
| 21 | |
Krish Sadhukhan | d5a0483 | 2021-06-09 14:03:39 -0400 | [diff] [blame] | 22 | static int vcpu_get_guest_mode(void *data, u64 *val) |
| 23 | { |
| 24 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data; |
| 25 | *val = vcpu->stat.guest_mode; |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_guest_mode_fops, vcpu_get_guest_mode, NULL, "%lld\n"); |
| 30 | |
Luiz Capitulino | 4f5758f | 2016-09-16 10:27:36 -0400 | [diff] [blame] | 31 | static int vcpu_get_tsc_offset(void *data, u64 *val) |
| 32 | { |
| 33 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data; |
| 34 | *val = vcpu->arch.tsc_offset; |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_offset_fops, vcpu_get_tsc_offset, NULL, "%lld\n"); |
| 39 | |
| 40 | static int vcpu_get_tsc_scaling_ratio(void *data, u64 *val) |
| 41 | { |
| 42 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data; |
| 43 | *val = vcpu->arch.tsc_scaling_ratio; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_scaling_fops, vcpu_get_tsc_scaling_ratio, NULL, "%llu\n"); |
| 48 | |
| 49 | static int vcpu_get_tsc_scaling_frac_bits(void *data, u64 *val) |
| 50 | { |
| 51 | *val = kvm_tsc_scaling_ratio_frac_bits; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_scaling_frac_fops, vcpu_get_tsc_scaling_frac_bits, NULL, "%llu\n"); |
| 56 | |
Paolo Bonzini | d56f513 | 2020-06-04 15:16:52 +0200 | [diff] [blame] | 57 | void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry) |
Luiz Capitulino | 235539b | 2016-09-07 14:47:23 -0400 | [diff] [blame] | 58 | { |
Krish Sadhukhan | d5a0483 | 2021-06-09 14:03:39 -0400 | [diff] [blame] | 59 | debugfs_create_file("guest_mode", 0444, debugfs_dentry, vcpu, |
| 60 | &vcpu_guest_mode_fops); |
Paolo Bonzini | d56f513 | 2020-06-04 15:16:52 +0200 | [diff] [blame] | 61 | debugfs_create_file("tsc-offset", 0444, debugfs_dentry, vcpu, |
Greg KH | 3e7093d | 2019-07-31 20:56:20 +0200 | [diff] [blame] | 62 | &vcpu_tsc_offset_fops); |
Luiz Capitulino | 4f5758f | 2016-09-16 10:27:36 -0400 | [diff] [blame] | 63 | |
Greg KH | 3e7093d | 2019-07-31 20:56:20 +0200 | [diff] [blame] | 64 | if (lapic_in_kernel(vcpu)) |
| 65 | debugfs_create_file("lapic_timer_advance_ns", 0444, |
Paolo Bonzini | d56f513 | 2020-06-04 15:16:52 +0200 | [diff] [blame] | 66 | debugfs_dentry, vcpu, |
Greg KH | 3e7093d | 2019-07-31 20:56:20 +0200 | [diff] [blame] | 67 | &vcpu_timer_advance_ns_fops); |
Wanpeng Li | 16ba3ab | 2019-05-20 16:18:07 +0800 | [diff] [blame] | 68 | |
Luiz Capitulino | 4f5758f | 2016-09-16 10:27:36 -0400 | [diff] [blame] | 69 | if (kvm_has_tsc_control) { |
Greg KH | 3e7093d | 2019-07-31 20:56:20 +0200 | [diff] [blame] | 70 | debugfs_create_file("tsc-scaling-ratio", 0444, |
Paolo Bonzini | d56f513 | 2020-06-04 15:16:52 +0200 | [diff] [blame] | 71 | debugfs_dentry, vcpu, |
Greg KH | 3e7093d | 2019-07-31 20:56:20 +0200 | [diff] [blame] | 72 | &vcpu_tsc_scaling_fops); |
| 73 | debugfs_create_file("tsc-scaling-ratio-frac-bits", 0444, |
Paolo Bonzini | d56f513 | 2020-06-04 15:16:52 +0200 | [diff] [blame] | 74 | debugfs_dentry, vcpu, |
Greg KH | 3e7093d | 2019-07-31 20:56:20 +0200 | [diff] [blame] | 75 | &vcpu_tsc_scaling_frac_fops); |
Luiz Capitulino | 4f5758f | 2016-09-16 10:27:36 -0400 | [diff] [blame] | 76 | } |
Luiz Capitulino | 235539b | 2016-09-07 14:47:23 -0400 | [diff] [blame] | 77 | } |
Peter Xu | 3bcd066 | 2021-07-30 18:04:52 -0400 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * This covers statistics <1024 (11=log(1024)+1), which should be enough to |
| 81 | * cover RMAP_RECYCLE_THRESHOLD. |
| 82 | */ |
| 83 | #define RMAP_LOG_SIZE 11 |
| 84 | |
| 85 | static const char *kvm_lpage_str[KVM_NR_PAGE_SIZES] = { "4K", "2M", "1G" }; |
| 86 | |
| 87 | static int kvm_mmu_rmaps_stat_show(struct seq_file *m, void *v) |
| 88 | { |
| 89 | struct kvm_rmap_head *rmap; |
| 90 | struct kvm *kvm = m->private; |
| 91 | struct kvm_memory_slot *slot; |
| 92 | struct kvm_memslots *slots; |
| 93 | unsigned int lpage_size, index; |
| 94 | /* Still small enough to be on the stack */ |
| 95 | unsigned int *log[KVM_NR_PAGE_SIZES], *cur; |
| 96 | int i, j, k, l, ret; |
| 97 | |
Nikunj A Dadhania | fffb532 | 2022-01-05 09:33:37 +0530 | [diff] [blame] | 98 | if (!kvm_memslots_have_rmaps(kvm)) |
| 99 | return 0; |
| 100 | |
Peter Xu | 3bcd066 | 2021-07-30 18:04:52 -0400 | [diff] [blame] | 101 | ret = -ENOMEM; |
| 102 | memset(log, 0, sizeof(log)); |
| 103 | for (i = 0; i < KVM_NR_PAGE_SIZES; i++) { |
| 104 | log[i] = kcalloc(RMAP_LOG_SIZE, sizeof(unsigned int), GFP_KERNEL); |
| 105 | if (!log[i]) |
| 106 | goto out; |
| 107 | } |
| 108 | |
| 109 | mutex_lock(&kvm->slots_lock); |
| 110 | write_lock(&kvm->mmu_lock); |
| 111 | |
| 112 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
Maciej S. Szmigiero | a54d806 | 2021-12-06 20:54:30 +0100 | [diff] [blame] | 113 | int bkt; |
| 114 | |
Peter Xu | 3bcd066 | 2021-07-30 18:04:52 -0400 | [diff] [blame] | 115 | slots = __kvm_memslots(kvm, i); |
Maciej S. Szmigiero | a54d806 | 2021-12-06 20:54:30 +0100 | [diff] [blame] | 116 | kvm_for_each_memslot(slot, bkt, slots) |
Peter Xu | 3bcd066 | 2021-07-30 18:04:52 -0400 | [diff] [blame] | 117 | for (k = 0; k < KVM_NR_PAGE_SIZES; k++) { |
| 118 | rmap = slot->arch.rmap[k]; |
| 119 | lpage_size = kvm_mmu_slot_lpages(slot, k + 1); |
| 120 | cur = log[k]; |
| 121 | for (l = 0; l < lpage_size; l++) { |
| 122 | index = ffs(pte_list_count(&rmap[l])); |
| 123 | if (WARN_ON_ONCE(index >= RMAP_LOG_SIZE)) |
| 124 | index = RMAP_LOG_SIZE - 1; |
| 125 | cur[index]++; |
| 126 | } |
| 127 | } |
Peter Xu | 3bcd066 | 2021-07-30 18:04:52 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | write_unlock(&kvm->mmu_lock); |
| 131 | mutex_unlock(&kvm->slots_lock); |
| 132 | |
| 133 | /* index=0 counts no rmap; index=1 counts 1 rmap */ |
| 134 | seq_printf(m, "Rmap_Count:\t0\t1\t"); |
| 135 | for (i = 2; i < RMAP_LOG_SIZE; i++) { |
| 136 | j = 1 << (i - 1); |
| 137 | k = (1 << i) - 1; |
| 138 | seq_printf(m, "%d-%d\t", j, k); |
| 139 | } |
| 140 | seq_printf(m, "\n"); |
| 141 | |
| 142 | for (i = 0; i < KVM_NR_PAGE_SIZES; i++) { |
| 143 | seq_printf(m, "Level=%s:\t", kvm_lpage_str[i]); |
| 144 | cur = log[i]; |
| 145 | for (j = 0; j < RMAP_LOG_SIZE; j++) |
| 146 | seq_printf(m, "%d\t", cur[j]); |
| 147 | seq_printf(m, "\n"); |
| 148 | } |
| 149 | |
| 150 | ret = 0; |
| 151 | out: |
| 152 | for (i = 0; i < KVM_NR_PAGE_SIZES; i++) |
| 153 | kfree(log[i]); |
| 154 | |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | static int kvm_mmu_rmaps_stat_open(struct inode *inode, struct file *file) |
| 159 | { |
| 160 | struct kvm *kvm = inode->i_private; |
| 161 | |
| 162 | if (!kvm_get_kvm_safe(kvm)) |
| 163 | return -ENOENT; |
| 164 | |
| 165 | return single_open(file, kvm_mmu_rmaps_stat_show, kvm); |
| 166 | } |
| 167 | |
| 168 | static int kvm_mmu_rmaps_stat_release(struct inode *inode, struct file *file) |
| 169 | { |
| 170 | struct kvm *kvm = inode->i_private; |
| 171 | |
| 172 | kvm_put_kvm(kvm); |
| 173 | |
| 174 | return single_release(inode, file); |
| 175 | } |
| 176 | |
| 177 | static const struct file_operations mmu_rmaps_stat_fops = { |
| 178 | .open = kvm_mmu_rmaps_stat_open, |
| 179 | .read = seq_read, |
| 180 | .llseek = seq_lseek, |
| 181 | .release = kvm_mmu_rmaps_stat_release, |
| 182 | }; |
| 183 | |
| 184 | int kvm_arch_create_vm_debugfs(struct kvm *kvm) |
| 185 | { |
| 186 | debugfs_create_file("mmu_rmaps_stat", 0644, kvm->debugfs_dentry, kvm, |
| 187 | &mmu_rmaps_stat_fops); |
| 188 | return 0; |
| 189 | } |