[v2] KVM: s390: interrupt: use READ_ONCE() before cmpxchg()

Message ID 20230109145456.2895385-1-hca@linux.ibm.com
State New
Headers
Series [v2] KVM: s390: interrupt: use READ_ONCE() before cmpxchg() |

Commit Message

Heiko Carstens Jan. 9, 2023, 2:54 p.m. UTC
  Use READ_ONCE() before cmpxchg() to prevent that the compiler generates
code that fetches the to be compared old value several times from memory.

Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
---

v2: make it compile

arch/s390/kvm/interrupt.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
  

Comments

Claudio Imbrenda Jan. 9, 2023, 4:03 p.m. UTC | #1
On Mon,  9 Jan 2023 15:54:56 +0100
Heiko Carstens <hca@linux.ibm.com> wrote:

> Use READ_ONCE() before cmpxchg() to prevent that the compiler generates
> code that fetches the to be compared old value several times from memory.
> 
> Signed-off-by: Heiko Carstens <hca@linux.ibm.com>

Reviewed-by: Claudio Imbrenda <imbrenda@linux.ibm.com>

> ---
> 
> v2: make it compile
> 
> arch/s390/kvm/interrupt.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/s390/kvm/interrupt.c b/arch/s390/kvm/interrupt.c
> index 1dae78deddf2..ab26aa53ee37 100644
> --- a/arch/s390/kvm/interrupt.c
> +++ b/arch/s390/kvm/interrupt.c
> @@ -83,8 +83,9 @@ static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
>  		struct esca_block *sca = vcpu->kvm->arch.sca;
>  		union esca_sigp_ctrl *sigp_ctrl =
>  			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
> -		union esca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
> +		union esca_sigp_ctrl new_val = {0}, old_val;
>  
> +		old_val = READ_ONCE(*sigp_ctrl);
>  		new_val.scn = src_id;
>  		new_val.c = 1;
>  		old_val.c = 0;
> @@ -95,8 +96,9 @@ static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
>  		struct bsca_block *sca = vcpu->kvm->arch.sca;
>  		union bsca_sigp_ctrl *sigp_ctrl =
>  			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
> -		union bsca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
> +		union bsca_sigp_ctrl new_val = {0}, old_val;
>  
> +		old_val = READ_ONCE(*sigp_ctrl);
>  		new_val.scn = src_id;
>  		new_val.c = 1;
>  		old_val.c = 0;
> @@ -126,16 +128,18 @@ static void sca_clear_ext_call(struct kvm_vcpu *vcpu)
>  		struct esca_block *sca = vcpu->kvm->arch.sca;
>  		union esca_sigp_ctrl *sigp_ctrl =
>  			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
> -		union esca_sigp_ctrl old = *sigp_ctrl;
> +		union esca_sigp_ctrl old;
>  
> +		old = READ_ONCE(*sigp_ctrl);
>  		expect = old.value;
>  		rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
>  	} else {
>  		struct bsca_block *sca = vcpu->kvm->arch.sca;
>  		union bsca_sigp_ctrl *sigp_ctrl =
>  			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
> -		union bsca_sigp_ctrl old = *sigp_ctrl;
> +		union bsca_sigp_ctrl old;
>  
> +		old = READ_ONCE(*sigp_ctrl);
>  		expect = old.value;
>  		rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
>  	}
  
Christian Borntraeger Jan. 10, 2023, 7:15 p.m. UTC | #2
Am 09.01.23 um 15:54 schrieb Heiko Carstens:
> Use READ_ONCE() before cmpxchg() to prevent that the compiler generates
> code that fetches the to be compared old value several times from memory.
> 
> Signed-off-by: Heiko Carstens <hca@linux.ibm.com>

Looks sane.

Reviewed-by: Christian Borntraeger <borntraeger@linux.ibm.com>

In case you want to take it via the s390 tree:
Acked-by: Christian Borntraeger <borntraeger@linux.ibm.com>

