From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751604AbcA0AL2 (ORCPT ); Tue, 26 Jan 2016 19:11:28 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:39311 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750826AbcA0ALZ (ORCPT ); Tue, 26 Jan 2016 19:11:25 -0500 Date: Tue, 26 Jan 2016 16:11:24 -0800 From: Andrew Morton To: Byungchul Park Cc: mingo@kernel.org, linux-kernel@vger.kernel.org, akinobu.mita@gmail.com, jack@suse.cz, torvalds@linux-foundation.org Subject: Re: [PATCH v2] lib/spinlock_debug.c: prevent an infinite recursive cycle in spin_dump() Message-Id: <20160126161124.cba2f003a4a21a6f11cf51a8@linux-foundation.org> In-Reply-To: <1453078692-15291-1-git-send-email-byungchul.park@lge.com> References: <1453078692-15291-1-git-send-email-byungchul.park@lge.com> X-Mailer: Sylpheed 3.4.1 (GTK+ 2.24.23; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 18 Jan 2016 09:58:12 +0900 Byungchul Park wrote: > It causes an infinite recursive cycle when using CONFIG_DEBUG_SPINLOCK, > in the spin_dump(). Backtrace prints printk() -> console_trylock() -> > do_raw_spin_lock() -> spint_bug() -> spin_dump() -> printk()... > infinitely. > > If the spin_bug() is called from a function like printk() which is > trying to obtain the console lock, we should prevent the debug spinlock > code from calling printk() again in that context. > lol. Excellent. > --- a/kernel/locking/spinlock_debug.c > +++ b/kernel/locking/spinlock_debug.c > @@ -67,11 +67,22 @@ static void spin_dump(raw_spinlock_t *lock, const char *msg) > dump_stack(); > } > > +extern int is_console_lock(raw_spinlock_t *lock); > + > static void spin_bug(raw_spinlock_t *lock, const char *msg) > { > if (!debug_locks_off()) > return; > > + /* > + * If this function is called from a function like printk() > + * which is trying to obtain the console lock, then we should > + * not call printk() any more. Or it will cause an infinite > + * recursive cycle! > + */ > + if (unlikely(is_console_lock(lock))) > + return; > + > spin_dump(lock, msg); > } > > diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c > index 2ce8826..50ea552 100644 > --- a/kernel/printk/printk.c > +++ b/kernel/printk/printk.c > @@ -119,6 +119,11 @@ static int __down_trylock_console_sem(unsigned long ip) > up(&console_sem);\ > } while (0) > > +int is_console_lock(raw_spinlock_t *lock) > +{ > + return &console_sem.lock == lock; > +} > + > /* > * This is used for debugging the mess that is the VT code by > * keeping track if we have the console semaphore held. It's I can't immediately think of anything better than this. It's a hack, but it's a small and quite clear hack.