From nobody Tue Dec 16 14:38:15 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 96975329E4D for ; Thu, 30 Oct 2025 20:10:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761855052; cv=none; b=kPVSBrPNr6UAuRRDSisCuDnHxpgxK4oNAU7tLg7klHuDM03arFwrVgVOLEZw1ZcRc1hVjoGbKWX+A5Qyhc8R4EAnRAqSNhlpNN2UjVTTdgAnklDhrJMr9OlegJgQHt2tKBeqrOOWjmvs1dMWcNkW7U0gThUzAgjeuucF1OmhDHs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761855052; c=relaxed/simple; bh=gvzcFRbzaqGBcuZ+f2VoTMDHPgCQZ6omIrzB4Q9yffU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rgKF5ucuNgal39BbVND8j95oRvUrkE6l1VteuEhj1wQLIYlxAj+GY5j9GmXApGqjO4YoHnShMwT9BUL57Xu9XsoqrbHi3glSpH0kPXf/3PY1zkEdmGhtxLJ3VhCFlaEdc6NNAjNVaSmcnbOfkCq6/kaBYlPgR2z8eFDU3sRjMUw= 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=u2FBLlUZ; arc=none smtp.client-ip=209.85.214.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="u2FBLlUZ" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-28bd8b3fa67so12820275ad.2 for ; Thu, 30 Oct 2025 13:10:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1761855050; x=1762459850; 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=JvdyzKJX9NuDfCF42GsCtwlMI06iCRR0AAdHR5FNyUo=; b=u2FBLlUZ+lwWe5HPcLxb3qjhYcUbALd6FK9vUvldg72EsVY7TAA5jr3WbKkhEsA9V3 2FrshuFmwwFimtFhlJ/aY3a45IXz+WEZUA0pr9zk2Opy0OlGfZDY/DcGuvAHWZChuoQ5 ztQGdkcxJZdoUNcisE2kM8ppJflGHPHZjKxuTmDF9KZRXpVwh/u4Mxq9xQj0blmEF7Xv rHYXyaX/+dx+EEMPOaa+6kJsNwwLpzgGDNb2C8b4YtIj1hNHC0NiSHrdDFSsbT2dMnrP vQTwlcjmEhn2U95+0w5Au4crPHiPzoov9EukihpvbVzEd/AWpYkyiiNd98p7unvXKeLy 01yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761855050; x=1762459850; 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=JvdyzKJX9NuDfCF42GsCtwlMI06iCRR0AAdHR5FNyUo=; b=sWuRctUBk4HCLYgxazQOCLzfsXRxEjHin4AKaEy/1t3zvk5TtAQART1kxJT9rqD3iQ tWrkZaSurgpmSfzBtDJx1BI3qJVQDRl6mrcD+p+htWO1FDeToQOVId6e04C374iJNQa8 51U8G8F//oYGVCF2IyKIQ63QVmlpd+Zzk4hYgYscbiq56u1H+aavmQsoUE9lEVqY98dz 7udvvDLnAh0vJrcHosHDF/iKDbcg6fpkDrF/Q+ahHqAZYGqGAIgzSntgf9FHStE5jpQp Jpbe9/KroZT1C5Yx4oVqjkArfb28txDMWBVAWyiWg7GzSaEG6I0Gg1dv6u6D0Q0HZ/Sh aEmA== X-Forwarded-Encrypted: i=1; AJvYcCUjr1eB5De1WW8UeENUg9xvCRZEOGD7bo1IPlK5cHttwyacjnQOIdLzaQB/6MaN2tIoQE6nYc+cUa+aGG0=@vger.kernel.org X-Gm-Message-State: AOJu0YyazkC2T65NxXnTKjfjjxrlvlXjrd7VK873hMqtzOLux/zm6gbo hBfT+IZVZjRqE35R7TUCq/8+q3/nRcuB+xyFy1fzexb/Z32JRXjPrMpgQ4F9YKRdtnLsx18hdJW WVgBomQ== X-Google-Smtp-Source: AGHT+IHEVTdCFV7pO4/z6pVQDy8N1YwVBneIfEZO+ruu2Aqfbau+04/y/mG+hlEqHWjdepdrmBSAOEyDdTs= X-Received: from plge18.prod.google.com ([2002:a17:902:cf52:b0:290:d7ff:80f8]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:38c8:b0:294:fda6:4723 with SMTP id d9443c01a7336-2951a6007f3mr12024995ad.60.1761855049987; Thu, 30 Oct 2025 13:10:49 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 30 Oct 2025 13:09:43 -0700 In-Reply-To: <20251030200951.3402865-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: <20251030200951.3402865-1-seanjc@google.com> X-Mailer: git-send-email 2.51.1.930.gacf6e81ea2-goog Message-ID: <20251030200951.3402865-21-seanjc@google.com> Subject: [PATCH v4 20/28] KVM: TDX: Assert that mmu_lock is held for write when removing S-EPT entries From: Sean Christopherson To: 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 , Sean Christopherson , Paolo Bonzini , "Kirill A. Shutemov" Cc: 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 , Binbin Wu , Michael Roth , Yan Zhao , Vishal Annapurve , Rick Edgecombe , Ackerley Tng Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Unconditionally assert that mmu_lock is held for write when removing S-EPT entries, not just when removing S-EPT entries triggers certain conditions, e.g. needs to do TDH_MEM_TRACK or kick vCPUs out of the guest. Conditionally asserting implies that it's safe to hold mmu_lock for read when those paths aren't hit, which is simply not true, as KVM doesn't support removing S-EPT entries under read-lock. Only two paths lead to remove_external_spte(), and both paths asserts that mmu_lock is held for write (tdp_mmu_set_spte() via lockdep, and handle_removed_pt() via KVM_BUG_ON()). Deliberately leave lockdep assertions in the "no vCPUs" helpers to document that wait_for_sept_zap is guarded by holding mmu_lock for write, and keep the conditional assert in tdx_track() as well, but with a comment to help explain why holding mmu_lock for write matters (above and beyond why tdx_sept_remove_private_spte()'s requirements). Signed-off-by: Sean Christopherson Reviewed-by: Binbin Wu Reviewed-by: Kai Huang --- arch/x86/kvm/vmx/tdx.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 63d4609cc3bc..999b519494e9 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1715,6 +1715,11 @@ static void tdx_track(struct kvm *kvm) if (unlikely(kvm_tdx->state !=3D TD_STATE_RUNNABLE)) return; =20 + /* + * The full sequence of TDH.MEM.TRACK and forcing vCPUs out of guest + * mode must be serialized, as TDH.MEM.TRACK will fail if the previous + * tracking epoch hasn't completed. + */ lockdep_assert_held_write(&kvm->mmu_lock); =20 err =3D tdh_mem_track(&kvm_tdx->td); @@ -1762,6 +1767,8 @@ static void tdx_sept_remove_private_spte(struct kvm *= kvm, gfn_t gfn, gpa_t gpa =3D gfn_to_gpa(gfn); u64 err, entry, level_state; =20 + lockdep_assert_held_write(&kvm->mmu_lock); + /* * HKID is released after all private pages have been removed, and set * before any might be populated. Warn if zapping is attempted when --=20 2.51.1.930.gacf6e81ea2-goog