> ---
> 
> v2: make it compile
> 
> arch/s390/kvm/interrupt.c | 12 ++++++++----
>   1 file changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/s390/kvm/interrupt.c b/arch/s390/kvm/interrupt.c
> index 1dae78deddf2..ab26aa53ee37 100644
> --- a/arch/s390/kvm/interrupt.c
> +++ b/arch/s390/kvm/interrupt.c
> @@ -83,8 +83,9 @@ static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
>   		struct esca_block *sca = vcpu->kvm->arch.sca;
>   		union esca_sigp_ctrl *sigp_ctrl =
>   			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
> -		union esca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
> +		union esca_sigp_ctrl new_val = {0}, old_val;
>   
> +		old_val = READ_ONCE(*sigp_ctrl);
>   		new_val.scn = src_id;
>   		new_val.c = 1;
>   		old_val.c = 0;
> @@ -95,8 +96,9 @@ static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
>   		struct bsca_block *sca = vcpu->kvm->arch.sca;
>   		union bsca_sigp_ctrl *sigp_ctrl =
>   			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
> -		union bsca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
> +		union bsca_sigp_ctrl new_val = {0}, old_val;
>   
> +		old_val = READ_ONCE(*sigp_ctrl);
>   		new_val.scn = src_id;
>   		new_val.c = 1;
>   		old_val.c = 0;
> @@ -126,16 +128,18 @@ static void sca_clear_ext_call(struct kvm_vcpu *vcpu)
>   		struct esca_block *sca = vcpu->kvm->arch.sca;
>   		union esca_sigp_ctrl *sigp_ctrl =
>   			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
> -		union esca_sigp_ctrl old = *sigp_ctrl;
> +		union esca_sigp_ctrl old;
>   
> +		old = READ_ONCE(*sigp_ctrl);
>   		expect = old.value;
>   		rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
>   	} else {
>   		struct bsca_block *sca = vcpu->kvm->arch.sca;
>   		union bsca_sigp_ctrl *sigp_ctrl =
>   			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
> -		union bsca_sigp_ctrl old = *sigp_ctrl;
> +		union bsca_sigp_ctrl old;
>   
> +		old = READ_ONCE(*sigp_ctrl);
>   		expect = old.value;
>   		rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
>   	}
  
Heiko Carstens Jan. 11, 2023, 12:11 p.m. UTC | #3
On Tue, Jan 10, 2023 at 08:15:50PM +0100, Christian Borntraeger wrote:
> Am 09.01.23 um 15:54 schrieb Heiko Carstens:
> > Use READ_ONCE() before cmpxchg() to prevent that the compiler generates
> > code that fetches the to be compared old value several times from memory.
> > 
> > Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
> 
> Looks sane.
> 
> Reviewed-by: Christian Borntraeger <borntraeger@linux.ibm.com>
> 
> In case you want to take it via the s390 tree:
> Acked-by: Christian Borntraeger <borntraeger@linux.ibm.com>

Given that there don't seem to be any pending s390 specific patches I
take it via the s390 tree.

Applied.
  

Patch

diff --git a/arch/s390/kvm/interrupt.c b/arch/s390/kvm/interrupt.c
index 1dae78deddf2..ab26aa53ee37 100644
--- a/arch/s390/kvm/interrupt.c
+++ b/arch/s390/kvm/interrupt.c
@@ -83,8 +83,9 @@  static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
 		struct esca_block *sca = vcpu->kvm->arch.sca;
 		union esca_sigp_ctrl *sigp_ctrl =
 			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
-		union esca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
+		union esca_sigp_ctrl new_val = {0}, old_val;
 
+		old_val = READ_ONCE(*sigp_ctrl);
 		new_val.scn = src_id;
 		new_val.c = 1;
 		old_val.c = 0;
@@ -95,8 +96,9 @@  static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
 		struct bsca_block *sca = vcpu->kvm->arch.sca;
 		union bsca_sigp_ctrl *sigp_ctrl =
 			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
-		union bsca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
+		union bsca_sigp_ctrl new_val = {0}, old_val;
 
+		old_val = READ_ONCE(*sigp_ctrl);
 		new_val.scn = src_id;
 		new_val.c = 1;
 		old_val.c = 0;
@@ -126,16 +128,18 @@  static void sca_clear_ext_call(struct kvm_vcpu *vcpu)
 		struct esca_block *sca = vcpu->kvm->arch.sca;
 		union esca_sigp_ctrl *sigp_ctrl =
 			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
-		union esca_sigp_ctrl old = *sigp_ctrl;
+		union esca_sigp_ctrl old;
 
+		old = READ_ONCE(*sigp_ctrl);
 		expect = old.value;
 		rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
 	} else {
 		struct bsca_block *sca = vcpu->kvm->arch.sca;
 		union bsca_sigp_ctrl *sigp_ctrl =
 			&(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
-		union bsca_sigp_ctrl old = *sigp_ctrl;
+		union bsca_sigp_ctrl old;
 
+		old = READ_ONCE(*sigp_ctrl);
 		expect = old.value;
 		rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
 	}