From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9B3EBCCD1AB for ; Wed, 22 Oct 2025 18:12:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=ABYw8u8vmB0c0jT+XkhUekGQMN61LWMY8pEVov9/l6c=; b=NQVMlvwd7neZMFlYc5lUgCH/t7 IIG93cVmwT9GuU8XRA3Czazp3sZvn8lXCjHH3m25HjobwBIwhTT29Sab4VMTq3iXMiZb/tL9G0WVl mQCcKZfakwcrVeIR4zLyA3C4eYXzRIl4/M+HrXpQ0mldICF9QUqBhxGk9RkC43Vi1X9xlG/kYnyL9 3g7jULA3aYGrMK4UkAQ/0H0FdsO631Os5Abyu6E4RNGe0trIWcxg9FkcSMjPASm0pehwBVvpsmZ0Q lOsEEKLTNLppkyrK3xdktkThBkORxwofZ7AMjKbceq4M9xNcLYloM449xTOOw3fFKqzWSG2jv7glB eVaPUzRQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1vBdKS-00000003woh-3ryD; Wed, 22 Oct 2025 18:12:56 +0000 Received: from mail-pj1-x1049.google.com ([2607:f8b0:4864:20::1049]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1vBdKO-00000003wlY-2Wjy for kvm-riscv@lists.infradead.org; Wed, 22 Oct 2025 18:12:55 +0000 Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-32eb864fe90so2978512a91.3 for ; Wed, 22 Oct 2025 11:12:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1761156771; x=1761761571; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=V5F7t24lC7jgleOK3FW+MwSMdfYKm48DJa8i0z/06to=; b=IkiaQomjJ1GcTS+i657wYk0ZSodmFptRaZVhySLbsFlmWjpRsavTl/BuLa32a6BneO EH+d/5jAZfMv0UVPBblxvrEZDM0z+Pxw0f8lod5i1ewR2FDdb8NTR5GWApuIS/uCLf6t rcxTo9x44n31fLsRbwCdq87L+mmAxeLjr799V396H2o/KhIAuLPRsKwHMy41s7SlazBK E2leXM45y4+IUPzB2pyLjX1p1hpH13iFBUGCDojhOL8HhhfhZoRuZJyjpduZquE56rUW GX+nEmKnF6Yy8NrAPdqzKoDMMLNLemwubxuyvvn0Jsdxle31R1Qfx6wjPLSl1PgJKsw0 hBuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761156771; x=1761761571; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=V5F7t24lC7jgleOK3FW+MwSMdfYKm48DJa8i0z/06to=; b=T4SbzDqrGHITP2ogtotjDMlukr5a9Z9yM7LguG6Zfc034ZPOk69zRuiGy0g4GX4jIk O/GbKaz9CDP2eqUCLHWc3MlLv6bJ6zpVpTvXA3zOkxj97snW4qWOzpI7KTrMJzfpCKXx vkqpHxOvqeUWSQqEIjXQvNJMy3Gjmy4nMVkYvpbXPENgua97nLywv/EeLx7LUXQyJK0M z3UefvOvjmvtP8Bgqw06O7HOK0CnejfAoAecgID8BjavXmPn6B6kjeJH4AtuVgEoCjDP FiMLBkEAx6Nw5UqFQzr45SFH8ZO8ig9q6p/ujRws6T8aJwCyZo8FzCdqLXkSwd9udhNd Ll2g== X-Forwarded-Encrypted: i=1; AJvYcCUhgl3AADeODMvXXn+xnYtK0OHSSajVZrhmSSeTTc9GfpYxYQF4vRKiHcyVANUvyoOnm4JCHzCBJXw=@lists.infradead.org X-Gm-Message-State: AOJu0YxZziHHPTGXT/i7VxQ6+WJKXlynXxBAcqFIyQGK1Tm5UusWfRWU fEmtr9w3/mlYj2Pv+H2J2Bo2fOFm5AWkz0SU37ZLWEgHZmnNiVyeZpYjt2IjyT531nAKF15OsyE 6EuIjAQ== X-Google-Smtp-Source: AGHT+IFAZr0wpbGx7b8wp6kFow05WwkzX+gl9q/tJGKoWGxIdF2WxHsL5OqcbuH+85tLuHrz8145UQS6wJM= X-Received: from pjbnm19.prod.google.com ([2002:a17:90b:19d3:b0:33d:cdb9:9adf]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3f8d:b0:33b:be31:8193 with SMTP id 98e67ed59e1d1-33bcf85d59dmr30647063a91.6.1761156768896; Wed, 22 Oct 2025 11:12:48 -0700 (PDT) Date: Wed, 22 Oct 2025 11:12:47 -0700 In-Reply-To: Mime-Version: 1.0 References: <20251017003244.186495-1-seanjc@google.com> <20251017003244.186495-5-seanjc@google.com> Message-ID: Subject: Re: [PATCH v3 04/25] KVM: x86/mmu: Add dedicated API to map guest_memfd pfn into TDP MMU From: Sean Christopherson To: Yan Zhao Cc: Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Madhavan Srinivasan , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Paolo Bonzini , "Kirill A. Shutemov" , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, x86@kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Ira Weiny , Kai Huang , Michael Roth , Vishal Annapurve , Rick Edgecombe , Ackerley Tng , Binbin Wu X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20251022_111252_643209_8C38F51F X-CRM114-Status: GOOD ( 29.03 ) X-BeenThere: kvm-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "kvm-riscv" Errors-To: kvm-riscv-bounces+kvm-riscv=archiver.kernel.org@lists.infradead.org On Wed, Oct 22, 2025, Yan Zhao wrote: > On Tue, Oct 21, 2025 at 09:36:52AM -0700, Sean Christopherson wrote: > > On Tue, Oct 21, 2025, Yan Zhao wrote: > > > On Thu, Oct 16, 2025 at 05:32:22PM -0700, Sean Christopherson wrote: > > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > > > index 18d69d48bc55..ba5cca825a7f 100644 > > > > --- a/arch/x86/kvm/mmu/mmu.c > > > > +++ b/arch/x86/kvm/mmu/mmu.c > > > > @@ -5014,6 +5014,65 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, > > > > return min(range->size, end - range->gpa); > > > > } > > > > > > > > +int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn) > > > > +{ > > > > + struct kvm_page_fault fault = { > > > > + .addr = gfn_to_gpa(gfn), > > > > + .error_code = PFERR_GUEST_FINAL_MASK | PFERR_PRIVATE_ACCESS, > > > > + .prefetch = true, > > > > + .is_tdp = true, > > > > + .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(vcpu->kvm), > > > > + > > > > + .max_level = PG_LEVEL_4K, > > > > + .req_level = PG_LEVEL_4K, > > > > + .goal_level = PG_LEVEL_4K, > > > > + .is_private = true, > > > > + > > > > + .gfn = gfn, > > > > + .slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn), > > > > + .pfn = pfn, > > > > + .map_writable = true, > > > > + }; > > > > + struct kvm *kvm = vcpu->kvm; > > > > + int r; > > > > + > > > > + lockdep_assert_held(&kvm->slots_lock); > > > Do we need to assert that filemap_invalidate_lock() is held as well? > > > > Hrm, a lockdep assertion would be nice to have, but it's obviously not strictly > > necessary, and I'm not sure it's worth the cost. To safely assert, KVM would need > Not sure. Maybe just add a comment? > But even with kvm_assert_gmem_invalidate_lock_held() and > lockdep_assert_held(&kvm->slots_lock), it seems that > kvm_tdp_mmu_map_private_pfn() still can't guarantee that the pfn is not stale. At some point we have to assume correctness. E.g. one could also argue that holding every locking in the universe still doesn't ensure the pfn is fresh, because theoretically guest_memfd could violate the locking scheme. Aha! And to further harden and document this code, this API can be gated on CONFIG_KVM_GUEST_MEMFD=y, as pointed out by the amazing-as-always test bot: https://lore.kernel.org/all/202510221928.ikBXHGCf-lkp@intel.com We could go a step further and gate it on CONFIG_KVM_INTEL_TDX=y, but I don't like that idea as I think it'd would be a net negative in terms of documenation, compared to checking CONFIG_KVM_GUEST_MEMFD. And in general I don't want to set a precedent of ifdef-ing common x86 based on what vendor code _currently_ needs an API. > e.g., if hypothetically those locks were released and re-acquired after getting > the pfn. > > > to first assert that the file refcount is elevated, e.g. to guard against > > guest_memfd _really_ screwing up and not grabbing a reference to the underlying > > file. > > > > E.g. it'd have to be something like this: > > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > index 94d7f32a03b6..5d46b2ac0292 100644 > > --- a/arch/x86/kvm/mmu/mmu.c > > +++ b/arch/x86/kvm/mmu/mmu.c > > @@ -5014,6 +5014,18 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, > > return min(range->size, end - range->gpa); > > } > > > > +static void kvm_assert_gmem_invalidate_lock_held(struct kvm_memory_slot *slot) > > +{ > > +#ifdef CONFIG_PROVE_LOCKING > > + if (WARN_ON_ONCE(!kvm_slot_has_gmem(slot)) || > > + WARN_ON_ONCE(!slot->gmem.file) || > > + WARN_ON_ONCE(!file_count(slot->gmem.file))) > > + return; > > + > > + lockdep_assert_held(file_inode(&slot->gmem.file)->i_mapping->invalidate_lock)); > lockdep_assert_held(&file_inode(slot->gmem.file)->i_mapping->invalidate_lock); > > +#endif > > +} > > + > > int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn) > > { > > struct kvm_page_fault fault = { > > @@ -5038,6 +5050,8 @@ int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn) > > > > lockdep_assert_held(&kvm->slots_lock); > > > > + kvm_assert_gmem_invalidate_lock_held(fault.slot); > > + > > if (KVM_BUG_ON(!tdp_mmu_enabled, kvm)) > > return -EIO; > > -- > > > > Which I suppose isn't that terrible? > Is it good if we test is_page_fault_stale()? e.g., No, because it can only get false positives, e.g. if an mmu_notifier invalidation on shared, non-guest_memfd memory. Though a sanity check would be nice to have; I believe we can simply do: diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index c5734ca5c17d..440fd8f80397 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -1273,6 +1273,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) struct kvm_mmu_page *sp; int ret = RET_PF_RETRY; + KVM_MMU_WARN_ON(!root || root->role.invalid); + kvm_mmu_hugepage_adjust(vcpu, fault); trace_kvm_mmu_spte_requested(fault); -- kvm-riscv mailing list kvm-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/kvm-riscv