Commit 9a0a75d3 authored by Marc Zyngier's avatar Marc Zyngier Committed by Oliver Upton

KVM: arm64: vgic: Make kvm_vgic_inject_irq() take a vcpu pointer

Passing a vcpu_id to kvm_vgic_inject_irq() is silly for two reasons:

- we often confuse vcpu_id and vcpu_idx
- we eventually have to convert it back to a vcpu
- we can't count

Instead, pass a vcpu pointer, which is unambiguous. A NULL vcpu
is also allowed for interrupts that are not private to a vcpu
(such as SPIs).
Reviewed-by: default avatarZenghui Yu <yuzenghui@huawei.com>
Signed-off-by: default avatarMarc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20230927090911.3355209-2-maz@kernel.orgSigned-off-by: default avatarOliver Upton <oliver.upton@linux.dev>
parent ce9ecca0
...@@ -458,7 +458,7 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, ...@@ -458,7 +458,7 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
timer_ctx->irq.level); timer_ctx->irq.level);
if (!userspace_irqchip(vcpu->kvm)) { if (!userspace_irqchip(vcpu->kvm)) {
ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id, ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu,
timer_irq(timer_ctx), timer_irq(timer_ctx),
timer_ctx->irq.level, timer_ctx->irq.level,
timer_ctx); timer_ctx);
......
...@@ -1134,27 +1134,23 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, ...@@ -1134,27 +1134,23 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
bool line_status) bool line_status)
{ {
u32 irq = irq_level->irq; u32 irq = irq_level->irq;
unsigned int irq_type, vcpu_idx, irq_num; unsigned int irq_type, vcpu_id, irq_num;
int nrcpus = atomic_read(&kvm->online_vcpus);
struct kvm_vcpu *vcpu = NULL; struct kvm_vcpu *vcpu = NULL;
bool level = irq_level->level; bool level = irq_level->level;
irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK; irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK; vcpu_id = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1); vcpu_id += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK; irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level); trace_kvm_irq_line(irq_type, vcpu_id, irq_num, irq_level->level);
switch (irq_type) { switch (irq_type) {
case KVM_ARM_IRQ_TYPE_CPU: case KVM_ARM_IRQ_TYPE_CPU:
if (irqchip_in_kernel(kvm)) if (irqchip_in_kernel(kvm))
return -ENXIO; return -ENXIO;
if (vcpu_idx >= nrcpus) vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
return -EINVAL;
vcpu = kvm_get_vcpu(kvm, vcpu_idx);
if (!vcpu) if (!vcpu)
return -EINVAL; return -EINVAL;
...@@ -1166,17 +1162,14 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, ...@@ -1166,17 +1162,14 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
if (!irqchip_in_kernel(kvm)) if (!irqchip_in_kernel(kvm))
return -ENXIO; return -ENXIO;
if (vcpu_idx >= nrcpus) vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
return -EINVAL;
vcpu = kvm_get_vcpu(kvm, vcpu_idx);
if (!vcpu) if (!vcpu)
return -EINVAL; return -EINVAL;
if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS) if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
return -EINVAL; return -EINVAL;
return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL); return kvm_vgic_inject_irq(kvm, vcpu, irq_num, level, NULL);
case KVM_ARM_IRQ_TYPE_SPI: case KVM_ARM_IRQ_TYPE_SPI:
if (!irqchip_in_kernel(kvm)) if (!irqchip_in_kernel(kvm))
return -ENXIO; return -ENXIO;
...@@ -1184,7 +1177,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, ...@@ -1184,7 +1177,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
if (irq_num < VGIC_NR_PRIVATE_IRQS) if (irq_num < VGIC_NR_PRIVATE_IRQS)
return -EINVAL; return -EINVAL;
return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL); return kvm_vgic_inject_irq(kvm, NULL, irq_num, level, NULL);
} }
return -EINVAL; return -EINVAL;
......
...@@ -348,7 +348,7 @@ static void kvm_pmu_update_state(struct kvm_vcpu *vcpu) ...@@ -348,7 +348,7 @@ static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
pmu->irq_level = overflow; pmu->irq_level = overflow;
if (likely(irqchip_in_kernel(vcpu->kvm))) { if (likely(irqchip_in_kernel(vcpu->kvm))) {
int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id, int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu,
pmu->irq_num, overflow, pmu); pmu->irq_num, overflow, pmu);
WARN_ON(ret); WARN_ON(ret);
} }
......
...@@ -23,7 +23,7 @@ static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e, ...@@ -23,7 +23,7 @@ static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e,
if (!vgic_valid_spi(kvm, spi_id)) if (!vgic_valid_spi(kvm, spi_id))
return -EINVAL; return -EINVAL;
return kvm_vgic_inject_irq(kvm, 0, spi_id, level, NULL); return kvm_vgic_inject_irq(kvm, NULL, spi_id, level, NULL);
} }
/** /**
......
...@@ -422,7 +422,7 @@ bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq, ...@@ -422,7 +422,7 @@ bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
/** /**
* kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
* @kvm: The VM structure pointer * @kvm: The VM structure pointer
* @cpuid: The CPU for PPIs * @vcpu: The CPU for PPIs or NULL for global interrupts
* @intid: The INTID to inject a new state to. * @intid: The INTID to inject a new state to.
* @level: Edge-triggered: true: to trigger the interrupt * @level: Edge-triggered: true: to trigger the interrupt
* false: to ignore the call * false: to ignore the call
...@@ -436,24 +436,22 @@ bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq, ...@@ -436,24 +436,22 @@ bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
* level-sensitive interrupts. You can think of the level parameter as 1 * level-sensitive interrupts. You can think of the level parameter as 1
* being HIGH and 0 being LOW and all devices being active-HIGH. * being HIGH and 0 being LOW and all devices being active-HIGH.
*/ */
int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid, int kvm_vgic_inject_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
bool level, void *owner) unsigned int intid, bool level, void *owner)
{ {
struct kvm_vcpu *vcpu;
struct vgic_irq *irq; struct vgic_irq *irq;
unsigned long flags; unsigned long flags;
int ret; int ret;
trace_vgic_update_irq_pending(cpuid, intid, level);
ret = vgic_lazy_init(kvm); ret = vgic_lazy_init(kvm);
if (ret) if (ret)
return ret; return ret;
vcpu = kvm_get_vcpu(kvm, cpuid);
if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS) if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
return -EINVAL; return -EINVAL;
trace_vgic_update_irq_pending(vcpu ? vcpu->vcpu_idx : 0, intid, level);
irq = vgic_get_irq(kvm, vcpu, intid); irq = vgic_get_irq(kvm, vcpu, intid);
if (!irq) if (!irq)
return -EINVAL; return -EINVAL;
......
...@@ -375,8 +375,8 @@ int kvm_vgic_map_resources(struct kvm *kvm); ...@@ -375,8 +375,8 @@ int kvm_vgic_map_resources(struct kvm *kvm);
int kvm_vgic_hyp_init(void); int kvm_vgic_hyp_init(void);
void kvm_vgic_init_cpu_hardware(void); void kvm_vgic_init_cpu_hardware(void);
int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid, int kvm_vgic_inject_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
bool level, void *owner); unsigned int intid, bool level, void *owner);
int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq, int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
u32 vintid, struct irq_ops *ops); u32 vintid, struct irq_ops *ops);
int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid); int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment