Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp3260402rwr; Sat, 22 Apr 2023 01:41:16 -0700 (PDT) X-Google-Smtp-Source: AKy350ZhYDupXuSd2gHnM+fseCqHEnD/ogwX867fWsCNw2GLYB12jk1cm14akqNoUhtNNLgvmxV5 X-Received: by 2002:a05:6a00:430b:b0:637:434a:75df with SMTP id cb11-20020a056a00430b00b00637434a75dfmr8703752pfb.0.1682152875921; Sat, 22 Apr 2023 01:41:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682152875; cv=none; d=google.com; s=arc-20160816; b=sgRZbc8jxI3lq8Koc5nE9OnfYibMqtnEe/YJcqIxmJHJWir75jfH+1oNU4asUs8n2Z Ha7I1Rw0FHwPPYwZYrVrkPX8HyKNM0PcRcYu87GWxmxy+vhtfEB0zilV2o8GXrJsB54n C2+bNeVw0p7+BLw0g0Ow3Pj11ywQBbi0eyllVDo4uBbvMmFIksBXgwv1VifOLPZh6QM7 NduTfYrHbr0xYbBXclavMkDcAz8BiXlobcFFggbNHIxlj/um8Ci3cHv/6lFDIw4HbI6J pQ9xKD9yLx/OwnjYLeILPO5PZ9k8LbIJWsnu2bOh25HABwi+2ScfYfRmn8TV8I8u6SpU TaCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=+aQN9XLWKAMHX9ca92J9NiyyKoOt1W6QokC8Q6kDPfk=; b=Mz7EBu692pzAdoJZrFxSks3e8w1sAXGOCparKNrqgAWR53ig2wE4gXJ0QR17XvIUZ2 6R+NItYsSWTtb8Ml3uanviNWgv3atanmAp2rjgKOM4mtKkfizZA/xIAiQ7I+NVpv0+vj lmxuE9LMkxTZEk0oV+3pbE/qdlVoFrMJzw0oYYOC8+lFqm8N3gz8xlTaa0bu8mPLHfCj bgMYtaAGtezfRO9wJsP9XdlBQ3lp3/DRUQEhr6tUPLS/YUoS3QngYfJKO2XnCCSRLdCh 2/+7ewOPBlz3MJvTwR3LYJo+AIVM0jTXfjttVySqmbxudFFwzXz2ExIO3kfKzULpSjOq hKrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=P7Gbw1s9; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m13-20020a65564d000000b00518ebc6f91csi6231187pgs.306.2023.04.22.01.41.00; Sat, 22 Apr 2023 01:41:15 -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=@gmail.com header.s=20221208 header.b=P7Gbw1s9; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229663AbjDVISm (ORCPT + 99 others); Sat, 22 Apr 2023 04:18:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229650AbjDVISk (ORCPT ); Sat, 22 Apr 2023 04:18:40 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1B7390; Sat, 22 Apr 2023 01:18:38 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-4edcc712d95so244414e87.1; Sat, 22 Apr 2023 01:18:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682151517; x=1684743517; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=+aQN9XLWKAMHX9ca92J9NiyyKoOt1W6QokC8Q6kDPfk=; b=P7Gbw1s9Wh1EXxMAi6t59x3ZCZ2nHdjVUVp1f41BUTulLfSnzO0/fqATuYv+etsK1s TjHBqVKnqQ7zIhYGofu6X/Xt252jK3Oy4Y3D2ObynuQKzZ0zXduTU5Csb3QNsgBqHudX b6BTxriXFuHmtZGvKa5NTE1YvmZtjAXinlGAJwb1CMQ3j5shcfRHb/C8UIhDn6rgKckz eEolHIdKfLQGLwRjueCqS869JF8W210Sfdjbtwx84GtpVklMR4L06oEuT9a+EfGlo48m sf4WhogmVy8Jui2tq30yfBBS60Cp0T2wV1EQ72NGjSIeq5sW9OliadYTVGsANY2D538t 0oAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682151517; x=1684743517; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+aQN9XLWKAMHX9ca92J9NiyyKoOt1W6QokC8Q6kDPfk=; b=RcdaNN9d/eQipzUySk/ZJSECUbRcY3wX9gJscCr2izL7oPEHD2k8C8GhNVI4awbNv3 HZYAyX1WHHBC0C8bvo0/fZZevyDOynP3MnYhRVTlTQ6v6jSZDCbSzAe70cs0AZ/TN/Z5 S/QOcz1DWchSkSO8Og4u/44B1BUgmDw+17tHBHgAN72NHo5MMteHVAh0OpFP3NpACYuk qn/ffLPcm+iEr5Cw+y67Sp0sotZeS5sKiGyIK0K72w0hpRAzwf3cX/0b5Qw9BleDxt1x kK6dF+7ujw/uWu2FQ4JuEouVDH0sBazck8h4rCLnPUOjwE53U17oDf+/uHsBbdHnh4yM A7gQ== X-Gm-Message-State: AAQBX9e7OBFBFuJeQ6gKjQtEWfH6XtSE4+ZYQehR7QzGfQU0jTVX9YK2 Y4TXa6ht4TyOc+dtCNvUATyN9C7fSlNLjA== X-Received: by 2002:a2e:9804:0:b0:2a9:f6f0:fc8c with SMTP id a4-20020a2e9804000000b002a9f6f0fc8cmr1660730ljj.3.1682151516726; Sat, 22 Apr 2023 01:18:36 -0700 (PDT) Received: from localhost (88-113-32-99.elisa-laajakaista.fi. [88.113.32.99]) by smtp.gmail.com with ESMTPSA id m15-20020a056512014f00b004e7fa99f2b5sm818818lfo.186.2023.04.22.01.18.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Apr 2023 01:18:36 -0700 (PDT) Date: Sat, 22 Apr 2023 11:18:34 +0300 From: Zhi Wang To: isaku.yamahata@intel.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack , Kai Huang , Sean Christopherson , Michael Roth Subject: Re: [PATCH v13 048/113] KVM: x86/mmu: Disallow dirty logging for x86 TDX Message-ID: <20230422111834.00003689@gmail.com> In-Reply-To: <23cc6df6ec2ca776c3efe148e70ba290aa3de319.1678643052.git.isaku.yamahata@intel.com> References: <23cc6df6ec2ca776c3efe148e70ba290aa3de319.1678643052.git.isaku.yamahata@intel.com> X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Sun, 12 Mar 2023 10:56:12 -0700 isaku.yamahata@intel.com wrote: > From: Isaku Yamahata > > TDX doesn't support dirty logging. Report dirty logging isn't supported so > that device model, for example qemu, can properly handle it. Silently > ignore on dirty logging on private GFNs of TDX. > > Signed-off-by: Sean Christopherson > Signed-off-by: Isaku Yamahata > --- > arch/x86/kvm/mmu/mmu.c | 3 +++ > arch/x86/kvm/mmu/tdp_mmu.c | 36 +++++++++++++++++++++++++++++++++++- > arch/x86/kvm/x86.c | 8 ++++++++ > include/linux/kvm_host.h | 1 + > virt/kvm/kvm_main.c | 10 +++++++++- > 5 files changed, 56 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index 3d68da838f94..1f250fa8ce36 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -6706,6 +6706,9 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, > for_each_rmap_spte(rmap_head, &iter, sptep) { > sp = sptep_to_sp(sptep); > > + /* Private page dirty logging is not supported yet. */ > + KVM_BUG_ON(is_private_sptep(sptep), kvm); > + > /* > * We cannot do huge page mapping for indirect shadow pages, > * which are found on the last rmap (level = 1) when not using > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > index a3402b33fa5d..58a236a69ec7 100644 > --- a/arch/x86/kvm/mmu/tdp_mmu.c > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > @@ -1474,9 +1474,27 @@ static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm, > * into this helper allow blocking; it'd be dead, wasteful code. > */ > for_each_tdp_mmu_root(kvm, root, range->slot->as_id) { > + gfn_t start; > + gfn_t end; > + > + /* > + * For now, operation on private GPA, e.g. dirty page logging, > + * isn't supported yet. > + */ > + if (is_private_sp(root)) > + continue; > + > rcu_read_lock(); > > - tdp_root_for_each_leaf_pte(iter, root, range->start, range->end) > + /* > + * For TDX shared mapping, set GFN shared bit to the range, > + * so the handler() doesn't need to set it, to avoid duplicated > + * code in multiple handler()s. > + */ > + start = kvm_gfn_to_shared(kvm, range->start); > + end = kvm_gfn_to_shared(kvm, range->end); > + > + tdp_root_for_each_leaf_pte(iter, root, start, end) > ret |= handler(kvm, &iter, range); > > rcu_read_unlock(); > @@ -1959,6 +1977,13 @@ void kvm_tdp_mmu_clear_dirty_pt_masked(struct kvm *kvm, > struct kvm_mmu_page *root; > > lockdep_assert_held_write(&kvm->mmu_lock); > + /* > + * First TDX generation doesn't support clearing dirty bit, > + * since there's no secure EPT API to support it. For now silently > + * ignore KVM_CLEAR_DIRTY_LOG. > + */ > + if (!kvm_arch_dirty_log_supported(kvm)) > + return; > for_each_tdp_mmu_root(kvm, root, slot->as_id) > clear_dirty_pt_masked(kvm, root, gfn, mask, wrprot); > } > @@ -2078,6 +2103,15 @@ bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm, > bool spte_set = false; > > lockdep_assert_held_write(&kvm->mmu_lock); > + > + /* > + * First TDX generation doesn't support write protecting private > + * mappings, silently ignore the request. KVM_GET_DIRTY_LOG etc > + * can reach here, no warning. > + */ > + if (!kvm_arch_dirty_log_supported(kvm)) > + return false; > + > for_each_tdp_mmu_root(kvm, root, slot->as_id) > spte_set |= write_protect_gfn(kvm, root, gfn, min_level); > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index a0960b468c74..6d7ca694e1c9 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -12589,6 +12589,9 @@ static void kvm_mmu_slot_apply_flags(struct kvm *kvm, > u32 new_flags = new ? new->flags : 0; > bool log_dirty_pages = new_flags & KVM_MEM_LOG_DIRTY_PAGES; > > + if (!kvm_arch_dirty_log_supported(kvm) && log_dirty_pages) > + return; > + > /* > * Update CPU dirty logging if dirty logging is being toggled. This > * applies to all operations. > @@ -13561,6 +13564,11 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, > } > EXPORT_SYMBOL_GPL(kvm_sev_es_string_io); > > +bool kvm_arch_dirty_log_supported(struct kvm *kvm) > +{ > + return kvm->arch.vm_type != KVM_X86_PROTECTED_VM; > +} > + Maybe introduce a new x86 ops for SNP/TDX to check this separately as SNP might still support it? With the current approach, I think both SNP/TDX will be affected. So does the later patch about page-tracking. Michael, can you confirm this? > EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_entry); > EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); > EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio); > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > index 5a144497c930..3cd537f4b38b 100644 > --- a/include/linux/kvm_host.h > +++ b/include/linux/kvm_host.h > @@ -1495,6 +1495,7 @@ bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu); > int kvm_arch_post_init_vm(struct kvm *kvm); > void kvm_arch_pre_destroy_vm(struct kvm *kvm); > int kvm_arch_create_vm_debugfs(struct kvm *kvm); > +bool kvm_arch_dirty_log_supported(struct kvm *kvm); > > #ifndef __KVM_HAVE_ARCH_VM_ALLOC > /* > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 42f01d0d6a49..e9f8225f3406 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -1700,10 +1700,18 @@ static void kvm_replace_memslot(struct kvm *kvm, > } > } > > +bool __weak kvm_arch_dirty_log_supported(struct kvm *kvm) > +{ > + return true; > +} > + > static int check_memory_region_flags(struct kvm *kvm, > const struct kvm_userspace_memory_region2 *mem) > { > - u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; > + u32 valid_flags = 0; > + > + if (kvm_arch_dirty_log_supported(kvm)) > + valid_flags |= KVM_MEM_LOG_DIRTY_PAGES; > > if (kvm_arch_has_private_mem(kvm)) > valid_flags |= KVM_MEM_PRIVATE;