From patchwork Tue Aug 29 23:19:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 13369802 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CB7E9C83F19 for ; Tue, 29 Aug 2023 23:20:29 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.592522.925357 (Exim 4.92) (envelope-from ) id 1qb80H-0001nM-U7; Tue, 29 Aug 2023 23:20:09 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 592522.925357; Tue, 29 Aug 2023 23:20:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qb80H-0001ix-9z; Tue, 29 Aug 2023 23:20:09 +0000 Received: by outflank-mailman (input) for mailman id 592522; Tue, 29 Aug 2023 23:20:07 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qb80F-0006kQ-7C for xen-devel@lists.xenproject.org; Tue, 29 Aug 2023 23:20:07 +0000 Received: from mx0b-0039f301.pphosted.com (mx0b-0039f301.pphosted.com [148.163.137.242]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 95a0de5e-46c2-11ee-9b0d-b553b5be7939; Wed, 30 Aug 2023 01:20:05 +0200 (CEST) Received: from pps.filterd (m0174682.ppops.net [127.0.0.1]) by mx0b-0039f301.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 37TFMk9h021983; Tue, 29 Aug 2023 23:19:49 GMT Received: from eur05-db8-obe.outbound.protection.outlook.com (mail-db8eur05lp2108.outbound.protection.outlook.com [104.47.17.108]) by mx0b-0039f301.pphosted.com (PPS) with ESMTPS id 3ssdvpbm4f-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 29 Aug 2023 23:19:49 +0000 Received: from VI1PR03MB3710.eurprd03.prod.outlook.com (2603:10a6:803:31::18) by AM7PR03MB6371.eurprd03.prod.outlook.com (2603:10a6:20b:1b8::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6699.35; Tue, 29 Aug 2023 23:19:43 +0000 Received: from VI1PR03MB3710.eurprd03.prod.outlook.com ([fe80::9d42:8444:f00d:7895]) by VI1PR03MB3710.eurprd03.prod.outlook.com ([fe80::9d42:8444:f00d:7895%4]) with mapi id 15.20.6699.034; Tue, 29 Aug 2023 23:19:43 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 95a0de5e-46c2-11ee-9b0d-b553b5be7939 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SaCwnb7DqXkGDSkPtJwPgLIRI55FRZy8DDj6du446NbjqSLu8ic5YDKlN7CJIEs1mk5sN4ZAMAQENSVyilwOnE4xE6cxIy0iO/QjcPpYsilk4i0kSfFfH32xA57F+DGCRxS3AZuFJFALbVuEXh6nx5D/KeU6lB0zzNptdPBQuJeMLyC5RiQJhbsPsu5SNkDCsjJp3jaWwU1CkozjYP3RnkxMBrmM3Nv2PD/Bapu6hyQ12KHQzBDIUFvdlthlY3DfqHBte1Rof+RYFInGNt65zU8Nce/WaQDBS3jxrTvmlpzImqD217wuVVnmCmF8YGi1CqPs+3RapunW80Gs+O3Sug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=d2+SjNS5ON5irHRUHXgAVHRcgI+kAagi6t5CcHlMjAs=; b=fztmfChJYRwAU++IwyQPmcllo5Gj3MTwECUuGjkZHv8UVBl6VvhvfQt/zJMuBzS75VDymuLbA8xrnGAccg/zrp4swySmYBTfLFVwU4adSKX333kYTya8dmwmJNrIjXFcLIVnf7x63E2NW6RpTXEn9lus3l80+hSzDUwx8xx6Pet4Jwpr5jrz64C6w9V+S0SMMrkIPyX5YhTB7ml2T+l4k+ltQmawwBs+iOnXpU2ezAiK5iPBrDhL6mf1tSHvBEKoGzul/+UqVRU6VJE18ce39L2o03W22rXCshx9U3YElCcdNrBWyKiIWPybEma597O1uzGePwvahP/9dHDoYsMtSg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=epam.com; dmarc=pass action=none header.from=epam.com; dkim=pass header.d=epam.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=epam.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=d2+SjNS5ON5irHRUHXgAVHRcgI+kAagi6t5CcHlMjAs=; b=igPIi1XHHt6eETC7O7H+lDkTeTDGPPEOlu+X1/ZEGFiBs6G+p3D/KkuEBZodx0RAh0GSaj7ErnqDT1z0UN3bkTkceJWpKdMynCnCM3jQTC/wFeyQuDPjyipaMmWlLB4vs5jjQBSmxmLcVcdgopjqvFdGaYKJ30Z3QffBjqH3/wwSOZEE+0misjeAc7gcryVY53TeUTO8qdq1Z81aZ5uLo3I78ysX14yS++HvKCWkmxLJfYwB0heL5bXB1FeUqjWKo1RlgdWk8WL3aLyDUFeScXPbHPaVLpufj8aUmZMyAJvWnaIa0Z1E4SdoTsFfABLmlv1SI3iZ9rPsCk3We9oCzw== From: Volodymyr Babchuk To: "xen-devel@lists.xenproject.org" CC: Stewart Hildebrand , Volodymyr Babchuk , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu , Paul Durrant , =?utf-8?q?Roger_Pau_Monn?= =?utf-8?q?=C3=A9?= , Kevin Tian Subject: [PATCH v9 01/16] pci: introduce per-domain PCI rwlock Thread-Topic: [PATCH v9 01/16] pci: introduce per-domain PCI rwlock Thread-Index: AQHZ2s9KWF3JKCAv4U2Fm0C+oJYn+A== Date: Tue, 29 Aug 2023 23:19:42 +0000 Message-ID: <20230829231912.4091958-2-volodymyr_babchuk@epam.com> References: <20230829231912.4091958-1-volodymyr_babchuk@epam.com> In-Reply-To: <20230829231912.4091958-1-volodymyr_babchuk@epam.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.41.0 x-ms-publictraffictype: Email x-ms-traffictypediagnostic: VI1PR03MB3710:EE_|AM7PR03MB6371:EE_ x-ms-office365-filtering-correlation-id: 13aee429-ce8d-4786-1bb8-08dba8e66d44 x-ld-processed: b41b72d0-4e9f-4c26-8a69-f949f367c91d,ExtAddr x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: EQYMY+iTNTd9gXBaIqovB0YzB/J8TZeQjhbvIbE4PHamUP4oOWwG1VA759p4DBucu6lfsVsnrsc2HxWCZO1k74r5Yod9wsbHMkZYjilkNjauiDvuUd8tHuaspsaSr64Yd0K3c2P3v1fhpkxcmcdqnFHJWbZRae6QT1sHiFUXziWIqexKCizLWqyxQdqvlEw/TlIfbLe7DpmoaI4Y8zffrK1IgqgLGqbG8ZcT1ORRWevqwoE20cervkRzLORc4YwpPKkeVLhrtVSuI+ndBavL3Ukeb1YQK8nVwi3IKbw21royAWYqMS6KkEJIBFcSFj0zTdcYikeCycmmdvomvU3JKG3tnrDkxv8saIjftKC1ok9mjMlI4vc7+GkVUScJ7mifcFYNdUNbqigexLy4BZBDYnnq9Zguf8RDkhiofxUrfCmC29eYEXXMmIdxnfXt1wioqHNNh32EPhaX/QmBIouaukXLPcVCvet3kolggfFbtx+MR0XuxWz4svHDf61u3CGL0hsPC39QPHxZgF0z3iWI2pGr8C9dvy+rS4Hx3NndFnySWj1EZfCcR5Smlsl3VpJPfD/5fHSJJg88+SaUfehEMwQnnKzvcu4OTgdsbosSXOncPqTsA2x5jih46QYZt8oe x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VI1PR03MB3710.eurprd03.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(346002)(39860400002)(376002)(396003)(136003)(366004)(451199024)(1800799009)(186009)(71200400001)(6512007)(6506007)(6486002)(478600001)(55236004)(76116006)(1076003)(2616005)(26005)(2906002)(6916009)(64756008)(7416002)(66446008)(54906003)(316002)(5660300002)(66476007)(66556008)(91956017)(66946007)(41300700001)(83380400001)(8936002)(4326008)(8676002)(36756003)(38070700005)(38100700002)(86362001)(122000001);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?1vI8rJ5J4wNIAagk2NXcK6FHbJdq?= =?utf-8?q?TMY1Tj/bHM4UyO7a0XQHFqK3qjK7ClxC11FXS9mGDrIdVFqolIkn6k0W4xaVb64+z?= =?utf-8?q?zN2J7+akQW7QjM0mW/Dzra9ud1zrVq9vWRHJc8fW5H+qLYqYLAgbPYlsQryl2cxgE?= =?utf-8?q?1rUeKkPOdM9jEaWM1dIRuhAEaEGJUCzmgxemQlDSL47CcC7YjtabAhEjjqFVpMAUf?= =?utf-8?q?vqZ63ya61DPeWUaT8tEdN88GCQi8UjEDkeo6I62AywF2YUsIHBC4POVewsSEKl+SZ?= =?utf-8?q?EaFW/EY0Nui2LcrOd5lqtl4eLj80C8W6E7II8bS5TJ3vTAO+aFq1qK3qPNZvwpvC5?= =?utf-8?q?L31h8Xav/LdhcJaO35sQfZShKGM8x6J443zGR4IgYlXlsbFaz4DhN2ZsX0M851nYt?= =?utf-8?q?Ax2FYbN0OaxIe/CDwuGe9I1sRvNU/NxtZCXP47xTNUnBfBHFBFcqcjD2z2393o468?= =?utf-8?q?ajvUPW16ZFrIb4JMEJAXmpl5UADMVYlxz9ZN7k8/MDsgu+RPv9LJvqHi5TPTTuBkJ?= =?utf-8?q?zvzpDVxmVT0F5O3cKMOG+/+Rk7Cb3QiKSITlA/AJ0rRsyNaiqKEu1Fh9PWV+RqsB0?= =?utf-8?q?NCuFxJEgf1+Gf/PfpbFUfw79o8aLBnuIpy+aFzx1kgxXyI7FoiZbvfLcHRG8oybRf?= =?utf-8?q?9cm5XixNtM0+dg/yTCBKBi9+bU85QRpbdsfyWpdcExYQqy2g8mRF92mv8+TDWed8f?= =?utf-8?q?KHTCQrAiaqhYa5JWRZ/A3R9nv2sMIHt0phmk+7zKTjkk8++vSrvh69p4kYjhNI9Eb?= =?utf-8?q?Dl0XikExebFFNOCLgoyzzy5A79pfIyzk3nhOHkBN1ggeWlOe4FLSv30zCev+I9CVU?= =?utf-8?q?/VYzBC4+m8w9iN87KIzVV1YZX1Rn7k2XCs+t9EgUV5YsBlWN2tqICvg3xGIrYs8YB?= =?utf-8?q?llIrGLbRAnhh9asO3nmQEGk0BT7ITds14RbLIjOyYw8n36oo8IS07XaKy5JK++Q0n?= =?utf-8?q?MdDhMB6F83Ex5IrRDgN7nWCl9Bgxd8skuIj67fpXRUKNmeglI4m7kEOccgxd+m1Gf?= =?utf-8?q?+fqH2yuqRROI6XNb2a5k2YR9X7ZOiPrjWcPlNNJME/j2ahZeYXJaWho4ObGSBgAUV?= =?utf-8?q?v1QmncwlSSJgwdFbn2QyrIvp/TfwHC0FCTLCUZfqzgNvKwxhe4BTnGiaY+3CrlZ3p?= =?utf-8?q?x/XRGpKUOw8lo1mbrg8L8MZhZXFSKQgWVwvUa0+lhOhLnOMQsHVTWjvYBDaw59BLq?= =?utf-8?q?5pfNaAPsCcktqzt5kOwsYYaW9su5g+JsogFwx/EjnOQXuZkzk3P1NKJDVInfuTr8Z?= =?utf-8?q?YsGRmKUHXPPRdyx4yIDtQJMNLy3WVkixRz7UDPC3S6is3V35tdM4NgtUA6Tt0SX1C?= =?utf-8?q?2wBeErL32W0pGXs25W8nRRXig8K/iXWm1vdwwXTN43bhLRxAEK1TBqxxfxHeQ/EVL?= =?utf-8?q?o0JmhfRy4ijySerZpI5G7ZthWiNI0Iyp0kXiLsoMRTKCHkxc8jCRXso0m9CEvIix/?= =?utf-8?q?39Nj2PpI9lQWw277HrJ/PsPBkwOpPTPe5CmHnugmMXVnEqZMrxBonH1UNdH8/zOIB?= =?utf-8?q?ZCU8RiIGuLtngKFinUWwSxkjfJPiHvYbmw=3D=3D?= Content-ID: <77E6C311CA0A814AA5B5E49E976390A3@eurprd03.prod.outlook.com> MIME-Version: 1.0 X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: VI1PR03MB3710.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 13aee429-ce8d-4786-1bb8-08dba8e66d44 X-MS-Exchange-CrossTenant-originalarrivaltime: 29 Aug 2023 23:19:42.1113 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: 1sr2BEAmCzL7Q2pEEbokTFEdlPRV+3ZkNdmeMwKh5gUgQnaXZg7VUVqWlJQLN6jJbBWksvWkrzVgGYhMVAk6kJAvyej4oXDy4FITDnorjII= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR03MB6371 X-Proofpoint-ORIG-GUID: ycFgeegD8pR__ThYRAgrZ-974DDffhRk X-Proofpoint-GUID: ycFgeegD8pR__ThYRAgrZ-974DDffhRk X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-08-29_16,2023-08-29_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 bulkscore=0 mlxscore=0 adultscore=0 priorityscore=1501 impostorscore=0 lowpriorityscore=0 phishscore=0 spamscore=0 suspectscore=0 mlxlogscore=999 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2308100000 definitions=main-2308290202 Add per-domain d->pci_lock that protects access to d->pdev_list. Purpose of this lock is to give guarantees to VPCI code that underlying pdev will not disappear under feet. This is a rw-lock, but this patch adds only write_lock()s. There will be read_lock() users in the next patches. This lock should be taken in write mode every time d->pdev_list is altered. This covers both accesses to d->pdev_list and accesses to pdev->domain_list fields. All write accesses also should be protected by pcidevs_lock() as well. Idea is that any user that wants read access to the list or to the devices stored in the list should use either this new d->pci_lock or old pcidevs_lock(). Usage of any of this two locks will ensure only that pdev of interest will not disappear from under feet and that the pdev still will be assigned to the same domain. Of course, any new users should use pcidevs_lock() when it is appropriate (e.g. when accessing any other state that is protected by the said lock). In case both the newly introduced per-domain rwlock and the pcidevs lock is taken, the later must be acquired first. Any write access to pdev->domain_list should be protected by both pcidevs_lock() and d->pci_lock in the write mode. Suggested-by: Roger Pau Monné Suggested-by: Jan Beulich Signed-off-by: Volodymyr Babchuk --- Changes in v9: - returned back "pdev->domain = target;" in AMD IOMMU code - used "source" instead of pdev->domain in IOMMU functions - added comment about lock ordering in the commit message - reduced locked regions - minor changes non-functional changes in various places Changes in v8: - New patch Changes in v8 vs RFC: - Removed all read_locks after discussion with Roger in #xendevel - pci_release_devices() now returns the first error code - extended commit message - added missing lock in pci_remove_device() - extended locked region in pci_add_device() to protect list_del() calls --- xen/common/domain.c | 1 + xen/drivers/passthrough/amd/pci_amd_iommu.c | 9 ++- xen/drivers/passthrough/pci.c | 71 +++++++++++++++++---- xen/drivers/passthrough/vtd/iommu.c | 9 ++- xen/include/xen/sched.h | 1 + 5 files changed, 78 insertions(+), 13 deletions(-) diff --git a/xen/common/domain.c b/xen/common/domain.c index 304aa04fa6..9b04a20160 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -651,6 +651,7 @@ struct domain *domain_create(domid_t domid, #ifdef CONFIG_HAS_PCI INIT_LIST_HEAD(&d->pdev_list); + rwlock_init(&d->pci_lock); #endif /* All error paths can depend on the above setup. */ diff --git a/xen/drivers/passthrough/amd/pci_amd_iommu.c b/xen/drivers/passthrough/amd/pci_amd_iommu.c index bea70db4b7..d219bd9453 100644 --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -476,7 +476,14 @@ static int cf_check reassign_device( if ( devfn == pdev->devfn && pdev->domain != target ) { - list_move(&pdev->domain_list, &target->pdev_list); + write_lock(&source->pci_lock); + list_del(&pdev->domain_list); + write_unlock(&source->pci_lock); + + write_lock(&target->pci_lock); + list_add(&pdev->domain_list, &target->pdev_list); + write_unlock(&target->pci_lock); + pdev->domain = target; } diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c index 33452791a8..79ca928672 100644 --- a/xen/drivers/passthrough/pci.c +++ b/xen/drivers/passthrough/pci.c @@ -454,7 +454,9 @@ static void __init _pci_hide_device(struct pci_dev *pdev) if ( pdev->domain ) return; pdev->domain = dom_xen; + write_lock(&dom_xen->pci_lock); list_add(&pdev->domain_list, &dom_xen->pdev_list); + write_unlock(&dom_xen->pci_lock); } int __init pci_hide_device(unsigned int seg, unsigned int bus, @@ -748,7 +750,9 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, if ( !pdev->domain ) { pdev->domain = hardware_domain; + write_lock(&hardware_domain->pci_lock); list_add(&pdev->domain_list, &hardware_domain->pdev_list); + write_unlock(&hardware_domain->pci_lock); /* * For devices not discovered by Xen during boot, add vPCI handlers @@ -758,7 +762,9 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, if ( ret ) { printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret); + write_lock(&hardware_domain->pci_lock); list_del(&pdev->domain_list); + write_unlock(&hardware_domain->pci_lock); pdev->domain = NULL; goto out; } @@ -766,7 +772,9 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, if ( ret ) { vpci_remove_device(pdev); + write_lock(&hardware_domain->pci_lock); list_del(&pdev->domain_list); + write_unlock(&hardware_domain->pci_lock); pdev->domain = NULL; goto out; } @@ -816,7 +824,11 @@ int pci_remove_device(u16 seg, u8 bus, u8 devfn) pci_cleanup_msi(pdev); ret = iommu_remove_device(pdev); if ( pdev->domain ) + { + write_lock(&pdev->domain->pci_lock); list_del(&pdev->domain_list); + write_unlock(&pdev->domain->pci_lock); + } printk(XENLOG_DEBUG "PCI remove device %pp\n", &pdev->sbdf); free_pdev(pseg, pdev); break; @@ -887,26 +899,61 @@ static int deassign_device(struct domain *d, uint16_t seg, uint8_t bus, int pci_release_devices(struct domain *d) { - struct pci_dev *pdev, *tmp; - u8 bus, devfn; - int ret; + int combined_ret; + LIST_HEAD(failed_pdevs); pcidevs_lock(); - ret = arch_pci_clean_pirqs(d); - if ( ret ) + + combined_ret = arch_pci_clean_pirqs(d); + if ( combined_ret ) { pcidevs_unlock(); - return ret; + return combined_ret; } - list_for_each_entry_safe ( pdev, tmp, &d->pdev_list, domain_list ) + + write_lock(&d->pci_lock); + + while ( !list_empty(&d->pdev_list) ) { - bus = pdev->bus; - devfn = pdev->devfn; - ret = deassign_device(d, pdev->seg, bus, devfn) ?: ret; + struct pci_dev *pdev = list_first_entry(&d->pdev_list, + struct pci_dev, + domain_list); + uint16_t seg = pdev->seg; + uint8_t bus = pdev->bus; + uint8_t devfn = pdev->devfn; + int ret; + + write_unlock(&d->pci_lock); + ret = deassign_device(d, seg, bus, devfn); + write_lock(&d->pci_lock); + if ( ret ) + { + const struct pci_dev *tmp; + + /* + * We need to check if deassign_device() left our pdev in + * domain's list. As we dropped the lock, we can't be sure + * that list wasn't permutated in some random way, so we + * need to traverse the whole list. + */ + for_each_pdev ( d, tmp ) + { + if ( tmp == pdev ) + { + list_move_tail(&pdev->domain_list, &failed_pdevs); + break; + } + } + + combined_ret = combined_ret ?: ret; + } } + + list_splice(&failed_pdevs, &d->pdev_list); + write_unlock(&d->pci_lock); pcidevs_unlock(); - return ret; + return combined_ret; } #define PCI_CLASS_BRIDGE_HOST 0x0600 @@ -1125,7 +1172,9 @@ static int __hwdom_init cf_check _setup_hwdom_pci_devices( if ( !pdev->domain ) { pdev->domain = ctxt->d; + write_lock(&ctxt->d->pci_lock); list_add(&pdev->domain_list, &ctxt->d->pdev_list); + write_unlock(&ctxt->d->pci_lock); setup_one_hwdom_device(ctxt, pdev); } else if ( pdev->domain == dom_xen ) diff --git a/xen/drivers/passthrough/vtd/iommu.c b/xen/drivers/passthrough/vtd/iommu.c index 0e3062c820..3228900c97 100644 --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -2806,7 +2806,14 @@ static int cf_check reassign_device_ownership( if ( devfn == pdev->devfn && pdev->domain != target ) { - list_move(&pdev->domain_list, &target->pdev_list); + write_lock(&source->pci_lock); + list_del(&pdev->domain_list); + write_unlock(&source->pci_lock); + + write_lock(&target->pci_lock); + list_add(&pdev->domain_list, &target->pdev_list); + write_unlock(&target->pci_lock); + pdev->domain = target; } diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index b4f43cd410..535a81fe90 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -460,6 +460,7 @@ struct domain #ifdef CONFIG_HAS_PCI struct list_head pdev_list; + rwlock_t pci_lock; #endif #ifdef CONFIG_HAS_PASSTHROUGH