Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp765989pxk; Thu, 24 Sep 2020 19:03:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwCqNcTJBFvN6Gu7xBrNqDSE4Vp7H+L973Iz16Bs65crXkQqvOuRWhmWHdoAUcxqJoLLAtJ X-Received: by 2002:a17:907:264c:: with SMTP id ar12mr562376ejc.80.1600999387280; Thu, 24 Sep 2020 19:03:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600999387; cv=none; d=google.com; s=arc-20160816; b=DOi3XWB3pAsZ9qxc+qykLYh8njODVg4i75HR/XuAR1wl+mRm+FaKRrY7dh8cEkDIJQ TNmZCN7fP7mDUdVJ7u0nhXzrF+fq8/zg1xNw79G+qt9rQrNAKlWqQMRNFY+rebnXwESN W+X3QIW7dglUYh28x6Ih6GUCHRcBFa65ouo4Hnr3tZah0Pk/ufgn7ChnU3wcolNYiSSo e7VEkg7+OE9bXY9yXMquA3HUkQPnofneiYq108oxn0o0V3hyUovo6AfEJm2SvZMAcBzY gx7tJyRWqv99XFTqDjc14/S3Ni2bm6fXlcIoi9BIVT5x12Vp9Y2G2GSbdUJ4JM+033Lh 8x8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=Y06C3d8gRlVIiH+a/DZUSriRyLlq/Qy1nRNdUsM+sAw=; b=K/c6aD+I5E3lCHTri4S4rA+l45NxuIdsFZT4/aBUxu9nXfBJNLKl7bo1+Z+PhJYWzf XYiRylIHTP6Cn5zDB3hn8x6vHpYglSYETUklZ+BeJ73NlwDn2I86QRVIhOiFl2/8Ezht yraXiGS5xpf39mU0c8KsjzkICXhVh+AFYZ36OFRJ1o7tIq9unx2XGRk/CnpoU8SVeX4m H01cN32d7lr9t2VqDJzmlGd9yzOQNzw8HVdyYR17h3N7px2qNbF6yJKvjhKe3lLHHE7M vW69GUDrSG3SMnalcATg4b3DapTcqDfLNsdF5kLEpR+d4oxw2d52ZnUSIzdaFClxKCzi DXQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=GXfea3Qo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u16si932670ejr.4.2020.09.24.19.02.43; Thu, 24 Sep 2020 19:03:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=GXfea3Qo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 S1726945AbgIYCAi (ORCPT + 99 others); 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 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