Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3516792pxb; Wed, 13 Oct 2021 07:35:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxHPcPHiN6uA/ZuvmmiKMEU0NI3H3STUfAY3GmiIbefhITWDptriePnv3ErNRBf47Ob4g4s X-Received: by 2002:a17:902:bb81:b0:12d:a7ec:3d85 with SMTP id m1-20020a170902bb8100b0012da7ec3d85mr36442191pls.17.1634135722626; Wed, 13 Oct 2021 07:35:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634135722; cv=none; d=google.com; s=arc-20160816; b=VRVChMXuitz58DX+HDN1dko115h6+RdqrCrUSNy7hiMO5jTkMppRtxOeJghQc8k6+W KBry029UcWEXl19VEbOViaixUtMxeLaRLY0vC+p6xZvbsjzpOE2tM79qgOL+YflxBunp ADnkuywd+CF/fy9N3Kr7VieahzXRPTrq26/JqBBljtvktjirZK4m8RN6ujnx3+KLp0kE aaZtVUT7NHhSayjbXuuageCHsbHE36D8hTiIl3+LCP7WhmaEW+qwl9EMMsPnh2++t5IN prl/puMaYHU4K1Yf0eL9P1VPGLTSmwbXuW9LeLCa2bl0LoW5maI402g5YYkyMy/oNOJX JFBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=NtWwj0vH7UOJ5LLhXdT42vgomo2Iy/QVg4NPh0ov+mc=; b=LwnOvwuF1aPIOOPkxk24BdyoxXj+2fk58WwM7LG1fbi7rIReVai7Aw9Rm9xyG98GkN NEi8ZO/e4CBhtI3zF59LP014yrMIvJkm6fqzyHutmzJHxL4ZRDrGpU+VJ6Snx3fJOQLy QZzlyrk4XXBkhrPBei+cS2h9V/O3mPX3ameggHD5lkUYDi2mDUSSvBAYZ06CtE9E0CYT gtGJjpQf1xCJV3ibehTt4LjMvTkvjlhOwlgc+sLPEOFLRLdVD3irg1X5dSgC0d20cEpE +xB1Ij62NF/kFFh5hHuqvpEoIZT2ZMN5mMQ+NbZxIgU5GLW9Rrn16BYpCFwovNPz+mGO M6ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=i4jHCEDM; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c15si20034616pfv.137.2021.10.13.07.34.57; Wed, 13 Oct 2021 07:35:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-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=20210112 header.b=i4jHCEDM; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 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 S234081AbhJMOgz (ORCPT + 99 others); Wed, 13 Oct 2021 10:36:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230076AbhJMOgt (ORCPT ); Wed, 13 Oct 2021 10:36:49 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CD62C061762 for ; Wed, 13 Oct 2021 07:34:42 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id pf6-20020a17090b1d8600b0019fa884ab85so4573956pjb.5 for ; Wed, 13 Oct 2021 07:34:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=NtWwj0vH7UOJ5LLhXdT42vgomo2Iy/QVg4NPh0ov+mc=; b=i4jHCEDMgA0MeyPQyBueIE183Ak+qcCdarKYGWRPkUbsoFRpR/IIG8+yyWU35jZA8N qt9Gsc06jQoaDSjLFsLCun/odI+PcXwZjX3t/l0tvyRGsZy5eJkHGIaoOYzvlDuYFFlN MIdgOfHGuvy0SL810seRJWQ7DYQjta/zNTMMSmK6c8FQ7aCskR3S+wHNOnfHwVBmn4nC fcO+YFvOPF2nSgSjlcYflMzl45jRp0Mnd8pwsT1TKR4l5J/m6KwuTUFFu8v1BBHaqVgS gJwIcf16TQtrx/rQbdxOOhHx35TR6YwE9FsnuZ0PLGlfrmNm1jT4+585xBMNJDVcMI2F 1Q0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=NtWwj0vH7UOJ5LLhXdT42vgomo2Iy/QVg4NPh0ov+mc=; b=jq1boWFrJEotsG9rBNEa3qUwZr0oZWYHX6L9nl7bsCCJJV62i/7Wo/RZ/zQaj4YpYZ B++3gVh4bc+zfu7AOj5NJMdUw6aB/lFq/V53zyBek1Ms63OwOjCto9X9YVisXkfnrkmd kOAVHgNbkj4/+Qd/v54CkJ7VoHltmP3gDntaU1DWBSy/+pFWstlLNue5T4kbyUZcauwa 4wq54J6aYqCZUJJ6xDbUXM6u8wvOZdt5FQLnG06r6XBGayLSuv3shL4lzVgs5cm/i/Ql y3tu8uxl2HmKH2a4F5RMaLtw0nz25KPqTYFLjGLyg4NvpwCRO+7P06tQyKNXnpfiMUt/ OrOQ== X-Gm-Message-State: AOAM531c0my0eghTF/GBwgbzNKE2StPjl2PF6wBPPwUOBKaHtZEw4QJL 4baEyl5a/lVXoqpVkpb6sjVa/g== X-Received: by 2002:a17:902:8d8c:b0:13d:be20:e279 with SMTP id v12-20020a1709028d8c00b0013dbe20e279mr36053185plo.5.1634135681790; Wed, 13 Oct 2021 07:34:41 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id d60sm6114241pjk.49.2021.10.13.07.34.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Oct 2021 07:34:41 -0700 (PDT) Date: Wed, 13 Oct 2021 14:34:37 +0000 From: Sean Christopherson To: Brijesh Singh Cc: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , "Kirill A . Shutemov" , Andi Kleen , tony.luck@intel.com, marcorr@google.com, sathyanarayanan.kuppuswamy@linux.intel.com Subject: Re: [PATCH Part2 v5 26/45] KVM: SVM: Mark the private vma unmerable for SEV-SNP guests Message-ID: References: <20210820155918.7518-1-brijesh.singh@amd.com> <20210820155918.7518-27-brijesh.singh@amd.com> <2a8bf18e-1413-f884-15c4-0927f34ee3b9@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2a8bf18e-1413-f884-15c4-0927f34ee3b9@amd.com> Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Wed, Oct 13, 2021, Brijesh Singh wrote: > > On 10/12/21 11:46 AM, Sean Christopherson wrote: > > On Fri, Aug 20, 2021, Brijesh Singh wrote: > >> When SEV-SNP is enabled, the guest private pages are added in the RMP > >> table; while adding the pages, the rmp_make_private() unmaps the pages > >> from the direct map. If KSM attempts to access those unmapped pages then > >> it will trigger #PF (page-not-present). > >> > >> Encrypted guest pages cannot be shared between the process, so an > >> userspace should not mark the region mergeable but to be safe, mark the > >> process vma unmerable before adding the pages in the RMP table. > > To be safe from what? Does the !PRESENT #PF crash the kernel? > > Yes, kernel crashes when KSM attempts to access to an unmaped pfn. Is this problem unique to nuking the direct map (patch 05), or would it also be a problem (in the form of an RMP violation) if the direct map were demoted to 4k pages? > [...] > >> + mmap_write_lock(kvm->mm); > >> + ret = snp_mark_unmergable(kvm, params.uaddr, params.len); > >> + mmap_write_unlock(kvm->mm); > > This does not, and practically speaking cannot, work. There are multiple TOCTOU > > bugs, here and in __snp_handle_page_state_change(). Userspace can madvise() the > > range at any later point, munmap()/mmap() the entire range, mess with the memslots > > in the PSC case, and so on and so forth. Relying on MADV_UNMERGEABLE for functional > > correctness simply cannot work in KVM, barring mmu_notifier and a big pile of code. > > AFAICT, ksm does not exclude the unmapped pfn from its scan list. We > need to tell ksm somehow to exclude the unmapped pfn from its scan list. > I understand that if userspace is messing with us, we have an issue, but > it's a userspace bug ;) To fix it right, we need to enhance ksm to > exclude the pfn when it is getting unmapped from the direct map. I > believe that work can be done outside of the SNP series. I am okay to > drop snp_mark_unmerable(), and until then, we just run with KSM > disabled. Thoughts? > > thanks