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?CtU1Or7rt19T6K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N00qofDq9RCLURx4odhshBs8MZlVnBPiWu3bPVN1Gx?= =?utf-8?q?T9Kn3SwZefblhUxpXBpA3NX/Uy975gisW7qI4g35wN52YPNW67hoxLBQkQCxSsp7T?= =?utf-8?q?E186JOVP2lblmJt8ctvbF93U4LY6OBA4f2VlWv0WnDsbqA5hrX02oswiIdjT1HesA?= =?utf-8?q?nYWMu4bpbRr3wOD8BFkZkzR86Hurw17AY2k9PGX2y5pOERPyCdR58wNRe7b5/Q5V2?= =?utf-8?q?caRKKO/ktnY8IrnlnrxvngZxtJszoqaNBOU9hPoUOHSwFiiv347PCaibDAE2mXREi?= =?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);