On Mon, Apr 19, 2021, Paolo Bonzini wrote: > On 19/04/21 10:49, Wanpeng Li wrote: > > I saw this splatting: > > > > ====================================================== > > WARNING: possible circular locking dependency detected > > 5.12.0-rc3+ #6 Tainted: G OE > > ------------------------------------------------------ > > qemu-system-x86/3069 is trying to acquire lock: > > ffffffff9c775ca0 (mmu_notifier_invalidate_range_start){+.+.}-{0:0}, > > at: __mmu_notifier_invalidate_range_end+0x5/0x190 > > > > but task is already holding lock: > > ffffaff7410a9160 (&kvm->mmu_notifier_slots_lock){.+.+}-{3:3}, at: > > kvm_mmu_notifier_invalidate_range_start+0x36d/0x4f0 [kvm] > > I guess it is possible to open-code the wait using a readers count and a > spinlock (see patch after signature). This allows including the > rcu_assign_pointer in the same critical section that checks the number > of readers. Also on the plus side, the init_rwsem() is replaced by > slightly nicer code. Ugh, the count approach is nearly identical to Ben's original code. Using a rwsem seemed so clever :-/ > IIUC this could be extended to non-sleeping invalidations too, but I > am not really sure about that. Yes, that should be fine. > There are some issues with the patch though: > > - I am not sure if this should be a raw spin lock to avoid the same issue > on PREEMPT_RT kernel. That said the critical section is so tiny that using > a raw spin lock may make sense anyway If using spinlock_t is problematic, wouldn't mmu_lock already be an issue? Or am I misunderstanding your concern? > - this loses the rwsem fairness. On the other hand, mm/mmu_notifier.c's > own interval-tree-based filter is also using a similar mechanism that is > likewise not fair, so it should be okay. The one concern I had with an unfair mechanism of this nature is that, in theory, the memslot update could be blocked indefinitely. > Any opinions? For now I placed the change below in kvm/queue, but I'm > leaning towards delaying this optimization to the next merge window. I think delaying it makes sense. > @@ -1333,9 +1351,22 @@ static struct kvm_memslots *install_new_memslots(struct kvm *kvm, > WARN_ON(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS); > slots->generation = gen | KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS; > - down_write(&kvm->mmu_notifier_slots_lock); > + /* > + * This cannot be an rwsem because the MMU notifier must not run > + * inside the critical section. A sleeping rwsem cannot exclude > + * that. How on earth did you decipher that from the splat? I stared at it for a good five minutes and was completely befuddled. > + */ > + spin_lock(&kvm->mn_invalidate_lock); > + prepare_to_rcuwait(&kvm->mn_memslots_update_rcuwait); > + while (kvm->mn_active_invalidate_count) { > + set_current_state(TASK_UNINTERRUPTIBLE); > + spin_unlock(&kvm->mn_invalidate_lock); > + schedule(); > + spin_lock(&kvm->mn_invalidate_lock); > + } > + finish_rcuwait(&kvm->mn_memslots_update_rcuwait); > rcu_assign_pointer(kvm->memslots[as_id], slots); > - up_write(&kvm->mmu_notifier_slots_lock); > + spin_unlock(&kvm->mn_invalidate_lock); > synchronize_srcu_expedited(&kvm->srcu); >