Skip to content

Commit

Permalink
printk: enable interrupts before calling console_trylock_for_printk()
Browse files Browse the repository at this point in the history
We need interrupts disabled when calling console_trylock_for_printk()
only so that cpu id we pass to can_use_console() remains valid (for
other things console_sem provides all the exclusion we need and
deadlocks on console_sem due to interrupts are impossible because we use
down_trylock()).  However if we are rescheduled, we are guaranteed to
run on an online cpu so we can easily just get the cpu id in
can_use_console().

We can lose a bit of performance when we enable interrupts in
vprintk_emit() and then disable them again in console_unlock() but OTOH
it can somewhat reduce interrupt latency caused by console_unlock()
especially since later in the patch series we will want to spin on
console_sem in console_trylock_for_printk().

Signed-off-by: Jan Kara <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
jankara authored and torvalds committed Jun 4, 2014
1 parent bd8d7cf commit 939f04b
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions kernel/printk/printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1418,10 +1418,9 @@ static int have_callable_console(void)
/*
* Can we actually use the console at this time on this cpu?
*
* Console drivers may assume that per-cpu resources have
* been allocated. So unless they're explicitly marked as
* being able to cope (CON_ANYTIME) don't call them until
* this CPU is officially up.
* Console drivers may assume that per-cpu resources have been allocated. So
* unless they're explicitly marked as being able to cope (CON_ANYTIME) don't
* call them until this CPU is officially up.
*/
static inline int can_use_console(unsigned int cpu)
{
Expand All @@ -1434,8 +1433,10 @@ static inline int can_use_console(unsigned int cpu)
* console_lock held, and 'console_locked' set) if it
* is successful, false otherwise.
*/
static int console_trylock_for_printk(unsigned int cpu)
static int console_trylock_for_printk(void)
{
unsigned int cpu = smp_processor_id();

if (!console_trylock())
return 0;
/*
Expand Down Expand Up @@ -1605,7 +1606,8 @@ asmlinkage int vprintk_emit(int facility, int level,
*/
if (!oops_in_progress && !lockdep_recursing(current)) {
recursion_bug = 1;
goto out_restore_irqs;
local_irq_restore(flags);
return 0;
}
zap_locks();
}
Expand Down Expand Up @@ -1708,17 +1710,22 @@ asmlinkage int vprintk_emit(int facility, int level,

logbuf_cpu = UINT_MAX;
raw_spin_unlock(&logbuf_lock);
lockdep_on();
local_irq_restore(flags);

/*
* 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 semaphore.
* The release will print out buffers and wake up /dev/kmsg and syslog()
* users.
*/
if (console_trylock_for_printk(this_cpu))
if (console_trylock_for_printk())
console_unlock();

lockdep_on();
out_restore_irqs:
local_irq_restore(flags);
preempt_enable();

return printed_len;
}
Expand Down

0 comments on commit 939f04b

Please sign in to comment.