Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp5578965imw; Wed, 20 Jul 2022 08:17:49 -0700 (PDT) X-Google-Smtp-Source: AGRyM1utML4hTSpijlkoWkPF19/B73G8IpbgOz3kvx2iKUPRlBK/m2OsIyyKNw/DtRXSa86sroBj X-Received: by 2002:a17:90b:203:b0:1f2:28db:e7d0 with SMTP id fy3-20020a17090b020300b001f228dbe7d0mr295546pjb.23.1658330269319; Wed, 20 Jul 2022 08:17:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658330269; cv=none; d=google.com; s=arc-20160816; b=KvIh+lbsL7fh16RwOgSZ+THQ1qylx5DXROcpZQtCQK801xU/6UMYhbaT7oyrmhQoPp niBO8EhBLrx6vqVjON63jwRfn3rNYoU1O4zddUzeb7Hq3vfS4m25rxkuLmRIA8aPKpzb v3IM+a68FKaeQb6GPg7HJXB3NA6Yi0eGIAXGvaD8PfcVRsk0gLRwv4h2O8Q9o1c80ZRW Y7FW22tBm0Nt87a5GwDv70ZZBk0tvBzF26J9si7STXVd88bDlwgW98sWoY438UNAG0g+ 2sEzXkPbuZVFZKCTwkpwSVpCYgwybouWB9rTBUgN3lfA0kFyQorLiRSwYo9jlx0eb6Bv BiJQ== 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 :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=cvhT3S9WVM0zbX99AylzeVt8EncWc0nM5sQ4BeMLhBk=; b=K0fyTukFzFo5y863P+mcsSrqkgFFEyfcbaAu/yJtpA/t9OILAdqZbV89yUMmkYVXIj jstJNZDmZsV/qlvPcjQkLruwXgoYDCLIZSKwoL/kFoWqP3+W/AS4mdkgn35Ebj95IJXj WquSxGQ6SsA16SbNw7eYJ6d7oZ5PVj2RiJ/ZjoVhRSzXZylALZTkoLa1rRsz690GBwn8 RFVTN9K8TzAElAORl9CX6c/Dbxa2UC2wFYdOFwHNrYy0tJ9axqMqujRx/hjvuo0uhSf5 tn/ZCiv+rqOraAzrKJVdPesl8BnpG6koutjxO/zfFmLq4vKGG+htT4M8BFpDs4Yj5NDY yEkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UzvpBvIv; 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 k11-20020a056a00168b00b0050d80e4935fsi23541645pfc.256.2022.07.20.08.17.34; Wed, 20 Jul 2022 08:17:49 -0700 (PDT) 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=UzvpBvIv; 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 S232620AbiGTOma (ORCPT + 99 others); Wed, 20 Jul 2022 10:42:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231239AbiGTOm3 (ORCPT ); Wed, 20 Jul 2022 10:42:29 -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 ESMTP id 6E9294E606 for ; Wed, 20 Jul 2022 07:42:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658328147; 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=cvhT3S9WVM0zbX99AylzeVt8EncWc0nM5sQ4BeMLhBk=; b=UzvpBvIv0zEd7m4CXN88yg4EWop+mXthhR84RibTwejlWl0LCUUUgaCYDDXiYSU+S/peyT tkLhMKPfXnrCwV2tX95MP7yx8D45MZgZtvpmgxqdDAuTsVLNfUJtrhPJ4wBFaT1HQrmpcS GxaaeJUq56j+WbLtKLs8gZ7We3XVMWs= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-196-RBoGz8fiMD-fcgWvxjAGvQ-1; Wed, 20 Jul 2022 10:42:26 -0400 X-MC-Unique: RBoGz8fiMD-fcgWvxjAGvQ-1 Received: by mail-qk1-f199.google.com with SMTP id n15-20020a05620a294f00b006b5768a0ed0so14241606qkp.7 for ; Wed, 20 Jul 2022 07:42:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=cvhT3S9WVM0zbX99AylzeVt8EncWc0nM5sQ4BeMLhBk=; b=lVMoJjfer0FJ3IGTe1fnMDQ0rQmUoFNI6VCsm5dtsLB9M/bf8pDLLk9QUSiNdSZqLm kFGBBHVps4kQ0g6vEl1Bn7etMtaoWWUWKGH0yeGqHUd7hIxCSLlziEyMDofBPnUpsacA taACR5z8j6AGyR/xN99dg8LP1ANufdKnGZXwVzBiwCEmdwnkkADA62V3ZKAJIRhtQ/ng LC4ko++By2nk6YSPqm6ddn5/ee7He347PMK8g40KIDEfuWEjttBc9paQiGZgCGtCKFbQ lUPdSsvz5/VrK1QasCwQNXtaAolmwMRtHmCeOO1R/fhdmScarIZ16X2RzuFebIU/3z33 Lgrw== X-Gm-Message-State: AJIora+t+e2M5uyyYuf4m/vtUmzW21aPEVB7UK+F4j2ygjywvLpMql4V OwshfWW/CUXar9mRvjUwPG10HBGXBPSEdDCjPo97yxMQjrCyoWxMfix7y/y1xvAAq+H7D9Nt0W2 /ff1isIdDyQRxVihPOGem0kXg X-Received: by 2002:a05:622a:1a14:b0:31e:e89f:4dda with SMTP id f20-20020a05622a1a1400b0031ee89f4ddamr15317179qtb.622.1658328145877; Wed, 20 Jul 2022 07:42:25 -0700 (PDT) X-Received: by 2002:a05:622a:1a14:b0:31e:e89f:4dda with SMTP id f20-20020a05622a1a1400b0031ee89f4ddamr15317158qtb.622.1658328145616; Wed, 20 Jul 2022 07:42:25 -0700 (PDT) Received: from [10.35.4.238] (bzq-82-81-161-50.red.bezeqint.net. [82.81.161.50]) by smtp.gmail.com with ESMTPSA id y206-20020a3764d7000000b006b5652edb93sm16190459qkb.48.2022.07.20.07.42.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jul 2022 07:42:24 -0700 (PDT) Message-ID: Subject: Re: [RFC PATCH v3 04/19] KVM: x86: mmu: allow to enable write tracking externally From: Maxim Levitsky To: Sean Christopherson Cc: kvm@vger.kernel.org, Wanpeng Li , Vitaly Kuznetsov , Jani Nikula , Paolo Bonzini , Tvrtko Ursulin , Rodrigo Vivi , Zhenyu Wang , Joonas Lahtinen , Tom Lendacky , Ingo Molnar , David Airlie , Thomas Gleixner , Dave Hansen , x86@kernel.org, intel-gfx@lists.freedesktop.org, Daniel Vetter , Borislav Petkov , Joerg Roedel , linux-kernel@vger.kernel.org, Jim Mattson , Zhi Wang , Brijesh Singh , "H. Peter Anvin" , intel-gvt-dev@lists.freedesktop.org, dri-devel@lists.freedesktop.org Date: Wed, 20 Jul 2022 17:42:17 +0300 In-Reply-To: <5ed0d0e5a88bbee2f95d794dbbeb1ad16789f319.camel@redhat.com> References: <20220427200314.276673-1-mlevitsk@redhat.com> <20220427200314.276673-5-mlevitsk@redhat.com> <5ed0d0e5a88bbee2f95d794dbbeb1ad16789f319.camel@redhat.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-5.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE 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 On Sun, 2022-05-22 at 13:22 +0300, Maxim Levitsky wrote: > On Thu, 2022-05-19 at 16:37 +0000, Sean Christopherson wrote: > > On Wed, Apr 27, 2022, Maxim Levitsky wrote: > > > @@ -5753,6 +5752,10 @@ int kvm_mmu_init_vm(struct kvm *kvm) > > >         node->track_write = kvm_mmu_pte_write; > > >         node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot; > > >         kvm_page_track_register_notifier(kvm, node); > > > > Can you add a patch to move this call to kvm_page_track_register_notifier() into > > mmu_enable_write_tracking(), and simultaneously add a WARN in the register path > > that page tracking is enabled? > > > > Oh, actually, a better idea. Add an inner __kvm_page_track_register_notifier() > > that is not exported and thus used only by KVM, invoke mmu_enable_write_tracking() > > from the exported kvm_page_track_register_notifier(), and then do the above. > > That will require modifying KVMGT and KVM in a single patch, but that's ok. > > > > That will avoid any possibility of an external user failing to enabling tracking > > before registering its notifier, and also avoids bikeshedding over what to do with > > the one-line wrapper to enable tracking. > > > > This is a good idea as well, especially looking at kvmgt and seeing that > it registers the page track notifier, when the vGPU is opened. > > I'll do this in the next series. > > Thanks for the review! After putting some thought into this, I am not 100% sure anymore I want to do it this way. Let me explain the current state of things: For mmu: - write tracking notifier is registered on VM initialization (that is pretty much always), and if it is called because write tracking was enabled due to some other reason (currently only KVMGT), it checks the number of shadow mmu pages and if zero, bails out. - write tracking enabled when shadow root is allocated. This can be kept as is by using the __kvm_page_track_register_notifier as you suggested. For KVMGT: - both write tracking and notifier are enabled when an vgpu mdev device is first opened. That 'works' only because KVMGT doesn't allow to assign more that one mdev to same VM, thus a per VM notifier and the write tracking for that VM are enabled at the same time Now for nested AVIC, this is what I would like to do: - just like mmu, I prefer to register the write tracking notifier, when the VM is created. - just like mmu, write tracking should only be enabled when nested AVIC is actually used first time, so that write tracking is not always enabled when you just boot a VM with nested avic supported, since the VM might not use nested at all. Thus I either need to use the __kvm_page_track_register_notifier too for AVIC (and thus need to export it) or I need to have a boolean (nested_avic_was_used_once) and register the write tracking notifier only when false and do it not on VM creation but on first attempt to use nested AVIC. Do you think this is worth it? I mean there is some value of registering the notifier only when needed (this way it is not called for nothing) but it does complicate things a bit. I can also stash this boolean (like 'bool registered;') into the 'struct kvm_page_track_notifier_node',  and thus allow the kvm_page_track_register_notifier to be called more that once -  then I can also get rid of __kvm_page_track_register_notifier.  What do you think about this? Best regards, Maxim Levitsky > > Best regards, >         Maxim Levitsky