Skip to contentSkip to conversation

kvm: add kvm cap values to debugfs

PATCH In review Suraj Jitindar Singh 3 participants 4 messages May 28, 2019 – Jun 20, 2019 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm: add kvm cap values to debugfs

KVM capabilities are used to communicate the availability of certain capabilities to userspace.

It might be nice to know these values without having to add debug printing to the userspace tool consuming this data.

Thus add a file in the kvm per vm debugfs file named "caps" which contains a file for each capability number which when read returns what would be returned when querying that capability.

---

I'm not sure if it'd be better to keep a per arch list of the revelevant capabilities to avoid adding an entry for irrelevant ones. Comments?

---
 include/linux/kvm_host.h |  1 +
 include/uapi/linux/kvm.h |  1 +
 virt/kvm/kvm_main.c      | 31 +++++++++++++++++++++++++++++++
 3 files changed, 33 insertions(+)
include/linux/kvm_host.h +1 -0
index 79fa4426509c..6cb45a8de818 100644
@@ -500,6 +500,7 @@ struct kvm {
 	bool manual_dirty_log_protect;
 	struct dentry *debugfs_dentry;
 	struct kvm_stat_data **debugfs_stat_data;
+	int debugfs_cap_data[KVM_CAP_MAX + 1];
 	struct srcu_struct srcu;
 	struct srcu_struct irq_srcu;
 	pid_t userspace_pid;
include/uapi/linux/kvm.h +1 -0
index 2fe12b40d503..7b5042ec5902 100644
@@ -993,6 +993,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_ARM_SVE 170
 #define KVM_CAP_ARM_PTRAUTH_ADDRESS 171
 #define KVM_CAP_ARM_PTRAUTH_GENERIC 172
+#define KVM_CAP_MAX	KVM_CAP_ARM_PTRAUTH_GENERIC
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
virt/kvm/kvm_main.c +31 -0
index 134ec0283a8a..b85a43263fb7 100644
@@ -117,6 +117,7 @@ EXPORT_SYMBOL_GPL(kvm_debugfs_dir);
 static int kvm_debugfs_num_entries;
 static const struct file_operations *stat_fops_per_vm[];
 
+static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg);
 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
 			   unsigned long arg);
 #ifdef CONFIG_KVM_COMPAT
@@ -594,6 +595,33 @@ static void kvm_destroy_vm_debugfs(struct kvm *kvm)
 	}
 }
 
+static int vm_cap_get(void *data, u64 *val)
+{
+	*val = *((int *) data);
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(cap_fops, vm_cap_get, NULL, "%llu\n");
+
+static void kvm_create_vm_cap_debugfs(struct kvm *kvm)
+{
+	char *dir_name = "caps";
+	struct dentry *dentry_p;
+	int i;
+
+	dentry_p = debugfs_create_dir(dir_name, kvm->debugfs_dentry);
+
+	for (i = 0; i <= KVM_CAP_MAX; i++) {
+		int *cap = &kvm->debugfs_cap_data[i];
+		char file_name[ITOA_MAX_LEN];
+
+		*cap = kvm_vm_ioctl_check_extension_generic(kvm, i);
+		snprintf(file_name, sizeof(file_name), "%d", i);
+		debugfs_create_file(file_name, 0444, dentry_p, cap, &cap_fops);
+	}
+}
+
 static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
 {
 	char dir_name[ITOA_MAX_LEN * 2];
@@ -623,6 +651,9 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
 		debugfs_create_file(p->name, 0644, kvm->debugfs_dentry,
 				    stat_data, stat_fops_per_vm[p->kind]);
 	}
+
+	kvm_create_vm_cap_debugfs(kvm);
+
 	return 0;
 }
 
Signature · 1 line
2.13.6
#1 lore raw
Quoted 77 lines · Suraj Jitindar Singh

KVM capabilities are used to communicate the availability of certain capabilities to userspace.

It might be nice to know these values without having to add debug printing to the userspace tool consuming this data.

Thus add a file in the kvm per vm debugfs file named "caps" which contains a file for each capability number which when read returns what would be returned when querying that capability.

---

I'm not sure if it'd be better to keep a per arch list of the revelevant capabilities to avoid adding an entry for irrelevant ones. Comments?

