Skip to content

Commit ce35ef2

Browse files
sjitindarsinghbonzini
authored andcommitted
kvm/stats: Update kvm stats to clear on write to their debugfs entry
Various kvm vm and vcpu stats are provided via debugfs entries. Currently there is no way to reset these stats back to zero. Add the ability to clear (reset back to zero) these stats on a per stat basis by writing to the debugfs files. Only a write value of 0 is accepted. Signed-off-by: Suraj Jitindar Singh <[email protected]> Signed-off-by: Radim Krčmář <[email protected]>
1 parent 868a32f commit ce35ef2

File tree

1 file changed

+72
-6
lines changed

1 file changed

+72
-6
lines changed

virt/kvm/kvm_main.c

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
595595
stat_data->kvm = kvm;
596596
stat_data->offset = p->offset;
597597
kvm->debugfs_stat_data[p - debugfs_entries] = stat_data;
598-
if (!debugfs_create_file(p->name, 0444,
598+
if (!debugfs_create_file(p->name, 0644,
599599
kvm->debugfs_dentry,
600600
stat_data,
601601
stat_fops_per_vm[p->kind]))
@@ -3663,11 +3663,23 @@ static int vm_stat_get_per_vm(void *data, u64 *val)
36633663
return 0;
36643664
}
36653665

3666+
static int vm_stat_clear_per_vm(void *data, u64 val)
3667+
{
3668+
struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
3669+
3670+
if (val)
3671+
return -EINVAL;
3672+
3673+
*(ulong *)((void *)stat_data->kvm + stat_data->offset) = 0;
3674+
3675+
return 0;
3676+
}
3677+
36663678
static int vm_stat_get_per_vm_open(struct inode *inode, struct file *file)
36673679
{
36683680
__simple_attr_check_format("%llu\n", 0ull);
36693681
return kvm_debugfs_open(inode, file, vm_stat_get_per_vm,
3670-
NULL, "%llu\n");
3682+
vm_stat_clear_per_vm, "%llu\n");
36713683
}
36723684

36733685
static const struct file_operations vm_stat_get_per_vm_fops = {
@@ -3693,11 +3705,26 @@ static int vcpu_stat_get_per_vm(void *data, u64 *val)
36933705
return 0;
36943706
}
36953707

3708+
static int vcpu_stat_clear_per_vm(void *data, u64 val)
3709+
{
3710+
int i;
3711+
struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
3712+
struct kvm_vcpu *vcpu;
3713+
3714+
if (val)
3715+
return -EINVAL;
3716+
3717+
kvm_for_each_vcpu(i, vcpu, stat_data->kvm)
3718+
*(u64 *)((void *)vcpu + stat_data->offset) = 0;
3719+
3720+
return 0;
3721+
}
3722+
36963723
static int vcpu_stat_get_per_vm_open(struct inode *inode, struct file *file)
36973724
{
36983725
__simple_attr_check_format("%llu\n", 0ull);
36993726
return kvm_debugfs_open(inode, file, vcpu_stat_get_per_vm,
3700-
NULL, "%llu\n");
3727+
vcpu_stat_clear_per_vm, "%llu\n");
37013728
}
37023729

37033730
static const struct file_operations vcpu_stat_get_per_vm_fops = {
@@ -3732,7 +3759,26 @@ static int vm_stat_get(void *_offset, u64 *val)
37323759
return 0;
37333760
}
37343761

3735-
DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
3762+
static int vm_stat_clear(void *_offset, u64 val)
3763+
{
3764+
unsigned offset = (long)_offset;
3765+
struct kvm *kvm;
3766+
struct kvm_stat_data stat_tmp = {.offset = offset};
3767+
3768+
if (val)
3769+
return -EINVAL;
3770+
3771+
spin_lock(&kvm_lock);
3772+
list_for_each_entry(kvm, &vm_list, vm_list) {
3773+
stat_tmp.kvm = kvm;
3774+
vm_stat_clear_per_vm((void *)&stat_tmp, 0);
3775+
}
3776+
spin_unlock(&kvm_lock);
3777+
3778+
return 0;
3779+
}
3780+
3781+
DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, vm_stat_clear, "%llu\n");
37363782

37373783
static int vcpu_stat_get(void *_offset, u64 *val)
37383784
{
@@ -3752,7 +3798,27 @@ static int vcpu_stat_get(void *_offset, u64 *val)
37523798
return 0;
37533799
}
37543800

3755-
DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
3801+
static int vcpu_stat_clear(void *_offset, u64 val)
3802+
{
3803+
unsigned offset = (long)_offset;
3804+
struct kvm *kvm;
3805+
struct kvm_stat_data stat_tmp = {.offset = offset};
3806+
3807+
if (val)
3808+
return -EINVAL;
3809+
3810+
spin_lock(&kvm_lock);
3811+
list_for_each_entry(kvm, &vm_list, vm_list) {
3812+
stat_tmp.kvm = kvm;
3813+
vcpu_stat_clear_per_vm((void *)&stat_tmp, 0);
3814+
}
3815+
spin_unlock(&kvm_lock);
3816+
3817+
return 0;
3818+
}
3819+
3820+
DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, vcpu_stat_clear,
3821+
"%llu\n");
37563822

37573823
static const struct file_operations *stat_fops[] = {
37583824
[KVM_STAT_VCPU] = &vcpu_stat_fops,
@@ -3770,7 +3836,7 @@ static int kvm_init_debug(void)
37703836

37713837
kvm_debugfs_num_entries = 0;
37723838
for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) {
3773-
if (!debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
3839+
if (!debugfs_create_file(p->name, 0644, kvm_debugfs_dir,
37743840
(void *)(long)p->offset,
37753841
stat_fops[p->kind]))
37763842
goto out_dir;

0 commit comments

Comments
 (0)