Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp336043rwb; Thu, 10 Nov 2022 01:18:23 -0800 (PST) X-Google-Smtp-Source: AMsMyM7xxdMadjpkuOI71i64oOu9g+JtVxzeEXkXzd2SNgSMxr1YA2kxmGDGTv1WrSK+uj+JqX4L X-Received: by 2002:a17:90a:fa46:b0:20d:5efa:84fc with SMTP id dt6-20020a17090afa4600b0020d5efa84fcmr80984545pjb.20.1668071903637; Thu, 10 Nov 2022 01:18:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668071903; cv=none; d=google.com; s=arc-20160816; b=F5cF0fHepgou4ublYCjMXX/IhQUOgHYgWGtbEO0TfMm9gTlmuIqmuZr6xXHTdX0HTW 3NrIYgD2ux8hlxBKkWoCQM93cnhNavi7JdQYsZxZi38rjf+T9ictI3Y9uzjcRT1WC5Ih h/khQ2h5zWwLskKWvOuo2wGsB3hPlABKioV7rUVMo/rL5kbE1JNF9keONetIOJabNSqM xHihm+vOkLo5v6l73q+fk5RaNUjRjc6t8L+wjFVw/Af7qpPeXRgXwP8l+dj7AffKy+E/ kf0PbehNfaof/vVwCHEYBfTyJvZx+cmPjTEsYKW69b5aqlcn378qGtU5WUZjPRRVKLbD ncLg== 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=euI2vGaCMOurHqYTryBqSzQToVvfrQztpsrGNy9bXsQ=; b=IrShhPn/sfPq7jyVVy45/gnf300K3zodRCTt8JU573R4hbGFVVqfM+6WbKPIfKj34Y GOPZKlkq2wMaugHGAlwgNvi9zC21f+zvYPMuQEFMqfZMTKSbgtUUq8QW+a2P4h3ESnOj vZIvRiRvqOxm44mEc2s+pyLr17zacSkw3UG/2JK/ogMNhjI/JUdXHp2rDLO8vrrfZjrG KjgtnlA7pIKqhR5COBnhGQsaqeXbWSuR8u6Hj4iUhSMuZUduTiPUW8cUOBuwBP1RqKyZ EfJ4ZAfIpv83sSphPiv70v92bl0+Isor/NlpPFAeM9qtV/tLc4wMidTrIhiHTrh+05TW t6aA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=TP1THSpJ; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y12-20020a655b4c000000b004588a5bd0bfsi21005771pgr.569.2022.11.10.01.18.10; Thu, 10 Nov 2022 01:18:23 -0800 (PST) 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=@infradead.org header.s=desiato.20200630 header.b=TP1THSpJ; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229686AbiKJJDp (ORCPT + 92 others); Thu, 10 Nov 2022 04:03:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229603AbiKJJDl (ORCPT ); Thu, 10 Nov 2022 04:03:41 -0500 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CAD033C; Thu, 10 Nov 2022 01:03:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=euI2vGaCMOurHqYTryBqSzQToVvfrQztpsrGNy9bXsQ=; b=TP1THSpJ+c3gRZNp5f+dqEfJoe wzxT9yY8Lb3IXEf6f3/b9Hr2C7XntmdicAQ7NQGD68PdDKXVGmbxIcs93VUmHmTD22kjI7o4pARUV cLGKyOvQ7Krk5HXHofjIm5rBTLkPhVwpLR+IniSy1b28WkKzPNPloH90eaUjHX1L9UetEv+Xs7JZc WyZvmgJ5SJSA/nWZoyMp8R5EO63PiUC01GKBKq6ZzO8xkDSGOiEULGYhqzdX/JFypgtER4Wnh/2DU vHN4c/m8Tlp+xrb0tvJcm0LJfv6OFolPpM9Dd/bMHGJyWV3uUXZdiKdFH17yHsbaBj3lrHpHfUlqO mKLxxmSQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1ot3T8-00AF8u-Qq; Thu, 10 Nov 2022 09:03:31 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 3169130013F; Thu, 10 Nov 2022 10:03:30 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 146BA2016A1E7; Thu, 10 Nov 2022 10:03:30 +0100 (CET) Date: Thu, 10 Nov 2022 10:03:30 +0100 From: Peter Zijlstra To: Xin Li Cc: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, kevin.tian@intel.com Subject: Re: [RESEND PATCH 5/6] KVM: x86/VMX: add kvm_vmx_reinject_nmi_irq() for NMI/IRQ reinjection Message-ID: References: <20221110061545.1531-1-xin3.li@intel.com> <20221110061545.1531-6-xin3.li@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221110061545.1531-6-xin3.li@intel.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE 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, Nov 09, 2022 at 10:15:44PM -0800, Xin Li wrote: > To eliminate dispatching NMI/IRQ through the IDT, add > kvm_vmx_reinject_nmi_irq(), which calls external_interrupt() > for IRQ reinjection. > > Lastly replace calling a NMI/IRQ handler in an IDT descriptor > with calling kvm_vmx_reinject_nmi_irq(). > > Signed-off-by: H. Peter Anvin (Intel) > Signed-off-by: Xin Li Idem. > +#if IS_ENABLED(CONFIG_KVM_INTEL) > +/* > + * KVM VMX reinjects NMI/IRQ on its current stack, it's a sync > + * call thus the values in the pt_regs structure are not used in > + * executing NMI/IRQ handlers, except cs.RPL and flags.IF, which > + * are both always 0 in the VMX NMI/IRQ reinjection context. Thus > + * we simply allocate a zeroed pt_regs structure on current stack > + * to call external_interrupt(). > + */ > +void kvm_vmx_reinject_nmi_irq(u32 vector) noinstr ? > +{ > + struct pt_regs irq_regs; > + > + memset(&irq_regs, 0, sizeof(irq_regs)); > + > + if (vector == NMI_VECTOR) > + return exc_nmi(&irq_regs); > + > + external_interrupt(&irq_regs, vector); > +} > +EXPORT_SYMBOL_GPL(kvm_vmx_reinject_nmi_irq); > +#endif > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > index 63247c57c72c..b457e4888468 100644 > --- a/arch/x86/kvm/vmx/vmx.c > +++ b/arch/x86/kvm/vmx/vmx.c > @@ -46,6 +46,7 @@ > #include > #include > #include > +#include > #include > #include > > @@ -6758,15 +6759,11 @@ static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) > memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); > } > > -void vmx_do_interrupt_nmi_irqoff(unsigned long entry); > - > -static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu, > - unsigned long entry) > +static void handle_interrupt_nmi_irqoff(struct kvm_vcpu *vcpu, u32 vector) > { > - bool is_nmi = entry == (unsigned long)asm_exc_nmi_noist; > - > - kvm_before_interrupt(vcpu, is_nmi ? KVM_HANDLING_NMI : KVM_HANDLING_IRQ); > - vmx_do_interrupt_nmi_irqoff(entry); > + kvm_before_interrupt(vcpu, vector == NMI_VECTOR ? > + KVM_HANDLING_NMI : KVM_HANDLING_IRQ); > + kvm_vmx_reinject_nmi_irq(vector); > kvm_after_interrupt(vcpu); > } > > @@ -6792,7 +6789,6 @@ static void handle_nm_fault_irqoff(struct kvm_vcpu *vcpu) > > static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx) > { > - const unsigned long nmi_entry = (unsigned long)asm_exc_nmi_noist; > u32 intr_info = vmx_get_intr_info(&vmx->vcpu); > > /* if exit due to PF check for async PF */ > @@ -6806,20 +6802,19 @@ static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx) > kvm_machine_check(); > /* We need to handle NMIs before interrupts are enabled */ > else if (is_nmi(intr_info)) > - handle_interrupt_nmi_irqoff(&vmx->vcpu, nmi_entry); > + handle_interrupt_nmi_irqoff(&vmx->vcpu, NMI_VECTOR); > } > > static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu) > { > u32 intr_info = vmx_get_intr_info(vcpu); > unsigned int vector = intr_info & INTR_INFO_VECTOR_MASK; > - gate_desc *desc = (gate_desc *)host_idt_base + vector; > > if (KVM_BUG(!is_external_intr(intr_info), vcpu->kvm, > "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info)) > return; > > - handle_interrupt_nmi_irqoff(vcpu, gate_offset(desc)); > + handle_interrupt_nmi_irqoff(vcpu, vector); > vcpu->arch.at_instruction_boundary = true; > } How does any of this work? You're calling into entry/noinstr code from a random context.