From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36FD218F2FA for ; Fri, 30 Aug 2024 23:45:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725061520; cv=none; b=TDzNiZgC/+6As2jXF7YKA0Qs/RT3oY6CGBJy9ry6xBirucRHsMxVbCVtux/LsiJypvyxBmkI61eX75nY2rzMkYb6vrFILtWGC+ddcXqHUDADrW+W2xBwjXVbIo9MfHUTARs8nuG6Tx9SJh4hwBr46Ge6NH8u3k7Z2ZmiHWNKc80= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725061520; c=relaxed/simple; bh=wMq8fcpcUbfH9RqkPRWJri2Nj7kzPTs23AYrLOXTvkk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NJCZN2oBVqqm/8QPSHuoNitmYJRW9EPfK4cqT6szps0mHaO3AhFwLh9yOdSEwxNcnXV+6SoFshnHxFmJZVrYCgrOGm8pAojaa3cDgVoPAavtGMUR97L7ilT9Fa14JPrkWSeG7QmH3J58wIHdPjpiJ1MKAg7SuzX6xCa3UvgjAwY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=n5E5gcI8; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="n5E5gcI8" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-690404fd230so41257037b3.3 for ; Fri, 30 Aug 2024 16:45:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725061518; x=1725666318; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=rJOwOLrN9fCKsj31ZOyoET6NEGryeoiVS8pcmwkgvZY=; b=n5E5gcI83b6uAQivqmHbIds7cjNDsSTx3PZ/W+u4/PY/dkjMRvPen6mAwR9wk3cJ14 9AMEEPrB6kqbJ5cu2An8sP6HQL0b0PL/4gjFazpS5gs1XdWGvIloIg+27HH+YMUE0NhL zsyHSmKiQfrUelXPXV5KHXZ3KQbb15fp9viAuAxkrHeqEWZxawelKH86+c3d1NqCH9h7 iLd0Pgo+6a2xsRZFzapOiFrPbGr1LSUadUur6NMXXoC5YL72GUcyWiE9D+hvfkikmRuh ujJwzxQqZHOcNpTblUoEg9bOd8uwEGyV0iA6o9KXd56sJonojpTH6THnNlFzczqhWo2R 0iJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725061518; x=1725666318; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=rJOwOLrN9fCKsj31ZOyoET6NEGryeoiVS8pcmwkgvZY=; b=JOK+WLNw1YZe6b4SptdLdDhzeFgGbAY841UbOIGvRxXOjbLtmSH+t0INBdXmRFZuGn 4IExNmsce92nOo49oDRHMdj8nFK8HmKJpxwxUFVFdcEtDbQZoLrzioWve9kzFYno3RwE 4ctA5pu5HXczIUMFWlEvr0l8DgqCsbDxFMskgopXc6KqQzk8Z+CEi/twlnbvIOUue9wA t5wOjnPpY3sdoloWU7+oTnu13CsldbX35Gc9lDQBq90eh0xeq5XclH1a6vk725kYZGZ+ aIMorpzvuVK5WhFMb7bVuhPUKsKcCPx1mlBhUgEvEhSVS4jpgDiH0um6Ud92MQ8wkMKj rkJA== X-Forwarded-Encrypted: i=1; AJvYcCWxltK0VmzOeJDCPlMPQmtG7YE7NliF9MaHb7kAuBChoYnI6IZwsF9E2M6pp6V/ddqrIQ0R6U0U6lxXbLg=@vger.kernel.org X-Gm-Message-State: AOJu0YzLmznnXOylg3U7cSzNxL1zg4QjChYYC7xGlUkNzJOA9cJcr/Qy QLjv6VgXFXBcKgYVWol2KO64FNP2ahVYaEd/nXVl1xWNpJqYtWnyWYt6lAbviyZGFkgF6WW5tEa 7UQ== X-Google-Smtp-Source: AGHT+IH1lPOudhT62PJGTofQxOruBAiqUYlrCNQI4oNs0c97UCsba7G2Vigk3ybkwDh2sT9EBq08lseCwvI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:2493:b0:e0e:4350:d7de with SMTP id 3f1490d57ef6-e1a7a1786a0mr6613276.9.1725061518076; Fri, 30 Aug 2024 16:45:18 -0700 (PDT) Date: Fri, 30 Aug 2024 16:45:16 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240608000639.3295768-1-seanjc@google.com> <20240608000639.3295768-2-seanjc@google.com> Message-ID: Subject: Re: [PATCH v3 1/8] KVM: Use dedicated mutex to protect kvm_usage_count to avoid deadlock From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Gao , Kai Huang Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Thu, Aug 15, 2024, Paolo Bonzini wrote: > On Thu, Aug 15, 2024 at 4:40=E2=80=AFPM Sean Christopherson 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 d= eadlock > > > > on x86 due to a chain of locks and SRCU synchronizations. Translat= ing the > > > > below lockdep splat, CPU1 #6 will wait on CPU0 #1, CPU0 #8 will wai= t 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->m= utex); > > > > 3 lock(&kvm->sr= cu); > > > > 4 lock(cpu_hotplug_lock); > > > > 5 lock(kvm_lock); > > > > 6 lock(&kvm->slots_lock); > > > > 7 lock(cpu_hotp= lug_lock); > > > > 8 sync(&kvm->srcu); > > > > > > > > Note, there are likely more potential deadlocks in KVM x86, e.g. th= e same > > > > pattern of taking cpu_hotplug_lock outside of kvm_lock likely exist= s 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() >=20 > Ah, I only looked in generic code. >=20 > 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, ...) se= em > 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 p= urging possible NX hugepages. And I think kvm_destroy_vm() can simply do a synchronize_srcu() after remov= ing 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.