Skip to contentSkip to conversation

Make kvm_device_ops registration dynamic

PATCH In review Will Deacon 2 participants 7 messages Sep 2, 2014 – Sep 16, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover Make kvm_device_ops registration dynamic

Hi all,

This is version 3 of the patches originally posted here:

v1: http://www.spinics.net/lists/kvm-arm/msg10219.html
v2: http://www.spinics.net/lists/kvm/msg105197.html

Changes since v2 include:

- Rebased onto 3.17-rc* (the vgic code changed a lot!)
- Added relevant acks

The mpic, flic and xics are still not ported over, as I don't want to risk breaking those devices (it's not clear at which point they need to be registered).

Thanks,

Will

--->8

Cornelia Huck (1):
KVM: s390: register flic ops dynamically

Will Deacon (3):
KVM: device: add simple registration mechanism for kvm_device_ops
KVM: ARM: vgic: register kvm_device_ops dynamically
KVM: VFIO: register kvm_device_ops dynamically

 arch/s390/kvm/kvm-s390.c |   3 +-
 arch/s390/kvm/kvm-s390.h |   1 +
 include/linux/kvm_host.h |   4 +-
 include/uapi/linux/kvm.h |  22 +++++--
 virt/kvm/arm/vgic.c      | 157 ++++++++++++++++++++++++-----------------------
 virt/kvm/kvm_main.c      |  57 +++++++++--------
 virt/kvm/vfio.c          |  22 ++++---
 7 files changed, 142 insertions(+), 124 deletions(-)
Signature · 1 line
2.1.0
#1 lore raw

PATCH v3 1/4 KVM: device: add simple registration mechanism for kvm_device_ops

kvm_ioctl_create_device currently has knowledge of all the device types and their associated ops. This is fairly inflexible when adding support for new in-kernel device emulations, so move what we currently have out into a table, which can support dynamic registration of ops by new drivers for virtual hardware.

---
 include/linux/kvm_host.h |  1 +
 include/uapi/linux/kvm.h | 22 +++++++++++-----
 virt/kvm/kvm_main.c      | 65 ++++++++++++++++++++++++++++--------------------
 3 files changed, 55 insertions(+), 33 deletions(-)
include/linux/kvm_host.h +1 -0
index a4c33b34fe3f..ca105f919f0b 100644
@@ -1068,6 +1068,7 @@ struct kvm_device_ops {
 void kvm_device_get(struct kvm_device *dev);
 void kvm_device_put(struct kvm_device *dev);
 struct kvm_device *kvm_device_from_filp(struct file *filp);
+int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type);
 
 extern struct kvm_device_ops kvm_mpic_ops;
 extern struct kvm_device_ops kvm_xics_ops;
include/uapi/linux/kvm.h +16 -6
index cf3a2ff440e4..5541024dd5d0 100644
@@ -947,15 +947,25 @@ struct kvm_device_attr {
 	__u64	addr;		/* userspace address of attr data */
 };
 
-#define KVM_DEV_TYPE_FSL_MPIC_20	1
-#define KVM_DEV_TYPE_FSL_MPIC_42	2
-#define KVM_DEV_TYPE_XICS		3
-#define KVM_DEV_TYPE_VFIO		4
 #define  KVM_DEV_VFIO_GROUP			1
 #define   KVM_DEV_VFIO_GROUP_ADD			1
 #define   KVM_DEV_VFIO_GROUP_DEL			2
