KVM: Fix write protection race during dirty logging
PATCH 1/1 KVM: Fix write protection race during dirty logging
From: Takuya Yoshikawa <[email protected]>
This patch fixes a race introduced by:
commit 95d4c16ce78cb6b7549a09159c409d52ddd18dae
KVM: Optimize dirty logging by rmap_write_protect()
During protecting pages for dirty logging, other threads may also try to protect a page in mmu_sync_children() or kvm_mmu_get_page().
In such a case, because get_dirty_log releases mmu_lock before flushing TLB's, the following race condition can happen:
A (get_dirty_log) B (another thread)
lock(mmu_lock)
clear pte.w
unlock(mmu_lock)
lock(mmu_lock)
pte.w is already cleared
unlock(mmu_lock)
skip TLB flush
return
...
TLB flush
Though thread B assumes the page has already been protected when it returns, the remaining TLB entry will break that assumption.
This patch fixes this problem by making get_dirty_log hold the mmu_lock until it flushes the TLB's.
--- arch/x86/kvm/x86.c | 11 +++++------ 1 files changed, 5 insertions(+), 6 deletions(-)
arch/x86/kvm/x86.c +5 -6
@@ -3022,6 +3022,8 @@ static void write_protect_slot(struct kvm *kvm,unsigned long *dirty_bitmap, unsigned long nr_dirty_pages) { + spin_lock(&kvm->mmu_lock); +/* Not many dirty pages compared to # of shadow pages. */ if (nr_dirty_pages < kvm->arch.n_used_mmu_pages) {unsigned long gfn_offset;
@@ -3029,16 +3031,13 @@ static void write_protect_slot(struct kvm *kvm,for_each_set_bit (gfn_offset, dirty_bitmap, memslot->npages) {unsigned long gfn = memslot->base_gfn + gfn_offset; -spin_lock (&kvm->mmu_lock);kvm_mmu_rmap_write_protect (kvm, gfn, memslot); -spin_unlock (&kvm->mmu_lock); }kvm_flush_remote_tlbs (kvm); - }else { -spin_lock (&kvm->mmu_lock); + }else kvm_mmu_slot_remove_write_access (kvm, memslot->id); -spin_unlock (&kvm->mmu_lock); - } + +spin_unlock (&kvm->mmu_lock); }/*
Signature · 1 line
1.7.5.4
Quoted 26 lines · Takuya Yoshikawa
From: Takuya Yoshikawa <[email protected]>
This patch fixes a race introduced by:
commit
95d4c16ce78cb6b7549a09159c409d52ddd18dae
KVM: Optimize dirty logging byrmap_write_protect()During protecting pages for dirty logging, other threads may also try to protect a page in
mmu_sync_children()orkvm_mmu_get_page().In such a case, because
get_dirty_logreleasesmmu_lockbefore flushing TLB's, the following race condition can happen:A (
get_dirty_log) B (another thread)
lock(mmu_lock)
clear pte.w
unlock(mmu_lock)
lock(mmu_lock)
pte.w is already cleared
unlock(mmu_lock)
skip TLB flush
return
...
TLB flush
Quoted 6 lines
Though thread B assumes the page has already been protected when it returns, the remaining TLB entry will break that assumption.
This patch fixes this problem by making
get_dirty_loghold themmu_lockuntil it flushes the TLB's.
I do not think this is a problem since the dirty page is logged when the writeable spte is being set, and in the end of get_dirty_log, all TLBs are always flushed.
Quoted 31 lines · Xiao Guangrong
Quoted 29 lines · Takuya Yoshikawa
From: Takuya Yoshikawa<[email protected]>
This patch fixes a race introduced by:
commit
95d4c16ce78cb6b7549a09159c409d52ddd18dae
KVM: Optimize dirty logging byrmap_write_protect()During protecting pages for dirty logging, other threads may also try to protect a page in
mmu_sync_children()orkvm_mmu_get_page().In such a case, because
get_dirty_logreleasesmmu_lockbefore flushing TLB's, the following race condition can happen:A (
get_dirty_log) B (another thread)
lock(mmu_lock)
clear pte.w
unlock(mmu_lock)
lock(mmu_lock)
pte.w is already cleared
unlock(mmu_lock)
skip TLB flush
return
...
TLB flushThough thread B assumes the page has already been protected when it returns, the remaining TLB entry will break that assumption.
Quoted 9 lines
Quoted 3 lines
This patch fixes this problem by making
get_dirty_loghold themmu_lockuntil it flushes the TLB's.I do not think this is a problem since the dirty page is logged when the writeable spte is being set, and in the end of
get_dirty_log, all TLBs are always flushed.
The victim is not GET_DIRTY_LOG but thread B; it needs to assure the page is protected before returning.
Thanks,
Takuya
Xiao Guangrong
Reviewed-byQuoted 37 lines · Takuya Yoshikawa
Quoted 36 lines · Xiao Guangrong
Quoted 32 lines · Takuya Yoshikawa
From: Takuya Yoshikawa<[email protected]>
This patch fixes a race introduced by:
commit
95d4c16ce78cb6b7549a09159c409d52ddd18dae
KVM: Optimize dirty logging byrmap_write_protect()During protecting pages for dirty logging, other threads may also try to protect a page in
mmu_sync_children()orkvm_mmu_get_page().In such a case, because
get_dirty_logreleasesmmu_lockbefore flushing TLB's, the following race condition can happen:A (
get_dirty_log) B (another thread)
lock(mmu_lock)
clear pte.w
unlock(mmu_lock)
lock(mmu_lock)
pte.w is already cleared
unlock(mmu_lock)
skip TLB flush
return
...
TLB flushThough thread B assumes the page has already been protected when it returns, the remaining TLB entry will break that assumption.
This patch fixes this problem by making
get_dirty_loghold themmu_lockuntil it flushes the TLB's.
Quoted 8 lines
Quoted 4 lines
I do not think this is a problem since the dirty page is logged when the writeable spte is being set, and in the end of
get_dirty_log, all TLBs are always flushed.The victim is not
GET_DIRTY_LOGbut thread B; it needs to assure the page is protected before returning.
Ah, right!
Quoted 44 lines · Xiao Guangrong
Quoted 42 lines · Takuya Yoshikawa
Quoted 40 lines · Xiao Guangrong
Quoted 26 lines · Takuya Yoshikawa
From: Takuya Yoshikawa<[email protected]>
This patch fixes a race introduced by:
commit
95d4c16ce78cb6b7549a09159c409d52ddd18dae
KVM: Optimize dirty logging byrmap_write_protect()During protecting pages for dirty logging, other threads may also try to protect a page in
mmu_sync_children()orkvm_mmu_get_page().In such a case, because
get_dirty_logreleasesmmu_lockbefore flushing TLB's, the following race condition can happen:A (
get_dirty_log) B (another thread)
lock(mmu_lock)
clear pte.w
unlock(mmu_lock)
lock(mmu_lock)
pte.w is already cleared
unlock(mmu_lock)
skip TLB flush
return
...
TLB flushQuoted 6 lines
Though thread B assumes the page has already been protected when it returns, the remaining TLB entry will break that assumption.
This patch fixes this problem by making
get_dirty_loghold themmu_lockuntil it flushes the TLB's.I do not think this is a problem since the dirty page is logged when the writeable spte is being set, and in the end of
get_dirty_log, all TLBs are always flushed.
Quoted 9 lines
Quoted 3 lines
The victim is not
GET_DIRTY_LOGbut thread B; it needs to assure the page is protected before returning.Ah, right!
Reviewed-by Xiao Guangrong <[email protected]>
I am worrying if other paths(such as like kvm_mmu_notifier_invalidate_page) which unmap/modify writeable ptes and flush TLBs out of mmu lock have the same problem?
Quoted 4 lines · Xiao Guangrong
I am worrying if other
paths(such as like kvm_mmu_notifier_invalidate_page)which unmap/modify writeable ptes and flush TLBs out of mmu lock have the same problem?
Yes, I think so ... (long standing bug?)
Let's ask Avi if he prefers to fix it separately then!
Takuya
Quoted 10 lines · Takuya Yoshikawa
Quoted 6 lines · Xiao Guangrong
I am worrying if other
paths(such as like kvm_mmu_notifier_invalidate_page)
which unmap/modify writeable ptes and flush TLBs out of mmu lock have the
same problem?Yes, I think so ... (long standing bug?)
Ugh, I looked at arch/x86/kvm for this problem but not at virt/kvm.
Quoted 2 lines
Let's ask Avi if he prefers to fix it separately then!
Separate fixes please.
Signature · 1 line
error compiling committee.c: too many arguments to function
Marcelo Tosatti
AppliedQuoted 29 lines · Takuya Yoshikawa
From: Takuya Yoshikawa <[email protected]>
This patch fixes a race introduced by:
commit
95d4c16ce78cb6b7549a09159c409d52ddd18dae
KVM: Optimize dirty logging byrmap_write_protect()During protecting pages for dirty logging, other threads may also try to protect a page in
mmu_sync_children()orkvm_mmu_get_page().In such a case, because
get_dirty_logreleasesmmu_lockbefore flushing TLB's, the following race condition can happen:A (
get_dirty_log) B (another thread)
lock(mmu_lock)
clear pte.w
unlock(mmu_lock)
lock(mmu_lock)
pte.w is already cleared
unlock(mmu_lock)
skip TLB flush
return
...
TLB flushThough thread B assumes the page has already been protected when it returns, the remaining TLB entry will break that assumption.
Quoted 7 lines
This patch fixes this problem by making
get_dirty_loghold themmu_lockuntil it flushes the TLB's.Signed-off-by Takuya Yoshikawa <[email protected]>--- arch/x86/kvm/x86.c | 11 +++++------ 1 files changed, 5 insertions(+), 6 deletions(-)
Applied, thanks.
Please don't forget the invalidate_page fix.
Quoted 1 line · On Wed, 8 Feb 2012 14:38:07 -0200 Marcelo Tosatti <[email protected]> wrote:
Please don't forget the
invalidate_pagefix.
I will send in a few days.
Takuya