Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7114282imu; Mon, 3 Dec 2018 07:58:31 -0800 (PST) X-Google-Smtp-Source: AFSGD/VYbQGyAjTybDdLzHm2OkeCGT0GSvH3ZOzwPyc6i+vkqoJ1W1dxgesY0Z68zlRs3RjQxW/a X-Received: by 2002:a62:5444:: with SMTP id i65mr16960762pfb.193.1543852711229; Mon, 03 Dec 2018 07:58:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543852711; cv=none; d=google.com; s=arc-20160816; b=kGmCyqIps0KuaRPQMCRoZZ+ftHqjA/KLZ1PId6VeWf1pKUbNJ0+o4ZrA1XSyNwrOuq hubB4tHHV5cWwHn26ADb1OnhOn5O38W8D+gOFbXzfkIM4V2DvHgwi3pFnTGZijB9jTBJ A8BfAlGOurPZLNdu3YW60VrpcyXA/pvC1tyaEO1CjescJFJ1Wj/5NycaRxn1jjC16CiR waSns41r8OyCkysSUZMjgSteXv7Z0+I/iNtXHnKQ6Cmkqk1hRYD8ow6ULSGiHYEpSI+O 3ZkFPQVorf5PHQK9VXCBoQy74JMw+GEEVQYrcIDIRW2VHwUCfyYzB93ZZyY1p0uo+RmG lOwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=35SwHc/wTajPXEk/ZNTQ/zNrwbuN3l3GZZC2HvWJrxg=; b=uSqva/Ztkw5/21axNNP9tg3XNL8GYxLmO0VTAqOBntXV90Ceg6b11AYCwhD6dLuO9S kkcEkeXzovFK2ujsaLwv+Pq9o+4eWcONslJv+Q7zPFTJHRdgoFcPySoUVw4S8xKSq5/p Z/HrDH0pdlO+UIUQWr0B7/ZFT0oXD7vTfSgRpHgIfqk0J42pNbCB2fdAomci9wjqtHrQ ZvM6kTKN92mrPgH8T5prX9o3rVICduHxt7b/9myug71Movk2PAxOIbttEOzLCwnMOwzH 7yF9EfbpWvCbaI5GAPbDmE0+fYoHnxhzVjrZ9qotPUKxqRoOnMTpARhAfp9RCyoFRcU7 qIYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=S0mZZZ5+; 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 e9si13611677plt.330.2018.12.03.07.58.06; Mon, 03 Dec 2018 07:58:31 -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=S0mZZZ5+; 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 S1726689AbeLCP5e (ORCPT + 99 others); Mon, 3 Dec 2018 10:57:34 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:35561 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726612AbeLCP5e (ORCPT ); Mon, 3 Dec 2018 10:57:34 -0500 Received: by mail-wm1-f68.google.com with SMTP id c126so6209863wmh.0 for ; Mon, 03 Dec 2018 07:57:28 -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:user-agent; bh=35SwHc/wTajPXEk/ZNTQ/zNrwbuN3l3GZZC2HvWJrxg=; b=S0mZZZ5+MfOGuu/3FzwDHO5aeWNAR/HGOxn+Zt2OUufHFFuasgx41bjzYd2rSmMf98 6FzB38Xfj3tkJEI+Pw3oiDJ3oek1XEHXy5cmcON3oKUXwO5+smVo2lCKRfLNRbWpPVUA sUbJ+6OrayX2kKlWZxqqMvpkEv5/N+oF2gjqE= 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:user-agent; bh=35SwHc/wTajPXEk/ZNTQ/zNrwbuN3l3GZZC2HvWJrxg=; b=cNjhN/8AnLOtfNhQtvplhrlhpGAg5Kh57f/7GGJuC2ML1iTTRy8EhBmJLT2PpiUJr9 eWi608TSYHU7hohaZNGgeZ1aT9B6QQbVontyKsA3oLBVfvaq2aeUtwiHjbrlNafeUVlO rx91dGwURb8rOqKGohgPYiUNfgp0x/Sy2rOAHn7/51J1EGX+tLc/7AUMdme850NiLITa sua3Rw4ChdWJ3ogTdfTFXY6+SPQWA+iNjREn0yjXbP6d1r9lzBPtbLXymvvqI4GWEirm KZ01NUmLDb35wjOxuBj+LDED0lFyefQ9jqXxjq1G9A3ZrTmUB6oCb0NnPP5B6OWntyt3 p77g== X-Gm-Message-State: AA+aEWa7heUnIPkcomAlnUGLJka97ZPyv5HWgHgKum60IH69PshRCalH cexA7L+V4yiwmsdB4ekR9j8tKg== X-Received: by 2002:a1c:7706:: with SMTP id t6mr8320911wmi.57.1543852647539; Mon, 03 Dec 2018 07:57:27 -0800 (PST) Received: from holly.lan (cpc141214-aztw34-2-0-cust773.18-1.cable.virginm.net. [86.9.19.6]) by smtp.gmail.com with ESMTPSA id s202sm7391190wme.40.2018.12.03.07.57.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 03 Dec 2018 07:57:26 -0800 (PST) Date: Mon, 3 Dec 2018 15:57:24 +0000 From: Daniel Thompson To: Douglas Anderson Cc: Jason Wessel , Will Deacon , kgdb-bugreport@lists.sourceforge.net, Peter Zijlstra , linux-mips@linux-mips.org, linux-sh@vger.kernel.org, linux-kernel@vger.kernel.org, Catalin Marinas , James Hogan , linux-hexagon@vger.kernel.org, Vineet Gupta , Rich Felker , Ralf Baechle , linux-snps-arc@lists.infradead.org, Yoshinori Sato , Benjamin Herrenschmidt , Paul Mackerras , Russell King , linux-arm-kernel@lists.infradead.org, Christophe Leroy , Michael Ellerman , Paul Burton , Richard Kuo , linuxppc-dev@lists.ozlabs.org Subject: Re: [PATCH v6 2/4] kgdb: Fix kgdb_roundup_cpus() for arches who used smp_call_function() Message-ID: <20181203155724.7g37vp43e4fd4xqk@holly.lan> References: <20181127173839.34328-1-dianders@chromium.org> <20181127173839.34328-3-dianders@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181127173839.34328-3-dianders@chromium.org> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 27, 2018 at 09:38:37AM -0800, Douglas Anderson wrote: > When I had lockdep turned on and dropped into kgdb I got a nice splat > on my system. Specifically it hit: > DEBUG_LOCKS_WARN_ON(current->hardirq_context) > > Specifically it looked like this: > sysrq: SysRq : DEBUG > ------------[ cut here ]------------ > DEBUG_LOCKS_WARN_ON(current->hardirq_context) > WARNING: CPU: 0 PID: 0 at .../kernel/locking/lockdep.c:2875 lockdep_hardirqs_on+0xf0/0x160 > CPU: 0 PID: 0 Comm: swapper/0 Not tainted 4.19.0 #27 > pstate: 604003c9 (nZCv DAIF +PAN -UAO) > pc : lockdep_hardirqs_on+0xf0/0x160 > ... > Call trace: > lockdep_hardirqs_on+0xf0/0x160 > trace_hardirqs_on+0x188/0x1ac > kgdb_roundup_cpus+0x14/0x3c > kgdb_cpu_enter+0x53c/0x5cc > kgdb_handle_exception+0x180/0x1d4 > kgdb_compiled_brk_fn+0x30/0x3c > brk_handler+0x134/0x178 > do_debug_exception+0xfc/0x178 > el1_dbg+0x18/0x78 > kgdb_breakpoint+0x34/0x58 > sysrq_handle_dbg+0x54/0x5c > __handle_sysrq+0x114/0x21c > handle_sysrq+0x30/0x3c > qcom_geni_serial_isr+0x2dc/0x30c > ... > ... > irq event stamp: ...45 > hardirqs last enabled at (...44): [...] __do_softirq+0xd8/0x4e4 > hardirqs last disabled at (...45): [...] el1_irq+0x74/0x130 > softirqs last enabled at (...42): [...] _local_bh_enable+0x2c/0x34 > softirqs last disabled at (...43): [...] irq_exit+0xa8/0x100 > ---[ end trace adf21f830c46e638 ]--- > > Looking closely at it, it seems like a really bad idea to be calling > local_irq_enable() in kgdb_roundup_cpus(). If nothing else that seems > like it could violate spinlock semantics and cause a deadlock. > > Instead, let's use a private csd alongside > smp_call_function_single_async() to round up the other CPUs. Using > smp_call_function_single_async() doesn't require interrupts to be > enabled so we can remove the offending bit of code. > > In order to avoid duplicating this across all the architectures that > use the default kgdb_roundup_cpus(), we'll add a "weak" implementation > to debug_core.c. > > Looking at all the people who previously had copies of this code, > there were a few variants. I've attempted to keep the variants > working like they used to. Specifically: > * For arch/arc we passed NULL to kgdb_nmicallback() instead of > get_irq_regs(). > * For arch/mips there was a bit of extra code around > kgdb_nmicallback() > > NOTE: In this patch we will still get into trouble if we try to round > up a CPU that failed to round up before. We'll try to round it up > again and potentially hang when we try to grab the csd lock. That's > not new behavior but we'll still try to do better in a future patch. > > Suggested-by: Daniel Thompson > Signed-off-by: Douglas Anderson > --- > > Changes in v6: > - Moved smp_call_function_single_async() error check to patch 3. > > Changes in v5: > - Add a comment about get_irq_regs(). > - get_cpu() => raw_smp_processor_id() in kgdb_roundup_cpus(). > - for_each_cpu() => for_each_online_cpu() > - Error check smp_call_function_single_async() > > Changes in v4: None > Changes in v3: > - No separate init call. > - Don't round up the CPU that is doing the rounding up. > - Add "#ifdef CONFIG_SMP" to match the rest of the file. > - Updated desc saying we don't solve the "failed to roundup" case. > - Document the ignored parameter. > > Changes in v2: > - Removing irq flags separated from fixing lockdep splat. > - Don't use smp_call_function (Daniel). > > arch/arc/kernel/kgdb.c | 10 ++-------- > arch/arm/kernel/kgdb.c | 12 ----------- > arch/arm64/kernel/kgdb.c | 12 ----------- > arch/hexagon/kernel/kgdb.c | 27 ------------------------- > arch/mips/kernel/kgdb.c | 9 +-------- > arch/powerpc/kernel/kgdb.c | 4 ++-- > arch/sh/kernel/kgdb.c | 12 ----------- Please could you re-send with the arch maintainers for these platforms included in the Cc: of the patch description rather than just in the e-mail header. I'd like to make sure arch maintainers have a chance to opt out if they want to (it's a weak symbol so an arch could chose not to use it). Otherwise I shall start testing shortly! Daniel. > include/linux/kgdb.h | 15 ++++++++++++-- > kernel/debug/debug_core.c | 41 ++++++++++++++++++++++++++++++++++++++ > 9 files changed, 59 insertions(+), 83 deletions(-) > > diff --git a/arch/arc/kernel/kgdb.c b/arch/arc/kernel/kgdb.c > index 0932851028e0..68d9fe4b5aa7 100644 > --- a/arch/arc/kernel/kgdb.c > +++ b/arch/arc/kernel/kgdb.c > @@ -192,18 +192,12 @@ void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip) > instruction_pointer(regs) = ip; > } > > -static void kgdb_call_nmi_hook(void *ignored) > +void kgdb_call_nmi_hook(void *ignored) > { > + /* Default implementation passes get_irq_regs() but we don't */ > kgdb_nmicallback(raw_smp_processor_id(), NULL); > } > > -void kgdb_roundup_cpus(void) > -{ > - local_irq_enable(); > - smp_call_function(kgdb_call_nmi_hook, NULL, 0); > - local_irq_disable(); > -} > - > struct kgdb_arch arch_kgdb_ops = { > /* breakpoint instruction: TRAP_S 0x3 */ > #ifdef CONFIG_CPU_BIG_ENDIAN > diff --git a/arch/arm/kernel/kgdb.c b/arch/arm/kernel/kgdb.c > index f21077b077be..d9a69e941463 100644 > --- a/arch/arm/kernel/kgdb.c > +++ b/arch/arm/kernel/kgdb.c > @@ -170,18 +170,6 @@ static struct undef_hook kgdb_compiled_brkpt_hook = { > .fn = kgdb_compiled_brk_fn > }; > > -static void kgdb_call_nmi_hook(void *ignored) > -{ > - kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs()); > -} > - > -void kgdb_roundup_cpus(void) > -{ > - local_irq_enable(); > - smp_call_function(kgdb_call_nmi_hook, NULL, 0); > - local_irq_disable(); > -} > - > static int __kgdb_notify(struct die_args *args, unsigned long cmd) > { > struct pt_regs *regs = args->regs; > diff --git a/arch/arm64/kernel/kgdb.c b/arch/arm64/kernel/kgdb.c > index 12c339ff6e75..da880247c734 100644 > --- a/arch/arm64/kernel/kgdb.c > +++ b/arch/arm64/kernel/kgdb.c > @@ -284,18 +284,6 @@ static struct step_hook kgdb_step_hook = { > .fn = kgdb_step_brk_fn > }; > > -static void kgdb_call_nmi_hook(void *ignored) > -{ > - kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs()); > -} > - > -void kgdb_roundup_cpus(void) > -{ > - local_irq_enable(); > - smp_call_function(kgdb_call_nmi_hook, NULL, 0); > - local_irq_disable(); > -} > - > static int __kgdb_notify(struct die_args *args, unsigned long cmd) > { > struct pt_regs *regs = args->regs; > diff --git a/arch/hexagon/kernel/kgdb.c b/arch/hexagon/kernel/kgdb.c > index 012e0e230ac2..b95d12038a4e 100644 > --- a/arch/hexagon/kernel/kgdb.c > +++ b/arch/hexagon/kernel/kgdb.c > @@ -115,33 +115,6 @@ void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc) > instruction_pointer(regs) = pc; > } > > -#ifdef CONFIG_SMP > - > -/** > - * kgdb_roundup_cpus - Get other CPUs into a holding pattern > - * > - * On SMP systems, we need to get the attention of the other CPUs > - * and get them be in a known state. This should do what is needed > - * to get the other CPUs to call kgdb_wait(). Note that on some arches, > - * the NMI approach is not used for rounding up all the CPUs. For example, > - * in case of MIPS, smp_call_function() is used to roundup CPUs. > - * > - * On non-SMP systems, this is not called. > - */ > - > -static void hexagon_kgdb_nmi_hook(void *ignored) > -{ > - kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs()); > -} > - > -void kgdb_roundup_cpus(void) > -{ > - local_irq_enable(); > - smp_call_function(hexagon_kgdb_nmi_hook, NULL, 0); > - local_irq_disable(); > -} > -#endif > - > > /* Not yet working */ > void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, > diff --git a/arch/mips/kernel/kgdb.c b/arch/mips/kernel/kgdb.c > index 2b05effc17b4..42f057a6c215 100644 > --- a/arch/mips/kernel/kgdb.c > +++ b/arch/mips/kernel/kgdb.c > @@ -207,7 +207,7 @@ void arch_kgdb_breakpoint(void) > ".set\treorder"); > } > > -static void kgdb_call_nmi_hook(void *ignored) > +void kgdb_call_nmi_hook(void *ignored) > { > mm_segment_t old_fs; > > @@ -219,13 +219,6 @@ static void kgdb_call_nmi_hook(void *ignored) > set_fs(old_fs); > } > > -void kgdb_roundup_cpus(void) > -{ > - local_irq_enable(); > - smp_call_function(kgdb_call_nmi_hook, NULL, 0); > - local_irq_disable(); > -} > - > static int compute_signal(int tt) > { > struct hard_trap_info *ht; > diff --git a/arch/powerpc/kernel/kgdb.c b/arch/powerpc/kernel/kgdb.c > index b0e804844be0..b4ce54d73337 100644 > --- a/arch/powerpc/kernel/kgdb.c > +++ b/arch/powerpc/kernel/kgdb.c > @@ -117,7 +117,7 @@ int kgdb_skipexception(int exception, struct pt_regs *regs) > return kgdb_isremovedbreak(regs->nip); > } > > -static int kgdb_call_nmi_hook(struct pt_regs *regs) > +static int kgdb_debugger_ipi(struct pt_regs *regs) > { > kgdb_nmicallback(raw_smp_processor_id(), regs); > return 0; > @@ -502,7 +502,7 @@ int kgdb_arch_init(void) > old__debugger_break_match = __debugger_break_match; > old__debugger_fault_handler = __debugger_fault_handler; > > - __debugger_ipi = kgdb_call_nmi_hook; > + __debugger_ipi = kgdb_debugger_ipi; > __debugger = kgdb_debugger; > __debugger_bpt = kgdb_handle_breakpoint; > __debugger_sstep = kgdb_singlestep; > diff --git a/arch/sh/kernel/kgdb.c b/arch/sh/kernel/kgdb.c > index cc57630f6bf2..14e012ad7c57 100644 > --- a/arch/sh/kernel/kgdb.c > +++ b/arch/sh/kernel/kgdb.c > @@ -314,18 +314,6 @@ BUILD_TRAP_HANDLER(singlestep) > local_irq_restore(flags); > } > > -static void kgdb_call_nmi_hook(void *ignored) > -{ > - kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs()); > -} > - > -void kgdb_roundup_cpus(void) > -{ > - local_irq_enable(); > - smp_call_function(kgdb_call_nmi_hook, NULL, 0); > - local_irq_disable(); > -} > - > static int __kgdb_notify(struct die_args *args, unsigned long cmd) > { > int ret; > diff --git a/include/linux/kgdb.h b/include/linux/kgdb.h > index 05e5b2eb0d32..24422865cd18 100644 > --- a/include/linux/kgdb.h > +++ b/include/linux/kgdb.h > @@ -176,14 +176,25 @@ kgdb_arch_handle_exception(int vector, int signo, int err_code, > char *remcom_out_buffer, > struct pt_regs *regs); > > +/** > + * kgdb_call_nmi_hook - Call kgdb_nmicallback() on the current CPU > + * @ignored: This parameter is only here to match the prototype. > + * > + * If you're using the default implementation of kgdb_roundup_cpus() > + * this function will be called per CPU. If you don't implement > + * kgdb_call_nmi_hook() a default will be used. > + */ > + > +extern void kgdb_call_nmi_hook(void *ignored); > + > /** > * kgdb_roundup_cpus - Get other CPUs into a holding pattern > * > * On SMP systems, we need to get the attention of the other CPUs > * and get them into a known state. This should do what is needed > * to get the other CPUs to call kgdb_wait(). Note that on some arches, > - * the NMI approach is not used for rounding up all the CPUs. For example, > - * in case of MIPS, smp_call_function() is used to roundup CPUs. > + * the NMI approach is not used for rounding up all the CPUs. Normally > + * those architectures can just not implement this and get the default. > * > * On non-SMP systems, this is not called. > */ > diff --git a/kernel/debug/debug_core.c b/kernel/debug/debug_core.c > index f3cadda45f07..10db2833a423 100644 > --- a/kernel/debug/debug_core.c > +++ b/kernel/debug/debug_core.c > @@ -55,6 +55,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -220,6 +221,46 @@ int __weak kgdb_skipexception(int exception, struct pt_regs *regs) > return 0; > } > > +#ifdef CONFIG_SMP > + > +/* > + * Default (weak) implementation for kgdb_roundup_cpus > + */ > + > +static DEFINE_PER_CPU(call_single_data_t, kgdb_roundup_csd); > + > +void __weak kgdb_call_nmi_hook(void *ignored) > +{ > + /* > + * NOTE: get_irq_regs() is supposed to get the registers from > + * before the IPI interrupt happened and so is supposed to > + * show where the processor was. In some situations it's > + * possible we might be called without an IPI, so it might be > + * safer to figure out how to make kgdb_breakpoint() work > + * properly here. > + */ > + kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs()); > +} > + > +void __weak kgdb_roundup_cpus(void) > +{ > + call_single_data_t *csd; > + int this_cpu = raw_smp_processor_id(); > + int cpu; > + > + for_each_online_cpu(cpu) { > + /* No need to roundup ourselves */ > + if (cpu == this_cpu) > + continue; > + > + csd = &per_cpu(kgdb_roundup_csd, cpu); > + csd->func = kgdb_call_nmi_hook; > + smp_call_function_single_async(cpu, csd); > + } > +} > + > +#endif > + > /* > * Some architectures need cache flushes when we set/clear a > * breakpoint: > -- > 2.20.0.rc0.387.gc7a69e6b6c-goog >