Skip to content

Commit 833b45d

Browse files
committed
kvm: x86, powerpc: do not allow clearing largepages debugfs entry
The largepages debugfs entry is incremented/decremented as shadow pages are created or destroyed. Clearing it will result in an underflow, which is harmless to KVM but ugly (and could be misinterpreted by tools that use debugfs information), so make this particular statistic read-only. Cc: [email protected] Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 2e4a759 commit 833b45d

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

arch/powerpc/kvm/book3s.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
#include "book3s.h"
3737
#include "trace.h"
3838

39-
#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
40-
#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
39+
#define VM_STAT(x, ...) offsetof(struct kvm, stat.x), KVM_STAT_VM, ## __VA_ARGS__
40+
#define VCPU_STAT(x, ...) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU, ## __VA_ARGS__
4141

4242
/* #define EXIT_DEBUG */
4343

@@ -69,8 +69,8 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
6969
{ "pthru_all", VCPU_STAT(pthru_all) },
7070
{ "pthru_host", VCPU_STAT(pthru_host) },
7171
{ "pthru_bad_aff", VCPU_STAT(pthru_bad_aff) },
72-
{ "largepages_2M", VM_STAT(num_2M_pages) },
73-
{ "largepages_1G", VM_STAT(num_1G_pages) },
72+
{ "largepages_2M", VM_STAT(num_2M_pages, .mode = 0444) },
73+
{ "largepages_1G", VM_STAT(num_1G_pages, .mode = 0444) },
7474
{ NULL }
7575
};
7676

arch/x86/kvm/x86.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
9292
static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
9393
#endif
9494

95-
#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
96-
#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
95+
#define VM_STAT(x, ...) offsetof(struct kvm, stat.x), KVM_STAT_VM, ## __VA_ARGS__
96+
#define VCPU_STAT(x, ...) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU, ## __VA_ARGS__
9797

9898
#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
9999
KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
@@ -212,7 +212,7 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
212212
{ "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
213213
{ "mmu_unsync", VM_STAT(mmu_unsync) },
214214
{ "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
215-
{ "largepages", VM_STAT(lpages) },
215+
{ "largepages", VM_STAT(lpages, .mode = 0444) },
216216
{ "max_mmu_page_hash_collisions",
217217
VM_STAT(max_mmu_page_hash_collisions) },
218218
{ NULL }

include/linux/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,13 +1090,15 @@ enum kvm_stat_kind {
10901090

10911091
struct kvm_stat_data {
10921092
int offset;
1093+
int mode;
10931094
struct kvm *kvm;
10941095
};
10951096

10961097
struct kvm_stats_debugfs_item {
10971098
const char *name;
10981099
int offset;
10991100
enum kvm_stat_kind kind;
1101+
int mode;
11001102
};
11011103
extern struct kvm_stats_debugfs_item debugfs_entries[];
11021104
extern struct dentry *kvm_debugfs_dir;

virt/kvm/kvm_main.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,9 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
617617

618618
stat_data->kvm = kvm;
619619
stat_data->offset = p->offset;
620+
stat_data->mode = p->mode ? p->mode : 0644;
620621
kvm->debugfs_stat_data[p - debugfs_entries] = stat_data;
621-
debugfs_create_file(p->name, 0644, kvm->debugfs_dentry,
622+
debugfs_create_file(p->name, stat_data->mode, kvm->debugfs_dentry,
622623
stat_data, stat_fops_per_vm[p->kind]);
623624
}
624625
return 0;
@@ -3929,7 +3930,9 @@ static int kvm_debugfs_open(struct inode *inode, struct file *file,
39293930
if (!refcount_inc_not_zero(&stat_data->kvm->users_count))
39303931
return -ENOENT;
39313932

3932-
if (simple_attr_open(inode, file, get, set, fmt)) {
3933+
if (simple_attr_open(inode, file, get,
3934+
stat_data->mode & S_IWUGO ? set : NULL,
3935+
fmt)) {
39333936
kvm_put_kvm(stat_data->kvm);
39343937
return -ENOMEM;
39353938
}
@@ -4177,7 +4180,8 @@ static void kvm_init_debug(void)
41774180

41784181
kvm_debugfs_num_entries = 0;
41794182
for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) {
4180-
debugfs_create_file(p->name, 0644, kvm_debugfs_dir,
4183+
int mode = p->mode ? p->mode : 0644;
4184+
debugfs_create_file(p->name, mode, kvm_debugfs_dir,
41814185
(void *)(long)p->offset,
41824186
stat_fops[p->kind]);
41834187
}

0 commit comments

Comments
 (0)