Skip to contentSkip to conversation

IO: Intelligent device lookup on bus

PATCH Superseded Sasha Levin 2 participants 4 messages Jul 24, 2011 – Jul 27, 2011

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 IO: Intelligent device lookup on bus

Currently the method of dealing with an IO operation on a bus (PIO/MMIO) is to call the read or write callback for each device registered on the bus until we find a device which handles it.

Since the number of devices on a bus can be significant due to ioeventfds and coalesced MMIO zones, this leads to a lot of overhead on each IO operation.

Instead of registering devices, we now register ranges which points to a device. Lookup is done using an efficient bsearch instead of a linear search.

Performance test was conducted by comparing exit count per second with 200 ioeventfds created on one byte and the guest is trying to access a different byte continuously (triggering usermode exits). Before the patch the guest has achieved 259k exits per second, after the patch the guest does 274k exits per second.

Changes in V3:
- Small fix in error path of i8259.

Changes in V2:
- Only register a device once.
- Support for multiple devices using same range.
- Added performance benchmark.
- Using bsearch() and sort() found in /lib/.

Cc Avi Kivity <[email protected]>
Cc Marcelo Tosatti <[email protected]>
---
 arch/x86/kvm/i8254.c      |    5 +-
 arch/x86/kvm/i8259.c      |   56 ++++++++++++++++++----
 arch/x86/kvm/irq.h        |    4 +-
 arch/x86/kvm/x86.c        |    6 ++-
 include/linux/kvm_host.h  |   18 ++++----
 virt/kvm/coalesced_mmio.c |    3 +-
 virt/kvm/eventfd.c        |    3 +-
 virt/kvm/ioapic.c         |    3 +-
 virt/kvm/kvm_main.c       |  112 ++++++++++++++++++++++++++++++++++++++++-----
 9 files changed, 171 insertions(+), 39 deletions(-)
arch/x86/kvm/i8254.c +3 -2
index efad723..094e057 100644
@@ -713,14 +713,15 @@ 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);
-	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, &pit->dev);
+	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, KVM_PIT_BASE_ADDRESS, KVM_PIT_MEM_LENGTH, &pit->dev);
 	if (ret < 0)
 		goto fail;
 
 	if (flags & KVM_PIT_SPEAKER_DUMMY) {
 		kvm_iodevice_init(&pit->speaker_dev, &speaker_dev_ops);
 		ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,
-						&pit->speaker_dev);
+                                              KVM_SPEAKER_BASE_ADDRESS, 4,
+					      &pit->speaker_dev);
 		if (ret < 0)
 			goto fail_unregister;
 	}
arch/x86/kvm/i8259.c +45 -11

90 lines not on this page · raw message

arch/x86/kvm/irq.h +3 -1
index 53e2d08..2086f2b 100644
@@ -66,7 +66,9 @@ struct kvm_pic {
 	struct kvm *kvm;
 	struct kvm_kpic_state pics[2]; /* 0 is master pic, 1 is slave pic */
 	int output;		/* intr from master PIC */
-	struct kvm_io_device dev;
+	struct kvm_io_device dev_master;
+	struct kvm_io_device dev_slave;
+	struct kvm_io_device dev_eclr;
 	void (*ack_notifier)(void *opaque, int irq);
 	unsigned long irq_states[16];
 };
arch/x86/kvm/x86.c +5 -1
index e80f0d7..4cea9cf 100644
@@ -3561,7 +3561,11 @@ long kvm_arch_vm_ioctl(struct file *filp,
 			if (r) {
 				mutex_lock(&kvm->slots_lock);
 				kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
-							  &vpic->dev);
+							  &vpic->dev_master);
+				kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
+							  &vpic->dev_slave);
+				kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
+							  &vpic->dev_eclr);
 				mutex_unlock(&kvm->slots_lock);
 				kfree(vpic);
 				goto create_irqchip_unlock;
include/linux/kvm_host.h +9 -9
index ff4d406..d0e42f3 100644
@@ -55,16 +55,16 @@ struct kvm;
 struct kvm_vcpu;
 extern struct kmem_cache *kvm_vcpu_cache;
 
