From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D47A2C4346E for ; Fri, 25 Sep 2020 02:00:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7602620888 for ; Fri, 25 Sep 2020 02:00:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GXfea3Qo" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727020AbgIYCAi (ORCPT ); Thu, 24 Sep 2020 22:00:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726718AbgIYCAi (ORCPT ); Thu, 24 Sep 2020 22:00:38 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7EA3C0613CE for ; Thu, 24 Sep 2020 19:00:37 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id r9so1201720ybd.20 for ; Thu, 24 Sep 2020 19:00:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=Y06C3d8gRlVIiH+a/DZUSriRyLlq/Qy1nRNdUsM+sAw=; b=GXfea3Qo5SPVCB6zPnPqFml4mMW4ETxCYvtg6DkSi8qUGa1Zwf9u6NLZJsEukeZZLO 9ozz3hSeWFxj2yzhjImj8k1WGGPrJq+u+ncjP0sWrEb1gSFusuBGk1k/JQFIwfVlxYbb nopXxvAEt2X3V6ShgrPyWnxP23jSmaAbt4En49JyFwCYca6Smxa1V37vrwXa8eKDhuE+ jZeIdWLYYapx1aH+qtubvC3Gzc76YFMhbNMl47t3s43mmYU5K7XD4QERdLROZUwhI/Pv 3R1nJ/KXxngXUsjI4UpazW297HMCdW3XBTGhx2NjcC+7NRbfPExV1pzqYCDjcjWO1Ws5 JThA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Y06C3d8gRlVIiH+a/DZUSriRyLlq/Qy1nRNdUsM+sAw=; b=ZjyMRRufrc4UtIMG77vDCAcC/VKAoVYIkrjBVfaMNtd3OjKBXZ0bGoK/65TTeJUJOq BxHLe9c1nEO7ny8NQxQH0FErunykJSFBUx+1tiFfzhcpq+hVpid8Nmf7abvy6yrfrzIp QW7CyLSNtB2HEHsOs8kj9pXrdRq9umGm0DXtyygOdp6uvv+EosiVLQjx8l4ZtFbg13zt JJQNY7Z4rwOheVNXpikzJwjZMJ0Cc8H13m/2cJ+tvlnTmXJ0zgVC3jGnhrNRF5x+BEET bVk4OAZfQJh/sSeTU4l9QaqmkYtQv+bXD0ctVNRDuqoiHXorbZtQaDXFodyCC36GccaB qYMQ== X-Gm-Message-State: AOAM531Ibm2+Hu80YCSxy0MO3iAPPJz+E7qBOIU397nNHhJbOELe/0Bl CuyGbN6L3LyaYd3+OwhCpJZyTLb2 X-Google-Smtp-Source: ABdhPJz5rQ1t0pb1JJRyVoLVT/N/+7t6zAsJOr3F9AwPqot8pRccyoYZLojtSZ41PDH3NySyRqTRfjIN Sender: "cfir via sendgmr" X-Received: from cfir.sea.corp.google.com ([2620:15c:158:202:a6ae:11ff:fe11:da08]) (user=cfir job=sendgmr) by 2002:a25:e80d:: with SMTP id k13mr2135631ybd.179.1600999237042; Thu, 24 Sep 2020 19:00:37 -0700 (PDT) Date: Thu, 24 Sep 2020 19:00:11 -0700 In-Reply-To: <20200807012303.3769170-1-cfir@google.com> Message-Id: <20200925020011.1159247-1-cfir@google.com> Mime-Version: 1.0 References: <20200807012303.3769170-1-cfir@google.com> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [PATCH] KVM: SVM: Mark SEV launch secret pages as dirty. From: Cfir Cohen To: "kvm @ vger . kernel . org" , Lendacky Thomas , Singh Brijesh Cc: Grimm Jon , David Rientjes , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" , x86@kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Cfir Cohen Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The LAUNCH_SECRET command performs encryption of the launch secret memory contents. Mark pinned pages as dirty, before unpinning them. This matches the logic in sev_launch_update_data(). Fixes: 9c5e0afaf157 ("KVM: SVM: Add support for SEV LAUNCH_SECRET command") Signed-off-by: Cfir Cohen --- Changelog since v2: - Added 'Fixes' tag, updated comments. Changelog since v1: - Updated commit message. arch/x86/kvm/svm/sev.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 5573a97f1520..55edaf3577a0 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -440,10 +440,8 @@ static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) } /* - * The LAUNCH_UPDATE command will perform in-place encryption of the - * memory content (i.e it will write the same memory region with C=1). - * It's possible that the cache may contain the data with C=0, i.e., - * unencrypted so invalidate it first. + * Flush (on non-coherent CPUs) before LAUNCH_UPDATE encrypts pages in + * place, the cache may contain data that was written unencrypted. */ sev_clflush_pages(inpages, npages); @@ -799,10 +797,9 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) } /* - * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the - * memory content (i.e it will write the same memory region with C=1). - * It's possible that the cache may contain the data with C=0, i.e., - * unencrypted so invalidate it first. + * Flush (on non-coherent CPUs) before DBG_{DE,EN}CRYPT reads or modifies + * the pages, flush the destination too in case the cache contains its + * current data. */ sev_clflush_pages(src_p, 1); sev_clflush_pages(dst_p, 1); @@ -850,7 +847,7 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) struct kvm_sev_launch_secret params; struct page **pages; void *blob, *hdr; - unsigned long n; + unsigned long n, i; int ret, offset; if (!sev_guest(kvm)) @@ -863,6 +860,12 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) if (!pages) return -ENOMEM; + /* + * Flush (on non-coherent CPUs) before LAUNCH_SECRET encrypts pages in + * place, the cache may contain data that was written unencrypted. + */ + sev_clflush_pages(pages, n); + /* * The secret must be copied into contiguous memory region, lets verify * that userspace memory pages are contiguous before we issue command. @@ -908,6 +911,11 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) e_free: kfree(data); e_unpin_memory: + /* content of memory is updated, mark pages dirty */ + for (i = 0; i < n; i++) { + set_page_dirty_lock(pages[i]); + mark_page_accessed(pages[i]); + } sev_unpin_memory(kvm, pages, n); return ret; } -- 2.28.0.681.g6f77f65b4e-goog