From nobody Thu Nov 14 22:06:22 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 298FEC7EE24 for ; Tue, 6 Jun 2023 14:52:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238274AbjFFOw4 (ORCPT ); Tue, 6 Jun 2023 10:52:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238492AbjFFOwe (ORCPT ); Tue, 6 Jun 2023 10:52:34 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2046.outbound.protection.outlook.com [40.107.243.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CB2A172C for ; Tue, 6 Jun 2023 07:52:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=J3qzCKNhIyu8FLgDX3jV5+0ctVSP6Lj8F9ziHC6UXA2/jDDHQud5H8fayXBaqnoNbNYu782l0scS3WvCSsXAcCiSpHtJIUJcQzHIhEwJKhSa6Jd7O/GWwf0Kx6rProxNmpoKS0dbu8hrhmiqJ5BLABhpmVz0v8bHf9cTwzrwdUKNOvH4zh7GuuoM49tQiRTILdkXVuDKiX5h/i9ONdDFHS0ZGdjy3BYaNcWqe+xIwhvna5k7beZwXFPgefpJJcVPBeUtg3HvFjCP2fKy/S4oryP2NUvJ58be7m7PSTEmOeX0v+LxlYDyk92W+xc2LWGGIzj4eQ018sv2EYakpTEnfA== 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=MWgBmrc/3MsRAqnwKmUfMfGVs279AeRT62omDfjvr4o=; b=WqwyGwuK/K6hQ/sgPngR8x5dmmKxywEidQWp+vSVQ10piqwKr1O4Xl4TJdnrpoX5cLLf2yDKTTKJqQ3bOiXV/kLGRSPz4xyR+z13KaOjdSyJzsOsKo91ckJyzlKOzckE3RXTGF3sx0W72fzjFWuyhDgzUo4UcBKm4erE6KdSvyIGThI+ZHTU2cTExoXNYtxDORHIfYvE9BAXmlWLlPesnOGf7B05OcGy1qbIdDRu46EwGHwUnW92g22076L0IubA4a17c5knsEs5b9Nm+gkXhG3/ynUE0xs4ktVXH2OoAD51msNo/GRx9Fo6No4/IhIQy60U1OiixSINbZIIjCQFBg== 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=MWgBmrc/3MsRAqnwKmUfMfGVs279AeRT62omDfjvr4o=; b=yw8LrMOmPapvk77IUOiTuEKqx3RD1yD85pzrHP4SOSpMbYoKY2J4QDX97FDF6iSGiJOmGxuZYz61epiSDygEjXwv3N3uYt2qrm64wwhLGBdCTy3SypVtu1/ZP/O4eMFghYzvi9w3+rUI7fVc7p0TF6iikbNMGwfWkd+d5mBQDFE= Received: from MW4PR03CA0079.namprd03.prod.outlook.com (2603:10b6:303:b6::24) by SN7PR12MB8772.namprd12.prod.outlook.com (2603:10b6:806:341::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.27; Tue, 6 Jun 2023 14:52:19 +0000 Received: from CO1NAM11FT009.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b6:cafe::c7) by MW4PR03CA0079.outlook.office365.com (2603:10b6:303:b6::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33 via Frontend Transport; Tue, 6 Jun 2023 14:52:19 +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 CO1NAM11FT009.mail.protection.outlook.com (10.13.175.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6455.33 via Frontend Transport; Tue, 6 Jun 2023 14:52:19 +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, 6 Jun 2023 09:52:15 -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 v9 4/6] x86/sev: Use large PSC requests if applicable Date: Tue, 6 Jun 2023 09:51:25 -0500 Message-ID: <050d17b460dfc237b51d72082e5df4498d3513cb.1686063086.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: <20230606142637.5171-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: CO1NAM11FT009:EE_|SN7PR12MB8772:EE_ X-MS-Office365-Filtering-Correlation-Id: b796b3d9-907a-4afd-f148-08db669da0cb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iJ2YAJNZ4gE+PwNLKI6HDQGtkJdN9uEZN7HvzNgBAwltvaqMB90EYliASTzEBEZve7921r5S6Tmupm8XUM8Wre9nvIvo5V1FvX6pRpUyV50qiOtmLbNcC+/uOEYHFrr5TK90RCqgR9wwNuRcUrkCN+dAZkCiIoZj90yFCdf89jy1Gi42KcBm8OpfK5wO9Mzy0yGgDUc7ll9wDgZLGBsBTwrPfV63lPEczjP8np+NZUxRvdfB/+Z9VFVqmOoO1khAyN9T/UYL2oFFBKZep/JqYhSeHkEWWpqWyzzJqG6qnlJuYnv8Xr7UdBOTS4xlII1aJfxSk83n+1j4fv8nhHo9aQO1t0l79vyBPIAHvXh8XNnATEUsS8a5rlViecmrhSv0cPikOB9rqwoqstqtzFoTgZk3GamPdhKtg/K7HRv3rOMzq0Om2Di0IrpENkeRSy9aoxish75Xt10HogA5lw7faZBP/mmdpwDyST5jRUYhynC3meWZk7qY4ustiyl84RJT3/qWmBcsN2pLIKy4f9QG+//4uoYP8tx3HgQLEN3LCIaCcQziFzZT0Q9dHpKfoYw1uo+rfzIEhpLV3DCVqJcbLzYa13B7XTvzyV1yKyfXDsbefBZwRoHZoQ+LmiGQvBVIxc0nzajyw4Q0PTxDUagiBor2b35KB9vRFQ26yxPg/E0UZmCcPyYJdMR41vRRdv9s7LQcInj1ZG25NPNw22zgWoLB7dOsDIsvbeTfyY336vmqOwWY09MR7tRIxlvZbzrb7RxNwcR4dsMQ6NBWuk7ESw== 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)(136003)(39860400002)(346002)(451199021)(40470700004)(36840700001)(46966006)(40460700003)(6666004)(478600001)(36860700001)(47076005)(426003)(36756003)(336012)(83380400001)(16526019)(82310400005)(186003)(81166007)(26005)(86362001)(82740400003)(2616005)(356005)(40480700001)(4326008)(41300700001)(316002)(70206006)(70586007)(7416002)(2906002)(8676002)(8936002)(5660300002)(54906003)(110136005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jun 2023 14:52:19.5269 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b796b3d9-907a-4afd-f148-08db669da0cb 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: CO1NAM11FT009.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB8772 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 7ca5c9ec8b52..e21e1c5397c1 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -80,11 +80,15 @@ extern void vc_no_ghcb(void); extern void vc_boot_ghcb(void); extern bool handle_vc_boot_ghcb(struct pt_regs *regs); =20 +/* PVALIDATE return codes */ +#define PVALIDATE_FAIL_SIZEMISMATCH 6 + /* Software defined (when rFlags.CF =3D 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 =20 /* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_PG_SIZE_2M 1 =20 #define RMPADJUST_VMSA_PAGE_BIT BIT(16) =20 diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 973756c89dac..17b3d003b2ea 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -657,32 +657,58 @@ static u64 __init get_jump_table_addr(void) return ret; } =20 -static void pvalidate_pages(unsigned long vaddr, unsigned long npages, boo= l 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; =20 - vaddr =3D vaddr & PAGE_MASK; - vaddr_end =3D vaddr + (npages << PAGE_SHIFT); + for (i =3D 0; i <=3D desc->hdr.end_entry; i++) { + e =3D &desc->entries[i]; + + vaddr =3D (unsigned long)pfn_to_kaddr(e->gfn); + size =3D e->pagesize ? RMP_PG_SIZE_2M : RMP_PG_SIZE_4K; + validate =3D e->operation =3D=3D SNP_PAGE_STATE_PRIVATE; + + rc =3D pvalidate(vaddr, size, validate); + if (rc =3D=3D PVALIDATE_FAIL_SIZEMISMATCH && size =3D=3D RMP_PG_SIZE_2M)= { + unsigned long vaddr_end =3D vaddr + PMD_SIZE; + + for (; vaddr < vaddr_end; vaddr +=3D PAGE_SIZE) { + rc =3D pvalidate(vaddr, RMP_PG_SIZE_4K, validate); + if (rc) + break; + } + } =20 - while (vaddr < vaddr_end) { - rc =3D 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 =3D vaddr + PAGE_SIZE; } } =20 -static void early_set_pages_state(unsigned long paddr, unsigned long npage= s, 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 =3D vaddr & PAGE_MASK; =20 paddr =3D paddr & PAGE_MASK; paddr_end =3D paddr + (npages << PAGE_SHIFT); =20 while (paddr < paddr_end) { + if (op =3D=3D SNP_PAGE_STATE_SHARED) { + /* Page validation must be rescinded before changing to shared */ + ret =3D 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. @@ -703,7 +729,15 @@ static void early_set_pages_state(unsigned long paddr,= unsigned long npages, enu paddr, GHCB_MSR_PSC_RESP_VAL(val))) goto e_term; =20 - paddr =3D paddr + PAGE_SIZE; + if (op =3D=3D SNP_PAGE_STATE_PRIVATE) { + /* Page validation must be performed after changing to private */ + ret =3D pvalidate(vaddr, RMP_PG_SIZE_4K, true); + if (WARN(ret, "Failed to validate address 0x%lx ret %d", paddr, ret)) + goto e_term; + } + + vaddr +=3D PAGE_SIZE; + paddr +=3D PAGE_SIZE; } =20 return; @@ -728,10 +762,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); } =20 void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long= paddr, @@ -746,11 +777,8 @@ void __init early_snp_set_memory_shared(unsigned long = vaddr, unsigned long paddr if (!(sev_status & MSR_AMD64_SEV_SNP_ENABLED)) return; =20 - /* Invalidate the memory pages before they are marked shared in the RMP t= able. */ - 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); } =20 void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc= _op op) @@ -834,10 +862,11 @@ static int vmgexit_psc(struct ghcb *ghcb, struct snp_= psc_desc *desc) return ret; } =20 -static void __set_pages_state(struct snp_psc_desc *data, unsigned long vad= dr, - 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; @@ -851,27 +880,37 @@ static void __set_pages_state(struct snp_psc_desc *da= ta, unsigned long vaddr, memset(data, 0, sizeof(*data)); i =3D 0; =20 - while (vaddr < vaddr_end) { - if (is_vmalloc_addr((void *)vaddr)) + while (vaddr < vaddr_end && i < ARRAY_SIZE(data->entries)) { + hdr->end_entry =3D i; + + if (is_vmalloc_addr((void *)vaddr)) { pfn =3D vmalloc_to_pfn((void *)vaddr); - else + use_large_entry =3D false; + } else { pfn =3D __pa(vaddr) >> PAGE_SHIFT; + use_large_entry =3D true; + } =20 e->gfn =3D pfn; e->operation =3D op; - hdr->end_entry =3D i; =20 - /* - * Current SNP implementation doesn't keep track of the RMP page - * size so use 4K for simplicity. - */ - e->pagesize =3D RMP_PG_SIZE_4K; + if (use_large_entry && IS_ALIGNED(vaddr, PMD_SIZE) && + (vaddr_end - vaddr) >=3D PMD_SIZE) { + e->pagesize =3D RMP_PG_SIZE_2M; + vaddr +=3D PMD_SIZE; + } else { + e->pagesize =3D RMP_PG_SIZE_4K; + vaddr +=3D PAGE_SIZE; + } =20 - vaddr =3D vaddr + PAGE_SIZE; e++; i++; } =20 + /* Page validation must be rescinded before changing to shared */ + if (op =3D=3D SNP_PAGE_STATE_SHARED) + pvalidate_pages(data); + local_irq_save(flags); =20 if (sev_cfg.ghcbs_initialized) @@ -879,6 +918,7 @@ static void __set_pages_state(struct snp_psc_desc *data= , unsigned long vaddr, else ghcb =3D boot_ghcb; =20 + /* 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); =20 @@ -886,29 +926,28 @@ static void __set_pages_state(struct snp_psc_desc *da= ta, unsigned long vaddr, __sev_put_ghcb(&state); =20 local_irq_restore(flags); + + /* Page validation must be performed after changing to private */ + if (op =3D=3D SNP_PAGE_STATE_PRIVATE) + pvalidate_pages(data); + + return vaddr; } =20 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; =20 /* 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); =20 vaddr =3D vaddr & PAGE_MASK; vaddr_end =3D vaddr + (npages << PAGE_SHIFT); =20 - while (vaddr < vaddr_end) { - /* Calculate the last vaddr that fits in one struct snp_psc_desc. */ - next_vaddr =3D min_t(unsigned long, vaddr_end, - (VMGEXIT_PSC_MAX_ENTRY * PAGE_SIZE) + vaddr); - - __set_pages_state(&desc, vaddr, next_vaddr, op); - - vaddr =3D next_vaddr; - } + while (vaddr < vaddr_end) + vaddr =3D __set_pages_state(&desc, vaddr, vaddr_end, op); } =20 void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) @@ -916,8 +955,6 @@ void snp_set_memory_shared(unsigned long vaddr, unsigne= d long npages) if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) return; =20 - pvalidate_pages(vaddr, npages, false); - set_pages_state(vaddr, npages, SNP_PAGE_STATE_SHARED); } =20 @@ -927,8 +964,6 @@ void snp_set_memory_private(unsigned long vaddr, unsign= ed long npages) return; =20 set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE); - - pvalidate_pages(vaddr, npages, true); } =20 static int snp_set_vmsa(void *va, bool vmsa) --=20 2.40.1