Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4679575pxj; Wed, 12 May 2021 10:43:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwDV7plGyWdpGIy5J3QT8pWNOyPnPDvV3Yf2X1rPSpojZ581c/cXS1Cqs66qp21H5ImTcfi X-Received: by 2002:a17:906:739c:: with SMTP id f28mr38030284ejl.259.1620841384191; Wed, 12 May 2021 10:43:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620841384; cv=none; d=google.com; s=arc-20160816; b=Kwmejoo+6N8bmaxQXTf/XDlCfTmau2uwmFU8KE/1/rWQs8ZiBal1z92to76SietIMV L8refcAyUpT2JP+v1Zz8UK3AqYNNWjZabT9tHXK7nJdmdcicDZ78HWIg4iwWNFLLBz1f FSg01z25xDae4hT6zedd0MzVZ8b1uY94Takn3yvADI51n+m9ulHMDYM2o6KxGiadtjGb APEV+AzZqS/tV0qGSaArSEGj2hEfVtlSJuAD4we1fBO1ny2462wZOMe0ZMDhEBXgthX/ GLBXTUah2qux4cDCFZn/Xn+XAJ9oPlmK7LPd1XkiQUOsVft69+EWX14ELw8YzrdPclaZ OnLA== 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=Ml4FlirfAM0Le7fdpn8CEXufxztA/hSk61DRpU9Z7dU=; b=W07KAokfJY9idque3XOS0R+Zxb0UP/r7X4VMylYtB4MaO+nuVUGUJxTwssyFA+4DXa 1AYpcrJKtjeq5xPzxfD8taD33tvdpwVZ9q8MdHIZoJzcjDzmD3uDONRRqMz4Eg5CCcGH 1fpOE172vek/x3QiTeIX8WJOiNBsRFZQ6oIEY/1UWuly9UNtEv3OirCwzswPaOy1UnxC SXjvrD6D6RfSGxrxh1Vtnz4ystM3tuM/ATmIdf3HIUD+a47q3x1gxXJg/R5wme1k0RAH YNP/ZhzQGCGSnKniZ29AML8omdmeZcj2mhPI2C4g6mXW+afl3F3vhWwG7XcFaMBUZxI5 80+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=h9gcbIMm; 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 f5si8008edx.395.2021.05.12.10.42.40; Wed, 12 May 2021 10:43:04 -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=h9gcbIMm; 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 S1348851AbhELRjR (ORCPT + 99 others); Wed, 12 May 2021 13:39:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236585AbhELQMX (ORCPT ); Wed, 12 May 2021 12:12:23 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66A6DC034610 for ; Wed, 12 May 2021 08:51:15 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id m124so18451684pgm.13 for ; Wed, 12 May 2021 08:51:15 -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=Ml4FlirfAM0Le7fdpn8CEXufxztA/hSk61DRpU9Z7dU=; b=h9gcbIMmQlnhLY9eH9K/D52LdTHz0QSead5AUJGn8prv4jJW8YLJk0yzikEktL2678 cV+V/+Ne2I1obYyvZzwTBXSQrcrPhHBTzjOzCOnWm1nTV0yHdXfUfg/Xt4y9n+0cOvAV cpVCAo1lyNLRqj/yuyta82edBvWKROo4XQHKwzyhy8FtyZ/7eBJb+ccoZSKzX8NgN5r7 FPgqGqqeJVU2s8xiqo/l5GMi8xDMOoHVbkOgKGkl4xwEetf1UfYX9gCs73d250Fi7IKI iZzuO+0I34INxObQA5bVLhxYs+1k69fdvDMa9yVl6EUU19jtwrqXy7zJkKNdKJQ+H1T/ 1KVQ== 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=Ml4FlirfAM0Le7fdpn8CEXufxztA/hSk61DRpU9Z7dU=; b=IL4/fTGYVgxVavF6hsf2c/d9Soyo3z6LmrGAfvufn7K78296oT0ZXZBUBoqOLlHrQ1 icPgAtLIhSZNhFIpdw/FIQ64f/Cw5+eK7DsCv3Qygosby1h1Op6DTtS54WXle3nOLrwj 5IPiYyw9iDNS4n6m7m/LN8bKP31hwLRldtRrJd3wQmmgerqRw1PDJ4xQ7/rpCm8i+ezI GyVMEQImfLnvha+g+bALOV0wHEnbZfQcPXEADb+OngHCO1gvNL1qJ1EJEPWd+f9RC38R dLm6avvd6d0XTzPZ4Iy24BNpAbV/fJoLY4t1KwMWi7x3DnyBSkPR+TuxSsvwWI9oOFir U1/A== X-Gm-Message-State: AOAM530ntoZODyox1jJ3H7nvT0bXZK6eAYA7ixYR2o6LPguNUmXzr2s2 nefy1dryktp+9V/IGSJyOrU/WA== X-Received: by 2002:a65:550e:: with SMTP id f14mr8841530pgr.160.1620834674732; Wed, 12 May 2021 08:51:14 -0700 (PDT) Received: from google.com (240.111.247.35.bc.googleusercontent.com. [35.247.111.240]) by smtp.gmail.com with ESMTPSA id h24sm218439pfn.180.2021.05.12.08.51.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 May 2021 08:51:14 -0700 (PDT) Date: Wed, 12 May 2021 15:51:10 +0000 From: Sean Christopherson To: Borislav Petkov Cc: Ashish Kalra , pbonzini@redhat.com, tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, srutherford@google.com, venu.busireddy@oracle.com, brijesh.singh@amd.com Subject: Re: [PATCH v2 2/4] mm: x86: Invoke hypercall when page encryption status is changed Message-ID: References: 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 Wed, May 12, 2021, Borislav Petkov wrote: > On Fri, Apr 23, 2021 at 03:58:43PM +0000, Ashish Kalra wrote: > > +static inline void notify_page_enc_status_changed(unsigned long pfn, > > + int npages, bool enc) > > +{ > > + PVOP_VCALL3(mmu.notify_page_enc_status_changed, pfn, npages, enc); > > +} > > Now the question is whether something like that is needed for TDX, and, > if so, could it be shared by both. Yes, TDX needs this same hook, but "can't" reuse the hypercall verbatime. Ditto for SEV-SNP. I wanted to squish everything into a single common hypercall, but that didn't pan out. The problem is that both TDX and SNP define their own versions of this so that any guest kernel that complies with the TDX|SNP specification will run cleanly on a hypervisor that also complies with the spec. This KVM-specific hook doesn't meet those requires because non-Linux guest support will be sketchy at best, and non-KVM hypervisor support will be non-existent. The best we can do, short of refusing to support TDX or SNP, is to make this KVM-specific hypercall compatible with TDX and SNP so that the bulk of the control logic is identical. The mechanics of actually invoking the hypercall will differ, but if done right, everything else should be reusable without modification. I had an in-depth analysis of this, but it was all off-list. Pasted below. TDX uses GPRs to communicate with the host, so it can tunnel "legacy" hypercalls from time zero. SNP could technically do the same (with a revised GHCB spec), but it'd be butt ugly. And of course trying to go that route for either TDX or SNP would run into the problem of having to coordinate the ABI for the "legacy" hypercall across all guests and hosts. So yeah, trying to remove any of the three (KVM vs. SNP vs. TDX) interfaces is sadly just wishful thinking. That being said, I do think we can reuse the KVM specific hypercall for TDX and SNP. Both will still need a {TDX,SNP}-specific GCH{I,B} protocol so that cross- vendor compatibility is guaranteed, but that shouldn't preclude a guest that is KVM enlightened from switching to the KVM specific hypercall once it can do so. More thoughts later on. > I guess a common structure could be used along the lines of what is in the > GHCB spec today, but that seems like overkill for SEV/SEV-ES, which will > only ever really do a single page range at a time (through > set_memory_encrypted() and set_memory_decrypted()). The reason for the > expanded form for SEV-SNP is that the OS can (proactively) adjust multiple > page ranges in advance. Will TDX need to do something similar? Yes, TDX needs the exact same thing. All three (SEV, SNP, and TDX) have more or less the exact same hook in the guest (Linux, obviously) kernel. > If so, the only real common piece in KVM is a function to track what pages > are shared vs private, which would only require a simple interface. It's not just KVM, it's also the relevant code in the guest kernel(s) and other hypervisors. And the MMU side of KVM will likely be able to share code, e.g. to act on the page size hint. > So for SEV/SEV-ES, a simpler hypercall interface to specify a single page > range is really all that is needed, but it must be common across > hypervisors. I think that was one Sean's points originally, we don't want > one hypercall for KVM, one for Hyper-V, one for VMware, one for Xen, etc. For the KVM defined interface (required for SEV/SEV-ES), I think it makes sense to make it a superset of the SNP and TDX protocols so that it _can_ be used in lieu of the SNP/TDX specific protocol. I don't know for sure whether or not that will actually yield better code and/or performance, but it costs us almost nothing and at least gives us the option of further optimizing the Linux+KVM combination. It probably shouldn't be a strict superset, as in practice I don't think SNP approach of having individual entries when batching multiple pages will yield the best performance. E.g. the vast majority (maybe all?) of conversions for a Linux guest will be physically contiguous and will have the same preferred page size, at which point there will be less overhead if the guest specifies a massive range as opposed to having to santize and fill a large buffer. TL;DR: I think the KVM hypercall should be something like this, so that it can be used for SNP and TDX, and possibly for other purposes, e.g. for paravirt performance enhancements or something. 8. KVM_HC_MAP_GPA_RANGE ----------------------- :Architecture: x86 :Status: active :Purpose: Request KVM to map a GPA range with the specified attributes. a0: the guest physical address of the start page a1: the number of (4kb) pages (must be contiguous in GPA space) a2: attributes where 'attributes' could be something like: bits 3:0 - preferred page size encoding 0 = 4kb, 1 = 2mb, 2 = 1gb, etc... bit 4 - plaintext = 0, encrypted = 1 bits 63:5 - reserved (must be zero)