Skip to contentSkip to conversation

iosignalfd

PATCH Superseded Gregory Haskins 2 participants 10 messages Jun 16, 2009 – Jun 21, 2009

Subsystems (3)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v7 cover iosignalfd

(Applies to kvm.git/master:c27b64a0)

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

This series has been tested against the kvm-eventfd unit test, and appears to be functioning properly. You can download this test here:

ftp://ftp.novell.com/dev/ghaskins/kvm-eventfd.tar.bz2

Please consider for inclusion to kvm.git

[
Changelog:

v7:
*) Implemented a resource limit (CONFIG_KVM_MAX_IOSIGNALFD_ITEMS)
to limit malicious/broken userspace from consuming arbitrary kernel memory.
*) Rebased to kvm.git/master:c27b64a0, which already includes
Marcelo's irq-lock rework.

v6:
*) Removed "FIXME?" comment on choice over RCU vs SRCU after
discussion/numbers from Paul. I think RCU is fine to use for now based on the conversation. We can always convert it later if need be.
*) Fixed the "group" free path to eliminate an RCU related race *) Fixed a memory/eventfd leak on shutdown for any iosignalfd's
which were still active at the time the guest shuts down.
*) Beefed up comments
*) Rebased to kvm.git/master:0281e88f + irq locking rework and
verified that kvm-eventfd unit test still passes.

v5:
*) Removed "cookie" field, which was a misunderstanding on my
part on what Avi wanted for a data-match feature
*) Added a new "trigger" data-match feature which I think is
much closer to what we need.
*) We retain the dev_count field in the io_bus infrastructure
and instead back-fill the array on removal.
*) Various minor cleanups
*) Rebased to kvm.git/master:25deed73

v4:
*) Fixed a bug in the original 2/4 where the PIT failure case
would potentially leave the io_bus components registered.
*) Condensed the v3 2/4 and 3/4 into one patch (2/2) since
the patches became interdependent with the fix described above
*) Rebased to kvm.git/master:74dfca0a

v3:
*) fixed patch 2/4 to handle error cases instead of BUG_ON *) implemented same HAVE_EVENTFD protection mechanism as
irqfd to prevent compilation errors on unsupported arches
*) completed testing
*) rebased to kvm.git/master:7391a6d5

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 (2):
KVM: add iosignalfd support
KVM: make io_bus interface more robust

 arch/x86/kvm/Kconfig      |    8 +
 arch/x86/kvm/i8254.c      |   22 ++
 arch/x86/kvm/i8259.c      |    9 +
 arch/x86/kvm/x86.c        |    3 
 include/linux/kvm.h       |   15 ++
 include/linux/kvm_host.h  |   16 +-
 virt/kvm/Kconfig          |    2 
 virt/kvm/coalesced_mmio.c |    8 +
 virt/kvm/eventfd.c        |  402 +++++++++++++++++++++++++++++++++++++++++++++
 virt/kvm/ioapic.c         |    9 +
 virt/kvm/kvm_main.c       |   41 ++++-
 11 files changed, 520 insertions(+), 15 deletions(-)
Signature · 1 line
Signature
#1 lore raw

PATCH v7 1/2 KVM: make io_bus interface more robust

Today kvm_io_bus_regsiter_dev() 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 we need to enhance the code to be more robust with the following changes:

1) Add a return value to the registration function
2) Fix up all the callsites to check the return code, handle any failures, and percolate the error up to the caller.
3) Add an unregister function that collapses holes in the array

---

 arch/x86/kvm/i8254.c      |   22 ++++++++++++++++++++--
 arch/x86/kvm/i8259.c      |    9 ++++++++-
 include/linux/kvm_host.h  |    6 ++++--
 virt/kvm/coalesced_mmio.c |    8 ++++++--
 virt/kvm/ioapic.c         |    9 +++++++--
 virt/kvm/kvm_main.c       |   30 ++++++++++++++++++++++++++++--
 6 files changed, 73 insertions(+), 11 deletions(-)
