From patchwork Thu Oct 12 22:09:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 13419850 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 C2AB2CDB484 for ; Thu, 12 Oct 2023 22:09:51 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.616075.957881 (Exim 4.92) (envelope-from ) id 1qr3sB-0003xB-AU; Thu, 12 Oct 2023 22:09:39 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 616075.957881; Thu, 12 Oct 2023 22:09:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qr3sB-0003w9-1X; Thu, 12 Oct 2023 22:09:39 +0000 Received: by outflank-mailman (input) for mailman id 616075; Thu, 12 Oct 2023 22:09:37 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qr3s9-00016v-2U for xen-devel@lists.xenproject.org; Thu, 12 Oct 2023 22:09:37 +0000 Received: from mx0b-0039f301.pphosted.com (mx0b-0039f301.pphosted.com [148.163.137.242]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 0694ae73-694c-11ee-98d4-6d05b1d4d9a1; Fri, 13 Oct 2023 00:09:35 +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 39CKqml5008371; Thu, 12 Oct 2023 22:09:19 GMT Received: from eur01-db5-obe.outbound.protection.outlook.com (mail-db5eur01lp2050.outbound.protection.outlook.com [104.47.2.50]) by mx0b-0039f301.pphosted.com (PPS) with ESMTPS id 3tpr7cr8dp-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 12 Oct 2023 22:09:19 +0000 Received: from VI1PR03MB3710.eurprd03.prod.outlook.com (2603:10a6:803:31::18) by DBAPR03MB6376.eurprd03.prod.outlook.com (2603:10a6:10:193::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6863.45; Thu, 12 Oct 2023 22:09:15 +0000 Received: from VI1PR03MB3710.eurprd03.prod.outlook.com ([fe80::f606:63f7:5875:6076]) by VI1PR03MB3710.eurprd03.prod.outlook.com ([fe80::f606:63f7:5875:6076%4]) with mapi id 15.20.6863.043; Thu, 12 Oct 2023 22:09:15 +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: 0694ae73-694c-11ee-98d4-6d05b1d4d9a1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UXISerJCozR/cSwWSnZVN/L+9TPx2Rzj4hJ/5MfQZS9Mtij8+zqQQ/Ulb+5kz9ycTTTC5E+fZ6T/32gd8OvFZgqLUpSmn2rWK/Ag6o/QqUpey52nbcLFVKShUNOTrC9ZNX2pcTBUmz6vXlzaSbl/gpt/208IXIk5aMiFCeqIy0WzgXYhvG9DncC18g2LV9uZIIhX4kXDBpzaVtGr2lgsnQT6PV31cgIax3fmORGBpeeanKD8Wm5uRVg07H3HYmGzjfTp6cX7B10FvmU63REt5veCHJtJUdMrOV5Hl/qetV//w1pOxVmJuGJOqp1f+Hd+ctG0RDX6pStzHC+ITIjrvw== 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=mKMPqeeqAlF1usHbA+jD7EzzRAsY3Lp/fUoH9/LNTBQ=; b=N8a6gYJ7S6sOVVs6/KN0tpX4sFbJYPhiZPgwRURtmP34r6FmFEmY4E3ZVy2OeRnx1qE317DEBTtOSFW9SlWpb8H8na6Ht35IlLnQ+AnUpRV2V8AXkgZzvPTjyHE+LBWdR4g3VV/jNH/0ku5dtg3JlQDGB4B3inA/t1uPWOYB3RUZW8vgROI3X+zbUQF72wrwS0uAPFyM1gECI8z363ip4OwR1ATONba4U1LcPBDSIJYNo5C+EP2Tg5qqawY6g3og6Mp4o4/up3QzshHbCZ7nu4raW4HoL8rOTuLkZFCvhJPWT0Mh8mtb3As6qaw+avbnJ1qaoxL3COCkLWShZqFcVg== 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=mKMPqeeqAlF1usHbA+jD7EzzRAsY3Lp/fUoH9/LNTBQ=; b=B6TKsS8Q7pFbjbb/rI8gVx1rx4CHgfALbJVTqKkmyCXCgjt093jxFpUFQP4I6IZnBShg0yExtaRP+b5nvqEduPuKyOZ4k79HBaZdQEUfHGqfUORQU6YqnQMAvTAswzUZ4Qn9vEzonFAY1BtFcVKyHja0ZTe+Xzo8+xv1sCTnsLnCFxWQWeAgPMTmTr1rfTJ/Fnq49Z8KU/VSUCTduizJCV6KLMZLGeiZU2buEFM2Mz7MS6wG5SpXUSZ7JDKersmEyYsECrGIhDwAcSEOshSm4ddafkU3R9FtP0afUHnERQEx9kvzh3HYvjHwKafUeTXacOUPRvaiMgqXBBJEZHQYJg== 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 v10 02/17] pci: introduce per-domain PCI rwlock Thread-Topic: [PATCH v10 02/17] pci: introduce per-domain PCI rwlock Thread-Index: AQHZ/Vi8UBjbIJy6YEm3bNbn9ygUkA== Date: Thu, 12 Oct 2023 22:09:15 +0000 Message-ID: <20231012220854.2736994-3-volodymyr_babchuk@epam.com> References: <20231012220854.2736994-1-volodymyr_babchuk@epam.com> In-Reply-To: <20231012220854.2736994-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.42.0 x-ms-publictraffictype: Email x-ms-traffictypediagnostic: VI1PR03MB3710:EE_|DBAPR03MB6376:EE_ x-ms-office365-filtering-correlation-id: 66e37b67-8e0a-4acb-e154-08dbcb6fdf86 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: CKd8MLleDT85CFN7KIFs/VrYxxMKc9ul7g+aJs7LWbaof7T1wkfU1RdjoIYbXL1aW5l2UJKHOeL7t0cwPe5CElKu2SwhgYYeUhzKgxWmAF9OxyAwmHVzSL+iMSNUyFGAk3ybZHGg+W/6xNpQVjhuDbc9QmMGPBcLnWuFqvAMNd6CMkM5IZY/qlAv+3WJcRZIxiikhtNvGzYf7jGCaCG17K3dX6p35o7+Itn/oEKyDVg7QvDhEzX/K3fbCZc6mc//CWKJotZyJC3YpQRFgTmT/OXfJKePJiJxEcDt0E5XiUjq0q6PlxEoJvdMhdE+E2F/3l4tVmWJxoBuZyKpyryokeyUA5rFm8CDLUJHPNUDJT8/DbNHsftZIt4m8PhyY6d0yf4eoo+Fgl0GAlrAQfWn6GnvCijcuwF3Ocx+ytaZ4ABJ2D/LseuElzEzn2DzKsbCc60k75LLzqkjAFdYNVCTfQxA4HI0kgh8WvwgvOlZ+FQAtAxQDj1KaUzT0p9jxBUcFw4NsYHJJl/STgflgZrVpfSG9FnyGd8Tw5QR57lJ3g4ceBGzJdj2tman03EXVz13PazVcXT0RH95gHgq3S1cWbaii8AnupLFCZxv8L2v/eZxdkkuwvwzXOE+jzlj+BFI 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)(376002)(39860400002)(396003)(346002)(366004)(136003)(230922051799003)(451199024)(64100799003)(186009)(1800799009)(2616005)(71200400001)(26005)(6512007)(6506007)(55236004)(36756003)(86362001)(38100700002)(38070700005)(122000001)(1076003)(83380400001)(7416002)(91956017)(41300700001)(64756008)(316002)(76116006)(54906003)(66946007)(66446008)(6916009)(66556008)(66476007)(2906002)(8936002)(5660300002)(8676002)(4326008)(6486002)(478600001);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?hD//dhkLidD2lqFsjZCIbiDE05yv?= =?utf-8?q?C11IO+Uzo44MHgxhssVZ/3Bq1Ap038yHObW6M9pYnzelOCCvcvwRqc/mmWEkRxy+i?= =?utf-8?q?kPXALI6syRGiRjxqRboUM+ztrG76P/EEzqAC3J7R2RtPYBURazmhV1iGjQxX9z4MR?= =?utf-8?q?+Z3NLEThDdHdOWUjOC/FKM0Vlny2+adUmm+kQY/HLb6X6GOgdUvd9TGJVwZJjE0ZO?= =?utf-8?q?dG2DbkPxBfj1h6pfREqo4MGXKTJYhqxIWTgdQziz4aUSpqeLCN27/rLdgmZWB2Bd8?= =?utf-8?q?NxtyWYXff3uu2e2mRQf+MkeJ882OrPmh+tgH/KDNz06oShuq8WgjR8t+RnQNZtJme?= =?utf-8?q?oBn43+0Yxp823VzCH3qzF8hyVJo6w5FB7D6DfvwXMDJzHMj2c/Bb0F9EbHPnxKs83?= =?utf-8?q?tJ5HHxva9Oix0Y7pzbqaRFgUB2Iu9SN/jgJAdmdgt4YnoYWB9zWIJhz2l/bFiO0/W?= =?utf-8?q?/bT1T5qTCtEFeKx9mSrp+a/8Y6YtMo6blsWFk1jhgZ94sr9eqET4thWcURF1bzmOR?= =?utf-8?q?VJiI7CLA1q7dc0sSmEyU++/udm6SvivtJGOlYRMmBEbUOzLd0jDKF/o8bjSzWUe2Z?= =?utf-8?q?Fvex8YWkQs/Ov3t+znuocRslJdn+VZfWlY+0yqiH52zKKYWQHinbZf97EM6l1aCLf?= =?utf-8?q?yhS3xQhssRt4rsD0IrlsDn8/n9t/LxgdSc7Iww5ZEBZzBOpISNPlribt2CdtupFFB?= =?utf-8?q?6jwQrd7imeAEnzga/EJmdcugn6EpZAf/SjeYq3wB6Xu6DSt/aIiSn4x0wjrBTpXeo?= =?utf-8?q?xtsS2O6fqUCFS26aDGMHkJIWHIy1V4SXDYuJdM4FSgTc5ogDZ2vmMuLY/6TQ6V5Zd?= =?utf-8?q?61EYGCNgHGkEVONwXO3cRaUDo0+qWnLReFnJa1n7QogaatXz+9YQejulmFL04tvFb?= =?utf-8?q?m9syeUTJQ2A0EHpR5fx7XdowisGxUo8XrMmxKK0bNpYLdhzhQB6OHjuIpjReb2Ppn?= =?utf-8?q?osQi5qdwKZtt0qZwWZQ3UZjB5MtoL/lmppwUxHFFMFt08oOC6D2xJIjg+d8fUowmT?= =?utf-8?q?YiY1WIWkdmNhIFsHyMXbkcq2VlrgPUvy+32xo2RsO1/W/pkPQ2amZaXbnLu+ijjgZ?= =?utf-8?q?zHq+jm/SLNi0es6cIA8ZLwTmmgCnyo7XM8YZ5N2QiLpNyTpzIYuJ4/Qm2SvOMAeHw?= =?utf-8?q?rbJ7rHiEHccen6iw2t7/4xOIRdu4qXzpUUmNQC6NmlzozfY2vk4IDkq8Fj9TLxOUX?= =?utf-8?q?upVS+ztt201O1QkvC3kK6bi4NAMRk5lgVMlc1jvhBCpC/QOE3zqUPyFgUGPNnz4QT?= =?utf-8?q?G7SIMaCWZg3qqn6R/yo1rHz3c2nXyXcg/DWKPqpDuJBGXSOBqMaol9Wg7l6+fTgYP?= =?utf-8?q?du6rIl0DFRgGzgXm8q7LU9D97PlZFB45DwsDVOInDQ4VaG/pmH6WzKyWqUNPUDJYe?= =?utf-8?q?aw+91Ynl5/VNYTM5zEP2xKWlRjsLZdT9YGmNhJo+WcI5XAKtByu+riiPFLZtbC/+z?= =?utf-8?q?+oDc5e1SJMvketjuLFMTOCkUKdqPpcxLoC/uF8MDU1WqsPbUvvnaqkbKlGM/pPoZl?= =?utf-8?q?YIZzo0NnJpBN5KvIAGvtu3/G8DN5HKfKgA=3D=3D?= Content-ID: 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: 66e37b67-8e0a-4acb-e154-08dbcb6fdf86 X-MS-Exchange-CrossTenant-originalarrivaltime: 12 Oct 2023 22:09:15.1897 (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: ax9t6PRfM1NWgQR+g8sQK1eHftN+s5NKCNACQY0wFqzIxx3j4UVDorCtKLU052VCFbJgccOln4irUyZAOvTlyC4xNZlu2eSlfF+/ovVBUuQ= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBAPR03MB6376 X-Proofpoint-GUID: mEd38r7guj_3gAoyQaBuv4MEc-YoAHxc X-Proofpoint-ORIG-GUID: mEd38r7guj_3gAoyQaBuv4MEc-YoAHxc X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-10-12_14,2023-10-12_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 spamscore=0 mlxscore=0 lowpriorityscore=0 suspectscore=0 priorityscore=1501 impostorscore=0 clxscore=1015 malwarescore=0 bulkscore=0 mlxlogscore=999 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2309180000 definitions=main-2310120186 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. 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. Suggested-by: Roger Pau Monné Suggested-by: Jan Beulich Signed-off-by: Volodymyr Babchuk Reviewed-by: Roger Pau Monné --- Changes in v10: - pdev->domain is assigned after removing from source domain but before adding to target domain in reassign_device() functions. 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 8f9ab01c0c..785c69e48b 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 836c24b02e..36a617bed4 100644 --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -476,8 +476,15 @@ 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); + pdev->domain = target; + + write_lock(&target->pci_lock); + list_add(&pdev->domain_list, &target->pdev_list); + write_unlock(&target->pci_lock); } /* diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c index 04d00c7c37..b8ad4fa07c 100644 --- a/xen/drivers/passthrough/pci.c +++ b/xen/drivers/passthrough/pci.c @@ -453,7 +453,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, @@ -746,7 +748,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 @@ -756,7 +760,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; } @@ -764,7 +770,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; } @@ -814,7 +822,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; @@ -885,26 +897,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 @@ -1124,7 +1171,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 d34c98d9c7..cdb1225578 100644 --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -2806,8 +2806,15 @@ 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); + pdev->domain = target; + + write_lock(&target->pci_lock); + list_add(&pdev->domain_list, &target->pdev_list); + write_unlock(&target->pci_lock); } if ( !has_arch_pdevs(source) ) diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 3609ef88c4..57391e74b6 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -461,6 +461,7 @@ struct domain #ifdef CONFIG_HAS_PCI struct list_head pdev_list; + rwlock_t pci_lock; #endif #ifdef CONFIG_HAS_PASSTHROUGH