-/*
- * It would be nice to use something smarter than a linear search, TBD...
- * Thankfully we dont expect many devices to register (famous last words :),
- * so until then it will suffice.  At least its abstracted so we can change
- * in one place.
- */
+struct kvm_io_range {
+	gpa_t addr;
+	int len;
+	struct kvm_io_device *dev;
+};
+
 struct kvm_io_bus {
 	int                   dev_count;
 #define NR_IOBUS_DEVS 300
-	struct kvm_io_device *devs[NR_IOBUS_DEVS];
+	struct kvm_io_range range[NR_IOBUS_DEVS];
 };
 
 enum kvm_bus {
@@ -77,8 +77,8 @@ int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
 		     int len, const void *val);
 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len,
 		    void *val);
-int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
-			    struct kvm_io_device *dev);
+int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
+			    int len, struct kvm_io_device *dev);
 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
 			      struct kvm_io_device *dev);
 
virt/kvm/coalesced_mmio.c +2 -1
index 2316ec1..a6ec206 100644
@@ -141,7 +141,8 @@ int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
 	dev->zone = *zone;
 
 	mutex_lock(&kvm->slots_lock);
-	ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev);
+	ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, zone->addr,
+				      zone->size, &dev->dev);
 	if (ret < 0)
 		goto out_free_dev;
 	list_add_tail(&dev->list, &kvm->coalesced_zones);
virt/kvm/eventfd.c +2 -1
index 73358d2..f59c1e8 100644
@@ -586,7 +586,8 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 
 	kvm_iodevice_init(&p->dev, &ioeventfd_ops);
 
-	ret = kvm_io_bus_register_dev(kvm, bus_idx, &p->dev);
+	ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length,
+				      &p->dev);
 	if (ret < 0)
 		goto unlock_fail;
 
virt/kvm/ioapic.c +2 -1
index 8df1ca1..86aeea1 100644
@@ -394,7 +394,8 @@ int kvm_ioapic_init(struct kvm *kvm)
 	kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
 	ioapic->kvm = kvm;
 	mutex_lock(&kvm->slots_lock);
-	ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &ioapic->dev);
+	ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, ioapic->base_address,
+                                      IOAPIC_MEM_LENGTH, &ioapic->dev);
 	mutex_unlock(&kvm->slots_lock);
 	if (ret < 0) {
 		kvm->arch.vioapic = NULL;
virt/kvm/kvm_main.c +100 -12

165 lines not on this page · raw message

Signature · 1 line
1.7.6
#1 lore raw
Quoted 23 lines · Sasha Levin

Currently the method of dealing with an IO operation on a bus (PIO/MMIO) is to call the read or write callback for each device registered on the bus until we find a device which handles it.

Since the number of devices on a bus can be significant due to ioeventfds and coalesced MMIO zones, this leads to a lot of overhead on each IO operation.

Instead of registering devices, we now register ranges which points to a device. Lookup is done using an efficient bsearch instead of a linear search.

Performance test was conducted by comparing exit count per second with 200 ioeventfds created on one byte and the guest is trying to access a different byte continuously (triggering usermode exits). Before the patch the guest has achieved 259k exits per second, after the patch the guest does 274k exits per second.

arch/x86/kvm/i8254.c +0 -0
index efad723..094e057 100644

arch/x86/kvm/i8254.c struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags) · line 716

Quoted 5 lines
arch/x86/kvm/i8254.c +1 -1
@@ -713,14 +713,15 @@ 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);
-	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,&pit->dev);
+	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, KVM_PIT_BASE_ADDRESS, KVM_PIT_MEM_LENGTH,&pit->dev);

Long line.

