From patchwork Fri Nov 4 17:31:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 9412965 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BBE5160723 for ; Fri, 4 Nov 2016 17:31:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AC24C2ADFB for ; Fri, 4 Nov 2016 17:31:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A0DC92B126; Fri, 4 Nov 2016 17:31:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 309A22B126 for ; Fri, 4 Nov 2016 17:31:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935162AbcKDRbo (ORCPT ); Fri, 4 Nov 2016 13:31:44 -0400 Received: from foss.arm.com ([217.140.101.70]:36598 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934916AbcKDRbn (ORCPT ); Fri, 4 Nov 2016 13:31:43 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D7D591596; Fri, 4 Nov 2016 10:31:42 -0700 (PDT) Received: from e104803-lin.lan (unknown [10.1.207.46]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B3D5E3F318; Fri, 4 Nov 2016 10:31:41 -0700 (PDT) From: Andre Przywara To: Will Deacon Cc: Marc Zyngier , kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Vladimir Murzin Subject: [PATCH v8 04/16] MSI-X: update GSI routing after changed MSI-X configuration Date: Fri, 4 Nov 2016 17:31:51 +0000 Message-Id: <20161104173203.21168-5-andre.przywara@arm.com> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20161104173203.21168-1-andre.przywara@arm.com> References: <20161104173203.21168-1-andre.przywara@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When we set up GSI routing to map MSIs to KVM's GSI numbers, we write the current device's MSI setup into the kernel routing table. However the device driver in the guest can use PCI configuration space accesses to change the MSI configuration (address and/or payload data). Whenever this happens after we have setup the routing table already, we must amend the previously sent data. So when MSI-X PCI config space accesses write address or payload, find the associated GSI number and the matching routing table entry and update the kernel routing table (only if the data has changed). This fixes vhost-net, where the queue's IRQFD was setup before the MSI vectors. Signed-off-by: Andre Przywara --- include/kvm/irq.h | 1 + irq.c | 31 +++++++++++++++++++++++++++++++ virtio/pci.c | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/include/kvm/irq.h b/include/kvm/irq.h index bb71521..f35eb7e 100644 --- a/include/kvm/irq.h +++ b/include/kvm/irq.h @@ -21,5 +21,6 @@ int irq__exit(struct kvm *kvm); int irq__allocate_routing_entry(void); int irq__add_msix_route(struct kvm *kvm, struct msi_msg *msg); +void irq__update_msix_route(struct kvm *kvm, u32 gsi, struct msi_msg *msg); #endif diff --git a/irq.c b/irq.c index a742aa2..895e5eb 100644 --- a/irq.c +++ b/irq.c @@ -93,6 +93,37 @@ int irq__add_msix_route(struct kvm *kvm, struct msi_msg *msg) return next_gsi++; } +static bool update_data(u32 *ptr, u32 newdata) +{ + if (*ptr == newdata) + return false; + + *ptr = newdata; + return true; +} + +void irq__update_msix_route(struct kvm *kvm, u32 gsi, struct msi_msg *msg) +{ + struct kvm_irq_routing_msi *entry; + unsigned int i; + bool changed; + + for (i = 0; i < irq_routing->nr; i++) + if (gsi == irq_routing->entries[i].gsi) + break; + if (i == irq_routing->nr) + return; + + entry = &irq_routing->entries[i].u.msi; + + changed = update_data(&entry->address_hi, msg->address_hi); + changed |= update_data(&entry->address_lo, msg->address_lo); + changed |= update_data(&entry->data, msg->data); + + if (changed) + ioctl(kvm->vm_fd, KVM_SET_GSI_ROUTING, irq_routing); +} + int __attribute__((weak)) irq__exit(struct kvm *kvm) { free(irq_routing); diff --git a/virtio/pci.c b/virtio/pci.c index 072e5b7..b3b4aac 100644 --- a/virtio/pci.c +++ b/virtio/pci.c @@ -152,6 +152,30 @@ static bool virtio_pci__io_in(struct ioport *ioport, struct kvm_cpu *vcpu, u16 p return ret; } +static void update_msix_map(struct virtio_pci *vpci, + struct msix_table *msix_entry, u32 vecnum) +{ + u32 gsi, i; + + /* Find the GSI number used for that vector */ + if (vecnum == vpci->config_vector) { + gsi = vpci->config_gsi; + } else { + for (i = 0; i < VIRTIO_PCI_MAX_VQ; i++) + if (vpci->vq_vector[i] == vecnum) + break; + if (i == VIRTIO_PCI_MAX_VQ) + return; + gsi = vpci->gsis[i]; + } + + if (gsi == 0) + return; + + msix_entry = &msix_entry[vecnum]; + irq__update_msix_route(vpci->kvm, gsi, &msix_entry->msg); +} + static bool virtio_pci__specific_io_out(struct kvm *kvm, struct virtio_device *vdev, u16 port, void *data, int size, int offset) { @@ -270,10 +294,16 @@ static void virtio_pci__msix_mmio_callback(struct kvm_cpu *vcpu, offset = vpci->msix_io_block; } - if (is_write) - memcpy(table + addr - offset, data, len); - else + if (!is_write) { memcpy(data, table + addr - offset, len); + return; + } + + memcpy(table + addr - offset, data, len); + + /* Did we just update the address or payload? */ + if (addr % 0x10 < 0xc) + update_msix_map(vpci, table, (addr - offset) / 16); } static void virtio_pci__signal_msi(struct kvm *kvm, struct virtio_pci *vpci, int vec)