From patchwork Fri Jan 11 16:58:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 1023694 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43bq0C4zD6z9sCX for ; Sat, 12 Jan 2019 04:01:47 +1100 (AEDT) Received: from localhost ([127.0.0.1]:46882 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gi0Bl-0002Mi-8H for incoming@patchwork.ozlabs.org; Fri, 11 Jan 2019 12:01:45 -0500 Received: from eggs.gnu.org ([209.51.188.92]:59400) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gi08U-0000ut-Dg for qemu-devel@nongnu.org; Fri, 11 Jan 2019 11:58:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gi08R-0001hs-GZ for qemu-devel@nongnu.org; Fri, 11 Jan 2019 11:58:22 -0500 Received: from mx1.redhat.com ([209.132.183.28]:40276) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gi08R-0001gu-03 for qemu-devel@nongnu.org; Fri, 11 Jan 2019 11:58:19 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 288BCC0587DF; Fri, 11 Jan 2019 16:58:18 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-217.ams2.redhat.com [10.36.116.217]) by smtp.corp.redhat.com (Postfix) with ESMTP id B07E8282D4; Fri, 11 Jan 2019 16:58:16 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, alex.williamson@redhat.com Date: Fri, 11 Jan 2019 17:58:00 +0100 Message-Id: <20190111165801.15181-2-eric.auger@redhat.com> In-Reply-To: <20190111165801.15181-1-eric.auger@redhat.com> References: <20190111165801.15181-1-eric.auger@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Fri, 11 Jan 2019 16:58:18 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/2] vfio-pci: Introduce vfio_register_event_notifier helper X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 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" The code used to attach the eventfd handler for the ERR and REQ irq indices can be factorized into a helper. In subsequent patches we will extend this helper to support other irq indices. We test the notification is allowed outside of the helper: respectively check vdev->pci_aer and VFIO_FEATURE_ENABLE_REQ. Depending on the returned value we set vdev->pci_aer and vdev->req_enabled. An error handle is introduced for future usage although not strictly useful here. Signed-off-by: Eric Auger --- hw/vfio/pci.c | 291 ++++++++++++++++++++++---------------------------- 1 file changed, 127 insertions(+), 164 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index c0cb1ec289..c589a4e666 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -105,6 +105,95 @@ static void vfio_intx_eoi(VFIODevice *vbasedev) vfio_unmask_single_irqindex(vbasedev, VFIO_PCI_INTX_IRQ_INDEX); } +/* + * vfio_register_event_notifier - setup/tear down eventfd + * notification and handling for IRQ indices that span over + * a single IRQ + * + * @vdev: VFIO device handle + * @index: IRQ index the eventfd/handler is associated to + * @target_state: true means notifier needs to be set up + * @handler to attach if @target_state is true + * @errp error handle + */ +static int vfio_register_event_notifier(VFIOPCIDevice *vdev, + int index, + bool target_state, + void (*handler)(void *opaque), + Error **errp) +{ + struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info), + .index = index }; + struct vfio_irq_set *irq_set; + EventNotifier *notifier; + int argsz, ret = 0; + int32_t *pfd, fd; + + switch (index) { + case VFIO_PCI_REQ_IRQ_INDEX: + notifier = &vdev->req_notifier; + break; + case VFIO_PCI_ERR_IRQ_INDEX: + notifier = &vdev->err_notifier; + break; + default: + return -EINVAL; + } + + if (ioctl(vdev->vbasedev.fd, + VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0 || irq_info.count < 1) { + error_setg_errno(errp, errno, + "no irq index %d available", index); + return -EINVAL; + } + + if (target_state) { + ret = event_notifier_init(notifier, 0); + if (ret) { + error_setg_errno(errp, -ret, + "Unable to init event notifier for irq index %d", + index); + return ret; + } + } + + argsz = sizeof(*irq_set) + sizeof(*pfd); + + irq_set = g_malloc0(argsz); + irq_set->argsz = argsz; + irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | + VFIO_IRQ_SET_ACTION_TRIGGER; + irq_set->index = index; + irq_set->start = 0; + irq_set->count = 1; + pfd = (int32_t *)&irq_set->data; + + if (!notifier) { + return -EINVAL; + } + + fd = event_notifier_get_fd(notifier); + + if (target_state) { + qemu_set_fd_handler(fd, handler, NULL, vdev); + *pfd = fd; + } else { + qemu_set_fd_handler(fd, NULL, NULL, vdev); + event_notifier_cleanup(notifier); + *pfd = -1; + } + + ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); + g_free(irq_set); + + if (ret) { + error_setg_errno(errp, -ret, + "vfio: Failed to %s eventfd signalling for index %d", + *pfd < 0 ? "set up" : "tear down", index); + } + return ret; +} + static void vfio_intx_enable_kvm(VFIOPCIDevice *vdev, Error **errp) { #ifdef CONFIG_KVM @@ -2621,86 +2710,6 @@ static void vfio_err_notifier_handler(void *opaque) vm_stop(RUN_STATE_INTERNAL_ERROR); } -/* - * Registers error notifier for devices supporting error recovery. - * If we encounter a failure in this function, we report an error - * and continue after disabling error recovery support for the - * device. - */ -static void vfio_register_err_notifier(VFIOPCIDevice *vdev) -{ - int ret; - int argsz; - struct vfio_irq_set *irq_set; - int32_t *pfd; - - if (!vdev->pci_aer) { - return; - } - - if (event_notifier_init(&vdev->err_notifier, 0)) { - error_report("vfio: Unable to init event notifier for error detection"); - vdev->pci_aer = false; - return; - } - - argsz = sizeof(*irq_set) + sizeof(*pfd); - - irq_set = g_malloc0(argsz); - irq_set->argsz = argsz; - irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | - VFIO_IRQ_SET_ACTION_TRIGGER; - irq_set->index = VFIO_PCI_ERR_IRQ_INDEX; - irq_set->start = 0; - irq_set->count = 1; - pfd = (int32_t *)&irq_set->data; - - *pfd = event_notifier_get_fd(&vdev->err_notifier); - qemu_set_fd_handler(*pfd, vfio_err_notifier_handler, NULL, vdev); - - ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); - if (ret) { - error_report("vfio: Failed to set up error notification"); - qemu_set_fd_handler(*pfd, NULL, NULL, vdev); - event_notifier_cleanup(&vdev->err_notifier); - vdev->pci_aer = false; - } - g_free(irq_set); -} - -static void vfio_unregister_err_notifier(VFIOPCIDevice *vdev) -{ - int argsz; - struct vfio_irq_set *irq_set; - int32_t *pfd; - int ret; - - if (!vdev->pci_aer) { - return; - } - - argsz = sizeof(*irq_set) + sizeof(*pfd); - - irq_set = g_malloc0(argsz); - irq_set->argsz = argsz; - irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | - VFIO_IRQ_SET_ACTION_TRIGGER; - irq_set->index = VFIO_PCI_ERR_IRQ_INDEX; - irq_set->start = 0; - irq_set->count = 1; - pfd = (int32_t *)&irq_set->data; - *pfd = -1; - - ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); - if (ret) { - error_report("vfio: Failed to de-assign error fd: %m"); - } - g_free(irq_set); - qemu_set_fd_handler(event_notifier_get_fd(&vdev->err_notifier), - NULL, NULL, vdev); - event_notifier_cleanup(&vdev->err_notifier); -} - static void vfio_req_notifier_handler(void *opaque) { VFIOPCIDevice *vdev = opaque; @@ -2716,86 +2725,6 @@ static void vfio_req_notifier_handler(void *opaque) } } -static void vfio_register_req_notifier(VFIOPCIDevice *vdev) -{ - struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info), - .index = VFIO_PCI_REQ_IRQ_INDEX }; - int argsz; - struct vfio_irq_set *irq_set; - int32_t *pfd; - - if (!(vdev->features & VFIO_FEATURE_ENABLE_REQ)) { - return; - } - - if (ioctl(vdev->vbasedev.fd, - VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0 || irq_info.count < 1) { - return; - } - - if (event_notifier_init(&vdev->req_notifier, 0)) { - error_report("vfio: Unable to init event notifier for device request"); - return; - } - - argsz = sizeof(*irq_set) + sizeof(*pfd); - - irq_set = g_malloc0(argsz); - irq_set->argsz = argsz; - irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | - VFIO_IRQ_SET_ACTION_TRIGGER; - irq_set->index = VFIO_PCI_REQ_IRQ_INDEX; - irq_set->start = 0; - irq_set->count = 1; - pfd = (int32_t *)&irq_set->data; - - *pfd = event_notifier_get_fd(&vdev->req_notifier); - qemu_set_fd_handler(*pfd, vfio_req_notifier_handler, NULL, vdev); - - if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) { - error_report("vfio: Failed to set up device request notification"); - qemu_set_fd_handler(*pfd, NULL, NULL, vdev); - event_notifier_cleanup(&vdev->req_notifier); - } else { - vdev->req_enabled = true; - } - - g_free(irq_set); -} - -static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) -{ - int argsz; - struct vfio_irq_set *irq_set; - int32_t *pfd; - - if (!vdev->req_enabled) { - return; - } - - argsz = sizeof(*irq_set) + sizeof(*pfd); - - irq_set = g_malloc0(argsz); - irq_set->argsz = argsz; - irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | - VFIO_IRQ_SET_ACTION_TRIGGER; - irq_set->index = VFIO_PCI_REQ_IRQ_INDEX; - irq_set->start = 0; - irq_set->count = 1; - pfd = (int32_t *)&irq_set->data; - *pfd = -1; - - if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) { - error_report("vfio: Failed to de-assign device request fd: %m"); - } - g_free(irq_set); - qemu_set_fd_handler(event_notifier_get_fd(&vdev->req_notifier), - NULL, NULL, vdev); - event_notifier_cleanup(&vdev->req_notifier); - - vdev->req_enabled = false; -} - static void vfio_realize(PCIDevice *pdev, Error **errp) { VFIOPCIDevice *vdev = PCI_VFIO(pdev); @@ -3069,8 +2998,29 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) goto out_teardown; } - vfio_register_err_notifier(vdev); - vfio_register_req_notifier(vdev); + if (vdev->pci_aer) { + /* + * Registers error notifier for devices supporting error recovery. + * If we encounter a failure in this function, we report an error + * and continue after disabling error recovery support for the + * device. + */ + vdev->pci_aer = + !vfio_register_event_notifier(vdev, VFIO_PCI_ERR_IRQ_INDEX, true, + vfio_err_notifier_handler, &err); + if (err) { + warn_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + } + } + + if (vdev->features & VFIO_FEATURE_ENABLE_REQ) { + vdev->req_enabled = + !vfio_register_event_notifier(vdev, VFIO_PCI_REQ_IRQ_INDEX, true, + vfio_req_notifier_handler, &err); + if (err) { + warn_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + } + } vfio_setup_resetfn_quirk(vdev); return; @@ -3106,9 +3056,22 @@ static void vfio_instance_finalize(Object *obj) static void vfio_exitfn(PCIDevice *pdev) { VFIOPCIDevice *vdev = PCI_VFIO(pdev); + Error *err = NULL; - vfio_unregister_req_notifier(vdev); - vfio_unregister_err_notifier(vdev); + if (vdev->req_enabled) { + vfio_register_event_notifier(vdev, VFIO_PCI_REQ_IRQ_INDEX, + false, NULL, &err); + if (err) { + warn_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + } + } + if (vdev->pci_aer) { + vfio_register_event_notifier(vdev, VFIO_PCI_ERR_IRQ_INDEX, + false, NULL, &err); + if (err) { + warn_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); + } + } pci_device_set_intx_routing_notifier(&vdev->pdev, NULL); vfio_disable_interrupts(vdev); if (vdev->intx.mmap_timer) { From patchwork Fri Jan 11 16:58:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 1023695 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43bq3B5fgFz9sBQ for ; Sat, 12 Jan 2019 04:04:21 +1100 (AEDT) Received: from localhost ([127.0.0.1]:47582 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gi0ED-00045E-Hw for incoming@patchwork.ozlabs.org; Fri, 11 Jan 2019 12:04:17 -0500 Received: from eggs.gnu.org ([209.51.188.92]:59398) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gi08U-0000up-BX for qemu-devel@nongnu.org; Fri, 11 Jan 2019 11:58:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gi08S-0001iq-O7 for qemu-devel@nongnu.org; Fri, 11 Jan 2019 11:58:22 -0500 Received: from mx1.redhat.com ([209.132.183.28]:33956) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gi08S-0001i9-IW for qemu-devel@nongnu.org; Fri, 11 Jan 2019 11:58:20 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B15E1BDE1; Fri, 11 Jan 2019 16:58:19 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-217.ams2.redhat.com [10.36.116.217]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7B56E1E9; Fri, 11 Jan 2019 16:58:18 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, alex.williamson@redhat.com Date: Fri, 11 Jan 2019 17:58:01 +0100 Message-Id: <20190111165801.15181-3-eric.auger@redhat.com> In-Reply-To: <20190111165801.15181-1-eric.auger@redhat.com> References: <20190111165801.15181-1-eric.auger@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Fri, 11 Jan 2019 16:58:19 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/2] vfio-pci: Use vfio_register_event_notifier in vfio_intx_enable_kvm X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 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" We can also use vfio_register_event_notifier() helper in vfio_intx_enable_kvm to set the signalling associated to VFIO_PCI_INTX_IRQ_INDEX. Signed-off-by: Eric Auger --- hw/vfio/pci.c | 38 +++++++------------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index c589a4e666..db0504ca10 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -136,6 +136,9 @@ static int vfio_register_event_notifier(VFIOPCIDevice *vdev, case VFIO_PCI_ERR_IRQ_INDEX: notifier = &vdev->err_notifier; break; + case VFIO_PCI_INTX_IRQ_INDEX: + notifier = &vdev->intx.interrupt; + break; default: return -EINVAL; } @@ -351,10 +354,8 @@ static void vfio_intx_update(PCIDevice *pdev) static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp) { uint8_t pin = vfio_pci_read_config(&vdev->pdev, PCI_INTERRUPT_PIN, 1); - int ret, argsz, retval = 0; - struct vfio_irq_set *irq_set; - int32_t *pfd; Error *err = NULL; + int ret; if (!pin) { return 0; @@ -376,34 +377,12 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp) } #endif - ret = event_notifier_init(&vdev->intx.interrupt, 0); + ret = vfio_register_event_notifier(vdev, VFIO_PCI_INTX_IRQ_INDEX, true, + vfio_intx_interrupt, errp); if (ret) { - error_setg_errno(errp, -ret, "event_notifier_init failed"); return ret; } - argsz = sizeof(*irq_set) + sizeof(*pfd); - - irq_set = g_malloc0(argsz); - irq_set->argsz = argsz; - irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER; - irq_set->index = VFIO_PCI_INTX_IRQ_INDEX; - irq_set->start = 0; - irq_set->count = 1; - pfd = (int32_t *)&irq_set->data; - - *pfd = event_notifier_get_fd(&vdev->intx.interrupt); - qemu_set_fd_handler(*pfd, vfio_intx_interrupt, NULL, vdev); - - ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); - if (ret) { - error_setg_errno(errp, -ret, "failed to setup INTx fd"); - qemu_set_fd_handler(*pfd, NULL, NULL, vdev); - event_notifier_cleanup(&vdev->intx.interrupt); - retval = -errno; - goto cleanup; - } - vfio_intx_enable_kvm(vdev, &err); if (err) { warn_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); @@ -413,10 +392,7 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp) trace_vfio_intx_enable(vdev->vbasedev.name); -cleanup: - g_free(irq_set); - - return retval; + return 0; } static void vfio_intx_disable(VFIOPCIDevice *vdev)