From patchwork Tue Apr 4 17:23:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Lendacky X-Patchwork-Id: 79320 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp76766vqo; Tue, 4 Apr 2023 11:22:10 -0700 (PDT) X-Google-Smtp-Source: AKy350ZOCAMQxYmbrhIpMpqf0ClT50cC3wOZ2Pt54uHGsRGnGvs4Uzz1Rg0MKHWds9H+S4sNNn+w X-Received: by 2002:a05:6a20:89a7:b0:d9:91d2:b81e with SMTP id h39-20020a056a2089a700b000d991d2b81emr2709022pzg.62.1680632530242; Tue, 04 Apr 2023 11:22:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1680632530; cv=pass; d=google.com; s=arc-20160816; b=UBEc4ELzfwbKyof5sxWaaUOtRjqK3Uo9X4roV//kFLhoHCVJnRoQbgZ56OjmyLmyrp srtOMe61TX3s9qm/nbrRJA5EEUDslw/5gE+QrAU2OShp+qceeRASx9NQ43aR2fZXBPVt iFtSolK+qltPn4fRSvKGIR1l0QaTCBvYEtHO2wwx9zuXVbLux5J00pTjRI84NtGGWVaC kU/o9DZz8sohBeD0fjXWDL1yYaXrL07ADwPIQiq8JUFTj4XlQxw6FhIShrrLQ6Syjx2r 3sQ7K/qZzOpZ11rgr2Cb6UwoCxx3bjt1Q9SBrwPbswlZKpktloxI35hKOOMjQBEJB7Sl oIbg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lXqpsRUcfiP69LtV94IcK09xDevFBvjDWYCNMYwyo1A=; b=hIwVX8uo5+TVVfj6KJvYU9KnV/hzZ/yh2q6mApIebVXTco/uftlNad8Iw1OO3KPHML L9fRu2XkKyoiI0v2+nF+yVjvZcpd9t92Whd9sAne24A8RcN4QT18afgU6hlsUxYNh5/P VOjIPy4WBNfeISlpQFX2p28VnDBDy/MT3S82Nvf1GVmpplGe1TLMbw51wV8QZfIXZuK3 Y5qqewwxMqAAO+PM/QbcKqMzfMP/vHblNhKz5vUBb+yAzvMLFpy6ZL/3yx1SmrLz6AAO ZlnMuaSe0whYOv5T+zAe9P42ai82STtuwyCkraUKmuB0y6RQxFVhhAiQk5VyL1f5oTcW S0Eg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=GBPBDfiv; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x5-20020aa79405000000b006280cd49409si11172914pfo.21.2023.04.04.11.21.57; Tue, 04 Apr 2023 11:22:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=GBPBDfiv; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236332AbjDDR0Z (ORCPT + 99 others); Tue, 4 Apr 2023 13:26:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236219AbjDDR0H (ORCPT ); Tue, 4 Apr 2023 13:26:07 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2041.outbound.protection.outlook.com [40.107.93.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA0CD76B4 for ; Tue, 4 Apr 2023 10:24:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=n+j0+NPkjGlYX/cHTMVUUUCxIcZO11dwTzpNe2p2QMQlfefw+9AeeFcFdlEsnKnGZfj8tRvyGsZoKZ7DBaSeeo3OBiRAL2Dl1qyci6JnR23BQg8/JQcPeLevYlhO2rvz5fGv0YtAiuXmhsR9UcTGmyoAdtrMC5ad4jxat5ATGYVlW7w7s9AYoxnNQ95C0Ka6nsMNfmOX5WleBS6HvPJ39bSFBYlx9Jg1/w1lSX9UWkKTaMocWco0rJJkGt1uVsFASlUfjyUsVmuE/OicI5pUzwFPBPGKI6jRPQldyE2GVL7dtik1gZ5KEq+l2VJP5XLV1Lg6TYLyaTYD3IFbgy+Lpw== 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=lXqpsRUcfiP69LtV94IcK09xDevFBvjDWYCNMYwyo1A=; b=kaV/Cn1lxa0ITWi1VIlL9F8LK8LYRvBGUox1TDecdTmQSfIJeNlX+UrsRWx7Xu3s/GkZcr+nTpR+s2ueheUqqyb7o02RadjSnO30O6/jLYfd3qkOZLXX4dmEq84Nm8LVR7DK0zvZKT/vsMi6Pl4Obqk9Nle6kQqjC/ud3FbMK5o9EtzFBj1Y4+awxh4zVKlbe0Fpgs/wej5tqy/y8e/QZIJc1AshIMI4eAPi/LE7BXCNugUvgWk+4AZFMI3zq0RULORxFvw0pu4Yrc6T/r07yV4WsFgDBulhXifU8l9ek/3nHn68+FJeIIAhsSuppX5UiuMHhgXkCbUk0KmYhuRwkw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lXqpsRUcfiP69LtV94IcK09xDevFBvjDWYCNMYwyo1A=; b=GBPBDfivo08bv9ALZPsZEFJDsli+B9XwtRYXzr4DCXvI0Dw1ytu4+PLMfboIBxDxsmVIgog7O722vKHbuGW0a+pQt3xHGOc3NQ2T6tJwnuceMRK/+QMjYo50Dc58YFieumsjw5mUvg4LyKGpWJlVmHbVbukK82ckXJNpUxphzQ4= Received: from DM6PR06CA0052.namprd06.prod.outlook.com (2603:10b6:5:54::29) by MW6PR12MB9017.namprd12.prod.outlook.com (2603:10b6:303:23b::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.35; Tue, 4 Apr 2023 17:23:28 +0000 Received: from DS1PEPF0000E642.namprd02.prod.outlook.com (2603:10b6:5:54:cafe::a1) by DM6PR06CA0052.outlook.office365.com (2603:10b6:5:54::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.20 via Frontend Transport; Tue, 4 Apr 2023 17:23:28 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DS1PEPF0000E642.mail.protection.outlook.com (10.167.17.198) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Tue, 4 Apr 2023 17:23:28 +0000 Received: from tlendack-t1.amdoffice.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Tue, 4 Apr 2023 12:23:26 -0500 From: Tom Lendacky To: , CC: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "Kirill A. Shutemov" , "H. Peter Anvin" , Michael Roth , Joerg Roedel , Dionna Glaze , Andy Lutomirski , Peter Zijlstra Subject: [PATCH v7 1/6] x86/sev: Fix calculation of end address based on number of pages Date: Tue, 4 Apr 2023 12:23:01 -0500 Message-ID: <09297dfcd621898aeb3901efadb835299c770a48.1680628986.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: References: <20230330114956.20342-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF0000E642:EE_|MW6PR12MB9017:EE_ X-MS-Office365-Filtering-Correlation-Id: 0f6df53b-8d3e-43d3-8471-08db35314e07 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TZS9UtQveil+NGCjttQaGLHBzOd/N3HHQmc9LioJbFx9F+V22qlmzh36XRA6RYlWALZB+KKXKuuivagKdsERXvvhQM2RlGgUKK2Cv8fRUvDPuj69i1pFn0K21Uz8JjVBI/Dv7IhEsUPwTIOZeLAUDRH9PsgdNlLAbMXxWQJ1P6Ryvyx7R7/DLzvH30C2CmBMxkrNxGWVS1fZLuItXeLmLsnWfhcjQ+qHAZRtuY0bLnIfJXoNRCxWvL9hFFoQLVtcuDgumwVpQdB9c5EvLYuBqQzRPJmFpKNwhTogept6evfEeGmbc7f0V949rXVIO2I0MiCs6LV8gg2GK3UcjFCOpgY+xat6BynBdB+9E4mJJ52sWzQc18cEtRXq2tbyWLVEcjq2JtECAutcIM0B86EE2fH+WXWerbbDIG3qhKRP9kt7hwisehSbfBbygPj7E4pzuMGNbsSCRFRCQdl7sY9g4kbx9FK+PdY5+C2qV7cNjHcDXoR4NbHgEe02gJZ2KbgfBH5iDukpV55DomgK1Pnw5sLsuvZ7VYKnJ9CDcPdRurkNn17G+XiEsxcqocQy0IG0KyuSWyhTfvG1ZDT+abxnUJtlIjJcD5P4iQCV5UtUWKh0rvdGqdjQLH74GeYjDxSjUL2BZOsA+OzzkoEo1xRa6C0uaUgHVrjdYLbWOXWnhVrzVN9gA3ZAHTEIWYkP9hWcVDGgmQ/Vsjd/zYMMgfphOPHnt5W3+nL5zW45ghlp830= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(396003)(376002)(39860400002)(136003)(346002)(451199021)(40470700004)(36840700001)(46966006)(6666004)(47076005)(2616005)(83380400001)(426003)(336012)(40460700003)(186003)(26005)(16526019)(86362001)(4326008)(70586007)(8676002)(70206006)(2906002)(356005)(82740400003)(40480700001)(41300700001)(81166007)(54906003)(316002)(110136005)(478600001)(82310400005)(36860700001)(5660300002)(8936002)(7416002)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2023 17:23:28.1061 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0f6df53b-8d3e-43d3-8471-08db35314e07 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF0000E642.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR12MB9017 X-Spam-Status: No, score=0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762270936204986950?= X-GMAIL-MSGID: =?utf-8?q?1762270936204986950?= When calculating an end address based on an unsigned int number of pages, any value greater than or equal to 0x100000 that is shift PAGE_SHIFT bits results in a 0 value, resulting in an invalid end address. Change the number of pages variable in various routines from an unsigned int to an unsigned long to calculate the end address correctly. Fixes: 5e5ccff60a29 ("x86/sev: Add helper for validating pages in early enc attribute changes") Fixes: dc3f3d2474b8 ("x86/mm: Validate memory when changing the C-bit") Signed-off-by: Tom Lendacky --- arch/x86/include/asm/sev.h | 16 ++++++++-------- arch/x86/kernel/sev.c | 14 +++++++------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index ebc271bb6d8e..a0a58c4122ec 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -187,12 +187,12 @@ static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) } void setup_ghcb(void); void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, - unsigned int npages); + unsigned long npages); void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, - unsigned int npages); + unsigned long npages); void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op); -void snp_set_memory_shared(unsigned long vaddr, unsigned int npages); -void snp_set_memory_private(unsigned long vaddr, unsigned int npages); +void snp_set_memory_shared(unsigned long vaddr, unsigned long npages); +void snp_set_memory_private(unsigned long vaddr, unsigned long npages); void snp_set_wakeup_secondary_cpu(void); bool snp_init(struct boot_params *bp); void __init __noreturn snp_abort(void); @@ -207,12 +207,12 @@ static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { return 0; } static inline void setup_ghcb(void) { } static inline void __init -early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } +early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned long npages) { } static inline void __init -early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } +early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned long npages) { } static inline void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op) { } -static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) { } -static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npages) { } +static inline void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) { } +static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npages) { } static inline void snp_set_wakeup_secondary_cpu(void) { } static inline bool snp_init(struct boot_params *bp) { return false; } static inline void snp_abort(void) { } diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 679026a640ef..72ef1e1fef84 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -643,7 +643,7 @@ static u64 __init get_jump_table_addr(void) return ret; } -static void pvalidate_pages(unsigned long vaddr, unsigned int npages, bool validate) +static void pvalidate_pages(unsigned long vaddr, unsigned long npages, bool validate) { unsigned long vaddr_end; int rc; @@ -660,7 +660,7 @@ static void pvalidate_pages(unsigned long vaddr, unsigned int npages, bool valid } } -static void __init early_set_pages_state(unsigned long paddr, unsigned int npages, enum psc_op op) +static void __init early_set_pages_state(unsigned long paddr, unsigned long npages, enum psc_op op) { unsigned long paddr_end; u64 val; @@ -699,7 +699,7 @@ static void __init early_set_pages_state(unsigned long paddr, unsigned int npage } void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, - unsigned int npages) + unsigned long npages) { /* * This can be invoked in early boot while running identity mapped, so @@ -721,7 +721,7 @@ void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long padd } void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, - unsigned int npages) + unsigned long npages) { /* * This can be invoked in early boot while running identity mapped, so @@ -877,7 +877,7 @@ static void __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); } -static void set_pages_state(unsigned long vaddr, unsigned int npages, int op) +static void set_pages_state(unsigned long vaddr, unsigned long npages, int op) { unsigned long vaddr_end, next_vaddr; struct snp_psc_desc *desc; @@ -902,7 +902,7 @@ static void set_pages_state(unsigned long vaddr, unsigned int npages, int op) kfree(desc); } -void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) +void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) { if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return; @@ -912,7 +912,7 @@ void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) set_pages_state(vaddr, npages, SNP_PAGE_STATE_SHARED); } -void snp_set_memory_private(unsigned long vaddr, unsigned int npages) +void snp_set_memory_private(unsigned long vaddr, unsigned long npages) { if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return; From patchwork Tue Apr 4 17:23:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Lendacky X-Patchwork-Id: 79307 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp60298vqo; Tue, 4 Apr 2023 10:54:54 -0700 (PDT) X-Google-Smtp-Source: AKy350Z6tUbkgAIlQ2zXAFGE4qjxyziYAnZOcnQLQeJYoTg8aH2IcxJy0se83Pabm8fHNopmsEEB X-Received: by 2002:a17:90a:195c:b0:240:7f0d:9232 with SMTP id 28-20020a17090a195c00b002407f0d9232mr3847388pjh.3.1680630894606; Tue, 04 Apr 2023 10:54:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1680630894; cv=pass; d=google.com; s=arc-20160816; b=aTB2nnBNvnIrIMKa0Adn4NrrO3N7bq3nH2PDqTSDDhuxoHaxxbo7RFSn6Ctu34Ixd9 y++w9CfD1+hjUIW1+0dZxMB5FmqX63T1vdrSOBKcWtn9SIuwpXja2vrZU1QBylc0a+nQ BOttgWr2IpJrZNjEW73TfIgMJijWKqZnrWgm9AQ+c1Vhwuu/TYMuYt1wI02OgeQ8Iz58 1/k+Fbk6jgtarvB6F8RbpciddRHPkuJCMp2zmekrl6Kh48YKYqBZairPt1S1CR/Ro2Vh 9//q/+xYfVnEDS4cWX3G56e586R8k3sEXpk/Qu0GfjsvqIk9yg/N7dkcFAey+s7X2kNa ZC3Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VOv+ipaumDjrfPyK/wSJc9kHKtQOD+u7/vsqjciMkrM=; b=PKrif1EqjMCrdWhyN2BGyd7S4EEaHGWy5y6egtaNd2XQ1KI7QNncAMcIzuUsOf9R1T UOnLcD93mXF6XgKiEwRF19NR9HGQD0c9TU52ClulwBCrPDv8TqQhQBEaKFfDQGytwEbi ff7tqX/Fye5qxIQ/VMPr4nGpnqiDL59/wcJGPZx/nBmtAPk1ITYe5KoYXkXGOUohkwxQ Q9ZNVP/4s0B1+cT4CYrdwZkD9P8eASfZTB3+XTjJICiF0HfOVu2fFq8bike5w9Bpzkaf GTuPofdYUIoyKU4cIZHzSdbIV3qeY5EAsJaSoeCmtbNmMdVYmU0H6iENfzlFWd/Dm3fo HpUg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=qX2X0eWQ; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id js16-20020a17090b149000b00237155f2303si10380367pjb.136.2023.04.04.10.54.42; Tue, 04 Apr 2023 10:54:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=qX2X0eWQ; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236379AbjDDRbk (ORCPT + 99 others); Tue, 4 Apr 2023 13:31:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236362AbjDDRbX (ORCPT ); Tue, 4 Apr 2023 13:31:23 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2061e.outbound.protection.outlook.com [IPv6:2a01:111:f400:7eab::61e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD5733A92 for ; Tue, 4 Apr 2023 10:30:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nMxGYzzlUyL7llyKmhl0RGb3kM6aV/CHhBICLMj2Fuxo9paE1lAaaxfE5pQ8ujua6zPq/1cMuE2kRmVXA5GEHeCdc9uG4f/0DdgS0rdSWFeIu3+xU8+g9Qh2Cirv0OpOVG2euaK6yiStKomnPY9yT3JtOlrNMg0e5tw0L6xAYpSBi2vN+3klzy9k5qZkyy8pD6S4PPBK+H1ghWPhKL4uzorLyFOnPAyKtkLZisJZGeoSsSEbOBf5eqDjdSaydUJAv75udWJaP1Jr5VHn3S6ECkSaKkMsHO8ESsNKwVbNt/MAe7f9M2vveyqWEqYs7VO84eEz9PhVg9SDfiw1DJKTHg== 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=VOv+ipaumDjrfPyK/wSJc9kHKtQOD+u7/vsqjciMkrM=; b=geJ4LvyuDPJ155DQQt+aakx1LgabUrEfY1IMsjo7v1LBREi/lkiU0tz+0tcr0qFefcFN+NKlIPELeyf9GF/QILCtq+ZcGDA4iK1XJfDUqvY+E6X5Y9wAKDzCKgZo6rCEEFwbri9U2oap1jPUrxeTe0U1yonOBW5TiQ2Zlt/xN/2MQ/cZftPIn9zvTeVwG/GqotToXC0TnY34rVI6TCgEEkwCmGU/cd/x79a8I1bz9I0sbT8LOGO017D530PmWRCYizyQXYYqvYWLqnJHL/ypCBVModMPKKH739G2NbN6g1a1jHCGInA+hR5oPcmaE8jNt7K5xRA7A1jasUaOl+L2DA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VOv+ipaumDjrfPyK/wSJc9kHKtQOD+u7/vsqjciMkrM=; b=qX2X0eWQuLY5UNdSV8f4FNRDDW7fOeOmlDKTX5punyueFzOwHiVRP/sUZjJaC6T36uh0DFO/QW1sw4cAnFlNPlI/hBR5zlly0NqS5Vi6ktFbOjrtkVQJikCM3YS6YE2cwVjrWvbGHwP8sXJgGNmVAHjOFmLdfE9DEVldq4RITd0= Received: from DS7PR03CA0168.namprd03.prod.outlook.com (2603:10b6:5:3b2::23) by DM4PR12MB7693.namprd12.prod.outlook.com (2603:10b6:8:103::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.35; Tue, 4 Apr 2023 17:23:36 +0000 Received: from DS1PEPF0000E63F.namprd02.prod.outlook.com (2603:10b6:5:3b2:cafe::4) by DS7PR03CA0168.outlook.office365.com (2603:10b6:5:3b2::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.35 via Frontend Transport; Tue, 4 Apr 2023 17:23:36 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DS1PEPF0000E63F.mail.protection.outlook.com (10.167.17.199) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Tue, 4 Apr 2023 17:23:36 +0000 Received: from tlendack-t1.amdoffice.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Tue, 4 Apr 2023 12:23:35 -0500 From: Tom Lendacky To: , CC: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "Kirill A. Shutemov" , "H. Peter Anvin" , Michael Roth , Joerg Roedel , Dionna Glaze , Andy Lutomirski , Peter Zijlstra Subject: [PATCH v7 2/6] x86/sev: Put PSC struct on the stack in prep for unaccepted memory support Date: Tue, 4 Apr 2023 12:23:02 -0500 Message-ID: X-Mailer: git-send-email 2.40.0 In-Reply-To: References: <20230330114956.20342-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF0000E63F:EE_|DM4PR12MB7693:EE_ X-MS-Office365-Filtering-Correlation-Id: 4c92790c-9a8e-4e95-4893-08db35315301 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cqPJGGbHB2n7LFHJ7fhkTo8RqZewfINOOOdJgF154T8jBvA8k+07xI60V3iN+zdJHYEh0FL2VUM3XTE5IUNlZMRED7EwEwKMynuMAfxMiU77+TFyQ3tfebdU0JvIUkoIR7i0H0ogGfxKCsUUa0oOunpTjSkh0Gm7NO/9RzGVFA6nIDahFClFDszFnj1d8NC21/qDw8cUfCgskj2tzM7HSQPEVs8+bo+xOGTGwTzWUPUSTpPPp0cKY4FTBDbV342Nzw3YZ3qCwxvWyRm6TDVKNdYUA/ubHRzrpBsfvxLFm3k/pdks8jKrgWNK7KzJFJoyD6pZ1R51x+yDkU5segX1iSoY9DmwYxe5lDOJg7YHMjnl9uS26c7ICDqHbvMp1P4pB3pc720iiAg5V7N0NfvbQo+dEo6k1nqtnFoaqruFTa42nlNLR4H56YnP2tWzwP38Ey5BH8gInGa6Y/nhqIbqxnAdE3cLVEmB+9KDmxJTQhvk27LjOQ5Vq0KnFbqXQbkXlfXGiBSdwjeDfjY9Qz33evg9tU6SmIZLeO/2iv+8unkFnJ06mi47uy2k1gUhvSLq68WgghRjURha5qkhUHGg7uKq01kwCcSOLeCfaYoavyXlGsw6Y7b1L7ojKGLYrTrNoAL2Qr5mvbpx89Xoc3otSZ9M8yMqWKCS/Hw/P4AtL76I8nf1VGxFT4dn0mD2XzVG1n9ssRWE/zCZV/+xaeNEnjPLN1G/3xTStt5PKK5Dhik= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(376002)(346002)(396003)(39860400002)(136003)(451199021)(46966006)(36840700001)(40470700004)(36860700001)(81166007)(966005)(426003)(82740400003)(356005)(336012)(83380400001)(16526019)(2616005)(6666004)(186003)(8936002)(26005)(2906002)(7416002)(47076005)(40480700001)(5660300002)(36756003)(478600001)(4326008)(70206006)(54906003)(110136005)(70586007)(40460700003)(8676002)(41300700001)(86362001)(316002)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2023 17:23:36.4382 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4c92790c-9a8e-4e95-4893-08db35315301 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF0000E63F.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB7693 X-Spam-Status: No, score=0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762269220914347441?= X-GMAIL-MSGID: =?utf-8?q?1762269220914347441?= In advance of providing support for unaccepted memory, switch from using kmalloc() for allocating the Page State Change (PSC) structure to using a local variable that lives on the stack. This is needed to avoid a possible recursive call into set_pages_state() if the kmalloc() call requires (more) memory to be accepted, which would result in a hang. The current size of the PSC struct is 2,032 bytes. To make the struct more stack friendly, reduce the number of PSC entries from 253 down to 64, resulting in a size of 520 bytes. This is a nice compromise on struct size and total PSC requests while still allowing parallel PSC operations across vCPUs. If the reduction in PSC entries results in any kind of performance issue (that is not seen at the moment), use of a larger static PSC struct, with fallback to the smaller stack version, can be investigated. For more background info on this decision, see the subthread in the Link: tag below. Signed-off-by: Tom Lendacky Link: https://lore.kernel.org/lkml/658c455c40e8950cb046dd885dd19dc1c52d060a.1659103274.git.thomas.lendacky@amd.com --- arch/x86/include/asm/sev-common.h | 9 +++++++-- arch/x86/kernel/sev.c | 10 ++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index b8357d6ecd47..8ddfdbe521d4 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -106,8 +106,13 @@ enum psc_op { #define GHCB_HV_FT_SNP BIT_ULL(0) #define GHCB_HV_FT_SNP_AP_CREATION BIT_ULL(1) -/* SNP Page State Change NAE event */ -#define VMGEXIT_PSC_MAX_ENTRY 253 +/* + * SNP Page State Change NAE event + * The VMGEXIT_PSC_MAX_ENTRY determines the size of the PSC structure, which + * is a local stack variable in set_pages_state(). Do not increase this value + * without evaluating the impact to stack usage. + */ +#define VMGEXIT_PSC_MAX_ENTRY 64 struct psc_hdr { u16 cur_entry; diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 72ef1e1fef84..a3d76eb344d6 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -880,11 +880,7 @@ static void __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, static void set_pages_state(unsigned long vaddr, unsigned long npages, int op) { unsigned long vaddr_end, next_vaddr; - struct snp_psc_desc *desc; - - desc = kmalloc(sizeof(*desc), GFP_KERNEL_ACCOUNT); - if (!desc) - panic("SNP: failed to allocate memory for PSC descriptor\n"); + struct snp_psc_desc desc; vaddr = vaddr & PAGE_MASK; vaddr_end = vaddr + (npages << PAGE_SHIFT); @@ -894,12 +890,10 @@ static void set_pages_state(unsigned long vaddr, unsigned long npages, int op) next_vaddr = min_t(unsigned long, vaddr_end, (VMGEXIT_PSC_MAX_ENTRY * PAGE_SIZE) + vaddr); - __set_pages_state(desc, vaddr, next_vaddr, op); + __set_pages_state(&desc, vaddr, next_vaddr, op); vaddr = next_vaddr; } - - kfree(desc); } void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) From patchwork Tue Apr 4 17:23:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Lendacky X-Patchwork-Id: 79315 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp74146vqo; Tue, 4 Apr 2023 11:16:47 -0700 (PDT) X-Google-Smtp-Source: AKy350ZDCFiPlodcnIKI/WTzPYsG6K1FDCy8XB0MOJ9ss2kFbDPPnV17vTCQZgaNNDbXQIey26wV X-Received: by 2002:a17:90b:4b4e:b0:23f:b609:e707 with SMTP id mi14-20020a17090b4b4e00b0023fb609e707mr4005427pjb.2.1680632206840; Tue, 04 Apr 2023 11:16:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1680632206; cv=pass; d=google.com; s=arc-20160816; b=pDnqCHThkdjFKUDFpOTUT5P6DjL05HC5hgzd84EqH9nRSjMhKdqxiMZo1IVdKeYOoa /wLeShVL/xr5GxuHXSYN72Kgw1XIjaiKOj5QBfIkV6GPxjWhS4m5+GRES4zfoPJHYwij H8AEDm07fZPU+Y4K/19tKyNrJV0GXPC0vWgA1cLtftGTql5StfmDPj3PHPNUR7mNuLtv 82/YcDz8d0X64Ojtsixct5d6w8aynhv5MINTHhDa60G/FFs9fDTPCndv5B+1DBI9qPf7 Gw5B60ivAWfslGgpgXPCelzqUh7bbNs/NOc/pzZ4st6/cYgs8XGp5yv1X1Qsi0jUZDPL fJdw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gUt+SP4Rhp34Dmz4p5CmKNOCjUN6npYGftMTE6Gdifg=; b=XqiqG/zqe9eQslYjMmAlBaFD4XZN4TLAq0rTtAbsoRW0I7plMLqwa3aZkerOq5wp3c 2LlgH7f6igtgGiuT0XDmuF5SPH5EsmwV4W1ui34iEWYTUMlRtV0Mf3QEV78hAAlBiaT/ OTPu47JUgjSnVWyiQGCx3Rs3y/lr8ZrxuUHeVM9NIkDnFyKTx6Tp+tLAEr/eOoJmthmT YLtREwb2l8Tqa9ZR9kHcmEf8+JCc10R0Zu+aqzjLTuBByTgH7Hw81++jOAVAXnIqcbV9 rA/BafhF6IuPMTUDpPk6+3opxySo6m4gx6U9v90DD6LifhwsxddRs76XBRxGYS+8GUU0 AIKg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=dT5ZySGh; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kb8-20020a17090ae7c800b0023c1f976f2dsi15992303pjb.85.2023.04.04.11.16.34; Tue, 04 Apr 2023 11:16:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=dT5ZySGh; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236329AbjDDR0k (ORCPT + 99 others); Tue, 4 Apr 2023 13:26:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236078AbjDDR0V (ORCPT ); Tue, 4 Apr 2023 13:26:21 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2042.outbound.protection.outlook.com [40.107.94.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84B9B5BA3 for ; Tue, 4 Apr 2023 10:24:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IlMoAV++XXZTWJpsVn9UqBiXYRquHS5JdJUtY6f2eQktSAlHBBkegJUxpR6CdRbJe3+cc8wATB0qZFn8KEvmv7LzU1lmbCjcGfdv9aWRIOsrnOdPx6kujZDgElJvYiIOkvhTRxXvFuj8fbyfbx30p8jv0u+0UgI/xW/iMwB29bDnvWBjJf5cnKdVm5hcsZTh1bVGylGlS+htZZ/zXD6koVX2L7D0zUQ5oSVERsQSSNs2jVWmHX/724NrbEcCIsI4F/U2co7Ck1eXkk/Jso4FbotrMyg3qFTu9sfqhBMimO/wzuvRq3DBICLQ/YDKi4VDcZ/P+GVhwsGKz0lxFhBDzA== 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=gUt+SP4Rhp34Dmz4p5CmKNOCjUN6npYGftMTE6Gdifg=; b=ljvmzhhoyBxUFFU28R9KnsD9xhnuzKziFEv5/wvgY486X55E9kTjik/LbJnt0/aNTRFCiHj8z5dzKiAmirP8hb8dyz16WWPcxgviiUu8qKTRy6mBuEVkPyLolsz9Yw92z59VLuOEL37Im0JsXxKyC2+RfvUspBlrrsjVW87dP3KETLbYQGhPsziCAs/KD+/cjUUnmqx+4Tf7CNvoe/JDCk01UIOFQarwn8/fpa3BSn+teW1f7II6+Rp+ivH5mecFlOLOEG/Hf9f+M7GjfE0DhkPrM4O5oX9lyQ72HIPTmy/Wi9pWc559DObTPuHdkFsx6Py8HeCLqvj28ZniHhc76w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=gUt+SP4Rhp34Dmz4p5CmKNOCjUN6npYGftMTE6Gdifg=; b=dT5ZySGhhBq/RXfXZcp3zzfY+WS/YqGnSQx01N5qi4oggycSIQV8hgwooCjfYygmn/egARPIzYELVWAXGs36QcGZsm7NJ4A9gtEpcPXBRJZSR83FIbYY8hlfesbz6uiO8NpYok4EZ39ZbTnVZxD8GVK/HgHkPtFSS0SB/dOXgHE= Received: from DS7PR05CA0098.namprd05.prod.outlook.com (2603:10b6:8:56::8) by MW5PR12MB5600.namprd12.prod.outlook.com (2603:10b6:303:195::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.33; Tue, 4 Apr 2023 17:23:44 +0000 Received: from DS1PEPF0000E641.namprd02.prod.outlook.com (2603:10b6:8:56:cafe::1e) by DS7PR05CA0098.outlook.office365.com (2603:10b6:8:56::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.28 via Frontend Transport; Tue, 4 Apr 2023 17:23:44 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DS1PEPF0000E641.mail.protection.outlook.com (10.167.17.201) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Tue, 4 Apr 2023 17:23:44 +0000 Received: from tlendack-t1.amdoffice.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Tue, 4 Apr 2023 12:23:43 -0500 From: Tom Lendacky To: , CC: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "Kirill A. Shutemov" , "H. Peter Anvin" , Michael Roth , Joerg Roedel , Dionna Glaze , Andy Lutomirski , Peter Zijlstra Subject: [PATCH v7 3/6] x86/sev: Allow for use of the early boot GHCB for PSC requests Date: Tue, 4 Apr 2023 12:23:03 -0500 Message-ID: <79ae3cce825217f150560a1ab6fc1fd947b406c4.1680628986.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: References: <20230330114956.20342-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF0000E641:EE_|MW5PR12MB5600:EE_ X-MS-Office365-Filtering-Correlation-Id: 849db2aa-54fa-4b9e-092e-08db353157d9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cNjr5B73+5FysCRj9XB+/Fz8RCNAorIdYh+elRBiZ5C8xyixUCqbntjAMrK2hfbB9/4Gc4VlPpdrqhRnLiLvYW57yDTmUnLiYTHEXhXGAmbKGmvgzDcmsL8tNf3QSwAyaMFIdgS/1Mq7VwGPmbVQKaSqNRpyTwmMAnhvdcwhp4hGx6oWv4K72X8bItD0ooIxfkqsh0DcsNFwfFcTttlB26xJ+vgJu9op3xaVIhEXRsCFba/AfhG51JsfH+maCByiHOAPEG5TzIQqanhuCCOHomNlr8sSL0R8zsKoY8eMT5SALh2AIxfV1PazzDb/ISgAsbSylwqFcCYTqYY4o2xrRqCiSekvzqoT/5Q42bppA0ht58KdbxO99gjrhtIxje2xbXoRLghnA2KkiJDeTPs3WJ8NvYLHkcn3Meb5EQu5LL7CoV7hCz+7shWmWkXg62z/CrfwZMLHkCXCB6oSxUcYwQs+SHJbehc8t8i4seP7Q+spDB/w5GqkhEjAAHUhEh6zLJj42cZ7tCtmVNhl312hPb/N6msdAmre7yY9sYWBjW+dgwSr7akfCR/5+nBQuKpgNrEdAG+8zGaFp5F2jAHwRJLqkRpFWw2XER8gi8rtl6ieePNAtca3BiggNBQ7kHXhjK3kk9xhs0K3g6uG+G5wtjFPPW7enLbn27+Z5OCDfnqrWvOU+KBffBcni6I73BjdhVwbJTGFtpKcDrid3Dc5/6N809rn8NeSp/vZNPLKNqs= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(346002)(396003)(376002)(136003)(451199021)(36840700001)(46966006)(40470700004)(316002)(54906003)(26005)(2616005)(36860700001)(336012)(426003)(110136005)(478600001)(47076005)(16526019)(82740400003)(186003)(6666004)(8676002)(70206006)(83380400001)(70586007)(86362001)(4326008)(2906002)(7416002)(81166007)(41300700001)(5660300002)(8936002)(40460700003)(40480700001)(356005)(82310400005)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2023 17:23:44.5943 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 849db2aa-54fa-4b9e-092e-08db353157d9 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF0000E641.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW5PR12MB5600 X-Spam-Status: No, score=0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762270596765770030?= X-GMAIL-MSGID: =?utf-8?q?1762270596765770030?= Using a GHCB for a page stage change (as opposed to the MSR protocol) allows for multiple pages to be processed in a single request. In prep for early PSC requests in support of unaccepted memory, update the invocation of vmgexit_psc() to be able to use the early boot GHCB and not just the per-CPU GHCB structure. In order to use the proper GHCB (early boot vs per-CPU), set a flag that indicates when the per-CPU GHCBs are available and registered. For APs, the per-CPU GHCBs are created before they are started and registered upon startup, so this flag can be used globally for the BSP and APs instead of creating a per-CPU flag. This will allow for a significant reduction in the number of MSR protocol page state change requests when accepting memory. Signed-off-by: Tom Lendacky --- arch/x86/kernel/sev.c | 61 +++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index a3d76eb344d6..1717bc4558f7 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -117,7 +117,19 @@ static DEFINE_PER_CPU(struct sev_es_save_area *, sev_vmsa); struct sev_config { __u64 debug : 1, - __reserved : 63; + + /* + * A flag used by __set_pages_state() that indicates when the + * per-CPU GHCB has been created and registered and thus can be + * used by the BSP instead of the early boot GHCB. + * + * For APs, the per-CPU GHCB is created before they are started + * and registered upon startup, so this flag can be used globally + * for the BSP and APs. + */ + ghcbs_initialized : 1, + + __reserved : 62; }; static struct sev_config sev_cfg __read_mostly; @@ -660,7 +672,7 @@ static void pvalidate_pages(unsigned long vaddr, unsigned long npages, bool vali } } -static void __init early_set_pages_state(unsigned long paddr, unsigned long npages, enum psc_op op) +static void early_set_pages_state(unsigned long paddr, unsigned long npages, enum psc_op op) { unsigned long paddr_end; u64 val; @@ -754,26 +766,13 @@ void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op WARN(1, "invalid memory op %d\n", op); } -static int vmgexit_psc(struct snp_psc_desc *desc) +static int vmgexit_psc(struct ghcb *ghcb, struct snp_psc_desc *desc) { int cur_entry, end_entry, ret = 0; struct snp_psc_desc *data; - struct ghcb_state state; struct es_em_ctxt ctxt; - unsigned long flags; - struct ghcb *ghcb; - /* - * __sev_get_ghcb() needs to run with IRQs disabled because it is using - * a per-CPU GHCB. - */ - local_irq_save(flags); - - ghcb = __sev_get_ghcb(&state); - if (!ghcb) { - ret = 1; - goto out_unlock; - } + vc_ghcb_invalidate(ghcb); /* Copy the input desc into GHCB shared buffer */ data = (struct snp_psc_desc *)ghcb->shared_buffer; @@ -830,20 +829,18 @@ static int vmgexit_psc(struct snp_psc_desc *desc) } out: - __sev_put_ghcb(&state); - -out_unlock: - local_irq_restore(flags); - return ret; } static void __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, unsigned long vaddr_end, int op) { + struct ghcb_state state; struct psc_hdr *hdr; struct psc_entry *e; + unsigned long flags; unsigned long pfn; + struct ghcb *ghcb; int i; hdr = &data->hdr; @@ -873,8 +870,20 @@ static void __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, i++; } - if (vmgexit_psc(data)) + local_irq_save(flags); + + if (sev_cfg.ghcbs_initialized) + ghcb = __sev_get_ghcb(&state); + else + ghcb = boot_ghcb; + + if (!ghcb || vmgexit_psc(ghcb, data)) sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); + + if (sev_cfg.ghcbs_initialized) + __sev_put_ghcb(&state); + + local_irq_restore(flags); } static void set_pages_state(unsigned long vaddr, unsigned long npages, int op) @@ -882,6 +891,10 @@ static void set_pages_state(unsigned long vaddr, unsigned long npages, int op) unsigned long vaddr_end, next_vaddr; struct snp_psc_desc desc; + /* Use the MSR protocol when a GHCB is not available. */ + if (!boot_ghcb) + return early_set_pages_state(__pa(vaddr), npages, op); + vaddr = vaddr & PAGE_MASK; vaddr_end = vaddr + (npages << PAGE_SHIFT); @@ -1259,6 +1272,8 @@ void setup_ghcb(void) if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) snp_register_per_cpu_ghcb(); + sev_cfg.ghcbs_initialized = true; + return; } From patchwork Tue Apr 4 17:23:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Lendacky X-Patchwork-Id: 79304 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp59832vqo; Tue, 4 Apr 2023 10:53:51 -0700 (PDT) X-Google-Smtp-Source: AKy350ZMFfWmfX0CDZux9gcNENF8UY399x3wcB5x1JiC/FS8pX3EbWdM6mkXphICO1Dg2TX0lXxi X-Received: by 2002:a17:907:3587:b0:92e:3b80:9841 with SMTP id ao7-20020a170907358700b0092e3b809841mr389978ejc.42.1680630830943; Tue, 04 Apr 2023 10:53:50 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1680630830; cv=pass; d=google.com; s=arc-20160816; b=K4JIL53yDSmj5g2MjY3guJRAM8ibt2WZ9EUz3naaPG8UNO3Ni//6XleKPEwg9DCnAQ 0M3yV8az8ouv13a1hIXxLZ6hLoWQmICj/VzwT9rEPCmUUbwPLt+zx1PtzCo5iOUfbWCS /27EYujPVvctgR4ttahRl5OX3+J3LQIAytR73cDUVgIRK11gpivWQTN7nHnU415VI4d/ ZDegXbMNncipRFssT6Jt2+v6raQeJeqvGNR7j8eZcPjwn8wc4zRQ6rLx0qiwXv+XESiF t3qJPTSNboUeVZb3zHlZMEylTlO82QDbmCTWh5GdgAuHvUVrF9mRpsQkPg+NdZ5mItLK j+IQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=laBHCktuaTs49KBTaBTUNDul6UTEAJippVrSxK+ihMA=; b=BKQ2z5WPOjn3DqPrxX/Y0HaTMk5AHm013c6ojPZWA/KsMqZvMRGOAsLjQbA6odAjjm rt+uQ9x+KJSU7MC9w4YVPABV0qL2cm2SEXvUJW5J3okMD8RNRTQAStY70d5vyNz6ugZ8 QlARTQj0+5rtTYd0LXg9YuBboOny3tR3POhbQfodpXVjGbMxEVHCUEG7a3fx+fv696wi d2Q0z1qU6+lH6pW0tQPrqhXOOK449zjh2hCMynRU+Lk2D5ZU7tW/uuA9RHdOEppCVayA bjVwSGUjIwNKUrvr2Z+zL9HB/EiZ8obDPllRGODf08TrUuzOt2bGRCvpsvfqp4akhS2W SilA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=f7htnHSB; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a23-20020a1709063e9700b008d78cb027b7si10995668ejj.667.2023.04.04.10.52.56; Tue, 04 Apr 2023 10:53:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=f7htnHSB; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236260AbjDDR1G (ORCPT + 99 others); Tue, 4 Apr 2023 13:27:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236249AbjDDR0n (ORCPT ); Tue, 4 Apr 2023 13:26:43 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2061e.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8a::61e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 010204C15 for ; Tue, 4 Apr 2023 10:25:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UODu6VZOwpTuozEeKEaqO14q+P1GWQzdISkqPQDXuyMyAAkn/nTpDMqSntxEshrKhOz6bNNO/HfwSoCRuNa85J4MTmr73ziYUQLTr+blRVx87DaWDSoKjMN8CGid9KTlLMCMpV3gD6lVAEDnkP2lS2pgHcZwIgV25SyTSmQxTOfWU/mRyCEtHfIu07DRCGLyh0X5vZnLLqOZYWpmOXRBsnLVPd7fYR5AzFiruRn7aNvhkOYljtVsmp12cqAT8ZJwO5Huyn6u1XkeMi84w11DREsFRz2WFaSIAcZWDs0YFkyiC2DPKucL13CwyOYX1/dsVu8Ya98rNY5uNwueIQqRNQ== 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=laBHCktuaTs49KBTaBTUNDul6UTEAJippVrSxK+ihMA=; b=UPFwXFRYDtcRyiPGS6/j2OW/UeSx8WJbV7CfA7h48LM3Pcap3hxHF3oyAAKsCRmtrVnVh2MpFMHMcKHkP1MjlANaooF6i60agyKPl8b/g/xkDc9ORqSpeFjz/kAuhyEUW4wFROD7b08uD6P6sGnes9ChFcLYBytvyjUYfUt838nYKW/CRN897HDgB//SnLEBLtkqgobP371FbX07Kl+GnyEA+b7iyUmVgiwV65jqPcfkRzD3SQeScC9c1BDeZZcV4bcadl6XZ2/YyJKkeknsuuHyCRDfVHjepO0wdkrkALKENJ5V928xuS+aoF7QQT+2LWnn82j2KAiyMOEnaCoKDw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=laBHCktuaTs49KBTaBTUNDul6UTEAJippVrSxK+ihMA=; b=f7htnHSB+r+7RBkhwpQ8Ur2AinDQekg3qXieaEsV1pjWU0nezNGkqsnvOBOAuaL6bhqSwLQgUT2J/IcdNaYx6amPdvpkVuJYK29aH75UVDyTcG15za9H+wejI1vF7BBcA/rtiIwBJdcz+YBKO3cXNIRpU5Gsxwd6ocRKpsttvik= Received: from DM6PR08CA0062.namprd08.prod.outlook.com (2603:10b6:5:1e0::36) by DS0PR12MB8575.namprd12.prod.outlook.com (2603:10b6:8:164::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.35; Tue, 4 Apr 2023 17:23:55 +0000 Received: from DS1PEPF0000E643.namprd02.prod.outlook.com (2603:10b6:5:1e0:cafe::be) by DM6PR08CA0062.outlook.office365.com (2603:10b6:5:1e0::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.35 via Frontend Transport; Tue, 4 Apr 2023 17:23:55 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DS1PEPF0000E643.mail.protection.outlook.com (10.167.17.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Tue, 4 Apr 2023 17:23:55 +0000 Received: from tlendack-t1.amdoffice.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Tue, 4 Apr 2023 12:23:51 -0500 From: Tom Lendacky To: , CC: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "Kirill A. Shutemov" , "H. Peter Anvin" , Michael Roth , Joerg Roedel , Dionna Glaze , Andy Lutomirski , Peter Zijlstra Subject: [PATCH v7 4/6] x86/sev: Use large PSC requests if applicable Date: Tue, 4 Apr 2023 12:23:04 -0500 Message-ID: X-Mailer: git-send-email 2.40.0 In-Reply-To: References: <20230330114956.20342-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF0000E643:EE_|DS0PR12MB8575:EE_ X-MS-Office365-Filtering-Correlation-Id: 45fada3f-8644-4cd0-fa1e-08db35315e7f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HdhwSgvr5N4jMUayCc+teo/1DYgG1gmrwbpeasxE79/qxO1VGGiEdqA+P8B2YSWmeTjipS+Ytn6+UUlqpO6N9B44vd9dyvcKo0MiM2lhjZUVC/Ls5MBzwoZpgbqk2ATWoLnD8Kc8Wx54vnUFGWuFgl+u4cIRLE62AzwINCzftn5Q+hetrSA+HVg4qBcQUHscbqsokXgc51EUne60aAKC0KJC8b5FJZx5hEeGKhTrijgSgaA7GlEJC1xeWI6vQR9QLGlqBUzHVD1cgDnOxi7V/3wINvjuJOsw/rlbpQjsM2Br9oQICIXKX2UMhgnNgMq58YSHCg+81eJyQjCxPGR3QK3tPIqwFp9n5LuNpz+ycXg4se7q1KGEtlpKjPc2Z+1M4oTDqXsPlGcIpfVwQeyVSg9ZsSgyb201ZghToMIc5sfBsRTfNEw1iFlpLrcGBjxOA0Zr1KERpbjzbBaeQ4NCESNY/UNTOePwxt8jYx3Rif5w7jPJ7vQ1YRzVNRax2PIJDFd3iAhIUE0jy2KL4MYG2qRZU3wQAeD0v2IJDXaoaYZ6hYVH0yStcxXIOQZmcK/jIzeQ9bkbWSpLht8vIx/fVUdn4N6dgFFSex+bL3h1mRgp+OdGervKjbRl6peMja1UUPioaQjTPC2jF+dzdVu5WZQNUigTsbrAUl/e4QU4FBWrvlkKrdGne5Wp2g/lfNE3ZkqdwPbdxQ2mlR9pjJQAj7zfVeFyouGzuevDOGS41q4= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(136003)(376002)(346002)(396003)(39860400002)(451199021)(40470700004)(46966006)(36840700001)(26005)(16526019)(186003)(40460700003)(36860700001)(82310400005)(478600001)(54906003)(316002)(110136005)(36756003)(7416002)(5660300002)(8936002)(2906002)(82740400003)(70206006)(86362001)(356005)(8676002)(4326008)(70586007)(81166007)(41300700001)(40480700001)(6666004)(2616005)(336012)(426003)(83380400001)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2023 17:23:55.7390 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 45fada3f-8644-4cd0-fa1e-08db35315e7f X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF0000E643.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8575 X-Spam-Status: No, score=0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO,SPF_HELO_PASS,SPF_NONE, T_FILL_THIS_FORM_SHORT autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762269154136987455?= X-GMAIL-MSGID: =?utf-8?q?1762269154136987455?= In advance of providing support for unaccepted memory, request 2M Page State Change (PSC) requests when the address range allows for it. By using a 2M page size, more PSC operations can be handled in a single request to the hypervisor. The hypervisor will determine if it can accommodate the larger request by checking the mapping in the nested page table. If mapped as a large page, then the 2M page request can be performed, otherwise the 2M page request will be broken down into 512 4K page requests. This is still more efficient than having the guest perform multiple PSC requests in order to process the 512 4K pages. In conjunction with the 2M PSC requests, attempt to perform the associated PVALIDATE instruction of the page using the 2M page size. If PVALIDATE fails with a size mismatch, then fallback to validating 512 4K pages. To do this, page validation is modified to work with the PSC structure and not just a virtual address range. Signed-off-by: Tom Lendacky --- arch/x86/include/asm/sev.h | 4 ++ arch/x86/kernel/sev.c | 125 ++++++++++++++++++++++++------------- 2 files changed, 84 insertions(+), 45 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index a0a58c4122ec..91b4f712ef18 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -78,11 +78,15 @@ extern void vc_no_ghcb(void); extern void vc_boot_ghcb(void); extern bool handle_vc_boot_ghcb(struct pt_regs *regs); +/* PVALIDATE return codes */ +#define PVALIDATE_FAIL_SIZEMISMATCH 6 + /* Software defined (when rFlags.CF = 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 /* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_PG_SIZE_2M 1 #define RMPADJUST_VMSA_PAGE_BIT BIT(16) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 1717bc4558f7..93de70340427 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -655,32 +655,58 @@ static u64 __init get_jump_table_addr(void) return ret; } -static void pvalidate_pages(unsigned long vaddr, unsigned long npages, bool validate) +static void pvalidate_pages(struct snp_psc_desc *desc) { - unsigned long vaddr_end; + struct psc_entry *e; + unsigned long vaddr; + unsigned int size; + unsigned int i; + bool validate; int rc; - vaddr = vaddr & PAGE_MASK; - vaddr_end = vaddr + (npages << PAGE_SHIFT); + for (i = 0; i <= desc->hdr.end_entry; i++) { + e = &desc->entries[i]; + + vaddr = (unsigned long)pfn_to_kaddr(e->gfn); + size = e->pagesize ? RMP_PG_SIZE_2M : RMP_PG_SIZE_4K; + validate = (e->operation == SNP_PAGE_STATE_PRIVATE) ? true : false; + + rc = pvalidate(vaddr, size, validate); + if (rc == PVALIDATE_FAIL_SIZEMISMATCH && size == RMP_PG_SIZE_2M) { + unsigned long vaddr_end = vaddr + PMD_SIZE; + + for (; vaddr < vaddr_end; vaddr += PAGE_SIZE) { + rc = pvalidate(vaddr, RMP_PG_SIZE_4K, validate); + if (rc) + break; + } + } - while (vaddr < vaddr_end) { - rc = pvalidate(vaddr, RMP_PG_SIZE_4K, validate); if (WARN(rc, "Failed to validate address 0x%lx ret %d", vaddr, rc)) sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE); - - vaddr = vaddr + PAGE_SIZE; } } -static void early_set_pages_state(unsigned long paddr, unsigned long npages, enum psc_op op) +static void early_set_pages_state(unsigned long vaddr, unsigned long paddr, + unsigned long npages, enum psc_op op) { unsigned long paddr_end; u64 val; + int ret; + + vaddr = vaddr & PAGE_MASK; paddr = paddr & PAGE_MASK; paddr_end = paddr + (npages << PAGE_SHIFT); while (paddr < paddr_end) { + if (op == SNP_PAGE_STATE_SHARED) { + /* Page validation must be rescinded before changing to shared */ + ret = pvalidate(vaddr, RMP_PG_SIZE_4K, false); + if (WARN(ret, "Failed to validate address 0x%lx ret %d", paddr, ret)) + goto e_term; + } + /* * Use the MSR protocol because this function can be called before * the GHCB is established. @@ -701,7 +727,15 @@ static void early_set_pages_state(unsigned long paddr, unsigned long npages, enu paddr, GHCB_MSR_PSC_RESP_VAL(val))) goto e_term; - paddr = paddr + PAGE_SIZE; + if (op == SNP_PAGE_STATE_PRIVATE) { + /* Page validation must be performed after changing to private */ + ret = pvalidate(vaddr, RMP_PG_SIZE_4K, true); + if (WARN(ret, "Failed to validate address 0x%lx ret %d", paddr, ret)) + goto e_term; + } + + vaddr += PAGE_SIZE; + paddr += PAGE_SIZE; } return; @@ -726,10 +760,7 @@ void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long padd * Ask the hypervisor to mark the memory pages as private in the RMP * table. */ - early_set_pages_state(paddr, npages, SNP_PAGE_STATE_PRIVATE); - - /* Validate the memory pages after they've been added in the RMP table. */ - pvalidate_pages(vaddr, npages, true); + early_set_pages_state(vaddr, paddr, npages, SNP_PAGE_STATE_PRIVATE); } void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, @@ -744,11 +775,8 @@ void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr if (!(sev_status & MSR_AMD64_SEV_SNP_ENABLED)) return; - /* Invalidate the memory pages before they are marked shared in the RMP table. */ - pvalidate_pages(vaddr, npages, false); - /* Ask hypervisor to mark the memory pages shared in the RMP table. */ - early_set_pages_state(paddr, npages, SNP_PAGE_STATE_SHARED); + early_set_pages_state(vaddr, paddr, npages, SNP_PAGE_STATE_SHARED); } void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op) @@ -832,10 +860,11 @@ static int vmgexit_psc(struct ghcb *ghcb, struct snp_psc_desc *desc) return ret; } -static void __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, - unsigned long vaddr_end, int op) +static unsigned long __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, + unsigned long vaddr_end, int op) { struct ghcb_state state; + bool use_large_entry; struct psc_hdr *hdr; struct psc_entry *e; unsigned long flags; @@ -849,27 +878,37 @@ static void __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, memset(data, 0, sizeof(*data)); i = 0; - while (vaddr < vaddr_end) { - if (is_vmalloc_addr((void *)vaddr)) + while (vaddr < vaddr_end && i < ARRAY_SIZE(data->entries)) { + hdr->end_entry = i; + + if (is_vmalloc_addr((void *)vaddr)) { pfn = vmalloc_to_pfn((void *)vaddr); - else + use_large_entry = false; + } else { pfn = __pa(vaddr) >> PAGE_SHIFT; + use_large_entry = true; + } e->gfn = pfn; e->operation = op; - hdr->end_entry = i; - /* - * Current SNP implementation doesn't keep track of the RMP page - * size so use 4K for simplicity. - */ - e->pagesize = RMP_PG_SIZE_4K; + if (use_large_entry && IS_ALIGNED(vaddr, PMD_SIZE) && + (vaddr_end - vaddr) >= PMD_SIZE) { + e->pagesize = RMP_PG_SIZE_2M; + vaddr += PMD_SIZE; + } else { + e->pagesize = RMP_PG_SIZE_4K; + vaddr += PAGE_SIZE; + } - vaddr = vaddr + PAGE_SIZE; e++; i++; } + /* Page validation must be rescinded before changing to shared */ + if (op == SNP_PAGE_STATE_SHARED) + pvalidate_pages(data); + local_irq_save(flags); if (sev_cfg.ghcbs_initialized) @@ -877,6 +916,7 @@ static void __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, else ghcb = boot_ghcb; + /* Invoke the hypervisor to perform the page state changes */ if (!ghcb || vmgexit_psc(ghcb, data)) sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); @@ -884,29 +924,28 @@ static void __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, __sev_put_ghcb(&state); local_irq_restore(flags); + + /* Page validation must be performed after changing to private */ + if (op == SNP_PAGE_STATE_PRIVATE) + pvalidate_pages(data); + + return vaddr; } static void set_pages_state(unsigned long vaddr, unsigned long npages, int op) { - unsigned long vaddr_end, next_vaddr; struct snp_psc_desc desc; + unsigned long vaddr_end; /* Use the MSR protocol when a GHCB is not available. */ if (!boot_ghcb) - return early_set_pages_state(__pa(vaddr), npages, op); + return early_set_pages_state(vaddr, __pa(vaddr), npages, op); vaddr = vaddr & PAGE_MASK; vaddr_end = vaddr + (npages << PAGE_SHIFT); - while (vaddr < vaddr_end) { - /* Calculate the last vaddr that fits in one struct snp_psc_desc. */ - next_vaddr = min_t(unsigned long, vaddr_end, - (VMGEXIT_PSC_MAX_ENTRY * PAGE_SIZE) + vaddr); - - __set_pages_state(&desc, vaddr, next_vaddr, op); - - vaddr = next_vaddr; - } + while (vaddr < vaddr_end) + vaddr = __set_pages_state(&desc, vaddr, vaddr_end, op); } void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) @@ -914,8 +953,6 @@ void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return; - pvalidate_pages(vaddr, npages, false); - set_pages_state(vaddr, npages, SNP_PAGE_STATE_SHARED); } @@ -925,8 +962,6 @@ void snp_set_memory_private(unsigned long vaddr, unsigned long npages) return; set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE); - - pvalidate_pages(vaddr, npages, true); } static int snp_set_vmsa(void *va, bool vmsa) From patchwork Tue Apr 4 17:23:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Lendacky X-Patchwork-Id: 79306 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp60063vqo; Tue, 4 Apr 2023 10:54:18 -0700 (PDT) X-Google-Smtp-Source: AKy350bZztZgiK6PqSqSn/wmce2ZpEKzm3HjC5HXU1GDQGV2DZCLfAE0U8pUonO5nVeJJqEYk7qo X-Received: by 2002:a17:903:1ca:b0:1a1:cb18:7f99 with SMTP id e10-20020a17090301ca00b001a1cb187f99mr47433plh.30.1680630857869; Tue, 04 Apr 2023 10:54:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1680630857; cv=pass; d=google.com; s=arc-20160816; b=iaI8rh2bsB0YBis6sTIDCuZmLh4OBaEoc5ajNuEi3C4/LaUgTudjJ+gOL9U2EftqoZ 5xFzRAXfLku2bMQeXZwVqFTMEBLJUtMQz5DMv9crNEwnGCyzXvdEZ5tp9bDHUOn8iojr MPs879BkbDaryM5e+5KxN1L6jl9/OKUZO1kEtpnC0QiZNfaBm6LzRgFIIC0AAhv83DF/ 89MdCcFoVsNv8+bKcmMkjIaqNycR32kZc7T+w7ci5huwZqtCTijN8aL5GGemUa4jJEan j3x2O3QmmAtlD2WrHQBcLDOe6EBRSuVEtvfwOwOORUWndRSdumrcO7y+O41mt6AphFoH vq7g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=fywtEUg2/HZm1kyVt0BIb7qHBiBH78lJ5UPD7c11XIM=; b=K/EVou5BHUJBHILEaH1Xa4Bz6oLNbrcv6ZykqQq3uvUF7VIOvAqaWGo3j/yomctNE/ YhEQdYQS5KzXxYmc/itjhgC6Yw+a7vvl5bjBcMSS2H9Gvs67Rj8KteEqM6BrxAJTVYsc no2w9r3NjZLAj24K2uBMUO2pTR+4zAhRlRVS/gBFv78aD+07ksgF2h6ClEIngueerX3H 3YznOODkEFO0PQKJHypqGkP/k0LGGC88PeY0VmbTepZbyHy8P2MoPoUw8wpzdh53u+19 jyuTe+ZDZX4j209GGH9yK96gRF5Dce/rNkhasXFJBnNDcvBNLN7E2LEySQ2hdRb+T9mf ROIw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=xz48Mp7O; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e9-20020a17090301c900b0019abec982c2si10900058plh.77.2023.04.04.10.54.05; Tue, 04 Apr 2023 10:54:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=xz48Mp7O; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236356AbjDDR3a (ORCPT + 99 others); Tue, 4 Apr 2023 13:29:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236318AbjDDR3O (ORCPT ); Tue, 4 Apr 2023 13:29:14 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on20611.outbound.protection.outlook.com [IPv6:2a01:111:f400:7ea9::611]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D15567ABE for ; Tue, 4 Apr 2023 10:27:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ks6lF7XM92fVtbLc1GstlC2PeTTVOLYAwGHWmZXAK29xf0eLXe47MjhT8N6o8CRd5h5gQcDsniEe8NRTQLa3JHOLwzI4mosZYe6Alv2K4HC+NJGh5zb4mnMqVVWdUWXOHfplkE5K5Tj14ZHYlUzuf7/Gwx7dokf5kLxDpG2Oy1ecqENSqIdZ+DlsFGeQ+d0vb8Id1HJAZozFaB0wnXnDv22Wlcl5vfli52epXmAjY68epcspI65FGpPhlovREXAte8lkiCVDvqrenmxNqBd7gAWa3jUgO0X8EoQg/Yt03dUrxYapLTS+6vPemMbxtW/zqSfSi50cuMoUfX5jQmiLHw== 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=fywtEUg2/HZm1kyVt0BIb7qHBiBH78lJ5UPD7c11XIM=; b=EGHzFlzJTGfTaCQ3MB36UldrFR/iXb8o7bMeTpiMLP/kyKT+CymqERwNklzknX6xUrhmN+4cdaMNDH6pAwWGcGKDQDJSOHItSjVuvYs17RD18sw28qCOXS/Y/1IGacQihAEHppb0nkhaJNJH+yhICD6HvUpDJztpcOmeiZ/867nKG1uXYcn5YzE5t34u4Lh0Zy5XkAG8sioKPceTUzeobA/O2eHflyrBw+sHaY8uks1YYooD1XrtqvaK79s0JKP7yEsh9gP5V4afcjC8839VQhpGIm7IpYwy+6EsqF0ey32N9xtIt/A0W0s/PSgP6MymeZ5X8jL/7EYmRNZOC2D1+w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fywtEUg2/HZm1kyVt0BIb7qHBiBH78lJ5UPD7c11XIM=; b=xz48Mp7OY14YHConZ6zf0hg73ttPDR7nFsmSi8bdZdgGyegEwgepdlSxf/dnldQ1c7ccpEnDm5rAPvEuF9rozbDbAsqobLK06VSmX/byjQQOEYHeU9OXL8BfKOAgregbNGfvktfsOKFOwu3KeV3nuSM9WAq4XTn1koyV4x28M0c= Received: from DM6PR06CA0100.namprd06.prod.outlook.com (2603:10b6:5:336::33) by DM6PR12MB4123.namprd12.prod.outlook.com (2603:10b6:5:21f::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.35; Tue, 4 Apr 2023 17:24:01 +0000 Received: from DS1PEPF0000E644.namprd02.prod.outlook.com (2603:10b6:5:336:cafe::e0) by DM6PR06CA0100.outlook.office365.com (2603:10b6:5:336::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.22 via Frontend Transport; Tue, 4 Apr 2023 17:24:01 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DS1PEPF0000E644.mail.protection.outlook.com (10.167.17.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Tue, 4 Apr 2023 17:24:01 +0000 Received: from tlendack-t1.amdoffice.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Tue, 4 Apr 2023 12:24:00 -0500 From: Tom Lendacky To: , CC: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "Kirill A. Shutemov" , "H. Peter Anvin" , Michael Roth , Joerg Roedel , Dionna Glaze , Andy Lutomirski , Peter Zijlstra Subject: [PATCH v7 5/6] x86/sev: Add SNP-specific unaccepted memory support Date: Tue, 4 Apr 2023 12:23:05 -0500 Message-ID: <1dd6d5e89d04fe421e650a1e53d550a264a77ad7.1680628986.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: References: <20230330114956.20342-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF0000E644:EE_|DM6PR12MB4123:EE_ X-MS-Office365-Filtering-Correlation-Id: b0e6ca5c-8d83-47c0-5dbb-08db3531620f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Sj0X4H66Ycr8Sl2SHMbm4xyEgiXwvhGW4rWXgo2CiXWNu4lypTmCD3TjNDDAFbL5wsLql9kRTLaxVjV1y0Ry20YoBe6WLlTrBedcqcLOiHOyQHXQs33mAA11o/V7eEzwCvQxcJ1yxC34Y4kArqx0ecWd6Dtqx3XpVulGbWjeGKIgM99ndRq7tMaBy/ioWp06QGwarJC9fYnCLR/9K7l3CKt31u0edLN2i+9tOsNuD0NEOLcoz/+QlC3aS6Me708eUZwfSOV94MgRRvEqsv4iaPtSvmqTImNNPTFmfjUBtqs0xKB7XvLuTQunO11lFSyFCIfzDOkSXUaIl/HA0/WkErXvmRkCL7a4KXSwDbRlXLNsit7LSkHB4cG9OfhnZ2xoPp8i1iy9mmoA6/hK4CxTrK5nliSBMSfWNTQlpfbk9zO8Ka/aCorGyzXQeMn1lMaJHeAuT1ScIRPXuDott0wDPDvCxDlND04r37d+9V9jGnd+UfImsAm7H0reW5CJ3PgfoWW2Tk/7c1gYy9IjjV3d3XjOE0YT5P4f/me839JQZcfAY7NuFpLF4NxaJwEBx4YW3aeYSPGYFjsDfqFdg80ct06aunIB04aOsXoDu4iDWLeuYx6QXWfkIFvCxXC1ot0KiaiBmWgDtUevxGVTZ7aP2QD+1FBDvUHu+fll//xqQOAILsL3uxHZSvITP1jWaGXCgskSmAfhwen8aEWquMeHtzkwqaw+MNgxuRdte0RnWJw= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(136003)(376002)(396003)(39860400002)(346002)(451199021)(40470700004)(36840700001)(46966006)(36860700001)(478600001)(316002)(110136005)(40460700003)(54906003)(8676002)(5660300002)(8936002)(36756003)(86362001)(30864003)(82310400005)(2906002)(7416002)(4326008)(40480700001)(82740400003)(70206006)(70586007)(356005)(41300700001)(81166007)(186003)(16526019)(47076005)(26005)(426003)(336012)(2616005)(83380400001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2023 17:24:01.7275 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b0e6ca5c-8d83-47c0-5dbb-08db3531620f X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF0000E644.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4123 X-Spam-Status: No, score=0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO,SPF_HELO_PASS,SPF_NONE, T_FILL_THIS_FORM_SHORT autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762269182709832528?= X-GMAIL-MSGID: =?utf-8?q?1762269182709832528?= Add SNP-specific hooks to the unaccepted memory support in the boot path (__accept_memory()) and the core kernel (accept_memory()) in order to support booting SNP guests when unaccepted memory is present. Without this support, SNP guests will fail to boot and/or panic() when unaccepted memory is present in the EFI memory map. The process of accepting memory under SNP involves invoking the hypervisor to perform a page state change for the page to private memory and then issuing a PVALIDATE instruction to accept the page. Since the boot path and the core kernel paths perform similar operations, move the pvalidate_pages() and vmgexit_psc() functions into sev-shared.c to avoid code duplication. Create the new header file arch/x86/boot/compressed/sev.h because adding the function declaration to any of the existing SEV related header files pulls in too many other header files, causing the build to fail. Signed-off-by: Tom Lendacky --- arch/x86/Kconfig | 2 + arch/x86/boot/compressed/mem.c | 3 + arch/x86/boot/compressed/sev.c | 54 ++++++++++++++- arch/x86/boot/compressed/sev.h | 23 +++++++ arch/x86/include/asm/sev.h | 3 + arch/x86/kernel/sev-shared.c | 103 +++++++++++++++++++++++++++++ arch/x86/kernel/sev.c | 112 ++++---------------------------- arch/x86/mm/unaccepted_memory.c | 4 ++ 8 files changed, 205 insertions(+), 99 deletions(-) create mode 100644 arch/x86/boot/compressed/sev.h diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 448cd869f0bd..9fd69128a7d8 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1543,11 +1543,13 @@ config X86_MEM_ENCRYPT config AMD_MEM_ENCRYPT bool "AMD Secure Memory Encryption (SME) support" depends on X86_64 && CPU_SUP_AMD + depends on EFI_STUB select DMA_COHERENT_POOL select ARCH_USE_MEMREMAP_PROT select INSTRUCTION_DECODER select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT + select UNACCEPTED_MEMORY help Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory diff --git a/arch/x86/boot/compressed/mem.c b/arch/x86/boot/compressed/mem.c index e6b92e822ddd..e6ab88dcfd7e 100644 --- a/arch/x86/boot/compressed/mem.c +++ b/arch/x86/boot/compressed/mem.c @@ -6,6 +6,7 @@ #include "find.h" #include "math.h" #include "tdx.h" +#include "sev.h" #include #define PMD_SHIFT 21 @@ -45,6 +46,8 @@ static inline void __accept_memory(phys_addr_t start, phys_addr_t end) /* Platform-specific memory-acceptance call goes here */ if (early_is_tdx_guest()) tdx_accept_memory(start, end); + else if (sev_snp_enabled()) + snp_accept_memory(start, end); else error("Cannot accept memory: unknown platform\n"); } diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 014b89c89088..09dc8c187b3c 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -115,7 +115,7 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, /* Include code for early handlers */ #include "../../kernel/sev-shared.c" -static inline bool sev_snp_enabled(void) +bool sev_snp_enabled(void) { return sev_status & MSR_AMD64_SEV_SNP_ENABLED; } @@ -181,6 +181,58 @@ static bool early_setup_ghcb(void) return true; } +static phys_addr_t __snp_accept_memory(struct snp_psc_desc *desc, + phys_addr_t pa, phys_addr_t pa_end) +{ + struct psc_hdr *hdr; + struct psc_entry *e; + unsigned int i; + + hdr = &desc->hdr; + memset(hdr, 0, sizeof(*hdr)); + + e = desc->entries; + + i = 0; + while (pa < pa_end && i < VMGEXIT_PSC_MAX_ENTRY) { + hdr->end_entry = i; + + e->gfn = pa >> PAGE_SHIFT; + e->operation = SNP_PAGE_STATE_PRIVATE; + if (IS_ALIGNED(pa, PMD_SIZE) && (pa_end - pa) >= PMD_SIZE) { + e->pagesize = RMP_PG_SIZE_2M; + pa += PMD_SIZE; + } else { + e->pagesize = RMP_PG_SIZE_4K; + pa += PAGE_SIZE; + } + + e++; + i++; + } + + if (vmgexit_psc(boot_ghcb, desc)) + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); + + pvalidate_pages(desc); + + return pa; +} + +void snp_accept_memory(phys_addr_t start, phys_addr_t end) +{ + struct snp_psc_desc desc = {}; + unsigned int i; + phys_addr_t pa; + + if (!boot_ghcb && !early_setup_ghcb()) + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); + + pa = start; + while (pa < end) + pa = __snp_accept_memory(&desc, pa, end); +} + void sev_es_shutdown_ghcb(void) { if (!boot_ghcb) diff --git a/arch/x86/boot/compressed/sev.h b/arch/x86/boot/compressed/sev.h new file mode 100644 index 000000000000..fc725a981b09 --- /dev/null +++ b/arch/x86/boot/compressed/sev.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * AMD SEV header for early boot related functions. + * + * Author: Tom Lendacky + */ + +#ifndef BOOT_COMPRESSED_SEV_H +#define BOOT_COMPRESSED_SEV_H + +#ifdef CONFIG_AMD_MEM_ENCRYPT + +bool sev_snp_enabled(void); +void snp_accept_memory(phys_addr_t start, phys_addr_t end); + +#else + +static inline bool sev_snp_enabled(void) { return false; } +static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { } + +#endif + +#endif diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 91b4f712ef18..67e81141a873 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -201,6 +201,7 @@ void snp_set_wakeup_secondary_cpu(void); bool snp_init(struct boot_params *bp); void __init __noreturn snp_abort(void); int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err); +void snp_accept_memory(phys_addr_t start, phys_addr_t end); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -225,6 +226,8 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in { return -ENOTTY; } + +static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { } #endif #endif diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 3a5b0c9c4fcc..be312db48a49 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -12,6 +12,9 @@ #ifndef __BOOT_COMPRESSED #define error(v) pr_err(v) #define has_cpuflag(f) boot_cpu_has(f) +#else +#undef WARN +#define WARN(condition, format...) (!!(condition)) #endif /* I/O parameters for CPUID-related helpers */ @@ -991,3 +994,103 @@ static void __init setup_cpuid_table(const struct cc_blob_sev_info *cc_info) cpuid_ext_range_max = fn->eax; } } + +static void pvalidate_pages(struct snp_psc_desc *desc) +{ + struct psc_entry *e; + unsigned long vaddr; + unsigned int size; + unsigned int i; + bool validate; + int rc; + + for (i = 0; i <= desc->hdr.end_entry; i++) { + e = &desc->entries[i]; + + vaddr = (unsigned long)pfn_to_kaddr(e->gfn); + size = e->pagesize ? RMP_PG_SIZE_2M : RMP_PG_SIZE_4K; + validate = (e->operation == SNP_PAGE_STATE_PRIVATE) ? true : false; + + rc = pvalidate(vaddr, size, validate); + if (rc == PVALIDATE_FAIL_SIZEMISMATCH && size == RMP_PG_SIZE_2M) { + unsigned long vaddr_end = vaddr + PMD_SIZE; + + for (; vaddr < vaddr_end; vaddr += PAGE_SIZE) { + rc = pvalidate(vaddr, RMP_PG_SIZE_4K, validate); + if (rc) + break; + } + } + + if (rc) { + WARN(1, "Failed to validate address 0x%lx ret %d", vaddr, rc); + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE); + } + } +} + +static int vmgexit_psc(struct ghcb *ghcb, struct snp_psc_desc *desc) +{ + int cur_entry, end_entry, ret = 0; + struct snp_psc_desc *data; + struct es_em_ctxt ctxt; + + vc_ghcb_invalidate(ghcb); + + /* Copy the input desc into GHCB shared buffer */ + data = (struct snp_psc_desc *)ghcb->shared_buffer; + memcpy(ghcb->shared_buffer, desc, min_t(int, GHCB_SHARED_BUF_SIZE, sizeof(*desc))); + + /* + * As per the GHCB specification, the hypervisor can resume the guest + * before processing all the entries. Check whether all the entries + * are processed. If not, then keep retrying. Note, the hypervisor + * will update the data memory directly to indicate the status, so + * reference the data->hdr everywhere. + * + * The strategy here is to wait for the hypervisor to change the page + * state in the RMP table before guest accesses the memory pages. If the + * page state change was not successful, then later memory access will + * result in a crash. + */ + cur_entry = data->hdr.cur_entry; + end_entry = data->hdr.end_entry; + + while (data->hdr.cur_entry <= data->hdr.end_entry) { + ghcb_set_sw_scratch(ghcb, (u64)__pa(data)); + + /* This will advance the shared buffer data points to. */ + ret = sev_es_ghcb_hv_call(ghcb, &ctxt, SVM_VMGEXIT_PSC, 0, 0); + + /* + * Page State Change VMGEXIT can pass error code through + * exit_info_2. + */ + if (WARN(ret || ghcb->save.sw_exit_info_2, + "SNP: PSC failed ret=%d exit_info_2=%llx\n", + ret, ghcb->save.sw_exit_info_2)) { + ret = 1; + goto out; + } + + /* Verify that reserved bit is not set */ + if (WARN(data->hdr.reserved, "Reserved bit is set in the PSC header\n")) { + ret = 1; + goto out; + } + + /* + * Sanity check that entry processing is not going backwards. + * This will happen only if hypervisor is tricking us. + */ + if (WARN(data->hdr.end_entry > end_entry || cur_entry > data->hdr.cur_entry, +"SNP: PSC processing going backward, end_entry %d (got %d) cur_entry %d (got %d)\n", + end_entry, data->hdr.end_entry, cur_entry, data->hdr.cur_entry)) { + ret = 1; + goto out; + } + } + +out: + return ret; +} diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 93de70340427..48cb926e28ae 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -655,38 +655,6 @@ static u64 __init get_jump_table_addr(void) return ret; } -static void pvalidate_pages(struct snp_psc_desc *desc) -{ - struct psc_entry *e; - unsigned long vaddr; - unsigned int size; - unsigned int i; - bool validate; - int rc; - - for (i = 0; i <= desc->hdr.end_entry; i++) { - e = &desc->entries[i]; - - vaddr = (unsigned long)pfn_to_kaddr(e->gfn); - size = e->pagesize ? RMP_PG_SIZE_2M : RMP_PG_SIZE_4K; - validate = (e->operation == SNP_PAGE_STATE_PRIVATE) ? true : false; - - rc = pvalidate(vaddr, size, validate); - if (rc == PVALIDATE_FAIL_SIZEMISMATCH && size == RMP_PG_SIZE_2M) { - unsigned long vaddr_end = vaddr + PMD_SIZE; - - for (; vaddr < vaddr_end; vaddr += PAGE_SIZE) { - rc = pvalidate(vaddr, RMP_PG_SIZE_4K, validate); - if (rc) - break; - } - } - - if (WARN(rc, "Failed to validate address 0x%lx ret %d", vaddr, rc)) - sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE); - } -} - static void early_set_pages_state(unsigned long vaddr, unsigned long paddr, unsigned long npages, enum psc_op op) { @@ -794,72 +762,6 @@ void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op WARN(1, "invalid memory op %d\n", op); } -static int vmgexit_psc(struct ghcb *ghcb, struct snp_psc_desc *desc) -{ - int cur_entry, end_entry, ret = 0; - struct snp_psc_desc *data; - struct es_em_ctxt ctxt; - - vc_ghcb_invalidate(ghcb); - - /* Copy the input desc into GHCB shared buffer */ - data = (struct snp_psc_desc *)ghcb->shared_buffer; - memcpy(ghcb->shared_buffer, desc, min_t(int, GHCB_SHARED_BUF_SIZE, sizeof(*desc))); - - /* - * As per the GHCB specification, the hypervisor can resume the guest - * before processing all the entries. Check whether all the entries - * are processed. If not, then keep retrying. Note, the hypervisor - * will update the data memory directly to indicate the status, so - * reference the data->hdr everywhere. - * - * The strategy here is to wait for the hypervisor to change the page - * state in the RMP table before guest accesses the memory pages. If the - * page state change was not successful, then later memory access will - * result in a crash. - */ - cur_entry = data->hdr.cur_entry; - end_entry = data->hdr.end_entry; - - while (data->hdr.cur_entry <= data->hdr.end_entry) { - ghcb_set_sw_scratch(ghcb, (u64)__pa(data)); - - /* This will advance the shared buffer data points to. */ - ret = sev_es_ghcb_hv_call(ghcb, &ctxt, SVM_VMGEXIT_PSC, 0, 0); - - /* - * Page State Change VMGEXIT can pass error code through - * exit_info_2. - */ - if (WARN(ret || ghcb->save.sw_exit_info_2, - "SNP: PSC failed ret=%d exit_info_2=%llx\n", - ret, ghcb->save.sw_exit_info_2)) { - ret = 1; - goto out; - } - - /* Verify that reserved bit is not set */ - if (WARN(data->hdr.reserved, "Reserved bit is set in the PSC header\n")) { - ret = 1; - goto out; - } - - /* - * Sanity check that entry processing is not going backwards. - * This will happen only if hypervisor is tricking us. - */ - if (WARN(data->hdr.end_entry > end_entry || cur_entry > data->hdr.cur_entry, -"SNP: PSC processing going backward, end_entry %d (got %d) cur_entry %d (got %d)\n", - end_entry, data->hdr.end_entry, cur_entry, data->hdr.cur_entry)) { - ret = 1; - goto out; - } - } - -out: - return ret; -} - static unsigned long __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr, unsigned long vaddr_end, int op) { @@ -964,6 +866,20 @@ void snp_set_memory_private(unsigned long vaddr, unsigned long npages) set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE); } +void snp_accept_memory(phys_addr_t start, phys_addr_t end) +{ + unsigned long vaddr; + unsigned int npages; + + if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) + return; + + vaddr = (unsigned long)__va(start); + npages = (end - start) >> PAGE_SHIFT; + + set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE); +} + static int snp_set_vmsa(void *va, bool vmsa) { u64 attrs; diff --git a/arch/x86/mm/unaccepted_memory.c b/arch/x86/mm/unaccepted_memory.c index a521f8c0987d..7e08f7a8bd63 100644 --- a/arch/x86/mm/unaccepted_memory.c +++ b/arch/x86/mm/unaccepted_memory.c @@ -8,6 +8,7 @@ #include #include #include +#include /* Protects unaccepted memory bitmap */ static DEFINE_SPINLOCK(unaccepted_memory_lock); @@ -65,6 +66,9 @@ void accept_memory(phys_addr_t start, phys_addr_t end) if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) { tdx_accept_memory(range_start * PMD_SIZE, range_end * PMD_SIZE); + } else if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) { + snp_accept_memory(range_start * PMD_SIZE, + range_end * PMD_SIZE); } else { panic("Cannot accept memory: unknown platform\n"); } From patchwork Tue Apr 4 17:23:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Lendacky X-Patchwork-Id: 79312 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp72466vqo; Tue, 4 Apr 2023 11:13:54 -0700 (PDT) X-Google-Smtp-Source: AKy350anDwZSXuIKK9MrJFYbNoJ+INDzpVDdU9m2yo34p0J+S87aVJnl1FiUXEEEW7mKHFfEZvcH X-Received: by 2002:a17:902:e542:b0:1a1:ee8c:eef8 with SMTP id n2-20020a170902e54200b001a1ee8ceef8mr4481732plf.2.1680632034641; Tue, 04 Apr 2023 11:13:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1680632034; cv=pass; d=google.com; s=arc-20160816; b=p8L5eJKSRPL4xY+OZPpL5hRIdYfAt9X2qR+4TVA4/QFpt9dW54wWmvRuDTnbtHrDFM MK90AUbXufc902s7WqImLcoEzwpcuiyzch5W1GR6XQqa4nH0Ffwe3vGvM60QFHOPriCt IHSGUBl3xajoz2MIhtiTlezgujpnY0PuMcR6681nntmsSLvJf0FhwR5iZfqtX8StruTh FA5SFwrxWJtwOjUpHvZOKfNfLneAHlnqpNNU/adLah9bCnoWUhXbU//QrpG2O0LV6JIT 7bVL6LEyD8Ydb124yQ3hDYd6oXm8b00ct+hvAfUT13tKhyrfNzenrPiAkd0eC6yHhEQU IQQA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Ua/JAhWpwRQN24wUKwp/qKfiLdX/ukBXUDTLahHI7u8=; b=pXNLFnvUhND0GSp9Rdo9M1gRWN87mhqpQo8JMQHaXSmdOMDVG+w5qnfOBzUjuH7csd lA609NLcmzRZTkZN4C7T8FLKmn3XrZt3ln2A5sSQeuZ3e4NFvUztTpd8rJgrRhtjaT1T mTDZW3GmnIE/u/CurVkW+cSl7ErujhC1inEZqJ33N4b3WSTokTC+3Sj8csuPTtJIzGgL hfxjxuFoHc+l25pG6RhzWL8edUGI85v5yLiUEVlmCI92GE7Xigt8qv1TX2HDtmHxfQu6 xk/RIvMwoKenl2Y1iCwVI4YF/ZaNjgT9jBXRTMvo9e+oMbMoiF8aBKQ6dyWJeZchI7MM OWhg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=0dvKFOey; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t20-20020a170902d21400b001a1d6a9c6c0si1363560ply.127.2023.04.04.11.13.41; Tue, 04 Apr 2023 11:13:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=0dvKFOey; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232851AbjDDR1h (ORCPT + 99 others); Tue, 4 Apr 2023 13:27:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236335AbjDDR1W (ORCPT ); Tue, 4 Apr 2023 13:27:22 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on20609.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe5a::609]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C79AF6E8C for ; Tue, 4 Apr 2023 10:25:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mh7RVM4kiD3uBi/GWrtM/heUFRc7fUTeKPxMGB2OAGs5OQimZS3emRRYC+PDuuUg4An/yCedC3LXd7/5Yh0975IeFzBVGIGQ0ZFwt9cdjpk75Clz3JBmDcupRXzBbYoa5WN1aXAHdOQTkEG9Vy3+dkEZuLSLr3Pwt6bkJQKU9jibL3kC/DDDt/ttVMq+FY38KTXBufGbKkjvC3lmHJNAlzUKHX5RKr6WKSDxp1iVFXNCtiViiAbUY/eBwZngkINhbixLsBkTcsHdgD5m8MKQXYkuwlNOHFk5Re7JQH0+vAP6we/EyADiATXlKyE3fgpD4YDy52zeL0ujul2kja8Pcw== 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=Ua/JAhWpwRQN24wUKwp/qKfiLdX/ukBXUDTLahHI7u8=; b=iT8Uu/CeH/RPu259Gje/agnGo2F5qR3PQ4F3qOhEFeaNR8aVUn4i+4IuezP3GTtI+7zpwn+LpaVA4VpTtu6nwh0vbmTkHYH/GHBkAzdAphPc399ukFtZy2E4OFMG/JnVATdzLv3kKCCVWnrpgZdcCuUfdTh/sW0Qo+gr8nPKhrF6rvUOnoa3MFUtk9bkHr8OTmeyasrbSlMKK91ajVMjA7+sLpwCmYLo3xGU6tJBkRQAt7q4Y0nHciEgcihNHFpxHm30wLWaZ31IeSQI0q3vYan9LEbT2uFG9D1llnMXN/7+ToDdaVVMu8O+7MhHUkedRmex6vRYqt6LzduM9vL4KQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Ua/JAhWpwRQN24wUKwp/qKfiLdX/ukBXUDTLahHI7u8=; b=0dvKFOeyPE1EnzIYZQM+TlYTTydEZwm035sHKM4kPIWQ7DPBsMklGyOcoj4L6/0Z5h3/X9t/i94EblxMysUBIVyctx+tnYMHlscpnEgIvly+6MZrUtItUlNcx8L1Y1W0bmU7XA/KB6OTD1uJAiuAxGgtf4HLVdoEtkZsCuz/DhA= Received: from DS7PR05CA0101.namprd05.prod.outlook.com (2603:10b6:8:56::21) by PH7PR12MB7892.namprd12.prod.outlook.com (2603:10b6:510:27e::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.35; Tue, 4 Apr 2023 17:24:10 +0000 Received: from DS1PEPF0000E641.namprd02.prod.outlook.com (2603:10b6:8:56:cafe::d0) by DS7PR05CA0101.outlook.office365.com (2603:10b6:8:56::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.28 via Frontend Transport; Tue, 4 Apr 2023 17:24:10 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DS1PEPF0000E641.mail.protection.outlook.com (10.167.17.201) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6178.30 via Frontend Transport; Tue, 4 Apr 2023 17:24:10 +0000 Received: from tlendack-t1.amdoffice.net (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Tue, 4 Apr 2023 12:24:08 -0500 From: Tom Lendacky To: , CC: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "Kirill A. Shutemov" , "H. Peter Anvin" , Michael Roth , Joerg Roedel , Dionna Glaze , Andy Lutomirski , Peter Zijlstra , Ard Biescheuvel , "Min M. Xu" , Gerd Hoffmann , James Bottomley , Tom Lendacky , Jiewen Yao , Erdem Aktas , "Kirill A. Shutemov" Subject: [PATCH v7 6/6] x86/efi: Safely enable unaccepted memory in UEFI Date: Tue, 4 Apr 2023 12:23:06 -0500 Message-ID: <1d38d28c2731075d66ac65b56b813a138900f638.1680628986.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: References: <20230330114956.20342-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF0000E641:EE_|PH7PR12MB7892:EE_ X-MS-Office365-Filtering-Correlation-Id: d218344c-abf9-4398-b8b4-08db3531673b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RZPSbBTGGfskKqXaaRVUSrkmszox9WKemfNijB+J6rvHN29l725paMEm55qnFVbjyIutH29jBVG3n7QuPd4Ei/zb2mA7KS3MnVIGwvbAW8/JAheKZw3Fdo9I1IO6Ht6tTVRwuEhkr1UwjLmhybFl9Nok/a+gzoJhT+BlLM3FCM0SGRFY7FeqCjg2V5xKooygI/PvvdE1Nus6ecwEm99SmhGA4Wmf2Z2tbzPC9y2SlnWQ8F/wes0FwubfWQwbcyvPLAm5L/Myh9RnXPNKAaLsU8/c2F6rxwBYAi7nIQHCHr2o35SskfrKr7XNg0YUErRzHZ1oUReVKpJ6/t7jpAlCL71FOtKeH/ceYdg3kxpJdXuPsdu9bkL49FwV9ikenVFqlACYFyGry/6XE0Fo7FOmE3+GaELWfpYP+c9io5fXKPQfuX3Md0QFMO/n73tZ5mEXkShGJ5pk3T/E2baaGRHolA6cK+m2vj2WeoyhsGuLFxj9HKFY5iilWdpKv0MjLwvkfWO4IJ3rTx4Pa6xQcognGSVO9oSB/n/5i77LqJpUIP8IyR0yjNylDJIHhC3izqhtMRnotVS8xDumPBA0J39GkCxNek/xvp2/dCdGVLxBFTDMIsCCpeJ/L2txc8Sk+YBX7jH9Xkm8+VhljhlCW1QUfPDjtiloL/be0eOqIg7DZ8a574HCwykbkOqh6oHfuiphO9uqQXlpUOypWXOIEa5kLxtqWPZxsx+DBrol88oOTps= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(39860400002)(376002)(136003)(396003)(451199021)(36840700001)(46966006)(40470700004)(86362001)(36756003)(82310400005)(2906002)(40480700001)(336012)(2616005)(47076005)(186003)(83380400001)(426003)(16526019)(26005)(8676002)(4326008)(70206006)(41300700001)(70586007)(36860700001)(478600001)(40460700003)(5660300002)(7416002)(82740400003)(110136005)(316002)(81166007)(8936002)(356005)(54906003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2023 17:24:10.3756 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d218344c-abf9-4398-b8b4-08db3531673b X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF0000E641.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7892 X-Spam-Status: No, score=0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762270416480474916?= X-GMAIL-MSGID: =?utf-8?q?1762270416480474916?= From: Dionna Glaze The UEFI v2.9 specification includes a new memory type to be used in environments where the OS must accept memory that is provided from its host. Before the introduction of this memory type, all memory was accepted eagerly in the firmware. In order for the firmware to safely stop accepting memory on the OS's behalf, the OS must affirmatively indicate support to the firmware. This is only a problem for AMD SEV-SNP, since Linux has had support for it since 5.19. The other technology that can make use of unaccepted memory, Intel TDX, does not yet have Linux support, so it can strictly require unaccepted memory support as a dependency of CONFIG_TDX and not require communication with the firmware. Enabling unaccepted memory requires calling a 0-argument enablement protocol before ExitBootServices. This call is only made if the kernel is compiled with UNACCEPTED_MEMORY=y This protocol will be removed after the end of life of the first LTS that includes it, in order to give firmware implementations an expiration date for it. When the protocol is removed, firmware will strictly infer that a SEV-SNP VM is running an OS that supports the unaccepted memory type. At the earliest convenience, when unaccepted memory support is added to Linux, SEV-SNP may take strict dependence in it. After the firmware removes support for the protocol, this patch should be reverted. [tl: address some checkscript warnings] Cc: Ard Biescheuvel Cc: "Min M. Xu" Cc: Gerd Hoffmann Cc: James Bottomley Cc: Tom Lendacky Cc: Jiewen Yao Cc: Erdem Aktas Cc: "Kirill A. Shutemov" Cc: Dave Hansen Cc: Borislav Petkov Signed-off-by: Dionna Glaze Signed-off-by: Tom Lendacky Nacked-by: Kirill A. Shutemov --- drivers/firmware/efi/libstub/x86-stub.c | 36 +++++++++++++++++++++++++ include/linux/efi.h | 3 +++ 2 files changed, 39 insertions(+) diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index 1afe7b5b02e1..119e201cfc68 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -27,6 +27,17 @@ const efi_dxe_services_table_t *efi_dxe_table; u32 image_offset __section(".data"); static efi_loaded_image_t *image = NULL; +typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t; +union sev_memory_acceptance_protocol { + struct { + efi_status_t (__efiapi * allow_unaccepted_memory)( + sev_memory_acceptance_protocol_t *); + }; + struct { + u32 allow_unaccepted_memory; + } mixed_mode; +}; + static efi_status_t preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom) { @@ -311,6 +322,29 @@ setup_memory_protection(unsigned long image_base, unsigned long image_size) #endif } +static void setup_unaccepted_memory(void) +{ + efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID; + sev_memory_acceptance_protocol_t *proto; + efi_status_t status; + + if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY)) + return; + + /* + * Enable unaccepted memory before calling exit boot services in order + * for the UEFI to not accept all memory on EBS. + */ + status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL, + (void **)&proto); + if (status != EFI_SUCCESS) + return; + + status = efi_call_proto(proto, allow_unaccepted_memory); + if (status != EFI_SUCCESS) + efi_err("Memory acceptance protocol failed\n"); +} + static const efi_char16_t apple[] = L"Apple"; static void setup_quirks(struct boot_params *boot_params, @@ -967,6 +1001,8 @@ asmlinkage unsigned long efi_main(efi_handle_t handle, setup_quirks(boot_params, bzimage_addr, buffer_end - buffer_start); + setup_unaccepted_memory(); + status = exit_boot(boot_params, handle); if (status != EFI_SUCCESS) { efi_err("exit_boot() failed!\n"); diff --git a/include/linux/efi.h b/include/linux/efi.h index 1d4f0343c710..e728b8cf6b73 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -436,6 +436,9 @@ void efi_native_runtime_setup(void); #define DELLEMC_EFI_RCI2_TABLE_GUID EFI_GUID(0x2d9f28a2, 0xa886, 0x456a, 0x97, 0xa8, 0xf1, 0x1e, 0xf2, 0x4f, 0xf4, 0x55) #define AMD_SEV_MEM_ENCRYPT_GUID EFI_GUID(0x0cf29b71, 0x9e51, 0x433a, 0xa3, 0xb7, 0x81, 0xf3, 0xab, 0x16, 0xb8, 0x75) +/* OVMF protocol GUIDs */ +#define OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID EFI_GUID(0xc5a010fe, 0x38a7, 0x4531, 0x8a, 0x4a, 0x05, 0x00, 0xd2, 0xfd, 0x16, 0x49) + typedef struct { efi_guid_t guid; u64 table;