powerpc/kvm: fix rare but potential deadlock scene
Subsystems (2)
PATCH v2 1/2 powerpc/kvm: fix rare but potential deadlock scene
Since kvmppc_hv_find_lock_hpte() is called from both virtmode and realmode, so it can trigger the deadlock.
Suppose the following scene:
Two physical cpuM, cpuN, two VM instances A, B, each VM has a group of vcpus.
If on cpuM, vcpu_A_1 holds bitlock X (HPTE_V_HVLOCK), then is switched out, and on cpuN, vcpu_A_2 try to lock X in realmode, then cpuN will be caught in realmode for a long time.
What makes things even worse if the following happens,
On cpuM, bitlockX is hold, on cpuN, Y is hold. vcpu_B_2 try to lock Y on cpuM in realmode vcpu_A_2 try to lock X on cpuN in realmode
Oops! deadlock happens
--- arch/powerpc/kvm/book3s_64_mmu_hv.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/powerpc/kvm/book3s_64_mmu_hv.c +5 -1
@@ -474,10 +474,13 @@ static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, }/* Find the HPTE in the hash table */ +preempt_disable (); index =kvmppc_hv_find_lock_hpte (kvm, eaddr, slb_v,HPTE_V_VALID |HPTE_V_ABSENT ); -if (index <0 ) +if (index <0 ) { +preempt_enable ();return -ENOENT ; + } hptep = (unsigned long *)(kvm->arch.hpt_virt + (index <<4 )); v = hptep[0 ] & ~HPTE_V_HVLOCK ; gr = kvm->arch.revmap[index].guest_rpte;
@@ -485,6 +488,7 @@ static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,/* Unlock the HPTE */ asmvolatile ("lwsync" : : :"memory" ); hptep[0 ] = v; +preempt_enable (); gpte->eaddr = eaddr; gpte->vpage = ((v &HPTE_V_AVPN ) <<4 ) | ((eaddr >>12 ) &0xfff );
Signature · 1 line
1.8.1.4
PATCH v2 2/2 powerpc/kvm: remove redundant assignment
ret is assigned twice with the same value, so remove the later one.
--- arch/powerpc/kvm/book3s_64_mmu_hv.c | 1 - 1 file changed, 1 deletion(-)
arch/powerpc/kvm/book3s_64_mmu_hv.c +0 -1
@@ -733,7 +733,6 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,lock_rmap (rmap);/* Check if we might have been invalidated; let the guest retry if so */ - ret =RESUME_GUEST ;if (mmu_notifier_retry (vcpu->kvm, mmu_seq)) {unlock_rmap (rmap); goto out_unlock;
Signature · 1 line
1.8.1.4
Quoted 11 lines · Liu Ping Fan
Since
kvmppc_hv_find_lock_hpte()is called from both virtmode and realmode, so it can trigger the deadlock.Suppose the following scene:
Two physical cpuM, cpuN, two VM instances A, B, each VM has a group of vcpus.
If on cpuM,
vcpu_A_1holds bitlock X (HPTE_V_HVLOCK), then is switched out, and on cpuN,vcpu_A_2try to lock X in realmode, then cpuN will be caught in realmode for a long time.
Quoted 8 lines
What makes things even worse if the following happens,
On cpuM, bitlockX is hold, on cpuN, Y is hold.vcpu_B_2try to lock Y on cpuM in realmodevcpu_A_2try to lock X on cpuN in realmodeOops! deadlock happens
Signed-off-by Liu Ping Fan <[email protected]>
Very nice catch :).
I think it makes a lot of sense to document the fact that kvmppc_hv_find_lock_hpte() should be called with preemption disabled in a comment above the function, so that next time when someone potentially calls it, he knows that he needs to put preempt_disable() around it.
Thanks a lot for finding this pretty subtle issue. May I ask how you got there? Did you actually see systems deadlock because of this?
Alex
Quoted 34 lines
---
arch/powerpc/kvm/book3s_64_mmu_hv.c| 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)arch/powerpc/kvm/book3s_64_mmu_hv.c +5
-1@@ -474,10 +474,13 @@ static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, }/* Find the HPTE in the hash table */ +preempt_disable (); index =kvmppc_hv_find_lock_hpte (kvm, eaddr, slb_v,HPTE_V_VALID |HPTE_V_ABSENT ); -if (index <0 ) +if (index <0 ) { +preempt_enable ();return -ENOENT ; + } hptep = (unsigned long *)(kvm->arch.hpt_virt + (index <<4 )); v = hptep[0 ] & ~HPTE_V_HVLOCK ; gr = kvm->arch.revmap[index].guest_rpte;@@ -485,6 +488,7 @@ static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,/* Unlock the HPTE */ asmvolatile ("lwsync" : : :"memory" ); hptep[0 ] = v; +preempt_enable (); gpte->eaddr = eaddr; gpte->vpage = ((v &HPTE_V_AVPN ) <<4 ) | ((eaddr >>12 ) &0xfff );Signature · 1 line
1.8.1.4
Re: [PATCH v2 2/2] powerpc/kvm: remove redundant assignment
In reply to Liu Ping FanQuoted 4 lines · Liu Ping Fan
ret is assigned twice with the same value, so remove the later one.
Signed-off-by Liu Ping Fan <[email protected]>Acked-by Paul Mackerras <[email protected]>
I suppose my last request for a patch description was slightly too abbreviated :). Sorry about that.
Imagine you are a Linux-stable maintainer. You have about 5000 patches in front of you and you want to figure out whether a patch should get backported into a stable tree or not.
It's very easy to read through the patch description.
It's reasonably easy to do a git show on the patch.
It's very hard to look at the actual surrounding code that was changed.
If I open a text editor on the file, I immediately see what you're saying:
Quoted 7 lines
ret = RESUME_GUEST;
preempt_disable();
while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
cpu_relax();
if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] ||
rev->guest_rpte != hpte[2])
/* HPTE has been changed under us; let the guest retry */Quoted 8 lines
goto out_unlock;
hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
rmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
lock_rmap(rmap);
/* Check if we might have been invalidated; let the guest retry if so */
ret = RESUME_GUEST;However, that scope is not given in the actual patch itself. If you look at the diff below, you have no idea whether the patch is fixing a bug or just removes duplication and doesn't actually have any effect. In fact, the compiled assembly should be the same with this patch and without. But you can't tell from the diff below.
So what I would like to see in the patch description is something that makes it easy to understand what's going on without the need to check out the source file. Something like
Quoted 17 lines
We redundantly set ret to
RESUME_GUESTtwice without changing it in between. Only do it once:ret = RESUME_GUEST; preempt_disable(); while (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) cpu_relax(); if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] || rev->guest_rpte != hpte[2]) /* HPTE has been changed under us; let the guest retry */ goto out_unlock; hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID; rmap = &memslot->arch.rmap[gfn - memslot->base_gfn]; lock_rmap(rmap); /* Check if we might have been invalidated; let the guest retry if so */ ret = RESUME_GUEST;
If I look at that patch description it immediately tells me "Ah, no need to worry, it's not a critical bug I need to backport". If you have a better idea how to express that I'm more than happy to take that too. Otherwise just let me know whether you like the description above and I'll modify it to the one that includes the code snippet when applying the patch.
Thanks a lot,
Alex
Quoted 19 lines
---
arch/powerpc/kvm/book3s_64_mmu_hv.c| 1 -1 file changed, 1 deletion(-)arch/powerpc/kvm/book3s_64_mmu_hv.c +0
-1@@ -733,7 +733,6 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,lock_rmap (rmap);/* Check if we might have been invalidated; let the guest retry if so */ - ret =RESUME_GUEST ;if (mmu_notifier_retry (vcpu->kvm, mmu_seq)) {unlock_rmap (rmap); goto out_unlock;Signature · 1 line
1.8.1.4
Quoted 19 lines · Alexander Graf
Quoted 16 lines · Liu Ping Fan
Since
kvmppc_hv_find_lock_hpte()is called from both virtmode and realmode, so it can trigger the deadlock.Suppose the following scene:
Two physical cpuM, cpuN, two VM instances A, B, each VM has a group of vcpus.
If on cpuM,
vcpu_A_1holds bitlock X (HPTE_V_HVLOCK), then is switched out, and on cpuN,vcpu_A_2try to lock X in realmode, then cpuN will be caught in realmode for a long time.What makes things even worse if the following happens,
On cpuM, bitlockX is hold, on cpuN, Y is hold.vcpu_B_2try to lock Y on cpuM in realmodevcpu_A_2try to lock X on cpuN in realmode
Quoted 8 lines
Quoted 3 lines
Oops! deadlock happens
Signed-off-by Liu Ping Fan <[email protected]>Very nice catch :).
I think it makes a lot of sense to document the fact that
kvmppc_hv_find_lock_hpte()should be called with preemption disabled in a comment above the function, so that next time when someone potentially calls it, he knows that he needs to putpreempt_disable()around it.
Ok, I will document them in v3
Quoted 2 lines
Thanks a lot for finding this pretty subtle issue. May I ask how you got there? Did you actually see systems deadlock because of this?
Intuition :). then I begin try to model a scene which causes the deadlock. And fortunately, I find a case to verify my suspension.
Regards,
Pingfan
Quoted 38 lines
Alex
Quoted 34 lines
---
arch/powerpc/kvm/book3s_64_mmu_hv.c| 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)arch/powerpc/kvm/book3s_64_mmu_hv.c +5
-2@@ -474,10 +474,13 @@ static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, }/* Find the HPTE in the hash table */ +preempt_disable (); index =kvmppc_hv_find_lock_hpte (kvm, eaddr, slb_v,HPTE_V_VALID |HPTE_V_ABSENT ); -if (index <0 ) +if (index <0 ) { +preempt_enable ();return -ENOENT ; + } hptep = (unsigned long *)(kvm->arch.hpt_virt + (index <<4 )); v = hptep[0 ] & ~HPTE_V_HVLOCK ; gr = kvm->arch.revmap[index].guest_rpte;@@ -485,6 +488,7 @@ static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,/* Unlock the HPTE */ asmvolatile ("lwsync" : : :"memory" ); hptep[0 ] = v; +preempt_enable (); gpte->eaddr = eaddr; gpte->vpage = ((v &HPTE_V_AVPN ) <<4 ) | ((eaddr >>12 ) &0xfff ); --1.8.1.4
Re: [PATCH v2 2/2] powerpc/kvm: remove redundant assignment
In reply to Alexander GrafQuoted 50 lines · Alexander Graf
Quoted 4 lines · Liu Ping Fan
ret is assigned twice with the same value, so remove the later one.
Signed-off-by Liu Ping Fan <[email protected]>Acked-by Paul Mackerras <[email protected]>I suppose my last request for a patch description was slightly too abbreviated :). Sorry about that.
Imagine you are a Linux-stable maintainer. You have about 5000 patches in front of you and you want to figure out whether a patch should get backported into a stable tree or not.
It's very easy to read through the patch description.
It's reasonably easy to do a git show on the patch.
It's very hard to look at the actual surrounding code that was changed.If I open a text editor on the file, I immediately see what you're saying:
Quoted 7 lines
ret = RESUME_GUEST; preempt_disable(); while (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) cpu_relax(); if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] || rev->guest_rpte != hpte[2]) /* HPTE has been changed under us; let the guest retry */Quoted 8 lines
goto out_unlock; hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID; rmap = &memslot->arch.rmap[gfn - memslot->base_gfn]; lock_rmap(rmap); /* Check if we might have been invalidated; let the guest retry if so */ ret = RESUME_GUEST;However, that scope is not given in the actual patch itself. If you look at the diff below, you have no idea whether the patch is fixing a bug or just removes duplication and doesn't actually have any effect. In fact, the compiled assembly should be the same with this patch and without. But you can't tell from the diff below.
So what I would like to see in the patch description is something that makes it easy to understand what's going on without the need to check out the source file. Something like
Quoted 12 lines
We redundantly set ret to
RESUME_GUESTtwice without changing it in between. Only do it once:ret = RESUME_GUEST; preempt_disable(); while (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) cpu_relax(); if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] || rev->guest_rpte != hpte[2]) /* HPTE has been changed under us; let the guest retry */ goto out_unlock; hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
Quoted 9 lines
Quoted 5 lines
rmap = &memslot->arch.rmap[gfn - memslot->base_gfn]; lock_rmap(rmap); /* Check if we might have been invalidated; let the guest retry if so */ ret = RESUME_GUEST;If I look at that patch description it immediately tells me "Ah, no need to worry, it's not a critical bug I need to backport". If you have a better idea how to express that I'm more than happy to take that too. Otherwise just let me know whether you like the description above and I'll modify it to the one that includes the code snippet when applying the patch.
Oh, yes. Thank you very much..
And I had a better understanding of the heavy work of maintainers :) Will keep this in mind.
Best regards,
Pingfan