]> www.infradead.org Git - users/jedix/linux-maple.git/commit
KVM: x86/mmu: Check yielded_gfn for forward progress iff resched is needed
authorSean Christopherson <seanjc@google.com>
Thu, 31 Oct 2024 17:06:32 +0000 (10:06 -0700)
committerSean Christopherson <seanjc@google.com>
Tue, 5 Nov 2024 02:37:18 +0000 (18:37 -0800)
commite287e43167139a6c644ba648be6b2bf39314eaae
treef55de2623b252321edf1aa21bb014074e1bc9c28
parent35ef80eb29ab5f7b7c7264c7f21a64b3aa046921
KVM: x86/mmu: Check yielded_gfn for forward progress iff resched is needed

Swap the order of the checks in tdp_mmu_iter_cond_resched() so that KVM
checks to see if a resched is needed _before_ checking to see if yielding
must be disallowed to guarantee forward progress.  Iterating over TDP MMU
SPTEs is a hot path, e.g. tearing down a root can touch millions of SPTEs,
and not needing to reschedule is by far the common case.  On the other
hand, disallowing yielding because forward progress has not been made is a
very rare case.

Returning early for the common case (no resched), effectively reduces the
number of checks from 2 to 1 for the common case, and should make the code
slightly more predictable for the CPU.

To resolve a weird conundrum where the forward progress check currently
returns false, but the need resched check subtly returns iter->yielded,
which _should_ be false (enforced by a WARN), return false unconditionally
(which might also help make the sequence more predictable).  If KVM has a
bug where iter->yielded is left danging, continuing to yield is neither
right nor wrong, it was simply an artifact of how the original code was
written.

Unconditionally returning false when yielding is unnecessary or unwanted
will also allow extracting the "should resched" logic to a separate helper
in a future patch.

Cc: David Matlack <dmatlack@google.com>
Reviewed-by: James Houghton <jthoughton@google.com>
Link: https://lore.kernel.org/r/20241031170633.1502783-2-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/mmu/tdp_mmu.c