Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3486382rdh; Thu, 28 Sep 2023 13:08:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGxyziDTFxqS0rN58cxEsIW+gU8cLVCYxLBBMdjCstP1JZuCRIZa3o8xx/wqvNJ/es/zsrn X-Received: by 2002:a17:902:e841:b0:1b6:bced:1dc2 with SMTP id t1-20020a170902e84100b001b6bced1dc2mr2410785plg.0.1695931685575; Thu, 28 Sep 2023 13:08:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695931685; cv=none; d=google.com; s=arc-20160816; b=q0yLOwYU/KVx02xNTfDISYy0d0Aq7o/hsxzPizf+/FRAddcUdULh2zH4AeM9MC6Kxz U/1WZ1G/WKUGHVAy71rMHa0Cg5N8K7C02Vb0Z/vUva0LcMO0tT6XyUxb30AtHHHShK+b t1vWR0nd7805Co4gws7TaQbnseQg9QR+qzsui461+DaqsnmFYxzC61gLScp0e3qn5ODl 8JdE2kRR3anh7XLmWp+RCpOAzDfhPlmj4NzyyQcE8PkVsCyqTQ/1xgpAJ5NStvXHttr2 z3u180JYXUKp5jOp/gRFeq2HCmSanoz61CNoFLCh50jvOWdfBse75NVTl9DEO4ALnWHZ dGVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=kglJ7MDXIf3EPYGcUm7QWoca/YSI3KryqQDz+PiB8OQ=; fh=4N0ilCeWNPUMy3p+fwQEcpDcSktLq8IMxbqwLAweA1k=; b=W5HofB9vQaju7qAXtrr3RyhLiuAFp8ry3bXPwDaSYRdXl/DRMrgPPDKUMHEe8IxsOx qLjfCMjE1Nf3mPZI0somByUd6AOJ7RnLli6gP5JGCp5vNhsBzIWw5MPkFXJPuVaYVPdI G+Q5f0UqdataZMjdegWmqVgMQZnX7dOJJZo2RkomJM5DS7D9UF9yJ1XBBJbB2UIoSM6o eRlD8euLKUjNxlv6ZJ8DVRatdQ8mC6fbk/oBKRkfZFN95MrFpTk+S2liiRRmFij0NxTg A0cZ3XwMp2PFhxBTnEraxbPYezbgKgtKYPqgKu/hTzC9IyK/URe+D0KsJnhnW3L5Nqi6 SYQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=b0vMuiiI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id h11-20020a170902f54b00b001c5f15d24ffsi17639179plf.116.2023.09.28.13.08.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 13:08:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=b0vMuiiI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id B18B6825B52D; Thu, 28 Sep 2023 09:31:20 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231167AbjI1Qav (ORCPT + 99 others); Thu, 28 Sep 2023 12:30:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231255AbjI1Qar (ORCPT ); Thu, 28 Sep 2023 12:30:47 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B75D1A8 for ; Thu, 28 Sep 2023 09:30:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695918604; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kglJ7MDXIf3EPYGcUm7QWoca/YSI3KryqQDz+PiB8OQ=; b=b0vMuiiIvoDrapdR7kL2Bd+2dKQet1JZOlhT/UhL0fKKn7I3uKJUYIRvHH+KVDwk0DHdSx FEZofVGXfir1jV9WqquZ7p6j0Ip1DJZ7wHBHnaQwHuNSOJXbjbTMtxRzIkp/W8poTa5zfC OnF9Gxg6uoEe4ORBUOLsK7HFskZYfps= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-606-DlzphYcnOs-th9PdQp_fug-1; Thu, 28 Sep 2023 12:30:01 -0400 X-MC-Unique: DlzphYcnOs-th9PdQp_fug-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7E845280BC9D; Thu, 28 Sep 2023 16:30:00 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6639C140E969; Thu, 28 Sep 2023 16:30:00 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH 3/3] KVM: x86/mmu: always take tdp_mmu_pages_lock Date: Thu, 28 Sep 2023 12:29:59 -0400 Message-Id: <20230928162959.1514661-4-pbonzini@redhat.com> In-Reply-To: <20230928162959.1514661-1-pbonzini@redhat.com> References: <20230928162959.1514661-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=2.7 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Thu, 28 Sep 2023 09:31:20 -0700 (PDT) It is cheap to take tdp_mmu_pages_lock in all write-side critical sections. We already do it all the time when zapping with read_lock(), so it is not a problem to do it from the kvm_tdp_mmu_zap_all() path (aka kvm_arch_flush_shadow_all(), aka VM destruction and MMU notifier release). Signed-off-by: Paolo Bonzini --- Documentation/virt/kvm/locking.rst | 6 ++---- arch/x86/kvm/mmu/tdp_mmu.c | 17 ++++++----------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/Documentation/virt/kvm/locking.rst b/Documentation/virt/kvm/locking.rst index 3a034db5e55f..381eb0e7d947 100644 --- a/Documentation/virt/kvm/locking.rst +++ b/Documentation/virt/kvm/locking.rst @@ -43,10 +43,8 @@ On x86: - vcpu->mutex is taken outside kvm->arch.hyperv.hv_lock and kvm->arch.xen.xen_lock -- kvm->arch.mmu_lock is an rwlock. kvm->arch.tdp_mmu_pages_lock and - kvm->arch.mmu_unsync_pages_lock are taken inside kvm->arch.mmu_lock, and - cannot be taken without already holding kvm->arch.mmu_lock (typically with - ``read_lock`` for the TDP MMU, thus the need for additional spinlocks). +- kvm->arch.mmu_lock is an rwlock and is taken outside + kvm->arch.tdp_mmu_pages_lock and kvm->arch.mmu_unsync_pages_lock Everything else is a leaf: no other lock is taken inside the critical sections. diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index b9abfa78808a..f61bc842067f 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -285,24 +285,19 @@ static void tdp_unaccount_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp) * the MMU lock and the operation must synchronize with other * threads that might be adding or removing pages. */ -static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp, - bool shared) +static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp) { + lockdep_assert_held(&kvm->mmu_lock); + tdp_unaccount_mmu_page(kvm, sp); if (!sp->nx_huge_page_disallowed) return; - if (shared) - spin_lock(&kvm->arch.tdp_mmu_pages_lock); - else - lockdep_assert_held_write(&kvm->mmu_lock); - + spin_lock(&kvm->arch.tdp_mmu_pages_lock); sp->nx_huge_page_disallowed = false; untrack_possible_nx_huge_page(kvm, sp); - - if (shared) - spin_unlock(&kvm->arch.tdp_mmu_pages_lock); + spin_unlock(&kvm->arch.tdp_mmu_pages_lock); } /** @@ -331,7 +326,7 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared) trace_kvm_mmu_prepare_zap_page(sp); - tdp_mmu_unlink_sp(kvm, sp, shared); + tdp_mmu_unlink_sp(kvm, sp); for (i = 0; i < SPTE_ENT_PER_PAGE; i++) { tdp_ptep_t sptep = pt + i; -- 2.39.1