Skip to contentSkip to conversation

iosignalfd

PATCH Superseded Gregory Haskins 2 participants 10 messages May 15, 2009 – May 18, 2009

Subsystems (4)

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v2 cover iosignalfd

[

Applies to kvm.git:833367b57c plus the irqfd patch, v8, as posted here:

http://lkml.org/lkml/2009/5/14/258

]

This is v2 of the series. For more details, please see the header to patch 4/4.

[
Changelog:

v2:
*) added optional data-matching capability (via cookie field) *) changed name from iofd to iosignalfd
*) added io_bus unregister function
*) implemented deassign feature

v1:
*) original release (integrated into irqfd v7 series as "iofd")
]

---

Gregory Haskins (4):
kvm: add iosignalfd support
kvm: add io_bus unregister function
kvm: add return value to kvm_io_bus_register_dev
eventfd: export eventfd interfaces for module use

 arch/x86/kvm/i8254.c      |    7 +-
 arch/x86/kvm/i8259.c      |    5 +
 fs/eventfd.c              |    3 +
 include/linux/kvm.h       |   15 ++++
 include/linux/kvm_host.h  |   10 ++-
 virt/kvm/coalesced_mmio.c |    4 +
 virt/kvm/eventfd.c        |  154 +++++++++++++++++++++++++++++++++++++++++++++
 virt/kvm/ioapic.c         |    4 +
 virt/kvm/kvm_main.c       |   62 ++++++++++++++++--
 9 files changed, 249 insertions(+), 15 deletions(-)
Signature · 1 line
Signature
#1 lore raw

PATCH v2 1/4 eventfd: export eventfd interfaces for module use

We want to use eventfd from KVM which can be compiled as a module, so export the interfaces.

---

 fs/eventfd.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
fs/eventfd.c +3 -0
index 2a701d5..3f0e197 100644
@@ -16,6 +16,7 @@
 #include <linux/anon_inodes.h>
 #include <linux/eventfd.h>
 #include <linux/syscalls.h>
+#include <linux/module.h>
 
 struct eventfd_ctx {
 	wait_queue_head_t wqh;
@@ -56,6 +57,7 @@ int eventfd_signal(struct file *file, int n)
 
 	return n;
 }
+EXPORT_SYMBOL_GPL(eventfd_signal);
 
 static int eventfd_release(struct inode *inode, struct file *file)
 {
@@ -197,6 +199,7 @@ struct file *eventfd_fget(int fd)
 
 	return file;
 }
+EXPORT_SYMBOL_GPL(eventfd_fget);
 
 SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
 {
#2 lore raw

PATCH v2 2/4 kvm: add return value to kvm_io_bus_register_dev

In reply to Gregory Haskins

Today this function returns void and will internally BUG_ON if it fails. We want to create dynamic MMIO/PIO entries driven from userspace later in the series, so enhance this API to return an error code on failure.

We also fix up all the callsites to check the return code and BUG_ON if it fails.

The net result should be identical behavior both before and after this patch. We are simply laying the groundwork for the dynamic usage

---

 arch/x86/kvm/i8254.c      |    7 +++++--
 arch/x86/kvm/i8259.c      |    5 ++++-
 include/linux/kvm_host.h  |    4 ++--
 virt/kvm/coalesced_mmio.c |    4 +++-
 virt/kvm/ioapic.c         |    4 +++-
 virt/kvm/kvm_main.c       |    7 +++++--
 6 files changed, 22 insertions(+), 9 deletions(-)
arch/x86/kvm/i8254.c +5 -2
index 4d6f0d2..cc274d6 100644
@@ -564,6 +564,7 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm)
 {
 	struct kvm_pit *pit;
 	struct kvm_kpit_state *pit_state;
+	int ret;
 
 	pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
 	if (!pit)
@@ -584,13 +585,15 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm)
 	pit->dev.write = pit_ioport_write;
 	pit->dev.in_range = pit_in_range;
 	pit->dev.private = pit;
-	kvm_io_bus_register_dev(&kvm->pio_bus, &pit->dev);
+	ret = kvm_io_bus_register_dev(&kvm->pio_bus, &pit->dev);
+	BUG_ON(ret < 0);
 
 	pit->speaker_dev.read = speaker_ioport_read;
 	pit->speaker_dev.write = speaker_ioport_write;
 	pit->speaker_dev.in_range = speaker_in_range;
 	pit->speaker_dev.private = pit;
