Skip to content

Commit 565f3be

Browse files
Takuya Yoshikawaavikivity
authored andcommitted
KVM: mmu_notifier: Flush TLBs before releasing mmu_lock
Other threads may process the same page in that small window and skip TLB flush and then return before these functions do flush. Signed-off-by: Takuya Yoshikawa <yoshikawa.takuya@oss.ntt.co.jp> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent db3fe4e commit 565f3be

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

‎virt/kvm/kvm_main.c‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,15 @@ static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
289289
*/
290290
idx = srcu_read_lock(&kvm->srcu);
291291
spin_lock(&kvm->mmu_lock);
292+
292293
kvm->mmu_notifier_seq++;
293294
need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
294-
spin_unlock(&kvm->mmu_lock);
295-
srcu_read_unlock(&kvm->srcu, idx);
296-
297295
/* we've to flush the tlb before the pages can be freed */
298296
if (need_tlb_flush)
299297
kvm_flush_remote_tlbs(kvm);
300298

299+
spin_unlock(&kvm->mmu_lock);
300+
srcu_read_unlock(&kvm->srcu, idx);
301301
}
302302

303303
static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
@@ -335,12 +335,12 @@ static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
335335
for (; start < end; start += PAGE_SIZE)
336336
need_tlb_flush |= kvm_unmap_hva(kvm, start);
337337
need_tlb_flush |= kvm->tlbs_dirty;
338-
spin_unlock(&kvm->mmu_lock);
339-
srcu_read_unlock(&kvm->srcu, idx);
340-
341338
/* we've to flush the tlb before the pages can be freed */
342339
if (need_tlb_flush)
343340
kvm_flush_remote_tlbs(kvm);
341+
342+
spin_unlock(&kvm->mmu_lock);
343+
srcu_read_unlock(&kvm->srcu, idx);
344344
}
345345

346346
static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
@@ -378,13 +378,14 @@ static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
378378

379379
idx = srcu_read_lock(&kvm->srcu);
380380
spin_lock(&kvm->mmu_lock);
381-
young = kvm_age_hva(kvm, address);
382-
spin_unlock(&kvm->mmu_lock);
383-
srcu_read_unlock(&kvm->srcu, idx);
384381

382+
young = kvm_age_hva(kvm, address);
385383
if (young)
386384
kvm_flush_remote_tlbs(kvm);
387385

386+
spin_unlock(&kvm->mmu_lock);
387+
srcu_read_unlock(&kvm->srcu, idx);
388+
388389
return young;
389390
}
390391

0 commit comments

Comments
 (0)