---
 include/linux/kvm_host.h |  1 +
 include/uapi/linux/kvm.h |  1 +
 virt/kvm/kvm_main.c      | 31 +++++++++++++++++++++++++++++++
 3 files changed, 33 insertions(+)
include/linux/kvm_host.h +1 -0
index 79fa4426509c..6cb45a8de818 100644
@@ -500,6 +500,7 @@ struct kvm {
 	bool manual_dirty_log_protect;
 	struct dentry *debugfs_dentry;
 	struct kvm_stat_data **debugfs_stat_data;
+	int debugfs_cap_data[KVM_CAP_MAX + 1];
 	struct srcu_struct srcu;
 	struct srcu_struct irq_srcu;
 	pid_t userspace_pid;
include/uapi/linux/kvm.h +1 -0
index 2fe12b40d503..7b5042ec5902 100644
@@ -993,6 +993,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_ARM_SVE 170
 #define KVM_CAP_ARM_PTRAUTH_ADDRESS 171
 #define KVM_CAP_ARM_PTRAUTH_GENERIC 172
+#define KVM_CAP_MAX	KVM_CAP_ARM_PTRAUTH_GENERIC
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
virt/kvm/kvm_main.c +16 -0
index 134ec0283a8a..b85a43263fb7 100644
@@ -117,6 +117,7 @@ EXPORT_SYMBOL_GPL(kvm_debugfs_dir);
 static int kvm_debugfs_num_entries;
 static const struct file_operations *stat_fops_per_vm[];
 
+static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg);
 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
 			   unsigned long arg);
 #ifdef CONFIG_KVM_COMPAT
@@ -594,6 +595,33 @@ static void kvm_destroy_vm_debugfs(struct kvm *kvm)
 	}
 }
 
+static int vm_cap_get(void *data, u64 *val)
+{
+	*val = *((int *) data);
+
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(cap_fops, vm_cap_get, NULL, "%llu\n");
+
+static void kvm_create_vm_cap_debugfs(struct kvm *kvm)
+{
+	char *dir_name = "caps";
+	struct dentry *dentry_p;
+	int i;
+

virt/kvm/kvm_main.c static void kvm_destroy_vm_debugfs(struct kvm *kvm) · line 622

Quoted 10 lines
virt/kvm/kvm_main.c +10 -0
+	dentry_p = debugfs_create_dir(dir_name, kvm->debugfs_dentry);
+
+	for (i = 0; i <= KVM_CAP_MAX; i++) {
+		int *cap = &kvm->debugfs_cap_data[i];
+		char file_name[ITOA_MAX_LEN];
+
+		*cap = kvm_vm_ioctl_check_extension_generic(kvm, i);
+		snprintf(file_name, sizeof(file_name), "%d", i);
+		debugfs_create_file(file_name, 0444, dentry_p, cap, &cap_fops);
+	}

So you get the information of the capabilities during creation of the VM. Are there any capabilities that might change over time? I do not know right now, but wouldnt it be better to actually call kvm_vm_ioctl_check_extension_generic in the fops callback?

Quoted 15 lines
virt/kvm/kvm_main.c +5 -0
+}
+
 static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
 {
 	char dir_name[ITOA_MAX_LEN * 2];
@@ -623,6 +651,9 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
 		debugfs_create_file(p->name, 0644, kvm->debugfs_dentry,
 				    stat_data, stat_fops_per_vm[p->kind]);
 	}
+
+	kvm_create_vm_cap_debugfs(kvm);
+
 	return 0;
 }
 
maintainer
#2 lore raw
In reply to Suraj Jitindar Singh
Quoted 5 lines · Suraj Jitindar Singh

KVM capabilities are used to communicate the availability of certain capabilities to userspace.

It might be nice to know these values without having to add debug printing to the userspace tool consuming this data.

Why not write a separate script that prints the capability values?

Paolo

#3 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Suraj Jitindar Singh

KVM capabilities are used to communicate the availability of certain
capabilities to userspace.

It might be nice to know these values without having to add debug printing to the userspace tool consuming this data.

Why not write a separate script that prints the capability values?

I agree that this makes more sense as a standalone userspace script. Dropping this patch.

Thanks,
Suraj

Quoted 2 lines

Paolo