linux-stable/arch/x86
Sean Christopherson 741e511b42 KVM: x86: Don't snapshot "max" TSC if host TSC is constant
Don't snapshot tsc_khz into max_tsc_khz during KVM initialization if the
host TSC is constant, in which case the actual TSC frequency will never
change and thus capturing the "max" TSC during initialization is
unnecessary, KVM can simply use tsc_khz during VM creation.

On CPUs with constant TSC, but not a hardware-specified TSC frequency,
snapshotting max_tsc_khz and using that to set a VM's default TSC
frequency can lead to KVM thinking it needs to manually scale the guest's
TSC if refining the TSC completes after KVM snapshots tsc_khz.  The
actual frequency never changes, only the kernel's calculation of what
that frequency is changes.  On systems without hardware TSC scaling, this
either puts KVM into "always catchup" mode (extremely inefficient), or
prevents creating VMs altogether.

Ideally, KVM would not be able to race with TSC refinement, or would have
a hook into tsc_refine_calibration_work() to get an alert when refinement
is complete.  Avoiding the race altogether isn't practical as refinement
takes a relative eternity; it's deliberately put on a work queue outside
of the normal boot sequence to avoid unnecessarily delaying boot.

Adding a hook is doable, but somewhat gross due to KVM's ability to be
built as a module.  And if the TSC is constant, which is likely the case
for every VMX/SVM-capable CPU produced in the last decade, the race can
be hit if and only if userspace is able to create a VM before TSC
refinement completes; refinement is slow, but not that slow.

For now, punt on a proper fix, as not taking a snapshot can help some
uses cases and not taking a snapshot is arguably correct irrespective of
the race with refinement.

[ dwmw2: Rebase on top of KVM-wide default_tsc_khz to ensure that all
         vCPUs get the same frequency even if we hit the race. ]

Cc: Suleiman Souhlal <suleiman@google.com>
Cc: Anton Romanov <romanton@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Message-Id: <20220225145304.36166-3-dwmw2@infradead.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2022-04-02 05:41:20 -04:00
..
boot Kbuild updates for v5.17 2022-01-19 11:15:19 +02:00
configs x86/kbuild: Enable CONFIG_KALLSYMS_ALL=y in the defconfigs 2022-01-08 22:55:29 +01:00
crypto lib/crypto: blake2s: avoid indirect calls to compression function for Clang CFI 2022-02-04 19:22:32 +01:00
entry Merge branch 'signal-for-v5.17' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace 2022-01-17 05:49:30 +02:00
events perf/x86/intel/pt: Fix crash with stop filters in single-range mode 2022-02-02 13:11:40 +01:00
hyperv hyperv-next for 5.17 2022-01-16 15:53:00 +02:00
ia32
include KVM: x86: Accept KVM_[GS]ET_TSC_KHZ as a VM ioctl. 2022-04-02 05:41:19 -04:00
kernel KVM: x86: fix sending PV IPI 2022-04-02 05:37:27 -04:00
kvm KVM: x86: Don't snapshot "max" TSC if host TSC is constant 2022-04-02 05:41:20 -04:00
lib - Get rid of all the .fixup sections because this generates 2022-01-12 16:31:19 -08:00
math-emu x86: Prepare asm files for straight-line-speculation 2021-12-08 12:25:37 +01:00
mm Merge branch 'akpm' (patches from Andrew) 2022-01-15 20:37:06 +02:00
net - Get rid of all the .fixup sections because this generates 2022-01-12 16:31:19 -08:00
pci PCI/sysfs: Find shadow ROM before static attribute initialization 2022-01-26 10:41:21 -06:00
platform - Get rid of all the .fixup sections because this generates 2022-01-12 16:31:19 -08:00
power x86: Prepare asm files for straight-line-speculation 2021-12-08 12:25:37 +01:00
purgatory x86/purgatory: Remove -nostdlib compiler flag 2021-12-30 14:13:06 +01:00
ras
realmode - Flush *all* mappings from the TLB after switching to the trampoline 2022-01-10 09:51:38 -08:00
tools x86/build: Use the proper name CONFIG_FW_LOADER 2021-12-29 22:20:38 +01:00
um bitmap patches for 5.17-rc1 2022-01-23 06:20:44 +02:00
video
xen x86/Xen: streamline (and fix) PV CPU enumeration 2022-02-03 08:25:04 +01:00
.gitignore
Kbuild
Kconfig ftrace: Have architectures opt-in for mcount build time sorting 2022-01-27 19:15:44 -05:00
Kconfig.assembler
Kconfig.cpu x86/mmx_32: Remove X86_USE_3DNOW 2021-12-11 09:09:45 +01:00
Kconfig.debug
Makefile x86: Add straight-line-speculation mitigation 2021-12-09 13:32:25 +01:00
Makefile.um
Makefile_32.cpu