On Tue, Apr 27, 2021 at 11:25 PM Paolo Bonzini <pbonzini@xxxxxxxxxx> wrote: > > On 28/04/21 00:36, Ben Gardon wrote: > > +void kvm_arch_assign_memslots(struct kvm *kvm, int as_id, > > + struct kvm_memslots *slots) > > +{ > > + mutex_lock(&kvm->arch.memslot_assignment_lock); > > + rcu_assign_pointer(kvm->memslots[as_id], slots); > > + mutex_unlock(&kvm->arch.memslot_assignment_lock); > > +} > > Does the assignment also needs the lock, or only the rmap allocation? I > would prefer the hook to be something like kvm_arch_setup_new_memslots. The assignment does need to be under the lock to prevent the following race: 1. Thread 1 (installing a new memslot): Acquires memslot assignment lock (or perhaps in this case rmap_allocation_lock would be more apt.) 2. Thread 1: Check alloc_memslot_rmaps (it is false) 3. Thread 1: doesn't allocate memslot rmaps for new slot 4. Thread 1: Releases memslot assignment lock 5. Thread 2 (allocating a shadow root): Acquires memslot assignment lock 6. Thread 2: Sets alloc_memslot_rmaps = true 7. Thread 2: Allocates rmaps for all existing slots 8. Thread 2: Releases memslot assignment lock 9. Thread 2: Sets shadow_mmu_active = true 10. Thread 1: Installs the new memslots 11. Thread 3: Null pointer dereference when trying to access rmaps on the new slot. Putting the assignment under the lock prevents 5-8 from happening between 2 and 10. I'm open to other ideas as far as how to prevent this race though. I admit this solution is not the most elegant looking. > > (Also it is useful to have a comment somewhere explaining why the > slots_lock does not work. IIUC there would be a deadlock because you'd > be taking the slots_lock inside an SRCU critical region, while usually > the slots_lock critical section is the one that includes a > synchronize_srcu; I should dig that up and document that ordering in > Documentation/virt/kvm too). Yeah, sorry about that. I should have added a comment to that effect. As you suspected, it's because of the slots lock / SRCU deadlock. Using the slots lock was my original implementation, until the deadlock issue came up. I can add comments about that in a v2. > > Paolo >