-	kvm_io_bus_register_dev(&kvm->pio_bus, &pit->speaker_dev);
+	ret = kvm_io_bus_register_dev(&kvm->pio_bus, &pit->speaker_dev);
+	BUG_ON(ret < 0);
 
 	kvm->arch.vpit = pit;
 	pit->kvm = kvm;
arch/x86/kvm/i8259.c +4 -1
index 1ccb50c..7d39b5b 100644
@@ -519,6 +519,8 @@ static void pic_irq_request(void *opaque, int level)
 struct kvm_pic *kvm_create_pic(struct kvm *kvm)
 {
 	struct kvm_pic *s;
+	int ret;
+
 	s = kzalloc(sizeof(struct kvm_pic), GFP_KERNEL);
 	if (!s)
 		return NULL;
@@ -538,6 +540,7 @@ struct kvm_pic *kvm_create_pic(struct kvm *kvm)
 	s->dev.write = picdev_write;
 	s->dev.in_range = picdev_in_range;
 	s->dev.private = s;
-	kvm_io_bus_register_dev(&kvm->pio_bus, &s->dev);
+	ret = kvm_io_bus_register_dev(&kvm->pio_bus, &s->dev);
+	BUG_ON(ret < 0);
 	return s;
 }
include/linux/kvm_host.h +2 -2
index dc91610..94c1a11 100644
@@ -61,8 +61,8 @@ void kvm_io_bus_init(struct kvm_io_bus *bus);
 void kvm_io_bus_destroy(struct kvm_io_bus *bus);
 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
 					  gpa_t addr, int len, int is_write);
-void kvm_io_bus_register_dev(struct kvm_io_bus *bus,
-			     struct kvm_io_device *dev);
+int kvm_io_bus_register_dev(struct kvm_io_bus *bus,
+			    struct kvm_io_device *dev);
 
 struct kvm_vcpu {
 	struct kvm *kvm;
virt/kvm/coalesced_mmio.c +3 -1
index 5ae620d..19945e1 100644
@@ -86,6 +86,7 @@ static void coalesced_mmio_destructor(struct kvm_io_device *this)
 int kvm_coalesced_mmio_init(struct kvm *kvm)
 {
 	struct kvm_coalesced_mmio_dev *dev;
+	int ret;
 
 	dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
 	if (!dev)
@@ -96,7 +97,8 @@ int kvm_coalesced_mmio_init(struct kvm *kvm)
 	dev->dev.private  = dev;
 	dev->kvm = kvm;
 	kvm->coalesced_mmio_dev = dev;
-	kvm_io_bus_register_dev(&kvm->mmio_bus, &dev->dev);
+	ret = kvm_io_bus_register_dev(&kvm->mmio_bus, &dev->dev);
+	BUG_ON(ret < 0);
 
 	return 0;
 }
virt/kvm/ioapic.c +3 -1
index 1eddae9..3eee4c9 100644
@@ -317,6 +317,7 @@ void kvm_ioapic_reset(struct kvm_ioapic *ioapic)
 int kvm_ioapic_init(struct kvm *kvm)
 {
 	struct kvm_ioapic *ioapic;
+	int ret;
 
 	ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
 	if (!ioapic)
@@ -328,7 +329,8 @@ int kvm_ioapic_init(struct kvm *kvm)
 	ioapic->dev.in_range = ioapic_in_range;
 	ioapic->dev.private = ioapic;
 	ioapic->kvm = kvm;
-	kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);
+	ret = kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);
+	BUG_ON(ret < 0);
 	return 0;
 }
 
virt/kvm/kvm_main.c +5 -2
index b2db766..60ba0cf 100644
@@ -2461,11 +2461,14 @@ struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
 	return NULL;
 }
 
