Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3986018pxf; Tue, 16 Mar 2021 02:53:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxByQGEDREVGcsOlK5UBfgrl6vwVd823I9tsaZR3zSqrA7eg/8FxHaVlO6Rr2yQJcN5TF9 X-Received: by 2002:a17:907:20e4:: with SMTP id rh4mr29068657ejb.369.1615888384650; Tue, 16 Mar 2021 02:53:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615888384; cv=none; d=google.com; s=arc-20160816; b=GGayqWRpJ3qCwOmh+TgEt02KSrRAliW89fsrVpA7O36Vq9lsizWOIhtQ5yY7sFUJwv 0vRh1rLLgKIQFDtTcsP/TZb2YmjlIMTE82tWjiHCTGAcuIrpcwnPC4psEhRcnG38/zxf Z6J9WX1QSchLZacm3bN0Ahm1NuWbFiZuwBGvhJ0sWm6RwKRgxYEuxsXzn3ehb1nwloo9 HumKN3TyxQkRP3F4ytnYWsn1tL7mWDckTSdEd997kvW/c3hxdleLItIiGJ2ZwnV2fIsL bIEQRYcSuzX9KBxAhNFeV8cYDREhFUjH8xicEa2M5x3uFLwsslil5wpZ8Ho12Y0LMg3X A8cQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=QuCOFoSjShd1ZnZCR15HVWEeAop5EAQVghLMyw1VBYc=; b=GCnwrpcRXgBh3CC67fDuujCEirEdNViUpCe48myTrmg4PpFXTsOgeDZp29o+MVGjR2 1jV9Rxkk5r5BRAZg/VQeDxgHVNRoN6HKuq4z57XMj/sMCzVmJd1rMhUD4l0iCyvQJhmq VXFbtefb7oocKngMybRhDQB8nUVhCIazyujmRXk1gONQYaVn/5dgI+zyBRpxMBIQZ2D0 Pl9QXsJ2F8aKi093ITLXk4lcmM2vaY/mC1yVP/S6wPqDqzErbeZReiBqKKbfpSeKMihZ hkNwNVDBimvZ8Nr8JuklkT/1qn0pBcjdcrq+tgPRwSLjiW67A1EYhDhDD1axhXrls9BN 9iaQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=siemens.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cm2si13075531edb.549.2021.03.16.02.52.42; Tue, 16 Mar 2021 02:53:04 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=siemens.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231293AbhCPJRl (ORCPT + 99 others); Tue, 16 Mar 2021 05:17:41 -0400 Received: from gecko.sbs.de ([194.138.37.40]:35646 "EHLO gecko.sbs.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231599AbhCPJRQ (ORCPT ); Tue, 16 Mar 2021 05:17:16 -0400 Received: from mail2.sbs.de (mail2.sbs.de [192.129.41.66]) by gecko.sbs.de (8.15.2/8.15.2) with ESMTPS id 12G9GSop010735 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 16 Mar 2021 10:16:28 +0100 Received: from [167.87.27.98] ([167.87.27.98]) by mail2.sbs.de (8.15.2/8.15.2) with ESMTP id 12G9GQ9R023727; Tue, 16 Mar 2021 10:16:26 +0100 Subject: Re: [PATCH 2/3] KVM: x86: guest debug: don't inject interrupts while single stepping To: Sean Christopherson , Maxim Levitsky Cc: kvm@vger.kernel.org, Vitaly Kuznetsov , linux-kernel@vger.kernel.org, Thomas Gleixner , Wanpeng Li , Kieran Bingham , Jessica Yu , Andrew Morton , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , Joerg Roedel , Jim Mattson , Borislav Petkov , Stefano Garzarella , "H. Peter Anvin" , Paolo Bonzini , Ingo Molnar References: <20210315221020.661693-1-mlevitsk@redhat.com> <20210315221020.661693-3-mlevitsk@redhat.com> From: Jan Kiszka Message-ID: <1259724f-1bdb-6229-2772-3192f6d17a4a@siemens.com> Date: Tue, 16 Mar 2021 10:16:26 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 16.03.21 00:37, Sean Christopherson wrote: > On Tue, Mar 16, 2021, Maxim Levitsky wrote: >> 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; > > Is this something userspace can deal with? E.g. disable IRQs and/or set NMI > blocking at the start of single-stepping, unwind at the end? Deviating this far > from architectural behavior will end in tears at some point. > Does this happen to address this suspicious workaround in the kernel? /* * The kernel doesn't use TF single-step outside of: * * - Kprobes, consumed through kprobe_debug_handler() * - KGDB, consumed through notify_debug() * * So if we get here with DR_STEP set, something is wonky. * * A known way to trigger this is through QEMU's GDB stub, * which leaks #DB into the guest and causes IST recursion. */ if (WARN_ON_ONCE(dr6 & DR_STEP)) regs->flags &= ~X86_EFLAGS_TF; (arch/x86/kernel/traps.c, exc_debug_kernel) I wonder why this got merged while no one fixed QEMU/KVM, for years? Oh, yeah, question to myself as well, dancing around broken guest debugging for a long time while trying to fix other issues... Jan >> + >> /* >> * 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 >> -- Siemens AG, T RDA IOT Corporate Competence Center Embedded Linux