Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp852964pxx; Tue, 27 Oct 2020 01:57:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxAakuskUltKym/Q7X5qTjtFlHcoCPwK6HmUv3LOnzIizAATZ0wvykHjUVNXjT5MLKXYXvA X-Received: by 2002:a17:906:7157:: with SMTP id z23mr1396175ejj.474.1603789079066; Tue, 27 Oct 2020 01:57:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603789079; cv=none; d=google.com; s=arc-20160816; b=zV7hCnZ7y2ls2I0ioD6DZSnvQHiq5sFAone+o1DkRd3EzwS6y0ZmViJpx52oUDXb5q fEYmutCeOY8PpMtNwFiPGBeNCkPSOZgN4Mi/yomIOoR7OnT6DPoKXXgU++UrEkbr5SIn WZ0yRY2lsUz8bQvWCDncwKq3IuY+Bpk0CEH4aC/KtpepSpiZxlgZ9BL5+PvJ0fdx8c3c QcQs3SCPwXVZtR6FQhSq9wW46P3av54sm40JA5vRLrUKQn1cQX57x+ooc/xojkCx3knF 5E4FxLX3EaKol6so8fQAuhdbkw41izRDGH5UqA++2LHsgfMjGwavdGsGizFV4Ic6IkSa nTpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=ogVNAEVFoyxovKo24VbUlDAoWOl0AGbLq2+4QHOTJls=; b=wq5/sX//WJ1nJGOTLIo2zZftM+95/o923wY67AB4rynDDlLHVnciwreM/Vbg3kTPFx r6rZ/Rp9FseVGergAuQQPjLUe216027wd3ecX6tLgm1htyM+yBhMWytLZzhjA7OmLQKs MigcA1bSA/ZOq/aEaNNMDb7wQ3XE4mNg46RgxWI4+dtz49JiDHONLLTYjEJxe2OkDjZd 5o8Yg4qh9Qsd9IaTym1SdoLXlxRwZrZeJ9JC1Oi6FXqXa2tMA/fR5LQAMWl/fJDJCnxA 9d6Q2LWl5y2sug6LaRESDg1Gu1ZvSBpoMe4QeJPD1jNf/b5NocYAVxKv9kB3fGy94mgr 4Hhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Qh1WEL/4"; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w11si552746ejz.380.2020.10.27.01.57.36; Tue, 27 Oct 2020 01:57:59 -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=@kernel.org header.s=default header.b="Qh1WEL/4"; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406319AbgJZXas (ORCPT + 99 others); Mon, 26 Oct 2020 19:30:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:32784 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406250AbgJZXaq (ORCPT ); Mon, 26 Oct 2020 19:30:46 -0400 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 675A0206DB for ; Mon, 26 Oct 2020 23:30:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603755045; bh=HGEo1Vp0eHrX0uYPz8ISeIcKVqaXNHvUFyeBXs1dDow=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Qh1WEL/4Ru5brZ5hnrEKPb/kGNDpiysrF2mR6JhLBvd3CHaSHxpm061VjFTAA0Im6 eClFuQKvh4Gy+Rtg+jJWKelSksww7hhTNEEDT5i3h4+KGfwRWz9aFG7zKES2OrpkVS xpcy+tN1KQFGqax8OW0KTGIb4anKsOwjihAe/rGI= Received: by mail-wr1-f41.google.com with SMTP id n15so14977945wrq.2 for ; Mon, 26 Oct 2020 16:30:45 -0700 (PDT) X-Gm-Message-State: AOAM533bVcqiD5BjyfRDzGB/jsgdUTK4snyqDgI8qXWjwxU7Hs3OS/qM 84befGtIcy+t4hQ6v1oEE2s0o6hADzWqQLINfbBvOA== X-Received: by 2002:adf:df03:: with SMTP id y3mr20031829wrl.70.1603755043953; Mon, 26 Oct 2020 16:30:43 -0700 (PDT) MIME-Version: 1.0 References: <20201026155521.GQ2594@hirez.programming.kicks-ass.net> <20201026160513.GC2651@hirez.programming.kicks-ass.net> <20201026163100.GR2594@hirez.programming.kicks-ass.net> <20201026165519.GD2651@hirez.programming.kicks-ass.net> In-Reply-To: <20201026165519.GD2651@hirez.programming.kicks-ass.net> From: Andy Lutomirski Date: Mon, 26 Oct 2020 16:30:32 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [REGRESSION] x86/debug: After PTRACE_SINGLESTEP DR_STEP is no longer reported in dr6 To: Peter Zijlstra Cc: Kyle Huey , open list , Thomas Gleixner , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , Linus Torvalds , "Robert O'Callahan" , Alexandre Chartre , "Paul E. McKenney" , Frederic Weisbecker , Paolo Bonzini , Sean Christopherson , Masami Hiramatsu , Petr Mladek , Joel Fernandes , Steven Rostedt , Boris Ostrovsky , Juergen Gross , Brian Gerst , Andy Lutomirski , Josh Poimboeuf , Daniel Thompson Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 26, 2020 at 9:55 AM Peter Zijlstra wrote: > > On Mon, Oct 26, 2020 at 05:31:00PM +0100, Peter Zijlstra wrote: > > In that respect, I think the current virtual_dr6 = 0 is placed wrong, it > > should only be in exc_debug_user(). The only 'problem' then is that we > > seem to be able to loose BTF, but perhaps that is already an extant bug. > > > > Consider: > > > > - perf: setup in-kernel #DB > > - tracer: ptrace(PTRACE_SINGLEBLOCK) > > - tracee: #DB on perf breakpoint, looses BTF > > - tracee .. never triggers actual blockstep > > > > Hmm ? Should we re-set BTF when TIF_BLOCKSTEP && !user_mode(regs) ? > > Something like so then. > > Or sould we also have the userspace #DB re-set BTF when it was !DR_STEP? > I need to go untangle that ptrace stuff :/ > > diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c > index 3c70fb34028b..31de8b0980ca 100644 > --- a/arch/x86/kernel/traps.c > +++ b/arch/x86/kernel/traps.c > @@ -793,19 +793,6 @@ static __always_inline unsigned long debug_read_clear_dr6(void) > set_debugreg(DR6_RESERVED, 6); > dr6 ^= DR6_RESERVED; /* Flip to positive polarity */ > > - /* > - * Clear the virtual DR6 value, ptrace routines will set bits here for > - * things we want signals for. > - */ > - current->thread.virtual_dr6 = 0; > - > - /* > - * The SDM says "The processor clears the BTF flag when it > - * generates a debug exception." Clear TIF_BLOCKSTEP to keep > - * TIF_BLOCKSTEP in sync with the hardware BTF flag. > - */ > - clear_thread_flag(TIF_BLOCKSTEP); > - > return dr6; > } > > @@ -873,6 +860,20 @@ static __always_inline void exc_debug_kernel(struct pt_regs *regs, > */ > WARN_ON_ONCE(user_mode(regs)); > > + if (test_thread_flag(TIF_BLOCKSTEP)) { > + /* > + * The SDM says "The processor clears the BTF flag when it > + * generates a debug exception." but PTRACE_BLOCKSTEP requested > + * it for userspace, but we just took a kernel #DB, so re-set > + * BTF. > + */ > + unsigned long debugctl; > + > + rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); > + debugctl |= DEBUGCTLMSR_BTF; > + wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); > + } > + > /* > * Catch SYSENTER with TF set and clear DR_STEP. If this hit a > * watchpoint at the same time then that will still be handled. > @@ -935,6 +936,26 @@ static __always_inline void exc_debug_user(struct pt_regs *regs, > irqentry_enter_from_user_mode(regs); > instrumentation_begin(); > > + /* > + * Clear the virtual DR6 value, ptrace routines will set bits here for > + * things we want signals for. > + */ > + current->thread.virtual_dr6 = 0; > + > + /* > + * If PTRACE requested SINGLE(BLOCK)STEP, make sure to reflect that in > + * the ptrace visible DR6 copy. > + */ > + if (test_thread_flag(TIF_BLOCKSTEP) || test_thread_flag(TIF_SINGLESTEP)) > + current->thread.virtual_dr6 |= (dr6 & DR_STEP); I'm guessing that this would fail a much simpler test, though: have a program use PUSHF to set TF and then read out DR6 from the SIGTRAP. I can whip up such a test if you like. Is there any compelling reason not to just drop the condition and do: current->thread.virtual_dr6 |= (dr6 & DR_STEP); unconditionally? This DR6 cause, along with ICEBP, have the regrettable distinctions of being the only causes that a user program can trigger all on its own without informing the kernel first. This means that we can't fully separate the concept of "user mode is single-stepping itself" from "ptrace or something else is causing the kernel to single step a program." I bet that, without making this tweak, the virtual_dr6 change will regress some horrific Wine use case. --Andy