Quoted 9 lines
arch/x86/kvm/i8254.c +5 -2
-static inline struct kvm_pic *to_pic(struct kvm_io_device *dev)
+static inline struct kvm_pic *to_pic(struct kvm_io_device *dev, gpa_t addr)
  {
-	return container_of(dev, struct kvm_pic, dev);
+	switch (addr) {
+	case 0x20:
+	case 0x21:
+		return container_of(dev, struct kvm_pic, dev_master);

arch/x86/kvm/i8254.c struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)

Quoted 10 lines
arch/x86/kvm/i8254.c +9 -0
+	case 0xa0:
+	case 0xa1:
+		return container_of(dev, struct kvm_pic, dev_slave);
+	case 0x4d0:
+	case 0x4d1:
+		return container_of(dev, struct kvm_pic, dev_eclr);
+	}
+
+	return NULL;
  }

Somewhat ugly. I think

int picdev_write_master(...)

   {
        return pcidev_write(container_of(...), ...);
    }

is nicer, no?

Quoted 31 lines
arch/x86/kvm/i8254.c +17 -6
@@ -560,16 +572,36 @@ struct kvm_pic *kvm_create_pic(struct kvm *kvm)
  	/*
  	 * Initialize PIO device
  	 */
-	kvm_iodevice_init(&s->dev,&picdev_ops);
+	kvm_iodevice_init(&s->dev_master,&picdev_ops);
+	kvm_iodevice_init(&s->dev_slave,&picdev_ops);
+	kvm_iodevice_init(&s->dev_eclr,&picdev_ops);
  	mutex_lock(&kvm->slots_lock);
-	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,&s->dev);
+	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x20, 2,&s->dev_master);
+	if (ret<  0)
+		goto fail_unlock;
+
+	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0xa0, 2,&s->dev_slave);
+	if (ret<  0)
+		goto fail_unlock;
+
+	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x4d0, 2,&s->dev_eclr);
+	if (ret<  0)
+		goto fail_unlock;
+
  	mutex_unlock(&kvm->slots_lock);
-	if (ret<  0) {
-		kfree(s);
-		return NULL;
-	}
 
  	return s;
+
+fail_unlock:

arch/x86/kvm/i8254.c struct kvm_pic *kvm_create_pic(struct kvm *kvm) · line 605

Quoted 10 lines
arch/x86/kvm/i8254.c +9 -0
+
+	mutex_unlock(&kvm->slots_lock);
+	kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,&s->dev_master);
+	kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,&s->dev_slave);
+	kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,&s->dev_eclr);
+
+	kfree(s);
+
+	return NULL;
  }

You're unregistering devices that were never registered. It may work now, but it's fragile.

Quoted 8 lines
arch/x86/kvm/i8254.c +0 -0
  	if (ret<  0)
  		goto out_free_dev;
  	list_add_tail(&dev->list,&kvm->coalesced_zones);
virt/kvm/eventfd.c +0 -0
index 73358d2..f59c1e8 100644

virt/kvm/eventfd.c kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 590

Quoted 6 lines
virt/kvm/eventfd.c +2 -1
@@ -586,7 +586,8 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 
  	kvm_iodevice_init(&p->dev,&ioeventfd_ops);
 
-	ret = kvm_io_bus_register_dev(kvm, bus_idx,&p->dev);
+	ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length,
+				&p->dev);

Should this be p->length or 1?

virt/kvm/eventfd.c static void kvm_io_bus_destroy(struct kvm_io_bus *bus) · line 2397

Quoted 9 lines
virt/kvm/eventfd.c +1 -1
  #include<asm/processor.h>
  #include<asm/io.h>
@@ -2391,24 +2393,94 @@ static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  	int i;
 
  	for (i = 0; i<  bus->dev_count; i++) {
-		struct kvm_io_device *pos = bus->devs[i];
+		struct kvm_io_device *pos = bus->range[i].dev;
 

This will call the destructor three times for the PIC. Is this safe?

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 26 lines · Avi Kivity
Quoted 25 lines · Sasha Levin

Currently the method of dealing with an IO operation on a bus (PIO/MMIO) is to call the read or write callback for each device registered on the bus until we find a device which handles it.

Since the number of devices on a bus can be significant due to ioeventfds and coalesced MMIO zones, this leads to a lot of overhead on each IO operation.

Instead of registering devices, we now register ranges which points to a device. Lookup is done using an efficient bsearch instead of a linear search.

Performance test was conducted by comparing exit count per second with 200 ioeventfds created on one byte and the guest is trying to access a different byte continuously (triggering usermode exits). Before the patch the guest has achieved 259k exits per second, after the patch the guest does 274k exits per second.

arch/x86/kvm/i8254.c +0 -0
index efad723..094e057 100644
@@ -713,14 +713,15 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
  	kvm_register_irq_mask_notifier(kvm, 0,&pit->mask_notifier);
 
Quoted 6 lines

arch/x86/kvm/i8254.c struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags) · line 716

