Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp5641416rwb; Wed, 21 Sep 2022 10:20:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6XSYpS5doClZKa1ciMdYgmVC67rx1vwzRi7bvdCDoLYhaN/gFDUQGYvCH814fGlo51eD/2 X-Received: by 2002:a17:90b:1d8b:b0:200:5367:5ecd with SMTP id pf11-20020a17090b1d8b00b0020053675ecdmr10714648pjb.165.1663780835936; Wed, 21 Sep 2022 10:20:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663780835; cv=none; d=google.com; s=arc-20160816; b=IRXys7NfC7PqHUYCcCRpQIadU7bYwP4JBjLLRptee5VNdrGhG5phy+CpSWd5gl1MMJ xror5Lh1oci8XC0HVi3ilV/ANAUtdTnBvlmHXXywv0L9nl51xLqBGARt5u868qhSl01L fqwc0EDi5rWpPBiDQeb/6993mPq9L3awbQwKKiYWWDfP8MgMz84+ul6iWCHqKa/ee0B7 3ADmRs+hy8UBwkWZIjws8SzABzRp3wIN3qAgcO5GRg0mEqH8SrwSMHpXyILGIPw2K6k7 ajHoG+r9jvp+FQJViAcpN7JefYZMQok3WY8ovxCMHIc6jbSRJ2rqJC+zwa2Cv7H4T3x6 173w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=JZfIBMlS2x8Yy9k3gjqr0aT1iELoQxmYkxILnyXMMIU=; b=iHVYgHfftzJlC3GWnmkQOnmpe0e2tohHpBwZLcst/XeyAcbN6pQdbpLZTlXJRabkur PnhpXaK438jNHR0VQof/MbbkOfSTJ5jIn1EHmFdWjJuYexJi/a6n5VgE+n/8qQdwjNWL oilN1zCfPYwpgvQrBQfvGiQG9TQEApAXl/fxrHlHg5+o05eGYWjuLcE6IzvWer8pz73n ds2L8JGTapKSrjLIrXuBJtQvKB4ibvI/iHcnAOH7Aksmh8NnWD5My+wUCLUnPf/PIMCC FQugsLqAvs++uD/EvEQ+qwpbQXq2HVLWfzOipJ1EYKOOEQeU7MsMjYCzC4Nd46t5XX4k a6SA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=QMTsI7sI; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f10-20020a17090ab94a00b001fd87298f34si3489901pjw.54.2022.09.21.10.20.24; Wed, 21 Sep 2022 10:20:35 -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=@google.com header.s=20210112 header.b=QMTsI7sI; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231834AbiIUQun (ORCPT + 99 others); Wed, 21 Sep 2022 12:50:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231422AbiIUQuQ (ORCPT ); Wed, 21 Sep 2022 12:50:16 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83A2095682 for ; Wed, 21 Sep 2022 09:45:31 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id e67so371138pgc.12 for ; Wed, 21 Sep 2022 09:45:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=JZfIBMlS2x8Yy9k3gjqr0aT1iELoQxmYkxILnyXMMIU=; b=QMTsI7sI0sDBmr4p1PRMig5+jVttyHA6AUnNS0PKcAkiLoVBwcc431trVt0qT6edyM +dGKqMAbZYLKJvcZKYhEDS8oIEl94it51RkKYnMFiD3y+DR3sTaI+1lImTXYOskCOuvr fTVNWFcW0sU/OHTVMjuZcBuhbScWU0VhNdQCSDpddFBYsat1912NiZwVydVpu5ycF+4/ 3VPUsi5q6BCQVl/3ONDnhPpe8ja46lRaOpFmyEfTQzwyWcH63YX5E8+9lKYNj76x16PC 8biwyzjt4ewkLeoQmVVvWIkbQZvsqJzIHdrmGxSAoRiDvzdARR58tBw22+z3ilC01GIj GBSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=JZfIBMlS2x8Yy9k3gjqr0aT1iELoQxmYkxILnyXMMIU=; b=XrWkyaYDVhmWMJanFDw8x6WJD8iMXU/Rd+HyEc+A0lRX3612PDJvFJzPhPyAUsObKQ HvikbNgSLW/KxHe8uQSW0vV6t536hJRmyIYaUq2m+mDZtm6UKF18bgkzfOvvZjpxpT2W F4u4NwtDyO5ibsZGJK+0WPckwidnAXO2QI+fxLHMbIEi+MekTNDfUmwwj7SxAxhRlgik 3mOEg+lgzZQXBu2FHkToY9dBkcTvvls7wfgo3bj7DULWzXfspNPnQpntxGvyPk90HZB+ N0bli5oWquksoua5pABl+xmIrtDfr0cJwNAutyX+CvsmF6oDT9fiK6+jMetrbIx6YcBV J61A== X-Gm-Message-State: ACrzQf0yam98LXIsrY4XYl210UETC9QzYyKnSfQmBTSTRSYbUakMNkT9 Jgs703tX2Vr7EpRuVkRL5qgtrA== X-Received: by 2002:a65:6a4d:0:b0:439:a0fb:322b with SMTP id o13-20020a656a4d000000b00439a0fb322bmr25639153pgu.10.1663778730870; Wed, 21 Sep 2022 09:45:30 -0700 (PDT) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id u1-20020a1709026e0100b00179988ca61bsm37972plk.161.2022.09.21.09.45.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 09:45:30 -0700 (PDT) Date: Wed, 21 Sep 2022 16:45:26 +0000 From: Sean Christopherson To: Vitaly Kuznetsov Cc: kvm@vger.kernel.org, Paolo Bonzini , Wanpeng Li , Jim Mattson , Michael Kelley , Siddharth Chandrasekaran , Yuan Yao , Maxim Levitsky , linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v10 02/39] KVM: x86: hyper-v: Resurrect dedicated KVM_REQ_HV_TLB_FLUSH flag Message-ID: References: <20220921152436.3673454-1-vkuznets@redhat.com> <20220921152436.3673454-3-vkuznets@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Wed, Sep 21, 2022, Sean Christopherson wrote: > On Wed, Sep 21, 2022, Vitaly Kuznetsov wrote: > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > index f62d5799fcd7..86504a8bfd9a 100644 > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -3418,11 +3418,17 @@ static inline void kvm_vcpu_flush_tlb_current(struct kvm_vcpu *vcpu) > > */ > > void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu) > > { > > - if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) > > + if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) { > > kvm_vcpu_flush_tlb_current(vcpu); > > + kvm_clear_request(KVM_REQ_HV_TLB_FLUSH, vcpu); > > This isn't correct, flush_tlb_current() flushes "host" TLB entries, i.e. guest-physical > mappings in Intel terminology, where flush_tlb_guest() and (IIUC) Hyper-V's paravirt > TLB flush both flesh "guest" TLB entries, i.e. linear and combined mappings. > > Amusing side topic, apparently I like arm's stage-2 terminology better than "TDP", > because I actually typed out "stage-2" first. > > > + } > > > > - if (kvm_check_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu)) > > + if (kvm_check_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu)) { > > + kvm_vcpu_flush_tlb_guest(vcpu); > > + kvm_clear_request(KVM_REQ_HV_TLB_FLUSH, vcpu); Looking at future patches where KVM needs to reset the FIFO when doing a "guest" TLB flush, i.e. needs to do more than just clearing the request, what about putting this in kvm_vcpu_flush_tlb_guest() right away? Ah, and there's already a second caller to kvm_vcpu_flush_tlb_guest(). I doubt KVM's paravirt TLB flush will ever collide with Hyper-V's paravirt TLB flush, but logically a "guest" flush that is initiated through KVM's paravirt interface should also clear Hyper-V's queue/request. And for consistency, slot this in before this patch: From: Sean Christopherson Date: Wed, 21 Sep 2022 09:35:34 -0700 Subject: [PATCH] KVM: x86: Move clearing of TLB_FLUSH_CURRENT to kvm_vcpu_flush_tlb_all() Clear KVM_REQ_TLB_FLUSH_CURRENT in kvm_vcpu_flush_tlb_all() instead of in its sole caller that processes KVM_REQ_TLB_FLUSH. Regardless of why/when kvm_vcpu_flush_tlb_all() is called, flushing "all" TLB entries also flushes "current" TLB entries. Ideally, there will never be another caller of kvm_vcpu_flush_tlb_all(), and moving the handling "requires" extra work to document the ordering requirement, but future Hyper-V paravirt TLB flushing support will add similar logic for flush "guest" (Hyper-V can flush a subset of "guest" entries). And in the Hyper-V case, KVM needs to do more than just clear the request, the queue of GPAs to flush also needs to purged, and doing all only in the request path is undesirable as kvm_vcpu_flush_tlb_guest() does have multiple callers (though it's unlikely KVM's paravirt TLB flush will coincide with Hyper-V's paravirt TLB flush). Move the logic even though it adds extra "work" so that KVM will be consistent with how flush requests are processed when the Hyper-V support lands. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f62d5799fcd7..3ea2e51a8cb5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3383,6 +3383,9 @@ static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu) { ++vcpu->stat.tlb_flush; static_call(kvm_x86_flush_tlb_all)(vcpu); + + /* Flushing all ASIDs flushes the current ASID... */ + kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); } static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu) @@ -10462,12 +10465,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) kvm_mmu_sync_roots(vcpu); if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu)) kvm_mmu_load_pgd(vcpu); - if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { + + /* + * Note, the order matters here, as flushing "all" TLB entries + * also flushes the "current" TLB entries, i.e. servicing the + * flush "all" will clear any request to flush "current". + */ + if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) kvm_vcpu_flush_tlb_all(vcpu); - - /* Flushing all ASIDs flushes the current ASID... */ - kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); - } kvm_service_local_tlb_flush_requests(vcpu); if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) { base-commit: ed102fe0b59586397b362a849bd7fb32582b77d8 --