arch/x86/kvm/i8254.c +20 -2
index 331705f..1c41715 100644
@@ -586,6 +586,7 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
 {
 	struct kvm_pit *pit;
 	struct kvm_kpit_state *pit_state;
+	int ret;
 
 	pit = kzalloc(sizeof(struct kvm_pit), GFP_KERNEL);
 	if (!pit)
@@ -620,14 +621,31 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
 	kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
 
 	kvm_iodevice_init(&pit->dev, &pit_dev_ops);
-	kvm_io_bus_register_dev(&kvm->pio_bus, &pit->dev);
+	ret = kvm_io_bus_register_dev(&kvm->pio_bus, &pit->dev);
+	if (ret < 0)
+		goto fail;
 
 	if (flags & KVM_PIT_SPEAKER_DUMMY) {
 		kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
-		kvm_io_bus_register_dev(&kvm->pio_bus, &pit->speaker_dev);
+		ret = kvm_io_bus_register_dev(&kvm->pio_bus,
+					      &pit->speaker_dev);
+		if (ret < 0)
+			goto fail;
 	}
 
 	return pit;
+
+fail:
+	if (flags & KVM_PIT_SPEAKER_DUMMY)
+		kvm_io_bus_unregister_dev(&kvm->pio_bus, &pit->speaker_dev);
+
+	kvm_io_bus_unregister_dev(&kvm->pio_bus, &pit->dev);
+
+	if (pit->irq_source_id >= 0)
+		kvm_free_irq_source_id(kvm, pit->irq_source_id);
+
+	kfree(pit);
+	return NULL;
 }
 
 void kvm_free_pit(struct kvm *kvm)
arch/x86/kvm/i8259.c +8 -1
index 148c52a..66e37c2 100644
@@ -532,6 +532,8 @@ static const struct kvm_io_device_ops picdev_ops = {
 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;
@@ -548,6 +550,11 @@ struct kvm_pic *kvm_create_pic(struct kvm *kvm)
 	 * Initialize PIO device
 	 */
 	kvm_iodevice_init(&s->dev, &picdev_ops);
-	kvm_io_bus_register_dev(&kvm->pio_bus, &s->dev);
+	ret = kvm_io_bus_register_dev(&kvm->pio_bus, &s->dev);
+	if (ret < 0) {
+		kfree(s);
+		return NULL;
+	}
+
 	return s;
 }
include/linux/kvm_host.h +4 -2
index e4e78db..eafa2b3 100644
@@ -61,8 +61,10 @@ 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);
+void kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
+			    struct kvm_io_device *dev);
 
 struct kvm_vcpu {
 	struct kvm *kvm;
virt/kvm/coalesced_mmio.c +6 -2
index 397f419..3e89db8 100644
@@ -94,6 +94,7 @@ static const struct kvm_io_device_ops coalesced_mmio_ops = {
 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)
@@ -102,9 +103,12 @@ int kvm_coalesced_mmio_init(struct kvm *kvm)
 	kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
 	dev->kvm = kvm;
 	kvm->coalesced_mmio_dev = dev;
-	kvm_io_bus_register_dev(&kvm->mmio_bus, &dev->dev);
 
-	return 0;
+	ret = kvm_io_bus_register_dev(&kvm->mmio_bus, &dev->dev);
+	if (ret < 0)
+		kfree(dev);
+
+	return ret;
 }
 
 int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
virt/kvm/ioapic.c +7 -2
index d8b2eca..28adb05 100644
@@ -335,6 +335,7 @@ static const struct kvm_io_device_ops ioapic_mmio_ops = {
 int kvm_ioapic_init(struct kvm *kvm)
 {
 	struct kvm_ioapic *ioapic;
+	int ret;
 
 	ioapic = kzalloc(sizeof(struct kvm_ioapic), GFP_KERNEL);
 	if (!ioapic)
@@ -343,7 +344,11 @@ int kvm_ioapic_init(struct kvm *kvm)
 	kvm_ioapic_reset(ioapic);
 	kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
 	ioapic->kvm = kvm;
-	kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);
-	return 0;
+
+	ret = kvm_io_bus_register_dev(&kvm->mmio_bus, &ioapic->dev);
+	if (ret < 0)
+		kfree(ioapic);
+
+	return ret;
 }
 
