From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 508123B5DE9; Fri, 17 Jul 2026 11:01:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784286084; cv=none; b=Cb+x+1Z2qXs4IZ/4tcL1yqXAGKke3Kotv2TsDoE4zwmoxWjIvky/7AQl1ocqWPhFRkRcsv8oyA9TNyvLERoOAiqDSdoMXJSVYL+wyqTJ+F684FZIAPu8Of5opsS4WIRG4KpQ6trn9xSFYJUAbw526DRHBSYCwmqUSZw89t8nt60= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784286084; c=relaxed/simple; bh=T5G+iHDXgUCImSHNoRtwWDcAlYylc5W0/XGtaZ9aAuE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VrKh/681VDWFh++mqY4xca4XHg6O2CWC9hMQEI75jlRmm5lH14fOxk2q8+vUMNyrnUdWgnz1jutjlbQJDroWk3LlFFsZiMbK4A3rbnGXn3gxDoiI0BVpQ2KKBxuA79Z06VC0OlscruN/cgpdnLsfHc1L16rgfoRPYTebprN578Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jKm+fA9B; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jKm+fA9B" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 790D41F00A3A; Fri, 17 Jul 2026 11:01:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784286069; bh=sy+3CQp5uMIEjT1xFCDDjV/XqRXnSP0C+Ski+YXq6Bg=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=jKm+fA9Btt2zwJNSK7aZF5mI4KNi2XmQjJiBVzsQvzda44O/cV0gCxm7lMHLkjqHa AAKsPeOeNs0bBOZmByzA3mvEkJnSHWE/Z7Fc2k+evvgGBTWkgApggPPlx3i5upSKDG fHb5FSMpwhaqGR2CQZorLurTTm4Jmod2DPRwm82YVX2DcxS3j/tB7kVL+OiRPGk8wo QZC0aPUBjqQiXvxNNiBeqzj24b4Y/CukxCa3twj8xYlTS2TpM4zkr6DSYsbzDGmykG oodfOswcQKjj6RAhTyQc6X51E0V1I5+r4OmuXlIVa85Vu2DMHLQRnxIuM/o9iIXgez G5py+HnHlPsuw== Date: Fri, 17 Jul 2026 12:01:03 +0100 From: Will Deacon To: Hongyan Xia Cc: Pu Hu , "mhiramat@kernel.org" , "ada.coupriediaz@arm.com" , "catalin.marinas@arm.com" , "davem@davemloft.net" , Jiazi Li , "linux-arm-kernel@lists.infradead.org" , "linux-kernel@vger.kernel.org" , "linux-trace-kernel@vger.kernel.org" , "naveen@kernel.org" , "yang@os.amperecomputing.com" Subject: Re: [RFC v3 2/2] arm64: kprobes: Allow reentering kprobes while single-stepping Message-ID: References: <20260710063242.228714-1-hupu@transsion.com> <20260710063242.228714-3-hupu@transsion.com> <0f049d38-b7f6-47f0-a410-95ad2aee7fd4@transsion.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Fri, Jul 17, 2026 at 01:51:12AM +0000, Hongyan Xia wrote: > On 7/16/2026 11:20 PM, Will Deacon wrote: > > On Thu, Jul 16, 2026 at 02:38:58PM +0000, Pu Hu wrote: > >> On 7/16/2026 9:24 PM, Will Deacon wrote: > >>> On Fri, Jul 10, 2026 at 06:32:55AM +0000, Pu Hu wrote: > >>>> From: Pu Hu > >>>> > >>>> A kprobe can be hit while another kprobe is in KPROBE_HIT_SS state. This > >>>> can happen when tracing or perf code runs from the debug exception path > >>>> while the first kprobe is preparing or executing its out-of-line > >>>> single-step instruction. > >>> > >>> I don't understand this part. The single-step runs with debug exceptions > >>> disabled (kprobes_save_local_irqflag() sets PSTATE.D) so how do we end > >>> up taking one? > >> > >> You are right that the single-step runs with debug exceptions disabled. > >> However, the case I was referring to is not a hardware breakpoint or a > >> software-step exception, but another Breakpoint Instruction exception > >> generated by executing a BRK instruction. A BRK instruction exception is > >> not masked by PSTATE.D, so it can still be taken while handling a kprobe. > >> > >> As far as I understand the architecture, there are two different cases here: > >> > >> - Breakpoint Instruction exceptions, generated by executing a BRK > >> instruction. > >> - Breakpoint exceptions, generated by the debug logic, for example by > >> programmed breakpoint registers. > >> > >> PSTATE.D masks debug exceptions such as hardware breakpoints, > >> watchpoints and software-step exceptions, but it does not mask > >> Breakpoint Instruction exceptions generated by BRK. This also seems > >> consistent with the pseudocode for BRK, > >> Arch64.SoftwareBreakpoint(imm16), which does not appear to check > >> PSTATE.D before taking the exception. > >> > >> Therefore, even if kprobes_save_local_irqflag() sets PSTATE.D while > >> handling the first kprobe, if the code executed from that path reaches > >> another instruction patched with BRK, it can still take a Breakpoint > >> Instruction exception. In other words, the nested case I mentioned is > >> another kprobe BRK being hit, not a hardware debug exception or a > >> software-step exception. > > > > Yes, that's correct, but if we're doing the out-of-line step, how do we > > end up executing a BRK? Or are you saying that it's the kprobes > > BRK64_OPCODE_KPROBES_SS instruction that we use to implement the > > single-step that is the problem? If so, how does taking that exception > > result in us executing tracing or perf code? > > > > Sorry for all the questions, I just haven't understood what's going on > > here from the commit message. > > The key is that, when you use 'perf --call-graph dwarf' to sample > certain events, kernel perf code will sample a piece of user stack each > time those events are hit, and copy_to/from_user() triggers page faults. > Say you are profiling preempt_enable events: > > 1st BRK -> preempt_disable() -> debug_exception() -> set SS state -> > preempt_enable() -> triggers perf -> perf_sample() -> sample user stack > using copy_to/from_user() -> page fault or 2nd BRK on the page fault path. > > The key is perf sampling the user stack while the 1st BRK is still > running. When a page fault is hit, a can of worms is released, including > a possible 2nd BRK. Thanks. So perf is run synchronously from the debug exception entry path, rather than because of a second exception taking place. Got it. But then it sounds like we should really make the debug exception handling path (at least, the part that runs for handling the kprobe step) noinstr to avoid getting into this state to begin with. Is that practical? Will