public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Chao Gao <chao.gao@intel.com>, Kai Huang <kai.huang@intel.com>
Subject: Re: [PATCH v3 1/8] KVM: Use dedicated mutex to protect kvm_usage_count to avoid deadlock
Date: Fri, 30 Aug 2024 16:45:16 -0700	[thread overview]
Message-ID: <ZtJZjIRdiN8e5_Es@google.com> (raw)
In-Reply-To: <CABgObfZSCZ-dgK3zWao573+RmZSPhnaoMsrify9-48UVhbKVdw@mail.gmail.com>

On Thu, Aug 15, 2024, Paolo Bonzini wrote:
> On Thu, Aug 15, 2024 at 4:40 PM Sean Christopherson <seanjc@google.com> wrote:
> >
> > On Wed, Aug 14, 2024, Paolo Bonzini wrote:
> > > On 6/8/24 02:06, Sean Christopherson wrote:
> > > > Use a dedicated mutex to guard kvm_usage_count to fix a potential deadlock
> > > > on x86 due to a chain of locks and SRCU synchronizations.  Translating the
> > > > below lockdep splat, CPU1 #6 will wait on CPU0 #1, CPU0 #8 will wait on
> > > > CPU2 #3, and CPU2 #7 will wait on CPU1 #4 (if there's a writer, due to the
> > > > fairness of r/w semaphores).
> > > >
> > > >      CPU0                     CPU1                     CPU2
> > > > 1   lock(&kvm->slots_lock);
> > > > 2                                                     lock(&vcpu->mutex);
> > > > 3                                                     lock(&kvm->srcu);
> > > > 4                            lock(cpu_hotplug_lock);
> > > > 5                            lock(kvm_lock);
> > > > 6                            lock(&kvm->slots_lock);
> > > > 7                                                     lock(cpu_hotplug_lock);
> > > > 8   sync(&kvm->srcu);
> > > >
> > > > Note, there are likely more potential deadlocks in KVM x86, e.g. the same
> > > > pattern of taking cpu_hotplug_lock outside of kvm_lock likely exists with
> > > > __kvmclock_cpufreq_notifier()
> > >
> > > Offhand I couldn't see any places where {,__}cpufreq_driver_target() is
> > > called within cpus_read_lock().  I didn't look too closely though.
> >
> > Anyways...
> >
> >   cpuhp_cpufreq_online()
> >   |
> >   -> cpufreq_online()
> >      |
> >      -> cpufreq_gov_performance_limits()
> >         |
> >         -> __cpufreq_driver_target()
> >            |
> >            -> __target_index()
> 
> Ah, I only looked in generic code.
> 
> Can you add a comment to the comment message suggesting switching the vm_list
> to RCU? All the occurrences of list_for_each_entry(..., &vm_list, ...) seem
> amenable to that, and it should be as easy to stick all or part of
> kvm_destroy_vm() behind call_rcu().

+1 to the idea of making vm_list RCU-protected, though I think we'd want to use
SRCU, e.g. set_nx_huge_pages() currently takes eash VM's slots_lock while purging
possible NX hugepages.

And I think kvm_destroy_vm() can simply do a synchronize_srcu() after removing
the VM from the list.  Trying to put kvm_destroy_vm() into an RCU callback would
probably be a bit of a disaster, e.g. kvm-intel.ko in particular currently does
some rather nasty things while destory a VM.

  reply	other threads:[~2024-08-30 23:45 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-08  0:06 [PATCH v3 0/8] KVM: Register cpuhp/syscore callbacks when enabling virt Sean Christopherson
2024-06-08  0:06 ` [PATCH v3 1/8] KVM: Use dedicated mutex to protect kvm_usage_count to avoid deadlock Sean Christopherson
2024-06-10  0:26   ` Huang, Kai
2024-08-14 18:06   ` Paolo Bonzini
2024-08-15 14:39     ` Sean Christopherson
2024-08-15 16:10       ` Paolo Bonzini
2024-08-30 23:45         ` Sean Christopherson [this message]
2024-09-02 13:03           ` Paolo Bonzini
2024-06-08  0:06 ` [PATCH v3 2/8] KVM: Register cpuhp and syscore callbacks when enabling hardware Sean Christopherson
2024-06-10  0:55   ` Huang, Kai
2024-08-14 18:12   ` Paolo Bonzini
2024-08-14 20:55     ` Sean Christopherson
2024-06-08  0:06 ` [PATCH v3 3/8] KVM: Rename functions related to enabling virtualization hardware Sean Christopherson
2024-06-08  0:06 ` [PATCH v3 4/8] KVM: Add a module param to allow enabling virtualization when KVM is loaded Sean Christopherson
2024-08-02 12:02   ` Huang, Kai
2024-08-02 12:06     ` Huang, Kai
2024-08-13  2:31     ` Sean Christopherson
2024-08-13  5:22       ` Huang, Kai
2024-08-13 23:47         ` Huang, Kai
2024-08-14 18:14   ` Paolo Bonzini
2024-06-08  0:06 ` [PATCH v3 5/8] KVM: Add arch hooks for enabling/disabling virtualization Sean Christopherson
2024-08-14 18:15   ` Paolo Bonzini
2024-06-08  0:06 ` [PATCH v3 6/8] x86/reboot: Unconditionally define cpu_emergency_virt_cb typedef Sean Christopherson
2024-06-08  0:06 ` [PATCH v3 7/8] KVM: x86: Register "emergency disable" callbacks when virt is enabled Sean Christopherson
2024-06-08  0:06 ` [PATCH v3 8/8] KVM: Enable virtualization at load/initialization by default Sean Christopherson
2024-08-14 18:20   ` Paolo Bonzini
2024-06-10  0:59 ` [PATCH v3 0/8] KVM: Register cpuhp/syscore callbacks when enabling virt Huang, Kai
2024-08-14 18:23 ` Paolo Bonzini
2024-08-14 22:17   ` Huang, Kai
2024-08-15 14:41     ` Sean Christopherson
2024-08-20  8:57 ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZtJZjIRdiN8e5_Es@google.com \
    --to=seanjc@google.com \
    --cc=chao.gao@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox