From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) (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 E82D942902C for ; Thu, 6 Aug 2026 21:40:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786052458; cv=none; b=khO+wghbIMsJ1ffAJqNBm+c8fLrqw7Y6Vc6rnGA3+3n7q7RiU69A6kbuGszMMERnswN3ClBs948qBpHzSWq8GEJHUjG30tY91q6PlHceiXwFpTA1FjCxwunDr6dQNJtdxSTq3ZUADMdAv01LJviqv4YnKGm76yHIEKz7Gcv9vuw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786052458; c=relaxed/simple; bh=YXvxBbhrwBIqcRqu7EKMvZk9hvjNwBNYInFYiCi5wRU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uC8qzGru0SuL2OqHhWHUEXn8caXDTEzXO0y4vZD7/wxuKk3w+FFqowZte1rnuWqUN+3WcapRKp+ELD9QIUeb3IxvVC7dmMGJtrVhI+85FwF4dvnJb30mAfUlH2DBUJ55SUT4CLRU6CGPPqiTl20aJCGK//XSBfM2Ks1h8YE27g4= 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=e/2TIBff; arc=none smtp.client-ip=209.85.210.198 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="e/2TIBff" Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-84e048a801dso3547115b3a.3 for ; Thu, 06 Aug 2026 14:40:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786052456; x=1786657256; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=rEEwTcg7bv7FkGEAqkUfC5yUVJEh4P3MKarrm6QtyZY=; b=e/2TIBffTAf1F/sIz6Vmg4Ta/254Zztm2NGBoJzFXzHopCtue/wZP0KHMFEd5seYoU MKEqG5/ipPrZzoTflckM0Rj3AjgZczv/6lEZIv4dAc7kW+/K0fcfeb1crfbNwN2UO70R +0jIHLZyktYs91+ah5ThuUJoeObVraPBy8Ep2XQV3l38x1B9QtHh/dCZOBcyVF+2RLoi kgz7sr5WWIeB0/2xnS+DS/ELJH0fm+Ibp6MAbweByXkpO8fgb56y+JS3xkxN0iTJWWFz wkR95b9fSe5Ka1MOqh1fRUfEfUa8FL+uoBXs1IBTDFcdHda3Ju2FjtSlB+pxD9Mf0j5s wPJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786052456; x=1786657256; h=content-type: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:content-type; bh=rEEwTcg7bv7FkGEAqkUfC5yUVJEh4P3MKarrm6QtyZY=; b=McdfWs28zqagNB9F2H36uzDD3kg+tEnQIY/HW1RmsVoUR2YkdqSqBsTM4kAhCRMWWG rM+W0MuHZp4khnRTGHN1bpWvsxWnxylT1mM0S4fkqt+0eV3bpTL0Egb8FKtK5bOFYOGD ODT5nBefXDuGcfe6+vABLGrsRDgheSPUxv0yZom707bS6/iYWW8sZdanO+mdWZfIk9Pa lZQr5uemJ8Ucpjvwp72wl0yYZt4LsBDzFFC9elbjCME2o5/if+VcGMugNyU65L/E+QDC yyKx4QqVNNPM0tGVGArDfE4NSaCbNJYz0CTSxgx2GvpEaI6vTXAsK1akJttIMzibRKvW P/Zw== X-Forwarded-Encrypted: i=1; AHgh+RqKd7GrDnqW4mUyj/z5IfRqokOnuJTrJpGteanImegUW5gsNggsTeJ6hraaVoHONU4mggttQhb7g3iQEdc=@vger.kernel.org X-Gm-Message-State: AOJu0YwGxxmCAoAIL1zpK9+e7luCK9YpOUlk8E71zwv6rSSTQtKRCHy2 w25rrM32QnZekYZf7w9XStBea2OWU+qNI7DUXaflQ5KTQzHXN8aFENLvo//Z+2l12C4TiUA6Oie SBKn0fA== X-Received: from pfn34.prod.google.com ([2002:a05:6a00:a222:b0:84a:31b0:7544]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:c81:b0:845:cdc1:a803 with SMTP id d2e1a72fcca58-84f2dfc4763mr19978858b3a.11.1786052455960; Thu, 06 Aug 2026 14:40:55 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 6 Aug 2026 14:40:48 -0700 In-Reply-To: <20260806214050.78058-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: <20260806214050.78058-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260806214050.78058-3-seanjc@google.com> Subject: [PATCH 2/4] KVM: x86/mmu: Harden "map private PFN" against unexpected root invalidation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang , Yan Zhao , Rick Edgecombe , Sashiko Bot Content-Type: text/plain; charset="UTF-8" Move kvm_tdp_mmu_map_private_pfn()'s reload of the MMU into its tight loop so that an unexpected root invalidation has a better chance of being handled gracefully, even though it should be impossible for the vCPU's root to be invalidated after the initial reload. As is, encountering an invalid root is *guaranteed* to put the task into an infinite loop (albeit a breakable loop that honors NEED_RESCHED). Add a WARN to try and detect bugs that break KVM's expectations, along with a comment to explain why it should be impossible for the root to be invalidated. Cc: Kai Huang Cc: Yan Zhao Cc: Rick Edgecombe Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 621b0a42f2a1..c6cac893cbad 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5184,10 +5184,6 @@ int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn) if (kvm_gfn_is_write_tracked(kvm, fault.slot, fault.gfn)) return -EPERM; - r = kvm_mmu_reload(vcpu); - if (r) - return r; - r = mmu_topup_memory_caches(vcpu, false); if (r) return r; @@ -5199,10 +5195,21 @@ int kvm_tdp_mmu_map_private_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn) if (kvm_test_request(KVM_REQ_VM_DEAD, vcpu)) return -EIO; + r = kvm_mmu_reload(vcpu); + if (r) + return r; + cond_resched(); guard(read_lock)(&kvm->mmu_lock); + /* + * Because slots_lock is held, it should be impossible for roots + * to be invalidated after the initial MMU reload. WARN, but + * continue and re-reload the MMU to try and keep the VM alive. + */ + WARN_ON_ONCE(kvm_test_request(KVM_REQ_MMU_FREE_OBSOLETE_ROOTS, vcpu)); + r = kvm_tdp_mmu_map(vcpu, &fault); } while (r == RET_PF_RETRY); -- 2.55.0.679.g6767b8d81c-goog