-#define KVM_DEV_TYPE_ARM_VGIC_V2	5
-#define KVM_DEV_TYPE_FLIC		6
+
+enum kvm_device_type {
+	KVM_DEV_TYPE_FSL_MPIC_20	= 1,
+#define KVM_DEV_TYPE_FSL_MPIC_20	KVM_DEV_TYPE_FSL_MPIC_20
+	KVM_DEV_TYPE_FSL_MPIC_42,
+#define KVM_DEV_TYPE_FSL_MPIC_42	KVM_DEV_TYPE_FSL_MPIC_42
+	KVM_DEV_TYPE_XICS,
+#define KVM_DEV_TYPE_XICS		KVM_DEV_TYPE_XICS
+	KVM_DEV_TYPE_VFIO,
+#define KVM_DEV_TYPE_VFIO		KVM_DEV_TYPE_VFIO
+	KVM_DEV_TYPE_ARM_VGIC_V2,
+#define KVM_DEV_TYPE_ARM_VGIC_V2	KVM_DEV_TYPE_ARM_VGIC_V2
+	KVM_DEV_TYPE_FLIC,
+#define KVM_DEV_TYPE_FLIC		KVM_DEV_TYPE_FLIC
+	KVM_DEV_TYPE_MAX,
+};
 
 /*
  * ioctls for VM fds
virt/kvm/kvm_main.c +38 -27

82 lines not on this page · raw message

Signature · 1 line
2.1.0
#2 lore raw

PATCH v3 2/4 KVM: ARM: vgic: register kvm_device_ops dynamically

In reply to Will Deacon

Now that we have a dynamic means to register kvm_device_ops, use that for the ARM VGIC, instead of relying on the static table.

---
 include/linux/kvm_host.h |   1 -
 virt/kvm/arm/vgic.c      | 157 ++++++++++++++++++++++++-----------------------
 virt/kvm/kvm_main.c      |   4 --
 3 files changed, 79 insertions(+), 83 deletions(-)
include/linux/kvm_host.h +0 -1
index ca105f919f0b..e9a069ddcb2b 100644
@@ -1073,7 +1073,6 @@ int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type);
 extern struct kvm_device_ops kvm_mpic_ops;
 extern struct kvm_device_ops kvm_xics_ops;
 extern struct kvm_device_ops kvm_vfio_ops;
-extern struct kvm_device_ops kvm_arm_vgic_v2_ops;
 extern struct kvm_device_ops kvm_flic_ops;
 
 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
virt/kvm/arm/vgic.c +79 -78

172 lines not on this page · raw message

virt/kvm/kvm_main.c +0 -4
index 81de6768d2c2..6aaa44c1506e 100644
@@ -2273,10 +2273,6 @@ static struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = {
 	[KVM_DEV_TYPE_VFIO]		= &kvm_vfio_ops,
 #endif
 
-#ifdef CONFIG_KVM_ARM_VGIC
-	[KVM_DEV_TYPE_ARM_VGIC_V2]	= &kvm_arm_vgic_v2_ops,
-#endif
-
 #ifdef CONFIG_S390
 	[KVM_DEV_TYPE_FLIC]		= &kvm_flic_ops,
 #endif
Signature · 1 line
2.1.0
#3 lore raw

PATCH v3 3/4 KVM: s390: register flic ops dynamically

In reply to Will Deacon

From: Cornelia Huck <[email protected]>

Using the new kvm_register_device_ops() interface makes us get rid of an #ifdef in common code.

---
 arch/s390/kvm/kvm-s390.c | 3 ++-
 arch/s390/kvm/kvm-s390.h | 1 +
 include/linux/kvm_host.h | 1 -
 virt/kvm/kvm_main.c      | 4 ----
 4 files changed, 3 insertions(+), 6 deletions(-)
arch/s390/kvm/kvm-s390.c +2 -1
index ce81eb2ab76a..63793641393c 100644
@@ -130,7 +130,8 @@ void kvm_arch_check_processor_compat(void *rtn)
 
 int kvm_arch_init(void *opaque)
 {
-	return 0;
+	/* Register floating interrupt controller interface. */
+	return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
 }
 
 void kvm_arch_exit(void)
arch/s390/kvm/kvm-s390.h +1 -0
index 3862fa2cefe0..04e90538c37e 100644
@@ -228,6 +228,7 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu);
 int psw_extint_disabled(struct kvm_vcpu *vcpu);
 void kvm_s390_destroy_adapters(struct kvm *kvm);
 int kvm_s390_si_ext_call_pending(struct kvm_vcpu *vcpu);
+extern struct kvm_device_ops kvm_flic_ops;
 
 /* implemented in guestdbg.c */
 void kvm_s390_backup_guest_per_regs(struct kvm_vcpu *vcpu);
include/linux/kvm_host.h +0 -1
index e9a069ddcb2b..73f796ad0d0b 100644
@@ -1073,7 +1073,6 @@ int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type);
 extern struct kvm_device_ops kvm_mpic_ops;
 extern struct kvm_device_ops kvm_xics_ops;
 extern struct kvm_device_ops kvm_vfio_ops;
-extern struct kvm_device_ops kvm_flic_ops;
 
 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
 
virt/kvm/kvm_main.c +0 -4
index 6aaa44c1506e..78b25a44db53 100644
@@ -2272,10 +2272,6 @@ static struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = {
 #ifdef CONFIG_KVM_VFIO
 	[KVM_DEV_TYPE_VFIO]		= &kvm_vfio_ops,
 #endif
-
-#ifdef CONFIG_S390
-	[KVM_DEV_TYPE_FLIC]		= &kvm_flic_ops,
-#endif
 };
 
 int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type)
