All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Krzysztof Woś" <krzysztof.wos@gmail.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH] KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations
Date: Thu, 03 Dec 2026 09:22:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-247451@kernel.org> (raw)
In-Reply-To: <179628525375.378593.18062933948337352088@gmail.com>

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ś <krzysztof.wos@gmail.com>

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() finishes.

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;
>  }
>  
> +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;
> +}

[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 sharing
a core helper keeps those invariants from drifting.

[ ... ]

> @@ -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;

[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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179628525375.378593.18062933948337352088@gmail.com?part=1

      reply	other threads:[~2026-12-03  9:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  8:07 [PATCH] KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations Krzysztof Woś
2026-12-03  9:22 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-247451@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=krzysztof.wos@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.