-void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
+int kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
 {
-	BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
+	if (bus->dev_count > (NR_IOBUS_DEVS-1))
+		return -ENOSPC;
 
 	bus->devs[bus->dev_count++] = dev;
+
+	return 0;
 }
 
 static struct notifier_block kvm_cpu_notifier = {
#3 lore raw

Re: [KVM PATCH v2 2/4] kvm: add return value to kvm_io_bus_register_dev

Quoted 10 lines · Gregory Haskins

Today this function returns void and will internally BUG_ON if it fails. We want to create dynamic MMIO/PIO entries driven from userspace later in the series, so enhance this API to return an error code on failure.

We also fix up all the callsites to check the return code and BUG_ON if it fails.

The net result should be identical behavior both before and after this patch. We are simply laying the groundwork for the dynamic usage

What happens if the dynamic user gets in there first, then the internal device (which is set up by userspace)? We'll just BUG().

Need to handle the failure gracefully, more or less.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#4 lore raw

Re: [KVM PATCH v2 2/4] kvm: add return value to kvm_io_bus_register_dev

Quoted 9 lines · Avi Kivity
Quoted 8 lines · Gregory Haskins

Today this function returns void and will internally BUG_ON if it fails. We want to create dynamic MMIO/PIO entries driven from userspace later in
the series, so enhance this API to return an error code on failure.

We also fix up all the callsites to check the return code and BUG_ON if it fails.

Quoted 6 lines
Quoted 3 lines

The net result should be identical behavior both before and after this patch. We are simply laying the groundwork for the dynamic usage

What happens if the dynamic user gets in there first, then the internal device (which is set up by userspace)? We'll just BUG().

Hmm..I was assuming the in-kernel stuff would have completed in early init before the dynamic stuff could even get in there. But thinking about it some more, you are right. Theres no reason why something like a iosignalfd ioctl couldnt come down before, for instance, enabling the in-kernel PIC. My bad.

Quoted 3 lines

Need to handle the failure gracefully, more or less.

Yeah, agreed. Will fix in next rev.

-Greg

#5 lore raw

PATCH v2 3/4 kvm: add io_bus unregister function

In reply to Gregory Haskins

We want to support the notion of dynamic MMIO/PIO registrations and therefore will need to support both register as well as unregister.

However, the current io_bus code is structured as a linear array and is not conducive to unregistering, so refactor to allow "holes" in the array. We then enhance the API with an unregister function.

---

 include/linux/kvm_host.h |    4 +++-
 virt/kvm/kvm_main.c      |   48 ++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 43 insertions(+), 9 deletions(-)
include/linux/kvm_host.h +3 -1
index 94c1a11..214089f 100644
@@ -52,7 +52,7 @@ extern struct kmem_cache *kvm_vcpu_cache;
  * in one place.
  */
 struct kvm_io_bus {
-	int                   dev_count;
+	spinlock_t lock;
 #define NR_IOBUS_DEVS 6
 	struct kvm_io_device *devs[NR_IOBUS_DEVS];
 };
@@ -63,6 +63,8 @@ struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
 					  gpa_t addr, int len, int is_write);
 int kvm_io_bus_register_dev(struct kvm_io_bus *bus,
 			    struct kvm_io_device *dev);
