Received: by 2002:a05:6a10:5594:0:0:0:0 with SMTP id ee20csp234861pxb; Mon, 25 Apr 2022 09:04:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy2DVjrVnspk1PG46ZxAAcRb5X6guoDkHbbkTgzcJYzQ69cpI25U4Ig1Z0RoG0KTUx5FL9j X-Received: by 2002:a67:f511:0:b0:32c:c7d0:de66 with SMTP id u17-20020a67f511000000b0032cc7d0de66mr1595382vsn.82.1650902652067; Mon, 25 Apr 2022 09:04:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650902652; cv=none; d=google.com; s=arc-20160816; b=MmO4ZfzOkmqblk/iJld1pB30r47HLMddq3XkS/M2uxbEaXKyqPBmolQQAj+b3ALltX hlY4o1+GjGndrfBFVF+APWg0rB6UmBP2wGv0SWl1hRYdlBeS/oRCP3jE+h2uQcPqwoV6 ME6ztkU472PdIqtzY82UQS4hklMbVNPdXUlghQWUAoUqyzka227vNY35E3WO6c4Ksn+b pj/lzkdh+3DcRsGeip/jfEIoecYEUx/LJGbLRvxMRara4yaPrjmm56MMH0yfP90y/CcM 6g1lqa5qBTd3QTeftFxjP+hKAue5LYdyB9+eXC1TudRRDa1GIp8rDdW6alVOTJtETa8V v0Lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:cc:to:from:date:references:in-reply-to :message-id:mime-version:user-agent:dkim-signature; bh=dTVneFUuxuaRYvbFayKpHXaZ1DQ6wzRxxMEn46Offuc=; b=pkyNK8L6ARLszXR1/8tIBlSWnV2BwBWm82r2jOgIFbY20HyB4fZFx/LPBpRiej+LKS ++DDtrDcJq9v5ViyXQJfjqqMndwzt/MXt6sXn/N+NZXL/6z29S4A11CqNYEUVwbXPbsN gtHDibOINvw4/cXbBlv1V+h71RykoJQkjj4VxlFNpmfzVmXUVj5adzg0kfN6/GfLqXvH 9wv9fOCzZGqgKdajSH3n9xXLZnHZvSKPK8ESHfpjrRZT6GuXwNaWCZfPy89iylDbf0Ro yUHwmuodDYmUOqiF8MmCh78ZiWoTdADF7U0ktV2QnRC/C0/FRpvPQ0k3wmxtl4UItVfp WoBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DcaQOwd2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dc24-20020a056102559800b0032c367877d2si675131vsb.91.2022.04.25.09.03.50; Mon, 25 Apr 2022 09:04:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@kernel.org header.s=k20201202 header.b=DcaQOwd2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242595AbiDYO4O (ORCPT + 99 others); Mon, 25 Apr 2022 10:56:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241240AbiDYO4L (ORCPT ); Mon, 25 Apr 2022 10:56:11 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 247ED34BB9 for ; Mon, 25 Apr 2022 07:53:07 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 57B51CE1721 for ; Mon, 25 Apr 2022 14:53:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9CBCDC385AE; Mon, 25 Apr 2022 14:53:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1650898383; bh=2d/1yAOVmYsCW9kJuQkBsU2TaXtcebq8zUB95NfIY+o=; h=In-Reply-To:References:Date:From:To:Cc:Subject:From; b=DcaQOwd2glVEIGv68OVG+JqWGxGpGsbOq9cnQ9+ci+DHgf/FSwnn6inBuWIlDT0j2 f2O/X9AdBeVZw0bxpVR2sjRcgL4c02GMUpuOBLfiWufgOuBG/50lfZjGooD9dcfty5 +tgeKgMHDUAC2gpFo3Gumc9JbT/c48Y/91WlUfMrKU1chTnaOwMsG3dcgGJtiPpk7X h9Qa9PG1X4OWoveTyLdciVuF6yqdQ5z9Frg2BWUIxG6mA6m73jQVBTcARjUP0i4YZA GF2hawVOkZ1jyldWVDtqeV1Hwldg759FPWrd8KPCSCqIq2RB/8tPJbj13xJ/0WUV0Q QKXvt4bHisc3g== Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailauth.nyi.internal (Postfix) with ESMTP id 7C3AC27C0054; Mon, 25 Apr 2022 10:53:00 -0400 (EDT) Received: from imap48 ([10.202.2.98]) by compute2.internal (MEProxy); Mon, 25 Apr 2022 10:53:00 -0400 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedruddugdektdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvvefutgesthdtredtreertdenucfhrhhomhepfdetnhgu hicunfhuthhomhhirhhskhhifdcuoehluhhtoheskhgvrhhnvghlrdhorhhgqeenucggtf frrghtthgvrhhnpedvhfeuvddthfdufffhkeekffetgffhledtleegffetheeugeejffdu hefgteeihfenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpegrnhguhidomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqudduiedukeeh ieefvddqvdeifeduieeitdekqdhluhhtoheppehkvghrnhgvlhdrohhrgheslhhinhhugi drlhhuthhordhush X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 0E2B821E006E; Mon, 25 Apr 2022 10:52:59 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.7.0-alpha0-569-g7622ad95cc-fm-20220421.002-g7622ad95 Mime-Version: 1.0 Message-Id: <27616b2f-1eff-42ff-91e0-047f531639ea@www.fastmail.com> In-Reply-To: <20220425134051.GA175928@chaop.bj.intel.com> References: <80aad2f9-9612-4e87-a27a-755d3fa97c92@www.fastmail.com> <83fd55f8-cd42-4588-9bf6-199cbce70f33@www.fastmail.com> <20220422105612.GB61987@chaop.bj.intel.com> <3b99f157-0f30-4b30-8399-dd659250ab8d@www.fastmail.com> <20220425134051.GA175928@chaop.bj.intel.com> Date: Mon, 25 Apr 2022 07:52:38 -0700 From: "Andy Lutomirski" To: "Chao Peng" Cc: "Sean Christopherson" , "Quentin Perret" , "Steven Price" , "kvm list" , "Linux Kernel Mailing List" , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, "Linux API" , qemu-devel@nongnu.org, "Paolo Bonzini" , "Jonathan Corbet" , "Vitaly Kuznetsov" , "Wanpeng Li" , "Jim Mattson" , "Joerg Roedel" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "the arch/x86 maintainers" , "H. Peter Anvin" , "Hugh Dickins" , "Jeff Layton" , "J . Bruce Fields" , "Andrew Morton" , "Mike Rapoport" , "Maciej S . Szmigiero" , "Vlastimil Babka" , "Vishal Annapurve" , "Yu Zhang" , "Kirill A. Shutemov" , "Nakajima, Jun" , "Dave Hansen" , "Andi Kleen" , "David Hildenbrand" , "Marc Zyngier" , "Will Deacon" Subject: Re: [PATCH v5 00/13] KVM: mm: fd-based approach for supporting KVM guest private memory Content-Type: text/plain X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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-kernel@vger.kernel.org On Mon, Apr 25, 2022, at 6:40 AM, Chao Peng wrote: > On Sun, Apr 24, 2022 at 09:59:37AM -0700, Andy Lutomirski wrote: >> >> >> 2. Bind the memfile to a VM (or at least to a VM technology). Now it's in the initial state appropriate for that VM. >> >> For TDX, this completely bypasses the cases where the data is prepopulated and TDX can't handle it cleanly. For SEV, it bypasses a situation in which data might be written to the memory before we find out whether that data will be unreclaimable or unmovable. > > This sounds a more strict rule to avoid semantics unclear. > > So userspace needs to know what excatly happens for a 'bind' operation. > This is different when binds to different technologies. E.g. for SEV, it > may imply after this call, the memfile can be accessed (through mmap or > what ever) from userspace, while for current TDX this should be not allowed. I think this is actually a good thing. While SEV, TDX, pKVM, etc achieve similar goals and have broadly similar ways of achieving them, they really are different, and having userspace be aware of the differences seems okay to me. (Although I don't think that allowing userspace to mmap SEV shared pages is particularly wise -- it will result in faults or cache incoherence depending on the variant of SEV in use.) > > And I feel we still need a third flow/operation to indicate the > completion of the initialization on the memfile before the guest's > first-time launch. SEV needs to check previous mmap-ed areas are munmap-ed > and prevent future userspace access. After this point, then the memfile > becomes truely private fd. Even that is technology-dependent. For TDX, this operation doesn't really exist. For SEV, I'm not sure (I haven't read the specs in nearly enough detail). For pKVM, I guess it does exist and isn't quite the same as a shared->private conversion. Maybe this could be generalized a bit as an operation "measure and make private" that would be supported by the technologies for which it's useful. > >> >> >> ---------------------------------------------- >> >> Now I have a question, since I don't think anyone has really answered it: how does this all work with SEV- or pKVM-like technologies in which private and shared pages share the same address space? I sounds like you're proposing to have a big memfile that contains private and shared pages and to use that same memfile as pages are converted back and forth. IO and even real physical DMA could be done on that memfile. Am I understanding correctly? > > For TDX case, and probably SEV as well, this memfile contains private memory > only. But this design at least makes it possible for usage cases like > pKVM which wants both private/shared memory in the same memfile and rely > on other ways like mmap/munmap or mprotect to toggle private/shared instead > of fallocate/hole punching. Hmm. Then we still need some way to get KVM to generate the correct SEV pagetables. For TDX, there are private memslots and shared memslots, and they can overlap. If they overlap and both contain valid pages at the same address, then the results may not be what the guest-side ABI expects, but everything will work. So, when a single logical guest page transitions between shared and private, no change to the memslots is needed. For SEV, this is not the case: everything is in one set of pagetables, and there isn't a natural way to resolve overlaps. If the memslot code becomes efficient enough, then the memslots could be fragmented. Or the memfile could support private and shared data in the same memslot. And if pKVM does this, I don't see why SEV couldn't also do it and hopefully reuse the same code. > >> >> If so, I think this makes sense, but I'm wondering if the actual memslot setup should be different. For TDX, private memory lives in a logically separate memslot space. For SEV and pKVM, it doesn't. I assume the API can reflect this straightforwardly. > > I believe so. The flow should be similar but we do need pass different > flags during the 'bind' to the backing store for different usages. That > should be some new flags for pKVM but the callbacks (API here) between > memfile_notifile and its consumers can be reused. And also some different flag in the operation that installs the fd as a memslot? > >> >> And the corresponding TDX question: is the intent still that shared pages aren't allowed at all in a TDX memfile? If so, that would be the most direct mapping to what the hardware actually does. > > Exactly. TDX will still use fallocate/hole punching to turn on/off the > private page. Once off, the traditional shared page will become > effective in KVM. Works for me. For what it's worth, I still think it should be fine to land all the TDX memfile bits upstream as long as we're confident that SEV, pKVM, etc can be added on without issues. I think we can increase confidence in this by either getting one other technology's maintainers to get far enough along in the design to be confident and/or by having a pure-kernel-software implementation that serves as a testbed. For the latter, maybe it could support two different models with little overhead: Pure software "interleaved" model: pages are shared or private and a hypercall converts them. The access mode is entirely determined by the state programmed by hypercall. I think this is essentially what Vishal implemented, but with the "HACK" replaced by something permanent and (if they're not already in the series) appropriate access checks implemented to actually protect the private memory. Pure software "separate" mode: one GPA bit is set aside as the shared vs private bit. The normal memslots are restricted to the shared half of GPA space. Private memslots use the private half. This works a lot like TDX. This would be new code. We don't *really* need this for testing, since TDX itself exercises the same programming model, but it would let people without TDX hardware exercise the interesting bits of the memory management. Paolo, etc: what do you think? > > Chao >> >> --Andy