From patchwork Mon Apr 25 08:43:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12825407 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 6D1E4C433F5 for ; Mon, 25 Apr 2022 08:43:34 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.312583.529896 (Exim 4.92) (envelope-from ) id 1niuJV-0006Kr-K0; Mon, 25 Apr 2022 08:43:21 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 312583.529896; Mon, 25 Apr 2022 08:43: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 1niuJV-0006Kk-Gt; Mon, 25 Apr 2022 08:43:21 +0000 Received: by outflank-mailman (input) for mailman id 312583; Mon, 25 Apr 2022 08:43: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 1niuJU-0005as-5B for xen-devel@lists.xenproject.org; Mon, 25 Apr 2022 08:43:20 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.109.102]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id c179822f-c473-11ec-8fc2-03012f2f19d4; Mon, 25 Apr 2022 10:43:19 +0200 (CEST) Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-ve1eur03lp2055.outbound.protection.outlook.com [104.47.9.55]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-3-b_RJjmPEOLKgT5WnXpataQ-1; Mon, 25 Apr 2022 10:43:18 +0200 Received: from DU2PR04MB8616.eurprd04.prod.outlook.com (2603:10a6:10:2db::16) by AM0PR04MB6098.eurprd04.prod.outlook.com (2603:10a6:208:141::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.21; Mon, 25 Apr 2022 08:43:15 +0000 Received: from DU2PR04MB8616.eurprd04.prod.outlook.com ([fe80::5cb0:5195:4203:7c2f]) by DU2PR04MB8616.eurprd04.prod.outlook.com ([fe80::5cb0:5195:4203:7c2f%9]) with mapi id 15.20.5186.021; Mon, 25 Apr 2022 08:43:15 +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: c179822f-c473-11ec-8fc2-03012f2f19d4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1650876199; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=o5dMhpfznMlqVzr+kJ/53g9HZAHAFuJCF88KGG0p/Pw=; b=gskIEYe9IOm7q3QfsHWYC2um8rRth94hdRAjXOeqLzzgOlwkvnjnTtnJ3qyM3ZYwNpcL7i XXfZ0yFtaVjMo+1Cw6RWnPhv0CqOBmpH9/wALLZ1Pj+sqc844dOQAtCp799P6Pj9JlMs6b dNoRD+ioyrDskObMH6tsdZfh7vw++zc= X-MC-Unique: b_RJjmPEOLKgT5WnXpataQ-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZftQTbEYgdFI+6kFvVAR1LxW+gdwYPevxgRcxbAjkJH2xeMrt1lhDdShjj4WxGNyXpSN8/wAYkXtG3mNdNvLiSiLehSEP1GOWXE7OjIFRwb8kL+waytULHoEbRruTU5us+n3Zp5XGJfkLAhGj3Mkf/aD/7K+HGjvICVAmXDXazDxdLVvnBxFr0HBJW5gQ5IiX54qaWTzMNagqB0pNz/bSqyOmiLXIFmBMvSajgYZSa9Ofn/DrLFImniq8DO46z2FsH5wV9XUAHA+tlVcNDWtNkdTFBzvF7veo47iWWYTsqrh3htOuju3MwoMm682VsCRL/yLNM+0ZiAie4PQIaPDXg== 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=o5dMhpfznMlqVzr+kJ/53g9HZAHAFuJCF88KGG0p/Pw=; b=hPrybism78QkDSlKfu4XCcxGX0U2vKBDRgDyZNGShWVF9DJlzW21kuN/2eom/yaoHbd6QkU2k1e/B+s+yoPc0B2lk5x9RgUItu2UTUcGjf8JsgYoO70kT/W8Yz5j8sB1TuWApNa/GJDFERrf07Eje1SkYjYHr/OHIqIayTomzPrI4sRTSnPw5GF6JhmHb1pu56K5gVcNFEpWvc8VwvaeDgwMs1I03wx3UrFjHo4tqOSYppSSkezTBcUZWagYru0351ZSQ7+YUn5o0HiXmu4d1r/5j5MjR4BvBfA/EdJ/scN3tm0BXRvLb7TKkkgoOdIoc/hdmC1nHPxbUl4rtm3Yyg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: Date: Mon, 25 Apr 2022 10:43:16 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1 Subject: [PATCH v4 17/21] 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: AM6PR10CA0100.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:209:8c::41) To DU2PR04MB8616.eurprd04.prod.outlook.com (2603:10a6:10:2db::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0f182e83-4d2b-4a78-34b6-08da2697a387 X-MS-TrafficTypeDiagnostic: AM0PR04MB6098: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: ytsBkCaiyoBdFjY91ut+WHsBc5lfa6NiEnTAJYQGLPRDVwVIshYUIGksbHMJmMwvoBhJKIDKjXXXXtW06f6og/+du/wk0zu2/8OqHw4opGJMo8fqCpit2HjL6DrzVkr5XO909B2v/jl+yb29yqWC/kE+2amqVj5BpRKJoc533Mt/MZyvDNP8AZAxYRNmaWNu2+jIi7g+AVw1mIoy5qqKmCmkCcrrp1ppHuV65kPVtarNyBpAuChiI0YkbK/BkzeaXM4rWkRhh1wWil/KBhe8BJsYlcFcR1bwfb8KQRtp34aEfKtZOShA9pAAdJi228p80QvWqochXLt/2tbvZtsaE5N9W84360ojKDwSl/z7ISzBoaRNO2arq47qReNO0FoLbk66JHVYiQzRX1XDs0FbyFPu95Z0fz9mNYA2RhKl9i6HyAwDhZ0rhqWbJ2oT/FtT4EIwQwO3bMdoyDvUCSKZXXlv0QsH5WAR1p+GDeQ0ljJqN4zunCwgGLjbDGDQioa5IQixe/vbZc6heBBl70NgBZVhVjFn/82Y1XdVeEyyDgrxjKCun+E4PI0CEg6ZgA7UJNAY+pnsSnW1Y2wF3Dvl8ivLJddl+xVMLnNUxCXGTVf+FWYT6HuCEBLC9hB20gPSWpiVIPVIHrnSmfNVSJFlntt4znH8KIzFGGvClAfOBZ2kqO7xf4M9cNl8voKRQ6JIkFifhMDqBT/Kqum6SaiX3hbT8nwu5qvgEeyas9rDFcE= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DU2PR04MB8616.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(5660300002)(2616005)(508600001)(8936002)(86362001)(6506007)(6512007)(2906002)(26005)(31696002)(38100700002)(6486002)(186003)(83380400001)(66476007)(8676002)(66556008)(66946007)(54906003)(316002)(31686004)(36756003)(6916009)(4326008)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?fQXg3czNMoecGfip8u4on5dsB1Cv?= =?utf-8?q?hhD6zfHa3PORa+nhMXjni12iOb0hiewSsW4r3a3cpdgKP3hFeOxQVMxUsLniu4PQh?= =?utf-8?q?Lt6bWQpPKxetD7bFvzQdI+iGIQuQY0zyWIAnIK7/ScHXN1tY6U5POLO4put996ur6?= =?utf-8?q?Cdi/Y8+VMEAUaBkBcKO97dJmV4a4pqFebWDR3cG//8eJZJxXOATMYRZhUW6Q9DWwn?= =?utf-8?q?M3b8M0lKSTy7NmANPxETSsm7NLjvxzWavwwj1e9URWcHQbaPI7efY6BFWEmUe0wLZ?= =?utf-8?q?isDbKDqI7srOJDOX2nAlE6NA3Jia+C7hWGHPyT3SJyyjWRnOIRGl9I5KtpZeVmuJp?= =?utf-8?q?8wyDxA1SBUv6YuY0VmMxx/wW2QXNdI4G7XB8ADfkKjB5Ll4w0MVt7G9lM3vrxazb4?= =?utf-8?q?zV3dQ1CRod3SL2qOlYWE0Ez4TuXNFgSF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22PseHUH3e?= =?utf-8?q?xC8FfWhgPwEeb5qshZsuuT13KAErVnJYDzLnToNzCFjgLuAT1nNPgkJi24wjNS/W1?= =?utf-8?q?JxHznX36/Ikr+jOPqEZwXXtkHmARqARlVgn/80+ulsSWPlnc1yfE0M4lZFMFE2y+7?= =?utf-8?q?dRyuxDOEBoU/BUi1R71X4CagBcp5bha1MPZ4ZY9UXosvg8vOTC7/h2ksRVu+Stwyr?= =?utf-8?q?73MAIlW2h7mJvJg9yucA7h/K1K87TKINLwh4CqSvFJ4r7ObLGf2OzllU0Wyuw1jYo?= =?utf-8?q?0RPzmBYmcDRjAl2Wt874BrGFLM/u2qVABOvf4Gu/dOoy+kfFAxQ+xuFNtt4t+VPj6?= =?utf-8?q?oKwdlcdFA43wLMfwXz6D9fhIRe3gVkZg=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0f182e83-4d2b-4a78-34b6-08da2697a387 X-MS-Exchange-CrossTenant-AuthSource: DU2PR04MB8616.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2022 08:43:15.2748 (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: agMlyq3K+fhi5YuXGvetiyiMKMi6oafnFHppyh2d4gNy0WJhVsZb9SsFfVrYfUX3Q7gO7gM4O8p66QuC5Jrj2w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR04MB6098 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); @@ -407,6 +412,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; @@ -447,8 +453,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);