Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp714802ybv; Thu, 20 Feb 2020 06:08:07 -0800 (PST) X-Google-Smtp-Source: APXvYqyTQEclPX9sLJfLmRefCa2TRiZ+CbgC+hhmiaDLmDLGBnrIkzaZO4NpgQs7ZJWFXttoiXfm X-Received: by 2002:a05:6808:4c7:: with SMTP id a7mr2133945oie.83.1582207687311; Thu, 20 Feb 2020 06:08:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582207687; cv=none; d=google.com; s=arc-20160816; b=A3LtJzK251seW4jWdz8tmjrb61gpqbrRH9U7wU3RBFzgXlJZqmTfWLpTiAp8fcZZgF 3r0uu5KTlJMh4DkfniuCOKlqElhm4owkDrOK7hmhRsvSp60JqqDq5M/G3ZlR9Erfdktr /8c4wUwiS9XR822qFXFdK2GM1C8wW9nqleK937L0oz5gT3tvxWhP+fvobX+4ut0P7Xkf MKF7lufaAQjupBucGx8F7TF2qtCiS9MQ6loejCwDvZ8l7MWPBmIpSqe/2PLtQs1UofWT 1Cne9AYvbiKWrqUPkBRvEkafJTHXxFkZN7c5kfgz0yJ54fOYo8EGb1JrdpvAaoGavH6j 07mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=a0F1N3hYW8XPNj9N+S4lYxuaShUHuL9nYAipAxCIxV0=; b=qcMOTnw9hlUySNDGH/3oFtepBPnhtfb3+CiuAM5UZEKc920MtqOKHPPWJDjqp+3nRZ TjSNM8yHeBFcoNO1Dq/tFYycIXBT7wC4w8Zg4nfNTuOyF4HnfZz1itV+4FrNnVzbhyAE xJBxdh3NCAvRbcm8Nh8kV8eZa04Vn+QvCYQSQIlsDGYqriD+2/xR/keCKQxBXQ2/kM8L 9BhSfySdQQVDAt7XsTWsRUPOMuktNOenOfRNci1rlK2I4AzLE2PlgXC2Cb0yhXYj3/9W dmwuZZLOdMIG8r50PaXjnElAzoWQctZVdGqTnqrmZfqZorKmOplh8b5BtwXuU5z0uMUY Jshg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=lCDh9AP3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j15si2772159oie.15.2020.02.20.06.07.47; Thu, 20 Feb 2020 06:08:07 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=lCDh9AP3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728248AbgBTOG5 (ORCPT + 99 others); Thu, 20 Feb 2020 09:06:57 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:34095 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727298AbgBTOG4 (ORCPT ); Thu, 20 Feb 2020 09:06:56 -0500 Received: by mail-wr1-f65.google.com with SMTP id n10so4814917wrm.1 for ; Thu, 20 Feb 2020 06:06:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=a0F1N3hYW8XPNj9N+S4lYxuaShUHuL9nYAipAxCIxV0=; b=lCDh9AP38v1XCfFwz0C+1p9iDiQBHd6gcRFBu+7yWtc8nR306339+u4SIty39ttOSw KL+DnTDIXBQd3MjiakCsGw/EmAOj3Ds4Tve3GvZaJLXyDZZnoT80T+88TlH7JpQr5Gxq aS6+gjE6ltXo+BnK6ryzUr5JqsDYZLRB4QoMDTc3wtpmfPqbbjzcIgWJJ6LHHtXuc++3 ZgQ/ZnbAMKbm3u+aBQnj2vRV1RMkNaCjbUrlE4kR+F0AlAUEKNEOsBwOpo2c1OSSIQiP Z/r8HyXzOdFKz68yB9RN+BYVwVggk6OhbHnbpZQpjQMHisvjAKND22ZwR7P0Zs+oOHlZ 7omQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=a0F1N3hYW8XPNj9N+S4lYxuaShUHuL9nYAipAxCIxV0=; b=ty8uJGFifitD8V17EVOauktCY2NaLY08JGd1AxLaNZYfs8gwVKCQG55pb/aLHw0Y1b GJCAXYzxnKlgI2kUMFG/I3V0sG7Eqwa2DYnKtmLi0CVRGQ7+voUjFyTnYxKAiLrHi0YM O8S8W3GPayJQg8HDyfQfuABfUZ2lkpoKQ37oIdciHxLl7lOrYwvrxBAiIj03cEe9PaZf jdJPFAidgYRnuOR0XFTM7iXrhFfP8d18KfU5SgYeOwzYA4BU/7uZSA8I6N7TE2xnwoRN 8H/4DJGiymAg/fQzXA1ohzLVDHka8gWkk/W1no4Loo3MitGOif8fBH4rzSusKWGQjFNJ CODw== X-Gm-Message-State: APjAAAUKY25CoLbNz6Pq/wP/8Y5E4a1arV4fE+J6ofgXQUkjQfcwlSvI +HYhNrVLAdWnMHLTWcCz/kVjBQ== X-Received: by 2002:a5d:4e91:: with SMTP id e17mr40697146wru.233.1582207613733; Thu, 20 Feb 2020 06:06:53 -0800 (PST) Received: from holly.lan (89-145-231-170.xdsl.murphx.net. [89.145.231.170]) by smtp.gmail.com with ESMTPSA id w1sm4664917wmc.11.2020.02.20.06.06.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2020 06:06:53 -0800 (PST) Date: Thu, 20 Feb 2020 14:06:50 +0000 From: Daniel Thompson To: minyard@acm.org Cc: Will Deacon , Catalin Marinas , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Corey Minyard Subject: Re: [PATCH v2] arm64:kgdb: Fix kernel single-stepping Message-ID: <20200220140650.tryvv3ishkxduujk@holly.lan> References: <20200219152403.3495-1-minyard@acm.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200219152403.3495-1-minyard@acm.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 19, 2020 at 09:24:03AM -0600, minyard@acm.org wrote: > From: Corey Minyard > > I was working on a single-step bug on kgdb on an ARM64 system, and I saw > this scenario: > > * A single step is setup to return to el1 > * The ERET return to el1 > * An interrupt is pending and runs before the instruction > * As soon as PSTATE.D (the debug disable bit) is cleared, the single > step happens in that location, not where it should have. > > This appears to be due to PSTATE.SS not being cleared when the exception > happens. Per section D.2.12.5 of the ARMv8 reference manual, that > appears to be incorrect, it says "As part of exception entry, the PE > does all of the following: ... Sets PSTATE.SS to 0." > > However, I appear to not be the first person who has noticed this. In > the el0-only portion of the kernel_entry macro in entry.S, I found the > following comment: "Ensure MDSCR_EL1.SS is clear, since we can unmask > debug exceptions when scheduling." Exactly the same scenario, except > coming from a userland single step, not a kernel one. > > As I was studying this, though, I realized that the following scenario > had an issue: > > * Kernel enables MDSCR.SS, MDSCR.KDE, MDSCR.MDE (unnecessary), and > PSTATE.SS to enable a single step in el1, for kgdb or kprobes, > on the current CPU's MDSCR register and the process' PSTATE.SS > register. > * Kernel returns from the exception with ERET. > * An interrupt or page fault happens on the instruction, causing the > instruction to not be run, but the exception handler runs. > * The exception causes the task to migrate to a new core. > * The return from the exception runs on a different processor now, > where the MDSCR values are not set up for a single step. > * The single step fails to happen. > > This is bad for kgdb, of course, but it seems really bad for kprobes if > this happens. > > To fix both these problems, rework the handling of single steps to clear > things out upon entry to the kernel from el1, and then to set up single > step when returning to el1, and not do the setup in debug-monitors.c. > This means that single stepping does not use > enable/disable_debug_monitors(); it is no longer necessary to track > those flags for single stepping. This is much like single stepping is > handled for el0. A new flag is added in pt_regs to enable single > stepping from el1. Unfortunately, the old value of PSTATE.SS cannot be > used for this because of the hardware bug mentioned earlier. > > As part of this, there is an interaction between single stepping and the > other users of debug monitors with the MDSCR.KDE bit. That bit has to > be set for both hardware breakpoints at el1 and single stepping at el1. > A new variable was created to store the cpu-wide value of MDSCR.KDE; the > single stepping code makes sure not to clear that bit on kernel entry if > it's set in the per-cpu variable. > > After fixing this and doing some more testing, I ran into another issue: > > * Kernel enables the pt_regs single step > * Kernel returns from the exception with ERET. > * An interrupt or page fault happens on the instruction, causing the > instruction to not be run, but the exception handler runs. > * The exception handling hits a breakpoint and stops. > * The user continues from the breakpoint, so the kernel is no longer > expecting a single step. > * On the return from the first exception, the single step flag in > pt_regs is still set, so a single step trap happens. > * The kernel keels over from an unexpected single step. > > There's no easy way to find the pt_regs that has the single step flag > set. So a thread info flag was added so that the single step could be > disabled in this case. Both that flag and the flag in pt_regs must be > set to enable a single step. > > Signed-off-by: Corey Minyard I've pointed the kgdbtest suite at this patch (and run one of the historically unstable test cases an extra 100 times just in case). kgdbtest hasn't got great coverage, runs the code in qemu and some of the strongest tests are still marked XFAIL on arm64 (for reasons unrelated to stepping). So the best I can say based on the above is that the test suite does not observe any regression (but equally no improvement). Nevertheless FWIW: Tested-by: Daniel Thompson Daniel.