Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3093763rwb; Mon, 19 Sep 2022 15:03:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM728rPjhr3OmwGSww5Wc03PdQ2Rkc4QqKjY/5lEe9Ty3dtIjsDvmNq04dqdx9n8J6t7xL3u X-Received: by 2002:a05:6a00:1743:b0:548:8629:ceab with SMTP id j3-20020a056a00174300b005488629ceabmr19980410pfc.23.1663625006575; Mon, 19 Sep 2022 15:03:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663625006; cv=none; d=google.com; s=arc-20160816; b=QmQDIB+vOeHG+IGdIXCotcpFOxdofiezZEahKVuAR1PMKsqoaZDPTcXYB0ot4mvdqh lbgecFJuPzlsDCB7ZLkkpGsVeezIoN+/5eNtobF3YPbVbFT+YhbeOH9d0iLdnePP1ang nC1pRCRw8opqjW7xuToekbDS+d+qd96BBncQt83L77m/10Wc15vhak/5qyq1OBnAVQzl ozBVmOQ3uXUPgjisz8zHonaR0LNONx9w1EMURmC+OozmYGCu5eoGePjS1WH+GjMDNeZN kZeevLcuVDSgAPulOx7dhCoLxWlXtWl2cFWvAsYUcOeBqNU96WeRrAD2M22jPm5mymgE 8YuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Ph6MNOnGKLISbGCGLjM6coGHkzrba1s1LnEs9FR4sTM=; b=Br7aw81UMCQNvjHJEw1+5w3vvSqh10de+6QU8C6Ifp8hKgDLzGZprm3rn7tRq/QPW3 PbR/2hjPetbjuJmok4ZZFf8PQ16Z8X4tFI/vu6bhM4gEhp2R7yxnOhvQ6eWqAzhze2Lp SkwBR5Q3fh7qgEi9lvn+J8genX+WV91iHgNGzZOPMm7bpXnKKmrHq8CV6TI+lOI2/Tzn psYrEyYORa5pF8pUxiReYJqNelpwnatVm+7GF7z9Wsy6BZhDLU1lOy/EvY+qPkmdcKBV kcRPT4lkde9FqjxyxQZMl9ZFyoIvtYTHyvLGhLq2ZwImD/GLr8Z37M3ge+8jq+7ndkbw Lkvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=VU+BHb8c; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n63-20020a632742000000b00437e609414dsi891963pgn.563.2022.09.19.15.03.07; Mon, 19 Sep 2022 15:03:26 -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=@google.com header.s=20210112 header.b=VU+BHb8c; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229535AbiISWDE (ORCPT + 99 others); Mon, 19 Sep 2022 18:03:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229568AbiISWDC (ORCPT ); Mon, 19 Sep 2022 18:03:02 -0400 Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D03743E64 for ; Mon, 19 Sep 2022 15:03:01 -0700 (PDT) Received: by mail-io1-xd2f.google.com with SMTP id v128so703689ioe.12 for ; Mon, 19 Sep 2022 15:03:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=Ph6MNOnGKLISbGCGLjM6coGHkzrba1s1LnEs9FR4sTM=; b=VU+BHb8cdS/Oa5JCSIUk+cXt21pVVXxcuXbg4Oet5MqxrY6yxDbfiqTPEx84/lbOj2 kJsyplHAsJmY60goMoUelqDKKpvW1eYrVaU2OgDhhDNeBqN22dSIaVdLng883i4zWf4h Gkld3gnvhbMhI1euLW8xw8xeU2zxN4G7HZab1Oj63H50nH8x+2uiqJTkQRZHQs0vH9JS 9Lqqi8b40cZDeV2EmscIqj/kUi1dzOkGriuMdGLWKZuGrYQTBdBfcQ511vceGfx3RDPX zAvh6W1gwAO80jRbnp3JmP5zEoAkIKdNHwFiL+17rzYZ/lzs5lxY5aMCug385OmLvmhB Hyrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=Ph6MNOnGKLISbGCGLjM6coGHkzrba1s1LnEs9FR4sTM=; b=xDXFbrI8z16mmT8TChBtrdELx+TFAs6e4YZKZJMlIutxOm9L2z2B6cK6/sbydqjYAa OuXpaRS/rf3thmnQieu5fBPbTaNbpTUN5Kte8Xc+6rgU2karJil1lf/YO33u1XxJR1zz yyXLLGlYl2g9fOkKwJAhZzetxlm5vCNjDY8xGq7JCwH8HHAEoZocGeAiFO5lkIHQ5pMS u0hMMDYzG7YvHVgkLBY6CUDyM/f5JZjbDPoB3m2fFQkEjmw2JmRwYDittSzfrit9Q5i8 wyatd4xKRI0JoaerQ2oLx1jse6RvfU15FTCpbLu0D7J8IGo95iHqp4iFEnNM8698Xj+l 5ulg== X-Gm-Message-State: ACrzQf2W0M+v8RwZZ4R030m9jFKYfin3sop5eNeY5Jtr7oq9BoowxQlQ tFo4uFh96ETL88gHTkcN9HRsJQ8LaaR/eMnNTH+dsQ== X-Received: by 2002:a05:6638:40a3:b0:35a:3f2d:a21c with SMTP id m35-20020a05663840a300b0035a3f2da21cmr9196001jam.221.1663624980515; Mon, 19 Sep 2022 15:03:00 -0700 (PDT) MIME-Version: 1.0 References: <78e30b5a25c926fcfdcaafea3d484f1bb25f20b9.1655761627.git.ashish.kalra@amd.com> <0716365f-3572-638b-e841-fcce7d30571a@amd.com> In-Reply-To: <0716365f-3572-638b-e841-fcce7d30571a@amd.com> From: Alper Gun Date: Mon, 19 Sep 2022 15:02:49 -0700 Message-ID: Subject: Re: [PATCH Part2 v6 37/49] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT To: Tom Lendacky 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_FILL_THIS_FORM_SHORT,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 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. > > > 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; > >>> +} > >>> +