From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C33E02253EF for ; Fri, 24 Jan 2025 19:11:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737745884; cv=none; b=MNPmCPQntjAoeHwUyk0f+3gWwgC3xNYhSMJ5N9Tve7FFlXc1sMXsaB+9U5zUJQHiEWXDppPBtIlidtO0ZAvqMfBYzcrP9o3mHuuC6KoSkKT1vWh2uLtcAOllKjMTMM2/kA4UVea7/wx6PlPQv79ut0IcPUDCTFddT/6vOJxY6ZM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737745884; c=relaxed/simple; bh=27nAJiwy8vNl5ENOjGy1QEtnVVB2ty2t0SrZitqHlPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=eOM675hpTYtsojNIDnAInVVMeSh+NbIywxBlal/iau/ohzn3kPyGmdJFjA6DTQ/uwzAb6cxIMJxpPSFdYee0wrOas8iKdoaw5cxt6jblQdvSomx0QcBNK8MpDCBcZBH3Yjt+fNkTbpPa5+TSGIQSBNpxINyKk/apmeuPsY72n3I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Dg99iJBW; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Dg99iJBW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737745881; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fFGYjJI+yyYMmuqTYYSVTNPCKJfp068RQBYfyuIBjkM=; b=Dg99iJBWREZN+4GoRXBCpk53d8pEch6T3//YC+yq9/3HgHU9PLllSdmrFRemFpdsEZeNpf ujo8tng+Yr//pB4Ti7ckOsAimByoX0Lfay/mPkgn/B79fQmUB3YBJRKACjiWDGv2fRwfAO TPpViE1Q/Ao/aTazm1HT4xvLpqe8SZg= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-575-X02C7rrwMdKW3_1z0OzNHw-1; Fri, 24 Jan 2025 14:11:15 -0500 X-MC-Unique: X02C7rrwMdKW3_1z0OzNHw-1 X-Mimecast-MFC-AGG-ID: X02C7rrwMdKW3_1z0OzNHw Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8AAC51801F26; Fri, 24 Jan 2025 19:11:12 +0000 (UTC) Received: from virtlab1023.lab.eng.rdu2.redhat.com (virtlab1023.lab.eng.rdu2.redhat.com [10.8.1.187]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E27DC19560A7; Fri, 24 Jan 2025 19:11:11 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com Subject: [PATCH 2/2] Documentation: explain issues with taking locks inside kvm_lock Date: Fri, 24 Jan 2025 14:11:09 -0500 Message-ID: <20250124191109.205955-3-pbonzini@redhat.com> In-Reply-To: <20250124191109.205955-1-pbonzini@redhat.com> References: <20250124191109.205955-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 kvm_lock should be used sparingly, and it is easy to protect vm_list walks with kvm_get_kvm and kvm_put_kvm. Make it a hard rule to drop kvm_lock before taking another mutex, and document it. Signed-off-by: Paolo Bonzini --- Documentation/virt/kvm/locking.rst | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/Documentation/virt/kvm/locking.rst b/Documentation/virt/kvm/locking.rst index c56d5f26c750..f94aad9b95ab 100644 --- a/Documentation/virt/kvm/locking.rst +++ b/Documentation/virt/kvm/locking.rst @@ -26,13 +26,6 @@ The acquisition orders for mutexes are as follows: are taken on the waiting side when modifying memslots, so MMU notifiers must not take either kvm->slots_lock or kvm->slots_arch_lock. -cpus_read_lock() vs kvm_lock: - -- Taking cpus_read_lock() outside of kvm_lock is problematic, despite that - being the official ordering, as it is quite easy to unknowingly trigger - cpus_read_lock() while holding kvm_lock. Use caution when walking vm_list, - e.g. avoid complex operations when possible. - For SRCU: - ``synchronize_srcu(&kvm->srcu)`` is called inside critical sections @@ -59,6 +52,23 @@ On x86: Everything else is a leaf: no other lock is taken inside the critical sections. +In particular no other mutex should be taken inside kvm_lock, and the +amount of code that can be run inside kvm_lock should be limited; this +is because ``cpus_read_lock()`` might be triggered unknowingly and cause +a circular dependency. For example, if you take ``kvm->slots_lock`` +inside ``kvm_lock``, the following can happen on x86: + +- ``kvm->srcu`` is synchronized with ``kvm->slots_lock`` taken +- you wait for ``kvm->slots_lock`` with ``kvm_lock`` taken +- ``__kvmclock_cpufreq_notifier()`` waits for ``kvm_lock`` and + is called within ``cpus_read_lock()``. +- ``KVM_RUN`` can trigger static key updates, which call ``cpus_read_lock()``, + with ``kvm->srcu`` taken +- therefore ``synchronize_srcu(&kvm->srcu)`` never completes. + +This rule applies to all architectures. + + 2. Exception ------------ @@ -238,6 +248,9 @@ time it will be set using the Dirty tracking mechanism described above. :Type: mutex :Arch: any :Protects: - vm_list + - kvm_createvm_count + - kvm_active_vms +:Comment: Do not take any mutex inside. ``kvm_usage_lock`` ^^^^^^^^^^^^^^^^^^ -- 2.43.5