From patchwork Fri Sep 23 10:56:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 12986447 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 B32CFC6FA82 for ; Fri, 23 Sep 2022 10:57:20 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.410718.653851 (Exim 4.92) (envelope-from ) id 1obgMl-00008i-Fo; Fri, 23 Sep 2022 10:57:07 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 410718.653851; Fri, 23 Sep 2022 10:57: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 1obgMl-00008b-Cm; Fri, 23 Sep 2022 10:57:07 +0000 Received: by outflank-mailman (input) for mailman id 410718; Fri, 23 Sep 2022 10:57: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 1obgMk-00008V-8U for xen-devel@lists.xenproject.org; Fri, 23 Sep 2022 10:57:06 +0000 Received: from esa1.hc3370-68.iphmx.com (esa1.hc3370-68.iphmx.com [216.71.145.142]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 713a8128-3b2e-11ed-9374-c1cf23e5d27e; Fri, 23 Sep 2022 12:56:59 +0200 (CEST) Received: from mail-mw2nam12lp2045.outbound.protection.outlook.com (HELO NAM12-MW2-obe.outbound.protection.outlook.com) ([104.47.66.45]) by ob1.hc3370-68.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 23 Sep 2022 06:57:00 -0400 Received: from SJ0PR03MB6360.namprd03.prod.outlook.com (2603:10b6:a03:395::11) by MW4PR03MB6865.namprd03.prod.outlook.com (2603:10b6:303:1b5::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5632.18; Fri, 23 Sep 2022 10:56:58 +0000 Received: from SJ0PR03MB6360.namprd03.prod.outlook.com ([fe80::861e:b46c:1acc:c576]) by SJ0PR03MB6360.namprd03.prod.outlook.com ([fe80::861e:b46c:1acc:c576%5]) with mapi id 15.20.5654.022; Fri, 23 Sep 2022 10:56:58 +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: 713a8128-3b2e-11ed-9374-c1cf23e5d27e DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1663930623; h=from:to:cc:subject:date:message-id: content-transfer-encoding:mime-version; bh=azB7uA7BlYQBKFYyY0T4fUkuZL+s1HPnWMTwc7kcd+c=; b=cAENZIQMYNSdh60kfp2OhDaTUN8T9JaUP/fl2UZ4Cu10UYISb12+In5P 8rCYZ62p2gHU7Dk8ZKl9z1Iufj//xKhtp0F7oalZngrgO2G2nVMPJYalw dPR5JXHJKNu8ghgyW9co/Sb6DHdig2FLCen7TWTWI7jWsBHOy6noubMx6 k=; X-IronPort-RemoteIP: 104.47.66.45 X-IronPort-MID: 81630643 X-IronPort-Reputation: None X-IronPort-Listener: OutboundMail X-IronPort-SenderGroup: RELAY_O365 X-IronPort-MailFlowPolicy: $RELAYED IronPort-Data: A9a23:h48JaKxnjH4TJCdi79R6t+f2xyrEfRIJ4+MujC+fZmUNrF6WrkUDy GsaUTiDMqmCYTagKtx+Oo7g8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ALKhY74E3W5AnYoYLSIly95FMzQjlPybAt/S zslBH43Lyqt7WexgXH32HewpxKkJ/Ky8dFBuaLls8JQw+c/DqAVcBEYfmvrTo1qOag5BIDl8 TNmQ4pO4BJ53bYbgiO0GnQ8jil9Axrx27pyFeej3emi9f+XigGB81Igp8cWgfF6mI71esMnZ 5j7ia8jd56HBnAlCPy65zjTBdxjHe5pnIkjKo6k2Ffa40Dc7VcxLZvtn+9KK1wUx4S1bpXXt WHVKrnlbdrmBKhHjvkV1BUsZCRti9ZJGbHfqAA0vbloAS+0koJjHfw//Zv4EvoxKhNN6Ws2N 60TZiA7Is+KPP+TZgNcdvpEvHHflDlcFbrDF+4B2jBOeUuB0/twqSHkIndotvaMKA18A== X-IronPort-AV: E=Sophos;i="5.93,339,1654574400"; d="scan'208";a="81630643" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Vkm30OQAaYK7L9W8ahCWPTC0gbc03Yz/ccPvoo8lwYGeT4Din71CQ49NZtYljuTDw6HjGhd+QDYZcSGStiRJXaSl3Pcn93jduYbswxruanZIU1VxdJCHz5Wmxf73Mn5Ls5CAXQys+LrVSGitwBMYrDUI9L61cJes3rkxNwmInXCt63lhFszyOeIylD4of/Fu9ST/Ui8HckXKmK+eVTyqrRSYU8yghhu5bkOxTswbGmam6hEYLPHcD+1eIrGnpawidkuasPKL23uKDwlZ9xtxve3aTFcwNZCMuZWy320NDCcthUSManpa0YeszKXzVceLWiCmaqeIEwzfWlkQESjh1w== 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=7fAJBf7y+p80xGCoM1HkmotHKEEUd2bdFx1zNnIlwxM=; b=SYs270M6XGeOMAjHhYcwg6w2eXPZRgiPxHDu/L2xzMu737Wfqbsh10y+MUmSMcjK0ypjTkiFqZQ4t2ux/tXzLhdokVDqmhfFiyf82GdUPySyye+a8l5bt9Yrd+3GsheWkLhmi2A0UItKy2kFjzNw5duuWCGZfJGyCCisnlBxB8TVl2seBDjuq0iGmB3S8K0P3dI7K/SEqkYJSQ4OU6p8nXiXNgl1jq1ZfFVh3PBwtoFeeNJW1fEBDbDCgBGbz4ZLwxIXh/RCx40l2bmoaNTnwi/SdpDQbgFL+o2Ev3J+NydhHnUbE1DJiA2KJkAEV/f6NVatQe1UcKDSesmi6n+m7A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.onmicrosoft.com; s=selector2-citrix-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7fAJBf7y+p80xGCoM1HkmotHKEEUd2bdFx1zNnIlwxM=; b=GfZgpFdFpUMP+BgEw0JbRFpfyBxOq1pp78/Yc6AmQAljScT6mcd3lR+uIiXGz8cXD3CRc4EDm6RbAHHCwWBE8IQGDa8OIQ85+fvpOg+A0/tNHoPFUmRYulwxuZqBbBd7/uqWNuBR34/juZmLMAdY4Qm35vISaXk9UQynpxbQJKQ= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com; From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Jun Nakajima , Kevin Tian , Jan Beulich , Andrew Cooper , George Dunlap , Wei Liu Subject: [PATCH] x86/ept: simplify detection of special pages for EMT calculation Date: Fri, 23 Sep 2022 12:56:48 +0200 Message-Id: <20220923105648.12587-1-roger.pau@citrix.com> X-Mailer: git-send-email 2.37.3 X-ClientProxiedBy: LO4P123CA0415.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18b::6) To SJ0PR03MB6360.namprd03.prod.outlook.com (2603:10b6:a03:395::11) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR03MB6360:EE_|MW4PR03MB6865:EE_ X-MS-Office365-Filtering-Correlation-Id: 25a2deee-5dc9-4e73-7eb9-08da9d525597 X-LD-Processed: 335836de-42ef-43a2-b145-348c2ee9ca5b,ExtAddr X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ws9umJc1OZa0fUUw1dh0oHSp4ZOY3G9R+dMtiF6ZhF0tWyFwCHvmiBRqOZLxTv6+izFBkHY5lMZr63qt3jaIV2urgmSXCmJvq52Cu5KtFYqVobF8C9JQI0UR3CP83xD1aoIKpRFWBohIVpSRALmNd6vsRVWKp3CCbf3SNWKHkS3BfVGeaPfPKjAr89aZ8w8jhP7JV+znclOJAUq2L32eYQJWxNx+42C1tXqHNSm78e1P+Sxjo+nwz+JTgis6st/uEQiSI7YUZE2GmRO+eJys5pM9/MIbrDsdJRROc6OfFZySyXlQ4XGg+JOwyZkj/qplWPw7b1QSsPujOwuJ8z21FcOAShA5lLHaM+5VOptgmuB5M1qZeqxv2FhcYMetg+E764bQmxLLgcrs6MJ9VYqEhMCixenMej/sqxWWvzquPXbXRQhKvzm3KNP8W98WUBg57ViKS+7FhTS0UOlBCEYwIopB7spXZS7munSu7yXnw4ye9wJWxZPrX7JmO6hLtxxkgE05SvhofO40Hswsh6bAu3YEoJmbm5AaIAdzzpUeB9wgo5xMFz45McyI9DT1avvYiFfXv6UzjMoQV/eP1uK5wnMEUyxREKHnzXybO8pmEdhuqH3Z+x5yzLijeIMYDvt5oEFyNGTqmIbzx+DjMmcDE83IUdTgbaxAr+g7MKWgh2XcpZISqi+Yz9gm1KGa18g07zKgm43QvcAycIxVEihiIA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR03MB6360.namprd03.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(376002)(136003)(396003)(346002)(366004)(39860400002)(451199015)(38100700002)(66476007)(5660300002)(83380400001)(82960400001)(6666004)(54906003)(66946007)(86362001)(6916009)(316002)(478600001)(4326008)(6512007)(1076003)(6506007)(26005)(186003)(66556008)(2616005)(6486002)(8676002)(36756003)(2906002)(41300700001)(8936002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?HUWQHEIm98cJE9jqX5fYwPdo/Uso?= =?utf-8?q?YKCZRNHv2soNKnbbvePD6Mz/YIHVtMp+CQUnPbY6OjrUEZrGltXswUSDbevKavdjW?= =?utf-8?q?cjIeUFmoR0hj5AcR3ZK9BQYBGIGUZ9/c5klokbxdPfwLjneafrnT+eV6BKh3OUtVK?= =?utf-8?q?R9JB/nRh6UPfcW8KxzzlmsJIXPu/rMsRMok44xspvfVT2SXOdUXF755hp1pwXE1DX?= =?utf-8?q?mqOSfDNbz/X07Wko8GsHTwUB2h7jRYdyVjmtRM2GlZGikd3DJSfAEG4biYgKKRVwO?= =?utf-8?q?fAcESiaxhUP0KDLHwhYeRkaodv2kWAdelXZcjOq/xprq8lDQs9rr/Qy34t98rvBNq?= =?utf-8?q?jACHgH2lMUH5h7fLmkVEurDno0GiM9G5AJiXUz0mITNzYmR6dh0GOjzbZBAeKyAPp?= =?utf-8?q?g8tPZsN6WB0jRkyTnQ1F2LemI58DvB7jihBcWwRb4OxPIDwlnnCGp1FcZnsSo8HxP?= =?utf-8?q?VTLB0KBZkioW+yhWS+VFRRhqugUuSkY7zg3MWxTk/2clv9YBORogj9hIHoGrr0TYW?= =?utf-8?q?v5IK2pEth0eneh0+g3NoC5Xry6iLzGIdd650IvaGr2HDYamFrfzj62k1QedwLL1cb?= =?utf-8?q?bXWh8XYCQyvoaUIu267TL41Vn7G/KTs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yL6v9Fj5fQeU0fh6rJ5kN2OJRqGKObBxocwbDFRnW0paFUU?= =?utf-8?q?6Jhmhsnbj14TRCukXKuW0JxyHcP39fFUtg=3D=3D?= X-OriginatorOrg: citrix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 25a2deee-5dc9-4e73-7eb9-08da9d525597 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR03MB6360.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Sep 2022 10:56:58.0206 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 335836de-42ef-43a2-b145-348c2ee9ca5b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: taNVh6fxlDsDqBiWNY7Y+6KsIuorocAY8/eC5HtgbfLsrWE4S8zkM26uZ0JGaA77wOaez9HZQ9L8J/OBTCivmw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR03MB6865 The current way to detect whether a page handled to epte_get_entry_emt() is special and needs a forced write-back cache attribute involves iterating over all the smaller 4K pages for superpages. Such loop consumes a high amount of CPU time for 1GiB pages (order 18): on a Xeon® Silver 4216 (Cascade Lake) at 2GHz this takes an average amount of time of 1.5ms. Note that this figure just accounts for the is_special_page() loop, and not the whole code of epte_get_entry_emt(). Also the resolve_misconfig() operation that calls into epte_get_entry_emt() is done while holding the p2m lock in write (exclusive) mode, which blocks concurrent EPT_MISCONFIG faults and prevents most guest hypercalls for progressing due to the need to take the p2m lock in read mode to access any guest provided hypercall buffers. Simplify the checking in epte_get_entry_emt() and remove the loop, assuming that there won't be superpages being only partially special. So far we have no special superpages added to the guest p2m, and in any case the forcing of the write-back cache attribute is a courtesy to the guest to avoid such ranges being accessed as uncached when not really needed. It's not acceptable for such assistance to tax the system so badly. Fixes: 60d1adfa18 ('x86/ept: fix shattering of special pages') Fixes: ca24b2ffdb ('x86/hvm: set 'ipat' in EPT for special pages') Signed-off-by: Roger Pau Monné --- xen/arch/x86/mm/p2m-ept.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/xen/arch/x86/mm/p2m-ept.c b/xen/arch/x86/mm/p2m-ept.c index b4919bad51..d0e1c31612 100644 --- a/xen/arch/x86/mm/p2m-ept.c +++ b/xen/arch/x86/mm/p2m-ept.c @@ -491,7 +491,6 @@ int epte_get_entry_emt(struct domain *d, gfn_t gfn, mfn_t mfn, { int gmtrr_mtype, hmtrr_mtype; struct vcpu *v = current; - unsigned long i, special_pgs; *ipat = false; @@ -518,26 +517,19 @@ int epte_get_entry_emt(struct domain *d, gfn_t gfn, mfn_t mfn, return MTRR_TYPE_UNCACHABLE; } - if ( type != p2m_mmio_direct && !is_iommu_enabled(d) && - !cache_flush_permitted(d) ) + if ( (type != p2m_mmio_direct && !is_iommu_enabled(d) && + !cache_flush_permitted(d)) || + /* + * Assume the whole page to be special if the first 4K chunk is: + * iterating over all possible 4K sub-pages for higher order pages is + * too expensive. + */ + is_special_page(mfn_to_page(mfn)) ) { *ipat = true; return MTRR_TYPE_WRBACK; } - for ( special_pgs = i = 0; i < (1ul << order); i++ ) - if ( is_special_page(mfn_to_page(mfn_add(mfn, i))) ) - special_pgs++; - - if ( special_pgs ) - { - if ( special_pgs != (1ul << order) ) - return -1; - - *ipat = true; - return MTRR_TYPE_WRBACK; - } - switch ( type ) { case p2m_mmio_direct: