From patchwork Sat Dec 2 01:27:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 13476720 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 B2D1EC4167B for ; Sat, 2 Dec 2023 01:27:48 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.645844.1008378 (Exim 4.92) (envelope-from ) id 1r9En9-00069F-9S; Sat, 02 Dec 2023 01:27:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 645844.1008378; Sat, 02 Dec 2023 01:27:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1r9En8-00067B-BM; Sat, 02 Dec 2023 01:27:34 +0000 Received: by outflank-mailman (input) for mailman id 645844; Sat, 02 Dec 2023 01:27:30 +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 1r9En3-000272-KR for xen-devel@lists.xenproject.org; Sat, 02 Dec 2023 01:27:29 +0000 Received: from mx0a-0039f301.pphosted.com (mx0a-0039f301.pphosted.com [148.163.133.242]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id f28e4afa-90b1-11ee-98e5-6d05b1d4d9a1; Sat, 02 Dec 2023 02:27:25 +0100 (CET) Received: from pps.filterd (m0174678.ppops.net [127.0.0.1]) by mx0a-0039f301.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3B1NLGuv008549; Sat, 2 Dec 2023 01:27:10 GMT Received: from eur03-dba-obe.outbound.protection.outlook.com (mail-dbaeur03lp2169.outbound.protection.outlook.com [104.47.51.169]) by mx0a-0039f301.pphosted.com (PPS) with ESMTPS id 3uqfjgj05q-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 02 Dec 2023 01:27:09 +0000 Received: from VI1PR03MB3710.eurprd03.prod.outlook.com (2603:10a6:803:31::18) by DU0PR03MB8599.eurprd03.prod.outlook.com (2603:10a6:10:3e6::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7046.27; Sat, 2 Dec 2023 01:27:05 +0000 Received: from VI1PR03MB3710.eurprd03.prod.outlook.com ([fe80::8e03:368:1fd7:1822]) by VI1PR03MB3710.eurprd03.prod.outlook.com ([fe80::8e03:368:1fd7:1822%6]) with mapi id 15.20.7046.027; Sat, 2 Dec 2023 01:27:04 +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: f28e4afa-90b1-11ee-98e5-6d05b1d4d9a1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EtsSzTTFKfMxr9RT3u7c667tfDkp40sFebXgbVBZl2pfkgxTcOQhVvsVtqSqgJMnsiEYM25PHvLkUR9u7lHnYd2Fsfm1mHQNiHmMTPRv9XaGFypUvJ0N1v2Q3cfBaNCIjFDxgl+D70MMlwzIHIZSEXPi1Qgw+uqLazQUYQkVQ4x3zHoB9yZIMa//ftyevRG7SK5xBMJQY3fjVrHZfECtBj/ykbjhjfZC6UCec63e5DmeLVBIyW3V5WOrK+HDZ8OvhP7Tsqo0/sE2LBij1D3Qja+fItcnsiC7u8AJs21Q518A4nnX+Zl8Z6v9tJKGiYyOggPx7lFBhCMYFTUXnyIsrw== 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=IICNCcNAN0CxumVVzE/EyahOFwl5SBHiAYz4cBvI7SY=; b=KPSiaLdszceynAJ4498AxfHYvxQSkaCsJu4sfh5jAinQIWoF8l4QLtXMJQUsmkFsqu74d6GwjW3ipyYoVL3vVr7RSQsPS9Zv42p892Eq0VHzmegIEMawguZ0bxOWatubmo2IVe+uMMHMWRt7GLE8EP/wqrDM3JjTFSagmgRH5Vkz6cwrXEdOxcckavX/9hj4d2+s8VRRwpKiAdDOeoR6JHN9X/jnVnNXNEIaZ7scuowcoTcXYwkJyF2avO1ZntZJQu5IO5Vl2y08Yp3eHcc5L8S9KyDjNo3Bavl9ZRS6ovKjb7N0KD3W1NlioH5SwmCfvQszE6qKEahRP54ZdJHsvQ== 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=IICNCcNAN0CxumVVzE/EyahOFwl5SBHiAYz4cBvI7SY=; b=cy94FqguVr4bbMwcACAJYI7hwhmvWhith8Pdx0FlH9CyPXyatOFwGQEgZeWIY7cGxCAlLYDIKzI+gVRNEdu/BZohljtDs9wRxB3iDCYHAo1o0PyTF8tXEaVl6lt3fQ8njw7ToySLgbWRl93TvpqmymSfSv3txKZu20pUtzfZhOUkqrFwm6OCfFVJ3wBZX2K00fFX/tk655yUzMdREjkf2hVKqVdeJOBxv0EUOz4I9rnXLLgCW3eLiT5a6G7PfxsbuPA5PgFoZ7mOySpQUqI8SG30Algr825S1NsPjKJL/nFG5W8c+CW2k/ZCvRJczKXXQph0wAqzFPp7gHeQ38jeiQ== 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 v11 02/17] pci: introduce per-domain PCI rwlock Thread-Topic: [PATCH v11 02/17] pci: introduce per-domain PCI rwlock Thread-Index: AQHaJL6n2Oa1PKslt0Wo3rzC+KkVjQ== Date: Sat, 2 Dec 2023 01:27:02 +0000 Message-ID: <20231202012556.2012281-3-volodymyr_babchuk@epam.com> References: <20231202012556.2012281-1-volodymyr_babchuk@epam.com> In-Reply-To: <20231202012556.2012281-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_|DU0PR03MB8599:EE_ x-ms-office365-filtering-correlation-id: 9001b78d-5e3f-4a6d-8e86-08dbf2d5caea 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: e0GE2z6cPR+Kk8pM706jF6sVXMbpqpThb0Q4GOjknq/TdSc8kmcS1fpUYUVDJVH7PJIDF0CVuLDrXWGH5CbdLt3Zek0DJavc8A4cDI5fSzqxhjBAqzggXk98cigogP2lF3yGQXRKP3IeZrRt0X83dD/3DyQ6/SE2QOkT/QrwaTAahOh1qm3uovi+P93rX7aRJsGUH8CxoZ0W66xDgSf9s9FoX4enWjGMUEZjbf2886FWYeoVc3jgg5zDgRktR7k2zw707lpvPMQyUkYHP3erVWqvTCNo11ENaYMlqfk7lBKma1SlFVcX053RkiODCtsaQrrfQbAfOEIZKWvqABesQIlqRFqqLx57QsGRu0viM8IYdFej8BW5a5jSBqYk/yvMPX9qx9wgLa3KjDZKVgmLJeQ6jhWNLVMn8OCiKcuSsB9+KY5NllrOKMxAxGVtU9qqKA3ykWeoX6Jvr5tmIfxxUU70o2dA/xWXDHbc8F0neBPgdT6+2TTkWYTQ1mEhaxo9ue8ULwJLzNVlnCYafxKYmpgm1n4qd7/ghN0jjuJ9S6HIZ75jNIXduybNBFjcH1exZITpGyu7Tr/0UtxH0HSLDKMv2D0tXPxb16cGbW1BDod9ceEaYzmms5SS3jW6eDbM 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)(136003)(396003)(39860400002)(346002)(376002)(366004)(230922051799003)(186009)(64100799003)(451199024)(1800799012)(5660300002)(7416002)(86362001)(4326008)(8676002)(8936002)(2906002)(38070700009)(41300700001)(36756003)(2616005)(1076003)(6512007)(6506007)(55236004)(83380400001)(6486002)(478600001)(26005)(71200400001)(38100700002)(122000001)(316002)(91956017)(54906003)(64756008)(66446008)(6916009)(66476007)(66556008)(76116006)(66946007);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?P5BO2oYAzU5fkC7pj/zfZYlgZ/aL?= =?utf-8?q?XLzpUw3CaPXjuQgfb1Dnm4VX7w2RD8ElND/7XzG0OmoNv4cCffEqJ8hmr5JpZbq/i?= =?utf-8?q?yv3KzEJinxjkooGpYC76vIREq3fBuiGD68ZxlSveEV2+YbaYNi3y5/SrNxCKGQ2l9?= =?utf-8?q?MxZEnqNDjY6YcVmmQGuHY+WOdFmazZFNTLDZavhOjEav+2aUZ7b5eUtGpX7rZm5PL?= =?utf-8?q?8dj0XyrXNalot/vRhyXk5ufhO3UYzygK/qE8sRVE0RFevvW5c98+JDlJOoHTFqqcN?= =?utf-8?q?FWyMcgXCzvogWD/KUXly1YM5kLGdyyB169LRwnrSXyYR5sh9GJ7omajrDDwI7ST/4?= =?utf-8?q?B1iiel/q4KaAtLWAXg1rPjr45o2szh9i6KzzuFGo6tM4IBVnuvkOPan5vtoc6ojCb?= =?utf-8?q?HrX1ihNMe8Z2Dgkf9KMa8mSMLAROWIvS9cvzDi5lxoLM2x3JqVECCxFTGsFdhHDld?= =?utf-8?q?Dmai7bAHugnqOVNEpBqlVzujnlxA9cQJlDcN8r9ajXWyMqr2blS9A8zGaKi6UbQI+?= =?utf-8?q?OEDQM0i41KUBCjWv4aWnJq3BDwpNIi2r7P68xKVoOZ6g+e0B7HogLZB8Sdu3EY75f?= =?utf-8?q?EiBx1YtsjdWjsQXlkO/iM6MA8e7b0BrClUIdz3LIoFiMKoA669dZNThmzrLI9c68j?= =?utf-8?q?FPh7sK+uIz3CjUMi4wc0tB1iKfuC6uGdMWuCyRoMWi/wnVxk//+MQ7sQ8yWO+aNTt?= =?utf-8?q?zETgRZJWzPw9J3P27TqYO9CViDudse/uy7vl1KzC9Hl5s+o2HNlYbiap5b4554VYl?= =?utf-8?q?F7AfRiruqS0sPxvmS8Go5ClJnhRqCwy0t27DFAHJc91pem0WiYzfTRljTkfIt+Fez?= =?utf-8?q?CE1Zar0yHqq6lDFdTNre07X4hE87BsM5QN40V2uhOiqH8UmW4FWfSfloBZTgjE7L7?= =?utf-8?q?dtZZGePYMiTShCRvLOo9OgXowkJZwWf9DfdtheQUi/48P3c5e9VzYGkgXEvHp/V5v?= =?utf-8?q?Bfs+Dab3xDHDRpHWdncCxH1N7+W8hdXb4RT6IyFTqUL9lM0criScPCYy+TlFUNOf3?= =?utf-8?q?ktrp0pqDl8Xzlu6q3El0bjsHyCUAsK9Bwj7hHbM0uZQBI4qTt5JINZ3Eau7kV41Oy?= =?utf-8?q?v8SxJ94LoOWlnQnJcSqh2qXzndeDwtSKqLvkRA5Rm0HDSLJu5jYyi+cIoD8mXDsi8?= =?utf-8?q?nLrkAfh9G2ShozEd7Zb1oWFj/OqgbJvTTAPi4wrC5q30fqt6QIzV9mFHR+bEqsI/e?= =?utf-8?q?nOy5tzccgwTnB06SEOS7+/M+UOsnh/zvAPCak3qI/ms8dMi6es5cK9wZa54oxG3kv?= =?utf-8?q?tnjFu2gaIqMF8uHu1sKXbq+g1KDZK0P8p6qR5b5xc6XgVZD8ZCob2JVBQaOc/4Ow9?= =?utf-8?q?D1XI9hNltWaPsDIL52SVK1fcQgFYyJl1QcUxWtvwwi3se8LLSjMhLGFR+Oix76d+X?= =?utf-8?q?eqFAm4amtp1kDtaucUZLPQ0ZBBucPJkg7hsQX4ImARPFetRFlduVIfu+FMRnRTNxs?= =?utf-8?q?wdM/vL6i1E2AdfsTYLhztISRa3VsCQooDuxBt3z6pjhk6pJLqgxwrP0HWGkH/CDdE?= =?utf-8?q?gAwFmUVGEBGr4FbRKKuHkw4m82jcA8utXQ=3D=3D?= Content-ID: <4992D0DFB71AB74A97E397CF7B083A59@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: 9001b78d-5e3f-4a6d-8e86-08dbf2d5caea X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Dec 2023 01:27:02.9281 (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: Heozpc5Ctl5PMKuY2ty3OCNbKXtAt4Qu/zOR/YGyq4jN65ILqtQEnUgNfse9debQS5zDQgVWjlyvH1lsFKs6rVE578AKchShrQNv/FgKkwg= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR03MB8599 X-Proofpoint-GUID: aR7fQrPtL88xBAhVHPNd2McuMJ03MFdI X-Proofpoint-ORIG-GUID: aR7fQrPtL88xBAhVHPNd2McuMJ03MFdI X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-12-01_24,2023-11-30_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 phishscore=0 lowpriorityscore=0 spamscore=0 clxscore=1015 suspectscore=0 impostorscore=0 adultscore=0 bulkscore=0 priorityscore=1501 mlxscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311060000 definitions=main-2312020008 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 latter must be acquired first. Suggested-by: Roger Pau Monné Suggested-by: Jan Beulich Signed-off-by: Volodymyr Babchuk Reviewed-by: Roger Pau Monné Acked-by: Stefano Stabellini --- 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 cd2ca6d49a..9b8902daa3 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 6bc73dc210..5cd208bbef 100644 --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -481,8 +481,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 e13b7d99db..d3dfab679e 100644 --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -2816,8 +2816,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