Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp973100lqt; Fri, 7 Jun 2024 04:38:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXKDJh+DGbDo/PI7GUThzE38JgVxLCc1GncA0ZLtrAZtRwfW0V/Q1SNZ6/Yay5aelmi16/f6jAdeCHI0TKQy+z47if+gEkks06AsFXm+g== X-Google-Smtp-Source: AGHT+IGyUp9thiPqkz2CEc0MNltqE4OlXg3CJyuQNIJOpcVbO5MmRzHv7wvFipClXSTjKCO3+kYH X-Received: by 2002:a05:6a20:9719:b0:1af:cdd4:9bf3 with SMTP id adf61e73a8af0-1b2f9a606f9mr1987850637.32.1717760312495; Fri, 07 Jun 2024 04:38:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717760312; cv=pass; d=google.com; s=arc-20160816; b=jIACoCMDcEX6bigB8wEi4SHIFEiTI0zY/Cs6jgz2SHuaKWCasI6EzrqCJMebMSCvVC GhyVkqMZG3+TXnPPuB1rvI+qS5kt9UtrigscvjtuqbVw+lO+kEUYrW5ZHvZ88MJosnvz 7Xj9eV4npBlcNbCmVYz1ciFvZEpDbpCwFIHYvnvXHtNlKskTL9mHtbuDC7ANMoNuhLRU cf5yUSRM/dXQ3IUaLQTK6wVTmCghpWKisW+4af3zD+IA+xNvUa0WS4eckSdVmWB6HTTS 3bdTCpUK5yK26GxzfdnGMtEwYCGyKt9AJiZ25s6EQh/1QUPll+dC80lhdT0kJcVzRS3r GoIw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=pzhopGWO5Z3fLTBer4K8pJwod91YQ/MXu5FiCbmJuWw=; fh=OLz0JLIXWsEXiCCd6ey2gA/hywp0JR8abBBbI6Ax24Q=; b=qpS0+HrEuHAjfv8PWB4FNiKEv/QeU2a9lxjLxUeQu2KtwMn2V7CUms2hRqBx6P84Yl +59AGQZmCuMktOqr8IntYQWxcBmz7D0Ed0Oe7DypC+jDcJAQoEMDVbrwWF38GXxtqNxU RhiH6PAaygOgUqam6f/Y1D5oVokQHGo+RGsxrmuk7SS/WGVX1ErnSWuuO59H3A9GRiop v9MFb2WMYAPeCxDEzMBUXXCB2IXcO/v2NhF8qXNcYV2V5ZkWRZEZ+g/3ZdtwzYpjnofy vacwFG/Udp+gawgCT586Fxi5RwqYz3OehufhadMEPLz4Wzfd4HTYERJ/bAL3CO6JVzzS o2tg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ur5VKiuW; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-205933-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205933-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d2e1a72fcca58-7041937a050si195480b3a.388.2024.06.07.04.38.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 04:38:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205933-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ur5VKiuW; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-205933-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205933-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 1A9552886F4 for ; Fri, 7 Jun 2024 11:38:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 98B3818FC95; Fri, 7 Jun 2024 11:38:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Ur5VKiuW" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D47B190661 for ; Fri, 7 Jun 2024 11:38:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717760290; cv=none; b=sPRSEqOWcvvMWJaHEBCsbYcezSOtXgysfYcyNT2Jezvc9F6cSgPag7P4mJs6iZ8fmmJ3NO3LfSmzskE9Dhxhbd9HU/lBniXezNAbNqfUYdRpU0tsrJi29j8K1MmWRzG0i0v669FDRcELsOeT4bHca0x1lnNL1kV2JAIs+jBqeWE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717760290; c=relaxed/simple; bh=ZwGz3dMIKiy1SGVc6DmAoKeJjQKPprY0dxrpiRbneyA=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=ODGCYepXuAHDj3KIcGhjl7FV1gFQ17txG+Wb7I78henh9qx/MjGIPvdxF6Tl1bjVP+z/HtZ3+Lm+b6it406oqYLoF2b1vAPSB/Hsl8o3YDdoY+08npje1TNpYqd8jKRm8Rt1AbAnolGoWAXsVB4AgEnmC27NpGwz2p8z6ypFWEM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Ur5VKiuW; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717760287; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=pzhopGWO5Z3fLTBer4K8pJwod91YQ/MXu5FiCbmJuWw=; b=Ur5VKiuWk8ook8uU3pXXs0+tc8AgjYIttBrQmzTlwlZNsECDJjhU5URThCkToe+tLtFExn tvdTCSqzkBwjRSKRWXBcUqkIPvIktP0+upFH4gLjN0ZhXHMBb4nD2xJuKLI59AjmeZSiJy A/ohFNpsdqz0x/v8IWEETZBU/vFdWIo= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-215-ghujTAsiNbqN-7_4EzWL6w-1; Fri, 07 Jun 2024 07:38:05 -0400 X-MC-Unique: ghujTAsiNbqN-7_4EzWL6w-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-35e0ea8575cso1266482f8f.3 for ; Fri, 07 Jun 2024 04:38:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717760285; x=1718365085; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=pzhopGWO5Z3fLTBer4K8pJwod91YQ/MXu5FiCbmJuWw=; b=vM/F6b+Lw9U4O1XVvIKptU1S1385mN4JGEuHAUOsS+Muzx804iPuzXJ0Myf4fZSxHR YkGjMjM9R72SDksdyvcaShTOOPyVyzULPk2F/wstfx4Gd/vdusITlg5WecfJkA1s5Dje 8KMjZH+ifxC6u/mtwjkl82QKVJu7+wpIzGvKn4gsOEGR6/aFMiU7dlYxZW++0SIA+39G c0oWABexw+yRD6b7lhALr3UvU3UPRRcTINlOw033dthHPY3Bv9f9fprHD0aWFjHF8J7G Sh4iDWJrfpYpXlEAgficpnwevQqsst07toI7GkeDKahIu14b5liQSOwpa8fylQknOJ6V YwJw== X-Forwarded-Encrypted: i=1; AJvYcCWrDd0Yy0IrBoEFS1Qebc/8JCsDkPxe07Qqf+T8v3O2Ag1DSKrXGWG/d6wrgh++O+n/wS4VlOdDBuPQL8sXqwG4xNlY8h5Lh+g+o9G+ X-Gm-Message-State: AOJu0Yydjbw5D6FDBEXstgRkCRl4LsUdev2pFvAdkeWClJQzOUq4N07a TqPbKvEAWfMepGX9qGsvnT5+o6gQNOhsRlxUBNU+Kbi+FQE5IYHXPWGW0d3fBFqP1Zz1UPdUr18 gfQAXuuFyRxDQa5WmqAKMncum3EdH1Hy+nV6IVjP0fyyb7nyhygeHVF9gECdz9enC8zWqyabVlO YbS/bP/tnVeIs6VOtrnM7v6JfZn96K2YcQJq38 X-Received: by 2002:adf:f60d:0:b0:35c:1961:899d with SMTP id ffacd0b85a97d-35efed4333emr1673630f8f.27.1717760284661; Fri, 07 Jun 2024 04:38:04 -0700 (PDT) X-Received: by 2002:adf:f60d:0:b0:35c:1961:899d with SMTP id ffacd0b85a97d-35efed4333emr1673612f8f.27.1717760284299; Fri, 07 Jun 2024 04:38:04 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240530210714.364118-1-rick.p.edgecombe@intel.com> <20240530210714.364118-12-rick.p.edgecombe@intel.com> In-Reply-To: <20240530210714.364118-12-rick.p.edgecombe@intel.com> From: Paolo Bonzini Date: Fri, 7 Jun 2024 13:37:52 +0200 Message-ID: Subject: Re: [PATCH v2 11/15] KVM: x86/tdp_mmu: Reflect tearing down mirror page tables To: Rick Edgecombe Cc: seanjc@google.com, kvm@vger.kernel.org, kai.huang@intel.com, dmatlack@google.com, erdemaktas@google.com, isaku.yamahata@gmail.com, linux-kernel@vger.kernel.org, sagis@google.com, yan.y.zhao@intel.com, Isaku Yamahata Content-Type: text/plain; charset="UTF-8" > --- > arch/x86/include/asm/kvm-x86-ops.h | 2 ++ > arch/x86/include/asm/kvm_host.h | 8 ++++++ > arch/x86/kvm/mmu/tdp_mmu.c | 45 ++++++++++++++++++++++++++++-- > 3 files changed, 53 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h > index 1877d6a77525..dae06afc6038 100644 > --- a/arch/x86/include/asm/kvm-x86-ops.h > +++ b/arch/x86/include/asm/kvm-x86-ops.h > @@ -97,6 +97,8 @@ KVM_X86_OP_OPTIONAL_RET0(get_mt_mask) > KVM_X86_OP(load_mmu_pgd) > KVM_X86_OP_OPTIONAL(reflect_link_spt) > KVM_X86_OP_OPTIONAL(reflect_set_spte) > +KVM_X86_OP_OPTIONAL(reflect_free_spt) > +KVM_X86_OP_OPTIONAL(reflect_remove_spte) > KVM_X86_OP(has_wbinvd_exit) > KVM_X86_OP(get_l2_tsc_offset) > KVM_X86_OP(get_l2_tsc_multiplier) > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index 20bb10f22ca6..0df4a31a0df9 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -1755,6 +1755,14 @@ struct kvm_x86_ops { > int (*reflect_set_spte)(struct kvm *kvm, gfn_t gfn, enum pg_level level, > kvm_pfn_t pfn); > > + /* Update mirrored page tables for page table about to be freed */ > + int (*reflect_free_spt)(struct kvm *kvm, gfn_t gfn, enum pg_level level, > + void *mirrored_spt); > + > + /* Update mirrored page table from spte getting removed, and flush TLB */ > + int (*reflect_remove_spte)(struct kvm *kvm, gfn_t gfn, enum pg_level level, > + kvm_pfn_t pfn); Again, maybe free_external_spt and zap_external_spte? Also, please rename the last argument to pfn_for_gfn. I'm not proud of it, but it took me over 10 minutes to understand if the pfn referred to the gfn itself, or to the external SP that holds the spte... There's a possibility that it isn't just me. :) (In general, this patch took me a _lot_ to review... there were a couple of places that left me incomprehensibly puzzled, more on this below). > bool (*has_wbinvd_exit)(void); > > u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu); > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > index 41b1d3f26597..1245f6a48dbe 100644 > --- a/arch/x86/kvm/mmu/tdp_mmu.c > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > @@ -346,6 +346,29 @@ static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp) > spin_unlock(&kvm->arch.tdp_mmu_pages_lock); > } > > +static void reflect_removed_spte(struct kvm *kvm, gfn_t gfn, > + u64 old_spte, u64 new_spte, > + int level) new_spte is not used and can be dropped. Also, tdp_mmu_zap_external_spte? > +{ > + bool was_present = is_shadow_present_pte(old_spte); > + bool was_leaf = was_present && is_last_spte(old_spte, level); Just put it below: if (!is_shadow_present_pte(old_spte)) return; /* Here we only care about zapping the external leaf PTEs. */ if (!is_last_spte(old_spte, level)) > + kvm_pfn_t old_pfn = spte_to_pfn(old_spte); > + int ret; > + > + /* > + * Allow only leaf page to be zapped. Reclaim non-leaf page tables page This comment left me confused, so I'll try to rephrase and see if I can explain what happens. Correct me if I'm wrong. The only paths to handle_removed_pt() are: - kvm_tdp_mmu_zap_leafs() - kvm_tdp_mmu_zap_invalidated_roots() but because kvm_mmu_zap_all_fast() does not operate on mirror roots, the latter can only happen at VM destruction time. But it's not clear why it's worth mentioning it here, or even why it is special at all. Isn't that just what handle_removed_pt() does at the end? Why does it matter that it's only done at VM destruction time? In other words, it seems to me that this comment is TMI. And if I am wrong (which may well be), the extra information should explain the "why" in more detail, and it should be around the call to reflect_free_spt, not here. > + return; > + /* Zapping leaf spte is allowed only when write lock is held. */ > + lockdep_assert_held_write(&kvm->mmu_lock); > + /* Because write lock is held, operation should success. */ > + ret = static_call(kvm_x86_reflect_remove_spte)(kvm, gfn, level, old_pfn); > + KVM_BUG_ON(ret, kvm); > +} > + > /** > * handle_removed_pt() - handle a page table removed from the TDP structure > * > @@ -441,6 +464,22 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) > } > handle_changed_spte(kvm, kvm_mmu_page_as_id(sp), gfn, > old_spte, REMOVED_SPTE, sp->role, shared); > + if (is_mirror_sp(sp)) { > + KVM_BUG_ON(shared, kvm); > + reflect_removed_spte(kvm, gfn, old_spte, REMOVED_SPTE, level); > + } > + } > + > + if (is_mirror_sp(sp) && > + WARN_ON(static_call(kvm_x86_reflect_free_spt)(kvm, sp->gfn, sp->role.level, > + kvm_mmu_mirrored_spt(sp)))) { Please use base_gfn and level here, instead of fishing them from sp. > + /* > + * Failed to free page table page in mirror page table and > + * there is nothing to do further. > + * Intentionally leak the page to prevent the kernel from > + * accessing the encrypted page. > + */ > + sp->mirrored_spt = NULL; > } > > call_rcu(&sp->rcu_head, tdp_mmu_free_sp_rcu_callback); > @@ -778,9 +817,11 @@ static u64 tdp_mmu_set_spte(struct kvm *kvm, int as_id, tdp_ptep_t sptep, > role.level = level; > handle_changed_spte(kvm, as_id, gfn, old_spte, new_spte, role, false); > > - /* Don't support setting for the non-atomic case */ > - if (is_mirror_sptep(sptep)) > + if (is_mirror_sptep(sptep)) { > + /* Only support zapping for the non-atomic case */ Like for patch 10, this comment should point out why we never get here for mirror SPs. Paolo > KVM_BUG_ON(is_shadow_present_pte(new_spte), kvm); > + reflect_removed_spte(kvm, gfn, old_spte, REMOVED_SPTE, level); > + } > > return old_spte; > } > -- > 2.34.1 >