Skip to contentSkip to conversation

KVM: ioeventfd cookies

PATCH Superseded Cornelia Huck 2 participants 4 messages Jul 3, 2013

Subsystems (7)
x86 KVM PowerPC s390 +3 more subsystems

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM: ioeventfd cookies

Hi,

here's the second version of the ioeventfd cookies support. Changes:

- provide kvm_io_bus_read_cookie as well
- refactoring of the cookie code in kvm_main.c
- adapt callers to changes

Cornelia Huck (2):
KVM: kvm-io: support cookies
KVM: s390: use cookies for ioeventfd

 arch/ia64/kvm/kvm-ia64.c   |  2 +-
 arch/powerpc/kvm/powerpc.c |  4 ++--
 arch/s390/kvm/diag.c       | 15 +++++++++---
 arch/x86/kvm/x86.c         |  8 ++++---
 include/linux/kvm_host.h   |  4 ++++
 virt/kvm/kvm_main.c        | 58 +++++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 81 insertions(+), 10 deletions(-)
Signature · 1 line
1.8.2.2
#1 lore raw

PATCH v2 1/2 KVM: kvm-io: support cookies

Add new functions kvm_io_bus_{read,write}_cookie() that allows users of the kvm io infrastructure to use a cookie value to speed up lookup of a device on an io bus.

kvm_io_bus_{read,write} now returns the index on the bus; existing callers have been fixed up to accept return codes > 0.

---
 arch/ia64/kvm/kvm-ia64.c   |  2 +-
 arch/powerpc/kvm/powerpc.c |  4 ++--
 arch/x86/kvm/x86.c         |  8 ++++---
 include/linux/kvm_host.h   |  4 ++++
 virt/kvm/kvm_main.c        | 58 +++++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 69 insertions(+), 7 deletions(-)
arch/ia64/kvm/kvm-ia64.c +1 -1
index 5b2dc0d..465ab54 100644
@@ -250,7 +250,7 @@ mmio:
 	else
 		r = kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, p->addr,
 				     p->size, &p->data);
-	if (r)
+	if (r < 0)
 		printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
 	p->state = STATE_IORESP_READY;
 
arch/powerpc/kvm/powerpc.c +2 -2
index 6316ee3..26c44d0 100644
@@ -649,7 +649,7 @@ int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
 
 	srcu_read_unlock(&vcpu->kvm->srcu, idx);
 
-	if (!ret) {
+	if (ret >= 0) {
 		kvmppc_complete_mmio_load(vcpu, run);
 		vcpu->mmio_needed = 0;
 		return EMULATE_DONE;
@@ -711,7 +711,7 @@ int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
 
 	srcu_read_unlock(&vcpu->kvm->srcu, idx);
 
-	if (!ret) {
+	if (ret >= 0) {
 		vcpu->mmio_needed = 0;
 		return EMULATE_DONE;
 	}
arch/x86/kvm/x86.c +5 -3
index 7d71c0f..62b325c 100644
@@ -3860,7 +3860,8 @@ static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
 		n = min(len, 8);
 		if (!(vcpu->arch.apic &&
 		      !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, n, v))
-		    && kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
+		    && (kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, n, v)
+			< 0))
 			break;
 		handled += n;
 		addr += n;
@@ -3880,7 +3881,8 @@ static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
 		n = min(len, 8);
 		if (!(vcpu->arch.apic &&
 		      !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, n, v))
-		    && kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
+		    && (kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, n, v)
+			< 0))
 			break;
 		trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
 		handled += n;
@@ -4361,7 +4363,7 @@ static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
 		r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
 				     vcpu->arch.pio.port, vcpu->arch.pio.size,
 				     pd);
-	return r;
+	return (r >= 0) ? 0 : r;
 }
 
 static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
include/linux/kvm_host.h +4 -0
index e3aae6d..60e261c9 100644
@@ -159,8 +159,12 @@ enum kvm_bus {
 
 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_write_cookie(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
+			    int len, const void *val, long cookie);
 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len,
 		    void *val);
+int kvm_io_bus_read_cookie(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
+			   int len, void *val, long cookie);
 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,
virt/kvm/kvm_main.c +57 -1

76 lines not on this page · raw message

Signature · 1 line
1.8.2.2
#2 lore raw

