Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1350375pxm; Thu, 3 Mar 2022 15:57:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJxZ+yNwl0yGa/UqQ0/Jrn/ERiD5ILgdEavaOUXgQsx5uxsYnON80dTZF8J/6G3yHz00hNZe X-Received: by 2002:a17:907:161f:b0:6da:aaaf:770f with SMTP id hb31-20020a170907161f00b006daaaaf770fmr1821979ejc.540.1646351859526; Thu, 03 Mar 2022 15:57:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646351859; cv=none; d=google.com; s=arc-20160816; b=liahTOuoIU1EkgZ1irR0CnAlnUm7pXWXJNzNxfmEdC8j6NIIkRb/F5PEtLx66fTGUz dAil4Su5JptBmv4HaznxqaXtc0UhOIiJZosTPqhwhMKbyfwYkbn1exCbBVGxhrn20tSd WYNUfXwfDsiQoQiGBc0kyL8Wg5Jbp8lYNh6XwaUsLnxImC9sTPPqDuIw3xU4opBp8CWd IRPHY2/7DFOaJNK71eEc3Vt4DUuz9FNFRWKNs2YD2ITyVdQwCi3HfnFHZrcFcc8wP+PT nF8yg3mUkbT05ulAyptWKU+EugNUmAMCyk2dpyyURosLbnN5T+COhsVKO/klnBRzfEfu elrA== 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:cc:to:from :dkim-signature; bh=tBfpxfdS75xaV9F32q3gt0kX6x3EHKO3to4m06m9CBQ=; b=D1DG76vMzAcZID7FCxvc56gj6tEEeyYkNcYYZAjHDdJWLYLMvXHD9Ysk04nIGQwj1M vNvUuFOZJnZVvf/FxUlAyQxX+6YdSK2mIvFs03BNRfZJ1CnopAfs4yO39g2VPlBNwpHE V+8X39Q4TSUsQ8Wqjsyh3fqAoqDYzy6bY4hcwj4euBFCIEx7hWflBmz7LUstRVEbes+V T3YGkmSE1Lh8PqBed9PSuaw3XB/mZAlGl/CTFlALmosunuJ4cxg3w3Y+09Hcd+ilYmn3 TVFjfZ9sm9UMtqE4nZpcQJG3kJfoDCRj0ZladAPvHk+FbcfU94qdsAurz4KFb+Pp0Il4 FxrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FcvoO1Rn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o11-20020a50c90b000000b00415fb6ac0d5si199917edh.155.2022.03.03.15.57.17; Thu, 03 Mar 2022 15:57:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FcvoO1Rn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236259AbiCCTlV (ORCPT + 99 others); Thu, 3 Mar 2022 14:41:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236250AbiCCTkk (ORCPT ); Thu, 3 Mar 2022 14:40:40 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A06141A39DA for ; Thu, 3 Mar 2022 11:39:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646336360; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tBfpxfdS75xaV9F32q3gt0kX6x3EHKO3to4m06m9CBQ=; b=FcvoO1RnPuKVAQkVk8AJPhJdLM/O1Wt6CmdQp0CdgF/0quKelreL0z/nsw/oeawRo60qBl 0Jr+kPWPHWHwtUTacY92ZnnDEpJzwYiTIXgZjjno1jBm9aiM6DXbe8Ol9/5XpA/W0uNAiF xa/eBmA+pJDt3iPYq2LD2HXbw1lEpt0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-517-0YmxRChDOFi917E33vE8sg-1; Thu, 03 Mar 2022 14:39:17 -0500 X-MC-Unique: 0YmxRChDOFi917E33vE8sg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6005E824FA8; Thu, 3 Mar 2022 19:39:15 +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 8BC745FC22; Thu, 3 Mar 2022 19:39:14 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , David Hildenbrand , David Matlack , Ben Gardon , Mingwei Zhang Subject: [PATCH v4 24/30] KVM: x86/mmu: Zap defunct roots via asynchronous worker Date: Thu, 3 Mar 2022 14:38:36 -0500 Message-Id: <20220303193842.370645-25-pbonzini@redhat.com> In-Reply-To: <20220303193842.370645-1-pbonzini@redhat.com> References: <20220303193842.370645-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Zap defunct roots, a.k.a. roots that have been invalidated after their last reference was initially dropped, asynchronously via the system work queue instead of forcing the work upon the unfortunate task that happened to drop the last reference. If a vCPU task drops the last reference, the vCPU is effectively blocked by the host for the entire duration of the zap. If the root being zapped happens be fully populated with 4kb leaf SPTEs, e.g. due to dirty logging being active, the zap can take several hundred seconds. Unsurprisingly, most guests are unhappy if a vCPU disappears for hundreds of seconds. E.g. running a synthetic selftest that triggers a vCPU root zap with ~64tb of guest memory and 4kb SPTEs blocks the vCPU for 900+ seconds. Offloading the zap to a worker drops the block time to <100ms. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Reviewed-by: Ben Gardon Message-Id: <20220226001546.360188-23-seanjc@google.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/mmu/tdp_mmu.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index e24a1bff9218..2456f880508d 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -170,13 +170,24 @@ void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root, */ if (!kvm_tdp_root_mark_invalid(root)) { refcount_set(&root->tdp_mmu_root_count, 1); - tdp_mmu_zap_root(kvm, root, shared); /* - * Give back the reference that was added back above. We now + * If the struct kvm is alive, we might as well zap the root + * in a worker. The worker takes ownership of the reference we + * just added to root and is flushed before the struct kvm dies. + */ + if (likely(refcount_read(&kvm->users_count))) { + tdp_mmu_schedule_zap_root(kvm, root); + return; + } + + /* + * The struct kvm is being destroyed, zap synchronously and give + * back immediately the reference that was added above. We now * know that the root is invalid, so go ahead and free it if * no one has taken a reference in the meanwhile. */ + tdp_mmu_zap_root(kvm, root, shared); if (!refcount_dec_and_test(&root->tdp_mmu_root_count)) return; } -- 2.31.1