From patchwork Thu Jul 20 00:32:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 13319661 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 E0361C001E0 for ; Thu, 20 Jul 2023 00:33:10 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.566256.884849 (Exim 4.92) (envelope-from ) id 1qMHb3-0001gC-GK; Thu, 20 Jul 2023 00:32:45 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 566256.884849; Thu, 20 Jul 2023 00:32:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qMHb3-0001ds-9a; Thu, 20 Jul 2023 00:32:45 +0000 Received: by outflank-mailman (input) for mailman id 566256; Thu, 20 Jul 2023 00:32:43 +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 1qMHb0-0001JN-Vo for xen-devel@lists.xenproject.org; Thu, 20 Jul 2023 00:32:43 +0000 Received: from mx0a-0039f301.pphosted.com (mx0a-0039f301.pphosted.com [148.163.133.242]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id ee9a29ce-2694-11ee-8611-37d641c3527e; Thu, 20 Jul 2023 02:32:40 +0200 (CEST) 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 36JMeiB5017190; Thu, 20 Jul 2023 00:32:37 GMT Received: from eur04-he1-obe.outbound.protection.outlook.com (mail-he1eur04lp2057.outbound.protection.outlook.com [104.47.13.57]) by mx0a-0039f301.pphosted.com (PPS) with ESMTPS id 3rxgyx9k43-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 20 Jul 2023 00:32:37 +0000 Received: from VI1PR03MB3710.eurprd03.prod.outlook.com (2603:10a6:803:31::18) by AS8PR03MB7783.eurprd03.prod.outlook.com (2603:10a6:20b:407::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.33; Thu, 20 Jul 2023 00:32:32 +0000 Received: from VI1PR03MB3710.eurprd03.prod.outlook.com ([fe80::68d2:d90f:ac32:7c85]) by VI1PR03MB3710.eurprd03.prod.outlook.com ([fe80::68d2:d90f:ac32:7c85%3]) with mapi id 15.20.6588.035; Thu, 20 Jul 2023 00:32:32 +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: ee9a29ce-2694-11ee-8611-37d641c3527e ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jwMuXJV4hDcgX76yzhHxDh+sOKtQ+0l0jRTb4FOfd4dDit1OA239Nnfnp/Xzf+uqHI/eQMq6UghXSpj6lDxhfqPuPPxF7VvODdudWWTq76HxmjGrOvhKQezbZpI8UEzcgQ3oF5lKoxTE9Tjb6AUvmJiZc2vrOgh5RDVGjeLofur903FeLWDD0y7dzbx8Cfeqv16H6eHqdap127mDHH8d6cA9c/4zMlLLD3UW2Vv1DCNWRPv1tLDVD4yMyhPEudv8+LhAYFSSttwHbEcq0P9evv7h4Bm8tZdQzu09cKkPMCrwbRc/UvYUDP+QwBUUI660cAm1A9PNdHi+4qbynshD/Q== 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=iKqHtyDUDA8wFxvQepqDFxbxA7jnJx0SzIFiYOphyxM=; b=eCqwV3mcykF2KIDkepRT2dKaSU+GWKBjiRkSDG8eX/QZbputTmrhKjDG1L3zeOUeMku97chKWeZSuSF7yCoKuMsGFT5RiGLqI2GUrvQfqNaNsjjCiwJXp5/lZ9sboB468v8Nu/NqyOkggViNMFpLKncuZ1JMKI3kHuSLLcuXBNBQT2V3mATXVLFFHTCO0lTWtXsMIC4kfBd2L7DKddfQUYe9yBk1kz8cJPCZ4lDA4Z68OtB6mxBj5VDkKn5DaztGx0xL6MZj6ocPh8jzcNCxoJjo1rlkO2+q7LY4uLnaX0thVUNBHGmvxjjJ/GXoze1EGI9QMTFK4EmTnIvmP00Bew== 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=iKqHtyDUDA8wFxvQepqDFxbxA7jnJx0SzIFiYOphyxM=; b=ZQBpaN6niSuGrXcawTKRpgYx6qZz/TsfszX99XpaxDmU1xbZMIlCyQEIJ14ER76wiW6amh7zwSyYazPFt1LrjOYp68TcSuLO3M3oGQRyNHZEddd13NAPpeFIl38Qb14bgNXvNcZr0dwlUk7HeQahgVd0mUym+t29V3oNlZBvLszt2jqEw+r6CPMDkihu4aHpQSCm1CLW6UGPXcGX7QLpO9H5FEGUXG6QH1OdDrbsJdCCPevfTlkwKRHXpq70KtXU0nKEuPGOXnABh3VjPfNu2YJBh/TwfxsIw7W9+kHk/5GHYq6ZFFOCkZmu7eIhYGUNe4+4+muQ7pyiL8nHgxF9LA== From: Volodymyr Babchuk To: "xen-devel@lists.xenproject.org" CC: Volodymyr Babchuk , =?utf-8?q?Roger_Pau_Monn?= =?utf-8?q?=C3=A9?= , Jan Beulich Subject: [PATCH v8 01/13] pci: introduce per-domain PCI rwlock Thread-Topic: [PATCH v8 01/13] pci: introduce per-domain PCI rwlock Thread-Index: AQHZuqGr3g/gocvkCU2kdSqq613AoQ== Date: Thu, 20 Jul 2023 00:32:31 +0000 Message-ID: <20230720003205.1828537-2-volodymyr_babchuk@epam.com> References: <20230720003205.1828537-1-volodymyr_babchuk@epam.com> In-Reply-To: <20230720003205.1828537-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_|AS8PR03MB7783:EE_ x-ms-office365-filtering-correlation-id: a41c2575-ed4d-4f4b-0c2a-08db88b8ce60 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: Tza7cq3IoswCttZHApdAfEwDOPMqbHTQnybXZNR+0LOP1FwzPl1Fp7Xmq++1sjFnIX+LohpBHtBhxqtoungcRyyZrc96M+k2ilMpED6Ws6u5PBVoVjSEY+GVkM8ZnNj2yWfOlZZtRX6OvS7SKYFtDpPHdEciGK6BzlfZxLV5pNeTOK2SNuUjaOHQvQb7PZsYWNBJiaazlp9GwCLeZLx28E6Mmybd+M9oIMTSWVwEtpUtbvdqe5odJsp9j/i/H9roOVPZ8OF8j1Jq9OlVOohzpb+PuUMyYzOvobzGRa7YV8DVGsVX9h/UodE2NL2osxYu0Fy+JM7IDM8wGS8ym/bOSAkw8iHmYnH9OYdym++LgsSruy0r42b7NVcyYLiR47++SFujq5QCRL3ZHv9CgHcrvuf0RJgDkehTU2pukiKtYDJ/Xr5vTZrWzIM0NSKjAARxGwq8hdUWFOR+txUgWIDxEYjTc2vgJAmGZm9iqnZgleUOEab9MtTp8pRrqPD4FlJK6Vag4g0EzK9sNxeecAaEPgz/Iwg3GiLnGnzOyxC/Tl8pFo9tBr8mb7NhlgZZowExF/H+m11FAFMTajNUZGQq9OLvJ8tqkTbPY7VVLXO7QD8f9r7DazSGbb6B1UDftIGR 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:(13230028)(4636009)(136003)(39860400002)(346002)(366004)(396003)(376002)(451199021)(55236004)(26005)(1076003)(6506007)(41300700001)(316002)(83380400001)(2616005)(186003)(6512007)(6486002)(122000001)(478600001)(66946007)(54906003)(71200400001)(4326008)(64756008)(38070700005)(66556008)(76116006)(66476007)(91956017)(6916009)(38100700002)(86362001)(5660300002)(66446008)(8936002)(8676002)(36756003)(2906002);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?EShXOCN3BVN8OW2QPu5AYfeoxNAN?= =?utf-8?q?w+xX3FRMMVrcQna0QY8Nw5R+Ej1fIaE4Np1fi/lXOE/AzuVXEsFxMlX4/LnIIFMht?= =?utf-8?q?amORsB19x6SZpNcqHw7hB/u5GRbbzYGSgAU6Gwdqs2BxjZy/752wsak14SSADNZWC?= =?utf-8?q?ObIO3k6AxTrqEo+YutxJQ2EhjRqYdYrYGmIj2KHftSEbEmgXfEViNBha5k6HPd1Ge?= =?utf-8?q?OoxeSBV7SxTra45zEMXafgrPmnk1dp8Z9vfuVLC+ZinT5OubnVwuG7m/ci4cRM7F7?= =?utf-8?q?ksk6dSCyCjON+Zog06HIURvO1Vj5YfQK5zfX++V/tztCixcq42/+I6nLQJvI0+JrT?= =?utf-8?q?owu1OMoXecG+H4xgqdKfjTilV1jea+E8gYtswVSEc76bB4h25J/L2Y42sOJvzDFLI?= =?utf-8?q?MEXtWw+IQ40pdL23dF6rlOuEGKEhIjFNNW3bEbeIMVHNj0le1My316Qa9JLH77vqo?= =?utf-8?q?+pHhC5/xhLajTWlb5/k88KRtKM1Rn8iUFqs/nMkhN7cmhrEuY5KNsFVfGe0Vr7A8l?= =?utf-8?q?ccLzKLEuqdknvarohLaCokk+MT/4338Z50zS8fNFDqimzL9KI8/4lL41BIwsCes8g?= =?utf-8?q?ZA2e2ezCB2vNol6kUkAGBghR+73Sme3Eakh6614FSGnL+hK6VNLxPbonKI+qKfVO3?= =?utf-8?q?MFiUhxSvLcjjZBnzZkZFdpYNfXHk9aVVlafhEYVJd1mRJCighrNVXIQWiy1El3TyK?= =?utf-8?q?WLcM0zz+yDoSnSrxgwZKhJ4WVpow64kOO6gHqgScAKYsBmTcejaZvq/8/5JnZ89jx?= =?utf-8?q?nYxajSRR+aPDya8/e7UhwLnJrCfWsj8efp6tUBvFu9z0RSyayKCCAkLUPbMAJlTda?= =?utf-8?q?dzIEQuu9J6riQpz0XUgxzJb6w9sVNe/tKBNkBZw3PbWm/CmdFddFZ8xY1UeZDYkEQ?= =?utf-8?q?NpeHRj3XWjnMTdVMBEkbWMgBgn9m0/5rU3MswHaI/wo3V2p+ikCvPrlOOGTd65RTI?= =?utf-8?q?OJ761Jf+Fi6mqhZzQ6CTj7TG1Uqot9VRnUDWJmi7B7/f7zooV/XzQV49ipn4q1dPq?= =?utf-8?q?SScXEUkTxiq6ZuCCWqQ3CH6HZaUFNgPJFBhufo3NM2zyg5DCjPHcrINtE5V2weqZZ?= =?utf-8?q?iLAyA8fjNyBIdKHXxS4KuWJipsASlVL8Dcb0m3Z2+5wSiOB5IaqwG0jifR/geOc3d?= =?utf-8?q?KbqXgyrSXu8mWyMe2O1GILzikw2nK6YTbiDMmFznsSY/IKYreVCkr6bsUeqIP9dYt?= =?utf-8?q?fqPOGJwaO6il4EOowzVtxicnDngYUDGkHgM7PfT+ysT7MZQ2uDv+Cw4Tn0lgV1Ej8?= =?utf-8?q?CcXiF4zV+Gp0bhq9B9I3tUn7Ji6rt67GCuNUS06xrEdvWucl+EiUm5tMBjz+u6PMe?= =?utf-8?q?XKFEpmEif3Ei5JgnX9Ehw5kKm07bfck3tgsV/ZoU2X9BGpctMHP2WueZvAfrC8xjO?= =?utf-8?q?2u1L4RbwvBDIhDIgj3Wm9mPIWMfWXd7K7lh+Hy2X6SKiSq249y/Vy6L3Te0qUuoIN?= =?utf-8?q?lCKplCWWaVbRUStcdEiLVZluf6jWZUTPxGuoVP3MT9/Yw+L9KXm1udkRXHQa1kOE6?= =?utf-8?q?+7EtXCGNpgDFf1KVTwy32MJUEv00EGsuGw=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: a41c2575-ed4d-4f4b-0c2a-08db88b8ce60 X-MS-Exchange-CrossTenant-originalarrivaltime: 20 Jul 2023 00:32:31.0563 (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: ON8KfDYwRzy+4rxSN6hp6WFYMVg/DtrqaVKH64tyOp4zqQkL7xpnRGkD4U/zgClCb2ANvCoOtqJl1aKJuxztblui1KYBPBl1jYAKMyZwTjc= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB7783 X-Proofpoint-ORIG-GUID: GVDTlzfBD9UJ1hjtXI1V19gWUeRZeGKf X-Proofpoint-GUID: GVDTlzfBD9UJ1hjtXI1V19gWUeRZeGKf X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-19_16,2023-07-19_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 impostorscore=0 priorityscore=1501 bulkscore=0 spamscore=0 adultscore=0 clxscore=1015 suspectscore=0 phishscore=0 mlxlogscore=999 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2306200000 definitions=main-2307200002 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). 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 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 | 68 +++++++++++++++++---- xen/drivers/passthrough/vtd/iommu.c | 9 ++- xen/include/xen/sched.h | 1 + 5 files changed, 74 insertions(+), 14 deletions(-) diff --git a/xen/common/domain.c b/xen/common/domain.c index caaa402637..5d8a8836da 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -645,6 +645,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 94e3775506..e2f2e2e950 100644 --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -476,8 +476,13 @@ static int cf_check reassign_device( if ( devfn == pdev->devfn && pdev->domain != target ) { - list_move(&pdev->domain_list, &target->pdev_list); - pdev->domain = target; + write_lock(&pdev->domain->pci_lock); + list_del(&pdev->domain_list); + write_unlock(&pdev->domain->pci_lock); + + 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 95846e84f2..5b4632ead2 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, @@ -747,6 +749,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, ret = 0; if ( !pdev->domain ) { + write_lock(&hardware_domain->pci_lock); pdev->domain = hardware_domain; list_add(&pdev->domain_list, &hardware_domain->pdev_list); @@ -760,6 +763,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret); list_del(&pdev->domain_list); pdev->domain = NULL; + write_unlock(&hardware_domain->pci_lock); goto out; } ret = iommu_add_device(pdev); @@ -768,8 +772,10 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, vpci_remove_device(pdev); list_del(&pdev->domain_list); pdev->domain = NULL; + write_unlock(&hardware_domain->pci_lock); goto out; } + write_unlock(&hardware_domain->pci_lock); } else iommu_enable_device(pdev); @@ -812,11 +818,13 @@ int pci_remove_device(u16 seg, u8 bus, u8 devfn) list_for_each_entry ( pdev, &pseg->alldevs_list, alldevs_list ) if ( pdev->bus == bus && pdev->devfn == devfn ) { + write_lock(&pdev->domain->pci_lock); vpci_remove_device(pdev); pci_cleanup_msi(pdev); ret = iommu_remove_device(pdev); if ( pdev->domain ) 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 +895,62 @@ 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 ) + write_lock(&d->pci_lock); + combined_ret = arch_pci_clean_pirqs(d); + if ( combined_ret ) { pcidevs_unlock(); - return ret; + write_unlock(&d->pci_lock); + return combined_ret; } - list_for_each_entry_safe ( pdev, tmp, &d->pdev_list, domain_list ) + + 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 ) + { + bool still_present = false; + 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 ) + { + still_present = true; + break; + } + } + if ( still_present ) + list_move(&pdev->domain_list, &failed_pdevs); + 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 +1169,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..55ee3f110d 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(&pdev->domain->pci_lock); + list_del(&pdev->domain_list); + write_unlock(&pdev->domain->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 85242a73d3..80dd150bbf 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