Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1091839rwb; Wed, 26 Jul 2023 07:29:30 -0700 (PDT) X-Google-Smtp-Source: APBJJlHw+5E7sA71aQm0KKUYqZck0cF9FsLwSkHDjKJMdKbx5Z/PLI+rfH/zDPpJgVyDPyRqpD+c X-Received: by 2002:a05:6358:e499:b0:134:e8c6:a888 with SMTP id by25-20020a056358e49900b00134e8c6a888mr2638071rwb.8.1690381769598; Wed, 26 Jul 2023 07:29:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690381769; cv=none; d=google.com; s=arc-20160816; b=yOcBlV9ZmYimp4XpDniNrsx/wefzrsHFRAroAMuV4jMdMUjdIkh6qPDRLwzDNe46Ps Yqi13/JmF4vteEee9zqJN3x4CwFGXWgNOF7sxdZwr9R4z8XhoBCfTGbjlbJW32ptW+AA +2hPq8dYpkSfdZ0EdWeNnyhmD9cnoA7fdZDT1206V8fJjDqoRdTdCbO1fRKyGt2FpFTA 1S8POV1+wHdtkYn3tOn9FmNz2u9h4YP03Gdh0KjJoSLJCRNlVyRkEJ4dXAuusYmmQQ1X RoCiDKkW4dvVHCQKqp3z101aps7fvPZ7Hgmb0eFzVhpM3ffxv1sjrbpMZV6Nge2fsvCd L0pw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8NMiTq/NJ8G71mTK+kun4ROve/cQ69SRajLeV7++tZE=; fh=2NfF+VcPdV2h2hRMeHegn63ohbGEV/1oT544guq3jvE=; b=TYhl5F+KGrfWa8iLQXxjpa0KGzWBNVX5KqEr17JpwgChTci2E+bKNtrfvhP01K5I7S j1qvc2KhUvHEGn6NPeUdgUXAC1MJ6DUgMzu6p4JUxAmc3se+itWEYxVj8/YZMNJ1k36Z ii321SKvAyTMGSVS3qQqXgkQfheuO94q4JM4nvexL65a1hTcDjr3DXuBPoW0xoLpweD6 jpRzAKMw9nmD4Z6/R1nYvt0s4Jd2+gqWA9W1MeQXblVnDSYOTq2kJ6iiCUcmXT102aip PmPQo+t0FYvRESvrN5PRFzw/ZkkS43rmszpJ6B6kGlQMjj9flaaBGjHijNPtQ1QpGnH7 KkCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=h1rS0CNj; 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 x189-20020a6386c6000000b00563db11bacbsi1983821pgd.466.2023.07.26.07.29.16; Wed, 26 Jul 2023 07:29:29 -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=h1rS0CNj; 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 S234256AbjGZOBy (ORCPT + 99 others); Wed, 26 Jul 2023 10:01:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234371AbjGZOBY (ORCPT ); Wed, 26 Jul 2023 10:01:24 -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 ED88B211F for ; Wed, 26 Jul 2023 07:00:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690380035; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8NMiTq/NJ8G71mTK+kun4ROve/cQ69SRajLeV7++tZE=; b=h1rS0CNjzssNjomxrjL8+dnMpjaB7QOAiT8rOC80R6mJm/E+nq7P1lHZrE44Z0gMfvHN7r ys5p3a5btIjdvzKbEjYkzYn9ljRTjXOtMNJSCx8nI/FG95Dd3K/dy6xmXCqzmp6YBYhMZy 8eyT5+l3yNvpVeoja9Yef2B0v+1NnmA= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-696-NU3wojVyMBiKj4ZDe-OLeg-1; Wed, 26 Jul 2023 10:00:33 -0400 X-MC-Unique: NU3wojVyMBiKj4ZDe-OLeg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5E7343C11A09; Wed, 26 Jul 2023 14:00:11 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2CC5E40C2063; Wed, 26 Jul 2023 14:00:07 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Borislav Petkov , Paolo Bonzini , Sean Christopherson , Dave Hansen , Thomas Gleixner , "H. Peter Anvin" , Ingo Molnar , Maxim Levitsky Subject: [PATCH v2 3/3] KVM: x86: check the kvm_cpu_get_interrupt result before using it Date: Wed, 26 Jul 2023 16:59:45 +0300 Message-Id: <20230726135945.260841-4-mlevitsk@redhat.com> In-Reply-To: <20230726135945.260841-1-mlevitsk@redhat.com> References: <20230726135945.260841-1-mlevitsk@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,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 The code was blindly assuming that kvm_cpu_get_interrupt never returns -1 when there is a pending interrupt. While this should be true, a bug in KVM can still cause this. If -1 is returned, the code before this patch was converting it to 0xFF, and 0xFF interrupt was injected to the guest, which results in an issue which was hard to debug. Add WARN_ON_ONCE to catch this case and skip the injection if this happens again. Signed-off-by: Maxim Levitsky --- arch/x86/kvm/x86.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a6b9bea62fb8ac..00b87fcf6da4af 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10203,9 +10203,13 @@ static int kvm_check_and_inject_events(struct kvm_vcpu *vcpu, if (r < 0) goto out; if (r) { - kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false); - static_call(kvm_x86_inject_irq)(vcpu, false); - WARN_ON(static_call(kvm_x86_interrupt_allowed)(vcpu, true) < 0); + int irq = kvm_cpu_get_interrupt(vcpu); + + if (!WARN_ON_ONCE(irq == -1)) { + kvm_queue_interrupt(vcpu, irq, false); + static_call(kvm_x86_inject_irq)(vcpu, false); + WARN_ON(static_call(kvm_x86_interrupt_allowed)(vcpu, true) < 0); + } } if (kvm_cpu_has_injectable_intr(vcpu)) static_call(kvm_x86_enable_irq_window)(vcpu); -- 2.26.3