Skip to contentSkip to conversation

powerpc/kvm: fix rare but potential deadlock scene

PATCH Superseded Liu Ping Fan 2 participants 6 messages Nov 7, 2013 – Nov 8, 2013

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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
index 043eec8..dbc1478 100644
@@ -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 */
 	asm volatile("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
#1 lore raw

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
index dbc1478..9b97b42 100644
@@ -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
#4 lore raw
In reply to Liu Ping Fan
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_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.

Quoted 8 lines

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

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
index 043eec8..dbc1478 100644
@@ -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 */
 	asm volatile("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
#2 lore raw

Re: [PATCH v2 2/2] powerpc/kvm: remove redundant assignment

In reply to Liu Ping Fan
Quoted 4 lines · Liu Ping Fan

ret is assigned twice with the same value, so remove the later one.

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_GUEST twice 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
index dbc1478..9b97b42 100644
@@ -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
#5 lore raw
In reply to Alexander Graf
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_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

Quoted 8 lines
Quoted 3 lines

Oops! deadlock happens

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.

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
index 043eec8..dbc1478 100644
@@ -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 */
      asm volatile("lwsync" : : : "memory");
      hptep[0] = v;
+     preempt_enable();
 
      gpte->eaddr = eaddr;
      gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
--

1.8.1.4

#3 lore raw

Re: [PATCH v2 2/2] powerpc/kvm: remove redundant assignment

In reply to Alexander Graf
Quoted 50 lines · Alexander Graf
Quoted 4 lines · Liu Ping Fan

ret is assigned twice with the same value, so remove the later one.

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_GUEST twice 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