powerpc: kvm: make the setup of hpte under the protection of KVMPPC_RMAP_LOCK_BIT
Subsystems (2)
PATCH 1/1 powerpc: kvm: make the setup of hpte under the protection of KVMPPC_RMAP_LOCK_BIT
In current code, the setup of hpte is under the risk of race with mmu_notifier_invalidate, i.e we may setup a hpte with a invalid pfn. Resolve this issue by sync the two actions by KVMPPC_RMAP_LOCK_BIT.
--- arch/powerpc/kvm/book3s_64_mmu_hv.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
arch/powerpc/kvm/book3s_64_mmu_hv.c +10 -5
@@ -754,19 +754,24 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,if (hptep[0 ] &HPTE_V_VALID ) {/* HPTE was previously valid, so we need to invalidate it */ -unlock_rmap (rmap); hptep[0 ] |=HPTE_V_ABSENT ;kvmppc_invalidate_hpte (kvm, hptep, index);/* don't lose previous R and C bits */ r |= hptep[1 ] & (HPTE_R_R |HPTE_R_C ); + + hptep[1 ] = r; +eieio (); + hptep[0 ] = hpte[0 ]; + asmvolatile ("ptesync" : : :"memory" ); +unlock_rmap (rmap); }else { + hptep[1 ] = r; +eieio (); + hptep[0 ] = hpte[0 ]; + asmvolatile ("ptesync" : : :"memory" );kvmppc_add_revmap_chain (kvm, rev, rmap, index,0 ); } - hptep[1 ] = r; -eieio (); - hptep[0 ] = hpte[0 ]; - asmvolatile ("ptesync" : : :"memory" );preempt_enable ();if (page &&hpte_is_writable (r))SetPageDirty (page);
Signature · 1 line
1.8.1.4
Quoted 3 lines · Liu Ping Fan
In current code, the setup of hpte is under the risk of race with
mmu_notifier_invalidate, i.e we may setup a hpte with a invalid pfn. Resolve this issue by sync the two actions byKVMPPC_RMAP_LOCK_BIT.
Please describe the race you think you see. I'm quite sure both Paul and I went over that code and somewhat convinced ourselves that it was ok but it's possible that we were both wrong :-)
Cheers,
Ben.
Quoted 39 lines
Signed-off-by Liu Ping Fan <[email protected]>--- arch/powerpc/kvm/book3s_64_mmu_hv.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)arch/powerpc/kvm/book3s_64_mmu_hv.c +10
-5@@ -754,19 +754,24 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,if (hptep[0 ] &HPTE_V_VALID ) {/* HPTE was previously valid, so we need to invalidate it */ -unlock_rmap (rmap); hptep[0 ] |=HPTE_V_ABSENT ;kvmppc_invalidate_hpte (kvm, hptep, index);/* don't lose previous R and C bits */ r |= hptep[1 ] & (HPTE_R_R |HPTE_R_C ); + + hptep[1 ] = r; +eieio (); + hptep[0 ] = hpte[0 ]; + asmvolatile ("ptesync" : : :"memory" ); +unlock_rmap (rmap); }else { + hptep[1 ] = r; +eieio (); + hptep[0 ] = hpte[0 ]; + asmvolatile ("ptesync" : : :"memory" );kvmppc_add_revmap_chain (kvm, rev, rmap, index,0 ); } - hptep[1 ] = r; -eieio (); - hptep[0 ] = hpte[0 ]; - asmvolatile ("ptesync" : : :"memory" );preempt_enable ();if (page &&hpte_is_writable (r))SetPageDirty (page);
Hope I am right. Take the following seq as an example
if (hptep[0] & HPTE_V_VALID) {
/* HPTE was previously valid, so we need to invalidate it */ unlock_rmap(rmap);
hptep[0] |= HPTE_V_ABSENT;kvmppc_invalidate_hpte(kvm, hptep, index);
/* don't lose previous R and C bits */
r |= hptep[1] & (HPTE_R_R | HPTE_R_C);
} else {kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
}
---------------------------------------------> if we try_to_unmap on
pfn at here, then @r contains a invalid pfn
hptep[1] = r;eieio();
hptep[0] = hpte[0];
asm volatile("ptesync" : : : "memory");
Thx.
Fan
Quoted 53 lines · Benjamin Herrenschmidt
Quoted 3 lines · Liu Ping Fan
In current code, the setup of hpte is under the risk of race with
mmu_notifier_invalidate, i.e we may setup a hpte with a invalid pfn. Resolve this issue by sync the two actions byKVMPPC_RMAP_LOCK_BIT.Please describe the race you think you see. I'm quite sure both Paul and I went over that code and somewhat convinced ourselves that it was ok but it's possible that we were both wrong :-)
Cheers,
Ben.Quoted 39 lines
Signed-off-by Liu Ping Fan <[email protected]>--- arch/powerpc/kvm/book3s_64_mmu_hv.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)arch/powerpc/kvm/book3s_64_mmu_hv.c +10
-5@@ -754,19 +754,24 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,if (hptep[0 ] &HPTE_V_VALID ) {/* HPTE was previously valid, so we need to invalidate it */ -unlock_rmap (rmap); hptep[0 ] |=HPTE_V_ABSENT ;kvmppc_invalidate_hpte (kvm, hptep, index);/* don't lose previous R and C bits */ r |= hptep[1 ] & (HPTE_R_R |HPTE_R_C ); + + hptep[1 ] = r; +eieio (); + hptep[0 ] = hpte[0 ]; + asmvolatile ("ptesync" : : :"memory" ); +unlock_rmap (rmap); }else { + hptep[1 ] = r; +eieio (); + hptep[0 ] = hpte[0 ]; + asmvolatile ("ptesync" : : :"memory" );kvmppc_add_revmap_chain (kvm, rev, rmap, index,0 ); } - hptep[1 ] = r; -eieio (); - hptep[0 ] = hpte[0 ]; - asmvolatile ("ptesync" : : :"memory" );preempt_enable ();if (page &&hpte_is_writable (r))SetPageDirty (page);
Quoted 18 lines · Liu ping fan
Hope I am right. Take the following seq as an example
if (hptep[0] &
HPTE_V_VALID) {
/* HPTE was previously valid, so we need to invalidate it */unlock_rmap(rmap);
hptep[0] |=HPTE_V_ABSENT;kvmppc_invalidate_hpte(kvm, hptep, index);
/* don't lose previous R and C bits */
r |= hptep[1] & (HPTE_R_R|HPTE_R_C);
} else {kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
}
---------------------------------------------> if wetry_to_unmapon
pfn at here, then @r contains a invalid pfn
hptep[1] = r;eieio();
hptep[0] = hpte[0];
asmvolatile("ptesync" : : : "memory");
If that was the case we would have the same race in kvmppc_do_h_enter().
I think the fact that the HPTE is locked will prevent the race, ie, HPTE_V_HVLOCK is set until hptep[0] is written to.
If I look at at the unmap case, my understanding is that it uses kvm_unmap_rmapp() which will also lock the HPTE (try_lock_hpte) and so shouldn't have a race vs the above code.
Or do you see a race I don't ?
Cheers,
Ben.
Quoted 58 lines
Thx.
FanQuoted 53 lines · Benjamin Herrenschmidt
Quoted 3 lines · Liu Ping Fan
In current code, the setup of hpte is under the risk of race with
mmu_notifier_invalidate, i.e we may setup a hpte with a invalid pfn. Resolve this issue by sync the two actions byKVMPPC_RMAP_LOCK_BIT.Please describe the race you think you see. I'm quite sure both Paul and I went over that code and somewhat convinced ourselves that it was ok but it's possible that we were both wrong :-)
Cheers,
Ben.Quoted 39 lines
Signed-off-by Liu Ping Fan <[email protected]>--- arch/powerpc/kvm/book3s_64_mmu_hv.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)arch/powerpc/kvm/book3s_64_mmu_hv.c +10
-5@@ -754,19 +754,24 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,if (hptep[0 ] &HPTE_V_VALID ) {/* HPTE was previously valid, so we need to invalidate it */ -unlock_rmap (rmap); hptep[0 ] |=HPTE_V_ABSENT ;kvmppc_invalidate_hpte (kvm, hptep, index);/* don't lose previous R and C bits */ r |= hptep[1 ] & (HPTE_R_R |HPTE_R_C ); + + hptep[1 ] = r; +eieio (); + hptep[0 ] = hpte[0 ]; + asmvolatile ("ptesync" : : :"memory" ); +unlock_rmap (rmap); }else { + hptep[1 ] = r; +eieio (); + hptep[0 ] = hpte[0 ]; + asmvolatile ("ptesync" : : :"memory" );kvmppc_add_revmap_chain (kvm, rev, rmap, index,0 ); } - hptep[1 ] = r; -eieio (); - hptep[0 ] = hpte[0 ]; - asmvolatile ("ptesync" : : :"memory" );preempt_enable ();if (page &&hpte_is_writable (r))SetPageDirty (page);
Quoted 20 lines · Benjamin Herrenschmidt
Quoted 18 lines · Liu ping fan
Hope I am right. Take the following seq as an example
if (hptep[0] &
HPTE_V_VALID) {
/* HPTE was previously valid, so we need to invalidate it */unlock_rmap(rmap);
hptep[0] |=HPTE_V_ABSENT;kvmppc_invalidate_hpte(kvm, hptep, index);
/* don't lose previous R and C bits */
r |= hptep[1] & (HPTE_R_R|HPTE_R_C);
} else {kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
}
---------------------------------------------> if wetry_to_unmapon
pfn at here, then @r contains a invalid pfn
hptep[1] = r;eieio();
hptep[0] = hpte[0];
asmvolatile("ptesync" : : : "memory");
Quoted 9 lines
If that was the case we would have the same race in
kvmppc_do_h_enter().I think the fact that the HPTE is locked will prevent the race, ie,
HPTE_V_HVLOCKis set until hptep[0] is written to.If I look at at the unmap case, my understanding is that it uses
kvm_unmap_rmapp()which will also lock the HPTE (try_lock_hpte) and so shouldn't have a race vs the above code.
Yes, you are right :)
Thx,
Fan
Quoted 65 lines
Or do you see a race I don't ?
Cheers,
Ben.Quoted 58 lines
Thx.
FanQuoted 53 lines · Benjamin Herrenschmidt
Quoted 3 lines · Liu Ping Fan
In current code, the setup of hpte is under the risk of race with
mmu_notifier_invalidate, i.e we may setup a hpte with a invalid pfn. Resolve this issue by sync the two actions byKVMPPC_RMAP_LOCK_BIT.Please describe the race you think you see. I'm quite sure both Paul and I went over that code and somewhat convinced ourselves that it was ok but it's possible that we were both wrong :-)
Cheers,
Ben.Quoted 39 lines
Signed-off-by Liu Ping Fan <[email protected]>--- arch/powerpc/kvm/book3s_64_mmu_hv.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)arch/powerpc/kvm/book3s_64_mmu_hv.c +10
-5@@ -754,19 +754,24 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,if (hptep[0 ] &HPTE_V_VALID ) {/* HPTE was previously valid, so we need to invalidate it */ -unlock_rmap (rmap); hptep[0 ] |=HPTE_V_ABSENT ;kvmppc_invalidate_hpte (kvm, hptep, index);/* don't lose previous R and C bits */ r |= hptep[1 ] & (HPTE_R_R |HPTE_R_C ); + + hptep[1 ] = r; +eieio (); + hptep[0 ] = hpte[0 ]; + asmvolatile ("ptesync" : : :"memory" ); +unlock_rmap (rmap); }else { + hptep[1 ] = r; +eieio (); + hptep[0 ] = hpte[0 ]; + asmvolatile ("ptesync" : : :"memory" );kvmppc_add_revmap_chain (kvm, rev, rmap, index,0 ); } - hptep[1 ] = r; -eieio (); - hptep[0 ] = hpte[0 ]; - asmvolatile ("ptesync" : : :"memory" );preempt_enable ();if (page &&hpte_is_writable (r))SetPageDirty (page);