From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796289762; b=aqLYbIDGpLsihrdAM9uYcyCetha9yi5EG/mig0SQtgQg9065cLkb2jloSbRoBa6vE+B5 4TZRkBJSSbsQLKbvqNcGIIXcTabyo9JohLP4tiJo6Qu8pkDRbtxnaQGzMYFxQaVUAnc7h 4v5zx50mO31bqNKGWY3BsvnEVMBxUIZpuwxl4XSh72Xc77Ikn97LYMT+a7qnHJ3be5tHv 2BR7OCrVBBCibi7HRxdACDpfpA4qjpxoDP5F+TBqoE9o3sf7qrqy2qsGwFhsVOrt03zt7 P8mUfdeFmuZqo3l7+fpkouZvKzi2npZDPEvU6fVyuiOJCbJi9nx3spukE6HQu4XOvnw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796289762; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=HqFw8OMrCh3Ew4IHcBmFINRQmJoAGjgNDMXdutHnu4A=; b=RDsK+NdlURE40gLkwaWG/FLUyeip4M0GG06YqzqJuln0t3mLvJhYNEsEDqBjtBLKE4k8 nKlHqK5S6iUQjJpwkva4SBvdxXM7tIBnZg7bKAfOrVPDQZdIJY/MvZMg1xY0vuED0gDfD aBkDt1DFqvAm+DtaiMGpZSM8g99WovGRKZGFW167+onzz0I3ms1QkpieZyLu8E9mq2qh8 UR8krRl7xOVD9UwQud0l5o3R8mpMvPzwZKm1vJv/mzYWPRy16qjzaNDB5TmYtZqg7gAVo 1qMTfAf+aMXYMdgLNWrd4CaPAcmle0gc3iqo+34qWUB3sBlsZ5WbH2TDB2iRszILxdg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Q8yw6fMk; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 43407606A32C for ; Thu, 3 Dec 2026 09:22:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Q8yw6fMk Received: by smtp.kernel.org (Postfix) with ESMTPSA id 041B71F00896; Thu, 3 Dec 2026 09:22:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796289762; bh=HqFw8OMrCh3Ew4IHcBmFINRQmJoAGjgNDMXdutHnu4A=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=Q8yw6fMkyGfSKXhrJtY42PUvyIHBOQGtQdoE7pSetkuRdApuYJtqFTOdGKxQr9PoK XbdLjm0JKhuxgmv8B93LxZT7+xuQM/5tDe2QaYpzhQndvo/yBSGutjIuBm/DasK9vV I00M1jc7C5rpkI44wTYoHDyJv/tuYxjlQw928xMHwKETPDRM83OsWIFQJqMfC3Vn0p U2yjifmdgP2wFAO93gjKvAZZh4Pe26glIz8LhILdeY0v22n4/ibyRDcfd3kExI0yfP MnzEB84BWO4uH7njtM4Ndl3LypXGZkwD8nnPtwSUEpnzTq9XKt/v0JOTuDK6UOKxC8 ObpvOMwJy+NCw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations Reply-To: sashiko-reviews@lists.linux.dev To: =?utf-8?b?S3J6eXN6dG9mIFdvxZs=?= Cc: kvm@vger.kernel.org In-Reply-To: <179628525375.378593.18062933948337352088@gmail.com> References: <179628525375.378593.18062933948337352088@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 09:22:41 +0000 X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] KVM: x86/mmu: skipping hugepage recovery leaves pages permanently fragmented - [Low] KVM: x86/mmu: Commit message overstates 2 MiB THP stale SPTE persistence - [Low] KVM: x86/mmu: tdp_mmu_invalidate_in_progress() duplicates core range check -- commit 108097c7b14815cdf087cf02d4ef5a7591086b62 Author: Krzysztof Wo=C5=9B KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations This commit adds a check in recover_huge_pages_range() to skip promoting non-leaf TDP MMU SPTEs whose hugepage GFN range overlaps an in-progress mmu_notifier invalidation. > 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(). [Severity: Low] Is the claim that a 4 KiB THP sub-range invalidation leaves a stale 2 MiB SPTE persisting past invalidate_range_end() accurate? Before splitting a 2 MiB THP for a sub-range operation (e.g. zap_pmd_range(= )), __split_huge_pmd() issues a nested mmu_notifier_invalidate_range_start() for the full 2 MiB range, which zaps any 2 MiB SPTE installed in the interim before the PMD is split and before the outer invalidate_range_end() finishe= s. The code change still avoids transiently violating host_pfn_mapping_level()= 's contract, so rewording this paragraph would avoid overstating the issue as a persistent stale mapping. > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > index 44dad106fad19..ce496e3d31e37 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; > } > =20 > +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 =3D=3D INVALID_GPA || > + kvm->mmu_invalidate_range_end =3D=3D INVALID_GPA)) > + return true; > + > + return start < kvm->mmu_invalidate_range_end && > + end > kvm->mmu_invalidate_range_start; > +} [Severity: Low] Can this range-overlap check be factored into a generic helper in include/linux/kvm_host.h instead of duplicating it here? tdp_mmu_invalidate_in_progress() has no x86 or TDP-MMU specifics and duplicates the range and INVALID_GPA checks in mmu_invalidate_retry_gfn(). No other arch/ code reads mmu_invalidate_range_start/end directly, so shari= ng a core helper keeps those invariants from drifting. [ ... ] > @@ -1796,6 +1812,16 @@ static void recover_huge_pages_range(struct kvm *k= vm, > if (iter.gfn < start || iter.gfn >=3D end) > continue; > =20 > + /* > + * 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; [Severity: Medium] Does skipping with continue here leave unaffected hugepages permanently fragmented as 4 KiB SPTEs? recover_huge_pages_range() is a one-shot pass called only when disabling dirty logging (kvm_mmu_slot_apply_flags() -> kvm_mmu_recover_huge_pages()), so skipped non-leaf SPTEs are never revisited. Because kvm_mmu_invalidate_range_add() merges concurrent invalidations into= a single min(start)..max(end) bounding box until all of them finish, two distant invalidations during a recovery walk can skip large regions of unaffected memory, leaving them fragmented until another dirty-log cycle. When tdp_mmu_invalidate_in_progress() is true, falling back to zapping collapsible non-leaf SPTEs, as before 13e2e4f62a4b ("KVM: x86/mmu: Recover TDP MMU huge page mappings in-place instead of zapping"), obeys host_pfn_mapping_level()'s contract and lets later faults rebuild hugepages. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/179628525375.378593= .18062933948337352088@gmail.com?part=3D1