From patchwork Thu Jul 2 19:51:19 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Konrad Rzeszutek Wilk X-Patchwork-Id: 490791 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)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 8EDAA140295 for ; Fri, 3 Jul 2015 05:52:23 +1000 (AEST) Received: from localhost ([::1]:38329 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAkWz-000425-Mo for incoming@patchwork.ozlabs.org; Thu, 02 Jul 2015 15:52:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43996) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAkWE-0002kK-3X for qemu-devel@nongnu.org; Thu, 02 Jul 2015 15:51:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZAkW8-0000HZ-Vi for qemu-devel@nongnu.org; Thu, 02 Jul 2015 15:51:34 -0400 Received: from aserp1040.oracle.com ([141.146.126.69]:29214) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZAkW8-0000H4-PS for qemu-devel@nongnu.org; Thu, 02 Jul 2015 15:51:28 -0400 Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id t62JpOHM022307 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 2 Jul 2015 19:51:24 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0022.oracle.com (8.13.8/8.13.8) with ESMTP id t62JpO0Q008261 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Thu, 2 Jul 2015 19:51:24 GMT Received: from abhmp0001.oracle.com (abhmp0001.oracle.com [141.146.116.7]) by aserv0121.oracle.com (8.13.8/8.13.8) with ESMTP id t62JpOBP013527; Thu, 2 Jul 2015 19:51:24 GMT Received: from l.oracle.com (/10.137.176.158) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 02 Jul 2015 12:51:24 -0700 Received: by l.oracle.com (Postfix, from userid 1000) id A98A56A02A0; Thu, 2 Jul 2015 15:51:22 -0400 (EDT) From: Konrad Rzeszutek Wilk To: stefano.stabellini@eu.citrix.com, xen-devel@lists.xenproject.org, qemu-devel@nongnu.org, JBeulich@suse.com Date: Thu, 2 Jul 2015 15:51:19 -0400 Message-Id: <1435866681-18468-9-git-send-email-konrad.wilk@oracle.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1435866681-18468-1-git-send-email-konrad.wilk@oracle.com> References: <1435866681-18468-1-git-send-email-konrad.wilk@oracle.com> X-Source-IP: aserv0022.oracle.com [141.146.126.234] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 141.146.126.69 Cc: Konrad Rzeszutek Wilk Subject: [Qemu-devel] [PATCH v1 08/10] xen/pt: Make xen_pt_unregister_device idempotent 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 To deal with xen_host_pci_[set|get]_ functions returning error values and clearing ourselves in the init function we should make the .exit (xen_pt_unregister_device) function be idempotent in case the generic code starts calling .exit (or for fun does it before calling .init!). Signed-off-by: Konrad Rzeszutek Wilk Reviewed-by: Stefano Stabellini --- hw/xen/xen-host-pci-device.c | 5 +++++ hw/xen/xen-host-pci-device.h | 1 + hw/xen/xen_pt.c | 22 ++++++++++++++++------ hw/xen/xen_pt.h | 2 ++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/hw/xen/xen-host-pci-device.c b/hw/xen/xen-host-pci-device.c index 743b37b..5b20570 100644 --- a/hw/xen/xen-host-pci-device.c +++ b/hw/xen/xen-host-pci-device.c @@ -387,6 +387,11 @@ error: return rc; } +bool xen_host_pci_device_closed(XenHostPCIDevice *d) +{ + return d->config_fd == -1; +} + void xen_host_pci_device_put(XenHostPCIDevice *d) { if (d->config_fd >= 0) { diff --git a/hw/xen/xen-host-pci-device.h b/hw/xen/xen-host-pci-device.h index c2486f0..16f4805 100644 --- a/hw/xen/xen-host-pci-device.h +++ b/hw/xen/xen-host-pci-device.h @@ -38,6 +38,7 @@ typedef struct XenHostPCIDevice { int xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain, uint8_t bus, uint8_t dev, uint8_t func); void xen_host_pci_device_put(XenHostPCIDevice *pci_dev); +bool xen_host_pci_device_closed(XenHostPCIDevice *d); int xen_host_pci_get_byte(XenHostPCIDevice *d, int pos, uint8_t *p); int xen_host_pci_get_word(XenHostPCIDevice *d, int pos, uint16_t *p); diff --git a/hw/xen/xen_pt.c b/hw/xen/xen_pt.c index 7575311..a094f7c 100644 --- a/hw/xen/xen_pt.c +++ b/hw/xen/xen_pt.c @@ -807,6 +807,7 @@ out: memory_listener_register(&s->memory_listener, &s->dev.bus_master_as); memory_listener_register(&s->io_listener, &address_space_io); + s->listener_set = true; XEN_PT_LOG(d, "Real physical device %02x:%02x.%d registered successfully!\n", s->hostaddr.bus, s->hostaddr.slot, s->hostaddr.function); @@ -818,10 +819,13 @@ static void xen_pt_unregister_device(PCIDevice *d) { XenPCIPassthroughState *s = XEN_PT_DEVICE(d); uint8_t machine_irq = s->machine_irq; - uint8_t intx = xen_pt_pci_intx(s); + uint8_t intx; int rc; - if (machine_irq) { + /* Note that if xen_host_pci_device_put had closed config_fd, then + * intx value becomes 0xff. */ + intx = xen_pt_pci_intx(s); + if (machine_irq && !xen_host_pci_device_closed(&s->real_device)) { rc = xc_domain_unbind_pt_irq(xen_xc, xen_domid, machine_irq, PT_IRQ_TYPE_PCI, pci_bus_num(d->bus), @@ -836,6 +840,7 @@ static void xen_pt_unregister_device(PCIDevice *d) } } + /* N.B. xen_pt_config_delete takes care of freeing them. */ if (s->msi) { xen_pt_msi_disable(s); } @@ -855,15 +860,20 @@ static void xen_pt_unregister_device(PCIDevice *d) machine_irq, errno); } } + s->machine_irq = 0; } /* delete all emulated config registers */ xen_pt_config_delete(s); - memory_listener_unregister(&s->memory_listener); - memory_listener_unregister(&s->io_listener); - - xen_host_pci_device_put(&s->real_device); + if (s->listener_set) { + memory_listener_unregister(&s->memory_listener); + memory_listener_unregister(&s->io_listener); + s->listener_set = false; + } + if (!xen_host_pci_device_closed(&s->real_device)) { + xen_host_pci_device_put(&s->real_device); + } } static Property xen_pci_passthrough_properties[] = { diff --git a/hw/xen/xen_pt.h b/hw/xen/xen_pt.h index 586d055..2b58521 100644 --- a/hw/xen/xen_pt.h +++ b/hw/xen/xen_pt.h @@ -221,6 +221,7 @@ struct XenPCIPassthroughState { MemoryListener memory_listener; MemoryListener io_listener; + bool listener_set; }; int xen_pt_config_init(XenPCIPassthroughState *s); @@ -286,6 +287,7 @@ static inline uint8_t xen_pt_pci_intx(XenPCIPassthroughState *s) " value=%i, acceptable range is 1 - 4\n", r_val); r_val = 0; } else { + /* Note that if s.real_device.config_fd is closed we make 0xff. */ r_val -= 1; }