Skip to content

Commit 09cbcef

Browse files
Milan Pandurovbonzini
Milan Pandurov
authored andcommitted
kvm: Refactor handling of VM debugfs files
We can store reference to kvm_stats_debugfs_item instead of copying its values to kvm_stat_data. This allows us to remove duplicated code and usage of temporary kvm_stat_data inside vm_stat_get et al. Signed-off-by: Milan Pandurov <[email protected]> Reviewed-by: Alexander Graf <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent e30a7d6 commit 09cbcef

File tree

2 files changed

+76
-73
lines changed

2 files changed

+76
-73
lines changed

include/linux/kvm_host.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -1108,9 +1108,8 @@ enum kvm_stat_kind {
11081108
};
11091109

11101110
struct kvm_stat_data {
1111-
int offset;
1112-
int mode;
11131111
struct kvm *kvm;
1112+
struct kvm_stats_debugfs_item *dbgfs_item;
11141113
};
11151114

11161115
struct kvm_stats_debugfs_item {
@@ -1119,6 +1118,10 @@ struct kvm_stats_debugfs_item {
11191118
enum kvm_stat_kind kind;
11201119
int mode;
11211120
};
1121+
1122+
#define KVM_DBGFS_GET_MODE(dbgfs_item) \
1123+
((dbgfs_item)->mode ? (dbgfs_item)->mode : 0644)
1124+
11221125
extern struct kvm_stats_debugfs_item debugfs_entries[];
11231126
extern struct dentry *kvm_debugfs_dir;
11241127

virt/kvm/kvm_main.c

+71-71
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct dentry *kvm_debugfs_dir;
113113
EXPORT_SYMBOL_GPL(kvm_debugfs_dir);
114114

115115
static int kvm_debugfs_num_entries;
116-
static const struct file_operations *stat_fops_per_vm[];
116+
static const struct file_operations stat_fops_per_vm;
117117

118118
static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
119119
unsigned long arg);
@@ -650,11 +650,11 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
650650
return -ENOMEM;
651651

652652
stat_data->kvm = kvm;
653-
stat_data->offset = p->offset;
654-
stat_data->mode = p->mode ? p->mode : 0644;
653+
stat_data->dbgfs_item = p;
655654
kvm->debugfs_stat_data[p - debugfs_entries] = stat_data;
656-
debugfs_create_file(p->name, stat_data->mode, kvm->debugfs_dentry,
657-
stat_data, stat_fops_per_vm[p->kind]);
655+
debugfs_create_file(p->name, KVM_DBGFS_GET_MODE(p),
656+
kvm->debugfs_dentry, stat_data,
657+
&stat_fops_per_vm);
658658
}
659659
return 0;
660660
}
@@ -4010,8 +4010,9 @@ static int kvm_debugfs_open(struct inode *inode, struct file *file,
40104010
return -ENOENT;
40114011

40124012
if (simple_attr_open(inode, file, get,
4013-
stat_data->mode & S_IWUGO ? set : NULL,
4014-
fmt)) {
4013+
KVM_DBGFS_GET_MODE(stat_data->dbgfs_item) & 0222
4014+
? set : NULL,
4015+
fmt)) {
40154016
kvm_put_kvm(stat_data->kvm);
40164017
return -ENOMEM;
40174018
}
@@ -4030,105 +4031,111 @@ static int kvm_debugfs_release(struct inode *inode, struct file *file)
40304031
return 0;
40314032
}
40324033

4033-
static int vm_stat_get_per_vm(void *data, u64 *val)
4034+
static int kvm_get_stat_per_vm(struct kvm *kvm, size_t offset, u64 *val)
40344035
{
4035-
struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
4036+
*val = *(ulong *)((void *)kvm + offset);
40364037

4037-
*val = *(ulong *)((void *)stat_data->kvm + stat_data->offset);
4038+
return 0;
4039+
}
4040+
4041+
static int kvm_clear_stat_per_vm(struct kvm *kvm, size_t offset)
4042+
{
4043+
*(ulong *)((void *)kvm + offset) = 0;
40384044

40394045
return 0;
40404046
}
40414047