virt/kvm/kvm_main.c +28 -2
index 9fab08e..b2cd59c 100644
@@ -2481,11 +2481,37 @@ 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)
+/* assumes kvm->lock held */
+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;
+}
+
+/* assumes kvm->lock held */
+void kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
+			       struct kvm_io_device *dev)
+{
+	int i;
+
+	for (i = 0; i < bus->dev_count; i++) {
+
+		if (bus->devs[i] == dev) {
+			int j;
+
+			/* backfill the hole */
+			for (j = i; j < bus->dev_count-1; j++)
+				bus->devs[j] = bus->devs[j+1];
+
+			bus->dev_count--;
+
+			break;
+		}
+	}
 }
 
 static struct notifier_block kvm_cpu_notifier = {
#2 lore raw

Re: [KVM PATCH v7 1/2] KVM: make io_bus interface more robust

Quoted 10 lines · Gregory Haskins
virt/kvm/kvm_main.c +9 -0
+
+/* assumes kvm->lock held */
+void kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
+			       struct kvm_io_device *dev)
+{
+	int i;
+
+	for (i = 0; i<  bus->dev_count; i++) {
+

virt/kvm/kvm_main.c struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, · line 2512

Quoted 10 lines
virt/kvm/kvm_main.c +10 -0
+		if (bus->devs[i] == dev) {
+			int j;
+
+			/* backfill the hole */
+			for (j = i; j<  bus->dev_count-1; j++)
+				bus->devs[j] = bus->devs[j+1];
+
+			bus->dev_count--;
+
+			break;

Could be simplified to

    if (bus->devs[i] == dev) {
        bus->devs[i] = bus->devs[--bus->dev_count];
        return;
    }
Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw

Re: [KVM PATCH v7 1/2] KVM: make io_bus interface more robust

Quoted 24 lines · Avi Kivity
Quoted 10 lines · Gregory Haskins
virt/kvm/kvm_main.c +9 -0
+
+/* assumes kvm->lock held */
+void kvm_io_bus_unregister_dev(struct kvm_io_bus *bus,
+                   struct kvm_io_device *dev)
+{
+    int i;
+
+    for (i = 0; i<  bus->dev_count; i++) {
+

virt/kvm/kvm_main.c struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, · line 2512

Quoted 10 lines
virt/kvm/kvm_main.c +10 -0
+        if (bus->devs[i] == dev) {
+            int j;
+
+            /* backfill the hole */
+            for (j = i; j<  bus->dev_count-1; j++)
+                bus->devs[j] = bus->devs[j+1];
+
+            bus->dev_count--;
+
+            break;

Could be simplified to

Quoted 5 lines
    if (bus->devs[i] == dev) {
        bus->devs[i] = bus->devs[--bus->dev_count];
        return;
    }

Clever!

Will do.

-Greg

#4 lore raw

PATCH v7 2/2 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.

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

---

 arch/x86/kvm/Kconfig     |    8 +
 arch/x86/kvm/x86.c       |    3 
 include/linux/kvm.h      |   15 ++
 include/linux/kvm_host.h |   10 +
 virt/kvm/Kconfig         |    2 
 virt/kvm/eventfd.c       |  402 ++++++++++++++++++++++++++++++++++++++++++++++
 virt/kvm/kvm_main.c      |   11 +
 7 files changed, 447 insertions(+), 4 deletions(-)
arch/x86/kvm/Kconfig +8 -0
index 7fbedfd..61b3b24 100644
@@ -74,6 +74,14 @@ config KVM_TRACE
 	  relayfs.  Note the ABI is not considered stable and will be
 	  modified in future updates.
 
+config KVM_MAX_IOSIGNALFD_ITEMS
+	int "Maximum IOSIGNALFD items per address"
+	depends on KVM
+	default "32"
+	---help---
+	  This option influences the maximum number of fd's per PIO/MMIO
+	  address that are allowed to register
+
 # OK, it's a little counter-intuitive to do this, but it puts it neatly under
 # the virtualization menu.
 source drivers/lguest/Kconfig
arch/x86/kvm/x86.c +3 -0
index 1b91ea7..c5fd38b 100644
@@ -1121,6 +1121,9 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_MCE:
 		r = KVM_MAX_MCE_BANKS;
 		break;
+	case KVM_CAP_IOSIGNALFD:
+		r = CONFIG_KVM_MAX_IOSIGNALFD_ITEMS;
+		break;
 	default:
 		r = 0;
 		break;
include/linux/kvm.h +15 -0
index 38ff31e..9de6486 100644
@@ -307,6 +307,19 @@ struct kvm_guest_debug {
 	struct kvm_guest_debug_arch arch;
 };
 
+#define KVM_IOSIGNALFD_FLAG_TRIGGER   (1 << 0)
+#define KVM_IOSIGNALFD_FLAG_PIO       (1 << 1)
+#define KVM_IOSIGNALFD_FLAG_DEASSIGN  (1 << 2)
+
+struct kvm_iosignalfd {
+	__u64 trigger;
+	__u64 addr;
+	__u32 len;
+	__u32 fd;
+	__u32 flags;
+	__u8  pad[36];
+};
+
 #define KVM_TRC_SHIFT           16
 /*
  * kvm trace categories
@@ -438,6 +451,7 @@ struct kvm_trace_rec {
 #define KVM_CAP_PIT2 33
 #endif
 #define KVM_CAP_SET_BOOT_CPU_ID 34
+#define KVM_CAP_IOSIGNALFD 35
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -544,6 +558,7 @@ struct kvm_irqfd {
 #define KVM_IRQFD                  _IOW(KVMIO, 0x76, struct kvm_irqfd)
 #define KVM_CREATE_PIT2		   _IOW(KVMIO, 0x77, struct kvm_pit_config)
 #define KVM_SET_BOOT_CPU_ID        _IO(KVMIO, 0x78)
+#define KVM_IOSIGNALFD             _IOW(KVMIO, 0x79, struct kvm_iosignalfd)
 
 /*
  * ioctls for vcpu fds
include/linux/kvm_host.h +8 -2
index eafa2b3..2e01409 100644
@@ -143,6 +143,7 @@ struct kvm {
 	struct kvm_io_bus pio_bus;
 #ifdef CONFIG_HAVE_KVM_EVENTFD
 	struct list_head irqfds;
+	struct list_head iosignalfds;
 #endif
 	struct kvm_vm_stat stat;
 	struct kvm_arch arch;
@@ -551,19 +552,24 @@ static inline void kvm_free_irq_routing(struct kvm *kvm) {}
 
 #ifdef CONFIG_HAVE_KVM_EVENTFD
 
-void kvm_irqfd_init(struct kvm *kvm);
+void kvm_eventfd_init(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);
 
 #else
 
-static inline void kvm_irqfd_init(struct kvm *kvm) {}
+static inline void kvm_eventfd_init(struct kvm *kvm) {}
 static inline int kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags)
 {
 	return -EINVAL;
 }
 
 static inline void kvm_irqfd_release(struct kvm *kvm) {}
+static inline int kvm_iosignalfd(struct kvm *kvm, struct kvm_iosignalfd *args)
+{
+	return -EINVAL;
+}
 
 #endif /* CONFIG_HAVE_KVM_EVENTFD */
 
virt/kvm/Kconfig +2 -0
index daece36..a4b427f 100644
@@ -12,3 +12,5 @@ config HAVE_KVM_EVENTFD
 
 config KVM_APIC_ARCHITECTURE
        bool
+
+
virt/kvm/eventfd.c +401 -1

425 lines not on this page · raw message

virt/kvm/kvm_main.c +10 -1
index b2cd59c..7fee49e 100644
@@ -971,7 +971,7 @@ static struct kvm *kvm_create_vm(void)
 	atomic_inc(&kvm->mm->mm_count);
 	spin_lock_init(&kvm->mmu_lock);
 	kvm_io_bus_init(&kvm->pio_bus);
-	kvm_irqfd_init(kvm);
+	kvm_eventfd_init(kvm);
 	mutex_init(&kvm->lock);
 	mutex_init(&kvm->irq_lock);
 	kvm_io_bus_init(&kvm->mmio_bus);
@@ -2227,6 +2227,15 @@ 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 data;
+
+		r = -EFAULT;
+		if (copy_from_user(&data, argp, sizeof data))
+			goto out;
+		r = kvm_iosignalfd(kvm, &data);
+		break;
+	}
 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
 	case KVM_SET_BOOT_CPU_ID:
 		r = 0;
#5 lore raw

Re: [KVM PATCH v7 2/2] KVM: add iosignalfd support

Quoted 66 lines · 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.

arch/x86/kvm/Kconfig config KVM_TRACE · line 84

Quoted 8 lines
arch/x86/kvm/Kconfig +8 -0
+config KVM_MAX_IOSIGNALFD_ITEMS
+	int "Maximum IOSIGNALFD items per address"
+	depends on KVM
+	default "32"
+	---help---
+	  This option influences the maximum number of fd's per PIO/MMIO
+	  address that are allowed to register
+

Is there a per-vm limit on iosignalfds? if not, userspace can exhaust kernel memory in that way.

We could limit the just total number of iosignafds, it's somewhat more natural.

virt/kvm/Kconfig config HAVE_KVM_EVENTFD · line 16

Quoted 11 lines
virt/kvm/Kconfig +2 -0
index daece36..a4b427f 100644
@@ -12,3 +12,5 @@ config HAVE_KVM_EVENTFD
 
  config KVM_APIC_ARCHITECTURE
         bool
+
+

Spurious, please drop.

Quoted 7 lines
virt/kvm/Kconfig +6 -0
+/*
+ * Design note: We create one PIO/MMIO device (iosignalfd_group) which
+ * aggregates  one or more iosignalfd_items.  Each item points to exactly one
+ * eventfd, and can be registered to trigger on any write to the group
+ * (wildcard), or to a write of a specific value.  If more than one item is to
+ * be supported, the addr/len ranges must all be identical in the group.  If a

virt/kvm/Kconfig

Quoted 10 lines
virt/kvm/Kconfig +10 -0
+ * trigger value is to be supported on a particular item, the group range must
+ * be exactly the width of the trigger.
+ */
+
+struct _iosignalfd_item {
+	struct list_head     list;
+	struct file         *file;
+	unsigned char       *match;
+	struct rcu_head      rcu;
+};

Why not u64 match?

Quoted 4 lines
virt/kvm/Kconfig +3 -0
+static int
+iosignalfd_is_match(struct _iosignalfd_group *group,
+		    struct _iosignalfd_item *item,

virt/kvm/Kconfig

Quoted 10 lines
virt/kvm/Kconfig +10 -0
+		    const void *val,
+		    int len)
+{
+	if (!item->match)
+		/* wildcard is a hit */
+		return true;
+
+	if (len != group->length)
+		/* mis-matched length is a miss */
+		return false;

Should check length before match (i.e. require correctly sized access).

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw

Re: [KVM PATCH v7 2/2] KVM: add iosignalfd support

Quoted 79 lines · Avi Kivity
Quoted 78 lines · 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.

Quoted 12 lines

arch/x86/kvm/Kconfig config KVM_TRACE · line 84

Quoted 9 lines
arch/x86/kvm/Kconfig +8 -0
+config KVM_MAX_IOSIGNALFD_ITEMS
+    int "Maximum IOSIGNALFD items per address"
+    depends on KVM
+    default "32"
+    ---help---
+      This option influences the maximum number of fd's per PIO/MMIO
+      address that are allowed to register
+

Is there a per-vm limit on iosignalfds? if not, userspace can exhaust kernel memory in that way.

Yeah, its already naturally limited by the maximum number of MMIO/PIO devices we can register (today this is 6 per VM). I should have documented that fact somewhere, tho.

Quoted 9 lines

We could limit the just total number of iosignafds, it's somewhat more natural.

Quoted 6 lines
virt/kvm/Kconfig +0 -0
index daece36..a4b427f 100644
@@ -12,3 +12,5 @@ config HAVE_KVM_EVENTFD
 
Quoted 7 lines

virt/kvm/Kconfig config HAVE_KVM_EVENTFD · line 16

Quoted 5 lines
virt/kvm/Kconfig +2 -0
  config KVM_APIC_ARCHITECTURE
         bool
+
+

Spurious, please drop.

Ack

Quoted 15 lines
Quoted 15 lines

+/*
+ * Design note: We create one PIO/MMIO device (iosignalfd_group) which
+ * aggregates one or more iosignalfd_items. Each item points to exactly one
+ * eventfd, and can be registered to trigger on any write to the group
+ * (wildcard), or to a write of a specific value. If more than one item is to
+ * be supported, the addr/len ranges must all be identical in the group. If a
+ * trigger value is to be supported on a particular item, the group range must
+ * be exactly the width of the trigger.
+ */

virt/kvm/Kconfig +2 -0
+
+struct _iosignalfd_item {
Quoted 8 lines

virt/kvm/Kconfig

Quoted 6 lines
virt/kvm/Kconfig +5 -0
+    struct list_head     list;
+    struct file         *file;
+    unsigned char       *match;
+    struct rcu_head      rcu;
+};

Why not u64 match?

Well, tbh it was primarily because it was starting to make my head hurt w.r.t. endianness ;). For instance, if someone wanted a u16 match, I would presumably have to understand the relevant endianess of the u64 so I compare the appropriate bytes against the data-register coming in from the [MM|P]IO. Using a pointer, I simply copy/memcmp the specified number of bytes and never have to worry about endianness.

As a minor bonus, item->match == NULL tells me its a wildcard. If I had item->match as a u64, I'd need a different state flag for "wildcard". NBD, but thought I would point it out.

Quoted 17 lines
Quoted 5 lines
virt/kvm/Kconfig +3 -0
+static int
+iosignalfd_is_match(struct _iosignalfd_group *group,
+            struct _iosignalfd_item *item,

virt/kvm/Kconfig

Quoted 10 lines
virt/kvm/Kconfig +10 -0
+            const void *val,
+            int len)
+{
+    if (!item->match)
+        /* wildcard is a hit */
+        return true;
+
+    if (len != group->length)
+        /* mis-matched length is a miss */
+        return false;

Should check length before match (i.e. require correctly sized access).

Perhaps, but my thinking is that group->length only matters for data-matching. You could conceivably have a larger window registered if you are using all wildcards. Not sure if this is really useful, but its the reason the code is that way today.

Thanks Avi,
-Greg

#7 lore raw

Re: [KVM PATCH v7 2/2] KVM: add iosignalfd support

Quoted 14 lines · Gregory Haskins
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/Kconfig +4 -0
+config KVM_MAX_IOSIGNALFD_ITEMS
+    int "Maximum IOSIGNALFD items per address"
+    depends on KVM
+    default "32"
Quoted 9 lines

arch/x86/kvm/Kconfig config KVM_TRACE · line 84

Quoted 6 lines
arch/x86/kvm/Kconfig +4 -0
+    ---help---
+      This option influences the maximum number of fd's per PIO/MMIO
+      address that are allowed to register
+

Is there a per-vm limit on iosignalfds? if not, userspace can exhaust kernel memory in that way.

Quoted 4 lines

Yeah, its already naturally limited by the maximum number of MMIO/PIO devices we can register (today this is 6 per VM). I should have documented that fact somewhere, tho.

We need to raise this limit drastically and to expose it. I suggest counting an all iosignalfd_items as part of the iodevice limit, so we don't have a bunch of little limits which no one understands.

Quoted 11 lines
Quoted 10 lines

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

Quoted 8 lines
virt/kvm/eventfd.c +6 -0
+struct _iosignalfd_item {
+    struct list_head     list;
+    struct file         *file;
+    unsigned char       *match;
+    struct rcu_head      rcu;
+};

Why not u64 match?

Quoted 7 lines

Well, tbh it was primarily because it was starting to make my head hurt w.r.t. endianness ;). For instance, if someone wanted a u16 match, I would presumably have to understand the relevant endianess of the u64 so I compare the appropriate bytes against the data-register coming in from the [MM|P]IO. Using a pointer, I simply copy/memcmp the specified number of bytes and never have to worry about endianness.

No, a u16 will naturally expand to a u64, and the emulator will generate the correct value. As long as we don't allow mismatched access sizes, we should be fine.

Quoted 4 lines

As a minor bonus, item->match == NULL tells me its a wildcard. If I had item->match as a u64, I'd need a different state flag for "wildcard". NBD, but thought I would point it out.

True, a pointer also supplies extra information. But until we get garbage collection as part of the Java rewrite, resource management is a pain and I prefer as few pointers as possible.

Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
virt/kvm/eventfd.c +8 -0
+static int
+iosignalfd_is_match(struct _iosignalfd_group *group,
+            struct _iosignalfd_item *item,
+            const void *val,
+            int len)
+{
+    if (!item->match)
+        /* wildcard is a hit */
Quoted 9 lines

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

Quoted 7 lines
virt/kvm/eventfd.c +5 -0
+        return true;
+
+    if (len != group->length)
+        /* mis-matched length is a miss */
+        return false;

Should check length before match (i.e. require correctly sized access).

Quoted 5 lines

Perhaps, but my thinking is that group->length only matters for data-matching. You could conceivably have a larger window registered if you are using all wildcards. Not sure if this is really useful, but its the reason the code is that way today.

My thinking is to have the code behave the same way. If you require matching lengths on data match, require it on wildcard as well.

Signature · 1 line
error compiling committee.c: too many arguments to function
#8 lore raw

Re: [KVM PATCH v7 2/2] KVM: add iosignalfd support

Quoted 15 lines · Avi Kivity
Quoted 14 lines · Gregory Haskins
Quoted 13 lines

arch/x86/kvm/Kconfig config KVM_TRACE · line 84

Quoted 10 lines
arch/x86/kvm/Kconfig +8 -0
+config KVM_MAX_IOSIGNALFD_ITEMS
+    int "Maximum IOSIGNALFD items per address"
+    depends on KVM
+    default "32"
+    ---help---
+      This option influences the maximum number of fd's per PIO/MMIO
+      address that are allowed to register
+

Is there a per-vm limit on iosignalfds? if not, userspace can exhaust kernel memory in that way.

Quoted 6 lines
Quoted 4 lines

Yeah, its already naturally limited by the maximum number of MMIO/PIO devices we can register (today this is 6 per VM). I should have documented that fact somewhere, tho.

We need to raise this limit drastically and to expose it.

Any suggestions on a target #? 512?

Quoted 3 lines

I suggest counting an all iosignalfd_items as part of the iodevice
limit, so we don't have a bunch of little limits which no one understands.

Yeah, I like this idea.

Quoted 20 lines
Quoted 12 lines
Quoted 11 lines

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

Quoted 9 lines
virt/kvm/eventfd.c +6 -0
+struct _iosignalfd_item {
+    struct list_head     list;
+    struct file         *file;
+    unsigned char       *match;
+    struct rcu_head      rcu;
+};

Why not u64 match?

Quoted 7 lines

Well, tbh it was primarily because it was starting to make my head hurt w.r.t. endianness ;). For instance, if someone wanted a u16 match, I would presumably have to understand the relevant endianess of the u64 so I compare the appropriate bytes against the data-register coming in from the [MM|P]IO. Using a pointer, I simply copy/memcmp the specified number of bytes and never have to worry about endianness.

Quoted 2 lines

No, a u16 will naturally expand to a u64, and the emulator will generate the correct value.

Right, I understand that part. What I mean specifically is at run-time when the IO comes in. I was thinking I would need to do a memcmp against the u64 and the data-register and it was hurting my head trying to figure out what pointer to pass to memcmp.

<lightbulb turns on>

Duh, I can just load the data-register into a u64 and check equality. Nevermind, I am a dumbass ;)

Quoted 10 lines

As long as we don't allow mismatched access sizes, we should be fine.

Quoted 4 lines

As a minor bonus, item->match == NULL tells me its a wildcard. If I had item->match as a u64, I'd need a different state flag for "wildcard". NBD, but thought I would point it out.

True, a pointer also supplies extra information. But until we get garbage collection as part of the Java rewrite, resource management is a pain and I prefer as few pointers as possible.

Oh man! And I was so looking forward to that rewrite.....

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 6 lines
virt/kvm/eventfd.c +3 -0
+static int
+iosignalfd_is_match(struct _iosignalfd_group *group,
+            struct _iosignalfd_item *item,

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

Quoted 10 lines
virt/kvm/eventfd.c +10 -0
+            const void *val,
+            int len)
+{
+    if (!item->match)
+        /* wildcard is a hit */
+        return true;
+
+    if (len != group->length)
+        /* mis-matched length is a miss */
+        return false;

Should check length before match (i.e. require correctly sized access).

Quoted 8 lines
Quoted 5 lines

Perhaps, but my thinking is that group->length only matters for data-matching. You could conceivably have a larger window registered if you are using all wildcards. Not sure if this is really useful, but its the reason the code is that way today.

My thinking is to have the code behave the same way. If you require matching lengths on data match, require it on wildcard as well.

Ack

Thanks Avi,
-Greg

#9 lore raw

Re: [KVM PATCH v7 2/2] KVM: add iosignalfd support

Quoted 24 lines · Gregory Haskins
Quoted 22 lines · Avi Kivity
Quoted 20 lines · Gregory Haskins
Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/Kconfig +5 -0
+config KVM_MAX_IOSIGNALFD_ITEMS
+    int "Maximum IOSIGNALFD items per address"
+    depends on KVM
+    default "32"
+    ---help---
Quoted 10 lines

arch/x86/kvm/Kconfig config KVM_TRACE · line 84

Quoted 6 lines
arch/x86/kvm/Kconfig +3 -0
+      This option influences the maximum number of fd's per PIO/MMIO
+      address that are allowed to register
+

Is there a per-vm limit on iosignalfds? if not, userspace can exhaust kernel memory in that way.

Yeah, its already naturally limited by the maximum number of MMIO/PIO devices we can register (today this is 6 per VM). I should have documented that fact somewhere, tho.

Quoted 5 lines
Quoted 2 lines

We need to raise this limit drastically and to expose it.

Any suggestions on a target #? 512?

Let's say 20 devices with 16 queues each. That gives 320 fds. So 512 seems like a good choice for now.

But don't make it Kconfigurable, there's no way the user will know what to put there.

Quoted 9 lines
Quoted 3 lines

No, a u16 will naturally expand to a u64, and the emulator will generate the correct value.

Right, I understand that part. What I mean specifically is at run-time when the IO comes in. I was thinking I would need to do a memcmp against the u64 and the data-register and it was hurting my head trying to figure out what pointer to pass to memcmp.

Quoted 5 lines

<lightbulb turns on>

Duh, I can just load the data-register into a u64 and check equality. Nevermind, I am a dumbass ;)

I see on your v8 what the load means. It's not so pretty. But we can have generic code do the load and pass a u64 instead of a pointer.

But please, only after this goes in.

Signature · 1 line
error compiling committee.c: too many arguments to function