From patchwork Fri Feb 2 21:33:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543455 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 22E8CC4828E for ; Fri, 2 Feb 2024 21:34:26 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675208.1050480 (Exim 4.92) (envelope-from ) id 1rW1Aw-0004DL-0m; Fri, 02 Feb 2024 21:34:18 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675208.1050480; Fri, 02 Feb 2024 21:34:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1Av-0004DE-TG; Fri, 02 Feb 2024 21:34:17 +0000 Received: by outflank-mailman (input) for mailman id 675208; Fri, 02 Feb 2024 21:34:15 +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 1rW1At-0004Ae-In for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:34:15 +0000 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on20600.outbound.protection.outlook.com [2a01:111:f403:2409::600]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id ce299130-c212-11ee-98f5-efadbce2ee36; Fri, 02 Feb 2024 22:34:13 +0100 (CET) Received: from CY5P221CA0005.NAMP221.PROD.OUTLOOK.COM (2603:10b6:930:b::17) by MW3PR12MB4380.namprd12.prod.outlook.com (2603:10b6:303:5a::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7270.11; Fri, 2 Feb 2024 21:34:07 +0000 Received: from CY4PEPF0000E9D7.namprd05.prod.outlook.com (2603:10b6:930:b:cafe::bc) by CY5P221CA0005.outlook.office365.com (2603:10b6:930:b::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.26 via Frontend Transport; Fri, 2 Feb 2024 21:34:07 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by CY4PEPF0000E9D7.mail.protection.outlook.com (10.167.241.78) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7249.19 via Frontend Transport; Fri, 2 Feb 2024 21:34:06 +0000 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:34:05 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:34:04 -0600 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: ce299130-c212-11ee-98f5-efadbce2ee36 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LSA2ldg0jJmljnpoOe9T5Z9S0suH/qKrPwSY2qe4G5jzThpAj8SzhtIYo8+oxFf9VaUCqJ+s3n+0FWF/mSXKgWptEooTEy/OvNMDfviGRmniHxMAXShvPnKg8rtpu47MCgIcNUtETmLifJuXJFxSkEFNmdnInitcACZN8mhD8imYtCjl4+fm/sFUfRLnJ2H3pa49XExPDQFW+IbZsBIv9rUokolQoLFldPiR9j87/lZ7j5ooTeJtuGVjmOCPN44I8kacFG9l73cRB84NDi+B8ezVY/Pv4Sg3SWfXAegzqA+wB9YRRvx3VRZL+6FYlo66Gq7eDtsJOwNaqPueVJvQ5A== 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=H3tS51EWeLyplN/iiO2R/OIVC2P7UkDQqG6IT45GkZU=; b=EnDMgCXzO0MCscxAcKXVGUQ20qmeYutRKKJonjUj0m1vPeScJzR2Mckukr3jKEU2QwpHLZUiXXJi1lfdfGSjmqoc5DtcByJkAG/2LQf6BrA5Q0iRWI7qZCf15y951EiceLRwxL7OxAnhiYnajVBGW3LPJYmEB0kd5XXHVYR90OuDe+4iMqtpSZVKXrBDlc6I8PahmfrEPvpJsWOBAQFqiRNqTDR7cc2Vi1B/gZ+oPMJb8qNnOy5rYgX+14ofGiyRJKP1ojwWRQptw68lJJHxs1h9WtZ7vAcbi5jkJCDmJ4wkVvOvKrNYzQwfHcJwCickRIaluDLsulGEwdowyoHS9A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=H3tS51EWeLyplN/iiO2R/OIVC2P7UkDQqG6IT45GkZU=; b=BbVGTg4g8dIi2wsuPfIMOApmaS1tEyrEBInF7Ah8FFYODgeTjqht9bhx6ADNjNRSjTxalhzdIkhiP1VSA5ARLZ6TEERVS9GMVwTBiLgR0v77ly5qy4qlQMlw03hgnw1YciIt7+y1YXZamoRK3MvkFKiiP4ECQoszrpXRDsWWZRI= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Stewart Hildebrand To: CC: Oleksandr Andrushchenko , Jan Beulich , Andrew Cooper , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu , George Dunlap , Julien Grall , Stefano Stabellini , Jun Nakajima , Kevin Tian , Paul Durrant , Volodymyr Babchuk , "Stewart Hildebrand" Subject: [PATCH v13 01/14] vpci: use per-domain PCI lock to protect vpci structure Date: Fri, 2 Feb 2024 16:33:05 -0500 Message-ID: <20240202213321.1920347-2-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D7:EE_|MW3PR12MB4380:EE_ X-MS-Office365-Filtering-Correlation-Id: 00f4f925-5f80-477b-8f2e-08dc2436af56 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wTQslJ6PtW5okJLReliSa9juXs3C+LdSaOTnJVOHZW+3eK3H2Mfx+ZNh6Kgx7V1HwKp4OcD5DQTzZGECRP17EJmkK8SOt/Da+A2AYHvhk0TW9eyjLSisIc9dp1SJJT5qqQrxxZds+jCfMFHK+enOpww5qApAGxy761qjYi6YKwRfW/EpA6OIYB6PXR7aYXW4ZXyrYXFx+OlbT3CnKOAgIOWHKKSHJNLtmDVKCpJfk2kTl2C2hoAoOQnc6zASTqRqd97T87ss062NoDpg+TaIYKQdtSKdxaQjYaaXxzGGrIyfrMk5SmrDavzyA9pwqULYNq+vunF1xDdGcP/eNcKX7v5bymph90oWbcqxZqKKa2bv5m0beohhREpSRFYDaSUQ+YINwHid1Z/Vh0QQcwopXEMzrBPicFstRPj/vls8psCFUk6dLrb5Yfo3PNF3q2efl4xBMZtvaJE2I5WIIHqfUtPmZTW/YBB5iANrGW0WANZFQeeL4ygDB4lAQ3/0NbjCCswO/f8XCo4rUnoAwZW/FzwDF1opqNB8sRmb/nb/6qxE1WiJMPzPgO3OPNBn0wxUhVZpCFCxkRurV8OsG+30Dl6sYKxHvHVgQDqJzEnFArx7XxBG2dYSYcoXfau0bwtmE+J1AjPaF8mVtx/VdX7tAvkomRioXpoyX1NniytY2D2jtObuX87O94EqG3vsl2HWx/ZyQh5UiuhLM5qsUGFFY4gC4lYlso6dzYIZdPNVWHJYeVsAAcsDMetiBdw0jXi4cYi6ziTucP/wTE0NY09E9/nyFMOuisezeWZByPP09aE= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(346002)(136003)(39860400002)(376002)(396003)(230922051799003)(82310400011)(64100799003)(186009)(451199024)(1800799012)(36840700001)(46966006)(40470700004)(83380400001)(81166007)(336012)(47076005)(2616005)(26005)(30864003)(36860700001)(426003)(1076003)(8936002)(5660300002)(7416002)(8676002)(44832011)(4326008)(6916009)(316002)(478600001)(2906002)(54906003)(70586007)(70206006)(82740400003)(356005)(36756003)(41300700001)(86362001)(40460700003)(40480700001)(36900700001)(309714004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:34:06.7349 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 00f4f925-5f80-477b-8f2e-08dc2436af56 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D7.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4380 From: Oleksandr Andrushchenko Use the per-domain PCI read/write lock to protect the presence of the pci device vpci field. This lock can be used (and in a few cases is used right away) so that vpci removal can be performed while holding the lock in write mode. Previously such removal could race with vpci_read for example. When taking both d->pci_lock and pdev->vpci->lock, they should be taken in this exact order: d->pci_lock then pdev->vpci->lock to avoid possible deadlock situations. 1. Per-domain's pci_lock is used to protect pdev->vpci structure from being removed. 2. Writing the command register and ROM BAR register may trigger modify_bars to run, which in turn may access multiple pdevs while checking for the existing BAR's overlap. The overlapping check, if done under the read lock, requires vpci->lock to be acquired on both devices being compared, which may produce a deadlock. It is not possible to upgrade read lock to write lock in such a case. So, in order to prevent the deadlock, use d->pci_lock in write mode instead. All other code, which doesn't lead to pdev->vpci destruction and does not access multiple pdevs at the same time, can still use a combination of the read lock and pdev->vpci->lock. 3. Drop const qualifier where the new rwlock is used and this is appropriate. 4. Do not call process_pending_softirqs with any locks held. For that unlock prior the call and re-acquire the locks after. After re-acquiring the lock there is no need to check if pdev->vpci exists: - in apply_map because of the context it is called (no race condition possible) - for MSI/MSI-X debug code because it is called at the end of pdev->vpci access and no further access to pdev->vpci is made 5. Use d->pci_lock around for_each_pdev and pci_get_pdev() while accessing pdevs in vpci code. 6. Switch vPCI functions to use per-domain pci_lock for ensuring pdevs do not go away. The vPCI functions call several MSI-related functions which already have existing non-vPCI callers. Change those MSI-related functions to allow using either pcidevs_lock() or d->pci_lock for ensuring pdevs do not go away. Holding d->pci_lock in read mode is sufficient. Note that this pdev protection mechanism does not protect other state or critical sections. These MSI-related functions already have other race condition and state protection mechanims (e.g. d->event_lock and msixtbl RCU), so we deduce that the use of the global pcidevs_lock() is to ensure that pdevs do not go away. Existing non-vPCI callers of these MSI-related functions will remain (ab)using the global pcidevs_lock() to ensure pdevs do not go away so as to minimize changes to existing non-vPCI call paths. 7. Introduce wrapper construct, pdev_list_is_read_locked(), for checking that pdevs do not go away. The purpose of this wrapper is to aid readability and document the intent of the pdev protection mechanism. Suggested-by: Roger Pau Monné Suggested-by: Jan Beulich Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Volodymyr Babchuk Signed-off-by: Stewart Hildebrand --- Changes in v13: - hold off adding Roger's R-b tag even though it was provided on v12.2 - use a wrapper construct to ease readability of odd-looking ASSERTs - new placement of ASSERT in __pci_enable_msix(), __pci_enable_msi(), and pci_enable_msi(). Rearrange/add pdev NULL check. - expand commit description with details about using either pcidevs_lock() or d->pci_lock Changes in v12.2: - drop Roger's R-b - drop both locks on error paths in vpci_msix_arch_print() - add another ASSERT in vpci_msix_arch_print(), to enforce the expectation both locks are held before calling vpci_msix_arch_print() - move pdev_done label in vpci_dump_msi() - update comments in vpci_dump_msi() to say locks (plural) Changes in v12.1: - use read_trylock() in vpci_msix_arch_print() - fixup in-code comments (revert double space, use DomXEN) in vpci_{read,write}() - minor updates in commit message - add Roger's R-b Changes in v12: - s/pci_rwlock/pci_lock/ in commit message - expand comment about scope of pci_lock in sched.h - in vpci_{read,write}, if hwdom is trying to access a device assigned to dom_xen, holding hwdom->pci_lock is sufficient (no need to hold dom_xen->pci_lock) - reintroduce ASSERT in vmx_pi_update_irte() - reintroduce ASSERT in __pci_enable_msi{x}() - delete note 6. in commit message about removing ASSERTs since we have reintroduced them Changes in v11: - Fixed commit message regarding possible spinlocks - Removed parameter from allocate_and_map_msi_pirq(), which was added in the prev version. Now we are taking pcidevs_lock in physdev_map_pirq() - Returned ASSERT to pci_enable_msi - Fixed case when we took read lock instead of write one - Fixed label indentation Changes in v10: - Moved printk pas locked area - Returned back ASSERTs - Added new parameter to allocate_and_map_msi_pirq() so it knows if it should take the global pci lock - Added comment about possible improvement in vpci_write - Changed ASSERT(rw_is_locked()) to rw_is_write_locked() in appropriate places - Renamed release_domain_locks() to release_domain_write_locks() - moved domain_done label in vpci_dump_msi() to correct place Changes in v9: - extended locked region to protect vpci_remove_device and vpci_add_handlers() calls - vpci_write() takes lock in the write mode to protect potential call to modify_bars() - renamed lock releasing function - removed ASSERT()s from msi code - added trylock in vpci_dump_msi Changes in v8: - changed d->vpci_lock to d->pci_lock - introducing d->pci_lock in a separate patch - extended locked region in vpci_process_pending - removed pcidevs_lockis vpci_dump_msi() - removed some changes as they are not needed with the new locking scheme - added handling for hwdom && dom_xen case --- xen/arch/x86/hvm/vmsi.c | 31 +++++++++++++-------- xen/arch/x86/hvm/vmx/vmx.c | 2 +- xen/arch/x86/irq.c | 8 +++--- xen/arch/x86/msi.c | 20 +++++++++----- xen/arch/x86/physdev.c | 2 ++ xen/drivers/passthrough/pci.c | 9 +++--- xen/drivers/vpci/header.c | 18 ++++++++++++ xen/drivers/vpci/msi.c | 30 +++++++++++++++++--- xen/drivers/vpci/msix.c | 52 ++++++++++++++++++++++++++++++----- xen/drivers/vpci/vpci.c | 24 ++++++++++++++-- xen/include/xen/sched.h | 15 +++++++++- 11 files changed, 170 insertions(+), 41 deletions(-) diff --git a/xen/arch/x86/hvm/vmsi.c b/xen/arch/x86/hvm/vmsi.c index 128f23636279..f29089178a59 100644 --- a/xen/arch/x86/hvm/vmsi.c +++ b/xen/arch/x86/hvm/vmsi.c @@ -468,7 +468,7 @@ int msixtbl_pt_register(struct domain *d, struct pirq *pirq, uint64_t gtable) struct msixtbl_entry *entry, *new_entry; int r = -EINVAL; - ASSERT(pcidevs_locked()); + ASSERT(pdev_list_is_read_locked(d)); ASSERT(rw_is_write_locked(&d->event_lock)); if ( !msixtbl_initialised(d) ) @@ -538,7 +538,7 @@ void msixtbl_pt_unregister(struct domain *d, struct pirq *pirq) struct pci_dev *pdev; struct msixtbl_entry *entry; - ASSERT(pcidevs_locked()); + ASSERT(pdev_list_is_read_locked(d)); ASSERT(rw_is_write_locked(&d->event_lock)); if ( !msixtbl_initialised(d) ) @@ -684,7 +684,7 @@ static int vpci_msi_update(const struct pci_dev *pdev, uint32_t data, { unsigned int i; - ASSERT(pcidevs_locked()); + ASSERT(rw_is_locked(&pdev->domain->pci_lock)); if ( (address & MSI_ADDR_BASE_MASK) != MSI_ADDR_HEADER ) { @@ -725,8 +725,8 @@ void vpci_msi_arch_update(struct vpci_msi *msi, const struct pci_dev *pdev) int rc; ASSERT(msi->arch.pirq != INVALID_PIRQ); + ASSERT(rw_is_locked(&pdev->domain->pci_lock)); - pcidevs_lock(); for ( i = 0; i < msi->vectors && msi->arch.bound; i++ ) { struct xen_domctl_bind_pt_irq unbind = { @@ -745,7 +745,6 @@ void vpci_msi_arch_update(struct vpci_msi *msi, const struct pci_dev *pdev) msi->arch.bound = !vpci_msi_update(pdev, msi->data, msi->address, msi->vectors, msi->arch.pirq, msi->mask); - pcidevs_unlock(); } static int vpci_msi_enable(const struct pci_dev *pdev, unsigned int nr, @@ -778,15 +777,14 @@ int vpci_msi_arch_enable(struct vpci_msi *msi, const struct pci_dev *pdev, int rc; ASSERT(msi->arch.pirq == INVALID_PIRQ); + ASSERT(rw_is_locked(&pdev->domain->pci_lock)); rc = vpci_msi_enable(pdev, vectors, 0); if ( rc < 0 ) return rc; msi->arch.pirq = rc; - pcidevs_lock(); msi->arch.bound = !vpci_msi_update(pdev, msi->data, msi->address, vectors, msi->arch.pirq, msi->mask); - pcidevs_unlock(); return 0; } @@ -797,8 +795,8 @@ static void vpci_msi_disable(const struct pci_dev *pdev, int pirq, unsigned int i; ASSERT(pirq != INVALID_PIRQ); + ASSERT(rw_is_locked(&pdev->domain->pci_lock)); - pcidevs_lock(); for ( i = 0; i < nr && bound; i++ ) { struct xen_domctl_bind_pt_irq bind = { @@ -814,7 +812,6 @@ static void vpci_msi_disable(const struct pci_dev *pdev, int pirq, write_lock(&pdev->domain->event_lock); unmap_domain_pirq(pdev->domain, pirq); write_unlock(&pdev->domain->event_lock); - pcidevs_unlock(); } void vpci_msi_arch_disable(struct vpci_msi *msi, const struct pci_dev *pdev) @@ -854,6 +851,7 @@ int vpci_msix_arch_enable_entry(struct vpci_msix_entry *entry, int rc; ASSERT(entry->arch.pirq == INVALID_PIRQ); + ASSERT(rw_is_locked(&pdev->domain->pci_lock)); rc = vpci_msi_enable(pdev, vmsix_entry_nr(pdev->vpci->msix, entry), table_base); if ( rc < 0 ) @@ -861,7 +859,6 @@ int vpci_msix_arch_enable_entry(struct vpci_msix_entry *entry, entry->arch.pirq = rc; - pcidevs_lock(); rc = vpci_msi_update(pdev, entry->data, entry->addr, 1, entry->arch.pirq, entry->masked); if ( rc ) @@ -869,7 +866,6 @@ int vpci_msix_arch_enable_entry(struct vpci_msix_entry *entry, vpci_msi_disable(pdev, entry->arch.pirq, 1, false); entry->arch.pirq = INVALID_PIRQ; } - pcidevs_unlock(); return rc; } @@ -895,6 +891,9 @@ int vpci_msix_arch_print(const struct vpci_msix *msix) { unsigned int i; + ASSERT(rw_is_locked(&msix->pdev->domain->pci_lock)); + ASSERT(spin_is_locked(&msix->pdev->vpci->lock)); + for ( i = 0; i < msix->max_entries; i++ ) { const struct vpci_msix_entry *entry = &msix->entries[i]; @@ -913,13 +912,23 @@ int vpci_msix_arch_print(const struct vpci_msix *msix) struct pci_dev *pdev = msix->pdev; spin_unlock(&msix->pdev->vpci->lock); + read_unlock(&pdev->domain->pci_lock); process_pending_softirqs(); + + if ( !read_trylock(&pdev->domain->pci_lock) ) + return -EBUSY; + /* NB: we assume that pdev cannot go away for an alive domain. */ if ( !pdev->vpci || !spin_trylock(&pdev->vpci->lock) ) + { + read_unlock(&pdev->domain->pci_lock); return -EBUSY; + } + if ( pdev->vpci->msix != msix ) { spin_unlock(&pdev->vpci->lock); + read_unlock(&pdev->domain->pci_lock); return -EAGAIN; } } diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index 1500dca6039f..5aac4863d30a 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -413,7 +413,7 @@ static int cf_check vmx_pi_update_irte(const struct vcpu *v, spin_unlock_irq(&desc->lock); - ASSERT(pcidevs_locked()); + ASSERT(pdev_list_is_read_locked(msi_desc->dev->domain)); return iommu_update_ire_from_msi(msi_desc, &msi_desc->msg); diff --git a/xen/arch/x86/irq.c b/xen/arch/x86/irq.c index bbae7751e494..e31144d82fd4 100644 --- a/xen/arch/x86/irq.c +++ b/xen/arch/x86/irq.c @@ -2164,7 +2164,7 @@ int map_domain_pirq( struct pci_dev *pdev; unsigned int nr = 0; - ASSERT(pcidevs_locked()); + ASSERT(pdev_list_is_read_locked(d)); ret = -ENODEV; if ( !cpu_has_apic ) @@ -2321,7 +2321,7 @@ int unmap_domain_pirq(struct domain *d, int pirq) if ( (pirq < 0) || (pirq >= d->nr_pirqs) ) return -EINVAL; - ASSERT(pcidevs_locked()); + ASSERT(pdev_list_is_read_locked(d)); ASSERT(rw_is_write_locked(&d->event_lock)); info = pirq_info(d, pirq); @@ -2886,6 +2886,8 @@ int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p, { int irq, pirq, ret; + ASSERT(pdev_list_is_read_locked(d)); + switch ( type ) { case MAP_PIRQ_TYPE_MSI: @@ -2915,7 +2917,6 @@ int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p, msi->irq = irq; - pcidevs_lock(); /* Verify or get pirq. */ write_lock(&d->event_lock); pirq = allocate_pirq(d, index, *pirq_p, irq, type, &msi->entry_nr); @@ -2931,7 +2932,6 @@ int allocate_and_map_msi_pirq(struct domain *d, int index, int *pirq_p, done: write_unlock(&d->event_lock); - pcidevs_unlock(); if ( ret ) { switch ( type ) diff --git a/xen/arch/x86/msi.c b/xen/arch/x86/msi.c index 335c0868a225..e008b6789a28 100644 --- a/xen/arch/x86/msi.c +++ b/xen/arch/x86/msi.c @@ -602,7 +602,7 @@ static int msi_capability_init(struct pci_dev *dev, unsigned int i, mpos; uint16_t control; - ASSERT(pcidevs_locked()); + ASSERT(pdev_list_is_read_locked(dev->domain)); pos = pci_find_cap_offset(dev->sbdf, PCI_CAP_ID_MSI); if ( !pos ) return -ENODEV; @@ -771,7 +771,7 @@ static int msix_capability_init(struct pci_dev *dev, if ( !pos ) return -ENODEV; - ASSERT(pcidevs_locked()); + ASSERT(pdev_list_is_read_locked(dev->domain)); control = pci_conf_read16(dev->sbdf, msix_control_reg(pos)); /* @@ -988,11 +988,11 @@ static int __pci_enable_msi(struct pci_dev *pdev, struct msi_info *msi, { struct msi_desc *old_desc; - ASSERT(pcidevs_locked()); - if ( !pdev ) return -ENODEV; + ASSERT(pdev_list_is_read_locked(pdev->domain)); + old_desc = find_msi_entry(pdev, msi->irq, PCI_CAP_ID_MSI); if ( old_desc ) { @@ -1043,9 +1043,12 @@ static int __pci_enable_msix(struct pci_dev *pdev, struct msi_info *msi, { struct msi_desc *old_desc; - ASSERT(pcidevs_locked()); + if ( !pdev ) + return -ENODEV; + + ASSERT(pdev_list_is_read_locked(pdev->domain)); - if ( !pdev || !pdev->msix ) + if ( !pdev->msix ) return -ENODEV; if ( msi->entry_nr >= pdev->msix->nr_entries ) @@ -1154,7 +1157,10 @@ int pci_prepare_msix(u16 seg, u8 bus, u8 devfn, bool off) int pci_enable_msi(struct pci_dev *pdev, struct msi_info *msi, struct msi_desc **desc) { - ASSERT(pcidevs_locked()); + if ( !pdev ) + return -ENODEV; + + ASSERT(pdev_list_is_read_locked(pdev->domain)); if ( !use_msi ) return -EPERM; diff --git a/xen/arch/x86/physdev.c b/xen/arch/x86/physdev.c index 47c4da0af7e1..369c9e788c1c 100644 --- a/xen/arch/x86/physdev.c +++ b/xen/arch/x86/physdev.c @@ -123,7 +123,9 @@ int physdev_map_pirq(domid_t domid, int type, int *index, int *pirq_p, case MAP_PIRQ_TYPE_MSI: case MAP_PIRQ_TYPE_MULTI_MSI: + pcidevs_lock(); ret = allocate_and_map_msi_pirq(d, *index, pirq_p, type, msi); + pcidevs_unlock(); break; default: diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c index 47c0eee7bdcc..c97dd4504a7a 100644 --- a/xen/drivers/passthrough/pci.c +++ b/xen/drivers/passthrough/pci.c @@ -750,7 +750,6 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, 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 @@ -759,18 +758,18 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, ret = vpci_add_handlers(pdev); 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; + printk(XENLOG_ERR "Setup of vPCI failed: %d\n", ret); goto out; } + write_unlock(&hardware_domain->pci_lock); ret = iommu_add_device(pdev); if ( ret ) { - vpci_remove_device(pdev); write_lock(&hardware_domain->pci_lock); + vpci_remove_device(pdev); list_del(&pdev->domain_list); write_unlock(&hardware_domain->pci_lock); pdev->domain = NULL; @@ -1146,7 +1145,9 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt, } while ( devfn != pdev->devfn && PCI_SLOT(devfn) == PCI_SLOT(pdev->devfn) ); + write_lock(&ctxt->d->pci_lock); err = vpci_add_handlers(pdev); + write_unlock(&ctxt->d->pci_lock); if ( err ) printk(XENLOG_ERR "setup of vPCI for d%d failed: %d\n", ctxt->d->domain_id, err); diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c index 58195549d50a..8f5850b8cf6d 100644 --- a/xen/drivers/vpci/header.c +++ b/xen/drivers/vpci/header.c @@ -173,6 +173,7 @@ bool vpci_process_pending(struct vcpu *v) if ( rc == -ERESTART ) return true; + write_lock(&v->domain->pci_lock); spin_lock(&v->vpci.pdev->vpci->lock); /* Disable memory decoding unconditionally on failure. */ modify_decoding(v->vpci.pdev, @@ -191,6 +192,7 @@ bool vpci_process_pending(struct vcpu *v) * failure. */ vpci_remove_device(v->vpci.pdev); + write_unlock(&v->domain->pci_lock); } return false; @@ -202,8 +204,20 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev, struct map_data data = { .d = d, .map = true }; int rc; + ASSERT(rw_is_write_locked(&d->pci_lock)); + while ( (rc = rangeset_consume_ranges(mem, map_range, &data)) == -ERESTART ) + { + /* + * It's safe to drop and reacquire the lock in this context + * without risking pdev disappearing because devices cannot be + * removed until the initial domain has been started. + */ + write_unlock(&d->pci_lock); process_pending_softirqs(); + write_lock(&d->pci_lock); + } + rangeset_destroy(mem); if ( !rc ) modify_decoding(pdev, cmd, false); @@ -244,6 +258,8 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) unsigned int i; int rc; + ASSERT(rw_is_write_locked(&pdev->domain->pci_lock)); + if ( !mem ) return -ENOMEM; @@ -524,6 +540,8 @@ static int cf_check init_header(struct pci_dev *pdev) int rc; bool mask_cap_list = false; + ASSERT(rw_is_write_locked(&pdev->domain->pci_lock)); + switch ( pci_conf_read8(pdev->sbdf, PCI_HEADER_TYPE) & 0x7f ) { case PCI_HEADER_TYPE_NORMAL: diff --git a/xen/drivers/vpci/msi.c b/xen/drivers/vpci/msi.c index a253ccbd7db7..dc71938e23f5 100644 --- a/xen/drivers/vpci/msi.c +++ b/xen/drivers/vpci/msi.c @@ -263,7 +263,7 @@ REGISTER_VPCI_INIT(init_msi, VPCI_PRIORITY_LOW); void vpci_dump_msi(void) { - const struct domain *d; + struct domain *d; rcu_read_lock(&domlist_read_lock); for_each_domain ( d ) @@ -275,6 +275,9 @@ void vpci_dump_msi(void) printk("vPCI MSI/MSI-X d%d\n", d->domain_id); + if ( !read_trylock(&d->pci_lock) ) + continue; + for_each_pdev ( d, pdev ) { const struct vpci_msi *msi; @@ -313,17 +316,36 @@ void vpci_dump_msi(void) { /* * On error vpci_msix_arch_print will always return without - * holding the lock. + * holding the locks. */ printk("unable to print all MSI-X entries: %d\n", rc); - process_pending_softirqs(); - continue; + goto pdev_done; } } + /* + * Unlock locks to process pending softirqs. This is + * potentially unsafe, as d->pdev_list can be changed in + * meantime. + */ spin_unlock(&pdev->vpci->lock); + read_unlock(&d->pci_lock); + pdev_done: process_pending_softirqs(); + if ( !read_trylock(&d->pci_lock) ) + { + printk("unable to access other devices for the domain\n"); + goto domain_done; + } } + read_unlock(&d->pci_lock); + domain_done: + /* + * We need this label at the end of the loop, but some + * compilers might not be happy about label at the end of the + * compound statement so we adding an empty statement here. + */ + ; } rcu_read_unlock(&domlist_read_lock); } diff --git a/xen/drivers/vpci/msix.c b/xen/drivers/vpci/msix.c index d1126a417da9..b6abab47efdd 100644 --- a/xen/drivers/vpci/msix.c +++ b/xen/drivers/vpci/msix.c @@ -147,6 +147,8 @@ static struct vpci_msix *msix_find(const struct domain *d, unsigned long addr) { struct vpci_msix *msix; + ASSERT(rw_is_locked(&d->pci_lock)); + list_for_each_entry ( msix, &d->arch.hvm.msix_tables, next ) { const struct vpci_bar *bars = msix->pdev->vpci->header.bars; @@ -163,7 +165,13 @@ static struct vpci_msix *msix_find(const struct domain *d, unsigned long addr) static int cf_check msix_accept(struct vcpu *v, unsigned long addr) { - return !!msix_find(v->domain, addr); + int rc; + + read_lock(&v->domain->pci_lock); + rc = !!msix_find(v->domain, addr); + read_unlock(&v->domain->pci_lock); + + return rc; } static bool access_allowed(const struct pci_dev *pdev, unsigned long addr, @@ -358,21 +366,35 @@ static int adjacent_read(const struct domain *d, const struct vpci_msix *msix, static int cf_check msix_read( struct vcpu *v, unsigned long addr, unsigned int len, unsigned long *data) { - const struct domain *d = v->domain; - struct vpci_msix *msix = msix_find(d, addr); + struct domain *d = v->domain; + struct vpci_msix *msix; const struct vpci_msix_entry *entry; unsigned int offset; *data = ~0UL; + read_lock(&d->pci_lock); + + msix = msix_find(d, addr); if ( !msix ) + { + read_unlock(&d->pci_lock); return X86EMUL_RETRY; + } if ( adjacent_handle(msix, addr) ) - return adjacent_read(d, msix, addr, len, data); + { + int rc = adjacent_read(d, msix, addr, len, data); + + read_unlock(&d->pci_lock); + return rc; + } if ( !access_allowed(msix->pdev, addr, len) ) + { + read_unlock(&d->pci_lock); return X86EMUL_OKAY; + } spin_lock(&msix->pdev->vpci->lock); entry = get_entry(msix, addr); @@ -404,6 +426,7 @@ static int cf_check msix_read( break; } spin_unlock(&msix->pdev->vpci->lock); + read_unlock(&d->pci_lock); return X86EMUL_OKAY; } @@ -491,19 +514,33 @@ static int adjacent_write(const struct domain *d, const struct vpci_msix *msix, static int cf_check msix_write( struct vcpu *v, unsigned long addr, unsigned int len, unsigned long data) { - const struct domain *d = v->domain; - struct vpci_msix *msix = msix_find(d, addr); + struct domain *d = v->domain; + struct vpci_msix *msix; struct vpci_msix_entry *entry; unsigned int offset; + read_lock(&d->pci_lock); + + msix = msix_find(d, addr); if ( !msix ) + { + read_unlock(&d->pci_lock); return X86EMUL_RETRY; + } if ( adjacent_handle(msix, addr) ) - return adjacent_write(d, msix, addr, len, data); + { + int rc = adjacent_write(d, msix, addr, len, data); + + read_unlock(&d->pci_lock); + return rc; + } if ( !access_allowed(msix->pdev, addr, len) ) + { + read_unlock(&d->pci_lock); return X86EMUL_OKAY; + } spin_lock(&msix->pdev->vpci->lock); entry = get_entry(msix, addr); @@ -579,6 +616,7 @@ static int cf_check msix_write( break; } spin_unlock(&msix->pdev->vpci->lock); + read_unlock(&d->pci_lock); return X86EMUL_OKAY; } diff --git a/xen/drivers/vpci/vpci.c b/xen/drivers/vpci/vpci.c index 72ef277c4f8e..475272b173f3 100644 --- a/xen/drivers/vpci/vpci.c +++ b/xen/drivers/vpci/vpci.c @@ -42,6 +42,8 @@ extern vpci_register_init_t *const __end_vpci_array[]; void vpci_remove_device(struct pci_dev *pdev) { + ASSERT(rw_is_write_locked(&pdev->domain->pci_lock)); + if ( !has_vpci(pdev->domain) || !pdev->vpci ) return; @@ -77,6 +79,8 @@ int vpci_add_handlers(struct pci_dev *pdev) const unsigned long *ro_map; int rc = 0; + ASSERT(rw_is_write_locked(&pdev->domain->pci_lock)); + if ( !has_vpci(pdev->domain) ) return 0; @@ -361,7 +365,7 @@ static uint32_t merge_result(uint32_t data, uint32_t new, unsigned int size, uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) { - const struct domain *d = current->domain; + struct domain *d = current->domain; const struct pci_dev *pdev; const struct vpci_register *r; unsigned int data_offset = 0; @@ -376,12 +380,18 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) /* * Find the PCI dev matching the address, which for hwdom also requires * consulting DomXEN. Passthrough everything that's not trapped. + * If this is hwdom and the device is assigned to DomXEN, acquiring hwdom's + * pci_lock is sufficient. */ + read_lock(&d->pci_lock); pdev = pci_get_pdev(d, sbdf); if ( !pdev && is_hardware_domain(d) ) pdev = pci_get_pdev(dom_xen, sbdf); if ( !pdev || !pdev->vpci ) + { + read_unlock(&d->pci_lock); return vpci_read_hw(sbdf, reg, size); + } spin_lock(&pdev->vpci->lock); @@ -428,6 +438,7 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size) ASSERT(data_offset < size); } spin_unlock(&pdev->vpci->lock); + read_unlock(&d->pci_lock); if ( data_offset < size ) { @@ -470,7 +481,7 @@ static void vpci_write_helper(const struct pci_dev *pdev, void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, uint32_t data) { - const struct domain *d = current->domain; + struct domain *d = current->domain; const struct pci_dev *pdev; const struct vpci_register *r; unsigned int data_offset = 0; @@ -484,7 +495,13 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, /* * Find the PCI dev matching the address, which for hwdom also requires * consulting DomXEN. Passthrough everything that's not trapped. + * If this is hwdom and the device is assigned to DomXEN, acquiring hwdom's + * pci_lock is sufficient. + * + * TODO: We need to take pci_locks in exclusive mode only if we + * are modifying BARs, so there is a room for improvement. */ + write_lock(&d->pci_lock); pdev = pci_get_pdev(d, sbdf); if ( !pdev && is_hardware_domain(d) ) pdev = pci_get_pdev(dom_xen, sbdf); @@ -493,6 +510,8 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, /* Ignore writes to read-only devices, which have no ->vpci. */ const unsigned long *ro_map = pci_get_ro_map(sbdf.seg); + write_unlock(&d->pci_lock); + if ( !ro_map || !test_bit(sbdf.bdf, ro_map) ) vpci_write_hw(sbdf, reg, size, data); return; @@ -534,6 +553,7 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, ASSERT(data_offset < size); } spin_unlock(&pdev->vpci->lock); + write_unlock(&d->pci_lock); if ( data_offset < size ) /* Tailing gap, write the remaining. */ diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 9da91e0e6244..c3adec1aca3c 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -462,7 +462,8 @@ struct domain #ifdef CONFIG_HAS_PCI struct list_head pdev_list; /* - * pci_lock protects access to pdev_list. + * pci_lock protects access to pdev_list. pci_lock also protects pdev->vpci + * structure from being removed. * * Any user *reading* from pdev_list, or from devices stored in pdev_list, * should hold either pcidevs_lock() or pci_lock in read mode. Optionally, @@ -628,6 +629,18 @@ struct domain unsigned int cdf; }; +/* + * Check for use in ASSERTs to ensure that: + * 1. we can *read* d->pdev_list + * 2. pdevs (belonging to this domain) do not go away + * 3. pdevs (belonging to this domain) do not get assigned to other domains + * This check is not suitable for protecting other state or critical regions. + */ +#define pdev_list_is_read_locked(d) ({ \ + struct domain *d_ = (d); \ + pcidevs_locked() || (d_ && rw_is_locked(&d_->pci_lock)); \ + }) + static inline struct page_list_head *page_to_list( struct domain *d, const struct page_info *pg) { From patchwork Fri Feb 2 21:33:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543456 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 BFB32C4828F for ; Fri, 2 Feb 2024 21:34:32 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675210.1050490 (Exim 4.92) (envelope-from ) id 1rW1B3-0004cB-D0; Fri, 02 Feb 2024 21:34:25 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675210.1050490; Fri, 02 Feb 2024 21:34:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1B3-0004c4-9O; Fri, 02 Feb 2024 21:34:25 +0000 Received: by outflank-mailman (input) for mailman id 675210; Fri, 02 Feb 2024 21:34:24 +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 1rW1B2-0003tJ-ND for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:34:24 +0000 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on20600.outbound.protection.outlook.com [2a01:111:f403:2412::600]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id d46e9809-c212-11ee-8a45-1f161083a0e0; Fri, 02 Feb 2024 22:34:24 +0100 (CET) Received: from CY5PR15CA0096.namprd15.prod.outlook.com (2603:10b6:930:7::11) by IA0PR12MB8304.namprd12.prod.outlook.com (2603:10b6:208:3dc::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.22; Fri, 2 Feb 2024 21:34:18 +0000 Received: from CY4PEPF0000E9DA.namprd05.prod.outlook.com (2603:10b6:930:7:cafe::28) by CY5PR15CA0096.outlook.office365.com (2603:10b6:930:7::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.30 via Frontend Transport; Fri, 2 Feb 2024 21:34:18 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by CY4PEPF0000E9DA.mail.protection.outlook.com (10.167.241.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7249.19 via Frontend Transport; Fri, 2 Feb 2024 21:34:17 +0000 Received: from SATLEXMB08.amd.com (10.181.40.132) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:34:17 -0600 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB08.amd.com (10.181.40.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32; Fri, 2 Feb 2024 13:34:17 -0800 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:34:16 -0600 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: d46e9809-c212-11ee-8a45-1f161083a0e0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gQwFfosgWnZmhUB4LiYM9HAJhM8YvsX3Mw1YVghC3BjWiiE+ZjV0U994eZpHu8MNQ0bOjWAOEjIr2vHoHJIvRQosxAOw2EqMH/XmyWihGNFxyTClFBjg0336ODgJQcQSnVq0LlCFehh8eFP34boG7pyWph72At6gNd0wiJtTe+S6FCrvw471TZMXGl0KFeTyivpQy2JPDuvaII04YnT/FLnHVkYz9vFI3VBhiht/RhynpvhYc15zHpbMp0D9nkzZKqJz+rnxdm/bmQhmR3b9CiwnLhVBaWmqx+pMXdtN+rwh62jCZL0PCqWjF8ChpTh5mWR6Dv77f3nT8N9Z35qzsw== 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=ibej2hdMvib6q8x4h3ru+hAP1vQUr+0gj5LhSbls42E=; b=W7AJGwzqrgRZNT17JmXWlx59PI/xEd1a5BHIYD3Ad1AKx0V/0eKJeASCMPCYZfuonu9kVGrbXvjRZX8hhXbdBlhlhwBKU3m2dWYMZW2GHkxIUvVrmRM2acAYPWg2VhkwAbGqkhwYdmza6ZGWxl4+/cLDRqMRdSUhIc9hYUyXtniHeBhMchcjwnHnbUwRK60Shw/bNb+ocSqY7D0Nvkz6XjoZOxZjZTqWUkHxbVru2qZKRXzNcsS07X9hxeDjHC5yurb/8NosWd91oXcWrVOB64atbi4l9lXwreyUOteEeZ9XCgWNSja12ZJdFOi/EN6I3dVIHzGYeErfIfTvBt8C/Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ibej2hdMvib6q8x4h3ru+hAP1vQUr+0gj5LhSbls42E=; b=FT3C3ykmAW7vPaoEfBIJ0CkrBY25zzz4/W060pUvLi0IJ3gmLSv0U7yCFx32eVfipDcrf+3+qN6GobFeQa/EOzDq8X0GlLjtvXZZe15CKUHxxpEfx/djCG7slRuOGAXPxBMQ8sjR9uX0eePUYcUIF9ss0z/1h80eCRdj2i3F6ok= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Stewart Hildebrand To: CC: Oleksandr Andrushchenko , =?utf-8?q?Ro?= =?utf-8?q?ger_Pau_Monn=C3=A9?= , "Volodymyr Babchuk" , Stewart Hildebrand Subject: [PATCH v13 02/14] vpci: restrict unhandled read/write operations for guests Date: Fri, 2 Feb 2024 16:33:06 -0500 Message-ID: <20240202213321.1920347-3-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9DA:EE_|IA0PR12MB8304:EE_ X-MS-Office365-Filtering-Correlation-Id: a9963615-368f-4bb0-0a57-08dc2436b5f9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JR3gm5O71QtrN9EeQJHiO2C4BzhNccb4XfG+9JqNRjBuUxXH92Tzf0r46OC/hIM8X2TgNzv/7HTWNJyysF1K5fZLTt15EB1UOEtqnLciihyZebkELEb3cHCvK8F9RNxa00fd9Iv0Zs1aBhv7MiJUEWFpNrx7x4Q7EUmllF0gJTPkYtoB2bWVyWBSY0B9KUyzu9c6m9J74MwSXcCL/Lsb7Z48qUxCcvjxLJvbdMiMZ/8US38PBVd2O5Y3+O5zmOAS9zLOHGTZFe/aGgYLe3py7SOnhIjA6g1QMOrj3kxSA93gJQSz0M/vSBVjkGJFZReaQ1XrlOonvIzxwj34H/+irm7rWoetnzlaUDRgrv2qckuOYu9WiS0z9jN4eWSGR0g8lB1KxIS3jVUgnJxhiUOUo+qiMobPr/gJ8JrhmBiTNSnpnaRKdbZNC7sWiZdhPnudzed1A5rkgj/GtOy0MGMZPXzwa38/YpP1VPC1hwwtuO7KshK/sB1fADqJxFTfl/X2Fbmn0AbcDobXYqNkTScGR+deBLTiMZPvXkRlvIptuPIHTcraKS/WZNVEgXFlXX3E2HgMJXyjxVjkORzzx1fUGSQmyr7SFSvV80nXe2n8niGI4kwoRvT1KEnZzUnOVD3LeorsT/thDOGRP7buvZVb630t42otaUxzp+iZ1Gz8foIQzWbu6rpBiOUx0oJNVEzMUtc5WfGnZi9hB1pd4xp5OLbk1WkF52uLnw2zSGEEOjN6LFzsA9QSlF0KkJS46Gu7ysroVT72woTQVGpdow/fSA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(136003)(396003)(376002)(39860400002)(346002)(230922051799003)(186009)(82310400011)(64100799003)(1800799012)(451199024)(46966006)(40470700004)(36840700001)(40480700001)(40460700003)(83380400001)(41300700001)(86362001)(36756003)(26005)(36860700001)(426003)(356005)(81166007)(54906003)(6916009)(336012)(2616005)(1076003)(70586007)(47076005)(44832011)(2906002)(5660300002)(8676002)(4326008)(6666004)(316002)(82740400003)(70206006)(478600001)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:34:17.8661 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a9963615-368f-4bb0-0a57-08dc2436b5f9 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9DA.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8304 From: Oleksandr Andrushchenko A guest would be able to read and write those registers which are not emulated and have no respective vPCI handlers, so it will be possible for it to access the hardware directly. In order to prevent a guest from reads and writes from/to the unhandled registers make sure only hardware domain can access the hardware directly and restrict guests from doing so. Suggested-by: Roger Pau Monné Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Volodymyr Babchuk Reviewed-by: Roger Pau Monné Signed-off-by: Stewart Hildebrand --- Since v9: - removed stray formatting change - added Roger's R-b tag Since v6: - do not use is_hwdom parameter for vpci_{read|write}_hw and use current->domain internally - update commit message New in v6 --- xen/drivers/vpci/vpci.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/xen/drivers/vpci/vpci.c b/xen/drivers/vpci/vpci.c index 475272b173f3..d545dc633c40 100644 --- a/xen/drivers/vpci/vpci.c +++ b/xen/drivers/vpci/vpci.c @@ -268,6 +268,10 @@ static uint32_t vpci_read_hw(pci_sbdf_t sbdf, unsigned int reg, { uint32_t data; + /* Guest domains are not allowed to read real hardware. */ + if ( !is_hardware_domain(current->domain) ) + return ~(uint32_t)0; + switch ( size ) { case 4: @@ -311,6 +315,10 @@ static uint32_t vpci_read_hw(pci_sbdf_t sbdf, unsigned int reg, static void vpci_write_hw(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, uint32_t data) { + /* Guest domains are not allowed to write real hardware. */ + if ( !is_hardware_domain(current->domain) ) + return; + switch ( size ) { case 4: From patchwork Fri Feb 2 21:33:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543457 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 BEC06C4828F for ; Fri, 2 Feb 2024 21:34:43 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675213.1050499 (Exim 4.92) (envelope-from ) id 1rW1BD-000562-LB; Fri, 02 Feb 2024 21:34:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675213.1050499; Fri, 02 Feb 2024 21:34: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 1rW1BD-00055v-Ib; Fri, 02 Feb 2024 21:34:35 +0000 Received: by outflank-mailman (input) for mailman id 675213; Fri, 02 Feb 2024 21:34:34 +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 1rW1BC-0003tJ-77 for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:34:34 +0000 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on20601.outbound.protection.outlook.com [2a01:111:f403:2409::601]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id dae8aeb1-c212-11ee-8a45-1f161083a0e0; Fri, 02 Feb 2024 22:34:33 +0100 (CET) Received: from BYAPR03CA0001.namprd03.prod.outlook.com (2603:10b6:a02:a8::14) by MW5PR12MB5600.namprd12.prod.outlook.com (2603:10b6:303:195::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7270.9; Fri, 2 Feb 2024 21:34:30 +0000 Received: from SJ1PEPF00001CDC.namprd05.prod.outlook.com (2603:10b6:a02:a8:cafe::2f) by BYAPR03CA0001.outlook.office365.com (2603:10b6:a02:a8::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.23 via Frontend Transport; Fri, 2 Feb 2024 21:34:29 +0000 Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CDC.mail.protection.outlook.com (10.167.242.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7202.16 via Frontend Transport; Fri, 2 Feb 2024 21:34:29 +0000 Received: from SATLEXMB08.amd.com (10.181.40.132) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:34:28 -0600 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB08.amd.com (10.181.40.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32; Fri, 2 Feb 2024 13:34:28 -0800 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:34:27 -0600 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: dae8aeb1-c212-11ee-8a45-1f161083a0e0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k1IV2fkW4MlwOgrgGzh8fEIhwjZP2sJrnLPO7NpcwlKavBVUBIp9/ewqlF+Rxztd2ShM9SjpnKPlNAnvCC/zrruphJzNM9PYS9fBzVqEYSwYlk46nbTXuYBDcOoWnM6qyExXx346dbZKGhoWS52O6qNuBfc2UqpIH+vwjHIv71gv3cLNbXE+Di771e8QzRHE9im7Ab3bfbMtHfd8bhyVBBUS6S2VYhZT9xdActlDZFKZQQyHkuKhSsWEjrZJth+I8VoHSEGfdNNcU+zD4+Qggn6sr8QcY6qAkGfqqu4IUJPF42wUC+s0SGuPhixmfaSnyJr1aLTGN+NyChVa+KsqtQ== 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=NWmHF1bTVpBmU7drHJ9mWbFr611+spd24HiT6koGFoM=; b=C6tx3ucDy0C7nxCqI/LNmR/HZSgUuXc+wfrSxJ8kT/pXiTCkKhopcXI5XockOn/bNzyQDDq/+bYBo47QvXhm75SiAiKzfk6DqaszaeNwakH1DAQpU900g8LI7GZEGEtPjx4QpeKNUAZg4KVrrePRGSVZ5dGAmI7AEjfRaazbDO8NFFkaKPVpzSLh/EcHDs2cIbNr5T68TQ9pChYkxaC5croqYGfD+xEiUfNM5sQkiRkthhMr5+XRc/kZc4OEmDLRe8weLoOu0YyAP6pIpOOvnuAa0NeR2hka9ar4Y5Pv8CZ01BND99FN/fUTkz1jQaffecVkYV6r7ikQgqYT0T60MA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=NWmHF1bTVpBmU7drHJ9mWbFr611+spd24HiT6koGFoM=; b=Yg1j149sHsTJl26CvEuTs9SfJCSNb49Klj79OmsEJsqEl1GppmhTD+8zC+qcwPMYlU0g1ds0WSfIk3Mvjn/1fAUZeX5549xWQfQ7WcDBU9ItifTmvMTgsjWsww7+rkWJ2QnmPg1e4nWsLfUBq/GQU/sdAgs0IH4qT6fIMlgCfEM= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C From: Stewart Hildebrand To: CC: Oleksandr Andrushchenko , Jan Beulich , Paul Durrant , =?utf-8?q?Rog?= =?utf-8?q?er_Pau_Monn=C3=A9?= , "Volodymyr Babchuk" , Stewart Hildebrand Subject: [PATCH v13 03/14] vpci: add hooks for PCI device assign/de-assign Date: Fri, 2 Feb 2024 16:33:07 -0500 Message-ID: <20240202213321.1920347-4-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CDC:EE_|MW5PR12MB5600:EE_ X-MS-Office365-Filtering-Correlation-Id: bc76e669-0c30-490a-0edc-08dc2436bcd8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 26CNyWiHFkKUwHCJUuvydOTrQInd9iUThSL+uq7k910Yh1Pf2BN7NaaAoFu1zbu8O/UrsAoPbBkh05Pt4uK3X7xNlCzURkOKL+JuiGWeY9RsJti0xxQeS4bGUKyNA87jKCsI1prAKTwlNTrh1kCVxP9eGWO6ZdAkh7su6Qhdxyr/O0Cr4N89a2nXp+GNIdhvD2sXXOZSkVU/6bnwC2nOTsW4sqJmV++P2GVlFMDcYPYm86YmVw6xFK+odCcGrwEBhqArnNnb41mnXFVoVlUWvTrP1zAze/bhVNy16H443O5Kz9a45jqtHI0BzyGl/jjNh7hojsvsXg3HFyvBy7/n+Ss24/RZ3/Ymlmz67iHAHbgLRQf8UErnPKJbMpO2rxH006zfF5csn6HsjlSlYZ1NqKwiiCvhNHCsQPPfJppUorBni/d9NQglrVoFFLSxuNd78CNz5fcpS4AY+Gfceb841A3rUYgDR1U1ZN85exqw6srSCmL5j7ArFUMOJhHyx+5LdUlHlffHTeDggxnLP77YBeQBSVD4cAGwBIIbK8z1rW9v/M5JZDFe3kj7HE+Jfqez5Hxk0LFyBntK84sVCwNxeuvZo5RSMJzWeDd77nVhk3zIO5SJr0uDSaUTT/TK50/hQa1jFb4ZWCCCquQmwa/JzLLxUO5Gt63w9GsMc8bwDc3xm0qj9krtIJ6AGW6cvNzR28uVVeMfHhXol/nfkL8K8LpYiQ+9QQ/ZqJqy/pevQtlWBNdvKj6DboBd+y99ckIBxzqifcDpZTxUft1GVgP1zg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(136003)(376002)(396003)(346002)(39860400002)(230922051799003)(82310400011)(64100799003)(186009)(451199024)(1800799012)(36840700001)(40470700004)(46966006)(40460700003)(40480700001)(36756003)(86362001)(478600001)(356005)(426003)(8936002)(82740400003)(1076003)(336012)(2616005)(41300700001)(81166007)(26005)(83380400001)(47076005)(44832011)(54906003)(36860700001)(6916009)(5660300002)(2906002)(6666004)(316002)(4326008)(70206006)(70586007)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:34:29.3627 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bc76e669-0c30-490a-0edc-08dc2436bcd8 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CDC.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW5PR12MB5600 From: Oleksandr Andrushchenko When a PCI device gets assigned/de-assigned we need to initialize/de-initialize vPCI state for the device. Also, rename vpci_add_handlers() to vpci_assign_device() and vpci_remove_device() to vpci_deassign_device() to better reflect role of the functions. Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Volodymyr Babchuk Reviewed-by: Roger Pau Monné Signed-off-by: Stewart Hildebrand Acked-by: Jan Beulich --- In v13: - Add Jan's A-b - Rebase on cb4ecb3cc17b ("pci: fail device assignment if phantom functions cannot be assigned") and add if ( rc ) goto done; in assign_device() In v12: - Add Roger's R-b - Clean up comment in xen/include/xen/vpci.h - Add comment in xen/drivers/passthrough/pci.c:deassign_device() to clarify vpci_assign_device() call In v11: - Call vpci_assign_device() in "deassign_device" if IOMMU call "reassign_device" was successful. In v10: - removed HAS_VPCI_GUEST_SUPPORT checks - HAS_VPCI_GUEST_SUPPORT config option (in Kconfig) as it is not used anywhere In v9: - removed previous vpci_[de]assign_device function and renamed existing handlers - dropped attempts to handle errors in assign_device() function - do not call vpci_assign_device for dom_io - use d instead of pdev->domain - use IS_ENABLED macro In v8: - removed vpci_deassign_device In v6: - do not pass struct domain to vpci_{assign|deassign}_device as pdev->domain can be used - do not leave the device assigned (pdev->domain == new domain) in case vpci_assign_device fails: try to de-assign and if this also fails, then crash the domain In v5: - do not split code into run_vpci_init - do not check for is_system_domain in vpci_{de}assign_device - do not use vpci_remove_device_handlers_locked and re-allocate pdev->vpci completely - make vpci_deassign_device void In v4: - de-assign vPCI from the previous domain on device assignment - do not remove handlers in vpci_assign_device as those must not exist at that point In v3: - remove toolstack roll-back description from the commit message as error are to be handled with proper cleanup in Xen itself - remove __must_check - remove redundant rc check while assigning devices - fix redundant CONFIG_HAS_VPCI check for CONFIG_HAS_VPCI_GUEST_SUPPORT - use REGISTER_VPCI_INIT machinery to run required steps on device init/assign: add run_vpci_init helper In v2: - define CONFIG_HAS_VPCI_GUEST_SUPPORT so dead code is not compiled for x86 In v1: - constify struct pci_dev where possible - do not open code is_system_domain() - extended the commit message --- xen/drivers/passthrough/pci.c | 28 ++++++++++++++++++++++++---- xen/drivers/vpci/header.c | 2 +- xen/drivers/vpci/vpci.c | 6 +++--- xen/include/xen/vpci.h | 10 +++++----- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c index c97dd4504a7a..4c0a836486ec 100644 --- a/xen/drivers/passthrough/pci.c +++ b/xen/drivers/passthrough/pci.c @@ -755,7 +755,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, * For devices not discovered by Xen during boot, add vPCI handlers * when Dom0 first informs Xen about such devices. */ - ret = vpci_add_handlers(pdev); + ret = vpci_assign_device(pdev); if ( ret ) { list_del(&pdev->domain_list); @@ -769,7 +769,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, if ( ret ) { write_lock(&hardware_domain->pci_lock); - vpci_remove_device(pdev); + vpci_deassign_device(pdev); list_del(&pdev->domain_list); write_unlock(&hardware_domain->pci_lock); pdev->domain = NULL; @@ -817,7 +817,7 @@ 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 ) { - vpci_remove_device(pdev); + vpci_deassign_device(pdev); pci_cleanup_msi(pdev); ret = iommu_remove_device(pdev); if ( pdev->domain ) @@ -875,6 +875,10 @@ static int deassign_device(struct domain *d, uint16_t seg, uint8_t bus, goto out; } + write_lock(&d->pci_lock); + vpci_deassign_device(pdev); + write_unlock(&d->pci_lock); + devfn = pdev->devfn; ret = iommu_call(hd->platform_ops, reassign_device, d, target, devfn, pci_to_dev(pdev)); @@ -886,6 +890,11 @@ static int deassign_device(struct domain *d, uint16_t seg, uint8_t bus, pdev->fault.count = 0; + write_lock(&target->pci_lock); + /* Re-assign back to hardware_domain */ + ret = vpci_assign_device(pdev); + write_unlock(&target->pci_lock); + out: if ( ret ) printk(XENLOG_G_ERR "%pd: deassign (%pp) failed (%d)\n", @@ -1146,7 +1155,7 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt, PCI_SLOT(devfn) == PCI_SLOT(pdev->devfn) ); write_lock(&ctxt->d->pci_lock); - err = vpci_add_handlers(pdev); + err = vpci_assign_device(pdev); write_unlock(&ctxt->d->pci_lock); if ( err ) printk(XENLOG_ERR "setup of vPCI for d%d failed: %d\n", @@ -1476,6 +1485,10 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn, u32 flag) if ( pdev->broken && d != hardware_domain && d != dom_io ) goto done; + write_lock(&pdev->domain->pci_lock); + vpci_deassign_device(pdev); + write_unlock(&pdev->domain->pci_lock); + rc = pdev_msix_assign(d, pdev); if ( rc ) goto done; @@ -1501,6 +1514,13 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn, u32 flag) pci_to_dev(pdev), flag); } + if ( rc ) + goto done; + + write_lock(&d->pci_lock); + rc = vpci_assign_device(pdev); + write_unlock(&d->pci_lock); + done: if ( rc ) { diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c index 8f5850b8cf6d..2f2d98ada012 100644 --- a/xen/drivers/vpci/header.c +++ b/xen/drivers/vpci/header.c @@ -191,7 +191,7 @@ bool vpci_process_pending(struct vcpu *v) * killed in order to avoid leaking stale p2m mappings on * failure. */ - vpci_remove_device(v->vpci.pdev); + vpci_deassign_device(v->vpci.pdev); write_unlock(&v->domain->pci_lock); } diff --git a/xen/drivers/vpci/vpci.c b/xen/drivers/vpci/vpci.c index d545dc633c40..ff4837391786 100644 --- a/xen/drivers/vpci/vpci.c +++ b/xen/drivers/vpci/vpci.c @@ -40,7 +40,7 @@ extern vpci_register_init_t *const __start_vpci_array[]; extern vpci_register_init_t *const __end_vpci_array[]; #define NUM_VPCI_INIT (__end_vpci_array - __start_vpci_array) -void vpci_remove_device(struct pci_dev *pdev) +void vpci_deassign_device(struct pci_dev *pdev) { ASSERT(rw_is_write_locked(&pdev->domain->pci_lock)); @@ -73,7 +73,7 @@ void vpci_remove_device(struct pci_dev *pdev) pdev->vpci = NULL; } -int vpci_add_handlers(struct pci_dev *pdev) +int vpci_assign_device(struct pci_dev *pdev) { unsigned int i; const unsigned long *ro_map; @@ -107,7 +107,7 @@ int vpci_add_handlers(struct pci_dev *pdev) } if ( rc ) - vpci_remove_device(pdev); + vpci_deassign_device(pdev); return rc; } diff --git a/xen/include/xen/vpci.h b/xen/include/xen/vpci.h index d20c301a3db3..99fe76f08ace 100644 --- a/xen/include/xen/vpci.h +++ b/xen/include/xen/vpci.h @@ -25,11 +25,11 @@ typedef int vpci_register_init_t(struct pci_dev *dev); static vpci_register_init_t *const x##_entry \ __used_section(".data.vpci." p) = x -/* Add vPCI handlers to device. */ -int __must_check vpci_add_handlers(struct pci_dev *pdev); +/* Assign vPCI to device by adding handlers. */ +int __must_check vpci_assign_device(struct pci_dev *pdev); /* Remove all handlers and free vpci related structures. */ -void vpci_remove_device(struct pci_dev *pdev); +void vpci_deassign_device(struct pci_dev *pdev); /* Add/remove a register handler. */ int __must_check vpci_add_register_mask(struct vpci *vpci, @@ -255,12 +255,12 @@ bool vpci_ecam_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int len, #else /* !CONFIG_HAS_VPCI */ struct vpci_vcpu {}; -static inline int vpci_add_handlers(struct pci_dev *pdev) +static inline int vpci_assign_device(struct pci_dev *pdev) { return 0; } -static inline void vpci_remove_device(struct pci_dev *pdev) { } +static inline void vpci_deassign_device(struct pci_dev *pdev) { } static inline void vpci_dump_msi(void) { } From patchwork Fri Feb 2 21:33:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543458 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 0F9C8C4828E for ; Fri, 2 Feb 2024 21:35:02 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675216.1050510 (Exim 4.92) (envelope-from ) id 1rW1BX-0005pJ-Vp; Fri, 02 Feb 2024 21:34:55 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675216.1050510; Fri, 02 Feb 2024 21:34:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1BX-0005pC-Rf; Fri, 02 Feb 2024 21:34:55 +0000 Received: by outflank-mailman (input) for mailman id 675216; Fri, 02 Feb 2024 21:34:54 +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 1rW1BW-0004Ae-4G for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:34:54 +0000 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on20601.outbound.protection.outlook.com [2a01:111:f403:2412::601]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id e56c383d-c212-11ee-98f5-efadbce2ee36; Fri, 02 Feb 2024 22:34:52 +0100 (CET) Received: from BY3PR05CA0011.namprd05.prod.outlook.com (2603:10b6:a03:254::16) by IA1PR12MB8538.namprd12.prod.outlook.com (2603:10b6:208:455::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.31; Fri, 2 Feb 2024 21:34:46 +0000 Received: from SJ1PEPF00001CE0.namprd05.prod.outlook.com (2603:10b6:a03:254:cafe::d9) by BY3PR05CA0011.outlook.office365.com (2603:10b6:a03:254::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.22 via Frontend Transport; Fri, 2 Feb 2024 21:34:46 +0000 Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CE0.mail.protection.outlook.com (10.167.242.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7202.16 via Frontend Transport; Fri, 2 Feb 2024 21:34:45 +0000 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:34:44 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:34:38 -0600 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: e56c383d-c212-11ee-98f5-efadbce2ee36 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IYxAPDVpiu3L0g27UXvfFoylhEbrW5YA12f7RZDgiUpmXE++LL+vqScpw9813n1J9x6u0Zpc416QwxFxlDgAki9LfurMdQojXtC/2VOFkOY7mbAFqMkIGd5Zv8GHDT2gZfAlDwl610GnH2Ue7WLwOSdB+yIlmXCXbvSfIKUxFCRWuWq2MhudgnH2FLA/7gNJhmyuwTSdLKqZ7rShKCszihaHEcbvxxpWt+cEYliV3k5/KJo0mbcK02zj6GjUDPZH45lNGU98EFW4SjOeiu0uHLEGkVjYvwMX06QFZdyDNLXw2IuZ76x4TQzdXlfYw7c1lo/G+6BgLpfwgk2dhuq8JQ== 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=xyiyI3xsKrX/cnHhNKfE69AhVBubvl5XKQMwzn7dWtQ=; b=OAB+PaOIGfo6v7aGYnYFcZIxCz93zKegteOCTUKtvEQdO9SkU7qIKXg5SJvMvGBXGdoEwc7KBWFX+djcSu1XeXQa4QocQMIjtCOwLWMGFyFhgS0WYGtr5RtDcuHmi/Knw3pntAQnCSQ7SvZoM2NXKRAVKJs7AnuBaEiDXBRz/I3KlBdqian6ms2TE6yBpv/qZBrlc6MEpYtoLDgMfP7lC9xmVu51qeB8odIV70z7auwYxWaP5DUdqAOPYS8u9XqtmDUH32ZvBnytXN5ogcZf38aXYGWbX+UmIj+ZcLewRr9FJ7j3Cm73djceg3EiqzEH4CLdXMGqrx5DtCcdhWzBQQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xyiyI3xsKrX/cnHhNKfE69AhVBubvl5XKQMwzn7dWtQ=; b=uDtqpQb/VaXETn246NN0JRJyTTvuJ5fAfdsCYKKPhSNniYLqH5GzUhFC4FwGGeTvDKv93FR7X3j4kdHbBxmO1tPX2Kj/qZY2Upg8D9PmKuZ81kIBDBHWBgWzLWnzjxkdVK5HjWg9dMTICsh6XjYTYV/ioOeluxu957pKIiATDrQ= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C From: Stewart Hildebrand To: CC: Volodymyr Babchuk , =?utf-8?q?Roger_Pau_Monn?= =?utf-8?q?=C3=A9?= , "Volodymyr Babchuk" , Stewart Hildebrand Subject: [PATCH v13 04/14] vpci/header: rework exit path in init_header() Date: Fri, 2 Feb 2024 16:33:08 -0500 Message-ID: <20240202213321.1920347-5-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CE0:EE_|IA1PR12MB8538:EE_ X-MS-Office365-Filtering-Correlation-Id: a80af3f4-acae-4c13-479e-08dc2436c68b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WSVPJi+gJuL0p7fuaFgh2pUAQxR1q+zSDOX14sKPtYT9STdwb1haXULoO0MxVrNvTmSl7XJX+VYKjhgHXmtyEjKJIWkNFO38IqR9unjkLgmceh0+o7nPyHvnZt4MMHkseOjj2eoABDYJurjF7HtFCBfFhOAdnL116KhIoYnwxjNzbcyzUnD85LJBZIVlryvyzLAl1K8JXF3ri7S1vCZSpWZZ01cgNLmdcFwUP41+YoCtz/donRhqlMV8rpmRBjImUkDPrr7M7T75D8fWeVNMdbMNb5AfvcYF9i+z9FxSundpSzxX3KsbXr0Kl8GjKVDwKCFbFJCLkRppBh13a4uGi2m79fg/6LXFwwsypAyslXCDGVcIO96a0uYMPTJ02+34WSLxHUQNp9vYTTLkGoSGz2jLAXTrM0PrtwYVsrzxswSvpoQyh0Jp3sslALJnDs2yEmaWJ0CBXLfDIcWjn14WYIoqHEKSHHrK/8IlZb7Lf3A9TpVgmHH5qYp2dne5KO+oNvoJgLrHCVU9g6avsr5/B/dsRHNHBxWazJ8ENEJ7C0ujJRe1YP6TzRpVsuY5XvkYB/7quK3LEmZulbkIMLVPY3UnhIbDHV8n3YLs5KDZoetsHXXFZ4lp5nXzbqUxHF6T47EuUvbmnmt/RQ7IcgrWNu/t2uzP2RZHllZxyrmgEucHnUBEe0pLA7ZfA8OOegxYwmWWKpwcgBFFrXwuLcPhT+vy6EZ65x+9iw9C/iux+fVe0jicGO8SAEl9RGSL0hyVVUCBF/d+rzNj1+9GMNbcgg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(396003)(376002)(136003)(39860400002)(346002)(230922051799003)(186009)(1800799012)(64100799003)(82310400011)(451199024)(40470700004)(36840700001)(46966006)(8676002)(426003)(336012)(2616005)(4326008)(1076003)(26005)(5660300002)(478600001)(41300700001)(6916009)(8936002)(36756003)(316002)(44832011)(86362001)(54906003)(70206006)(70586007)(6666004)(2906002)(82740400003)(81166007)(47076005)(36860700001)(356005)(83380400001)(40480700001)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:34:45.6368 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a80af3f4-acae-4c13-479e-08dc2436c68b X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CE0.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8538 From: Volodymyr Babchuk Introduce "fail" label in init_header() function to have the centralized error return path. This is the pre-requirement for the future changes in this function. This patch does not introduce functional changes. Suggested-by: Roger Pau Monné Signed-off-by: Volodymyr Babchuk Acked-by: Roger Pau Monné Signed-off-by: Stewart Hildebrand --- In v12: - s/init_bars/init_header/ - Re-order tags - Fixup scissors line In v11: - Do not remove empty line between "goto fail;" and "continue;" In v10: - Added Roger's A-b tag. In v9: - New in v9 --- xen/drivers/vpci/header.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c index 2f2d98ada012..803fe4bb99a6 100644 --- a/xen/drivers/vpci/header.c +++ b/xen/drivers/vpci/header.c @@ -656,10 +656,7 @@ static int cf_check init_header(struct pci_dev *pdev) rc = vpci_add_register(pdev->vpci, vpci_hw_read32, bar_write, reg, 4, &bars[i]); if ( rc ) - { - pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd); - return rc; - } + goto fail; continue; } @@ -679,10 +676,7 @@ static int cf_check init_header(struct pci_dev *pdev) rc = pci_size_mem_bar(pdev->sbdf, reg, &addr, &size, (i == num_bars - 1) ? PCI_BAR_LAST : 0); if ( rc < 0 ) - { - pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd); - return rc; - } + goto fail; if ( size == 0 ) { @@ -697,10 +691,7 @@ static int cf_check init_header(struct pci_dev *pdev) rc = vpci_add_register(pdev->vpci, vpci_hw_read32, bar_write, reg, 4, &bars[i]); if ( rc ) - { - pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd); - return rc; - } + goto fail; } /* Check expansion ROM. */ @@ -722,6 +713,10 @@ static int cf_check init_header(struct pci_dev *pdev) } return (cmd & PCI_COMMAND_MEMORY) ? modify_bars(pdev, cmd, false) : 0; + + fail: + pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd); + return rc; } REGISTER_VPCI_INIT(init_header, VPCI_PRIORITY_MIDDLE); From patchwork Fri Feb 2 21:33:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543459 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 C7516C4828F for ; Fri, 2 Feb 2024 21:35:09 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675218.1050520 (Exim 4.92) (envelope-from ) id 1rW1Be-0006Ac-BP; Fri, 02 Feb 2024 21:35:02 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675218.1050520; Fri, 02 Feb 2024 21:35:02 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1Be-0006AV-8W; Fri, 02 Feb 2024 21:35:02 +0000 Received: by outflank-mailman (input) for mailman id 675218; Fri, 02 Feb 2024 21:35:01 +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 1rW1Bd-0003tJ-Hr for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:35:01 +0000 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on20601.outbound.protection.outlook.com [2a01:111:f403:2417::601]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id ea8d533f-c212-11ee-8a45-1f161083a0e0; Fri, 02 Feb 2024 22:35:00 +0100 (CET) Received: from DS7PR06CA0011.namprd06.prod.outlook.com (2603:10b6:8:2a::6) by DM6PR12MB4265.namprd12.prod.outlook.com (2603:10b6:5:211::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7270.12; Fri, 2 Feb 2024 21:34:57 +0000 Received: from CY4PEPF0000E9D9.namprd05.prod.outlook.com (2603:10b6:8:2a:cafe::d5) by DS7PR06CA0011.outlook.office365.com (2603:10b6:8:2a::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.31 via Frontend Transport; Fri, 2 Feb 2024 21:34:57 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by CY4PEPF0000E9D9.mail.protection.outlook.com (10.167.241.77) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7249.19 via Frontend Transport; Fri, 2 Feb 2024 21:34:56 +0000 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:34:56 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:34:55 -0600 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: ea8d533f-c212-11ee-8a45-1f161083a0e0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PjJQLFq2KtvqVaN/Tm2Ex8k70gamTp6bwQQWhrzQPMbFuyzFxrU6rex1iFGnPWJqiByAZaRCjiFDnjJjtKAIWevOcMImk9bMcGiQPpX0P/WfltgxWlnK80L21dNRR2MsN2DWRQm4B6im0t+q5P8s3aVx1q7YAvUaIsNxaY6g9748K+6gxTFYDrknGGHzdWyoW1W1iAlmDjqQHra9I3Y2vg5YDXQreEXvS3C1CpS2bj9EdpFLOK9nOm8WtDLbYS50wE19rzpQRI+zSVHkOw4SWhHEgX+g/eX6MFscyEIP1miVJ4Acur6nzYL7+YoZOA4jT1gxLf24zO4n4QwjqOqTXA== 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=3fv9DTE0P0KLmwQzPHqaJ/yxioeZbVVsJLRZmvugJrA=; b=cwd9UEcWrsXAEAap3OK6GoReAC7oF3fzD0SzB6/yhnur054AYY37dBLyJsJOnSgBz+YbV7yLzFiXfU9ELuRc8sOgoqv/RtFiOPFSIdZMhxgVJJd0QDgexQdglaEqwTVqzy4jn++nqFhPlGuOq/zO/7lSbrxf9W5wlL4XbL6jZdiwmXw2ZeSoTLRq70BKvopcD8hrFzEDooAYJNzi/9nbA3DZXN5ivYcLMxLHWTuoc6G3bOM6NLITf57exlbRAAUT6bEHuWpXwndJ7B5aqM5iZDeMYcQ5nvl7XOXCkzcjVaZ5YKIONZbt8Xnw5cclks21ytdxHOWQEZ8JYxEMSa5Tbw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3fv9DTE0P0KLmwQzPHqaJ/yxioeZbVVsJLRZmvugJrA=; b=kKFkQXJVa2as7Kwh/DiORASGaAjUdCbXawJE2QeonZBgPCuqid3OynZ+WP/F4rF1u/vx3UYY0oIg21wKuDE0wcbcj7Llry8SYAh2CKpS1djeiChTiRVdQ6mDw7k8TEvI4NEgtj+KLe0mjVizk7gbUTJJN5CU1J7FbuVKx+BUz9w= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Stewart Hildebrand To: CC: Oleksandr Andrushchenko , =?utf-8?q?Ro?= =?utf-8?q?ger_Pau_Monn=C3=A9?= , "Volodymyr Babchuk" , Stewart Hildebrand Subject: [PATCH v13 05/14] vpci/header: implement guest BAR register handlers Date: Fri, 2 Feb 2024 16:33:09 -0500 Message-ID: <20240202213321.1920347-6-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D9:EE_|DM6PR12MB4265:EE_ X-MS-Office365-Filtering-Correlation-Id: 6336fe6a-39fa-4bb8-fc01-08dc2436cd2c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: W33MB+IUMfFJVkt+OdUiGD1Ek3orT1eAO7jFBQVCSL1r/dw6oorWkGUL8Q7F/4VkxbnyOHWyD5f+FvatBV5beImKOMkHYO39EJbM58xCR/2lPBeZd9/atLQn7lR8Vf4ISr9DV/rMtUN0al/WtpbmZIrHlDRG60EPQLyaxkcXHVlTnq4ERa6s7b3S+loJ7orh8e/hJYNmXIXajwtw3vQ/k1BScGSwQICHuoXFdrSZOnF6bnBr+SZ4uTs+NvTV9G6DHfby6g+1xRLy2px+xiTMF3DhsO9nn/3ae5G0/yrBy+smd41+TeRqGw9zE3Ro86GV4ogYhtuxaY1+/sX3tx+yfBQIy8UVfO3ToFc0YxkUHkHNnl6M4vDfW093L4LWPB1i0GpVLh0q2sEkM3e6lhw4q8jrhcHVnVq2k7de56RV7saXNlDb1xyLkLwOXuOFuZFjC292HApbBudLf68plvlBpwvMbCgk/48Sg6zbxGaJetkcY2hCG5IEYhPVIOaGuhs+kqmESvwQyWDdJs+0cCySnfaAoCASoCKs5w77Bu7INSYjw3xMBUT2uOdOKRa33k/Eser1xjmlnK5+CncZzjNAgaeuFTFI4W5Rkpi8qC7NJ7widCsFyEhczMMAHDrSBmp5TP/LOWqTPIsevEDjBOAmP+u0yeh5nUn4WHJ38Q9yvT6wB15LTTpBJQhcZBupZYrPOT1ervRiIKZiF5uIjAhIJ+WwetAuLc7u14RHdi18ZJRTTPwMT9cqOJvK/7Ta5zplUIEXVRsjsHkhHxwMBgUR/A== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(39860400002)(136003)(346002)(376002)(396003)(230273577357003)(230922051799003)(186009)(64100799003)(82310400011)(451199024)(1800799012)(40470700004)(46966006)(36840700001)(40460700003)(40480700001)(41300700001)(86362001)(1076003)(36756003)(478600001)(356005)(2616005)(81166007)(83380400001)(47076005)(26005)(2906002)(54906003)(4326008)(36860700001)(316002)(6666004)(82740400003)(426003)(336012)(44832011)(8936002)(70586007)(70206006)(8676002)(5660300002)(6916009)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:34:56.7785 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6336fe6a-39fa-4bb8-fc01-08dc2436cd2c X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D9.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4265 From: Oleksandr Andrushchenko Add relevant vpci register handlers when assigning PCI device to a domain and remove those when de-assigning. This allows having different handlers for different domains, e.g. hwdom and other guests. Emulate guest BAR register values: this allows creating a guest view of the registers and emulates size and properties probe as it is done during PCI device enumeration by the guest. All empty, IO and ROM BARs for guests are emulated by returning 0 on reads and ignoring writes: this BARs are special with this respect as their lower bits have special meaning, so returning default ~0 on read may confuse guest OS. Introduce is_hwdom convenience variable and convert an existing is_hardware_domain() check. Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Volodymyr Babchuk Reviewed-by: Roger Pau Monné Signed-off-by: Stewart Hildebrand --- In v12: - Add Roger's R-b - Get rid of empty_bar_read, use vpci_read_val instead - Convert an existing is_hardware_domain() check to is_hwdom - Re-indent usage of ternary operator - Use ternary operator to avoid re-indenting expansion ROM block In v11: - Access guest_addr after adjusting for MEM64_HI bar in guest_bar_write() - guest bar handlers renamed and now _mem_ part to denote that they are handling only memory BARs - refuse to update guest BAR address if BAR is enabled In v10: - ull -> ULL to be MISRA-compatbile - Use PAGE_OFFSET() instead of combining with ~PAGE_MASK - Set type of empty bars to VPCI_BAR_EMPTY In v9: - factored-out "fail" label introduction in init_bars() - replaced #ifdef CONFIG_X86 with IS_ENABLED() - do not pass bars[i] to empty_bar_read() handler - store guest's BAR address instead of guests BAR register view Since v6: - unify the writing of the PCI_COMMAND register on the error path into a label - do not introduce bar_ignore_access helper and open code - s/guest_bar_ignore_read/empty_bar_read - update error message in guest_bar_write - only setup empty_bar_read for IO if !x86 Since v5: - make sure that the guest set address has the same page offset as the physical address on the host - remove guest_rom_{read|write} as those just implement the default behaviour of the registers not being handled - adjusted comment for struct vpci.addr field - add guest handlers for BARs which are not handled and will otherwise return ~0 on read and ignore writes. The BARs are special with this respect as their lower bits have special meaning, so returning ~0 doesn't seem to be right Since v4: - updated commit message - s/guest_addr/guest_reg Since v3: - squashed two patches: dynamic add/remove handlers and guest BAR handler implementation - fix guest BAR read of the high part of a 64bit BAR (Roger) - add error handling to vpci_assign_device - s/dom%pd/%pd - blank line before return Since v2: - remove unneeded ifdefs for CONFIG_HAS_VPCI_GUEST_SUPPORT as more code has been eliminated from being built on x86 Since v1: - constify struct pci_dev where possible - do not open code is_system_domain() - simplify some code3. simplify - use gdprintk + error code instead of gprintk - gate vpci_bar_{add|remove}_handlers with CONFIG_HAS_VPCI_GUEST_SUPPORT, so these do not get compiled for x86 - removed unneeded is_system_domain check - re-work guest read/write to be much simpler and do more work on write than read which is expected to be called more frequently - removed one too obvious comment --- xen/drivers/vpci/header.c | 109 +++++++++++++++++++++++++++++++++++--- xen/include/xen/vpci.h | 3 ++ 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c index 803fe4bb99a6..39e11e141b38 100644 --- a/xen/drivers/vpci/header.c +++ b/xen/drivers/vpci/header.c @@ -478,6 +478,69 @@ static void cf_check bar_write( pci_conf_write32(pdev->sbdf, reg, val); } +static void cf_check guest_mem_bar_write(const struct pci_dev *pdev, + unsigned int reg, uint32_t val, + void *data) +{ + struct vpci_bar *bar = data; + bool hi = false; + uint64_t guest_addr; + + if ( bar->type == VPCI_BAR_MEM64_HI ) + { + ASSERT(reg > PCI_BASE_ADDRESS_0); + bar--; + hi = true; + } + else + { + val &= PCI_BASE_ADDRESS_MEM_MASK; + } + + guest_addr = bar->guest_addr; + guest_addr &= ~(0xffffffffULL << (hi ? 32 : 0)); + guest_addr |= (uint64_t)val << (hi ? 32 : 0); + + /* Allow guest to size BAR correctly */ + guest_addr &= ~(bar->size - 1); + + /* + * Xen only cares whether the BAR is mapped into the p2m, so allow BAR + * writes as long as the BAR is not mapped into the p2m. + */ + if ( bar->enabled ) + { + /* If the value written is the current one avoid printing a warning. */ + if ( guest_addr != bar->guest_addr ) + gprintk(XENLOG_WARNING, + "%pp: ignored guest BAR %zu write while mapped\n", + &pdev->sbdf, bar - pdev->vpci->header.bars + hi); + return; + } + bar->guest_addr = guest_addr; +} + +static uint32_t cf_check guest_mem_bar_read(const struct pci_dev *pdev, + unsigned int reg, void *data) +{ + const struct vpci_bar *bar = data; + uint32_t reg_val; + + if ( bar->type == VPCI_BAR_MEM64_HI ) + { + ASSERT(reg > PCI_BASE_ADDRESS_0); + bar--; + return bar->guest_addr >> 32; + } + + reg_val = bar->guest_addr; + reg_val |= bar->type == VPCI_BAR_MEM32 ? PCI_BASE_ADDRESS_MEM_TYPE_32 : + PCI_BASE_ADDRESS_MEM_TYPE_64; + reg_val |= bar->prefetchable ? PCI_BASE_ADDRESS_MEM_PREFETCH : 0; + + return reg_val; +} + static void cf_check rom_write( const struct pci_dev *pdev, unsigned int reg, uint32_t val, void *data) { @@ -539,6 +602,7 @@ static int cf_check init_header(struct pci_dev *pdev) struct vpci_bar *bars = header->bars; int rc; bool mask_cap_list = false; + bool is_hwdom = is_hardware_domain(pdev->domain); ASSERT(rw_is_write_locked(&pdev->domain->pci_lock)); @@ -564,7 +628,7 @@ static int cf_check init_header(struct pci_dev *pdev) if ( rc ) return rc; - if ( !is_hardware_domain(pdev->domain) ) + if ( !is_hwdom ) { if ( pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST ) { @@ -653,8 +717,11 @@ static int cf_check init_header(struct pci_dev *pdev) if ( i && bars[i - 1].type == VPCI_BAR_MEM64_LO ) { bars[i].type = VPCI_BAR_MEM64_HI; - rc = vpci_add_register(pdev->vpci, vpci_hw_read32, bar_write, reg, - 4, &bars[i]); + rc = vpci_add_register(pdev->vpci, + is_hwdom ? vpci_hw_read32 + : guest_mem_bar_read, + is_hwdom ? bar_write : guest_mem_bar_write, + reg, 4, &bars[i]); if ( rc ) goto fail; @@ -665,6 +732,14 @@ static int cf_check init_header(struct pci_dev *pdev) if ( (val & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO ) { bars[i].type = VPCI_BAR_IO; + if ( !IS_ENABLED(CONFIG_X86) && !is_hwdom ) + { + rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL, + reg, 4, (void *)0); + if ( rc ) + goto fail; + } + continue; } if ( (val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == @@ -681,6 +756,15 @@ static int cf_check init_header(struct pci_dev *pdev) if ( size == 0 ) { bars[i].type = VPCI_BAR_EMPTY; + + if ( !is_hwdom ) + { + rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL, + reg, 4, (void *)0); + if ( rc ) + goto fail; + } + continue; } @@ -688,14 +772,18 @@ static int cf_check init_header(struct pci_dev *pdev) bars[i].size = size; bars[i].prefetchable = val & PCI_BASE_ADDRESS_MEM_PREFETCH; - rc = vpci_add_register(pdev->vpci, vpci_hw_read32, bar_write, reg, 4, - &bars[i]); + rc = vpci_add_register(pdev->vpci, + is_hwdom ? vpci_hw_read32 : guest_mem_bar_read, + is_hwdom ? bar_write : guest_mem_bar_write, + reg, 4, &bars[i]); if ( rc ) goto fail; } /* Check expansion ROM. */ - rc = pci_size_mem_bar(pdev->sbdf, rom_reg, &addr, &size, PCI_BAR_ROM); + rc = is_hwdom ? pci_size_mem_bar(pdev->sbdf, rom_reg, &addr, &size, + PCI_BAR_ROM) + : 0; if ( rc > 0 && size ) { struct vpci_bar *rom = &header->bars[num_bars]; @@ -711,6 +799,15 @@ static int cf_check init_header(struct pci_dev *pdev) if ( rc ) rom->type = VPCI_BAR_EMPTY; } + else if ( !is_hwdom ) + { + /* TODO: Check expansion ROM, we do not handle ROM for guests for now */ + header->bars[num_bars].type = VPCI_BAR_EMPTY; + rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL, + rom_reg, 4, (void *)0); + if ( rc ) + goto fail; + } return (cmd & PCI_COMMAND_MEMORY) ? modify_bars(pdev, cmd, false) : 0; diff --git a/xen/include/xen/vpci.h b/xen/include/xen/vpci.h index 99fe76f08ace..b0e38a5a1acb 100644 --- a/xen/include/xen/vpci.h +++ b/xen/include/xen/vpci.h @@ -87,7 +87,10 @@ struct vpci { struct vpci_header { /* Information about the PCI BARs of this device. */ struct vpci_bar { + /* Physical (host) address. */ uint64_t addr; + /* Guest address. */ + uint64_t guest_addr; uint64_t size; enum { VPCI_BAR_EMPTY, From patchwork Fri Feb 2 21:33:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543460 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 AFD28C4828F for ; Fri, 2 Feb 2024 21:35:22 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675222.1050530 (Exim 4.92) (envelope-from ) id 1rW1Bq-0006gk-Mm; Fri, 02 Feb 2024 21:35:14 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675222.1050530; Fri, 02 Feb 2024 21:35:14 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1Bq-0006gb-Jl; Fri, 02 Feb 2024 21:35:14 +0000 Received: by outflank-mailman (input) for mailman id 675222; Fri, 02 Feb 2024 21:35:13 +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 1rW1Bp-0003tJ-Dn for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:35:13 +0000 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on20600.outbound.protection.outlook.com [2a01:111:f403:200a::600]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id f1ae0816-c212-11ee-8a45-1f161083a0e0; Fri, 02 Feb 2024 22:35:12 +0100 (CET) Received: from CY5PR15CA0217.namprd15.prod.outlook.com (2603:10b6:930:88::6) by PH0PR12MB7929.namprd12.prod.outlook.com (2603:10b6:510:284::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7270.12; Fri, 2 Feb 2024 21:35:08 +0000 Received: from CY4PEPF0000E9D8.namprd05.prod.outlook.com (2603:10b6:930:88:cafe::19) by CY5PR15CA0217.outlook.office365.com (2603:10b6:930:88::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.31 via Frontend Transport; Fri, 2 Feb 2024 21:35:08 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by CY4PEPF0000E9D8.mail.protection.outlook.com (10.167.241.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7249.19 via Frontend Transport; Fri, 2 Feb 2024 21:35:08 +0000 Received: from SATLEXMB08.amd.com (10.181.40.132) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:35:07 -0600 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB08.amd.com (10.181.40.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32; Fri, 2 Feb 2024 13:35:07 -0800 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:35:06 -0600 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: f1ae0816-c212-11ee-8a45-1f161083a0e0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CAXrtD/XwX3PvWAIhRp4bxDCq+MabzGDbD72l1O/6mk0GcPAxDxE43gClQ98r8QDLG4MqzUo2TSnbBRsKQF8+Db4fovSKAlnKPYtstegwEyuOYgeuO9E02V1Jia8IGGUIt7iMTxevbFWFoPjM5UzUo1Jbxh10hxvDp5H2D85DOkZssJG0kQNIymtkZxJ1JMIkIHQLNR/0VtfKVZLS/Xmg7va8BuJOg9uCAjOl6CDelH8GWnvDeHSPDKHAVi/0gRLmy6OJ9rw3LBbYdZQ/lKeTg0YuU62LLmIy1AG3T93LqNCSrNtjdqIb5honhOZO+Xe0mPHJKeu1XKEjw30xZSlZQ== 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=tIlYNCEQ+4nfk+sRGPCKjR3ey13meQvjdP5M2lt1W5E=; b=Xi5QYI/t2UdRWUt6hWYnmQPMdGqg1idlzoaBmb8jHeaJ+/bXt5Wk6tksfqhuMQ1O5xK+43AHqEaDuj605Z5/gtUanWyKTMWNZNcltighm9LYUAnKCMp2ILurnMivhX9i2DMB0YNoH38Wt/0l2i1H0LQvo9q0Xr9nzQlp6FhUMpp6lDlxQMzPqtjO/mxKPrRwc7XoX4JCvy8to8q5Mg8k1CACbFn8zv2ZlojjkbMrGLk7V/+XL2n0pRWpQSwwPAFvkB4kUOIhENdjS3ofR92e77ajJJQVlyO8QvylRuOxVdK9u3IhCOLnNcchO3NYhS59EVWuijAArPOujWg2MgC7Bg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=tIlYNCEQ+4nfk+sRGPCKjR3ey13meQvjdP5M2lt1W5E=; b=rKsCCfAwUSUOprM33XzmL/66B3IyxsDuVmTkXf/ExfcmnvFUU0UstsLaYPEAdWGwFqH/PbwP9nhHA/P3YhfLss8x+HmmppNmNydfsF9uhlUIcyQ2Dpfq8erNmk1834j1XOuzwfCiXm7loWEfZQjhTkqffOiJJIfO7avJCElyU3M= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Stewart Hildebrand To: CC: Oleksandr Andrushchenko , Andrew Cooper , George Dunlap , "Jan Beulich" , Julien Grall , "Stefano Stabellini" , Wei Liu , Volodymyr Babchuk , Stewart Hildebrand Subject: [PATCH v13 06/14] rangeset: add RANGESETF_no_print flag Date: Fri, 2 Feb 2024 16:33:10 -0500 Message-ID: <20240202213321.1920347-7-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D8:EE_|PH0PR12MB7929:EE_ X-MS-Office365-Filtering-Correlation-Id: df09e7b0-9791-4897-ae4f-08dc2436d424 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: uN8K93cdDoxuD3pHr3+1GeRTsqTTvunKnDa22WBCOP45j+emRvrSLvFxc5SVUi9GgHj2q5Vhh3CpPcwQ1P26Z4pWIUgnxINdld+ALMc0Sm3slYLjQhFCgeIMvuEoU2y+5suvTZJc6kaohBv39G+6+Fwfv8HlRVBZnalaqiQienuspFCfBENetyChFDZMzWIENanUNCfO2Dqmy+8mBymzy6T7t/rk8mpkKghp5jj81vFNZZhi3hBNWUOjBO5O1smNDsUPbm22Zw36Y5ZhluFrTCQmE2mTyaXZnonfYAJMcb+33eSmFfa/ol3OamQXgjDWZbdVEgiDDNpliLN7dN/doES7QUCyXfvVopMoGcbfi71FL4nt89rONSGz8E9fBAQ7l95IiCXN3a4LliN8Fh7ZdXJPOWyVYh7QnxUyOQLQJ7RkCxpHOd84kaW+cE/ESCgMsn8q/g9VDb8qcfOjMjsq5xBuU0aHW+RugIrioBrvSjwFnPQpcxO7XLWDalHOUQpQ3WL+faFbJqME2cG8H1eO2bkzqzDa8RhV0Qi1+w7QOHhknlgwqhocws9f59RMzRUv9kDu8M1KlBFkr4Q5l8A3EDXxvw9rvq5UHY61Oobji3Du1UkRmw9pslPveFdRrVTRJny3AKycdfIpFZTLsH+Oo4JVYb/ZibGrvx9q3x+VrERnwnCrinq4riLEJoPYT4WBZMM/yhk4ELhLTQ0D6yAuny6lx5BbNaPVA82bXQnUmm4zJZsBSF6Iz4kEDpeLJ7TnOWDso789ZZD8XeosY2fJYA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(376002)(136003)(39860400002)(396003)(346002)(230922051799003)(64100799003)(82310400011)(186009)(1800799012)(451199024)(40470700004)(46966006)(36840700001)(478600001)(41300700001)(82740400003)(83380400001)(36860700001)(40460700003)(40480700001)(81166007)(356005)(47076005)(336012)(426003)(6666004)(54906003)(5660300002)(44832011)(2906002)(4326008)(8676002)(26005)(36756003)(8936002)(316002)(6916009)(70586007)(70206006)(86362001)(2616005)(1076003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:35:08.4837 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: df09e7b0-9791-4897-ae4f-08dc2436d424 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D8.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7929 From: Oleksandr Andrushchenko There are range sets which should not be printed, so introduce a flag which allows marking those as such. Implement relevant logic to skip such entries while printing. While at it also simplify the definition of the flags by directly defining those without helpers. Suggested-by: Jan Beulich Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Volodymyr Babchuk Reviewed-by: Jan Beulich Signed-off-by: Stewart Hildebrand --- Since v5: - comment indentation (Jan) Since v1: - update BUG_ON with new flag - simplify the definition of the flags --- xen/common/rangeset.c | 5 ++++- xen/include/xen/rangeset.h | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/xen/common/rangeset.c b/xen/common/rangeset.c index 16a4c3b842e6..0ccd53caac52 100644 --- a/xen/common/rangeset.c +++ b/xen/common/rangeset.c @@ -433,7 +433,7 @@ struct rangeset *rangeset_new( INIT_LIST_HEAD(&r->range_list); r->nr_ranges = -1; - BUG_ON(flags & ~RANGESETF_prettyprint_hex); + BUG_ON(flags & ~(RANGESETF_prettyprint_hex | RANGESETF_no_print)); r->flags = flags; safe_strcpy(r->name, name ?: "(no name)"); @@ -575,6 +575,9 @@ void rangeset_domain_printk( list_for_each_entry ( r, &d->rangesets, rangeset_list ) { + if ( r->flags & RANGESETF_no_print ) + continue; + printk(" "); rangeset_printk(r); printk("\n"); diff --git a/xen/include/xen/rangeset.h b/xen/include/xen/rangeset.h index 8be0722787ed..87bd956962b5 100644 --- a/xen/include/xen/rangeset.h +++ b/xen/include/xen/rangeset.h @@ -49,8 +49,9 @@ void rangeset_limit( /* Flags for passing to rangeset_new(). */ /* Pretty-print range limits in hexadecimal. */ -#define _RANGESETF_prettyprint_hex 0 -#define RANGESETF_prettyprint_hex (1U << _RANGESETF_prettyprint_hex) +#define RANGESETF_prettyprint_hex (1U << 0) + /* Do not print entries marked with this flag. */ +#define RANGESETF_no_print (1U << 1) bool __must_check rangeset_is_empty( const struct rangeset *r); From patchwork Fri Feb 2 21:33:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543465 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 7FBB0C4828F for ; Fri, 2 Feb 2024 21:37:31 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675237.1050580 (Exim 4.92) (envelope-from ) id 1rW1Dv-0001Cy-Gw; Fri, 02 Feb 2024 21:37:23 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675237.1050580; Fri, 02 Feb 2024 21:37:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1Dv-0001CX-BF; Fri, 02 Feb 2024 21:37:23 +0000 Received: by outflank-mailman (input) for mailman id 675237; Fri, 02 Feb 2024 21:37:21 +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 1rW1C7-0004Ae-4j for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:35:31 +0000 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on20601.outbound.protection.outlook.com [2a01:111:f403:200a::601]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id fba113e6-c212-11ee-98f5-efadbce2ee36; Fri, 02 Feb 2024 22:35:29 +0100 (CET) Received: from SJ0PR03CA0381.namprd03.prod.outlook.com (2603:10b6:a03:3a1::26) by CY5PR12MB6298.namprd12.prod.outlook.com (2603:10b6:930:21::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7270.13; Fri, 2 Feb 2024 21:35:25 +0000 Received: from SJ1PEPF00001CE3.namprd05.prod.outlook.com (2603:10b6:a03:3a1:cafe::f) by SJ0PR03CA0381.outlook.office365.com (2603:10b6:a03:3a1::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.27 via Frontend Transport; Fri, 2 Feb 2024 21:35:24 +0000 Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CE3.mail.protection.outlook.com (10.167.242.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7202.16 via Frontend Transport; Fri, 2 Feb 2024 21:35:24 +0000 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:35:23 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:35:17 -0600 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: fba113e6-c212-11ee-98f5-efadbce2ee36 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JfeohXYcssNx4LGdnMYO1l6efKA2Cu4pWzgNugzpbp0WI3n+F98n0GqUP0JLNg7pcfDIR9w4+wIDzaJawO8YRqDlMsEqd1C75+U+Y5zKeUlAoQNywrn4zZkXVaQ2O1DPy9sgOuYrSQnKqxDAu+RnKt+qFC5mnHCikAeH0r/FfPuXFf3YFquVam/ixdfbkRpC3cpSfSONj7sCnnEX4aow0gonPk7gYWmSkLDnYRDPBF/6YFyhFFRoSU8wVFOYWA2lQJWwLVN48eyJ68sar9B9WIZUmpNm3U3ig3c+9VZIvocvWifww/Bbd/zdz5bqfqzS7VJ8sKSJ0B7blP7xanF7NQ== 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=Qi56JEzWMgKoMsc50mwUebNiiljtIu6vzi5g/b8KIBY=; b=kEb6n/l4euu0BG8KsUgcZjMIxggmkdndvzmYPDqCQZ5dMuqXtMko0tbwAMm9CEgc3XxjM27iBxVSlpTC9803CtGNcPyXYe+JCPPpWTbqb1QsA+eG2ldPEIR6Lpg1OG4GCfkYqJB1rBBqcSzmBrVhEOv4hhUxxf2uLRMHqWQb9/XZejlIRe+2Jn/hCI76rQV6Uv7Z0R3CFT7525Lzgx5AP2XWXi1L8rltAkemmVdj7iFsu2pXQiNXiG5vZoLD/SXsinPnNYzId0c7lTL0NGAjjmGOt/KPMwxVE6zLqKLiKcYOWN1D+ktuXApxWm8VjAaNNCntqxMyBNgxyhPFUOJ6dQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Qi56JEzWMgKoMsc50mwUebNiiljtIu6vzi5g/b8KIBY=; b=k4Ys+m/wdNa1Pkyi/my9j9CLIW8jMOAnsTQBw3N+dqAndW7VMbuHc3KbcADrqjTnPgQgIxQKq7rBi9ImZECRkmumgh0HOXKzgDw9bjaDXT6gv/1SBxz53s1TyPdWT3ixpcoblttqHflhNH/U3KqHdNcKAd/0WNl+HeLgE0JxeH8= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C From: Stewart Hildebrand To: CC: Volodymyr Babchuk , Andrew Cooper , George Dunlap , "Jan Beulich" , Julien Grall , "Stefano Stabellini" , Wei Liu , Volodymyr Babchuk , Stewart Hildebrand Subject: [PATCH v13 07/14] rangeset: add rangeset_purge() function Date: Fri, 2 Feb 2024 16:33:11 -0500 Message-ID: <20240202213321.1920347-8-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CE3:EE_|CY5PR12MB6298:EE_ X-MS-Office365-Filtering-Correlation-Id: cbd32703-21c8-4311-cf92-08dc2436dd84 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wA0uZjREnZav4KOm+CgHBzzFX+GbLx8RJW3/lFefgpDJGl5mpchTDav0KXHRhl/N1TJSeg1iC9aOx0EE41f/nC/S+jd5XCHXUQBGgrLn4d4jjjEfbG+T/wzFbwYWiKUBAYpSP0nLLDq1d0wScq8F7g14sGuYVi1Gw8LfXRWZgPpcfhVvsddbgSfCghTMzd/qhjHcGJKsubHplszvEe0Sek5+G0Bh026uVjSVf9KsEm8T04DDwjxyCiAHIGlWLJUGwZxHBMcLKL/zUL9WJCQTFz04o9+Vv9Ydx77/c6kdNkZc1hOoK8s3lXf9V1TPcuUOinXR/XNe9CrAFdBM5AExOHKnRq9lGWEB159TRKUMdDfpQytgtrCmwIrkLrJSqOU4fUrIgynNB2kEiixyn5x09BiW6miaPXuaqm6YoAn9oErMLJ+I0/oI03bOEDM1sQkoGB1ZrjqElR4q7ui/KDozBFz6pMb+EiZLr0+kwP8I9EKchyLkH/xgQD/9+vEa/B50HKgXLu3mGJyL1URtQtH0u3mgn++o0i78pK1oaETuOSBOMAJD4sTYKHeNTerFKztCF9iCJEbHGGTWuILWcbQJ0QlAdtHjtFynE6X4Liy7MLp+7hi04NB8VmGWSyz9UZbKRvbUi9clWbPD0cUs0fvZ29k5dwCgqT0xp2/kvwetHfA4OpTM9UKbMg6T5T22TFUZS4Y+YlUsrske9seX5UsEE5XCcMXsIVnAHjF+xBsPZC/RExBd0TgYPkAlSEF7ejqttL4VLYXtPuOBKIIHFqbnfQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(346002)(136003)(39860400002)(376002)(396003)(230922051799003)(64100799003)(451199024)(82310400011)(1800799012)(186009)(40470700004)(46966006)(36840700001)(36756003)(2906002)(5660300002)(41300700001)(1076003)(2616005)(83380400001)(336012)(26005)(6916009)(316002)(426003)(54906003)(6666004)(70206006)(70586007)(478600001)(40460700003)(40480700001)(36860700001)(47076005)(82740400003)(356005)(81166007)(8936002)(44832011)(4326008)(8676002)(86362001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:35:24.1820 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cbd32703-21c8-4311-cf92-08dc2436dd84 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CE3.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6298 From: Volodymyr Babchuk This function can be used when user wants to remove all rangeset entries but do not want to destroy rangeset itself. Signed-off-by: Volodymyr Babchuk Signed-off-by: Stewart Hildebrand Acked-by: Jan Beulich --- Changes in v13: - Added Jan's A-b Changes in v12: - s/rangeset_empty/rangeset_purge/ Changes in v11: - Now the function only empties rangeset, without removing it from domain's list Changes in v10: - New in v10. The function is used in "vpci/header: handle p2m range sets per BAR" --- xen/common/rangeset.c | 16 ++++++++++++---- xen/include/xen/rangeset.h | 3 ++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/xen/common/rangeset.c b/xen/common/rangeset.c index 0ccd53caac52..b75590f90744 100644 --- a/xen/common/rangeset.c +++ b/xen/common/rangeset.c @@ -448,11 +448,20 @@ struct rangeset *rangeset_new( return r; } -void rangeset_destroy( - struct rangeset *r) +void rangeset_purge(struct rangeset *r) { struct range *x; + if ( r == NULL ) + return; + + while ( (x = first_range(r)) != NULL ) + destroy_range(r, x); +} + +void rangeset_destroy( + struct rangeset *r) +{ if ( r == NULL ) return; @@ -463,8 +472,7 @@ void rangeset_destroy( spin_unlock(&r->domain->rangesets_lock); } - while ( (x = first_range(r)) != NULL ) - destroy_range(r, x); + rangeset_purge(r); xfree(r); } diff --git a/xen/include/xen/rangeset.h b/xen/include/xen/rangeset.h index 87bd956962b5..96c918082501 100644 --- a/xen/include/xen/rangeset.h +++ b/xen/include/xen/rangeset.h @@ -56,7 +56,7 @@ void rangeset_limit( bool __must_check rangeset_is_empty( const struct rangeset *r); -/* Add/claim/remove/query a numeric range. */ +/* Add/claim/remove/query/purge a numeric range. */ int __must_check rangeset_add_range( struct rangeset *r, unsigned long s, unsigned long e); int __must_check rangeset_claim_range(struct rangeset *r, unsigned long size, @@ -70,6 +70,7 @@ bool __must_check rangeset_overlaps_range( int rangeset_report_ranges( struct rangeset *r, unsigned long s, unsigned long e, int (*cb)(unsigned long s, unsigned long e, void *data), void *ctxt); +void rangeset_purge(struct rangeset *r); /* * Note that the consume function can return an error value apart from From patchwork Fri Feb 2 21:33:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543461 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 5C4DBC4828F for ; Fri, 2 Feb 2024 21:35:50 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675227.1050539 (Exim 4.92) (envelope-from ) id 1rW1CJ-0007UP-0H; Fri, 02 Feb 2024 21:35:43 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675227.1050539; Fri, 02 Feb 2024 21:35:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1CI-0007UI-SX; Fri, 02 Feb 2024 21:35:42 +0000 Received: by outflank-mailman (input) for mailman id 675227; Fri, 02 Feb 2024 21:35:41 +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 1rW1CH-0003tJ-Dj for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:35:41 +0000 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on20600.outbound.protection.outlook.com [2a01:111:f403:2009::600]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 02493886-c213-11ee-8a45-1f161083a0e0; Fri, 02 Feb 2024 22:35:40 +0100 (CET) Received: from CY5PR15CA0220.namprd15.prod.outlook.com (2603:10b6:930:88::22) by BL1PR12MB5850.namprd12.prod.outlook.com (2603:10b6:208:395::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7270.11; Fri, 2 Feb 2024 21:35:35 +0000 Received: from CY4PEPF0000E9D8.namprd05.prod.outlook.com (2603:10b6:930:88:cafe::bc) by CY5PR15CA0220.outlook.office365.com (2603:10b6:930:88::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.30 via Frontend Transport; Fri, 2 Feb 2024 21:35:35 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by CY4PEPF0000E9D8.mail.protection.outlook.com (10.167.241.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7249.19 via Frontend Transport; Fri, 2 Feb 2024 21:35:35 +0000 Received: from SATLEXMB05.amd.com (10.181.40.146) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:35:34 -0600 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB05.amd.com (10.181.40.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:35:34 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:35:33 -0600 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: 02493886-c213-11ee-8a45-1f161083a0e0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oWw7qFCFgb7Y6CQAyPCxLZ7UyiP5+MEy4Aw84yglAmarrZJG3L/KDmyiPsHsL6WVuos3RNShPZmCUsDALq2f8Ien99sQ3AullPXr/O+GJuqBrAVTbD7Xi53aDFg4ip+Fa+gldfkPiYdE0lP3vvYbarQbF8TZFfgVzFrtzXknYIFW8CqrSOawE4VB/+U0xOiY/V2hvb0Q1LG7IX1Ap5gSFV0cW4peBWxO6ZACPTEJV8ck3SX9xqNM/n9elNhfaMoXtqY00tSH1CUlX+26Yq1V3B7NTShFDHbm6e8dDJX7YfsxWKpFa98VjvMj7kFRqLbtrF0RgiUz3LpTheiV+41jSQ== 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=HDy9jvm+K+yrePwNekCbbD5gVDRQvCSbWFl/2rlmQPI=; b=hC7d7WpTLHjV6dPiKMvNeF8LjdJzBaElTejbn1za9K+OlvJ9a6pbDR+4+blBXw31XJqHw9j/pwJl3XNBIiTrv3uULbbW+odFnaGrk+dxFsajXraKCdfcsvyuKC41TD6T6y7M83P1yUWMgk2jOZInQs8nJqKR5mX9xE72WWRqwbmFqtfcE2lIJq+NF4fSTziLyLA2GTgR5BRod8SKvqxeGkPlOvuXZ7y5lb2mFIsp59TnEOPC1tB44DzvL6RHmxSnNsNZIPKmv+H1WTBAh/u7WjTPTNiQMOKWDMZkL7J7neQY3NhhekYqJqeGNvvn8HftNzHPwUBzyjUMYtQSVVTneg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HDy9jvm+K+yrePwNekCbbD5gVDRQvCSbWFl/2rlmQPI=; b=fdRW8+qVIWcLlJcdIa94/THaCsB3HshFHmoHwvFAQFqm3lOiwbPT5/3Ecs097ylsW/eqoBQQC1dxO2y0Ev5iaBAAbBQx6nGWQZnEVW2fvApPo1bCj7bpTbCB8X94m2WiS6yTVw7UEw1+Al+2NIlnstbgGmT9UiG+QI9HY6caaPo= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Stewart Hildebrand To: CC: Oleksandr Andrushchenko , =?utf-8?q?Ro?= =?utf-8?q?ger_Pau_Monn=C3=A9?= , "Volodymyr Babchuk" , Stewart Hildebrand Subject: [PATCH v13 08/14] vpci/header: handle p2m range sets per BAR Date: Fri, 2 Feb 2024 16:33:12 -0500 Message-ID: <20240202213321.1920347-9-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D8:EE_|BL1PR12MB5850:EE_ X-MS-Office365-Filtering-Correlation-Id: 266ebd28-21d1-45c1-49f7-08dc2436e40f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: g1TZxLMAuXKKeKCZDmkJh4IGyFyQ8okVd3gRN/aif429iN6n4qjOmgNE/d2AtoeT9hqThTA0Vt/qLPZF4/MpiQt55XikR2cvf5egcy8ttGRoYgpWOB7aVg+4GS9lE+EyTdUhvC4N6dxpFjTHaavYgnTSQcw+4qhNvOasLaYc9CF0Ckcb4+jyP6gX12meXjs66M/8UT4fW0fl0Bt9bJxWukcpDlc1NqsCoAukpIRb5c8lW1JnFcR8cYeXO5/KDgbkVhRarkOKaKEB2m7DQbF92d9/uNC3LpCTzfR7h2xrm3Npo/9n0+Xu3CoYZ50mNQrVgD99A7GfOt8dGH+H4tk43rV/3qR3F/sc5KQEU3KeSLNmZKMeyC+gO0NsVt1dRd14Psh06FDDwLCClJeyDPDKTlKXgopa4+StSr2YYB4E+wXgq9iBUUrQG4p9nHkm4yOPEl+DlgfzkxXx7rIEGh/4a5Tb2BbHKkjOJSXMjm4TPzLSd25x7KLujjYwMo+uagJebaJYlVNNxHE7V0ouUbAURkq8uFMnTLdh+elXqQJOQrv35TUz9+y2mlnPnuT1R558UkMtlqGCVV0ZZr9rduEBclPMYz4f+m4+27Z3XiuNc7sW/4wt8yUMs8w1RH8+dKSXzINEYcK0pdVj0DfK9j+Wg+rS7WYy/zxa+2UZ9XqZPXb8uh9C83JRP++oKuLHSQIlAdRwE+/vX7f38+gvcFpSrblxJMRZm2oitUrwKVI8aKOKLUyH5KRI/+HlKMSUX9horh2wEkjUcfTEueMd7Co+3w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(39860400002)(346002)(136003)(376002)(396003)(230273577357003)(230922051799003)(1800799012)(451199024)(186009)(82310400011)(64100799003)(46966006)(40470700004)(36840700001)(40480700001)(40460700003)(83380400001)(336012)(86362001)(41300700001)(356005)(36756003)(47076005)(426003)(36860700001)(4326008)(5660300002)(26005)(2616005)(81166007)(2906002)(1076003)(70206006)(6666004)(30864003)(316002)(82740400003)(70586007)(6916009)(8936002)(8676002)(44832011)(54906003)(478600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:35:35.1559 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 266ebd28-21d1-45c1-49f7-08dc2436e40f X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D8.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5850 From: Oleksandr Andrushchenko Instead of handling a single range set, that contains all the memory regions of all the BARs and ROM, have them per BAR. As the range sets are now created when a PCI device is added and destroyed when it is removed so make them named and accounted. Note that rangesets were chosen here despite there being only up to 3 separate ranges in each set (typically just 1). But rangeset per BAR was chosen for the ease of implementation and existing code re-usability. Also note that error handling of vpci_process_pending() is slightly modified, and that vPCI handlers are no longer removed if the creation of the mappings in vpci_process_pending() fails, as that's unlikely to lead to a functional device in any case. This is in preparation of making non-identity mappings in p2m for the MMIOs. Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Volodymyr Babchuk Reviewed-by: Roger Pau Monné Signed-off-by: Stewart Hildebrand --- In v12: - s/rangeset_empty/rangeset_purge/ - change i to num_bars for expansion ROM (purely cosmetic change) In v11: - Modified commit message to note changes in error handling in vpci_process_pending() - Removed redundant ASSERT() in defer_map. There is no reason to introduce it in this patch and there is no other patch where introducing that ASSERT() was appropriate. - Fixed formatting - vpci_process_pending() clears v->vpci.pdev if it failed checks at the beginning - Added Roger's R-B tag In v10: - Added additional checks to vpci_process_pending() - vpci_process_pending() now clears rangeset in case of failure - Fixed locks in vpci_process_pending() - Fixed coding style issues - Fixed error handling in init_bars In v9: - removed d->vpci.map_pending in favor of checking v->vpci.pdev != NULL - printk -> gprintk - renamed bar variable to fix shadowing - fixed bug with iterating on remote device's BARs - relaxed lock in vpci_process_pending - removed stale comment Since v6: - update according to the new locking scheme - remove odd fail label in modify_bars Since v5: - fix comments - move rangeset allocation to init_bars and only allocate for MAPPABLE BARs - check for overlap with the already setup BAR ranges Since v4: - use named range sets for BARs (Jan) - changes required by the new locking scheme - updated commit message (Jan) Since v3: - re-work vpci_cancel_pending accordingly to the per-BAR handling - s/num_mem_ranges/map_pending and s/uint8_t/bool - ASSERT(bar->mem) in modify_bars - create and destroy the rangesets on add/remove --- xen/drivers/vpci/header.c | 257 ++++++++++++++++++++++++++------------ xen/drivers/vpci/vpci.c | 6 + xen/include/xen/vpci.h | 2 +- 3 files changed, 185 insertions(+), 80 deletions(-) diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c index 39e11e141b38..feccd070ddd0 100644 --- a/xen/drivers/vpci/header.c +++ b/xen/drivers/vpci/header.c @@ -162,63 +162,107 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd, bool vpci_process_pending(struct vcpu *v) { - if ( v->vpci.mem ) + struct pci_dev *pdev = v->vpci.pdev; + struct map_data data = { + .d = v->domain, + .map = v->vpci.cmd & PCI_COMMAND_MEMORY, + }; + struct vpci_header *header = NULL; + unsigned int i; + + if ( !pdev ) + return false; + + read_lock(&v->domain->pci_lock); + + if ( !pdev->vpci || (v->domain != pdev->domain) ) { - struct map_data data = { - .d = v->domain, - .map = v->vpci.cmd & PCI_COMMAND_MEMORY, - }; - int rc = rangeset_consume_ranges(v->vpci.mem, map_range, &data); + v->vpci.pdev = NULL; + read_unlock(&v->domain->pci_lock); + return false; + } + + header = &pdev->vpci->header; + for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) + { + struct vpci_bar *bar = &header->bars[i]; + int rc; + + if ( rangeset_is_empty(bar->mem) ) + continue; + + rc = rangeset_consume_ranges(bar->mem, map_range, &data); if ( rc == -ERESTART ) + { + read_unlock(&v->domain->pci_lock); return true; + } - write_lock(&v->domain->pci_lock); - spin_lock(&v->vpci.pdev->vpci->lock); - /* Disable memory decoding unconditionally on failure. */ - modify_decoding(v->vpci.pdev, - rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd, - !rc && v->vpci.rom_only); - spin_unlock(&v->vpci.pdev->vpci->lock); - - rangeset_destroy(v->vpci.mem); - v->vpci.mem = NULL; if ( rc ) - /* - * FIXME: in case of failure remove the device from the domain. - * Note that there might still be leftover mappings. While this is - * safe for Dom0, for DomUs the domain will likely need to be - * killed in order to avoid leaking stale p2m mappings on - * failure. - */ - vpci_deassign_device(v->vpci.pdev); - write_unlock(&v->domain->pci_lock); + { + spin_lock(&pdev->vpci->lock); + /* Disable memory decoding unconditionally on failure. */ + modify_decoding(pdev, v->vpci.cmd & ~PCI_COMMAND_MEMORY, + false); + spin_unlock(&pdev->vpci->lock); + + /* Clean all the rangesets */ + for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) + if ( !rangeset_is_empty(header->bars[i].mem) ) + rangeset_purge(header->bars[i].mem); + + v->vpci.pdev = NULL; + + read_unlock(&v->domain->pci_lock); + + if ( !is_hardware_domain(v->domain) ) + domain_crash(v->domain); + + return false; + } } + v->vpci.pdev = NULL; + + spin_lock(&pdev->vpci->lock); + modify_decoding(pdev, v->vpci.cmd, v->vpci.rom_only); + spin_unlock(&pdev->vpci->lock); + + read_unlock(&v->domain->pci_lock); return false; } static int __init apply_map(struct domain *d, const struct pci_dev *pdev, - struct rangeset *mem, uint16_t cmd) + uint16_t cmd) { struct map_data data = { .d = d, .map = true }; - int rc; + struct vpci_header *header = &pdev->vpci->header; + int rc = 0; + unsigned int i; ASSERT(rw_is_write_locked(&d->pci_lock)); - while ( (rc = rangeset_consume_ranges(mem, map_range, &data)) == -ERESTART ) + for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) { - /* - * It's safe to drop and reacquire the lock in this context - * without risking pdev disappearing because devices cannot be - * removed until the initial domain has been started. - */ - write_unlock(&d->pci_lock); - process_pending_softirqs(); - write_lock(&d->pci_lock); - } + struct vpci_bar *bar = &header->bars[i]; - rangeset_destroy(mem); + if ( rangeset_is_empty(bar->mem) ) + continue; + + while ( (rc = rangeset_consume_ranges(bar->mem, map_range, + &data)) == -ERESTART ) + { + /* + * It's safe to drop and reacquire the lock in this context + * without risking pdev disappearing because devices cannot be + * removed until the initial domain has been started. + */ + write_unlock(&d->pci_lock); + process_pending_softirqs(); + write_lock(&d->pci_lock); + } + } if ( !rc ) modify_decoding(pdev, cmd, false); @@ -226,7 +270,7 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev, } static void defer_map(struct domain *d, struct pci_dev *pdev, - struct rangeset *mem, uint16_t cmd, bool rom_only) + uint16_t cmd, bool rom_only) { struct vcpu *curr = current; @@ -237,7 +281,6 @@ static void defer_map(struct domain *d, struct pci_dev *pdev, * started for the same device if the domain is not well-behaved. */ curr->vpci.pdev = pdev; - curr->vpci.mem = mem; curr->vpci.cmd = cmd; curr->vpci.rom_only = rom_only; /* @@ -251,33 +294,33 @@ static void defer_map(struct domain *d, struct pci_dev *pdev, static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) { struct vpci_header *header = &pdev->vpci->header; - struct rangeset *mem = rangeset_new(NULL, NULL, 0); struct pci_dev *tmp, *dev = NULL; const struct domain *d; const struct vpci_msix *msix = pdev->vpci->msix; - unsigned int i; + unsigned int i, j; int rc; ASSERT(rw_is_write_locked(&pdev->domain->pci_lock)); - if ( !mem ) - return -ENOMEM; - /* - * Create a rangeset that represents the current device BARs memory region - * and compare it against all the currently active BAR memory regions. If - * an overlap is found, subtract it from the region to be mapped/unmapped. + * Create a rangeset per BAR that represents the current device memory + * region and compare it against all the currently active BAR memory + * regions. If an overlap is found, subtract it from the region to be + * mapped/unmapped. * - * First fill the rangeset with all the BARs of this device or with the ROM + * First fill the rangesets with the BAR of this device or with the ROM * BAR only, depending on whether the guest is toggling the memory decode * bit of the command register, or the enable bit of the ROM BAR register. */ for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) { - const struct vpci_bar *bar = &header->bars[i]; + struct vpci_bar *bar = &header->bars[i]; unsigned long start = PFN_DOWN(bar->addr); unsigned long end = PFN_DOWN(bar->addr + bar->size - 1); + if ( !bar->mem ) + continue; + if ( !MAPPABLE_BAR(bar) || (rom_only ? bar->type != VPCI_BAR_ROM : (bar->type == VPCI_BAR_ROM && !header->rom_enabled)) || @@ -293,14 +336,31 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) continue; } - rc = rangeset_add_range(mem, start, end); + rc = rangeset_add_range(bar->mem, start, end); if ( rc ) { printk(XENLOG_G_WARNING "Failed to add [%lx, %lx]: %d\n", start, end, rc); - rangeset_destroy(mem); return rc; } + + /* Check for overlap with the already setup BAR ranges. */ + for ( j = 0; j < i; j++ ) + { + struct vpci_bar *prev_bar = &header->bars[j]; + + if ( rangeset_is_empty(prev_bar->mem) ) + continue; + + rc = rangeset_remove_range(prev_bar->mem, start, end); + if ( rc ) + { + gprintk(XENLOG_WARNING, + "%pp: failed to remove overlapping range [%lx, %lx]: %d\n", + &pdev->sbdf, start, end, rc); + return rc; + } + } } /* Remove any MSIX regions if present. */ @@ -310,14 +370,21 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) unsigned long end = PFN_DOWN(vmsix_table_addr(pdev->vpci, i) + vmsix_table_size(pdev->vpci, i) - 1); - rc = rangeset_remove_range(mem, start, end); - if ( rc ) + for ( j = 0; j < ARRAY_SIZE(header->bars); j++ ) { - printk(XENLOG_G_WARNING - "Failed to remove MSIX table [%lx, %lx]: %d\n", - start, end, rc); - rangeset_destroy(mem); - return rc; + const struct vpci_bar *bar = &header->bars[j]; + + if ( rangeset_is_empty(bar->mem) ) + continue; + + rc = rangeset_remove_range(bar->mem, start, end); + if ( rc ) + { + gprintk(XENLOG_WARNING, + "%pp: failed to remove MSIX table [%lx, %lx]: %d\n", + &pdev->sbdf, start, end, rc); + return rc; + } } } @@ -357,27 +424,37 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) for ( i = 0; i < ARRAY_SIZE(tmp->vpci->header.bars); i++ ) { - const struct vpci_bar *bar = &tmp->vpci->header.bars[i]; - unsigned long start = PFN_DOWN(bar->addr); - unsigned long end = PFN_DOWN(bar->addr + bar->size - 1); - - if ( !bar->enabled || - !rangeset_overlaps_range(mem, start, end) || - /* - * If only the ROM enable bit is toggled check against - * other BARs in the same device for overlaps, but not - * against the same ROM BAR. - */ - (rom_only && tmp == pdev && bar->type == VPCI_BAR_ROM) ) + const struct vpci_bar *remote_bar = &tmp->vpci->header.bars[i]; + unsigned long start = PFN_DOWN(remote_bar->addr); + unsigned long end = PFN_DOWN(remote_bar->addr + + remote_bar->size - 1); + + if ( !remote_bar->enabled ) continue; - rc = rangeset_remove_range(mem, start, end); - if ( rc ) + for ( j = 0; j < ARRAY_SIZE(header->bars); j++) { - printk(XENLOG_G_WARNING "Failed to remove [%lx, %lx]: %d\n", - start, end, rc); - rangeset_destroy(mem); - return rc; + const struct vpci_bar *bar = &header->bars[j]; + + if ( !rangeset_overlaps_range(bar->mem, start, end) || + /* + * If only the ROM enable bit is toggled check against + * other BARs in the same device for overlaps, but not + * against the same ROM BAR. + */ + (rom_only && + tmp == pdev && + bar->type == VPCI_BAR_ROM) ) + continue; + + rc = rangeset_remove_range(bar->mem, start, end); + if ( rc ) + { + gprintk(XENLOG_WARNING, + "%pp: failed to remove [%lx, %lx]: %d\n", + &pdev->sbdf, start, end, rc); + return rc; + } } } } @@ -401,10 +478,10 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) * will always be to establish mappings and process all the BARs. */ ASSERT((cmd & PCI_COMMAND_MEMORY) && !rom_only); - return apply_map(pdev->domain, pdev, mem, cmd); + return apply_map(pdev->domain, pdev, cmd); } - defer_map(dev->domain, dev, mem, cmd, rom_only); + defer_map(dev->domain, dev, cmd, rom_only); return 0; } @@ -593,6 +670,18 @@ static void cf_check rom_write( rom->addr = val & PCI_ROM_ADDRESS_MASK; } +static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, + unsigned int i) +{ + char str[32]; + + snprintf(str, sizeof(str), "%pp:BAR%u", &pdev->sbdf, i); + + bar->mem = rangeset_new(pdev->domain, str, RANGESETF_no_print); + + return !bar->mem ? -ENOMEM : 0; +} + static int cf_check init_header(struct pci_dev *pdev) { uint16_t cmd; @@ -748,6 +837,10 @@ static int cf_check init_header(struct pci_dev *pdev) else bars[i].type = VPCI_BAR_MEM32; + rc = bar_add_rangeset(pdev, &bars[i], i); + if ( rc ) + goto fail; + rc = pci_size_mem_bar(pdev->sbdf, reg, &addr, &size, (i == num_bars - 1) ? PCI_BAR_LAST : 0); if ( rc < 0 ) @@ -798,6 +891,12 @@ static int cf_check init_header(struct pci_dev *pdev) 4, rom); if ( rc ) rom->type = VPCI_BAR_EMPTY; + else + { + rc = bar_add_rangeset(pdev, rom, num_bars); + if ( rc ) + goto fail; + } } else if ( !is_hwdom ) { diff --git a/xen/drivers/vpci/vpci.c b/xen/drivers/vpci/vpci.c index ff4837391786..260b72875ee1 100644 --- a/xen/drivers/vpci/vpci.c +++ b/xen/drivers/vpci/vpci.c @@ -42,6 +42,8 @@ extern vpci_register_init_t *const __end_vpci_array[]; void vpci_deassign_device(struct pci_dev *pdev) { + unsigned int i; + ASSERT(rw_is_write_locked(&pdev->domain->pci_lock)); if ( !has_vpci(pdev->domain) || !pdev->vpci ) @@ -67,6 +69,10 @@ void vpci_deassign_device(struct pci_dev *pdev) if ( pdev->vpci->msix->table[i] ) iounmap(pdev->vpci->msix->table[i]); } + + for ( i = 0; i < ARRAY_SIZE(pdev->vpci->header.bars); i++ ) + rangeset_destroy(pdev->vpci->header.bars[i].mem); + xfree(pdev->vpci->msix); xfree(pdev->vpci->msi); xfree(pdev->vpci); diff --git a/xen/include/xen/vpci.h b/xen/include/xen/vpci.h index b0e38a5a1acb..817ee9ee7300 100644 --- a/xen/include/xen/vpci.h +++ b/xen/include/xen/vpci.h @@ -92,6 +92,7 @@ struct vpci { /* Guest address. */ uint64_t guest_addr; uint64_t size; + struct rangeset *mem; enum { VPCI_BAR_EMPTY, VPCI_BAR_IO, @@ -176,7 +177,6 @@ struct vpci { struct vpci_vcpu { /* Per-vcpu structure to store state while {un}mapping of PCI BARs. */ - struct rangeset *mem; struct pci_dev *pdev; uint16_t cmd; bool rom_only : 1; From patchwork Fri Feb 2 21:33:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543463 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 D8198C4828F for ; Fri, 2 Feb 2024 21:37:27 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675233.1050559 (Exim 4.92) (envelope-from ) id 1rW1Ds-0000L2-N3; Fri, 02 Feb 2024 21:37:20 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675233.1050559; Fri, 02 Feb 2024 21:37:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1Ds-0000K8-K5; Fri, 02 Feb 2024 21:37:20 +0000 Received: by outflank-mailman (input) for mailman id 675233; Fri, 02 Feb 2024 21:37:20 +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 1rW1CT-0003tJ-81 for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:35:53 +0000 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on20601.outbound.protection.outlook.com [2a01:111:f403:240a::601]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 09c28148-c213-11ee-8a45-1f161083a0e0; Fri, 02 Feb 2024 22:35:52 +0100 (CET) Received: from SJ0PR13CA0064.namprd13.prod.outlook.com (2603:10b6:a03:2c4::9) by SA1PR12MB8919.namprd12.prod.outlook.com (2603:10b6:806:38e::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.24; Fri, 2 Feb 2024 21:35:48 +0000 Received: from SJ1PEPF00001CDD.namprd05.prod.outlook.com (2603:10b6:a03:2c4:cafe::43) by SJ0PR13CA0064.outlook.office365.com (2603:10b6:a03:2c4::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.23 via Frontend Transport; Fri, 2 Feb 2024 21:35:48 +0000 Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CDD.mail.protection.outlook.com (10.167.242.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7202.16 via Frontend Transport; Fri, 2 Feb 2024 21:35:47 +0000 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:35:46 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:35:45 -0600 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: 09c28148-c213-11ee-8a45-1f161083a0e0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IvOct/gWcaV2Y1adym3+GJbEf/FtNIA9bai7PPs4lDnFAN/wCpcgZG/dJ1mqADUEHECkSr0mPbweljWIG5hwo1mP6ijnoQ0+9T/0ugtUis1IsmmgqhpGP3ynxNCEk/sEghrJLlsPcfZn/Q0xMcRgrvk7QDCrgU6SonvViJOPqleMMCvWy06nCcWMVTfsP2STRiDtE7DZu4fp3b9sBXX5rn/PaaZbqFvSMkDnt7p4HBXYAK+cYW28O0GxGsp5dFh/QeHz121pxomK8VcCK413WoMLFTeb7FEXfgkf+BRRKDf8hGHpuuBk6gk7kmIHMDX3jYkY1rnb8ujHR7kNcXZ4mw== 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=BtY/WrBc7LpwZJnW3+E9MtL7jMXCACUj7apzU1DoyGY=; b=j/CO97zn7v7z/t5peFsom/yE4f3QJDs8isdlryGxR71RdMn5UwvVNEWZPrIq5erQqpSeeAOY66mLWOV57LHYjSmt9rEncmtleV0TPOXj+UHPRIYlZr/hEAvfwy3lROxWFEaDvK6ZUZ+HMvYR6ymkMxGl/zhTTd+zB9K8vFO4u8KdO0FEIWta9S/i7Ej5eMHpGedKoej21kQif8++/CQRv85M0ixzISJKI6ONPcceSJiovBOIwLbpUTvO4HfTYQ20LPFAYtLn7HYs/Vfu6WONIG1AyJulDNkbq0R+IRnupCW5L2/v7x19LhR7oRVrfptnZVnGkL1L763OC9p8hBbeuw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BtY/WrBc7LpwZJnW3+E9MtL7jMXCACUj7apzU1DoyGY=; b=427UUaaXuaWL/LHQdkd+Alv5YOI/wFzCULiOsfEhM7FROQUNQNKFi+JpCKcvUSDWBBHStFk5EMkG41jA4vOHz6INTuUsP8ZqZt4u0JTWyUDmYRX1xSS1TlHkIsON9pB+NBN9j4aEOvRHXsA4kL+l1N5SjZUQUTkpnHFu/QWul0Y= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C From: Stewart Hildebrand To: CC: Oleksandr Andrushchenko , =?utf-8?q?Ro?= =?utf-8?q?ger_Pau_Monn=C3=A9?= , "Daniel P. Smith" , Volodymyr Babchuk , Stewart Hildebrand Subject: [PATCH v13 09/14] vpci/header: program p2m with guest BAR view Date: Fri, 2 Feb 2024 16:33:13 -0500 Message-ID: <20240202213321.1920347-10-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CDD:EE_|SA1PR12MB8919:EE_ X-MS-Office365-Filtering-Correlation-Id: b1ba2c8c-ddbb-475d-f107-08dc2436eb68 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hENbE8imIVJ2HS2JJqWAsEV6JyTe5iQDHbGHKofm+lIV0R9JNzosNzoh4LY9WuYY3RPaPcGxnVu3LIH8Px5K5eVtKOrUUChbKhEAadAwTIP3ym5oZpNxpx0LOQ4mqCRN0oCbzJTOPTyawkLtykymvJ7A4sfL3+pvJjMxTtIOulHT0VHqsu5+HMw7Qa6YDvvW3gdo6RR7O60GSeQMJII5lBNF7Ey+s1Hg8MMJxoQ3/rjPDN6dC7V7cFn9fZWNr4PsiSOb5/lro9eaU32lRTRBayRwqYikTD4tX1KkypMcCbSQLpSXtfv1EQFjCKYIKh/THBzZKeod62dWTvM/vRHiokgYN4KrvrNJrHWc4NkviZdtw1XZXZ0DPzR35aYuhs+RtBiw9FBy84KKQ+R6MAvrcd/Ebtj+W+mbdkq84OOJ/3gmcLKgYCZmF4bEPxQjnK1Fd680INqypcZRhj4t7p/LygcSTbHNYAygkXm7p4U5idwG/6978YkVgV50iD+AVEVbRXYjytbzMwfB+ecKMcExx+ToqyQMj1NbWIdvBxVqhSa5tYCibVzLhN/U/Tf/WYjLh+VM7Eg7+QxUE84tT9T5Ij+McwnvAz58fTqM9faYxaCpq8SuqlBMCHLhNdnm/dpo2U99a+oQfJoKsNJfvFS3F2bb5FVtzHvu0qKTCrRX/uJMMGeaxhuK8DfhuHZ9IvsLctcQHSqB6uYWJt4ZKs35wFb2CErhroSGm/AZJMq9JSbI16/V9HtynxAzt+VVNvf/iZZQ9Zssc7WDiSL/EoKAsg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(39860400002)(346002)(376002)(136003)(396003)(230922051799003)(230273577357003)(451199024)(186009)(1800799012)(82310400011)(64100799003)(36840700001)(46966006)(40470700004)(426003)(336012)(6666004)(2616005)(1076003)(83380400001)(26005)(478600001)(41300700001)(36860700001)(8936002)(8676002)(4326008)(36756003)(6916009)(44832011)(356005)(316002)(40460700003)(40480700001)(2906002)(54906003)(70206006)(5660300002)(70586007)(82740400003)(81166007)(30864003)(47076005)(86362001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:35:47.4874 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b1ba2c8c-ddbb-475d-f107-08dc2436eb68 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CDD.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB8919 From: Oleksandr Andrushchenko Take into account guest's BAR view and program its p2m accordingly: gfn is guest's view of the BAR and mfn is the physical BAR value. This way hardware domain sees physical BAR values and guest sees emulated ones. Hardware domain continues getting the BARs identity mapped, while for domUs the BARs are mapped at the requested guest address without modifying the BAR address in the device PCI config space. Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Volodymyr Babchuk Signed-off-by: Stewart Hildebrand Reviewed-by: Roger Pau Monné --- In v12.3: - Update arguments passed to permission error prints in map_range() In v12.2: - Slightly tweak print format in modify_bars() In v12.1: - ASSERT(rangeset_is_empty()) in modify_bars() - Fixup print format in modify_bars() - Make comment single line in bar_write() - Add Roger's R-b In v12: - Update guest_addr in rom_write() - Use unsigned long for start_mfn and map_mfn to reduce mfn_x() calls - Use existing vmsix_table_*() functions - Change vmsix_table_base() to use .guest_addr In v11: - Add vmsix_guest_table_addr() and vmsix_guest_table_base() functions to access guest's view of the VMSIx tables. - Use MFN (not GFN) to check access permissions - Move page offset check to this patch - Call rangeset_remove_range() with correct parameters In v10: - Moved GFN variable definition outside the loop in map_range() - Updated printk error message in map_range() - Now BAR address is always stored in bar->guest_addr, even for HW dom, this removes bunch of ugly is_hwdom() checks in modify_bars() - vmsix_table_base() now uses .guest_addr instead of .addr In v9: - Extended the commit message - Use bar->guest_addr in modify_bars - Extended printk error message in map_range - Moved map_data initialization so .bar can be initialized during declaration Since v5: - remove debug print in map_range callback - remove "identity" from the debug print Since v4: - moved start_{gfn|mfn} calculation into map_range - pass vpci_bar in the map_data instead of start_{gfn|mfn} - s/guest_addr/guest_reg Since v3: - updated comment (Roger) - removed gfn_add(map->start_gfn, rc); which is wrong - use v->domain instead of v->vpci.pdev->domain - removed odd e.g. in comment - s/d%d/%pd in altered code - use gdprintk for map/unmap logs Since v2: - improve readability for data.start_gfn and restructure ?: construct Since v1: - s/MSI/MSI-X in comments --- xen/drivers/vpci/header.c | 83 ++++++++++++++++++++++++++++++--------- xen/include/xen/vpci.h | 3 +- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c index feccd070ddd0..47648c395132 100644 --- a/xen/drivers/vpci/header.c +++ b/xen/drivers/vpci/header.c @@ -34,6 +34,7 @@ struct map_data { struct domain *d; + const struct vpci_bar *bar; bool map; }; @@ -41,26 +42,37 @@ static int cf_check map_range( unsigned long s, unsigned long e, void *data, unsigned long *c) { const struct map_data *map = data; + /* Start address of the BAR as seen by the guest. */ + unsigned long start_gfn = PFN_DOWN(map->bar->guest_addr); + /* Physical start address of the BAR. */ + unsigned long start_mfn = PFN_DOWN(map->bar->addr); int rc; for ( ; ; ) { unsigned long size = e - s + 1; + /* + * Ranges to be mapped don't always start at the BAR start address, as + * there can be holes or partially consumed ranges. Account for the + * offset of the current address from the BAR start. + */ + unsigned long map_mfn = start_mfn + s - start_gfn; + unsigned long m_end = map_mfn + size - 1; - if ( !iomem_access_permitted(map->d, s, e) ) + if ( !iomem_access_permitted(map->d, map_mfn, m_end) ) { printk(XENLOG_G_WARNING "%pd denied access to MMIO range [%#lx, %#lx]\n", - map->d, s, e); + map->d, map_mfn, m_end); return -EPERM; } - rc = xsm_iomem_mapping(XSM_HOOK, map->d, s, e, map->map); + rc = xsm_iomem_mapping(XSM_HOOK, map->d, map_mfn, m_end, map->map); if ( rc ) { printk(XENLOG_G_WARNING "%pd XSM denied access to MMIO range [%#lx, %#lx]: %d\n", - map->d, s, e, rc); + map->d, map_mfn, m_end, rc); return rc; } @@ -73,8 +85,8 @@ static int cf_check map_range( * - {un}map_mmio_regions doesn't support preemption. */ - rc = map->map ? map_mmio_regions(map->d, _gfn(s), size, _mfn(s)) - : unmap_mmio_regions(map->d, _gfn(s), size, _mfn(s)); + rc = map->map ? map_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn)) + : unmap_mmio_regions(map->d, _gfn(s), size, _mfn(map_mfn)); if ( rc == 0 ) { *c += size; @@ -83,8 +95,9 @@ static int cf_check map_range( if ( rc < 0 ) { printk(XENLOG_G_WARNING - "Failed to identity %smap [%lx, %lx] for d%d: %d\n", - map->map ? "" : "un", s, e, map->d->domain_id, rc); + "Failed to %smap [%lx %lx] -> [%lx %lx] for %pd: %d\n", + map->map ? "" : "un", s, e, map_mfn, + map_mfn + size, map->d, rc); break; } ASSERT(rc < size); @@ -163,10 +176,6 @@ static void modify_decoding(const struct pci_dev *pdev, uint16_t cmd, bool vpci_process_pending(struct vcpu *v) { struct pci_dev *pdev = v->vpci.pdev; - struct map_data data = { - .d = v->domain, - .map = v->vpci.cmd & PCI_COMMAND_MEMORY, - }; struct vpci_header *header = NULL; unsigned int i; @@ -186,6 +195,11 @@ bool vpci_process_pending(struct vcpu *v) for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) { struct vpci_bar *bar = &header->bars[i]; + struct map_data data = { + .d = v->domain, + .map = v->vpci.cmd & PCI_COMMAND_MEMORY, + .bar = bar, + }; int rc; if ( rangeset_is_empty(bar->mem) ) @@ -236,7 +250,6 @@ bool vpci_process_pending(struct vcpu *v) static int __init apply_map(struct domain *d, const struct pci_dev *pdev, uint16_t cmd) { - struct map_data data = { .d = d, .map = true }; struct vpci_header *header = &pdev->vpci->header; int rc = 0; unsigned int i; @@ -246,6 +259,7 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev, for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) { struct vpci_bar *bar = &header->bars[i]; + struct map_data data = { .d = d, .map = true, .bar = bar }; if ( rangeset_is_empty(bar->mem) ) continue; @@ -311,12 +325,16 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) * First fill the rangesets with the BAR of this device or with the ROM * BAR only, depending on whether the guest is toggling the memory decode * bit of the command register, or the enable bit of the ROM BAR register. + * + * For non-hardware domain we use guest physical addresses. */ for ( i = 0; i < ARRAY_SIZE(header->bars); i++ ) { struct vpci_bar *bar = &header->bars[i]; unsigned long start = PFN_DOWN(bar->addr); unsigned long end = PFN_DOWN(bar->addr + bar->size - 1); + unsigned long start_guest = PFN_DOWN(bar->guest_addr); + unsigned long end_guest = PFN_DOWN(bar->guest_addr + bar->size - 1); if ( !bar->mem ) continue; @@ -336,11 +354,26 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) continue; } - rc = rangeset_add_range(bar->mem, start, end); + ASSERT(rangeset_is_empty(bar->mem)); + + /* + * Make sure that the guest set address has the same page offset + * as the physical address on the host or otherwise things won't work as + * expected. + */ + if ( PAGE_OFFSET(bar->guest_addr) != PAGE_OFFSET(bar->addr) ) + { + gprintk(XENLOG_G_WARNING, + "%pp: can't map BAR%u - offset mismatch: %#lx vs %#lx\n", + &pdev->sbdf, i, bar->guest_addr, bar->addr); + return -EINVAL; + } + + rc = rangeset_add_range(bar->mem, start_guest, end_guest); if ( rc ) { printk(XENLOG_G_WARNING "Failed to add [%lx, %lx]: %d\n", - start, end, rc); + start_guest, end_guest, rc); return rc; } @@ -352,12 +385,12 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) if ( rangeset_is_empty(prev_bar->mem) ) continue; - rc = rangeset_remove_range(prev_bar->mem, start, end); + rc = rangeset_remove_range(prev_bar->mem, start_guest, end_guest); if ( rc ) { gprintk(XENLOG_WARNING, "%pp: failed to remove overlapping range [%lx, %lx]: %d\n", - &pdev->sbdf, start, end, rc); + &pdev->sbdf, start_guest, end_guest, rc); return rc; } } @@ -425,8 +458,8 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only) for ( i = 0; i < ARRAY_SIZE(tmp->vpci->header.bars); i++ ) { const struct vpci_bar *remote_bar = &tmp->vpci->header.bars[i]; - unsigned long start = PFN_DOWN(remote_bar->addr); - unsigned long end = PFN_DOWN(remote_bar->addr + + unsigned long start = PFN_DOWN(remote_bar->guest_addr); + unsigned long end = PFN_DOWN(remote_bar->guest_addr + remote_bar->size - 1); if ( !remote_bar->enabled ) @@ -513,6 +546,8 @@ static void cf_check bar_write( struct vpci_bar *bar = data; bool hi = false; + ASSERT(is_hardware_domain(pdev->domain)); + if ( bar->type == VPCI_BAR_MEM64_HI ) { ASSERT(reg > PCI_BASE_ADDRESS_0); @@ -543,6 +578,8 @@ static void cf_check bar_write( */ bar->addr &= ~(0xffffffffULL << (hi ? 32 : 0)); bar->addr |= (uint64_t)val << (hi ? 32 : 0); + /* Update guest address, so hardware domain BAR is identity mapped. */ + bar->guest_addr = bar->addr; /* Make sure Xen writes back the same value for the BAR RO bits. */ if ( !hi ) @@ -639,11 +676,14 @@ static void cf_check rom_write( } if ( !rom->enabled ) + { /* * If the ROM BAR is not mapped update the address field so the * correct address is mapped into the p2m. */ rom->addr = val & PCI_ROM_ADDRESS_MASK; + rom->guest_addr = rom->addr; + } if ( !header->bars_mapped || rom->enabled == new_enabled ) { @@ -667,7 +707,10 @@ static void cf_check rom_write( return; if ( !new_enabled ) + { rom->addr = val & PCI_ROM_ADDRESS_MASK; + rom->guest_addr = rom->addr; + } } static int bar_add_rangeset(const struct pci_dev *pdev, struct vpci_bar *bar, @@ -862,6 +905,7 @@ static int cf_check init_header(struct pci_dev *pdev) } bars[i].addr = addr; + bars[i].guest_addr = addr; bars[i].size = size; bars[i].prefetchable = val & PCI_BASE_ADDRESS_MEM_PREFETCH; @@ -884,6 +928,7 @@ static int cf_check init_header(struct pci_dev *pdev) rom->type = VPCI_BAR_ROM; rom->size = size; rom->addr = addr; + rom->guest_addr = addr; header->rom_enabled = pci_conf_read32(pdev->sbdf, rom_reg) & PCI_ROM_ADDRESS_ENABLE; diff --git a/xen/include/xen/vpci.h b/xen/include/xen/vpci.h index 817ee9ee7300..e89c571890b2 100644 --- a/xen/include/xen/vpci.h +++ b/xen/include/xen/vpci.h @@ -216,7 +216,8 @@ int vpci_msix_arch_print(const struct vpci_msix *msix); */ static inline paddr_t vmsix_table_base(const struct vpci *vpci, unsigned int nr) { - return vpci->header.bars[vpci->msix->tables[nr] & PCI_MSIX_BIRMASK].addr; + return vpci->header.bars[vpci->msix->tables[nr] & + PCI_MSIX_BIRMASK].guest_addr; } static inline paddr_t vmsix_table_addr(const struct vpci *vpci, unsigned int nr) From patchwork Fri Feb 2 21:33:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543466 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 91969C48291 for ; Fri, 2 Feb 2024 21:37:32 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675238.1050589 (Exim 4.92) (envelope-from ) id 1rW1Dw-0001d0-Pl; Fri, 02 Feb 2024 21:37:24 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675238.1050589; Fri, 02 Feb 2024 21:37:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1Dw-0001bw-MH; Fri, 02 Feb 2024 21:37:24 +0000 Received: by outflank-mailman (input) for mailman id 675238; Fri, 02 Feb 2024 21:37:22 +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 1rW1Ce-0003tJ-Uy for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:36:05 +0000 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on20631.outbound.protection.outlook.com [2a01:111:f400:7eaa::631]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 1041dd65-c213-11ee-8a45-1f161083a0e0; Fri, 02 Feb 2024 22:36:03 +0100 (CET) Received: from BY3PR05CA0005.namprd05.prod.outlook.com (2603:10b6:a03:254::10) by DS7PR12MB5838.namprd12.prod.outlook.com (2603:10b6:8:79::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.35; Fri, 2 Feb 2024 21:36:00 +0000 Received: from SJ1PEPF00001CE0.namprd05.prod.outlook.com (2603:10b6:a03:254:cafe::2a) by BY3PR05CA0005.outlook.office365.com (2603:10b6:a03:254::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.22 via Frontend Transport; Fri, 2 Feb 2024 21:36:00 +0000 Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CE0.mail.protection.outlook.com (10.167.242.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7202.16 via Frontend Transport; Fri, 2 Feb 2024 21:35:59 +0000 Received: from SATLEXMB05.amd.com (10.181.40.146) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:35:59 -0600 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB05.amd.com (10.181.40.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:35:59 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:35:57 -0600 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: 1041dd65-c213-11ee-8a45-1f161083a0e0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oNzwinYcJJSme46a4s+2b94tFowL2cHyt5VCYSpQoKQcfmj2TJwRIEklsjENqH//1ghEVxT6jJfsq1APAh2fawq8oTsH1R8HdwTTzNtxejL67M5U7iOY0xf1RsxjzT1m/J2ckE+pDIv4hZsbzSPvWGxv/kewE/cb0i+ccj1ZoA+UYNT1VlyZptSt+VS+qWe6BAB+6+VK7AMjxFsemZigZLvzR/wtCeQMbeUP5THvVjpzIb9LqLjYDMKy12Mowoy43fxxpAxDyxOVkm6WQsKGi/rQuQiLOYGflvPZQdm8ngAefmmc7fhqCA5zVY7lEIKWrYz1HEo0LvHY4QwjJ/ttvg== 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=fhjSNHqgk5XVfapJjt8Oh6Wc0r8aqkJq3Ia//oNN7uA=; b=VRHaH297bBHkpV6L1/K1meALhKCHNchBqDpl8wgRg7u8e6sDNUWat4FdQH+TnCTAI92+SK/DnvmDLQ08HWYy5SpNParYvRuwfyeod22YpDuj4uCL+2USLhUhgODAAPb8xrA1dEk5+rSDLz1DJBaL/+aCDCy1tJRj5TLQo38gzDg5AQFFN1jb+pScRHpq7xRYjn3SEryst7CKIq0+MDMZFXzNN4Xjf8TkM8Jqkhidnu4vxztF7NlAx2otXIfO5fQY93QSWoG+llFH80dgwP78evDwt0Y6pPDOmZyAV+G6wygtlYZz2bQh9girWPOBrCjhL7ADXFIehe0BwallzkE8hw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fhjSNHqgk5XVfapJjt8Oh6Wc0r8aqkJq3Ia//oNN7uA=; b=TfKRBo/y1ROvr6RlmamvsDobCH6N1C4xZz72Im24s363UzPya9f/+J79xlXa4n/CPfdCAQpIEpbXWVg+gN9PozKuk/7RmakwSR/lp7GazetnovbfYcr0kIYI/KvZvb/h/p3WpdAkn3pHnMI85KQd6iWHCSvSrofwiSDVDuAxs1U= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C From: Stewart Hildebrand To: CC: Oleksandr Andrushchenko , =?utf-8?q?Ro?= =?utf-8?q?ger_Pau_Monn=C3=A9?= , Andrew Cooper , George Dunlap , "Jan Beulich" , Julien Grall , "Stefano Stabellini" , Wei Liu , Volodymyr Babchuk , Stewart Hildebrand Subject: [PATCH v13 10/14] vpci/header: emulate PCI_COMMAND register for guests Date: Fri, 2 Feb 2024 16:33:14 -0500 Message-ID: <20240202213321.1920347-11-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CE0:EE_|DS7PR12MB5838:EE_ X-MS-Office365-Filtering-Correlation-Id: d3b26f68-4d2e-4929-265e-08dc2436f2d4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ijowIiVS/xfTikK2Q9f1ShhLcqaELW+irl/FfF5KZpv3e7E7iYqkEeaDmn3VUiakugk2W0PM7soeVn3L75KOJquyC6YdEMJsjXCX10PrUYbKkPrb4abuk2H+oW9W8IXgTNI5kicAk2pfggpGlca/4rqz++Fed7BoXi0wkbY3nBHTmrbW+JZLV72nxGob81/G5eI1VnbE0LiB7ZJOdSMMoanzrtUWO93zz4h0MC81g5y4BTwY65B9MFHfZK9sh82QjcOkLTC7L7ZOc1TM6xeZdOJR3R1xt9YF6RR8OwxldG+T62EQ8y1oqZvMkjpEgyhwJwrOAZDzGakMzoKWzRk3nd/TYrbOzE7nrzAFVuddyUHm5ciPJ97dyOIdV7GRiKghESiHJnyiPtiiRYbuCtOXmWLp1BI1oLXTV8GbiMjA5np21mkcf+oCIH1IkJ6oP0X7rGlsW/fOEX92KR1GLzarjsLibV5Df9JicH0s2vzg6jpfKYxPN2GvvpprVu+2dCmiecfYYSEP+vfaJGIZZbutea5IejOOwtoqv3I2Yr4Mvqe0EJCJLYGQWbqVeZpKEkexhHQGfOgL4HLy4ijzLv5X5p0QfIt+WyTn6fCfmork79RWEiNzDK/V7R4VhSqrRsxTj/0LuuO3QT2rELBO1CncEdjAuh0EFLAGYnh0ei7Ye+4ZnhLcYA7MZQDEvPm1H9TNET9afsX8uoTWH+0tRiMS2xitUA24d16tSDyObQkQPKnQYKyvO+TMvBy2cZwIFETJbVnIooc3277cbFUbgVaQtA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(39860400002)(136003)(346002)(376002)(396003)(230273577357003)(230922051799003)(186009)(82310400011)(64100799003)(451199024)(1800799012)(40470700004)(36840700001)(46966006)(44832011)(82740400003)(54906003)(70586007)(8676002)(86362001)(2906002)(8936002)(30864003)(5660300002)(70206006)(4326008)(7416002)(36860700001)(478600001)(6916009)(36756003)(316002)(81166007)(966005)(6666004)(47076005)(356005)(83380400001)(26005)(336012)(41300700001)(40480700001)(40460700003)(1076003)(2616005)(426003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:35:59.9341 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d3b26f68-4d2e-4929-265e-08dc2436f2d4 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CE0.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB5838 From: Oleksandr Andrushchenko Xen and/or Dom0 may have put values in PCI_COMMAND which they expect to remain unaltered. PCI_COMMAND_SERR bit is a good example: while the guest's (domU) view of this will want to be zero (for now), the host having set it to 1 should be preserved, or else we'd effectively be giving the domU control of the bit. Thus, PCI_COMMAND register needs proper emulation in order to honor host's settings. According to "PCI LOCAL BUS SPECIFICATION, REV. 3.0", section "6.2.2 Device Control" the reset state of the command register is typically 0, so when assigning a PCI device use 0 as the initial state for the guest's (domU) view of the command register. Here is the full list of command register bits with notes about PCI/PCIe specification, and how Xen handles the bit. QEMU's behavior is also documented here since that is our current reference implementation for PCI passthrough. PCI_COMMAND_IO (bit 0) PCIe 6.1: RW PCI LB 3.0: RW QEMU: (emu_mask) QEMU provides an emulated view of this bit. Guest writes do not propagate to hardware. QEMU sets this bit to 1 in hardware if an I/O BAR is exposed to the guest. Xen domU: (rsvdp_mask) We treat this bit as RsvdP for now since we don't yet support I/O BARs for domUs. Xen dom0: We allow dom0 to control this bit freely. PCI_COMMAND_MEMORY (bit 1) PCIe 6.1: RW PCI LB 3.0: RW QEMU: (emu_mask) QEMU provides an emulated view of this bit. Guest writes do not propagate to hardware. QEMU sets this bit to 1 in hardware if a Memory BAR is exposed to the guest. Xen domU/dom0: We handle writes to this bit by mapping/unmapping BAR regions. Xen domU: For devices assigned to DomUs, memory decoding will be disabled at the time of initialization. PCI_COMMAND_MASTER (bit 2) PCIe 6.1: RW PCI LB 3.0: RW QEMU: Pass through writes to hardware. Xen domU/dom0: Pass through writes to hardware. PCI_COMMAND_SPECIAL (bit 3) PCIe 6.1: RO, hardwire to 0 PCI LB 3.0: RW QEMU: Pass through writes to hardware. Xen domU/dom0: Pass through writes to hardware. PCI_COMMAND_INVALIDATE (bit 4) PCIe 6.1: RO, hardwire to 0 PCI LB 3.0: RW QEMU: Pass through writes to hardware. Xen domU/dom0: Pass through writes to hardware. PCI_COMMAND_VGA_PALETTE (bit 5) PCIe 6.1: RO, hardwire to 0 PCI LB 3.0: RW QEMU: Pass through writes to hardware. Xen domU/dom0: Pass through writes to hardware. PCI_COMMAND_PARITY (bit 6) PCIe 6.1: RW PCI LB 3.0: RW QEMU: (emu_mask) QEMU provides an emulated view of this bit. Guest writes do not propagate to hardware. Xen domU: (rsvdp_mask) We treat this bit as RsvdP. Xen dom0: We allow dom0 to control this bit freely. PCI_COMMAND_WAIT (bit 7) PCIe 6.1: RO, hardwire to 0 PCI LB 3.0: hardwire to 0 QEMU: res_mask Xen domU: (rsvdp_mask) We treat this bit as RsvdP. Xen dom0: We allow dom0 to control this bit freely. PCI_COMMAND_SERR (bit 8) PCIe 6.1: RW PCI LB 3.0: RW QEMU: (emu_mask) QEMU provides an emulated view of this bit. Guest writes do not propagate to hardware. Xen domU: (rsvdp_mask) We treat this bit as RsvdP. Xen dom0: We allow dom0 to control this bit freely. PCI_COMMAND_FAST_BACK (bit 9) PCIe 6.1: RO, hardwire to 0 PCI LB 3.0: RW QEMU: (emu_mask) QEMU provides an emulated view of this bit. Guest writes do not propagate to hardware. Xen domU: (rsvdp_mask) We treat this bit as RsvdP. Xen dom0: We allow dom0 to control this bit freely. PCI_COMMAND_INTX_DISABLE (bit 10) PCIe 6.1: RW PCI LB 3.0: RW QEMU: (emu_mask) QEMU provides an emulated view of this bit. Guest writes do not propagate to hardware. QEMU checks if INTx was mapped for a device. If it is not, then guest can't control PCI_COMMAND_INTX_DISABLE bit. Xen domU: We prohibit a guest from enabling INTx if MSI(X) is enabled. Xen dom0: We allow dom0 to control this bit freely. Bits 11-15 PCIe 6.1: RsvdP PCI LB 3.0: Reserved QEMU: res_mask Xen domU/dom0: rsvdp_mask Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Volodymyr Babchuk Signed-off-by: Stewart Hildebrand --- RFC: There is an unaddressed question for Roger: should we update the guest view of the PCI_COMMAND_INTX_DISABLE bit in msi.c/msix.c:control_write()? See prior discussion at [1]. [1] https://lore.kernel.org/xen-devel/86b25777-788c-4b9a-8166-a6f8174bedc9@suse.com/ In v13: - Update right away (don't defer) PCI_COMMAND_MEMORY bit in guest_cmd variable in cmd_write() - Make comment single line in xen/drivers/vpci/msi.c:control_write() - Rearrange memory decoding disabling snippet in init_header() In v12: - Rework patch using vpci_add_register_mask() - Add bitmask #define in pci_regs.h according to PCIe 6.1 spec, except don't add the RO bits because they were RW in PCI LB 3.0 spec. - Move and expand TODO comment about properly emulating bits - Update guest_cmd in msi.c/msix.c:control_write() - Simplify cmd_write(), thanks to rsvdp_mask - Update commit description In v11: - Fix copy-paste mistake: vpci->msi should be vpci->msix - Handle PCI_COMMAND_IO - Fix condition for disabling INTx in the MSI-X code - Show domU changes to only allowed bits - Show PCI_COMMAND_MEMORY write only after P2M was altered - Update comments in the code In v10: - Added cf_check attribute to guest_cmd_read - Removed warning about non-zero cmd - Updated comment MSI code regarding disabling INTX - Used ternary operator in vpci_add_register() call - Disable memory decoding for DomUs in init_bars() In v9: - Reworked guest_cmd_read - Added handling for more bits Since v6: - fold guest's logic into cmd_write - implement cmd_read, so we can report emulated INTx state to guests - introduce header->guest_cmd to hold the emulated state of the PCI_COMMAND register for guests Since v5: - add additional check for MSI-X enabled while altering INTX bit - make sure INTx disabled while guests enable MSI/MSI-X Since v3: - gate more code on CONFIG_HAS_MSI - removed logic for the case when MSI/MSI-X not enabled --- xen/drivers/vpci/header.c | 57 ++++++++++++++++++++++++++++++++++---- xen/drivers/vpci/msi.c | 7 +++++ xen/drivers/vpci/msix.c | 7 +++++ xen/include/xen/pci_regs.h | 1 + xen/include/xen/vpci.h | 3 ++ 5 files changed, 69 insertions(+), 6 deletions(-) diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c index 47648c395132..5d13ea975cc2 100644 --- a/xen/drivers/vpci/header.c +++ b/xen/drivers/vpci/header.c @@ -524,9 +524,21 @@ static void cf_check cmd_write( { struct vpci_header *header = data; + if ( !is_hardware_domain(pdev->domain) ) + { + const struct vpci *vpci = pdev->vpci; + + if ( (vpci->msi && vpci->msi->enabled) || + (vpci->msix && vpci->msix->enabled) ) + cmd |= PCI_COMMAND_INTX_DISABLE; + + header->guest_cmd = cmd; + } + /* * Let Dom0 play with all the bits directly except for the memory - * decoding one. + * decoding one. Bits that are not allowed for DomU are already + * handled above and by the rsvdp_mask. */ if ( header->bars_mapped != !!(cmd & PCI_COMMAND_MEMORY) ) /* @@ -540,6 +552,14 @@ static void cf_check cmd_write( pci_conf_write16(pdev->sbdf, reg, cmd); } +static uint32_t cf_check guest_cmd_read( + const struct pci_dev *pdev, unsigned int reg, void *data) +{ + const struct vpci_header *header = data; + + return header->guest_cmd; +} + static void cf_check bar_write( const struct pci_dev *pdev, unsigned int reg, uint32_t val, void *data) { @@ -754,9 +774,23 @@ static int cf_check init_header(struct pci_dev *pdev) return -EOPNOTSUPP; } - /* Setup a handler for the command register. */ - rc = vpci_add_register(pdev->vpci, vpci_hw_read16, cmd_write, PCI_COMMAND, - 2, header); + /* + * Setup a handler for the command register. + * + * TODO: If support for emulated bits is added, re-visit how to handle + * PCI_COMMAND_PARITY, PCI_COMMAND_SERR, and PCI_COMMAND_FAST_BACK. + */ + rc = vpci_add_register_mask(pdev->vpci, + is_hwdom ? vpci_hw_read16 : guest_cmd_read, + cmd_write, PCI_COMMAND, 2, header, 0, 0, + PCI_COMMAND_RSVDP_MASK | + (is_hwdom ? 0 + : PCI_COMMAND_IO | + PCI_COMMAND_PARITY | + PCI_COMMAND_WAIT | + PCI_COMMAND_SERR | + PCI_COMMAND_FAST_BACK), + 0); if ( rc ) return rc; @@ -836,9 +870,20 @@ static int cf_check init_header(struct pci_dev *pdev) if ( pdev->ignore_bars ) return 0; - /* Disable memory decoding before sizing. */ cmd = pci_conf_read16(pdev->sbdf, PCI_COMMAND); - if ( cmd & PCI_COMMAND_MEMORY ) + + /* + * Clear PCI_COMMAND_MEMORY and PCI_COMMAND_IO for DomUs, so they will + * always start with memory decoding disabled and to ensure that we will not + * call modify_bars() at the end of this function. + */ + if ( !is_hwdom ) + cmd &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO); + + header->guest_cmd = cmd; + + /* Disable memory decoding before sizing. */ + if ( !is_hwdom || (cmd & PCI_COMMAND_MEMORY) ) pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd & ~PCI_COMMAND_MEMORY); for ( i = 0; i < num_bars; i++ ) diff --git a/xen/drivers/vpci/msi.c b/xen/drivers/vpci/msi.c index dc71938e23f5..1e747ad9bba8 100644 --- a/xen/drivers/vpci/msi.c +++ b/xen/drivers/vpci/msi.c @@ -70,6 +70,13 @@ static void cf_check control_write( if ( vpci_msi_arch_enable(msi, pdev, vectors) ) return; + + /* Make sure domU doesn't enable INTx while enabling MSI. */ + if ( !is_hardware_domain(pdev->domain) ) + { + pci_intx(pdev, false); + pdev->vpci->header.guest_cmd |= PCI_COMMAND_INTX_DISABLE; + } } else vpci_msi_arch_disable(msi, pdev); diff --git a/xen/drivers/vpci/msix.c b/xen/drivers/vpci/msix.c index b6abab47efdd..d4f756ce287a 100644 --- a/xen/drivers/vpci/msix.c +++ b/xen/drivers/vpci/msix.c @@ -135,6 +135,13 @@ static void cf_check control_write( } } + /* Make sure domU doesn't enable INTx while enabling MSI-X. */ + if ( new_enabled && !msix->enabled && !is_hardware_domain(pdev->domain) ) + { + pci_intx(pdev, false); + pdev->vpci->header.guest_cmd |= PCI_COMMAND_INTX_DISABLE; + } + msix->masked = new_masked; msix->enabled = new_enabled; diff --git a/xen/include/xen/pci_regs.h b/xen/include/xen/pci_regs.h index 9909b27425a5..b2f2e43e864d 100644 --- a/xen/include/xen/pci_regs.h +++ b/xen/include/xen/pci_regs.h @@ -48,6 +48,7 @@ #define PCI_COMMAND_SERR 0x100 /* Enable SERR */ #define PCI_COMMAND_FAST_BACK 0x200 /* Enable back-to-back writes */ #define PCI_COMMAND_INTX_DISABLE 0x400 /* INTx Emulation Disable */ +#define PCI_COMMAND_RSVDP_MASK 0xf800 #define PCI_STATUS 0x06 /* 16 bits */ #define PCI_STATUS_IMM_READY 0x01 /* Immediate Readiness */ diff --git a/xen/include/xen/vpci.h b/xen/include/xen/vpci.h index e89c571890b2..77320a667e55 100644 --- a/xen/include/xen/vpci.h +++ b/xen/include/xen/vpci.h @@ -107,6 +107,9 @@ struct vpci { } bars[PCI_HEADER_NORMAL_NR_BARS + 1]; /* At most 6 BARS + 1 expansion ROM BAR. */ + /* Guest (domU only) view of the PCI_COMMAND register. */ + uint16_t guest_cmd; + /* * Store whether the ROM enable bit is set (doesn't imply ROM BAR * is mapped into guest p2m) if there's a ROM BAR on the device. From patchwork Fri Feb 2 21:33:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543467 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 DFB82C48298 for ; Fri, 2 Feb 2024 21:37:33 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675239.1050597 (Exim 4.92) (envelope-from ) id 1rW1Dx-0001ix-Bi; Fri, 02 Feb 2024 21:37:25 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675239.1050597; Fri, 02 Feb 2024 21:37:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1Dx-0001hC-2M; Fri, 02 Feb 2024 21:37:25 +0000 Received: by outflank-mailman (input) for mailman id 675239; Fri, 02 Feb 2024 21:37:23 +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 1rW1Cv-0003tJ-8L for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:36:21 +0000 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on20600.outbound.protection.outlook.com [2a01:111:f403:2412::600]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 1a1ed3fc-c213-11ee-8a45-1f161083a0e0; Fri, 02 Feb 2024 22:36:20 +0100 (CET) Received: from BYAPR21CA0029.namprd21.prod.outlook.com (2603:10b6:a03:114::39) by CH2PR12MB4924.namprd12.prod.outlook.com (2603:10b6:610:6b::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7270.11; Fri, 2 Feb 2024 21:36:13 +0000 Received: from SJ1PEPF00001CE1.namprd05.prod.outlook.com (2603:10b6:a03:114:cafe::97) by BYAPR21CA0029.outlook.office365.com (2603:10b6:a03:114::39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7270.6 via Frontend Transport; Fri, 2 Feb 2024 21:36:12 +0000 Received: from SATLEXMB04.amd.com (165.204.84.17) by SJ1PEPF00001CE1.mail.protection.outlook.com (10.167.242.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7202.16 via Frontend Transport; Fri, 2 Feb 2024 21:36:12 +0000 Received: from SATLEXMB08.amd.com (10.181.40.132) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:36:11 -0600 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB08.amd.com (10.181.40.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32; Fri, 2 Feb 2024 13:36:11 -0800 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:36:10 -0600 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: 1a1ed3fc-c213-11ee-8a45-1f161083a0e0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IwOOgn2rYg5D/WvmsCUT8qoBhJQrAtgLYklDazN56h+7gozf/U1RXmKI9yOBn5UqYAU5VAakFNeEQklGiMySOeEe27427haZH6cPdzyj84+ZLcraC9IHApv2uHUGUK1ZXXX7JDojZjEvw4vLosNgA14rPUSf6V1RdYfeoJsiCyRaw7nwdlYqja/AFsgGDH4UO+InEeAA88jg2ZOVTGUjouSDuJLhCxV5cRnygN0uEK9rTSghmt3p2jYxHjUktqYMUb49MC0O1cJa4W5ilxL3rVIU4IsRYZcnWLzx7nOTyXpO35yd4WLZHxxLBGd+JjWmMjQy+mVQ4Br+K05jpmAcEQ== 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=G/OcqFAAiB6EoPEXzP0cP8TrQpQFguNrAiXOKI4bieI=; b=ct4wHl7v6EYAOeYVrA88XoHXd22z7VpeZbEU571h3N/MsRtBIqrmYgUlgfWjRYtxGiz7ribJn86J6rcBlGtzanWOguvVJUKUQqOTJ4R6lRDQJDzjYTE4VBqcK70t3GMzIwjGWmGWOeIkvRO8YMgddvMfXcCLeXob2y4S6WGn50TkRIIqdVFhibehf3xRpEKEAIdOR2KxEMb8sq12em5i9kVF5wMaq9dzt7CZC9YAR2nJMxREHG1ZajJ7HMlDtPt6XrHiq0DjNY9sQF/bo/ITs10sTJ2dxLwvpU14sL8H2zzknNv6DqQWPHOE9LIEn+UnbzMdY3Xq98exWewfHpnwiw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=G/OcqFAAiB6EoPEXzP0cP8TrQpQFguNrAiXOKI4bieI=; b=Ude+pJ4U0Qn5DhphKlbgGg2PKATdoQiMFuxK4mtdj3cw/UCnS4hzOJrEsW2X8pU0aIdOhHwVeQ6AeHBPp4eRglz0Da00i5p4233HxvJERi7ErKnQ3WVuFQ9iR1SFlnF2mGSCOzH1WTdiIcFAy4mzYn9ggVfEgri2WXD6deZ9dnU= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C From: Stewart Hildebrand To: CC: Oleksandr Andrushchenko , Andrew Cooper , George Dunlap , "Jan Beulich" , Julien Grall , "Stefano Stabellini" , Wei Liu , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , "Volodymyr Babchuk" , Stewart Hildebrand Subject: [PATCH v13 11/14] vpci: add initial support for virtual PCI bus topology Date: Fri, 2 Feb 2024 16:33:15 -0500 Message-ID: <20240202213321.1920347-12-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00001CE1:EE_|CH2PR12MB4924:EE_ X-MS-Office365-Filtering-Correlation-Id: 9ce17121-21b0-493f-f0fa-08dc2436fa45 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1LPvKEVU8yLAbZTsAkiEDS9+ieooTes7LRyUKxTsETRdw5IcipVtDLAG/y7h1vtY5UP9KnITXORg1yGxABVX57lU6ctZ/6Ce3VzzTVF3p0BpqtA/vxGIHUjUegat5JZkFLeJgHB218YTmS0UP8npI6PXXm0lGuaCvGVIhlfGD9xXFkNQ35VzqWlRi/BSdQvR/1MfH/cWS90HajWBXS/9GKKrU8Njj3JlQxOj9w7OszD0ky3jQDea7wSJaUT9kw5l7x2pD/uEg+c+I+YrcmqF2KK2Vs2ABINHfov+mjpuyFmPIERDW7ZPHr2hqsalJoFWPmnB8A5jkNoUUo3TvvcMpczlvEd6ptOZKN4kw3KcZqe3UNKcomkdgJTfcTf3fHWKhhe6MqoO0njB4khaI1hGAof2thp8HLpVBwpaFS0SyB9jl/sbJsa2KZzJdIC6cm9vGr0axmC8S6/gyJyYq0SQA/Y0f6XaqEmfJYybM3GQuUU8ZVyJvJjGKk4355IMCA96b/5gSAAmLmyQVpZEm5zm31hLISj/AQfv7qu+ZghCM1TM3Ne3p6FBhWC2qWKcOnrLHll6d7hUcTpPJdZRIhdiG11ES6oHYvT/sF07ThtBmvs01zGPUcsvxed2hed1KhY6s1ZONhuwvxtvKQj/sCKDlr/Jwt9nfqL64gdNWWB9D01s46/Sqj3xyeki+jMJr3cBpN9iUvPta/OBdaN58EicKoUh+wk69gFCiaQGXNu+lDQxp/jmTf7Ne07CYd1XTqF09/ZngU1NwlVOL7/4ldtJHA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(376002)(136003)(346002)(396003)(39860400002)(230922051799003)(186009)(82310400011)(451199024)(64100799003)(1800799012)(36840700001)(40470700004)(46966006)(83380400001)(26005)(1076003)(47076005)(2616005)(36860700001)(426003)(8936002)(70206006)(7416002)(8676002)(44832011)(81166007)(4326008)(82740400003)(2906002)(316002)(54906003)(5660300002)(6916009)(478600001)(70586007)(36756003)(41300700001)(336012)(356005)(86362001)(40460700003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:36:12.4083 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9ce17121-21b0-493f-f0fa-08dc2436fa45 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00001CE1.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4924 From: Oleksandr Andrushchenko Assign SBDF to the PCI devices being passed through with bus 0. The resulting topology is where PCIe devices reside on the bus 0 of the root complex itself (embedded endpoints). This implementation is limited to 32 devices which are allowed on a single PCI bus. Please note, that at the moment only function 0 of a multifunction device can be passed through. Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Volodymyr Babchuk Signed-off-by: Stewart Hildebrand --- In v13: - s/depends on/select/ in Kconfig - check pdev->sbdf.fn instead of two booleans in add_virtual_device() - comment #endifs in sched.h - clarify comment about limits in vpci.h with seg/bus limit In v11: - Fixed code formatting - Removed bogus write_unlock() call - Fixed type for new_dev_number In v10: - Removed ASSERT(pcidevs_locked()) - Removed redundant code (local sbdf variable, clearing sbdf during device removal, etc) - Added __maybe_unused attribute to "out:" label - Introduced HAS_VPCI_GUEST_SUPPORT Kconfig option, as this is the first patch where it is used (previously was in "vpci: add hooks for PCI device assign/de-assign") In v9: - Lock in add_virtual_device() replaced with ASSERT (thanks, Stewart) In v8: - Added write lock in add_virtual_device Since v6: - re-work wrt new locking scheme - OT: add ASSERT(pcidevs_write_locked()); to add_virtual_device() Since v5: - s/vpci_add_virtual_device/add_virtual_device and make it static - call add_virtual_device from vpci_assign_device and do not use REGISTER_VPCI_INIT machinery - add pcidevs_locked ASSERT - use DECLARE_BITMAP for vpci_dev_assigned_map Since v4: - moved and re-worked guest sbdf initializers - s/set_bit/__set_bit - s/clear_bit/__clear_bit - minor comment fix s/Virtual/Guest/ - added VPCI_MAX_VIRT_DEV constant (PCI_SLOT(~0) + 1) which will be used later for counting the number of MMIO handlers required for a guest (Julien) Since v3: - make use of VPCI_INIT - moved all new code to vpci.c which belongs to it - changed open-coded 31 to PCI_SLOT(~0) - added comments and code to reject multifunction devices with functions other than 0 - updated comment about vpci_dev_next and made it unsigned int - implement roll back in case of error while assigning/deassigning devices - s/dom%pd/%pd Since v2: - remove casts that are (a) malformed and (b) unnecessary - add new line for better readability - remove CONFIG_HAS_VPCI_GUEST_SUPPORT ifdef's as the relevant vPCI functions are now completely gated with this config - gate common code with CONFIG_HAS_VPCI_GUEST_SUPPORT New in v2 --- xen/drivers/Kconfig | 4 +++ xen/drivers/vpci/vpci.c | 57 +++++++++++++++++++++++++++++++++++++++++ xen/include/xen/sched.h | 10 +++++++- xen/include/xen/vpci.h | 12 +++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) diff --git a/xen/drivers/Kconfig b/xen/drivers/Kconfig index db94393f47a6..20050e9bb8b3 100644 --- a/xen/drivers/Kconfig +++ b/xen/drivers/Kconfig @@ -15,4 +15,8 @@ source "drivers/video/Kconfig" config HAS_VPCI bool +config HAS_VPCI_GUEST_SUPPORT + bool + select HAS_VPCI + endmenu diff --git a/xen/drivers/vpci/vpci.c b/xen/drivers/vpci/vpci.c index 260b72875ee1..3cd142068f4e 100644 --- a/xen/drivers/vpci/vpci.c +++ b/xen/drivers/vpci/vpci.c @@ -40,6 +40,49 @@ extern vpci_register_init_t *const __start_vpci_array[]; extern vpci_register_init_t *const __end_vpci_array[]; #define NUM_VPCI_INIT (__end_vpci_array - __start_vpci_array) +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +static int add_virtual_device(struct pci_dev *pdev) +{ + struct domain *d = pdev->domain; + unsigned int new_dev_number; + + if ( is_hardware_domain(d) ) + return 0; + + ASSERT(rw_is_write_locked(&pdev->domain->pci_lock)); + + /* + * Each PCI bus supports 32 devices/slots at max or up to 256 when + * there are multi-function ones which are not yet supported. + */ + if ( pdev->sbdf.fn ) + { + gdprintk(XENLOG_ERR, "%pp: only function 0 passthrough supported\n", + &pdev->sbdf); + return -EOPNOTSUPP; + } + new_dev_number = find_first_zero_bit(d->vpci_dev_assigned_map, + VPCI_MAX_VIRT_DEV); + if ( new_dev_number == VPCI_MAX_VIRT_DEV ) + return -ENOSPC; + + __set_bit(new_dev_number, &d->vpci_dev_assigned_map); + + /* + * Both segment and bus number are 0: + * - we emulate a single host bridge for the guest, e.g. segment 0 + * - with bus 0 the virtual devices are seen as embedded + * endpoints behind the root complex + * + * TODO: add support for multi-function devices. + */ + pdev->vpci->guest_sbdf = PCI_SBDF(0, 0, new_dev_number, 0); + + return 0; +} + +#endif /* CONFIG_HAS_VPCI_GUEST_SUPPORT */ + void vpci_deassign_device(struct pci_dev *pdev) { unsigned int i; @@ -49,6 +92,12 @@ void vpci_deassign_device(struct pci_dev *pdev) if ( !has_vpci(pdev->domain) || !pdev->vpci ) return; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT + if ( pdev->vpci->guest_sbdf.sbdf != ~0 ) + __clear_bit(pdev->vpci->guest_sbdf.dev, + &pdev->domain->vpci_dev_assigned_map); +#endif + spin_lock(&pdev->vpci->lock); while ( !list_empty(&pdev->vpci->handlers) ) { @@ -105,6 +154,13 @@ int vpci_assign_device(struct pci_dev *pdev) INIT_LIST_HEAD(&pdev->vpci->handlers); spin_lock_init(&pdev->vpci->lock); +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT + pdev->vpci->guest_sbdf.sbdf = ~0; + rc = add_virtual_device(pdev); + if ( rc ) + goto out; +#endif + for ( i = 0; i < NUM_VPCI_INIT; i++ ) { rc = __start_vpci_array[i](pdev); @@ -112,6 +168,7 @@ int vpci_assign_device(struct pci_dev *pdev) break; } + out: __maybe_unused; if ( rc ) vpci_deassign_device(pdev); diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index c3adec1aca3c..daa3c1743047 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -484,7 +484,15 @@ struct domain * 2. pdev->vpci->lock */ rwlock_t pci_lock; -#endif +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT + /* + * The bitmap which shows which device numbers are already used by the + * virtual PCI bus topology and is used to assign a unique SBDF to the + * next passed through virtual PCI device. + */ + DECLARE_BITMAP(vpci_dev_assigned_map, VPCI_MAX_VIRT_DEV); +#endif /* CONFIG_HAS_VPCI_GUEST_SUPPORT */ +#endif /* CONFIG_HAS_PCI */ #ifdef CONFIG_HAS_PASSTHROUGH struct domain_iommu iommu; diff --git a/xen/include/xen/vpci.h b/xen/include/xen/vpci.h index 77320a667e55..a69c640d065a 100644 --- a/xen/include/xen/vpci.h +++ b/xen/include/xen/vpci.h @@ -21,6 +21,14 @@ typedef int vpci_register_init_t(struct pci_dev *dev); #define VPCI_ECAM_BDF(addr) (((addr) & 0x0ffff000) >> 12) +/* + * Maximum number of devices supported by the virtual bus topology: + * each PCI bus supports 32 devices/slots at max or up to 256 when + * there are multi-function ones which are not yet supported. + * This limit implies only segment 0, bus 0 is supported. + */ +#define VPCI_MAX_VIRT_DEV (PCI_SLOT(~0) + 1) + #define REGISTER_VPCI_INIT(x, p) \ static vpci_register_init_t *const x##_entry \ __used_section(".data.vpci." p) = x @@ -175,6 +183,10 @@ struct vpci { struct vpci_arch_msix_entry arch; } entries[]; } *msix; +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT + /* Guest SBDF of the device. */ + pci_sbdf_t guest_sbdf; +#endif #endif }; From patchwork Fri Feb 2 21:33:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543468 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 B81EAC4828F for ; Fri, 2 Feb 2024 21:37:34 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675241.1050609 (Exim 4.92) (envelope-from ) id 1rW1Dy-0002FO-Sw; Fri, 02 Feb 2024 21:37:26 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675241.1050609; Fri, 02 Feb 2024 21:37:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1Dy-0002Dw-LN; Fri, 02 Feb 2024 21:37:26 +0000 Received: by outflank-mailman (input) for mailman id 675241; Fri, 02 Feb 2024 21:37:24 +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 1rW1D8-0003tJ-FF for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:36:34 +0000 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on20600.outbound.protection.outlook.com [2a01:111:f403:2412::600]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 22084f02-c213-11ee-8a45-1f161083a0e0; Fri, 02 Feb 2024 22:36:33 +0100 (CET) Received: from DM6PR02CA0161.namprd02.prod.outlook.com (2603:10b6:5:332::28) by SN7PR12MB7370.namprd12.prod.outlook.com (2603:10b6:806:299::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.24; Fri, 2 Feb 2024 21:36:29 +0000 Received: from CY4PEPF0000E9DB.namprd05.prod.outlook.com (2603:10b6:5:332:cafe::78) by DM6PR02CA0161.outlook.office365.com (2603:10b6:5:332::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.26 via Frontend Transport; Fri, 2 Feb 2024 21:36:29 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by CY4PEPF0000E9DB.mail.protection.outlook.com (10.167.241.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7249.19 via Frontend Transport; Fri, 2 Feb 2024 21:36:29 +0000 Received: from SATLEXMB05.amd.com (10.181.40.146) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:36:28 -0600 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB05.amd.com (10.181.40.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:36:27 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:36:21 -0600 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: 22084f02-c213-11ee-8a45-1f161083a0e0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fGmoGsXwDOzFWKt37IsnRD24EDWaF5PhKPWGQNi5TAC0gAtFWizs3+A95NEYtFB7zSLBcHvHtqolVckoeMcYsLu1lyMLld7qHvWiHh/sjVHATYBlXrUmrC3t/VVUsx59Lr7cy0xggMpfFA5mnjFNZ8Puobo+Nhuw4TSDqwxKRS9GGNeVAnEJs8IFKyii9awFkpw7jLjVNldn6tDFiE6/UdR8870iH/UWckP1uIaCy3bJTV4qRv1Ea1phYWK3lK/oYHeDDdo8RuLZZaYB+nRZWPvRzoKL/+Si4FmCQb7RRjg0hYTFvdjzvbmFbIe/N+suBLFwE/nfnTqdR0RpZUSC1A== 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=eJMXVdcAYBu7iSzlaGDF0CzLtmxwO7mBpjOdIW/Bo5w=; b=jsI752toqx8WVIw2uuGDY23GacvmXh7F4Qpl8cSJvbmnqa7R8wVctj/6cIgif5Oua0d1mbIZgaQSUfKGQUFMwiy5BWHDknIq5OhQK3/whIdK7ZcD1JB/KY7losRRXI+rdQ3lEpaZDlIGXLdPUr8s2ZCgQNuok76y/W3GU5klbDLZ1f4B7dlJQq2touJz3gXRlPtQSTshyiftE8ptClgRkgEYBxWm7cvuasf7bGoUghhI5N2aDZ41eBN13MmwhrXP9RgEZhRkdSRdNn5m1jEWI+Y2zMmGX3bzjYx4aFFbDfUaUCgoFldLQqIgBtjs1xjysK4Yh4oMs2pyk6dBK6G7ng== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=eJMXVdcAYBu7iSzlaGDF0CzLtmxwO7mBpjOdIW/Bo5w=; b=2dsZIp+++bUGi/IEc67cA+wst93hTfHHqqH7QJ9WzA1tC4bJXErDdxL0IPQOSt3mudt53It1uuioGx+3lGLoUE44phOPOHS2txzJ9rKGRmrq6n4Xhlo5FiSI/o5Kq+s8tvQ95u80k+6VpiUVsE33kPKaOW4fcYsihl2B+8av52c= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Stewart Hildebrand To: CC: Oleksandr Andrushchenko , "Stefano Stabellini" , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , =?utf-8?q?Roger_Pau_Monn?= =?utf-8?q?=C3=A9?= , "Volodymyr Babchuk" , Stewart Hildebrand Subject: [PATCH v13 12/14] xen/arm: translate virtual PCI bus topology for guests Date: Fri, 2 Feb 2024 16:33:16 -0500 Message-ID: <20240202213321.1920347-13-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9DB:EE_|SN7PR12MB7370:EE_ X-MS-Office365-Filtering-Correlation-Id: 758d98da-fd84-460d-aa11-08dc24370430 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xuRGVq7rbp7nSYM1rtri/KW0RhyGyFeLnalZfu8gWv9MmNx8Q0tNb0f8qOdXo7r4Hfh99sv9KJxbC4DOZtC/++cI+3WdbT+Jh57aYU1os3HGfC72W+dfnQMVi0vbs9e9NFA92/rEo9PuxZMmaLoj6jCDdEHhv1ojG2r47ol76wdV9dP5DRo1OYzNaQ7GH/J/KLJHL9svzNHRXIjL8JVAmMkiUfVh8MWpu7A+GGH5xeg/yxA4rKJZVpn1iAwafbXSr30IwwNvsFaBsvSWpwySn0n4OIQJUIMIM7vHhFcXoyAi1u6xPaUU3CGiS65ms387GEn/IGA9cVXtCirpwWvIfpbRRSG1z8AtEkzlOPohonpVRzAF/4uGlMsIzXIb3Q7KfjwUR3zqv7EwM+2zbA2stlC10s9YmmNZdcgdqQ7xhAa22bpgQtk41SyrI1iANJRpyDAcKdC6J0SXdPY0ycICj/cYExM8zfOutH9V1f6R+FtIBftoBPv1O/L/Ik4x5hMErnz8RGJyWiOR7VghyVaJ+/hwYt9c6d/ErWdEujJhEY/+zCU6ji8xDybynVAVq/IEPi6GBCDQF4dhWf2UxnJthgXkGs1jA+lPCa3XVOXDxMpuC43uuSXcIBE5ulQJwdOwrYopIz4RBAr8VWVOI8pCO32F3lexpl+iNG/RbcTWsEYvYeYEv5HqojNpOEp6xtJaQ33Hp4gVmFO0Z7jljActP+eohTlxh7KPA+IDH6f2EcmVPtrcbgljK4+F/sXpdbjDNdFpTquGOSGzn5melops2w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(396003)(136003)(346002)(376002)(39860400002)(230922051799003)(64100799003)(1800799012)(82310400011)(451199024)(186009)(40470700004)(46966006)(36840700001)(44832011)(8936002)(2906002)(5660300002)(86362001)(36756003)(356005)(82740400003)(81166007)(478600001)(36860700001)(1076003)(83380400001)(426003)(336012)(26005)(41300700001)(6666004)(2616005)(316002)(8676002)(47076005)(70206006)(6916009)(4326008)(54906003)(70586007)(40460700003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:36:29.0773 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 758d98da-fd84-460d-aa11-08dc24370430 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9DB.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7370 From: Oleksandr Andrushchenko There are three originators for the PCI configuration space access: 1. The domain that owns physical host bridge: MMIO handlers are there so we can update vPCI register handlers with the values written by the hardware domain, e.g. physical view of the registers vs guest's view on the configuration space. 2. Guest access to the passed through PCI devices: we need to properly map virtual bus topology to the physical one, e.g. pass the configuration space access to the corresponding physical devices. 3. Emulated host PCI bridge access. It doesn't exist in the physical topology, e.g. it can't be mapped to some physical host bridge. So, all access to the host bridge itself needs to be trapped and emulated. Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Volodymyr Babchuk Signed-off-by: Stewart Hildebrand --- In v11: - Fixed format issues - Added ASSERT_UNREACHABLE() to the dummy implementation of vpci_translate_virtual_device() - Moved variable in vpci_sbdf_from_gpa(), now it is easier to follow the logic in the function Since v9: - Commend about required lock replaced with ASSERT() - Style fixes - call to vpci_translate_virtual_device folded into vpci_sbdf_from_gpa Since v8: - locks moved out of vpci_translate_virtual_device() Since v6: - add pcidevs locking to vpci_translate_virtual_device - update wrt to the new locking scheme Since v5: - add vpci_translate_virtual_device for #ifndef CONFIG_HAS_VPCI_GUEST_SUPPORT case to simplify ifdefery - add ASSERT(!is_hardware_domain(d)); to vpci_translate_virtual_device - reset output register on failed virtual SBDF translation Since v4: - indentation fixes - constify struct domain - updated commit message - updates to the new locking scheme (pdev->vpci_lock) Since v3: - revisit locking - move code to vpci.c Since v2: - pass struct domain instead of struct vcpu - constify arguments where possible - gate relevant code with CONFIG_HAS_VPCI_GUEST_SUPPORT New in v2 --- xen/arch/arm/vpci.c | 47 +++++++++++++++++++++++++++++++---------- xen/drivers/vpci/vpci.c | 24 +++++++++++++++++++++ xen/include/xen/vpci.h | 12 +++++++++++ 3 files changed, 72 insertions(+), 11 deletions(-) diff --git a/xen/arch/arm/vpci.c b/xen/arch/arm/vpci.c index 3bc4bb55082a..7a6a0017d132 100644 --- a/xen/arch/arm/vpci.c +++ b/xen/arch/arm/vpci.c @@ -7,31 +7,51 @@ #include -static pci_sbdf_t vpci_sbdf_from_gpa(const struct pci_host_bridge *bridge, - paddr_t gpa) +static bool vpci_sbdf_from_gpa(struct domain *d, + const struct pci_host_bridge *bridge, + paddr_t gpa, pci_sbdf_t *sbdf) { - pci_sbdf_t sbdf; + bool translated = true; + + ASSERT(sbdf); if ( bridge ) { - sbdf.sbdf = VPCI_ECAM_BDF(gpa - bridge->cfg->phys_addr); - sbdf.seg = bridge->segment; - sbdf.bus += bridge->cfg->busn_start; + sbdf->sbdf = VPCI_ECAM_BDF(gpa - bridge->cfg->phys_addr); + sbdf->seg = bridge->segment; + sbdf->bus += bridge->cfg->busn_start; } else - sbdf.sbdf = VPCI_ECAM_BDF(gpa - GUEST_VPCI_ECAM_BASE); + { + /* + * For the passed through devices we need to map their virtual SBDF + * to the physical PCI device being passed through. + */ + sbdf->sbdf = VPCI_ECAM_BDF(gpa - GUEST_VPCI_ECAM_BASE); + read_lock(&d->pci_lock); + translated = vpci_translate_virtual_device(d, sbdf); + read_unlock(&d->pci_lock); + } - return sbdf; + return translated; } static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info, register_t *r, void *p) { struct pci_host_bridge *bridge = p; - pci_sbdf_t sbdf = vpci_sbdf_from_gpa(bridge, info->gpa); + pci_sbdf_t sbdf; /* data is needed to prevent a pointer cast on 32bit */ unsigned long data; + ASSERT(!bridge == !is_hardware_domain(v->domain)); + + if ( !vpci_sbdf_from_gpa(v->domain, bridge, info->gpa, &sbdf) ) + { + *r = ~0UL; + return 1; + } + if ( vpci_ecam_read(sbdf, ECAM_REG_OFFSET(info->gpa), 1U << info->dabt.size, &data) ) { @@ -39,7 +59,7 @@ static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info, return 1; } - *r = ~0ul; + *r = ~0UL; return 0; } @@ -48,7 +68,12 @@ static int vpci_mmio_write(struct vcpu *v, mmio_info_t *info, register_t r, void *p) { struct pci_host_bridge *bridge = p; - pci_sbdf_t sbdf = vpci_sbdf_from_gpa(bridge, info->gpa); + pci_sbdf_t sbdf; + + ASSERT(!bridge == !is_hardware_domain(v->domain)); + + if ( !vpci_sbdf_from_gpa(v->domain, bridge, info->gpa, &sbdf) ) + return 1; return vpci_ecam_write(sbdf, ECAM_REG_OFFSET(info->gpa), 1U << info->dabt.size, r); diff --git a/xen/drivers/vpci/vpci.c b/xen/drivers/vpci/vpci.c index 3cd142068f4e..6b732bb2358e 100644 --- a/xen/drivers/vpci/vpci.c +++ b/xen/drivers/vpci/vpci.c @@ -81,6 +81,30 @@ static int add_virtual_device(struct pci_dev *pdev) return 0; } +/* + * Find the physical device which is mapped to the virtual device + * and translate virtual SBDF to the physical one. + */ +bool vpci_translate_virtual_device(const struct domain *d, pci_sbdf_t *sbdf) +{ + const struct pci_dev *pdev; + + ASSERT(!is_hardware_domain(d)); + ASSERT(rw_is_locked(&d->pci_lock)); + + for_each_pdev ( d, pdev ) + { + if ( pdev->vpci && (pdev->vpci->guest_sbdf.sbdf == sbdf->sbdf) ) + { + /* Replace guest SBDF with the physical one. */ + *sbdf = pdev->sbdf; + return true; + } + } + + return false; +} + #endif /* CONFIG_HAS_VPCI_GUEST_SUPPORT */ void vpci_deassign_device(struct pci_dev *pdev) diff --git a/xen/include/xen/vpci.h b/xen/include/xen/vpci.h index a69c640d065a..a23320562ed7 100644 --- a/xen/include/xen/vpci.h +++ b/xen/include/xen/vpci.h @@ -303,6 +303,18 @@ static inline bool __must_check vpci_process_pending(struct vcpu *v) } #endif +#ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT +bool vpci_translate_virtual_device(const struct domain *d, pci_sbdf_t *sbdf); +#else +static inline bool vpci_translate_virtual_device(const struct domain *d, + pci_sbdf_t *sbdf) +{ + ASSERT_UNREACHABLE(); + + return false; +} +#endif + #endif /* From patchwork Fri Feb 2 21:33:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543464 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 BA910C4828E for ; Fri, 2 Feb 2024 21:37:31 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675235.1050570 (Exim 4.92) (envelope-from ) id 1rW1Du-0000nU-3z; Fri, 02 Feb 2024 21:37:22 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675235.1050570; Fri, 02 Feb 2024 21:37:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1Du-0000nG-10; Fri, 02 Feb 2024 21:37:22 +0000 Received: by outflank-mailman (input) for mailman id 675235; Fri, 02 Feb 2024 21:37:21 +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 1rW1DN-0003tJ-W2 for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:36:49 +0000 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on20600.outbound.protection.outlook.com [2a01:111:f403:240a::600]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 2be7ad32-c213-11ee-8a45-1f161083a0e0; Fri, 02 Feb 2024 22:36:49 +0100 (CET) Received: from DM6PR02CA0140.namprd02.prod.outlook.com (2603:10b6:5:332::7) by SJ0PR12MB6686.namprd12.prod.outlook.com (2603:10b6:a03:479::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.34; Fri, 2 Feb 2024 21:36:45 +0000 Received: from CY4PEPF0000E9DB.namprd05.prod.outlook.com (2603:10b6:5:332:cafe::f) by DM6PR02CA0140.outlook.office365.com (2603:10b6:5:332::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.23 via Frontend Transport; Fri, 2 Feb 2024 21:36:45 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by CY4PEPF0000E9DB.mail.protection.outlook.com (10.167.241.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7249.19 via Frontend Transport; Fri, 2 Feb 2024 21:36:45 +0000 Received: from SATLEXMB05.amd.com (10.181.40.146) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:36:45 -0600 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB05.amd.com (10.181.40.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:36:44 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:36:38 -0600 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: 2be7ad32-c213-11ee-8a45-1f161083a0e0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EVSXQSO3N1wbviduaw4m+NX851dUp+Bn5GdbSbl1f9NkPiXE/OTQt0D/EOWJVNehf5Sjxv++aoZ5wOB93rNvoKW+Qsc4qYBvfMkJnCZ5qOcuIlYDFE8r4sb3b0HSx5FJKsJ0Ud/PTzrwVnHF54TY3q5zJwoHTMogHAyLTnH/7JmJACBXpXugm56GNaZRROqlxug0zOX5poLGR+h7M55BxVKuv4kwSwTfJ8d1YnzlmUkGWXr7ueop+nuv8OHAwBGvbm1rXNkXB69Jxjxfn+JFkcPfoyMRHgUXsG2OGrgS77bEc2RGn1P8LAxw/Vq8eaLZH6ZFpMBOkhk48Cfm6Klzpg== 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=PsoOL/G8DIgXV37+ao0tajW6v6ZQDcdP8FQCKLy06D4=; b=Uz9QF7TrTujvGsb+B1GRDjC6D2j1ByKYm8vrEzErYgIC15SMtrNo0bQR4XH35WN2Kz6uhbHNh9E4TcJxA8iJ75FsR/OPiHLu2BCVhlRJT3dlt19Udsc7fULUPe+bNUn5ZqhthStp2yxGCA35v1N/Lcq10Ko2fgOYi/2pDm2EOvk+dNLbC6XHWFBMsr50GqeldBmKw2mIpT73/8okYnWu/Cq443CnCi5psuTdgMrDkIKWXKFMIlwFeP8f05KjaKjwtbDAZqfFCmCE2up80X3RHcxJmP5VC22PX3r1u26Ckgp4OPFKu7N4aB9o+DGMni0Hes/ywvEtdvfMyP/Qpb9GyA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PsoOL/G8DIgXV37+ao0tajW6v6ZQDcdP8FQCKLy06D4=; b=cKtf/lvYRNTuY/AAUj8hOqEzr1bBc+OvemF3Ct0LRXY7U9SlsdHIwOt2gpcj0AI1mIYs9Pig59wkfblvrQK8kguPhTsXecjRe/5Rqt6usDeatILb92vSW4RwV0zYU3KJV5cczZp+JkFBOw0+sDmhvBzyvmATvBuzDvNDxZl1vp8= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Stewart Hildebrand To: CC: Oleksandr Andrushchenko , "Stefano Stabellini" , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , Julien Grall , Volodymyr Babchuk , Stewart Hildebrand Subject: [PATCH v13 13/14] xen/arm: account IO handlers for emulated PCI MSI-X Date: Fri, 2 Feb 2024 16:33:17 -0500 Message-ID: <20240202213321.1920347-14-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9DB:EE_|SJ0PR12MB6686:EE_ X-MS-Office365-Filtering-Correlation-Id: 3c3a574b-cb66-4213-990d-08dc24370e01 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NfwLVJTBJiWeqB0L159LB+HbaZ/MGzX0kikt5eg8b/pmVJSknxC+yL47d06bnnVTKB3/k8Y9KZSJqmLpoyxBV4DvJJfS4AsQor6X7rY9f3kTPK7E3A9xuKxksTjc4K+FgHD6xJDZVlgZYTSwPVw+kDo1tedBqwg69+L//Dgv+nlGyimAySVjO3eAlgo7yids20tOzioL68IkqQA2TrcDtPX9HE2KeQxwoEYAPUpONfCbGH9w14iuAInYv/VtIPyU/dNsICzCi0Tuopybdft6OVbMakDpUjC8T1gNysNQH1z35vTk2zLE8QWk/lIL6nUZOF3Wdc2B6PhRfQorPKwbnyHizGZHpAaSN0NiiU95bPUO6g4UeAA6deV6wva2tnobtncgyPruN68Z7a+NKFkndxCBhOpwFz/eut3cYg70mVQyzw8S98OYTTY20sExjYf75Q4g5KHplbfQdeImU8SIlT1Ixi2BsMLCv89Wai0bTD8gR8cp/e6nT4oabSt/eudZZOvg1dcNaINT1kNXO0Am6TYGkYwW2spF64qDumw0bcuvCDa02HlTfs0lyAr5gdt8t8alBczi8gXnNCgEHRby3lb64lA0ThjvGySQ6LXlaoi5sEsxdyqYqSRUymuJMz+I/fEdP9ObHq5ohCMI2ErublMbWt1a30m9EE9kS3CByXDe4+fVvmEiKDlNjPeKvkJCl8taCZKycessNU18ZIrAvdAaM0/hb2kbw05Kz+E4UHSQtUWtH2e24+aAoAjGOndz6/1QeGoz8gjh1PIbuJw06A== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(136003)(376002)(346002)(396003)(39860400002)(230922051799003)(1800799012)(64100799003)(186009)(82310400011)(451199024)(46966006)(36840700001)(40470700004)(66899024)(336012)(54906003)(47076005)(36860700001)(8936002)(82740400003)(81166007)(41300700001)(36756003)(86362001)(15650500001)(70586007)(6666004)(8676002)(4326008)(316002)(426003)(83380400001)(26005)(44832011)(1076003)(2616005)(6916009)(5660300002)(2906002)(356005)(478600001)(70206006)(40460700003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:36:45.5461 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3c3a574b-cb66-4213-990d-08dc24370e01 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9DB.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6686 From: Oleksandr Andrushchenko At the moment, we always allocate an extra 16 slots for IO handlers (see MAX_IO_HANDLER). So while adding IO trap handlers for the emulated MSI-X registers we need to explicitly tell that we have additional IO handlers, so those are accounted. Signed-off-by: Oleksandr Andrushchenko Acked-by: Julien Grall Signed-off-by: Volodymyr Babchuk Signed-off-by: Stewart Hildebrand --- This actually moved here from the part 2 of the prep work for PCI passthrough on Arm as it seems to be the proper place for it. Since v5: - optimize with IS_ENABLED(CONFIG_HAS_PCI_MSI) since VPCI_MAX_VIRT_DEV is defined unconditionally New in v5 --- xen/arch/arm/vpci.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/xen/arch/arm/vpci.c b/xen/arch/arm/vpci.c index 7a6a0017d132..348ba0fbc860 100644 --- a/xen/arch/arm/vpci.c +++ b/xen/arch/arm/vpci.c @@ -130,6 +130,8 @@ static int vpci_get_num_handlers_cb(struct domain *d, unsigned int domain_vpci_get_num_mmio_handlers(struct domain *d) { + unsigned int count; + if ( !has_vpci(d) ) return 0; @@ -150,7 +152,17 @@ unsigned int domain_vpci_get_num_mmio_handlers(struct domain *d) * For guests each host bridge requires one region to cover the * configuration space. At the moment, we only expose a single host bridge. */ - return 1; + count = 1; + + /* + * There's a single MSI-X MMIO handler that deals with both PBA + * and MSI-X tables per each PCI device being passed through. + * Maximum number of emulated virtual devices is VPCI_MAX_VIRT_DEV. + */ + if ( IS_ENABLED(CONFIG_HAS_PCI_MSI) ) + count += VPCI_MAX_VIRT_DEV; + + return count; } /* From patchwork Fri Feb 2 21:33:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stewart Hildebrand X-Patchwork-Id: 13543462 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 AC110C4828F for ; Fri, 2 Feb 2024 21:37:19 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.675232.1050550 (Exim 4.92) (envelope-from ) id 1rW1Dk-0008Ho-Dj; Fri, 02 Feb 2024 21:37:12 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 675232.1050550; Fri, 02 Feb 2024 21:37:12 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rW1Dk-0008Hh-Aj; Fri, 02 Feb 2024 21:37:12 +0000 Received: by outflank-mailman (input) for mailman id 675232; Fri, 02 Feb 2024 21:37:10 +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 1rW1Di-0008HK-Ny for xen-devel@lists.xenproject.org; Fri, 02 Feb 2024 21:37:10 +0000 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on20601.outbound.protection.outlook.com [2a01:111:f403:2416::601]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 36d2369e-c213-11ee-98f5-efadbce2ee36; Fri, 02 Feb 2024 22:37:08 +0100 (CET) Received: from DM6PR02CA0166.namprd02.prod.outlook.com (2603:10b6:5:332::33) by SA0PR12MB4352.namprd12.prod.outlook.com (2603:10b6:806:9c::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7270.9; Fri, 2 Feb 2024 21:37:04 +0000 Received: from CY4PEPF0000E9DB.namprd05.prod.outlook.com (2603:10b6:5:332:cafe::8) by DM6PR02CA0166.outlook.office365.com (2603:10b6:5:332::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7249.26 via Frontend Transport; Fri, 2 Feb 2024 21:37:03 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by CY4PEPF0000E9DB.mail.protection.outlook.com (10.167.241.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7249.19 via Frontend Transport; Fri, 2 Feb 2024 21:37:03 +0000 Received: from SATLEXMB05.amd.com (10.181.40.146) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:37:03 -0600 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB05.amd.com (10.181.40.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 2 Feb 2024 15:37:02 -0600 Received: from ubuntu.mshome.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Fri, 2 Feb 2024 15:36:56 -0600 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: 36d2369e-c213-11ee-98f5-efadbce2ee36 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VOwCokeJ+nAkYL5YQpg0yBcovVPhOFz8a3t2YyQCXNDgWZC+K+8Dr0k6eQU9WCr3yBFUlEgEJaHK9fLtMzw2de4XDyge8oc7GgqP58i6snvaHOmDZWakuAWHzVgkOzPuqQ64AeE3yJvKEDcEAtz2t5X0b9HLvyvonS/A3n0MfMiqPt9ERI2jdhtRrsBkh0vzjkHU8Ocz7DKJy0eJxkUKkSAWzGlLqxOuvLSD5l3iepdK+axqF4jLcD8LXWhwxJAyI170bi1OvbyFLwTGxkQK/1UZ8Yb5zdEHF7hcOj2LIKp2XFbeYSa7I2XeIPlC4ljktNDW4evQCOinuP/CpPLl9g== 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=GP/sADPnaw4TMrLkrvzS2aR+Na6StFaFDNxGi2x57hw=; b=DX6OsOgxQt9xx84rX1T8v1+ukzDYU5QSJ1OHkweUcF7y0I+SLELcJYvVnwYbsfCEMX2iZQfO+YnyJJr8fhkalCMcE4h7tLu8evOtddP6MwjGEFoCt/qIgYXm4rB/vjgALNj92KoZLFhUgDbraehnbWqANETDl2mMjMiYhWLSeJR4JCBRfdDvbR2xUHcnT9H96YEFpgYZA9yKaQ6V4of4FASD3FCNSknZVCVUO7HLgPELpBvsHNe7RZbWrXGy3c6v9UYwPqyzmjJuPlu8ikybEUGYSK+TTolkBx6Mj7hxMs3lK91gRYz8qq++qbgmyjyJBCQVKaRsGW5hqnGiIZ696A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GP/sADPnaw4TMrLkrvzS2aR+Na6StFaFDNxGi2x57hw=; b=VDB96vADUAr6Y/qYqTv3M41heMpZkBxJjyiMJe6yw1d4pF+GsdBS6jzBCPdpRBMj2iv73d3dMeE0CECizgPJ8uQXmCTsYx+SPdynt3NQKoYYreBOiiKM/QS67NovTfw/WkR0ddXTPYGLXHDCJqDcsBytB4URhT4JnIriW7ZqxlA= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Stewart Hildebrand To: CC: Volodymyr Babchuk , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , "Volodymyr Babchuk" , Stewart Hildebrand Subject: [PATCH v13 14/14] arm/vpci: honor access size when returning an error Date: Fri, 2 Feb 2024 16:33:18 -0500 Message-ID: <20240202213321.1920347-15-stewart.hildebrand@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240202213321.1920347-1-stewart.hildebrand@amd.com> References: <20240202213321.1920347-1-stewart.hildebrand@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9DB:EE_|SA0PR12MB4352:EE_ X-MS-Office365-Filtering-Correlation-Id: c8852deb-b112-438c-dec6-08dc243718cc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /g0GtkjSkbXxLposSJFD3xdRkY3vRTT0d+AqyCBY6ZRKFEEPkf1roKI9wPAaLM1jVIIFORc/gltpxNAzvC4PiCurY9foWBpR7FMmf58OEPxQPizzKi2tYScISHyeysPcPIvQOsfMMdgoV5LklvF84VvKlU36976RrxmnfqY0A1CNEwiy2O7OZBMCaac/hXMhdjwoPP0aA8rMrracOhiLWagWlETWLId5WSa1PjXF9y+OHc21RdLR9UBhT4n4yfRN4uI/DbHI7voAOLlEs6zQdC/ZNlbtfGQiZBDf9chIvT5JNfuAHMyNCFo0IuBFBs2WX9nrfvn968EnZGqSkVCAzY+v94XDjc7GxlEDm2I5KgFprrejhojINDWMd0wuMrKb7W/V0uwfGOVDFpdfrwi54F1ZjADSWF47KbVnvp4sTJ1mlVTczs0dMCLDSVizAKHG6C7wDu0rrmrtCIGE5vQ2ghrgwr4uqgtQKae1BsL+4900Cxzu/xMikSAyzFyk50RjEne+Qbs+/IE2v91VjExReM/OTpV3qyzx4mxzb/w/YHhbiC9/N6LBYcuhPrsREfrVvPJRyNTkSxlElDT6ur+Dr/l39OgcdRukXKz9jFUP4VXxkA5cbTV4bcCMgL61M0RJ//sXBoYKmkyf0b4BP4lm+YqawXq4q5lOpxejuRLpOo/MIvnoA+/yzPqfreVrU2SzPxkqWrm2EGWefDe18rEwr0n4CnD+dgiCREQz640Oba7mAmAELoGr0EdqGoLSf/LluUr97cfgCZHCVtORkH6YCA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(136003)(376002)(346002)(396003)(39860400002)(230922051799003)(451199024)(82310400011)(186009)(64100799003)(1800799012)(36840700001)(40470700004)(46966006)(316002)(70206006)(2616005)(336012)(426003)(26005)(8936002)(41300700001)(36860700001)(6666004)(83380400001)(47076005)(1076003)(8676002)(4326008)(5660300002)(44832011)(2906002)(54906003)(478600001)(36756003)(70586007)(6916009)(86362001)(82740400003)(81166007)(356005)(40480700001)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2024 21:37:03.6554 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c8852deb-b112-438c-dec6-08dc243718cc X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9DB.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4352 From: Volodymyr Babchuk Guest can try to read config space using different access sizes: 8, 16, 32, 64 bits. We need to take this into account when we are returning an error back to MMIO handler, otherwise it is possible to provide more data than requested: i.e. guest issues LDRB instruction to read one byte, but we are writing 0xFFFFFFFFFFFFFFFF in the target register. Signed-off-by: Volodymyr Babchuk Signed-off-by: Stewart Hildebrand --- v9->10: * New patch in v10. --- xen/arch/arm/vpci.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xen/arch/arm/vpci.c b/xen/arch/arm/vpci.c index 348ba0fbc860..aaf9d9120c3d 100644 --- a/xen/arch/arm/vpci.c +++ b/xen/arch/arm/vpci.c @@ -41,6 +41,8 @@ static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info, { struct pci_host_bridge *bridge = p; pci_sbdf_t sbdf; + const uint8_t access_size = (1 << info->dabt.size) * 8; + const uint64_t access_mask = GENMASK_ULL(access_size - 1, 0); /* data is needed to prevent a pointer cast on 32bit */ unsigned long data; @@ -48,7 +50,7 @@ static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info, if ( !vpci_sbdf_from_gpa(v->domain, bridge, info->gpa, &sbdf) ) { - *r = ~0UL; + *r = access_mask; return 1; } @@ -59,7 +61,7 @@ static int vpci_mmio_read(struct vcpu *v, mmio_info_t *info, return 1; } - *r = ~0UL; + *r = access_mask; return 0; }