From nobody Mon Feb 9 05:52:16 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AC7C33122E for ; Thu, 29 Jan 2026 01:15:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769649345; cv=none; b=JD+9tUjWxgFjN8yxWcinzblMWWjJXr9VRuZJAPd4ReObaUTjZOLUzJVP+kuw9kZCRbDG3JS6bBMCsEdp1Os2WZd3FpxChy6D/p9n0GrZO68qYm0BWWbed4K8YdUCmv4wa8io/TuO7a/mXx8yF7JJLGqlP+LkKPEHMzfDNiVzUeM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769649345; c=relaxed/simple; bh=kkghoN8ozlhuTfHP923gQ1EuVfkrW8+BxHy4JMV4lfQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gRUuZcZsVdBDw7l+VqpRqyItSWLhBJc5asa6sC/NVKUXpTrWAWz2njUtqgwTvKIskl5dR6PlEMZ97QLEbBRcLxOm8hJ8XAw5tfaf86uNN6wbM8hpFcRjXD48zWmNA9RzUsfcG9z9FVd1MG+GFzizqm1xZhMZbc1ZQoXlnnOZmsA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nDjEaaSE; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nDjEaaSE" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c6124a9fb86so902298a12.3 for ; Wed, 28 Jan 2026 17:15:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769649342; x=1770254142; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=SAUKDZ4/BIicH8X7DG5XUGdjBDs3Mzg+V7hlpmye//I=; b=nDjEaaSELq7tsG+ZZmKg2Ii3ysgvGBmoIxvjEKOc32tQwuzJvNBjFhXgZMrx+p1hHJ rF1n01/Z2iOp5Jpl1hP2ZRXC4C2BuHVTD+bNGbyfzP0nz6Gx73APfGsKmE6eydovc4ok klmP99k5WzoIM1fHglVMXV8kpADvRSnJKrEWoZjaKND/m4UhQj7faJ5q45wQjdgyVJ5M RCe5mMVLmUNCpbmMQVKQBn5FGGhrAwUC+xB2//QWSQpfHgpplacvag7C4MzT7M+dSHCH RUveKjkGnGlIfpNPt36Tq2dTZp5zHK0c6cR6bfihRgBg56y7lMoreW13aVj1In4ii6os jwqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769649342; x=1770254142; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=SAUKDZ4/BIicH8X7DG5XUGdjBDs3Mzg+V7hlpmye//I=; b=cRBqcyx+eeHGBKwDfR1ZJB6fiF/4tQqqkzcDOjFImAFTZQ3fHvG6jDAb04WQRzBcXB KEqC9wFdwfNL0zxUoxWvvWR0u/AEY1rYnY1qo5oVegxwmHbkklDXvfM8tahV6YrwTsXe wHjzEY+Erx7akl9y7z2g+jbCFU9LZZtZQRj2eSUrtyhuKTvZ3VhENJPlc5vPb7zs2MR1 l33tagYHRTSHuECMIjnoggHuWqbrqHTV/hgFNrul428+DrW+olNhl8iMX5HgMaYlLubA jvkvlqK0DgbaT9D4IEeJnqXxtZ7VPal+34zcmu21g4yK0J9cVNaQeV398X7QWe0Wac+X 5TTQ== X-Gm-Message-State: AOJu0YxxfiWsKEs2zY7WfFxhvSy5RLSW9XCur3bZKkLxPxP0vIhDR3Ga iTkRqyYFz6YirSX9IMCuINjgXClNTSMsKpKxgnlEeIp0Sv5rFFtVdXkXPCeXYoEPnhmLtJlIazr t74Mplg== X-Received: from plbkf12.prod.google.com ([2002:a17:903:5cc:b0:2a0:fb2a:79f0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:a8e:b0:38b:d9b5:5de2 with SMTP id adf61e73a8af0-38ec63c6233mr5358315637.50.1769649342527; Wed, 28 Jan 2026 17:15:42 -0800 (PST) Reply-To: Sean Christopherson Date: Wed, 28 Jan 2026 17:14:40 -0800 In-Reply-To: <20260129011517.3545883-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129011517.3545883-1-seanjc@google.com> X-Mailer: git-send-email 2.53.0.rc1.217.geba53bf80e-goog Message-ID: <20260129011517.3545883-9-seanjc@google.com> Subject: [RFC PATCH v5 08/45] KVM: x86/mmu: Propagate mirror SPTE removal to S-EPT in handle_changed_spte() From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Kiryl Shutsemau , Sean Christopherson , Paolo Bonzini Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org, Kai Huang , Rick Edgecombe , Yan Zhao , Vishal Annapurve , Ackerley Tng , Sagi Shahar , Binbin Wu , Xiaoyao Li , Isaku Yamahata Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Invoke .remove_external_spte() in handle_changed_spte() as appropriate instead of relying on callers to do the right thing. Relying on callers to invoke .remove_external_spte() is confusing and brittle, e.g. subtly relies tdp_mmu_set_spte_atomic() never removing SPTEs, and removing an S-EPT entry in tdp_mmu_set_spte() is bizarre (yeah, the VM is bugged so it doesn't matter in practice, but it's still weird). Implementing rules-based logic in a common chokepoint will also make it easier to reason about the correctness of splitting hugepages when support for S-EPT hugepages comes along. Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/tdp_mmu.c | 43 +++++++++++++------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 8743cd020d12..27ac520f2a89 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -359,25 +359,6 @@ static void tdp_mmu_unlink_sp(struct kvm *kvm, struct = kvm_mmu_page *sp) spin_unlock(&kvm->arch.tdp_mmu_pages_lock); } =20 -static void remove_external_spte(struct kvm *kvm, gfn_t gfn, u64 old_spte, - int level) -{ - /* - * External (TDX) SPTEs are limited to PG_LEVEL_4K, and external - * PTs are removed in a special order, involving free_external_spt(). - * But remove_external_spte() will be called on non-leaf PTEs via - * __tdp_mmu_zap_root(), so avoid the error the former would return - * in this case. - */ - if (!is_last_spte(old_spte, level)) - return; - - /* Zapping leaf spte is allowed only when write lock is held. */ - lockdep_assert_held_write(&kvm->mmu_lock); - - kvm_x86_call(remove_external_spte)(kvm, gfn, level, old_spte); -} - /** * handle_removed_pt() - handle a page table removed from the TDP structure * @@ -473,11 +454,6 @@ static void handle_removed_pt(struct kvm *kvm, tdp_pte= p_t pt, bool shared) } handle_changed_spte(kvm, kvm_mmu_page_as_id(sp), sptep, gfn, old_spte, FROZEN_SPTE, level, shared); - - if (is_mirror_sp(sp)) { - KVM_BUG_ON(shared, kvm); - remove_external_spte(kvm, gfn, old_spte, level); - } } =20 if (is_mirror_sp(sp) && @@ -590,10 +566,21 @@ static void handle_changed_spte(struct kvm *kvm, int = as_id, tdp_ptep_t sptep, * the paging structure. Note the WARN on the PFN changing without the * SPTE being converted to a hugepage (leaf) or being zapped. Shadow * pages are kernel allocations and should never be migrated. + * + * When removing leaf entries from a mirror, immediately propagate the + * changes to the external page tables. Note, non-leaf mirror entries + * are handled by handle_removed_pt(), as TDX requires that all leaf + * entries are removed before the owning page table. Note #2, writes + * to make mirror PTEs shadow-present are propagated to external page + * tables by __tdp_mmu_set_spte_atomic(), as KVM needs to ensure the + * external page table was successfully updated before marking the + * mirror SPTE present. */ if (was_present && !was_leaf && (is_leaf || !is_present || WARN_ON_ONCE(pfn_changed))) handle_removed_pt(kvm, spte_to_child_pt(old_spte, level), shared); + else if (was_leaf && is_mirror_sptep(sptep) && !is_leaf) + kvm_x86_call(remove_external_spte)(kvm, gfn, level, old_spte); } =20 static inline int __must_check __tdp_mmu_set_spte_atomic(struct kvm *kvm, @@ -725,12 +712,10 @@ static u64 tdp_mmu_set_spte(struct kvm *kvm, int as_i= d, tdp_ptep_t sptep, =20 /* * Users that do non-atomic setting of PTEs don't operate on mirror - * roots, so don't handle it and bug the VM if it's seen. + * roots. Bug the VM as this path doesn't propagate such writes to the + * external page tables. */ - if (is_mirror_sptep(sptep)) { - KVM_BUG_ON(is_shadow_present_pte(new_spte), kvm); - remove_external_spte(kvm, gfn, old_spte, level); - } + KVM_BUG_ON(is_mirror_sptep(sptep) && is_shadow_present_pte(new_spte), kvm= ); =20 return old_spte; } --=20 2.53.0.rc1.217.geba53bf80e-goog