Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp3137817pxy; Mon, 3 May 2021 16:24:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxENhDmfTMgMEIS13O8qSv0RM+NzMs0MXQ1oDS7snycKfRuCMFGM99bPIwbUiui4soVxHsv X-Received: by 2002:a62:170e:0:b029:1fa:7161:fd71 with SMTP id 14-20020a62170e0000b02901fa7161fd71mr20759660pfx.35.1620084253002; Mon, 03 May 2021 16:24:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620084252; cv=none; d=google.com; s=arc-20160816; b=xFT7krIXEBNXG+D0ZIEaivqJHmJqJm/p3WYnt9K48Wg/pf1Wp1PNB45q+n2ruRm7N0 cDyspDs/JecBzC4lpCHO28E256qLEE4unJZ5WOt4o9H3Mh4eZiZtS3wQSNSBF9JHhWQN tM1dm5bzW8qHSOyECR7pqpaWK0rzTykHyMqCcEW6PxXhbSxsKxL+XSEbOv23A2LDS53Y TF/B8rWbKn+ow4cRdz9o6PsG3TCMIG+6cjnK8mEbiD6ph72W1wh+WE9XwEJOJjnLFAR/ z6BQPX7VYT7EnkH1DEoVUMS7JyXV6bmwlfVHk90E93hDn/HOmG0RSRVRSIWAOmokduSA UF9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=sATyLz2AUjh/NEl61N+58SEsQ85RImR+kWn9Q6lYFwQ=; b=PL77XCi/aOS6P9cwMiZke/Bh1buA4fGm1Vp4B7lqjR5FNUk2ZWdPYO6YFyfCEi1RDd hew0cVDIkocTWgHWjIR/X5Mj2zxR2yLfXGnLwvQ6ctKaKx6T6AqxUOc7NTNobsUl/JVb VOnav5tZip6YGrkhoHK2ABWWUnlqzPaAA5lpwnNNZfsnRF6b2uDwXP/DwSgzvrzB8RXP np172EJ6vAJysiHiWHJjjtxsWDZQUxOSxgmm8iVLNkl1ctFkD7oIn2EVWb42TIMG1Ht8 Xoo4nY9Aqi5AGTurAgcPJQuMwvggADseINB4/T1BJYsKwiLDEvR7i+3ZWZbU8j5M42sH ZGvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=vProBvIT; 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 q64si517406pgq.401.2021.05.03.16.23.59; Mon, 03 May 2021 16:24:12 -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=vProBvIT; 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 S229951AbhECXYH (ORCPT + 99 others); Mon, 3 May 2021 19:24:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229950AbhECXYG (ORCPT ); Mon, 3 May 2021 19:24:06 -0400 Received: from mail-il1-x12b.google.com (mail-il1-x12b.google.com [IPv6:2607:f8b0:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69F13C061574 for ; Mon, 3 May 2021 16:23:12 -0700 (PDT) Received: by mail-il1-x12b.google.com with SMTP id a9so4928284ilh.9 for ; Mon, 03 May 2021 16:23:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=sATyLz2AUjh/NEl61N+58SEsQ85RImR+kWn9Q6lYFwQ=; b=vProBvITgZKqrAHCml0iNoYIiE62Yp13NhB/JmOG04iqdKAyZA2xYLUWfpaj51UHV0 WB88Be5RuzNvmMGjUMsQMU1sqGHXcrdsfnMPemghFFr+uJ6wznro8yedBnVtJ1oEg3QO wG9vjPgOljuLbfhCaTe/h63BbHHHTrjxe+fpSOWY84RrUeQVZnHCee4ffSwA+Gccj56o PBDd36Lglqn+rYHRiM7uHjdhf2tLr40hkDL1kE2JYfCh6EnrYkqGJ59fWJDk3iq+sqsy Vmn9W37qmvh0R2OEfAOHpVqtUMrFVF5ep5ICBJ26wJ5TYOnkB1huiHbGFwpOyE6c+1Xr oVoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=sATyLz2AUjh/NEl61N+58SEsQ85RImR+kWn9Q6lYFwQ=; b=D9ytSlCwLv7S31Gfn05bab2oVsd/6fFSqnfivEwQnM/PSvce4HdCAMyd9ZQJEllETZ vOw6kYA4QbPO/glZmS3+314QXavQ9umnOB+MHMKiQa3sJ9g9D5GIMCTIJnRZAo2EFTQS ohOMegkl1ixEpWZngcF9/E38mu92n2wUa11xjTCsydHKt6BzVLNQzDBSSJ8U8rUnq+p6 7n7FMYKxu3ZbDUME8NIbXA9Ujms+8c42cfXdVDv7pxdZ6QNCh5u9sjW4530Nm+XfSd91 6Q7bm502caz+ainqTY0Mx9F3Z3wCZcdE/As17WB5Jm0pTkLTHHtApizvSi2bkBahmeol Sbkg== X-Gm-Message-State: AOAM531bykbw3/WJ6bFDei8pc3eYiwP2YxQwfQA42kJH8THn49A8TjuC Gs7vXmnOm48mARtPfN78JeSAHdRsKPhR+LuNojkkhQ== X-Received: by 2002:a05:6e02:969:: with SMTP id q9mr7460898ilt.285.1620084191701; Mon, 03 May 2021 16:23:11 -0700 (PDT) MIME-Version: 1.0 References: <20210429104707.203055-1-pbonzini@redhat.com> <20210429104707.203055-3-pbonzini@redhat.com> In-Reply-To: From: Steve Rutherford Date: Mon, 3 May 2021 16:22:35 -0700 Message-ID: Subject: Re: [PATCH v3 2/2] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall To: Paolo Bonzini Cc: Sean Christopherson , LKML , KVM list , Joerg Roedel , Brijesh Singh , Tom Lendacky , Ashish Kalra , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Borislav Petkov , X86 ML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, May 1, 2021 at 2:01 AM Paolo Bonzini wrote: > > On 30/04/21 22:10, Sean Christopherson wrote: > > On Thu, Apr 29, 2021, Paolo Bonzini wrote: > >> diff --git a/Documentation/virt/kvm/msr.rst b/Documentation/virt/kvm/msr.rst > >> index 57fc4090031a..cf1b0b2099b0 100644 > >> --- a/Documentation/virt/kvm/msr.rst > >> +++ b/Documentation/virt/kvm/msr.rst > >> @@ -383,5 +383,10 @@ MSR_KVM_MIGRATION_CONTROL: > >> data: > >> This MSR is available if KVM_FEATURE_MIGRATION_CONTROL is present in > >> CPUID. Bit 0 represents whether live migration of the guest is allowed. > >> + > >> When a guest is started, bit 0 will be 1 if the guest has encrypted > >> - memory and 0 if the guest does not have encrypted memory. > >> + memory and 0 if the guest does not have encrypted memory. If the > >> + guest is communicating page encryption status to the host using the > >> + ``KVM_HC_PAGE_ENC_STATUS`` hypercall, it can set bit 0 in this MSR to > >> + allow live migration of the guest. The MSR is read-only if > >> + ``KVM_FEATURE_HC_PAGE_STATUS`` is not advertised to the guest. > > > > I still don't get the desire to tie MSR_KVM_MIGRATION_CONTROL to PAGE_ENC_STATUS > > in any way shape or form. I can understand making it read-only or dropping > > writes if it's not intercepted by userspace, but making it read-only for > > non-encrypted guests makes it useful only for encrypted guests, which defeats > > the purpose of genericizing the MSR. > > Yeah, I see your point. On the other hand by making it unconditionally > writable we must implement the writability in KVM, because a read-only > implementation would not comply with the spec. > > >> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > >> index e9c40be9235c..0c2524bbaa84 100644 > >> --- a/arch/x86/kvm/x86.c > >> +++ b/arch/x86/kvm/x86.c > >> @@ -3279,6 +3279,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) > >> if (!guest_pv_has(vcpu, KVM_FEATURE_MIGRATION_CONTROL)) > >> return 1; > >> > >> + /* > >> + * This implementation is only good if userspace has *not* > >> + * enabled KVM_FEATURE_HC_PAGE_ENC_STATUS. If userspace > >> + * enables KVM_FEATURE_HC_PAGE_ENC_STATUS it must set up an > >> + * MSR filter in order to accept writes that change bit 0. > >> + */ > >> if (data != !static_call(kvm_x86_has_encrypted_memory)(vcpu->kvm)) > >> return 1; > > > > This behavior doesn't match the documentation. > > > > a. The MSR is not read-only for legacy guests since they can write '0'. > > b. The MSR is not read-only if KVM_FEATURE_HC_PAGE_STATUS isn't advertised, > > a guest with encrypted memory can write '1' regardless of whether userspace > > has enabled KVM_FEATURE_HC_PAGE_STATUS. > > Right, I should have said "not changeable" rather than "read-only". > > > c. The MSR is never fully writable, e.g. a guest with encrypted memory can set > > bit 0, but not clear it. This doesn't seem intentional? > > It is intentional, clearing it would mean preserving the value in the > kernel so that userspace can read it. > > So... I don't know, all in all having both the separate CPUID and the > userspace implementation reeks of overengineering. It should be either > of these: > > - separate CPUID bit, MSR unconditionally writable and implemented in > KVM. Userspace is expected to ignore the MSR value for encrypted guests > unless KVM_FEATURE_HC_PAGE_STATUS is exposed. Userspace should respect > it even for unencrypted guests (not a migration-DoS vector, because > userspace can just not expose the feature). > > - make it completely independent from migration, i.e. it's just a facet > of MSR_KVM_PAGE_ENC_STATUS saying whether the bitmap is up-to-date. It > would use CPUID bit as the encryption status bitmap and have no code at > all in KVM (userspace needs to set up the filter and implement everything). As far as I know, because of MSR filtering, the only "code" that needs to be in KVM for MSR handling is a #define reserving the PV feature number and a #define for the MSR number. Arguably, you don't even need to add the new PV bits to the supported cpuid, since MSR filtering is really what determines if kernel support is present. > > At this point I very much prefer the latter, which is basically Ashish's > earlier patch. The minor distinction would be that if you expose the cpuid bit to the guest you plan on intercepting the MSR with filters, and would not need any handler code in the kernel. Steve > > Paolo