Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4701836pxv; Tue, 27 Jul 2021 14:11:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVC+srjWLNJcrh6xqiaUzETfCOj1B9MzEScfGIlldDhmhfiMUqkZLKvrBMTCBneYuglhsj X-Received: by 2002:a17:907:e8c:: with SMTP id ho12mr23950952ejc.552.1627420313231; Tue, 27 Jul 2021 14:11:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627420313; cv=none; d=google.com; s=arc-20160816; b=qsDCmHubhap+6u0849y/4IFn7GXP080d4KwpJUSahkIwQkNNTzzdoVjQXxNC2ZOlJv k2LmSrlPrRpEQx4gGDF+fmBKM2Td2B3cabJvyx0mb+1Iqg9KVMFAo0i6lITqrpAqwxW5 53i7mqVWl5gfUXshgy2Ix9+vwu5c6H39FVFHFFkATGTSJfFV8TQZnXUyQd6UN7oruEM6 s3E94/F0z3O/3c+6hx/9CA/PaS9u/YogPNrtv1WhSN0UlKWxLOru3IsMHu3qhVl6mDCJ atokDr3Ai3UyNkClseX3bHmQw/IS7In5PuVGrbnjseY8N9QR8aCMmECk344zRbogDv50 GDOg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=epqNANSa//JBOqbfDJlxqgrU5mQ/ZOYCzCBkRvtlhEQ=; b=GIWpPOVnvSnojmGY7XDmZlvLmMR+KfaD1pWSozrKxEhlI2+DT1+kbGzsw645JfQizh 1LX7Rd26UCruXsKvxmMRVszD+z9Z/WhwFTVQWhJLztQfXA8Rhbyp1g5nDAKj+GAqHk77 LU4a43yXuS/fIsCOeREXKY/JwjBGDismUSyXsESRv8OciLF70ETle5yTsNwalM9ccnRS 4U2v5N0UKPp8ZqDfhXibJq6WZxCEe5SOkh9GpjgeMS45SgDerG+eQEB3grJD6b/05LQ+ arZstvNT/UxBtI8TikSkBVrfqnH7YsT5LNHNGgWLAmG6ktxz4Sj+g24JmO8gWtnkaTgu QSRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fIFANsUT; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 8si2252481ejx.341.2021.07.27.14.11.30; Tue, 27 Jul 2021 14:11:53 -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=@redhat.com header.s=mimecast20190719 header.b=fIFANsUT; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232903AbhG0VKD (ORCPT + 99 others); Tue, 27 Jul 2021 17:10:03 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:25056 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235298AbhG0VJX (ORCPT ); Tue, 27 Jul 2021 17:09:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1627420162; 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; bh=epqNANSa//JBOqbfDJlxqgrU5mQ/ZOYCzCBkRvtlhEQ=; b=fIFANsUTEron8+Eu12pb+XtY1X1cQRtVXZxvnoxhhuTsHERCtEOHtPYnMlr2Rw5Wtzy25h AnkA+hZxPGTkVlHmQ0noppsiiYAp3Rc5Nzgs0rBB8Ye7RLalK9pt9hSE8ouppCHkl1bgbN rvjLu4AFmywN79j/J62ssK5Vdn7uE8w= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-468-IR3A2OY7PgWndQt2uHqkyQ-1; Tue, 27 Jul 2021 17:09:18 -0400 X-MC-Unique: IR3A2OY7PgWndQt2uHqkyQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9D52C8010F4; Tue, 27 Jul 2021 21:09:17 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 27AE460C9F; Tue, 27 Jul 2021 21:09:17 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, stable@vger.kernel.org, Stas Sergeev Subject: [PATCH v3] KVM: x86: accept userspace interrupt only if no event is injected Date: Tue, 27 Jul 2021 17:09:16 -0400 Message-Id: <20210727210916.1652841-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Once an exception has been injected, any side effects related to the exception (such as setting CR2 or DR6) have been taked place. Therefore, once KVM sets the VM-entry interruption information field or the AMD EVENTINJ field, the next VM-entry must deliver that exception. Pending interrupts are processed after injected exceptions, so in theory it would not be a problem to use KVM_INTERRUPT when an injected exception is present. However, DOSEMU is using run->ready_for_interrupt_injection to detect interrupt windows and then using KVM_SET_SREGS/KVM_SET_REGS to inject the interrupt manually. For this to work, the interrupt window must be delayed after the completion of the previous event injection. Cc: stable@vger.kernel.org Reported-by: Stas Sergeev Tested-by: Stas Sergeev Fixes: 71cc849b7093 ("KVM: x86: Fix split-irqchip vs interrupt injection window request") Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4116567f3d44..e5d5c5ed7dd4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4358,8 +4358,17 @@ static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu) { - return kvm_arch_interrupt_allowed(vcpu) && - kvm_cpu_accept_dm_intr(vcpu); + /* + * Do not cause an interrupt window exit if an exception + * is pending or an event needs reinjection; userspace + * might want to inject the interrupt manually using KVM_SET_REGS + * or KVM_SET_SREGS. For that to work, we must be at an + * instruction boundary and with no events half-injected. + */ + return (kvm_arch_interrupt_allowed(vcpu) && + kvm_cpu_accept_dm_intr(vcpu) && + !kvm_event_needs_reinjection(vcpu) && + !vcpu->arch.exception.pending); } static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, -- 2.27.0