From patchwork Wed Jul 3 11:45:15 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cornelia Huck X-Patchwork-Id: 2816371 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 74DB79F3EB for ; Wed, 3 Jul 2013 11:45:32 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F1B952016D for ; Wed, 3 Jul 2013 11:45:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 777A520169 for ; Wed, 3 Jul 2013 11:45:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755995Ab3GCLpZ (ORCPT ); Wed, 3 Jul 2013 07:45:25 -0400 Received: from e06smtp12.uk.ibm.com ([195.75.94.108]:45254 "EHLO e06smtp12.uk.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755967Ab3GCLpY convert rfc822-to-8bit (ORCPT ); Wed, 3 Jul 2013 07:45:24 -0400 Received: from /spool/local by e06smtp12.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 3 Jul 2013 12:40:17 +0100 Received: from d06dlp02.portsmouth.uk.ibm.com (9.149.20.14) by e06smtp12.uk.ibm.com (192.168.101.142) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 3 Jul 2013 12:40:15 +0100 Received: from b06cxnps4075.portsmouth.uk.ibm.com (d06relay12.portsmouth.uk.ibm.com [9.149.109.197]) by d06dlp02.portsmouth.uk.ibm.com (Postfix) with ESMTP id AA2352190056; Wed, 3 Jul 2013 12:49:04 +0100 (BST) Received: from d06av03.portsmouth.uk.ibm.com (d06av03.portsmouth.uk.ibm.com [9.149.37.213]) by b06cxnps4075.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r63Bj8tR53936300; Wed, 3 Jul 2013 11:45:08 GMT Received: from d06av03.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av03.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id r63BjIhl032059; Wed, 3 Jul 2013 05:45:19 -0600 Received: from gondolin (dyn-9-152-224-218.boeblingen.de.ibm.com [9.152.224.218]) by d06av03.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id r63BjIgS032047; Wed, 3 Jul 2013 05:45:18 -0600 Date: Wed, 3 Jul 2013 13:45:15 +0200 From: Cornelia Huck To: Paolo Bonzini Cc: Gleb Natapov , Christian Borntraeger , Heiko Carstens , Martin Schwidefsky , KVM , linux-s390 Subject: Re: [PATCH 1/2] KVM: kvm-io: support cookies Message-ID: <20130703134515.0b5095b9@gondolin> In-Reply-To: <51D403B8.5040002@redhat.com> References: <1372841780-29645-1-git-send-email-cornelia.huck@de.ibm.com> <1372841780-29645-2-git-send-email-cornelia.huck@de.ibm.com> <51D3E96E.5080405@redhat.com> <51D3ED13.2020408@redhat.com> <20130703125138.400ac1ef@gondolin> <51D403B8.5040002@redhat.com> Organization: IBM Deutschland Research & Development GmbH Vorsitzende des Aufsichtsrats: Martina Koederitz =?UTF-8?B?R2VzY2jDpGZ0c2bDvGhydW5nOg==?= Dirk Wittkopp Sitz der Gesellschaft: =?UTF-8?B?QsO2Ymxpbmdlbg==?= Registergericht: Amtsgericht Stuttgart, HRB 243294 Mime-Version: 1.0 X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13070311-8372-0000-0000-0000067B09F1 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Wed, 03 Jul 2013 12:58:00 +0200 Paolo Bonzini wrote: > Il 03/07/2013 12:51, Cornelia Huck ha scritto: > > On Wed, 03 Jul 2013 11:21:23 +0200 > > Paolo Bonzini wrote: > > > >> Il 03/07/2013 11:05, Paolo Bonzini ha scritto: > >>> Nice idea, though I don't really like the duplication between > >>> kvm_io_bus_write and kvm_io_bus_write_cookie. > >>> > >>> Can you make kvm_io_bus_write, and perhaps kvm_io_bus_read too, return > >>> the cookie, and return -EINVAL here if the cookie is garbage? > >> > >> On second though---no need to return -EINVAL, you can just pass the > >> cookie by value and tail-call kvm_io_bus_write. Whatever makes the s390 > >> code look nicer. > > > > It would probably be easier to have the non-cookie functions call the > > cookie functions with a negative cookie value, like the following > > (untested): > > That would work too, but it adds a small overhead to the non-cookie > calls. If Gleb agrees with you, it is fine; otherwise, I'd prefer that > one explicitly requests the cookie on input. Just to compare, I also did a patch that returns the index instead (equally untested). Changing the callers was straightforward; we gain an extra scu_dereference() in the invalid cookie case. From ff45cdc27e47020251f0100759adf372c19a29c4 Mon Sep 17 00:00:00 2001 From: Cornelia Huck Date: Tue, 2 Jul 2013 13:30:56 +0200 Subject: [PATCH 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. Signed-off-by: Cornelia Huck --- 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(-) diff --git a/arch/ia64/kvm/kvm-ia64.c b/arch/ia64/kvm/kvm-ia64.c index 5b2dc0d..465ab54 100644 --- a/arch/ia64/kvm/kvm-ia64.c +++ b/arch/ia64/kvm/kvm-ia64.c @@ -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; diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index 6316ee3..26c44d0 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -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; } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7d71c0f..62b325c 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -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, diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index e3aae6d..60e261c9 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -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, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 1580dd4..ed34f10 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -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++; } 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) +{ + 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_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)