Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4009492pxv; Mon, 19 Jul 2021 14:20:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxkGovu7nb9EdYnTU2XRULo/XcDjNPxNiw2g9TIriqiyTo81dI3UqrMDpBm9K1E635gLoKx X-Received: by 2002:a17:906:d105:: with SMTP id b5mr29271833ejz.50.1626729650196; Mon, 19 Jul 2021 14:20:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626729650; cv=none; d=google.com; s=arc-20160816; b=EwZ9nfI+3hu2mlWL70b7UZ6l06DqGtaz6rrARgOfogf/5Ext8VdteJku+hCh6QC7qo 5i009LTthzxRab6/fRHixBUxzNcfogAr7fkr0cyjPpk4CVCg0YEGwRoNtvgxExhokQUn qCv8KqlZSSmaNuAed1EtgmI1n11zzUUJDsN6ERU7hh5N3DyYmzNJgovCwQ9o5Sa1adx7 cHLY9JsF3YbX7NsS6l/wJ613o52mUtAa6EzZgE2w6u9JWyQ/ZlI0V+mfjumjcgFEqMza T+C1gqOYXF6D0NTh+4p5mXcrj9yiUHevsgk/WfqXB/Oer84awCv1suzZ1hPhmqjT7IFl DoDg== 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=AFTM2GRWufz5H7ZF2I53fhdcy31mPSD1WmaoCuSp/kE=; b=ftcZxx4ud2KlUfn4iE0PURgJbmfUdpcejZGTmU80l2SoxR77aNT3MB/fWbY594HSi6 mDSGq2l2USowM6v8EiYOT5WuWeGTHoYIPxOLTbP6ZKhhI17xoj7L7cVo1HCMiuIWUnRg Uqek/ttbRENkVI60L1sHm9/Qo0rJGm13Tg1vxwfb3g/r4Mw1sjYY8Xd4EWOVZ7P2lIC5 I/s/wflQ3vjaUHwyUhSTvjkgLQU0Qw3XfbR96D3R6lFNa0/LCKd3SZs4jrZLEXFV/3wh lFvtpjzkc1i6nRx+hp+Y0gUx+rKYLu77Tl3MoAaSDfXydmob5e/iijt/iwy2wGR8wRda qUGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EaCLfVRU; 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 dy9si23229765edb.114.2021.07.19.14.20.17; Mon, 19 Jul 2021 14:20:50 -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=EaCLfVRU; 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 S1387697AbhGSU1z (ORCPT + 99 others); Mon, 19 Jul 2021 16:27:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1387494AbhGSUL1 (ORCPT ); Mon, 19 Jul 2021 16:11:27 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CAA0C061574 for ; Mon, 19 Jul 2021 13:50:20 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id 21so17675091pfp.3 for ; Mon, 19 Jul 2021 13:52:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=AFTM2GRWufz5H7ZF2I53fhdcy31mPSD1WmaoCuSp/kE=; b=EaCLfVRUeWm1VySqrv8i2wNaa1wkPklvEK+xtMHdsSwgX2wFqb1Td6Qxx/Gb5l+cVb iFDBBPj7D4Vc1AccEll1JyWJ1bUw6f6E5fWHeRlt33bQVWVeosWbQYPcJTxJJzXLzM9O kIducZm41jdtN6sUFDlUG707INiMICk4cgWapIwGO3HfFDCHIJXCNZP4gXRP+tRtGBAe upzRB6mNR4POD1tE5HNErufrnwLonZnXJR1npy0SArVe6sk5bxak769zdSKot2JBoe2R 8I+m0tueFJM91D9WIUbZQDxA3BDZrk+shYWYLpiKWRmq9uaqtm4emtrPiHXDahsjmVPG OV2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=AFTM2GRWufz5H7ZF2I53fhdcy31mPSD1WmaoCuSp/kE=; b=neCVKbyepg7KWlkoUQeTRI5M7j+bf3/hQQL5nmqMSm9zo1QB6McCNhKEW/B/49KpB6 OLP0SrFPyVjvEdf6v9l+9OLYL6dNiQ5F6zyqq6wQ4Bc6txZuKi/l6iYfCBQquLDQT4B4 +Ed58zhCyPDvT06b+xiJhyfW7zGPV8l8RvFYdAERWD8PQqJXjgguYu5Cwk+IPjCMOBZD 10YJ6qA6YoPJ9AkiIVplwgmf8u9TSwNLAVkzL7DvfpBk7LZfe3Le95Z3dwXtG8oqBmer ydp6SYoZS0gopAwFs46Arov9LQTAA98AM0qePa/NEmv8TKzkoYLuIfB+DgsIezmuIhJz 2PqA== X-Gm-Message-State: AOAM532njvQQcJQ994B6jfz/wcF6Ya5AAw6DBdphoHZHsfQraG0o2sAW EzkL9AtxxqVtEJkPBn3ObD1xUnxghhjQrg== X-Received: by 2002:a62:3045:0:b029:32b:880f:c03a with SMTP id w66-20020a6230450000b029032b880fc03amr27819158pfw.22.1626727923390; Mon, 19 Jul 2021 13:52:03 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id v10sm381814pjd.29.2021.07.19.13.52.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jul 2021 13:52:02 -0700 (PDT) Date: Mon, 19 Jul 2021 20:51:58 +0000 From: Sean Christopherson To: Brijesh Singh Cc: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com Subject: Re: [PATCH Part2 RFC v4 24/40] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command Message-ID: References: <20210707183616.5620-1-brijesh.singh@amd.com> <20210707183616.5620-25-brijesh.singh@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 16, 2021, Brijesh Singh wrote: > > On 7/16/21 3:01 PM, Sean Christopherson wrote: > > I'm having a bit of deja vu... This flow needs to hold kvm->srcu to do a memslot > > lookup. > > > > That said, IMO having KVM do the hva->gpa is not a great ABI. The memslots are > > completely arbitrary (from a certain point of view) and have no impact on the > > validity of the memory pinning or PSP command. E.g. a memslot update while this > > code is in-flight would be all kinds of weird. > > > > In other words, make userspace provide both the hva (because it's sadly needed > > to pin memory) as well as the target gpa. That prevents KVM from having to deal > > with memslot lookups and also means that userspace can issue the command before > > configuring the memslots (though I've no idea if that's actually feasible for > > any userspace VMM). > > The operation happen during the guest creation time so I was not sure if > memslot will be updated while we are executing this command. But I guess > its possible that a VMM may run different thread which may update > memslot while another thread calls the encryption. I'll let userspace > provide both the HVA and GPA as you recommended. I'm not worried about a well-behaved userspace VMM, I'm worried about the code KVM has to carry to guard against a misbehaving VMM. > >> + ret = -EINVAL; > >> + goto e_unpin; > >> + } > >> + > >> + psize = page_level_size(level); > >> + pmask = page_level_mask(level); > > Is there any hope of this path supporting 2mb/1gb pages in the not-too-distant > > future? If not, then I vote to do away with the indirection and just hardcode > > 4kg sizes in the flow. I.e. if this works on 4kb chunks, make that obvious. > > No plans to do 1g/2mb in this path. I will make that obvious by > hardcoding it. > > > >> + gpa = gpa & pmask; > >> + > >> + /* Transition the page state to pre-guest */ > >> + memset(&e, 0, sizeof(e)); > >> + e.assigned = 1; > >> + e.gpa = gpa; > >> + e.asid = sev_get_asid(kvm); > >> + e.immutable = true; > >> + e.pagesize = X86_TO_RMP_PG_LEVEL(level); > >> + ret = rmpupdate(inpages[i], &e); > > What happens if userspace pulls a stupid and assigns the same page to multiple > > SNP guests? Does RMPUPDATE fail? Can one RMPUPDATE overwrite another? > > The RMPUPDATE is available to the hv and it can call anytime with > whatever it want. The important thing is the RMPUPDATE + PVALIDATE > combination is what locks the page. In this case, PSP firmware updates > the RMP table and also validates the page. > > If someone else attempts to issue another RMPUPDATE then Validated bit > will be cleared and page is no longer used as a private. Access to > unvalidated page will cause #VC. Hmm, and there's no indication on success that the previous entry was assigned? Adding a tracepoint in rmpupdate() to allow tracking transitions is probably a good idea, otherwise debugging RMP violations and/or unexpected #VC is going to be painful. And/or if the kernel/KVM behavior is to never reassign directly and reading an RMP entry isn't prohibitively expensive, then we could add a sanity check that the RMP is unassigned and reject rmpupdate() if the page is already assigned. Probably not worth it if the overhead is noticeable, but it could be nice to have if things go sideways. > >> +e_unpin: > >> + /* Content of memory is updated, mark pages dirty */ > >> + memset(&e, 0, sizeof(e)); > >> + for (i = 0; i < npages; i++) { > >> + set_page_dirty_lock(inpages[i]); > >> + mark_page_accessed(inpages[i]); > >> + > >> + /* > >> + * If its an error, then update RMP entry to change page ownership > >> + * to the hypervisor. > >> + */ > >> + if (ret) > >> + rmpupdate(inpages[i], &e); > > This feels wrong since it's purging _all_ RMP entries, not just those that were > > successfully modified. And maybe add a RMP "reset" helper, e.g. why is zeroing > > the RMP entry the correct behavior? > > By default all the pages are hypervior owned (i.e zero). If the > LAUNCH_UPDATE was successful then page should have transition from the > hypervisor owned to guest valid. By zero'ing it are reverting it back to > hypevisor owned. > > I agree that I optimize it to clear the modified entries only and leave > everything else as a default. To be clear, it's not just an optimization. Pages that haven't yet been touched may be already owned by a different VM (or even this VM). I.e. "reverting" those pages would actually result in a form of corruption. It's somewhat of a moot point because assigning a single page to multiple guests is going to be fatal anyways, but potentially making a bug worse by introducing even more noise/confusion is not good.