4042-
static int vm_stat_clear_per_vm(void *data, u64 val)
4048+
static int kvm_get_stat_per_vcpu(struct kvm *kvm, size_t offset, u64 *val)
40434049
{
4044-
struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
4050+
int i;
4051+
struct kvm_vcpu *vcpu;
40454052

4046-
if (val)
4047-
return -EINVAL;
4053+
*val = 0;
40484054

4049-
*(ulong *)((void *)stat_data->kvm + stat_data->offset) = 0;
4055+
kvm_for_each_vcpu(i, vcpu, kvm)
4056+
*val += *(u64 *)((void *)vcpu + offset);
40504057

40514058
return 0;
40524059
}
40534060

4054-
static int vm_stat_get_per_vm_open(struct inode *inode, struct file *file)
4061+
static int kvm_clear_stat_per_vcpu(struct kvm *kvm, size_t offset)
40554062
{
4056-
__simple_attr_check_format("%llu\n", 0ull);
4057-
return kvm_debugfs_open(inode, file, vm_stat_get_per_vm,
4058-
vm_stat_clear_per_vm, "%llu\n");
4059-
}
4063+
int i;
4064+
struct kvm_vcpu *vcpu;
40604065

4061-
static const struct file_operations vm_stat_get_per_vm_fops = {
4062-
.owner = THIS_MODULE,
4063-
.open = vm_stat_get_per_vm_open,
4064-
.release = kvm_debugfs_release,
4065-
.read = simple_attr_read,
4066-
.write = simple_attr_write,
4067-
.llseek = no_llseek,
4068-
};
4066+
kvm_for_each_vcpu(i, vcpu, kvm)
4067+
*(u64 *)((void *)vcpu + offset) = 0;
4068+
4069+
return 0;
4070+
}
40694071

4070-
static int vcpu_stat_get_per_vm(void *data, u64 *val)
4072+
static int kvm_stat_data_get(void *data, u64 *val)
40714073
{
4072-
int i;
4074+
int r = -EFAULT;
40734075
struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
4074-
struct kvm_vcpu *vcpu;
4075-
4076-
*val = 0;
40774076

4078-
kvm_for_each_vcpu(i, vcpu, stat_data->kvm)
4079-
*val += *(u64 *)((void *)vcpu + stat_data->offset);
4077+
switch (stat_data->dbgfs_item->kind) {
4078+
case KVM_STAT_VM:
4079+
r = kvm_get_stat_per_vm(stat_data->kvm,
4080+
stat_data->dbgfs_item->offset, val);
4081+
break;
4082+
case KVM_STAT_VCPU:
4083+
r = kvm_get_stat_per_vcpu(stat_data->kvm,
4084+
stat_data->dbgfs_item->offset, val);
4085+
break;
4086+
}
40804087

4081-
return 0;
4088+
return r;
40824089
}
40834090

4084-
static int vcpu_stat_clear_per_vm(void *data, u64 val)
4091+
static int kvm_stat_data_clear(void *data, u64 val)
40854092
{
4086-
int i;
4093+
int r = -EFAULT;
40874094
struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
4088-
struct kvm_vcpu *vcpu;
40894095

40904096
if (val)
40914097
return -EINVAL;
40924098

4093-
kvm_for_each_vcpu(i, vcpu, stat_data->kvm)
4094-
*(u64 *)((void *)vcpu + stat_data->offset) = 0;
4099+
switch (stat_data->dbgfs_item->kind) {
4100+
case KVM_STAT_VM:
4101+
r = kvm_clear_stat_per_vm(stat_data->kvm,
4102+
stat_data->dbgfs_item->offset);
4103+
break;
4104+
case KVM_STAT_VCPU:
4105+
r = kvm_clear_stat_per_vcpu(stat_data->kvm,
4106+
stat_data->dbgfs_item->offset);
4107+
break;
4108+
}
40954109

4096-
return 0;
4110+
return r;
40974111
}
40984112

4099-
static int vcpu_stat_get_per_vm_open(struct inode *inode, struct file *file)
4113+
static int kvm_stat_data_open(struct inode *inode, struct file *file)
41004114
{
41014115
__simple_attr_check_format("%llu\n", 0ull);
4102-
return kvm_debugfs_open(inode, file, vcpu_stat_get_per_vm,
4103-
vcpu_stat_clear_per_vm, "%llu\n");
4116+
return kvm_debugfs_open(inode, file, kvm_stat_data_get,
4117+
kvm_stat_data_clear, "%llu\n");
41044118
}
41054119