Re: [PATCH v2 1/2] KVM: kvm-io: support cookies

Quoted 56 lines · Cornelia Huck

Add new functions kvm_io_bus_{read,write}_cookie() that allows users of the kvm io infrastructure to use a cookie value to speed up lookup of a device on an io bus.

kvm_io_bus_{read,write} now returns the index on the bus; existing callers have been fixed up to accept return codes > 0.

---
 arch/ia64/kvm/kvm-ia64.c   |  2 +-
 arch/powerpc/kvm/powerpc.c |  4 ++--
 arch/x86/kvm/x86.c         |  8 ++++---
 include/linux/kvm_host.h   |  4 ++++
 virt/kvm/kvm_main.c        | 58 +++++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 69 insertions(+), 7 deletions(-)
arch/ia64/kvm/kvm-ia64.c +1 -1
index 5b2dc0d..465ab54 100644
@@ -250,7 +250,7 @@ mmio:
 	else
 		r = kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, p->addr,
 				     p->size, &p->data);
-	if (r)
+	if (r < 0)
 		printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
 	p->state = STATE_IORESP_READY;
 
arch/powerpc/kvm/powerpc.c +2 -2
index 6316ee3..26c44d0 100644
@@ -649,7 +649,7 @@ int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
 
 	srcu_read_unlock(&vcpu->kvm->srcu, idx);
 
-	if (!ret) {
+	if (ret >= 0) {
 		kvmppc_complete_mmio_load(vcpu, run);
 		vcpu->mmio_needed = 0;
 		return EMULATE_DONE;
@@ -711,7 +711,7 @@ int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
 
 	srcu_read_unlock(&vcpu->kvm->srcu, idx);
 
-	if (!ret) {
+	if (ret >= 0) {
 		vcpu->mmio_needed = 0;
 		return EMULATE_DONE;
 	}
arch/x86/kvm/x86.c +0 -0
index 7d71c0f..62b325c 100644

arch/x86/kvm/x86.c static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, · line 3864

Quoted 6 lines
arch/x86/kvm/x86.c +2 -1
@@ -3860,7 +3860,8 @@ static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
 		n = min(len, 8);
 		if (!(vcpu->arch.apic &&
 		      !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, n, v))
-		    && kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
+		    && (kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, n, v)
+			< 0))

Better to go over 80 characters then break the line like that.

Quoted 44 lines
arch/x86/kvm/x86.c +3 -2
 			break;
 		handled += n;
 		addr += n;
@@ -3880,7 +3881,8 @@ static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
 		n = min(len, 8);
 		if (!(vcpu->arch.apic &&
 		      !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, n, v))
-		    && kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
+		    && (kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, n, v)
+			< 0))
 			break;
 		trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
 		handled += n;
@@ -4361,7 +4363,7 @@ static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
 		r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
 				     vcpu->arch.pio.port, vcpu->arch.pio.size,
 				     pd);
-	return r;
+	return (r >= 0) ? 0 : r;
 }
 
 static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
include/linux/kvm_host.h +4 -0
index e3aae6d..60e261c9 100644
@@ -159,8 +159,12 @@ enum kvm_bus {
 
 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_write_cookie(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
+			    int len, const void *val, long cookie);
 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len,
 		    void *val);
+int kvm_io_bus_read_cookie(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
+			   int len, void *val, long cookie);
 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,
virt/kvm/kvm_main.c +0 -0
index 1580dd4..ed34f10 100644

virt/kvm/kvm_main.c int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, · line 2888

Quoted 6 lines
virt/kvm/kvm_main.c +1 -1
@@ -2884,13 +2884,41 @@ int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
 	while (idx < bus->dev_count &&
 		kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
 		if (!kvm_iodevice_write(bus->range[idx].dev, addr, len, val))
-			return 0;
+			return idx;
 		idx++;

Do not see the same change for kvm_io_bus_read().

Quoted 8 lines
virt/kvm/kvm_main.c +3 -0
 	}
 
 	return -EOPNOTSUPP;
 }
 
+/* kvm_io_bus_write_cookie - called under kvm->slots_lock */
+int kvm_io_bus_write_cookie(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
+			    int len, const void *val, long cookie)

