Skip to contentSkip to conversation

KVM: s390/diag: fix racy access of physical cpu number in diag 9c handler

PATCH In review Christian Borntraeger 3 participants 3 messages May 23, 2023

Subsystems (3)
lore
Share & tools
lore

Christian Borntraeger

author maintainer
#0 lore raw

PATCH 1/1 KVM: s390/diag: fix racy access of physical cpu number in diag 9c handler

We do check for target CPU == -1, but this might change at the time we are going to use it. Hold the physical target CPU in a local variable to avoid out-of-bound accesses to the cpu arrays.

Cc Pierre Morel <[email protected]>
Fixes 87e28a15c42c ("KVM: s390: diag9c (directed yield) forwarding")
---
 arch/s390/kvm/diag.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/s390/kvm/diag.c +5 -3
index 807fa9da1e72..3c65b8258ae6 100644
@@ -166,6 +166,7 @@ static int diag9c_forwarding_overrun(void)
 static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
 {
 	struct kvm_vcpu *tcpu;
+	int tcpu_cpu;
 	int tid;
 
 	tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
@@ -181,14 +182,15 @@ static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
 		goto no_yield;
 
 	/* target guest VCPU already running */
-	if (READ_ONCE(tcpu->cpu) >= 0) {
+	tcpu_cpu = READ_ONCE(tcpu->cpu);
+	if (tcpu_cpu >= 0) {
 		if (!diag9c_forwarding_hz || diag9c_forwarding_overrun())
 			goto no_yield;
 
 		/* target host CPU already running */
-		if (!vcpu_is_preempted(tcpu->cpu))
+		if (!vcpu_is_preempted(tcpu_cpu))
 			goto no_yield;
-		smp_yield_cpu(tcpu->cpu);
+		smp_yield_cpu(tcpu_cpu);
 		VCPU_EVENT(vcpu, 5,
 			   "diag time slice end directed to %d: yield forwarded",
 			   tid);
Signature · 1 line
2.40.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 35 lines · Christian Borntraeger

We do check for target CPU == -1, but this might change at the time we are going to use it. Hold the physical target CPU in a local variable to avoid out-of-bound accesses to the cpu arrays.

Cc Pierre Morel <[email protected]>
Fixes 87e28a15c42c ("KVM: s390: diag9c (directed yield) forwarding")
---
  arch/s390/kvm/diag.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
arch/s390/kvm/diag.c +3 -1
index 807fa9da1e72..3c65b8258ae6 100644
@@ -166,6 +166,7 @@ static int diag9c_forwarding_overrun(void)
  static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
  {
  	struct kvm_vcpu *tcpu;
+	int tcpu_cpu;
  	int tid;
 
  	tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
@@ -181,14 +182,15 @@ static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
  		goto no_yield;
 
  	/* target guest VCPU already running */
-	if (READ_ONCE(tcpu->cpu) >= 0) {
+	tcpu_cpu = READ_ONCE(tcpu->cpu);
+	if (tcpu_cpu >= 0) {
  		if (!diag9c_forwarding_hz || diag9c_forwarding_overrun())
  			goto no_yield;

arch/s390/kvm/diag.c static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu) · line 196

Quoted 10 lines
arch/s390/kvm/diag.c +2 -2
 
  		/* target host CPU already running */
-		if (!vcpu_is_preempted(tcpu->cpu))
+		if (!vcpu_is_preempted(tcpu_cpu))
  			goto no_yield;
-		smp_yield_cpu(tcpu->cpu);
+		smp_yield_cpu(tcpu_cpu);
  		VCPU_EVENT(vcpu, 5,
  			   "diag time slice end directed to %d: yield forwarded",
  			   tid);
Signature · 3 lines
Thanks,

David / dhildenb
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Christian Borntraeger
Quoted 9 lines · On Tue, 23 May 2023 16:05:00 +0200 Christian Borntraeger…

We do check for target CPU == -1, but this might change at the time we are going to use it. Hold the physical target CPU in a local variable to avoid out-of-bound accesses to the cpu arrays.

Cc Pierre Morel <[email protected]>
Fixes 87e28a15c42c ("KVM: s390: diag9c (directed yield) forwarding")
Quoted 36 lines
---
 arch/s390/kvm/diag.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/s390/kvm/diag.c +5 -3
index 807fa9da1e72..3c65b8258ae6 100644
@@ -166,6 +166,7 @@ static int diag9c_forwarding_overrun(void)
 static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
 {
 	struct kvm_vcpu *tcpu;
+	int tcpu_cpu;
 	int tid;
 
 	tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
@@ -181,14 +182,15 @@ static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
 		goto no_yield;
 
 	/* target guest VCPU already running */
-	if (READ_ONCE(tcpu->cpu) >= 0) {
+	tcpu_cpu = READ_ONCE(tcpu->cpu);
+	if (tcpu_cpu >= 0) {
 		if (!diag9c_forwarding_hz || diag9c_forwarding_overrun())
 			goto no_yield;
 
 		/* target host CPU already running */
-		if (!vcpu_is_preempted(tcpu->cpu))
+		if (!vcpu_is_preempted(tcpu_cpu))
 			goto no_yield;
-		smp_yield_cpu(tcpu->cpu);
+		smp_yield_cpu(tcpu_cpu);
 		VCPU_EVENT(vcpu, 5,
 			   "diag time slice end directed to %d: yield forwarded",
 			   tid);