Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2537181pxb; Tue, 13 Apr 2021 04:30:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw4fzDSkPtJoiNOse9TR/Ef7Hd0785MvgFvDRvbxzQWZTqb487DPasjTsl5LV4DEzkWCkKm X-Received: by 2002:a62:5cc3:0:b029:203:54be:e4c9 with SMTP id q186-20020a625cc30000b029020354bee4c9mr29009190pfb.80.1618313436361; Tue, 13 Apr 2021 04:30:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618313436; cv=none; d=google.com; s=arc-20160816; b=rBGsKckosgEAv/J5j1vomCktcaG+DbiDAj98XrcM9lxFiuQOEnT0+EmPC3rjdym2bI ay2Uc+FMxFo9Ty5HmAFPmIOX6OuV5OgTM69RncIfjmFa4Zn3OJKz/UqWWTL0ko32EPUb fyidV+k9IT98dcEQjDyk22YzpSLi60eS7a3ULqikMxkXqDZCoo5W85MENdfJBc/IL3w+ MwKc6LY4hzSZuzurAYl/laS7y32nKHEEmOmGhdzIszuLDnOnHW0bH8Njx8ESn2rKDNIb aZ83pV38KPW5vqpCxPVTWVr43/0K50jrH2QYFgBeKD/odUBsVUpI7dJrom+MijV0KxZ8 th0g== 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=LTAUlRV+90mXaog4LE7DEsSgTnXw1ApT5g7eDZ+3hQQ=; b=X5vaLMhjUU1ozz4QaXfMv4FKSDLWSu6webG5c62npOFTw8daHu7DYn4yCfBndxiX15 faxeB94hIX/FAqWv0jfpfp2j6stIP9i3l+0TwLg3YiGq9nbpPwZi0gZ7d4+459YpkQo4 2X1D4FIYIwwRNpKn3afPSXD6rpxrQPDVkrBmbj+EQCcMIyRoAanrFghS5v92uC8KNWjK OwPs1t92qtA29LtKvkndHDkyZ/8qPbP4jVLg9kP7AixCQ1sofjsKA9hCF+EYuJIzPHeQ ep4eCtWLa5maxeL39TVG55G/UCsQsXPRPwE5DTEYxH20PFu9xDLHxCaHMX7a+M3BgzBc O7gA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EOQdsGxc; 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 136si20723901pfz.261.2021.04.13.04.30.23; Tue, 13 Apr 2021 04:30:36 -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=20161025 header.b=EOQdsGxc; 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 S241052AbhDLVoG (ORCPT + 99 others); Mon, 12 Apr 2021 17:44:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240646AbhDLVoD (ORCPT ); Mon, 12 Apr 2021 17:44:03 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67165C061574 for ; Mon, 12 Apr 2021 14:43:41 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id z16so10417695pga.1 for ; Mon, 12 Apr 2021 14:43:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=LTAUlRV+90mXaog4LE7DEsSgTnXw1ApT5g7eDZ+3hQQ=; b=EOQdsGxc+trZ9fBNqii1aYmL+oejnNTIpAmeJxEDRtdv4sfpgl7CUHgZDeu0bzilfo Ad1P6/PTP7qzpUVzhfxZkSse+TSPAK5HIfUeanowvW60r32lgP66yn+90YMmPzDHeS+Q gN0qv9EKIq2UrdKOuGWLEZInZ9IbXf3jXjeaq+okQNM70WNHO2oyNe40kGKx6q3fp3hj 3nEDYeAVKe2W5jj8S48nyesHC432eKHVfm72/FI3OZh8IblL7iMO1ZqcghmMvMrLxV+M EfxoVNNr9U1B4spOlvqUCL9Pjj0WFo3+f2J+r4kojtqyQNEJbPGP7ywIk1aRVsG5zHyT i2yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=LTAUlRV+90mXaog4LE7DEsSgTnXw1ApT5g7eDZ+3hQQ=; b=TGuNcx9iUfZKiYV3u7IWFabqxApUUG6xvF8jI+evNJaeQm8WUQAwIX97Jn9hU39ehs SDblLl5vOD6JQWyqqtlq0gB7pOg1rLLpvviVo7382IIRbOJ7NGKj6XsfkamHShUnt6rD LMsqgAHn7+ralYVA4GoySxWkT+2gwXPMB0BwXXzLPMyaMFEJPnkzAlcwE5BX93n8Ki7M GKmAA/y3onMBRWPwCBf1vwaDJiwQJxerBL6V/VX+KeT5TStS6SeQLChbP9sIYVH441RO //A4XB4Wu0UvXoStMmLxBzwE8O1SHjPjkUrOyJhuh/H2+j0orm6O/8jCZIsFn1UG1qHj OFYQ== X-Gm-Message-State: AOAM531Shc3bKjB96J0DCtQFh9I6M8sqzMgmewaNwEwLOaLVYFCuniuB DDRtYFaGxUav4Wbgeiyinq8rzw== X-Received: by 2002:a63:d50c:: with SMTP id c12mr11168928pgg.145.1618263820594; Mon, 12 Apr 2021 14:43:40 -0700 (PDT) Received: from google.com (240.111.247.35.bc.googleusercontent.com. [35.247.111.240]) by smtp.gmail.com with ESMTPSA id 77sm12720008pgf.55.2021.04.12.14.43.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Apr 2021 14:43:39 -0700 (PDT) Date: Mon, 12 Apr 2021 21:43:35 +0000 From: Sean Christopherson To: Lai Jiangshan Cc: Paolo Bonzini , LKML , kvm@vger.kernel.org, Filippo Sironi , David Woodhouse , "v4.7+" , Wanpeng Li Subject: Re: [PATCH 2/2] KVM: x86: Fix split-irqchip vs interrupt injection window request Message-ID: References: <20201127112114.3219360-1-pbonzini@redhat.com> <20201127112114.3219360-3-pbonzini@redhat.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 Fri, Apr 09, 2021, Lai Jiangshan wrote: > On Fri, Nov 27, 2020 at 7:26 PM Paolo Bonzini wrote: > > > > kvm_cpu_accept_dm_intr and kvm_vcpu_ready_for_interrupt_injection are > > a hodge-podge of conditions, hacked together to get something that > > more or less works. But what is actually needed is much simpler; > > in both cases the fundamental question is, do we have a place to stash > > an interrupt if userspace does KVM_INTERRUPT? > > > > In userspace irqchip mode, that is !vcpu->arch.interrupt.injected. > > Currently kvm_event_needs_reinjection(vcpu) covers it, but it is > > unnecessarily restrictive. > > > > In split irqchip mode it's a bit more complicated, we need to check > > kvm_apic_accept_pic_intr(vcpu) (the IRQ window exit is basically an INTACK > > cycle and thus requires ExtINTs not to be masked) as well as > > !pending_userspace_extint(vcpu). However, there is no need to > > check kvm_event_needs_reinjection(vcpu), since split irqchip keeps > > pending ExtINT state separate from event injection state, and checking > > kvm_cpu_has_interrupt(vcpu) is wrong too since ExtINT has higher > > priority than APIC interrupts. In fact the latter fixes a bug: > > when userspace requests an IRQ window vmexit, an interrupt in the > > local APIC can cause kvm_cpu_has_interrupt() to be true and thus > > kvm_vcpu_ready_for_interrupt_injection() to return false. When this > > happens, vcpu_run does not exit to userspace but the interrupt window > > vmexits keep occurring. The VM loops without any hope of making progress. > > > > Once we try to fix these with something like > > > > return kvm_arch_interrupt_allowed(vcpu) && > > - !kvm_cpu_has_interrupt(vcpu) && > > - !kvm_event_needs_reinjection(vcpu) && > > - kvm_cpu_accept_dm_intr(vcpu); > > + (!lapic_in_kernel(vcpu) > > + ? !vcpu->arch.interrupt.injected > > + : (kvm_apic_accept_pic_intr(vcpu) > > + && !pending_userspace_extint(v))); > > > > we realize two things. First, thanks to the previous patch the complex > > conditional can reuse !kvm_cpu_has_extint(vcpu). Second, the interrupt > > window request in vcpu_enter_guest() > > > > bool req_int_win = > > dm_request_for_irq_injection(vcpu) && > > kvm_cpu_accept_dm_intr(vcpu); > > > > should be kept in sync with kvm_vcpu_ready_for_interrupt_injection(): > > it is unnecessary to ask the processor for an interrupt window > > if we would not be able to return to userspace. Therefore, the > > complex conditional is really the correct implementation of > > kvm_cpu_accept_dm_intr(vcpu). It all makes sense: > > > > - we can accept an interrupt from userspace if there is a place > > to stash it (and, for irqchip split, ExtINTs are not masked). > > Interrupts from userspace _can_ be accepted even if right now > > EFLAGS.IF=0. > > Hello, Paolo > > If userspace does KVM_INTERRUPT, vcpu->arch.interrupt.injected is > set immediately, and in inject_pending_event(), we have > > else if (!vcpu->arch.exception.pending) { > if (vcpu->arch.nmi_injected) { > kvm_x86_ops.set_nmi(vcpu); > can_inject = false; > } else if (vcpu->arch.interrupt.injected) { > kvm_x86_ops.set_irq(vcpu); > can_inject = false; > } > } > > I'm curious about that can the kvm_x86_ops.set_irq() here be possible > to queue the irq with EFLAGS.IF=0? If not, which code prevents it? The interrupt is only directly injected if the local APIC is _not_ in-kernel. If userspace is managing the local APIC, my understanding is that userspace is also responsible for honoring EFLAGS.IF, though KVM aids userspace by updating vcpu->run->ready_for_interrupt_injection when exiting to userspace. When userspace is modeling the local APIC, that resolves to kvm_vcpu_ready_for_interrupt_injection(): return kvm_arch_interrupt_allowed(vcpu) && kvm_cpu_accept_dm_intr(vcpu); where kvm_arch_interrupt_allowed() checks EFLAGS.IF (and an edge case related to nested virtualization). KVM also captures EFLAGS.IF in vcpu->run->if_flag. For whatever reason, QEMU checks both vcpu->run flags before injecting an IRQ, maybe to handle a case where QEMU itself clears EFLAGS.IF? > I'm asking about this because I just noticed that interrupt can > be queued when exception pending, and this patch relaxed it even > more. > > Note: interrupt can NOT be queued when exception pending > until 664f8e26b00c7 ("KVM: X86: Fix loss of exception which > has not yet been injected") which I think is dangerous.