aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Senozhatsky <sergey.senozhatsky@gmail.com>2016-03-17 17:21:23 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-03-17 18:09:34 -0400
commit6b97a20d3a7909daa06625d4440c2c52d7bf08d7 (patch)
treeaa794a72f34aceddfaacea169feec753460ab889
parenta8199371afc27946d72f0d53e938e78d2ea0bae3 (diff)
printk: set may_schedule for some of console_trylock() callers
console_unlock() allows to cond_resched() if its caller has set `console_may_schedule' to 1, since 8d91f8b15361 ("printk: do cond_resched() between lines while outputting to consoles"). The rules are: -- console_lock() always sets `console_may_schedule' to 1 -- console_trylock() always sets `console_may_schedule' to 0 However, console_trylock() callers (among them is printk()) do not always call printk() from atomic contexts, and some of them can cond_resched() in console_unlock(), so console_trylock() can set `console_may_schedule' to 1 for such processes. For !CONFIG_PREEMPT_COUNT kernels, however, console_trylock() always sets `console_may_schedule' to 0. It's possible to drop explicit preempt_disable()/preempt_enable() in vprintk_emit(), because console_unlock() and console_trylock() are now smart enough: a) console_unlock() does not cond_resched() when it's unsafe (console_trylock() takes care of that) b) console_unlock() does can_use_console() check. Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Reviewed-by: Petr Mladek <pmladek@suse.com> Cc: Jan Kara <jack@suse.com> Cc: Tejun Heo <tj@kernel.org> Cc: Kyle McMartin <kyle@kernel.org> Cc: Dave Jones <davej@codemonkey.org.uk> Cc: Calvin Owens <calvinowens@fb.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--kernel/printk/printk.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 2523332bd998..a6d023c3b852 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1758,20 +1758,12 @@ asmlinkage int vprintk_emit(int facility, int level,
1758 if (!in_sched) { 1758 if (!in_sched) {
1759 lockdep_off(); 1759 lockdep_off();
1760 /* 1760 /*
1761 * Disable preemption to avoid being preempted while holding
1762 * console_sem which would prevent anyone from printing to
1763 * console
1764 */
1765 preempt_disable();
1766
1767 /*
1768 * Try to acquire and then immediately release the console 1761 * Try to acquire and then immediately release the console
1769 * semaphore. The release will print out buffers and wake up 1762 * semaphore. The release will print out buffers and wake up
1770 * /dev/kmsg and syslog() users. 1763 * /dev/kmsg and syslog() users.
1771 */ 1764 */
1772 if (console_trylock()) 1765 if (console_trylock())
1773 console_unlock(); 1766 console_unlock();
1774 preempt_enable();
1775 lockdep_on(); 1767 lockdep_on();
1776 } 1768 }
1777 1769
@@ -2122,7 +2114,20 @@ int console_trylock(void)
2122 return 0; 2114 return 0;
2123 } 2115 }
2124 console_locked = 1; 2116 console_locked = 1;
2125 console_may_schedule = 0; 2117 /*
2118 * When PREEMPT_COUNT disabled we can't reliably detect if it's
2119 * safe to schedule (e.g. calling printk while holding a spin_lock),
2120 * because preempt_disable()/preempt_enable() are just barriers there
2121 * and preempt_count() is always 0.
2122 *
2123 * RCU read sections have a separate preemption counter when
2124 * PREEMPT_RCU enabled thus we must take extra care and check
2125 * rcu_preempt_depth(), otherwise RCU read sections modify
2126 * preempt_count().
2127 */
2128 console_may_schedule = !oops_in_progress &&
2129 preemptible() &&
2130 !rcu_preempt_depth();
2126 return 1; 2131 return 1;
2127} 2132}
2128EXPORT_SYMBOL(console_trylock); 2133EXPORT_SYMBOL(console_trylock);