From patchwork Thu Sep 12 21:27:31 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 274614 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 5B23B2C0174 for ; Fri, 13 Sep 2013 07:28:03 +1000 (EST) Received: from localhost ([::1]:43901 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VKEQj-0001EE-G9 for incoming@patchwork.ozlabs.org; Thu, 12 Sep 2013 17:28:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34263) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VKEQP-0001E1-NW for qemu-devel@nongnu.org; Thu, 12 Sep 2013 17:27:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VKEQJ-0006sf-Jc for qemu-devel@nongnu.org; Thu, 12 Sep 2013 17:27:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47280) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VKEQJ-0006sP-BU for qemu-devel@nongnu.org; Thu, 12 Sep 2013 17:27:35 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r8CLRWH7002054 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 12 Sep 2013 17:27:32 -0400 Received: from bling.home (ovpn-113-59.phx2.redhat.com [10.3.113.59]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r8CLRVrv000997; Thu, 12 Sep 2013 17:27:32 -0400 To: kvm@vger.kernel.org, gleb@redhat.com From: Alex Williamson Date: Thu, 12 Sep 2013 15:27:31 -0600 Message-ID: <20130912212536.8901.49462.stgit@bling.home> User-Agent: StGit/0.16 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: aik@ozlabs.ru, bsd@redhat.com, qemu-devel@nongnu.org, mst@redhat.com Subject: [Qemu-devel] [RFC PATCH] vfio-pci: Make use of new KVM-VFIO device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add and remove groups from the KVM virtual VFIO device as we make use of them. This allows KVM to optimize for performance and correctness based on properties of the group. Signed-off-by: Alex Williamson --- This patch is enabled by: [RFC PATCH 0/3] kvm/vfio: Manage KVM IOMMU coherency with virtual VFIO device (kvm list for those watching from qemu-devel) hw/misc/vfio.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c index 98d372f..02b0f5a 100644 --- a/hw/misc/vfio.c +++ b/hw/misc/vfio.c @@ -53,6 +53,8 @@ #define VFIO_ALLOW_MMAP 1 #define VFIO_ALLOW_KVM_INTX 1 +int vfio_kvm_device_fd = -1; + struct VFIODevice; typedef struct VFIOQuirk { @@ -3032,6 +3034,61 @@ static void vfio_pci_reset_handler(void *opaque) } } +static void vfio_kvm_device_add_group(VFIOGroup *group) +{ +#ifdef CONFIG_KVM + struct kvm_device_attr attr = { + .group = KVM_DEV_VFIO_ADD_GROUP, + .addr = group->fd, + }; + + if (vfio_kvm_device_fd < 0) { + struct kvm_create_device cd = { + .type = KVM_DEV_TYPE_VFIO, + }; + + if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) { + DPRINTF("KVM_CREATE_DEVICE: %m\n"); + return; + } + + vfio_kvm_device_fd = cd.fd; + } + + if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { + error_report("Failed to add group %d to KVM VFIO device: %m", + group->groupid); + } +#endif +} + +static void vfio_kvm_device_del_group(VFIOGroup *group) +{ +#ifdef CONFIG_KVM + struct kvm_device_attr attr = { + .group = KVM_DEV_VFIO_DEL_GROUP, + .addr = group->fd, + }; + + if (vfio_kvm_device_fd < 0) { + return; + } + + if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { + error_report("Failed to remove group %d to KVM VFIO device: %m", + group->groupid); + } +#endif +} + +static void vfio_kvm_device_cleanup(void) +{ + if (vfio_kvm_device_fd >= 0) { + close(vfio_kvm_device_fd); + vfio_kvm_device_fd = -1; + } +} + static int vfio_connect_container(VFIOGroup *group) { VFIOContainer *container; @@ -3180,6 +3237,8 @@ static VFIOGroup *vfio_get_group(int groupid) QLIST_INSERT_HEAD(&group_list, group, next); + vfio_kvm_device_add_group(group); + return group; } @@ -3189,6 +3248,7 @@ static void vfio_put_group(VFIOGroup *group) return; } + vfio_kvm_device_del_group(group); vfio_disconnect_container(group); QLIST_REMOVE(group, next); DPRINTF("vfio_put_group: close group->fd\n"); @@ -3197,6 +3257,7 @@ static void vfio_put_group(VFIOGroup *group) if (QLIST_EMPTY(&group_list)) { qemu_unregister_reset(vfio_pci_reset_handler, NULL); + vfio_kvm_device_cleanup(); } }