Re: [PATCH v3 29/32] KVM: arm64: GICv4.1: Allow SGIs to switch between HW and SW interrupts

Marc Zyngier
2020-01-15T13:32:32Z

On 2020-01-15 03:49, Zenghui Yu wrote:
> Hi,
>
> On 2020/1/15 10:49, Shaokun Zhang wrote:
>> Hi Marc, [This is from Nianyao]
>>
>> On 2019/12/24 19:10, Marc Zyngier wrote:
>>> In order to let a guest buy in the new, active-less SGIs, we
>>> need to be able to switch between the two modes.
>>>
>>> Handle this by stopping all guest activity, transfer the state
>>> from one mode to the other, and resume the guest.
>>>
>>> Signed-off-by: Marc Zyngier <maz@kernel.org>
>>> ---
>
> [...]
>
>>> diff --git a/virt/kvm/arm/vgic/vgic-v4.c
>>> b/virt/kvm/arm/vgic/vgic-v4.c
>>> index c2fcde104ea2..063785fd2dc7 100644
>>> --- a/virt/kvm/arm/vgic/vgic-v4.c
>>> +++ b/virt/kvm/arm/vgic/vgic-v4.c
>>> @@ -97,6 +97,102 @@ static irqreturn_t vgic_v4_doorbell_handler(int
>>> irq, void *info)
>>> return IRQ_HANDLED;
>>> }
>>> +static void vgic_v4_sync_sgi_config(struct its_vpe *vpe, struct
>>> vgic_irq *irq)
>>> +{
>>> + vpe->sgi_config[irq->intid].enabled = irq->enabled;
>>> + vpe->sgi_config[irq->intid].group = irq->group;
>>> + vpe->sgi_config[irq->intid].priority = irq->priority;
>>> +}
>>> +
>>> +static void vgic_v4_enable_vsgis(struct kvm_vcpu *vcpu)
>>> +{
>>> + struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
>>> + int i;
>>> +
>>> + /*
>>> + * With GICv4.1, every virtual SGI can be directly injected. So
>>> + * let's pretend that they are HW interrupts, tied to a host
>>> + * IRQ. The SGI code will do its magic.
>>> + */
>>> + for (i = 0; i < VGIC_NR_SGIS; i++) {
>>> + struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, i);
>>> + struct irq_desc *desc;
>>> + int ret;
>>> +
>>> + if (irq->hw) {
>>> + vgic_put_irq(vcpu->kvm, irq);
>>> + continue;
>>> + }
>>> +
>>> + irq->hw = true;
>>> + irq->host_irq = irq_find_mapping(vpe->sgi_domain, i);
>>
>> I think we need to check whether irq_find_mapping returns 0.
>>
>>> + vgic_v4_sync_sgi_config(vpe, irq);
>>> + /*
>>> + * SGIs are initialised as disabled. Enable them if
>>> + * required by the rest of the VGIC init code.
>>> + */
>>> + desc = irq_to_desc(irq->host_irq);
>>> + ret = irq_domain_activate_irq(irq_desc_get_irq_data(desc),
>>> + false);
>>
>> If irq->host_irq is not valid , in irq_domain_activate_irq, it will
>> trigger NULL pointer
>> dereference in host kernel.
>> I meet a problem here. When hw support GIC4.1, and host kernel is
>> started with
>> kvm-arm.vgic_v4_enable=0, starting a virtual machine will trigger NULL
>> pointer
>> dereference in host.
>
> I think the thing is that we should _not_ try to configure vSGIs at all
> if kvm-arm.vgic_v4_enable=0 (which indicates we don't allow use of the
> GICv4 of direct injection).
>
> We currently set kvm_vgic_global_state.has_gicv4_1 to true if HW
> support
> GICv4.1, regardless whatever the gicv4_enable is (see patch#23 -
> vgic_v3_probe). I think this is what actually needs fixing.

Yes, my point exactly. I've pushed out a potential fix [1], and I'd be
grateful if you could let me know whether that fixes it for you.

Thanks,

M.

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms.git/commit/?h=irq/gic-v4.1-devel&id=b82c2ee1d3fef66fb85793965c344260f618219d
--
Jazz is not dead. It just smells funny...