From patchwork Fri May 27 11:12:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863291 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 A1C17C433F5 for ; Fri, 27 May 2022 11:12:26 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.337942.562650 (Exim 4.92) (envelope-from ) id 1nuXt7-0007XK-L0; Fri, 27 May 2022 11:12:13 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 337942.562650; Fri, 27 May 2022 11:12:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXt7-0007XD-Hm; Fri, 27 May 2022 11:12:13 +0000 Received: by outflank-mailman (input) for mailman id 337942; Fri, 27 May 2022 11:12:11 +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 1nuXt5-0006tv-El for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:12:11 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id da520db6-ddad-11ec-837f-e5687231ffcc; Fri, 27 May 2022 13:12:10 +0200 (CEST) Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-ve1eur03lp2055.outbound.protection.outlook.com [104.47.9.55]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-40-eiF3JyVZPQa2T0-rr7aKNA-1; Fri, 27 May 2022 13:12:09 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by PR3PR04MB7210.eurprd04.prod.outlook.com (2603:10a6:102:8f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.15; Fri, 27 May 2022 11:12:08 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:12:08 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: da520db6-ddad-11ec-837f-e5687231ffcc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653649930; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9E1G4wR+PqEH6Q/fpL7fSV+Z1/ahzOqCt4WvVVI8szc=; b=XrTJi1g73F3wZ3FuE34P/wvSEDVxbew+cTrHk3SNizB2G+3vfzAjVIJU6HDSFsBolptW07 ueCy8WNR9UfzvYOhB72bphMiQQcDk70wNAzke+bBd6NotIzzEL6Dg7pWbr5llS7pw4UOrn 1eQPlRnUZax0rHnznrypiM+gB1rTkcw= X-MC-Unique: eiF3JyVZPQa2T0-rr7aKNA-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Z9/u5YLxd2PRcf4XKD/GEcir5aRLl5iNzJcFJcEHc/G7JkowlSLwcTQFtEjxU6VZUsIm2u73wqvWCxQNoVDQgCr0zo0XLZq1xmw3+rqp+pDcvvS5O32SjCpzT4AnGGOCq/59xZE7Zc2/E7JGnTgdK2abZg9T2yBr7OYX0mwjnoGhs4YiWCcMTBt6e+UNOTQ7TlRzuDCSuJIraIqRNkQ0jTVn7HtRnVahA8nBE6DT74lPowk21s6FTNtE3LH0RUygZ0kk6GlPPIC1Gxbr6XfBXp56y7wDTaP7q5wZUqa2xmqhIyn71vCcafOehWXpnTWsJSRki/dK1mdKWxOw9A0tQg== 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=9E1G4wR+PqEH6Q/fpL7fSV+Z1/ahzOqCt4WvVVI8szc=; b=Lk8OLdLx3u0XAiYVHV/uwp+4R4ZL31IJtUMA6zaqhsnB9hhaodQJ1blw3AlzVK5FSutjS4gxFh4BHp5By9pwDpUbqdMHcts9CapBM/jUP+RiTUfHA1cnqJOBKFcrin3P8Pq/Rc3MyCmag08h1oBlcwYCFiTyKUnA99Dzm2gYxn+/BY33qZyyYFGAHOjt1uPXiDQmOe6CVk/svoC7OD6anTWMPRsJT57B7uNk0f/2+UEyj8JAOKHiz5hnaI6oHQlwRrS1G5fMTA1Pmyjv0Ac77KPqEd0+drcYqDQQ8jxy4gRGCGZdhim9gu4jKS6OoEkRTQididf3Lv3DOFiTttvUlQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <1de2cc0a-e89c-6be9-9d6e-a10219f6f9aa@suse.com> Date: Fri, 27 May 2022 13:12:06 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 01/15] IOMMU/x86: restrict IO-APIC mappings for PV Dom0 Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AM5P194CA0005.EURP194.PROD.OUTLOOK.COM (2603:10a6:203:8f::15) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9446ce4b-94c1-4d15-1622-08da3fd1bd14 X-MS-TrafficTypeDiagnostic: PR3PR04MB7210:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4JnqS3xQm+MpDtJwOEXOotAtiA1CfGoRlxKEGfIcvTHRVg2xbd3rm9eJUg4D5RIj/2mAdMmFRtpItWdACjbjXBm66xHsKpJYgDOpUZHJDAQOpmqhcos3qnfqs+dxzr9o6xbQrIOigymw7EEKa7uMffgxfUkkVLcTQdr8hQCRED5OYLZeR1ElxVL6qzffTEXTdWqlStu1SRxfEZaoc4kZ7V9CkDzfwOx3dhxqzTk6J4J5GQs8YE46e4qcEtygWMbmFJeAf+X10K+L4555Ex6DTM/cekCjdAJkn38fqhYK57+3rWu2+KUYf2QWA1XPTiXE5i+b13nBWWhmBy1/Qs9Ve4YTlYEr/9RvR8sESUo+F8z39nGgqcmOqKvmcYX+XEh8BzIjyj9W8YMak2MBkPezvu/rpvcQA8gRunqoOSVua/LNUe8RZR3vhbMKYVUBMoSnpe8wPnqkD0A1C0g4HaStBgr2iso/rubCMy+KH2+H97S8ldCIrSMxV2yxFRPLRaD+cZrAy+MZ67OT/udhEGx4Kdn5BEEWNrS6Xz+XbcIRUtzM4bazI891YXgnhW8kD3oMWgXxQofznOHHWJ/qu+tfeKy7L+5WCaco/uwZvivtz9BvU1eY4JVBCPgYEuJTsYmC6IcPpOYiD2CCjQtaFGssjONxkUrTYwf5H09uZMvDlBwFodxtmI2KothtJHmB6xmytvd3+a1KbMectfJZ6RUfE0dInFuHfxhjERMt0aaEHFk= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(4326008)(6486002)(8676002)(66476007)(6512007)(83380400001)(8936002)(508600001)(5660300002)(66946007)(66556008)(26005)(38100700002)(2906002)(86362001)(6506007)(186003)(54906003)(316002)(36756003)(6916009)(31686004)(31696002)(2616005)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?9cdmQXmv8Q3wqfhaY+ChtFUdu/9m?= =?utf-8?q?FNceHmVX+UH355dUMQv3aN+lHnywEIWduwjlxI5S4ijcl3gsqW4jg//hUXcCcs0gZ?= =?utf-8?q?KSujv92op4pMNX1i9h0Pye/jzDUZnJ10P8YowkjLuepLsWQsrB5qYHs7ZY4vKRW5Y?= =?utf-8?q?plyhYtx2VUVMhHD1p1JyO2O77M3X9vBqiWWnhuHQndfgRW3xm/KtgnLVROBYcjM8Y?= =?utf-8?q?RuVKS25cjRe0vGrnbF8ZAU7yxe4UAKSMM7mssZ2GDsKadHV5SYgn4ARGgdblplOdE?= =?utf-8?q?xaBqmEKc7ZExj89rT9OUqnqB1edujZAUS7G7uiIuRphak/4aNPSUxhvrkVLWKlORI?= =?utf-8?q?HPW02sX0RTEG2mBDcB+N5sop+O2u2h/n7Kg/xLNnS0/pdr2dPA3UAJCgEi6Kp9o7g?= =?utf-8?q?pMk2zpMHvgdm0FACuTMrSF1F4OPjRlE+MU95fv2cNfSKMT3bOdSVdLsBcV9vTPjNm?= =?utf-8?q?KRFIWdS3IeslVtv442PGvG1LLk6dNcg2ESHMsYcxz74XvGBrr3bzPIM8qOZIQ66rY?= =?utf-8?q?cev2VOLqRv9VK5e+nZq6MZ5iZcmLbNVkKIA98shZ/xR8wZ3oRO3JYjIOW5zyhnPqr?= =?utf-8?q?PTvtMiWig5sb82WN4cWzzishSPCvlzVb05U/WlNQgetTT51uh1AvlqY4Hkiw4TyP4?= =?utf-8?q?iYcSxxKhLxAj/SjH/lT9lhUu99JvNZpgs+TyTlmUYwu80NR0E7wGVhnpZdFmLaL+E?= =?utf-8?q?uGads5r6TPOpIacfTbLVs1C2qDhIApltVBmLbVPYMNHKR01XdaResS9cK+hgAeaC0?= =?utf-8?q?yoDNBE0KmGQuJe1i100j4J5AWPW7/G6l6oEhk+kfdD5FU+bAE3Tw/3xDoLR8TFoBO?= =?utf-8?q?Qd+/TbC7Q4eOOctdgRd0U22XfdgJ5YKJSvsA+QHuVtCgG9l09+BF1TzghoHZURz51?= =?utf-8?q?Cprz1YGFUtxsY/e0oaTzEDEBfR3R87b+4dFyX2dpBizJvZRCTjRO6JoTk5Xoh05RE?= =?utf-8?q?2yTwW2IenCK/GcG8RQrTsMPyjm2CmviBGzznEMB63xRqYbmxrJfqxdHBz1/0sLmqH?= =?utf-8?q?RDJn40dLMleOUc4AiJTedhX/tulAghlT5TJuZq5i6SE7az7FLW1K56ZdLLC3Ue2kP?= =?utf-8?q?MVR5yfbJDrvlMHUIIp+egv0QlXGcAENKZZqgFWxNXHVu3DCyUD7hjq24F0hkEvKts?= =?utf-8?q?DS3wFxn7BEUaIAA5RhgGCvSVbjjqzQpwGgtNyvEUDG0D1KTzY+EY7YO8i2YIbSutw?= =?utf-8?q?HMnvz8/ms2QqOhDqaWiHtXM+y9RUXeYVpGAsQfm+xXCdIA4Q7O5vMW9OpgdA646d+?= =?utf-8?q?E3LRpdjoqyYinSa2ZRa1UBFW6/8SXlHWk8tBGLs/E81K9TVomQxT6se00grpp3ZKo?= =?utf-8?q?C2zlRvYuxeD+y31BB3Z0UmAbZTh8S6FxuZ3TZWpyqZPHb0C6ms+SsHFmIrFJHEoZo?= =?utf-8?q?mHVu+mqTEO9D/AYNBaYfulusF5kGLQMx8Zv2jNLHgMlrleF3yMKT4W2yHLsFnbTcE?= =?utf-8?q?MeGV8AMkQLUc9PvUprDFEE+kH70zmDOadG5l1RZgmsRT9XRdr89Va16g02Hc1t/k1?= =?utf-8?q?prm1GIw0bFqUJR/PfNQnqHaFUiNUi71jQEGUcKy/c7V6meAQmzYxCp0O5wdZeMSRt?= =?utf-8?q?cAPU9elyPdsKPde5npQKf9Ou3kEMGKJ/vw2GjIer0OfNXlt05wPIV2eQBF629zOQA?= =?utf-8?q?xTLO2DZO64uuRn4R6Nkkww7o2Fx9am2w=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9446ce4b-94c1-4d15-1622-08da3fd1bd14 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:12:08.0833 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: F5x0Juf9oxVOlfdbMv15/uHfN4vdP92LPP9inOqf8PIn55yS3LsZxzVLIh59tNaC3MB9wB41L1DZfbs3S+a1zA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR04MB7210 While already the case for PVH, there's no reason to treat PV differently here, though of course the addresses get taken from another source in this case. Except that, to match CPU side mappings, by default we permit r/o ones. This then also means we now deal consistently with IO-APICs whose MMIO is or is not covered by E820 reserved regions. Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monné --- v5: Extend to also cover e.g. HPET, which in turn means explicitly excluding PCI MMCFG ranges. [integrated] v1: Integrate into series. [standalone] v2: Keep IOMMU mappings in sync with CPU ones. --- a/xen/drivers/passthrough/x86/iommu.c +++ b/xen/drivers/passthrough/x86/iommu.c @@ -13,6 +13,7 @@ */ #include +#include #include #include #include @@ -275,12 +276,12 @@ void iommu_identity_map_teardown(struct } } -static bool __hwdom_init hwdom_iommu_map(const struct domain *d, - unsigned long pfn, - unsigned long max_pfn) +static unsigned int __hwdom_init hwdom_iommu_map(const struct domain *d, + unsigned long pfn, + unsigned long max_pfn) { mfn_t mfn = _mfn(pfn); - unsigned int i, type; + unsigned int i, type, perms = IOMMUF_readable | IOMMUF_writable; /* * Set up 1:1 mapping for dom0. Default to include only conventional RAM @@ -289,44 +290,75 @@ static bool __hwdom_init hwdom_iommu_map * that fall in unusable ranges for PV Dom0. */ if ( (pfn > max_pfn && !mfn_valid(mfn)) || xen_in_range(pfn) ) - return false; + return 0; switch ( type = page_get_ram_type(mfn) ) { case RAM_TYPE_UNUSABLE: - return false; + return 0; case RAM_TYPE_CONVENTIONAL: if ( iommu_hwdom_strict ) - return false; + return 0; break; default: if ( type & RAM_TYPE_RESERVED ) { if ( !iommu_hwdom_inclusive && !iommu_hwdom_reserved ) - return false; + perms = 0; } - else if ( is_hvm_domain(d) || !iommu_hwdom_inclusive || pfn > max_pfn ) - return false; + else if ( is_hvm_domain(d) ) + return 0; + else if ( !iommu_hwdom_inclusive || pfn > max_pfn ) + perms = 0; } /* Check that it doesn't overlap with the Interrupt Address Range. */ if ( pfn >= 0xfee00 && pfn <= 0xfeeff ) - return false; + return 0; /* ... or the IO-APIC */ - for ( i = 0; has_vioapic(d) && i < d->arch.hvm.nr_vioapics; i++ ) - if ( pfn == PFN_DOWN(domain_vioapic(d, i)->base_address) ) - return false; + if ( has_vioapic(d) ) + { + for ( i = 0; i < d->arch.hvm.nr_vioapics; i++ ) + if ( pfn == PFN_DOWN(domain_vioapic(d, i)->base_address) ) + return 0; + } + else if ( is_pv_domain(d) ) + { + /* + * Be consistent with CPU mappings: Dom0 is permitted to establish r/o + * ones there (also for e.g. HPET in certain cases), so it should also + * have such established for IOMMUs. + */ + if ( iomem_access_permitted(d, pfn, pfn) && + rangeset_contains_singleton(mmio_ro_ranges, pfn) ) + perms = IOMMUF_readable; + } /* * ... or the PCIe MCFG regions. * TODO: runtime added MMCFG regions are not checked to make sure they * don't overlap with already mapped regions, thus preventing trapping. */ if ( has_vpci(d) && vpci_is_mmcfg_address(d, pfn_to_paddr(pfn)) ) - return false; + return 0; + else if ( is_pv_domain(d) ) + { + /* + * Don't extend consistency with CPU mappings to PCI MMCFG regions. + * These shouldn't be accessed via DMA by devices. + */ + const struct acpi_mcfg_allocation *cfg = pci_mmcfg_config; + + for ( i = 0; i < pci_mmcfg_config_num; ++i, ++cfg ) + if ( pfn >= PFN_DOWN(cfg->address) + PCI_BDF(cfg->start_bus_number, + 0, 0) && + pfn <= PFN_DOWN(cfg->address) + PCI_BDF(cfg->end_bus_number, + ~0, ~0)) + return 0; + } - return true; + return perms; } void __hwdom_init arch_iommu_hwdom_init(struct domain *d) @@ -368,15 +400,19 @@ void __hwdom_init arch_iommu_hwdom_init( for ( ; i < top; i++ ) { unsigned long pfn = pdx_to_pfn(i); + unsigned int perms = hwdom_iommu_map(d, pfn, max_pfn); int rc; - if ( !hwdom_iommu_map(d, pfn, max_pfn) ) + if ( !perms ) rc = 0; else if ( paging_mode_translate(d) ) - rc = p2m_add_identity_entry(d, pfn, p2m_access_rw, 0); + rc = p2m_add_identity_entry(d, pfn, + perms & IOMMUF_writable ? p2m_access_rw + : p2m_access_r, + 0); else rc = iommu_map(d, _dfn(pfn), _mfn(pfn), 1ul << PAGE_ORDER_4K, - IOMMUF_readable | IOMMUF_writable, &flush_flags); + perms, &flush_flags); if ( rc ) printk(XENLOG_WARNING "%pd: identity %smapping of %lx failed: %d\n", From patchwork Fri May 27 11:12:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863294 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 F2BFDC433F5 for ; Fri, 27 May 2022 11:13:05 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.337948.562662 (Exim 4.92) (envelope-from ) id 1nuXtl-000847-VX; Fri, 27 May 2022 11:12:53 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 337948.562662; Fri, 27 May 2022 11:12:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXtl-00083z-Rm; Fri, 27 May 2022 11:12:53 +0000 Received: by outflank-mailman (input) for mailman id 337948; Fri, 27 May 2022 11:12:53 +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 1nuXtl-0007su-6L for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:12:53 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id f2fff49c-ddad-11ec-bd2c-47488cf2e6aa; Fri, 27 May 2022 13:12:52 +0200 (CEST) Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-ve1eur03lp2052.outbound.protection.outlook.com [104.47.9.52]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-32-v_-qEy5CNiC5Ki2ODG4WqQ-1; Fri, 27 May 2022 13:12:50 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by PR3PR04MB7210.eurprd04.prod.outlook.com (2603:10a6:102:8f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.15; Fri, 27 May 2022 11:12:49 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:12:49 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f2fff49c-ddad-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653649971; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jci9rQkWPMy6ohkXdrpyJreaPBMSTK/7GbgkwVczOus=; b=Lr6j5J+MkNnIirJwHFN6STnhsN2vx9SLW9NjvONB89vv455tn39Yu2FhidoRf7xcPaxi9o 4VDmaF06GH/7SHxqdCr+1vfRKgGdRohZEuHmFwGtxEFj+zPXAFoRacb43EVzIml0hjXvEK Lpbue+IIaOxzBiDggbkxZHg2HMGkwQs= X-MC-Unique: v_-qEy5CNiC5Ki2ODG4WqQ-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AO7suNExYLdn6OVAfJjW7iPGd+a0KkFG3dQ+/mDCuKlS6uo7X7leS6ruKeU4n5XgzWhkvgaaqQH44IEM2Nq9dE3istpTYl58c3TdG4AZDC0UpzhyQ/x79OzKUEIv00Fm4B7ZHNaHr3kSHQ+Geq9i3TW6TMpW9bXRScfd3oeDOE/ElRqYP0sKzQb7DVHKnEeJkuun+LBWZt0soKRZp2mH6ZV9cdT5AFPnMuRQ8lBKVE89zGJVrzlFpLr1PtBMYrnqXzQ/qgnguWN4PlSt7XhIbS8NWE5eqtJn44ph7K/nW0P7hCFDjAan/wgK5fpeKARvdvjZVvLFUa6EwOMhnLSPQg== 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=jci9rQkWPMy6ohkXdrpyJreaPBMSTK/7GbgkwVczOus=; b=YrHYl5pIpRlY0AI6mCjJZG+NDJ+0EaYI9WyAB0t+7kvMwyxEX3ZiIqr3IYFKOt5KfUKakRwysdgaiEEUF4WwgrHQDLL308RCHiSkbyIc42OAe64x5UOM5zTw69GWZQpLQj7h2Q43+p9KEDo/GiIQGYOxTIFHEUQSw8T3h6cjzE7NXtqi2AUHqMjC8TWJ1s6VUK/DWO8t+lxV1addpBZ1Y0g/L1uyJO45CwVR/fuSd+ZTt0umg+m/yWe+jXTTbJO5uJ97fCjBnZsbBWBWlPdOD8UPkm6JpgUDbu4HLPV20CRiAbWq7sJkZUYyeWAmXzvKJx2VZeFXGwV0OuzfFQPuig== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <67fd1ed1-4a62-c014-51c0-f547e33fb427@suse.com> Date: Fri, 27 May 2022 13:12:48 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 02/15] IOMMU/x86: perform PV Dom0 mappings in batches Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AM5P194CA0003.EURP194.PROD.OUTLOOK.COM (2603:10a6:203:8f::13) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: eabe028b-169d-4df5-8973-08da3fd1d5e5 X-MS-TrafficTypeDiagnostic: PR3PR04MB7210:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MRW8tla3aMYvXRm9dgtv4TCjdZInXqGokV+Cmh+Aigcq6M9Sy9fX8E+R+zyoPWNz8/Ssr2a+hCvtiTNiSNd/HLRlzRAafWpwfacWti74dI027NG25V/OC6NoYFKehz0qYfFM0PbG/CI7Xlx6wi5A0zY6RIJIBaw+BuVm2MmWC6fbPCvGE62065jretALuvCcEIYBwhQqfGn2x+/aKjV+i53xJoEBF75JG8NdyJ6pzjGXiVuMEmUSIHXS/qIdqvklZ/sY58gAAaQ/JdrAb2KhqPijFrg0MTxI2pEeTRaUh8m/oYa+VOTr5BBmsKG5bXW+YhM9wx6sad/bDxoDvV6MxCSdE1dyczqQQJmfiMbRwr6v/ln5ktaEEqWBm4sZz/GQ/wkiUNtTUjjuUHQ/TS1f2x3cIEYNwfieAl4YLbMXvnPkSgOUdnFrUBTZd+HSI/eO31hozOI7ToyJcj2+6Gx/la/Hetg0B6gX/fzuU5vd2H7TkbxL/5mbknt2JnmvzpnLYEZtnnR28wrEHkG3zCr6XIKcTugJLU+jvk6XCylf3UDvFvZN6GYCW90pR2SVfaXyoKIlUNhMitMR15mbDmgt/jbNJGZ5GYdv4EcT7omOyrltVwEhjHKskFqiUlaYJYdsn7cJMmMfKa6IwW11pCG9rbazVpLLs62gVzmWf4tdFLmYp8Dt8Htf/jjutb8bX0OZVqrByX8VmYunQxqNkMaBAgO12XYfZIYisapgu5QZT6XVfnucCHlac0z8b9pkkUzu X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(4326008)(6486002)(8676002)(66476007)(6512007)(83380400001)(8936002)(508600001)(5660300002)(66946007)(66556008)(26005)(30864003)(38100700002)(2906002)(86362001)(6506007)(186003)(54906003)(316002)(36756003)(6916009)(31686004)(31696002)(2616005)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?QijIMjAUZcr/OhU5RYvU+Y5wxCqe?= =?utf-8?q?qDQgMyyJhNcPRwtqSPixmYWRd30I/6AIiOOF3jtVI9kaTpFFphT+GM2bA4jwvEQtZ?= =?utf-8?q?3Gl4IIRV1nidKLpxqtb6kHKKpU23ckgYoyobLJMmnWTOJbBfAJ0l0eZaiATe2K/Fx?= =?utf-8?q?0eUY7OTcplOY6szaIXiiqCLoFJKgOQDYG0zL/XNFMO02q7O100hBqFugdp1F5VSIC?= =?utf-8?q?s0jS+XkcBalWzoe1z+Ap4ir9d69HhnR0pdZM0QotYqsGikTl28CvUF0KJQBs+jKsb?= =?utf-8?q?jX6XbeO4ZxikMnSGsVCpOlEKBpFrpb0gHOYtPm26977O1rhzDNofIRpdaKgEKErny?= =?utf-8?q?CZR4+nnyNqLLg2oHV7W+UyvbOj8cGKS63/R8WxYAykt3IyjsRnLKCLV//8ercB7Q6?= =?utf-8?q?fRtQ8rgE7GYWFs35OFitkM1sLIVObPEyZ8fboQTafWnLQVFYM2w5qMMCsuFU72dUd?= =?utf-8?q?wyQd4+2E6UEnq0IZsNIL+MvlbayOFqXLUi+KjVFZQMDtnsT0Mti131CAYmgZI53s+?= =?utf-8?q?vf9KUhg8asGYX6Q4dAQ/37g4+6NN9dXlBKPyd1y8hFJOasoE50v/NhzId2qB3+dw/?= =?utf-8?q?USn5VWlhQrgXin5vF3QUPqqr4r+6x0h0UUE0gYIUxINhEOeqeA35bp361adAmBj73?= =?utf-8?q?9YGgh5UsrYqsUV3BtB/yzK6G+CUDKUdSyfaC4nu6Mfsj/jmT+DqqEbzZZS11DziL4?= =?utf-8?q?g698RGJ6cPfjEdei5fIlLnOwtf4RzzPKv1tU5YD9dklIuDjT+lpK8voV2RcAeyOyD?= =?utf-8?q?Xxrws3BKwOo8Fs44FdxcfvGqa6oHWiLzccvpCYEEsYGPN+l3H1lvBWhnOuSRx9y29?= =?utf-8?q?28WHX0NpUVOmOduMaDZhl1+BdDZ7nel+6KT7ceDbegYXHLVvt9UxmzMm//S7+1Esh?= =?utf-8?q?Vc0/1PoE12N3F3PJQt62agQoP0vItMC6x+VeFhLIMIex6xy5LYEgsA21Q0gYMVOFQ?= =?utf-8?q?ThzMCpr49dSWCl4yzW28ghHhe2XV15zSWjeOxz592x/s0js5NdkfBnJpR9v3l9gmp?= =?utf-8?q?uKr0VscZVAPwquEni32ayjA8kx2//xZ+zfXNQ1PO1UDfrhQ5O+csrirD3GprTdjyK?= =?utf-8?q?mmiN3c/XvJTRWV30LmTgxbh+rTTI9qk54MGFIhT7exfyZJdsqgZuWQdJVdwfmxOMo?= =?utf-8?q?XjL2wnI3rLRWji9BlXKsyjHsnd7ujBhaNs4sZNHW+2jUw9ATvWLvgRQ+YPSojurfN?= =?utf-8?q?hgCjMIM9ZxxTiBirKv6t/4wkBAbxm5jpJmccXMmaOI7Q+gVroJltkg/plcKr1NE1P?= =?utf-8?q?S5D2sC2yfztg3HF2Kf6yapjGx8gBLMDkN8KOFqj2QqFzzueOa1rEhMRlX395SwgSX?= =?utf-8?q?cT7gcqG10uYZ+og5bfMobzzvYbWLTAxNGcOMeF2OY+Jr0NswJAbreLcjCAjGclkbV?= =?utf-8?q?J4trTDE7Remu31/rhYyc9gnykPQV+KHpGzHqPBys11r07ikRsPy4kkcBWNL0GMu2P?= =?utf-8?q?VzYDIMO16+BoHVnqt9/KAdDHOU7NETbkdepMpMq+8mQOYKe9TZDyX9hFuPZXg85s/?= =?utf-8?q?h0j/xhDCqtFjZxB88aMjN/LO7uij6SsJAIOqqRxX4OeAidaXl8ej8eVjExxdGKDNt?= =?utf-8?q?Bp9BBYZsWphdhvAFvo5jDmNfzQdzZbYHFZy+6GBqLEuOPyiUYHsDmsW+duIdKqbY1?= =?utf-8?q?rt+eG0d7i4meEKmcYAYYZ4ClErCGhSRQ=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: eabe028b-169d-4df5-8973-08da3fd1d5e5 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:12:49.7057 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: cU3QR5Goi97FAvExfwXPCDTPTaeKAXVbcB9SO7MLINsSe6K5PuO82TE8ohB5ck+xnKcyDeasgarLfZ0ZZlHrQA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR04MB7210 For large page mappings to be easily usable (i.e. in particular without un-shattering of smaller page mappings) and for mapping operations to then also be more efficient, pass batches of Dom0 memory to iommu_map(). In dom0_construct_pv() and its helpers (covering strict mode) this additionally requires establishing the type of those pages (albeit with zero type references). The earlier establishing of PGT_writable_page | PGT_validated requires the existing places where this gets done (through get_page_and_type()) to be updated: For pages which actually have a mapping, the type refcount needs to be 1. There is actually a related bug that gets fixed here as a side effect: Typically the last L1 table would get marked as such only after get_page_and_type(..., PGT_writable_page). While this is fine as far as refcounting goes, the page did remain mapped in the IOMMU in this case (when "iommu=dom0-strict"). Signed-off-by: Jan Beulich Acked-by: Roger Pau Monné --- Subsequently p2m_add_identity_entry() may want to also gain an order parameter, for arch_iommu_hwdom_init() to use. While this only affects non-RAM regions, systems typically have 2-16Mb of reserved space immediately below 4Gb, which hence could be mapped more efficiently. Eventually we may want to overhaul this logic to use a rangeset based approach instead, punching holes into originally uniformly large-page- mapped regions. Doing so right here would first and foremost be yet more of a change. The installing of zero-ref writable types has in fact shown (observed while putting together the change) that despite the intention by the XSA-288 changes (affecting DomU-s only) for Dom0 a number of sufficiently ordinary pages (at the very least initrd and P2M ones as well as pages that are part of the initial allocation but not part of the initial mapping) still have been starting out as PGT_none, meaning that they would have gained IOMMU mappings only the first time these pages would get mapped writably. Consequently an open question is whether iommu_memory_setup() should set the pages to PGT_writable_page independent of need_iommu_pt_sync(). I didn't think I need to address the bug mentioned in the description in a separate (prereq) patch, but if others disagree I could certainly break out that part (needing to first use iommu_legacy_unmap() then). Note that 4k P2M pages don't get (pre-)mapped in setup_pv_physmap(): They'll end up mapped via the later get_page_and_type(). As to the way these refs get installed: I've chosen to avoid the more expensive {get,put}_page_and_type(), favoring to put in place the intended type directly. I guess I could be convinced to avoid this bypassing of the actual logic; I merely think it's unnecessarily expensive. Note also that strictly speaking the iommu_iotlb_flush_all() here (as well as the pre-existing one in arch_iommu_hwdom_init()) shouldn't be needed: Actual hooking up (AMD) or enabling of translation (VT-d) occurs only afterwards anyway, so nothing can have made it into TLBs just yet. --- v3: Fold iommu_map() into (the now renamed) iommu_memory_setup(). Move iommu_unmap() into mark_pv_pt_pages_rdonly(). Adjust (split) log message in arch_iommu_hwdom_init(). --- a/xen/arch/x86/pv/dom0_build.c +++ b/xen/arch/x86/pv/dom0_build.c @@ -46,7 +46,8 @@ void __init dom0_update_physmap(bool com static __init void mark_pv_pt_pages_rdonly(struct domain *d, l4_pgentry_t *l4start, unsigned long vpt_start, - unsigned long nr_pt_pages) + unsigned long nr_pt_pages, + unsigned int *flush_flags) { unsigned long count; struct page_info *page; @@ -71,6 +72,14 @@ static __init void mark_pv_pt_pages_rdon ASSERT((page->u.inuse.type_info & PGT_type_mask) <= PGT_root_page_table); ASSERT(!(page->u.inuse.type_info & ~(PGT_type_mask | PGT_pae_xen_l2))); + /* + * Page table pages need to be removed from the IOMMU again in case + * iommu_memory_setup() ended up mapping them. + */ + if ( need_iommu_pt_sync(d) && + iommu_unmap(d, _dfn(mfn_x(page_to_mfn(page))), 1, flush_flags) ) + BUG(); + /* Read-only mapping + PGC_allocated + page-table page. */ page->count_info = PGC_allocated | 3; page->u.inuse.type_info |= PGT_validated | 1; @@ -107,11 +116,43 @@ static __init void mark_pv_pt_pages_rdon unmap_domain_page(pl3e); } +static void __init iommu_memory_setup(struct domain *d, const char *what, + struct page_info *page, unsigned long nr, + unsigned int *flush_flags) +{ + int rc; + mfn_t mfn = page_to_mfn(page); + + if ( !need_iommu_pt_sync(d) ) + return; + + rc = iommu_map(d, _dfn(mfn_x(mfn)), mfn, nr, + IOMMUF_readable | IOMMUF_writable, flush_flags); + if ( rc ) + { + printk(XENLOG_ERR "pre-mapping %s MFN [%lx,%lx) into IOMMU failed: %d\n", + what, mfn_x(mfn), mfn_x(mfn) + nr, rc); + return; + } + + /* + * For successfully established IOMMU mappings the type of the page(s) + * needs to match (for _get_page_type() to unmap upon type change). Set + * the page(s) to writable with no type ref. + */ + for ( ; nr--; ++page ) + { + ASSERT(!page->u.inuse.type_info); + page->u.inuse.type_info = PGT_writable_page | PGT_validated; + } +} + static __init void setup_pv_physmap(struct domain *d, unsigned long pgtbl_pfn, unsigned long v_start, unsigned long v_end, unsigned long vphysmap_start, unsigned long vphysmap_end, - unsigned long nr_pages) + unsigned long nr_pages, + unsigned int *flush_flags) { struct page_info *page = NULL; l4_pgentry_t *pl4e, *l4start = map_domain_page(_mfn(pgtbl_pfn)); @@ -177,6 +218,10 @@ static __init void setup_pv_physmap(stru L3_PAGETABLE_SHIFT - PAGE_SHIFT, MEMF_no_scrub)) != NULL ) { + iommu_memory_setup(d, "P2M 1G", page, + SUPERPAGE_PAGES * SUPERPAGE_PAGES, + flush_flags); + *pl3e = l3e_from_page(page, L1_PROT|_PAGE_DIRTY|_PAGE_PSE); vphysmap_start += 1UL << L3_PAGETABLE_SHIFT; continue; @@ -203,6 +248,9 @@ static __init void setup_pv_physmap(stru L2_PAGETABLE_SHIFT - PAGE_SHIFT, MEMF_no_scrub)) != NULL ) { + iommu_memory_setup(d, "P2M 2M", page, SUPERPAGE_PAGES, + flush_flags); + *pl2e = l2e_from_page(page, L1_PROT|_PAGE_DIRTY|_PAGE_PSE); vphysmap_start += 1UL << L2_PAGETABLE_SHIFT; continue; @@ -311,6 +359,7 @@ int __init dom0_construct_pv(struct doma unsigned long initrd_pfn = -1, initrd_mfn = 0; unsigned long count; struct page_info *page = NULL; + unsigned int flush_flags = 0; start_info_t *si; struct vcpu *v = d->vcpu[0]; void *image_base = bootstrap_map(image); @@ -573,6 +622,9 @@ int __init dom0_construct_pv(struct doma BUG(); } initrd->mod_end = 0; + + iommu_memory_setup(d, "initrd", mfn_to_page(_mfn(initrd_mfn)), + PFN_UP(initrd_len), &flush_flags); } printk("PHYSICAL MEMORY ARRANGEMENT:\n" @@ -606,6 +658,13 @@ int __init dom0_construct_pv(struct doma process_pending_softirqs(); + /* + * Map the full range here and then punch holes for page tables + * alongside marking them as such in mark_pv_pt_pages_rdonly(). + */ + iommu_memory_setup(d, "init-alloc", mfn_to_page(_mfn(alloc_spfn)), + alloc_epfn - alloc_spfn, &flush_flags); + mpt_alloc = (vpt_start - v_start) + pfn_to_paddr(alloc_spfn); if ( vinitrd_start ) mpt_alloc -= PAGE_ALIGN(initrd_len); @@ -690,7 +749,8 @@ int __init dom0_construct_pv(struct doma l1tab++; page = mfn_to_page(_mfn(mfn)); - if ( !page->u.inuse.type_info && + if ( (!page->u.inuse.type_info || + page->u.inuse.type_info == (PGT_writable_page | PGT_validated)) && !get_page_and_type(page, d, PGT_writable_page) ) BUG(); } @@ -719,7 +779,7 @@ int __init dom0_construct_pv(struct doma } /* Pages that are part of page tables must be read only. */ - mark_pv_pt_pages_rdonly(d, l4start, vpt_start, nr_pt_pages); + mark_pv_pt_pages_rdonly(d, l4start, vpt_start, nr_pt_pages, &flush_flags); /* Mask all upcalls... */ for ( i = 0; i < XEN_LEGACY_MAX_VCPUS; i++ ) @@ -794,7 +854,7 @@ int __init dom0_construct_pv(struct doma { pfn = pagetable_get_pfn(v->arch.guest_table); setup_pv_physmap(d, pfn, v_start, v_end, vphysmap_start, vphysmap_end, - nr_pages); + nr_pages, &flush_flags); } /* Write the phys->machine and machine->phys table entries. */ @@ -825,7 +885,9 @@ int __init dom0_construct_pv(struct doma if ( get_gpfn_from_mfn(mfn) >= count ) { BUG_ON(compat); - if ( !page->u.inuse.type_info && + if ( (!page->u.inuse.type_info || + page->u.inuse.type_info == (PGT_writable_page | + PGT_validated)) && !get_page_and_type(page, d, PGT_writable_page) ) BUG(); @@ -841,8 +903,12 @@ int __init dom0_construct_pv(struct doma #endif while ( pfn < nr_pages ) { - if ( (page = alloc_chunk(d, nr_pages - domain_tot_pages(d))) == NULL ) + count = domain_tot_pages(d); + if ( (page = alloc_chunk(d, nr_pages - count)) == NULL ) panic("Not enough RAM for DOM0 reservation\n"); + + iommu_memory_setup(d, "chunk", page, domain_tot_pages(d) - count, + &flush_flags); while ( pfn < domain_tot_pages(d) ) { mfn = mfn_x(page_to_mfn(page)); @@ -857,6 +923,10 @@ int __init dom0_construct_pv(struct doma } } + /* Use while() to avoid compiler warning. */ + while ( iommu_iotlb_flush_all(d, flush_flags) ) + break; + if ( initrd_len != 0 ) { si->mod_start = vinitrd_start ?: initrd_pfn; --- a/xen/drivers/passthrough/x86/iommu.c +++ b/xen/drivers/passthrough/x86/iommu.c @@ -363,8 +363,8 @@ static unsigned int __hwdom_init hwdom_i void __hwdom_init arch_iommu_hwdom_init(struct domain *d) { - unsigned long i, top, max_pfn; - unsigned int flush_flags = 0; + unsigned long i, top, max_pfn, start, count; + unsigned int flush_flags = 0, start_perms = 0; BUG_ON(!is_hardware_domain(d)); @@ -395,9 +395,9 @@ void __hwdom_init arch_iommu_hwdom_init( * First Mb will get mapped in one go by pvh_populate_p2m(). Avoid * setting up potentially conflicting mappings here. */ - i = paging_mode_translate(d) ? PFN_DOWN(MB(1)) : 0; + start = paging_mode_translate(d) ? PFN_DOWN(MB(1)) : 0; - for ( ; i < top; i++ ) + for ( i = start, count = 0; i < top; ) { unsigned long pfn = pdx_to_pfn(i); unsigned int perms = hwdom_iommu_map(d, pfn, max_pfn); @@ -406,20 +406,41 @@ void __hwdom_init arch_iommu_hwdom_init( if ( !perms ) rc = 0; else if ( paging_mode_translate(d) ) + { rc = p2m_add_identity_entry(d, pfn, perms & IOMMUF_writable ? p2m_access_rw : p2m_access_r, 0); + if ( rc ) + printk(XENLOG_WARNING + "%pd: identity mapping of %lx failed: %d\n", + d, pfn, rc); + } + else if ( pfn != start + count || perms != start_perms ) + { + commit: + rc = iommu_map(d, _dfn(start), _mfn(start), count, start_perms, + &flush_flags); + if ( rc ) + printk(XENLOG_WARNING + "%pd: IOMMU identity mapping of [%lx,%lx) failed: %d\n", + d, pfn, pfn + count, rc); + SWAP(start, pfn); + start_perms = perms; + count = 1; + } else - rc = iommu_map(d, _dfn(pfn), _mfn(pfn), 1ul << PAGE_ORDER_4K, - perms, &flush_flags); + { + ++count; + rc = 0; + } - if ( rc ) - printk(XENLOG_WARNING "%pd: identity %smapping of %lx failed: %d\n", - d, !paging_mode_translate(d) ? "IOMMU " : "", pfn, rc); - if (!(i & 0xfffff)) + if ( !(++i & 0xfffff) ) process_pending_softirqs(); + + if ( i == top && count ) + goto commit; } /* Use if to avoid compiler warning */ From patchwork Fri May 27 11:13:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863295 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 C3693C433EF for ; Fri, 27 May 2022 11:13:27 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.337955.562673 (Exim 4.92) (envelope-from ) id 1nuXu8-0000CD-Ah; Fri, 27 May 2022 11:13:16 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 337955.562673; Fri, 27 May 2022 11:13:16 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXu8-0000C6-7Y; Fri, 27 May 2022 11:13:16 +0000 Received: by outflank-mailman (input) for mailman id 337955; Fri, 27 May 2022 11:13:15 +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 1nuXu6-0007su-Nw for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:13:14 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 000aced3-ddae-11ec-bd2c-47488cf2e6aa; Fri, 27 May 2022 13:13:13 +0200 (CEST) Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03lp2171.outbound.protection.outlook.com [104.47.51.171]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-32-1QpSct_ePjua-SOYLNkHqw-1; Fri, 27 May 2022 13:13:12 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by PR3PR04MB7210.eurprd04.prod.outlook.com (2603:10a6:102:8f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.15; Fri, 27 May 2022 11:13:11 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:13:11 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 000aced3-ddae-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653649993; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CwR4uJvcM3qrVCLzIeP2CVCTgiXaow7zHN5BzR104SM=; b=EnhP/dpcHTRxWZiJjF2cuP8Zmu24lizho1YZ86AxI+HVuK31nNlf6GIzcz23UUT7Gi4tlT EOsQV1vkHnZ/3w+THLTBOtOSE8OvidVqkWV7ESeLe2MZ3JGLza6a170/5ZiwkBrD51guNf WyhQOayhA7u3LW5wli4tMZgiWw44Ht4= X-MC-Unique: 1QpSct_ePjua-SOYLNkHqw-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UW0aKbEr0PgBv+J3viEw8PVACE+OLI2m8h2Af7C1VD5Tu4gelkB4S+cAMyu0cZuyApgzAVmiMgHsc5WDMpiBhlnKEUcT3BaqZxUqHTr6eFKCUbeYsQ1nTrzI1EhUvuHV2nnd4u/o1YSVlNg9vGxHDAu606347pXR4Mmp/cTUxaxoqaWsyuRssDICp/wj9m+J2lWxtYz3yEffusrwfDFKhEClhe2nIhwgjQ1qD6DzN7lRM0rKOdqEK599UYGKzvIiSh9X++wu3/nPXwWRDCBrfPvUWZws5IEe7FLywrpRsAEluje7xDekwdHTf7ZMBoqlK6OuCvWOtFM2pFHewtVwww== 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=CwR4uJvcM3qrVCLzIeP2CVCTgiXaow7zHN5BzR104SM=; b=V2MbilDINZdogmHtx/isqzIuw8M85hgopi6syu0t61LE9Yc/3iW7QCDdogIs7CjUvFJieiffrbgfISy8+LWKXxkIzvTFol+3dX8WvZHsxQYArbrQJxG7JHZbLBBjaHCz9RMjYPwowVjLUmwHCH3TKNCk4s6GVaGcOCSV1ADLyXIlZABzkjV3fE+kKUtC5fpM52NlkevWtOkrp8RyTv9SO7xCAfqq4bxvUqZsr2BsKoMUhytn5oeKrPfWHWQUJyMxGNpYaJn66CnASFS8k8CwSnH2ixthPSs2Q0wBIPhaiN9eBP/2fhhCvKxHLc6kGLDWQe14K8j3EdORCiB4WuVDaA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <614413d8-5043-f0e3-929b-f161fa89bb35@suse.com> Date: Fri, 27 May 2022 13:13:09 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 03/15] IOMMU/x86: support freeing of pagetables Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AM6P193CA0089.EURP193.PROD.OUTLOOK.COM (2603:10a6:209:88::30) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 944c75ec-6132-4d5d-9366-08da3fd1e2d2 X-MS-TrafficTypeDiagnostic: PR3PR04MB7210:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OLQHv9i5XNduo5s2Fnt6mz/HqWTdcpmjzijsEnRg1o728tT+D960uHUCMKXJ0ZDfU3VeU723zRBaxAgmOsUdGKTXedQ2LOm/xb89eFzuJToil++7OqNa++skLbHOwx7pv8uJZVQAwSy7Bh/RMM4ie0H87p+J7ol90xbLE+RDJtxKKrrknI/D1brVzKNVPpvcLaYUTITFqL7oy9yEUQXEDEehQHs+9XYlGfchpZaXiCAixLhHuuWvEVquBCUDdmkoBWLCgpBDdBacM6OCXWjKK4RiJaphHI+8GYntuFDLO8bh2bI0XanPv3hF225xIepN+tIY75VK1WGHk15G6rq5t5/SWPUUsdNzfqNEqOmN2Ka16GDyD3HWTbEwHwGWDjWCFIi3eAK8UPqIuWKMd02+BhKhsUwlrE1LqDvBgB96a/OT5oeMvnFDQLZ6oqmCamB+XGZvgiGqesqdZuGvmcSSuZn5jxUVcgTfYk3d9do2MM0UEBu5Ecweru8n2SpP6N6bfppaPRjWwDVUw4VWejhtyXuv9AV+Vg7UYeiVHgr8egHiS2N3C+PW1DlrNBd2hFdRyNx2jU4lTWx5ymUHepQGb+uDAbY0kCvixa5N/Bl8EgX6WyYnYcipLZtjameHvhEjRNfQBLF2qnrpBPG+hL78h8p9jX2Yu8x+cYlQNDsBznjuRk7wJjoVuOespFkY7S8D20/BUAMXOzUF5GIVewU1IHD414VZs3VG9OsEOnGjuJ18CVGhjE8o8NYNyvcOWtYxCdV4PFARIzqIQj7bA6VvxlGXQQt3C8ofZFP2QfKTZj9+o+mqjcQREymHglO/bAux X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(4326008)(6486002)(8676002)(66476007)(6512007)(83380400001)(8936002)(508600001)(5660300002)(66946007)(66556008)(26005)(38100700002)(2906002)(86362001)(6506007)(186003)(54906003)(316002)(36756003)(6916009)(31686004)(31696002)(2616005)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?+GYv94KwTx16uYGlI6UI/A9TIO+4?= =?utf-8?q?2ZXzZYVDrrqHMavda8z8duGpKdXOtp/4eg7jWKnZzkQWRHpCgwtHf2QU53V79TDuM?= =?utf-8?q?p4hSSiMiStGZFQI1tzkxhOChdOBXD9GI14anuSfQEW05tIj7q2AExYXESt1NJjzvS?= =?utf-8?q?DL8VgqsEFvpMrcy9By6cfrZzcGUEddZP5MW12RaLhRAWHYrgGw01YNs1PJrfJ6ph7?= =?utf-8?q?fsgVSXB/VcOY1pNp4YLBbkisn54y/ISc3OXb1Yn7g6+vp+yNd8YEqkpHOZNrcxO8a?= =?utf-8?q?pnfNlj85kDlO38b2qf9/lft0/ifwvLEMT7MtXOZIGo86Gx1TNCRFoWd+1nxn6fxBk?= =?utf-8?q?MqyxZBIaRt0KYj4qFWeitdG/2yNU9VC8xO9gCb9xtclRzCyfTxSzVKvJd44QKpag5?= =?utf-8?q?HeRvg+nFm6RCYeLemK7zjkPfFn+WAjM8XPFIidqh68feSScy2cKUjfpxszJWC++v3?= =?utf-8?q?p3OtNJq64whu/4Jc4Wc7qaBqg6IDuL34SL7mWdpOCT6wkOgj6bcfOLLS1nW8v5R5c?= =?utf-8?q?GAx9rLKOkw7LTZH5a2YiErkkyAdSGDvXfDgmdqLrwsqANp11sO8PFZbGb3ZdA2zlJ?= =?utf-8?q?cXNFSV0nIfkSsqSoVieEIgsV7nXLw8PDVEqf1t1bJfhdiVt6or5j+JV6U5pXPdmM9?= =?utf-8?q?9CZNn0RaMpUbR4CmtO7umV2eURBI9NjZMHjJGdP2+nK1skF/MK0x/jjeMfWbV8P1p?= =?utf-8?q?PJgPJAQk/Fw0zHpDeRsDSuzqqVOKbqgrp/t5P3hmG080wCHLzxyzFLCURRpwevVLw?= =?utf-8?q?JlpPVArHYfk1tIkx26oimjykFgsLdmpTKouFiXrpKSNBra4SjWp63m3WLdY02YyZ8?= =?utf-8?q?4VVo8lvo6HAD+i21OQFFlxPVMhL6v1uItgdg1wd2Q8hu8VwiSNUOW0BWb7E2L5jVk?= =?utf-8?q?LQETPtAQuvD2khPpiqg+l+RJJaXAf6xTKhdK9Pk1+VNAYbggTZfoBA6zAuRPrjUTY?= =?utf-8?q?wbHLr7ygHqCp+b1bhos235PhnBlOtIsZfSXpIGd13qRJvGT5mNXfp4/iAs2oa7wxT?= =?utf-8?q?dtFHk5+b/Ub4A+OflAxMsVsExmlamFF0/hedOxFDqLGUP3ZvVVNSgLnr3hc1n1s+h?= =?utf-8?q?uVqoksRVQKu/8LEaHlrB6X0ub6qe1HuT60/BfL8PQ3v+I9AJP48Q6aOOpKZMNWhp8?= =?utf-8?q?OLWTosk916Swr+9TLnae9WqnO2HKVNCObHZYIJIys83PF5tkhPOrWHmj9eXepxGsJ?= =?utf-8?q?/56UXegh1K/qHxjTniLDmfuGa1CDu2V9e5ssgZbvfqVuc5XKr0P753HVIv4tYMwA1?= =?utf-8?q?6bdO0sgYkCvjEJeWOqFuhiJ4yH60ZkF0AhdOpZj6VMNXm3nOa7v7LwFnROUmZjwSc?= =?utf-8?q?ionLdS9uAdw8bNpLUmkzzB/xt/ev4U8bAryJG+g3AkV6b7xJc6s6mZ/bxTJj1X+9H?= =?utf-8?q?OM4GnZPHIiTxu/cBpPeulHtx4UhkdSICdSDmlfXNUWhb/p/ryY1lgTQgeuPf3C0rU?= =?utf-8?q?3EGwRw6WKmLAW49/JeBMerQXs5h+7ZGlZNjn7R7iPIvbnhQU7Jgt6QZE9rURScCm5?= =?utf-8?q?C7NKvzyd+LCucLKB3Wc+StLzXDL2U15l7Gw1qw4UU7q3v8/pllT6ogHFwktArzWc9?= =?utf-8?q?hYtePqxssman1qMejfujUN5gwzfrEEjvkR6yqdhoGExNPIoJH5knezLGbe2cXGkVt?= =?utf-8?q?TYrnW4rC4nbU/7awA2POlS6fyhMAecYw=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 944c75ec-6132-4d5d-9366-08da3fd1e2d2 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:13:11.3918 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: zzhVvwcenFqUYNbNfQZt8g7UTFQ0+Hlq4+O5A6QWkvXQs8V0/s7aPPH8s7/a3pm3jr+mlOhWtqqJzczjKT/ydQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR04MB7210 For vendor specific code to support superpages we need to be able to deal with a superpage mapping replacing an intermediate page table (or hierarchy thereof). Consequently an iommu_alloc_pgtable() counterpart is needed to free individual page tables while a domain is still alive. Since the freeing needs to be deferred until after a suitable IOTLB flush was performed, released page tables get queued for processing by a tasklet. Signed-off-by: Jan Beulich --- I was considering whether to use a softirq-tasklet instead. This would have the benefit of avoiding extra scheduling operations, but come with the risk of the freeing happening prematurely because of a process_pending_softirqs() somewhere. --- v5: Fix CPU_UP_PREPARE for BIGMEM. Schedule tasklet in CPU_DOWN_FAILED when list is not empty. Skip all processing in CPU_DEAD when list is empty. v4: Change type of iommu_queue_free_pgtable()'s 1st parameter. Re-base. v3: Call process_pending_softirqs() from free_queued_pgtables(). --- a/xen/arch/x86/include/asm/iommu.h +++ b/xen/arch/x86/include/asm/iommu.h @@ -147,6 +147,7 @@ void iommu_free_domid(domid_t domid, uns int __must_check iommu_free_pgtables(struct domain *d); struct domain_iommu; struct page_info *__must_check iommu_alloc_pgtable(struct domain_iommu *hd); +void iommu_queue_free_pgtable(struct domain_iommu *hd, struct page_info *pg); #endif /* !__ARCH_X86_IOMMU_H__ */ /* --- a/xen/drivers/passthrough/x86/iommu.c +++ b/xen/drivers/passthrough/x86/iommu.c @@ -12,6 +12,7 @@ * this program; If not, see . */ +#include #include #include #include @@ -566,6 +567,98 @@ struct page_info *iommu_alloc_pgtable(st return pg; } +/* + * Intermediate page tables which get replaced by large pages may only be + * freed after a suitable IOTLB flush. Hence such pages get queued on a + * per-CPU list, with a per-CPU tasklet processing the list on the assumption + * that the necessary IOTLB flush will have occurred by the time tasklets get + * to run. (List and tasklet being per-CPU has the benefit of accesses not + * requiring any locking.) + */ +static DEFINE_PER_CPU(struct page_list_head, free_pgt_list); +static DEFINE_PER_CPU(struct tasklet, free_pgt_tasklet); + +static void free_queued_pgtables(void *arg) +{ + struct page_list_head *list = arg; + struct page_info *pg; + unsigned int done = 0; + + while ( (pg = page_list_remove_head(list)) ) + { + free_domheap_page(pg); + + /* Granularity of checking somewhat arbitrary. */ + if ( !(++done & 0x1ff) ) + process_pending_softirqs(); + } +} + +void iommu_queue_free_pgtable(struct domain_iommu *hd, struct page_info *pg) +{ + unsigned int cpu = smp_processor_id(); + + spin_lock(&hd->arch.pgtables.lock); + page_list_del(pg, &hd->arch.pgtables.list); + spin_unlock(&hd->arch.pgtables.lock); + + page_list_add_tail(pg, &per_cpu(free_pgt_list, cpu)); + + tasklet_schedule(&per_cpu(free_pgt_tasklet, cpu)); +} + +static int cf_check cpu_callback( + struct notifier_block *nfb, unsigned long action, void *hcpu) +{ + unsigned int cpu = (unsigned long)hcpu; + struct page_list_head *list = &per_cpu(free_pgt_list, cpu); + struct tasklet *tasklet = &per_cpu(free_pgt_tasklet, cpu); + + switch ( action ) + { + case CPU_DOWN_PREPARE: + tasklet_kill(tasklet); + break; + + case CPU_DEAD: + if ( !page_list_empty(list) ) + { + page_list_splice(list, &this_cpu(free_pgt_list)); + INIT_PAGE_LIST_HEAD(list); + tasklet_schedule(&this_cpu(free_pgt_tasklet)); + } + break; + + case CPU_UP_PREPARE: + INIT_PAGE_LIST_HEAD(list); + fallthrough; + case CPU_DOWN_FAILED: + tasklet_init(tasklet, free_queued_pgtables, list); + if ( !page_list_empty(list) ) + tasklet_schedule(tasklet); + break; + } + + return NOTIFY_DONE; +} + +static struct notifier_block cpu_nfb = { + .notifier_call = cpu_callback, +}; + +static int __init cf_check bsp_init(void) +{ + if ( iommu_enabled ) + { + cpu_callback(&cpu_nfb, CPU_UP_PREPARE, + (void *)(unsigned long)smp_processor_id()); + register_cpu_notifier(&cpu_nfb); + } + + return 0; +} +presmp_initcall(bsp_init); + bool arch_iommu_use_permitted(const struct domain *d) { /* From patchwork Fri May 27 11:13:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863307 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 0A47DC433F5 for ; Fri, 27 May 2022 11:21:54 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.338027.562796 (Exim 4.92) (envelope-from ) id 1nuY2C-00077M-RI; Fri, 27 May 2022 11:21:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 338027.562796; Fri, 27 May 2022 11:21:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuY2C-000765-I1; Fri, 27 May 2022 11:21:36 +0000 Received: by outflank-mailman (input) for mailman id 338027; Fri, 27 May 2022 11:21:35 +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 1nuXuY-0007su-AT for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:13:42 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 106feff7-ddae-11ec-bd2c-47488cf2e6aa; Fri, 27 May 2022 13:13:41 +0200 (CEST) Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03lp2168.outbound.protection.outlook.com [104.47.51.168]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-38-fiwWDwBhOeqP70CORUxCWA-1; Fri, 27 May 2022 13:13:39 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by PR3PR04MB7210.eurprd04.prod.outlook.com (2603:10a6:102:8f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.15; Fri, 27 May 2022 11:13:38 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:13:38 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 106feff7-ddae-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650021; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DQnfN5ec44h4k+UnrTQoza7hV/xKmWkd1XV4maZZ/38=; b=Bl7pj7CzH293SUnFvkkRxeQeKUm7o5OufbD4xS1ipPXUcaWHMLGCtp3xIwMHthoTeTYXUC 9JRxPzakqtv+3tCZ9QM1sPSyMOne1CxxTquH3olgHMHcViGDdzcoNNkm4wRAOP4fr+6NBH V/KgZgT+Q1bySPzdpfd5WwpG41CY/rA= X-MC-Unique: fiwWDwBhOeqP70CORUxCWA-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=c69wk1Z0OtpGGq0WBV/4w9KTHO8Gw2kCcehE83d6yGZrzfKv5ZeQDMDZVS9eQFQf4PiULBOAkJGOm+vI9y8IppNuw3Qu1qBKeGcSsZ3y89kfCrFhouCAtIKaFFZo0cdVzxKEWi59Bpqow7Jmk85suSTUrrp2X1l4XUHrChyEdYpp3flqtmECAXIbTL9PVnQlaHs/wXPTFDt1uVQEqKLZfdbsuAdf1zTrSJyi/P7cpjQ/TEmcQWuUo9VPg2PiexUC/UnBWRSRi/r5mFijdE5x6qAmBoFCVWZnQy9LsdWNjAbdLTi7cfwgVEwRSK0xnP30LPLy3SlGFS5eU6ndwPRL9g== 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=AxiRfgvA8ZTM896+3sr+fZ7fI2k/BkIKHoJCeGpTy2A=; b=kHfnK7DciwV7yRZpDYXcdKurq014sRybjAgCoZsmzQ4pRvUviPVgaiDnxPzSQCYt1szMW0j2k4DntbY9T/e4R/S4ehhwgInSEd+WZc//+CXQb2eBEKNyPe1FQv4ckspJB/j7FCRJYL/nfeI5tNXEYFXP/5f5Ep2TZxwQmyjLcajGZRyX9hic7fRYKkNo+Q6zApm37XJ7KRrK4qwXQw2bRJVFO9HrNiDvAhGZoW9UOeis0tdk69AHT7i/4zLUgU3U3yNsrZvsF5ei2jRgoxfJ7KIxwMc82LIcxH4J7y302xa1mJ8yLi5xK+kmxIVWUsqL/DzgR9Dk/ytKzO1S7A8k3A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: Date: Fri, 27 May 2022 13:13:36 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 04/15] AMD/IOMMU: allow use of superpage mappings Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AS9PR06CA0325.eurprd06.prod.outlook.com (2603:10a6:20b:45b::14) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: add242b3-37c1-4ea0-04ca-08da3fd1f321 X-MS-TrafficTypeDiagnostic: PR3PR04MB7210:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: frSo6a9Gr0H+ZlkH3dnr2Sx/8n36p60w9l3i5UEmZKKBhHLdX4+2UunpCh3OmylAKcqq9ZP1fABTpkOivl/JqNcgw9Xn4xUAyOzLS0i9/Kg5mDXxb1AHPV07SkED5NHmW/aRNK0tdg/HHFsEXvDkfl3qVMPnKPDf1oZGXxAV/PpAPln7fF8sWZNOibqv4quAPwyyeyucc34NRBEjIF9Poh6Nistxrrodm01U3gc9EBe7vEHLE1Mqix8GFsuVfU5ad0n0tIXU6xtJTsdPAgUbK62PQtWphDz2BIuv67BoNlermTid9ACTB4TI2CyIeSm4RsbnZZLsNzjjYvCCdZ1R7MAKKazLOY0tU3+0eN2jPUQO9Et0H0qXwMbQv1SFslzCmuj6jCYb/n6APTOiEclrGTRjzhBr4hZonx++kWaXXBkuXWwmSWJROfmcdJdAi2ghihugFV7almpTxtNZ01VJmwBp8KaL3/OjRGVahR4irFF7kOWFdoYa7mZsnoObcRFXn41a7u+jKAwJje7b+9TIc0q8CSTzpv4gDgt+lSBKcHbDH9IB058OhHguhFKUtoiQFZ2KTsXX6Ed8c/72LOmyJ/nbi++4HUKTev7g3a3pyIbBk9C3vxWgUQlbeGP3OAXbZ3aOwnPCUhoIRv272heNrispyMBZ7h0A6BzE+wnbbEGmpTyd8gxV3WGGLQfDKFJN8m3XCoHOWFURDCUrGT8H7SKFM/G7tIRKGcYDCfJ0Tq0= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(4326008)(6486002)(8676002)(66476007)(6512007)(83380400001)(8936002)(508600001)(5660300002)(66946007)(66556008)(26005)(38100700002)(2906002)(86362001)(6506007)(186003)(54906003)(316002)(36756003)(6916009)(31686004)(31696002)(2616005)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Xaz5O0bRRd92b2qVsuWvM67Ldd6oDF3vUq04yULd01RN9S/GeXDCPYY5jmX8bfDxkyWtMW43N272KSpzGwWPLyQAhtjAq/3S6iHuVZUj86zMsATVhriQ5jR12bvzrY86v/YcKGspnIj9/PI9/x0SXVgAVLnY4T/GC3RhLnKI2pBGTmbldcm4joNXZR2yYm5yxJPsMztSlhhYmG1Xkzst4AJS3LbEuDghCtYCSeGkLC/uz4vaIEUidn2/yqqPPNtegjX8+GceSSQVDywh9p/DWTSRcoNXnMkZpSMpyhluXzKZcRQwOIAJ12lt/EtduYDXb66LO99GE6H1fXJtrclEd0QQ5ufLzwlR38NLoh59V0ii/52EPCBydEw6McEB1a2vUFx+DhOeBTDH/iWXj+oAOqo2mbr4p1fcgqviQTDVEboCyY3Q0+zc/sCp2SLkjiki2gCG3eVLYUtCECGIcZC3MndVO6ReLCvvMapaWuiuto7DcJU+bxeEATBcrFXD5T9vmvtKPekbNdD/bdkAv8RX/Nlqh5fXOdZjccZQPnauUAG5pSJ8nQSacTPHrGWg2FzeHw3uSQh9KZ2omdeiJt6hlmDK2JeGau8jePk8y1Lw90WDDaGCQd9bRR8hVPnNfIdyTzgEqK6SWm+OuzAkwTnQ2J4V3uKeGQg0SkqRcCgVhj9/GU2I22mpNO4fxHQigwWANPZgrz0U1C7XRlC/u7QNbxbof4SqpmRz2e1pUsUQe/TXIyInqHTIXGfEqkv1erHjNvRzCZDPRDYZKhoMl8Wev/OG1KeRtvcatGUWM+ktsgn/c+92kjn088mi76gafLKXWxPWjiwWvqYcXjxQQg6hq39N1wwuR21d4cSpcWcUEg0+lG3muhlCd7RY26pSOKmvWvyEY/fKP7bY5wLaZvfxVoW9cmTbb75C2O8+QDy8z9sZIEb089ac6CaLJ9PQaTJzFMJbN/hWbHdctFH3TbgqSZVvxAJhHCzSNqwt98B+n1VxD3Ork819kGtq5rWt/cMh0wdd9fpRvWnSHrfmteP2e0GMkYkvhcmzgdEWoYKbBs6/HVy79zlz+aCcag5RjP4x7jYTwZp9L1PcgoyGVPW0rczMiqp9g/f9u/Toy73ShE/yTQ6jt9AfXWxewzotz9Ej2Ovil29TbXxv+8eVLxCSc9GC+XVbd3okcwSvU1XOql+JPY0bRpJ9WnvtXWl29NVFoAu0f4U1iAmaP3O2SxX3TkN9ipaC+LjtoLk9+hk3flqqAy52KRs1j8YS1DjldU7yIqsfJmPM5xMknwSirlHEMMfRTcd+W/YaKXbXDaoEqkCE5/BBzMNMJvTE1SPqVA6XhZmMd0TxKhIMJVR8RpxhG4f5BqwN1XLQBDQdzCfnGqBH/506hIWXP6cU1xdR1j7qqIemgmMWSUXoDS1HO5MiCzTWWXI227d8LiokVWp+jM1sbjIhFICxpgMLJjajCZiIhdcpH0U/Hi43/VAl0A9YtC9ovyBfk4oRoeksr32KY3eGxjK3gbZooqlmIbl8w8W6gd+fXoa3OswhVPYl6be1MpvZOrbGg9GNvxwYslUd9dqRSkj1OIxgNYI3ZDD99HZkmYb2xDwNX8h0ub9dUJzru8lc4T0JgoGeTqZzyOyWKk1WKnMqIDNFRsA4Mpz2WGonCOk1GJ/jPUJE8rScUJhIL9w28dyUNtNKN0eDp9Od7PC3AB9aVT2b6CevpZrH/uFlxvp09R0l6IEmVyTlSbkQ5Q== X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: add242b3-37c1-4ea0-04ca-08da3fd1f321 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:13:38.7494 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ARsKo5ldFsCbhgjvkkfFOMel/tnf5fG3x6yuqWgsv+4GJsVuxUHaHyeOolESoyMUMfiql+TTla5gyPS21/G13Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR04MB7210 No separate feature flags exist which would control availability of these; the only restriction is HATS (establishing the maximum number of page table levels in general), and even that has a lower bound of 4. Thus we can unconditionally announce 2M and 1G mappings. (Via non- default page sizes the implementation in principle permits arbitrary size mappings, but these require multiple identical leaf PTEs to be written, which isn't all that different from having to write multiple consecutive PTEs with increasing frame numbers. IMO that's therefore beneficial only on hardware where suitable TLBs exist; I'm unaware of such hardware.) Note that in principle 512G and 256T mappings could also be supported right away, but the freeing of page tables (to be introduced in subsequent patches) when replacing a sufficiently populated tree with a single huge page would need suitable preemption, which will require extra work. Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monné --- v5: Drop PAGE_SIZE_512G. In amd_iommu_{,un}map_page() assert page order is supported. v4: Change type of queue_free_pt()'s 1st parameter. Re-base. v3: Rename queue_free_pt()'s last parameter. Replace "level > 1" checks where possible. --- a/xen/drivers/passthrough/amd/iommu_map.c +++ b/xen/drivers/passthrough/amd/iommu_map.c @@ -32,12 +32,13 @@ static unsigned int pfn_to_pde_idx(unsig } static union amd_iommu_pte clear_iommu_pte_present(unsigned long l1_mfn, - unsigned long dfn) + unsigned long dfn, + unsigned int level) { union amd_iommu_pte *table, *pte, old; table = map_domain_page(_mfn(l1_mfn)); - pte = &table[pfn_to_pde_idx(dfn, 1)]; + pte = &table[pfn_to_pde_idx(dfn, level)]; old = *pte; write_atomic(&pte->raw, 0); @@ -351,15 +352,39 @@ static int iommu_pde_from_dfn(struct dom return 0; } +static void queue_free_pt(struct domain_iommu *hd, mfn_t mfn, unsigned int level) +{ + if ( level > 1 ) + { + union amd_iommu_pte *pt = map_domain_page(mfn); + unsigned int i; + + for ( i = 0; i < PTE_PER_TABLE_SIZE; ++i ) + if ( pt[i].pr && pt[i].next_level ) + { + ASSERT(pt[i].next_level < level); + queue_free_pt(hd, _mfn(pt[i].mfn), pt[i].next_level); + } + + unmap_domain_page(pt); + } + + iommu_queue_free_pgtable(hd, mfn_to_page(mfn)); +} + int cf_check amd_iommu_map_page( struct domain *d, dfn_t dfn, mfn_t mfn, unsigned int flags, unsigned int *flush_flags) { struct domain_iommu *hd = dom_iommu(d); + unsigned int level = (IOMMUF_order(flags) / PTE_PER_TABLE_SHIFT) + 1; int rc; unsigned long pt_mfn = 0; union amd_iommu_pte old; + ASSERT((hd->platform_ops->page_sizes >> IOMMUF_order(flags)) & + PAGE_SIZE_4K); + spin_lock(&hd->arch.mapping_lock); /* @@ -384,7 +409,7 @@ int cf_check amd_iommu_map_page( return rc; } - if ( iommu_pde_from_dfn(d, dfn_x(dfn), 1, &pt_mfn, flush_flags, true) || + if ( iommu_pde_from_dfn(d, dfn_x(dfn), level, &pt_mfn, flush_flags, true) || !pt_mfn ) { spin_unlock(&hd->arch.mapping_lock); @@ -394,8 +419,8 @@ int cf_check amd_iommu_map_page( return -EFAULT; } - /* Install 4k mapping */ - old = set_iommu_pte_present(pt_mfn, dfn_x(dfn), mfn_x(mfn), 1, + /* Install mapping */ + old = set_iommu_pte_present(pt_mfn, dfn_x(dfn), mfn_x(mfn), level, (flags & IOMMUF_writable), (flags & IOMMUF_readable)); @@ -403,8 +428,13 @@ int cf_check amd_iommu_map_page( *flush_flags |= IOMMU_FLUSHF_added; if ( old.pr ) + { *flush_flags |= IOMMU_FLUSHF_modified; + if ( IOMMUF_order(flags) && old.next_level ) + queue_free_pt(hd, _mfn(old.mfn), old.next_level); + } + return 0; } @@ -413,8 +443,15 @@ int cf_check amd_iommu_unmap_page( { unsigned long pt_mfn = 0; struct domain_iommu *hd = dom_iommu(d); + unsigned int level = (order / PTE_PER_TABLE_SHIFT) + 1; union amd_iommu_pte old = {}; + /* + * While really we could unmap at any granularity, for now we assume unmaps + * are issued by common code only at the same granularity as maps. + */ + ASSERT((hd->platform_ops->page_sizes >> order) & PAGE_SIZE_4K); + spin_lock(&hd->arch.mapping_lock); if ( !hd->arch.amd.root_table ) @@ -423,7 +460,7 @@ int cf_check amd_iommu_unmap_page( return 0; } - if ( iommu_pde_from_dfn(d, dfn_x(dfn), 1, &pt_mfn, flush_flags, false) ) + if ( iommu_pde_from_dfn(d, dfn_x(dfn), level, &pt_mfn, flush_flags, false) ) { spin_unlock(&hd->arch.mapping_lock); AMD_IOMMU_ERROR("invalid IO pagetable entry dfn = %"PRI_dfn"\n", @@ -435,14 +472,19 @@ int cf_check amd_iommu_unmap_page( if ( pt_mfn ) { /* Mark PTE as 'page not present'. */ - old = clear_iommu_pte_present(pt_mfn, dfn_x(dfn)); + old = clear_iommu_pte_present(pt_mfn, dfn_x(dfn), level); } spin_unlock(&hd->arch.mapping_lock); if ( old.pr ) + { *flush_flags |= IOMMU_FLUSHF_modified; + if ( order && old.next_level ) + queue_free_pt(hd, _mfn(old.mfn), old.next_level); + } + return 0; } --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -747,7 +747,7 @@ static void cf_check amd_dump_page_table } static const struct iommu_ops __initconst_cf_clobber _iommu_ops = { - .page_sizes = PAGE_SIZE_4K, + .page_sizes = PAGE_SIZE_4K | PAGE_SIZE_2M | PAGE_SIZE_1G, .init = amd_iommu_domain_init, .hwdom_init = amd_iommu_hwdom_init, .quarantine_init = amd_iommu_quarantine_init, --- a/xen/include/xen/page-defs.h +++ b/xen/include/xen/page-defs.h @@ -21,4 +21,14 @@ #define PAGE_MASK_64K PAGE_MASK_GRAN(64K) #define PAGE_ALIGN_64K(addr) PAGE_ALIGN_GRAN(64K, addr) +#define PAGE_SHIFT_2M 21 +#define PAGE_SIZE_2M PAGE_SIZE_GRAN(2M) +#define PAGE_MASK_2M PAGE_MASK_GRAN(2M) +#define PAGE_ALIGN_2M(addr) PAGE_ALIGN_GRAN(2M, addr) + +#define PAGE_SHIFT_1G 30 +#define PAGE_SIZE_1G PAGE_SIZE_GRAN(1G) +#define PAGE_MASK_1G PAGE_MASK_GRAN(1G) +#define PAGE_ALIGN_1G(addr) PAGE_ALIGN_GRAN(1G, addr) + #endif /* __XEN_PAGE_DEFS_H__ */ From patchwork Fri May 27 11:14:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863296 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 E4DA1C433F5 for ; Fri, 27 May 2022 11:14:24 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.337963.562684 (Exim 4.92) (envelope-from ) id 1nuXv3-0000ty-Kn; Fri, 27 May 2022 11:14:13 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 337963.562684; Fri, 27 May 2022 11:14:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXv3-0000tr-Hh; Fri, 27 May 2022 11:14:13 +0000 Received: by outflank-mailman (input) for mailman id 337963; Fri, 27 May 2022 11:14:12 +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 1nuXv2-0008SK-19 for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:14:12 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.109.102]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 22274aad-ddae-11ec-837f-e5687231ffcc; Fri, 27 May 2022 13:14:11 +0200 (CEST) Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-ve1eur03lp2050.outbound.protection.outlook.com [104.47.9.50]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-27-fmljMXHfPk-f5mcWokOwhw-1; Fri, 27 May 2022 13:14:09 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by PR3PR04MB7210.eurprd04.prod.outlook.com (2603:10a6:102:8f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.15; Fri, 27 May 2022 11:14:08 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:14:08 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 22274aad-ddae-11ec-837f-e5687231ffcc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650050; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qr+KZP5Q4gC1/YO9J87FKq30YhywATFS4ksFiE6GDtE=; b=hPSwJ3iYlUtUzgluIMOnZMRrUXfKi/6cNcNmWwUaAjrz83+uGak1X8K5RlXk1jQO5fRSTv /Yt/Zpw9eDmBhkpl0LcTe7mk9teLpGEj6F3P5lhDgDtsrIyAXoT6Zjnz/8dj7n+AvBydT2 oThSMZtoFKdqW0KQgK8bk5yDMF1JIZU= X-MC-Unique: fmljMXHfPk-f5mcWokOwhw-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=I9m7/WYFjm7m5/zAK8CUrPo/Ovs8K7xHl+tCNJC2MfqGmP1LRizO/KsTFBZ/aMyP6tUqM+Ybp6Qf6/LuYZQl6DdEHrYU7gAWmtPy2cTU1L5GoJuTMqZHKj+PpNEdZ9wCBV1C/f38A1y2JICLfd9F/buMPRz7oNm6PgGkzXhTGyYXYSYvukWvlUsgZH1ucCr+L8gU0gHdYYl/3/qTdJsiluiVXnjgvnrElPjGcnUupgz90JghhJRM/iUyMvQMMMbKK7QFV6ougYiCSo/NUgrbx2JsQ+zonQXHFm6R5nXGUu3ebwuek1Wdj5OFrKH2FMrtbceNb9dr2V7Wl17MDqNpTg== 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=A+qwjWOHPpUCurQ63u+c7grNKTN+vnNpokZN1aH+eg0=; b=SIoCQIN/cCq7IIW2Ga/2B7dfP+O6K2LrNQMGYUvkl0TE9/TuD5Z+q/nrppDuHq/eEhx21myKlabl2grXxBnWRVpiRQ5cwUMpUplqoNI03cgcFUetaCdvfJEU6qKt9tSOYla1EMamFkRkFOOe+W1bFxExCfb00y3R9y1JB1SAi8AGNcSg9vNS0fFySyjd2TNTP708sMyGxFdJN2g9DIMmJ7S8kLbYbOZKkdE1+2J4IzKMO7cws/GTJJ1pWqZ3O7msOrkVIvx8oG42RPwAZFKKDdttK9y+MLiVHiOakUmID4qzJOlrn7t4eS4lw9G4w4Z0ch/4CM5v7qtjzIOTZep/cQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <108ae661-147e-c107-c826-1db67e0f0e5b@suse.com> Date: Fri, 27 May 2022 13:14:06 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 05/15] VT-d: allow use of superpage mappings Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AS9PR06CA0375.eurprd06.prod.outlook.com (2603:10a6:20b:460::19) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 44ad1ae0-ccc4-47ba-381a-08da3fd204dc X-MS-TrafficTypeDiagnostic: PR3PR04MB7210:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: APk5CsImZyV4iHiy+CsXo/pqpfwe7q9fK8CN8jydj/aqQeLkkFDKfAE8dkmoStRpFqB3WSD7aIuns4UQjtTW3o0IFdhVodVFqu4OoMLuPBRS06O+GCz35zwgWCjbX403GGA0wx3ugdPlcPYjdNS53J5mDqDQL0y9g21+/0J3cua1FH0yrzannacUlypmItqm9FvGD/li20yoDqnWDymKOJH8wf0taTI2XiWqFRxwK3QrUApOGgaWbBSj5BdLbjrOfpuCXlm8ksBu1431YrwWz7K3tYKXke2XGIJSEd6IPtM5ceNfbv2vet0VraPDmSXzsPwUlBLkA8RBMgQC9ifqLGZUBwimNGydXMgI8xeRK05bIzDQfxjM8Xs9uvHhCbAyNUs1UU607sC0HeOYq3rOahK73wplfj5xBokZb44fPda+gCbXq9l591kgIIcFlFDTUXpte4j6RN+rePi9e9QLyqcOpYOsVeyhjTe9psty6RxMjiPxPngUh1dy/bT/gG0xgknFk2WwhfwyndZoAPfaRVWAe/TYRwqlGOEpnpgTJub09ByRuz+9FR7118M3645pgncEJmQ65wCCzN/f2JAJBct+fe9Y/VW6dAq9NOX9vPLGVZalWBs0fNNNmhwPlBF8gdrZDE/BHxxzVXu7X/NZiS8lKolMXj1OcKoDETSD9Ac3DMB9wSEzzglBg/2ElaVI2ajdBOf97eV3Veo3OA0mQ3isxk6qW2EePz5Be8qunv1xjoDDcidDHT+yWbiWDp6b X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(4326008)(6486002)(8676002)(66476007)(6512007)(83380400001)(8936002)(508600001)(5660300002)(66946007)(66556008)(26005)(38100700002)(2906002)(86362001)(6506007)(186003)(54906003)(316002)(36756003)(6916009)(31686004)(31696002)(2616005)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UL6SuA47fxEt9dHrRcLtF7R9Eow6uKVxuEzpV0ULyeUHKY59wjMhLPtEo59n632xhuDPLs1Wn8Jn2EYEoZKjo42APdCC6JmCehE0morsQHnFM/5n8uJ3KCYm3xUCjv14yz9WAlEP9z3l6KsUK1BPE0AW1GGjDJrqEkLrhxBDwRtwebS4u3RX5Nsqp1+pNMXIhKayQ7Z6xkx0CdXoKPENBaehBmFG30my8V3izDjfNutIO2BUSn6+unPGhfoAmKgiYicMWys6olOPqfBXJN6lAajp1wgSKnoP96mB6w1DlUDB0f77nNT/bA0ypaj7r7c868Q4dfTQgNGSkAly/9Po4edzvBiz9f4/+SGULl6o//CLhXZEZR3BCEVErx7efn6uohI43nJB2o2SV1aC5MrP2TaFlmxU8UEem78iJwagwUuQA6HlemqB6dR2Nv7lY3LouJVuE7GWevMmjEt0RBnVL+EmoZVoEcgjndC3UTLLw5OQ2EvDpatzCXdmQri1PmZkInn6s4HmWeZaPkfZqyuBkZYO6cK/TjZWbguDHdh94tgo3xuLb/UQxSWikDP94cdV4lH3Ol3N81k5U1bQMhRj9Svas7MFHdLynBntHdAjRBCuOcQgjShHw2lQvpNUMjnkxM9E3byFMLKhsmNIln8b4JytYZTBj5ypPBBhYN+hIuzTX0TGDpOYQU74nfsqAHZWgUwvBwNGQDdqMDcnSdx3ZKM/ZTDLegx7vkSEgvtVQFWLRXEXUB70c7Ha38tA2YRsk/ZDmvb2OOKLY+/4tEINc/3uguDk9gVeRnnWR+psUE2ZZW34ULVMfNsQ6ORCUK6ZRG0XsLViHJiycQEvoza2sM9pdZuKZUyc8KfFyB/gvrCpebT+lbigWJYUc+nwlptwMTaaJvFgNx00yn3C3aKhkUE2J8IsJTpCQJm3EE9n5lTqlzDEAl1Lvvy4EWBo3OLupda2IZCt9G6Et9Q2sx0FyvOUQpfRXgSBDE7tkHOSwLtysHplZq6aXtJkiycD474vEPAmPe/vUtrDXA9IgLku4JQeTMtzDbRZruh96d6tMKDG06zGxeEI/GE9QrWUFHUbommxisvtAJK8gV+mK4BjrDvP+0wsNZkJBJvO6aYDMF1HDmUQgoM7llp7jaw666o/8zAWtr2PXSIX8wDs9Z+GoGkdLmjvmvWQZGTeZNSzPp/+iu6uOqfABWz4moxKkcI3XhLhHZMBSF8MxH1EzXFeoiDDiQJGjNlLupahjQ04Y+VAwb2uybHALmSmbykHh4KUuBn1IEFyOmoBv1AL2eyCIHHV+pjWhuyEUu0hDjgoM7GRCrSSztJ6ylQ5b5ONaiTXLCiKcsv9cfxyjnPWykB9KbB6eJIf8eiN2jKLqhzKk1Bn/PwrMHvEL+RaePdYBvW9rszX0ftQrmxi+GytrQCo5h89MhaCehO435qv0s5ZlwdoNeXug+rcEOhO4E9vaJoEF6TM//6/fEAAYEP8NQ5iDfjtYwNe+Vv12hSCtTuRkOicglcn67gOlG9nKETZiYZLcrCHPn7IU9UuQjDWjpxHbhbTA0I1/emsm8WuFWGEUr+D8Fi7bhZEZ8MR6xXLZW/nEE9dqZCxqM3lVivKJ8rIHFt9d9jwqXahMDBfDP97rN6coj6/Ua6tz2T/tVMsM+38PgCXvT2hkhG7hgky5ABfaINFF94UW94sDuUhmoyacf9xZwnVnVfKpn3wn5a4oqnqphBSLN0HeMXiSJ6EfCZ25A== X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 44ad1ae0-ccc4-47ba-381a-08da3fd204dc X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:14:08.5444 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 4V45kra9gd3xz7dKR++5pMJh1GI2dkk25mYNQpwz9vUq5ZSge1MRyvygP/3vNOIfqGTb4+C0cKB/19mtAWLkFw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR04MB7210 ... depending on feature availability (and absence of quirks). Also make the page table dumping function aware of superpages. Signed-off-by: Jan Beulich Reviewed-by: Kevin Tian Reviewed-by: Roger Pau Monné --- v5: In intel_iommu_{,un}map_page() assert page order is supported. v4: Change type of queue_free_pt()'s 1st parameter. Re-base. v3: Rename queue_free_pt()'s last parameter. Replace "level > 1" checks where possible. Tighten assertion. --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -784,18 +784,37 @@ static int __must_check cf_check iommu_f return iommu_flush_iotlb(d, INVALID_DFN, 0, 0); } +static void queue_free_pt(struct domain_iommu *hd, mfn_t mfn, unsigned int level) +{ + if ( level > 1 ) + { + struct dma_pte *pt = map_domain_page(mfn); + unsigned int i; + + for ( i = 0; i < PTE_NUM; ++i ) + if ( dma_pte_present(pt[i]) && !dma_pte_superpage(pt[i]) ) + queue_free_pt(hd, maddr_to_mfn(dma_pte_addr(pt[i])), + level - 1); + + unmap_domain_page(pt); + } + + iommu_queue_free_pgtable(hd, mfn_to_page(mfn)); +} + /* clear one page's page table */ static int dma_pte_clear_one(struct domain *domain, daddr_t addr, unsigned int order, unsigned int *flush_flags) { struct domain_iommu *hd = dom_iommu(domain); - struct dma_pte *page = NULL, *pte = NULL; + struct dma_pte *page = NULL, *pte = NULL, old; u64 pg_maddr; + unsigned int level = (order / LEVEL_STRIDE) + 1; spin_lock(&hd->arch.mapping_lock); - /* get last level pte */ - pg_maddr = addr_to_dma_page_maddr(domain, addr, 1, flush_flags, false); + /* get target level pte */ + pg_maddr = addr_to_dma_page_maddr(domain, addr, level, flush_flags, false); if ( pg_maddr < PAGE_SIZE ) { spin_unlock(&hd->arch.mapping_lock); @@ -803,7 +822,7 @@ static int dma_pte_clear_one(struct doma } page = (struct dma_pte *)map_vtd_domain_page(pg_maddr); - pte = page + address_level_offset(addr, 1); + pte = &page[address_level_offset(addr, level)]; if ( !dma_pte_present(*pte) ) { @@ -812,14 +831,20 @@ static int dma_pte_clear_one(struct doma return 0; } + old = *pte; dma_clear_pte(*pte); - *flush_flags |= IOMMU_FLUSHF_modified; spin_unlock(&hd->arch.mapping_lock); iommu_sync_cache(pte, sizeof(struct dma_pte)); unmap_vtd_domain_page(page); + *flush_flags |= IOMMU_FLUSHF_modified; + + if ( order && !dma_pte_superpage(old) ) + queue_free_pt(hd, maddr_to_mfn(dma_pte_addr(old)), + order / LEVEL_STRIDE); + return 0; } @@ -2097,8 +2122,12 @@ static int __must_check cf_check intel_i struct domain_iommu *hd = dom_iommu(d); struct dma_pte *page, *pte, old, new = {}; u64 pg_maddr; + unsigned int level = (IOMMUF_order(flags) / LEVEL_STRIDE) + 1; int rc = 0; + ASSERT((hd->platform_ops->page_sizes >> IOMMUF_order(flags)) & + PAGE_SIZE_4K); + /* Do nothing if VT-d shares EPT page table */ if ( iommu_use_hap_pt(d) ) return 0; @@ -2121,7 +2150,7 @@ static int __must_check cf_check intel_i return 0; } - pg_maddr = addr_to_dma_page_maddr(d, dfn_to_daddr(dfn), 1, flush_flags, + pg_maddr = addr_to_dma_page_maddr(d, dfn_to_daddr(dfn), level, flush_flags, true); if ( pg_maddr < PAGE_SIZE ) { @@ -2130,13 +2159,15 @@ static int __must_check cf_check intel_i } page = (struct dma_pte *)map_vtd_domain_page(pg_maddr); - pte = &page[dfn_x(dfn) & LEVEL_MASK]; + pte = &page[address_level_offset(dfn_to_daddr(dfn), level)]; old = *pte; dma_set_pte_addr(new, mfn_to_maddr(mfn)); dma_set_pte_prot(new, ((flags & IOMMUF_readable) ? DMA_PTE_READ : 0) | ((flags & IOMMUF_writable) ? DMA_PTE_WRITE : 0)); + if ( IOMMUF_order(flags) ) + dma_set_pte_superpage(new); /* Set the SNP on leaf page table if Snoop Control available */ if ( iommu_snoop ) @@ -2157,14 +2188,26 @@ static int __must_check cf_check intel_i *flush_flags |= IOMMU_FLUSHF_added; if ( dma_pte_present(old) ) + { *flush_flags |= IOMMU_FLUSHF_modified; + if ( IOMMUF_order(flags) && !dma_pte_superpage(old) ) + queue_free_pt(hd, maddr_to_mfn(dma_pte_addr(old)), + IOMMUF_order(flags) / LEVEL_STRIDE); + } + return rc; } static int __must_check cf_check intel_iommu_unmap_page( struct domain *d, dfn_t dfn, unsigned int order, unsigned int *flush_flags) { + /* + * While really we could unmap at any granularity, for now we assume unmaps + * are issued by common code only at the same granularity as maps. + */ + ASSERT((dom_iommu(d)->platform_ops->page_sizes >> order) & PAGE_SIZE_4K); + /* Do nothing if VT-d shares EPT page table */ if ( iommu_use_hap_pt(d) ) return 0; @@ -2519,6 +2562,7 @@ static int __init cf_check vtd_setup(voi { struct acpi_drhd_unit *drhd; struct vtd_iommu *iommu; + unsigned int large_sizes = PAGE_SIZE_2M | PAGE_SIZE_1G; int ret; bool reg_inval_supported = true; @@ -2561,6 +2605,11 @@ static int __init cf_check vtd_setup(voi cap_sps_2mb(iommu->cap) ? ", 2MB" : "", cap_sps_1gb(iommu->cap) ? ", 1GB" : ""); + if ( !cap_sps_2mb(iommu->cap) ) + large_sizes &= ~PAGE_SIZE_2M; + if ( !cap_sps_1gb(iommu->cap) ) + large_sizes &= ~PAGE_SIZE_1G; + #ifndef iommu_snoop if ( iommu_snoop && !ecap_snp_ctl(iommu->ecap) ) iommu_snoop = false; @@ -2632,6 +2681,9 @@ static int __init cf_check vtd_setup(voi if ( ret ) goto error; + ASSERT(iommu_ops.page_sizes == PAGE_SIZE_4K); + iommu_ops.page_sizes |= large_sizes; + register_keyhandler('V', vtd_dump_iommu_info, "dump iommu info", 1); return 0; @@ -2964,7 +3016,7 @@ static void vtd_dump_page_table_level(pa continue; address = gpa + offset_level_address(i, level); - if ( next_level >= 1 ) + if ( next_level && !dma_pte_superpage(*pte) ) vtd_dump_page_table_level(dma_pte_addr(*pte), next_level, address, indent + 1); else From patchwork Fri May 27 11:16:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863297 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 0A8F0C433F5 for ; Fri, 27 May 2022 11:16:50 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.337971.562694 (Exim 4.92) (envelope-from ) id 1nuXxK-0001cx-4q; Fri, 27 May 2022 11:16:34 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 337971.562694; Fri, 27 May 2022 11:16:34 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXxK-0001cq-24; Fri, 27 May 2022 11:16:34 +0000 Received: by outflank-mailman (input) for mailman id 337971; Fri, 27 May 2022 11:16:32 +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 1nuXxI-0001ck-9J for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:16:32 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.109.102]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 7576a164-ddae-11ec-837f-e5687231ffcc; Fri, 27 May 2022 13:16:30 +0200 (CEST) Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03lp2168.outbound.protection.outlook.com [104.47.51.168]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-31-B9DPddiIN52f1adwB54yWw-1; Fri, 27 May 2022 13:16:29 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM6PR04MB5831.eurprd04.prod.outlook.com (2603:10a6:20b:a8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Fri, 27 May 2022 11:16:27 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:16:27 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7576a164-ddae-11ec-837f-e5687231ffcc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650190; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bM6AKT2K8jEmZRUNQZbq7buu+njl5MQC2zWp1UMeQTk=; b=baKMvUM1JDVOsxLPJuIQw7pas24OnCSdA7ZzK3oMz3xBFnVfVGNQ+/hSgiXOVnfsVvMGix oy7Zq/Hxh9MndqVSEatz7AhvH0a9Gk1/j/tuU65vdbvU+AwAFoMYZ8TvedhBkGGqdoC0zN UEnbNuVloUh6HsCnsousrAiGyYMO+c4= X-MC-Unique: B9DPddiIN52f1adwB54yWw-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=edGC8k8OfY+4mXs+IRiHqfqsiaWtdBltJ7KU6Mk9NrxOGGtSwcAbU5hboRRGgcrf421L4ACrKVqdK8KFygokU4/3GbRX+/DDh5/U64SWE3iNpk7QDU39armjdmlvDBt5HWdDEHglv2K8CLyYROIIed3qwuhz5B008wbvyvB61BeClGpmI9il4d55L9qN3/5ulKpilp5yU50WwugEHYW6m6oEFnwQmqACCYmrSbCBDQpDNWOPz3YQJScwtSQWChChG4xMYdk5eGu77GPRPUZNtP2LZsF5cXjmuzvOKuktGGHIsjc/fXlyc9AItrl4pX+atOTXCSviXD61pDCn9v5rJA== 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=ffLWJC6aWwnaCLPYH0Qbww7Cy6DESc8jbX3Bj63/FiM=; b=f0m1nj8lAmd/8yWL8YJj7Got8Cb0hri6BxfRS/qzi8uFdyJQwl7jphJZQkRp4HOaB8lYTn81n02rdEOU8jVfUh+mGGR12sqol44PMFpIYi2P3AFzEAYeLd1fl1A1l8BVuy/Ns57iJsahZ5/fXScDM2O3GlOWlS/YzO+e0jqFvcrtb6/FFNKRphPm+WB4MBEyk6dIO6/m0NGBmbUf9E3ic7x1y2+kDO8dfdE5AQjCHZEih8xyDU2amA8eo0OmagEpopK4zeSnujSj4yFt02ZH6zb+cjSsxmINvYBzHGSxfonEgVU9VmBVNdGIB6fgy9D6Ve/QpUbw/d0aFeIsUQoxiw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <2807d8c3-054d-81a4-172d-9a08f023d300@suse.com> Date: Fri, 27 May 2022 13:16:24 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 06/15] IOMMU: fold flush-all hook into "flush one" Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Julien Grall , Rahul Singh , Bertrand Marquis , Stefano Stabellini , Volodymyr Babchuk References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AS8PR05CA0029.eurprd05.prod.outlook.com (2603:10a6:20b:311::34) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3d1abd8a-c1b4-4791-6fc7-08da3fd2578e X-MS-TrafficTypeDiagnostic: AM6PR04MB5831:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Brywkv2oRW/XHE/tfRdtEQCvd68Lfk6eQTLHsJDeI0mcd4tQhpUBaNwjbMYxF8feX3R5cZrda8+dwfJZl1ZvpcuTtTcwRnyanuqO9T/EvQ54a6hUvQtXXg1NQuchriRaJuN/u+8ueQbqMuDlrA62NkJ+ZNxq3+fCfkoyMIhwzLTtNCBQxHq6/xBWesdDTKFYYL8jSkpNg6DWE7gHB3GGimIYz96sAEYv3mX7tMUFrRv9te8LVwNC2W4WM2lxPPHs5JLwoGZpkXSbE4UHXL5aAoOnoPLaOZls4Mfx7YiDoPctMIgwla30tijAPobIRHwqNoXzedWi3lMTG0dVOs1ewIiqZl+z6eK4dB/rhbo3sjbL8e4AAlM2j/KpIClU+iHYU1bHNeVSmPmLYvAqpTnUlNFTpQEbC0E8bLN/SguO3ojtaw3sMjXeO2FqTcp3XA9WuSuJXBmzPc4gIhij7mv9vMaNtng9+LQoGLh0y8B6KuDKy/tzbx1kau/OWfXqHAc3qIPi2ceYEhjT9cmtfkiDildgiWgTsKzTNFTpQdQrgTrDsI5rTo48GXtOM/y2IwL1FrNg/hg4Q2b1+FRWw4VHu5HITgEQArg6v4aRdFYWSPzh74ifYTfL534k4M9KlQfnFcsPIfgHFiwyaHBZ25AMwV8paDqbinUISYK19cwYeqEhoz60lseZB1vEeY25qykDB6MRtMhwl2PL57+cf4QvWPHn+lOcbxWbiSXWNidr8uMST4SBua1+UcgUTe1jLvbq X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(6512007)(2616005)(6666004)(26005)(6506007)(316002)(66556008)(66946007)(54906003)(508600001)(8676002)(4326008)(66476007)(6486002)(86362001)(38100700002)(31696002)(186003)(83380400001)(5660300002)(31686004)(2906002)(8936002)(36756003)(6916009)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: wWIDvZvDsSY76jAYGPsRY7YJR94ywwYnIwjZTgPEVB9uyy1sJKcOl+u7PzvRUzcYS+MT/oYaz+BGVhiOiNK3hBp8aPUKKFtS/Jch5j2JlD/m+jCq1T0qqQ/JU21vJoZPJFBnsZt654BtcAPOgys5j1JGdYFo4G+dxUwXs+zq0RCwVnG7g32naeB8tgAo79ielTEEX7oPgqdYs3l+rDQEM1ESJT02pqWMzy2n9SSA4M4Lor2wwNudCzr06sztwVJkoFsHMsa9Mb+d+/DO9ne0ZIFdL3a+mjfRvSLGwUT0BsXgKEoOPJ9/UL3eqiAQQWma+k2gcyd0pZmF6gy8K8bLejHhPbudWHJXXv+La5LJL3M+8CFQ4n3KV39fDrNOjv6LlRbD5UuEGpaCpIXX+7lkqmZPXelroUXGgnmeiJyvrNu3PUhArOZDtzr+mh76joQwB8g1mVHndgNEh39NUjzOr+nJPHsjFPGr9j6EIG6Es8k8T3vgLWSsYdIW+OZtLJRqc/0k56nULIJOYNp6RAgOAupvsF3mw0a2ATgAZc8tfIs5+Xe6hM12yhJIEDCc30HLanOde2bWJ09RPeu9/UCkuSjvpEoxFvP7dtTH/yTXaCD3T5zl1FIvRqRuWHcWNC+8B+F5lr6ciJw9dHm8Zq+Z7SqCOjyQsi9SYhbxoc6t5lpwsgL5x4msl45q61VV7uRcmm8Dsi1V9bAn6dBwZ6f5x4c0J9XTfDYdhHRoMwiJxMenXh7TLGvKmbtE/RHNaVYpReOAngvEwaC3Erx3ziV7nLl6Osax+lzSgH+cgMI1AkWBmadky46SaEw8earESTAvZp4Gw3uAzRFpL+zS0kH7hW2YvO5/ALBHIzi5t8MNFIt4cwLEqpQBan6misHGxvTDP56L3yfV+FMm0TMMwr0lMi0nHp+XwLJoO2rp+wDUjsGuuSoH4jz1UThZxkWsoysvWiPH0D3ERDnLaQjrdv+cWhYBSADL4bbUweOnztkZ3BiYsCdia9ZX+GT96UNQRAk8yjGyodwvnaCmVf9wxFONqlJMvw6GUwNaaeYgrpLxMHnB4mW5WAtFVr1HatKoCG8A8lm4j4qX1QB5Dg7ZDnaDdjPrg0O7xrUlq8KB33wLsEfBc9qR+Fhv3/KZLuXgfQYl3F1QlVhNYUlq3Gr+N8Cf9eK+DE1i/3ZaKmCuHPBfoS3TTrf+wUrHUQxW5ErkRzJVG1TEWgS1V9BcPBoglRxpSXEeIHgBj0yuzk2KABqUYAOlftyVkaL4vi4AGkIg+A0XQ+KLQZ6ozwkoLVVTIMj8gf0NiBr8MVyol/5jG9i2Rs6zRmDAvvPY1GUibgF4Yy17b3pilq9AlzTrVGpCSVyHp6sV9+tq1Tfu7Mz/l0MpJqo5PEjXvJyUtUDmvjOWVRzZT/nVZ+57ns9ZGZVij+f6xJCOqencKK9MS3JsufKnvNpo1ukFRbB0IWCZOs33YxL5n+f6AA6xz2P+WtziubTBYfwY1wUUWqQP2zQCMLn9bi8DrQSLrY0nRU3WNSuygGkJJg6wQRBee+QuPLkfb8+7q5dD4YePyK0qbvOlO+pryu2+wkbx2AYinKcvvXRK+c0yei3oUTD3oQum5XGfHMhhtwquUy1+v9nykelud8hTNMupJ+2QwiKXAP9OcR+wEKpoISwt0j/1EMXo8v2VEOoH/levEC0xUNRv9HCx9Yy8j5t2E7+gQVy3n38+nmlDYSFj7JOLKYWzuS/wxCNHpOKxOg== X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3d1abd8a-c1b4-4791-6fc7-08da3fd2578e X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:16:27.3637 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Tj3ua4thj8HHTjvhKUnoBOgOS2PaUfhIs8BryJxABJRc0lGjsIOSLTWDSDzPWfgLjkQkDiTOTfpMoHcz75wdaQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB5831 Having a separate flush-all hook has always been puzzling me some. We will want to be able to force a full flush via accumulated flush flags from the map/unmap functions. Introduce a respective new flag and fold all flush handling to use the single remaining hook. Note that because of the respective comments in SMMU and IPMMU-VMSA code, I've folded the two prior hook functions into one. For SMMU-v3, which lacks a comment towards incapable hardware, I've left both functions in place on the assumption that selective and full flushes will eventually want separating. Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monné Reviewed-by: Oleksandr Tyshchenko # IPMMU-VMSA, SMMU-V2 Reviewed-by: Rahul Singh # SMMUv3 Acked-by: Julien Grall # Arm Reviewed-by: Kevin Tian --- TBD: What we really are going to need is for the map/unmap functions to specify that a wider region needs flushing than just the one covered by the present set of (un)maps. This may still be less than a full flush, but at least as a first step it seemed better to me to keep things simple and go the flush-all route. --- v4: Re-base. v3: Re-base over changes earlier in the series. v2: New. --- a/xen/drivers/passthrough/amd/iommu.h +++ b/xen/drivers/passthrough/amd/iommu.h @@ -258,7 +258,6 @@ int cf_check amd_iommu_get_reserved_devi int __must_check cf_check amd_iommu_flush_iotlb_pages( struct domain *d, dfn_t dfn, unsigned long page_count, unsigned int flush_flags); -int __must_check cf_check amd_iommu_flush_iotlb_all(struct domain *d); void amd_iommu_print_entries(const struct amd_iommu *iommu, unsigned int dev_id, dfn_t dfn); --- a/xen/drivers/passthrough/amd/iommu_map.c +++ b/xen/drivers/passthrough/amd/iommu_map.c @@ -548,15 +548,18 @@ int cf_check amd_iommu_flush_iotlb_pages { unsigned long dfn_l = dfn_x(dfn); - ASSERT(page_count && !dfn_eq(dfn, INVALID_DFN)); - ASSERT(flush_flags); + if ( !(flush_flags & IOMMU_FLUSHF_all) ) + { + ASSERT(page_count && !dfn_eq(dfn, INVALID_DFN)); + ASSERT(flush_flags); + } /* Unless a PTE was modified, no flush is required */ if ( !(flush_flags & IOMMU_FLUSHF_modified) ) return 0; - /* If the range wraps then just flush everything */ - if ( dfn_l + page_count < dfn_l ) + /* If so requested or if the range wraps then just flush everything. */ + if ( (flush_flags & IOMMU_FLUSHF_all) || dfn_l + page_count < dfn_l ) { amd_iommu_flush_all_pages(d); return 0; @@ -581,13 +584,6 @@ int cf_check amd_iommu_flush_iotlb_pages return 0; } - -int cf_check amd_iommu_flush_iotlb_all(struct domain *d) -{ - amd_iommu_flush_all_pages(d); - - return 0; -} int amd_iommu_reserve_domain_unity_map(struct domain *d, const struct ivrs_unity_map *map, --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -759,7 +759,6 @@ static const struct iommu_ops __initcons .map_page = amd_iommu_map_page, .unmap_page = amd_iommu_unmap_page, .iotlb_flush = amd_iommu_flush_iotlb_pages, - .iotlb_flush_all = amd_iommu_flush_iotlb_all, .reassign_device = reassign_device, .get_device_group_id = amd_iommu_group_id, .enable_x2apic = iov_enable_xt, --- a/xen/drivers/passthrough/arm/ipmmu-vmsa.c +++ b/xen/drivers/passthrough/arm/ipmmu-vmsa.c @@ -1000,13 +1000,19 @@ out: } /* Xen IOMMU ops */ -static int __must_check ipmmu_iotlb_flush_all(struct domain *d) +static int __must_check ipmmu_iotlb_flush(struct domain *d, dfn_t dfn, + unsigned long page_count, + unsigned int flush_flags) { struct ipmmu_vmsa_xen_domain *xen_domain = dom_iommu(d)->arch.priv; + ASSERT(flush_flags); + if ( !xen_domain || !xen_domain->root_domain ) return 0; + /* The hardware doesn't support selective TLB flush. */ + spin_lock(&xen_domain->lock); ipmmu_tlb_invalidate(xen_domain->root_domain); spin_unlock(&xen_domain->lock); @@ -1014,16 +1020,6 @@ static int __must_check ipmmu_iotlb_flus return 0; } -static int __must_check ipmmu_iotlb_flush(struct domain *d, dfn_t dfn, - unsigned long page_count, - unsigned int flush_flags) -{ - ASSERT(flush_flags); - - /* The hardware doesn't support selective TLB flush. */ - return ipmmu_iotlb_flush_all(d); -} - static struct ipmmu_vmsa_domain *ipmmu_get_cache_domain(struct domain *d, struct device *dev) { @@ -1360,7 +1356,6 @@ static const struct iommu_ops ipmmu_iomm .hwdom_init = arch_iommu_hwdom_init, .teardown = ipmmu_iommu_domain_teardown, .iotlb_flush = ipmmu_iotlb_flush, - .iotlb_flush_all = ipmmu_iotlb_flush_all, .assign_device = ipmmu_assign_device, .reassign_device = ipmmu_reassign_device, .map_page = arm_iommu_map_page, --- a/xen/drivers/passthrough/arm/smmu.c +++ b/xen/drivers/passthrough/arm/smmu.c @@ -2648,11 +2648,17 @@ static int force_stage = 2; */ static u32 platform_features = ARM_SMMU_FEAT_COHERENT_WALK; -static int __must_check arm_smmu_iotlb_flush_all(struct domain *d) +static int __must_check arm_smmu_iotlb_flush(struct domain *d, dfn_t dfn, + unsigned long page_count, + unsigned int flush_flags) { struct arm_smmu_xen_domain *smmu_domain = dom_iommu(d)->arch.priv; struct iommu_domain *cfg; + ASSERT(flush_flags); + + /* ARM SMMU v1 doesn't have flush by VMA and VMID */ + spin_lock(&smmu_domain->lock); list_for_each_entry(cfg, &smmu_domain->contexts, list) { /* @@ -2669,16 +2675,6 @@ static int __must_check arm_smmu_iotlb_f return 0; } -static int __must_check arm_smmu_iotlb_flush(struct domain *d, dfn_t dfn, - unsigned long page_count, - unsigned int flush_flags) -{ - ASSERT(flush_flags); - - /* ARM SMMU v1 doesn't have flush by VMA and VMID */ - return arm_smmu_iotlb_flush_all(d); -} - static struct iommu_domain *arm_smmu_get_domain(struct domain *d, struct device *dev) { @@ -2863,7 +2859,6 @@ static const struct iommu_ops arm_smmu_i .add_device = arm_smmu_dt_add_device_generic, .teardown = arm_smmu_iommu_domain_teardown, .iotlb_flush = arm_smmu_iotlb_flush, - .iotlb_flush_all = arm_smmu_iotlb_flush_all, .assign_device = arm_smmu_assign_dev, .reassign_device = arm_smmu_reassign_dev, .map_page = arm_iommu_map_page, --- a/xen/drivers/passthrough/arm/smmu-v3.c +++ b/xen/drivers/passthrough/arm/smmu-v3.c @@ -3416,7 +3416,6 @@ static const struct iommu_ops arm_smmu_i .hwdom_init = arch_iommu_hwdom_init, .teardown = arm_smmu_iommu_xen_domain_teardown, .iotlb_flush = arm_smmu_iotlb_flush, - .iotlb_flush_all = arm_smmu_iotlb_flush_all, .assign_device = arm_smmu_assign_dev, .reassign_device = arm_smmu_reassign_dev, .map_page = arm_iommu_map_page, --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -478,15 +478,12 @@ int iommu_iotlb_flush_all(struct domain const struct domain_iommu *hd = dom_iommu(d); int rc; - if ( !is_iommu_enabled(d) || !hd->platform_ops->iotlb_flush_all || + if ( !is_iommu_enabled(d) || !hd->platform_ops->iotlb_flush || !flush_flags ) return 0; - /* - * The operation does a full flush so we don't need to pass the - * flush_flags in. - */ - rc = iommu_call(hd->platform_ops, iotlb_flush_all, d); + rc = iommu_call(hd->platform_ops, iotlb_flush, d, INVALID_DFN, 0, + flush_flags | IOMMU_FLUSHF_all); if ( unlikely(rc) ) { if ( !d->is_shutting_down && printk_ratelimit() ) --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -772,18 +772,21 @@ static int __must_check cf_check iommu_f struct domain *d, dfn_t dfn, unsigned long page_count, unsigned int flush_flags) { - ASSERT(page_count && !dfn_eq(dfn, INVALID_DFN)); - ASSERT(flush_flags); + if ( flush_flags & IOMMU_FLUSHF_all ) + { + dfn = INVALID_DFN; + page_count = 0; + } + else + { + ASSERT(page_count && !dfn_eq(dfn, INVALID_DFN)); + ASSERT(flush_flags); + } return iommu_flush_iotlb(d, dfn, flush_flags & IOMMU_FLUSHF_modified, page_count); } -static int __must_check cf_check iommu_flush_iotlb_all(struct domain *d) -{ - return iommu_flush_iotlb(d, INVALID_DFN, 0, 0); -} - static void queue_free_pt(struct domain_iommu *hd, mfn_t mfn, unsigned int level) { if ( level > 1 ) @@ -3197,7 +3200,6 @@ static const struct iommu_ops __initcons .resume = vtd_resume, .crash_shutdown = vtd_crash_shutdown, .iotlb_flush = iommu_flush_iotlb_pages, - .iotlb_flush_all = iommu_flush_iotlb_all, .get_reserved_device_memory = intel_iommu_get_reserved_device_memory, .dump_page_tables = vtd_dump_page_tables, }; --- a/xen/include/xen/iommu.h +++ b/xen/include/xen/iommu.h @@ -147,9 +147,11 @@ enum { _IOMMU_FLUSHF_added, _IOMMU_FLUSHF_modified, + _IOMMU_FLUSHF_all, }; #define IOMMU_FLUSHF_added (1u << _IOMMU_FLUSHF_added) #define IOMMU_FLUSHF_modified (1u << _IOMMU_FLUSHF_modified) +#define IOMMU_FLUSHF_all (1u << _IOMMU_FLUSHF_all) int __must_check iommu_map(struct domain *d, dfn_t dfn, mfn_t mfn, unsigned long page_count, unsigned int flags, @@ -281,7 +283,6 @@ struct iommu_ops { int __must_check (*iotlb_flush)(struct domain *d, dfn_t dfn, unsigned long page_count, unsigned int flush_flags); - int __must_check (*iotlb_flush_all)(struct domain *d); int (*get_reserved_device_memory)(iommu_grdm_t *, void *); void (*dump_page_tables)(struct domain *d); From patchwork Fri May 27 11:17:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863298 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 71C9CC433EF for ; Fri, 27 May 2022 11:17:27 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.337978.562705 (Exim 4.92) (envelope-from ) id 1nuXxz-0002A7-DV; Fri, 27 May 2022 11:17:15 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 337978.562705; Fri, 27 May 2022 11:17:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXxz-00029y-Al; Fri, 27 May 2022 11:17:15 +0000 Received: by outflank-mailman (input) for mailman id 337978; Fri, 27 May 2022 11:17:14 +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 1nuXxy-0001xA-Ha for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:17:14 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.109.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 8ef28913-ddae-11ec-bd2c-47488cf2e6aa; Fri, 27 May 2022 13:17:13 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05lp2111.outbound.protection.outlook.com [104.47.17.111]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-39-dBmytWmVNeSxHDLZLFECnA-1; Fri, 27 May 2022 13:17:11 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM6PR04MB5831.eurprd04.prod.outlook.com (2603:10a6:20b:a8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Fri, 27 May 2022 11:17:09 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:17:09 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 8ef28913-ddae-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650233; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fkjYnsuJuIkyi038qFhXwji2/Rxpp4JG0amuTKIuhJU=; b=FYDjvmFsXTb2LDfTo+Wfp+gP6bbl9LChp6S3wnzMHnqiCBWTcTDhIgzzmeembQrnQaapWk 3A1XGTujwxTBoEWmQN+YXIjRjqKkC7LRVBi3flmWGfrV4NtGBZo1jIXxtAQrU0kCL0NhiF HKuptOrvVBklbrXHTpcryCZJGK0+yow= X-MC-Unique: dBmytWmVNeSxHDLZLFECnA-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GaiVRM7IsofcFjdsYnr/lvCCJ2LKSMJcB1TSnm7OWgBWE9dgispmLhM2/DJMTRB8TKmLJ98e8V/xn3YXuzAwkis8LEciMRRxhI1I2cfwK4KutMZuXdclPMOtsNIuJyO4O9C11JAR9l1ZeVvyGN9GxVVJA7AENiNkB1iX/fuKi2PieCijDrnGw0FGBfglfbFqjrRvcB23yTjOozGiHEUIrIs0H9HFzJ0TqXfYmatPoWym3snI6swZFsAfdMUTvQnfyVL3sIz8mM2sI4tBqReXEjpJXj8+xL6U78j/0wn9Hh2w7QASxwgGDzliGnUH8K4InQ2VcIAYTw/TrThIlHanJg== 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=RkfpNnHTVuEAPWGDCPWVLqkwh9PtWcL+B3weEOaUpQY=; b=lFAHPueMCaUYBOdm9GM43qgC7pk9uslA+VmHteOjHVpgfm4AMLpummoEBFHtw+TTh77NYgQ+8FKZaDuCHx6h06gJOJwc9ZlUd7uT7/E7HCuVLD1gXEAuCW/+TP5g+k95QX7MG5osptpUv7RCONr9aa/oJ2b0gkDkeqbSd5AUr85nCBhcp++FGZySFEV5FcTjEOcI6R+/lQR2K6SDrvDqQ2pzgCj1JoFFyGP+XxTl4KVaRwRPU5hcOUkytBIvkbx6cBIycln3BjW9jj5cTxxeRK4YSUUJut++fHQ1vTkIf/OOfTa5BHpoFTate0Jsjih1ht655C8xaW0ILR8shbL1nQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <1fec512a-8c7b-69b5-40bf-88b42e9ecb7d@suse.com> Date: Fri, 27 May 2022 13:17:08 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 07/15] x86: introduce helper for recording degree of contiguity in page tables Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AS8PR05CA0020.eurprd05.prod.outlook.com (2603:10a6:20b:311::25) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d999bae7-e061-4c62-c6b6-08da3fd270e7 X-MS-TrafficTypeDiagnostic: AM6PR04MB5831:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: x2aP4wBf3Asdk8nuF2D3A7UefiwMF/84sheBRy3YoZpDJRgKCyZGrvNxmC9+W2inPTpip0sd8rijs/OmRWiyvTe6StmhQ83k8tJJssSQzWN1HD9RhoVvaww4A/r/qtaLfSzyTXBXge7FXEo+0ngg+c5h5Y6FTR2/yWBEPSrwN0NA6UeuEewNv4THS0VDSpX9FWMvUt7hfr3wZe+H7D4/slFvyFoIpSGG3DzsB+AEqTI9fafSGlYxKmCIODJIdliQ9YI82q+gKjesx85aZ2iHTYCkqAPd35eLodPMku4XAZWriKG9RkbqB3v/VN+PzlVTS+rgyr/2T4sM/8e9hCDV02Slhk+AcYZ/4PTckkkP+4yZZeg3xGbharO35w/Wz2wvU+XIec5PbgGyy3/UUrZpdZxOlHnw6h7MnJf7hCZGcIprJACUgXCNFmn6+Q/oXijH4eMemYVPwZn8HjZlgh1492mRkS3+qCmfRf2ZQFwGoWJJLr2vgNBA1iNIUyIBkVm1TrWyahY64id6lou468cHyeZ8k6RtHk2EHuUQaH25UHsAk0C7rpwDhsFfhOUjbYPJXss/KxcvCzLUju2UvvEsNh+MwZBo34BvbyuWhzsZ2peS1ZpdQaA9jwBOdvtmQyNUKA4n6A7la8Y62+bcICTd0IZeaWnWp2j8KodgmU+WS4WjQVIPSbdk8qnQP0U0eUTbkRsHpiDWhHO1s1UcuhhdJsCODgUPNYXNRUhcKNVjjrUV/oog9OIFCge2q32HMuEC X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(6512007)(2616005)(26005)(6506007)(316002)(66556008)(66946007)(54906003)(508600001)(8676002)(4326008)(66476007)(6486002)(86362001)(38100700002)(31696002)(186003)(83380400001)(5660300002)(31686004)(2906002)(8936002)(36756003)(6916009)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: lpnYAR/1UiEYox+o/8FG7fJd210Tsl/sW23QRdqypYJ9a3cQUYuOCXabIVCBUs7Re7KxfLAGNk8ZaWQ0SvDqmfLy6vDZDuVKwJ14Eb64yOYj14G5utPDguKBat08KH6UqMTse/zo1sRmZ2ER2Lb76jKs3/Bi9c3GorEkWmLzvUr0a2YQdp2yK1f9JuqtAFKjnBlzAeeDUUgmEOsc3ZLD40fpIYlLCdt5ndKM0mPVo1d7wGae+CP6XWA/qKwk/euYQhCATBSiOi6OQOADC2OV5/gzu+G8JlMICuum5dX4L/ExI5m/4eCGVK97Au5GwOpIiqwa/RHkP6894not3ezpYtZsiSGcN/zBf9Ew55a/zoHwnKmUSOFRC5zIWqb7JcpXU7NZolAU21txuEuHD2iOxtQim+FP9CAbzm6S3NZJOGLadX4C2egbVZIDRNjauFsRtZgC+6JWcAIPTbYqPQ4t7rIUSiR6P+1H081l2dELJ59z3phbIRVLgT9czSjArqSuSHqFqFhKjUwVInyUtiFvBjoIpr2AUO+EekunRyRnqekymFB00ehE0uCMEf+4PfM2YqrVCRFY3yrEYNhBgkdLDkrHXxhmEsIBwyhX7hqRW22MgBDSePzLBvUCCgg/D9qCK9PRnEgU4dhXCHHsJOrQZfw9TwcU/XENy43DrUlC8yMmK+ud33jxKCaV7QxCLyctb3fHrVyCygGfbXZa/O0rQzEKZEIwnVTDtriQn0BYHpUYrM/sThLqtFkEet5pKXlGcLIVdhghB6sH5y1Mqf/LS/bVnZe6OHl+NxcVI1ypPiQfOy0lOwWiCbZcjjQ2qbOa767W/IkLA2+2ERdS6ShC5iTfTTgLBTMD91uNq3x6U8y9KBOp6E1PLYJbhPVRJlPg5MbLd72CEsKhRxPeXF6EXtR5UOa/uVQQl0p9lcF6yzDKxdpxYLfR3fua1sCDQw1kbWpSLoTPLjvX1zrEqJA7awEvAl656s5TXeKUz65MFnUnbzUzHb38FAICXrJycUeUrELJbGEMuXWdKsJ+MA1SjcGV9HhcHMBIwb7q7UYNk6ygtpq+rMXJfuDwJpwz9oSiKG5EN6h4y9IcPTp+ZOgFsEbdkZhy4vNMHeR1pnjpPTL6S64Q/mdU7+IRYUaii2hiDF944ZoLF1Ei7ygA7KD1n/UawaU5wk1w3aktvMH66Q1yy5Zm9d6ooILis5vpmbbyFwYTROQtnvfJoN+zl15I57pPM6T9vOMCv3ii5SZPyC05eoc7+3tXG3cua7ellMV9XDsCoQ2tMctbk97mt4sR7UOHQbOzuezbGKqPlp38qzFgkxLBf5urwmVowF/yoWnWaaRsd/uzDEe5H9dFQKcSItaR9oAr0LTS8r5vNS0UFjNeA774aIa8kbPnEGPP6V2Fo+H0krnBfZhp7juK/m0PSsskRpZFekaJoc7uBJmFAsxjoTOhYnTR+nR4vv36vxFUeCEvnvm2gsL72EjDJxrFOVu9TNKqaPpto4xCXgxtRbtTuyeuneak6wdymva5G77uMELR/rdcSGOts3EC6uhuYog7oVBiqlDx3i2FmjXZmvjKdzQIKZGNI/H3RfwDxZ+Eqff1Th/hAOOyPJnxgjhA6XFTQYYidU/ZjuUrbWN+rqaXkvphkSNc+4HpGTWIlBMb4+zt/oHZ0N6K7AHco9mqKBoct+BGfQ+PE8d0OTKKjcaURlb2ZOtJkLh21fX0dA/5WBiCYeOFD8PGMiaNwQLY0Q== X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: d999bae7-e061-4c62-c6b6-08da3fd270e7 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:17:09.7672 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: znrazilBfjJYgAwkY3rO3/hQTUHWvi2dn6/2sLX9NHq0giw6Ga3hK+mTl0zJLAc2VhsJ86b5AQNuw0FpLVnX6Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB5831 This is a re-usable helper (kind of a template) which gets introduced without users so that the individual subsequent patches introducing such users can get committed independently of one another. See the comment at the top of the new file. To demonstrate the effect, if a page table had just 16 entries, this would be the set of markers for a page table with fully contiguous mappings: index 0 1 2 3 4 5 6 7 8 9 A B C D E F marker 4 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 "Contiguous" here means not only present entries with successively increasing MFNs, each one suitably aligned for its slot, but also a respective number of all non-present entries. Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monné --- @Roger: I've retained your R-b, but I was on the edge of dropping it. --- v5: Bail early from step 1 if possible. Arrange for consumers who are just after CONTIG_{LEVEL_SHIFT,NR}. Extend comment. v3: Rename function and header. Introduce IS_CONTIG(). v2: New. --- /dev/null +++ b/xen/arch/x86/include/asm/pt-contig-markers.h @@ -0,0 +1,110 @@ +#ifndef __ASM_X86_PT_CONTIG_MARKERS_H +#define __ASM_X86_PT_CONTIG_MARKERS_H + +/* + * Short of having function templates in C, the function defined below is + * intended to be used by multiple parties interested in recording the + * degree of contiguity in mappings by a single page table. + * + * Scheme: Every entry records the order of contiguous successive entries, + * up to the maximum order covered by that entry (which is the number of + * clear low bits in its index, with entry 0 being the exception using + * the base-2 logarithm of the number of entries in a single page table). + * While a few entries need touching upon update, knowing whether the + * table is fully contiguous (and can hence be replaced by a higher level + * leaf entry) is then possible by simply looking at entry 0's marker. + * + * Prereqs: + * - CONTIG_MASK needs to be #define-d, to a value having at least 4 + * contiguous bits (ignored by hardware), before including this file (or + * else only CONTIG_LEVEL_SHIFT and CONTIG_NR will become available), + * - page tables to be passed to the helper need to be initialized with + * correct markers, + * - not-present entries need to be entirely clear except for the marker. + */ + +/* This is the same for all anticipated users, so doesn't need passing in. */ +#define CONTIG_LEVEL_SHIFT 9 +#define CONTIG_NR (1 << CONTIG_LEVEL_SHIFT) + +#ifdef CONTIG_MASK + +#include +#include +#include + +#define GET_MARKER(e) MASK_EXTR(e, CONTIG_MASK) +#define SET_MARKER(e, m) \ + ((void)((e) = ((e) & ~CONTIG_MASK) | MASK_INSR(m, CONTIG_MASK))) + +#define IS_CONTIG(kind, pt, i, idx, shift, b) \ + ((kind) == PTE_kind_leaf \ + ? (((pt)[i] ^ (pt)[idx]) & ~CONTIG_MASK) == (1ULL << ((b) + (shift))) \ + : !((pt)[i] & ~CONTIG_MASK)) + +enum PTE_kind { + PTE_kind_null, + PTE_kind_leaf, + PTE_kind_table, +}; + +static bool pt_update_contig_markers(uint64_t *pt, unsigned int idx, + unsigned int level, enum PTE_kind kind) +{ + unsigned int b, i = idx; + unsigned int shift = (level - 1) * CONTIG_LEVEL_SHIFT + PAGE_SHIFT; + + ASSERT(idx < CONTIG_NR); + ASSERT(!(pt[idx] & CONTIG_MASK)); + + /* Step 1: Reduce markers in lower numbered entries. */ + while ( i ) + { + b = find_first_set_bit(i); + i &= ~(1U << b); + if ( GET_MARKER(pt[i]) <= b ) + break; + SET_MARKER(pt[i], b); + } + + /* An intermediate table is never contiguous with anything. */ + if ( kind == PTE_kind_table ) + return false; + + /* + * Present entries need in-sync index and address to be a candidate + * for being contiguous: What we're after is whether ultimately the + * intermediate table can be replaced by a superpage. + */ + if ( kind != PTE_kind_null && + idx != ((pt[idx] >> shift) & (CONTIG_NR - 1)) ) + return false; + + /* Step 2: Check higher numbered entries for contiguity. */ + for ( b = 0; b < CONTIG_LEVEL_SHIFT && !(idx & (1U << b)); ++b ) + { + i = idx | (1U << b); + if ( !IS_CONTIG(kind, pt, i, idx, shift, b) || GET_MARKER(pt[i]) != b ) + break; + } + + /* Step 3: Update markers in this and lower numbered entries. */ + for ( ; SET_MARKER(pt[idx], b), b < CONTIG_LEVEL_SHIFT; ++b ) + { + i = idx ^ (1U << b); + if ( !IS_CONTIG(kind, pt, i, idx, shift, b) || GET_MARKER(pt[i]) != b ) + break; + idx &= ~(1U << b); + } + + return b == CONTIG_LEVEL_SHIFT; +} + +#undef IS_CONTIG +#undef SET_MARKER +#undef GET_MARKER +#undef CONTIG_MASK + +#endif /* CONTIG_MASK */ + +#endif /* __ASM_X86_PT_CONTIG_MARKERS_H */ From patchwork Fri May 27 11:17:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863301 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 F2A0EC433FE for ; Fri, 27 May 2022 11:17:54 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.337986.562717 (Exim 4.92) (envelope-from ) id 1nuXyR-0002li-Qm; Fri, 27 May 2022 11:17:43 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 337986.562717; Fri, 27 May 2022 11:17:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXyR-0002lb-Nq; Fri, 27 May 2022 11:17:43 +0000 Received: by outflank-mailman (input) for mailman id 337986; Fri, 27 May 2022 11:17:42 +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 1nuXyQ-0001xA-G4 for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:17:42 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9f73572c-ddae-11ec-bd2c-47488cf2e6aa; Fri, 27 May 2022 13:17:41 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05lp2111.outbound.protection.outlook.com [104.47.17.111]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-40-U5vlanUjPPKZrKiI8f-M4g-1; Fri, 27 May 2022 13:17:39 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM6PR04MB5831.eurprd04.prod.outlook.com (2603:10a6:20b:a8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Fri, 27 May 2022 11:17:37 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:17:37 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 9f73572c-ddae-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650261; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yOmd6nlV3KqMUXfqufTnFOwdi1OjszdzmaKj6+ae9bI=; b=co63amCtMSIcFcYCVdR0FBOQz7md9iwAmFLWYa7UPtoq+8oOFdZEHp9baaZXFqw1cwnvcl +7RJyJT7L3wyEMuJQrd8EJ2Mls5ddIDlTfeRiKWguclzOeoak1GJryAASXs5J5kXajYhmV FM1DVY2nxDpRKbWo54Yvidhxzwyh2LY= X-MC-Unique: U5vlanUjPPKZrKiI8f-M4g-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cycgSMFvQJJguyJomSb4NqgXvqeY+u/O30zML15NWW5Glux3Y8sfj6ReU5ZgdDAj1Q2flBeC/HmYfAokeM/hVwLA/ZbRU0pXa36UGXxwTSWGS18b/R8n+KbrJmGH4ND5XMB9d4kp3BXDOJ9bKmTN99a6op2w7K09XsPOQ43rggXOjS+4gbyhJWMif8Xuwu5CIkyKvgHoMQG+s2f2FlFZIA27TpLAeW/Wjh/gshWP3z3VaOVtUvYg44fixX+aK19UZwazgqjY3jEu94OdekKWWhCmJGFOSEpqkdq14YiN8oIfXzu5cpiXdyg9ExkaVm2ac1oEfmCSnMvhCR0z+SYq1A== 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=yOmd6nlV3KqMUXfqufTnFOwdi1OjszdzmaKj6+ae9bI=; b=Oj7xDNdwkx7zT/+mG8fJQS6gKGyWDmGPk1FFfMmv4qvGrH7ZQUPCseG+EyfjliPlcjdpMMJ1ALW7ka6776izvPYke3BXc5KbLNUeXaLNVHXQ0jK86fbJoKXEhmbpqFwa6kWmsNuUU4ZepSSBJhOY0fqC7L0r64ag1bxp/Y4UuPo0pjg1emqqepywphrxiOLxNibLA5NdEpY/jCxJfr8gN8Xt+0rGI8Pvv3qhKx1vCaauK1OqchVzdPXT5/ofQ6kksnPaXplOl0fbJrH3SY6VSr8weaTwskvLGohyN4iLIxapuin3GkRMzoSHJ4Uvv4zF+vBVQ1UV3UzxLQgpIOOEyg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <1df469a9-ddf2-2036-105a-f303f0554f06@suse.com> Date: Fri, 27 May 2022 13:17:35 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 08/15] IOMMU/x86: prefill newly allocate page tables Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AS9PR04CA0125.eurprd04.prod.outlook.com (2603:10a6:20b:531::16) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5c85d85b-2366-4447-e41b-08da3fd28184 X-MS-TrafficTypeDiagnostic: AM6PR04MB5831:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DCW/+L1wfhFMsE1TCd+QyiYZjHhlmKkANqwjQywJ6vex93MOk7PteQcygzBqzluK30h5p0illniaC2v4vbhoJH5AvwKaItTxBXHqdzSw1XTnVlcy7FxZire6oYEHofhCjWshkrfWhXyY0hx5cQ8E/CLTK7dDqyF+36sOVpwuyVho4yx2LTpgc4nUvk1RTc34eEYYpJSRIORAeeyZPhDN5Ek+wMyAY9vzdr/nyuiA49IumpGDGTiN+45r2YOujR3lboAWxZVwWJGXAboZM0vcCOgM1/yPN9GBeqSLt5o+NfJJjA88Y/SDdYuTpm0Wj4iF2mhhtbjOWrHlsUBpcv/KTgkM3xnCz6/Ix8RL93a2IZDSmUnJhtE1p5oH6d79mx4vdswAGKPhvxuQKiwuX1CATs5FdQ1s+eBn6UrpB3lZA9YuAona+4w5lkziGG2fZHeXckYZPTvlN9b3FnB++s3wjt9xP5Pux7BRMOcI2eEtyCWmJGpz5jgEGS9rMhokbiaigQQ+PN3pMQ/iyn1etDVkeh6fr1mO5KvXCHeFrGkiASCNOa6onuupXAVtRdGbTpp7Wy6f9nd8QZy41mZa+kYfRfa+Hyh/mjSZMY0KU3OcPdxYE0vSepPSte9m5dbc29CtyzzGdsp9D66nD/sEtFGWib9kPZA2n2y+3YPk47OMUekfvEttgPPZEOGE6LGsMNQRsMgP4guph4FjrrocUQQR1/8aBTy/vCNBQgYCiYcsqiGdDvdP+t4gGIG9dXOizKSZ X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(6512007)(2616005)(26005)(6506007)(316002)(66556008)(66946007)(54906003)(508600001)(8676002)(4326008)(66476007)(6486002)(86362001)(38100700002)(31696002)(186003)(83380400001)(5660300002)(31686004)(2906002)(8936002)(36756003)(6916009)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?lVDMe0cNhKuYihPI2LtzX0BTZ5VB?= =?utf-8?q?wPbF1f8Ri0B04caYXM76Z4Xg/HfbKL5eLvK/k4OtfpeZkU1yNaqcR96WflwVHEwr2?= =?utf-8?q?r4nUw5k4VGHKop1QplpYH7lQqV+LsNub/V4CVhgIM0WRwfWDlH1LytE6+7qYFcgxj?= =?utf-8?q?dee6Neqpo98rzAeneIzgc5P46YnuIBmImcjN3j3eVf3SzN3qHmk7Jzp5bRQxrBkrK?= =?utf-8?q?kzQ2OPXNUfq+zJ/cF3M9oH2RHAWJXmdkZaDieyDiX+cGBxLrEPxnmakmvOknahmLD?= =?utf-8?q?PAXXPlzDzkPw31Me/Cvm5VxfUPjyVaQUUdGA7D1xkGVWURJP6ZyeRZIEYG5VsCSik?= =?utf-8?q?YkNkUf1XEq6yyG7AQxPrOiptXVUt9hKyZhZGRfmZd42opidY0tPt+dPUY9JyNVpLq?= =?utf-8?q?a53oXbJxfQCqnGkqUdAIRyzXAMwjzjudmptnAYp3mj4r2AxCGXvc1RrJ5RqKEqfNg?= =?utf-8?q?mTIfk1gLMbpfXMyQm0kxyr3K3imOnLMg3vOuQXOPGx9b4Xjt7dSmAzBdWS75L/uOl?= =?utf-8?q?e1VCXrp/bq3q9eHzEG3f5eYmzmftGaV1STaVSNsJi0D5wF4jG/qYXsGuMlEv20c8O?= =?utf-8?q?CHLdbQvLAFIGHr4XpEycFakS33yc1UI4Hh8CnoeklBszkLyS1wleF+7llV3LuWTLO?= =?utf-8?q?VhGT5leNucBK5kO38bWjQ0jCSeUrXB3ncD4W5XQuWnP/tdRuKMU//Rlvz6dEZ/+T8?= =?utf-8?q?zqKaMhFWpsric0Pfa+7p2DPwmlwBKwRIq2vIYJfY3mRreD6IN7XzekIdt2O9GlAXR?= =?utf-8?q?WOTEd5RVPSn2N5yvDPJPtsUcm57KXCoPFSdx31iH/jhbfnC9hu+CDykMvOT+aRNUm?= =?utf-8?q?dCjsSo9Gp+8GtPkLFXXdDz/BG0+3POec3MaLYhdMt9n6xQMz1mGClFNkDdVNK3p2N?= =?utf-8?q?DLm/eOwb/N6s7XZF28+xs6iwc2ITOB8LQz1WnQmTyYY88FTAi+rPYVDS6A5YvJ8Kf?= =?utf-8?q?3yb9c/uLDRDk6un/Fzm+X4B/Z18wON7fCwOqoS/eqoYVGypSnmBs2bilzWQVRQjip?= =?utf-8?q?d83+96vqRXaTOXdEfEDIJrYbXfpkueE8fZEoB5eI2QsUdycTX+fTARmdqwDgRbbhm?= =?utf-8?q?/JHQh5he29aLirtUIb5xZB3OerAlP5IAF47K64i4GES0LzwZk3nZldmQpP/JUUwPq?= =?utf-8?q?d03YgspOBl2AeQOLdZNwCNMQwsaYTqN0D1kubh0Rd8U4swheYwGSVSrGJjxCt2AlS?= =?utf-8?q?Q6TSXmc/hSAOcRv491Gae6/3oDUGE3hvVX3QYpI5Z/RDoxyGKRCnH0Ekts9/GWgRz?= =?utf-8?q?LVJNC1+HXcZytJowkRZJ+1F845bTKJ80f4cALXRz15Ww5Ju9PEGKSBmN4D9ut3X7W?= =?utf-8?q?10P5+6ItehAlKV8FaxJtThE+A8nIZiwccTa/R/DsVx1sVEtrlGYzv8vHB3Wo8mHoR?= =?utf-8?q?Ps3EML0BhsFeuitYvUpxs9wOW7X/9C4o7bUDARaMRIkBX8r6wQGIH2Ox7RMw6zQjb?= =?utf-8?q?Mkibkj3QfK2wUahm0js1X46wmv5obEoxorS526X7zgcKtb7++dDKoVT6ZkVo1Kebc?= =?utf-8?q?iCz8BJdxdspEdhGgh8e+51xeJYDpphfKEUxxm9aMxc7dmGlZnr/UOitIgJVQ1Pxr2?= =?utf-8?q?hvZspY/iRkOLj9eWkXObV8mz4jE3RbuqqNTfJ3v0opEpBfUOnevLuOLFAJTYYodvw?= =?utf-8?q?Tryto5+p+q2lu3PmrsqDnW8bOvKn4tAA=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5c85d85b-2366-4447-e41b-08da3fd28184 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:17:37.6248 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Xen5K9JNHfQeTxp7SGpY3wvV6jDteF7P5UormBq3gGRLo/EfYSc2d+KrrXbeCIxYDk2DbC9Yoq4qx+BgarlPbA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB5831 Page tables are used for two purposes after allocation: They either start out all empty, or they are filled to replace a superpage. Subsequently, to replace all empty or fully contiguous page tables, contiguous sub-regions will be recorded within individual page tables. Install the initial set of markers immediately after allocation. Make sure to retain these markers when further populating a page table in preparation for it to replace a superpage. The markers are simply 4-bit fields holding the order value of contiguous entries. To demonstrate this, if a page table had just 16 entries, this would be the initial (fully contiguous) set of markers: index 0 1 2 3 4 5 6 7 8 9 A B C D E F marker 4 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 "Contiguous" here means not only present entries with successively increasing MFNs, each one suitably aligned for its slot, and identical attributes, but also a respective number of all non-present (zero except for the markers) entries. Signed-off-by: Jan Beulich Reviewed-by: Kevin Tian Reviewed-by: Roger Pau Monné --- An alternative to the ASSERT()s added to set_iommu_ptes_present() would be to make the function less general-purpose; it's used in a single place only after all (i.e. it might as well be folded into its only caller). While in VT-d's comment ahead of struct dma_pte I'm adjusting the description of the high bits, I'd like to note that the description of some of the lower bits isn't correct either. Yet I don't think adjusting that belongs here. --- v5: Assert next_mfn is suitably aligned in set_iommu_ptes_present(). Use CONTIG_LEVEL_SHIFT in favor of PAGE_SHIFT-3. v4: Add another comment referring to pt-contig-markers.h. Re-base. v3: Add comments. Re-base. v2: New. --- a/xen/arch/x86/include/asm/iommu.h +++ b/xen/arch/x86/include/asm/iommu.h @@ -146,7 +146,8 @@ void iommu_free_domid(domid_t domid, uns int __must_check iommu_free_pgtables(struct domain *d); struct domain_iommu; -struct page_info *__must_check iommu_alloc_pgtable(struct domain_iommu *hd); +struct page_info *__must_check iommu_alloc_pgtable(struct domain_iommu *hd, + uint64_t contig_mask); void iommu_queue_free_pgtable(struct domain_iommu *hd, struct page_info *pg); #endif /* !__ARCH_X86_IOMMU_H__ */ --- a/xen/drivers/passthrough/amd/iommu-defs.h +++ b/xen/drivers/passthrough/amd/iommu-defs.h @@ -446,11 +446,13 @@ union amd_iommu_x2apic_control { #define IOMMU_PAGE_TABLE_U32_PER_ENTRY (IOMMU_PAGE_TABLE_ENTRY_SIZE / 4) #define IOMMU_PAGE_TABLE_ALIGNMENT 4096 +#define IOMMU_PTE_CONTIG_MASK 0x1e /* The ign0 field below. */ + union amd_iommu_pte { uint64_t raw; struct { bool pr:1; - unsigned int ign0:4; + unsigned int ign0:4; /* Covered by IOMMU_PTE_CONTIG_MASK. */ bool a:1; bool d:1; unsigned int ign1:2; --- a/xen/drivers/passthrough/amd/iommu_map.c +++ b/xen/drivers/passthrough/amd/iommu_map.c @@ -21,6 +21,8 @@ #include "iommu.h" +#include + /* Given pfn and page table level, return pde index */ static unsigned int pfn_to_pde_idx(unsigned long pfn, unsigned int level) { @@ -113,9 +115,23 @@ static void set_iommu_ptes_present(unsig return; } + ASSERT(!(next_mfn & (page_sz - 1))); + while ( nr_ptes-- ) { - set_iommu_pde_present(pde, next_mfn, 0, iw, ir); + ASSERT(!pde->next_level); + ASSERT(!pde->u); + + if ( pde > table ) + ASSERT(pde->ign0 == find_first_set_bit(pde - table)); + else + ASSERT(pde->ign0 == CONTIG_LEVEL_SHIFT); + + pde->iw = iw; + pde->ir = ir; + pde->fc = true; /* See set_iommu_pde_present(). */ + pde->mfn = next_mfn; + pde->pr = true; ++pde; next_mfn += page_sz; @@ -295,7 +311,7 @@ static int iommu_pde_from_dfn(struct dom mfn = next_table_mfn; /* allocate lower level page table */ - table = iommu_alloc_pgtable(hd); + table = iommu_alloc_pgtable(hd, IOMMU_PTE_CONTIG_MASK); if ( table == NULL ) { AMD_IOMMU_ERROR("cannot allocate I/O page table\n"); @@ -325,7 +341,7 @@ static int iommu_pde_from_dfn(struct dom if ( next_table_mfn == 0 ) { - table = iommu_alloc_pgtable(hd); + table = iommu_alloc_pgtable(hd, IOMMU_PTE_CONTIG_MASK); if ( table == NULL ) { AMD_IOMMU_ERROR("cannot allocate I/O page table\n"); @@ -726,7 +742,7 @@ static int fill_qpt(union amd_iommu_pte * page table pages, and the resulting allocations are always * zeroed. */ - pgs[level] = iommu_alloc_pgtable(hd); + pgs[level] = iommu_alloc_pgtable(hd, 0); if ( !pgs[level] ) { rc = -ENOMEM; @@ -784,7 +800,7 @@ int cf_check amd_iommu_quarantine_init(s return 0; } - pdev->arch.amd.root_table = iommu_alloc_pgtable(hd); + pdev->arch.amd.root_table = iommu_alloc_pgtable(hd, 0); if ( !pdev->arch.amd.root_table ) return -ENOMEM; --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -342,7 +342,7 @@ int amd_iommu_alloc_root(struct domain * if ( unlikely(!hd->arch.amd.root_table) && d != dom_io ) { - hd->arch.amd.root_table = iommu_alloc_pgtable(hd); + hd->arch.amd.root_table = iommu_alloc_pgtable(hd, 0); if ( !hd->arch.amd.root_table ) return -ENOMEM; } --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -334,7 +334,7 @@ static uint64_t addr_to_dma_page_maddr(s goto out; pte_maddr = level; - if ( !(pg = iommu_alloc_pgtable(hd)) ) + if ( !(pg = iommu_alloc_pgtable(hd, 0)) ) goto out; hd->arch.vtd.pgd_maddr = page_to_maddr(pg); @@ -376,7 +376,7 @@ static uint64_t addr_to_dma_page_maddr(s } pte_maddr = level - 1; - pg = iommu_alloc_pgtable(hd); + pg = iommu_alloc_pgtable(hd, DMA_PTE_CONTIG_MASK); if ( !pg ) break; @@ -388,12 +388,13 @@ static uint64_t addr_to_dma_page_maddr(s struct dma_pte *split = map_vtd_domain_page(pte_maddr); unsigned long inc = 1UL << level_to_offset_bits(level - 1); - split[0].val = pte->val; + split[0].val |= pte->val & ~DMA_PTE_CONTIG_MASK; if ( inc == PAGE_SIZE ) split[0].val &= ~DMA_PTE_SP; for ( offset = 1; offset < PTE_NUM; ++offset ) - split[offset].val = split[offset - 1].val + inc; + split[offset].val |= + (split[offset - 1].val & ~DMA_PTE_CONTIG_MASK) + inc; iommu_sync_cache(split, PAGE_SIZE); unmap_vtd_domain_page(split); @@ -2176,7 +2177,7 @@ static int __must_check cf_check intel_i if ( iommu_snoop ) dma_set_pte_snp(new); - if ( old.val == new.val ) + if ( !((old.val ^ new.val) & ~DMA_PTE_CONTIG_MASK) ) { spin_unlock(&hd->arch.mapping_lock); unmap_vtd_domain_page(page); @@ -3064,7 +3065,7 @@ static int fill_qpt(struct dma_pte *this * page table pages, and the resulting allocations are always * zeroed. */ - pgs[level] = iommu_alloc_pgtable(hd); + pgs[level] = iommu_alloc_pgtable(hd, 0); if ( !pgs[level] ) { rc = -ENOMEM; @@ -3121,7 +3122,7 @@ static int cf_check intel_iommu_quaranti if ( !drhd ) return -ENODEV; - pg = iommu_alloc_pgtable(hd); + pg = iommu_alloc_pgtable(hd, 0); if ( !pg ) return -ENOMEM; --- a/xen/drivers/passthrough/vtd/iommu.h +++ b/xen/drivers/passthrough/vtd/iommu.h @@ -253,7 +253,10 @@ struct context_entry { * 2-6: reserved * 7: super page * 8-11: available - * 12-63: Host physcial address + * 12-51: Host physcial address + * 52-61: available (52-55 used for DMA_PTE_CONTIG_MASK) + * 62: reserved + * 63: available */ struct dma_pte { u64 val; @@ -263,6 +266,7 @@ struct dma_pte { #define DMA_PTE_PROT (DMA_PTE_READ | DMA_PTE_WRITE) #define DMA_PTE_SP (1 << 7) #define DMA_PTE_SNP (1 << 11) +#define DMA_PTE_CONTIG_MASK (0xfull << PADDR_BITS) #define dma_clear_pte(p) do {(p).val = 0;} while(0) #define dma_set_pte_readable(p) do {(p).val |= DMA_PTE_READ;} while(0) #define dma_set_pte_writable(p) do {(p).val |= DMA_PTE_WRITE;} while(0) @@ -276,7 +280,7 @@ struct dma_pte { #define dma_pte_write(p) (dma_pte_prot(p) & DMA_PTE_WRITE) #define dma_pte_addr(p) ((p).val & PADDR_MASK & PAGE_MASK_4K) #define dma_set_pte_addr(p, addr) do {\ - (p).val |= ((addr) & PAGE_MASK_4K); } while (0) + (p).val |= ((addr) & PADDR_MASK & PAGE_MASK_4K); } while (0) #define dma_pte_present(p) (((p).val & DMA_PTE_PROT) != 0) #define dma_pte_superpage(p) (((p).val & DMA_PTE_SP) != 0) --- a/xen/drivers/passthrough/x86/iommu.c +++ b/xen/drivers/passthrough/x86/iommu.c @@ -26,6 +26,7 @@ #include #include #include +#include #include const struct iommu_init_ops *__initdata iommu_init_ops; @@ -538,11 +539,12 @@ int iommu_free_pgtables(struct domain *d return 0; } -struct page_info *iommu_alloc_pgtable(struct domain_iommu *hd) +struct page_info *iommu_alloc_pgtable(struct domain_iommu *hd, + uint64_t contig_mask) { unsigned int memflags = 0; struct page_info *pg; - void *p; + uint64_t *p; #ifdef CONFIG_NUMA if ( hd->node != NUMA_NO_NODE ) @@ -554,7 +556,29 @@ struct page_info *iommu_alloc_pgtable(st return NULL; p = __map_domain_page(pg); - clear_page(p); + + if ( contig_mask ) + { + /* See pt-contig-markers.h for a description of the marker scheme. */ + unsigned int i, shift = find_first_set_bit(contig_mask); + + ASSERT((CONTIG_LEVEL_SHIFT & (contig_mask >> shift)) == CONTIG_LEVEL_SHIFT); + + p[0] = (CONTIG_LEVEL_SHIFT + 0ull) << shift; + p[1] = 0; + p[2] = 1ull << shift; + p[3] = 0; + + for ( i = 4; i < PAGE_SIZE / 8; i += 4 ) + { + p[i + 0] = (find_first_set_bit(i) + 0ull) << shift; + p[i + 1] = 0; + p[i + 2] = 1ull << shift; + p[i + 3] = 0; + } + } + else + clear_page(p); iommu_sync_cache(p, PAGE_SIZE); From patchwork Fri May 27 11:18:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863302 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 C01F0C433EF for ; Fri, 27 May 2022 11:19:01 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.337992.562727 (Exim 4.92) (envelope-from ) id 1nuXzW-0003QQ-3v; Fri, 27 May 2022 11:18:50 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 337992.562727; Fri, 27 May 2022 11:18:50 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXzW-0003QJ-1B; Fri, 27 May 2022 11:18:50 +0000 Received: by outflank-mailman (input) for mailman id 337992; Fri, 27 May 2022 11:18:49 +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 1nuXzV-0003Q5-00 for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:18:49 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.109.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id c72a2f96-ddae-11ec-bd2c-47488cf2e6aa; Fri, 27 May 2022 13:18:47 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05lp2105.outbound.protection.outlook.com [104.47.17.105]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-45-XwqzWwDnOiq6zlJXvt8Egg-1; Fri, 27 May 2022 13:18:46 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM6PR04MB5831.eurprd04.prod.outlook.com (2603:10a6:20b:a8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Fri, 27 May 2022 11:18:44 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:18:44 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: c72a2f96-ddae-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650327; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=56Ikp8kYb60S5Ww1u1UKlg6+iuxYU2FuDji0p2qDOgI=; b=eHfCWYLvp5PdKGmrPpOYIaGsIF2WXotqVYHeP2y7w6+L/gedNqkNfDA9zPEi/DbGPveq2w x7csRtHh5MrCaVtmkki57px/g/frNt5RcQS2JQ1NJIMUgfkKqr0Z4vMdnpfxTvBX5o5HMq 7429nOqpxgfS4gpfYqPDf8sJDn9UKUI= X-MC-Unique: XwqzWwDnOiq6zlJXvt8Egg-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hQ1jIvc5l9Tv/CeQtecHdQ8E7lGXuclAN7bJWF+PruSxY3que6ueVvGnYZ+aVgpXhxmn99wB8VppE56/vyGZQn6FaVO4l26y+RkCPr7lOCM16Yu6bj+KLyohvJ+QM0f6fY/jHAz/vFtEE7wtnvvnnQq+4Jn2sEEjdYKv2cRzCZBugYGwm6aRjQ12NoKgPdxIycfkenVTsSy5EtpBm8KA5s1nrj9NmdCXtJYuhwcVZujUXKBeLg89NFq2+chuPUn3teIQehfcmY538qEHloMXSVYt+7sSm8NwRWSTr4MnxSZt5EUdsr05HQLO8dxXUpafVVH5qR5pir8LRCEZNhSkSQ== 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=i1SRbMZ5vtUn4A75NysmPVCeWEjl1KBkL8Pu3NNkQeY=; b=M7fWYVj5h67HGeunumVuSpEDuQJLlblXqTV5AF6qd0iJdl3Gd5pRr6tVu23xUnj2y1X5lh7t+gOnLRbXieLyIkEdK5ZyB0cEPVWpejcrRh02Boi8CGzZHPXXmoIWhEMmOzpec2xDnagIXhK4WcG2t5hMhiCYfKyX2sJTY+ADGg4JNyfpnoeAQd80DhW3ooJVtF1FgYZlSz6Ao99p7NvnWjCym2uPgpk5A2of/WPpSR5bgk2aW6/BtS3g9y7PPHPNdc+EsOaACbqwAwcNTCqUGYGDFqEmae5BlpleODJWoEN3VA9Qkze3f4RUJ0S2vjJpavOvY8PfdmueU8Dk/CaAOQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: Date: Fri, 27 May 2022 13:18:43 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 09/15] AMD/IOMMU: free all-empty page tables Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AM6P195CA0048.EURP195.PROD.OUTLOOK.COM (2603:10a6:209:87::25) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 636a0464-50b8-4d97-14f7-08da3fd2a989 X-MS-TrafficTypeDiagnostic: AM6PR04MB5831:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9PkAyeijGi6cEcXwQ9DvWJ3Ri26IejewGqboGyjNIMd9jHTKkkoL3qGjleOdrbD0j4uNzzkrCXLDYnaMfhCi1rZqFXbbRL1P9DzewWxno8d7WbYaZmCN8YaieAo3oOJVy/zaCLH9hfSNrQiEUSkpMZ+J92cibPuUC/LJqIKjfrwf0ueydF/MjEnZMrX0EL1bxTsHuhFeZXdnK33bXSBkleYqGHy3EJ6Y4N8pYb22iZAuC2HgGkfe6yMX62pSZyU3Sd1E1fDeqCbvH5b6htS9/Vc1EhOnFDzmImGYIlsJwgG9gComX4FK0XIz2zDlpoA3Y+gmsCHFM1FkhrkSBkQvdmqrsIskyqelKIUFbRJ7hE81py3bRcvafkqqo8vzXAT236T0Ue4LadE91lSlHpHeOQcD0Nak/8ebK5UcfyrCHxVLXtzWmis6FBgoQvCPvpU6Mw3mo6i9nwFzJCutXgIhV+qJuaQQRwBrngdhDxwq7zGZ3+nRVV40B02iTdYk4U6nv184colaVoBBvThEcwQ5nFY7RHZXCshLkLzPfolUOFMYkW2fzsttQV4+M1ECWWdnW/aREB1v88i4a2ZJeeMKLsdlgLGRn+62qw448H3BZYda0a746RHRg8+uc8PUhDS2kn3aIES37uditkzr+bUbsLh+GnIXULa+cROnwgSomxaxNj0G+hqZIZbdRlZBNfEm4aKj60FaR95UAmjsj41I0/Poj206g/lIM5q24+QMjVE= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(6512007)(2616005)(26005)(6506007)(316002)(66556008)(66946007)(54906003)(508600001)(8676002)(4326008)(66476007)(6486002)(86362001)(38100700002)(31696002)(186003)(83380400001)(5660300002)(31686004)(2906002)(8936002)(36756003)(6916009)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pmCCWHymAv7iWq2CxkCpIZyhnSEjQ9RZ7lU0y06Cn8UX96+KFUjEB1GRmc7DG7tJUM/Pzos0bc9CvDUT0kzS1JH3KDVONWFIphkaqv4veiguiYV3LAx9rC8roVXfGoA1aAL0g7AgfLAWY6kxUMt5ULOK+pGB2OKkf6jUZw3uC5p9P+4v3LuEq72q4cZzrZ9X4XocRY+JgReDqucEySIRqLKNwCv+2yoZdRhzj996GtRao8M4IJ6weWbiXka803NGk+fTyrolId0i5I16o3k++LV3awxCiuaItaRcZodtMVBsXn8NX1HzNQXxgQhO3IrZVxftYK6ZcWqoBPCTimAcnT7edClzSz0GGFbUIj7Yg50knkoWQdVjU7pLJT2+FI5bbZ6mREAk9OQ1H7/c3Vr8mWck3ulLu+3yc6QQFO7EcwN0rbliNoYe+fGAuHx/XL5QHDeM18SZdHp9NAFlWqFzerU0ShJOPrxmQ9KJ0f0ynJqo//f+H7MZ7PYTQCFrKPcCchCvuf7rPJVzSWxQ5Tx67NMAZVipRyBnQqtc1n6H/1JlAhabXjE0Ke7wgug8GV4o7OcH6BCRpCpfwvlm6SREhVrW6cuy3RKGjQVnNMJA2SnBC19c8VSEmtB6dap/O3mSVSQtyvP1Eb0Dg/qQ+Y4UIwDt8a6EXecQtDKPIw2T5vg1OCfDUnk/M9SSlL7jjvKcUG7c/z5eDlxLOB0+6MS6sSA8jLT0NUKEqnqRhKSJv+6YRCToqOo62Bl9I73E3eavb1cTQR0Oj+NvXsVU6sQrn/oVDVHX06Q+Wpf5LKj0Dwe9iHCsIzRU2QELhVY8BbmFdf+2zWg9mZqgZph71kuGj2gz544G9z8IvnFhHrfRoU9Z7HrBTaojKdpgU3kCtpteUXaKr0SmVUVJRh4bsuS8HOAtZFj568SxMI57btlmGkCEX9uukfjN0hLaVQHRSt4JaMdFaeFj0ggNfDVt1z1Zn20AgMPSOuInVulcG0z4PfW2u/waBjz5148jCyDbMPYuUHIhy+y2Td3kuGlPF6xYHlBMGEBqcVN9EYALtrvQ6vL4oaU95VLAttVKZdA3PBqWiUxLudDDOIx9EM9ZdeIK481AMTc7bGTQjHHkorSDwB0iBQ7+w0n3ClPrM4qLn6yi17SwoUUNosoSWxV2/CNDhU0pTnl2Qb2EdGf+8WwOVBanVXWj8Ho0CVKMYxBcFEZzYeqScyn2tDJhM2tSkNgpK+Lll8+zNOprZtk9OojwTVpUaQWCkg5MMLnnm9nFQJqe2NjKetOc/LdrTEfhFjePcnF1nrAMCOrhdGKJnoaEIJ96ZD4nAcs/4TctJgMRbZ7mTVXtV7R7rgnefucBt5uGosQDN/RJkEEJGEfzuOT+54Lo8m0tGIwh+JRGuCXa13z+WhykK6y64RDUTYYbzD4rsOu2tvraDqq/cUV9z0yDjrg7Yi31roZkBbLhdTWJ1cTYTuPf6na4eNDddkimknZfnPNOuvViHiy+lMXoo3NpprgS5Roxi4J8idCLhg0b9fj6JMNdpfR4VjEJQALo+zKa610c6KYSmn28YzN+VSh7iyrbkMW6OUF1cvCyBdy4EPHVsqEmjlpsLtqW0+hAzvNWWVbfdOjpd3PaOujFNkA2Uwac6e8i3tl1b8wjr5BiUYp6c5Fm+1Ypb4O3G++yjbXhmbwJNLvFiwaSe/swKpxYiXAtfFpFstdUnqVbWvwpF5bO0dYqaTekpD99LCvNrmM2iQ== X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 636a0464-50b8-4d97-14f7-08da3fd2a989 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:18:44.8236 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 7/E1biq/NjL2zz/1494dgbf/cWWAgfn6rr6S5txm2FvQm2XDIrUJYPFJFe+G36K/i5C+GYtZ5iCpSp7AXaWTfw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB5831 When a page table ends up with no present entries left, it can be replaced by a non-present entry at the next higher level. The page table itself can then be scheduled for freeing. Note that while its output isn't used there yet, pt_update_contig_markers() right away needs to be called in all places where entries get updated, not just the one where entries get cleared. Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monné --- v5: Re-base over changes earlier in the series. v4: Re-base over changes earlier in the series. v3: Re-base over changes earlier in the series. v2: New. --- a/xen/drivers/passthrough/amd/iommu_map.c +++ b/xen/drivers/passthrough/amd/iommu_map.c @@ -21,6 +21,7 @@ #include "iommu.h" +#define CONTIG_MASK IOMMU_PTE_CONTIG_MASK #include /* Given pfn and page table level, return pde index */ @@ -35,16 +36,20 @@ static unsigned int pfn_to_pde_idx(unsig static union amd_iommu_pte clear_iommu_pte_present(unsigned long l1_mfn, unsigned long dfn, - unsigned int level) + unsigned int level, + bool *free) { union amd_iommu_pte *table, *pte, old; + unsigned int idx = pfn_to_pde_idx(dfn, level); table = map_domain_page(_mfn(l1_mfn)); - pte = &table[pfn_to_pde_idx(dfn, level)]; + pte = &table[idx]; old = *pte; write_atomic(&pte->raw, 0); + *free = pt_update_contig_markers(&table->raw, idx, level, PTE_kind_null); + unmap_domain_page(table); return old; @@ -87,7 +92,11 @@ static union amd_iommu_pte set_iommu_pte if ( !old.pr || old.next_level || old.mfn != next_mfn || old.iw != iw || old.ir != ir ) + { set_iommu_pde_present(pde, next_mfn, 0, iw, ir); + pt_update_contig_markers(&table->raw, pfn_to_pde_idx(dfn, level), + level, PTE_kind_leaf); + } else old.pr = false; /* signal "no change" to the caller */ @@ -326,6 +335,9 @@ static int iommu_pde_from_dfn(struct dom smp_wmb(); set_iommu_pde_present(pde, next_table_mfn, next_level, true, true); + pt_update_contig_markers(&next_table_vaddr->raw, + pfn_to_pde_idx(dfn, level), + level, PTE_kind_table); *flush_flags |= IOMMU_FLUSHF_modified; } @@ -351,6 +363,9 @@ static int iommu_pde_from_dfn(struct dom next_table_mfn = mfn_x(page_to_mfn(table)); set_iommu_pde_present(pde, next_table_mfn, next_level, true, true); + pt_update_contig_markers(&next_table_vaddr->raw, + pfn_to_pde_idx(dfn, level), + level, PTE_kind_table); } else /* should never reach here */ { @@ -487,8 +502,24 @@ int cf_check amd_iommu_unmap_page( if ( pt_mfn ) { + bool free; + /* Mark PTE as 'page not present'. */ - old = clear_iommu_pte_present(pt_mfn, dfn_x(dfn), level); + old = clear_iommu_pte_present(pt_mfn, dfn_x(dfn), level, &free); + + while ( unlikely(free) && ++level < hd->arch.amd.paging_mode ) + { + struct page_info *pg = mfn_to_page(_mfn(pt_mfn)); + + if ( iommu_pde_from_dfn(d, dfn_x(dfn), level, &pt_mfn, + flush_flags, false) ) + BUG(); + BUG_ON(!pt_mfn); + + clear_iommu_pte_present(pt_mfn, dfn_x(dfn), level, &free); + *flush_flags |= IOMMU_FLUSHF_all; + iommu_queue_free_pgtable(hd, pg); + } } spin_unlock(&hd->arch.mapping_lock); From patchwork Fri May 27 11:19:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863303 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 9855BC433F5 for ; Fri, 27 May 2022 11:19:21 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.337996.562739 (Exim 4.92) (envelope-from ) id 1nuXzr-0003y1-Fy; Fri, 27 May 2022 11:19:11 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 337996.562739; Fri, 27 May 2022 11:19:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXzr-0003xu-CY; Fri, 27 May 2022 11:19:11 +0000 Received: by outflank-mailman (input) for mailman id 337996; Fri, 27 May 2022 11:19:09 +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 1nuXzp-0003Q5-LU for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:19:09 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id d39886e6-ddae-11ec-bd2c-47488cf2e6aa; Fri, 27 May 2022 13:19:08 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05lp2113.outbound.protection.outlook.com [104.47.17.113]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-32-VrqiV7sxN8aZYJhd7ghbZw-1; Fri, 27 May 2022 13:19:07 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM6PR04MB5831.eurprd04.prod.outlook.com (2603:10a6:20b:a8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Fri, 27 May 2022 11:19:06 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:19:06 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: d39886e6-ddae-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650348; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VvZXvUCO2HfiLDgA+1pEGxziHFCsDEOEbqjxz7fQFbQ=; b=dSXAWTASJpjEMXgq9T2h3PHFHKDrHbxZGTiC4hKEmyHvNGeDCOQOFIdPLYD5xdo1ujSx0z gOKdUfty0v0xm+cL8LKfWOwp9u/rtX3dtg1Acry5+YOYd+URky05zyq52TImYlfsITyTid CG+mVRFHVSI9efGlB7TjoeUKXSEQsHU= X-MC-Unique: VrqiV7sxN8aZYJhd7ghbZw-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YTiudUoFpZDE0DFCI96KtvMkY11r5WmBm4yroTSZEDNlg+vO+WqCbQE3UNjsZFVFS9X1tHJv3r9FL9+vQR2xgyu4qY701d+KIyaD+YTOZkZpFJpmoSpi5SJHn1QoyznSvSTT4Oq23iImmv9NbeCYq20SbMfyV9G073iWpUsK4oMl1xUozaggp0AeMQShsIqtMUBISHPtmYbBeUBQa+T2zsmVkOnMx9NdmX0SwtOkYTYwDz7XLVXLqnzlxFsw/HOpi+gHUBaxyFF5Oye0EBnzVDuu5/fNsKi6mSC6AlB5KLiQEGUaSWjVOK1pED2HLxa43G/refx6nR6ABn4QnMtl3g== 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=gp73oXP7vi27KdOxrBUVp9LMt+daZO3hrpdzDy50sao=; b=ZEPXErJ2tJB753rl9dkuc+dSj6q3BB3e4aTcjMXTJbuPb/5UC2mHLL/1nUaUqf0m42XVIPElYqyuZoJj9449Fjcsb9T69fqkmb3Gl6RYyL+SSaARRPKnqma9F2mXnP9VB8qSj9bre0CUvc8+tmqlG0rS7VBNVtx3Cv+PuSe6IpkrGGOFEhkCQwM6aPgpxkMAME/ATApVMjgS6rV7IEwJnBk+8yA2N5IpVqKOoYWNYQP1ngWDKYkK1ez3b2Ff6y4g3bvLqLNZgn6ZzGz++VsePbzr/8Ukgv9t8NmuSb0TBs0kQnPWx3MxmqnOHgqgPT0sSWZKQjoskO+1KAzF1lBJNQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: Date: Fri, 27 May 2022 13:19:04 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 10/15] VT-d: free all-empty page tables Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AM6P195CA0054.EURP195.PROD.OUTLOOK.COM (2603:10a6:209:87::31) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 214a9375-7521-48c5-e73c-08da3fd2b63c X-MS-TrafficTypeDiagnostic: AM6PR04MB5831:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: F8h3UrV/HMqPmEJJSV6VQTVY0PiN3EUfjhstZKsSjA7n/RbwvPD418RcrOEw+UqN+tT4NkFXdUS1JI2fzwOwh/8FYHXqoZ1C1hjqZLyNco9+azXzHIVNEq/JOOK2fxBZwvxixLQkYkPzFvV0MD/5WTEEUxKqqPcAWL6lhtYMqYvxxdhhDjIuMN7z+sbrcY7ndITjBhkbc0ilJGHrqMg5zZFWoJqsCEHxX7tiZ+oXNjGBl5PNe/fv1TezxN4/EmqRsY4VeAKHIdBSAt6D1/0S9oCkcG6vDPOO7PufVMM6bfou3hP2nBM5L980SJFkWn9IDdD5v/Cq+0r5XViQ1lkHP0Xkigz7jtlbXc3dpM9lgFjEcwrOub3sd639t2WohOg1SHaIP52DNAsoERDjK3p+Y37P5rlein8vHVo+F278JYAFdJ4U6dVee0AOAAc6WKtJhRRAzHFcXlEKdPKceFAa2w+3z8SevWOPShtYqM8g1yYMlXdhNQorZwyVPTz+x8aqFg971o7ZwJ4hWLYGJ+bss0+ewDgJoTZRfVfMjefPR16LPKPe4YrQwj1i4Hna9Isf7S32DbvKLhVdkLYpQGdXto6VQZjyWi25MRw/k+lg0dk7n8q9N1qkTmGpyEQs7UTrMy1qKXQ7Ygn/pfnDQvxCIGimkMu9R4ZAnsbjc340ZYybf3Q8LsMM1YCRZeoewtqyGWpnM3QzN9xV2dQRuhqJBtIJKY/pUvhjhIqJ5Qc6W64= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(6512007)(2616005)(26005)(6506007)(316002)(66556008)(66946007)(54906003)(508600001)(8676002)(4326008)(66476007)(6486002)(86362001)(38100700002)(31696002)(186003)(83380400001)(5660300002)(31686004)(2906002)(8936002)(36756003)(6916009)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: IngeTXIAHkSvlcCwf4TFlFFobT9cos0+tPO+itz50RXk0kPRQ3u0ubTGR7g0cOk/OX2WGNC0gIGPEa8i+Pp7B6xAWJAeAIVPHLD5WifL4HhyUbK1bHKBw5CjJr3D5K+g83koXjr0MJsKPsBaT/37uKeNK9LSfwtY8mVJmiRBqVwqWhzCEU/OXqVsiGEgIe/2CrMkzik2gDM8tGq8SK8IoDv8KGi/+dz45jzZcksHqtpc/pKvH4uTHEuFi15vOAnzpfwDdOc0yntww8Okd+OXaZPW5SVQo9JAJ9/Juya7VVCe6OPDe8fmJEEfQViavwdJuapVcAjNIQyLx/dXXW76MAYiGDv6TRKXk4BRbc/Gebyl4/39jeEjXyw0uttJqv03k7rqoAh9f4uzgV0PcEhwD63a0oT62PL0DUwQLr/EVJqstpNHTB/eWuHwSGR3VHviXaUiUWb1FohKxetpbrLloh0u+dlw1yK0yMJ1SUDCgI5qmXV27XnyFpdMojaqby0XYZ/kkPXm6vujsTFur3OHyAq6D9dMWQ5yNiNanOHjqvYuEZJONOaQm5TEOpW3r4AFUpsCMj2a8S/lHM12nhPtCjpMHBXqNnmpPqnnbSyz8zkkok1/4JdmfVc2sGM7kYfFyNcaCHgRHgziB3tSdOPSyIyC5rQ5zXZYzbpdn42Bh8AhsV1MCMqOgZmc4lGmTmOfJ9Bry5VdqJWmZz2ByDL51P4aUgYthqM3GMudaNIpRJQol11YL+nibYVNTkSniyJP89TbqbtLJHgqP8XWSHuDaABMefcOvqO9uqkiynGokupyYVca7R8znP264LQBKL8Da6OpMei0gkbjhIi/B8ilhKPcDGvowfSDYaXOHZaoj1U9oMi0eWL0rpYLGQ27VFiYZ4pYIdLY/SMOqoSOqsx2ecjy93288t7s1+2Vu02NmNDnf1GpsajCkdXOXV3DBzfzVJsizGYSrOx+OXLKyROD9Uhu9rc02z+S5O2f/xaEKqbwjZjBwVEitHu9QWMNyPjJsfgEZkJ+ADNFvbmEQ/e7Fx7WXGGi2eZ5G9EGqNulVbmxEppRSUIUc3yFRdUW36FyRiwjC88MYh4zgrNJmn9DoruT0XuKRPVLfejVYjOKW6K1yXmMhtTcKYyOK5AaTKPRW1qcE6MDXI/MC4a297TJJNctPjolOD4gz+IvPhYmqORICt6EeJVkfYmqCS9jX3Goymvyqv5CXjFy1+MX8IphjBs1oK5qoHYANKcb7OdIWy/oqrvDPiS2QSXmtuMff+hOLrwY5w4y/Px0j3wDi27zM0hvPQm+etKoWImmKnq/Y+PQKgwm6L+BQqBR6Vadc/RC6A19Xi3chvp6VmiyIbuuZXkRHemyFVI1F0I1hqjunJlTVNs2joqTEuxqBAKtEYUzFgdOzMcxwPR0I9yhe7+RDLNRLFlDP7UWWfU5hBZRybZDTTSfHA3Mledz5oH7dzGmc1lnKwzDzy8p2X3i3OZqJ7Ye3+nEwDMEqgjF6J8ChdOCKMIBR8bUTq3+z3QC9leuDJ/eYqCNXf1Z4e8aNfW9qC/aKIKSj2OmIyVFL6sBKgURZtvMw+Tzroe6rC6w8EcigdKdrnwqPcioXc1MC3hX7qBsp3uzewzoza1KSSk9dBiuiosxiE+NOcAHHk+Br39iqkSEmwVVW31l7ZsarAcJFBSNVTJHFdug0BrzbixwIYJ/fHwggzg3jAoFYjC7ndNzTSEKg8zI56APq5rZDm8tZQ== X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 214a9375-7521-48c5-e73c-08da3fd2b63c X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:19:06.0879 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: JT9xBbFubXtuG9LNEJnHd5u8sCtEIUqHhGnmqKl2nLJrFMHf+ZNIpSPb6/dkaZLf3FnSArP/YkXv2JPeEhUjyg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB5831 When a page table ends up with no present entries left, it can be replaced by a non-present entry at the next higher level. The page table itself can then be scheduled for freeing. Note that while its output isn't used there yet, pt_update_contig_markers() right away needs to be called in all places where entries get updated, not just the one where entries get cleared. Note further that while pt_update_contig_markers() updates perhaps several PTEs within the table, since these are changes to "avail" bits only I do not think that cache flushing would be needed afterwards. Such cache flushing (of entire pages, unless adding yet more logic to me more selective) would be quite noticable performance-wise (very prominent during Dom0 boot). Also note that cache sync-ing is likely more strict than necessary. This is both to be on the safe side as well as to maintain the pattern of all updates of (potentially) live tables being accompanied by a flush (if so needed). Signed-off-by: Jan Beulich Reviewed-by: Kevin Tian Reviewed-by: Roger Pau Monné --- v4: Re-base over changes earlier in the series. v3: Properly bound loop. Re-base over changes earlier in the series. v2: New. --- The hang during boot on my Latitude E6410 (see the respective code comment) was pretty close after iommu_enable_translation(). No errors, no watchdog would kick in, just sometimes the first few pixel lines of the next log message's (XEN) prefix would have made it out to the screen (and there's no serial there). It's been a lot of experimenting until I figured the workaround (which I consider ugly, but halfway acceptable). I've been trying hard to make sure the workaround wouldn't be masking a real issue, yet I'm still wary of it possibly doing so ... My best guess at this point is that on these old IOMMUs the ignored bits 52...61 aren't really ignored for present entries, but also aren't "reserved" enough to trigger faults. This guess is from having tried to set other bits in this range (unconditionally, and with the workaround here in place), which yielded the same behavior. --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -43,6 +43,9 @@ #include "vtd.h" #include "../ats.h" +#define CONTIG_MASK DMA_PTE_CONTIG_MASK +#include + /* dom_io is used as a sentinel for quarantined devices */ #define QUARANTINE_SKIP(d, pgd_maddr) ((d) == dom_io && !(pgd_maddr)) #define DEVICE_DOMID(d, pdev) ((d) != dom_io ? (d)->domain_id \ @@ -405,6 +408,9 @@ static uint64_t addr_to_dma_page_maddr(s write_atomic(&pte->val, new_pte.val); iommu_sync_cache(pte, sizeof(struct dma_pte)); + pt_update_contig_markers(&parent->val, + address_level_offset(addr, level), + level, PTE_kind_table); } if ( --level == target ) @@ -837,9 +843,31 @@ static int dma_pte_clear_one(struct doma old = *pte; dma_clear_pte(*pte); + iommu_sync_cache(pte, sizeof(*pte)); + + while ( pt_update_contig_markers(&page->val, + address_level_offset(addr, level), + level, PTE_kind_null) && + ++level < min_pt_levels ) + { + struct page_info *pg = maddr_to_page(pg_maddr); + + unmap_vtd_domain_page(page); + + pg_maddr = addr_to_dma_page_maddr(domain, addr, level, flush_flags, + false); + BUG_ON(pg_maddr < PAGE_SIZE); + + page = map_vtd_domain_page(pg_maddr); + pte = &page[address_level_offset(addr, level)]; + dma_clear_pte(*pte); + iommu_sync_cache(pte, sizeof(*pte)); + + *flush_flags |= IOMMU_FLUSHF_all; + iommu_queue_free_pgtable(hd, pg); + } spin_unlock(&hd->arch.mapping_lock); - iommu_sync_cache(pte, sizeof(struct dma_pte)); unmap_vtd_domain_page(page); @@ -2185,8 +2213,21 @@ static int __must_check cf_check intel_i } *pte = new; - iommu_sync_cache(pte, sizeof(struct dma_pte)); + + /* + * While the (ab)use of PTE_kind_table here allows to save some work in + * the function, the main motivation for it is that it avoids a so far + * unexplained hang during boot (while preparing Dom0) on a Westmere + * based laptop. + */ + pt_update_contig_markers(&page->val, + address_level_offset(dfn_to_daddr(dfn), level), + level, + (hd->platform_ops->page_sizes & + (1UL << level_to_offset_bits(level + 1)) + ? PTE_kind_leaf : PTE_kind_table)); + spin_unlock(&hd->arch.mapping_lock); unmap_vtd_domain_page(page); From patchwork Fri May 27 11:19:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863304 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 CEEB3C433F5 for ; Fri, 27 May 2022 11:19:47 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.338005.562750 (Exim 4.92) (envelope-from ) id 1nuY0G-0004ZP-P0; Fri, 27 May 2022 11:19:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 338005.562750; Fri, 27 May 2022 11:19:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuY0G-0004ZG-LH; Fri, 27 May 2022 11:19:36 +0000 Received: by outflank-mailman (input) for mailman id 338005; Fri, 27 May 2022 11:19:35 +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 1nuY0F-0003mu-Ql for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:19:35 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.109.102]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id e342c681-ddae-11ec-837f-e5687231ffcc; Fri, 27 May 2022 13:19:35 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05lp2107.outbound.protection.outlook.com [104.47.17.107]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-7-MUXn2TkgO1CFstY9F908aA-1; Fri, 27 May 2022 13:19:33 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM6PR04MB5831.eurprd04.prod.outlook.com (2603:10a6:20b:a8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Fri, 27 May 2022 11:19:30 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:19:30 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: e342c681-ddae-11ec-837f-e5687231ffcc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650374; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pUFaGQ6IIZ0mSnkSmdZGDLADzXQZSEblHdc1xu2R4MQ=; b=m9baytdMN1j7lHF/gkt3ZtUaM9AsF3eN0OpXLj2uMK70ClJmYXHnDczZHF52hLZEax0sfn ty/k1Uji5uvA1jHlLic0kRNYNIdweMLjlt4QE2Fz4YrGPvd7VM5xctzYm7iP7Omboz/qHQ edmJwcHSqlxK96gHgsWZ3vBBIDN9SMg= X-MC-Unique: MUXn2TkgO1CFstY9F908aA-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PHpu0BtdT1Lw1BCzvt3xSSnNYvdrC9UH/PbGr/CnpBRWurOdXNqs0A2ivbae1brsWWtHlOw3biUxPDD5KlHUvgbkLs7wm3I8tsIwechTbKrTbYdCVxCLyslZjBplyYcgyDwJFuzptBDkzu1cnW2E8zT/mfNaxTfPheYHf/Dudu1rIIFr1gURz6klT7fsITKXe2Aeb1FKLGau2/hXWvVm/44W9Lw//9Ql86aQHYpjgNq++xgutkIi1UYQ0VlgEhNp8t50MMzQR3DrBTyQqRzA4tI9fCWiexejUwdfdSrf7Vnw2kJasAwnzjO5PVv8os3HyyTsCDO6ivH+G2vo5bQfIQ== 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=XpBKX8iKhA8N8952sSDbijOunJ7uuUUiGIDbjZy4Htk=; b=Jd4oL0qUyEBkZl6TKbncabZ8064FdzykCRMXzzO3uVg5alcAVA8X9Q8QL/m6VFKoK3ZJnWV5jIomxU3PdREBJ3pPwNyYi0hQgSE93F2JTkEksh0ClzOq/YLg1LzDRGZrqvzMfsFWW1Wng95aZbTVSpGrCxG+VQ2lCAsFXpJDaQ9xdSPOgyZuvCUMzbl2l3+sOlMuVcTCjTnvABFzLfirS82CXoajNMjbWw0pE+PVWFXSyMa/N+vznzjMilabGiObTlDxmSaaToE25bKbYRAg22HdtDdWdgS4VXz224xhYCIRV7t2MTDniEbunlTgH2YvJ2q2GR8dZ40AFCWCoKNQXA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <0e9c8019-9318-280b-98c4-81c0c1b1d558@suse.com> Date: Fri, 27 May 2022 13:19:28 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 11/15] AMD/IOMMU: replace all-contiguous page tables by superpage mappings Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AM6P192CA0102.EURP192.PROD.OUTLOOK.COM (2603:10a6:209:8d::43) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 17c3e8c0-0410-4a2d-c737-08da3fd2c48a X-MS-TrafficTypeDiagnostic: AM6PR04MB5831:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Bi9MKv+LLOr4dZATwU/CeF+D3xiTIOV+u+P1S8ZNDISmU8YGmvgivZeCVtpC1G4XvANdJhIiBcZ+FiQ0WCXBH6PRh732wd8oWUC6JZJBkDwx2rG/QMepUSG32whSdTOIRvCt4lcNSSfDGTzyyH6OzFG8cs5Qx79BzH38MDD1BCDopXYX0ZOuv3Fvuu1zGsgJ/kSUX4AF9KgLWB5hJb++FaMLYYLR0fDmrhrVDOBMl3TtRaAJ1Cgw7Ct5SIut+VBlgjNLMtBdFlE0uFiA2r3zZfLdUMfdiMQ/IWdoQBFDbnzHXmTsXq4ix7aRTyKaesqdZZ3ZsuGBqGn4FE9tRVqwRq74Tic0lcu3fgDmQPgz1T6MFGiY1muYMGwhqtwZ7IMzQWUiiCKZvQsGvkWWDA7h/26/Ks180OOfCqdxMBy8CKkFprIU1tgVA5fzzalAKzFJkjtnT87xigWVM6Pio6yVGE0vSqxxAcuzEwJLpS6Y1F3kNXv4U2PLszjB3eb18CmjGWQtj7W387MgpAqR/L9nFOc3Tr/CnW2ze8MYiO8vBK58yzFF8Aq2kqqewkel3ALSK27U2qYpcYU6yHx53x4YRHuULBkrAJgAuB5nRM0GJkJ64llKptWx0cxCw4rHFKAUBCokW/9dWia2mtaNSfjxkNthmGijvXRR0XmpxresdjWZJ+d3XbToDUmaCDUS0hcLdGOkYZ3XeBThxxLYjdW657VkP0jwyhKtYg5bzRPauhkQAZUsQAheOzXwAj2nNxrC X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(6512007)(2616005)(26005)(6506007)(316002)(66556008)(66946007)(54906003)(508600001)(8676002)(4326008)(66476007)(6486002)(86362001)(38100700002)(31696002)(186003)(83380400001)(5660300002)(31686004)(2906002)(8936002)(36756003)(6916009)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: EJHUuHiIagACBR33f8poW0/DOaOqc2jmVyflSq8WA4pTwjlR3Gnxv7WUq8Rb7Vlks8QWEp8JW15HgA0UDTQpFSrYg3bU2eSX/P8Ofa4acw0h9GD/qXDrOQbmGtINsWq5fm4KtwpSa4/+39jhyBdyOPGOS9gvSOz6yQqwxY3QVhqT0B1IntQKo1dC+4ZjnZPh7V8hsGO98nIJYeDQyPx63WPv9xUX7nNy/wUEViS1htrd+6COd6aMT3yeNeIQ9wsBONWfnVIWBlcUOOymMq/O0JbiyBSVPZF73N2wpJLzovOJYaaVslC2WIHTNx8XXGYHQFVGneUFx1NCO1lpzYcff1CJWGNg92WmX1hfYgjas+9sZOMAe4asEkNpuRQQsLYkvzkJNt5sATTVV/OnBGoqMNBSs/EhgrWpQAHaNi4FhrNAi6G8JBdTJopRlRbjbbkz93k5J8mrEQmRjMtOihZCvNH24qJSebuld6UPWDHvtnTEraYiW+6P9USULTHJO9j7dQsMfqxP/D8jaRSl3Hj0a3jTEbtfMj2703/65u9HWMiBp3G3FMqxHi8XtO/n2zlxGT64OhMV5kUja3CnGnoVU9JP0D0R4w40QMSGKoKwvO5/TS6Qyal8n/w+7+pS+OFbA0dGUy9ZBQ3o/5knUg4+aDj4M6iA6pu26dVDlhse10xfJXw2wjf7QwqdflfQOFIgzKfKt0GW6+F67ZRMBd7+u82KCwPip6dKf9NuohmREWA//UyppThYSvjlcD2pRncS4vy8XvfTR+F8e4i+G0RZxQ89/tO9mMr8dH6qsm5RvRuJPR/rAgVkfTZx/MMLbib7kJ8bDbjI3J/plAxb8MgSrF4c2OWaOKx9qLGrlWRWxSqDnLN7UIy54JCpT87LM3ZsLmy0rp4uQLC0fhP/wjkEuHFPgW21Bq/IGKygIP4yun3hLiikx5dWLzVJbOi94Uclo+3MDUpgpVIKblCUhtLs8yIQZYjLZ+fQiBnT5+6d8KeEFpcMS6gvytD2vS5mA7h/z9ca29WfG81CVAicshlfadCaeocQkafHI7xBEVFnmrjPec5app/vKMQ4ugQZXIUQ8Z3oaw5kp4H2QdOTO6E+P7c0E6Ar0Q1+XR0/Z+WW3UD3Mn+ctAaRhRIYtHwtCYQjNC3p1sBCf0O7VXJiG5AJ+ghIkZw+5+AY50JfmM4RFz5hVFZX/yr61hy4ftEP4+c1ZIBIKRJ+LXfJ0P419H+eqlGEfYZ1EI0hsRUruzSLG56D0qnc/Pb5SHq9rcC15su0wRBZoJBYeR8fAVfb9tRhqh1glhysXvfWpIyi0e3oOMkmy3cxnssH2SCPnYrZWunhcegHUmJLLNxntsmjXdk6vrxVAhb4Gy60bnz+d17u81JIZ3FFxfFORv1t8QcceuOe/97BfUwcl+y4EUutR6sfms+f0HZwofLlE4EoNiJlsNkMZZQQr8bA/U2NpcogRKTTgPF96wAq0Vf4RWNhoapIK+/ScJ9DSxNidHJ9v3V8HsHh4yqwqt3+/h0B/pRg/D6pHOzkx7YNEDps2Im90hjpScChmOcW/wqFhkhr9E/yIRrwIbi6aFpCeIJCi8/LSeFXDi73yQJdDvlaOi5UM4KvM1iJvEilAV/hLB7mZk+eLjtfTP5bwrZP6kPwUZ0iCugkR5SahT33kRa88yBR1CzO/vYil5Ttx3m8eJ26RIal8yYarJBrkGxg4lrACz6lbF7bMBiwmCDaLWw3735JZCLdnQ== X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 17c3e8c0-0410-4a2d-c737-08da3fd2c48a X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:19:30.0864 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: CPoaZDDBUz3i6vdr1Dcveaid17xFuxvbsrqoTec0cd4aa4t/rm/Z+8A7DudsWVKhpQXtXHS176mpL6rKg99wMQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB5831 When a page table ends up with all contiguous entries (including all identical attributes), it can be replaced by a superpage entry at the next higher level. The page table itself can then be scheduled for freeing. Signed-off-by: Jan Beulich Reviewed-by: Roger Pau Monné --- Unlike the freeing of all-empty page tables, this causes quite a bit of back and forth for PV domains, due to their mapping/unmapping of pages when they get converted to/from being page tables. It may therefore be worth considering to delay re-coalescing a little, to avoid doing so when the superpage would otherwise get split again pretty soon. But I think this would better be the subject of a separate change anyway. Of course this could also be helped by more "aware" kernel side behavior: They could avoid immediately mapping freed page tables writable again, in anticipation of re-using that same page for another page table elsewhere. --- v4: Re-base over changes earlier in the series. v3: New. --- a/xen/drivers/passthrough/amd/iommu_map.c +++ b/xen/drivers/passthrough/amd/iommu_map.c @@ -81,7 +81,8 @@ static union amd_iommu_pte set_iommu_pte unsigned long dfn, unsigned long next_mfn, unsigned int level, - bool iw, bool ir) + bool iw, bool ir, + bool *contig) { union amd_iommu_pte *table, *pde, old; @@ -94,11 +95,15 @@ static union amd_iommu_pte set_iommu_pte old.iw != iw || old.ir != ir ) { set_iommu_pde_present(pde, next_mfn, 0, iw, ir); - pt_update_contig_markers(&table->raw, pfn_to_pde_idx(dfn, level), - level, PTE_kind_leaf); + *contig = pt_update_contig_markers(&table->raw, + pfn_to_pde_idx(dfn, level), + level, PTE_kind_leaf); } else + { old.pr = false; /* signal "no change" to the caller */ + *contig = false; + } unmap_domain_page(table); @@ -409,6 +414,7 @@ int cf_check amd_iommu_map_page( { struct domain_iommu *hd = dom_iommu(d); unsigned int level = (IOMMUF_order(flags) / PTE_PER_TABLE_SHIFT) + 1; + bool contig; int rc; unsigned long pt_mfn = 0; union amd_iommu_pte old; @@ -452,8 +458,26 @@ int cf_check amd_iommu_map_page( /* Install mapping */ old = set_iommu_pte_present(pt_mfn, dfn_x(dfn), mfn_x(mfn), level, - (flags & IOMMUF_writable), - (flags & IOMMUF_readable)); + flags & IOMMUF_writable, + flags & IOMMUF_readable, &contig); + + while ( unlikely(contig) && ++level < hd->arch.amd.paging_mode ) + { + struct page_info *pg = mfn_to_page(_mfn(pt_mfn)); + unsigned long next_mfn; + + if ( iommu_pde_from_dfn(d, dfn_x(dfn), level, &pt_mfn, flush_flags, + false) ) + BUG(); + BUG_ON(!pt_mfn); + + next_mfn = mfn_x(mfn) & (~0UL << (PTE_PER_TABLE_SHIFT * (level - 1))); + set_iommu_pte_present(pt_mfn, dfn_x(dfn), next_mfn, level, + flags & IOMMUF_writable, + flags & IOMMUF_readable, &contig); + *flush_flags |= IOMMU_FLUSHF_modified | IOMMU_FLUSHF_all; + iommu_queue_free_pgtable(hd, pg); + } spin_unlock(&hd->arch.mapping_lock); From patchwork Fri May 27 11:19:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863308 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 B05FDC433FE for ; Fri, 27 May 2022 11:21:54 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.338023.562783 (Exim 4.92) (envelope-from ) id 1nuY2B-0006tT-QV; Fri, 27 May 2022 11:21:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 338023.562783; Fri, 27 May 2022 11:21: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 1nuY2B-0006tE-MX; Fri, 27 May 2022 11:21:35 +0000 Received: by outflank-mailman (input) for mailman id 338023; Fri, 27 May 2022 11:21:34 +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 1nuY0j-0003mu-8i for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:20:05 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id f366a85b-ddae-11ec-837f-e5687231ffcc; Fri, 27 May 2022 13:20:02 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05lp2104.outbound.protection.outlook.com [104.47.17.104]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-44-_zmL6YhwNZuTX0hg1el-9g-1; Fri, 27 May 2022 13:19:58 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM6PR04MB5831.eurprd04.prod.outlook.com (2603:10a6:20b:a8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Fri, 27 May 2022 11:19:57 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:19:56 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f366a85b-ddae-11ec-837f-e5687231ffcc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650401; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ccB0FqtHHL8l/2emiNG86bYyLELu/eicE503PEmq7h8=; b=WjtNNSvpY6yp+prMCXRILKROyfcjL4tXe0VB+hPcTFVjkTfW3jdXuirSKVghZEHoVdLFEg UuR0EDWa/+0kZOF7OeZ2LpfkQbiMMG8w/DHlw9iqs/nfoI+M7T+eF4M13DCjKer//l75pu 6idjsOhCierLIjThgA8W+F2hsmxW3UY= X-MC-Unique: _zmL6YhwNZuTX0hg1el-9g-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hqcgB5aCd9CfB+WL5HOGL6Ua+tmEhP2YNYTgvtbV4alIZ+OZcYK3mUBuy8Hqd+3zhjB/b8PfCnGg7rZlkOTVDJucdOPfJMeK611Oq7DFaJA3zEn2jTj8cs5E1mgLbwLS+Zs8zgVo7BEJzQ+EpKZ83rRZnVyXHSknd/czI9joQB74mg2x4gqsT3hC84oati0FtEiKSVeVxNfIViMnjW3lHXLZ3k8Htk1GXw6814704dd4NEcIdZ72teIv8kQ5dlg2+1SBxPltuolMTCFD9rQHhC3OzyP/zzgPpruh4LhdKfmtEpd5vQUBizyN+PGlcWBkHo6Wi9+3seum3afkSfKYGg== 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=ccB0FqtHHL8l/2emiNG86bYyLELu/eicE503PEmq7h8=; b=nR2wIqM8czn0wSh7TaziZ3IEC04OaQprfOliqW78kHzM2XWN3CTwdZIBs1ygUI0Q0YOFiEEA9JYB+aGkAyx5t3zIz6F91ydM4NhL9lrvnvYMKBIUxsXDS/72BpNy5WOE1D6g6g8rTrhNfQi0AKveXbOsyfk4Lx+cLAsJ/HtdKxCuQ2yEN/ptzP8g7mbdMLZdmkDGBZkUFjm/77br35ZOmBhTsxcK2vZLiKs3qMI+JNuu/z1Ah1oscZEer4J6rC+0FjIFKSWkaJnNIYm5ElvCe4+kvb7kkFs0T5kqFgZ4lmB8pEkDeNp1FgEqWcTlyyLf7WHxO3yIu6d7H4v8n2b0kw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: Date: Fri, 27 May 2022 13:19:55 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 12/15] VT-d: replace all-contiguous page tables by superpage mappings Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AS9PR06CA0249.eurprd06.prod.outlook.com (2603:10a6:20b:45f::22) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d92381c6-47a4-4cb9-8c1b-08da3fd2d48a X-MS-TrafficTypeDiagnostic: AM6PR04MB5831:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: W44r6fVakDa+hkDWyb6nmGlaZsN51S5U0drRzYW05Np8DYGEmw6yRUDhKLxyguTCq0YV0+5z/3IOD7CPdRGWVTqIuJJAQw+DYtns5Lejd3zPt1yLgE4S3CwndfdeD1rxfvtBuw/Kld+iXG9Sp4HmF5iKaKEhrzhaWPQDA5xLXRzJu7yRdoGcV9OzT5R04gsOfPgRvJBlNmMMfh7gn5E4C66Fla9s7l6iP74/gf+HITnsQNzZ2ylij4OF2r1jYZsCqX7k2FdPrAy+gM7q2pIp0NgLpKk7iXjh9G3HtGVzzIZgx5T8YfvxC3HhKXJEKLktZ+mdKWjcJGf1ZlsUiFCfwsUJqIG5fo7LXcO/HudW8XItA8j3bL00PEFu+HgsCLlnL8VGNiP12qLI58oTrRJxGvpsufDVdMcJQWutRE6Rv3lRDAoj28qoE6Hn4519gQKJ8mp8SQCEZkaOCLoFvkxoROIa/pCycHIu09kHIEU/G+b/9C39TlFUmToe/3Jnew2M9TJkSbCF/VXJ71w5VAKq4a+TsvODxkn9mntGTBdmUrWcpD622bSyZdNOEK0Zwxb1K5oAjsKoLg996bHMy7zu3lEKoX8zjhmve6RsP/cwzw23fybi0R11ob+eUNcyEUgYePKAieTI2mpV4mj/N99oaAss5mYo2FkHBK6PyC7hjkh5nN8ZnRMofqFydXCGePB7JnOmgQdYq9lpxdcsDS0S27K9mJb1rIsdxoeICNOGGZF1U//db7NVWgJRu2/dOQpI X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(6512007)(2616005)(26005)(6506007)(316002)(66556008)(66946007)(54906003)(508600001)(8676002)(4326008)(66476007)(6486002)(86362001)(38100700002)(31696002)(186003)(83380400001)(5660300002)(31686004)(2906002)(8936002)(36756003)(6916009)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?pcNZvsMv5L1vkCF5BbrCLpOXAxSD?= =?utf-8?q?jjQ2bxq8EgNbYQHxFqGVsrgTPiAZkTcLh4wdJPoNawoTdEXnxNwJzMUjBWX/JnAAv?= =?utf-8?q?z36IELYaGctrb4/okx6yWwqEDOzEbciLohgzqHRVJu21jeWzJzIE0560qJKHthFes?= =?utf-8?q?S1kWwo/7TA5GwkQDJEYIOvw41nFL3ijktOO90QbwbTwt23r+S531jYu2whHdYGDYa?= =?utf-8?q?wr9N0b14h+FYXWl4AFdcbtW68I2/hNlwsBVw43gRBPWhWa/Sj856uya0BwpVW+U+P?= =?utf-8?q?xgA7CvPDQjhsoXANIgFYlECAwp5TIGjbhpv9wdxjH9bEUiJiDorLccA/0XMUhY1ae?= =?utf-8?q?XvCC8QUgrlS5nabi1qKxpciZ14qZw/OoBVIL9v2ImR3l77vYcZ8zEgHVtvHjl83SZ?= =?utf-8?q?VmwkkrrpTVkADxYHm/BMNmZWPXSyiL/8gyYJU0ajwxhKnN5w9+DHIUtUq2ZcrJS5t?= =?utf-8?q?5iRqyuKgKYhR71l02FRT8fsCnfIaRMO5J3JF0lFnCBtTSHLsGT96nN/5228zD7J7d?= =?utf-8?q?pXDROOKFDrienzb6wkz3hRrChTFdJ9aN82vqkkVEk6/XKDr2+E6uUJ59qGvCqlecU?= =?utf-8?q?Meb0mip+9+v6C4Y4PqiP6yEBnlQLv1vLCPYFCX76J9uADGfmQdse6e/fkl04hpX6D?= =?utf-8?q?Cd3XtXP9hM1sfMCNVjXHurKzJ34EOaKHWksF/AMgJteD4KEN809lectwI4sR8hs50?= =?utf-8?q?M0Nh5txXWjj7Xz+FbQHQ7BuW3U35CV+xnjt5hxCouF7eLHn9T7cHRCyof6+AwbQxJ?= =?utf-8?q?aeo2kmeRf7GlKOx03bO1Bfqk3iekHD6rnqrfLKSCEYsAxPao/0xpvCpQPkfiCUSYe?= =?utf-8?q?HM+gCWJ/t2aUgefZfmslPAP4Scb0PrE7k4C70t3ACMn857C/+0D4iRsNhb8xIf8tH?= =?utf-8?q?VfXrbSRE6mgwlq0zVqGdWteyW3rD/yGiqBXBxLU8hMHfXNdytrquvZAFA060p/kFD?= =?utf-8?q?YiTF/BzdZw6f8wlITz7u/GxK6AafQFN+D8H7aBuMCIb9A+RalX34fcZSETzri3YYu?= =?utf-8?q?nOP6RvMNtRntv+Lt5zuei9nCLG+xT498NOFHxNz3/yZ9Pbp4jjN2IUK08yyHXnLHA?= =?utf-8?q?zdjAGXyPeJeAbquHfT+5Ja19ulVM2cf7ENXCnPVdt2nyS9gia81+Uz9wnbhVXrMWj?= =?utf-8?q?0QKl0iNnD8NnskTRq8/VWX24hxVC24KvY0d8ACQ1RIII9G8yKgXnVS3hgsNNI/ash?= =?utf-8?q?dklhyVbVPrD7TzmGd0GHos+cJlZOaretqak/8EVqNmHhOf+5qTVbl/zezCqUcukCF?= =?utf-8?q?ZD1ozytEqvmBTUnAQhLulQgFAqqqTZa06PiO/iGTNwBtEAgArQhl64SuM15SQuYkd?= =?utf-8?q?64NHuEr24UYTKdw57D3Hg7qqaiqgfXu19wLtSdL4siwZqfNzESTX4PcY/VluvqzQQ?= =?utf-8?q?jrdTWfD0Bwpsouojnl8F7+44K4CgpnmE3mnTy6JT7nLr2vFq8Ubc+lMflkwAorQHf?= =?utf-8?q?Vj/GPRF2KSTe8ZyEf9KdETmybsP6N1RklNYTEkQZcAgMg+Pro8WxTTPrAmm28nDz6?= =?utf-8?q?OpNNmLfBcWIiwpnhXS/sMbf1SfcpO8tJ/excZClJX/am3h5M5BSLHcWxwRWQi2CtV?= =?utf-8?q?aPPZCxM3eRAvnzaQVXy+0H58fvs5SEqtf7FM6RLFhCdwbAcLwZ180GIEfLCvd8MCO?= =?utf-8?q?Cx4Ld5IdG1G3U1oTtwZRUuiM3YcSN5SQ=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: d92381c6-47a4-4cb9-8c1b-08da3fd2d48a X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:19:56.9284 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 0eIXHQ41+V/iIoSnDGdl0bfuYbSg5pZi2H/B+o7H0ccnlnE3+CoGnm1PcdQWnEq3R+xppYpntTmnTNSudMnkOg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB5831 When a page table ends up with all contiguous entries (including all identical attributes), it can be replaced by a superpage entry at the next higher level. The page table itself can then be scheduled for freeing. The adjustment to LEVEL_MASK is merely to avoid leaving a latent trap for whenever we (and obviously hardware) start supporting 512G mappings. Note that cache sync-ing is likely more strict than necessary. This is both to be on the safe side as well as to maintain the pattern of all updates of (potentially) live tables being accompanied by a flush (if so needed). Signed-off-by: Jan Beulich Reviewed-by: Kevin Tian Reviewed-by: Roger Pau Monné --- Unlike the freeing of all-empty page tables, this causes quite a bit of back and forth for PV domains, due to their mapping/unmapping of pages when they get converted to/from being page tables. It may therefore be worth considering to delay re-coalescing a little, to avoid doing so when the superpage would otherwise get split again pretty soon. But I think this would better be the subject of a separate change anyway. Of course this could also be helped by more "aware" kernel side behavior: They could avoid immediately mapping freed page tables writable again, in anticipation of re-using that same page for another page table elsewhere. --- v4: Re-base over changes earlier in the series. v3: New. --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -2219,14 +2219,35 @@ static int __must_check cf_check intel_i * While the (ab)use of PTE_kind_table here allows to save some work in * the function, the main motivation for it is that it avoids a so far * unexplained hang during boot (while preparing Dom0) on a Westmere - * based laptop. + * based laptop. This also has the intended effect of terminating the + * loop when super pages aren't supported anymore at the next level. */ - pt_update_contig_markers(&page->val, - address_level_offset(dfn_to_daddr(dfn), level), - level, - (hd->platform_ops->page_sizes & - (1UL << level_to_offset_bits(level + 1)) - ? PTE_kind_leaf : PTE_kind_table)); + while ( pt_update_contig_markers(&page->val, + address_level_offset(dfn_to_daddr(dfn), level), + level, + (hd->platform_ops->page_sizes & + (1UL << level_to_offset_bits(level + 1)) + ? PTE_kind_leaf : PTE_kind_table)) ) + { + struct page_info *pg = maddr_to_page(pg_maddr); + + unmap_vtd_domain_page(page); + + new.val &= ~(LEVEL_MASK << level_to_offset_bits(level)); + dma_set_pte_superpage(new); + + pg_maddr = addr_to_dma_page_maddr(d, dfn_to_daddr(dfn), ++level, + flush_flags, false); + BUG_ON(pg_maddr < PAGE_SIZE); + + page = map_vtd_domain_page(pg_maddr); + pte = &page[address_level_offset(dfn_to_daddr(dfn), level)]; + *pte = new; + iommu_sync_cache(pte, sizeof(*pte)); + + *flush_flags |= IOMMU_FLUSHF_modified | IOMMU_FLUSHF_all; + iommu_queue_free_pgtable(hd, pg); + } spin_unlock(&hd->arch.mapping_lock); unmap_vtd_domain_page(page); --- a/xen/drivers/passthrough/vtd/iommu.h +++ b/xen/drivers/passthrough/vtd/iommu.h @@ -232,7 +232,7 @@ struct context_entry { /* page table handling */ #define LEVEL_STRIDE (9) -#define LEVEL_MASK ((1 << LEVEL_STRIDE) - 1) +#define LEVEL_MASK (PTE_NUM - 1UL) #define PTE_NUM (1 << LEVEL_STRIDE) #define level_to_agaw(val) ((val) - 2) #define agaw_to_level(val) ((val) + 2) From patchwork Fri May 27 11:20:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863306 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 761C6C433EF for ; Fri, 27 May 2022 11:21:54 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.338024.562791 (Exim 4.92) (envelope-from ) id 1nuY2C-0006y6-Ao; Fri, 27 May 2022 11:21:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 338024.562791; Fri, 27 May 2022 11:21:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuY2C-0006w2-0s; Fri, 27 May 2022 11:21:36 +0000 Received: by outflank-mailman (input) for mailman id 338024; Fri, 27 May 2022 11:21:34 +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 1nuY1H-0003mu-Du for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:20:39 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.109.102]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 093bed15-ddaf-11ec-837f-e5687231ffcc; Fri, 27 May 2022 13:20:38 +0200 (CEST) Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04lp2050.outbound.protection.outlook.com [104.47.13.50]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-3-_Hr4YX7tPrC7z9jcvVbOHQ-1; Fri, 27 May 2022 13:20:36 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM6PR04MB5831.eurprd04.prod.outlook.com (2603:10a6:20b:a8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Fri, 27 May 2022 11:20:34 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:20:34 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 093bed15-ddaf-11ec-837f-e5687231ffcc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650438; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2x2jaOdslxN8JhgKIhN6FboWhHnFSS8ihgO/7F5za20=; b=fLpea1hHIl1AJcC4LVowliNBazDORKJAdsOy54mAtsIHmPriUVBuNszJu9rFaKNPY54owF CFz5gWZ6AXLlvTreMC5i/FluRRQ8K4Jtu6dgcqbd4jqsqPr+DjmZTZZ3W+qvzf8ExYlvy6 XPXHalRmoO1us6xIm2g8/NQTLDJa8hU= X-MC-Unique: _Hr4YX7tPrC7z9jcvVbOHQ-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SAHBHXhTNzCGtLpbO3eN+wtb3Mnm9Cj+8D+hSKZCgiTL7HRaAM0Vc5Z4IMOirQSOooPs/wDX3hzbHbVK9RuBsliOvn1rdVzNxx54lPjvHK1oC94gazzbFU1xC8hcl1hB6XqoV5w1EdjouCQb/XyAaTJnxrAMJOffBUUuBnYxbe2WCtUVKf820Uj2ulHeL9sS0JnILW7Shcmvqjdb0O1EsQRFeVGCUuVkTwcCp2H43aVa1wffoav0pLT2PJY/i/xvnrxOf7zTFy+91Zi4fNm6QwW+2HWlXVd5ileOrYTswpVbkDjth/iU2SCgcLKgQsg5tAqSpgRjgPKRM3kIUh/d9A== 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=mp5Lj3VLjBJ96BBU/R2oY/5qvo6N3bduCfhHn2y6m6c=; b=XLJ1LVsjEEexKlz50VwhNDkX8afhgaaAGwWSl2I718CP1BOsSMKdIAXF8rWcuqrAHmXQXpPTnIkRAqVoKk4+7PKfSZLycPlersChNANLQOVSmOOugemvBW/iK/W51eBrDWsJ0PgebZ3mX784bPL2yXHOoZ7mpUGHps8fcqDrRwvKaoZ2NbFHCZ1YdAwTC7n39XAk1nCoyO0a7yCZR1EsMzLUYypx2P+yrJTaLA51hnKN+dxfiOVTIWyfBT9MzJx9pHJM96oqPHQM8XdHieT6nmc8TLWaKKQhzR5G6k+tyPzGq73CQhid3sqDRIdzOBEToCIsoqOjra8pmlVEl1wtZA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: Date: Fri, 27 May 2022 13:20:32 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 13/15] IOMMU/x86: add perf counters for page table splitting / coalescing Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Wei Liu References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AM6PR08CA0025.eurprd08.prod.outlook.com (2603:10a6:20b:c0::13) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5c9dde31-d382-43e5-ad08-08da3fd2eb18 X-MS-TrafficTypeDiagnostic: AM6PR04MB5831:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Bey/pb6DnrAQlAGYARc5xJHKLRV5109YobdiTwPu9u/wPNS2inPiE4zUJxfB/GLiaoyDYXG/PW0yzKJ3Ynt0J/9tel0ib7eWWWpR9lN4wVDre0wLsbSvLtJtNr+8p3taBqlhqtbZQqACWUXwllh/KdV8Yz3FM7qgeOFWX981jKijpS1Uk8Hm7Z/opkxt6s7G/1DXC6rSImlWXjzCNp4IDcdXu0vhVZfPVyPFkaZXS4iyzN1mWV2A57yDnARBTd2cSKz5i1SPnl5s86G16CMrbydrIvEXhiTrQCcVLmHV9gqWLOE3uvgqPZlBSA+UmyzfDjXCYnTT39papb8jISVAMKrGH5M+my8KiK9xa8hwgYEc4SLqN2J7JUrZCa609DwCW1RCWmAL/jxOkD0kraRO+E6a/4sQQ2xJ+HSuSeuVUEwsVs5jHW5agDJMH5C0R8yPH6DA6QrM2CeHlnl/l3FvjnplBOHGUSbJyOr8GCr5Q7hm6DLv2XHnyRmiFECL69uJki5FQeYK1mYms7cPFBgHvkXXtBvrwNvhMxdsd8XjNp37olc19ZH1EJfYumjz8p6CzbW++sDbPqjorw7GKXarneUqz+t5G6rcM9vqpX5E7s2zoMU9TKKG6sBY8SEIvmG3OO64dIE5tdyc3+Ta5OHJK7ySDqjIA02Gmna19+6EZICIFnwUGx1kh8m+wtdIcXHNdcTlGYP1d/ysLn8CXJqcc1Vf9dW7vgH7u7tJjXQqhwNu50vCIg9VYNXP0GJ10m8e X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(6512007)(2616005)(26005)(6506007)(316002)(66556008)(66946007)(54906003)(508600001)(8676002)(4326008)(66476007)(6486002)(86362001)(38100700002)(31696002)(186003)(83380400001)(5660300002)(31686004)(2906002)(8936002)(36756003)(6916009)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 6T+WIkT4tAjs80URul34bfBsMP0InLY3OnpQ71chnkJiW3Z/6C6h0skI3vC/uEhn/6/H1ulOxCbN784bXYJsnlDXKEDzweHFszCUAXUdzYBnrH598ZXmPghDT3if9YO6zfZL9vDKuUT5yAq4QG3iKMbViIhP/djfIE2c/1miWtElKeWfyg5hpZabIL6pY8qsYZjevjcwx+kg2oWNImHc0WXAzg2fZADL8fiiXC93TkDBB+30pkowKTS+XiZBaT5hgZBkOIiXA1x5qt+U0+TpQaIbhupxKNmOLzwsYiSk5YvF/CY7jE0f6rabzVtZ4ohsSt5JU91nHIusZ2V5GQOEmNNIu4hptJjaEV/AmsMH0RwB1tdvRpi5AuLIViJ+9TEvh7AKRbsevwl+taH0L0pl4JtUBHnxPZPOwYx+h3vZAiF6aupcdjeD2esJBpzVuP1/49h2f1xzIN6z2ACuX7HBs6UfF4zTP0GSO6MXFOQtc1ewpnbtpWCgDMQtlEHwikkdtTT5cX6UUQ30WNyHsZT+T7REgROP9apLJvsroyxFCWrJjfqjCCoDOZi01fIit/vLnd31ihxOk/BwAeCM4a8SYYt+eLRj6V7xroWHJ4YZBtzxekquaPQThsmAg3VBZXJFBph1Ir9QEiPbHslpM5xVMch7a8fZXBo32aQQONUp1bnr13n6CVvOiM1yvd9KVF18S6c6AsfBV4eAvEfafuFWKyv0qn4fD2DrAujcsG45gNcq97US2lvRfBtel3fQJY+hsjWV69fnMTyRPjjv4loXfsH8sppTkc/SLVtFrF1afwnRKASvjV+7JxuX7FaHUsqPrjhPcnGfXMQasHw7BImHlShZdjAM8V3mlF2uSt4UBiFWNyq19uk5m8LW9eWH2PhcrnlnhT4EPptbJ7WYF+78+Ccc56T9CXZTvLvN0LVQ3uFvhRx2NRj6+YEWMvqJ2Na+yv+r2htA1rv4qiWL42fa11LEd3yI4GPRisayopmBrMZ6GzTBnKSABDmDhFeGF3g3PR+75UX84/xdqjx2C7jnuVyBdyfQp2F9H5rdWmlA6mhvZrCnbIlTSdOF29KSZjQvd/gSkkB5ocUgn4ArX+qCkfc0a4gpAa4gVQRC3YGdPID6V1pLGg6vZMTv1SyflF4RyD/zCW7muoyJMVfLmYPc4Ak+lTAq7D+KuOl6raxMOfosxsrcLzxPalt+iurzSUsrrHb5OyFAht5rYOGQEVcBf8V3Zyfn2U3G0nGGigP04CjJgqfcwW5eE2zNS5z2BqOyE2AD8OfhsVgvo+2MSL/yJy6+SG7oqNE/1L0XumIWJjrirT+WnXwR2pxZ2NlTIjS+7lnKSu7di83nI0uxypZ2R3hIKvWszwXPRXZWqx0cUZSx7tFOay+MRKxdWcFfex9/F0TvJnEn9/glAzDPyaJDyd/eGCF/b3WUABQv0x2Nl3gKPQYkpEH91luL08ccqWzNGGlaZ980AqI5qitChEoieGerTB3+yl3SM4mMs+2BMnQ71V4h7kHwzu4QrtQwYvu58OAOL5H8J5jdfortupyC20d2u/SwjYF347rnSzgAthuBMSGn/hZ8yMdhdxTJQwtqMs5il4lPUeAr4WVZVLIcDRtobHHhJqe1JJDcq7xiI8BBf/YfGwRD9jh0/izBd21Y1j2MefB2hFnbm+/AotKtEJDiDFgtPFHfxWi0ekCgtYmaUksJoJGWFybAaqGyaXVyZIllc7Zbz96PiP9IVq2vEw== X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5c9dde31-d382-43e5-ad08-08da3fd2eb18 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:20:34.7385 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 5x7mui+CpkO441Nuq8AehptNj6Zbs5jF5qN+YisD+BHc7FT7RrbBq48LwbV0u237BSWFZSEysROwofqML03Htw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB5831 Signed-off-by: Jan Beulich Reviewed-by: Kevin tian Reviewed-by: Roger Pau Monné --- v3: New. --- a/xen/arch/x86/include/asm/perfc_defn.h +++ b/xen/arch/x86/include/asm/perfc_defn.h @@ -125,4 +125,7 @@ PERFCOUNTER(realmode_exits, "vmexit PERFCOUNTER(pauseloop_exits, "vmexits from Pause-Loop Detection") +PERFCOUNTER(iommu_pt_shatters, "IOMMU page table shatters") +PERFCOUNTER(iommu_pt_coalesces, "IOMMU page table coalesces") + /*#endif*/ /* __XEN_PERFC_DEFN_H__ */ --- a/xen/drivers/passthrough/amd/iommu_map.c +++ b/xen/drivers/passthrough/amd/iommu_map.c @@ -345,6 +345,8 @@ static int iommu_pde_from_dfn(struct dom level, PTE_kind_table); *flush_flags |= IOMMU_FLUSHF_modified; + + perfc_incr(iommu_pt_shatters); } /* Install lower level page table for non-present entries */ @@ -477,6 +479,7 @@ int cf_check amd_iommu_map_page( flags & IOMMUF_readable, &contig); *flush_flags |= IOMMU_FLUSHF_modified | IOMMU_FLUSHF_all; iommu_queue_free_pgtable(hd, pg); + perfc_incr(iommu_pt_coalesces); } spin_unlock(&hd->arch.mapping_lock); @@ -543,6 +546,7 @@ int cf_check amd_iommu_unmap_page( clear_iommu_pte_present(pt_mfn, dfn_x(dfn), level, &free); *flush_flags |= IOMMU_FLUSHF_all; iommu_queue_free_pgtable(hd, pg); + perfc_incr(iommu_pt_coalesces); } } --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -404,6 +404,8 @@ static uint64_t addr_to_dma_page_maddr(s if ( flush_flags ) *flush_flags |= IOMMU_FLUSHF_modified; + + perfc_incr(iommu_pt_shatters); } write_atomic(&pte->val, new_pte.val); @@ -865,6 +867,7 @@ static int dma_pte_clear_one(struct doma *flush_flags |= IOMMU_FLUSHF_all; iommu_queue_free_pgtable(hd, pg); + perfc_incr(iommu_pt_coalesces); } spin_unlock(&hd->arch.mapping_lock); @@ -2247,6 +2250,7 @@ static int __must_check cf_check intel_i *flush_flags |= IOMMU_FLUSHF_modified | IOMMU_FLUSHF_all; iommu_queue_free_pgtable(hd, pg); + perfc_incr(iommu_pt_coalesces); } spin_unlock(&hd->arch.mapping_lock); From patchwork Fri May 27 11:20:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863305 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 A6D8EC433F5 for ; Fri, 27 May 2022 11:21:21 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.338013.562761 (Exim 4.92) (envelope-from ) id 1nuY1l-00060W-47; Fri, 27 May 2022 11:21:09 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 338013.562761; Fri, 27 May 2022 11:21:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuY1l-00060P-0O; Fri, 27 May 2022 11:21:09 +0000 Received: by outflank-mailman (input) for mailman id 338013; Fri, 27 May 2022 11:21:08 +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 1nuY1k-00060H-2E for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:21:08 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 19e57dd7-ddaf-11ec-bd2c-47488cf2e6aa; Fri, 27 May 2022 13:21:06 +0200 (CEST) Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03lp2177.outbound.protection.outlook.com [104.47.51.177]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-38-1zjZN68sNRC4HqIvRuFawg-1; Fri, 27 May 2022 13:21:04 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by HE1PR0401MB2522.eurprd04.prod.outlook.com (2603:10a6:3:85::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.15; Fri, 27 May 2022 11:21:01 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:21:01 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 19e57dd7-ddaf-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650466; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rr0SXJIhqPFcpvsJUpdSD/Fb1591DtjkDEfmbHUCDiI=; b=F/cRumbzNzCbFN833TDbFSLzxr3W54dszlPH/EYYJTBT6EOmBodRmJ+ht2Y5oE1q5PLz1g VH/SLfYghGTBhVMgRNdLax6YlQkxqPLKT8oxRXjq6trN6/wXVdhrJtYKxPsDi9bqexHf3q ZEVPZPMrHtVU3bm2R1CKNjtJBURSH1I= X-MC-Unique: 1zjZN68sNRC4HqIvRuFawg-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gg5GgiQK7GxnOkQBy0ekPu4MajoVvJEV0hZqPj61oyOnlfAw8SUGjztzbKaiiZkWbjkj+q7o/yqqWFcOdVzC22oWYZV0FigGAOK+5NGAflc+lUZZtAgSWrrLYo/ozcScRZkdbEsr7AMUpD6ZnTg7EiH5pMEnd/D9VlIJdQAKjECYIGwB0azW10B0dzai+kQ0WT5FdYeJtG8JLR+vf4nSuy0QcgOuuqWTW9wMrF6yi0trptsV9TpKchhhzcvDfT6Znrm09meiC9eaMcWqK9yYCj15bLchntAlPgLP7arZqbkcF8YZvbo1b3CA45vvYTatZkjOEnMpKgbwUN/+3t5qpA== 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=PX82Sk7UOiPch4uhVxxmVUF3Sh6BPeVOjcXNAE8rHco=; b=QPIhT6f5jvDnzNwQejsIhmtXyam2qq8xHit2VlT0uIdtJRC241ZgoTulYURYOn8xf7M27yiZDPORTGRK40ij+0Kc6xNqQGnLef+OLfBVu97fY5Ah3Wjej8MfWWNj6LsnoPz7LDH0LycSg2x0zR14HWGbHMRM/xw+4fiPFo3X5XLDVmZFDfWf9Z8ANGiEeKKUxhHj3mSQ3IAI5TWrkbykEIPufVRHS8tC+Q2OzH/mNkiWRWBtDqhlHARCgbwuBI4harMfZQwGnXm7LguXcSKgAUMNylc8wBezuvVhd8mwi/hPcBlGgWLc+nxLFHZlJ8rB3zZL+5prowiWqVrsufnCfA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: Date: Fri, 27 May 2022 13:20:59 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 14/15] VT-d: fold iommu_flush_iotlb{,_pages}() Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AM5PR0601CA0056.eurprd06.prod.outlook.com (2603:10a6:206::21) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cc6041e9-9271-47c7-00f6-08da3fd2fac5 X-MS-TrafficTypeDiagnostic: HE1PR0401MB2522:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JGQdM8r22Jw/tfmi6W3x2n6Z6RtW9U86d+8vQ+xEiB9Jp3M7V0/tGGDwn2ga2l62ee6nrSdKpp8yZ+/eFK1nOS421dpHOP6KxwisknegyAkYzpq9CMzi2jPuFyrleYZJMbA3eBolHQwxGioCEcWRHAM8kV/yOswG4xWfjMUZR5FV6Y1wcZDPHJtq4M9HqpepbC/5/snZXJtEsebjEIIYRuS/pMSkUya2Dba17K+OEuKrjW69YoKpHOrISrFGREceOQ02kQ7PPBKwqOrnG02zpADDK7Xr9COACLptoBmSP7bzmDFbAsjur4/L9tlNiOlyNct1fAcgYwJ1WTikkDRyHbIYedw/03mzCI/vlP2Bt/XyKu1MkCfWFLdJsf7Yi94TSaCu/tuNidf8cxMEZvLLhF37iCqfdknZmS21Y6YNl0qIW/cCrenQq6pdTc/L9RM1k7d+e3tlElpPFsFE0TIfaWDjOsKB70+WKBoVsXsDC7z8bQ2CrDYTzA0MQmfinGpd7lQt2LtA80B702+CqXaoEvsG7ZrF3YjkXAzannXG87E0pKtf4G7MVzEBg4ITnRu2Oj6JIMKxYA6Uu8okfOd19wncZgYGeCvhx+ph1nOZ/3C0rKqR5XwaWRVUPZaDZLijoVZF/gFqmkGHFXz2Ep+UriDsdR3uuGn/9ZO9o7sN/4wBBx2R5fbnvgBzxUzueudIn0v5BMfsO03vz2yJyZmiuoEV7GLBPm3ZgVJ45zec7t0= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(2906002)(66946007)(26005)(6486002)(4326008)(6512007)(66556008)(316002)(66476007)(508600001)(6506007)(54906003)(8676002)(31686004)(2616005)(83380400001)(6916009)(38100700002)(186003)(36756003)(5660300002)(8936002)(31696002)(86362001)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: dKiQNOXR4eZDvv5LlULs8+oMhayf/w2YR+63S8APNo7JEKozvak+491+3nrQnoIGsnYo+k9AJf9h2WDZK/7FnsNxzZh/lgBf1LWSpBf1K5MaleynO0zbLfgE2Nqn0/D7hf6JlbruOe/hicb2Vx1NQxJNnNDSnPqr6YSDtwixxTtaEG0C5zGBMg0Y/7M5Er3II0KPBPrYeE+l6MT5JisT52jsbGSnzbLvsv/qPRtA4keuPBDme8wQ6NY1grRZOHwh08lDGdHQyQHUmZdNTPKTbiOaa1KMB5mzseLqbRj6DF5EvZ+t92ymo0s5NvayrtVEVKPiI4Z/MYNqLVh9krYnUP3zC1wBd3bEIX548PwwQH6+ak13ERX4ySp6kDYXj6G8tklPElJEbnhEEtk6vj2GLnBFRAoeY6Wh7lKqWvyQuEnAYbXmtkU3gLRGMB3tN63tSTXb0Wj1YdQDqj3E6oq6M3UEMWWyRwlUh2s2LluyKx8/sBIIlsm3x5Csigt6rBpHGTA0YT2A6/wEmbS3OZjYJ77aebe8tRehojUnvQ2ToJ+wKdb7KiwqeIRVW/ARghEhPFfpcWGqYpy1tlmnF27oxcNuiIr4Jp3kgkBaOB7JsL7izhAwCMmHHu9Cpq6Xik7hDOUPNO/QSGj7ULVKIhNkmBoSu06m+sRu7/N5JNIvh2+QYLMLhKRn2fz/or4ec78JqvuH+0ci0jB/v2Uu96wb5QWCS05V6eyTKgT60LjqgIBGEMj+GDr9XLTugRSW5Li+SeemN8lS5GsO7+p66sy49EFzEt8xzx7qhsCsuWpxDuXnbPMVKPDU9eW6q1zEzrfL1FWuTd8whdepZ/4vqHfvhl8bGIVhm3ML/7usMTJLb78NN3hrhXxcrMb0rpx0FHLs4PS39bJQI/fB03IRxi2zIwt+xDSLEbQFIcd5kg0nHZZd3HXiolVUxvqKaQ4KNQVwnSkJtfZOv8GLRMFRrE6OjMTCPcR7wSj3DigluxgURlwrQepUrUKYe97zil7HkTcrzg8UtdgtJqNDRbAop0RWr5Pma4L8iw9npva5LdyhMhI7gJap5NABYrXMvD7PKjb5xFM9OgG2O6iX5ezGWzRT0ybHSnYiCOxwfpdEMfwy4IWZK2G/By+zlIkWRYy4d6NwZkSkl2AGBUrCVa7zAVlPBAY9JoXU5KGGyJvM1Q/rzfF+zZJNnUGhzEA7RVQciAOP196qHcTTJwhAT9nmx5YBapHfSeTaS0Wm7RXOn4z0cUqvangAvTfghB8JWNu9hHBjRPkDiOxZNX0zYoEcEEPvG1M3qc2QH1cRbaevTv9yyXd/EGR4fh4TkuN1JXmTOhuap8cjc2NJkJnxXA5Xs8b+XeSy+72pLTVEFoJ5ed5JM3oYmYDB5id/ViA6hYu7GkkxvDY7hmJ2/gGdeTwFPbJlPRhgiIjMLM7ZpSJnaDfw1L4H/pYOYXTBPFgmzoCSBsuYaDyt5DSdv4cPf3ZcsasPnd4wUvBFuPwE8m9vMLQ0XNoVmwhhwphZeDjjBkshODkBrqNSpb8b50RzM8B3x6r3QK48Lt2fOaO7LdJwSnqh+ATUdKK684Zeb1ZzQt2xIVDNriR/6VRYgwrf1/ZmW2LF0u42cbROInt2oon/p8lxPFOFxUZdgxobIZtXAs6S4vLA/UCEZEYHlCwy3Wx+04IQD8sp5QxYHqb2IuHdaoMCTXUJQSUzrAMsOLDqZqyy8fjAhwTHH0vcLS9oSbLXncSdRg== X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: cc6041e9-9271-47c7-00f6-08da3fd2fac5 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:21:01.0493 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 6/m1hy7JSQQDdrgO/ndP6seZWS2YMqSzgbJe71DfsVA3tBk9r8y2Xdlpgnf2YYP2ysYQTgvc+kRBAaLra8PmwA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0401MB2522 With iommu_flush_iotlb_all() gone, iommu_flush_iotlb_pages() is merely a wrapper around the not otherwise called iommu_flush_iotlb(). Fold both functions. No functional change intended. Signed-off-by: Jan Beulich Reviewed-by: Kevin Tian Reviewed-by: Roger Pau Monné --- v4: New. --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -728,9 +728,9 @@ static int __must_check iommu_flush_all( return rc; } -static int __must_check iommu_flush_iotlb(struct domain *d, dfn_t dfn, - bool_t dma_old_pte_present, - unsigned long page_count) +static int __must_check cf_check iommu_flush_iotlb(struct domain *d, dfn_t dfn, + unsigned long page_count, + unsigned int flush_flags) { struct domain_iommu *hd = dom_iommu(d); struct acpi_drhd_unit *drhd; @@ -739,6 +739,17 @@ static int __must_check iommu_flush_iotl int iommu_domid; int ret = 0; + if ( flush_flags & IOMMU_FLUSHF_all ) + { + dfn = INVALID_DFN; + page_count = 0; + } + else + { + ASSERT(page_count && !dfn_eq(dfn, INVALID_DFN)); + ASSERT(flush_flags); + } + /* * No need pcideves_lock here because we have flush * when assign/deassign device @@ -765,7 +776,7 @@ static int __must_check iommu_flush_iotl rc = iommu_flush_iotlb_psi(iommu, iommu_domid, dfn_to_daddr(dfn), get_order_from_pages(page_count), - !dma_old_pte_present, + !(flush_flags & IOMMU_FLUSHF_modified), flush_dev_iotlb); if ( rc > 0 ) @@ -777,25 +788,6 @@ static int __must_check iommu_flush_iotl return ret; } -static int __must_check cf_check iommu_flush_iotlb_pages( - struct domain *d, dfn_t dfn, unsigned long page_count, - unsigned int flush_flags) -{ - if ( flush_flags & IOMMU_FLUSHF_all ) - { - dfn = INVALID_DFN; - page_count = 0; - } - else - { - ASSERT(page_count && !dfn_eq(dfn, INVALID_DFN)); - ASSERT(flush_flags); - } - - return iommu_flush_iotlb(d, dfn, flush_flags & IOMMU_FLUSHF_modified, - page_count); -} - static void queue_free_pt(struct domain_iommu *hd, mfn_t mfn, unsigned int level) { if ( level > 1 ) @@ -3266,7 +3258,7 @@ static const struct iommu_ops __initcons .suspend = vtd_suspend, .resume = vtd_resume, .crash_shutdown = vtd_crash_shutdown, - .iotlb_flush = iommu_flush_iotlb_pages, + .iotlb_flush = iommu_flush_iotlb, .get_reserved_device_memory = intel_iommu_get_reserved_device_memory, .dump_page_tables = vtd_dump_page_tables, }; From patchwork Fri May 27 11:21:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863309 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 07118C4332F for ; Fri, 27 May 2022 11:21:55 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.338019.562772 (Exim 4.92) (envelope-from ) id 1nuY27-0006Zh-I2; Fri, 27 May 2022 11:21:31 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 338019.562772; Fri, 27 May 2022 11:21:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuY27-0006ZY-Ey; Fri, 27 May 2022 11:21:31 +0000 Received: by outflank-mailman (input) for mailman id 338019; Fri, 27 May 2022 11:21:29 +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 1nuY25-0006Ml-NT for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:21:29 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 26c4d764-ddaf-11ec-837f-e5687231ffcc; Fri, 27 May 2022 13:21:28 +0200 (CEST) Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03lp2172.outbound.protection.outlook.com [104.47.51.172]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-38-OMchAvVAOgOd_pNorEldkA-1; Fri, 27 May 2022 13:21:27 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by HE1PR0401MB2522.eurprd04.prod.outlook.com (2603:10a6:3:85::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.15; Fri, 27 May 2022 11:21:25 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:21:25 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 26c4d764-ddaf-11ec-837f-e5687231ffcc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650488; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3NJESEP16cpa4nN52+FBPmemD9Gs3qUaoc8yPLoMudo=; b=Ier2PKC/NvC2Oxuea+yUJr//WmUt9+YlqOPUBrSMWYh3+gtw8Y7KGHro0CMqgJEZxIIYrL 8pSGsveu2MF8CC8C6e3XJaP7wEhucZY0KLqqFfa4Q20NuDgdY0MoniGPI27Rf9rDVsUne8 Xwos+ZB+mhDDCVgr3QmGWomi27/cSOs= X-MC-Unique: OMchAvVAOgOd_pNorEldkA-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aOxb/ez4tiKBhV2d9dx84nl6WKyDS6kG+CDrC9ETpGeAfDmTJRSHLF7OLWRFcQGSOA6qTMQuwc2D/LXiaSsj4VepApIfxllf5DGGm5nJ10Uup9P84o+Hg/ggnKLnlMyseBEY8k8zPtSgW6uQLq0j4tG3Bz5/XKo1Zk94or6a6+M2NWnaTCaM7qx5kmdj7Mif5EOzqzJ8FbLGoZ/Uk+qi7iMDUq0fUJ8kz6P1lTbE1BIUOvO+NfPECL7dC2G4buc4sygeGVpxiVxnOOiqu7LclcIWUoiOyIBqL+oMl9JRFA1VrtckCJ4wpcb9Ei/UmwjwvEeSMAPsc6ikm0Suybv3AA== 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=aNGQdoucDFou5j3M8E7Hd4miL8kBXhKfjN3w6aHaDIs=; b=eoy/UHqGfBBzqayZJAtVwr9RO1eNVZHfo/3MHfjamIzt76wDAyr5qEyWOcv4PxCMeXG7iTcH2wGkg8mkCMA+cmtK1e/3tlPUHaniiXLwMMy2js9cUPxMyQyhM+mhI2Su8YzszUoER/ZEHmpcfHVFTMdqLEhBlb315iLpU/ZwDj/VpVs+vMf9S9yD8G4hHeQLwRGj6tEdKeg/CaLfzvupL3uSi4m+qttVXeoxd33yPm9DXTH19wj/WzxZ3OkvpnWNubHBFLCDw4qgVdlq8wwOSjJoaues16pHnxXs9PJeN6LDt0+HbCd7VNjQsC9V6t19vrfz/JHNMhBztWMB8ud5jQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <6f80cf5d-536e-4e4a-5c87-4b55efcac3a5@suse.com> Date: Fri, 27 May 2022 13:21:23 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 15/15] VT-d: fold dma_pte_clear_one() into its only caller Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" CC: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AS9PR04CA0048.eurprd04.prod.outlook.com (2603:10a6:20b:46a::27) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e8ec8054-aa7a-4057-53d7-08da3fd30947 X-MS-TrafficTypeDiagnostic: HE1PR0401MB2522:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KNMXAsJssjBYFHc9j/v2eLnA8QVQDpqZ88WdRljp3hTSiQnwjJgS+Bnod9oHi+8mENb7+JAEOxERmxwL1ghiqwMhzWyjFbkqLAvQMYO68LRep1mrOWMApFKNv2AmRJus48iDzdpaIxk+WrvFgkpKPJyK6asii8mG4WN/5JZkNX60m/QROKquy7TBfJotmSmBZmxwtgMMnIekcboNBwWHicI6/Mnjlqy3aj4RZTZ3pTvoHysVwEnRiph6pwNx9u2spPjQlWawK146S8joBiNTOa3H9c+4dx5tOb9ycid63OaE9JZoL3D1G+1eoGinKk92d3fsaw55p9oH6uTl4qWFVMdcMKsdZKr7+QYE6oCJrUGW2PledSVkMfMjaj6OiNv6uPR4Vq5TuXHpui+m+pu2DnT2cU9W7YZu+dHuApVBv5OCqc15FGD+768xMeLQBx7YaIVJ8uX5bAk1bOzeikYhFpcxJFSAbuwPU9Zps/92LgpSO7osMHH/Ado+4mg5+yWJPTwqvuPSHxdHyGqorEPcKsZ6Opx6l1LFxEdRsIXOAELgq1OfKEvP3NzTrgmtHJGdyIgnoP8Ebn4GKreWdFfXf9kOanJcNTEYdV9xmBVUGGA5ESSwTUePWRz+oj7m87Ay3QRTI29Slkzug8SFffasXyD2jwY2tcJ97+Pr/rHGZAq7jip8vZIleR+O7gOwFWK3Zw+BlFZYfRX9w1BMFPYK3IGJJk8v/OLMoQxWwXcpj/k= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(2906002)(66946007)(26005)(6486002)(4326008)(6512007)(66556008)(316002)(66476007)(508600001)(6506007)(54906003)(8676002)(31686004)(2616005)(83380400001)(6916009)(38100700002)(186003)(36756003)(5660300002)(8936002)(31696002)(86362001)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ZcZ2CAZTRS/1Ie39/UD9K3TCjRLkZoj+6i/QPritidV6SDsNjpQx3VOZnpvH73dnlCGyYqJqfU3cxODOFZFBSaZR5uiydWmpGL/Nu/lm+2DakysmajEGGzdqQ3+JXzmum8KAHsXYpftDP8O6vyKPg0mf/xZOFVbNpWWYSQUmsRiQPemG/jgJVbHxno0EvkMePZAof2GvtqtxzHXtCHKPEggNlU9Me2OKHG5XMmBHOA2TwwwYdrc4vxSn2OjEjl/deJ0iIJxujP3VVX6qyFGm6O5zqSFmdmkycYlXSZXaIrOtVIDhNlWHjODF+PsJkZVexFpjq+pq5d9EaJrL/F5xdtnTUZjvwPMvhCokjLOSFrYXgzz1fKKxvxzQEomMWkh0SOtS8e9wJQV3VPlUIJUg2Ca0yJaURlmucz4hKntz0TNX8AKs/jmXyLS8gvQnGXZH7gTHtAzqrTHmMs5aid3woGp1qvo1fONuPlqWzM9bFACcCsfq7ApV3kngpFk7NkVryMc6zSLok5PDTxL4NA9y9OQ1DhhhOKw26JzB0mhmiDviPsgXj//u05Xwhr//Zj5TiXHPs6Wwwxe1tWMEe1xEySZViNpj4YeDHvOp+Gtic7Uzoyv7k6v/0oY92r6ygE29B/rUVfNoVt++bi/2G2rGS7VNF5ka0L9pdTzUCv37sXZAHj7AR5/kUbMXCex/St+Up4B8hyer8b9ytJ2EtiFFlEqBG2F11BTQutoVkOzUheUGdWmEHH9Zpc/yysnf2TgCvBG/lRHldeHDGHPOBoX7eZnvY/6ky239yQfuhFsJijo7HfmBllVaaIU6Envt5zhPx5CDcUJ51StOjC0pDYZvJjSmjA/NnbcH8fxku0RKGERFpiCjUKDK8OCbqKlbnIc9ab6KAvnuorserEY9kPW4ElRGnalXpHdzmlTubauSHPhnLufUBxbBVGunJkIfnH2/rJnm4+odgR4v2a3KfqAZ/3Pmreof9Nlilf3i3Y4kZuu2/skLkUnpmKPyj01jcY25Jc8pqazYpwahGZtdhkomPruxv+6yCyCS8QVI3vsRgM+2Apxt0qsXdL8MHJ0td0AFdh7ON7KjEq9T3l85Yp+lZT9RFv1AJflPjTDj/H1eLnhIbegi6w5tw6Y1wtn6gfnNLlM/8T8KpDQVLOZBkfifRKzAT409drMOu2Yv4JjfQfhB+nym6i8/ct738UIQlPpyxUan8BBlccoNDlgSYCinfJAS0IGGwsrqoR71O2h70gDVGrJ6/YEeidkwX9nhtwq3i8AzI/PFJ1JvIQCW1FZLmInnCDtLUJ7kOT9ji9hIKPEjLCXQylqRPSjpnebQ3+qO3TLNQZl8U7iqcJADmZQLNyUmnjGUWFB2bwOpSehTCnwHRgx7K20H6+3NDYB4FhY9i37SO1TBrgBvkfUSzGjkDEwXz6PgndXOaHnKO8P6AT6i2XJrSHT8A6hkVXUsnfY3RsHecklB4HwdJlSXXRWEN3Ph59TXYgqVY4rh6MamkmdICtO0vz3zW7vN/BMh62MGiCg+4U8L+7Jye7o+X3IhT/hJlyzD6cQK+4TBweZ0hPum/57c9uQrH4Zm7PYqMXDiMYHIqoKtGjxnIBIKmlRsK1E90WUP4Dp/Yl+3Ps2Vt51N0h+qq7Lm0cR1qs25BFxJ9oHW33nZ31P24H7u00zbCNIb6xKalCIFgCyRdEjo1wBGDsUEkOXoLl3R8KV1RX7c6DDfk3HWls0kt6n5p2S1AQ== X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: e8ec8054-aa7a-4057-53d7-08da3fd30947 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:21:25.4071 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 1Xsb5gN8ZtoXDIIJAmrPT3D6XymVg9YQo4EopeUZKFDizA+g0u2a8QFVJKnfo8Is42seBgdvObVVh9DDfIyZiw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0401MB2522 This way intel_iommu_unmap_page() ends up quite a bit more similar to intel_iommu_map_page(). No functional change intended. Signed-off-by: Jan Beulich Reviewed-by: Kevin Tian Reviewed-by: Roger Pau Monné --- v5: Re-base of changes earlier in the series. v4: New. --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -806,75 +806,6 @@ static void queue_free_pt(struct domain_ iommu_queue_free_pgtable(hd, mfn_to_page(mfn)); } -/* clear one page's page table */ -static int dma_pte_clear_one(struct domain *domain, daddr_t addr, - unsigned int order, - unsigned int *flush_flags) -{ - struct domain_iommu *hd = dom_iommu(domain); - struct dma_pte *page = NULL, *pte = NULL, old; - u64 pg_maddr; - unsigned int level = (order / LEVEL_STRIDE) + 1; - - spin_lock(&hd->arch.mapping_lock); - /* get target level pte */ - pg_maddr = addr_to_dma_page_maddr(domain, addr, level, flush_flags, false); - if ( pg_maddr < PAGE_SIZE ) - { - spin_unlock(&hd->arch.mapping_lock); - return pg_maddr ? -ENOMEM : 0; - } - - page = (struct dma_pte *)map_vtd_domain_page(pg_maddr); - pte = &page[address_level_offset(addr, level)]; - - if ( !dma_pte_present(*pte) ) - { - spin_unlock(&hd->arch.mapping_lock); - unmap_vtd_domain_page(page); - return 0; - } - - old = *pte; - dma_clear_pte(*pte); - iommu_sync_cache(pte, sizeof(*pte)); - - while ( pt_update_contig_markers(&page->val, - address_level_offset(addr, level), - level, PTE_kind_null) && - ++level < min_pt_levels ) - { - struct page_info *pg = maddr_to_page(pg_maddr); - - unmap_vtd_domain_page(page); - - pg_maddr = addr_to_dma_page_maddr(domain, addr, level, flush_flags, - false); - BUG_ON(pg_maddr < PAGE_SIZE); - - page = map_vtd_domain_page(pg_maddr); - pte = &page[address_level_offset(addr, level)]; - dma_clear_pte(*pte); - iommu_sync_cache(pte, sizeof(*pte)); - - *flush_flags |= IOMMU_FLUSHF_all; - iommu_queue_free_pgtable(hd, pg); - perfc_incr(iommu_pt_coalesces); - } - - spin_unlock(&hd->arch.mapping_lock); - - unmap_vtd_domain_page(page); - - *flush_flags |= IOMMU_FLUSHF_modified; - - if ( order && !dma_pte_superpage(old) ) - queue_free_pt(hd, maddr_to_mfn(dma_pte_addr(old)), - order / LEVEL_STRIDE); - - return 0; -} - static int iommu_set_root_entry(struct vtd_iommu *iommu) { u32 sts; @@ -2264,11 +2195,17 @@ static int __must_check cf_check intel_i static int __must_check cf_check intel_iommu_unmap_page( struct domain *d, dfn_t dfn, unsigned int order, unsigned int *flush_flags) { + struct domain_iommu *hd = dom_iommu(d); + daddr_t addr = dfn_to_daddr(dfn); + struct dma_pte *page = NULL, *pte = NULL, old; + uint64_t pg_maddr; + unsigned int level = (order / LEVEL_STRIDE) + 1; + /* * While really we could unmap at any granularity, for now we assume unmaps * are issued by common code only at the same granularity as maps. */ - ASSERT((dom_iommu(d)->platform_ops->page_sizes >> order) & PAGE_SIZE_4K); + ASSERT((hd->platform_ops->page_sizes >> order) & PAGE_SIZE_4K); /* Do nothing if VT-d shares EPT page table */ if ( iommu_use_hap_pt(d) ) @@ -2278,7 +2215,62 @@ static int __must_check cf_check intel_i if ( iommu_hwdom_passthrough && is_hardware_domain(d) ) return 0; - return dma_pte_clear_one(d, dfn_to_daddr(dfn), order, flush_flags); + spin_lock(&hd->arch.mapping_lock); + /* get target level pte */ + pg_maddr = addr_to_dma_page_maddr(d, addr, level, flush_flags, false); + if ( pg_maddr < PAGE_SIZE ) + { + spin_unlock(&hd->arch.mapping_lock); + return pg_maddr ? -ENOMEM : 0; + } + + page = map_vtd_domain_page(pg_maddr); + pte = &page[address_level_offset(addr, level)]; + + if ( !dma_pte_present(*pte) ) + { + spin_unlock(&hd->arch.mapping_lock); + unmap_vtd_domain_page(page); + return 0; + } + + old = *pte; + dma_clear_pte(*pte); + iommu_sync_cache(pte, sizeof(*pte)); + + while ( pt_update_contig_markers(&page->val, + address_level_offset(addr, level), + level, PTE_kind_null) && + ++level < min_pt_levels ) + { + struct page_info *pg = maddr_to_page(pg_maddr); + + unmap_vtd_domain_page(page); + + pg_maddr = addr_to_dma_page_maddr(d, addr, level, flush_flags, false); + BUG_ON(pg_maddr < PAGE_SIZE); + + page = map_vtd_domain_page(pg_maddr); + pte = &page[address_level_offset(addr, level)]; + dma_clear_pte(*pte); + iommu_sync_cache(pte, sizeof(*pte)); + + *flush_flags |= IOMMU_FLUSHF_all; + iommu_queue_free_pgtable(hd, pg); + perfc_incr(iommu_pt_coalesces); + } + + spin_unlock(&hd->arch.mapping_lock); + + unmap_vtd_domain_page(page); + + *flush_flags |= IOMMU_FLUSHF_modified; + + if ( order && !dma_pte_superpage(old) ) + queue_free_pt(hd, maddr_to_mfn(dma_pte_addr(old)), + order / LEVEL_STRIDE); + + return 0; } static int cf_check intel_iommu_lookup_page(