1 // SPDX-License-Identifier: GPL-2.0-only 2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 3 4 #include <linux/kvm_host.h> 5 6 #include <asm/irq_remapping.h> 7 #include <asm/cpu.h> 8 9 #include "lapic.h" 10 #include "irq.h" 11 #include "posted_intr.h" 12 #include "trace.h" 13 #include "vmx.h" 14 15 /* 16 * Maintain a per-CPU list of vCPUs that need to be awakened by wakeup_handler() 17 * when a WAKEUP_VECTOR interrupted is posted. vCPUs are added to the list when 18 * the vCPU is scheduled out and is blocking (e.g. in HLT) with IRQs enabled. 19 * The vCPUs posted interrupt descriptor is updated at the same time to set its 20 * notification vector to WAKEUP_VECTOR, so that posted interrupt from devices 21 * wake the target vCPUs. vCPUs are removed from the list and the notification 22 * vector is reset when the vCPU is scheduled in. 23 */ 24 static DEFINE_PER_CPU(struct list_head, wakeup_vcpus_on_cpu); 25 /* 26 * Protect the per-CPU list with a per-CPU spinlock to handle task migration. 27 * When a blocking vCPU is awakened _and_ migrated to a different pCPU, the 28 * ->sched_in() path will need to take the vCPU off the list of the _previous_ 29 * CPU. IRQs must be disabled when taking this lock, otherwise deadlock will 30 * occur if a wakeup IRQ arrives and attempts to acquire the lock. 31 */ 32 static DEFINE_PER_CPU(raw_spinlock_t, wakeup_vcpus_on_cpu_lock); 33 34 #define PI_LOCK_SCHED_OUT SINGLE_DEPTH_NESTING 35 36 static inline struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) 37 { 38 return &(to_vmx(vcpu)->pi_desc); 39 } 40 41 static int pi_try_set_control(struct pi_desc *pi_desc, u64 *pold, u64 new) 42 { 43 /* 44 * PID.ON can be set at any time by a different vCPU or by hardware, 45 * e.g. a device. PID.control must be written atomically, and the 46 * update must be retried with a fresh snapshot an ON change causes 47 * the cmpxchg to fail. 48 */ 49 if (!try_cmpxchg64(&pi_desc->control, pold, new)) 50 return -EBUSY; 51 52 return 0; 53 } 54 55 void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) 56 { 57 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); 58 struct vcpu_vmx *vmx = to_vmx(vcpu); 59 struct pi_desc old, new; 60 unsigned long flags; 61 unsigned int dest; 62 63 /* 64 * To simplify hot-plug and dynamic toggling of APICv, keep PI.NDST and 65 * PI.SN up-to-date even if there is no assigned device or if APICv is 66 * deactivated due to a dynamic inhibit bit, e.g. for Hyper-V's SyncIC. 67 */ 68 if (!enable_apicv || !lapic_in_kernel(vcpu)) 69 return; 70 71 /* 72 * If the vCPU wasn't on the wakeup list and wasn't migrated, then the 73 * full update can be skipped as neither the vector nor the destination 74 * needs to be changed. 75 */ 76 if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR && vcpu->cpu == cpu) { 77 /* 78 * Clear SN if it was set due to being preempted. Again, do 79 * this even if there is no assigned device for simplicity. 80 */ 81 if (pi_test_and_clear_sn(pi_desc)) 82 goto after_clear_sn; 83 return; 84 } 85 86 local_irq_save(flags); 87 88 /* 89 * If the vCPU was waiting for wakeup, remove the vCPU from the wakeup 90 * list of the _previous_ pCPU, which will not be the same as the 91 * current pCPU if the task was migrated. 92 */ 93 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR) { 94 raw_spinlock_t *spinlock = &per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu); 95 96 /* 97 * In addition to taking the wakeup lock for the regular/IRQ 98 * context, tell lockdep it is being taken for the "sched out" 99 * context as well. vCPU loads happens in task context, and 100 * this is taking the lock of the *previous* CPU, i.e. can race 101 * with both the scheduler and the wakeup handler. 102 */ 103 raw_spin_lock(spinlock); 104 spin_acquire(&spinlock->dep_map, PI_LOCK_SCHED_OUT, 0, _RET_IP_); 105 list_del(&vmx->pi_wakeup_list); 106 spin_release(&spinlock->dep_map, _RET_IP_); 107 raw_spin_unlock(spinlock); 108 } 109 110 dest = cpu_physical_id(cpu); 111 if (!x2apic_mode) 112 dest = (dest << 8) & 0xFF00; 113 114 old.control = READ_ONCE(pi_desc->control); 115 do { 116 new.control = old.control; 117 118 /* 119 * Clear SN (as above) and refresh the destination APIC ID to 120 * handle task migration (@cpu != vcpu->cpu). 121 */ 122 new.ndst = dest; 123 __pi_clear_sn(&new); 124 125 /* 126 * Restore the notification vector; in the blocking case, the 127 * descriptor was modified on "put" to use the wakeup vector. 128 */ 129 new.nv = POSTED_INTR_VECTOR; 130 } while (pi_try_set_control(pi_desc, &old.control, new.control)); 131 132 local_irq_restore(flags); 133 134 after_clear_sn: 135 136 /* 137 * Clear SN before reading the bitmap. The VT-d firmware 138 * writes the bitmap and reads SN atomically (5.2.3 in the 139 * spec), so it doesn't really have a memory barrier that 140 * pairs with this, but we cannot do that and we need one. 141 */ 142 smp_mb__after_atomic(); 143 144 if (!pi_is_pir_empty(pi_desc)) 145 pi_set_on(pi_desc); 146 } 147 148 static bool vmx_can_use_vtd_pi(struct kvm *kvm) 149 { 150 return irqchip_in_kernel(kvm) && enable_apicv && 151 kvm_arch_has_assigned_device(kvm) && 152 irq_remapping_cap(IRQ_POSTING_CAP); 153 } 154 155 /* 156 * Put the vCPU on this pCPU's list of vCPUs that needs to be awakened and set 157 * WAKEUP as the notification vector in the PI descriptor. 158 */ 159 static void pi_enable_wakeup_handler(struct kvm_vcpu *vcpu) 160 { 161 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); 162 struct vcpu_vmx *vmx = to_vmx(vcpu); 163 struct pi_desc old, new; 164 165 lockdep_assert_irqs_disabled(); 166 167 /* 168 * Acquire the wakeup lock using the "sched out" context to workaround 169 * a lockdep false positive. When this is called, schedule() holds 170 * various per-CPU scheduler locks. When the wakeup handler runs, it 171 * holds this CPU's wakeup lock while calling try_to_wake_up(), which 172 * can eventually take the aforementioned scheduler locks, which causes 173 * lockdep to assume there is deadlock. 174 * 175 * Deadlock can't actually occur because IRQs are disabled for the 176 * entirety of the sched_out critical section, i.e. the wakeup handler 177 * can't run while the scheduler locks are held. 178 */ 179 raw_spin_lock_nested(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu), 180 PI_LOCK_SCHED_OUT); 181 list_add_tail(&vmx->pi_wakeup_list, 182 &per_cpu(wakeup_vcpus_on_cpu, vcpu->cpu)); 183 raw_spin_unlock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu)); 184 185 WARN(pi_test_sn(pi_desc), "PI descriptor SN field set before blocking"); 186 187 old.control = READ_ONCE(pi_desc->control); 188 do { 189 /* set 'NV' to 'wakeup vector' */ 190 new.control = old.control; 191 new.nv = POSTED_INTR_WAKEUP_VECTOR; 192 } while (pi_try_set_control(pi_desc, &old.control, new.control)); 193 194 /* 195 * Send a wakeup IPI to this CPU if an interrupt may have been posted 196 * before the notification vector was updated, in which case the IRQ 197 * will arrive on the non-wakeup vector. An IPI is needed as calling 198 * try_to_wake_up() from ->sched_out() isn't allowed (IRQs are not 199 * enabled until it is safe to call try_to_wake_up() on the task being 200 * scheduled out). 201 */ 202 if (pi_test_on(&new)) 203 __apic_send_IPI_self(POSTED_INTR_WAKEUP_VECTOR); 204 } 205 206 static bool vmx_needs_pi_wakeup(struct kvm_vcpu *vcpu) 207 { 208 /* 209 * The default posted interrupt vector does nothing when 210 * invoked outside guest mode. Return whether a blocked vCPU 211 * can be the target of posted interrupts, as is the case when 212 * using either IPI virtualization or VT-d PI, so that the 213 * notification vector is switched to the one that calls 214 * back to the pi_wakeup_handler() function. 215 */ 216 return vmx_can_use_ipiv(vcpu) || vmx_can_use_vtd_pi(vcpu->kvm); 217 } 218 219 void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) 220 { 221 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); 222 223 if (!vmx_needs_pi_wakeup(vcpu)) 224 return; 225 226 if (kvm_vcpu_is_blocking(vcpu) && !vmx_interrupt_blocked(vcpu)) 227 pi_enable_wakeup_handler(vcpu); 228 229 /* 230 * Set SN when the vCPU is preempted. Note, the vCPU can both be seen 231 * as blocking and preempted, e.g. if it's preempted between setting 232 * its wait state and manually scheduling out. 233 */ 234 if (vcpu->preempted) 235 pi_set_sn(pi_desc); 236 } 237 238 /* 239 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. 240 */ 241 void pi_wakeup_handler(void) 242 { 243 int cpu = smp_processor_id(); 244 struct list_head *wakeup_list = &per_cpu(wakeup_vcpus_on_cpu, cpu); 245 raw_spinlock_t *spinlock = &per_cpu(wakeup_vcpus_on_cpu_lock, cpu); 246 struct vcpu_vmx *vmx; 247 248 raw_spin_lock(spinlock); 249 list_for_each_entry(vmx, wakeup_list, pi_wakeup_list) { 250 251 if (pi_test_on(&vmx->pi_desc)) 252 kvm_vcpu_wake_up(&vmx->vcpu); 253 } 254 raw_spin_unlock(spinlock); 255 } 256 257 void __init pi_init_cpu(int cpu) 258 { 259 INIT_LIST_HEAD(&per_cpu(wakeup_vcpus_on_cpu, cpu)); 260 raw_spin_lock_init(&per_cpu(wakeup_vcpus_on_cpu_lock, cpu)); 261 } 262 263 bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu) 264 { 265 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); 266 267 return pi_test_on(pi_desc) || 268 (pi_test_sn(pi_desc) && !pi_is_pir_empty(pi_desc)); 269 } 270 271 272 /* 273 * Bail out of the block loop if the VM has an assigned 274 * device, but the blocking vCPU didn't reconfigure the 275 * PI.NV to the wakeup vector, i.e. the assigned device 276 * came along after the initial check in vmx_vcpu_pi_put(). 277 */ 278 void vmx_pi_start_assignment(struct kvm *kvm) 279 { 280 if (!irq_remapping_cap(IRQ_POSTING_CAP)) 281 return; 282 283 kvm_make_all_cpus_request(kvm, KVM_REQ_UNBLOCK); 284 } 285 286 /* 287 * vmx_pi_update_irte - set IRTE for Posted-Interrupts 288 * 289 * @kvm: kvm 290 * @host_irq: host irq of the interrupt 291 * @guest_irq: gsi of the interrupt 292 * @set: set or unset PI 293 * returns 0 on success, < 0 on failure 294 */ 295 int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, 296 uint32_t guest_irq, bool set) 297 { 298 struct kvm_kernel_irq_routing_entry *e; 299 struct kvm_irq_routing_table *irq_rt; 300 bool enable_remapped_mode = true; 301 struct kvm_lapic_irq irq; 302 struct kvm_vcpu *vcpu; 303 struct vcpu_data vcpu_info; 304 int idx, ret = 0; 305 306 if (!vmx_can_use_vtd_pi(kvm)) 307 return 0; 308 309 idx = srcu_read_lock(&kvm->irq_srcu); 310 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); 311 if (guest_irq >= irq_rt->nr_rt_entries || 312 hlist_empty(&irq_rt->map[guest_irq])) { 313 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", 314 guest_irq, irq_rt->nr_rt_entries); 315 goto out; 316 } 317 318 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { 319 if (e->type != KVM_IRQ_ROUTING_MSI) 320 continue; 321 /* 322 * VT-d PI cannot support posting multicast/broadcast 323 * interrupts to a vCPU, we still use interrupt remapping 324 * for these kind of interrupts. 325 * 326 * For lowest-priority interrupts, we only support 327 * those with single CPU as the destination, e.g. user 328 * configures the interrupts via /proc/irq or uses 329 * irqbalance to make the interrupts single-CPU. 330 * 331 * We will support full lowest-priority interrupt later. 332 * 333 * In addition, we can only inject generic interrupts using 334 * the PI mechanism, refuse to route others through it. 335 */ 336 337 kvm_set_msi_irq(kvm, e, &irq); 338 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) || 339 !kvm_irq_is_postable(&irq)) 340 continue; 341 342 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); 343 vcpu_info.vector = irq.vector; 344 345 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, 346 vcpu_info.vector, vcpu_info.pi_desc_addr, set); 347 348 if (!set) 349 continue; 350 351 enable_remapped_mode = false; 352 353 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); 354 if (ret < 0) { 355 printk(KERN_INFO "%s: failed to update PI IRTE\n", 356 __func__); 357 goto out; 358 } 359 } 360 361 if (enable_remapped_mode) 362 ret = irq_set_vcpu_affinity(host_irq, NULL); 363 364 ret = 0; 365 out: 366 srcu_read_unlock(&kvm->irq_srcu, idx); 367 return ret; 368 } 369