2016-05-21 08:00:33 +08:00
|
|
|
/*
|
|
|
|
* internal.h - printk internal definitions
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <linux/percpu.h>
|
|
|
|
|
printk: introduce per-cpu safe_print seq buffer
This patch extends the idea of NMI per-cpu buffers to regions
that may cause recursive printk() calls and possible deadlocks.
Namely, printk() can't handle printk calls from schedule code
or printk() calls from lock debugging code (spin_dump() for instance);
because those may be called with `sem->lock' already taken or any
other `critical' locks (p->pi_lock, etc.). An example of deadlock
can be
vprintk_emit()
console_unlock()
up() << raw_spin_lock_irqsave(&sem->lock, flags);
wake_up_process()
try_to_wake_up()
ttwu_queue()
ttwu_activate()
activate_task()
enqueue_task()
enqueue_task_fair()
cfs_rq_of()
task_of()
WARN_ON_ONCE(!entity_is_task(se))
vprintk_emit()
console_trylock()
down_trylock()
raw_spin_lock_irqsave(&sem->lock, flags)
^^^^ deadlock
and some other cases.
Just like in NMI implementation, the solution uses a per-cpu
`printk_func' pointer to 'redirect' printk() calls to a 'safe'
callback, that store messages in a per-cpu buffer and flushes
them back to logbuf buffer later.
Usage example:
printk()
printk_safe_enter_irqsave(flags)
//
// any printk() call from here will endup in vprintk_safe(),
// that stores messages in a special per-CPU buffer.
//
printk_safe_exit_irqrestore(flags)
The 'redirection' mechanism, though, has been reworked, as suggested
by Petr Mladek. Instead of using a per-cpu @print_func callback we now
keep a per-cpu printk-context variable and call either default or nmi
vprintk function depending on its value. printk_nmi_entrer/exit and
printk_safe_enter/exit, thus, just set/celar corresponding bits in
printk-context functions.
The patch only adds printk_safe support, we don't use it yet.
Link: http://lkml.kernel.org/r/20161227141611.940-4-sergey.senozhatsky@gmail.com
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Tejun Heo <tj@kernel.org>
Cc: Calvin Owens <calvinowens@fb.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Peter Hurley <peter@hurleysoftware.com>
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
2016-12-27 22:16:06 +08:00
|
|
|
#ifdef CONFIG_PRINTK
|
|
|
|
|
|
|
|
#define PRINTK_SAFE_CONTEXT_MASK 0x7fffffff
|
|
|
|
#define PRINTK_NMI_CONTEXT_MASK 0x80000000
|
|
|
|
|
|
|
|
extern raw_spinlock_t logbuf_lock;
|
|
|
|
|
|
|
|
__printf(1, 0) int vprintk_default(const char *fmt, va_list args);
|
|
|
|
__printf(1, 0) int vprintk_func(const char *fmt, va_list args);
|
|
|
|
void __printk_safe_enter(void);
|
|
|
|
void __printk_safe_exit(void);
|
|
|
|
|
|
|
|
#define printk_safe_enter_irqsave(flags) \
|
|
|
|
do { \
|
|
|
|
local_irq_save(flags); \
|
|
|
|
__printk_safe_enter(); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define printk_safe_exit_irqrestore(flags) \
|
|
|
|
do { \
|
|
|
|
__printk_safe_exit(); \
|
|
|
|
local_irq_restore(flags); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define printk_safe_enter_irq() \
|
|
|
|
do { \
|
|
|
|
local_irq_disable(); \
|
|
|
|
__printk_safe_enter(); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define printk_safe_exit_irq() \
|
|
|
|
do { \
|
|
|
|
__printk_safe_exit(); \
|
|
|
|
local_irq_enable(); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
__printf(1, 0) int vprintk_func(const char *fmt, va_list args) { return 0; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In !PRINTK builds we still export logbuf_lock spin_lock, console_sem
|
|
|
|
* semaphore and some of console functions (console_unlock()/etc.), so
|
|
|
|
* printk-safe must preserve the existing local IRQ guarantees.
|
|
|
|
*/
|
|
|
|
#define printk_safe_enter_irqsave(flags) local_irq_save(flags)
|
|
|
|
#define printk_safe_exit_irqrestore(flags) local_irq_restore(flags)
|
|
|
|
|
|
|
|
#define printk_safe_enter_irq() local_irq_disable()
|
|
|
|
#define printk_safe_exit_irq() local_irq_enable()
|
|
|
|
|
|
|
|
#endif /* CONFIG_PRINTK */
|