Quoted 3 lines
arch/x86/kvm/i8254.c +1 -1
  	kvm_iodevice_init(&pit->dev,&pit_dev_ops);
-	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,&pit->dev);
+	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, KVM_PIT_BASE_ADDRESS, KVM_PIT_MEM_LENGTH,&pit->dev);

Long line.

Will fix.

Quoted 22 lines
Quoted 9 lines
arch/x86/kvm/i8254.c +5 -2
-static inline struct kvm_pic *to_pic(struct kvm_io_device *dev)
+static inline struct kvm_pic *to_pic(struct kvm_io_device *dev, gpa_t addr)
  {
-	return container_of(dev, struct kvm_pic, dev);
+	switch (addr) {
+	case 0x20:
+	case 0x21:
+		return container_of(dev, struct kvm_pic, dev_master);

arch/x86/kvm/i8254.c struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)

Quoted 10 lines
arch/x86/kvm/i8254.c +9 -0
+	case 0xa0:
+	case 0xa1:
+		return container_of(dev, struct kvm_pic, dev_slave);
+	case 0x4d0:
+	case 0x4d1:
+		return container_of(dev, struct kvm_pic, dev_eclr);
+	}
+
+	return NULL;
  }

Somewhat ugly. I think

Quoted 6 lines
     int picdev_write_master(...)
    {
         return pcidev_write(container_of(...), ...);
     }

is nicer, no?

It would mean we need a total of 6 wrappers for master, slave and eclr instead of this switch, if that sounds ok I'll change it.

Quoted 29 lines
Quoted 29 lines
arch/x86/kvm/i8254.c +15 -6
@@ -560,16 +572,36 @@ struct kvm_pic *kvm_create_pic(struct kvm *kvm)
  	/*
  	 * Initialize PIO device
  	 */
-	kvm_iodevice_init(&s->dev,&picdev_ops);
+	kvm_iodevice_init(&s->dev_master,&picdev_ops);
+	kvm_iodevice_init(&s->dev_slave,&picdev_ops);
+	kvm_iodevice_init(&s->dev_eclr,&picdev_ops);
  	mutex_lock(&kvm->slots_lock);
-	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS,&s->dev);
+	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x20, 2,&s->dev_master);
+	if (ret<  0)
+		goto fail_unlock;
+
+	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0xa0, 2,&s->dev_slave);
+	if (ret<  0)
+		goto fail_unlock;
+
+	ret = kvm_io_bus_register_dev(kvm, KVM_PIO_BUS, 0x4d0, 2,&s->dev_eclr);
+	if (ret<  0)
+		goto fail_unlock;
+
  	mutex_unlock(&kvm->slots_lock);
-	if (ret<  0) {
-		kfree(s);
-		return NULL;
-	}
 
Quoted 16 lines

arch/x86/kvm/i8254.c struct kvm_pic *kvm_create_pic(struct kvm *kvm) · line 605

Quoted 13 lines
arch/x86/kvm/i8254.c +11 -0
  	return s;
+
+fail_unlock:
+
+	mutex_unlock(&kvm->slots_lock);
+	kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,&s->dev_master);
+	kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,&s->dev_slave);
+	kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,&s->dev_eclr);
+
+	kfree(s);
+
+	return NULL;
  }

You're unregistering devices that were never registered. It may work now, but it's fragile.

I'll fix that.

