KVM: s390x: fix SCK locking
Claudio Imbrenda
maintainerPATCH 1/1 KVM: s390x: fix SCK locking
When handling the SCK instruction, the kvm lock is taken, even though the vcpu lock is already being held. The normal locking order is kvm lock first and then vcpu lock. This is can (and in some circumstances does) lead to deadlocks.
The function kvm_s390_set_tod_clock is called both by the SCK handler and by some IOCTLs to set the clock. The IOCTLs will not hold the vcpu lock, so they can safely take the kvm lock. The SCK handler holds the vcpu lock, but will also somehow need to acquire the kvm lock without relinquishing the vcpu lock.
The solution is to factor out the code to set the clock, and provide two wrappers. One is called like the original function and does the locking, the other is called kvm_s390_try_set_tod_clock and uses trylock to try to acquire the kvm lock. This new wrapper is then used in the SCK handler. If locking fails, -EAGAIN is returned, which is eventually propagated to userspace, thus also freeing the vcpu lock and allowing for forward progress.
This is not the most efficient or elegant way to solve this issue, but the SCK instruction is deprecated and its performance is not critical.
The goal of this patch is just to provide a simple but correct way to fix the bug.
--- arch/s390/kvm/kvm-s390.c | 19 ++++++++++++++++--- arch/s390/kvm/kvm-s390.h | 4 ++-- arch/s390/kvm/priv.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-)
arch/s390/kvm/kvm-s390.c +16 -3
@@ -3869,14 +3869,12 @@ static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)return 0 ; } -void kvm_s390_set_tod_clock (struct kvm *kvm, -const struct kvm_s390_vm_tod_clock *gtod) +static void __kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) {struct kvm_vcpu *vcpu;union tod_clock clk;unsigned long i; -mutex_lock (&kvm->lock);preempt_disable ();store_tod_clock_ext (&clk);
@@ -3897,7 +3895,22 @@ void kvm_s390_set_tod_clock(struct kvm *kvm,kvm_s390_vcpu_unblock_all (kvm);preempt_enable (); +} + +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ +mutex_lock (&kvm->lock); +__kvm_s390_set_tod_clock (kvm, gtod); +mutex_unlock (&kvm->lock); +} + +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ +if (!mutex_trylock (&kvm->lock)) +return 0 ; +__kvm_s390_set_tod_clock (kvm, gtod);mutex_unlock (&kvm->lock); +return 1 ; }/**
arch/s390/kvm/kvm-s390.h +2 -2
@@ -349,8 +349,8 @@ int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);int kvm_s390_handle_sigp_pei (struct kvm_vcpu *vcpu);/* implemented in kvm-s390.c */ -void kvm_s390_set_tod_clock (struct kvm *kvm, -const struct kvm_s390_vm_tod_clock *gtod); +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod); +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod);long kvm_arch_fault_in_page (struct kvm_vcpu *vcpu,gpa_t gpa,int writable);int kvm_s390_store_status_unloaded (struct kvm_vcpu *vcpu,unsigned long addr);int kvm_s390_vcpu_store_status (struct kvm_vcpu *vcpu,unsigned long addr);
arch/s390/kvm/priv.c +13 -1
@@ -102,7 +102,19 @@ static int handle_set_clock(struct kvm_vcpu *vcpu)return kvm_s390_inject_prog_cond (vcpu, rc);VCPU_EVENT (vcpu,3 ,"SCK: setting guest TOD to 0x%llx" , gtod.tod); -kvm_s390_set_tod_clock (vcpu->kvm, >od); +/* +* To set the TOD clock we need to take the kvm lock, but we are +* already holding the vcpu lock, and the usual lock order is the +* opposite. Therefore we use trylock instead of lock, and if the +* kvm lock cannot be taken, we retry the instruction and return +* -EAGAIN to userspace, thus freeing the vcpu lock. +* The SCK instruction is considered legacy and at this point it's +* not worth the effort to find a nicer solution. +*/ +if (!kvm_s390_try_set_tod_clock (vcpu->kvm, >od)) { +kvm_s390_retry_instr (vcpu); +return -EAGAIN ; + }kvm_s390_set_psw_cc (vcpu,0 );return 0 ;
Signature · 1 line
2.34.1
Quoted 19 lines · Claudio Imbrenda
When handling the SCK instruction, the kvm lock is taken, even though the vcpu lock is already being held. The normal locking order is kvm lock first and then vcpu lock. This is can (and in some circumstances does) lead to deadlocks.
The function
kvm_s390_set_tod_clockis called both by the SCK handler and by some IOCTLs to set the clock. The IOCTLs will not hold the vcpu lock, so they can safely take the kvm lock. The SCK handler holds the vcpu lock, but will also somehow need to acquire the kvm lock without relinquishing the vcpu lock.The solution is to factor out the code to set the clock, and provide two wrappers. One is called like the original function and does the locking, the other is called
kvm_s390_try_set_tod_clockand uses trylock to try to acquire the kvm lock. This new wrapper is then used in the SCK handler. If locking fails,-EAGAINis returned, which is eventually propagated to userspace, thus also freeing the vcpu lock and allowing for forward progress.
Quoted 8 lines
This is not the most efficient or elegant way to solve this issue, but the SCK instruction is deprecated and its performance is not critical.
The goal of this patch is just to provide a simple but correct way to fix the bug.
Fixes 6a3f95a6b04c ("KVM: s390: Intercept SCK instruction")Signed-off-by Claudio Imbrenda <[email protected]>
Quoted 30 lines
--- arch/s390/kvm/kvm-s390.c | 19 ++++++++++++++++--- arch/s390/kvm/kvm-s390.h | 4 ++-- arch/s390/kvm/priv.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-)arch/s390/kvm/kvm-s390.c +1
-3@@ -3869,14 +3869,12 @@ static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)return 0 ; } -void kvm_s390_set_tod_clock (struct kvm *kvm, -const struct kvm_s390_vm_tod_clock *gtod) +static void __kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) {struct kvm_vcpu *vcpu;union tod_clock clk;unsigned long i; -mutex_lock (&kvm->lock);preempt_disable ();store_tod_clock_ext (&clk);@@ -3897,7 +3895,22 @@ void kvm_s390_set_tod_clock(struct kvm *kvm,kvm_s390_vcpu_unblock_all (kvm);preempt_enable ();
arch/s390/kvm/kvm-s390.c void kvm_s390_set_tod_clock(struct kvm *kvm, · line 3907
Quoted 10 lines
arch/s390/kvm/kvm-s390.c +10
-0+} + +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ +mutex_lock (&kvm->lock); +__kvm_s390_set_tod_clock (kvm, gtod); +mutex_unlock (&kvm->lock); +} + +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod)
Why int instead of bool?
Quoted 8 lines
arch/s390/kvm/kvm-s390.c +5
-0+{ +if (!mutex_trylock (&kvm->lock)) +return 0 ; +__kvm_s390_set_tod_clock (kvm, gtod);mutex_unlock (&kvm->lock); +return 1 ; }
[...]
Claudio Imbrenda
maintainerQuoted 58 lines · On Wed, 2 Mar 2022 11:15:23 +0100 Janis Schoetterl-Glausch…
Quoted 19 lines · Claudio Imbrenda
When handling the SCK instruction, the kvm lock is taken, even though the vcpu lock is already being held. The normal locking order is kvm lock first and then vcpu lock. This is can (and in some circumstances does) lead to deadlocks.
The function
kvm_s390_set_tod_clockis called both by the SCK handler and by some IOCTLs to set the clock. The IOCTLs will not hold the vcpu lock, so they can safely take the kvm lock. The SCK handler holds the vcpu lock, but will also somehow need to acquire the kvm lock without relinquishing the vcpu lock.The solution is to factor out the code to set the clock, and provide two wrappers. One is called like the original function and does the locking, the other is called
kvm_s390_try_set_tod_clockand uses trylock to try to acquire the kvm lock. This new wrapper is then used in the SCK handler. If locking fails,-EAGAINis returned, which is eventually propagated to userspace, thus also freeing the vcpu lock and allowing for forward progress.Quoted 8 lines
This is not the most efficient or elegant way to solve this issue, but the SCK instruction is deprecated and its performance is not critical.
The goal of this patch is just to provide a simple but correct way to fix the bug.
Fixes 6a3f95a6b04c ("KVM: s390: Intercept SCK instruction")Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Janis Schoetterl-Glausch <[email protected]>Quoted 28 lines
--- arch/s390/kvm/kvm-s390.c | 19 ++++++++++++++++--- arch/s390/kvm/kvm-s390.h | 4 ++-- arch/s390/kvm/priv.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-)arch/s390/kvm/kvm-s390.c +1
-3@@ -3869,14 +3869,12 @@ static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)return 0 ; } -void kvm_s390_set_tod_clock (struct kvm *kvm, -const struct kvm_s390_vm_tod_clock *gtod) +static void __kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) {struct kvm_vcpu *vcpu;union tod_clock clk;unsigned long i; -mutex_lock (&kvm->lock);preempt_disable ();store_tod_clock_ext (&clk);@@ -3897,7 +3895,22 @@ void kvm_s390_set_tod_clock(struct kvm *kvm,
Quoted 14 lines
arch/s390/kvm/kvm-s390.c void kvm_s390_set_tod_clock(struct kvm *kvm, · line 3907
Quoted 12 lines
arch/s390/kvm/kvm-s390.c +10
-0kvm_s390_vcpu_unblock_all (kvm);preempt_enable (); +} + +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ +mutex_lock (&kvm->lock); +__kvm_s390_set_tod_clock (kvm, gtod); +mutex_unlock (&kvm->lock); +} + +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod)Why int instead of bool?
to be consistent with mutex_trylock, which also returns int
Quoted 10 lines
Quoted 9 lines
arch/s390/kvm/kvm-s390.c +5
-0+{ +if (!mutex_trylock (&kvm->lock)) +return 0 ; +__kvm_s390_set_tod_clock (kvm, gtod);mutex_unlock (&kvm->lock); +return 1 ; }[...]
Am 01.03.22 um 15:33 schrieb Claudio Imbrenda:
Quoted 19 lines
When handling the SCK instruction, the kvm lock is taken, even though the vcpu lock is already being held. The normal locking order is kvm lock first and then vcpu lock. This is can (and in some circumstances does) lead to deadlocks.
The function
kvm_s390_set_tod_clockis called both by the SCK handler and by some IOCTLs to set the clock. The IOCTLs will not hold the vcpu lock, so they can safely take the kvm lock. The SCK handler holds the vcpu lock, but will also somehow need to acquire the kvm lock without relinquishing the vcpu lock.The solution is to factor out the code to set the clock, and provide two wrappers. One is called like the original function and does the locking, the other is called
kvm_s390_try_set_tod_clockand uses trylock to try to acquire the kvm lock. This new wrapper is then used in the SCK handler. If locking fails,-EAGAINis returned, which is eventually propagated to userspace, thus also freeing the vcpu lock and allowing for forward progress.
Quoted 8 lines
This is not the most efficient or elegant way to solve this issue, but the SCK instruction is deprecated and its performance is not critical.
The goal of this patch is just to provide a simple but correct way to fix the bug.
Fixes 6a3f95a6b04c ("KVM: s390: Intercept SCK instruction")Signed-off-by Claudio Imbrenda <[email protected]>
Quoted 89 lines
--- arch/s390/kvm/kvm-s390.c | 19 ++++++++++++++++--- arch/s390/kvm/kvm-s390.h | 4 ++-- arch/s390/kvm/priv.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-)arch/s390/kvm/kvm-s390.c +16
-3@@ -3869,14 +3869,12 @@ static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)return 0 ; } -void kvm_s390_set_tod_clock (struct kvm *kvm, -const struct kvm_s390_vm_tod_clock *gtod) +static void __kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) {struct kvm_vcpu *vcpu;union tod_clock clk;unsigned long i; -mutex_lock (&kvm->lock);preempt_disable ();store_tod_clock_ext (&clk);@@ -3897,7 +3895,22 @@ void kvm_s390_set_tod_clock(struct kvm *kvm,kvm_s390_vcpu_unblock_all (kvm);preempt_enable (); +} + +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ +mutex_lock (&kvm->lock); +__kvm_s390_set_tod_clock (kvm, gtod); +mutex_unlock (&kvm->lock); +} + +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ +if (!mutex_trylock (&kvm->lock)) +return 0 ; +__kvm_s390_set_tod_clock (kvm, gtod);mutex_unlock (&kvm->lock); +return 1 ; }/** arch/s390/kvm/kvm-s390.h +2
-2@@ -349,8 +349,8 @@ int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);int kvm_s390_handle_sigp_pei (struct kvm_vcpu *vcpu);/* implemented in kvm-s390.c */ -void kvm_s390_set_tod_clock (struct kvm *kvm, -const struct kvm_s390_vm_tod_clock *gtod); +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod); +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod);long kvm_arch_fault_in_page (struct kvm_vcpu *vcpu,gpa_t gpa,int writable);int kvm_s390_store_status_unloaded (struct kvm_vcpu *vcpu,unsigned long addr);int kvm_s390_vcpu_store_status (struct kvm_vcpu *vcpu,unsigned long addr);arch/s390/kvm/priv.c +13
-1@@ -102,7 +102,19 @@ static int handle_set_clock(struct kvm_vcpu *vcpu)return kvm_s390_inject_prog_cond (vcpu, rc);VCPU_EVENT (vcpu,3 ,"SCK: setting guest TOD to 0x%llx" , gtod.tod); -kvm_s390_set_tod_clock (vcpu->kvm, >od); +/* +* To set the TOD clock we need to take the kvm lock, but we are +* already holding the vcpu lock, and the usual lock order is the +* opposite. Therefore we use trylock instead of lock, and if the +* kvm lock cannot be taken, we retry the instruction and return +* -EAGAIN to userspace, thus freeing the vcpu lock. +* The SCK instruction is considered legacy and at this point it's +* not worth the effort to find a nicer solution. +*/ +if (!kvm_s390_try_set_tod_clock (vcpu->kvm, >od)) { +kvm_s390_retry_instr (vcpu); +return -EAGAIN ; + }kvm_s390_set_psw_cc (vcpu,0 );return 0 ;
Christian Borntraeger
AppliedAm 01.03.22 um 15:33 schrieb Claudio Imbrenda:
Quoted 19 lines
When handling the SCK instruction, the kvm lock is taken, even though the vcpu lock is already being held. The normal locking order is kvm lock first and then vcpu lock. This is can (and in some circumstances does) lead to deadlocks.
The function
kvm_s390_set_tod_clockis called both by the SCK handler and by some IOCTLs to set the clock. The IOCTLs will not hold the vcpu lock, so they can safely take the kvm lock. The SCK handler holds the vcpu lock, but will also somehow need to acquire the kvm lock without relinquishing the vcpu lock.The solution is to factor out the code to set the clock, and provide two wrappers. One is called like the original function and does the locking, the other is called
kvm_s390_try_set_tod_clockand uses trylock to try to acquire the kvm lock. This new wrapper is then used in the SCK handler. If locking fails,-EAGAINis returned, which is eventually propagated to userspace, thus also freeing the vcpu lock and allowing for forward progress.
Quoted 8 lines
This is not the most efficient or elegant way to solve this issue, but the SCK instruction is deprecated and its performance is not critical.
The goal of this patch is just to provide a simple but correct way to fix the bug.
Fixes 6a3f95a6b04c ("KVM: s390: Intercept SCK instruction")Signed-off-by Claudio Imbrenda <[email protected]>
Thanks applied. I will also add cc stable when queueing.
Quoted 89 lines
--- arch/s390/kvm/kvm-s390.c | 19 ++++++++++++++++--- arch/s390/kvm/kvm-s390.h | 4 ++-- arch/s390/kvm/priv.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-)arch/s390/kvm/kvm-s390.c +16
-3@@ -3869,14 +3869,12 @@ static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)return 0 ; } -void kvm_s390_set_tod_clock (struct kvm *kvm, -const struct kvm_s390_vm_tod_clock *gtod) +static void __kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) {struct kvm_vcpu *vcpu;union tod_clock clk;unsigned long i; -mutex_lock (&kvm->lock);preempt_disable ();store_tod_clock_ext (&clk);@@ -3897,7 +3895,22 @@ void kvm_s390_set_tod_clock(struct kvm *kvm,kvm_s390_vcpu_unblock_all (kvm);preempt_enable (); +} + +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ +mutex_lock (&kvm->lock); +__kvm_s390_set_tod_clock (kvm, gtod); +mutex_unlock (&kvm->lock); +} + +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ +if (!mutex_trylock (&kvm->lock)) +return 0 ; +__kvm_s390_set_tod_clock (kvm, gtod);mutex_unlock (&kvm->lock); +return 1 ; }/** arch/s390/kvm/kvm-s390.h +2
-2@@ -349,8 +349,8 @@ int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);int kvm_s390_handle_sigp_pei (struct kvm_vcpu *vcpu);/* implemented in kvm-s390.c */ -void kvm_s390_set_tod_clock (struct kvm *kvm, -const struct kvm_s390_vm_tod_clock *gtod); +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod); +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod);long kvm_arch_fault_in_page (struct kvm_vcpu *vcpu,gpa_t gpa,int writable);int kvm_s390_store_status_unloaded (struct kvm_vcpu *vcpu,unsigned long addr);int kvm_s390_vcpu_store_status (struct kvm_vcpu *vcpu,unsigned long addr);arch/s390/kvm/priv.c +13
-1@@ -102,7 +102,19 @@ static int handle_set_clock(struct kvm_vcpu *vcpu)return kvm_s390_inject_prog_cond (vcpu, rc);VCPU_EVENT (vcpu,3 ,"SCK: setting guest TOD to 0x%llx" , gtod.tod); -kvm_s390_set_tod_clock (vcpu->kvm, >od); +/* +* To set the TOD clock we need to take the kvm lock, but we are +* already holding the vcpu lock, and the usual lock order is the +* opposite. Therefore we use trylock instead of lock, and if the +* kvm lock cannot be taken, we retry the instruction and return +* -EAGAIN to userspace, thus freeing the vcpu lock. +* The SCK instruction is considered legacy and at this point it's +* not worth the effort to find a nicer solution. +*/ +if (!kvm_s390_try_set_tod_clock (vcpu->kvm, >od)) { +kvm_s390_retry_instr (vcpu); +return -EAGAIN ; + }kvm_s390_set_psw_cc (vcpu,0 );return 0 ;
Am 01.03.22 um 15:33 schrieb Claudio Imbrenda:
Quoted 99 lines
When handling the SCK instruction, the kvm lock is taken, even though the vcpu lock is already being held. The normal locking order is kvm lock first and then vcpu lock. This is can (and in some circumstances does) lead to deadlocks.
The function
kvm_s390_set_tod_clockis called both by the SCK handler and by some IOCTLs to set the clock. The IOCTLs will not hold the vcpu lock, so they can safely take the kvm lock. The SCK handler holds the vcpu lock, but will also somehow need to acquire the kvm lock without relinquishing the vcpu lock.The solution is to factor out the code to set the clock, and provide two wrappers. One is called like the original function and does the locking, the other is called
kvm_s390_try_set_tod_clockand uses trylock to try to acquire the kvm lock. This new wrapper is then used in the SCK handler. If locking fails,-EAGAINis returned, which is eventually propagated to userspace, thus also freeing the vcpu lock and allowing for forward progress.This is not the most efficient or elegant way to solve this issue, but the SCK instruction is deprecated and its performance is not critical.
The goal of this patch is just to provide a simple but correct way to fix the bug.
Fixes 6a3f95a6b04c ("KVM: s390: Intercept SCK instruction")Signed-off-by Claudio Imbrenda <[email protected]>--- arch/s390/kvm/kvm-s390.c | 19 ++++++++++++++++--- arch/s390/kvm/kvm-s390.h | 4 ++-- arch/s390/kvm/priv.c | 14 +++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-)arch/s390/kvm/kvm-s390.c +16
-3@@ -3869,14 +3869,12 @@ static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)return 0 ; } -void kvm_s390_set_tod_clock (struct kvm *kvm, -const struct kvm_s390_vm_tod_clock *gtod) +static void __kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) {struct kvm_vcpu *vcpu;union tod_clock clk;unsigned long i; -mutex_lock (&kvm->lock);preempt_disable ();store_tod_clock_ext (&clk);@@ -3897,7 +3895,22 @@ void kvm_s390_set_tod_clock(struct kvm *kvm,kvm_s390_vcpu_unblock_all (kvm);preempt_enable (); +} + +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ +mutex_lock (&kvm->lock); +__kvm_s390_set_tod_clock (kvm, gtod); +mutex_unlock (&kvm->lock); +} + +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ +if (!mutex_trylock (&kvm->lock)) +return 0 ; +__kvm_s390_set_tod_clock (kvm, gtod);mutex_unlock (&kvm->lock); +return 1 ; }/** arch/s390/kvm/kvm-s390.h +2
-2@@ -349,8 +349,8 @@ int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);int kvm_s390_handle_sigp_pei (struct kvm_vcpu *vcpu);/* implemented in kvm-s390.c */ -void kvm_s390_set_tod_clock (struct kvm *kvm, -const struct kvm_s390_vm_tod_clock *gtod); +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod); +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod);long kvm_arch_fault_in_page (struct kvm_vcpu *vcpu,gpa_t gpa,int writable);int kvm_s390_store_status_unloaded (struct kvm_vcpu *vcpu,unsigned long addr);int kvm_s390_vcpu_store_status (struct kvm_vcpu *vcpu,unsigned long addr);arch/s390/kvm/priv.c +0
-0@@ -102,7 +102,19 @@ static int handle_set_clock(struct kvm_vcpu *vcpu)return kvm_s390_inject_prog_cond (vcpu, rc);VCPU_EVENT (vcpu,3 ,"SCK: setting guest TOD to 0x%llx" , gtod.tod);
arch/s390/kvm/priv.c static int handle_set_clock(struct kvm_vcpu *vcpu) · line 113
Quoted 10 lines
arch/s390/kvm/priv.c +9
-1-kvm_s390_set_tod_clock (vcpu->kvm, >od); +/* +* To set the TOD clock we need to take the kvm lock, but we are +* already holding the vcpu lock, and the usual lock order is the +* opposite. Therefore we use trylock instead of lock, and if the +* kvm lock cannot be taken, we retry the instruction and return +* -EAGAIN to userspace, thus freeing the vcpu lock. +* The SCK instruction is considered legacy and at this point it's +* not worth the effort to find a nicer solution. +*/
To comply more with usual comment style (no we, us) and to give more context on the legacy I will slightly modify the comment before sending out.
/*
* To set the TOD clock the kvm lock must be taken, but the vcpu
* lock is already held in handle_set_clock. The usual lock order
* is the opposite.
* As SCK is deprecated and should not be used in several cases
* like the existence of the multiple epoch facility or TOD clock
* steering (see Principles of Operation) a slow path can be used.
* If the lock can not be taken via try_lock, the instruction will
* be retried via -EAGAIN at a later point in time.
*/
Ok with everybody?
Quoted 7 lines
arch/s390/kvm/priv.c +4
-0+if (!kvm_s390_try_set_tod_clock (vcpu->kvm, >od)) { +kvm_s390_retry_instr (vcpu); +return -EAGAIN ; + }kvm_s390_set_psw_cc (vcpu,0 );return 0 ;
Quoted 107 lines · Christian Borntraeger
Am 01.03.22 um 15:33 schrieb Claudio Imbrenda:
Quoted 92 lines
When handling the SCK instruction, the kvm lock is taken, even though the vcpu lock is already being held. The normal locking order is kvm lock first and then vcpu lock. This is can (and in some circumstances does) lead to deadlocks.
The function
kvm_s390_set_tod_clockis called both by the SCK handler and by some IOCTLs to set the clock. The IOCTLs will not hold the vcpu lock, so they can safely take the kvm lock. The SCK handler holds the vcpu lock, but will also somehow need to acquire the kvm lock without relinquishing the vcpu lock.The solution is to factor out the code to set the clock, and provide two wrappers. One is called like the original function and does the locking, the other is called
kvm_s390_try_set_tod_clockand uses trylock to try to acquire the kvm lock. This new wrapper is then used in the SCK handler. If locking fails,-EAGAINis returned, which is eventually propagated to userspace, thus also freeing the vcpu lock and allowing for forward progress.This is not the most efficient or elegant way to solve this issue, but the SCK instruction is deprecated and its performance is not critical.
The goal of this patch is just to provide a simple but correct way to fix the bug.
Fixes 6a3f95a6b04c ("KVM: s390: Intercept SCK instruction")Signed-off-by Claudio Imbrenda <[email protected]>---
arch/s390/kvm/kvm-s390.c| 19 ++++++++++++++++---arch/s390/kvm/kvm-s390.h| 4 ++-- arch/s390/kvm/priv.c | 14 +++++++++++++-3 files changed, 31 insertions(+), 6 deletions(-)arch/s390/kvm/kvm-s390.c +16
-1@@ -3869,14 +3869,12 @@ static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) return 0; } -void kvm_s390_set_tod_clock(struct kvm *kvm, - const struct kvm_s390_vm_tod_clock *gtod) +static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) { struct kvm_vcpu *vcpu; union tod_clock clk; unsigned long i; - mutex_lock(&kvm->lock); preempt_disable(); store_tod_clock_ext(&clk);@@ -3897,7 +3895,22 @@ void kvm_s390_set_tod_clock(struct kvm *kvm, kvm_s390_vcpu_unblock_all(kvm); preempt_enable(); +} + +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ + mutex_lock(&kvm->lock); + __kvm_s390_set_tod_clock(kvm, gtod); + mutex_unlock(&kvm->lock); +} + +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ + if (!mutex_trylock(&kvm->lock)) + return 0; + __kvm_s390_set_tod_clock(kvm, gtod); mutex_unlock(&kvm->lock); + return 1; } /**arch/s390/kvm/kvm-s390.h +2
-2@@ -349,8 +349,8 @@ int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu); int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu); /* implemented in kvm-s390.c */ -void kvm_s390_set_tod_clock (struct kvm *kvm, - const struct kvm_s390_vm_tod_clock *gtod); +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod); +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod); long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable); int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr); int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr);arch/s390/kvm/priv.c +0
-0@@ -102,7 +102,19 @@ static int handle_set_clock(struct kvm_vcpu *vcpu) return kvm_s390_inject_prog_cond(vcpu, rc); VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", gtod.tod);arch/s390/kvm/priv.c static int handle_set_clock(struct kvm_vcpu *vcpu) · line 112
Quoted 10 lines
arch/s390/kvm/priv.c +9
-1- kvm_s390_set_tod_clock(vcpu->kvm, >od); + /* +* To set the TOD clock we need to take the kvm lock, but we are +* already holding the vcpu lock, and the usual lock order is the +* opposite. Therefore we use trylock instead of lock, and if the +* kvm lock cannot be taken, we retry the instruction and return +* -EAGAIN to userspace, thus freeing the vcpu lock. +* The SCK instruction is considered legacy and at this point it's +* not worth the effort to find a nicer solution. +*/ To comply more with usual comment style (no we, us) and to give more context on the legacy I will slightly modify the comment before sending out.
Quoted 5 lines
/*
* To set the TOD clock the kvm lock must be taken, but the vcpu
* lock is already held inhandle_set_clock. The usual lock order
* is the opposite.
* As SCK is deprecated and should not be used in several cases
I think you'd want commas around that clause, i.e.
* As SCK is deprecated and should not be used in several cases,
* for example when the multiple-opoch or the TOD-clock-steering
* facility is installed (see Principles of Operation),
* a slow path can be used.
Quoted 15 lines
* like the existence of the multiple epoch facility or TOD clock> * steering (see Principles of Operation) a slow path can be used.
* If the lock can not be taken viatry_lock, the instruction will
* be retried via-EAGAINat a later point in time.
*/Ok with everybody?
Quoted 6 lines
arch/s390/kvm/priv.c +4
-0+ if (!kvm_s390_try_set_tod_clock(vcpu->kvm, >od)) { + kvm_s390_retry_instr(vcpu); + return -EAGAIN; + } kvm_s390_set_psw_cc(vcpu, 0); return 0;
Claudio Imbrenda
maintainerQuoted 114 lines · On Mon, 14 Mar 2022 15:02:13 +0100 Janis Schoetterl-Glausch…
Quoted 107 lines · Christian Borntraeger
Am 01.03.22 um 15:33 schrieb Claudio Imbrenda:
Quoted 92 lines
When handling the SCK instruction, the kvm lock is taken, even though the vcpu lock is already being held. The normal locking order is kvm lock first and then vcpu lock. This is can (and in some circumstances does) lead to deadlocks.
The function
kvm_s390_set_tod_clockis called both by the SCK handler and by some IOCTLs to set the clock. The IOCTLs will not hold the vcpu lock, so they can safely take the kvm lock. The SCK handler holds the vcpu lock, but will also somehow need to acquire the kvm lock without relinquishing the vcpu lock.The solution is to factor out the code to set the clock, and provide two wrappers. One is called like the original function and does the locking, the other is called
kvm_s390_try_set_tod_clockand uses trylock to try to acquire the kvm lock. This new wrapper is then used in the SCK handler. If locking fails,-EAGAINis returned, which is eventually propagated to userspace, thus also freeing the vcpu lock and allowing for forward progress.This is not the most efficient or elegant way to solve this issue, but the SCK instruction is deprecated and its performance is not critical.
The goal of this patch is just to provide a simple but correct way to fix the bug.
Fixes 6a3f95a6b04c ("KVM: s390: Intercept SCK instruction")Signed-off-by Claudio Imbrenda <[email protected]>---
arch/s390/kvm/kvm-s390.c| 19 ++++++++++++++++---arch/s390/kvm/kvm-s390.h| 4 ++-- arch/s390/kvm/priv.c | 14 +++++++++++++-3 files changed, 31 insertions(+), 6 deletions(-)arch/s390/kvm/kvm-s390.c +16
-1@@ -3869,14 +3869,12 @@ static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) return 0; } -void kvm_s390_set_tod_clock(struct kvm *kvm, - const struct kvm_s390_vm_tod_clock *gtod) +static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) { struct kvm_vcpu *vcpu; union tod_clock clk; unsigned long i; - mutex_lock(&kvm->lock); preempt_disable(); store_tod_clock_ext(&clk);@@ -3897,7 +3895,22 @@ void kvm_s390_set_tod_clock(struct kvm *kvm, kvm_s390_vcpu_unblock_all(kvm); preempt_enable(); +} + +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ + mutex_lock(&kvm->lock); + __kvm_s390_set_tod_clock(kvm, gtod); + mutex_unlock(&kvm->lock); +} + +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod) +{ + if (!mutex_trylock(&kvm->lock)) + return 0; + __kvm_s390_set_tod_clock(kvm, gtod); mutex_unlock(&kvm->lock); + return 1; } /**arch/s390/kvm/kvm-s390.h +2
-2@@ -349,8 +349,8 @@ int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu); int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu); /* implemented in kvm-s390.c */ -void kvm_s390_set_tod_clock (struct kvm *kvm, - const struct kvm_s390_vm_tod_clock *gtod); +void kvm_s390_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod); +int kvm_s390_try_set_tod_clock (struct kvm *kvm,const struct kvm_s390_vm_tod_clock *gtod); long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable); int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr); int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr);arch/s390/kvm/priv.c +0
-0@@ -102,7 +102,19 @@ static int handle_set_clock(struct kvm_vcpu *vcpu) return kvm_s390_inject_prog_cond(vcpu, rc); VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", gtod.tod);arch/s390/kvm/priv.c static int handle_set_clock(struct kvm_vcpu *vcpu) · line 112
Quoted 10 lines
arch/s390/kvm/priv.c +9
-1- kvm_s390_set_tod_clock(vcpu->kvm, >od); + /* +* To set the TOD clock we need to take the kvm lock, but we are +* already holding the vcpu lock, and the usual lock order is the +* opposite. Therefore we use trylock instead of lock, and if the +* kvm lock cannot be taken, we retry the instruction and return +* -EAGAIN to userspace, thus freeing the vcpu lock. +* The SCK instruction is considered legacy and at this point it's +* not worth the effort to find a nicer solution. +*/ To comply more with usual comment style (no we, us) and to give more context on the legacy I will slightly modify the comment before sending out.
Quoted 5 lines
/*
* To set the TOD clock the kvm lock must be taken, but the vcpu
* lock is already held inhandle_set_clock. The usual lock order
* is the opposite.
* As SCK is deprecated and should not be used in several cases
Quoted 6 lines
I think you'd want commas around that clause, i.e.
* As SCK is deprecated and should not be used in several cases,
* for example when the multiple-opoch or the TOD-clock-steering
* facility is installed (see Principles of Operation),
* a slow path can be used.
+1
looks good with the commas
Quoted 16 lines
Quoted 15 lines
* like the existence of the multiple epoch facility or TOD clock> * steering (see Principles of Operation) a slow path can be used.
* If the lock can not be taken viatry_lock, the instruction will
* be retried via-EAGAINat a later point in time.
*/Ok with everybody?
Quoted 6 lines
arch/s390/kvm/priv.c +4
-0+ if (!kvm_s390_try_set_tod_clock(vcpu->kvm, >od)) { + kvm_s390_retry_instr(vcpu); + return -EAGAIN; + } kvm_s390_set_psw_cc(vcpu, 0); return 0;