Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp6324227rwl; Tue, 4 Apr 2023 10:48:58 -0700 (PDT) X-Google-Smtp-Source: AKy350ZXGka9QCBx/UwMyKuTs6otQmWLzWjAVvcZuPn4nWlegtlVbbkj3eXjypmzL1ijFicEijDo X-Received: by 2002:a05:6402:1490:b0:4fa:4b1c:5ea3 with SMTP id e16-20020a056402149000b004fa4b1c5ea3mr320163edv.23.1680630538082; Tue, 04 Apr 2023 10:48:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1680630538; cv=pass; d=google.com; s=arc-20160816; b=IVVmaJ5VskB9u4K6iPsV8YGHD220tThrYh+/lQUc/FXupThtMsWDeKjMAypeRYKIvR Giu9GqHi++THHjFfLhlGV9BcOQan8GRpnv2Om2xSEqChaCITHf6+/0vvVA03RW5MNP3j 0DCAB/6jsq9UitGk+h0A/48m9ZU5GrXyAS59d3h2CIXZRfPjdzSJvcMnU6tPGxfg/IrS 17eVpiQ2lffvemMBr2y3JCbkA/K0q78ZRRR5z8a8XPUh9hcZvkT7MZ6/39uNl8EZUA67 CRGPHi6YBPqF0yBoJjG3FHA9nZ9nf0V5MXIjec5RXBLyxUGizE9mRJ7rwfQT7sXtpHSI MKPw== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s19-20020aa7cb13000000b005021d2480f8si9427440edt.282.2023.04.04.10.48.32; Tue, 04 Apr 2023 10:48:58 -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 S236181AbjDDR0k (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 Content-Transfer-Encoding: 8bit Content-Type: text/plain 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 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; } -- 2.40.0