KVM: arm: vgic fixes for 5.7
Subsystems (3)
Marc Zyngier
maintainerv2 cover KVM: arm: vgic fixes for 5.7
Here's a few vgic fixes I've been piling on during the merge window, plus a couple that Zenghui contributed, and which I added to the mix.
The first patch is a silly off-by-one bug in the ACTIVE handling code, where we miss fail to stop the guest if writing to the first set of GICv2 SPIs. Oopsie boo.
The second patch improves the handling of the ACTIVE registers, which we never synchronise on the read side (the distributor state can only be updated when the vcpu exits). Let's fix it the same way we do it on the write side (stop-the-world, read, restart). Yes, this is expensive.
The following two patches deal with an issue where we consider the HW state of an interrupt when responding to a userspace access. We should never do this, as the guest shouldn't be running at this stage and if it is, it is absolutely fine to return random bits to userspace. It could also be that there is no active guest context at this stage, and you end up with an Oops, which nobody really enjoys.
The last two patches fix a couple of memory leaks.
Marc Zyngier (4):
KVM: arm: vgic: Fix limit condition when writing to GICD_I[CS]ACTIVER
KVM: arm: vgic: Synchronize the whole guest on GIC{D,R}_I{S,C}ACTIVER
read
KVM: arm: vgic: Only use the virtual state when userspace accesses
enable bits
KVM: arm: vgic-v2: Only use the virtual state when userspace accesses
pending bits
Zenghui Yu (2):
KVM: arm64: vgic-v3: Retire all pending LPIs on vcpu destroy
KVM: arm64: vgic-its: Fix memory leak on the error path of
vgic_add_lpi()
virt/kvm/arm/vgic/vgic-init.c | 6 + virt/kvm/arm/vgic/vgic-its.c | 11 +- virt/kvm/arm/vgic/vgic-mmio-v2.c | 16 ++- virt/kvm/arm/vgic/vgic-mmio-v3.c | 28 +++-- virt/kvm/arm/vgic/vgic-mmio.c | 183 +++++++++++++++++++++++++------ virt/kvm/arm/vgic/vgic-mmio.h | 19 ++++ 6 files changed, 207 insertions(+), 56 deletions(-)
Signature · 1 line
2.26.1
Marc Zyngier
maintainerPATCH v2 1/6 KVM: arm: vgic: Fix limit condition when writing to GICD_I[CS]ACTIVER
When deciding whether a guest has to be stopped we check whether this is a private interrupt or not. Unfortunately, there's an off-by-one bug here, and we fail to recognize a whole range of interrupts as being global (GICv2 SPIs 32-63).
Fix the condition from > to be >=.
--- virt/kvm/arm/vgic/vgic-mmio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio.c +2 -2
@@ -444,7 +444,7 @@ static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,static void vgic_change_active_prepare (struct kvm_vcpu *vcpu,u32 intid) {if (vcpu->kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 || - intid >VGIC_NR_PRIVATE_IRQS ) + intid >=VGIC_NR_PRIVATE_IRQS )kvm_arm_halt_guest (vcpu->kvm); }
@@ -452,7 +452,7 @@ static void vgic_change_active_prepare(struct kvm_vcpu *vcpu, u32 intid)static void vgic_change_active_finish (struct kvm_vcpu *vcpu,u32 intid) {if (vcpu->kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 || - intid >VGIC_NR_PRIVATE_IRQS ) + intid >=VGIC_NR_PRIVATE_IRQS )kvm_arm_resume_guest (vcpu->kvm); }
Signature · 1 line
2.26.1
Marc Zyngier
maintainerPATCH v2 2/6 KVM: arm: vgic: Synchronize the whole guest on GIC{D,R}_I{S,C}ACTIVER read
In reply to Marc ZyngierWhen a guest tries to read the active state of its interrupts, we currently just return whatever state we have in memory. This means that if such an interrupt lives in a List Register on another CPU, we fail to obsertve the latest active state for this interrupt.
In order to remedy this, stop all the other vcpus so that they exit and we can observe the most recent value for the state. This is similar to what we are doing for the write side of the same registers, and results in new MMIO handlers for userspace (which do not need to stop the guest, as it is supposed to be stopped already).
--- virt/kvm/arm/vgic/vgic-mmio-v2.c | 4 +- virt/kvm/arm/vgic/vgic-mmio-v3.c | 12 ++-- virt/kvm/arm/vgic/vgic-mmio.c | 100 ++++++++++++++++++++----------- virt/kvm/arm/vgic/vgic-mmio.h | 3 + 4 files changed, 75 insertions(+), 44 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +2 -2
@@ -422,11 +422,11 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_SET , vgic_mmio_read_active, vgic_mmio_write_sactive, -NULL , vgic_mmio_uaccess_write_sactive,1 , + vgic_uaccess_read_active, vgic_mmio_uaccess_write_sactive,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_CLEAR , vgic_mmio_read_active, vgic_mmio_write_cactive, -NULL , vgic_mmio_uaccess_write_cactive,1 , + vgic_uaccess_read_active, vgic_mmio_uaccess_write_cactive,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PRI, vgic_mmio_read_priority, vgic_mmio_write_priority, NULL, NULL,
virt/kvm/arm/vgic/vgic-mmio-v3.c +6 -6
@@ -553,11 +553,11 @@ static const struct vgic_register_region vgic_v3_dist_registers[] = { VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ISACTIVER , vgic_mmio_read_active, vgic_mmio_write_sactive, -NULL , vgic_mmio_uaccess_write_sactive,1 , + vgic_uaccess_read_active, vgic_mmio_uaccess_write_sactive,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ICACTIVER , vgic_mmio_read_active, vgic_mmio_write_cactive, -NULL , vgic_mmio_uaccess_write_cactive, + vgic_uaccess_read_active, vgic_mmio_uaccess_write_cactive,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IPRIORITYR, vgic_mmio_read_priority, vgic_mmio_write_priority, NULL, NULL,
@@ -625,12 +625,12 @@ static const struct vgic_register_region vgic_v3_rd_registers[] = { VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH_UACCESS (SZ_64K +GICR_ISACTIVER0 , vgic_mmio_read_active, vgic_mmio_write_sactive, -NULL , vgic_mmio_uaccess_write_sactive, -4 , VGIC_ACCESS_32bit), + vgic_uaccess_read_active, vgic_mmio_uaccess_write_sactive,4 , + VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH_UACCESS (SZ_64K +GICR_ICACTIVER0 , vgic_mmio_read_active, vgic_mmio_write_cactive, -NULL , vgic_mmio_uaccess_write_cactive, -4 , VGIC_ACCESS_32bit), + vgic_uaccess_read_active, vgic_mmio_uaccess_write_cactive,4 , + VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (SZ_64K +GICR_IPRIORITYR0 , vgic_mmio_read_priority, vgic_mmio_write_priority,32 , VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
virt/kvm/arm/vgic/vgic-mmio.c +64 -36
142 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h +3 -0
@@ -152,6 +152,9 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,unsigned long vgic_mmio_read_active (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len); +unsigned long vgic_uaccess_read_active (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); +void vgic_mmio_write_cactive (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len,unsigned long val);
Signature · 1 line
2.26.1
Re: [PATCH v2 2/6] KVM: arm: vgic: Synchronize the whole guest on GIC{D,R}_I{S,C}ACTIVER read
Quoted 11 lines · Marc Zyngier
When a guest tries to read the active state of its interrupts, we currently just return whatever state we have in memory. This means that if such an interrupt lives in a List Register on another CPU, we fail to obsertve the latest active state for this interrupt.
In order to remedy this, stop all the other vcpus so that they exit and we can observe the most recent value for the state. This is similar to what we are doing for the write side of the same registers, and results in new MMIO handlers for userspace (which do not need to stop the guest, as it is supposed to be stopped already).
Thanks for the changes! Checked for other users of VGIC_NR_PRIVATE_IRQS, also for not missing other ACTIVE bit register handlers. Looks good to me!
Quoted 3 lines
Reported-by Julien Grall <[email protected]>Signed-off-by Marc Zyngier <[email protected]>
Cheers,
Andre
Quoted 227 lines
--- virt/kvm/arm/vgic/vgic-mmio-v2.c | 4 +- virt/kvm/arm/vgic/vgic-mmio-v3.c | 12 ++-- virt/kvm/arm/vgic/vgic-mmio.c | 100 ++++++++++++++++++++----------- virt/kvm/arm/vgic/vgic-mmio.h | 3 + 4 files changed, 75 insertions(+), 44 deletions(-)virt/kvm/arm/vgic/vgic-mmio-v2.c +2
-2@@ -422,11 +422,11 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_SET , vgic_mmio_read_active, vgic_mmio_write_sactive, -NULL , vgic_mmio_uaccess_write_sactive,1 , + vgic_uaccess_read_active, vgic_mmio_uaccess_write_sactive,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ACTIVE_CLEAR , vgic_mmio_read_active, vgic_mmio_write_cactive, -NULL , vgic_mmio_uaccess_write_cactive,1 , + vgic_uaccess_read_active, vgic_mmio_uaccess_write_cactive,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PRI, vgic_mmio_read_priority, vgic_mmio_write_priority, NULL, NULL,virt/kvm/arm/vgic/vgic-mmio-v3.c +6
-6@@ -553,11 +553,11 @@ static const struct vgic_register_region vgic_v3_dist_registers[] = { VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ISACTIVER , vgic_mmio_read_active, vgic_mmio_write_sactive, -NULL , vgic_mmio_uaccess_write_sactive,1 , + vgic_uaccess_read_active, vgic_mmio_uaccess_write_sactive,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ICACTIVER , vgic_mmio_read_active, vgic_mmio_write_cactive, -NULL , vgic_mmio_uaccess_write_cactive, + vgic_uaccess_read_active, vgic_mmio_uaccess_write_cactive,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_IPRIORITYR, vgic_mmio_read_priority, vgic_mmio_write_priority, NULL, NULL,@@ -625,12 +625,12 @@ static const struct vgic_register_region vgic_v3_rd_registers[] = { VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH_UACCESS (SZ_64K +GICR_ISACTIVER0 , vgic_mmio_read_active, vgic_mmio_write_sactive, -NULL , vgic_mmio_uaccess_write_sactive, -4 , VGIC_ACCESS_32bit), + vgic_uaccess_read_active, vgic_mmio_uaccess_write_sactive,4 , + VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH_UACCESS (SZ_64K +GICR_ICACTIVER0 , vgic_mmio_read_active, vgic_mmio_write_cactive, -NULL , vgic_mmio_uaccess_write_cactive, -4 , VGIC_ACCESS_32bit), + vgic_uaccess_read_active, vgic_mmio_uaccess_write_cactive,4 , + VGIC_ACCESS_32bit),REGISTER_DESC_WITH_LENGTH (SZ_64K +GICR_IPRIORITYR0 , vgic_mmio_read_priority, vgic_mmio_write_priority,32 , VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),virt/kvm/arm/vgic/vgic-mmio.c +64
-36@@ -348,8 +348,39 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, } } -unsigned long vgic_mmio_read_active (struct kvm_vcpu *vcpu, -gpa_t addr,unsigned int len) + +/* +* If we are fiddling with an IRQ's active state, we have to make sure the IRQ +* is not queued on some running VCPU's LRs, because then the change to the +* active state can be overwritten when the VCPU's state is synced coming back +* from the guest. +* +* For shared interrupts as well as GICv3 private interrupts, we have to +* stop all the VCPUs because interrupts can be migrated while we don't hold +* the IRQ locks and we don't want to be chasing moving targets. +* +* For GICv2 private interrupts we don't have to do anything because +* userspace accesses to the VGIC state already require all VCPUs to be +* stopped, and only the VCPU itself can modify its private interrupts +* active state, which guarantees that the VCPU is not running. +*/ +static void vgic_access_active_prepare (struct kvm_vcpu *vcpu,u32 intid) +{ +if (vcpu->kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 || + intid >=VGIC_NR_PRIVATE_IRQS ) +kvm_arm_halt_guest (vcpu->kvm); +} + +/* See vgic_access_active_prepare */ +static void vgic_access_active_finish (struct kvm_vcpu *vcpu,u32 intid) +{ +if (vcpu->kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 || + intid >=VGIC_NR_PRIVATE_IRQS ) +kvm_arm_resume_guest (vcpu->kvm); +} + +static unsigned long __vgic_mmio_read_active (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) {u32 intid =VGIC_ADDR_TO_INTID (addr,1 );u32 value =0 ;@@ -359,6 +390,10 @@ unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu,for (i =0 ; i < len *8 ; i++) {struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i); +/* +* Even for HW interrupts, don't evaluate the HW state as +* all the guest is interested in is the virtual state. +*/ if (irq->active) value |= (1U << i);@@ -368,6 +403,29 @@ unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu,return value; } +unsigned long vgic_mmio_read_active (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); +u32 val; + +mutex_lock (&vcpu->kvm->lock); +vgic_access_active_prepare (vcpu, intid); + + val =__vgic_mmio_read_active (vcpu, addr, len); + +vgic_access_active_finish (vcpu, intid); +mutex_unlock (&vcpu->kvm->lock); + +return val; +} + +unsigned long vgic_uaccess_read_active (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +return __vgic_mmio_read_active (vcpu, addr, len); +} +/* Must be called with irq->irq_lock held */ static void vgic_hw_irq_change_active (struct kvm_vcpu *vcpu,struct vgic_irq *irq,bool active,bool is_uaccess)@@ -426,36 +484,6 @@ static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,raw_spin_unlock_irqrestore (&irq->irq_lock, flags); } -/* -* If we are fiddling with an IRQ's active state, we have to make sure the IRQ -* is not queued on some running VCPU's LRs, because then the change to the -* active state can be overwritten when the VCPU's state is synced coming back -* from the guest. -* -* For shared interrupts, we have to stop all the VCPUs because interrupts can -* be migrated while we don't hold the IRQ locks and we don't want to be -* chasing moving targets. -* -* For private interrupts we don't have to do anything because userspace -* accesses to the VGIC state already require all VCPUs to be stopped, and -* only the VCPU itself can modify its private interrupts active state, which -* guarantees that the VCPU is not running. -*/ -static void vgic_change_active_prepare (struct kvm_vcpu *vcpu,u32 intid) -{ -if (vcpu->kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 || - intid >=VGIC_NR_PRIVATE_IRQS ) -kvm_arm_halt_guest (vcpu->kvm); -} - -/* See vgic_change_active_prepare */ -static void vgic_change_active_finish (struct kvm_vcpu *vcpu,u32 intid) -{ -if (vcpu->kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 || - intid >=VGIC_NR_PRIVATE_IRQS ) -kvm_arm_resume_guest (vcpu->kvm); -} -static void __vgic_mmio_write_cactive (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len,unsigned long val)@@ -477,11 +505,11 @@ void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,u32 intid =VGIC_ADDR_TO_INTID (addr,1 );mutex_lock (&vcpu->kvm->lock); -vgic_change_active_prepare (vcpu, intid); +vgic_access_active_prepare (vcpu, intid);__vgic_mmio_write_cactive (vcpu, addr, len, val); -vgic_change_active_finish (vcpu, intid); +vgic_access_active_finish (vcpu, intid);mutex_unlock (&vcpu->kvm->lock); }@@ -514,11 +542,11 @@ void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,u32 intid =VGIC_ADDR_TO_INTID (addr,1 );mutex_lock (&vcpu->kvm->lock); -vgic_change_active_prepare (vcpu, intid); +vgic_access_active_prepare (vcpu, intid);__vgic_mmio_write_sactive (vcpu, addr, len, val); -vgic_change_active_finish (vcpu, intid); +vgic_access_active_finish (vcpu, intid);mutex_unlock (&vcpu->kvm->lock); }virt/kvm/arm/vgic/vgic-mmio.h +3
-0@@ -152,6 +152,9 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,unsigned long vgic_mmio_read_active (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len); +unsigned long vgic_uaccess_read_active (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len); +void vgic_mmio_write_cactive (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len,unsigned long val);
Marc Zyngier
maintainerPATCH v2 3/6 KVM: arm: vgic: Only use the virtual state when userspace accesses enable bits
In reply to Marc ZyngierThere is no point in accessing the HW when writing to any of the ISENABLER/ICENABLER registers from userspace, as only the guest should be allowed to change the HW state.
Introduce new userspace-specific accessors that deal solely with the virtual state.
--- virt/kvm/arm/vgic/vgic-mmio-v2.c | 6 +++-- virt/kvm/arm/vgic/vgic-mmio-v3.c | 16 +++++++----- virt/kvm/arm/vgic/vgic-mmio.c | 42 ++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 8 ++++++ 4 files changed, 64 insertions(+), 8 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +4 -2
@@ -409,10 +409,12 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { NULL, vgic_mmio_uaccess_write_v2_group, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ENABLE_SET , - vgic_mmio_read_enable, vgic_mmio_write_senable,NULL ,NULL ,1 , + vgic_mmio_read_enable, vgic_mmio_write_senable, +NULL , vgic_uaccess_write_senable,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_ENABLE_CLEAR , - vgic_mmio_read_enable, vgic_mmio_write_cenable,NULL ,NULL ,1 , + vgic_mmio_read_enable, vgic_mmio_write_cenable, +NULL , vgic_uaccess_write_cenable,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PENDING_SET, vgic_mmio_read_pending, vgic_mmio_write_spending, NULL, NULL, 1,
virt/kvm/arm/vgic/vgic-mmio-v3.c +10 -6
@@ -538,10 +538,12 @@ static const struct vgic_register_region vgic_v3_dist_registers[] = { vgic_mmio_read_group, vgic_mmio_write_group, NULL, NULL, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ISENABLER , - vgic_mmio_read_enable, vgic_mmio_write_senable,NULL ,NULL ,1 , + vgic_mmio_read_enable, vgic_mmio_write_senable, +NULL , vgic_uaccess_write_senable,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED (GICD_ICENABLER , - vgic_mmio_read_enable, vgic_mmio_write_cenable,NULL ,NULL ,1 , + vgic_mmio_read_enable, vgic_mmio_write_cenable, +NULL , vgic_uaccess_write_cenable,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ_SHARED(GICD_ISPENDR, vgic_mmio_read_pending, vgic_mmio_write_spending,
@@ -609,11 +611,13 @@ static const struct vgic_register_region vgic_v3_rd_registers[] = {REGISTER_DESC_WITH_LENGTH (SZ_64K +GICR_IGROUPR0 , vgic_mmio_read_group, vgic_mmio_write_group,4 , VGIC_ACCESS_32bit), -REGISTER_DESC_WITH_LENGTH (SZ_64K +GICR_ISENABLER0 , - vgic_mmio_read_enable, vgic_mmio_write_senable,4 , +REGISTER_DESC_WITH_LENGTH_UACCESS (SZ_64K +GICR_ISENABLER0 , + vgic_mmio_read_enable, vgic_mmio_write_senable, +NULL , vgic_uaccess_write_senable,4 , VGIC_ACCESS_32bit), -REGISTER_DESC_WITH_LENGTH (SZ_64K +GICR_ICENABLER0 , - vgic_mmio_read_enable, vgic_mmio_write_cenable,4 , +REGISTER_DESC_WITH_LENGTH_UACCESS (SZ_64K +GICR_ICENABLER0 , + vgic_mmio_read_enable, vgic_mmio_write_cenable, +NULL , vgic_uaccess_write_cenable,4 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_LENGTH_UACCESS(SZ_64K + GICR_ISPENDR0, vgic_mmio_read_pending, vgic_mmio_write_spending,
virt/kvm/arm/vgic/vgic-mmio.c +42 -0
48 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h +8 -0
@@ -138,6 +138,14 @@ void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val); +int vgic_uaccess_write_senable (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); + +int vgic_uaccess_write_cenable (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); +unsigned long vgic_mmio_read_pending (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len);
Signature · 1 line
2.26.1
Re: [PATCH v2 3/6] KVM: arm: vgic: Only use the virtual state when userspace accesses enable bits
Hi Marc,
Quoted 8 lines · Marc Zyngier
There is no point in accessing the HW when writing to any of the ISENABLER/ICENABLER registers from userspace, as only the guest should be allowed to change the HW state.
Introduce new userspace-specific accessors that deal solely with the virtual state.
Reported-by James Morse <[email protected]>
Tested on both machines I've hit this on:
and perhaps more useful:
Thanks,
James
Marc Zyngier
maintainerPATCH v2 4/6 KVM: arm: vgic-v2: Only use the virtual state when userspace accesses pending bits
In reply to Marc ZyngierThere is no point in accessing the HW when writing to any of the ISPENDR/ICPENDR registers from userspace, as only the guest should be allowed to change the HW state.
Introduce new userspace-specific accessors that deal solely with the virtual state. Note that the API differs from that of GICv3, where userspace exclusively uses ISPENDR to set the state. Too bad we can't reuse it.
--- virt/kvm/arm/vgic/vgic-mmio-v2.c | 6 +++-- virt/kvm/arm/vgic/vgic-mmio.c | 41 ++++++++++++++++++++++++++++++++ virt/kvm/arm/vgic/vgic-mmio.h | 8 +++++++ 3 files changed, 53 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-mmio-v2.c +4 -2
@@ -417,10 +417,12 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { NULL, vgic_uaccess_write_cenable, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_SET , - vgic_mmio_read_pending, vgic_mmio_write_spending,NULL ,NULL ,1 , + vgic_mmio_read_pending, vgic_mmio_write_spending, +NULL , vgic_uaccess_write_spending,1 , VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_CLEAR , - vgic_mmio_read_pending, vgic_mmio_write_cpending,NULL ,NULL ,1 , + vgic_mmio_read_pending, vgic_mmio_write_cpending, +NULL , vgic_uaccess_write_cpending,1 , VGIC_ACCESS_32bit), REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ACTIVE_SET, vgic_mmio_read_active, vgic_mmio_write_sactive,
virt/kvm/arm/vgic/vgic-mmio.c +41 -0
53 lines not on this page · raw message
virt/kvm/arm/vgic/vgic-mmio.h +8 -0
@@ -157,6 +157,14 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val); +int vgic_uaccess_write_spending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); + +int vgic_uaccess_write_cpending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val); +unsigned long vgic_mmio_read_active (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len);
Signature · 1 line
2.26.1
Re: [PATCH v2 4/6] KVM: arm: vgic-v2: Only use the virtual state when userspace accesses pending bits
Hi Marc,
Quoted 14 lines · Marc Zyngier
There is no point in accessing the HW when writing to any of the ISPENDR/ICPENDR registers from userspace, as only the guest should be allowed to change the HW state.
Introduce new userspace-specific accessors that deal solely with the virtual state. Note that the API differs from that of GICv3, where userspace exclusively uses ISPENDR to set the state. Too bad we can't reuse it.
virt/kvm/arm/vgic/vgic-mmio-v2.c +0
-0
virt/kvm/arm/vgic/vgic-mmio-v2.c static const struct vgic_register_region vgic_v2_dist_registers[] = { · line 422
Quoted 7 lines
virt/kvm/arm/vgic/vgic-mmio-v2.c +2
-1@@ -417,10 +417,12 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { NULL, vgic_uaccess_write_cenable, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_SET , - vgic_mmio_read_pending, vgic_mmio_write_spending, NULL, NULL, 1, + vgic_mmio_read_pending, vgic_mmio_write_spending, +NULL , vgic_uaccess_write_spending,1 , VGIC_ACCESS_32bit),
vgic_mmio_write_spending() has some homebrew detection for is_uaccess, which causes vgic_hw_irq_spending() to do nothing. Isn't that now dead-code with this change?
Quoted 6 lines
virt/kvm/arm/vgic/vgic-mmio.c +0
-0@@ -321,6 +321,27 @@ void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_spending(struct kvm_vcpu *vcpu, · line 331
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +10
-0+int vgic_uaccess_write_spending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); +int i; +unsigned long flags; + +for_each_set_bit (i, &val, len *8 ) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);
vgic_mmio_write_spending() has:
| /* GICD_ISPENDR0 SGI bits are WI *
and bales out early. Is GIC_DIST_PENDING_SET the same register? (If so, shouldn't that be true for PPI too?)
Quoted 12 lines
virt/kvm/arm/vgic/vgic-mmio.c +9
-0+raw_spin_lock_irqsave (&irq->irq_lock, flags); + irq->pending_latch = true; +vgic_queue_irq_unlock (vcpu->kvm, irq, flags); + +vgic_put_irq (vcpu->kvm, irq); + } + +return 0 ; +}@@ -390,6 +411,26 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, · line 421
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +10
-0+int vgic_uaccess_write_cpending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); +int i; +unsigned long flags; + +for_each_set_bit (i, &val, len *8 ) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);
Same dumb question about GICD_ICPENDR0!?
virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, · line 433
Quoted 9 lines
virt/kvm/arm/vgic/vgic-mmio.c +9
-0+raw_spin_lock_irqsave (&irq->irq_lock, flags); + irq->pending_latch = false; +raw_spin_unlock_irqrestore (&irq->irq_lock, flags); + +vgic_put_irq (vcpu->kvm, irq); + } + +return 0 ; +}
Thanks,
James
Marc Zyngier
maintainerRe: [PATCH v2 4/6] KVM: arm: vgic-v2: Only use the virtual state when userspace accesses pending bits
On Fri, 17 Apr 2020 12:22:10 +0100
James Morse <[email protected]> wrote:
Hi James,
Quoted 12 lines
Hi Marc,
Quoted 8 lines · Marc Zyngier
There is no point in accessing the HW when writing to any of the ISPENDR/ICPENDR registers from userspace, as only the guest should be allowed to change the HW state.
Introduce new userspace-specific accessors that deal solely with the virtual state. Note that the API differs from that of GICv3, where userspace exclusively uses ISPENDR to set the state. Too bad we can't reuse it.
Quoted 15 lines
virt/kvm/arm/vgic/vgic-mmio-v2.c static const struct vgic_register_region vgic_v2_dist_registers[] = { · line 422
Quoted 12 lines
virt/kvm/arm/vgic/vgic-mmio-v2.c +2
-1@@ -417,10 +417,12 @@ static const struct vgic_register_region vgic_v2_dist_registers[] = { NULL, vgic_uaccess_write_cenable, 1, VGIC_ACCESS_32bit),REGISTER_DESC_WITH_BITS_PER_IRQ (GIC_DIST_PENDING_SET , - vgic_mmio_read_pending, vgic_mmio_write_spending, NULL, NULL, 1, + vgic_mmio_read_pending, vgic_mmio_write_spending, +NULL , vgic_uaccess_write_spending,1 , VGIC_ACCESS_32bit),
vgic_mmio_write_spending()has some homebrew detection foris_uaccess, which causesvgic_hw_irq_spending()to do nothing. Isn't that now dead-code with this change?
Very good point, this deserves a cleanup.
Quoted 20 lines
Quoted 6 lines
virt/kvm/arm/vgic/vgic-mmio.c +0
-0@@ -321,6 +321,27 @@ void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_spending(struct kvm_vcpu *vcpu, · line 331
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +10
-0+int vgic_uaccess_write_spending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); +int i; +unsigned long flags; + +for_each_set_bit (i, &val, len *8 ) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);
vgic_mmio_write_spending()has:
| /*GICD_ISPENDR0SGI bits are WI *
Quoted 2 lines
and bales out early. Is
GIC_DIST_PENDING_SETthe same register? (If so, shouldn't that be true for PPI too?)
Hmmm. It's a bit more complicated (surprisingly).
Yes, the SGI pending bits are WI from the guest perspective (as required by the spec). But we still need to be able to restore them from userspace, and I bet 82e40f558de56 ("KVM: arm/arm64: vgic-v2:
Handle SGI bits in GICD_I{S,C}PENDR0 as WI") has broken migration with GICv2 (if you migrated with a pending SGI, you cannot restore it...).
Now, there is still a bug here, in the sense that we need to indicate which vcpu is the source of the SGI (this is a GICv2-special). Unfortunately, we don't have a way to communicate this architecturally. The only option we have is to make it up (as a self-SGI, for example). But this is pretty broken at the architectural level TBH.
On the other hand, PPIs are just fine.
Quoted 14 lines
Quoted 13 lines
virt/kvm/arm/vgic/vgic-mmio.c +9
-0+raw_spin_lock_irqsave (&irq->irq_lock, flags); + irq->pending_latch = true; +vgic_queue_irq_unlock (vcpu->kvm, irq, flags); + +vgic_put_irq (vcpu->kvm, irq); + } + +return 0 ; +}@@ -390,6 +411,26 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
Quoted 12 lines
virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, · line 420
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +10
-0+int vgic_uaccess_write_cpending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); +int i; +unsigned long flags; + +for_each_set_bit (i, &val, len *8 ) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);Same dumb question about
GICD_ICPENDR0!?
Not dumb at all! Given that we previously allowed this to be accessed from userspace (well, before we broke it again), it should be able to clear *something*. If we adopt the self-SGI behaviour as above, we will get away with it.
Here's what I'm proposing to add to this patch, together with a
Nobody is using GICv2, obviously... :-/
Thanks,
M.
virt/kvm/arm/vgic/vgic-mmio.c +30 -23
@@ -261,17 +261,6 @@ unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,return value; } -/* Must be called with irq->irq_lock held */ -static void vgic_hw_irq_spending (struct kvm_vcpu *vcpu,struct vgic_irq *irq, -bool is_uaccess) -{ -if (is_uaccess) -return ; - - irq->pending_latch = true; -vgic_irq_set_phys_active (irq, true); -} -static bool is_vgic_v2_sgi (struct kvm_vcpu *vcpu,struct vgic_irq *irq) { return (vgic_irq_is_sgi(irq->intid) &&
@@ -282,7 +271,6 @@ void vgic_mmio_write_spending(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val) { - bool is_uaccess = !kvm_get_running_vcpu(); u32 intid = VGIC_ADDR_TO_INTID(addr, 1);int i;unsigned long flags;
@@ -312,10 +300,10 @@ void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,continue ; } + irq->pending_latch = true;if (irq->hw) -vgic_hw_irq_spending (vcpu, irq, is_uaccess); -else - irq->pending_latch = true; +vgic_irq_set_phys_active (irq, true); +vgic_queue_irq_unlock (vcpu->kvm, irq, flags);vgic_put_irq (vcpu->kvm, irq); }
@@ -334,6 +322,15 @@ int vgic_uaccess_write_spending(struct kvm_vcpu *vcpu,raw_spin_lock_irqsave (&irq->irq_lock, flags); irq->pending_latch = true; + +/* +* GICv2 SGIs are terribly broken. We can't restore +* the source of the interrupt, so just pick the vcpu +* itself as the source... +*/ +if (is_vgic_v2_sgi (vcpu, irq)) + irq->source |=BIT (vcpu->vcpu_id); +vgic_queue_irq_unlock (vcpu->kvm, irq, flags);vgic_put_irq (vcpu->kvm, irq);
@@ -343,12 +340,8 @@ int vgic_uaccess_write_spending(struct kvm_vcpu *vcpu, }/* Must be called with irq->irq_lock held */ -static void vgic_hw_irq_cpending (struct kvm_vcpu *vcpu,struct vgic_irq *irq, -bool is_uaccess) +static void vgic_hw_irq_cpending (struct kvm_vcpu *vcpu,struct vgic_irq *irq) { -if (is_uaccess) -return ; - irq->pending_latch = false;/*
@@ -371,7 +364,6 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val) { - bool is_uaccess = !kvm_get_running_vcpu(); u32 intid = VGIC_ADDR_TO_INTID(addr, 1);int i;unsigned long flags;
@@ -402,7 +394,7 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, }if (irq->hw) -vgic_hw_irq_cpending (vcpu, irq, is_uaccess); +vgic_hw_irq_cpending (vcpu, irq);else irq->pending_latch = false;
@@ -423,7 +415,22 @@ int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu,struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);raw_spin_lock_irqsave (&irq->irq_lock, flags); - irq->pending_latch = false; +/* +* More fun with GICv2 SGIs! If we're clearing one of them +* from userspace, which source vcpu to clear? Let's pick +* the target vcpu itself (consistent whith the way we +* populate them on the ISPENDR side), and only clear the +* pending state if no sources are left (insert expletive +* here). +*/ +if (is_vgic_v2_sgi (vcpu, irq)) { + irq->source &= ~BIT (vcpu->vcpu_id); +if (!irq->source) + irq->pending_latch = false; + }else { + irq->pending_latch = false; + } +raw_spin_unlock_irqrestore (&irq->irq_lock, flags);vgic_put_irq (vcpu->kvm, irq);
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v2 4/6] KVM: arm: vgic-v2: Only use the virtual state when userspace accesses pending bits
Hi Marc,
Quoted 36 lines · Marc Zyngier
Quoted 30 lines · On Fri, 17 Apr 2020 12:22:10 +0100 James Morse <[email protected]> wrote:
Quoted 15 lines · Marc Zyngier
There is no point in accessing the HW when writing to any of the ISPENDR/ICPENDR registers from userspace, as only the guest should be allowed to change the HW state.
Introduce new userspace-specific accessors that deal solely with the virtual state. Note that the API differs from that of GICv3, where userspace exclusively uses ISPENDR to set the state. Too bad we can't reuse it.
virt/kvm/arm/vgic/vgic-mmio.c +0
-0@@ -321,6 +321,27 @@ void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_spending(struct kvm_vcpu *vcpu, · line 331
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +10
-0+int vgic_uaccess_write_spending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); +int i; +unsigned long flags; + +for_each_set_bit (i, &val, len *8 ) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);
vgic_mmio_write_spending()has:
| /*GICD_ISPENDR0SGI bits are WI *Quoted 2 lines
and bales out early. Is
GIC_DIST_PENDING_SETthe same register? (If so, shouldn't that be true for PPI too?)Hmmm. It's a bit more complicated (surprisingly).
Quoted 7 lines
Yes, the SGI pending bits are WI from the guest perspective (as required by the spec).
But we still need to be able to restore them
from userspace, and I bet82e40f558de56("KVM: arm/arm64: vgic-v2:
Handle SGI bits in GICD_I{S,C}PENDR0 as WI") has broken migration with GICv2 (if you migrated with a pending SGI, you cannot restore it...).
Fun! It looks like the ioctl() would succeed, but nothing happened. Once you restart the guest one CPU may wait forever for the victim to respond.
Quoted 6 lines
Now, there is still a bug here, in the sense that we need to indicate which vcpu is the source of the SGI (this is a GICv2-special). Unfortunately, we don't have a way to communicate this architecturally. The only option we have is to make it up (as a self-SGI, for example). But this is pretty broken at the architectural level TBH.
On the other hand, PPIs are just fine.
Yup, wrong spec, I was looking at the same register in GICv3! It looks like the GICv3 text is there because those registers live in the redistributor instead... duh!
Quoted 31 lines
Quoted 14 lines
virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, · line 421
Quoted 12 lines
virt/kvm/arm/vgic/vgic-mmio.c +10
-0@@ -390,6 +411,26 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, +int vgic_uaccess_write_cpending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); +int i; +unsigned long flags; + +for_each_set_bit (i, &val, len *8 ) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);Same dumb question about
GICD_ICPENDR0!?Not dumb at all! Given that we previously allowed this to be accessed from userspace (well, before we broke it again), it should be able to clear *something*. If we adopt the self-SGI behaviour as above, we will get away with it.
Here's what I'm proposing to add to this patch, together with a
Fixes 82e40f558de56 ("KVM: arm/arm64: vgic-v2: Handle SGI bits in GICD_I{S,C}PENDR0 as WI")Nobody is using GICv2, obviously... :-/
virt/kvm/arm/vgic/vgic-mmio.c +0
-0
virt/kvm/arm/vgic/vgic-mmio.c int vgic_uaccess_write_spending(struct kvm_vcpu *vcpu, · line 329
Quoted 8 lines
virt/kvm/arm/vgic/vgic-mmio.c +5
-0@@ -334,6 +322,15 @@ int vgic_uaccess_write_spending(struct kvm_vcpu *vcpu,raw_spin_lock_irqsave (&irq->irq_lock, flags); irq->pending_latch = true; + +/* +* GICv2 SGIs are terribly broken. We can't restore +* the source of the interrupt, so just pick the vcpu +* itself as the source...
Makes sense, this way you can't have an SGI coming from an offline CPU!
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +4
-0+*/ +if (is_vgic_v2_sgi (vcpu, irq)) + irq->source |=BIT (vcpu->vcpu_id); +vgic_queue_irq_unlock (vcpu->kvm, irq, flags);vgic_put_irq (vcpu->kvm, irq);@@ -423,7 +415,22 @@ int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu,struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);
virt/kvm/arm/vgic/vgic-mmio.c int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu, · line 424
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +7
-1raw_spin_lock_irqsave (&irq->irq_lock, flags); - irq->pending_latch = false; +/* +* More fun with GICv2 SGIs! If we're clearing one of them +* from userspace, which source vcpu to clear? Let's pick +* the target vcpu itself (consistent whith the way we +* populate them on the ISPENDR side), and only clear the +* pending state if no sources are left (insert expletive +* here).
But I'm not so sure about this. Doesn't this mean that user-space can't clear pending-SGI? Only if its pending due to self-SGI. I'm not sure when user-space would want to do this, so it may not matter.
Using ffs() you could clear the lowest pending source, I assume if its pending, there is likely only one source. If not, user-space can eventually clear pending SGI with at most nr-vcpu calls ... and ffs() could double up as the missing expletive!
(but if user-space never actually does this, then we should do the simplest thing)
virt/kvm/arm/vgic/vgic-mmio.c int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu, · line 436
Quoted 12 lines
virt/kvm/arm/vgic/vgic-mmio.c +9
-0+*/ +if (is_vgic_v2_sgi (vcpu, irq)) { + irq->source &= ~BIT (vcpu->vcpu_id); +if (!irq->source) + irq->pending_latch = false; + }else { + irq->pending_latch = false; + } +raw_spin_unlock_irqrestore (&irq->irq_lock, flags);vgic_put_irq (vcpu->kvm, irq);
Otherwise looks good to me,
Thanks,
James
[0]
https://static.docs.arm.com/ihi0069/f/IHI0069F_gic_architecture_specification_v3_and_v4.1.pdf
Marc Zyngier
maintainerRe: [PATCH v2 4/6] KVM: arm: vgic-v2: Only use the virtual state when userspace accesses pending bits
On Fri, 17 Apr 2020 17:48:34 +0100
James Morse <[email protected]> wrote:
Hi James,
Quoted 42 lines
Hi Marc,
Quoted 38 lines · Marc Zyngier
Quoted 30 lines · On Fri, 17 Apr 2020 12:22:10 +0100 James Morse <[email protected]> wrote:
Quoted 15 lines · Marc Zyngier
There is no point in accessing the HW when writing to any of the ISPENDR/ICPENDR registers from userspace, as only the guest should be allowed to change the HW state.
Introduce new userspace-specific accessors that deal solely with the virtual state. Note that the API differs from that of GICv3, where userspace exclusively uses ISPENDR to set the state. Too bad we can't reuse it.
virt/kvm/arm/vgic/vgic-mmio.c +0
-0@@ -321,6 +321,27 @@ void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_spending(struct kvm_vcpu *vcpu, · line 331
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +10
-0+int vgic_uaccess_write_spending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); +int i; +unsigned long flags; + +for_each_set_bit (i, &val, len *8 ) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);
vgic_mmio_write_spending()has:
| /*GICD_ISPENDR0SGI bits are WI *Quoted 2 lines
and bales out early. Is
GIC_DIST_PENDING_SETthe same register? (If so, shouldn't that be true for PPI too?)Hmmm. It's a bit more complicated (surprisingly).
Yes, the SGI pending bits are WI from the guest perspective (as required by the spec).
Quoted 7 lines
Quoted 4 lines
But we still need to be able to restore them
from userspace, and I bet82e40f558de56("KVM: arm/arm64: vgic-v2:
Handle SGI bits in GICD_I{S,C}PENDR0 as WI") has broken migration with GICv2 (if you migrated with a pending SGI, you cannot restore it...).Fun! It looks like the
ioctl()would succeed, but nothing happened. Once you restart the guest one CPU may wait forever for the victim to respond.
Yup. I can only see two reason for this not being reported: nobody tests live migration with GICv2 (most probable), or we're incredibly lucky by having never take a snapshot of a pending SGI. Either way, this needs fixing.
Quoted 65 lines
Quoted 6 lines
Now, there is still a bug here, in the sense that we need to indicate which vcpu is the source of the SGI (this is a GICv2-special). Unfortunately, we don't have a way to communicate this architecturally. The only option we have is to make it up (as a self-SGI, for example). But this is pretty broken at the architectural level TBH.
On the other hand, PPIs are just fine.Yup, wrong spec, I was looking at the same register in GICv3! It looks like the GICv3 text is there because those registers live in the redistributor instead... duh!
Quoted 31 lines
Quoted 14 lines
virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, · line 421
Quoted 12 lines
virt/kvm/arm/vgic/vgic-mmio.c +10
-0@@ -390,6 +411,26 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, +int vgic_uaccess_write_cpending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); +int i; +unsigned long flags; + +for_each_set_bit (i, &val, len *8 ) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);Same dumb question about
GICD_ICPENDR0!?Not dumb at all! Given that we previously allowed this to be accessed from userspace (well, before we broke it again), it should be able to clear *something*. If we adopt the self-SGI behaviour as above, we will get away with it.
Here's what I'm proposing to add to this patch, together with a
Fixes 82e40f558de56 ("KVM: arm/arm64: vgic-v2: Handle SGI bits in GICD_I{S,C}PENDR0 as WI")Nobody is using GICv2, obviously... :-/
virt/kvm/arm/vgic/vgic-mmio.c +0
-0virt/kvm/arm/vgic/vgic-mmio.c int vgic_uaccess_write_spending(struct kvm_vcpu *vcpu, · line 329
Quoted 8 lines
virt/kvm/arm/vgic/vgic-mmio.c +5
-0@@ -334,6 +322,15 @@ int vgic_uaccess_write_spending(struct kvm_vcpu *vcpu,raw_spin_lock_irqsave (&irq->irq_lock, flags); irq->pending_latch = true; + +/* +* GICv2 SGIs are terribly broken. We can't restore +* the source of the interrupt, so just pick the vcpu +* itself as the source... Makes sense, this way you can't have an SGI coming from an offline CPU!
Quoted 11 lines
virt/kvm/arm/vgic/vgic-mmio.c +4
-0+*/ +if (is_vgic_v2_sgi (vcpu, irq)) + irq->source |=BIT (vcpu->vcpu_id); +vgic_queue_irq_unlock (vcpu->kvm, irq, flags);vgic_put_irq (vcpu->kvm, irq);@@ -423,7 +415,22 @@ int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu,struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);
Quoted 13 lines
virt/kvm/arm/vgic/vgic-mmio.c int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu, · line 424
Quoted 9 lines
virt/kvm/arm/vgic/vgic-mmio.c +7
-1raw_spin_lock_irqsave (&irq->irq_lock, flags); - irq->pending_latch = false; +/* +* More fun with GICv2 SGIs! If we're clearing one of them +* from userspace, which source vcpu to clear? Let's pick +* the target vcpu itself (consistent whith the way we +* populate them on the ISPENDR side), and only clear the +* pending state if no sources are left (insert expletive +* here). But I'm not so sure about this. Doesn't this mean that user-space can't clear pending-SGI? Only if its pending due to self-SGI. I'm not sure when user-space would want to do this, so it may not matter.
In general, userspace just sets the pending bit, and doesn't bother clearing anything (because by default, there is nothing to clear).
Quoted 3 lines
Using
ffs()you could clear the lowest pending source, I assume if its pending, there is likely only one source. If not, user-space can eventually clear pending SGI with at most nr-vcpu calls ... andffs()could double up as the missing expletive!
;-)
Quoted 1 line
(but if user-space never actually does this, then we should do the simplest thing)
A third way would be to align on what GICv3 does, which is that ISPENDR is used for both setting and clearing in one go. Given that the current state it broken (and has been for some time now), I'm tempted to adopt the same behaviour...
What do you think?
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v2 4/6] KVM: arm: vgic-v2: Only use the virtual state when userspace accesses pending bits
Hi Marc,
Quoted 61 lines · Marc Zyngier
Quoted 40 lines · On Fri, 17 Apr 2020 17:48:34 +0100 James Morse <[email protected]> wrote:
Quoted 38 lines · Marc Zyngier
Quoted 30 lines · On Fri, 17 Apr 2020 12:22:10 +0100 James Morse <[email protected]> wrote:
Quoted 15 lines · Marc Zyngier
There is no point in accessing the HW when writing to any of the ISPENDR/ICPENDR registers from userspace, as only the guest should be allowed to change the HW state.
Introduce new userspace-specific accessors that deal solely with the virtual state. Note that the API differs from that of GICv3, where userspace exclusively uses ISPENDR to set the state. Too bad we can't reuse it.
virt/kvm/arm/vgic/vgic-mmio.c +0
-0@@ -321,6 +321,27 @@ void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,virt/kvm/arm/vgic/vgic-mmio.c void vgic_mmio_write_spending(struct kvm_vcpu *vcpu, · line 331
Quoted 10 lines
virt/kvm/arm/vgic/vgic-mmio.c +10
-0+int vgic_uaccess_write_spending (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len, +unsigned long val) +{ +u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); +int i; +unsigned long flags; + +for_each_set_bit (i, &val, len *8 ) { +struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);
vgic_mmio_write_spending()has:
| /*GICD_ISPENDR0SGI bits are WI *Quoted 2 lines
and bales out early. Is
GIC_DIST_PENDING_SETthe same register? (If so, shouldn't that be true for PPI too?)Hmmm. It's a bit more complicated (surprisingly).
Yes, the SGI pending bits are WI from the guest perspective (as required by the spec).
Quoted 7 lines
Quoted 4 lines
But we still need to be able to restore them
from userspace, and I bet82e40f558de56("KVM: arm/arm64: vgic-v2:
Handle SGI bits in GICD_I{S,C}PENDR0 as WI") has broken migration with GICv2 (if you migrated with a pending SGI, you cannot restore it...).Fun! It looks like the
ioctl()would succeed, but nothing happened. Once you restart the guest one CPU may wait forever for the victim to respond.Yup. I can only see two reason for this not being reported: nobody tests live migration with GICv2 (most probable), or we're incredibly lucky by having never take a snapshot of a pending SGI. Either way, this needs fixing.
Quoted 6 lines
Quoted 6 lines
Now, there is still a bug here, in the sense that we need to indicate which vcpu is the source of the SGI (this is a GICv2-special). Unfortunately, we don't have a way to communicate this architecturally. The only option we have is to make it up (as a self-SGI, for example). But this is pretty broken at the architectural level TBH.
On the other hand, PPIs are just fine.
[...]
Quoted 22 lines
Quoted 8 lines
Quoted 8 lines
Not dumb at all! Given that we previously allowed this to be accessed from userspace (well, before we broke it again), it should be able to clear *something*. If we adopt the self-SGI behaviour as above, we will get away with it.
virt/kvm/arm/vgic/vgic-mmio.c +0
-0@@ -423,7 +415,22 @@ int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu,struct vgic_irq *irq =vgic_get_irq (vcpu->kvm, vcpu, intid + i);Quoted 13 lines
virt/kvm/arm/vgic/vgic-mmio.c int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu, · line 424
Quoted 9 lines
virt/kvm/arm/vgic/vgic-mmio.c +7
-1raw_spin_lock_irqsave (&irq->irq_lock, flags); - irq->pending_latch = false; +/* +* More fun with GICv2 SGIs! If we're clearing one of them +* from userspace, which source vcpu to clear? Let's pick +* the target vcpu itself (consistent whith the way we +* populate them on the ISPENDR side), and only clear the +* pending state if no sources are left (insert expletive +* here). But I'm not so sure about this. Doesn't this mean that user-space can't clear pending-SGI? Only if its pending due to self-SGI. I'm not sure when user-space would want to do this, so it may not matter.
Quoted 4 lines
In general, userspace just sets the pending bit, and doesn't bother clearing anything (because by default, there is nothing to clear).
Quoted 1 line
(but if user-space never actually does this, then we should do the simplest thing)
Adding printk() to this combined patch and using 'loadvm' on the qemu console, I see Qemu writing '0xffffffff' into cpending to clear all 16 SGIs. I guess it is 'resetting' the in-kernel state to replace it with the state read from disk.
Quoted 6 lines
A third way would be to align on what GICv3 does, which is that ISPENDR is used for both setting and clearing in one go. Given that the current state it broken (and has been for some time now), I'm tempted to adopt the same behaviour...
What do you think?
I think Qemu is expecting the bank of cpending writes to clear whatever the kernel has stored, so that it can replay the new state. Ignoring the cpending writes means the kernel keeps an interrupt pending if nothing else in that 64bit group was set. Its not what Qemu expects, it looks like we'd get away with it, but I don't think we should do it!
I think we should let user-space write to those WI registers, and clearing the SGIs should clear all sources of SGI...
(N.B. I hit the original issue by typing 'loadvm' in the wrong terminal)
Thanks,
James
Marc Zyngier
maintainerRe: [PATCH v2 4/6] KVM: arm: vgic-v2: Only use the virtual state when userspace accesses pending bits
Hi James,
Quoted 5 lines · James Morse
Hi Marc,
Quoted 2 lines · Marc Zyngier
On Fri, 17 Apr 2020 17:48:34 +0100
James Morse <[email protected]> wrote:
[...]
Quoted 24 lines
Quoted 1 line
Quoted 1 line
(but if user-space never actually does this, then we should do the simplest thing)
Adding
printk()to this combined patch and using 'loadvm' on the qemu console, I see Qemu
writing '0xffffffff' into cpending to clear all 16 SGIs. I guess it is 'resetting' the
in-kernel state to replace it with the state read from disk.Quoted 6 lines
A third way would be to align on what GICv3 does, which is that ISPENDR is used for both setting and clearing in one go. Given that the current state it broken (and has been for some time now), I'm tempted to adopt the same behaviour...
What do you think?
I think Qemu is expecting the bank of cpending writes to clear whatever the kernel has
stored, so that it can replay the new state. Ignoring the cpending writes means the kernel
keeps an interrupt pending if nothing else in that 64bit group was set. Its not what Qemu
expects, it looks like we'd get away with it, but I don't think we should do it!
Quoted 2 lines
I think we should let user-space write to those WI registers, and clearing the SGIs should clear all sources of SGI...
I'd be happy with that. Let me rework the patch, and I'll post the series again shortly.
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Marc Zyngier
maintainerPATCH v2 5/6 KVM: arm64: vgic-v3: Retire all pending LPIs on vcpu destroy
In reply to Marc ZyngierFrom: Zenghui Yu <[email protected]>
It's likely that the vcpu fails to handle all virtual interrupts if userspace decides to destroy it, leaving the pending ones stay in the ap_list. If the un-handled one is a LPI, its vgic_irq structure will be eventually leaked because of an extra refcount increment in vgic_queue_irq_unlock().
This was detected by kmemleak on almost every guest destroy, the backtrace is as follows:
unreferenced object 0xffff80725aed5500 (size 128):
comm "CPU 5/KVM", pid 40711, jiffies 4298024754 (age 166366.512s) hex dump (first 32 bytes):
00 00 00 00 00 00 00 00 08 01 a9 73 6d 80 ff ff ...........sm... c8 61 ee a9 00 20 ff ff 28 1e 55 81 6c 80 ff ff .a... ..(.U.l... backtrace:
[<000000004bcaa122>] kmem_cache_alloc_trace+0x2dc/0x418 [<0000000069c7dabb>] vgic_add_lpi+0x88/0x418 [<00000000bfefd5c5>] vgic_its_cmd_handle_mapi+0x4dc/0x588 [<00000000cf993975>] vgic_its_process_commands.part.5+0x484/0x1198 [<000000004bd3f8e3>] vgic_its_process_commands+0x50/0x80 [<00000000b9a65b2b>] vgic_mmio_write_its_cwriter+0xac/0x108 [<0000000009641ebb>] dispatch_mmio_write+0xd0/0x188 [<000000008f79d288>] __kvm_io_bus_write+0x134/0x240 [<00000000882f39ac>] kvm_io_bus_write+0xe0/0x150 [<0000000078197602>] io_mem_abort+0x484/0x7b8 [<0000000060954e3c>] kvm_handle_guest_abort+0x4cc/0xa58 [<00000000e0d0cd65>] handle_exit+0x24c/0x770 [<00000000b44a7fad>] kvm_arch_vcpu_ioctl_run+0x460/0x1988 [<0000000025fb897c>] kvm_vcpu_ioctl+0x4f8/0xee0 [<000000003271e317>] do_vfs_ioctl+0x160/0xcd8 [<00000000e7f39607>] ksys_ioctl+0x98/0xd8
Fix it by retiring all pending LPIs in the ap_list on the destroy path.
p.s. I can also reproduce it on a normal guest shutdown. It is because userspace still send LPIs to vcpu (through KVM_SIGNAL_MSI ioctl) while the guest is being shutdown and unable to handle it. A little strange though and haven't dig further...
--- virt/kvm/arm/vgic/vgic-init.c | 6 ++++++ 1 file changed, 6 insertions(+)
virt/kvm/arm/vgic/vgic-init.c +6 -0
@@ -348,6 +348,12 @@ void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) {struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; +/* +* Retire all pending LPIs on this vcpu anyway as we're +* going to destroy it. +*/ +vgic_flush_pending_lpis (vcpu); +INIT_LIST_HEAD (&vgic_cpu->ap_list_head); }
Signature · 1 line
2.26.1
Marc Zyngier
maintainerPATCH v2 6/6 KVM: arm64: vgic-its: Fix memory leak on the error path of vgic_add_lpi()
In reply to Marc ZyngierFrom: Zenghui Yu <[email protected]>
If we're going to fail out the vgic_add_lpi(), let's make sure the allocated vgic_irq memory is also freed. Though it seems that both cases are unlikely to fail.
--- virt/kvm/arm/vgic/vgic-its.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +9 -2
@@ -96,14 +96,21 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid,* We "cache" the configuration table entries in our struct vgic_irq's. * However we only have those structs for mapped IRQs, so we read in * the respective config data from memory here upon mapping the LPI. +* +* Should any of these fail, behave as if we couldn't create the LPI +* by dropping the refcount and returning the error. */ ret =update_lpi_config (kvm, irq,NULL , false); -if (ret) +if (ret) { +vgic_put_irq (kvm, irq);return ERR_PTR (ret); + } ret =vgic_v3_lpi_sync_pending_status (kvm, irq); -if (ret) +if (ret) { +vgic_put_irq (kvm, irq);return ERR_PTR (ret); + }return irq; }
Signature · 1 line
2.26.1