Commit de6fcbdb authored by Sergey Senozhatsky's avatar Sergey Senozhatsky Committed by Petr Mladek

printk: convert the rest to printk-safe

This patch converts the rest of logbuf users (which are
out of printk recursion case, but can deadlock in printk).
To make printk-safe usage easier the patch introduces 4
helper macros:
- logbuf_lock_irq()/logbuf_unlock_irq()
  lock/unlock the logbuf lock and disable/enable local IRQ

- logbuf_lock_irqsave(flags)/logbuf_unlock_irqrestore(flags)
  lock/unlock the logbuf lock and saves/restores local IRQ state

Link: http://lkml.kernel.org/r/20161227141611.940-9-sergey.senozhatsky@gmail.com
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Steven Rostedt <rostedt@goodmis.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: default avatarSergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
parent 8b1742c9
...@@ -370,6 +370,34 @@ __packed __aligned(4) ...@@ -370,6 +370,34 @@ __packed __aligned(4)
*/ */
DEFINE_RAW_SPINLOCK(logbuf_lock); DEFINE_RAW_SPINLOCK(logbuf_lock);
/*
* Helper macros to lock/unlock logbuf_lock and switch between
* printk-safe/unsafe modes.
*/
#define logbuf_lock_irq() \
do { \
printk_safe_enter_irq(); \
raw_spin_lock(&logbuf_lock); \
} while (0)
#define logbuf_unlock_irq() \
do { \
raw_spin_unlock(&logbuf_lock); \
printk_safe_exit_irq(); \
} while (0)
#define logbuf_lock_irqsave(flags) \
do { \
printk_safe_enter_irqsave(flags); \
raw_spin_lock(&logbuf_lock); \
} while (0)
#define logbuf_unlock_irqrestore(flags) \
do { \
raw_spin_unlock(&logbuf_lock); \
printk_safe_exit_irqrestore(flags); \
} while (0)
#ifdef CONFIG_PRINTK #ifdef CONFIG_PRINTK
DECLARE_WAIT_QUEUE_HEAD(log_wait); DECLARE_WAIT_QUEUE_HEAD(log_wait);
/* the next printk record to read by syslog(READ) or /proc/kmsg */ /* the next printk record to read by syslog(READ) or /proc/kmsg */
...@@ -801,20 +829,21 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, ...@@ -801,20 +829,21 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
ret = mutex_lock_interruptible(&user->lock); ret = mutex_lock_interruptible(&user->lock);
if (ret) if (ret)
return ret; return ret;
raw_spin_lock_irq(&logbuf_lock);
logbuf_lock_irq();
while (user->seq == log_next_seq) { while (user->seq == log_next_seq) {
if (file->f_flags & O_NONBLOCK) { if (file->f_flags & O_NONBLOCK) {
ret = -EAGAIN; ret = -EAGAIN;
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
goto out; goto out;
} }
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
ret = wait_event_interruptible(log_wait, ret = wait_event_interruptible(log_wait,
user->seq != log_next_seq); user->seq != log_next_seq);
if (ret) if (ret)
goto out; goto out;
raw_spin_lock_irq(&logbuf_lock); logbuf_lock_irq();
} }
if (user->seq < log_first_seq) { if (user->seq < log_first_seq) {
...@@ -822,7 +851,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, ...@@ -822,7 +851,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
user->idx = log_first_idx; user->idx = log_first_idx;
user->seq = log_first_seq; user->seq = log_first_seq;
ret = -EPIPE; ret = -EPIPE;
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
goto out; goto out;
} }
...@@ -835,7 +864,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, ...@@ -835,7 +864,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
user->idx = log_next(user->idx); user->idx = log_next(user->idx);
user->seq++; user->seq++;
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
if (len > count) { if (len > count) {
ret = -EINVAL; ret = -EINVAL;
...@@ -862,7 +891,7 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence) ...@@ -862,7 +891,7 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
if (offset) if (offset)
return -ESPIPE; return -ESPIPE;
raw_spin_lock_irq(&logbuf_lock); logbuf_lock_irq();
switch (whence) { switch (whence) {
case SEEK_SET: case SEEK_SET:
/* the first record */ /* the first record */
...@@ -886,7 +915,7 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence) ...@@ -886,7 +915,7 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
default: default:
ret = -EINVAL; ret = -EINVAL;
} }
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
return ret; return ret;
} }
...@@ -900,7 +929,7 @@ static unsigned int devkmsg_poll(struct file *file, poll_table *wait) ...@@ -900,7 +929,7 @@ static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
poll_wait(file, &log_wait, wait); poll_wait(file, &log_wait, wait);
raw_spin_lock_irq(&logbuf_lock); logbuf_lock_irq();
if (user->seq < log_next_seq) { if (user->seq < log_next_seq) {
/* return error when data has vanished underneath us */ /* return error when data has vanished underneath us */
if (user->seq < log_first_seq) if (user->seq < log_first_seq)
...@@ -908,7 +937,7 @@ static unsigned int devkmsg_poll(struct file *file, poll_table *wait) ...@@ -908,7 +937,7 @@ static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
else else
ret = POLLIN|POLLRDNORM; ret = POLLIN|POLLRDNORM;
} }
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
return ret; return ret;
} }
...@@ -938,10 +967,10 @@ static int devkmsg_open(struct inode *inode, struct file *file) ...@@ -938,10 +967,10 @@ static int devkmsg_open(struct inode *inode, struct file *file)
mutex_init(&user->lock); mutex_init(&user->lock);
raw_spin_lock_irq(&logbuf_lock); logbuf_lock_irq();
user->idx = log_first_idx; user->idx = log_first_idx;
user->seq = log_first_seq; user->seq = log_first_seq;
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
file->private_data = user; file->private_data = user;
return 0; return 0;
...@@ -1083,13 +1112,13 @@ void __init setup_log_buf(int early) ...@@ -1083,13 +1112,13 @@ void __init setup_log_buf(int early)
return; return;
} }
raw_spin_lock_irqsave(&logbuf_lock, flags); logbuf_lock_irqsave(flags);
log_buf_len = new_log_buf_len; log_buf_len = new_log_buf_len;
log_buf = new_log_buf; log_buf = new_log_buf;
new_log_buf_len = 0; new_log_buf_len = 0;
free = __LOG_BUF_LEN - log_next_idx; free = __LOG_BUF_LEN - log_next_idx;
memcpy(log_buf, __log_buf, __LOG_BUF_LEN); memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
raw_spin_unlock_irqrestore(&logbuf_lock, flags); logbuf_unlock_irqrestore(flags);
pr_info("log_buf_len: %d bytes\n", log_buf_len); pr_info("log_buf_len: %d bytes\n", log_buf_len);
pr_info("early log buf free: %d(%d%%)\n", pr_info("early log buf free: %d(%d%%)\n",
...@@ -1267,7 +1296,7 @@ static int syslog_print(char __user *buf, int size) ...@@ -1267,7 +1296,7 @@ static int syslog_print(char __user *buf, int size)
size_t n; size_t n;
size_t skip; size_t skip;
raw_spin_lock_irq(&logbuf_lock); logbuf_lock_irq();
if (syslog_seq < log_first_seq) { if (syslog_seq < log_first_seq) {
/* messages are gone, move to first one */ /* messages are gone, move to first one */
syslog_seq = log_first_seq; syslog_seq = log_first_seq;
...@@ -1275,7 +1304,7 @@ static int syslog_print(char __user *buf, int size) ...@@ -1275,7 +1304,7 @@ static int syslog_print(char __user *buf, int size)
syslog_partial = 0; syslog_partial = 0;
} }
if (syslog_seq == log_next_seq) { if (syslog_seq == log_next_seq) {
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
break; break;
} }
...@@ -1294,7 +1323,7 @@ static int syslog_print(char __user *buf, int size) ...@@ -1294,7 +1323,7 @@ static int syslog_print(char __user *buf, int size)
syslog_partial += n; syslog_partial += n;
} else } else
n = 0; n = 0;
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
if (!n) if (!n)
break; break;
...@@ -1323,7 +1352,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear) ...@@ -1323,7 +1352,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
if (!text) if (!text)
return -ENOMEM; return -ENOMEM;
raw_spin_lock_irq(&logbuf_lock); logbuf_lock_irq();
if (buf) { if (buf) {
u64 next_seq; u64 next_seq;
u64 seq; u64 seq;
...@@ -1371,12 +1400,12 @@ static int syslog_print_all(char __user *buf, int size, bool clear) ...@@ -1371,12 +1400,12 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
idx = log_next(idx); idx = log_next(idx);
seq++; seq++;
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
if (copy_to_user(buf + len, text, textlen)) if (copy_to_user(buf + len, text, textlen))
len = -EFAULT; len = -EFAULT;
else else
len += textlen; len += textlen;
raw_spin_lock_irq(&logbuf_lock); logbuf_lock_irq();
if (seq < log_first_seq) { if (seq < log_first_seq) {
/* messages are gone, move to next one */ /* messages are gone, move to next one */
...@@ -1390,7 +1419,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear) ...@@ -1390,7 +1419,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
clear_seq = log_next_seq; clear_seq = log_next_seq;
clear_idx = log_next_idx; clear_idx = log_next_idx;
} }
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
kfree(text); kfree(text);
return len; return len;
...@@ -1477,7 +1506,7 @@ int do_syslog(int type, char __user *buf, int len, int source) ...@@ -1477,7 +1506,7 @@ int do_syslog(int type, char __user *buf, int len, int source)
break; break;
/* Number of chars in the log buffer */ /* Number of chars in the log buffer */
case SYSLOG_ACTION_SIZE_UNREAD: case SYSLOG_ACTION_SIZE_UNREAD:
raw_spin_lock_irq(&logbuf_lock); logbuf_lock_irq();
if (syslog_seq < log_first_seq) { if (syslog_seq < log_first_seq) {
/* messages are gone, move to first one */ /* messages are gone, move to first one */
syslog_seq = log_first_seq; syslog_seq = log_first_seq;
...@@ -1505,7 +1534,7 @@ int do_syslog(int type, char __user *buf, int len, int source) ...@@ -1505,7 +1534,7 @@ int do_syslog(int type, char __user *buf, int len, int source)
} }
error -= syslog_partial; error -= syslog_partial;
} }
raw_spin_unlock_irq(&logbuf_lock); logbuf_unlock_irq();
break; break;
/* Size of the log buffer */ /* Size of the log buffer */
case SYSLOG_ACTION_SIZE_BUFFER: case SYSLOG_ACTION_SIZE_BUFFER:
...@@ -1682,9 +1711,8 @@ asmlinkage int vprintk_emit(int facility, int level, ...@@ -1682,9 +1711,8 @@ asmlinkage int vprintk_emit(int facility, int level,
boot_delay_msec(level); boot_delay_msec(level);
printk_delay(); printk_delay();
printk_safe_enter_irqsave(flags);
/* This stops the holder of console_sem just where we want him */ /* This stops the holder of console_sem just where we want him */
raw_spin_lock(&logbuf_lock); logbuf_lock_irqsave(flags);
/* /*
* The printf needs to come first; we need the syslog * The printf needs to come first; we need the syslog
* prefix which might be passed-in as a parameter. * prefix which might be passed-in as a parameter.
...@@ -1727,8 +1755,7 @@ asmlinkage int vprintk_emit(int facility, int level, ...@@ -1727,8 +1755,7 @@ asmlinkage int vprintk_emit(int facility, int level,
printed_len += log_output(facility, level, lflags, dict, dictlen, text, text_len); printed_len += log_output(facility, level, lflags, dict, dictlen, text, text_len);
raw_spin_unlock(&logbuf_lock); logbuf_unlock_irqrestore(flags);
printk_safe_exit_irqrestore(flags);
/* If called from the scheduler, we can not call up(). */ /* If called from the scheduler, we can not call up(). */
if (!in_sched) { if (!in_sched) {
...@@ -2501,10 +2528,10 @@ void register_console(struct console *newcon) ...@@ -2501,10 +2528,10 @@ void register_console(struct console *newcon)
* console_unlock(); will print out the buffered messages * console_unlock(); will print out the buffered messages
* for us. * for us.
*/ */
raw_spin_lock_irqsave(&logbuf_lock, flags); logbuf_lock_irqsave(flags);
console_seq = syslog_seq; console_seq = syslog_seq;
console_idx = syslog_idx; console_idx = syslog_idx;
raw_spin_unlock_irqrestore(&logbuf_lock, flags); logbuf_unlock_irqrestore(flags);
/* /*
* We're about to replay the log buffer. Only do this to the * We're about to replay the log buffer. Only do this to the
* just-registered console to avoid excessive message spam to * just-registered console to avoid excessive message spam to
...@@ -2803,12 +2830,12 @@ void kmsg_dump(enum kmsg_dump_reason reason) ...@@ -2803,12 +2830,12 @@ void kmsg_dump(enum kmsg_dump_reason reason)
/* initialize iterator with data about the stored records */ /* initialize iterator with data about the stored records */
dumper->active = true; dumper->active = true;
raw_spin_lock_irqsave(&logbuf_lock, flags); logbuf_lock_irqsave(flags);
dumper->cur_seq = clear_seq; dumper->cur_seq = clear_seq;
dumper->cur_idx = clear_idx; dumper->cur_idx = clear_idx;
dumper->next_seq = log_next_seq; dumper->next_seq = log_next_seq;
dumper->next_idx = log_next_idx; dumper->next_idx = log_next_idx;
raw_spin_unlock_irqrestore(&logbuf_lock, flags); logbuf_unlock_irqrestore(flags);
/* invoke dumper which will iterate over records */ /* invoke dumper which will iterate over records */
dumper->dump(dumper, reason); dumper->dump(dumper, reason);
...@@ -2893,9 +2920,9 @@ bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog, ...@@ -2893,9 +2920,9 @@ bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
unsigned long flags; unsigned long flags;
bool ret; bool ret;
raw_spin_lock_irqsave(&logbuf_lock, flags); logbuf_lock_irqsave(flags);
ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len); ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
raw_spin_unlock_irqrestore(&logbuf_lock, flags); logbuf_unlock_irqrestore(flags);
return ret; return ret;
} }
...@@ -2934,7 +2961,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, ...@@ -2934,7 +2961,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
if (!dumper->active) if (!dumper->active)
goto out; goto out;
raw_spin_lock_irqsave(&logbuf_lock, flags); logbuf_lock_irqsave(flags);
if (dumper->cur_seq < log_first_seq) { if (dumper->cur_seq < log_first_seq) {
/* messages are gone, move to first available one */ /* messages are gone, move to first available one */
dumper->cur_seq = log_first_seq; dumper->cur_seq = log_first_seq;
...@@ -2943,7 +2970,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, ...@@ -2943,7 +2970,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
/* last entry */ /* last entry */
if (dumper->cur_seq >= dumper->next_seq) { if (dumper->cur_seq >= dumper->next_seq) {
raw_spin_unlock_irqrestore(&logbuf_lock, flags); logbuf_unlock_irqrestore(flags);
goto out; goto out;
} }
...@@ -2985,7 +3012,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, ...@@ -2985,7 +3012,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
dumper->next_seq = next_seq; dumper->next_seq = next_seq;
dumper->next_idx = next_idx; dumper->next_idx = next_idx;
ret = true; ret = true;
raw_spin_unlock_irqrestore(&logbuf_lock, flags); logbuf_unlock_irqrestore(flags);
out: out:
if (len) if (len)
*len = l; *len = l;
...@@ -3023,9 +3050,9 @@ void kmsg_dump_rewind(struct kmsg_dumper *dumper) ...@@ -3023,9 +3050,9 @@ void kmsg_dump_rewind(struct kmsg_dumper *dumper)
{ {
unsigned long flags; unsigned long flags;
raw_spin_lock_irqsave(&logbuf_lock, flags); logbuf_lock_irqsave(flags);
kmsg_dump_rewind_nolock(dumper); kmsg_dump_rewind_nolock(dumper);
raw_spin_unlock_irqrestore(&logbuf_lock, flags); logbuf_unlock_irqrestore(flags);
} }
EXPORT_SYMBOL_GPL(kmsg_dump_rewind); EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment