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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E5A2C433FE for ; Fri, 4 Mar 2022 06:48:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238577AbiCDGtU (ORCPT ); Fri, 4 Mar 2022 01:49:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233196AbiCDGtR (ORCPT ); Fri, 4 Mar 2022 01:49:17 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BAAD3793B2 for ; Thu, 3 Mar 2022 22:48:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646376508; 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=SYw46/juAkVUmAthbyDhSf57VAFYaSUG9vWYk421L9A=; b=TkIvDvAvwkFcxAyotONlGicDbzZ7TSN2owC8pYbRk/pVNGdCNysLzEm/cZykLkwPayZJ5g kjmwT+DoZElinFgShaxI2Nw5nEmn8bGSVO9aDx3XrnWpw1sOot+6W56TzK3qaaCgYbbDxp 2I9N9PeXt2QRin7TqoCsXbfIgJCa0lc= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-60-l8fGs-wcNlydSii7iNoacw-1; Fri, 04 Mar 2022 01:48:27 -0500 X-MC-Unique: l8fGs-wcNlydSii7iNoacw-1 Received: by mail-ed1-f70.google.com with SMTP id s7-20020a508dc7000000b0040f29ccd65aso4132888edh.1 for ; Thu, 03 Mar 2022 22:48:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=SYw46/juAkVUmAthbyDhSf57VAFYaSUG9vWYk421L9A=; b=mdfuihfTSc1RjBg/10GJa97WdfAGx/EwG80OIfGeVL0T+nDGy09PWK3x6oxyOUVcuk Fv5Gwx818gc0YhtI5YPb3FBnsl8mFA7LAl/pgy7D3EFLd8y7C3XWnbk+mDZs7ZxLk1n+ YJxoa58nI1RUc6dZ8zG/gZ9IJqk+0qXc5wMqV7GYI/Cjj7EUrrhkdaC3lExMgOOTlJb1 JocJLnefsfrI4rLCb93/hGTG1MVFi8sAjlRCjZ0s4S4/7hR97F7QLbr/tVzZZIzRStJ5 t/n6Hg5VRiEFsfm84RA5fFoHGg/ozrJlW5I2kiDrB9b+evkCjVFCEWqqS2tfRHVoOGKs WzjA== X-Gm-Message-State: AOAM533hW8ecjN7inGGggC4gknAcDRx3ikf/rJwcqFYcsuHtq/wsi7ng wDtv+wT+BZWYZOeY3QBrQA/NNAdKRv+uRIstvwuUBjvM0BLfsWuUpAhFq3sWFcMFKfGtkPvIZ3B YFwLnbnSbhkeLntq98vFLTqug X-Received: by 2002:a05:6402:50d4:b0:413:2a27:6b56 with SMTP id h20-20020a05640250d400b004132a276b56mr38090079edb.228.1646376506581; Thu, 03 Mar 2022 22:48:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJy1wppOn/f0V2yisR+TjpMzXr2rsZtTf4v8keVhYhkYrwzXX2jWMUo09KtjajHgzmgLDaDAPw== X-Received: by 2002:a05:6402:50d4:b0:413:2a27:6b56 with SMTP id h20-20020a05640250d400b004132a276b56mr38090071edb.228.1646376506390; Thu, 03 Mar 2022 22:48:26 -0800 (PST) Received: from ?IPV6:2001:b07:6468:f312:63a7:c72e:ea0e:6045? ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.googlemail.com with ESMTPSA id f5-20020a1709067f8500b006da68bfdfc7sm1431372ejr.12.2022.03.03.22.48.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Mar 2022 22:48:25 -0800 (PST) Message-ID: Date: Fri, 4 Mar 2022 07:48:19 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH v4 21/30] KVM: x86/mmu: Zap invalidated roots via asynchronous worker Content-Language: en-US To: Sean Christopherson Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , David Hildenbrand , David Matlack , Ben Gardon , Mingwei Zhang References: <20220303193842.370645-1-pbonzini@redhat.com> <20220303193842.370645-22-pbonzini@redhat.com> From: Paolo Bonzini In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/3/22 22:32, Sean Christopherson wrote: > The re-queue scenario happens if a root is queued and zapped, but is kept alive > by a vCPU that hasn't yet put its reference. If another memslot comes along before > the (sleeping) vCPU drops its reference, this will re-queue the root. > > It's not a major problem in this patch as it's a small amount of wasted effort, > but it will be an issue when the "put" path starts using the queue, as that will > create a scenario where a memslot update (or NX toggle) can come along while a > defunct root is in the zap queue. As of this patch it's not a problem because kvm_tdp_mmu_invalidate_all_roots()'s caller holds kvm->slots_lock, so kvm_tdp_mmu_invalidate_all_roots() is guarantee to queue its work items on an empty workqueue. In effect the workqueue is just a fancy list. But as you point out in the review to patch 24, it becomes a problem when there's no kvm->slots_lock to guarantee that. Then it needs to check that the root isn't already invalid. >>> The only issue is that kvm_tdp_mmu_invalidate_all_roots() now assumes that >>> there is at least one reference in kvm->users_count; so if the VM is dying >>> just go through the slow path, as there is nothing to gain by using the fast >>> zapping. >> This isn't actually implemented.:-) > Oh, and when you implement it (or copy paste), can you also add a lockdep and a > comment about the check being racy, but that the race is benign? It took me a > second to realize why it's safe to use a work queue without holding a reference > to @kvm. I didn't remove the paragraph from the commit message, but I think it's unnecessary now. The workqueue is flushed in kvm_mmu_zap_all_fast() and kvm_mmu_uninit_tdp_mmu(), unlike the buggy patch, so it doesn't need to take a reference to the VM. I think I don't even need to check kvm->users_count in the defunct root case, as long as kvm_mmu_uninit_tdp_mmu() flushes and destroys the workqueue before it checks that the lists are empty. I'll wait to hear from you later today before sending out v5. Paolo