From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CF73DC433FE for ; Thu, 3 Nov 2022 15:41:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=JLoayR0XDC/0ERXg0E10q2Yt1nM2zUXjWjJaWc92Taw=; b=yZqvZXaJIDNurO Rjg5p0NiQ1epVfCP0Y2P7W67KVqFQFkHo+3QROH+h6xkmaUrEVIsRIq2zxnlAa1jYrYVG1NrkOmrI PGDIAIwN7s2rGHK23OqywZSKXR1nLyhcHKfde/gMY289SeNxaAOwbIGdgT8BclTCrlw3OZfF/huLk Mroubya7FaXRyehE7/+OK1tqYqta3rR8Y4DTsedhcLSbozdWmgyor0K7yuTJ0YQ1D0gmB4fxbuw96 VM/g3/LZlrjhvohsutsn0plq6dSNbVnqIytMmsxOM4fiPUExoYgQ5rVYgrWxprU6B+7c5jBuMMdD2 XEE+i5TGrBrNDeIy2QTA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oqcK4-000SEw-8S; Thu, 03 Nov 2022 15:40:06 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oqc3s-000KxZ-Eo for linux-arm-kernel@lists.infradead.org; Thu, 03 Nov 2022 15:23:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667488999; 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=ex9w6AF29xEwpNLs+Jj1aaojn2ZZjhH0ZX9iUoTOfgc=; b=JYIcN4Qvut2ueP8N4RFbgV0SYq5S8nvRybbNe2ttD9jexTpnOj2XngkrGJD88PEFd9PSiO khIJaLvEJXZECQvfBP2WCcEPF4mGZDl/ojdIeFDtnqLr+D9EM85KqMhDa91FrMO3n72YH3 QJLdpwtvDAncQDbMJaVB0ruJZizXNQ0= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-416-Hxvch2QoMa6iBJv_5FiijQ-1; Thu, 03 Nov 2022 11:23:17 -0400 X-MC-Unique: Hxvch2QoMa6iBJv_5FiijQ-1 Received: by mail-ej1-f70.google.com with SMTP id sg37-20020a170907a42500b007adaedb5ba2so1455964ejc.18 for ; Thu, 03 Nov 2022 08:23:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ex9w6AF29xEwpNLs+Jj1aaojn2ZZjhH0ZX9iUoTOfgc=; b=YgTFb/7QZrHfbqp2ws4afUXXW5rnwh0FySMdXk0b5E8lUm7ID+buC8Sx0qRqJnfU2Y jcdFpO6txbnoRngrUbX6YWJZHyuSY6oSTipwvYxrL9EfTi4HbH9fhLkmtdxyM2HgjO0e CgxIP3+2/cT+zxUJ0vARfRvwlWCiZLKY9YjSMpFYLyVbLNESsZnLfSJ+jw1sGSbrJ6hC T5lhdAiGnQhjHE1LNHBwuo1MnxiDONDMQGQXd26SIP0ogiJVcXZyBgfvxsZLEQ77P07d nFr+aVTbeQZXyZGAeV14ur5TfodSTj1jbSj6KaZa9/YGF2mBYt0Tn9cvIdiQnKR7KPBo IhAw== X-Gm-Message-State: ACrzQf0dcO0wBo27LLPjKGVUQqsZnlalu51PRHSMECjM2114EdfYvkPF 5sEhE7a3dAGYM/CYvmPbdTcz+ZZlx3N4kMkrtkCbX1KrMXiPRyEoaCfoM9i1RNjI14ZsGDncact FULwuQmViZ7r7OdhPKbWJWv/kbuc4o+GecQQ= X-Received: by 2002:a17:907:8a07:b0:7ad:e111:9f1f with SMTP id sc7-20020a1709078a0700b007ade1119f1fmr18706645ejc.748.1667488996716; Thu, 03 Nov 2022 08:23:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7w/ZtXgKJ7V+2Z8oeupCi1g9Jll17Pmqv+y4uPqlw3EJEEB8/pAd5jt1iBgMRagb/0UlEDkg== X-Received: by 2002:a17:907:8a07:b0:7ad:e111:9f1f with SMTP id sc7-20020a1709078a0700b007ade1119f1fmr18706603ejc.748.1667488996425; Thu, 03 Nov 2022 08:23:16 -0700 (PDT) Received: from ?IPV6:2001:b07:6468:f312:1c09:f536:3de6:228c? ([2001:b07:6468:f312:1c09:f536:3de6:228c]) by smtp.googlemail.com with ESMTPSA id 9-20020a170906218900b007a9c3831409sm596520eju.137.2022.11.03.08.23.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Nov 2022 08:23:15 -0700 (PDT) Message-ID: <7b6ce80e-7f1f-11cd-8bde-8d8fa9fd7e1d@redhat.com> Date: Thu, 3 Nov 2022 16:23:12 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.0 Subject: Re: [PATCH 39/44] KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Matthew Rosato , Eric Farman , Vitaly Kuznetsov Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton , Atish Patra , David Hildenbrand , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, Isaku Yamahata , Fabiano Rosas , Michael Ellerman , Chao Gao , Thomas Gleixner , Yuan Yao References: <20221102231911.3107438-1-seanjc@google.com> <20221102231911.3107438-40-seanjc@google.com> From: Paolo Bonzini In-Reply-To: <20221102231911.3107438-40-seanjc@google.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221103_082320_604121_ACC66DCD X-CRM114-Status: GOOD ( 23.42 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 11/3/22 00:19, Sean Christopherson wrote: > +- kvm_lock is taken outside kvm->mmu_lock Not surprising since one is a mutex and one is an rwlock. :) You can drop this hunk as well as the "Opportunistically update KVM's locking documentation" sentence in the commit message. > - vcpu->mutex is taken outside kvm->arch.hyperv.hv_lock > > - kvm->arch.mmu_lock is an rwlock. kvm->arch.tdp_mmu_pages_lock and > @@ -216,15 +220,11 @@ time it will be set using the Dirty tracking mechanism described above. > :Type: mutex > :Arch: any > :Protects: - vm_list > - > -``kvm_count_lock`` > -^^^^^^^^^^^^^^^^^^ > - > -:Type: raw_spinlock_t > -:Arch: any > -:Protects: - hardware virtualization enable/disable > -:Comment: 'raw' because hardware enabling/disabling must be atomic /wrt > - migration. > + - kvm_usage_count > + - hardware virtualization enable/disable > + - module probing (x86 only) What do you mean exactly by "module probing"? Is it anything else than what is serialized by vendor_module_lock? Paolo > +:Comment: KVM also disables CPU hotplug via cpus_read_lock() during > + enable/disable. > > ``kvm->mn_invalidate_lock`` > ^^^^^^^^^^^^^^^^^^^^^^^^^^^ > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 4e765ef9f4bd..c8d92e6c3922 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -100,7 +100,6 @@ EXPORT_SYMBOL_GPL(halt_poll_ns_shrink); > */ > > DEFINE_MUTEX(kvm_lock); > -static DEFINE_RAW_SPINLOCK(kvm_count_lock); > LIST_HEAD(vm_list); > > static cpumask_var_t cpus_hardware_enabled; > @@ -5028,9 +5027,10 @@ static void hardware_enable_nolock(void *junk) > > static int kvm_online_cpu(unsigned int cpu) > { > + unsigned long flags; > int ret = 0; > > - raw_spin_lock(&kvm_count_lock); > + mutex_lock(&kvm_lock); > /* > * Abort the CPU online process if hardware virtualization cannot > * be enabled. Otherwise running VMs would encounter unrecoverable > @@ -5039,13 +5039,16 @@ static int kvm_online_cpu(unsigned int cpu) > if (kvm_usage_count) { > WARN_ON_ONCE(atomic_read(&hardware_enable_failed)); > > + local_irq_save(flags); > hardware_enable_nolock(NULL); > + local_irq_restore(flags); > + > if (atomic_read(&hardware_enable_failed)) { > atomic_set(&hardware_enable_failed, 0); > ret = -EIO; > } > } > - raw_spin_unlock(&kvm_count_lock); > + mutex_unlock(&kvm_lock); > return ret; > } > > @@ -5061,10 +5064,13 @@ static void hardware_disable_nolock(void *junk) > > static int kvm_offline_cpu(unsigned int cpu) > { > - raw_spin_lock(&kvm_count_lock); > - if (kvm_usage_count) > + mutex_lock(&kvm_lock); > + if (kvm_usage_count) { > + preempt_disable(); > hardware_disable_nolock(NULL); > - raw_spin_unlock(&kvm_count_lock); > + preempt_enable(); > + } > + mutex_unlock(&kvm_lock); > return 0; > } > > @@ -5079,9 +5085,11 @@ static void hardware_disable_all_nolock(void) > > static void hardware_disable_all(void) > { > - raw_spin_lock(&kvm_count_lock); > + cpus_read_lock(); > + mutex_lock(&kvm_lock); > hardware_disable_all_nolock(); > - raw_spin_unlock(&kvm_count_lock); > + mutex_unlock(&kvm_lock); > + cpus_read_unlock(); > } > > static int hardware_enable_all(void) > @@ -5097,7 +5105,7 @@ static int hardware_enable_all(void) > * Disable CPU hotplug to prevent scenarios where KVM sees > */ > cpus_read_lock(); > - raw_spin_lock(&kvm_count_lock); > + mutex_lock(&kvm_lock); > > kvm_usage_count++; > if (kvm_usage_count == 1) { > @@ -5110,7 +5118,7 @@ static int hardware_enable_all(void) > } > } > > - raw_spin_unlock(&kvm_count_lock); > + mutex_unlock(&kvm_lock); > cpus_read_unlock(); > > return r; > @@ -5716,6 +5724,15 @@ static void kvm_init_debug(void) > > static int kvm_suspend(void) > { > + /* > + * Secondary CPUs and CPU hotplug are disabled across the suspend/resume > + * callbacks, i.e. no need to acquire kvm_lock to ensure the usage count > + * is stable. Assert that kvm_lock is not held as a paranoid sanity > + * check that the system isn't suspended when KVM is enabling hardware. > + */ > + lockdep_assert_not_held(&kvm_lock); > + lockdep_assert_irqs_disabled(); > + > if (kvm_usage_count) > hardware_disable_nolock(NULL); > return 0; > @@ -5723,10 +5740,11 @@ static int kvm_suspend(void) > > static void kvm_resume(void) > { > - if (kvm_usage_count) { > - lockdep_assert_not_held(&kvm_count_lock); > + lockdep_assert_not_held(&kvm_lock); > + lockdep_assert_irqs_disabled(); > + > + if (kvm_usage_count) > hardware_enable_nolock(NULL); > - } > } > > static struct syscore_ops kvm_syscore_ops = { _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel