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 8d91f8b153 ("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>
This commit is contained in:
Sergey Senozhatsky 2016-03-17 14:21:23 -07:00 committed by Linus Torvalds
parent a8199371af
commit 6b97a20d3a

View file

@ -1757,13 +1757,6 @@ asmlinkage int vprintk_emit(int facility, int level,
/* 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) {
lockdep_off(); lockdep_off();
/*
* Disable preemption to avoid being preempted while holding
* console_sem which would prevent anyone from printing to
* console
*/
preempt_disable();
/* /*
* Try to acquire and then immediately release the console * Try to acquire and then immediately release the console
* semaphore. The release will print out buffers and wake up * semaphore. The release will print out buffers and wake up
@ -1771,7 +1764,6 @@ asmlinkage int vprintk_emit(int facility, int level,
*/ */
if (console_trylock()) if (console_trylock())
console_unlock(); console_unlock();
preempt_enable();
lockdep_on(); lockdep_on();
} }
@ -2122,7 +2114,20 @@ int console_trylock(void)
return 0; return 0;
} }
console_locked = 1; console_locked = 1;
console_may_schedule = 0; /*
* When PREEMPT_COUNT disabled we can't reliably detect if it's
* safe to schedule (e.g. calling printk while holding a spin_lock),
* because preempt_disable()/preempt_enable() are just barriers there
* and preempt_count() is always 0.
*
* RCU read sections have a separate preemption counter when
* PREEMPT_RCU enabled thus we must take extra care and check
* rcu_preempt_depth(), otherwise RCU read sections modify
* preempt_count().
*/
console_may_schedule = !oops_in_progress &&
preemptible() &&
!rcu_preempt_depth();
return 1; return 1;
} }
EXPORT_SYMBOL(console_trylock); EXPORT_SYMBOL(console_trylock);