Skip to contentSkip to conversation

KVM: arm/arm64: ensure vcpu target is unset on reset failure

PATCH In review Andrew Jones 2 participants 3 messages Apr 4, 2019 – Apr 5, 2019 KVM arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm/arm64: ensure vcpu target is unset on reset failure

A failed KVM_ARM_VCPU_INIT, should not set the vcpu target, as the vcpu target is used by kvm_vcpu_initialized() to determine if other vcpu ioctls may proceed. We need to set the target before calling kvm_reset_vcpu(), but if that call fails, we should then unset it.

---
 virt/kvm/arm/arm.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
virt/kvm/arm/arm.c +9 -3
index 99c37384ba7b..7e5724ae1efd 100644
@@ -934,7 +934,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
 			       const struct kvm_vcpu_init *init)
 {
-	unsigned int i;
+	unsigned int i, ret;
 	int phys_target = kvm_target_cpu();
 
 	if (init->target != phys_target)
@@ -969,9 +969,15 @@ static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
 	vcpu->arch.target = phys_target;
 
 	/* Now we know what it is, we can reset it. */
-	return kvm_reset_vcpu(vcpu);
-}
+	ret = kvm_reset_vcpu(vcpu);
+	if (ret) {
+		vcpu->arch.target = -1;
+		bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
+		return ret;
+	}
 
+	return 0;
+}
 
 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
 					 struct kvm_vcpu_init *init)
Signature · 1 line
2.20.1
#1 lore raw
Quoted 26 lines · Andrew Jones

A failed KVM_ARM_VCPU_INIT, should not set the vcpu target, as the vcpu target is used by kvm_vcpu_initialized() to determine if other vcpu ioctls may proceed. We need to set the target before calling kvm_reset_vcpu(), but if that call fails, we should then unset it.

---
 virt/kvm/arm/arm.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
virt/kvm/arm/arm.c +1 -1
index 99c37384ba7b..7e5724ae1efd 100644
@@ -934,7 +934,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
 			       const struct kvm_vcpu_init *init)
 {
-	unsigned int i;
+	unsigned int i, ret;
 	int phys_target = kvm_target_cpu();
 
 	if (init->target != phys_target)

virt/kvm/arm/arm.c static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, · line 976

Quoted 10 lines
virt/kvm/arm/arm.c +5 -2
@@ -969,9 +969,15 @@ static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
 	vcpu->arch.target = phys_target;
 
 	/* Now we know what it is, we can reset it. */
-	return kvm_reset_vcpu(vcpu);
-}
+	ret = kvm_reset_vcpu(vcpu);
+	if (ret) {
+		vcpu->arch.target = -1;
+		bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
+		return ret;

This return could trivially be removed...

virt/kvm/arm/arm.c static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, · line 980

Quoted 4 lines
virt/kvm/arm/arm.c +3 -0
+	}
 
+	return 0;
+}

... and this turned into 'return ret'.

I've tentatively applied this to the 5.1-fixes branch. Let me know if you're OK with it.

Thanks,

	M.
Signature · 1 line
Jazz is not dead, it just smell funny.
#2 lore raw
Quoted 43 lines · Marc Zyngier
Quoted 26 lines · Andrew Jones

A failed KVM_ARM_VCPU_INIT, should not set the vcpu target, as the vcpu target is used by kvm_vcpu_initialized() to determine if other vcpu ioctls may proceed. We need to set the target before calling kvm_reset_vcpu(), but if that call fails, we should then unset it.

---
 virt/kvm/arm/arm.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
virt/kvm/arm/arm.c +1 -1
index 99c37384ba7b..7e5724ae1efd 100644
@@ -934,7 +934,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
 			       const struct kvm_vcpu_init *init)
 {
-	unsigned int i;
+	unsigned int i, ret;
 	int phys_target = kvm_target_cpu();
 
 	if (init->target != phys_target)

virt/kvm/arm/arm.c static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, · line 976

Quoted 10 lines
virt/kvm/arm/arm.c +5 -2
@@ -969,9 +969,15 @@ static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
 	vcpu->arch.target = phys_target;
 
 	/* Now we know what it is, we can reset it. */
-	return kvm_reset_vcpu(vcpu);
-}
+	ret = kvm_reset_vcpu(vcpu);
+	if (ret) {
+		vcpu->arch.target = -1;
+		bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
+		return ret;

This return could trivially be removed...

Quoted 2 lines
virt/kvm/arm/arm.c +1 -0
+	}
 
Quoted 8 lines

virt/kvm/arm/arm.c static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, · line 980

Quoted 2 lines
virt/kvm/arm/arm.c +2 -0
+	return 0;
+}

... and this turned into 'return ret'.

I've tentatively applied this to the 5.1-fixes branch. Let me know if you're OK with it.

Either way is fine by me. I actually did it this way on purpose though because I preferred the way the explicit 'return 0' at the bottom documented that we were sure at that point of success, so no longer needed to be concerned that target should be reset to -1.

Thanks,
drew