Skip to content

Commit

Permalink
printk: lock/unlock console only for new logbuf entries
Browse files Browse the repository at this point in the history
Prior to commit 5c2992e ("printk: remove console flushing special
cases for partial buffered lines") we would do console_cont_flush()
for each pr_cont() to print cont fragments, so console_unlock() would
actually print data:

	pr_cont();
	 console_lock();
	 console_unlock()
	  console_cont_flush(); // print cont fragment
	...
	pr_cont();
	 console_lock();
	 console_unlock()
	  console_cont_flush(); // print cont fragment

We don't do console_cont_flush() anymore, so when we do pr_cont()
console_unlock() does nothing (unless we flushed the cont buffer):

	pr_cont();
	 console_lock();
	 console_unlock();      // noop
	...
	pr_cont();
	 console_lock();
	 console_unlock();      // noop
	...
	pr_cont();
	  cont_flush();
	    console_lock();
	    console_unlock();   // print data

We also wakeup klogd purposelessly for pr_cont() output - un-flushed
cont buffer is not stored in log_buf; there is nothing to pull.

Thus we can console_lock()/console_unlock()/wake_up_klogd() only when
we know that we log_store()-ed a message and there is something to
print to the consoles/syslog.

Link: http://lkml.kernel.org/r/[email protected]
To: Steven Rostedt <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Dmitriy Vyukov <[email protected]>
Cc: Tetsuo Handa <[email protected]>
Cc: Tejun Heo <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: LKML <[email protected]>
Cc: Sergey Senozhatsky <[email protected]>
Signed-off-by: Sergey Senozhatsky <[email protected]>
Signed-off-by: Petr Mladek <[email protected]>
  • Loading branch information
sergey-senozhatsky authored and pmladek committed Oct 12, 2018
1 parent 9627808 commit 3ac37a9
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions kernel/printk/printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1890,8 +1890,9 @@ asmlinkage int vprintk_emit(int facility, int level,
const char *fmt, va_list args)
{
int printed_len;
bool in_sched = false;
bool in_sched = false, pending_output;
unsigned long flags;
u64 curr_log_seq;

if (level == LOGLEVEL_SCHED) {
level = LOGLEVEL_DEFAULT;
Expand All @@ -1903,11 +1904,13 @@ asmlinkage int vprintk_emit(int facility, int level,

/* This stops the holder of console_sem just where we want him */
logbuf_lock_irqsave(flags);
curr_log_seq = log_next_seq;
printed_len = vprintk_store(facility, level, dict, dictlen, fmt, args);
pending_output = (curr_log_seq != log_next_seq);
logbuf_unlock_irqrestore(flags);

/* If called from the scheduler, we can not call up(). */
if (!in_sched) {
if (!in_sched && pending_output) {
/*
* Disable preemption to avoid being preempted while holding
* console_sem which would prevent anyone from printing to
Expand All @@ -1924,7 +1927,8 @@ asmlinkage int vprintk_emit(int facility, int level,
preempt_enable();
}

wake_up_klogd();
if (pending_output)
wake_up_klogd();
return printed_len;
}
EXPORT_SYMBOL(vprintk_emit);
Expand Down

0 comments on commit 3ac37a9

Please sign in to comment.