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 B5C5CCCD1BB for ; Wed, 22 Oct 2025 18:13:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=V5F7t24lC7jgleOK3FW+MwSMdfYKm48DJa8i0z/06to=; b=usvMa0Sj0AO9xdIEW4uQkrNu6U MdCl5HT6hHE/dHvx0kcfJQ/6WmxAVvtm+7EsvvgNcEqJPs84wvZ4elzjd6Sh8ZT7MBSjXc+VQp0R+ qfR4gmLsRBwtxY+4wrRE8b66mbow/tyOU3ehMj6ONFYtt1BXQKbMXbXt/Y/ZfbPQLhW8LRNHEaS8j hjIZJaJ1dpVOM2sRMg5sSG2M4fbtAiiq14a1Aas1OXX0hA/vsan0p8iJci8JFyd9DKCY4sYjvXo4T rAhH4bgIvfUHHF5Y+MevZQRBgGHaUPi3iF4TY8fuTum2i6dVln/frN3roxAF6CxWuIdh0eGcDpKy9 1MHg8HVg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1vBdKS-00000003woQ-2e3i; Wed, 22 Oct 2025 18:12:56 +0000 Received: from mail-pj1-x104a.google.com ([2607:f8b0:4864:20::104a]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1vBdKO-00000003wlJ-3FVQ for linux-arm-kernel@lists.infradead.org; Wed, 22 Oct 2025 18:12:55 +0000 Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-33d75897745so3562760a91.0 for ; Wed, 22 Oct 2025 11:12:51 -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=ZhM3d7BHl1/fEW9XiAtso5YlweiL2HKLJZ5+Rjl9YNb+JeprG+8OLyQ6755gdPMwIN XfmAH5k1HQAJa6Npl9n/LF+Du90xsjWZX9n1lpoTR6LyKODAUC4STtHlzbB4wal4eWwp ebEJz8ETVAlcWrc9BgPtImkhvClb6//Z33cpipti3FT7Dc2/FNPtmsWpeQXndJ0uMz5D lfbhfcnIuBhvlkmsBTJ2x6Q14sjoJxrxnsgH3Cr2LUNHMazqmRBge219TNM73XVLm8au 7oi7Qt8CUNFsAWcPNsxPmPrU3fNI4MW7zmMvJwNbzoBgBINz5PKy4MVxNM4+LBOtPLkQ dJGA== X-Forwarded-Encrypted: i=1; AJvYcCXNmFrHcVpqkOeTl4yNAftBrR3on9JR/xu5IRq/lor+wMYx+SscXxhGYSx2uJ/cx4rMLy/iz4vzkmOfXFkoF42V@lists.infradead.org X-Gm-Message-State: AOJu0Yxoh8qcuSAALyIo+sDsglF7hUBiMARuj5JZSQJokhEG/kqlcCBT gYmcT7qB8zXrOPY1xwp0rFYCjfxHKbXscoWpYPEpSSqvn1e4jGw7aLjH3BC2j1LRfWkp/PKVQHn UwFEJ0g== 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 Content-Type: text/plain; charset="us-ascii" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20251022_111252_816438_3E5C04B8 X-CRM114-Status: GOOD ( 30.66 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=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);