1. 24 Jan, 2023 1 commit
    • Paolo Bonzini's avatar
      Merge branch 'kvm-hw-enable-refactor' into HEAD · edd731d7
      Paolo Bonzini authored
      The main theme of this series is to kill off kvm_arch_init(),
      kvm_arch_hardware_(un)setup(), and kvm_arch_check_processor_compat(), which
      all originated in x86 code from way back when, and needlessly complicate
      both common KVM code and architecture code.  E.g. many architectures don't
      mark functions/data as __init/__ro_after_init purely because kvm_init()
      isn't marked __init to support x86's separate vendor modules.
      
      The idea/hope is that with those hooks gone (moved to arch code), it will
      be easier for x86 (and other architectures) to modify their module init
      sequences as needed without having to fight common KVM code.  E.g. I'm
      hoping that ARM can build on this to simplify its hardware enabling logic,
      especially the pKVM side of things.
      
      There are bug fixes throughout this series.  They are more scattered than
      I would usually prefer, but getting the sequencing correct was a gigantic
      pain for many of the x86 fixes due to needing to fix common code in order
      for the x86 fix to have any meaning.  And while the bugs are often fatal,
      they aren't all that interesting for most users as they either require a
      malicious admin or broken hardware, i.e. aren't likely to be encountered
      by the vast majority of KVM users.  So unless someone _really_ wants a
      particular fix isolated for backporting, I'm not planning on shuffling
      patches.
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      edd731d7
  2. 29 Dec, 2022 39 commits
    • Sean Christopherson's avatar
      KVM: Clean up error labels in kvm_init() · 9f1a4c00
      Sean Christopherson authored
      Convert the last two "out" lables to "err" labels now that the dust has
      settled, i.e. now that there are no more planned changes to the order
      of things in kvm_init().
      
      Use "err" instead of "out" as it's easier to describe what failed than it
      is to describe what needs to be unwound, e.g. if allocating a per-CPU kick
      mask fails, KVM needs to free any masks that were allocated, and of course
      needs to unwind previous operations.
      Reported-by: default avatarChao Gao <chao.gao@intel.com>
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-51-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      9f1a4c00
    • Sean Christopherson's avatar
      KVM: Opt out of generic hardware enabling on s390 and PPC · 441f7bfa
      Sean Christopherson authored
      Allow architectures to opt out of the generic hardware enabling logic,
      and opt out on both s390 and PPC, which don't need to manually enable
      virtualization as it's always on (when available).
      
      In addition to letting s390 and PPC drop a bit of dead code, this will
      hopefully also allow ARM to clean up its related code, e.g. ARM has its
      own per-CPU flag to track which CPUs have enable hardware due to the
      need to keep hardware enabled indefinitely when pKVM is enabled.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Acked-by: default avatarAnup Patel <anup@brainfault.org>
      Message-Id: <20221130230934.1014142-50-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      441f7bfa
    • Sean Christopherson's avatar
      KVM: Register syscore (suspend/resume) ops early in kvm_init() · 35774a9f
      Sean Christopherson authored
      Register the suspend/resume notifier hooks at the same time KVM registers
      its reboot notifier so that all the code in kvm_init() that deals with
      enabling/disabling hardware is bundled together.  Opportunstically move
      KVM's implementations to reside near the reboot notifier code for the
      same reason.
      
      Bunching the code together will allow architectures to opt out of KVM's
      generic hardware enable/disable logic with minimal #ifdeffery.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-49-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      35774a9f
    • Isaku Yamahata's avatar
      KVM: Make hardware_enable_failed a local variable in the "enable all" path · e6fb7d6e
      Isaku Yamahata authored
      Rework detecting hardware enabling errors to use a local variable in the
      "enable all" path to track whether or not enabling was successful across
      all CPUs.  Using a global variable complicates paths that enable hardware
      only on the current CPU, e.g. kvm_resume() and kvm_online_cpu().
      
      Opportunistically add a WARN if hardware enabling fails during
      kvm_resume(), KVM is all kinds of hosed if CPU0 fails to enable hardware.
      The WARN is largely futile in the current code, as KVM BUG()s on spurious
      faults on VMX instructions, e.g. attempting to run a vCPU on CPU if
      hardware enabling fails will explode.
      
        ------------[ cut here ]------------
        kernel BUG at arch/x86/kvm/x86.c:508!
        invalid opcode: 0000 [#1] SMP
        CPU: 3 PID: 1009 Comm: CPU 4/KVM Not tainted 6.1.0-rc1+ #11
        Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
        RIP: 0010:kvm_spurious_fault+0xa/0x10
        Call Trace:
         vmx_vcpu_load_vmcs+0x192/0x230 [kvm_intel]
         vmx_vcpu_load+0x16/0x60 [kvm_intel]
         kvm_arch_vcpu_load+0x32/0x1f0
         vcpu_load+0x2f/0x40
         kvm_arch_vcpu_ioctl_run+0x19/0x9d0
         kvm_vcpu_ioctl+0x271/0x660
         __x64_sys_ioctl+0x80/0xb0
         do_syscall_64+0x2b/0x50
         entry_SYSCALL_64_after_hwframe+0x46/0xb0
      
      But, the WARN may provide a breadcrumb to understand what went awry, and
      someday KVM may fix one or both of those bugs, e.g. by finding a way to
      eat spurious faults no matter the context (easier said than done due to
      side effects of certain operations, e.g. Intel's VMCLEAR).
      Signed-off-by: default avatarIsaku Yamahata <isaku.yamahata@intel.com>
      [sean: rebase, WARN on failure in kvm_resume()]
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-48-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      e6fb7d6e
    • Sean Christopherson's avatar
      KVM: Use a per-CPU variable to track which CPUs have enabled virtualization · 37d25881
      Sean Christopherson authored
      Use a per-CPU variable instead of a shared bitmap to track which CPUs
      have successfully enabled virtualization hardware.  Using a per-CPU bool
      avoids the need for an additional allocation, and arguably yields easier
      to read code.  Using a bitmap would be advantageous if KVM used it to
      avoid generating IPIs to CPUs that failed to enable hardware, but that's
      an extreme edge case and not worth optimizing, and the low level helpers
      would still want to keep their individual checks as attempting to enable
      virtualization hardware when it's already enabled can be problematic,
      e.g. Intel's VMXON will fault.
      
      Opportunistically change the order in hardware_enable_nolock() to set
      the flag if and only if hardware enabling is successful, instead of
      speculatively setting the flag and then clearing it on failure.
      
      Add a comment explaining that the check in hardware_disable_nolock()
      isn't simply paranoia.  Waaay back when, commit 1b6c0168 ("KVM: Keep
      track of which cpus have virtualization enabled"), added the logic as a
      guards against CPU hotplug racing with hardware enable/disable.  Now that
      KVM has eliminated the race by taking cpu_hotplug_lock for read (via
      cpus_read_lock()) when enabling or disabling hardware, at first glance it
      appears that the check is now superfluous, i.e. it's tempting to remove
      the per-CPU flag entirely...
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-47-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      37d25881
    • Isaku Yamahata's avatar
      KVM: Remove on_each_cpu(hardware_disable_nolock) in kvm_exit() · 667a83bf
      Isaku Yamahata authored
      Drop the superfluous invocation of hardware_disable_nolock() during
      kvm_exit(), as it's nothing more than a glorified nop.
      
      KVM automatically disables hardware on all CPUs when the last VM is
      destroyed, and kvm_exit() cannot be called until the last VM goes
      away as the calling module is pinned by an elevated refcount of the fops
      associated with /dev/kvm.  This holds true even on x86, where the caller
      of kvm_exit() is not kvm.ko, but is instead a dependent module, kvm_amd.ko
      or kvm_intel.ko, as kvm_chardev_ops.owner is set to the module that calls
      kvm_init(), not hardcoded to the base kvm.ko module.
      Signed-off-by: default avatarIsaku Yamahata <isaku.yamahata@intel.com>
      [sean: rework changelog]
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-46-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      667a83bf
    • Isaku Yamahata's avatar
      KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock · 0bf50497
      Isaku Yamahata authored
      Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock now
      that KVM hooks CPU hotplug during the ONLINE phase, which can sleep.
      Previously, KVM hooked the STARTING phase, which is not allowed to sleep
      and thus could not take kvm_lock (a mutex).  This effectively allows the
      task that's initiating hardware enabling/disabling to preempted and/or
      migrated.
      
      Note, the Documentation/virt/kvm/locking.rst statement that kvm_count_lock
      is "raw" because hardware enabling/disabling needs to be atomic with
      respect to migration is wrong on multiple fronts.  First, while regular
      spinlocks can be preempted, the task holding the lock cannot be migrated.
      Second, preventing migration is not required.  on_each_cpu() disables
      preemption, which ensures that cpus_hardware_enabled correctly reflects
      hardware state.  The task may be preempted/migrated between bumping
      kvm_usage_count and invoking on_each_cpu(), but that's perfectly ok as
      kvm_usage_count is still protected, e.g. other tasks that call
      hardware_enable_all() will be blocked until the preempted/migrated owner
      exits its critical section.
      
      KVM does have lockless accesses to kvm_usage_count in the suspend/resume
      flows, but those are safe because all tasks must be frozen prior to
      suspending CPUs, and a task cannot be frozen while it holds one or more
      locks (userspace tasks are frozen via a fake signal).
      
      Preemption doesn't need to be explicitly disabled in the hotplug path.
      The hotplug thread is pinned to the CPU that's being hotplugged, and KVM
      only cares about having a stable CPU, i.e. to ensure hardware is enabled
      on the correct CPU.  Lockep, i.e. check_preemption_disabled(), plays nice
      with this state too, as is_percpu_thread() is true for the hotplug thread.
      Signed-off-by: default avatarIsaku Yamahata <isaku.yamahata@intel.com>
      Co-developed-by: default avatarSean Christopherson <seanjc@google.com>
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-45-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      0bf50497
    • Sean Christopherson's avatar
      KVM: Ensure CPU is stable during low level hardware enable/disable · 2c106f29
      Sean Christopherson authored
      Use the non-raw smp_processor_id() in the low hardware enable/disable
      helpers as KVM absolutely relies on the CPU being stable, e.g. KVM would
      end up with incorrect state if the task were migrated between accessing
      cpus_hardware_enabled and actually enabling/disabling hardware.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-44-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      2c106f29
    • Chao Gao's avatar
      KVM: Disable CPU hotplug during hardware enabling/disabling · e4aa7f88
      Chao Gao authored
      Disable CPU hotplug when enabling/disabling hardware to prevent the
      corner case where if the following sequence occurs:
      
        1. A hotplugged CPU marks itself online in cpu_online_mask
        2. The hotplugged CPU enables interrupt before invoking KVM's ONLINE
           callback
        3  hardware_{en,dis}able_all() is invoked on another CPU
      
      the hotplugged CPU will be included in on_each_cpu() and thus get sent
      through hardware_{en,dis}able_nolock() before kvm_online_cpu() is called.
      
              start_secondary { ...
                      set_cpu_online(smp_processor_id(), true); <- 1
                      ...
                      local_irq_enable();  <- 2
                      ...
                      cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); <- 3
              }
      
      KVM currently fudges around this race by keeping track of which CPUs have
      done hardware enabling (see commit 1b6c0168 "KVM: Keep track of which
      cpus have virtualization enabled"), but that's an inefficient, convoluted,
      and hacky solution.
      Signed-off-by: default avatarChao Gao <chao.gao@intel.com>
      [sean: split to separate patch, write changelog]
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-43-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      e4aa7f88
    • Chao Gao's avatar
      KVM: Rename and move CPUHP_AP_KVM_STARTING to ONLINE section · aaf12a7b
      Chao Gao authored
      The CPU STARTING section doesn't allow callbacks to fail. Move KVM's
      hotplug callback to ONLINE section so that it can abort onlining a CPU in
      certain cases to avoid potentially breaking VMs running on existing CPUs.
      For example, when KVM fails to enable hardware virtualization on the
      hotplugged CPU.
      
      Place KVM's hotplug state before CPUHP_AP_SCHED_WAIT_EMPTY as it ensures
      when offlining a CPU, all user tasks and non-pinned kernel tasks have left
      the CPU, i.e. there cannot be a vCPU task around. So, it is safe for KVM's
      CPU offline callback to disable hardware virtualization at that point.
      Likewise, KVM's online callback can enable hardware virtualization before
      any vCPU task gets a chance to run on hotplugged CPUs.
      
      Drop kvm_x86_check_processor_compatibility()'s WARN that IRQs are
      disabled, as the ONLINE section runs with IRQs disabled.  The WARN wasn't
      intended to be a requirement, e.g. disabling preemption is sufficient,
      the IRQ thing was purely an aggressive sanity check since the helper was
      only ever invoked via SMP function call.
      
      Rename KVM's CPU hotplug callbacks accordingly.
      Suggested-by: default avatarThomas Gleixner <tglx@linutronix.de>
      Signed-off-by: default avatarChao Gao <chao.gao@intel.com>
      Signed-off-by: default avatarIsaku Yamahata <isaku.yamahata@intel.com>
      Reviewed-by: default avatarYuan Yao <yuan.yao@intel.com>
      [sean: drop WARN that IRQs are disabled]
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-42-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      aaf12a7b
    • Chao Gao's avatar
      KVM: x86: Do compatibility checks when onlining CPU · c82a5c5c
      Chao Gao authored
      Do compatibility checks when enabling hardware to effectively add
      compatibility checks when onlining a CPU.  Abort enabling, i.e. the
      online process, if the (hotplugged) CPU is incompatible with the known
      good setup.
      
      At init time, KVM does compatibility checks to ensure that all online
      CPUs support hardware virtualization and a common set of features. But
      KVM uses hotplugged CPUs without such compatibility checks. On Intel
      CPUs, this leads to #GP if the hotplugged CPU doesn't support VMX, or
      VM-Entry failure if the hotplugged CPU doesn't support all features
      enabled by KVM.
      
      Note, this is little more than a NOP on SVM, as SVM already checks for
      full SVM support during hardware enabling.
      
      Opportunistically add a pr_err() if setup_vmcs_config() fails, and
      tweak all error messages to output which CPU failed.
      Signed-off-by: default avatarChao Gao <chao.gao@intel.com>
      Co-developed-by: default avatarSean Christopherson <seanjc@google.com>
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Acked-by: default avatarKai Huang <kai.huang@intel.com>
      Message-Id: <20221130230934.1014142-41-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      c82a5c5c
    • Sean Christopherson's avatar
      KVM: x86: Move CPU compat checks hook to kvm_x86_ops (from kvm_x86_init_ops) · d83420c2
      Sean Christopherson authored
      Move the .check_processor_compatibility() callback from kvm_x86_init_ops
      to kvm_x86_ops to allow a future patch to do compatibility checks during
      CPU hotplug.
      
      Do kvm_ops_update() before compat checks so that static_call() can be
      used during compat checks.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarKai Huang <kai.huang@intel.com>
      Message-Id: <20221130230934.1014142-40-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      d83420c2
    • Sean Christopherson's avatar
      KVM: SVM: Check for SVM support in CPU compatibility checks · 325fc957
      Sean Christopherson authored
      Check that SVM is supported and enabled in the processor compatibility
      checks.  SVM already checks for support during hardware enabling,
      i.e. this doesn't really add new functionality.  The net effect is that
      KVM will refuse to load if a CPU doesn't have SVM fully enabled, as
      opposed to failing KVM_CREATE_VM.
      
      Opportunistically move svm_check_processor_compat() up in svm.c so that
      it can be invoked during hardware enabling in a future patch.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-39-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      325fc957
    • Sean Christopherson's avatar
      KVM: VMX: Shuffle support checks and hardware enabling code around · 8504ef21
      Sean Christopherson authored
      Reorder code in vmx.c so that the VMX support check helpers reside above
      the hardware enabling helpers, which will allow KVM to perform support
      checks during hardware enabling (in a future patch).
      
      No functional change intended.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-38-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      8504ef21
    • Sean Christopherson's avatar
      KVM: x86: Do VMX/SVM support checks directly in vendor code · d4193132
      Sean Christopherson authored
      Do basic VMX/SVM support checks directly in vendor code instead of
      implementing them via kvm_x86_ops hooks.  Beyond the superficial benefit
      of providing common messages, which isn't even clearly a net positive
      since vendor code can provide more precise/detailed messages, there's
      zero advantage to bouncing through common x86 code.
      
      Consolidating the checks will also simplify performing the checks
      across all CPUs (in a future patch).
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-37-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      d4193132
    • Sean Christopherson's avatar
      KVM: VMX: Use current CPU's info to perform "disabled by BIOS?" checks · 462689b3
      Sean Christopherson authored
      Use this_cpu_has() instead of boot_cpu_has() to perform the effective
      "disabled by BIOS?" checks for VMX.  This will allow consolidating code
      between vmx_disabled_by_bios() and vmx_check_processor_compat().
      
      Checking the boot CPU isn't a strict requirement as any divergence in VMX
      enabling between the boot CPU and other CPUs will result in KVM refusing
      to load thanks to the aforementioned vmx_check_processor_compat().
      
      Furthermore, using the boot CPU was an unintentional change introduced by
      commit a4d0b2fd ("KVM: VMX: Use VMX feature flag to query BIOS
      enabling").  Prior to using the feature flags, KVM checked the raw MSR
      value from the current CPU.
      Reported-by: default avatarKai Huang <kai.huang@intel.com>
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarKai Huang <kai.huang@intel.com>
      Message-Id: <20221130230934.1014142-36-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      462689b3
    • Sean Christopherson's avatar
      KVM: x86: Unify pr_fmt to use module name for all KVM modules · 8d20bd63
      Sean Christopherson authored
      Define pr_fmt using KBUILD_MODNAME for all KVM x86 code so that printks
      use consistent formatting across common x86, Intel, and AMD code.  In
      addition to providing consistent print formatting, using KBUILD_MODNAME,
      e.g. kvm_amd and kvm_intel, allows referencing SVM and VMX (and SEV and
      SGX and ...) as technologies without generating weird messages, and
      without causing naming conflicts with other kernel code, e.g. "SEV: ",
      "tdx: ", "sgx: " etc.. are all used by the kernel for non-KVM subsystems.
      
      Opportunistically move away from printk() for prints that need to be
      modified anyways, e.g. to drop a manual "kvm: " prefix.
      
      Opportunistically convert a few SGX WARNs that are similarly modified to
      WARN_ONCE; in the very unlikely event that the WARNs fire, odds are good
      that they would fire repeatedly and spam the kernel log without providing
      unique information in each print.
      
      Note, defining pr_fmt yields undesirable results for code that uses KVM's
      printk wrappers, e.g. vcpu_unimpl().  But, that's a pre-existing problem
      as SVM/kvm_amd already defines a pr_fmt, and thankfully use of KVM's
      wrappers is relatively limited in KVM x86 code.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarPaul Durrant <paul@xen.org>
      Message-Id: <20221130230934.1014142-35-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      8d20bd63
    • Sean Christopherson's avatar
      KVM: x86: Use KBUILD_MODNAME to specify vendor module name · 08a9d59c
      Sean Christopherson authored
      Use KBUILD_MODNAME to specify the vendor module name instead of manually
      writing out the name to make it a bit more obvious that the name isn't
      completely arbitrary.  A future patch will also use KBUILD_MODNAME to
      define pr_fmt, at which point using KBUILD_MODNAME for kvm_x86_ops.name
      further reinforces the intended usage of kvm_x86_ops.name.
      
      No functional change intended.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-34-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      08a9d59c
    • Sean Christopherson's avatar
      KVM: Drop kvm_arch_check_processor_compat() hook · 81a1cf9f
      Sean Christopherson authored
      Drop kvm_arch_check_processor_compat() and its support code now that all
      architecture implementations are nops.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarPhilippe Mathieu-Daudé <philmd@linaro.org>
      Reviewed-by: Eric Farman <farman@linux.ibm.com>	# s390
      Acked-by: default avatarAnup Patel <anup@brainfault.org>
      Reviewed-by: default avatarKai Huang <kai.huang@intel.com>
      Message-Id: <20221130230934.1014142-33-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      81a1cf9f
    • Sean Christopherson's avatar
      KVM: x86: Do CPU compatibility checks in x86 code · 3045c483
      Sean Christopherson authored
      Move the CPU compatibility checks to pure x86 code, i.e. drop x86's use
      of the common kvm_x86_check_cpu_compat() arch hook.  x86 is the only
      architecture that "needs" to do per-CPU compatibility checks, moving
      the logic to x86 will allow dropping the common code, and will also
      give x86 more control over when/how the compatibility checks are
      performed, e.g. TDX will need to enable hardware (do VMXON) in order to
      perform compatibility checks.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarIsaku Yamahata <isaku.yamahata@intel.com>
      Reviewed-by: default avatarKai Huang <kai.huang@intel.com>
      Message-Id: <20221130230934.1014142-32-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      3045c483
    • Sean Christopherson's avatar
      KVM: VMX: Make VMCS configuration/capabilities structs read-only after init · 58ca1930
      Sean Christopherson authored
      Tag vmcs_config and vmx_capability structs as __init, the canonical
      configuration is generated during hardware_setup() and must never be
      modified after that point.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-31-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      58ca1930
    • Sean Christopherson's avatar
      KVM: Drop kvm_arch_{init,exit}() hooks · a578a0a9
      Sean Christopherson authored
      Drop kvm_arch_init() and kvm_arch_exit() now that all implementations
      are nops.
      
      No functional change intended.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: Eric Farman <farman@linux.ibm.com>	# s390
      Reviewed-by: default avatarPhilippe Mathieu-Daudé <philmd@linaro.org>
      Acked-by: default avatarAnup Patel <anup@brainfault.org>
      Message-Id: <20221130230934.1014142-30-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      a578a0a9
    • Sean Christopherson's avatar
      KVM: s390: Mark __kvm_s390_init() and its descendants as __init · 6c30cd2e
      Sean Christopherson authored
      Tag __kvm_s390_init() and its unique helpers as __init.  These functions
      are only ever called during module_init(), but could not be tagged
      accordingly while they were invoked from the common kvm_arch_init(),
      which is not __init because of x86.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarEric Farman <farman@linux.ibm.com>
      Message-Id: <20221130230934.1014142-29-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      6c30cd2e
    • Sean Christopherson's avatar
      KVM: s390: Do s390 specific init without bouncing through kvm_init() · b8449265
      Sean Christopherson authored
      Move the guts of kvm_arch_init() into a new helper, __kvm_s390_init(),
      and invoke the new helper directly from kvm_s390_init() instead of
      bouncing through kvm_init().  Invoking kvm_arch_init() is the very
      first action performed by kvm_init(), i.e. this is a glorified nop.
      
      Moving setup to __kvm_s390_init() will allow tagging more functions as
      __init, and emptying kvm_arch_init() will allow dropping the hook
      entirely once all architecture implementations are nops.
      
      No functional change intended.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarEric Farman <farman@linux.ibm.com>
      Reviewed-by: default avatarPhilippe Mathieu-Daudé <philmd@linaro.org>
      Message-Id: <20221130230934.1014142-28-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      b8449265
    • Sean Christopherson's avatar
      KVM: PPC: Move processor compatibility check to module init · ae19b15d
      Sean Christopherson authored
      Move KVM PPC's compatibility checks to their respective module_init()
      hooks, there's no need to wait until KVM's common compat check, nor is
      there a need to perform the check on every CPU (provided by common KVM's
      hook), as the compatibility checks operate on global data.
      
        arch/powerpc/include/asm/cputable.h: extern struct cpu_spec *cur_cpu_spec;
        arch/powerpc/kvm/book3s.c: return 0
        arch/powerpc/kvm/e500.c: strcmp(cur_cpu_spec->cpu_name, "e500v2")
        arch/powerpc/kvm/e500mc.c: strcmp(cur_cpu_spec->cpu_name, "e500mc")
                                   strcmp(cur_cpu_spec->cpu_name, "e5500")
                                   strcmp(cur_cpu_spec->cpu_name, "e6500")
      
      Cc: Fabiano Rosas <farosas@linux.ibm.com>
      Cc: Michael Ellerman <mpe@ellerman.id.au>
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Acked-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc)
      Message-Id: <20221130230934.1014142-27-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      ae19b15d
    • Sean Christopherson's avatar
      KVM: RISC-V: Tag init functions and data with __init, __ro_after_init · 45b66dc1
      Sean Christopherson authored
      Now that KVM setup is handled directly in riscv_kvm_init(), tag functions
      and data that are used/set only during init with __init/__ro_after_init.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Acked-by: default avatarAnup Patel <anup@brainfault.org>
      Message-Id: <20221130230934.1014142-26-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      45b66dc1
    • Sean Christopherson's avatar
      KVM: RISC-V: Do arch init directly in riscv_kvm_init() · 20deee32
      Sean Christopherson authored
      Fold the guts of kvm_arch_init() into riscv_kvm_init() instead of
      bouncing through kvm_init()=>kvm_arch_init().  Functionally, this is a
      glorified nop as invoking kvm_arch_init() is the very first action
      performed by kvm_init().
      
      Moving setup to riscv_kvm_init(), which is tagged __init, will allow
      tagging more functions and data with __init and __ro_after_init.  And
      emptying kvm_arch_init() will allow dropping the hook entirely once all
      architecture implementations are nops.
      
      No functional change intended.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarPhilippe Mathieu-Daudé <philmd@linaro.org>
      Acked-by: default avatarAnup Patel <anup@brainfault.org>
      Message-Id: <20221130230934.1014142-25-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      20deee32
    • Sean Christopherson's avatar
      KVM: MIPS: Register die notifier prior to kvm_init() · eed9fcdf
      Sean Christopherson authored
      Call kvm_init() only after _all_ setup is complete, as kvm_init() exposes
      /dev/kvm to userspace and thus allows userspace to create VMs (and call
      other ioctls).
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarPhilippe Mathieu-Daudé <philmd@linaro.org>
      Message-Id: <20221130230934.1014142-24-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      eed9fcdf
    • Sean Christopherson's avatar
      KVM: MIPS: Setup VZ emulation? directly from kvm_mips_init() · 3fb8e89a
      Sean Christopherson authored
      Invoke kvm_mips_emulation_init() directly from kvm_mips_init() instead
      of bouncing through kvm_init()=>kvm_arch_init().  Functionally, this is
      a glorified nop as invoking kvm_arch_init() is the very first action
      performed by kvm_init().
      
      Emptying kvm_arch_init() will allow dropping the hook entirely once all
      architecture implementations are nops.
      
      No functional change intended.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarPhilippe Mathieu-Daudé <philmd@linaro.org>
      Message-Id: <20221130230934.1014142-23-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      3fb8e89a
    • Sean Christopherson's avatar
      KVM: MIPS: Hardcode callbacks to hardware virtualization extensions · 1cfc1c7b
      Sean Christopherson authored
      Now that KVM no longer supports trap-and-emulate (see commit 45c7e8af
      "MIPS: Remove KVM_TE support"), hardcode the MIPS callbacks to the
      virtualization callbacks.
      
      Harcoding the callbacks eliminates the technically-unnecessary check on
      non-NULL kvm_mips_callbacks in kvm_arch_init().  MIPS has never supported
      multiple in-tree modules, i.e. barring an out-of-tree module, where
      copying and renaming kvm.ko counts as "out-of-tree", KVM could never
      encounter a non-NULL set of callbacks during module init.
      
      The callback check is also subtly broken, as it is not thread safe,
      i.e. if there were multiple modules, loading both concurrently would
      create a race between checking and setting kvm_mips_callbacks.
      
      Given that out-of-tree shenanigans are not the kernel's responsibility,
      hardcode the callbacks to simplify the code.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarPhilippe Mathieu-Daudé <philmd@linaro.org>
      Message-Id: <20221130230934.1014142-22-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      1cfc1c7b
    • Sean Christopherson's avatar
      KVM: arm64: Mark kvm_arm_init() and its unique descendants as __init · 53bf620a
      Sean Christopherson authored
      Tag kvm_arm_init() and its unique helper as __init, and tag data that is
      only ever modified under the kvm_arm_init() umbrella as read-only after
      init.
      
      Opportunistically name the boolean param in kvm_timer_hyp_init()'s
      prototype to match its definition.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-21-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      53bf620a
    • Sean Christopherson's avatar
      KVM: arm64: Do arm/arch initialization without bouncing through kvm_init() · 1dc0f02d
      Sean Christopherson authored
      Do arm/arch specific initialization directly in arm's module_init(), now
      called kvm_arm_init(), instead of bouncing through kvm_init() to reach
      kvm_arch_init().  Invoking kvm_arch_init() is the very first action
      performed by kvm_init(), so from a initialization perspective this is a
      glorified nop.
      
      Avoiding kvm_arch_init() also fixes a mostly benign bug as kvm_arch_exit()
      doesn't properly unwind if a later stage of kvm_init() fails.  While the
      soon-to-be-deleted comment about compiling as a module being unsupported
      is correct, kvm_arch_exit() can still be called by kvm_init() if any step
      after the call to kvm_arch_init() succeeds.
      
      Add a FIXME to call out that pKVM initialization isn't unwound if
      kvm_init() fails, which is a pre-existing problem inherited from
      kvm_arch_exit().
      
      Making kvm_arch_init() a nop will also allow dropping kvm_arch_init() and
      kvm_arch_exit() entirely once all other architectures follow suit.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-20-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      1dc0f02d
    • Sean Christopherson's avatar
      KVM: arm64: Unregister perf callbacks if hypervisor finalization fails · 78b3bf48
      Sean Christopherson authored
      Undo everything done by init_subsystems() if a later initialization step
      fails, i.e. unregister perf callbacks in addition to unregistering the
      power management notifier.
      
      Fixes: bfa79a80 ("KVM: arm64: Elevate hypervisor mappings creation at EL2")
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-19-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      78b3bf48
    • Sean Christopherson's avatar
      KVM: arm64: Free hypervisor allocations if vector slot init fails · 6baaeda8
      Sean Christopherson authored
      Teardown hypervisor mode if vector slot setup fails in order to avoid
      leaking any allocations done by init_hyp_mode().
      
      Fixes: b881cdce ("KVM: arm64: Allocate hyp vectors statically")
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-18-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      6baaeda8
    • Marc Zyngier's avatar
      KVM: arm64: Simplify the CPUHP logic · 466d27e4
      Marc Zyngier authored
      For a number of historical reasons, the KVM/arm64 hotplug setup is pretty
      complicated, and we have two extra CPUHP notifiers for vGIC and timers.
      
      It looks pretty pointless, and gets in the way of further changes.
      So let's just expose some helpers that can be called from the core
      CPUHP callback, and get rid of everything else.
      
      This gives us the opportunity to drop a useless notifier entry,
      as well as tidy-up the timer enable/disable, which was a bit odd.
      Signed-off-by: default avatarMarc Zyngier <maz@kernel.org>
      Signed-off-by: default avatarIsaku Yamahata <isaku.yamahata@intel.com>
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-17-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      466d27e4
    • Sean Christopherson's avatar
      KVM: x86: Serialize vendor module initialization (hardware setup) · 3af4a9e6
      Sean Christopherson authored
      Acquire a new mutex, vendor_module_lock, in kvm_x86_vendor_init() while
      doing hardware setup to ensure that concurrent calls are fully serialized.
      KVM rejects attempts to load vendor modules if a different module has
      already been loaded, but doesn't handle the case where multiple vendor
      modules are loaded at the same time, and module_init() doesn't run under
      the global module_mutex.
      
      Note, in practice, this is likely a benign bug as no platform exists that
      supports both SVM and VMX, i.e. barring a weird VM setup, one of the
      vendor modules is guaranteed to fail a support check before modifying
      common KVM state.
      
      Alternatively, KVM could perform an atomic CMPXCHG on .hardware_enable,
      but that comes with its own ugliness as it would require setting
      .hardware_enable before success is guaranteed, e.g. attempting to load
      the "wrong" could result in spurious failure to load the "right" module.
      
      Introduce a new mutex as using kvm_lock is extremely deadlock prone due
      to kvm_lock being taken under cpus_write_lock(), and in the future, under
      under cpus_read_lock().  Any operation that takes cpus_read_lock() while
      holding kvm_lock would potentially deadlock, e.g. kvm_timer_init() takes
      cpus_read_lock() to register a callback.  In theory, KVM could avoid
      such problematic paths, i.e. do less setup under kvm_lock, but avoiding
      all calls to cpus_read_lock() is subtly difficult and thus fragile.  E.g.
      updating static calls also acquires cpus_read_lock().
      
      Inverting the lock ordering, i.e. always taking kvm_lock outside
      cpus_read_lock(), is not a viable option as kvm_lock is taken in various
      callbacks that may be invoked under cpus_read_lock(), e.g. x86's
      kvmclock_cpufreq_notifier().
      
      The lockdep splat below is dependent on future patches to take
      cpus_read_lock() in hardware_enable_all(), but as above, deadlock is
      already is already possible.
      
        ======================================================
        WARNING: possible circular locking dependency detected
        6.0.0-smp--7ec93244f194-init2 #27 Tainted: G           O
        ------------------------------------------------------
        stable/251833 is trying to acquire lock:
        ffffffffc097ea28 (kvm_lock){+.+.}-{3:3}, at: hardware_enable_all+0x1f/0xc0 [kvm]
      
                     but task is already holding lock:
        ffffffffa2456828 (cpu_hotplug_lock){++++}-{0:0}, at: hardware_enable_all+0xf/0xc0 [kvm]
      
                     which lock already depends on the new lock.
      
                     the existing dependency chain (in reverse order) is:
      
                     -> #1 (cpu_hotplug_lock){++++}-{0:0}:
               cpus_read_lock+0x2a/0xa0
               __cpuhp_setup_state+0x2b/0x60
               __kvm_x86_vendor_init+0x16a/0x1870 [kvm]
               kvm_x86_vendor_init+0x23/0x40 [kvm]
               0xffffffffc0a4d02b
               do_one_initcall+0x110/0x200
               do_init_module+0x4f/0x250
               load_module+0x1730/0x18f0
               __se_sys_finit_module+0xca/0x100
               __x64_sys_finit_module+0x1d/0x20
               do_syscall_64+0x3d/0x80
               entry_SYSCALL_64_after_hwframe+0x63/0xcd
      
                     -> #0 (kvm_lock){+.+.}-{3:3}:
               __lock_acquire+0x16f4/0x30d0
               lock_acquire+0xb2/0x190
               __mutex_lock+0x98/0x6f0
               mutex_lock_nested+0x1b/0x20
               hardware_enable_all+0x1f/0xc0 [kvm]
               kvm_dev_ioctl+0x45e/0x930 [kvm]
               __se_sys_ioctl+0x77/0xc0
               __x64_sys_ioctl+0x1d/0x20
               do_syscall_64+0x3d/0x80
               entry_SYSCALL_64_after_hwframe+0x63/0xcd
      
                     other info that might help us debug this:
      
         Possible unsafe locking scenario:
      
               CPU0                    CPU1
               ----                    ----
          lock(cpu_hotplug_lock);
                                       lock(kvm_lock);
                                       lock(cpu_hotplug_lock);
          lock(kvm_lock);
      
                      *** DEADLOCK ***
      
        1 lock held by stable/251833:
         #0: ffffffffa2456828 (cpu_hotplug_lock){++++}-{0:0}, at: hardware_enable_all+0xf/0xc0 [kvm]
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-16-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      3af4a9e6
    • Sean Christopherson's avatar
      KVM: VMX: Do _all_ initialization before exposing /dev/kvm to userspace · e32b1200
      Sean Christopherson authored
      Call kvm_init() only after _all_ setup is complete, as kvm_init() exposes
      /dev/kvm to userspace and thus allows userspace to create VMs (and call
      other ioctls).  E.g. KVM will encounter a NULL pointer when attempting to
      add a vCPU to the per-CPU loaded_vmcss_on_cpu list if userspace is able to
      create a VM before vmx_init() configures said list.
      
       BUG: kernel NULL pointer dereference, address: 0000000000000008
       #PF: supervisor write access in kernel mode
       #PF: error_code(0x0002) - not-present page
       PGD 0 P4D 0
       Oops: 0002 [#1] SMP
       CPU: 6 PID: 1143 Comm: stable Not tainted 6.0.0-rc7+ #988
       Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
       RIP: 0010:vmx_vcpu_load_vmcs+0x68/0x230 [kvm_intel]
        <TASK>
        vmx_vcpu_load+0x16/0x60 [kvm_intel]
        kvm_arch_vcpu_load+0x32/0x1f0 [kvm]
        vcpu_load+0x2f/0x40 [kvm]
        kvm_arch_vcpu_create+0x231/0x310 [kvm]
        kvm_vm_ioctl+0x79f/0xe10 [kvm]
        ? handle_mm_fault+0xb1/0x220
        __x64_sys_ioctl+0x80/0xb0
        do_syscall_64+0x2b/0x50
        entry_SYSCALL_64_after_hwframe+0x46/0xb0
       RIP: 0033:0x7f5a6b05743b
        </TASK>
       Modules linked in: vhost_net vhost vhost_iotlb tap kvm_intel(+) kvm irqbypass
      
      Cc: stable@vger.kernel.org
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-15-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      e32b1200
    • Sean Christopherson's avatar
      KVM: x86: Move guts of kvm_arch_init() to standalone helper · 4f8396b9
      Sean Christopherson authored
      Move the guts of kvm_arch_init() to a new helper, kvm_x86_vendor_init(),
      so that VMX can do _all_ arch and vendor initialization before calling
      kvm_init().  Calling kvm_init() must be the _very_ last step during init,
      as kvm_init() exposes /dev/kvm to userspace, i.e. allows creating VMs.
      
      No functional change intended.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Message-Id: <20221130230934.1014142-14-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      4f8396b9
    • Sean Christopherson's avatar
      KVM: VMX: Move Hyper-V eVMCS initialization to helper · 451d39e8
      Sean Christopherson authored
      Move Hyper-V's eVMCS initialization to a dedicated helper to clean up
      vmx_init(), and add a comment to call out that the Hyper-V init code
      doesn't need to be unwound if vmx_init() ultimately fails.
      
      No functional change intended.
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      Reviewed-by: default avatarVitaly Kuznetsov <vkuznets@redhat.com>
      Message-Id: <20221130230934.1014142-13-seanjc@google.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      451d39e8