Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp4878300rwb; Wed, 17 Aug 2022 07:30:48 -0700 (PDT) X-Google-Smtp-Source: AA6agR44tdpfsODx3kjW9yq3L5AZpxloi1vRTNKGosKd08AzZYTWyinUmU0BG8KoBulhF9wwGv7Y X-Received: by 2002:a17:902:720c:b0:172:a1ff:8c21 with SMTP id ba12-20020a170902720c00b00172a1ff8c21mr1891981plb.159.1660746648005; Wed, 17 Aug 2022 07:30:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660746647; cv=none; d=google.com; s=arc-20160816; b=nUmgVYAVBprmbNcg8N2zvQbTIX7wEzmSS4J9zeKSq9m6wXe7Bu2+BiUrIypwuuGArJ VD4WpgTML0XuG5ZT/c67GZbqMusc0Uq+rqV3qQTrDTFLrqnq9NLdpYjxKHXXqG8ay+f5 ZXnn3qK4qfMyb5hyj7WCNyV1ISHhT+3eRddBtaMM675QGxBWhZQYAafKdX7YVtRsADQn r4nZIGQp7bDBGE+qwcwM9/zyJvBSXEEdSHSGOTRe3Xk7wq3lKvIX3zaGDTLC4zUY+0Xb lIzyFeLEOHO4mmsaqZgNzVUuaRiub0Gq/5vLJxc8AFsK0I3xAceHVoG20aOukUjAeh1z Kb+A== 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=BkBigNaDlXGP8QLup2qk8r/1tlI8dFIAgIklvPQD/3s=; b=RBbgHhm1Lvcx0ESi+cXW5fAayMMatUOqfiyh2M2uY3lM8wg8XJbL0Ug4Y+gkPzwXNB jlSKcVhZ7ZjYuVpFYb4crDcFPaxWUHkym/Kka+6dxIimZwCYiqY79LPsi0xws5fffGzY obgN+mXRiTmxhpF4gTOaYjAssx5KZutSlFWsNAWLUZz6M2LgdpYxbqB60vGfkW1MmWpn ilZ6xQlBqhXM9QMXW1Rxa3wSNKezSJOzFaE4v4m+K1E1GIZmYlF9+kyvPoZ5WqhbQL9k I1RtbAKs2+2r7QSpjZzd9H8APYNboJUbqUR5+BqUlzM2o3vzDYc227DS/u+/5KvgEwQT vtzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="W97e/qSz"; 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 h127-20020a636c85000000b004297f0279ccsi7242992pgc.226.2022.08.17.07.30.35; Wed, 17 Aug 2022 07:30:47 -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="W97e/qSz"; 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 S239815AbiHQOLi (ORCPT + 99 others); Wed, 17 Aug 2022 10:11:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239999AbiHQOLc (ORCPT ); Wed, 17 Aug 2022 10:11:32 -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 ESMTPS id B2B5C3DBC6 for ; Wed, 17 Aug 2022 07:11:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660745490; 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=BkBigNaDlXGP8QLup2qk8r/1tlI8dFIAgIklvPQD/3s=; b=W97e/qSzC0roqAD8jFkxxZRxMNp1U3Diy2JDJHeAYPbTsqvXbnnpg7nd46ZwY6YrYG6mKq N/r08TFiOJBFiJOvKCR5ZkgZHey8c6Yrks/xUbw6RMgfqhBpdc2ORlpYVGHCkhjDNczFpu 05M0PrGX3EvgOQmnoY/2BUlczC1TgoY= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-282-FvrVUi3KNxWAqW-h_mwOFg-1; Wed, 17 Aug 2022 10:11:29 -0400 X-MC-Unique: FvrVUi3KNxWAqW-h_mwOFg-1 Received: by mail-wr1-f71.google.com with SMTP id x6-20020adfbb46000000b00225260970a9so434688wrg.18 for ; Wed, 17 Aug 2022 07:11:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc; bh=BkBigNaDlXGP8QLup2qk8r/1tlI8dFIAgIklvPQD/3s=; b=HBY+YAjK7OwLMf5q3ki8PHNomNTU/N2V7UMQyorr1IDF9PQeqC6q+3qsY/qX1KfZL6 ePCjalN8qMX+5MIypp4ZuzAx7FX3xzV0LKQfTbWp8V8f4CK0oYRI74eBdOOyxF9Y3YON MrJ5j3kh/aodNcOmCESNK/0kYp8EXvV79kt0eYJV9CbGcWTZX4pfiozaqoXKBzae/Qrm /+NaUcl/iN5b82YgINITpfA6vu4Ed2LHmFO9lfv2eqZ6o0ngmjY7D9yWG34rbu+b0IUs nqflgdhl8vnr7dTu8KUBlO5PAlqHJ2KRjUdK6AGo/Mqj+uwIM4Fpogwu/WCKpGAYD02o 6WjA== X-Gm-Message-State: ACgBeo2AO05SCr4acT+W7nnfKRkacDbMx0DOLV2dehBuNYC0yiEftLEf HE0eQ+VhIXZ3kpQN01/8GZiJE2eiuIVmJ4veQ67Swxxp9nsogBT2kP2pn8R6voys1p64NqJ34YG EJBSlgbAOehM4EYF7s/vrcM/b X-Received: by 2002:a5d:648b:0:b0:222:cc32:c292 with SMTP id o11-20020a5d648b000000b00222cc32c292mr13905159wri.463.1660745488133; Wed, 17 Aug 2022 07:11:28 -0700 (PDT) X-Received: by 2002:a5d:648b:0:b0:222:cc32:c292 with SMTP id o11-20020a5d648b000000b00222cc32c292mr13905142wri.463.1660745487810; Wed, 17 Aug 2022 07:11:27 -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 x11-20020adff0cb000000b002217e3f41f1sm13104183wro.106.2022.08.17.07.11.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Aug 2022 07:11:27 -0700 (PDT) Message-ID: <226aca0a2cc95f57364f330793a2f13446fcf7a0.camel@redhat.com> Subject: Re: [PATCH v2 8/9] KVM: x86: lapic does not have to process INIT if it is blocked From: Maxim Levitsky To: Sean Christopherson , Paolo Bonzini Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, vkuznets@redhat.com Date: Wed, 17 Aug 2022 17:11:25 +0300 In-Reply-To: References: <20220811210605.402337-1-pbonzini@redhat.com> <20220811210605.402337-9-pbonzini@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.1 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,T_SCC_BODY_TEXT_LINE autolearn=ham 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, 2022-08-17 at 00:07 +0000, Sean Christopherson wrote: > On Thu, Aug 11, 2022, Paolo Bonzini wrote: > > Do not return true from kvm_apic_has_events, and consequently from > > kvm_vcpu_has_events, if the vCPU is not going to process an INIT. > > > > Signed-off-by: Paolo Bonzini > > --- > >  arch/x86/include/asm/kvm_host.h | 1 + > >  arch/x86/kvm/i8259.c            | 2 +- > >  arch/x86/kvm/lapic.h            | 2 +- > >  arch/x86/kvm/x86.c              | 5 +++++ > >  arch/x86/kvm/x86.h              | 5 ----- > >  5 files changed, 8 insertions(+), 7 deletions(-) > > > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > > index 293ff678fff5..1ce4ebc41118 100644 > > --- a/arch/x86/include/asm/kvm_host.h > > +++ b/arch/x86/include/asm/kvm_host.h > > @@ -2042,6 +2042,7 @@ void __user *__x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, > >                                      u32 size); > >  bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu); > >  bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu); > > +bool kvm_vcpu_latch_init(struct kvm_vcpu *vcpu); > >   > >  bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq, > >                              struct kvm_vcpu **dest_vcpu); > > diff --git a/arch/x86/kvm/i8259.c b/arch/x86/kvm/i8259.c > > index e1bb6218bb96..177555eea54e 100644 > > --- a/arch/x86/kvm/i8259.c > > +++ b/arch/x86/kvm/i8259.c > > @@ -29,9 +29,9 @@ > >  #include > >  #include > >  #include > > -#include "irq.h" > > +#include > >   > > -#include > > +#include "irq.h" > >  #include "trace.h" > >   > >  #define pr_pic_unimpl(fmt, ...)        \ > > diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h > > index 117a46df5cc1..12577ddccdfc 100644 > > --- a/arch/x86/kvm/lapic.h > > +++ b/arch/x86/kvm/lapic.h > > @@ -225,7 +225,7 @@ static inline bool kvm_vcpu_apicv_active(struct kvm_vcpu *vcpu) > >   > >  static inline bool kvm_apic_has_events(struct kvm_vcpu *vcpu) > >  { > > -       return lapic_in_kernel(vcpu) && vcpu->arch.apic->pending_events; > > +       return lapic_in_kernel(vcpu) && vcpu->arch.apic->pending_events && !kvm_vcpu_latch_init(vcpu); > > Blech, the kvm_apic_has_events() name is awful (as is pending_events), e.g. it > really should be kvm_apic_has_pending_sipi_or_init(). 110% agree. > > To avoid the odd kvm_vcpu_latch_init() declaration and the long line above, what > if we open code this in kvm_vcpu_has_events() like we do for NMI, SMI, etc...? > > And as follow-up, I would love to rename kvm_vcpu_latch_init() => kvm_vcpu_init_blocked(), > kvm_apic_has_events(), and pending_events. > > E.g. for this patch just do: > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 9f11b505cbee..559900736a71 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -12533,7 +12533,8 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) >         if (!list_empty_careful(&vcpu->async_pf.done)) >                 return true; > > -       if (kvm_apic_has_events(vcpu)) > +       /* comment explaning that SIPIs are dropped in this case. */ > +       if (kvm_apic_has_events(vcpu) && !kvm_vcpu_latch_init(vcpu)) >                 return true; I personally don't know if I prefer this or the original patch. > >         if (vcpu->arch.pv.pv_unhalted) > While reviwing this, I noticed that we have this code: static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); /* * TODO: Last condition latch INIT signals on vCPU when * vCPU is in guest-mode and vmcb12 defines intercept on INIT. * To properly emulate the INIT intercept, * svm_check_nested_events() should call nested_svm_vmexit() * if an INIT signal is pending. */ return !gif_set(svm) || (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT)); } Is this workaround still needed? svm_check_nested_events does check the apic's INIT/SIPI status. Currently the '.apic_init_signal_blocked' is called from kvm_vcpu_latch_init which itself is currently called from kvm_vcpu_latch_init which happens after we would vmexit if INIT is intercepted by nested hypervisor. Best regards, Maxim Levitsky