4106-
static const struct file_operations vcpu_stat_get_per_vm_fops = {
4107-
.owner = THIS_MODULE,
4108-
.open = vcpu_stat_get_per_vm_open,
4120+
static const struct file_operations stat_fops_per_vm = {
4121+
.owner = THIS_MODULE,
4122+
.open = kvm_stat_data_open,
41094123
.release = kvm_debugfs_release,
4110-
.read = simple_attr_read,
4111-
.write = simple_attr_write,
4112-
.llseek = no_llseek,
4113-
};
4114-
4115-
static const struct file_operations *stat_fops_per_vm[] = {
4116-
[KVM_STAT_VCPU] = &vcpu_stat_get_per_vm_fops,
4117-
[KVM_STAT_VM] = &vm_stat_get_per_vm_fops,
4124+
.read = simple_attr_read,
4125+
.write = simple_attr_write,
4126+
.llseek = no_llseek,
41184127
};
41194128

41204129
static int vm_stat_get(void *_offset, u64 *val)
41214130
{
41224131
unsigned offset = (long)_offset;
41234132
struct kvm *kvm;
4124-
struct kvm_stat_data stat_tmp = {.offset = offset};
41254133
u64 tmp_val;
41264134

41274135
*val = 0;
41284136
mutex_lock(&kvm_lock);
41294137
list_for_each_entry(kvm, &vm_list, vm_list) {
4130-
stat_tmp.kvm = kvm;
4131-
vm_stat_get_per_vm((void *)&stat_tmp, &tmp_val);
4138+
kvm_get_stat_per_vm(kvm, offset, &tmp_val);
41324139
*val += tmp_val;
41334140
}
41344141
mutex_unlock(&kvm_lock);
@@ -4139,15 +4146,13 @@ static int vm_stat_clear(void *_offset, u64 val)
41394146
{
41404147
unsigned offset = (long)_offset;
41414148
struct kvm *kvm;
4142-
struct kvm_stat_data stat_tmp = {.offset = offset};
41434149

41444150
if (val)
41454151
return -EINVAL;
41464152

41474153
mutex_lock(&kvm_lock);
41484154
list_for_each_entry(kvm, &vm_list, vm_list) {
4149-
stat_tmp.kvm = kvm;
4150-
vm_stat_clear_per_vm((void *)&stat_tmp, 0);
4155+
kvm_clear_stat_per_vm(kvm, offset);
41514156
}
41524157
mutex_unlock(&kvm_lock);
41534158

@@ -4160,14 +4165,12 @@ static int vcpu_stat_get(void *_offset, u64 *val)
41604165
{
41614166
unsigned offset = (long)_offset;
41624167
struct kvm *kvm;
4163-
struct kvm_stat_data stat_tmp = {.offset = offset};
41644168
u64 tmp_val;
41654169

41664170
*val = 0;
41674171
mutex_lock(&kvm_lock);
41684172
list_for_each_entry(kvm, &vm_list, vm_list) {
4169-
stat_tmp.kvm = kvm;
4170-
vcpu_stat_get_per_vm((void *)&stat_tmp, &tmp_val);
4173+
kvm_get_stat_per_vcpu(kvm, offset, &tmp_val);
41714174
*val += tmp_val;
41724175
}
41734176
mutex_unlock(&kvm_lock);
@@ -4178,15 +4181,13 @@ static int vcpu_stat_clear(void *_offset, u64 val)
41784181
{
41794182
unsigned offset = (long)_offset;
41804183
struct kvm *kvm;
4181-
struct kvm_stat_data stat_tmp = {.offset = offset};
41824184

41834185
if (val)
41844186
return -EINVAL;
41854187

41864188
mutex_lock(&kvm_lock);
41874189
list_for_each_entry(kvm, &vm_list, vm_list) {
4188-
stat_tmp.kvm = kvm;
4189-
vcpu_stat_clear_per_vm((void *)&stat_tmp, 0);
4190+
kvm_clear_stat_per_vcpu(kvm, offset);
41904191
}
41914192
mutex_unlock(&kvm_lock);
41924193

@@ -4259,9 +4260,8 @@ static void kvm_init_debug(void)
42594260

42604261
kvm_debugfs_num_entries = 0;
42614262
for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) {
4262-
int mode = p->mode ? p->mode : 0644;
4263-
debugfs_create_file(p->name, mode, kvm_debugfs_dir,
4264-
(void *)(long)p->offset,
4263+
debugfs_create_file(p->name, KVM_DBGFS_GET_MODE(p),
4264+
kvm_debugfs_dir, (void *)(long)p->offset,
42654265
stat_fops[p->kind]);
42664266
}
42674267
}

0 commit comments

Comments
 (0)