From patchwork Fri Nov 27 11:09:30 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Mladek X-Patchwork-Id: 7712901 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B9622BF90C for ; Fri, 27 Nov 2015 11:12:39 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CC2192066C for ; Fri, 27 Nov 2015 11:12:38 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8B2EF20641 for ; Fri, 27 Nov 2015 11:12:37 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1a2GvN-0005vx-TI; Fri, 27 Nov 2015 11:10:45 +0000 Received: from mx2.suse.de ([195.135.220.15]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1a2Gum-0004U3-Cf for linux-arm-kernel@lists.infradead.org; Fri, 27 Nov 2015 11:10:12 +0000 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 391A1AD77; Fri, 27 Nov 2015 11:08:10 +0000 (UTC) From: Petr Mladek To: Andrew Morton Subject: [PATCH v2 3/5] printk/nmi: Try hard to print Oops message in NMI context Date: Fri, 27 Nov 2015 12:09:30 +0100 Message-Id: <1448622572-16900-4-git-send-email-pmladek@suse.com> X-Mailer: git-send-email 1.8.5.6 In-Reply-To: <1448622572-16900-1-git-send-email-pmladek@suse.com> References: <1448622572-16900-1-git-send-email-pmladek@suse.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151127_031008_980629_CE715B20 X-CRM114-Status: GOOD ( 16.45 ) X-Spam-Score: -2.6 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-mips@linux-mips.org, Daniel Thompson , Jiri Kosina , linux-cris-kernel@axis.com, linux-s390@vger.kernel.org, Peter Zijlstra , x86@kernel.org, linux-kernel@vger.kernel.org, Steven Rostedt , adi-buildroot-devel@lists.sourceforge.net, Ingo Molnar , Petr Mladek , linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, Russell King , Thomas Gleixner , linuxppc-dev@lists.ozlabs.org, Petr Mladek , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Oops messages are important for debugging. We should try hard to get them directly into the ring buffer and print them to the console even in NMI context. This is problematic because the needed locks might already be taken. What we can do, though, is to zap all printk locks. We already do this when a printk recursion is detected. This should be safe because the system is crashing and there shouldn't be any printk caller that would cause the deadlock. We should also flush the printk NMI buffers when Oops begins. bust_spinlocks() might sound like a bad location but it already does several other printk-related operations. Finally, we have to serialize the printing to do not mix backtraces from different CPUs. A simple spinlock is enough. It has already been used for this purpose, see the commit a9edc88093287183a ("x86/nmi: Perform a safe NMI stack trace on all CPUs"). Signed-off-by: Petr Mladek --- arch/s390/mm/fault.c | 1 + kernel/printk/nmi.c | 6 +++++- kernel/printk/printk.c | 7 +++++++ lib/bust_spinlocks.c | 1 + lib/nmi_backtrace.c | 8 ++++++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/arch/s390/mm/fault.c b/arch/s390/mm/fault.c index ec1a30d0d11a..6566344db263 100644 --- a/arch/s390/mm/fault.c +++ b/arch/s390/mm/fault.c @@ -80,6 +80,7 @@ void bust_spinlocks(int yes) { if (yes) { oops_in_progress = 1; + printk_nmi_flush(); } else { int loglevel_save = console_loglevel; console_unblank(); diff --git a/kernel/printk/nmi.c b/kernel/printk/nmi.c index 05b4c09110f9..34ba760ae794 100644 --- a/kernel/printk/nmi.c +++ b/kernel/printk/nmi.c @@ -202,7 +202,11 @@ void __init printk_nmi_init(void) void printk_nmi_enter(void) { - this_cpu_write(printk_func, vprintk_nmi); + /* + * We try hard to print the messages directly when Oops is in progress. + */ + if (!oops_in_progress) + this_cpu_write(printk_func, vprintk_nmi); } void printk_nmi_exit(void) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 88641c74163d..e970b623ae26 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1705,6 +1705,13 @@ asmlinkage int vprintk_emit(int facility, int level, } lockdep_off(); + /* + * Messages are passed from NMI context using an extra buffer. + * The only exception is when Oops is in progress. In this case + * we try hard to get them out directly. + */ + if (unlikely(oops_in_progress && in_nmi())) + zap_locks(); raw_spin_lock(&logbuf_lock); logbuf_cpu = this_cpu; diff --git a/lib/bust_spinlocks.c b/lib/bust_spinlocks.c index f8e0e5367398..7a98098b0fef 100644 --- a/lib/bust_spinlocks.c +++ b/lib/bust_spinlocks.c @@ -20,6 +20,7 @@ void __attribute__((weak)) bust_spinlocks(int yes) { if (yes) { ++oops_in_progress; + printk_nmi_flush(); } else { #ifdef CONFIG_VT unblank_screen(); diff --git a/lib/nmi_backtrace.c b/lib/nmi_backtrace.c index 26caf51cc238..026e9f16e742 100644 --- a/lib/nmi_backtrace.c +++ b/lib/nmi_backtrace.c @@ -74,14 +74,22 @@ void nmi_trigger_all_cpu_backtrace(bool include_self, bool nmi_cpu_backtrace(struct pt_regs *regs) { + static arch_spinlock_t lock = __ARCH_SPIN_LOCK_UNLOCKED; int cpu = smp_processor_id(); if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) { + /* Serialize backtraces when printing directly. */ + if (unlikely(oops_in_progress)) + arch_spin_lock(&lock); + pr_warn("NMI backtrace for cpu %d\n", cpu); if (regs) show_regs(regs); else dump_stack(); + + if (unlikely(oops_in_progress)) + arch_spin_unlock(&lock); cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask)); return true; }