Skip to content

Commit 4f2a8d3

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
printk: Fix console_sem vs logbuf_lock unlock race
Fix up the fallout from commit 0b5e1c5 ("printk: Release console_sem after logbuf_lock"). The reason for unlocking the console_sem under the logbuf_lock is that a concurrent printk() might fill up the buffer but fail to acquire the console sem, resulting in a missed write to the console until a subsequent console_sem acquire/release cycle. Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: efault@gmx.de Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Andrew Morton <akpm@linux-foundation.org> Link: http://lkml.kernel.org/r/1308734409.1022.14.camel@twins Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 0b5e1c5 commit 4f2a8d3

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

‎kernel/printk.c‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ void console_unlock(void)
12441244
{
12451245
unsigned long flags;
12461246
unsigned _con_start, _log_end;
1247-
unsigned wake_klogd = 0;
1247+
unsigned wake_klogd = 0, retry = 0;
12481248

12491249
if (console_suspended) {
12501250
up(&console_sem);
@@ -1253,6 +1253,7 @@ void console_unlock(void)
12531253

12541254
console_may_schedule = 0;
12551255

1256+
again:
12561257
for ( ; ; ) {
12571258
spin_lock_irqsave(&logbuf_lock, flags);
12581259
wake_klogd |= log_start - log_end;
@@ -1273,8 +1274,23 @@ void console_unlock(void)
12731274
if (unlikely(exclusive_console))
12741275
exclusive_console = NULL;
12751276

1276-
spin_unlock_irqrestore(&logbuf_lock, flags);
1277+
spin_unlock(&logbuf_lock);
1278+
12771279
up(&console_sem);
1280+
1281+
/*
1282+
* Someone could have filled up the buffer again, so re-check if there's
1283+
* something to flush. In case we cannot trylock the console_sem again,
1284+
* there's a new owner and the console_unlock() from them will do the
1285+
* flush, no worries.
1286+
*/
1287+
spin_lock(&logbuf_lock);
1288+
if (con_start != log_end)
1289+
retry = 1;
1290+
spin_unlock_irqrestore(&logbuf_lock, flags);
1291+
if (retry && console_trylock())
1292+
goto again;
1293+
12781294
if (wake_klogd)
12791295
wake_up_klogd();
12801296
}

0 commit comments

Comments
 (0)