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 #include "tdx.h"
15
16 /*
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 when
19 * 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 its
21 * notification vector to WAKEUP_VECTOR, so that posted interrupt from devices
22 * wake the target vCPUs. vCPUs are removed from the list and the notification
23 * vector is reset when the vCPU is scheduled in.
24 */
25 static 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, the
29 * ->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 will
31 * occur if a wakeup IRQ arrives and attempts to acquire the lock.
32 */
33 static DEFINE_PER_CPU(raw_spinlock_t, wakeup_vcpus_on_cpu_lock);
34
35 #define PI_LOCK_SCHED_OUT SINGLE_DEPTH_NESTING
36
vcpu_to_pi_desc(struct kvm_vcpu * vcpu)37 static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
38 {
39 return &(to_vt(vcpu)->pi_desc);
40 }
41
pi_try_set_control(struct pi_desc * pi_desc,u64 * pold,u64 new)42 static 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 the
47 * update must be retried with a fresh snapshot an ON change causes
48 * the cmpxchg to fail.
49 */
50 if (!try_cmpxchg64(&pi_desc->control, pold, new))
51 return -EBUSY;
52
53 return 0;
54 }
55
vmx_vcpu_pi_load(struct kvm_vcpu * vcpu,int cpu)56 void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
57 {
58 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
59 struct vcpu_vt *vt = to_vt(vcpu);
60 struct pi_desc old, new;
61 unsigned long flags;
62 unsigned int dest;
63
64 /*
65 * To simplify hot-plug and dynamic toggling of APICv, keep PI.NDST and
66 * PI.SN up-to-date even if there is no assigned device or if APICv is
67 * deactivated due to a dynamic inhibit bit, e.g. for Hyper-V's SyncIC.
68 */
69 if (!enable_apicv || !lapic_in_kernel(vcpu))
70 return;
71
72 /*
73 * If the vCPU wasn't on the wakeup list and wasn't migrated, then the
74 * full update can be skipped as neither the vector nor the destination
75 * needs to be changed.
76 */
77 if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR && vcpu->cpu == cpu) {
78 /*
79 * Clear SN if it was set due to being preempted. Again, do
80 * this even if there is no assigned device for simplicity.
81 */
82 if (pi_test_and_clear_sn(pi_desc))
83 goto after_clear_sn;
84 return;
85 }
86
87 local_irq_save(flags);
88
89 /*
90 * If the vCPU was waiting for wakeup, remove the vCPU from the wakeup
91 * list of the _previous_ pCPU, which will not be the same as the
92 * current pCPU if the task was migrated.
93 */
94 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR) {
95 raw_spinlock_t *spinlock = &per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu);
96
97 /*
98 * In addition to taking the wakeup lock for the regular/IRQ
99 * context, tell lockdep it is being taken for the "sched out"
100 * context as well. vCPU loads happens in task context, and
101 * this is taking the lock of the *previous* CPU, i.e. can race
102 * with both the scheduler and the wakeup handler.
103 */
104 raw_spin_lock(spinlock);
105 spin_acquire(&spinlock->dep_map, PI_LOCK_SCHED_OUT, 0, _RET_IP_);
106 list_del(&vt->pi_wakeup_list);
107 spin_release(&spinlock->dep_map, _RET_IP_);
108 raw_spin_unlock(spinlock);
109 }
110
111 dest = cpu_physical_id(cpu);
112 if (!x2apic_mode)
113 dest = (dest << 8) & 0xFF00;
114
115 old.control = READ_ONCE(pi_desc->control);
116 do {
117 new.control = old.control;
118
119 /*
120 * Clear SN (as above) and refresh the destination APIC ID to
121 * handle task migration (@cpu != vcpu->cpu).
122 */
123 new.ndst = dest;
124 __pi_clear_sn(&new);
125
126 /*
127 * Restore the notification vector; in the blocking case, the
128 * descriptor was modified on "put" to use the wakeup vector.
129 */
130 new.nv = POSTED_INTR_VECTOR;
131 } while (pi_try_set_control(pi_desc, &old.control, new.control));
132
133 local_irq_restore(flags);
134
135 after_clear_sn:
136
137 /*
138 * Clear SN before reading the bitmap. The VT-d firmware
139 * writes the bitmap and reads SN atomically (5.2.3 in the
140 * spec), so it doesn't really have a memory barrier that
141 * pairs with this, but we cannot do that and we need one.
142 */
143 smp_mb__after_atomic();
144
145 if (!pi_is_pir_empty(pi_desc))
146 pi_set_on(pi_desc);
147 }
148
vmx_can_use_vtd_pi(struct kvm * kvm)149 static bool vmx_can_use_vtd_pi(struct kvm *kvm)
150 {
151 return irqchip_in_kernel(kvm) && kvm_arch_has_irq_bypass() &&
152 kvm_arch_has_assigned_device(kvm);
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 */
pi_enable_wakeup_handler(struct kvm_vcpu * vcpu)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_vt *vt = to_vt(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(&vt->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
vmx_needs_pi_wakeup(struct kvm_vcpu * vcpu)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) && !is_td_vcpu(vcpu)) ||
217 vmx_can_use_vtd_pi(vcpu->kvm);
218 }
219
vmx_vcpu_pi_put(struct kvm_vcpu * vcpu)220 void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
221 {
222 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
223
224 if (!vmx_needs_pi_wakeup(vcpu))
225 return;
226
227 if (kvm_vcpu_is_blocking(vcpu) &&
228 ((is_td_vcpu(vcpu) && tdx_interrupt_allowed(vcpu)) ||
229 (!is_td_vcpu(vcpu) && !vmx_interrupt_blocked(vcpu))))
230 pi_enable_wakeup_handler(vcpu);
231
232 /*
233 * Set SN when the vCPU is preempted. Note, the vCPU can both be seen
234 * as blocking and preempted, e.g. if it's preempted between setting
235 * its wait state and manually scheduling out.
236 */
237 if (vcpu->preempted)
238 pi_set_sn(pi_desc);
239 }
240
241 /*
242 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
243 */
pi_wakeup_handler(void)244 void pi_wakeup_handler(void)
245 {
246 int cpu = smp_processor_id();
247 struct list_head *wakeup_list = &per_cpu(wakeup_vcpus_on_cpu, cpu);
248 raw_spinlock_t *spinlock = &per_cpu(wakeup_vcpus_on_cpu_lock, cpu);
249 struct vcpu_vt *vt;
250
251 raw_spin_lock(spinlock);
252 list_for_each_entry(vt, wakeup_list, pi_wakeup_list) {
253
254 if (pi_test_on(&vt->pi_desc))
255 kvm_vcpu_wake_up(vt_to_vcpu(vt));
256 }
257 raw_spin_unlock(spinlock);
258 }
259
pi_init_cpu(int cpu)260 void __init pi_init_cpu(int cpu)
261 {
262 INIT_LIST_HEAD(&per_cpu(wakeup_vcpus_on_cpu, cpu));
263 raw_spin_lock_init(&per_cpu(wakeup_vcpus_on_cpu_lock, cpu));
264 }
265
pi_apicv_pre_state_restore(struct kvm_vcpu * vcpu)266 void pi_apicv_pre_state_restore(struct kvm_vcpu *vcpu)
267 {
268 struct pi_desc *pi = vcpu_to_pi_desc(vcpu);
269
270 pi_clear_on(pi);
271 memset(pi->pir, 0, sizeof(pi->pir));
272 }
273
pi_has_pending_interrupt(struct kvm_vcpu * vcpu)274 bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu)
275 {
276 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
277
278 return pi_test_on(pi_desc) ||
279 (pi_test_sn(pi_desc) && !pi_is_pir_empty(pi_desc));
280 }
281
282
283 /*
284 * Bail out of the block loop if the VM has an assigned
285 * device, but the blocking vCPU didn't reconfigure the
286 * PI.NV to the wakeup vector, i.e. the assigned device
287 * came along after the initial check in vmx_vcpu_pi_put().
288 */
vmx_pi_start_assignment(struct kvm * kvm)289 void vmx_pi_start_assignment(struct kvm *kvm)
290 {
291 if (!kvm_arch_has_irq_bypass())
292 return;
293
294 kvm_make_all_cpus_request(kvm, KVM_REQ_UNBLOCK);
295 }
296
297 /*
298 * vmx_pi_update_irte - set IRTE for Posted-Interrupts
299 *
300 * @kvm: kvm
301 * @host_irq: host irq of the interrupt
302 * @guest_irq: gsi of the interrupt
303 * @set: set or unset PI
304 * returns 0 on success, < 0 on failure
305 */
vmx_pi_update_irte(struct kvm * kvm,unsigned int host_irq,uint32_t guest_irq,bool set)306 int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq,
307 uint32_t guest_irq, bool set)
308 {
309 struct kvm_kernel_irq_routing_entry *e;
310 struct kvm_irq_routing_table *irq_rt;
311 bool enable_remapped_mode = true;
312 struct kvm_lapic_irq irq;
313 struct kvm_vcpu *vcpu;
314 struct vcpu_data vcpu_info;
315 int idx, ret = 0;
316
317 if (!vmx_can_use_vtd_pi(kvm))
318 return 0;
319
320 idx = srcu_read_lock(&kvm->irq_srcu);
321 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
322 if (guest_irq >= irq_rt->nr_rt_entries ||
323 hlist_empty(&irq_rt->map[guest_irq])) {
324 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
325 guest_irq, irq_rt->nr_rt_entries);
326 goto out;
327 }
328
329 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
330 if (e->type != KVM_IRQ_ROUTING_MSI)
331 continue;
332 /*
333 * VT-d PI cannot support posting multicast/broadcast
334 * interrupts to a vCPU, we still use interrupt remapping
335 * for these kind of interrupts.
336 *
337 * For lowest-priority interrupts, we only support
338 * those with single CPU as the destination, e.g. user
339 * configures the interrupts via /proc/irq or uses
340 * irqbalance to make the interrupts single-CPU.
341 *
342 * We will support full lowest-priority interrupt later.
343 *
344 * In addition, we can only inject generic interrupts using
345 * the PI mechanism, refuse to route others through it.
346 */
347
348 kvm_set_msi_irq(kvm, e, &irq);
349 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
350 !kvm_irq_is_postable(&irq))
351 continue;
352
353 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
354 vcpu_info.vector = irq.vector;
355
356 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
357 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
358
359 if (!set)
360 continue;
361
362 enable_remapped_mode = false;
363
364 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
365 if (ret < 0) {
366 printk(KERN_INFO "%s: failed to update PI IRTE\n",
367 __func__);
368 goto out;
369 }
370 }
371
372 if (enable_remapped_mode)
373 ret = irq_set_vcpu_affinity(host_irq, NULL);
374
375 ret = 0;
376 out:
377 srcu_read_unlock(&kvm->irq_srcu, idx);
378 return ret;
379 }
380