Quoted 11 lines
Quoted 11 lines
arch/x86/kvm/i8254.c +0 -0
  	if (ret<  0)
  		goto out_free_dev;
  	list_add_tail(&dev->list,&kvm->coalesced_zones);
virt/kvm/eventfd.c +0 -0
index 73358d2..f59c1e8 100644
@@ -586,7 +586,8 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 
Quoted 7 lines

virt/kvm/eventfd.c kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 590

Quoted 5 lines
virt/kvm/eventfd.c +2 -1
  	kvm_iodevice_init(&p->dev,&ioeventfd_ops);
 
-	ret = kvm_io_bus_register_dev(kvm, bus_idx,&p->dev);
+	ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length,
+				&p->dev);

Should this be p->length or 1?

We register p->length since when we process a write, the operation should be fully contained within the IO space of the device.

We verify that the write happens on the first byte within ioeventfd write handler.

Quoted 6 lines
Quoted 6 lines
virt/kvm/eventfd.c +0 -0
  #include<asm/processor.h>
  #include<asm/io.h>
@@ -2391,24 +2393,94 @@ static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  	int i;
 
Quoted 6 lines

virt/kvm/eventfd.c static void kvm_io_bus_destroy(struct kvm_io_bus *bus) · line 2397

Quoted 4 lines
virt/kvm/eventfd.c +1 -1
  	for (i = 0; i<  bus->dev_count; i++) {
-		struct kvm_io_device *pos = bus->devs[i];
+		struct kvm_io_device *pos = bus->range[i].dev;
 

This will call the destructor three times for the PIC. Is this safe?

PIC doesn't have a destructor for devices, the code above just does nothing for PIC devices.

Signature · 1 line
Sasha.
#3 lore raw
Quoted 27 lines · Sasha Levin
Quoted 27 lines
Quoted 17 lines

-static inline struct kvm_pic *to_pic(struct kvm_io_device *dev) +static inline struct kvm_pic *to_pic(struct kvm_io_device *dev, gpa_t addr)
{
- return container_of(dev, struct kvm_pic, dev);
+ switch (addr) {
+ case 0x20:
+ case 0x21:
+ return container_of(dev, struct kvm_pic, dev_master);
+ case 0xa0:
+ case 0xa1:
+ return container_of(dev, struct kvm_pic, dev_slave);
+ case 0x4d0:
+ case 0x4d1:
+ return container_of(dev, struct kvm_pic, dev_eclr);
+ }
+

Quoted 2 lines
 +	return NULL;
   }

Somewhat ugly. I think

      int picdev_write_master(...)
     {
          return pcidev_write(container_of(...), ...);
      }
Quoted 4 lines
Quoted 1 line

is nicer, no?

It would mean we need a total of 6 wrappers for master, slave and eclr instead of this switch, if that sounds ok I'll change it.

IMO, they're better than the switch.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

   	if (ret<   0)
   		goto out_free_dev;
   	list_add_tail(&dev->list,&kvm->coalesced_zones);

diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c
index 73358d2..f59c1e8 100644
--- a/virt/kvm/eventfd.c
+++ b/virt/kvm/eventfd.c
@@ -586,7 +586,8 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)

   	kvm_iodevice_init(&p->dev,&ioeventfd_ops);

- ret = kvm_io_bus_register_dev(kvm, bus_idx,&p->dev);
+ ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length,
+ &p->dev);

Quoted 7 lines
Quoted 1 line

Should this be p->length or 1?

We register p->length since when we process a write, the operation should be fully contained within the IO space of the device.

We verify that the write happens on the first byte within ioeventfd write handler.

Ok.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
   #include<asm/processor.h>
   #include<asm/io.h>

@@ -2391,24 +2393,94 @@ static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
int i;

   	for (i = 0; i<   bus->dev_count; i++) {

- struct kvm_io_device *pos = bus->devs[i];
+ struct kvm_io_device *pos = bus->range[i].dev;

Quoted 4 lines
Quoted 1 line

This will call the destructor three times for the PIC. Is this safe?

PIC doesn't have a destructor for devices, the code above just does nothing for PIC devices.

Ok.

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