// SPDX-License-Identifier: GPL-2.0-only1#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt23#include <linux/kvm_host.h>4#include <linux/kvm_irqfd.h>56#include <asm/irq_remapping.h>7#include <asm/cpu.h>89#include "lapic.h"10#include "irq.h"11#include "posted_intr.h"12#include "trace.h"13#include "vmx.h"14#include "tdx.h"1516/*17* Maintain a per-CPU list of vCPUs that need to be awakened by wakeup_handler()18* when a WAKEUP_VECTOR interrupted is posted. vCPUs are added to the list when19* the vCPU is scheduled out and is blocking (e.g. in HLT) with IRQs enabled.20* The vCPUs posted interrupt descriptor is updated at the same time to set its21* notification vector to WAKEUP_VECTOR, so that posted interrupt from devices22* wake the target vCPUs. vCPUs are removed from the list and the notification23* vector is reset when the vCPU is scheduled in.24*/25static DEFINE_PER_CPU(struct list_head, wakeup_vcpus_on_cpu);26/*27* Protect the per-CPU list with a per-CPU spinlock to handle task migration.28* When a blocking vCPU is awakened _and_ migrated to a different pCPU, the29* ->sched_in() path will need to take the vCPU off the list of the _previous_30* CPU. IRQs must be disabled when taking this lock, otherwise deadlock will31* occur if a wakeup IRQ arrives and attempts to acquire the lock.32*/33static DEFINE_PER_CPU(raw_spinlock_t, wakeup_vcpus_on_cpu_lock);3435#define PI_LOCK_SCHED_OUT SINGLE_DEPTH_NESTING3637static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)38{39return &(to_vt(vcpu)->pi_desc);40}4142static int pi_try_set_control(struct pi_desc *pi_desc, u64 *pold, u64 new)43{44/*45* PID.ON can be set at any time by a different vCPU or by hardware,46* e.g. a device. PID.control must be written atomically, and the47* update must be retried with a fresh snapshot an ON change causes48* the cmpxchg to fail.49*/50if (!try_cmpxchg64(&pi_desc->control, pold, new))51return -EBUSY;5253return 0;54}5556void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)57{58struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);59struct vcpu_vt *vt = to_vt(vcpu);60struct pi_desc old, new;61unsigned long flags;62unsigned int dest;6364/*65* To simplify hot-plug and dynamic toggling of APICv, keep PI.NDST and66* PI.SN up-to-date even if there is no assigned device or if APICv is67* deactivated due to a dynamic inhibit bit, e.g. for Hyper-V's SyncIC.68*/69if (!enable_apicv || !lapic_in_kernel(vcpu))70return;7172/*73* If the vCPU wasn't on the wakeup list and wasn't migrated, then the74* full update can be skipped as neither the vector nor the destination75* needs to be changed. Clear SN even if there is no assigned device,76* again for simplicity.77*/78if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR && vcpu->cpu == cpu) {79if (pi_test_and_clear_sn(pi_desc))80goto after_clear_sn;81return;82}8384local_irq_save(flags);8586/*87* If the vCPU was waiting for wakeup, remove the vCPU from the wakeup88* list of the _previous_ pCPU, which will not be the same as the89* current pCPU if the task was migrated.90*/91if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR) {92raw_spinlock_t *spinlock = &per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu);9394/*95* In addition to taking the wakeup lock for the regular/IRQ96* context, tell lockdep it is being taken for the "sched out"97* context as well. vCPU loads happens in task context, and98* this is taking the lock of the *previous* CPU, i.e. can race99* with both the scheduler and the wakeup handler.100*/101raw_spin_lock(spinlock);102spin_acquire(&spinlock->dep_map, PI_LOCK_SCHED_OUT, 0, _RET_IP_);103list_del(&vt->pi_wakeup_list);104spin_release(&spinlock->dep_map, _RET_IP_);105raw_spin_unlock(spinlock);106}107108dest = cpu_physical_id(cpu);109if (!x2apic_mode)110dest = (dest << 8) & 0xFF00;111112old.control = READ_ONCE(pi_desc->control);113do {114new.control = old.control;115116/*117* Clear SN (as above) and refresh the destination APIC ID to118* handle task migration (@cpu != vcpu->cpu).119*/120new.ndst = dest;121__pi_clear_sn(&new);122123/*124* Restore the notification vector; in the blocking case, the125* descriptor was modified on "put" to use the wakeup vector.126*/127new.nv = POSTED_INTR_VECTOR;128} while (pi_try_set_control(pi_desc, &old.control, new.control));129130local_irq_restore(flags);131132after_clear_sn:133134/*135* Clear SN before reading the bitmap. The VT-d firmware136* writes the bitmap and reads SN atomically (5.2.3 in the137* spec), so it doesn't really have a memory barrier that138* pairs with this, but we cannot do that and we need one.139*/140smp_mb__after_atomic();141142if (!pi_is_pir_empty(pi_desc))143pi_set_on(pi_desc);144}145146static bool vmx_can_use_vtd_pi(struct kvm *kvm)147{148/*149* Note, reading the number of possible bypass IRQs can race with a150* bypass IRQ being attached to the VM. vmx_pi_start_bypass() ensures151* blockng vCPUs will see an elevated count or get KVM_REQ_UNBLOCK.152*/153return irqchip_in_kernel(kvm) && kvm_arch_has_irq_bypass() &&154READ_ONCE(kvm->arch.nr_possible_bypass_irqs);155}156157/*158* Put the vCPU on this pCPU's list of vCPUs that needs to be awakened and set159* WAKEUP as the notification vector in the PI descriptor.160*/161static void pi_enable_wakeup_handler(struct kvm_vcpu *vcpu)162{163struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);164struct vcpu_vt *vt = to_vt(vcpu);165struct pi_desc old, new;166167lockdep_assert_irqs_disabled();168169/*170* Acquire the wakeup lock using the "sched out" context to workaround171* a lockdep false positive. When this is called, schedule() holds172* various per-CPU scheduler locks. When the wakeup handler runs, it173* holds this CPU's wakeup lock while calling try_to_wake_up(), which174* can eventually take the aforementioned scheduler locks, which causes175* lockdep to assume there is deadlock.176*177* Deadlock can't actually occur because IRQs are disabled for the178* entirety of the sched_out critical section, i.e. the wakeup handler179* can't run while the scheduler locks are held.180*/181raw_spin_lock_nested(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu),182PI_LOCK_SCHED_OUT);183list_add_tail(&vt->pi_wakeup_list,184&per_cpu(wakeup_vcpus_on_cpu, vcpu->cpu));185raw_spin_unlock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu));186187WARN(pi_test_sn(pi_desc), "PI descriptor SN field set before blocking");188189old.control = READ_ONCE(pi_desc->control);190do {191/* set 'NV' to 'wakeup vector' */192new.control = old.control;193new.nv = POSTED_INTR_WAKEUP_VECTOR;194} while (pi_try_set_control(pi_desc, &old.control, new.control));195196/*197* Send a wakeup IPI to this CPU if an interrupt may have been posted198* before the notification vector was updated, in which case the IRQ199* will arrive on the non-wakeup vector. An IPI is needed as calling200* try_to_wake_up() from ->sched_out() isn't allowed (IRQs are not201* enabled until it is safe to call try_to_wake_up() on the task being202* scheduled out).203*/204if (pi_test_on(&new))205__apic_send_IPI_self(POSTED_INTR_WAKEUP_VECTOR);206}207208static bool vmx_needs_pi_wakeup(struct kvm_vcpu *vcpu)209{210/*211* The default posted interrupt vector does nothing when212* invoked outside guest mode. Return whether a blocked vCPU213* can be the target of posted interrupts, as is the case when214* using either IPI virtualization or VT-d PI, so that the215* notification vector is switched to the one that calls216* back to the pi_wakeup_handler() function.217*/218return (vmx_can_use_ipiv(vcpu) && !is_td_vcpu(vcpu)) ||219vmx_can_use_vtd_pi(vcpu->kvm);220}221222void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)223{224struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);225226if (!vmx_needs_pi_wakeup(vcpu))227return;228229/*230* If the vCPU is blocking with IRQs enabled and ISN'T being preempted,231* enable the wakeup handler so that notification IRQ wakes the vCPU as232* expected. There is no need to enable the wakeup handler if the vCPU233* is preempted between setting its wait state and manually scheduling234* out, as the task is still runnable, i.e. doesn't need a wake event235* from KVM to be scheduled in.236*237* If the wakeup handler isn't being enabled, Suppress Notifications as238* the cost of propagating PIR.IRR to PID.ON is negligible compared to239* the cost of a spurious IRQ, and vCPU put/load is a slow path.240*/241if (!vcpu->preempted && kvm_vcpu_is_blocking(vcpu) &&242((is_td_vcpu(vcpu) && tdx_interrupt_allowed(vcpu)) ||243(!is_td_vcpu(vcpu) && !vmx_interrupt_blocked(vcpu))))244pi_enable_wakeup_handler(vcpu);245else246pi_set_sn(pi_desc);247}248249/*250* Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.251*/252void pi_wakeup_handler(void)253{254int cpu = smp_processor_id();255struct list_head *wakeup_list = &per_cpu(wakeup_vcpus_on_cpu, cpu);256raw_spinlock_t *spinlock = &per_cpu(wakeup_vcpus_on_cpu_lock, cpu);257struct vcpu_vt *vt;258259raw_spin_lock(spinlock);260list_for_each_entry(vt, wakeup_list, pi_wakeup_list) {261262if (pi_test_on(&vt->pi_desc))263kvm_vcpu_wake_up(vt_to_vcpu(vt));264}265raw_spin_unlock(spinlock);266}267268void __init pi_init_cpu(int cpu)269{270INIT_LIST_HEAD(&per_cpu(wakeup_vcpus_on_cpu, cpu));271raw_spin_lock_init(&per_cpu(wakeup_vcpus_on_cpu_lock, cpu));272}273274void pi_apicv_pre_state_restore(struct kvm_vcpu *vcpu)275{276struct pi_desc *pi = vcpu_to_pi_desc(vcpu);277278pi_clear_on(pi);279memset(pi->pir, 0, sizeof(pi->pir));280}281282bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu)283{284struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);285286return pi_test_on(pi_desc) ||287(pi_test_sn(pi_desc) && !pi_is_pir_empty(pi_desc));288}289290291/*292* Kick all vCPUs when the first possible bypass IRQ is attached to a VM, as293* blocking vCPUs may scheduled out without reconfiguring PID.NV to the wakeup294* vector, i.e. if the bypass IRQ came along after vmx_vcpu_pi_put().295*/296void vmx_pi_start_bypass(struct kvm *kvm)297{298if (WARN_ON_ONCE(!vmx_can_use_vtd_pi(kvm)))299return;300301kvm_make_all_cpus_request(kvm, KVM_REQ_UNBLOCK);302}303304int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm,305unsigned int host_irq, uint32_t guest_irq,306struct kvm_vcpu *vcpu, u32 vector)307{308if (vcpu) {309struct intel_iommu_pi_data pi_data = {310.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)),311.vector = vector,312};313314return irq_set_vcpu_affinity(host_irq, &pi_data);315} else {316return irq_set_vcpu_affinity(host_irq, NULL);317}318}319320321