From patchwork Thu Jun 9 10:16:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12875165 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 DE763C433EF for ; Thu, 9 Jun 2022 10:16:56 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344864.570470 (Exim 4.92) (envelope-from ) id 1nzFDZ-0000U5-Py; Thu, 09 Jun 2022 10:16:45 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344864.570470; Thu, 09 Jun 2022 10:16:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nzFDZ-0000Ty-Mv; Thu, 09 Jun 2022 10:16:45 +0000 Received: by outflank-mailman (input) for mailman id 344864; Thu, 09 Jun 2022 10:16:44 +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 1nzFDY-0000Tp-BV for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:16:44 +0000 Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04on060b.outbound.protection.outlook.com [2a01:111:f400:fe0e::60b]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 426952f3-e7dd-11ec-b605-df0040e90b76; Thu, 09 Jun 2022 12:16:43 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by DBBPR04MB6297.eurprd04.prod.outlook.com (2603:10a6:10:cd::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.12; Thu, 9 Jun 2022 10:16:40 +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.5332.013; Thu, 9 Jun 2022 10:16:40 +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: 426952f3-e7dd-11ec-b605-df0040e90b76 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NINFdV5wdugjbFGv+VeBa0HOfbxcLzdRpzCI+uLmjTn7geSfuN7QuYBH+DfpQr3tCU5+jyHjIUSNPvldFzTWWDyP+gql68TEjfmX3r/3Axfg9ZH7LgXo7ahbM3ubHV2aX06vsKZJeYZjk8dPlJa9mQTQI/0wkI5PlrBg2l5YFo0K2HTaJt05ZoRMy3nAi/cwbXkQ6bKowXyhKG0s87YiFEwnlFNY7p7lwf5392O9M4oUnnamPqwxmVZdGBcL99dTkqOvBXQROPRaQ/yxIzPgwYls3BQ0YUgfiN0K3y5DhpmkqseoFOAmQDe3LPuwXaoRMeRfmnq4mTRDylZzrhb1pQ== 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=Ht+gcaO8t0HFmkoBoateZqPcuX/ukcu9CpBSQf8lM4E=; b=SStxS6jrPCDvvEeUj2EVq6vPntsqTq6mKsMl74/IsidWfBsIo6ioKqBH34D64ZmUSWgH7sjHShNVxBA4aYJlUI9paS5uUAAXKauoWySJCBG55FMB/MBSr5UgvILNDk4cNcr6RnBjSolae3PHjG2wEZEBgIK3WJ/rMefqP+zgbIE6/dwswhwPz3/1aSN2Yr6BqwIV8y7nZmDp+Nyn+SONYdM0hhsbw4k8V8AkNgpTFNuWdgKnlhYr3tmh+sgnze6aKgdhwfdcaEZA/OKg76Du/m+uXCdxyO3cqF2IHqJDEPo32lu8UkJ6QyyJU2NcdsPY12msXwEIBbdWr50jFbbn8Q== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Ht+gcaO8t0HFmkoBoateZqPcuX/ukcu9CpBSQf8lM4E=; b=al6pmNttL96AmQH75aiOumkwO8AmMxEd3EIm7f1c39KvCJGoCWxyQVTp3n+00u2+Ul/SXYDm7Al8xfW64tSoxCsBUN7FEOp5uEpZSI4C7i2qkN9YCBy6jDLZDKU6d4Lr8j8VJspba7NQIZ8w212x/e503hCoqF0Nfpc51lXQbtWcdeZwChySWLg+07k5w7QDEfDoualaNdRMgioQQGDf/vPbuRY/04u5X/axZQo54EChv8dxLnCeSIC9Wn5kd0ZQEY0VgYgTkc4nVY6dJeNdoCABEe63Jt/94G1bFchEQk69OSs33X+YAE1U5lA3jDoXEOWJuoE4G1vJS1q2wQbqCw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <24eb0b99-c2c4-08aa-740d-df94d2505599@suse.com> Date: Thu, 9 Jun 2022 12:16:38 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 01/12] 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?= , Wei Liu References: In-Reply-To: X-ClientProxiedBy: AM6PR0202CA0045.eurprd02.prod.outlook.com (2603:10a6:20b:3a::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: dfb4dec2-d371-4f9f-136c-08da4a012515 X-MS-TrafficTypeDiagnostic: DBBPR04MB6297: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: /4LkUC97aHneSm/krzy/YdIb6CsGLuxPwJkvPW69rAMjFAnLzhVIVemhY0875FG6wP1qFiOpZFUHlnQPA8Nl6bbc5S8BUtDyFmVpf2/x7jQ0Ks4E7aXHayV8a1psIPNdqpwolswCBOCqJRS0i8zlYCnnJFttS6L5Hv932dOCCbLw4DUSHDgcd6veOmh7pteu37AjlnMC8N6jISXpFlSipvJYNIsE4tsrOIVkLzetFpl55L4VolC3ySqzVGeEEC/6v9FENiQ9BfEmzccKqQNvqrL2wuKN8+AnuneoU6O7o3qD2tXgOwVTAQ+UcLPTZ+Jxox+lRly1moML9aO/5WTypzfDLmFyvIX2BK6hQgf9AYgTsg6CV56KHp4+h3d0bRHl3XjcoGs3g2Yp4bj/HANvGip65YcWDcwPe+esuERdU2nt9hdzcyHrjiwkewuEL/ujaU8ubHDx8Ru/yFPPEubxp/VuTbsagWzn4oIx0alVPxsOB5Usj4/exhmTe2A1eiiCl/j/Stf6XEbj0wI4abtdaFKrSUhk5tQAjuH3zz2/apqDXSBRo+O7a5b+MJXM0wdYrs5ae4ZCajSbDkxlixQvyH8iv9xI1XMArg9NdJx5kZhnUww7HvbqmoUiiOgqQSbAseQky+9wkAkygMwq4mQPd1Fx4lz8XCVJcgwgE5vj+V1uxwj2kRKAgbxhTkmlqDG9Mqa1yAUCyM1XmkFufUwJzZAWwCau4Aa5eoZ5cNgLdXyiL63ipcpz3DvfGJD5w7kt+A741t6rAU0fujmjdr8w6VkR/R8k3h2WGvThzrmSn2dZ22cC8fBwY9BljfURHNbH 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)(2616005)(36756003)(83380400001)(31696002)(26005)(31686004)(6512007)(6506007)(2906002)(4326008)(54906003)(8936002)(5660300002)(316002)(6486002)(6916009)(38100700002)(508600001)(86362001)(186003)(66556008)(66476007)(8676002)(66946007)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?AE9Nrd/toMtSZ7tn6T0sHHdnuv/x?= =?utf-8?q?SArW0mQtpd6Q8gHvnbE51nMooG2bvo6/F6gcFwzAt94AepkXm/M9Gv51biUOGKrr5?= =?utf-8?q?XIEpHVYr1a5+50sfNbRKhdkLBgh/PTIOktPQz+WjynLM+AcLLKjcjPXyKUGVudXNa?= =?utf-8?q?XyOPsS1H9FQy6RjD6bfb+iGW7wS3Egr5sGgRyy1zTuf4tQd7SimItxLIFrAp7+Zfr?= =?utf-8?q?N4tK/SZXQjxkfWlIGSe6c27hVjn0xcHu0u7qyhUxSCRvEz14w7Ho/mxNGlzN6UZkf?= =?utf-8?q?QZqUFp3PXWaQyBx+TbzXQInzbCKWy8R/S29rUpSjmWT2cLajXg6fYmElq5/rvM+Cj?= =?utf-8?q?rzludo21PjhK0PznYNEw5PLEh0UtBovK1VM/sr8JQFstcdH3zui4KT3UrNFO88sPv?= =?utf-8?q?XCnhlXzcuf+NbA8qXEYV84t9tLVD0ClV745/+3k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J49I3AhL4pyMHNap?= =?utf-8?q?96sUtj1o5PbTN2bFLvDeOMMmTKMkKXCUqc9oSE8hgHbiYYjl/qL0F0R8POERYliNx?= =?utf-8?q?M6p38DNzXjqxkfDkXJEuRv31Fm/tZJ6zi7PIcHgmixNT8FTLtavPdMlQR4HVMWgH+?= =?utf-8?q?dIii0PjnV3O/6k2FU5eFzMWB5Ce6quhYc5XYjXxfFCPEHL/UpDrFzIvwHBtQeSEF5?= =?utf-8?q?xRl7kTl57suYfyruK60L0v33U4c/fEDQUb3DkglKmBjk3E4ekd2Hn3/Kketk1WOmF?= =?utf-8?q?QpECA8ldRP/NAzB+YxjpvNtcyzhCstyvdnLhgxug82Oo+e6XRSOwQxQtjcldP9Ns0?= =?utf-8?q?4oC2UREZO8Q+MElq66MLHCNMdt78YXgw=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: dfb4dec2-d371-4f9f-136c-08da4a012515 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:16:40.5313 (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: HXmYS6QAkDhzJ4I+YoqdChPw7Q9V8mQIvBdvaF01LwSdtStsMpsoejum6Ir0c1CjnkSBTSabnP44pvFHL+sK/A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR04MB6297 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 Reviewed-by: Roger Pau Monné --- 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. --- v6: Extend comment on the use of process_pending_softirqs(). 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 @@ -551,6 +552,103 @@ 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); + + /* + * Just to be on the safe side, check for processing softirqs every + * once in a while. Generally it is expected that parties queuing + * pages for freeing will find a need for preemption before too many + * pages can be queued. Granularity of checking is 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 Thu Jun 9 10:17: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: 12875177 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 418E6C433EF for ; Thu, 9 Jun 2022 10:25:44 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344948.570591 (Exim 4.92) (envelope-from ) id 1nzFM0-0007oC-PZ; Thu, 09 Jun 2022 10:25:28 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344948.570591; Thu, 09 Jun 2022 10:25:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nzFM0-0007o1-M6; Thu, 09 Jun 2022 10:25:28 +0000 Received: by outflank-mailman (input) for mailman id 344948; Thu, 09 Jun 2022 10:25:27 +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 1nzFEF-0000Tp-NL for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:17:27 +0000 Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04on062a.outbound.protection.outlook.com [2a01:111:f400:fe0e::62a]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 5c263a2f-e7dd-11ec-b605-df0040e90b76; Thu, 09 Jun 2022 12:17:26 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by DBBPR04MB6297.eurprd04.prod.outlook.com (2603:10a6:10:cd::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.12; Thu, 9 Jun 2022 10:17: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.5332.013; Thu, 9 Jun 2022 10:17: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: 5c263a2f-e7dd-11ec-b605-df0040e90b76 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e7rHyLtlrpWi5R0XdyzjaEKcOE7LTFYo+BWVNTEciVbNS5hjkWdVxWfmbfCZ+VDfVZ/AISGuXbDP/Z+rdPcWEWAR19TMtH6v/svZ/gecHsnH3/B+j4Ej8eISkL1S7sr44ZsDVYdB+3pV/ZCaoJYPwoCIHFRJFUPK7CawlrgkoE+lwcJ9Kqj31hd+33jRkcIxOQ+jTTVfcsgPuwPVb02F4nuRgcXnejTj7910VWRqNROzFcjqrZrOcMH59s6ZtWByr0dcXDyojJ7JrqHMkCVBCt17hFwHANFoEeAggN9JFXDw30G4kbP7iCoofyTZuyEQLG3pNFzQfLqPgGIiHTnZlQ== 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=g2qWJv01a2c4+rYUCaFJsVUw5bwP3FyDZ7zpU5yOzbI=; b=jHl1LIl4L1BbsWyarVD2X7Od4GSoygFZZHEzJvBwdW1boDqVmjEwpV+j2axBvUcd8/xD2nm8t1uavUJ7LyQuGiKCGOwLjC/uK9C25FMeQK5MjGcrRUn+mn7XQZRKwE1yFoIBJ4twYxQmdxyS/wCBazQP1N0rCwF3R3GUITNWPHj+Xa0u+jHUaxYVlzc4xMim3FwYcNltEV8n0c0ccpTEJveC7MG8trjglMX8vwF1T9NttuUZGg2WerqCL1bAkPzEt5VkPL0T3PGchLPh+b5RF5nhh/iBEC6VUwT0f6bxIBzAVeCYDy1y0IDGPNDu+nDUw4jCuVFQUnKXPWex2VL9cg== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=g2qWJv01a2c4+rYUCaFJsVUw5bwP3FyDZ7zpU5yOzbI=; b=rsxK6MCO8GU9dgOs7R/kYyqbOaImxEuTQ5B2Kvd63TNjSrQmObEh5axSxE7RmePJygjT7hei+v/0YLjR4eQ0rILfZvsrYAY5oebEVBcvyfLEdqB8n6ZnxcipYUHkcN4cGwWluk9CgDSmTNSnsYixd+pUGkW5LlPfLLAKTqDGQ+HHUeemK6+fwlvxZkvmD2N2IMdqGT104AhOrSBqs4HPo4j1fgntW1Ke07Asm8scNQ3qQvgvc3qX83WvGWEyAch8h1iwbffNRUctO2vpB3gHSc4z21V/HCrYLxSlfddrleWvzWq5CbYgY0XM5n1bFPAubAVWJP9kLNaXeyNoGtiNdg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <99086452-43a8-2d93-ab4d-0343a0259259@suse.com> Date: Thu, 9 Jun 2022 12:17:23 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 02/12] IOMMU/x86: new command line option to suppress 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?= , Wei Liu , Kevin Tian References: In-Reply-To: X-ClientProxiedBy: AM6PR0202CA0061.eurprd02.prod.outlook.com (2603:10a6:20b:3a::38) 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: 0b7d5f78-bdeb-4b7e-a227-08da4a013fb2 X-MS-TrafficTypeDiagnostic: DBBPR04MB6297: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: oEmrcC25lCjyViFTbCFB7piXd1+8puPHZ73LujkTIM6HrlMlBqJvi0FNwFoixdQWqBxatpejFSNUnzM3gXr83oUo1jUXG8e9mvvX3dX2+YwyibN+8zPcoYc0BxFer2n1AKO1rNYC9+Kecpe54T2NNsfoDAN8p1ifrNRpxxQ4qEHUzC4o2e2Hzw0rjxMV1CK+BycksacoBi9KoXv4SahomNSL0k2iIDTSIpwrwInmf3nswRp7pzaBshA79HDVr7n4Y9L+7Q9U4h9xq8d3R5bs6ymOhzsYYLbT4O1x/KlPh1LWBwi9VMGRLIjs8L3HrV96kq4MakmwTTppTjyQm16dMj7RyVvbsJXA0jqt1v9ikKegAZYDHBxmQdaUA2ycn62zwo5v2i72ICYyYzYiKYvraJSQInWO1TVxJfptKi6wtaRYJKOqViZAed1aoPX9L07t81eJbx/dBif4Vwqz0LgLvgkspUbKGRxQQpu1pdNaA/IddTvKWJa6CUgAgV8+7kPW9dYa5haXeVsoVS+AfJsD4q6sKFdzfo1LI+uVZE88D3ZWHbfdrntrUoyr0VkbUYvJxPX1rt+w9F0ZoS35/dtdgh49maAmVGnXp8ErFMyDYpsRZWenpH02L3NSJZRoZWdyT7Jr+AM0KWey+uE1w0BYTrt5t7FKrKI4aAE1KDkXDumCqpeP9oQY81qfeBVGbGxU61orYChIGO1I4Oebq1FN63PMaTIJEfNdhcAMgQSDVDjbNknHMP038EeK+iDL3bAj 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)(2616005)(36756003)(83380400001)(31696002)(26005)(31686004)(6512007)(6506007)(2906002)(4326008)(54906003)(8936002)(5660300002)(316002)(6486002)(6916009)(38100700002)(508600001)(86362001)(186003)(66556008)(66476007)(8676002)(66946007)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?GBcyc27EvQ0Vej0UsugSq91fuDEs?= =?utf-8?q?h6RqHO9yoS8Fw7VMdomIPtoaPQk/AuX8+5q2Gdn7Fjp1N6AZY46a1vpKROsEMqRhO?= =?utf-8?q?pU7rZ53TkN372lh2owaR+WRKiyK8/isy3os9wuHE5s454fsq07zucw6pGQFKjhM/T?= =?utf-8?q?izgQDv8VD3VjSkZ68/GW1lUCgz7z0s+gSqiJ4UtuqRQ3Y0CGYHJwQsuUGuorC6QXL?= =?utf-8?q?5Tet2Y5MicgWdDNgW+pZKrLwtIznjJ2Zq3q3wY/vjjFZV5mInX7NZlG8N+ldgyIHL?= =?utf-8?q?VjSPugA/PSdP57SxCYzkYpxJxiQ9Zjc6T0f8JQ/L5/S2S6Bo3+GG+JsS/pMRVjs6g?= =?utf-8?q?RsIAX8meVS3O+JB6ILK/yFcJ8+FutccZcmJaWT2NV6LIn3YEcoB3Y1KWYZx3/pFfT?= =?utf-8?q?H1n+6+A8fN4k3Ni6zDvJV7I08e71/ElnztFZXI82RSleR/mYzPbAc7SbcmZBJMTJE?= =?utf-8?q?i0A4PWrKqvVDWGHH8fgZW3HbCH1Suju0dm3o6lFVVz7BTbDU2E7OQl31mXj0GRLeL?= =?utf-8?q?XbvTwsHoJ+FzSy/cm7FWCjhIMwwTkuZwJcrB6JrVKObv7B8kjMaz5RKk2zM/YEQdt?= =?utf-8?q?J5jGQ0zbttr/Ow+yazPbRAxMV6md5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AtL3+/NlR3XYXQJEGpLt?= =?utf-8?q?DuBi9H2/dCkvtScsMUM8ClaB484pC6Mo7XjvN9tC7nUVgtTThNnXiYUHbzXJjGqjn?= =?utf-8?q?lE/bOpMnWz2yT5RoJd67W6H/MTYuSOJgShkyv8zLHo0GTUmUA0P82ysJN0Ib3NQSP?= =?utf-8?q?twR7sDECO833xtoBkgl0mECdymudCrJg=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0b7d5f78-bdeb-4b7e-a227-08da4a013fb2 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:17:25.1846 (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: yz2dJUtuY39TSqr5sOyPWELuQHHZQ3pI5v2/1N5qV1Nz/GupZVBZR/om0UjH+QDRvwKU5CqpbDaWJ5SaBuN0KA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR04MB6297 Before actually enabling their use, provide a means to suppress it in case of problems. Note that using the option can also affect the sharing of page tables in the VT-d / EPT combination: If EPT would use large page mappings but the option is in effect, page table sharing would be suppressed (to properly fulfill the admin request). Requested-by: Roger Pau Monné Signed-off-by: Jan Beulich --- v6: New. --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -1405,7 +1405,7 @@ detection of systems known to misbehave ### iommu = List of [ , verbose, debug, force, required, quarantine[=scratch-page], - sharept, intremap, intpost, crash-disable, + sharept, superpages, intremap, intpost, crash-disable, snoop, qinval, igfx, amd-iommu-perdev-intremap, dom0-{passthrough,strict} ] @@ -1481,6 +1481,12 @@ boolean (e.g. `iommu=no`) can override t This option is ignored on ARM, and the pagetables are always shared. +* The `superpages` boolean controls whether superpage mappings may be used + in IOMMU page tables. If using this option is necessary to fix an issue, + please report a bug. + + This option is only valid on x86. + * The `intremap` boolean controls the Interrupt Remapping sub-feature, and is active by default on compatible hardware. On x86 systems, the first generation of IOMMUs only supported DMA remapping, and Interrupt Remapping --- a/xen/arch/x86/include/asm/iommu.h +++ b/xen/arch/x86/include/asm/iommu.h @@ -132,7 +132,7 @@ extern bool untrusted_msi; int pi_update_irte(const struct pi_desc *pi_desc, const struct pirq *pirq, const uint8_t gvec); -extern bool iommu_non_coherent; +extern bool iommu_non_coherent, iommu_superpages; static inline void iommu_sync_cache(const void *addr, unsigned int size) { --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -88,6 +88,8 @@ static int __init cf_check parse_iommu_p iommu_igfx = val; else if ( (val = parse_boolean("qinval", s, ss)) >= 0 ) iommu_qinval = val; + else if ( (val = parse_boolean("superpages", s, ss)) >= 0 ) + iommu_superpages = val; #endif else if ( (val = parse_boolean("verbose", s, ss)) >= 0 ) iommu_verbose = val; --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -2213,7 +2213,8 @@ static bool __init vtd_ept_page_compatib if ( rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, ept_cap) != 0 ) return false; - return (ept_has_2mb(ept_cap) && opt_hap_2mb) <= cap_sps_2mb(vtd_cap) && + return iommu_superpages && + (ept_has_2mb(ept_cap) && opt_hap_2mb) <= cap_sps_2mb(vtd_cap) && (ept_has_1gb(ept_cap) && opt_hap_1gb) <= cap_sps_1gb(vtd_cap); } --- a/xen/drivers/passthrough/x86/iommu.c +++ b/xen/drivers/passthrough/x86/iommu.c @@ -31,6 +31,7 @@ const struct iommu_init_ops *__initdata iommu_init_ops; struct iommu_ops __ro_after_init iommu_ops; bool __read_mostly iommu_non_coherent; +bool __initdata iommu_superpages = true; enum iommu_intremap __read_mostly iommu_intremap = iommu_intremap_full; @@ -104,8 +105,13 @@ int __init iommu_hardware_setup(void) mask_IO_APIC_setup(ioapic_entries); } + if ( !iommu_superpages ) + iommu_ops.page_sizes &= PAGE_SIZE_4K; + rc = iommu_init_ops->setup(); + ASSERT(iommu_superpages || iommu_ops.page_sizes == PAGE_SIZE_4K); + if ( ioapic_entries ) { restore_IO_APIC_setup(ioapic_entries, rc); From patchwork Thu Jun 9 10:17: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: 12875166 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 C9A1FC433EF for ; Thu, 9 Jun 2022 10:18:09 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344878.570492 (Exim 4.92) (envelope-from ) id 1nzFEg-0001eh-Gw; Thu, 09 Jun 2022 10:17:54 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344878.570492; Thu, 09 Jun 2022 10:17:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nzFEg-0001ea-Ds; Thu, 09 Jun 2022 10:17:54 +0000 Received: by outflank-mailman (input) for mailman id 344878; Thu, 09 Jun 2022 10:17: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 1nzFEf-0001eG-3a for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:17:53 +0000 Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04on0629.outbound.protection.outlook.com [2a01:111:f400:fe0e::629]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 6b31da17-e7dd-11ec-bd2c-47488cf2e6aa; Thu, 09 Jun 2022 12:17:52 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by DBBPR04MB6297.eurprd04.prod.outlook.com (2603:10a6:10:cd::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.12; Thu, 9 Jun 2022 10:17: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.5332.013; Thu, 9 Jun 2022 10:17: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: 6b31da17-e7dd-11ec-bd2c-47488cf2e6aa ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VuZwRaVZOZULWr7YNvcOn45z8LRSdDG8yBRnBcjztnBO3ZFkxurmrOnRvRZLK/T9Jz5KFdgxYDxch8Dyt2gJDkXi+tONpgDCu+k7HJSN6kLnHbV2zei5Qc3ME8LizT/Ji6Hhm0hRK4JEufFbMz2aUKkUTBixvwfexNswa10hHoDwGVEXuvNZLxKt7gAIX+w2vacTs4EXfhnFLBdN2KdFbeGi9EBF0kO89nU6LKfiIs7yYMbZf4+n3HNIbsMmBEpDQxvPvkwCpJ971qjeDKg3wIvR50OcIwgQa5wH5vix9qMOixSphQHzvI3kEy91HBzj1ch30l+4sqoh9CKlHqVrGQ== 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=BH9AgLBo4YDVtaM0nrNSYwAoL7RFOTOC/JcQKuxsHG6NkMy0JJZmP9Wmr7jZYhiDh7f7WBEYAbTySJ+0tEQx4GShuMRIQ7iMs3t02ozinY12KlzCefJ7akvjB/cYCvZsBn0442pXRrzK8u0AN3dLRYjkuOMnk00K1EXLdPcdfbYTY3NSfTpChOL8RvDgPs2hCp5kjDKQaSDNOmvNssEFfX95ndhdN/eytHXc+9E6ob6ETKBDSPJoemcPXBY7YJ5bkzaorRdhqoyANWR+lD0lytG6N25SbO45prxD8OuKdAKcNJ51JVsVU8cAhO7QiQdZiEXhHlSdlYPfTSH984JgGQ== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=AxiRfgvA8ZTM896+3sr+fZ7fI2k/BkIKHoJCeGpTy2A=; b=JoG35Y0rnua6N8FOZ3UAT/x5CFgDtE1kqBy4BqzYcTQGLT9HwFT/OGueEwtXOYw/GbbfwBIVzlWckVy4ppI3jbIIenmiI2PNi6X2n9xhvsAOjkJTPGcX1slwsQsyL4Tqf7NmGXWDodK68spENWCH/1Cl6cOYEAcglR1LNlhDKpsrhU6FcCzF6zQ9292vpgPhN9hPTC/98c2t7COL5U4ozMRIqj3HC/bEdDK1yfV/t2MwYazcdA/3sFCnv7ltDTtj97U6dGOKdz98KgwiRvkXjNG11JRaS6mJGyH4b8Akrj6XGuABRUORyXwSc58LKEkPc2fkPTiLuzvYyY/WdQ20zQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: Date: Thu, 9 Jun 2022 12:17:48 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 03/12] 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: In-Reply-To: X-ClientProxiedBy: AS8PR04CA0009.eurprd04.prod.outlook.com (2603:10a6:20b:310::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: d7e8f5f5-7357-4601-53f6-08da4a014e56 X-MS-TrafficTypeDiagnostic: DBBPR04MB6297: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: 3R/Q6DIY1wA4zQqvgybq5IySLmywL9Vj4o85gUEBvPsvetMRPm03pO24wliT92gxwQLPEv7UP0AUdiFxf8UeW3eUpQXUQ0hQXkWkieBqKtG6ha2CxqE/yM6rkhx3bS6lpK2CgzEts70w2QFNN1zgq+ed3iDyXYaJjnDNpQKeRIphXx07PLf+/tYTplecZB17toauAatRf+adeWqpFL9KS050YuGBU5CbFCXVObp5ySqjYCdzQKgfBQbZS4WMimt2Il8hHVDXyTvVemBy6SkQfQdGCJm9VG9N3e4gUKVLKqqSnY/t4kXJNqNMvZN0aOCym+0CYTTzYWzREw3Y4zl/pkUeLpLgWEtTxMSno0D3I/sxjVEsSFhtWBDFO8Y8d5bfQAT9HJdznwU7znR6jko4QJhHNRqg4a3h4tKqC/kwcBCwKT+GCpa1TrMmtL+vzKhGyR1o7qlF/skIqq88LxunGjXV3X/u7uTeKqE937a+4gw6hfvtfUO9nMwy8W3ki9cIFoZJAx6BE4WedVt56r/IZAyc3k/5L5o+5KEZvaiMwwbxnyoxMdI6inqU+lKHhfLytFZHYm0bigpkVgb2PdO4wi55nV9rCP0oSjcxxnDPs315+H64aEjWShnWyQyK7ax7qDjxGJDnykoDWVjw1t7xC0DDbdkR7q9h6o3rRiL4gGRHoIuEOI6OjxzBz0h6OFedihAGKMglqRc3gNCjjFOU/WSBtHZklFeTKHAgC/Rvc/o= 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)(2616005)(36756003)(83380400001)(31696002)(26005)(31686004)(6512007)(6506007)(2906002)(4326008)(54906003)(8936002)(5660300002)(316002)(6486002)(6916009)(38100700002)(508600001)(86362001)(186003)(66556008)(66476007)(8676002)(66946007)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?IRldYAneXZI7gBnmeNwmULvysmHz?= =?utf-8?q?Ptv7t0pRZ4mB8KGORLw4erkj343Zihj7dtNWnCuFKFud63weyKbi11uaqtwT2FmWX?= =?utf-8?q?DUgpQkNlHe6uQif1uW0bUN6PyYNpFHXt1hulEKgHomGaNiyj+BQIUacSNgKt9MMLQ?= =?utf-8?q?1e4iVyH4LiV+qfvTtKgag5dLR8HFf+7ECfJ0KrQZfTZCKK5/P6vSvjW30RK5nhgQ1?= =?utf-8?q?gFk6n72G2ttX6AovaVjiEX16Y6aLs9o85AmzwQfRwZyjCciq29QRMU5mAIqYz+fGR?= =?utf-8?q?D+x07M7chFybLXf3cDfxRBzyWavID/d4UtK7Holb+RVah5UwfGb5CJhcugdtpyvD/?= =?utf-8?q?Q1Na9uT8ht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ZAaSuv7gdEnYB/otwwHdQ/9kUqJXj4lQbXyefUGV8cpceGXxxgdbT/rYpYW9Or?= =?utf-8?q?kgTTLZ3Bp8Lu1lvFqd06HDRrpy3kKLvTtsOsDt/JXqp6aSsu8Uzm5NQ2IEK0blt9i?= =?utf-8?q?7fLNSIRMJ6kiOi8aX1+8DfpEMpZAyhnCVIZL5DuaPiQerLVPk1m83WAJ4VGnslPfE?= =?utf-8?q?EZPYGe/fW/ZpomJjESqhBFAdaJsiqyQ+H0HNDPpHJy5/5tYxbGixMR56dloaEY8vy?= =?utf-8?q?lgpIsH42AfMetT7qlCACHCs/OX/9gaeHI7dQlC42c43KOr+QkauZUNxQeesaPEq0F?= =?utf-8?q?/yxfeyd62tc+Nnok8V3rH5H0WaLbkxkB5Ys9E3Dwz+AsBn8CHMvPPJyrA+zMypXud?= =?utf-8?q?++5R2O61VfQvOAwuDHDErgQLgDLLb2rbjSyjyM8ClJqq8637GcCX+AjFs1a8cWH8p?= =?utf-8?q?KJykywPveAt/L1QDWeeeef8zDDOm6Pag=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: d7e8f5f5-7357-4601-53f6-08da4a014e56 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:17:49.7143 (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: x7YmCFx/gyE4T16TDgIg23liYij8Wh85TRRphxCr/8f08HvvHFfGKdX03gsQeTde/MDa3+BHJg/Y3GHesgKxLg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR04MB6297 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 Thu Jun 9 10:18:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12875167 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 106B8C433EF for ; Thu, 9 Jun 2022 10:18:28 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344881.570503 (Exim 4.92) (envelope-from ) id 1nzFF1-00027r-PK; Thu, 09 Jun 2022 10:18:15 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344881.570503; Thu, 09 Jun 2022 10:18: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 1nzFF1-00027h-MF; Thu, 09 Jun 2022 10:18:15 +0000 Received: by outflank-mailman (input) for mailman id 344881; Thu, 09 Jun 2022 10:18:14 +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 1nzFF0-000264-9E for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:18:14 +0000 Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04on0611.outbound.protection.outlook.com [2a01:111:f400:fe0d::611]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 77eaa28c-e7dd-11ec-b605-df0040e90b76; Thu, 09 Jun 2022 12:18:13 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by DU2PR04MB8790.eurprd04.prod.outlook.com (2603:10a6:10:2e1::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Thu, 9 Jun 2022 10:18: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.5332.013; Thu, 9 Jun 2022 10:18: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: 77eaa28c-e7dd-11ec-b605-df0040e90b76 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DaR4wWkNvCjzOLg9rWoHFv8x0ogd7WgRJk9ugUcN35Y8HKoGQsEIYNP4mjuPLPXYIBKhF2FEqT/h2r+LFRuocknzytZfla8rHy7vN4dR+s/clp53R+0mIfXJA5Sm4r2zPty6IG9jOhvtkcpKq55yaxVEN5+k+DWSz0XfeSmctU1NjTuGoBNTTHtyj5lfsVCULOGhZfbjl4bKAu1l2w+Glsybz2WHkgg4dzOEGcavuijf8YKpuV2Yl3P26fClIjSkyPCTzE+J9y7MZV16lu1qIqntzt8bRasakcCMjuxcRowP0Z5zFMpO/ENv9eh2YqfQkMX3I+QTkof/XbRQuNAn1w== 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=nYq1b9sHpYOMu/7plOnkihexpK35czv31oBFyEyWWuo=; b=TVyCmsdizZX/7J0a2+6kXQOhB8eLu4WuTK7bz/DYYsaEyWy2CCPRlVXzg7H7ndMhz/y+RJg+ycs4hJors+QQapL4rW1Vy84vD1/y2D5prxbdRYqWYn/Ox6AX/LLt1oR5lf9OLcrWSvQhQbew6S80vgnhBSRgaZaMnDNfYJ4UESsixU0nflJO7vMwOwJE6bMY+2B4cH+QZ4Tbng9eAfth93aqv2RYzGFIP2IgRzhzpPPXjhWU5MNVf531EYxMnkltllisbl7gyb8WXCnf5k7hUX8iN/ZOZCPGtYidWx/9Ds0vNOqxdpJ5nnCJaHuSBI1OpZiS3bT5pvRAFuwnWwwfdw== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=nYq1b9sHpYOMu/7plOnkihexpK35czv31oBFyEyWWuo=; b=bEtwslfUSOAUr720RO8IdKRPY7ukKrOKcfw3p3S/M1HG2Vf7vK0kemxLji0C9O/o+eLKYHp5Q3XLYq416rBiuFmZsvw7k14ygRoqtesOPBYwQVagixG1B6Y5+WHH67rQKOS0pxVvcgFpB/OJ5r1Pi24becsqJI0XmzCA4PLzNdIkqcbLzALx2F7/QDL9J0Ve73TEmXbSEQwIaYTEmjcdq3/bEqfgx0/ux06ZfQHCCTQUrd31CyZGXFoloW7aJ9YQNpgUtoVVMfCvkkGvy2Mw5O1KOaFk0A6zAsCmCEuEWqKba6L0ECyGEl6HkMVuL81JMvyc5DpqnbExCEotYckZ3A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: Date: Thu, 9 Jun 2022 12:18:09 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 04/12] 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: In-Reply-To: X-ClientProxiedBy: AS9PR04CA0131.eurprd04.prod.outlook.com (2603:10a6:20b:531::28) 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: ff67f553-29e7-400a-d708-08da4a015afb X-MS-TrafficTypeDiagnostic: DU2PR04MB8790: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: rcDKphobPpyg2XD4eV+IHvRKo/IeKj9+MWjKXYjRjUI4zcrpPYjtcKQHJSUNgBi2KIa0xWB1OxPD2v34XQfEAb5YnqIMHCp6SNaNpGExOuUAXfEZYVyZEJ0xHKrpPQ0CMu5bj9ZiTPDOstyi6c1+VXY3ROvA8Tol34bMLFjPXVpLU2N9oXEwPq1kXPH+qMlNxpuL5SmZ8XZWdE7AcW2Y70LLnSP1WyPZsgGigXo8ZTfyUWcxq3kDpc7Ky5Q5z+eZD4GkpB8R1eFrPysp9/N2U1taJulTN+Y6/TganLXK6cOv0a4r2kSs/VTryu61K8P9fGhJAmzuFUCBfv1hWJLUjuiiW+I/RtVn1aZ0k/qjZLTZenNCClkx5iRV2t7iQqg/UX1OGZl80AqoiYq7pLBTwPpIdIVH0uyHc56WCBt0Zzsm3l6Drl+0f0BV/XWAIgywXDVTq9jotdhRC6RdKO3I5cCMnNZdwwVNZqrg1Z2U/k96cPVMKM0iY220M3nQFqiAj1nY2JoUcUEC8VPXMEAK4c+aiukzwpxsZPrZBx3LGfayBe0HLzzazZne4BLAUvUshXjLSbuvhBvGejwxKrtNQtqBgSZMDnF4GAGvhiax92sXMHf2Asg72FQBjOOZDXsaWN/0rnB7Bqcm9u6u0mAxZOraNcOKdqYe9Y6wuQMmq7iC6OVWv88P3sMV77c7776kAV5r1+flklwxP828MBZSu+wGHM+PGTJAHmU7HL2vebluL0VIPpmNMA3bfzTWTGmE 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)(26005)(6512007)(31696002)(6486002)(6506007)(5660300002)(186003)(66946007)(83380400001)(2906002)(86362001)(508600001)(8936002)(36756003)(2616005)(38100700002)(66476007)(66556008)(8676002)(4326008)(6916009)(316002)(54906003)(31686004)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?HS08EmAFKSxeUqGswrkfhpa0x3um?= =?utf-8?q?gyKu8PRMKOvAyUKAgye/hOf3PDL/n3K0pcg0JvZaQCyvsPeYUNIF8VMlX54b5vHXI?= =?utf-8?q?qPcRiMRdGkUUjxZhvSdnyVzP8kS0zbXibPZ9Ic2BZc82p6LGHYhJVotcJqZ+mDdcO?= =?utf-8?q?4P/wwXygFQLU0zwdqNdel/C5Ze/GFFVUO9HXjRXxSpMM/Wn8PSGkQgD/nvJphqVAO?= =?utf-8?q?xQEDk+ErDyL0ZMrZh1Qg8HKJ0B08PuG6sPZx5h9i6JpDVYvqDeL1hjA1LPEvVtRvo?= =?utf-8?q?HsLDTwmBMgEByRqnTqlvxIoGel/PbkOmCmU+tO3ML1fTOv4Idviswl5AYnx1T6eA7?= =?utf-8?q?ywQVAIbuZEK6hlPo8sBcT2aebmVtwSv2UnocUa401Il8kAPWKrKoBJMFMHss2R7nS?= =?utf-8?q?k1fJhEXjfsjPRjzPqHdzA2/xNmucYzm1hYEztnydlrW1Lofc4ebp1GZnMpzpWkwhQ?= =?utf-8?q?A0uAZKO/ADtL28II3zop1D8TTo6qyL30qJG44jp+jvU7sBVLzkhYHx8TrmKODeFMW?= =?utf-8?q?hq2nln8iFwLAF8+HzxWvHiq5g19Znnz4Cg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qKEZVGy9sRJLn8BbX?= =?utf-8?q?XHXsC7hIU6Wmc6VTfl0kgEeowg9oJZbEw3wLZ/TeZKjxXU1k0QdjkSa1ydjAF8qLH?= =?utf-8?q?My7TaHYlW+ID1pBpK8ef+CepDpkRlHmM6FU2UqCu+PNLVztRkaWzQduzLaKoTGH0a?= =?utf-8?q?W7I+8sTZL97HZjEXHX+HWRoPJHAWWPsp2+9jOTgRBcX0IVeMYLehSiUPhxt9TW4l3?= =?utf-8?q?wh7CCWBlpco7xgvXJZjzyjrqo6Afi7Vw=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: ff67f553-29e7-400a-d708-08da4a015afb X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:18:10.9629 (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: sUs2cUBD3ZPyLCVKWVJVXpTHP5n+ceV5RpWJk2WWBtvofpRjs1k4TN0t6nrYvjnT08S4LWf4TOB0Xo4QSqCS3g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR04MB8790 ... 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é --- v6: Re-base over addition of "iommu=no-superpages" command line option. 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 @@ -779,18 +779,37 @@ static int __must_check cf_check iommu_f return ret; } +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); @@ -798,7 +817,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) ) { @@ -807,14 +826,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; } @@ -2092,8 +2117,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; @@ -2116,7 +2145,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 ) { @@ -2125,13 +2154,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 ) @@ -2152,14 +2183,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; @@ -2515,6 +2558,7 @@ static int __init cf_check vtd_setup(voi { struct acpi_drhd_unit *drhd; struct vtd_iommu *iommu; + unsigned int large_sizes = iommu_superpages ? PAGE_SIZE_2M | PAGE_SIZE_1G : 0; int ret; bool reg_inval_supported = true; @@ -2557,6 +2601,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; @@ -2628,6 +2677,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; @@ -2960,7 +3012,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 Thu Jun 9 10: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: 12875171 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 B6CB3C433EF for ; Thu, 9 Jun 2022 10:18:59 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344895.570514 (Exim 4.92) (envelope-from ) id 1nzFFZ-0002qj-7k; Thu, 09 Jun 2022 10:18:49 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344895.570514; Thu, 09 Jun 2022 10:18:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nzFFZ-0002qc-40; Thu, 09 Jun 2022 10:18:49 +0000 Received: by outflank-mailman (input) for mailman id 344895; Thu, 09 Jun 2022 10:18:48 +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 1nzFFX-000264-OF for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:18:47 +0000 Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04on0625.outbound.protection.outlook.com [2a01:111:f400:fe0d::625]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 8c3db64c-e7dd-11ec-b605-df0040e90b76; Thu, 09 Jun 2022 12:18:46 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by DU2PR04MB8790.eurprd04.prod.outlook.com (2603:10a6:10:2e1::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Thu, 9 Jun 2022 10:18:45 +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.5332.013; Thu, 9 Jun 2022 10:18:45 +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: 8c3db64c-e7dd-11ec-b605-df0040e90b76 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KzbRGGGfZwiRpIwszqMeYG8MPArqTM5DUedH7Qsip/Lvz9fntrSo5qK6j3Y3+iNMm7flGN8k5VZWUcmNjAVHOMpFjYdxJNH9a3sfksECBRoGZzNOOUDvichbN0SdFfgcht3B+Jb3xVA8dM1h5C4ECSW9rKmJatknbn2g/uIsalNo65OSOQvSxPYcUpcRU7Eik3kEIJJEDzt12bgHFWCmg8J7hcIHy+z9VQvBWJgx7aHlcxedXqpuBj7D+Fnems3v20HzUtx5tOWT8t3ClO4s8zxuFlzLnRuPpkh6yySlTS0LHnIszx+TYIil4c7zpP8/DA+PB/MARAVrmIxAdxC1hQ== 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=nzH6EFMW+e3+Xs7xettJZg30tav9rjlAdUzEqGTjlFU=; b=WZq5xNk8k/aZ3WAC/pYR7TnmZ9xZMUSFIMmIaPf57q9RAoDl1q3HjOqTpa8QBgos0DCAKwF1OsEHzHpNIdDh1bDsLbJiAVmZMOUox8USRh5GUdERceYfuXJUbOxUss4dtE2OLJF0NVBHkO3jGlma14Kl9WmfNgNzUn4flyvGSc2bPhBYVA+H710l6I1boKDcJT9MctbhldV3gbiPyAkxWw9YgYLczhfZlQjKPHrJ0q5e80l55A32363DYcp9p0IjfUlZOpuSmuoQW4LpqLPXUqvGUSuPItzbeKdOU3qzm4CpMqF9XHfMLafTLzwC6eAL2eaHYyREMLyLLj9Tvcbpww== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=nzH6EFMW+e3+Xs7xettJZg30tav9rjlAdUzEqGTjlFU=; b=4ZIAI3IbTXjtYrTEodr/B8hdhaiWqSTIbRjV4Z4RitEQjM5oW1qIzcdvimWGLFdUreD28VPzgpq8m6BSTjTgUY9VlYtFkny9QuQ1zQ+pwvWQagjc0LagpCDTP2l/CSJDnJcYPiR2R4Wlo4pZHblefDpvKMwJBNwCDH64AMOvJe6kWFJoRptRvz+cVo7XDMI3DM1XPStpgfFP3EQ3SJLsiOKDPP0GqVZHjeJvDd2+sCgo2KsL48r02hJvzFP3OgTt9JIZzCJ4Tmhvn29yYVyCpVEvZqCIl41QjWP683KmclSEmq2vdajNomjcnm41+Rcjmu9h2XNJ8xOFbAkopo0zAw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <4932f8c0-26e5-4289-ef7b-6bba27a9216c@suse.com> Date: Thu, 9 Jun 2022 12:18:43 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 05/12] 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?= References: In-Reply-To: X-ClientProxiedBy: AM7PR02CA0029.eurprd02.prod.outlook.com (2603:10a6:20b:100::39) 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: e503443c-0bb0-4455-9fcf-08da4a016f84 X-MS-TrafficTypeDiagnostic: DU2PR04MB8790: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: 5MoHlvozFHMcq9CkQ4I7r6cMveOku/ksD7Vy+IoIccFLbt5RgtEZOyFAIACS3pQsuqyHUHsqLcYnguZC8Q228BrumWB3OX0jEMAtmJcRBoFVhhVKtpQR8jb0fM9Z1VNEfFwD4S7SpJl77AFzCn1LdO++RmURUKfd2qRv+ZZ4Nx3Jv8WJlWWw86c2XyB3wpwoExRRSPxkLwhbnarTqnO7Jh8jB5y6wZRA/CjnvY7leO3Hlm9cSI2J3boSpiyL5dbUILq8mG6gC7mzcu6Lx0hOk2aPlb1GtsMzRfG68wK9FDL6+IB6s+EOlgswN+9KD1Xacn5i9IlGDPvFO2R9MDWpptJw/6ZkoRtBO6W3hCBMmMy2FRG4M/N6TVT6lNBn5j8ya/HaH/SKm03MVTLqpkYb0AkvisBUps0xbfKyXb29yypNn4Fx3V/1fEjcobY35M+Nk4ztZx4cI/YD9VwSUAkBuqjx0snTk7MUPhFzcVv/s+Na08gj27xLVkK9sVyr4I7GKtXsPt3EGGtvcZ3DLder9JnPgmrNLTAEvOXcU17XbdggLvpdSt/UwR6VQzExtRotxQGc6rxtcW7k4nKheYkkIgNvFPdt19QXqEy5HrFNgj0oV0DdWkD/1uxw4tTk6xoOXogaIkFeH4Ne6xIlAqa527jIkVVvxUrqlfIyuH3dzIsamFbsFQKkYIFavbQhEj42yN7xGNEXvRTvr6c2/N/haCUVhEw6c5qi6+k3cznW74XQO5gRfWLqomA6DQ+ggSGB 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)(26005)(6512007)(31696002)(6486002)(6506007)(5660300002)(186003)(66946007)(83380400001)(2906002)(86362001)(508600001)(8936002)(36756003)(2616005)(38100700002)(66476007)(66556008)(8676002)(4326008)(6916009)(316002)(54906003)(31686004)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?HLZFygntkrlgxJJ3SJ75oeBUZwxm?= =?utf-8?q?93Y+/F/2N1IOXUgk8fh8/04xyT9J2c6s6jl21FDnAm4OJ9oyln6HI3wTCMTWDKUYW?= =?utf-8?q?8GV8lP56M2x4weKwgVzAOTJ1phNuGk+ef/TR3kL15VGaTZx7+PhDfTPMi5lE0qrYm?= =?utf-8?q?0Ugdwtq+Peh6nZYFu3luavYRqOicNYYBCjGfhBcmulu1NIYiicNWzCyqlYnOtZTOx?= =?utf-8?q?aalspUVymN9WhRtYt/v/CdKy6fpYxNtbBeCy3ng+v2pzhMAQ4T7bJNsnbpjEVzIgI?= =?utf-8?q?JGhekBhRLqfZyO6U4khrj8pj8eFQ4XSBn4i5uDdyoiDNAEvY4XBA/l4VYbV26rjfp?= =?utf-8?q?x0try9TXK6DDQMyncdW9lXOm2oxbNrh5WQaYJ2b7awEE3spuEv547yS5Vg82yYJXO?= =?utf-8?q?qH0S1JGc3zfr3IBnwJl7edW2WReCOMAKyIRooFkexhDZnHI3RPYutl825JBkifLsT?= =?utf-8?q?cMvyKk6DriAipumZ/p2EmCO/P7J6HhjUZj+Kb5yY11zQIMg0ZlRaYPlnS8aZMrxVW?= =?utf-8?q?4ik5Wm0yLe37gLOYy+1XfNeGU1//0vO8dzbk1d6QlzSyt/cazQZAk9D+icQZpjYOT?= =?utf-8?q?8bRPduays1Kj242ViQ2/qxSAu0nwF3FJJC51b8BhOEKnExhGM9EzAm1+1ShI1Qj2V?= =?utf-8?q?i5AP6jF8OuGEjksN1/aAi5LPEevaRGD+cYQkereJ0OmtHimoF/WCAQvwEx5ETfsy/?= =?utf-8?q?DBgmRaIILXgTDKw/M5vFgEeOYthJjr9uEwHB2YF9cR+ujnjEcdVSVyUO8ETs34J4v?= =?utf-8?q?7HK7f5A1JMIyNKTob7dYiJtBlBhzjtpnUT1HCHSei5dEvo4fQ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suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: e503443c-0bb0-4455-9fcf-08da4a016f84 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:18:45.3981 (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: ozcmZ5vgmGKPwYSYcnY6JQ3eJ9ICOyayjUBhirOlZEyIgVol2GxN6TZ+rkZN8oXwO911m/h/2HNJzkn2AlrXqQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR04MB8790 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é --- 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 Thu Jun 9 10:19:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12875180 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 15E7DC43334 for ; Thu, 9 Jun 2022 10:25:48 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344958.570613 (Exim 4.92) (envelope-from ) id 1nzFM9-0008Vw-HY; Thu, 09 Jun 2022 10:25:37 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344958.570613; Thu, 09 Jun 2022 10:25:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nzFM9-0008Vh-Bc; Thu, 09 Jun 2022 10:25:37 +0000 Received: by outflank-mailman (input) for mailman id 344958; Thu, 09 Jun 2022 10:25:36 +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 1nzFG1-000264-44 for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:19:17 +0000 Received: from EUR02-HE1-obe.outbound.protection.outlook.com (mail-he1eur02on0604.outbound.protection.outlook.com [2a01:111:f400:fe05::604]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 9d8311c2-e7dd-11ec-b605-df0040e90b76; Thu, 09 Jun 2022 12:19:16 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by PAXPR04MB8559.eurprd04.prod.outlook.com (2603:10a6:102:216::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Thu, 9 Jun 2022 10:19:14 +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.5332.013; Thu, 9 Jun 2022 10:19:14 +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: 9d8311c2-e7dd-11ec-b605-df0040e90b76 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V4Tl17E8XHTZoVwz1PEI6v2e0gSS8GxWHxUU6FNPRDySPytFRDzHpERGfVJYm0z0XprLHQcz7WYCsOE9mnvrFH2HdT6zbxKwpvw5VzfbcF1CHjma/efbamOVyPBftMiWBKv3hS0QRFfC9ha3gqT52u8YFHvTEHVZp/IOzlsXITlXalEfHWNceL9bNOg8MPTOMMYMOE3NKhpysTrbF8bGNiPhopp2/zod5731Yy5QdBxNBijqje9DwnNnQsEv74q5uX6Ea78A7QaYkcUwyxE/XVzemLr3Bn+Bjypc1s8eRsE9AwjH3qK0DuAD/vW+cDICe3C/de3YSyrhd6JmHxAnHQ== 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=Hd1OcMBHkK7Yj2mkyRz7GlV2YWhhqUm/Sfm8Zi4SK5Y=; b=iW6VrXM12rVjjBVQJQWUy7fgy9UZACYQVxVvl1RYc+qebagTI9OafFdPi9aTXT5zs/Mi/T2cgGqDIT4opzVC/aR3TGYfXM3lQB63r3fZpOPHFYbLGlm93bYW9TKInDXS117pf01y8tU/ixaKm8EkJaHw3QJEFtTIXk4dRFrHKRpNA5y7+EE7KT1ewgFpDXo/idpk73k2+jJLf0y+qJzaysWDY4vc/0KcOmSFBOFG/39BANCg55+bCS2tae458rDb/AWsjDYtGGghrk7NoSwS28XQSuDA+tpSvwWBhjJudWuALDDt/4VChDKnrPqJFeL1DgAzr8J4wtPvIjkU+WGyJg== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Hd1OcMBHkK7Yj2mkyRz7GlV2YWhhqUm/Sfm8Zi4SK5Y=; b=eNRCsHzq2no7pRMrWKzaXWXjtYJuIjprLYlCyXvLEc6fFe/GpbEmKSj7vYjNcNX26ck73dClkA9PPBQurlW86bSUMEtV/LiUV7DwAZMPhP1j+DagqimmAymgIcNiaoHaZ3pxmRIpvj7Z8EwveVk9ZqIc7kUTVUpsfqr966cJVPliXDqApCx78vPTzE96igYW4S6KqEEqdIm1tKoNUY0HpyW86TgsruVew3aFzGDK/ZAiJkJIZaIl4UCORV11SxmsEWvsNcQWolrizatkMICIElJkW0hcb8g7lBoKoscd4DH8tZdDvvWKTyZU6ksXQUIhXe1KVmG7VXkUAotbhI0BkA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <5e0d611e-b96c-2aae-1f0e-f5cafddde77a@suse.com> Date: Thu, 9 Jun 2022 12:19:12 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 06/12] 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: In-Reply-To: X-ClientProxiedBy: AS9PR04CA0121.eurprd04.prod.outlook.com (2603:10a6:20b:531::26) 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: 12aa2a13-4c96-456d-8ace-08da4a0180b5 X-MS-TrafficTypeDiagnostic: PAXPR04MB8559: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: KnG7lqmCP//t8u/KZbcr+1apZ/keyGyqIZVeFMzU9N8SgqT2mBxNAjBII9lauXx0odzP6Y9m2x6y8eLvTMKds/SAsj0yrEUE9H9h+S1aFchUwMtlj/v/bI1+qoZRoSPrYoqsuibYuAfQJUOKblGxqrFO3xhTBA1iBL2hS9s9qHj/wMkN+UzqNYPUM+9/3DzI4CBD8Yo/OqWhdBtAlbLXooROnam3kGhO/bYa6afm9KvtwxRg+QUhhjwERCmmAsQOtTtcRlP1gxHqIENler3C9uCeoY1LBIVk4H0V6jPkGo3phcVNsLPCo8r+qSTzgz5d4RphmyWwraE12W+vh4tcGbBRmta/mML6kxjJn6mx5iuiVzWtY1b6OIUGYPphNRdkWMPvgpXgDNFNa3eBnQI758JaZ6Y+90UGvanc9QitoO970YbVEhK7xHj1HA2NVPrn0wxwzZm0TZOJ7EkOOn00TKcpzxQKhIcQ7khbSNcgSeQ9rZ+WvGkbnXxzQXiwKHlcjD3RMOyHTucEuqSRyrR1nqlKgdfvnU9e/A/6LANx79O7H/JAIEzU9PLHWH5rZq6C8B7etaqeylZS4tpJnL35tkU5/Vorqys2Zz5RBYieMQ9x33EtUX2htFXy9Bj2SQrLqDrXSjhvrsyAlu1vZBQQoHf2T5j9GiDFfLPxzArojABS6Pg5DvyeUr2EPkaCHNa88LbC0YGt6jMU0riTw2BRrH/NYU14ZAkkta3h/lOF179KwLM/wbyCPYlMnKUOqdH2 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)(316002)(36756003)(66476007)(66556008)(66946007)(54906003)(6916009)(8676002)(4326008)(83380400001)(31686004)(6486002)(86362001)(5660300002)(6506007)(2906002)(508600001)(186003)(2616005)(38100700002)(8936002)(6512007)(26005)(31696002)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?3mSThvXnpdtrVYl+8UIVVH+Dyvtl?= =?utf-8?q?6KwFdYH3YHjONnQdFpoC3cbGc45pER1We5/3bxs0cKneycMMKw4JpmENFbnrWvi58?= =?utf-8?q?oLBKgzJ3e8NpntTz/DqdX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1vEifsddTtkiOE40FGgZiWFwNFjXzFIoHRVHR2?= =?utf-8?q?Pn+Ln0WWQQEsPD5vOQSOWgsRvxXUuceGUrF3UVzEZn7ZX5y7dG8BFXyd1nOCWTO34?= =?utf-8?q?/Vd+zVHWwUQ5JD4o1mlWpl1jjd2obTMJexkzG2mgna9pbLhjwqyRNO4pET7J9iH/S?= =?utf-8?q?Vtylvc/fyz52XMoMdzltaLxTTPlCEiGcVSmN60s+zcLOtAQuLhz97kTJiDo0BvyVD?= =?utf-8?q?7adqp0Mq38zOapkUtJPANdlSydZHCPEWmhWGFWd52hvD9Oq9YCEfzn7gyPKiRKks1?= =?utf-8?q?B+EHN6c9rdTvHx/wF9DfScMBvC2EqjAWS88z7+UC6c11AN4wTIS5x3mLaSZ4a0rNW?= =?utf-8?q?aziosZ7sUVvNwF9Nx6oN45uGmKq1XcSJwfeYABdd5/7DvBu0eKHrSg6Y0Tqyqn/Dj?= =?utf-8?q?CrXu00Y2ULlOu3PW5+gLiY5LhqlKNACILTT3JAlM8NFSu+B4kQpy+aaCluWFBpB5n?= =?utf-8?q?njsswa0K3UOv8PcSIynoAYjVvKwLdbFARLZQKdtpMwOKjx3Jlreff4WgqTooOXLuY?= =?utf-8?q?qcidmdQ9Bh5ypGP06ElDcRIrJmxBmGpXNfaYhU97BfjdLkgATskiPMhrICNww4Aj1?= =?utf-8?q?Ylbz8EYEicqtQPXFwPZxsXiJ2NZM/j5YAR0zfttluA1zHYzmtHGV7c3PN8IkoiDDq?= =?utf-8?q?GFkgcA9jxHs2jxtSWuuGHkQsJUvxtA1w=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 12aa2a13-4c96-456d-8ace-08da4a0180b5 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:19:14.2712 (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: Gvk5mqf1sb7xrJdh7xLVZv9zRgO8cyk5nVb5ahEarn7RLUGEcyUwREj7N8gOFWHtyCchQdQJEXjkr3Jx8AI34A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR04MB8559 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. --- v6: Use sizeof(). 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); @@ -2168,7 +2169,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); @@ -3057,7 +3058,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; @@ -3114,7 +3115,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; @@ -529,11 +530,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 ) @@ -545,7 +547,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 / sizeof(*p); 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 Thu Jun 9 10:19:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12875172 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 6D81DC433EF for ; Thu, 9 Jun 2022 10:19:57 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344904.570525 (Exim 4.92) (envelope-from ) id 1nzFGS-0003UR-GQ; Thu, 09 Jun 2022 10:19:44 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344904.570525; Thu, 09 Jun 2022 10:19:44 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nzFGS-0003UK-Di; Thu, 09 Jun 2022 10:19:44 +0000 Received: by outflank-mailman (input) for mailman id 344904; Thu, 09 Jun 2022 10:19:43 +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 1nzFGR-0003U3-MK for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:19:43 +0000 Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-ve1eur03on0615.outbound.protection.outlook.com [2a01:111:f400:fe09::615]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id ad4bb61e-e7dd-11ec-bd2c-47488cf2e6aa; Thu, 09 Jun 2022 12:19:42 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by PAXPR04MB8559.eurprd04.prod.outlook.com (2603:10a6:102:216::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Thu, 9 Jun 2022 10:19:41 +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.5332.013; Thu, 9 Jun 2022 10:19:41 +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: ad4bb61e-e7dd-11ec-bd2c-47488cf2e6aa ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OSG56MYUeDTBjUZtKwynHQfPfGEbVYGWxTbP6fxJb2xBFGwpc8RPGlHipIOoRbTs2z+84O98tlYeDS2415adZqc9yrH9Q7JATFDiNQJTn+cgu67jCEQ6z8B2YbkGwi/sjFkXJOyhZmx518gTx5qOw7hdOHB2yEjHsW8If/Qdh4TaN0QToQNwIiWM5QY5qR/skCVaR19bxGYnbfGyvzEM/7Kn3I8rh0ptF0ZpNw9OLAWlY7HRVK/aMFD4agRFBbO8toP3iuG0x1nr/c5+kzM4ZPjxw4sEdnqapD6eTZdMq8kqkLTqqCfypLul0OXwEG4f/fpKR+StgEmfRHU7YESrfg== 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=QUtiBQexOROx22dYmxZBzyjk2js3wpx89FbfdPCf8rMCZtTY50PYyAyB+ifoyFLuhYDYXnkckF5gQkED1WsaZOJYRsL6abetr1Zy/GOkmCcFQA0dNXgctURr3uHKXRmWUuRRgXPYzr0g4uah9tMo3zuJdQZ7nfONHnORaffzeQ/qKm5BMJhnTsfSOmxKBXiJehseRrf5/abjQ5HdChlhGK7H+dFxJDoDbtV+jex4dg0Y2iqQ9w1pkKP92KgWRT7S25qBZqmhnmwHocEKw7dhF4XOqKAzjTTJYanip2LunAoT7xLQSGZpaYo07BFoA0HUztibU4pce5LTa23nycInhg== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=i1SRbMZ5vtUn4A75NysmPVCeWEjl1KBkL8Pu3NNkQeY=; b=4RD3TPjgOKvP/ILG99eZi6mlFo40A8sfUPofFnLfdgg/N5df/3FsMxA+6EzC80EDGAotxahVFTMwceuAUmGik0JsTQ59oTTbqMdQuf+7i1bdIZt3rv9nEnrMDDXAnjf4nrIX/gITLkwnrKBU/mO2UNlwbhulFc7FfgTQqe4Ky/Ns3PZGkaLRX0InBOvJFhbdzb6/vnD/tXDteyzM16UBGuIcElAHTjfAgHEw42sDbX50YR4EfIWk7KIa8+OgdEIgGTYyrjsD7gDBzE7xr9gpxYyzYddi7OR5hI49kBx8N5uwhaUbd090v2i2FP6zPn+DqIUFh22AahTa+1k+je9Z3w== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <730648db-7a2b-ce0c-675d-cddca116474f@suse.com> Date: Thu, 9 Jun 2022 12:19:39 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 07/12] 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: In-Reply-To: X-ClientProxiedBy: AM7PR03CA0028.eurprd03.prod.outlook.com (2603:10a6:20b:130::38) 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: 0449f426-36e3-4ae6-e953-08da4a0190d1 X-MS-TrafficTypeDiagnostic: PAXPR04MB8559: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: RVsT2Ku1jdNEZbZ0N+GhSIS70NTG05KKVgjDtwjuB34wV2kaSpVewDZmpdz8jOyTXae42iLJB7/oNVZlQRissuEHdWF2JimEAsyf3Q7NhMvl6tAa5ERLjvovoFnjRbWcksIvIhGPStuqelZlQhtmgmPSQouZ6vC7dPqEdGvKr30FR3KF5SiUBKXDacTehgZ+y92wDCDf8HdKODs+2Fj28QC8gehcwFgAXM1nWBK739/MrVfX1rNdUA2miU9tPaoSRa6xyx59k0t7n58CpHu+GQm6VQGqag8duZfUmrjY+Aw/5sZJpTJfdr0A++NGVSLs4rgz+9Aqqk5PuJ452xVVcmDOTj0o4o8WbQ6LBTEOYLh7vO00pFueCv3aHmFfvCC1OB5oUyilArrQEzIx51y4vP2kJvN6qgIo8HnBUW0gUqkt2uqcg9lm5ZsGUAvyD0MVi/ivwiEeJB/kVI30qUDhs1MWbhV6Sqe2sSscrCcvZlNpyNae88SKgPEkOcXC5HaNFa7OTFtslJDvmUrgqx1zV7gnVpwP3IEzY2i12PPpSQIlsBH4oREhdiHY2g3MiK7Ifc7LSwXxbeo4mrL6fhay9ZlaN2Oj5EfBWggkodtIwHyc06w8jPHtRdz6CNjJGS1zldcGCujLiz84cMC6wRIibu8cwJt8vO21Ix7tJKgwzolm0ZiYtbhT6DiRaXf+RDTxrsam9fbAckj2ZTWka5KAAYi3pK3DtU0ouutCHus0mcA= 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)(316002)(36756003)(66476007)(66556008)(66946007)(54906003)(6916009)(8676002)(4326008)(83380400001)(31686004)(6486002)(86362001)(5660300002)(6506007)(2906002)(508600001)(186003)(2616005)(38100700002)(8936002)(6512007)(26005)(31696002)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?Vm7lDTbNI51Shg6HZO/smIfqDRZK?= =?utf-8?q?wiKnknfQKSdP2ZUsFQNewTC1HBatCPgDrbOYEinwkUefKVK9Ts9zrbgvj0LVdHfP/?= =?utf-8?q?LLfWElhLD0GKMwAi2jxI+aoi6a0nL+cuiQsN5SDu2Obedec2lix4F5pug5SLCftRs?= =?utf-8?q?S7mmU3Cr22sZ9SW68yK/UwMkUOnb2T4SjErmqb8cY7555WLZooVPuRJQyUeCIq5HN?= =?utf-8?q?gwJ0kVilWAZAHxoqlGs8nbgCeCpM5MsLbAUfQghFjS2QWIqkbX+CqszTQWUzTOQoN?= =?utf-8?q?kN9URgf1+aMdH+KiRM2RV7LXf2qKooL9wn2JJl5XPUSxlhFqPGnIheSTrI8ZyY4ax?= =?utf-8?q?M/OfCzZx3wei++yqNgrEUNnCoj+yV3qfr/DnvKH1tmYcRUr/+EqcSN+beKdWwloal?= =?utf-8?q?QMHf6gtdEknibV+X4BeRQqKjQG0clcPEWUgCCwSkx64rZFw4BU5A5N2TAOWSedarY?= =?utf-8?q?fBRdDrTodTwch0hd2oFxCXn0gRHUjO/FTSb+udJPBsKLWDLyGer6Krv/f7O5H3I2C?= =?utf-8?q?Abdkvi/LiJVSswAL8dYQKwPlr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sQ27r+Qw8D2znMk7jhYWfmBhu/ImnlBa?= =?utf-8?q?AcVWmBaTM3KZCQsNlVBHBoEBbb/OI+ph20D3dOHn5blYuAB89a45Pf+iiiCbzVz5y?= =?utf-8?q?owG1NwaWHjdkPQy3tSTkPtn46XYzpWiYSSpTqUKjVoItMAIEpKz3Qp0J+DEkuLoUR?= =?utf-8?q?OBE46JMI7q00B1Oh9f73etlXEyxQI7/BMucLCMh/8HyosvLPMggVKMIu6Z4TC/jLv?= =?utf-8?q?lqnnhQAU4VLINjKgmm6icEmQsaP3XucQ=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0449f426-36e3-4ae6-e953-08da4a0190d1 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:19:41.2851 (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: X59Sn3qNiA4CQ9aWhDZL50U5FAq0MK/9X1RIsdI9z5SMoTXLTUE5kh4u+29rMXaG7rr+SrZCHimj5UB44Eijsw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR04MB8559 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 Thu Jun 9 10:20:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12875173 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 15159C43334 for ; Thu, 9 Jun 2022 10:20:32 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344912.570536 (Exim 4.92) (envelope-from ) id 1nzFH3-0004nt-PR; Thu, 09 Jun 2022 10:20:21 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344912.570536; Thu, 09 Jun 2022 10:20:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nzFH3-0004nm-Me; Thu, 09 Jun 2022 10:20:21 +0000 Received: by outflank-mailman (input) for mailman id 344912; Thu, 09 Jun 2022 10:20:20 +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 1nzFH1-0003pZ-To for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:20:20 +0000 Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-ve1eur03on061f.outbound.protection.outlook.com [2a01:111:f400:fe09::61f]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id c2e9ca3d-e7dd-11ec-b605-df0040e90b76; Thu, 09 Jun 2022 12:20:19 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by PAXPR04MB8559.eurprd04.prod.outlook.com (2603:10a6:102:216::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Thu, 9 Jun 2022 10:20:17 +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.5332.013; Thu, 9 Jun 2022 10:20:17 +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: c2e9ca3d-e7dd-11ec-b605-df0040e90b76 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ha9OO4+1M3UBYk8ybev9pGOJZyTzGjeYTcPtTIovQn6VxYSIYDc/U+3msdTbfwpp66HzLpXaO8RJJPUk1kO8ALHjplrq+J4fKaFobZFTfTV0QmITV6WmcsICZJQvXtfkjNUM5XSE9nS+LTjCNIfQTo4KRaZIbk5CPf48BI1sL0OG6HmgLO1ckWExWK3RACgfVe3Pd/WJH37AyxDYQWvig7TMteRUNdW9PAMEXAPeQuuiINteV7ydt2mcjKRaHH5OnK1D0qeOO8ElrTfn/rePtJprJYZ831W9UXZmDMMRml4+PSdP1JsYqSK+QP7CIY0F0GEi8z+pQ47lO9mOIXsEqA== 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=8x0wNvPPnwdgiVsTXX9OqkK4ScAeymQhxLWbmrRAFA4=; b=TUZGXIqg9V9sQoAChk/pXd8TXeUojT1FHSRzhmeamhxwLWIOq5wqtxWhF7RpqMK7HzS20Qnn5q73EfXKbU3YUD9CJYqhuBBB3Vc7ARxqV1Yk7nf9FWrJXdhR53oBkuvScfXse0+d1+I2TfgmYfv/ejFWdmIJ29+HUhaFBbZJeQ9G1KbCvjpdo7ElQjRsnsyhmgjaZGHfdQsHhB/AbhYfLvv0wdONXWlhc0pL+DasVttmJHoC1rW0DQzfxPaHqF6DvG4NgvRuIYkRNLojI5BqGuoWU4ELN3WXLBpSDX+1aCKffUvkfuQTUeCFeBiDZlgoV/UGiNDHBBztlQWFrS9LCQ== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=8x0wNvPPnwdgiVsTXX9OqkK4ScAeymQhxLWbmrRAFA4=; b=Q+S8oiRd2Zu1eRP1x8lu7kTkRLbcuW8TzVJ7IqaN1rFxyrVHYij9n7/ZHNEQEBKJq2HPrik4RgvleZ75ftE9ngmRtlaIpYhyeeuGFDoPcb7ymhujjixgpHheGnr68f35vNJy+DaQ/D6RQ8OU7mREM8eTYxiC3rdrnx2aA4tJgWwenuSb04BBezqLTi2duncrYirTve3shpXTgCB3duO4cLxTf+iRv++inAO+j4LVBxBT7GT+ptZ+44kxS0Il5qlJT4VBfmuN3cPaXxT/iR1K1/dXSFIpljyEp0Kpd3cqsdpt8MhqLJ2dkcSYmDtYZO4qPmuKMqJZ2oVR/S2qfBhJiQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: Date: Thu, 9 Jun 2022 12:20:15 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 08/12] 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: In-Reply-To: X-ClientProxiedBy: AS9PR06CA0490.eurprd06.prod.outlook.com (2603:10a6:20b:49b::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: 999a4e3c-cbf9-41ab-7945-08da4a01a66a X-MS-TrafficTypeDiagnostic: PAXPR04MB8559: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: RBWul+2N8hKk7UzY2w/Ylocv14sNkIWIPfPrHK3vLSN8FkDhnz84EZYU3vEgXfYGdsib4mB7xfF5qI+1/SOj1IWyZ7ZO3n0voEr7UF0u1PxTWJXVytPaJsOjhGtrum1aayjOYPGUm8OV6b/g1ZdOzQpXTwj1fEpr63Px8wm005BGOwwSIcMHNcQARzRrKckzjiJTfJvXYL1emOZbPoQt3RVt6ITxSokfnG5R69tReereyhRq7pQ70MJzP8QgBU+u5U6HPuQqDWkXT7GPg8CTZlrQRqcqLyeWk/wG2ndFllUhnPRF9EDza7Ck4Wqxf3oQfUiEGCRj1YJosQRS0fuOq2IDc09IiqfLo+iO1JlWyRTVmb7+pgjjKHWrauEaD3E3wiZNuVxGqomwuGE8EIKRk1vVAd4mvWdeiILbk3rmL10gukDGVBvnFnNBEwSk4/guaamt52EGMAXJfY4Z6mFfI0XV5N6XIsWFK6dRqxQB70nljET/tkxTBUYgt2fqtn1Lx2LKk2h1y5p5kBtax6Qzt9MuQfR7SyyMGHh3fMmtfKBW0GuaiG/seoDtxzMGEpjplc90juDLv+H+qGdJ9ayT57syzMt06sLaDnoF6ZhlUOYnAJ6CbQ8RH1bLx2M4xRE5DIdKtcGYTkAzTsmEZm8hKIBqnWVoMvG9WjlqibDCiQJrMqqi7vDfZxWbF6UzuaV0PoX+T3sZ+8eaP6ruzryWHy1G6Ou92Nai51eHw3u+PAg= 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)(508600001)(186003)(2906002)(2616005)(6486002)(86362001)(5660300002)(6506007)(26005)(6512007)(8936002)(31696002)(38100700002)(66556008)(36756003)(66476007)(316002)(31686004)(83380400001)(6916009)(66946007)(54906003)(8676002)(4326008)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?ONknwXb8GllvMpQXLaQruclkk0An?= =?utf-8?q?MXuutdhwM0O6et2MiFZCnzogSO25WXUO4jm3m9bk4UVP3QvbpXLf5xEV9WozvXQLX?= =?utf-8?q?7b9IOP1i/9QCOBoYFkIZYP4yHn2LvITt4g6Qvj77idbZC9Bxt2C7eppD6Trt8UDty?= =?utf-8?q?xSEKo0D0MmsS7n1UhhZafoGq+oIeTlqBOfGYfRHXDyt3qhpBWX8oJ8CTsSzcb8SMl?= =?utf-8?q?K6UbpOUY4I4dchLPtzzNaqXWJcv4nMDOoeK+eikyWnwC3L4xJ9gSQ/a8wQsiNA4r7?= =?utf-8?q?5YUhA7aaRZTGvfy/nSKpGBd67kXia93IjIlVARJZLbRC2ipu2kvyWTPklnDyV39cc?= =?utf-8?q?opB+zVe0KysMGUEHCxlY2/oiR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TX6wdV/CnQpUZQcGXdyDpxB3HqsxIb2AZQJajv/Edcfbot+iu?= =?utf-8?q?N6h/ksadgWD08++/e0UAQqtYBRGVOKh9K3qSxi5B+Q3JXFcnWbTytOESB3lVMGMGM?= =?utf-8?q?aWz11oBqlDs/sMNWpxpg1aw/VJ3j2T4nd55fsVsrf9zFxEvMTe+SUHnGeeqjsWlUX?= =?utf-8?q?gkP0VhLnPSmDiTDZmeqdF0EBedibWrcSWyTF0SAy9xYo9skpYHybj0wetTN70y9e3?= =?utf-8?q?dfL7Txo4m206Qdwpx5VU5V4cJZsWMl0ufnsoda+541m1mTn03b3X6hloXL1mkbsN7?= =?utf-8?q?r1k4i95esiQYDoCz08M5Mz+sDvuGbywoPYJG6lmVuDkX2zlJHkvT85b7LeTp34SBv?= =?utf-8?q?0xmsouN6Vi30plAdJWtLM3acMP5jGzcrGB6z+2lEibg48NSX5wtUdoRKRJ7jLV/WI?= =?utf-8?q?EEidiA165z6MU0HkcdkxXmlZI5OXgafw=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 999a4e3c-cbf9-41ab-7945-08da4a01a66a X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:20:17.5171 (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: /vrrtSeyijmSHkq+4iG5A8M/w3D3gFAwV3oEpaZqNWibimu9T8lywHFlJ4xiYoUV5pm0fVpk7lY1pInT/ZSohQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR04MB8559 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 ) @@ -829,9 +835,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); @@ -2177,8 +2205,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 Thu Jun 9 10:20:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12875174 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 C3AFBC433EF for ; Thu, 9 Jun 2022 10:21:02 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344919.570547 (Exim 4.92) (envelope-from ) id 1nzFHS-0005LF-7E; Thu, 09 Jun 2022 10:20:46 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344919.570547; Thu, 09 Jun 2022 10:20:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nzFHS-0005L7-3p; Thu, 09 Jun 2022 10:20:46 +0000 Received: by outflank-mailman (input) for mailman id 344919; Thu, 09 Jun 2022 10:20:45 +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 1nzFHQ-0003U3-Om for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:20:44 +0000 Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-ve1eur03on0618.outbound.protection.outlook.com [2a01:111:f400:fe09::618]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id d1b8863c-e7dd-11ec-bd2c-47488cf2e6aa; Thu, 09 Jun 2022 12:20:44 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by PAXPR04MB8559.eurprd04.prod.outlook.com (2603:10a6:102:216::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Thu, 9 Jun 2022 10:20:42 +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.5332.013; Thu, 9 Jun 2022 10:20:42 +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: d1b8863c-e7dd-11ec-bd2c-47488cf2e6aa ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=F56ymm6uSQP3kHQK5HRsC1JzZMNMfH7ySFKl3tGTY58SrqujyIy7+zEueGC0AIwPU5zeZTTrFf++ofyJjV8kv9KPjEIMhMtexIB8YrHlTNGYa94tWWoTzTvAl9yOy8UGFwi003sjIJ/+oK3PNWgY1EwmKun9d+wUf2BDmBVCRHYraF6uXdn7DK/L/6bFGlIsgBpklOvyXFNL6jC8OZ0CuTWuECdbTyEvSHBcNaf4iJwbeKor/HPXbUmy7TOS8Fs+QzaolPA6PmG0B8ajnxqH7CnbRT+glPrDMBF6pagqjgRPo6Hyonl/e8EU1KzNSMGk0P1k82kXmfB2rsgoQfMFAA== 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=Nj/oQXki34+K4RyLOQZK/8x+ZszeqkYK5o2Ej+VIeBsCYpnUxLeR4i6vad+SEl6jfeZGfxMgXczxFZ0g9x+3Nini3EeAI1A2PXg3aIfTd9U+U5G107wNt5BYvRkuajUNrprkzlHcg5Y9FQdb0KWA5e3c7meoGCoHLXEQUGGxLLAAqtkBeKixq3MJr6fRhQnFJsbx0sEVsDfWbH4v6mvLoOevGgSEzkZUFPnCQzSIq+E4Yz6Ogdu+lZO239eAVYopoO1li/jUKa+3N0ykVE46g191FsQLiKQVmRHBosALLVFWhgnnyQRSleVeL1dDd8qFQup5KxPSCyc0nnAw2yU+wQ== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XpBKX8iKhA8N8952sSDbijOunJ7uuUUiGIDbjZy4Htk=; b=hI1LiLH4da6Dx70WByVSmXBQXxnKIVyzG9OTrKLWfTUF7397ysKXFg79XaD5en+E+kBURZOtsmnof+4HaLx9rgH3tL2kc5S+DOLWjcoa50mCVQCb2NijHke32TA7MCILEGwmYSsQF7fbB6RLdwO9+2Je6hF2MO2M29GgHrHP40eWGG/PsN3V7p/2YdnrX74GGQ+7FfpUHoeMXpNfxO1tplMEmTk9kV3r1MVXCSLHu/HAWYWa3urkSvu4GlBJF6KI4NZc6ffOaFc/3i8Vf0FYFuDDQTMGGiE/GWlBU/HjA/lzMJOgGrdfLZJDJarxpvWtIC2nAkjExu/+EJpt0yKB7g== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <31b3f7a0-c7ca-70e4-0ac3-7a2bfb65facc@suse.com> Date: Thu, 9 Jun 2022 12:20:41 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 09/12] 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: In-Reply-To: X-ClientProxiedBy: AM6P193CA0059.EURP193.PROD.OUTLOOK.COM (2603:10a6:209:8e::36) 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: b75b3e96-f223-4843-24bc-08da4a01b56b X-MS-TrafficTypeDiagnostic: PAXPR04MB8559: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: SQ1JvmN4zwr8KLmrjtOm9d1apfORBTxe4K+uDAT/TUXFDi/xKA5vkhiU+ndUATNSvRrsfSrJDO5fZWHnhliFFx32agwsTupqia2+ftPzCEKKvOK70gv4en56TOwaD3O5yNsHrmEyzqXtlnL4Z01quxktVGxxlwW9qCsYIeOi0CR4FqjsT0HknJiIsNQ+Ag2MoNvqtmSCtOjKVg2L2zdRAF/8tuPXn16b0st6MUzBIHP0AaB87+C3KmAQEGEI0u6k/xuWTaFtbfY+h8VnU6h9IoN7hQ+npbTahMcjZLPA7ocLeRfJ9GBrYlscpSaIsLTah2g2uabYEPPx+hTLnsopXLBxzYMbn/UDrsH1CuBFI48rCTgVpL565dnCxCVpQqe/hKVqvn6e+PFSH0ITv6MbajT8ooUz1ww22L8wFTJwOen8NXMtcOs+jVsOKZSlD6ORqyW3u1JIczJ4x/iXa4/INFGKvx6dijj7wF/rULqVvvt0kMNttUS0pfkT/D7Ar+iN/dYFo+fs/52atk4OCZr55Jsc4lnYjH6hzMaibunuz45ubQ5dRNJRZYfpOjSZUvT70SqMQNBGSqsvWO9fyBlLC4iR7q2vzkocGposlBHgD8abC4qbMq9rvce8JAec8q7Igm8GQHjZkU3jk6Ws0hBrqYINUqveBiaGJszHDLliDP8PZmfwXxc46aqU9e+QIxZPz8UZKB7ELlLBFzDsF1O8PPhUGlonUVo6DTw4I5TLx8nzFnu75y2lVeAbYLhgYo5f 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)(508600001)(186003)(2906002)(2616005)(6486002)(86362001)(5660300002)(6506007)(26005)(6512007)(8936002)(31696002)(38100700002)(66556008)(36756003)(66476007)(316002)(31686004)(83380400001)(6916009)(66946007)(54906003)(8676002)(4326008)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?HiUCdMTApD4k7VqvPBk5nmnPeBT8?= =?utf-8?q?/mg8JzzfAdDOtCLClN3fXALxm/6srXxwaGHiWQXArhKZulu1iwiCCLg6sqSwciIjr?= =?utf-8?q?06kPbFbtE1UQ6mNAgteb1MBp7bcPl2cQBIR6yv9CEeXD/WWm0GqfdH2pxPuTB9vhr?= =?utf-8?q?VRzP6LoT2grn008lPqPwOnWbsMDRvLCcgcE5v0I0sGbv0PAjr8UsDrTtpDFmoJ6D5?= =?utf-8?q?FENTQU3X1JC+A5FSEc4KzkIU776qDTqsMbsF3BQuvBrXwAoVKWCl32JAkXBUuU5CY?= =?utf-8?q?aSKBtwzue8aLDH0bc+LEsDmRLXbR3QDB2h+2y1G3LoesY8kFxqy8OUbNuG+AVI76C?= =?utf-8?q?/E3GaPMcvnHkncn7ciAtGoiWkMy5k2LA3rBtjthVMBuSkZRd9l0Oqcc44K5jx/wZJ?= =?utf-8?q?ECFx+gBe9e4+NQalHhKpcgsASAQV2IQqufJEmESP7cM+EMohe4SLCyj2CRRFWo46i?= =?utf-8?q?CtU1Or7rt19T6KSPc2/5sDukrvmV4eez9DrIyIOgV3GrjK49SCAvsTFFJ8CZkxQJn?= =?utf-8?q?Jg6tU8NwTW3g9y3e0lgKREKpLZ58b9VACqkNTd9cGPc3WQD09RgF0s7Es7+fdCTqN?= =?utf-8?q?g7eDw6tuUsDaS96/5NeS9AujJOy5aNQv014ecpZe5ndhVm4tbgshoUoo7asyGPodQ?= =?utf-8?q?8Ia1pI+OZPQa4Ay5WBLRjRS9WUjZuYkKHQJFfAjFMTvxkBOw2MjvLYlz0tMsv9a01?= =?utf-8?q?Q/XsGYz9Ia24mmCmwCt53UxTcWpq6q8kcf2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XREi?= =?utf-8?q?1rSgc6yQUGDLMTvtmThb09tKKNQ8jZJw=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: b75b3e96-f223-4843-24bc-08da4a01b56b X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:20:42.6873 (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: GQin/3QcNARuNzq+W4GIkhgFo4yy/7zW4lsIJYg0mFiUoAmuTrLBMu1Un/3mGojNq+Ze32D/tSzbCVLcsc9CGA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR04MB8559 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 Thu Jun 9 10:21:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12875175 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 E0484C43334 for ; Thu, 9 Jun 2022 10:21:18 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344927.570558 (Exim 4.92) (envelope-from ) id 1nzFHn-0005wI-GM; Thu, 09 Jun 2022 10:21:07 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344927.570558; Thu, 09 Jun 2022 10:21:07 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nzFHn-0005w9-Ce; Thu, 09 Jun 2022 10:21:07 +0000 Received: by outflank-mailman (input) for mailman id 344927; Thu, 09 Jun 2022 10:21:06 +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 1nzFHl-0003pZ-SD for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:21:05 +0000 Received: from EUR02-VE1-obe.outbound.protection.outlook.com (mail-ve1eur02on061b.outbound.protection.outlook.com [2a01:111:f400:fe06::61b]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id de436bff-e7dd-11ec-b605-df0040e90b76; Thu, 09 Jun 2022 12:21:05 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by DU2PR04MB8790.eurprd04.prod.outlook.com (2603:10a6:10:2e1::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Thu, 9 Jun 2022 10:21:03 +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.5332.013; Thu, 9 Jun 2022 10:21:03 +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: de436bff-e7dd-11ec-b605-df0040e90b76 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cVZltJrc9tJhV0TJXGKBJiv/jE/uZjLNuGyZ5b/s98sAkRMerr2SokaV857aSrFPexBqn4XufxBrK6NjItyvYkUpKKeMxX0/G7sGe3Xi2RV7Tcc4+kR/Vgd1jo5WMHAzcYYYlp37qdVnTFQgQKEGIEib+0MKldYovzSygBhTI7qYNwuxuT47Csh7IzogDCfxhYdMIDCGATUWOwQGZW+E/0yjLRUPSPnRB0AHKt5gpYWS0vqOkxPb0L5+OJ2iCe23Iiwd9t+89kCrJKtx/ZXL4IlO1ygresvekFSZBY8zF4ulHqvTOWn3jURkUXhMDaUt104RmUccnpm7JlsS4ti53Q== 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=DfKw4w4lT3delgxvxQXi2BzPNtcGD1oKtqm/W3kaSWA=; b=Af97b+RCDElkLr8SANvxVYT552+x3K/XZdoeBTjV1zaPMH7xVSkllS32207WYmDnFn2T1D2Cneh4OminKErMQB0BkaGeP8u5Yk7WkZnEBXJJFBcoxhNY00ZMlO8rRLhvcEkw0Xg+suFn5HB/eJzouIWp93BnIxJhddd/GHnPOjbKyoMip2L2uBlcWsBY9uuz+P5YOsca/Dm+grcjKKwwf3Wb5BsmCqyVOfOZroJMCl8bIuyWCsTpZN+mI++DM1DNCP7US15Ls3DcWCLF10P9yWTKF9ArCHwOCqRTaeG9VHoP8HUiND0YsqnNRAekRQbAeyyp8goyu8w9aq3BA9XtYA== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DfKw4w4lT3delgxvxQXi2BzPNtcGD1oKtqm/W3kaSWA=; b=tptJBasTf5Dw6WYVu/scgPvrPQISklOLfF2Oh0wDBm0vuDFUNdJvZnIWwGR2WE3RGFPRSLg6z6o+oz3K4Q54cwGNy/ODUTP5JQwIcaqPPe3+MyHyICs9gdZDUMA0SHrDmuNBya9yp5N2CBsEEHetFd37/NRSf9WYxsoOGM7/XKdaPjIULjlSh50ldfZ2f96e5mDQM+Rl/lqaIoDrVtE6xFAl4iC14PXMcCxXh+PvGc74f7Ijagq1IpcuW3Nme95EYL6ew03g+jLmyQgROj0cCVRbuNYiDJniyvKobgZZWl9ByYE3ANO/omhcCo+hDtC/AhDq8dLibTa1gBysK+eGrw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <0013f62e-395e-464c-30dc-e8e800b1a976@suse.com> Date: Thu, 9 Jun 2022 12:21:01 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 10/12] 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: In-Reply-To: X-ClientProxiedBy: FR2P281CA0022.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:14::9) 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: 2dd9b4b4-8c06-4b0d-8847-08da4a01c18d X-MS-TrafficTypeDiagnostic: DU2PR04MB8790: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: K1SpXDsla+jeYa1FpP3Ie45MYaTA2fKc+ldvPOorZdsR2pj0bQKg4tbQYKqpBnuv4pJ+vj2qOWoIBEYZUPcdzw4HpF1BxDt48iwFnC4/Tez3Lo5Og1sMCH3TUFo6B6ci2ZBRyCzzwV0XJ4wufKsHe40giL7dS9e3IERfSXRXzqMne5ONVnfZ4US9h+YfccUn31gjjnc9RR9SK3grQ1Zli6dYdLrAGN1kmQaNIp5HoEomDGMDNnon1QswxuxQ5NIxzcNlhY7rSqNVB1YeZXy4tjvbBBSDb0mhd60U6QiKYfKjRLS7D+oeeLBmkMwQPSnWfB+loaf/ZRyCkwsn4IBPXpxxBLxmyOd4aMDhR3xp4NgBPn67yNDcJ2x7Qgs4LO8MSWIWP/tmtRlv0pzHwHdGjEPjYkQ6sFbqrHsPOe57ACs3fE3PR+jRJ39wfBn5qiEaphPrEJyMVxuLMkUaN5gkOUkJO/Qo1oWGvauzlnNUx4PbsO6vsRup6IxDN1wJS96r2d00+30hbNAmJZNdZt8WcEi/WnCd3sRECr7eWEo33qcNPmobiE6JIuRNzjq5RRoeiwMDShdI6iDiQV1AtgH/cregqHFpgoutvX1oCC43l1yflO7OsCIQgOFCO9PC3IiKrhgx7dbBOgGY3qj21UnIIEOu7jT4XBmNp9m8lOSaxbefUxtBvSDG0eQs/nd6qTgILQK12ESyoCfGJxrOWhglGDe/UQXyrBPTPcGLJDKf4bbzYIHcNEESAE55SxN7d9eC 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)(26005)(6512007)(31696002)(6486002)(6506007)(5660300002)(186003)(66946007)(83380400001)(2906002)(86362001)(508600001)(8936002)(36756003)(2616005)(38100700002)(66476007)(66556008)(8676002)(4326008)(6916009)(316002)(54906003)(31686004)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?05Vo2w/UuVQAOlnNniuZAcAlQaOt?= =?utf-8?q?zzKlR6K0tDm+VyKGDeUbe0k+MfzA+pIcdQBSt6bFqjLIhBjWSFFxOqh1qp5nMsURw?= =?utf-8?q?hTXYYb9QcFyXaMnyCEs/8y3GsSrwxdWj9AmhAwaaU5P32h18dwgmwOJKobJRl7ZJ/?= =?utf-8?q?0LSJQCD6SMcoSHY5+KYtOnZ7fvMDIbhy2vBE8PxZTWjuxO5LiRgtxBBjOhiOmfpm/?= =?utf-8?q?XWC+UF5a3zc60Qtop4JmgYbJ14CfA7/Zqti123SxQaMF37DMR4keRP1UfnCDtUoZM?= =?utf-8?q?9fLAWWE9v3GWUctSyjbdskyoWk6UrgosmLgTlX4EaTJDe4fsgLNphCujJrM/sAiZN?= =?utf-8?q?NR41UPu8NHIjwnYOgtoqucK1RKMU4qh0Biw3yDz/+a7YNWPCaFiOZ+nu+pPAj6KJD?= =?utf-8?q?gMoU8wAiBDGVZsABKGAHlsEq2SDTQjD6TLDBudZp2Rey/Fq2wpNCelt3Bk4U+aMlK?= =?utf-8?q?Wl8AoG6vmFJfoE2te4PMMg31zge1+E1XRnNP60OR1PdcU1H6jjna62HVMtFHDLB2c?= =?utf-8?q?zRrtZuKQtqLe9Lnsw3trYWyk5f46dma3R0Lc+0HEk0nh7w3X/+OFdSI4TZG1uezcF?= =?utf-8?q?v8VVzC2KEBYNSZIOdJApHDoISE51ly+Rqfg0VAm0IyeWAI3g8+evrZ0jS7crlIWox?= =?ut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q?WoqARzs5tuO9NcdpETKdUM8aPJhZFZMZijGy3X6kihXG2k9jcQSf+YG7VJtq74kQg?= =?utf-8?q?WnRasFv6xQcOh95IQ0HrI36KA08gVqaICEy5VdX6eRAgJQCW59ExbMu1xny4SNRTY?= =?utf-8?q?TtooI9g2NNudqwuXdPKhWlPK0ta1WIWw=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2dd9b4b4-8c06-4b0d-8847-08da4a01c18d X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:21:03.0766 (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: PtGJt879e4RoS3madOEV4blfsfIosL/0oxgMzSHlMzqxBN2vo2dmkLnAiPyIygNfxRDK0ZHJF/6yYI9s4m0vvg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR04MB8790 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 @@ -2211,14 +2211,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 Thu Jun 9 10:21:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12875179 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 77CE8C433EF for ; Thu, 9 Jun 2022 10:25:46 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344956.570602 (Exim 4.92) (envelope-from ) id 1nzFM8-0008Cz-1r; Thu, 09 Jun 2022 10:25:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344956.570602; Thu, 09 Jun 2022 10:25: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 1nzFM7-0008CX-Uu; Thu, 09 Jun 2022 10:25:35 +0000 Received: by outflank-mailman (input) for mailman id 344956; Thu, 09 Jun 2022 10:25: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 1nzFI5-0003pZ-H5 for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:21:25 +0000 Received: from EUR02-VE1-obe.outbound.protection.outlook.com (mail-ve1eur02on0605.outbound.protection.outlook.com [2a01:111:f400:fe06::605]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id ea18e1e8-e7dd-11ec-b605-df0040e90b76; Thu, 09 Jun 2022 12:21:24 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by DU2PR04MB8790.eurprd04.prod.outlook.com (2603:10a6:10:2e1::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Thu, 9 Jun 2022 10:21:23 +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.5332.013; Thu, 9 Jun 2022 10:21:23 +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: ea18e1e8-e7dd-11ec-b605-df0040e90b76 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Odal0snAZ8F9yN2IlfiSiVIK9OwJDNdkM+AZyUBxTsCvEj1sc3v5AuyoKtnunKVw7dMHpgEos5Z69dYG6yR/wtci/DEnkzgfYZlCfEA0l4vtvv/Bmrp2RUBvfkCBCDi24M1sjkcyJtkw5kyYoKmtOYDpugONdTA3bCyl1wQq8GKOwJ11jpehA6rkgwCIYIvLexDfeVbGi3G/1ZMJ5Ha2wVZxhIhec7VnoCmNcqBwxD74BOV9FYu84T38HO6e3WSVABMlQwuSKAo1Ux7ieDhSqv5Kih7yDzMHbUUlJMK+4MVtddo2+Le839AY8KsHZXcDNypJ33BkPFJe8Jd7SfgeHw== 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=iNSivqMUGJNXw6YV9IuRH2YBw0MQFLGrycARpWhZOTo=; b=MFtefsviZuhrRF3fj+CZ28MqyK1OaOe6LT1BXQCHRaAEl/qImrkuOIdCDLCjV3MgkJoOBMsd3xbSBu1EvGbZAM3I64YTFjwLJdksGCSrkqGS390rh40ZRF1BIpMASKB0Td87LXYJws2sP4mBzqd6uOcRnAl3nDjGKNTXW7PqlTpKlr3t2/jdOETfY/c0+IZPNlBT9iMVDokHcCtPBFi/jnDQ4SIkPWrbQg+Hk1NboK0qY+n2hQ44SpnxjN7ywi6WpDK+J5pIFpJXTQId3ojg7TqnWFSY+mDvqVWmpFZDYaPCV4wdfVvVlAQoQQb4jvYhmzK4MBYOAYAtlV2CtzpMiw== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=iNSivqMUGJNXw6YV9IuRH2YBw0MQFLGrycARpWhZOTo=; b=KNUbVApEC4UPIpV7LsnKtKBhpDqY3q9tOy7QJhEpI2HN3OX87pcA8L83dBBEgjj5OGa9qubomQDAof+suyPrYiEIJAPQmuNxVtrBWKy4gW0Uak6GqRnmygZiztkIvz10c0vRoPliC4XkU+QUdnWBQLAGLf+drDwl+S1GVQgxmy2KeFZsod7lQ6EObgIDaADLbyjsCjHdvq4g1LCqmefSRhoiNcLGKiA1v++O6X+liO3MX4/ofmfwS9eDdU2J/u+yLDF3v7YFMwMUUl0Ju+mrbBZb3UB6jx4sefoujEeira2KLkHhgWxJM5KAtypymyvKKOjQTQvkKFI2fnFJegaqsw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <5569f3d3-7315-3fd1-3e73-0cb610eb3f43@suse.com> Date: Thu, 9 Jun 2022 12:21:21 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 11/12] 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?= References: In-Reply-To: X-ClientProxiedBy: AS8PR04CA0146.eurprd04.prod.outlook.com (2603:10a6:20b:127::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: d9a45ac9-ad1e-4875-4c29-08da4a01cdd1 X-MS-TrafficTypeDiagnostic: DU2PR04MB8790: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: CQaUxOHFIyLGzQDuN3kk7V7d1Sx4Qvv5WE8f41ZFqzwbSqHFBX8k+I7n/e1fJGGXI2cJOmOO6xGc++jyt2SYj5I3f2CyDKV7+db5zqscHqEgcyl6Ql8hQi147Jo2sa+/wb40FbxjhV2678mxJLzYdGZibA+BaxLf2e0jk1dLIEun1t1enSifdDPfP1Vt7NUJX4WNAN9K2G0+7Tg6hCSPOVDMPPFdgkUBcl67K0GWhoPzlMse3jv5l+gSa9TavaHRhPdbMaXG+5S5rRRuz4+0al0OHztIyqtPbTwcptmOX7QSGoUDFElj+L8fqoMWx7z+pI0UXhsxB8FpRm6EiiyRq3JfAtzmfij/YXc0S0MQX0lzKlDEQzrVlsbWSqupszkMtBBywY/rtfXARddFQ8+BPivL6jvoH9b85w9k4K75NqG5+jPZHg+VN7SWofbfv10n3TIJir7ZFnNwNzhCrcIla9MgOLITaL+60YJ8gwVLI7/WtcjJaFOE+rfew1pcKAmA6Zy2Zxu4Em7q0/ececBWPvczs7OGNkgwmcHSzQ2aX6TPNybNrV84L5Nh5/YLSt7rhmRGCv4LRTlYzIKehmEX7mmUhU4TlDRonKZDOg5bCaPprS/jce3p2CvnIb5uwSxvuEqD705TRtWpN5rOe112NUOizrQGeRuuEgWNI6383rwB4Un2t6j0K0OdTcQWHF/yLq2Zc5wBvGPVbaSXnp1qjRqEoN+cUmsuEo3oxW0BK8wiI1kHUXCcGEN8sM1NMZ88 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)(26005)(6512007)(31696002)(6486002)(6506007)(5660300002)(186003)(66946007)(83380400001)(2906002)(86362001)(508600001)(8936002)(36756003)(2616005)(38100700002)(66476007)(66556008)(8676002)(4326008)(6916009)(316002)(54906003)(31686004)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?EbvgDUc8ZcdyTyksQUJF+vgxd4l3?= =?utf-8?q?2vFBaqMMCSG87+N4a8EVGoCPoVsz5bNYmfYhLiWUDUCguc0k2DDD1jyeFEYdLbPlh?= =?utf-8?q?pYRTVnfJGi4SMAoYb+sFaKgfySuyYhMutaSljI0W+U6ZcsmdhEfZpf0XJ6PHQ/Z3I?= =?utf-8?q?Jl/Z6vFSk9OS02as+xbPw/g65JcYnX1KbcE7oWwBHq+FElauXMJEVEhTW+V6heFqK?= =?utf-8?q?M2bRyXw9er2RS76lwp1l+LnEqPtkG7J9B02QezFfH3O/bC9REQRFpaijFei2fAUpa?= =?utf-8?q?eN36fFSbA5kVr1gKDNbBVIJEYy5YsLknuW8X+ETt+ZQK2SHtRgBK19MMXptKIgkgb?= =?utf-8?q?YpVJukjjsCuLtrw8BI3ScrI4k2b1XURcEznH83Skj1uo9+7IyTdcqv+bUjcPlDAHu?= =?utf-8?q?0jmwkfKzRHke0f0k2iSaXpgPKmZUnZc8kiekbKzIWOV2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3gP5bi+XYN3/PxJ?= =?utf-8?q?SZ0JrlQn4bsfCnCxsuKLeANPiERtny0IbM0zc61WyL6x6y77XdgaBZw7xSglC+BD0?= =?utf-8?q?zow3SdFW5HcsMgPEMi343wgP2hXKlodGE/FSkly+eQANj9AB3Kl3gB1xQD11s2Ojf?= =?utf-8?q?SZM2nr2RQvvOWkPj6czdm97AiYBrFWOd94t/9LrquGVU58SRxyAKPQlxZRu4AHFLC?= =?utf-8?q?jQNv3bwfs8CDYGIjoPqyo5TcB6zbRtF9EM24TpeJfUH+50Qot3dn/djiEyHFgIfAs?= =?utf-8?q?J6uAl2Y9sJeVDgwFYwQiM2Ow7lePfDzXGqHM4vlKqTgRzDHrkoISMHOjEHY0cNsJf?= =?utf-8?q?z4sNghp/8zJWRPrIQyhVt/s5yvjacKZg=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: d9a45ac9-ad1e-4875-4c29-08da4a01cdd1 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:21:23.5908 (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: JHrOC8FivjUt+RQ/6nvDeVYA9jL5WMxe1ZLdaHzKmDKr8aWEm3ZpPgkgg805HuYYuBIxypm5el4/N/SmyT7DSA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR04MB8790 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); @@ -857,6 +859,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); @@ -2239,6 +2242,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 Thu Jun 9 10:21: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: 12875178 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 BEC7AC43334 for ; Thu, 9 Jun 2022 10:25:45 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.344942.570580 (Exim 4.92) (envelope-from ) id 1nzFLx-0007Sy-Hz; Thu, 09 Jun 2022 10:25:25 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 344942.570580; Thu, 09 Jun 2022 10:25:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nzFLx-0007SU-Dy; Thu, 09 Jun 2022 10:25:25 +0000 Received: by outflank-mailman (input) for mailman id 344942; Thu, 09 Jun 2022 10:25:24 +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 1nzFIR-0003pZ-Ts for xen-devel@lists.xenproject.org; Thu, 09 Jun 2022 10:21:47 +0000 Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04on061b.outbound.protection.outlook.com [2a01:111:f400:fe0d::61b]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id f70c16e7-e7dd-11ec-b605-df0040e90b76; Thu, 09 Jun 2022 12:21:46 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by DU2PR04MB8790.eurprd04.prod.outlook.com (2603:10a6:10:2e1::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Thu, 9 Jun 2022 10:21: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.5332.013; Thu, 9 Jun 2022 10:21: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: f70c16e7-e7dd-11ec-b605-df0040e90b76 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ITIHncsS5i6juTV/OQ1dadMZFTNGBGE/cDmro+DuVd3y3qytYIV8pJDYuJqyS7askxMnYDqyx6se6bIrjEKnj6wwrnmM/K/yAY1K8KjaP6rKfkAbDDhsmOfTq4I5YgYJ3/9wwBpHHe8QZgPkL4n5lKOf/i6vLgalwDK0GCd1h5YBvI7bCY28UcLXF05alB8GO2n6ULR0UVniV2NdU65mBI4N8eNeVAO1yQqqrHb15YQ4A4Tjz2gnM+penn37RpNXWrB9OU0hBkZ7oqCB3MYsGpWkGHQVsteug6pn6/PJ8dKRw/8eh8r91vVzVO1IP9xPkmUw6yhmhyGX9m0hW5ElGA== 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=VzF/WEsM4wYMSbleMTCRK+yfIaQ7T4UtmEmrBurN+i7sJ9D/f6FaJTfIbYyGHFLAlosf84hzdkYzHEJ8uljnKHNznzbKmcWONR1V6+EaQMoI25suRclxWdk4udo+rYJ8m+XwJ0fDQ5GZl9gksqC1SiOOOZs3+95C3iG2fnrUeFVmTePtfWuetkNj9aTvHwws/dcNzizYp/u3EyAVi5h5fvTnb2OX0rShux73uQgTfr+jkpdI7HNqoTb23tnHg+nng3uJYMqJGLe2ZhgKApIRnEYDRqg6SMyZpkaX1s1ImUzLcuGixrMA7d9f215B5pRgfskJ4AGq9QTLDeXbzYs/UA== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aNGQdoucDFou5j3M8E7Hd4miL8kBXhKfjN3w6aHaDIs=; b=QcorO23eUpXazUWkY+jKT6nWBUWoitabBH5iY6WmuJcNNzU4xUBUv4g/Lpey4CjVRCeEJrnhXzb4Nf18G2gHRXBScqT0p/WllolbAYXiacQsKpMvSn4WodxT+87oCubVmIVlnkdPAxU7gKBpWDTLWKDqS8KlSv5z9VIW3jr76RC+cuO8NGteOS9H5cpoYp3SiMTqwgnYhaEELWRNa9+HNb4IDEIV5MYMMMdVu+oA8cVX6cjSFK0YmUgaSco9hParby/AK5IbiRCUjGLPj4YdZaECmMRFm/rpCkRRdAkclDo+iEoNQUgU/qplFQMee/bhpBGrQQaB7jQfeZfiseRcCg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <10a6eee9-099e-59c1-7dcc-60bdb9902703@suse.com> Date: Thu, 9 Jun 2022 12:21:43 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: [PATCH v6 12/12] 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: In-Reply-To: X-ClientProxiedBy: AS9PR06CA0473.eurprd06.prod.outlook.com (2603:10a6:20b:49a::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: ffe31b03-f075-4088-0a26-08da4a01da72 X-MS-TrafficTypeDiagnostic: DU2PR04MB8790: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: PC4veMMXI+eII7Wml7Hvnh5o4eAhZtVk5BnBJXZ1EBeawvLsOsRgzwWB+x8gdGwFP5EGw5QqwQEnNaL9K8500w5q+2s43kq+1reYYCHUHN7TIUseiRFmE7QoUho1BA6u6CiI2zHNSM3JwnrOsrbWVP81a57jdlqyYDNeh4P9xhx1+yZYZkcsjq10FhgPjhTwwgX1obLXjpkmSaAu3iia77/x0q3VwAyJnKucV/Fni6oNJDaH6JQ5Uz54SMrL+RSX/6FUJJgjYw6f6Q2EbXn9aqDu29LgcyOPOYLAVi1Jb/iFSBNKaPmGQOLfsxaOJLrwVAwFRqIb5SWAywJg/fmxwwCSTEuU4RgfZHtBL5+IVyzcQMnD2GtNEvWLOsWfDFcWr/M/ndESGZ6w+SIsyEKsWlWCfF2Iv2HCryqh63gcdU824keqEePAyBUnu56JM38jqgdRimLolg4wvzAGM1yqHvPcwbCJc+VQu2ClImJVbFI0fr/ZuB60ebH9zOBDL5SAxxVHTDx9CDta8vwcyY1z9pQ3aGCnUAmnc1wfvxWa4SAPOjP+fCB7LCliYMLM3xH98tYSVUaAHGfvabQd/72ScPn8kCiVvt7c7DrhRpPTaUXPKP5rDsNC8SZTgaytUY7frFyL8vvOTjK2aLsLd51MieyyZzkCCu5u26/AGjdeK4CYKS5+O6mdA2z7WAruXg09aZaAs2AI7zIBFyUjQczgWdlH3sYuNy2dD8ypd7Ak9+c= 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)(66476007)(6916009)(66556008)(8676002)(4326008)(54906003)(31686004)(316002)(31696002)(5660300002)(6506007)(6486002)(26005)(6512007)(38100700002)(36756003)(2616005)(186003)(8936002)(66946007)(83380400001)(2906002)(86362001)(508600001)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?Mg+nwEHZyF9o9pxv9wYhrzsCjAcy?= =?utf-8?q?utHrGwxCHlDnnXg1TLB3qvB+l8yKCLJzaMgxbRX4Dt+GcDp15ThuBaXYQ7JEHREff?= =?utf-8?q?wzj9RQ1HeGNP6zHc71U6oe7OrMzXNLkMJMhTc271ZvhDb+NvUEaZ3ID9nQBRdyv4L?= =?utf-8?q?RqZwBLXwxpW2u/RamwFvfo6E+HIyoAqlQqoh/nCZPv7EJYltOq2qxfhIO27UZRQBR?= =?utf-8?q?Bk1tadFbjkmVt3+siY6l04CVD19Gew2BKw9Z43drb9zOGKyKi5jCM4XNjOSSX+WGV?= =?utf-8?q?htK58ICGIIdoiEWtUFuEQNeUTAimykus5K8FbFf0vGZCuRqRGG4SSPXntU1ptrJ3u?= =?utf-8?q?jzw/0zMQQzAkZcFdJUXk1ElIBWl6BI8Y95qQVh+9yZIMGNz+0DRTW8blFWxZKa7ZS?= =?utf-8?q?Evny13GCliQMg1sBsUBIpFcxAm8qMCJGxn9oAhexctamiJtHMF1nobzqShxDnAAk8?= =?utf-8?q?tYNtD6zUei1IaBg1JoKUorpXmHZzXAdFVKL/0m5U0NxEiz3R9Be1/si5cghGx/flZ?= =?utf-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q+BrCH2Mgi2JpiwtgCi0B8P3CfFNji4WQfkQKI9VjR3faofPN+6SMVLF44?= =?utf-8?q?Fuksk2qMrqZHcENzoZNxgIwnbTevpeSAb4tnnclVTCO8wJJ5q2eJIlyyHB4Y9diXZ?= =?utf-8?q?k+1ChMe1iCOSQb4kJAxZl/adnXUSwKcPUMAcuNgowuT6Z++6kJe46Bs40YwxqyQKx?= =?utf-8?q?96ga2UhmnxiirbyVZxJV0Bf6J+ZSR77Fr9nGRjbW9eAtWlDnTufW51wOzFFydxph3?= =?utf-8?q?PXCpnFovIJJA6vsnhj6XhOgA6rTeHm+A=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: ffe31b03-f075-4088-0a26-08da4a01da72 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2022 10:21:44.8082 (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: NE19DrHYCw9eNRXiHFZo5CthOYUaSwPVAjtq7php6oBacDI+uMdBGv4PB5rlLAbcBY9Z5u7/mJQRS9Sd+HLqww== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR04MB8790 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(