Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3114772rwb; Mon, 19 Sep 2022 15:25:22 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6Q6Y5jqemeKEfa/0JWeabMgTcSDpzhJNJCsrazQ2pVETKmOalwPnho2a+mXclUR2GDMXvk X-Received: by 2002:a17:907:7625:b0:779:e6f7:a669 with SMTP id jy5-20020a170907762500b00779e6f7a669mr14448573ejc.472.1663626322398; Mon, 19 Sep 2022 15:25:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1663626322; cv=pass; d=google.com; s=arc-20160816; b=soO/ZCdIsL6GYP93xckOUQ5cfZBW94hiMvLX94H9LslKKZXpbPvIexZIdMioNqV6d2 Ph6XLvhtGSfkEVQOCCwIb+wJMY1uxgRIGxrBtkWzICRyM+UB1vQlcbnzxtqCqthoVciL mzu4yP3sjkVik2eqfev4ZgGcefT8llK5ynHH+JJBgoSk6NDFUhBemaoonz4/a+sG8eEO 5fK+v2jxpezxEHDbMTGXrNrZGXzZCA/mZmd6mV0+qPQAKF/LzUcX5aqXEqMmAwH2OFmU UYt0JUsXnD/GAI7FAQqdK2A+V7o3W51/xzf8dWvqK002XGkMsK/+cJNX/cGxynaXk0D9 q8Wg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :in-reply-to:from:references:cc:to:content-language:subject :user-agent:date:message-id:dkim-signature; bh=Ztc1Ph71OP89no6zx8qe1bQXZ/oEWV/B1svj5WgKoF8=; b=YdwBiXU/fDXaddB5i7mLkXb69oVZ8LzuJq2Zzv+9sebHSVVDcGs3ukseb+ARt0pyHB U/d0kDH3uQcqzz533tE5gRdAhkygm19C1ujfdHCNpB89caQxTDKTrclYqHjy64cvT5G9 pntPkQE7TCYuGqVzR9BfCIY9DvfV0ReAaZFo+3bvWtey2VrgHgHnc3cGbL8pM1TMHK+s 3z94jXqUGK2awjQFXizNY2B5mD2IEAwvfxzBwYdcGzd5L64s9xqvQsYBxnovlbunpLcW xsHejMCcWJXdeEyqEgSSQczpSyRP2soi4hNLsI1JPBk8t3tw0xIZPJVa4N7d9707aNhf 53qA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=mQ9Wt3Tt; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-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 s4-20020a17090699c400b00779a69067afsi4934446ejn.830.2022.09.19.15.24.49; Mon, 19 Sep 2022 15:25:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-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=mQ9Wt3Tt; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-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 S229572AbiISWSN (ORCPT + 99 others); Mon, 19 Sep 2022 18:18:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229473AbiISWSM (ORCPT ); Mon, 19 Sep 2022 18:18:12 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2062.outbound.protection.outlook.com [40.107.244.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11F5549B7B; Mon, 19 Sep 2022 15:18:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jj4qJr9mgB4a0EaS+sltQF+jrW5inP+UWuGnbZEgieRsqvVbEzsbQY+AfnwAg1kUJCdj2pya1dguy5+9rQv5OVzzit24D+ds5HLelsPGoApHs2VM74fXpPZAxx41EDik3M2pSHOJbhWx+puaBQV0o/1jreYrWyw383mpQ0eGndXGpJS3KA0s7qyIN/NyJKpuQd7vom0MfaeRb31UY2lRYB9X7fV80oDxSStCXOw/l0V6iiMl7O1pK0StpZ1QYfrCKsmKJpK0hIIZopWr7RH1PP+BDyfCLYO7TW+BX5oH0r5/yx5g254/em7wLXeA29t26M8qUiZ74ur7EZPZdMEyqw== 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=Ztc1Ph71OP89no6zx8qe1bQXZ/oEWV/B1svj5WgKoF8=; b=McH7XnZZX79MaP91hGmOIzbeWpHnYyUTJa/xMx+YgBPJV2K5Sj0H3f+mOMZj3plUH8DXnHb6fqGfh1FFbBs7tcWFctVw0H7Rm8E7jdS7rEWGuS3+b5UgpQ5v35OUKqWCsgnqgthu44zUBRrjPHAz9cbag/C4rw8XuQECNcZHir8uzzZUhQbY1fGchuL+DHyLpNWzqJQVG5ndlYlEho2kroRTWGqfuBjYDtGR+qRXdTEa+M+205+TUgSZ1RzM7/bbkudpkc89t2V6EyRONlf06xyv/EyiFMTOExAb6lxHS7s/oyvuk+hxxqHsuJVA2C5CntJ8+zH0wH5eNcGkNmpgMA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=Ztc1Ph71OP89no6zx8qe1bQXZ/oEWV/B1svj5WgKoF8=; b=mQ9Wt3TtSlzjFmd9m1pc2YgCRlNwmMovB8zco9Pp0rZ+rR4TxI1AVfVB/MapMqbjpYVqDKOvXCbFd/3ZuiPOu1wZJFe/I6Ho894PWgp2yMzUAm4y4frJz1q5mG/LGhhmn8kI19RVU7tlrYgx+U7/TrnKm4rdFdU1efbg7hbdmKI= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from DM4PR12MB5229.namprd12.prod.outlook.com (2603:10b6:5:398::12) by SA1PR12MB7342.namprd12.prod.outlook.com (2603:10b6:806:2b3::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5632.19; Mon, 19 Sep 2022 22:18:08 +0000 Received: from DM4PR12MB5229.namprd12.prod.outlook.com ([fe80::c175:4c:c0d:1396]) by DM4PR12MB5229.namprd12.prod.outlook.com ([fe80::c175:4c:c0d:1396%4]) with mapi id 15.20.5632.021; Mon, 19 Sep 2022 22:18:08 +0000 Message-ID: <8113b5d4-31c6-012c-fc0c-78a9bdbb1e69@amd.com> Date: Mon, 19 Sep 2022 17:18:03 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH Part2 v6 37/49] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT Content-Language: en-US To: Alper Gun Cc: Peter Gonda , Ashish Kalra , the arch/x86 maintainers , LKML , kvm list , linux-coco@lists.linux.dev, Linux Memory Management List , Linux Crypto Mailing List , Thomas Gleixner , Ingo Molnar , Joerg Roedel , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , "Kirill A . Shutemov" , Andi Kleen , Tony Luck , Marc Orr , Sathyanarayanan Kuppuswamy , "Dr. David Alan Gilbert" , jarkko@kernel.org References: <78e30b5a25c926fcfdcaafea3d484f1bb25f20b9.1655761627.git.ashish.kalra@amd.com> <0716365f-3572-638b-e841-fcce7d30571a@amd.com> From: Tom Lendacky In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: BL0PR02CA0009.namprd02.prod.outlook.com (2603:10b6:207:3c::22) To DM4PR12MB5229.namprd12.prod.outlook.com (2603:10b6:5:398::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM4PR12MB5229:EE_|SA1PR12MB7342:EE_ X-MS-Office365-Filtering-Correlation-Id: e697dbea-de69-4c7c-3c76-08da9a8cd4e0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CH6jUlvmQcFCq9btxQCSwGuX43CbqseqWzW8buKnbW33GBCGudA8GjPMjiAT4lhfFvt704vCW3mogcOS/PJFhO+MuoxogcBerb93oInR+Y5vsphEHykYr36GyxvRO9/hKXfjtFTAQKOYrNwnWs3Mr7YcDsIFg0AaSrAnqbQQBaRHwGXMRwNm8nJbPLNJonaPz5JfCKtug7nLwbXa05OJWbULxyWBXMN4c/zOaCs7IjzstyTaM6ubsYCa6eDFCRTZlN8B4q2sA5gJ9iGI2w6fAMUJtJDWlCoMJ6SFg4/Ry3BlHeqqY6SXTRNOnJzFkr16jjm/Rlx88roPVdejoSI8z+w+VuFLBTO/Ya0jjHzmXtsNWyDqoZvizMVGPop3WC/kBGBYhM2b2EAd2yKMnN4QjzZdClxN6JzOPIncxmbGXzyvcQtR6b5HOZNEhcm7rh9AW09Sf3/dv23fPJmgC1rRYHlSCXonjTMiYEUJz/SRHRbWQxneayYrZ8ZTsXtGfuqiT9SaqvTblEHQFhBEgfh0WESJyYdhjQSDcwxgQArh4lzXz552DWeJRPmdXXaq33PiOAojdLSIspshI9YOFpr0xcdWTMTSvbHmtPTkpM7yGrnFvnWEQy1C6egO2lAu6LRxFoVU6RZoJQ31oQBkvUmuPjx1RGgBwAhaDt3SUJvox4VK1LKCtPXVxU4n43iSSQXOPKBKMKVCey0YSnuO8JqpnbcOR1OsvWwZgDYIS/dwQBBv0xuloOFZSTgeV0twKZAVWxGl22c40Bi2/2WXSdI4chmuThYTFvdX1wReu8PPKCQ= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM4PR12MB5229.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(376002)(396003)(346002)(39860400002)(366004)(136003)(451199015)(2906002)(36756003)(31686004)(316002)(83380400001)(38100700002)(4326008)(8676002)(7406005)(7416002)(66556008)(5660300002)(66476007)(66946007)(54906003)(8936002)(31696002)(86362001)(186003)(478600001)(2616005)(26005)(6512007)(6916009)(6666004)(53546011)(6506007)(6486002)(41300700001)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?cnVvdkRSVWxjSXkrU1VwRTBGQkwwdk5vZlJQdnppK1NlT2IybkV5bHBtaVNC?= =?utf-8?B?RWdSTEdvMTZEN0FpMkhSaVVxSFlSSGl3M3BzL0VLL1crdE1FVXFldk1MR1JN?= =?utf-8?B?OGRVaXFmRjRYZHJ6QjZWcHpiTUd4Q0NOcmROTlBY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BLy9MbVlrTVBDNDZtYXkrTm0wM2sySHpXL0ZiWFdvRnhDVDVTajhX?= =?utf-8?B?QklyQ1VBRDJJbldWb3RYdzlMR3FxVjdqRFZoRXowTmptdDUvaTVIdCtZQzRx?= =?utf-8?B?L3FISU4wZWx3a3VaVmJQVWhaMDQvYWtHS1FvMmh6cXN1L3l3NkpPazVPNUtQ?= =?utf-8?Q?uI2RDNfg8Z55KeSpjsUjbbWBE?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: e697dbea-de69-4c7c-3c76-08da9a8cd4e0 X-MS-Exchange-CrossTenant-AuthSource: DM4PR12MB5229.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Sep 2022 22:18:08.6098 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: QvLdqwgPsYlbu8ixy02mNkr/iQ6NHGqTpIv+ISRmBo6CZt0iHOintaLUjjOSGpZfMNMM2TPFkHFM6rCS0+MuRw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB7342 X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,T_FILL_THIS_FORM_SHORT autolearn=ham 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-crypto@vger.kernel.org On 9/19/22 17:02, Alper Gun wrote: > On Mon, Sep 19, 2022 at 2:38 PM Tom Lendacky wrote: >> >> On 9/19/22 12:53, Alper Gun wrote: >>> On Fri, Aug 19, 2022 at 9:54 AM Peter Gonda wrote: >>>> >>>>> + >>>>> +static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op, gpa_t gpa, >>>>> + int level) >>>>> +{ >>>>> + struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info; >>>>> + struct kvm *kvm = vcpu->kvm; >>>>> + int rc, npt_level; >>>>> + kvm_pfn_t pfn; >>>>> + gpa_t gpa_end; >>>>> + >>>>> + gpa_end = gpa + page_level_size(level); >>>>> + >>>>> + while (gpa < gpa_end) { >>>>> + /* >>>>> + * If the gpa is not present in the NPT then build the NPT. >>>>> + */ >>>>> + rc = snp_check_and_build_npt(vcpu, gpa, level); >>>>> + if (rc) >>>>> + return -EINVAL; >>>>> + >>>>> + if (op == SNP_PAGE_STATE_PRIVATE) { >>>>> + hva_t hva; >>>>> + >>>>> + if (snp_gpa_to_hva(kvm, gpa, &hva)) >>>>> + return -EINVAL; >>>>> + >>>>> + /* >>>>> + * Verify that the hva range is registered. This enforcement is >>>>> + * required to avoid the cases where a page is marked private >>>>> + * in the RMP table but never gets cleanup during the VM >>>>> + * termination path. >>>>> + */ >>>>> + mutex_lock(&kvm->lock); >>>>> + rc = is_hva_registered(kvm, hva, page_level_size(level)); >>>>> + mutex_unlock(&kvm->lock); >>>>> + if (!rc) >>>>> + return -EINVAL; >>>>> + >>>>> + /* >>>>> + * Mark the userspace range unmerable before adding the pages >>>>> + * in the RMP table. >>>>> + */ >>>>> + mmap_write_lock(kvm->mm); >>>>> + rc = snp_mark_unmergable(kvm, hva, page_level_size(level)); >>>>> + mmap_write_unlock(kvm->mm); >>>>> + if (rc) >>>>> + return -EINVAL; >>>>> + } >>>>> + >>>>> + write_lock(&kvm->mmu_lock); >>>>> + >>>>> + rc = kvm_mmu_get_tdp_walk(vcpu, gpa, &pfn, &npt_level); >>>>> + if (!rc) { >>>>> + /* >>>>> + * This may happen if another vCPU unmapped the page >>>>> + * before we acquire the lock. Retry the PSC. >>>>> + */ >>>>> + write_unlock(&kvm->mmu_lock); >>>>> + return 0; >>>>> + } >>>> >>>> I think we want to return -EAGAIN or similar if we want the caller to >>>> retry, right? I think returning 0 here hides the error. >>>> >>> >>> The problem here is that the caller(linux guest kernel) doesn't retry >>> if PSC fails. The current implementation in the guest kernel is that >>> if a page state change request fails, it terminates the VM with >>> GHCB_TERM_PSC reason. >>> Returning 0 here is not a good option because it will fail the PSC >>> silently and will probably cause a nested RMP fault later. Returning >> >> Returning 0 here is ok because the PSC current index into the PSC >> structure will not be updated and the guest will then retry (see the loop >> in vmgexit_psc() in arch/x86/kernel/sev.c). >> >> Thanks, >> Tom > > But the host code updates the index. It doesn't leave the loop because > rc is 0. The guest will think that it is successful. > rc = __snp_handle_page_state_change(vcpu, op, gpa, level); > if (rc) > goto out; > > Also the page state change request with MSR is not retried. It > terminates the VM if the MSR request fails. Ah, right. I see what you mean. It should probably return a -EAGAIN instead of 0 and then the if (rc) check should be modified to specifically look for -EAGAIN and goto out after setting rc to 0. But that does leave the MSR protocol open to the problem that you mention, so, yes, retry logic in snp_handle_page_state_change() for a -EAGAIN seems reasonable. Thanks, Tom > >> >>> an error also terminates the guest immediately with current guest >>> implementation. I think the best approach here is adding a retry logic >>> to this function. Retrying without returning an error should help it >>> work because snp_check_and_build_npt will be called again and in the >>> second attempt this should work. >>> >>>>> + >>>>> + /* >>>>> + * Adjust the level so that we don't go higher than the backing >>>>> + * page level. >>>>> + */ >>>>> + level = min_t(size_t, level, npt_level); >>>>> + >>>>> + trace_kvm_snp_psc(vcpu->vcpu_id, pfn, gpa, op, level); >>>>> + >>>>> + switch (op) { >>>>> + case SNP_PAGE_STATE_SHARED: >>>>> + rc = snp_make_page_shared(kvm, gpa, pfn, level); >>>>> + break; >>>>> + case SNP_PAGE_STATE_PRIVATE: >>>>> + rc = rmp_make_private(pfn, gpa, level, sev->asid, false); >>>>> + break; >>>>> + default: >>>>> + rc = -EINVAL; >>>>> + break; >>>>> + } >>>>> + >>>>> + write_unlock(&kvm->mmu_lock); >>>>> + >>>>> + if (rc) { >>>>> + pr_err_ratelimited("Error op %d gpa %llx pfn %llx level %d rc %d\n", >>>>> + op, gpa, pfn, level, rc); >>>>> + return rc; >>>>> + } >>>>> + >>>>> + gpa = gpa + page_level_size(level); >>>>> + } >>>>> + >>>>> + return 0; >>>>> +} >>>>> +