Signature · 1 line
2.1.0
#4 lore raw

PATCH v3 4/4 KVM: VFIO: register kvm_device_ops dynamically

In reply to Will Deacon

Now that we have a dynamic means to register kvm_device_ops, use that for the VFIO kvm device, instead of relying on the static table.

This is achieved by a module_init call to register the ops with KVM.

---
 include/linux/kvm_host.h |  1 -
 virt/kvm/kvm_main.c      |  4 ----
 virt/kvm/vfio.c          | 22 +++++++++++++++-------
 3 files changed, 15 insertions(+), 12 deletions(-)
include/linux/kvm_host.h +0 -1
index 73f796ad0d0b..55a1618a19dc 100644
@@ -1072,7 +1072,6 @@ int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type);
 
 extern struct kvm_device_ops kvm_mpic_ops;
 extern struct kvm_device_ops kvm_xics_ops;
-extern struct kvm_device_ops kvm_vfio_ops;
 
 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
 
virt/kvm/kvm_main.c +0 -4
index 78b25a44db53..e9a703eca8c1 100644
@@ -2268,10 +2268,6 @@ static struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = {
 #ifdef CONFIG_KVM_XICS
 	[KVM_DEV_TYPE_XICS]		= &kvm_xics_ops,
 #endif
-
-#ifdef CONFIG_KVM_VFIO
-	[KVM_DEV_TYPE_VFIO]		= &kvm_vfio_ops,
-#endif
 };
 
 int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type)
virt/kvm/vfio.c +15 -7
index ba1a93f935c7..bb11b36ee8a2 100644
@@ -246,6 +246,16 @@ static void kvm_vfio_destroy(struct kvm_device *dev)
 	kfree(dev); /* alloc by kvm_ioctl_create_device, free by .destroy */
 }
 
+static int kvm_vfio_create(struct kvm_device *dev, u32 type);
+
+static struct kvm_device_ops kvm_vfio_ops = {
+	.name = "kvm-vfio",
+	.create = kvm_vfio_create,
+	.destroy = kvm_vfio_destroy,
+	.set_attr = kvm_vfio_set_attr,
+	.has_attr = kvm_vfio_has_attr,
+};
+
 static int kvm_vfio_create(struct kvm_device *dev, u32 type)
 {
 	struct kvm_device *tmp;
@@ -268,10 +278,8 @@ static int kvm_vfio_create(struct kvm_device *dev, u32 type)
 	return 0;
 }
 
-struct kvm_device_ops kvm_vfio_ops = {
-	.name = "kvm-vfio",
-	.create = kvm_vfio_create,
-	.destroy = kvm_vfio_destroy,
-	.set_attr = kvm_vfio_set_attr,
-	.has_attr = kvm_vfio_has_attr,
-};
+static int __init kvm_vfio_ops_init(void)
+{
+	return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
+}
+module_init(kvm_vfio_ops_init);
Signature · 1 line
2.1.0
#5 lore raw
In reply to Will Deacon

Hi Paolo,

Quoted 7 lines · Will Deacon

Hi all,

This is version 3 of the patches originally posted here:

v1: http://www.spinics.net/lists/kvm-arm/msg10219.html
v2: http://www.spinics.net/lists/kvm/msg105197.html

Quoted 8 lines

Changes since v2 include:

- Rebased onto 3.17-rc* (the vgic code changed a lot!)
- Added relevant acks

The mpic, flic and xics are still not ported over, as I don't want to risk breaking those devices (it's not clear at which point they need to be registered).

Any further comments on this lot?

Will

maintainer
#6 lore raw
In reply to Will Deacon

Il 02/09/2014 11:27, Will Deacon ha scritto:

Quoted 2 lines

The mpic, flic and xics are still not ported over, as I don't want to risk breaking those devices

Actually FLIC is ported. :)

Quoted 10 lines
 arch/s390/kvm/kvm-s390.c |   3 +-
 arch/s390/kvm/kvm-s390.h |   1 +
 include/linux/kvm_host.h |   4 +-
 include/uapi/linux/kvm.h |  22 +++++--
 virt/kvm/arm/vgic.c      | 157 ++++++++++++++++++++++++-----------------------
 virt/kvm/kvm_main.c      |  57 +++++++++--------
 virt/kvm/vfio.c          |  22 ++++---
 7 files changed, 142 insertions(+), 124 deletions(-)

Thanks, applying to kvm/queue. Alex (Graf) and Paul, can you look at MPIC and XICS?

Paolo