Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3658084pxf; Mon, 15 Mar 2021 15:14:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxMHwG5ohxA45nawTEi06P0GUKLbHZfIYDlX5OmZQkSAwomTkqo/6d9U/Efw2O8moK05ASE X-Received: by 2002:aa7:d2d5:: with SMTP id k21mr31884448edr.216.1615846497838; Mon, 15 Mar 2021 15:14:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615846497; cv=none; d=google.com; s=arc-20160816; b=mK9UwIka4atYH78Dxk6x51w8cIBF9MbOxE1Nkg7VUeSSO6BhQz0vJMrDrtwYSIo/0/ ozhCFjlh6iWtGAd2diKP89lMwngFhJRkus3s5egQxFqu8ntk6JTuV4oPQH1Zq/aEYB+2 hM4pszzyqRkE8hXj5667S/GUtH6YC1BJsonPVa4D+BO35QxGX4WfkEuMugYVvJ76RrWK TEZEdBuzRT+pm/NTbqTudBMUmoEHXhQNwmwXKhfy54Gl8LjJ5DdgVWh8U4BWJFfcbp3p ulvP8a8faIq8aJDuZ/LDpGC5DGll4voqoPY8EG1EyF3gjadMidvQYwvsG6/ttFpflQmS jVdw== 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=G9NrupMURdEuUMDF610xSyzzhCAN0PBHUS0Y8SXmWZA=; b=fmFOVFkHsGHqbySSlQeeNujp3yYAdl37KElFi8mvebTo4ZeTqQ0dygcq/rC1ZQDISa BVWmAiLFmjXsou6YyTzLW7+9ZPJBWwHneGwlMn54R1lzjHPTmFPENE6vRXon8USoSZah ZVwujev3LCNHFEjmdBPnd89fpfU4rX33WoWC0iYZx0t0/BYUJ5b1lPzxkxuPmpDoS8Le 3dlvZ/27nReTLolwdEX8bwUOXkoPMlHxvSfBEG75q2xWDrxgw935qHa/oY9WZObjxCAQ 4iLHinRi4o1NYNX70q4IR07OCKu+UeUxUNW2HLtyIpZQ0WdZqen6VTmwaeFc6OOvbXIH zl3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XZJbFivZ; 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 a1si13296990edx.487.2021.03.15.15.14.35; Mon, 15 Mar 2021 15:14:57 -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=XZJbFivZ; 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 S232070AbhCOWLP (ORCPT + 99 others); Mon, 15 Mar 2021 18:11:15 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:24277 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232515AbhCOWKp (ORCPT ); Mon, 15 Mar 2021 18:10:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615846244; 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=G9NrupMURdEuUMDF610xSyzzhCAN0PBHUS0Y8SXmWZA=; b=XZJbFivZjnhOUBtrzMNC20lWDuG9/Mu2NnP+EvDx9U+VhFN82/SrXokF5YFaKWtT4zBRT3 tA1tV2Lx6QYbQdPCibX7DYGuHGFsX4X18KXC3xjw3ZGC/ZIECswIKqKlqWmqBbO2K17ugW RxAbtdJ3WdH4vZ7N6oAfoZ2QvkPhHdc= 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-311-3Oks0f7rMSyKvgBBSD5aJg-1; Mon, 15 Mar 2021 18:10:40 -0400 X-MC-Unique: 3Oks0f7rMSyKvgBBSD5aJg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3AB18100C619; Mon, 15 Mar 2021 22:10:38 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.207.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0264A5F706; Mon, 15 Mar 2021 22:10:31 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: Vitaly Kuznetsov , linux-kernel@vger.kernel.org, Thomas Gleixner , Wanpeng Li , Kieran Bingham , Jessica Yu , Jan Kiszka , Andrew Morton , x86@kernel.org (maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)), Joerg Roedel , Sean Christopherson , Jim Mattson , Borislav Petkov , Stefano Garzarella , Maxim Levitsky , "H. Peter Anvin" , Paolo Bonzini , Ingo Molnar Subject: [PATCH 2/3] KVM: x86: guest debug: don't inject interrupts while single stepping Date: Tue, 16 Mar 2021 00:10:19 +0200 Message-Id: <20210315221020.661693-3-mlevitsk@redhat.com> In-Reply-To: <20210315221020.661693-1-mlevitsk@redhat.com> References: <20210315221020.661693-1-mlevitsk@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This change greatly helps with two issues: * Resuming from a breakpoint is much more reliable. When resuming execution from a breakpoint, with interrupts enabled, more often than not, KVM would inject an interrupt and make the CPU jump immediately to the interrupt handler and eventually return to the breakpoint, to trigger it again. From the user point of view it looks like the CPU never executed a single instruction and in some cases that can even prevent forward progress, for example, when the breakpoint is placed by an automated script (e.g lx-symbols), which does something in response to the breakpoint and then continues the guest automatically. If the script execution takes enough time for another interrupt to arrive, the guest will be stuck on the same breakpoint RIP forever. * Normal single stepping is much more predictable, since it won't land the debugger into an interrupt handler, so it is much more usable. (If entry to an interrupt handler is desired, the user can still place a breakpoint at it and resume the guest, which won't activate this workaround and let the gdb still stop at the interrupt handler) Since this change is only active when guest is debugged, it won't affect KVM running normal 'production' VMs. Signed-off-by: Maxim Levitsky Tested-by: Stefano Garzarella --- arch/x86/kvm/x86.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a9d95f90a0487..b75d990fcf12b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8458,6 +8458,12 @@ static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit can_inject = false; } + /* + * Don't inject interrupts while single stepping to make guest debug easier + */ + if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) + return; + /* * Finally, inject interrupt events. If an event cannot be injected * due to architectural conditions (e.g. IF=0) a window-open exit -- 2.26.2