From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.177 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796287528; b=veGQTvVy1VM5lwc+uu3/vsXJkw1pXDm0Tw9BC7Cy4ql2RtE8muNJXUyufHK23cmeZAh8 q1Izs0BTAjuY3YoA/ATrfjfjxuc2jCbfDMTlbGf+A2yuhY2EMmHAu4wM9wTHk7kaAs0R/ qFq/ZPP3C9HMxckvgmOPr+h425uQ+FkNg6FiBQ5EpqgxIBVRuYYgzskabn0JGaMO8H58D Ku1ogBzW+dgnv+z9Wrvu8FuCkd4INJA1ZbnPrfReTLK+y8dl593oNtIjjhU0Tik7KGD8n f9fRQ4nrPiTvykmDrzb3n5arLHY6qE5Z6mLLTn0bk8fB5pxK+JtxP17rVbALh4dT2fA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796287528; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:Content-Type:Content-Transfer-Encoding:MIME-Version; bh=VJTtxbY3gU8a0HufJTXh0lf/QLAeDLuVxRK2ciwm8m4=; b=0Kv5+Ikf9bskPbdGxT6ITkwGMTk9kFUp5/VjeRwUDcG6+hVegg2MP7+Z9OK6sORG/8Ei 1NKzYp5y9R+W+Dm8wHmr8KJjbBN61rbkQpEx4WUMnjcw7gB3l8ZYoP1yU2Q+tw5uL4xPL 5/HZQ+9QDYQQMz8bwtYFLadvwM+cAYqHrtos5lGVV66JaltsnEImg6AW0EdF9RvypcfpF 8EDZTbX/xIayGR6P3Dy4qJG4STSUcQ8q8xsCeaoG66WMLQXhP5Bg+sJA0F5qXT47Y8Ux4 OFKJO31A92jT7ZCv7YLYR1TR2LgTNxJwRCWB1wuKkozapVM4nnaOeVHfEFo7ltX42Ng== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=jDdN9WjQ; arc=none smtp.remote-ip=74.125.82.177 Received: from mail-dy1-f177.google.com (mail-dy1-f177.google.com [74.125.82.177]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF858606A333 for ; Thu, 3 Dec 2026 08:45:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AF858606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=jDdN9WjQ Received: by mail-dy1-f177.google.com with SMTP id 5a478bee46e88-38747f42472so306290eec.0 for ; Thu, 03 Dec 2026 00:45:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796287527; x=1796892327; darn=vger.kernel.org; h=mime-version:content-transfer-encoding:content-type:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=VJTtxbY3gU8a0HufJTXh0lf/QLAeDLuVxRK2ciwm8m4=; b=jDdN9WjQ4+4aSfBKRjSX1/KyC8AWLSNCmp8ibD28NWlrT577sERsCwoqN59cBd08mQ 9VEIwmblIsMz6HbouoDJNQ4EULWwSevJgZXgNqZFbphqdKXDgwbrhR+KqscuIvkvECj6 WtOWnFfEgTRhccdR6ZtQ7JZyM7fjl2ud+sf8ihbCUlZUyRqLIkgySkdl3+49Q4A07qyv rGFezg6NbZpgRjwzP0yTgootaCzRlSEfK/n+DFEdYjAH05EXYkoDdMtXntACjW769bg4 d6MNlFas3mRwNKX+XYKloJ4DmDO//y3mCHNMeTcvlg5vVxLJYJ6MUIRxRtYXxhGQvnZ9 PDaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796287527; x=1796892327; h=mime-version:content-transfer-encoding:content-type:message-id:date :subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=VJTtxbY3gU8a0HufJTXh0lf/QLAeDLuVxRK2ciwm8m4=; b=heGH939tPZCDlPbV5lJLR208WH8p1LjYgF1S92EQJwAoiwuDq6zxSLf/lQGWeN3IpT mT9xQd3WL3016inzP5IrLq/0ybQx7P5+arNnylKKrmwzs8rOKKSiKJtTTtmaRCvnrvfK as/4w3SwsdFj6WXTbb6Ey1ROHpPd9mQzqMgwndQ8rwEJZc34Ss8/Hwk+EcK0rBiFtLh+ DONWh7BKlPcAeQHkG8sbbNAi7DudTS+rVCYR1ChYEbUricowG9i8flY4mcoXjrNav1KS X+Pd/9iiEavAwKAQU6lR7FSBJ6LrtRMLNKbAneXXpkG78lgPK1CSe11mQfPdvRinVBoP iu5w== X-Gm-Message-State: APuU4Z/QBeQvVtECTRLuv026RMqU7CFQJ4fR5uquzwE9VsTQUg0ML8Me r6cnyTehjbvdm0OlK5xgzSupDylI51BMXuoKqV4trNQ/uKIGyoYWW0mlmFWye+9ECKs= X-Gm-Gg: AZFdTZcaA0yEJBPRAuFscBiZUT+R4H/4PReM604m7rN7KEKBj4qM5StU+nJ+F94Flg6 050trwiS/KhmoNDEhWNJXymourfFcvBnzsXELoATpCFjv9APSptZIHQZ03L4TGRpYsQ5H/8UHeF 08O4Fyrc+bH/WAwtuobbOXwoWbGCn91R8TKoYdmuDB4fw2qhbJ3r30AB9hqbzxNBX7YVSxQrDgD TGDjEXYd9iRK3seHUnctqFQdXPDj9E7y0/S7tHem/7y01UWS3JRIhW0NwzKwzn8/V9VrWmRh928 vQOKKmvJjopvQpwEDQiIk0DiZoLFaWmWOhXJf/SQkXsGzwpgW3Jgfnn9XQmPs/JwEnyovithPx2 drcDVNf7RkEQPE293Iiorc71EBNl9OPGSbq2T8C5Pg3YglWQQJ+uTfXir5O7qPfnWKJRZo6rv/P F9pflK01GqXA0kHonoCnKMOb2cuUEiJSGNugioXgwvFqYHq+Iq79Q1Tv+CSdF7KxWcWY6EXQxJU 5Szn3Fsv3AuaOLk2w1f6O9chxbYOOW22WYMA/7zZiiA7t02t1BigMAjOMIC929TJPTEzjKJsMfa U455qasRguq6OaQ2DWevodrY//PT1ro0BQe0ACPxkeZD6g0g88MVejNIfffwqMst+JbJNEhGwYG 5X4Gc8DtGIR88iH5tt63yOfKSVICcQNcJLHVTfwUq57CqhooegQ== X-Received: by 2002:a05:7301:2419:b0:367:185e:867a with SMTP id 5a478bee46e88-38571027a5dmr45910302eec.38.1796287526754; Thu, 03 Dec 2026 00:45:26 -0800 (PST) Received: from [127.0.1.1] (M106072044160.v4.enabler.ne.jp. [106.72.44.160]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385677c371esm126578272eec.23.2026.12.03.00.45.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 00:45:26 -0800 (PST) From: Krzysztof =?utf-8?q?Wo=C5=9B?= To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations Date: Thu, 03 Dec 2026 08:07:33 +0000 Message-ID: <179628525375.378593.18062933948337352088@gmail.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Skip promoting a non-leaf TDP MMU SPTE in recover_huge_pages_range() if an in-progress mmu_notifier invalidation overlaps the candidate hugepage GFN range [iter.gfn, iter.gfn + KVM_PAGES_PER_HPAGE(iter.level)). host_pfn_mapping_level() requires callers installing new mappings under mmu_lock to ensure no mmu_notifier invalidation is in progress for the target HVA range (either via mmu_invalidate_retry_gfn() or by already holding a valid mapping covering the entire HVA range). Unlike kvm_mmu_zap_collapsible_spte(), which only zaps 4 KiB SPTEs, recover_huge_pages_range() promotes a non-leaf SPTE to a present 2 MiB or 1 GiB leaf SPTE in place via tdp_mmu_make_huge_spte() and tdp_mmu_set_spte_atomic(). Because tdp_mmu_make_huge_spte() succeeds as soon as any single child leaf SPTE in the range is shadow-present, having a shadow-present non-leaf parent SPTE does not guarantee the absence of an active sub-range invalidation on another GFN within that hugepage. If the primary MMU runs kvm_mmu_notifier_invalidate_range_start() on a 4 KiB sub-range of a 2 MiB THP (zapping that 4 KiB SPTE and releasing mmu_lock while mmu_invalidate_in_progress remains non-zero and before the host PMD is split or cleared) concurrently with disabling dirty logging, recover_huge_pages_range() can still observe PG_LEVEL_2M from host_pfn_mapping_level(), synthesize a 2 MiB leaf SPTE from a surviving sibling 4 KiB SPTE, and install a stale present mapping covering the invalidated sub-range that persists past invalidate_range_end(). Checking the full hugepage GFN span against mmu_invalidate_in_progress and mmu_invalidate_range_start..end while holding mmu_lock for read prevents installing huge SPTEs over actively invalidated sub-ranges, as starting or expanding an invalidation range requires holding mmu_lock for write. When tdp_mmu_iter_cond_resched() drops and re-acquires mmu_lock (or tdp_mmu_make_huge_spte() / tdp_mmu_set_spte_atomic() retries), control loops back to retry: and re-evaluates the overlap check under the newly acquired lock. Found via TLA+ concurrency model checking and LLM-assisted static review of TDP MMU invalidation paths on commit d06ce6f1eb5e, and manually verified against the mmu_notifier locking protocol. Build-tested on x86_64 (GCC 15.2.0) with KVM_INTEL=m, KVM_AMD=m, KVM_WERROR=y, KVM_SMM=y, KVM_XEN=y, and PROVE_LOCKING=y; no runtime reproducer was executed for the concurrent dirty-log teardown and sub-range THP invalidation race. Fixes: 13e2e4f62a4b ("KVM: x86/mmu: Recover TDP MMU huge page mappings in-place instead of zapping") Cc: stable@vger.kernel.org Signed-off-by: Krzysztof Woś --- Base-commit: d06ce6f1eb5eeffd4ebcfde345dc34a98131d106 arch/x86/kvm/mmu/tdp_mmu.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 44dad106f..ce496e3d3 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -1750,6 +1750,22 @@ static int tdp_mmu_make_huge_spte(struct kvm *kvm, return -ENOENT; } +static bool tdp_mmu_invalidate_in_progress(struct kvm *kvm, + gfn_t start, gfn_t end) +{ + lockdep_assert_held(&kvm->mmu_lock); + + if (likely(!kvm->mmu_invalidate_in_progress)) + return false; + + if (WARN_ON_ONCE(kvm->mmu_invalidate_range_start == INVALID_GPA || + kvm->mmu_invalidate_range_end == INVALID_GPA)) + return true; + + return start < kvm->mmu_invalidate_range_end && + end > kvm->mmu_invalidate_range_start; +} + static void recover_huge_pages_range(struct kvm *kvm, struct kvm_mmu_page *root, const struct kvm_memory_slot *slot) @@ -1796,6 +1812,16 @@ static void recover_huge_pages_range(struct kvm *kvm, if (iter.gfn < start || iter.gfn >= end) continue; + /* + * Do not promote to a huge SPTE if an in-progress mmu_notifier + * invalidation overlaps the hugepage range, as only a subset of + * child SPTEs may have been zapped while the host mapping is + * still huge, violating host_pfn_mapping_level()'s contract. + */ + if (tdp_mmu_invalidate_in_progress(kvm, iter.gfn, + iter.gfn + KVM_PAGES_PER_HPAGE(iter.level))) + continue; + max_mapping_level = kvm_mmu_max_mapping_level(kvm, NULL, slot, iter.gfn); if (max_mapping_level < iter.level) continue;