Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1794809pxb; Thu, 28 Oct 2021 10:09:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz/HtCxtCEeIuQZFF+bhF6PufoYaIQrJYqOGW8alKAG4PvmCJmAJr+vGPmLfEype7FkQ48a X-Received: by 2002:a17:90a:2bca:: with SMTP id n10mr5941993pje.241.1635440941750; Thu, 28 Oct 2021 10:09:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635440941; cv=none; d=google.com; s=arc-20160816; b=Elz5RxZu346P4Tf6GdooVMiez/a2X+AQxlyvfVAn+Bn4yN6WgZUStkIKaEyNNY91aA +mQvGziT8U0lq7G566TsOaX9fp9nDV1gkKzthTIP0pwqHJzzY5fiakCiGsXA6ass9dCF zeogEv8+6XAc+3oz1wt9mel5n0NjMrR2Ylv1N5FipstrXo/9Rrme/6F6ukWwd/R2mIbd ZBZG483/JvIOYiqG3qe0rtIvocSNkiGGUYEa3VfftZZiHTA4DkGdxaAp0h4m2OaumtcB nSDA5Huh4mxxflWVpyLD+MRvQVxSZY8LFNcoleucVC5GWP7ELplsRe6yEWfY9B+CBSNP J8rw== 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=5FMOUmei9Yd20y1XWjfcdbxiSsU6/CKiTZiNSpEJsLk=; b=Y2Z7BMMhBG/Ed6wNKmP9ClUiR4dPDQ/3AOY1FkFZkUBoPnLwS+clNm609uirikmRv0 KrP8qj7qLpFeH5rtgDH3UPgb63KhSNoTkO7WOwxyNXJzdHumHO1IZcj4H16n2PyHRULs pw2xMnjagioCbZqErWaZtiWqmlUcZk9cPqCpzqd7hL2VonS8gLU7Q7aV+5Hat8BdEaSD Cz2WvSDmh9VOE7247fXIT7O+uimNIj+0QnWYV7f7Hxy/AUxG4AYnffntN+8Oimf+EL6C luPaFTPuok2weGmWBqXYtbODLrUfwRHS7g/CeBLO+bMV1aeUF+pwv2YhJQI2ciVoDteC N0XQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=goNQkkMJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mu11si6715126pjb.16.2021.10.28.10.08.48; Thu, 28 Oct 2021 10:09:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=goNQkkMJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S230467AbhJ1RJT (ORCPT + 99 others); Thu, 28 Oct 2021 13:09:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230094AbhJ1RJQ (ORCPT ); Thu, 28 Oct 2021 13:09:16 -0400 Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6EF7C061570 for ; Thu, 28 Oct 2021 10:06:48 -0700 (PDT) Received: by mail-pg1-x536.google.com with SMTP id 83so7005778pgc.8 for ; Thu, 28 Oct 2021 10:06:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=5FMOUmei9Yd20y1XWjfcdbxiSsU6/CKiTZiNSpEJsLk=; b=goNQkkMJXpMPhPYFdVULDytrjQcoVu2lfwn3WuXA8ZOWfVU/JBHyZvLlEiPgcloYqE Jhz0+bgiXe/YWIVtHqD9uWlRDISXdiszKG8C5B2x8qq+87xgln6oUfJqk8Pl2O4vpYUv aElTLuihNiGwbXbMGeTzi+TT9xRVMesWVtGd6b3dr0Gyg3Ld7XrPFh8WgKV/HqWCqye6 IN651B4/j+icyywyqYjZAUlmNgSWr91NVE2zYdE4jrE7wi4kHo7ce24TfGAFy44uEg6R fWLCdqwL2dJ2VgUcEM+jrTRcLtG9T2m9NM5+w4j3OENqjzp/2Dbrk6u9v0EBK753yfNN PvuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=5FMOUmei9Yd20y1XWjfcdbxiSsU6/CKiTZiNSpEJsLk=; b=43w6ZGd6oJ4wN+HJw+5F0VVIaTLSRDwUTJgRUy4WsE4kNF466qdxco3xfE/FdTtZW7 Qoe8DvAsI8EupFlGMyW15XZ78cMBSZCFgp50g/3Pc3TzS5lj6brl+7dAmKLRL5RwzqHG q6KUe1bIqABOPGF2BuOIXPSZqvZh2LoBz4MBdCJqpiZRsu0WdzE9Ms4fScV13YJOFZXU iiKZQIqbWb+juWFctWxs/tN2g6pdxeght+xwPxQyikcK7izwd3JlCHm6x9pOuAycGd7G JIC1P28MTCXYm/8RA7Bznk1LXtMrMdKHNSNdHR3d70R3U4iFAokYnCgM6GJ/r5qgD3ku TCKQ== X-Gm-Message-State: AOAM531K13BGupRdeuFr8YDqtVbIUSvzRc+UgCE+x5crq7ia1Cb+yWQ2 AuDOjCUJvSw3JA4Qcl9W6m2b/w== X-Received: by 2002:a05:6a00:181a:b0:47c:1057:52e with SMTP id y26-20020a056a00181a00b0047c1057052emr5549485pfa.76.1635440808097; Thu, 28 Oct 2021 10:06:48 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id y19sm4044516pfn.23.2021.10.28.10.06.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Oct 2021 10:06:47 -0700 (PDT) Date: Thu, 28 Oct 2021 17:06:43 +0000 From: Sean Christopherson To: Maxim Levitsky Cc: Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini , James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , Oliver Upton , Jing Zhang Subject: Re: [PATCH v2 35/43] KVM: SVM: Signal AVIC doorbell iff vCPU is in guest mode Message-ID: References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-36-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 28, 2021, Maxim Levitsky wrote: > On Fri, 2021-10-08 at 19:12 -0700, Sean Christopherson wrote: > > Signal the AVIC doorbell iff the vCPU is running in the guest. If the vCPU > > is not IN_GUEST_MODE, it's guaranteed to pick up any pending IRQs on the > > next VMRUN, which unconditionally processes the vIRR. > > > > Add comments to document the logic. > > > > Signed-off-by: Sean Christopherson > > --- > > arch/x86/kvm/svm/avic.c | 14 ++++++++++++-- > > 1 file changed, 12 insertions(+), 2 deletions(-) > > > > diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c > > index 208c5c71e827..cbf02e7e20d0 100644 > > --- a/arch/x86/kvm/svm/avic.c > > +++ b/arch/x86/kvm/svm/avic.c > > @@ -674,7 +674,12 @@ int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) > > kvm_lapic_set_irr(vec, vcpu->arch.apic); > > smp_mb__after_atomic(); > > > > - if (avic_vcpu_is_running(vcpu)) { > > + /* > > + * Signal the doorbell to tell hardware to inject the IRQ if the vCPU > > + * is in the guest. If the vCPU is not in the guest, hardware will > > + * automatically process AVIC interrupts at VMRUN. > > + */ > > + if (vcpu->mode == IN_GUEST_MODE) { > > int cpu = READ_ONCE(vcpu->cpu); > > > > /* > > @@ -687,8 +692,13 @@ int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) > > if (cpu != get_cpu()) > > wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpu)); > > put_cpu(); > > - } else > > + } else { > > + /* > > + * Wake the vCPU if it was blocking. KVM will then detect the > > + * pending IRQ when checking if the vCPU has a wake event. > > + */ > > kvm_vcpu_wake_up(vcpu); > > + } > > > > return 0; > > } > > It makes sense indeed to avoid ringing the doorbell when the vCPU is not in > the guest mode. > > I do wonder if we want to call kvm_vcpu_wake_up always otherwise, as the vCPU > might be just outside of the guest mode and not scheduled out. I don't know > how expensive is kvm_vcpu_wake_up in this case. IIUC, you're asking if we should do something like: if (vcpu->mode == IN_GUEST_MODE) { } else if (!is_vcpu_loaded(vcpu)) { kvm_vcpu_wake_up(); } The answer is that kvm_vcpu_wake_up(), which is effectively rcuwait_wake_up(), is very cheap except for specific configurations that may or may not be valid for production[*]. Practically speaking, is_vcpu_loaded() doesn't exist and should never exist because it's inherently racy. The closest we have would be else if (vcpu != kvm_get_running_vcpu()) { kvm_vcpu_wake_up(); } but that's extremely unlikely to be a net win because getting the current vCPU requires atomics to disable/re-enable preemption, especially if rcuwait_wake_up() is modified to avoid the rcu lock/unlock. TL;DR: rcuwait_wake_up() is cheap, and if it's too expensive, a better optimization would be to make it less expensive. [*] https://lkml.kernel.org/r/20211020110638.797389-1-pbonzini@redhat.com > Before this patch, the avic_vcpu_is_running would only be false when the vCPU > is scheduled out (e.g when vcpu_put was done on it) > > Best regards, > Maxim Levitsky >