+int kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
+			    struct kvm_io_device *dev);
 
 struct kvm_vcpu {
 	struct kvm *kvm;
virt/kvm/kvm_main.c +40 -8

78 lines not on this page · raw message

#6 lore raw

PATCH v2 4/4 kvm: add iosignalfd support

In reply to Gregory Haskins

iosignalfd is a mechanism to register PIO/MMIO regions to trigger an eventfd signal when written to by a guest. Host userspace can register any arbitrary IO address with a corresponding eventfd and then pass the eventfd to a specific end-point of interest for handling.

Normal IO requires a blocking round-trip since the operation may cause side-effects in the emulated model or may return data to the caller. Therefore, an IO in KVM traps from the guest to the host, causes a VMX/SVM "heavy-weight" exit back to userspace, and is ultimately serviced by qemu's device model synchronously before returning control back to the vcpu.

However, there is a subclass of IO which acts purely as a trigger for other IO (such as to kick off an out-of-band DMA request, etc). For these patterns, the synchronous call is particularly expensive since we really only want to simply get our notification transmitted asychronously and return as quickly as possible. All the sychronous infrastructure to ensure proper data-dependencies are met in the normal IO case are just unecessary overhead for signalling. This adds additional computational load on the system, as well as latency to the signalling path.

Therefore, we provide a mechanism for registration of an in-kernel trigger point that allows the VCPU to only require a very brief, lightweight exit just long enough to signal an eventfd. This also means that any clients compatible with the eventfd interface (which includes userspace and kernelspace equally well) can now register to be notified. The end result should be a more flexible and higher performance notification API for the backend KVM hypervisor and perhipheral components.

To test this theory, we built a test-harness called "doorbell". This module has a function called "doorbell_ring()" which simply increments a counter for each time the doorbell is signaled. It supports signalling from either an eventfd, or an ioctl().

We then wired up two paths to the doorbell: One via QEMU via a registered io region and through the doorbell ioctl(). The other is direct via iosignalfd.

You can download this test harness here:

ftp://ftp.novell.com/dev/ghaskins/doorbell.tar.bz2

The measured results are as follows:

qemu-mmio:       110000 iops, 9.09us rtt
iosignalfd-mmio: 200100 iops, 5.00us rtt
iosignalfd-pio:  367300 iops, 2.72us rtt

I didn't measure qemu-pio, because I have to figure out how to register a PIO region with qemu's device model, and I got lazy. However, for now we can extrapolate based on the data from the NULLIO runs of +2.56us for MMIO, and -350ns for HC, we get:

qemu-pio:      153139 iops, 6.53us rtt
iosignalfd-hc: 412585 iops, 2.37us rtt

these are just for fun, for now, until I can gather more data.

Here is a graph for your convenience:

http://developer.novell.com/wiki/images/7/76/Iofd-chart.png

The conclusion to draw is that we save about 4us by skipping the userspace hop.

--------------------

---

 include/linux/kvm.h      |   15 ++++
 include/linux/kvm_host.h |    2 +
 virt/kvm/eventfd.c       |  154 ++++++++++++++++++++++++++++++++++++++++++++++
 virt/kvm/kvm_main.c      |   13 ++++
 4 files changed, 184 insertions(+), 0 deletions(-)
include/linux/kvm.h +15 -0
index a1ecc6a..9372b12 100644
@@ -292,6 +292,19 @@ struct kvm_guest_debug {
 	struct kvm_guest_debug_arch arch;
 };
 
+#define KVM_IOSIGNALFD_FLAG_DEASSIGN  (1 << 0)
+#define KVM_IOSIGNALFD_FLAG_PIO       (1 << 1)
+#define KVM_IOSIGNALFD_FLAG_COOKIE    (1 << 2)
+
+struct kvm_iosignalfd {
+	__u64 cookie;
+	__u64 addr;
+	__u32 len;
+	__u32 fd;
+	__u32 flags;
+	__u8  pad[12];
+};
+
 #define KVM_TRC_SHIFT           16
 /*
  * kvm trace categories
@@ -416,6 +429,7 @@ struct kvm_trace_rec {
 /* Another bug in KVM_SET_USER_MEMORY_REGION fixed: */
 #define KVM_CAP_JOIN_MEMORY_REGIONS_WORKS 30
 #define KVM_CAP_IRQFD 31
+#define KVM_CAP_IOSIGNALFD 32
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -509,6 +523,7 @@ struct kvm_irqfd {
 			_IOW(KVMIO, 0x74, struct kvm_assigned_msix_entry)
 #define KVM_DEASSIGN_DEV_IRQ       _IOW(KVMIO, 0x75, struct kvm_assigned_irq)
 #define KVM_IRQFD                  _IOW(KVMIO, 0x76, struct kvm_irqfd)
+#define KVM_IOSIGNALFD             _IOW(KVMIO, 0x77, struct kvm_iosignalfd)
 
 /*
  * ioctls for vcpu fds
include/linux/kvm_host.h +2 -0
index 214089f..4e4b174 100644
@@ -137,6 +137,7 @@ struct kvm {
 	struct kvm_io_bus mmio_bus;
 	struct kvm_io_bus pio_bus;
 	struct list_head irqfds;
+	struct list_head iosignalfds;
 	struct kvm_vm_stat stat;
 	struct kvm_arch arch;
 	atomic_t users_count;
@@ -530,5 +531,6 @@ static inline void kvm_free_irq_routing(struct kvm *kvm) {}
 
 int kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags);
 void kvm_irqfd_release(struct kvm *kvm);
+int kvm_iosignalfd(struct kvm *kvm, struct kvm_iosignalfd *args);
 
 #endif
virt/kvm/eventfd.c +154 -0

169 lines not on this page · raw message

virt/kvm/kvm_main.c +13 -0
index 5f5e443..a381dd0 100644
@@ -984,6 +984,7 @@ static struct kvm *kvm_create_vm(void)
 	spin_lock_init(&kvm->mmu_lock);
 	kvm_io_bus_init(&kvm->pio_bus);
 	INIT_LIST_HEAD(&kvm->irqfds);
+	INIT_LIST_HEAD(&kvm->iosignalfds);
 	mutex_init(&kvm->lock);
 	kvm_io_bus_init(&kvm->mmio_bus);
 	init_rwsem(&kvm->slots_lock);
@@ -2219,6 +2220,18 @@ static long kvm_vm_ioctl(struct file *filp,
 		r = kvm_irqfd(kvm, data.fd, data.gsi, data.flags);
 		break;
 	}
+	case KVM_IOSIGNALFD: {
+		struct kvm_iosignalfd entry;
+
+		r = -EFAULT;
+		if (copy_from_user(&entry, argp, sizeof entry))
+			goto out;
+
+		r = kvm_iosignalfd(kvm, &entry);
+		if (r)
+			goto out;
+		break;
+	}
 	default:
 		r = kvm_arch_vm_ioctl(filp, ioctl, arg);
 	}
#7 lore raw

Re: [KVM PATCH v2 4/4] kvm: add iosignalfd support

Review

Quoted 3 lines · Gregory Haskins
(quoted diff) +2 -0
+
+struct _iosignalfd {

Why the underscore? It's unorthodox for a structure.

virt/kvm/eventfd.c kvm_irqfd_release(struct kvm *kvm) · line 220

Quoted 8 lines
virt/kvm/eventfd.c +7 -0
+	u64                  cookie;
+	u64                  addr;
+	size_t               length;
+	struct file         *file;
+	struct list_head     list;
+	struct kvm_io_device dev;
+};

How will that work with multiple identical addresses and different cookies? Will the code iterate over all of them and just fire for one?

I guess that's the best way for now.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#8 lore raw

Re: [KVM PATCH v2 4/4] kvm: add iosignalfd support

Quoted 6 lines · Avi Kivity

Review

Quoted 3 lines · Gregory Haskins
(quoted diff) +2 -0
+
+struct _iosignalfd {

Why the underscore? It's unorthodox for a structure.

I tend to do that to denote "this is a private structure", but it makes more sense if we are building a factory interface for a corresponding public version of the structure of the same name. In this case, I do not think I was using that pattern, but old habits die hard. ;) I will get rid of this extra underscore in the next rev.

Quoted 11 lines

virt/kvm/eventfd.c kvm_irqfd_release(struct kvm *kvm) · line 220

Quoted 10 lines
virt/kvm/eventfd.c +7 -0
+    u64                  cookie;
+    u64                  addr;
+    size_t               length;
+    struct file         *file;
+    struct list_head     list;
+    struct kvm_io_device dev;
+};
Quoted 2 lines

How will that work with multiple identical addresses and different cookies? Will the code iterate over all of them and just fire for one?

Yeah, primarily because I think the io_bus code will stop once it finds the first in_range() match. But I put this logic in anyway in preparation for the future when we may perhaps support such a notion (similar to what you had me do with the irqfd side).

-Greg

#9 lore raw
In reply to Gregory Haskins
Quoted 6 lines · Gregory Haskins

[

Applies to kvm.git:833367b57c plus the irqfd patch, v8, as posted here:

http://lkml.org/lkml/2009/5/14/258

I should also mention: NOT FOR INCLUSION

I am still testing this code, so this is an rfc for now.

Quoted 39 lines

]

This is v2 of the series. For more details, please see the header to patch 4/4.

[
Changelog:

v2:
*) added optional data-matching capability (via cookie field) *) changed name from iofd to iosignalfd
*) added io_bus unregister function
*) implemented deassign feature

v1:
*) original release (integrated into irqfd v7 series as "iofd")
]

---

Gregory Haskins (4):
kvm: add iosignalfd support
kvm: add io_bus unregister function
kvm: add return value to kvm_io_bus_register_dev
eventfd: export eventfd interfaces for module use

 arch/x86/kvm/i8254.c      |    7 +-
 arch/x86/kvm/i8259.c      |    5 +
 fs/eventfd.c              |    3 +
 include/linux/kvm.h       |   15 ++++
 include/linux/kvm_host.h  |   10 ++-
 virt/kvm/coalesced_mmio.c |    4 +
 virt/kvm/eventfd.c        |  154 +++++++++++++++++++++++++++++++++++++++++++++
 virt/kvm/ioapic.c         |    4 +
 virt/kvm/kvm_main.c       |   62 ++++++++++++++++--
 9 files changed, 249 insertions(+), 15 deletions(-)