virt/kvm/kvm_main.c int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, · line 2906

Quoted 10 lines
virt/kvm/kvm_main.c +10 -0
+{
+	struct kvm_io_bus *bus;
+	struct kvm_io_range range;
+
+	range = (struct kvm_io_range) {
+		.addr = addr,
+		.len = len,
+	};
+
+	bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);

I would refactor code a little bit to avoid calling srcu_dereference twice and avoid changing kvm_io_bus_write() return semantics:

add __kvm_io_bus_write() functions that gets range and bus as parameters, call it here and from kvm_io_bus_write(). Same for the read.

Quoted 55 lines
virt/kvm/kvm_main.c +43 -0
+
+	/* First try the device referenced by cookie. */
+	if ((cookie >= 0) && (cookie < bus->dev_count) &&
+	    (kvm_io_bus_sort_cmp(&range, &bus->range[cookie]) == 0))
+		if (!kvm_iodevice_write(bus->range[cookie].dev, addr, len,
+					val))
+			return cookie;
+
+	/*
+	 * cookie contained garbage; fall back to search and return the
+	 * correct cookie value.
+	 */
+	return kvm_io_bus_write(kvm, bus_idx, addr, len, val);
+}
+
 /* kvm_io_bus_read - called under kvm->slots_lock */
 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
 		    int len, void *val)
@@ -2919,6 +2947,34 @@ int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
 	return -EOPNOTSUPP;
 }
 
+/* kvm_io_bus_read_cookie - called under kvm->slots_lock */
+int kvm_io_bus_read_cookie(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
+			   int len, void *val, long cookie)
+{
+	struct kvm_io_bus *bus;
+	struct kvm_io_range range;
+
+	range = (struct kvm_io_range) {
+		.addr = addr,
+		.len = len,
+	};
+
+	bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
+
+	/* First try the device referenced by cookie. */
+	if ((cookie >= 0) && (cookie < bus->dev_count) &&
+	    (kvm_io_bus_sort_cmp(&range, &bus->range[cookie]) == 0))
+		if (!kvm_iodevice_read(bus->range[cookie].dev, addr, len,
+				       val))
+			return cookie;
+
+	/*
+	 * cookie contained garbage; fall back to search and return the
+	 * correct cookie value.
+	 */
+	return kvm_io_bus_read(kvm, bus_idx, addr, len, val);
+}
+
 /* Caller must hold slots_lock. */
 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
 			    int len, struct kvm_io_device *dev)
Signature · 1 line
1.8.2.2

--
Gleb.

#3 lore raw

PATCH v2 2/2 KVM: s390: use cookies for ioeventfd

In reply to Cornelia Huck

Make use of cookies for the virtio ccw notification hypercall to speed up lookup of devices on the io bus.

---
 arch/s390/kvm/diag.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
arch/s390/kvm/diag.c +12 -3
index 3074475..e88c76e 100644
@@ -119,11 +119,20 @@ static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
 	 * The layout is as follows:
 	 * - gpr 2 contains the subchannel id (passed as addr)
 	 * - gpr 3 contains the virtqueue index (passed as datamatch)
+	 * - gpr 4 contains the index on the bus (optionally)
 	 */
-	ret = kvm_io_bus_write(vcpu->kvm, KVM_VIRTIO_CCW_NOTIFY_BUS,
-				vcpu->run->s.regs.gprs[2],
-				8, &vcpu->run->s.regs.gprs[3]);
+	ret = kvm_io_bus_write_cookie(vcpu->kvm, KVM_VIRTIO_CCW_NOTIFY_BUS,
+				      vcpu->run->s.regs.gprs[2],
+				      8, &vcpu->run->s.regs.gprs[3],
+				      vcpu->run->s.regs.gprs[4]);
 	srcu_read_unlock(&vcpu->kvm->srcu, idx);
+
+	/*
+	 * Return cookie in gpr 2, but don't overwrite the register if the
+	 * diagnose will be handled by userspace.
+	 */
+	if (ret != -EOPNOTSUPP)
+		vcpu->run->s.regs.gprs[2] = ret;
 	/* kvm_io_bus_write returns -EOPNOTSUPP if it found no match. */
 	return ret < 0 ? ret : 0;
 }
Signature · 1 line
1.8.2.2